File Coverage

blib/lib/Apache/RandomImage.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Apache::RandomImage;
2              
3 1     1   30129 use strict;
  1         2  
  1         37  
4 1     1   6 use warnings;
  1         2  
  1         33  
5              
6 1     1   841 use DirHandle;
  1         2460  
  1         70  
7 1     1   440 use mod_perl;
  0            
  0            
8              
9             BEGIN {
10             my $MP2 = ( exists $ENV{MOD_PERL_API_VERSION} and
11             $ENV{MOD_PERL_API_VERSION} >= 2 );
12              
13             if (defined $MP2) {
14             require Apache2::RequestRec;
15             require Apache2::RequestUtil;
16             require Apache2::SubRequest;
17             require Apache2::Log;
18             require Apache2::Const;
19             Apache2::Const->import(qw(OK DECLINED NOT_FOUND));
20             }
21             else {
22             require Apache::Constants;
23             Apache::Constants->import(qw(OK DECLINED NOT_FOUND));
24             }
25             }
26              
27              
28             =head1 NAME
29              
30             Apache::RandomImage - Lightweight module to randomly display images from a directory.
31              
32             =head1 VERSION
33              
34             Version 0.3
35              
36             =cut
37              
38             # http://module-build.sourceforge.net/META-spec-current.html
39             # Does not like v0.3 versions :-/
40             #use version; our $VERSION = qv('0.3');
41             our $VERSION = '0.3';
42              
43             =head1 SYNOPSIS
44              
45             Configure this module as a response handler to activate this module. The following
46             examples will result in an image being randomly selected from the "images" directory.
47              
48             #mod_perl2 (PerlResponseHandler)
49            
50             SetHandler modperl
51             PerlSetVar Suffixes "gif png jpg"
52             PerlResponseHandler Apache::RandomImage
53            
54              
55             #mod_perl1 (PerlHandler)
56            
57             SetHandler perl-script
58             PerlSetVar Suffixes "gif png jpg tif jpeg"
59             PerlHandler Apache::RandomImage
60            
61              
62             =head1 DESCRIPTION
63              
64             Apache::RandomImage will randomly select an image from the dirname of the requested location.
65             You need to specify a white-space separated list of B with I,
66             otherwise the request will be declined.
67              
68             =head1 FUNCTIONS
69              
70             =head2 handler
71              
72             Apache response handler
73              
74             =cut
75             sub handler {
76             my $r = shift;
77             my $uri = $r->uri();
78             $uri =~ s|[^/]+$||x;
79              
80             my $dir = $r->document_root() . $uri;
81              
82             my $dh = DirHandle->new($dir);
83             if (not $dh) {
84             $r->log_error("Cannot open directory $dir: $!");
85             return NOT_FOUND;
86             }
87              
88             my @suffixes = split('\s+',$r->dir_config("Suffixes"));
89             return DECLINED unless scalar @suffixes;
90              
91             my @images;
92             foreach my $file ( $dh->read() ) {
93             next unless grep { $file =~ /\.$_$/xi } @suffixes;
94             push (@images, $file);
95             }
96              
97             return NOT_FOUND unless scalar @images;
98              
99             my $image = $images[rand @images];
100             $r->internal_redirect_handler("$uri/$image");
101              
102             return OK;
103             }
104              
105             =head1 Imported constants
106              
107             =head2 OK
108              
109             See Apache::Constants or Apache2::Const documentation
110              
111             =head2 DECLINED
112              
113             See Apache::Constants or Apache2::Const documentation
114              
115             =head2 NOT_FOUND
116              
117             See Apache::Constants or Apache2::Const documentation
118              
119             =head1 SEE ALSO
120              
121             =over 4
122              
123             =item L
124              
125             =item L
126              
127             =back
128              
129             =head1 AUTHOR
130              
131             Michael Kroell, C<< >>
132              
133             =head1 BUGS
134              
135             Please report any bugs or feature requests to C, or through
136             the web interface at L. I will be notified, and then you'll
137             automatically be notified of progress on your bug as I make changes.
138              
139              
140             =head1 SUPPORT
141              
142             You can find documentation for this module with the perldoc command.
143              
144             perldoc Apache::RandomImage
145              
146              
147             You can also look for information at:
148              
149             =over 4
150              
151             =item * RT: CPAN's request tracker
152              
153             L
154              
155             =item * AnnoCPAN: Annotated CPAN documentation
156              
157             L
158              
159             =item * CPAN Ratings
160              
161             L
162              
163             =item * Search CPAN
164              
165             L
166              
167             =back
168              
169              
170             =head1 ACKNOWLEDGEMENTS
171              
172             Apache::RandomImage was inspired by L
173              
174             =head1 COPYRIGHT
175              
176             Copyright 2003-2009 Michael Kroell, all rights reserved.
177              
178             =head1 LICENSE
179              
180             This program is free software; you can redistribute it and/or modify it
181             under the same terms as Perl itself.
182              
183              
184             =cut
185              
186             1; # End of Apache::RandomImage
187              
188