UploadSvr/Directory.pm untested

filter: Statementstmt 0.0% 0 / 198 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 320 CC 34 SCAR 67.4
CC
34
Cov
0%
dispatch
1190.0
new
2.0
handler
2.0
CRAP 843.5
1 package Apache::UploadSvr::Directory;
2  
3 use Apache::Constants qw(OK DECLINED AUTH_REQUIRED SERVER_ERROR);
4 use Apache::UploadSvr;
5 use CGI;
6 use Data::Dumper;
7 use DirHandle;
8 use File::Basename qw(basename dirname);
9 use File::Path;
10 use Image::Magick;
11 use IO::File;
12 use strict;
13 use vars qw( $VERSION @ISA );
14  
15 $VERSION = sprintf "%d.%03d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
16  
17 @ISA = qw(Apache::UploadSvr); # secure_transaction, dict
18  
19 sub new {
20 my($class,%arg) = @_;
21 bless {%arg}, $class;
22 }
23  
24 sub handler {
25 my $r = shift;
26 my $cgi = CGI->new;
27 # Directory not really suited well for the stage area
28 my $userclass = $r->dir_config("Apache_UploadSvr_Usermgr")
29 || "Apache::UploadSvr::User";
30 eval "require $userclass;";
31 no strict "refs";
32 my $self = __PACKAGE__->new( CGI => $cgi, R => $r );
33 $self->{USERREF} = $userclass->new($self);
34 $self->dispatch;
35 }
36  
37 sub dispatch {
38 my($self) = @_;
39 my $cgi = $self->{CGI};
40 my $r = $self->{R};
41 my(@m,$w,$cache,$has_changed);
42 my $DirCache = $r->dir_config("DirCache") || ".dirchache";
43 my $document_root = $r->document_root;
44 my $directory = dirname($r->filename);
45 my $stage = $r->dir_config("apache_stage_regex") ||
46 q{ ^ (/STAGE/[^/]*) (.*) $ };
47 return DECLINED if $r->uri =~ m| $stage |ox;
48 if (basename($directory) eq $DirCache) {
49 # warn "directory[$directory]DirCache[$DirCache]";
50 return DECLINED;
51 }
52 my $userref = $self->{USERREF};
53 # warn "userref[$userref]";
54 return AUTH_REQUIRED unless exists $userref->{user};
55  
56 my $dir_uri = substr($directory,length($document_root)) || "";
57 $dir_uri =~ s|/*$|/|;
58 my $write_perm = $self->has_perms($dir_uri);
59 # warn "write_perm[$write_perm]user[$userref->{user}]";
60 $write_perm++ if $userref->{user} eq "admin";
61 return DECLINED unless $write_perm;
62  
63 my $dh = DirHandle->new($directory);
64 my $expect_missings;
65 if (-f "$directory/$DirCache/$DirCache" && -r _) {
66 my $fh = IO::File->new;
67 if ($fh->open("$directory/$DirCache/$DirCache")) {
68 local $/;
69 eval <$fh>;
70 }
71 } else {
72 eval {mkpath "$directory/$DirCache";};
73 $expect_missings = 1 if $@;
74 }
75 my $att = $expect_missings ? " (not writeable by server)" : "";
76 my(@dirlisting,%dirlisting);
77  
78 push @m, qq{<HTML><HEAD><TITLE>Directory $dir_uri</TITLE></HEAD><BODY>};
79  
80 if ($cgi->param('delete')) {
81 my @delete = $cgi->param('delete');
82 my @todo;
83 warn "delete[@delete]";
84 for my $d (@delete) {
85 push @todo, "unpublish $dir_uri$d\n";
86 }
87 my $todo = join "", @todo;
88  
89 push @m, $self->secure_transaction($todo);
90 }
91  
92 push @m, qq{<H3>Contents of Directory: $dir_uri$att</H3><FORM METHOD=POST><TABLE BORDER=2 CELLPADDING=5 CELLSPACING=5>};
93  
94 my(@rows);
95 for my $dirent (sort $dh->read) {
96 next if $dirent eq ".";
97 next if $dirent eq $DirCache;
98 next if "/$dirent/" =~ m| $stage |ox; # don't show the STAGE
99 next unless -e "$directory/$dirent";
100 # warn "dirent[$dirent]";
101 $cache->{$dirent}{seen} = undef; # we check with exists only
102 push @dirlisting, $dirent;
103 my($display_as,$href,$size,$mtime,$localtime,$imgsize,$pic,$line);
104 #### STAT ####
105 stat "$directory/$dirent";
106 $mtime = (stat _)[9];
107 $display_as = $dirent; # speak for yourself
108 my $imq = Image::Magick->new;
109 if (ref($cache) && $cache->{$dirent}{mtime} == $mtime) {
110 $line = $cache->{$dirent}{line};
111 # I believe, we do not need to check if the thumbnail's still there
112 } else {
113 if (-d _) {
114 $href = "$dirent/...";
115 $size = "-";
116 $localtime = "-";
117 $imgsize = qq{<A HREF="$href"><IMG BORDER=0 SRC="/icons/dir.gif"
118 WIDTH=20 HEIGHT=22></A>};
119 $pic = "";
120 $display_as = "Parent Directory" if $dirent eq "..";
121 } else {
122 $href = $dirent;
123 $size = -s _;
124 #### EOSTAT ####
125 $localtime = localtime($mtime);
126 $localtime =~ s/ (\d) / 0$1 /;
127 my($imgx, $imgy, $im_size, $imgtype);
128 ($imgx, $imgy, $im_size, $imgtype) =
129 split(',', $imq->Ping("$directory/$dirent"))
130 if $dirent =~ /(?:GIF|JPE?G|XBM|PNG|BMP)$/i;
131 # warn "dirent[$dirent]imgx[$imgx]imgtype[$imgtype]";
132 $imgsize = $imgtype =~ /^(?:GIF.*|JPE?G|XBM|PNG|BMP)$/ ?
133 qq{$imgx x $imgy} : "";
134 if ($imgsize) {
135 my($scalex,$scaley,$scalemax,$thsrc);
136 if ($expect_missings) {
137 $thsrc = "/icons/unknown.gif";
138 $scalex = 20;
139 $scaley = 22;
140 } else {
141 $thsrc = "$DirCache/$dirent";
142 #### STAT ####
143 stat "$directory/$thsrc";
144 my $thmtime = (stat _)[9];
145 my $thstatsize = (stat _)[7];
146 #### EOSTAT ####
147 unless ($thmtime > $mtime && $thstatsize) {
148 $scalemax = 31;
149 if ($imgx > $scalemax && $imgx > $imgy) {
150 $scalex = $scalemax;
151 $scaley = int($imgy*$scalemax/$imgx+.5) || 1;
152 } elsif ($imgy > $scalemax) {
153 $scaley = $scalemax;
154 $scalex = int($imgx*$scalemax/$imgy+.5) || 1;
155 } else {
156 $scalex = $imgx;
157 $scaley = $imgy;
158 }
159 my $incode = "$directory/$dirent";
160 if (-r $incode){
161 my $orig = $imq->Read($incode);
162 $imq->Sample(width=>$scalex,height=>$scaley);
163 my $err = $imq->Write(filename=>"$directory/$thsrc");
164 if ($err) {
165 warn "Could not write [$err]: $!";
166 $thsrc = "/icons/unknown.gif";
167 $scalex = 20;
168 $scaley = 22;
169 }
170 } else {
171 warn "Could not open input [$incode]: $!";
172 return SERVER_ERROR;
173 }
174 }
175 }
176 $pic = qq{<A HREF="$href"><IMG SRC="$thsrc" BORDER=0 WIDTH="$scalex" HEIGHT="$scaley"></A>};
177 }
178 }
179 $has_changed=1;
180 $line = [
181 qq{<TD>$pic</TD>},
182 qq{<TD>$imgsize</TD>},
183 qq{<TD><A HREF="$href">$display_as</A></TD>},
184 $size eq "-" ?
185 "<TD></TD>" :
186 "checkbox",
187 qq{<TD ALIGN=CENTER>$size</TD>},
188 qq{<TD ALIGN=CENTER>$localtime</TD>}
189 ];
190  
191 $cache->{$dirent}{mtime} = $mtime;
192 $cache->{$dirent}{line} = $line;
193 }
194 push @rows, $line;
195 }
196 $dh->close;
197 foreach my $olddirent (keys %$cache) {
198 if (exists $cache->{$olddirent}{seen}) {
199 delete $cache->{$olddirent}{seen};
200 } else {
201 delete $cache->{$olddirent};
202 $has_changed = 1;
203 }
204 }
205 if ($has_changed) {
206 my $fh = IO::File->new;
207 if ($fh->open(">$directory/$DirCache/$DirCache")) {
208 $fh->print(Data::Dumper->new([$cache],["cache"])->Dump);
209 } else {
210 warn "Could not write >$directory/$DirCache/$DirCache: $!";
211 }
212 }
213 my(@chkbox,$columns,$red);
214 if ($write_perm) {
215 $columns = 6;
216 } else {
217 $columns = 5;
218 }
219 if ($write_perm) {
220 @dirlisting{@dirlisting} = (" ") x @dirlisting;
221 @chkbox = split /<BR>/, $cgi->checkbox_group(-name => "delete",
222 'values' => \@dirlisting,
223 'linebreak'=>'true',
224 labels => \%dirlisting
225 );
226 $red = "#fa8888";
227 push @m, "<TR><TD colspan=3></TD>";
228 push @m, qq{<TD bgcolor="$red">};
229 push @m, $cgi->submit(-name => 'Delete');
230 push @m, qq{</TR>};
231 for my $e (0..$#rows) {
232 my $l = $rows[$e];
233 $l->[3] = qq{<TD ALIGN=CENTER BGCOLOR=$red>$chkbox[$e]</TD>} if
234 "$l->[3]" eq "checkbox";
235 }
236 } else {
237 for my $e (0..$#rows) {
238 my $l = $rows[$e];
239 splice @$l, 3, 1;
240 }
241 }
242  
243 for my $e (0..$#rows) {
244 my $l = $rows[$e];
245 push @m, "<TR>";
246 for my $c (@$l) {
247 push @m, $c;
248 }
249 push @m, "</TR>";
250 }
251  
252 push @m, "</FORM></TABLE></BODY></HTML>";
253  
254 $r->content_type("text/html");
255 $r->send_http_header;
256  
257 print @m;
258 OK;
259 }
260  
261 1;