| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | package Apache::UploadSvr; | ||
| 4 | |||
| 5 | use Apache::Constants qw(DECLINED OK SERVER_ERROR); | ||
| 6 | use CGI; | ||
| 7 | warn sprintf "%s", $INC{"CGI.pm"}; | ||
| 8 | use Cwd; | ||
| 9 | use DirHandle; | ||
| 10 | use ExtUtils::Manifest; | ||
| 11 | use File::Basename qw(basename dirname); | ||
| 12 | use File::Find; | ||
| 13 | use File::Path (); | ||
| 14 | use HTML::FormatText (); | ||
| 15 | use HTML::Parse 'parse_html'; | ||
| 16 | use HTTP::Date; | ||
| 17 | use IO::File; | ||
| 18 | use Mail::Send; | ||
| 19 | use URI::URL; | ||
| 20 | use Apache::UploadSvr::Dictionary; | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use vars qw( @Legalactions %Legalactions $VERSION); | ||
| 24 | |||
| 25 | $VERSION = sprintf "%d.%03d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/; | ||
| 26 | |||
| 27 | @Legalactions = qw/publish unzip lowercase linkcheck delete/; | ||
| 28 | %Legalactions = ( | ||
| 29 | 'delete' => "D002", | ||
| 30 | 'publish' => "D003", | ||
| 31 | 'unzip' => "D004", | ||
| 32 | 'lowercase' => "D005", | ||
| 33 | 'linkcheck' => "D006", | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub time { | ||
| 37 | my($self) = @_; | ||
| 38 | $self->{TIME} ||= sprintf "%010d", time; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub new { | ||
| 42 | my($class,%arg) = @_; | ||
| 43 | bless {%arg}, $class; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub dict { | ||
| 47 | my($self,$code,@arg) = @_; | ||
| 48 | my $lang = $self->language; | ||
| 49 | Apache::UploadSvr::Dictionary->fetch($lang,$code,@arg); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub language { | ||
| 53 | my($self) = @_; | ||
| 54 | return $self->{LANGUAGE} if exists $self->{LANGUAGE}; | ||
| 55 | |||
| 56 | my(%lang_wanted,$lang_match); | ||
| 57 | my($default_weight) = 1; | ||
| 58 | foreach my $e ( split /,/, $self->{R}->header_in('Accept-Language') ) { # de-DE,en,de,ja,hr | ||
| 59 | my($l,$v) = $e =~ / ^ \s* ([\w\-]+) \s* (.*) /x; | ||
| 60 | my($q) = $v =~ / ^ ; \s* q \s* = \s* ([\d\.]+) /x; | ||
| 61 | $q ||= $default_weight; | ||
| 62 | $default_weight -= 0.0001; | ||
| 63 | $lang_wanted{$l} = $q; | ||
| 64 | } | ||
| 65 | for my $l (sort {$lang_wanted{$b} <=> $lang_wanted{$a}} | ||
| 66 | keys %lang_wanted){ | ||
| 67 | if (Apache::UploadSvr::Dictionary->exists($l)) { | ||
| 68 | $lang_match = $l; | ||
| 69 | } elsif (Apache::UploadSvr::Dictionary->exists(substr($l,0,2))) { | ||
| 70 | $lang_match = substr($l,0,2); | ||
| 71 | } else { | ||
| 72 | next; | ||
| 73 | } | ||
| 74 | last; | ||
| 75 | } | ||
| 76 | $lang_match ||= "de"; | ||
| 77 | $self->{LANGUAGE} = $lang_match; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub handler { | ||
| 81 | my($r) = @_; | ||
| 82 | # local($/) = "\n"; | ||
| 83 | my $cgi = CGI->new; | ||
| 84 | my $self = __PACKAGE__->new( | ||
| 85 | CGI => $cgi, | ||
| 86 | R => $r, | ||
| 87 | USERCLASS => $r->dir_config("Apache_UploadSvr_Usermgr") || "Apache::UploadSvr::User" | ||
| 88 | ); | ||
| 89 | $self->dispatch; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub as_string { | ||
| 93 | my($self) = @_; | ||
| 94 | require Data::Dumper; | ||
| 95 | join "", "<PRE>", Data::Dumper::Dumper($self), "</PRE>\n"; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub dispatch { | ||
| 99 | my($self) = @_; | ||
| 100 | my $r = $self->{R}; | ||
| 101 | my $cgi = $self->{CGI}; | ||
| 102 | |||
| 103 | my($filename, $content,$stagedir,$stageuri,$what_we_did, | ||
| 104 | $sectrans,$transdir); | ||
| 105 | |||
| 106 | my $document_root = $self->document_root; | ||
| 107 | my $time = $self->time; | ||
| 108 | eval "require $self->{USERCLASS};"; # there are more efficient ways... | ||
| 109 | no strict "refs"; | ||
| 110 | my $userref = $self->{USERCLASS}->new($self); | ||
| 111 | $self->{USERREF} = $userref; | ||
| 112 | return SERVER_ERROR unless exists $userref->{user}; | ||
| 113 | |||
| 114 | $stageuri = $r->dir_config("stageuri") || "/STAGE"; | ||
| 115 | $stagedir = "$document_root$stageuri"; | ||
| 116 | $stageuri .= "/$userref->{user}"; | ||
| 117 | $stagedir .= "/$userref->{user}"; | ||
| 118 | $self->{STAGEURI} = $stageuri; | ||
| 119 | $self->{STAGEDIR} = $stagedir; | ||
| 120 | |||
| 121 | $userref->{permitted} ||= []; | ||
| 122 | |||
| 123 | if ($r->method eq "PUT") { | ||
| 124 | my $uri = $r->uri; | ||
| 125 | # $uri =~ s|/STAGE||; | ||
| 126 | # I have never tried if PUT works this way or not. Code is just a | ||
| 127 | # placeholder | ||
| 128 | $r->read($content, $r->header_in("Content-length")); | ||
| 129 | $filename = $stagedir . $uri; | ||
| 130 | } elsif ($r->method eq "POST" or $r->method eq "GET") { | ||
| 131 | if ( $cgi->param('SUBMITup')) { | ||
| 132 | my($handle,$targetdir); | ||
| 133 | if ($handle = $cgi->param('HTTPUPLOAD')) { | ||
| 134 | $targetdir = $cgi->param('TARGETDIR') || "/"; | ||
| 135 | no strict; | ||
| 136 | local $/; | ||
| 137 | $content = <$handle>; | ||
| 138 | close $handle; | ||
| 139 | $handle =~ s(.*\/)(); # no slash | ||
| 140 | $handle =~ s(.*\\)(); # no backslash | ||
| 141 | $handle =~ s(.*:)(); # no colon | ||
| 142 | $filename = "$stagedir$targetdir/$handle"; | ||
| 143 | # warn "filename[$filename]"; | ||
| 144 | $filename =~ s|/+|/|g; | ||
| 145 | } else { | ||
| 146 | $what_we_did = $self->dict('D007'); | ||
| 147 | } | ||
| 148 | } elsif ($cgi->param('SUBMITaction')) { | ||
| 149 | my $action = $cgi->param('ACTION'); | ||
| 150 | if ($action) { | ||
| 151 | $what_we_did = ""; | ||
| 152 | my(@files,$allflag); | ||
| 153 | my(@stagedfiles) = $cgi->param('STAGEDFILES'); | ||
| 154 | warn "stagedfiles[@stagedfiles]"; | ||
| 155 | for my $f (@stagedfiles) { | ||
| 156 | if ($f eq " ALL") { | ||
| 157 | $allflag++; | ||
| 158 | last; | ||
| 159 | } | ||
| 160 | push @files, $f; | ||
| 161 | } | ||
| 162 | if ($allflag) { | ||
| 163 | chdir $stagedir; | ||
| 164 | my $manifind = ExtUtils::Manifest::manifind(); | ||
| 165 | @files = map { "/$_" } keys %$manifind; | ||
| 166 | } | ||
| 167 | if (@files) { | ||
| 168 | for my $f (@files) { | ||
| 169 | if (index($f,"../")>=0) { | ||
| 170 | $what_we_did .= $self->dict("D008",$f); | ||
| 171 | next; | ||
| 172 | } | ||
| 173 | unless (-f "$stagedir$f"){ | ||
| 174 | $what_we_did .= $self->dict("D009",$f); | ||
| 175 | next; | ||
| 176 | } | ||
| 177 | if ($action eq "publish") { | ||
| 178 | my($done,$error) = $self->publish($f); | ||
| 179 | if ($done) { | ||
| 180 | $sectrans .= $done; | ||
| 181 | } else { | ||
| 182 | $what_we_did .= $error; | ||
| 183 | } | ||
| 184 | } elsif ($self->can($action)) { | ||
| 185 | if (exists $Legalactions{$action}) { | ||
| 186 | $what_we_did .= $self->$action($f,\@files); | ||
| 187 | } else { | ||
| 188 | $what_we_did .= $self->dict("D010",$action); | ||
| 189 | } | ||
| 190 | } else { | ||
| 191 | $what_we_did .= $self->dict("D058",$action); | ||
| 192 | } | ||
| 193 | } # for @files | ||
| 194 | $what_we_did = $self->secure_transaction($sectrans) if $sectrans; | ||
| 195 | } else { # no files | ||
| 196 | $what_we_did = $self->dict("D011",$action); | ||
| 197 | } | ||
| 198 | } else { # no action | ||
| 199 | $what_we_did = $self->dict("D012",$self->dict("D013")); | ||
| 200 | } | ||
| 201 | } elsif ($cgi->param("SUBMITtrans")) { | ||
| 202 | $what_we_did = $self->transhandler(); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | # warn "filename[$filename] content[$content]"; | ||
| 207 | if ($filename && $content) { | ||
| 208 | my $tdir = dirname($filename); | ||
| 209 | File::Path::mkpath($tdir); | ||
| 210 | my $fh; | ||
| 211 | $fh = IO::File->new or die "Could not open new filehandle: $!"; | ||
| 212 | unless ($fh->open(">$filename")) { | ||
| 213 | $r->log_error("Couldn't open >$filename: $!"); | ||
| 214 | return SERVER_ERROR; | ||
| 215 | } | ||
| 216 | $fh->print($content); | ||
| 217 | $fh->close; | ||
| 218 | # $filename =~ s|.*?/STAGE/[^/]+(.*)|/STAGE$1|; | ||
| 219 | my $basename = basename($filename); | ||
| 220 | my $dirname = substr(dirname($filename),length($document_root)); | ||
| 221 | $what_we_did = $self->dict("D014",$dirname,$basename,$basename,$dirname); | ||
| 222 | } | ||
| 223 | |||
| 224 | my($file_listing,$manifind); | ||
| 225 | if ( | ||
| 226 | chdir $stagedir and | ||
| 227 | $manifind = ExtUtils::Manifest::manifind() and | ||
| 228 | %$manifind | ||
| 229 | ) { | ||
| 230 | my(%legalactions); | ||
| 231 | while (my($k,$v) = each %Legalactions) { | ||
| 232 | $legalactions{$k} = $self->dict($v); | ||
| 233 | } | ||
| 234 | my $actions = $cgi-> | ||
| 235 | scrolling_list( | ||
| 236 | -name => 'ACTION', | ||
| 237 | 'values' => [@Legalactions], | ||
| 238 | default => [], | ||
| 239 | size => 5, | ||
| 240 | labels => \%legalactions, | ||
| 241 | ); | ||
| 242 | |||
| 243 | my $submit = $cgi->submit( | ||
| 244 | -name=>'SUBMITaction', | ||
| 245 | value=> $self->dict("D013") | ||
| 246 | ); | ||
| 247 | |||
| 248 | my(@m); | ||
| 249 | push @m, $self->dict("D015", $self->dict("D013")), $actions, $submit; | ||
| 250 | push @m, qq{<BR> <TABLE BORDER=1 CELLPADDING=8 }; | ||
| 251 | push @m, qq{CELLSPACING=2> <TR VALIGN=BOTTOM> <TH>}; | ||
| 252 | push @m, $self->dict("D016"); | ||
| 253 | push @m, qq{</TH><TH>}; | ||
| 254 | push @m, $self->dict("D017"); | ||
| 255 | push @m, qq{</TH> <TH> </TH> <TH>}; | ||
| 256 | push @m, $self->dict("D018"); | ||
| 257 | push @m, qq{</TH><TH>}; | ||
| 258 | push @m, $self->dict("D019"); | ||
| 259 | push @m, qq{</TH> </TR>}; | ||
| 260 | |||
| 261 | my(@rows); | ||
| 262 | for my $f (sort keys %$manifind) { | ||
| 263 | my @stat = stat $f; | ||
| 264 | push @rows, [ | ||
| 265 | qq{/$f}, | ||
| 266 | qq{$userref->{user}}, | ||
| 267 | qq{$stat[7]}, | ||
| 268 | time2str($stat[9]), | ||
| 269 | ]; | ||
| 270 | } | ||
| 271 | unshift @rows, [" ALL"]; | ||
| 272 | my(%dirlisting,@dirlisting); | ||
| 273 | @dirlisting = map {$_->[0]} @rows; | ||
| 274 | @dirlisting{@dirlisting} = (" ") x @dirlisting; | ||
| 275 | my(@chkbox) = split /<BR>/, $cgi->checkbox_group( | ||
| 276 | -name => 'STAGEDFILES', | ||
| 277 | 'values' => \@dirlisting, | ||
| 278 | linebreak=>'true', | ||
| 279 | labels=> \%dirlisting | ||
| 280 | ); | ||
| 281 | |||
| 282 | for my $e (0..$#rows) { | ||
| 283 | push @m, "<TR>"; | ||
| 284 | my @l = @{$rows[$e]}; | ||
| 285 | if ($l[0] eq " ALL") { | ||
| 286 | push @m, sprintf( | ||
| 287 | "<TD ALIGN=RIGHT>%s</TD><TD COLSPAN=4>%s</TD>", | ||
| 288 | $chkbox[$e], | ||
| 289 | "perform above selected action on all files below" | ||
| 290 | ); | ||
| 291 | } else { | ||
| 292 | push @m, sprintf( | ||
| 293 | qq{<TD ALIGN=RIGHT>%s</TD><TD> %s </TD><TD><A HREF="/STAGE/%s%s">view</A></TD><TD ALIGN=RIGHT> %d </TD><TD> %s </TD>}, | ||
| 294 | $chkbox[$e], | ||
| 295 | @l[0,1,0,2,3] | ||
| 296 | ); | ||
| 297 | } | ||
| 298 | push @m, qq{</TR>}; | ||
| 299 | } | ||
| 300 | push @m, "</TABLE>"; | ||
| 301 | |||
| 302 | $file_listing = join "", @m; | ||
| 303 | } else { | ||
| 304 | $file_listing = $self->dict("D020"); | ||
| 305 | } | ||
| 306 | |||
| 307 | File::Find::finddepth( sub { | ||
| 308 | return unless -d $_; | ||
| 309 | rmdir $_; # may fail | ||
| 310 | }, $stagedir); | ||
| 311 | |||
| 312 | |||
| 313 | $r->content_type("text/html"); | ||
| 314 | $r->send_http_header; | ||
| 315 | |||
| 316 | my(@m); | ||
| 317 | push @m, $cgi->start_html( | ||
| 318 | -title => $self->dict("D021"), | ||
| 319 | author => 'andreas.koenig@kulturbox.de' | ||
| 320 | ); | ||
| 321 | if ($time - $userref->{lastlogin} > 7200) { | ||
| 322 | push @m, $self->hello; | ||
| 323 | } | ||
| 324 | $userref->{lastlogin} = $time; | ||
| 325 | if ($what_we_did) { | ||
| 326 | unless ( $what_we_did =~ /^<TABLE/ ) { | ||
| 327 | $what_we_did =~ s/^/<TABLE BORDER=2><TR><TD>/; | ||
| 328 | $what_we_did =~ s|$|</TD></TR></TABLE>|; | ||
| 329 | } | ||
| 330 | } else { | ||
| 331 | $what_we_did = "<HR>"; | ||
| 332 | } | ||
| 333 | push @m, $what_we_did; | ||
| 334 | push @m, $self->upload_form; | ||
| 335 | push @m, qq{<HR><H4>Delete, Unzip, Publish, etc.</H4>}; | ||
| 336 | push @m, $file_listing; | ||
| 337 | push @m, "<HR>"; | ||
| 338 | # push @m, $self->as_string; | ||
| 339 | push @m, $cgi->endform; | ||
| 340 | push @m, $cgi->end_html; | ||
| 341 | $cgi->print(@m); | ||
| 342 | } | ||
| 343 | |||
| 344 | sub secure_transaction { | ||
| 345 | my($self,$sectrans) = @_; | ||
| 346 | my $r = $self->{R}; # attn: Directory.pm calls this with a different $self | ||
| 347 | my($userref) = $self->{USERREF}; | ||
| 348 | |||
| 349 | my($what_we_did); | ||
| 350 | my $script_name = $r->dir_config("Apache_UploadSvr_myuri"); | ||
| 351 | unless ($script_name) { | ||
| 352 | # script_name may be different from this script_name | ||
| 353 | $script_name = $r->path_info ? | ||
| 354 | substr($r->uri, 0, length($r->uri)-length($r->path_info)) : | ||
| 355 | $r->uri; | ||
| 356 | } | ||
| 357 | my $me_url = URI::URL->new( | ||
| 358 | "http://" . | ||
| 359 | $r->server->server_hostname . | ||
| 360 | $script_name)->as_string; | ||
| 361 | my $secret = 100000 + int rand 900000; | ||
| 362 | my $secretfile; | ||
| 363 | my $transdir = $r->dir_config('Apache_UploadSvr_transdir'); | ||
| 364 | unless ($transdir) { | ||
| 365 | $r->log_error("No Apache_UploadSvr_transdir specified. Setting to /tmp"); | ||
| 366 | $transdir = "/tmp"; | ||
| 367 | } | ||
| 368 | $secret++ while -f ($secretfile = "$transdir/$userref->{user}$secret"); | ||
| 369 | my $fh = IO::File->new(">$secretfile") or | ||
| 370 | die "Couldn't create secretfile $secretfile: $!"; | ||
| 371 | my $plural = split(/\n/, $sectrans) > 1; | ||
| 372 | my $mailtext = join("\n", | ||
| 373 | $self->dict($plural ? "D025" : "D024"), | ||
| 374 | $sectrans, | ||
| 375 | $self->dict("D026"), | ||
| 376 | " $me_url?SUBMITtrans=$secret", | ||
| 377 | ); | ||
| 378 | $fh->print($sectrans); | ||
| 379 | $fh->close; | ||
| 380 | my($msg) = Mail::Send | ||
| 381 | ->new( | ||
| 382 | "Subject" => $self->dict("D027"), | ||
| 383 | "To" => qq{"$userref->{fullname}" <$userref->{email}>} | ||
| 384 | ); | ||
| 385 | my $from = $r->dir_config("Apache_UploadSvr_from"); | ||
| 386 | $msg->add("From",$from) if $from; | ||
| 387 | my $sendh = $msg->open or $r->log_error("Could not open sendmail"); | ||
| 388 | $sendh->print($mailtext); | ||
| 389 | $what_we_did = <<EOS; | ||
| 390 | <TABLE BORDER=2 CELLPADDING=5><TR><TD BGCOLOR="#ff8888"> | ||
| 391 | EOS | ||
| 392 | |||
| 393 | if ($sendh->close) { | ||
| 394 | warn "Uploader sent mail to $userref->{email} and closed successfully"; | ||
| 395 | $what_we_did .= $self->dict("D028", $userref->{email}); | ||
| 396 | } else { | ||
| 397 | warn "Uploader tried to send mail, but...: $!"; | ||
| 398 | $what_we_did .= $self->dict("D029", $!); | ||
| 399 | } | ||
| 400 | $what_we_did .= qq{</TD></TR></TABLE>}; | ||
| 401 | $what_we_did; | ||
| 402 | } | ||
| 403 | |||
| 404 | sub upload_form { | ||
| 405 | my($self) = @_; | ||
| 406 | my $cgi = $self->{CGI}; | ||
| 407 | my $r = $self->{R}; | ||
| 408 | my($userref) = $self->{USERREF}; | ||
| 409 | |||
| 410 | my(@m); | ||
| 411 | push @m, $cgi->start_multipart_form(-action => $r->uri); | ||
| 412 | push @m, "<H4>Upload</H4>\n"; | ||
| 413 | push @m, $self->dict("D030"); | ||
| 414 | push @m, "<BR>"; | ||
| 415 | push @m, $cgi->hidden('HIDDEN1',"VALUE1"); | ||
| 416 | push @m, $cgi->filefield(-name => "HTTPUPLOAD", size => 48); | ||
| 417 | push @m, $cgi->hidden('HIDDEN2',"VALUE2"); | ||
| 418 | push @m, qq{<BR>\n}; | ||
| 419 | push @m, $self->dict("D031"); | ||
| 420 | push @m, join(", ", @{$userref->{permitted} || []}); | ||
| 421 | push @m, qq{ \)<BR>\n}; | ||
| 422 | push @m, $cgi->textfield( | ||
| 423 | -name => "TARGETDIR", | ||
| 424 | size => 63, | ||
| 425 | maxlength => 63, | ||
| 426 | 'default' => $userref->{permitted}[0], | ||
| 427 | ); | ||
| 428 | push @m, "\n<BR>"; | ||
| 429 | push @m, $cgi->submit( | ||
| 430 | -name=>'SUBMITup', | ||
| 431 | value=> $self->dict("D032") | ||
| 432 | ); | ||
| 433 | join "", @m; | ||
| 434 | } | ||
| 435 | |||
| 436 | sub hello { | ||
| 437 | my($self) = @_; | ||
| 438 | my($cgi) = $self->{CGI}; | ||
| 439 | my($userref) = $self->{USERREF}; | ||
| 440 | my(@m); | ||
| 441 | my @hello = ("D033".."D037"); | ||
| 442 | my $time = $self->time; | ||
| 443 | my @time = localtime($time); | ||
| 444 | my $daytime = $time[2]; | ||
| 445 | if ($daytime >= 7 && $daytime < 10) { | ||
| 446 | push @hello, "D038"; | ||
| 447 | } elsif ($daytime >= 19 || $daytime < 1) { | ||
| 448 | push @hello, "D039"; | ||
| 449 | } elsif ($daytime >= 10) { | ||
| 450 | push @hello, "D040"; | ||
| 451 | if ($daytime >=11 && $daytime < 14) { | ||
| 452 | push @hello, "D042"; | ||
| 453 | } | ||
| 454 | } else { | ||
| 455 | push @hello, "D043"; | ||
| 456 | } | ||
| 457 | |||
| 458 | my $hello = $self->dict($hello[rand @hello]); | ||
| 459 | |||
| 460 | my(@m); | ||
| 461 | push @m, qq{<H3>$hello $userref->{salut} $userref->{lastname},</H3>}; | ||
| 462 | push @m, $self->dict('D001'); | ||
| 463 | push @m, $self->dict("D044"); | ||
| 464 | push @m, qq{<P>\n}; | ||
| 465 | join "", @m; | ||
| 466 | } | ||
| 467 | |||
| 468 | sub document_root { | ||
| 469 | my($self) = @_; | ||
| 470 | return $self->{DOCUMENT_ROOT} if exists $self->{DOCUMENT_ROOT}; | ||
| 471 | my $document_root = $self->{R}->document_root; | ||
| 472 | $document_root =~ s|/+$||; # trailing slashes disturb processing here | ||
| 473 | $self->{DOCUMENT_ROOT} = $document_root; | ||
| 474 | } | ||
| 475 | |||
| 476 | sub request { shift->{R} } | ||
| 477 | |||
| 478 | sub transhandler { | ||
| 479 | my($self) = @_; | ||
| 480 | my($userref) = $self->{USERREF}; | ||
| 481 | my($what_we_did); | ||
| 482 | my($cgi) = $self->{CGI}; | ||
| 483 | my($r) = $self->{R}; | ||
| 484 | my $stagedir = $self->{STAGEDIR}; | ||
| 485 | my $secret = $cgi->param("SUBMITtrans"); | ||
| 486 | my $transdir = $r->dir_config('Apache_UploadSvr_transdir'); | ||
| 487 | unless ($transdir) { | ||
| 488 | $r->log_error("No Apache_UploadSvr_transdir specified. Setting to /tmp"); | ||
| 489 | $transdir = "/tmp"; | ||
| 490 | } | ||
| 491 | my $trashdir = $r->dir_config('Apache_UploadSvr_trashdir'); | ||
| 492 | unless ($trashdir) { | ||
| 493 | $r->log_error("No Apache_UploadSvr_trashdir specified. Setting to /tmp"); | ||
| 494 | $trashdir = "/tmp"; | ||
| 495 | } | ||
| 496 | File::Path::mkpath($trashdir); | ||
| 497 | my $dh = DirHandle->new($trashdir) or die; | ||
| 498 | my $time = $self->time; | ||
| 499 | for my $d ($dh->read) { | ||
| 500 | my $old = "$trashdir/$d"; | ||
| 501 | stat $old; | ||
| 502 | next unless -f _; | ||
| 503 | next unless (stat _)[9] < $time - 7 * 86400; | ||
| 504 | unlink $old; | ||
| 505 | } | ||
| 506 | my $dh = DirHandle->new($transdir) or die | ||
| 507 | "Couldn't opendir $transdir directory: $!"; | ||
| 508 | for my $dirent ($dh->read) { | ||
| 509 | my $file = "$transdir/$dirent"; | ||
| 510 | stat $file; | ||
| 511 | if (-f _ && (-M _ > 3)) { | ||
| 512 | unlink $file; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | my $efile = "$transdir/$userref->{user}$secret"; | ||
| 516 | my $document_root = $self->document_root; | ||
| 517 | if (-r $efile) { | ||
| 518 | my $fh = IO::File->new($efile) or die "Couldn't open $efile: $!"; | ||
| 519 | my($doit,@done); | ||
| 520 | while ( defined($doit = <$fh>) ) { | ||
| 521 | chomp $doit; | ||
| 522 | my($command,@args) = split " ", $doit; | ||
| 523 | if ($command =~ /^\s*\#/) { | ||
| 524 | next; | ||
| 525 | } | ||
| 526 | if ($command eq "publish") { | ||
| 527 | my $f = $args[0]; | ||
| 528 | my($targetdir,$absfile,$targetfile); | ||
| 529 | $targetfile = "$document_root$f"; | ||
| 530 | $targetdir = dirname($targetfile); | ||
| 531 | $absfile = "$stagedir$f"; | ||
| 532 | eval { | ||
| 533 | File::Path::mkpath($targetdir); | ||
| 534 | rename($absfile, $targetfile) or die $!; | ||
| 535 | }; | ||
| 536 | push @done, $@ ? $self->dict("D022",$doit,$@) | ||
| 537 | : $self->dict("D023",$f,$f); | ||
| 538 | $r->log_error("doit[$doit]ERR[$@]targetfile[$targetfile] targetdir[$targetdir] absfile[$absfile]"); | ||
| 539 | } elsif ($command eq "unpublish") { | ||
| 540 | my $f = $args[0]; | ||
| 541 | my($rmfile, $trashfile); | ||
| 542 | $rmfile = "$document_root$f"; | ||
| 543 | $trashfile = $trashdir . "/" . basename($f); | ||
| 544 | if ($self->has_perms($f)) { | ||
| 545 | if (-f $rmfile) { | ||
| 546 | if (rename $rmfile, $trashfile) { | ||
| 547 | push @done, qq{<B>unpublish</B> $f<BR>}; | ||
| 548 | my $rmf = $rmfile; | ||
| 549 | while () { | ||
| 550 | my $rmd = File::Basename::dirname($rmf); | ||
| 551 | my $d = File::Basename::dirname($f); | ||
| 552 | my $dh = DirHandle->new($rmd) or die "Couldn't diropen $d: $!"; | ||
| 553 | my @dirent = $dh->read; | ||
| 554 | if (@dirent == 3 && -d "$rmd/.dircache") { | ||
| 555 | File::Path::rmtree("$rmd/.dircache"); | ||
| 556 | pop @dirent; | ||
| 557 | } | ||
| 558 | if (@dirent == 2) { # empty directory | ||
| 559 | if ( rmdir $rmd ) { | ||
| 560 | push @done, qq{ }; | ||
| 561 | push @done, $self->dict("D045",$d); | ||
| 562 | $f = $d; | ||
| 563 | $rmf = $rmd; | ||
| 564 | } else { | ||
| 565 | last; | ||
| 566 | } | ||
| 567 | } else { | ||
| 568 | last; | ||
| 569 | } | ||
| 570 | } | ||
| 571 | } else { | ||
| 572 | push @done, $self->dict("D022",$doit,$!); | ||
| 573 | } | ||
| 574 | } else { | ||
| 575 | push @done, $self->dict("D046",$doit); | ||
| 576 | $r->log_error("DEBUG: rmfile[$rmfile]"); | ||
| 577 | } | ||
| 578 | } else { | ||
| 579 | push @done, $self->dict("D047",$doit); | ||
| 580 | $r->log_error(qq{DEBUG:rmfile[$rmfile]trashfile[$trashfile]user[$userref->{user}]}); | ||
| 581 | } | ||
| 582 | } | ||
| 583 | } | ||
| 584 | $fh->close; | ||
| 585 | unlink $efile or die "Couldn't unlink $efile"; | ||
| 586 | $what_we_did = join("\n", | ||
| 587 | $self->dict("D048",$secret), | ||
| 588 | @done | ||
| 589 | ); | ||
| 590 | } else { | ||
| 591 | $what_we_did = $self->dict("D049", $secret); | ||
| 592 | } | ||
| 593 | warn scalar(localtime) . $what_we_did; | ||
| 594 | $what_we_did; | ||
| 595 | } | ||
| 596 | |||
| 597 | sub has_perms { | ||
| 598 | my($self,$f) = @_; | ||
| 599 | # warn "has_perms f[$f]"; | ||
| 600 | my $userref = $self->{USERREF}; | ||
| 601 | $userref->has_perms($f); | ||
| 602 | } | ||
| 603 | |||
| 604 | sub unzip { | ||
| 605 | my($self,$f) = @_; | ||
| 606 | my $stagedir = $self->{STAGEDIR}; | ||
| 607 | my $absfile = "$stagedir$f"; | ||
| 608 | my $done; | ||
| 609 | my $fromdir = dirname($absfile); | ||
| 610 | my $fromfile = basename($f); | ||
| 611 | chdir $fromdir; | ||
| 612 | my $system; | ||
| 613 | if ($fromfile =~ /\.t(ar\.)?gz$/i){ | ||
| 614 | $system="tar xvzf $fromfile"; | ||
| 615 | } elsif ($fromfile =~ /\.zip$/i){ | ||
| 616 | $system="unzip -a $fromfile"; | ||
| 617 | } elsif ($fromfile =~ /\.gz$/i) { | ||
| 618 | $system="gzip -dv $fromfile"; | ||
| 619 | } | ||
| 620 | if ($system) { | ||
| 621 | my $out = `$system 2>&1`; | ||
| 622 | my $ret = $? >> 8; | ||
| 623 | if ($ret == 0) { | ||
| 624 | $done = $self->dict("D051",$system); | ||
| 625 | } else { | ||
| 626 | $done = join("", | ||
| 627 | $self->dict("D050",$system), | ||
| 628 | "<PRE>", | ||
| 629 | $out, | ||
| 630 | "</PRE>"); | ||
| 631 | } | ||
| 632 | } else { | ||
| 633 | $done = $self->dict("D052",$f); | ||
| 634 | } | ||
| 635 | $done; | ||
| 636 | } | ||
| 637 | |||
| 638 | sub publish { | ||
| 639 | my($self,$f) = @_; | ||
| 640 | my $sectrans = ""; | ||
| 641 | my $error = ""; | ||
| 642 | if ($self->has_perms($f)) { | ||
| 643 | $sectrans = qq{ publish $f\n}; | ||
| 644 | } else { | ||
| 645 | $error = $self->dict("D053",$f); | ||
| 646 | } | ||
| 647 | return($sectrans,$error); | ||
| 648 | } | ||
| 649 | |||
| 650 | sub lowercase { | ||
| 651 | my($self,$f) = @_; | ||
| 652 | my $stagedir = $self->{STAGEDIR}; | ||
| 653 | my $done; | ||
| 654 | my $lc = lc $f; | ||
| 655 | if ($lc eq $f) { | ||
| 656 | $done = $self->dict("D054", $f); | ||
| 657 | } else { | ||
| 658 | my $targetfile = "$stagedir$lc"; | ||
| 659 | my $targetdir = dirname("$targetfile"); | ||
| 660 | File::Path::mkpath($targetdir); | ||
| 661 | my $absfile = "$stagedir$f"; | ||
| 662 | my $ok = $self->dict(rename($absfile, $targetfile) ? "D055" : "D056"); | ||
| 663 | $done = $self->dict("D057", $f, $lc, $ok); | ||
| 664 | } | ||
| 665 | return $done; | ||
| 666 | } | ||
| 667 | |||
| 668 | sub delete { | ||
| 669 | my($self,$f) = @_; | ||
| 670 | my $stagedir = $self->{STAGEDIR}; | ||
| 671 | my $absfile = "$stagedir$f"; | ||
| 672 | my $ok = $self->dict(unlink($absfile) ? "D055" : "D056"); | ||
| 673 | return "<B>delete</B> $absfile [$ok]<BR>"; | ||
| 674 | } | ||
| 675 | |||
| 676 | sub linkcheck { # no dictionary used in this subroutine | ||
| 677 | my($self,$f,$files) = @_; | ||
| 678 | my $stageuri = $self->{STAGEURI}; | ||
| 679 | my($r) = $self->{R}; | ||
| 680 | my $display_method = (@$files > 1) ? "as_line" : "as_table"; | ||
| 681 | my $document_root = $self->document_root; | ||
| 682 | my(@done,%seen); | ||
| 683 | my($cntf,$cntn,$cnta,$try); | ||
| 684 | my $servername = $r->server->server_hostname, | ||
| 685 | $display_method ||= ""; | ||
| 686 | $try = "view"; | ||
| 687 | require HTML::LinkExtor; | ||
| 688 | my $p = HTML::LinkExtor->new; | ||
| 689 | $p->parse_file("$document_root$stageuri$f"); | ||
| 690 | my $s_uri = URI::URL->new("http://$servername$stageuri$f"); | ||
| 691 | my $b_uri = URI::URL->new("http://$servername$f"); | ||
| 692 | for my $link ($p->links) { | ||
| 693 | my($rlink, $slink, @comment); | ||
| 694 | my $tag = shift @$link; | ||
| 695 | my %attr = @$link; | ||
| 696 | my($k,$v,@attr); | ||
| 697 | while (($k,$v) = each %attr) { | ||
| 698 | my $x = qq{$k="$v"}; | ||
| 699 | while ($x =~ s/(.{1,35}\b)//) { | ||
| 700 | push @attr, $1; | ||
| 701 | } | ||
| 702 | push @attr, $x; | ||
| 703 | } | ||
| 704 | my $href; | ||
| 705 | if ($href = $attr{href} || $attr{src} || $attr{background}) { | ||
| 706 | if ($seen{$href}++) { | ||
| 707 | $rlink = $slink = "-"; | ||
| 708 | push @comment, "see above"; | ||
| 709 | } else { | ||
| 710 | my $t_uri = URI::URL->new($href); | ||
| 711 | my $found = 0; | ||
| 712 | my $rbase = $t_uri->abs($b_uri); | ||
| 713 | my $sbase = $t_uri->abs($s_uri); | ||
| 714 | if ($rbase->path =~ m|^/../|) { | ||
| 715 | $rlink = $slink = "bad path"; | ||
| 716 | } elsif ($rbase->scheme ne "http") { | ||
| 717 | my $scheme = $rbase->scheme; | ||
| 718 | $rlink = qq{<a href="$href">$try</A>}; | ||
| 719 | $slink = "-"; | ||
| 720 | push @comment, qq{protocol $scheme not tested}; | ||
| 721 | $found++; | ||
| 722 | $cntn++; | ||
| 723 | } elsif ($rbase->host ne $servername) { | ||
| 724 | $rlink = qq{<a href="$href">$try</A>}; | ||
| 725 | $slink = qq{-}; | ||
| 726 | push @comment, qq{remote host not tested}; | ||
| 727 | $found++; | ||
| 728 | $cntn++; | ||
| 729 | } else { | ||
| 730 | # real link, stage link | ||
| 731 | $rlink = $slink = "needs work"; | ||
| 732 | my $path = $rbase->path; | ||
| 733 | my $subr = $r->lookup_uri($path); | ||
| 734 | my $file = $subr->filename; | ||
| 735 | stat $file; | ||
| 736 | if (-f _ || -d _) { | ||
| 737 | $found++; | ||
| 738 | if ($rbase->frag) { | ||
| 739 | my $abs = $rbase->path ."#". $rbase->frag; | ||
| 740 | $rlink = qq{<a href="$abs">$try</A>}; | ||
| 741 | if ($rbase->path eq $f) { | ||
| 742 | # anchortesten? | ||
| 743 | } | ||
| 744 | $cnta++; | ||
| 745 | push @comment, "Real Link anchor not tested"; | ||
| 746 | } else { | ||
| 747 | my $abs = $rbase->as_string; | ||
| 748 | $rlink = qq{<a href="$abs">$try</A>}; | ||
| 749 | push @comment, "Real Link OK"; | ||
| 750 | } | ||
| 751 | } else { | ||
| 752 | # could really run a subrequest | ||
| 753 | my $abs = $rbase->as_string; | ||
| 754 | $rlink = qq{file not found, try to <a href="$abs">$try</A>}; | ||
| 755 | } | ||
| 756 | $path = $sbase->path; | ||
| 757 | stat "$document_root$path"; | ||
| 758 | if (-f _ || -d _) { | ||
| 759 | $found++; | ||
| 760 | if ($sbase->frag) { | ||
| 761 | my $abs = $sbase->path ."#". $sbase->frag; | ||
| 762 | $slink = qq{<a href="$abs">$try</A>}; | ||
| 763 | $cnta++; | ||
| 764 | push @comment, "Stage Link anchor not tested"; | ||
| 765 | } else { | ||
| 766 | my $abs = $sbase->as_string; | ||
| 767 | $slink = qq{<a href="$abs">$try</A>}; | ||
| 768 | push @comment, "Stage OK"; | ||
| 769 | } | ||
| 770 | } else { | ||
| 771 | $slink = "not found"; | ||
| 772 | } | ||
| 773 | } | ||
| 774 | unless ($found) { | ||
| 775 | $rlink = "<B>$rlink</B>"; | ||
| 776 | $slink = "<B>$slink</B>"; | ||
| 777 | $cntf++; | ||
| 778 | } | ||
| 779 | } | ||
| 780 | } else { | ||
| 781 | $rlink = $slink = "no href, no src, not tested"; | ||
| 782 | } | ||
| 783 | my $attr = join " ", @attr; | ||
| 784 | my $comment = join ", ", @comment; | ||
| 785 | $attr =~ s/\"/"/g; | ||
| 786 | push @done, sprintf(<<EOS, | ||
| 787 | <TR><TD>%s %s</TD><TD ALIGN=CENTER>%s</TD><TD | ||
| 788 | ALIGN=CENTER>%s</TD><TD>%s</TD></TR> | ||
| 789 | EOS | ||
| 790 | $tag, $attr, $rlink, $slink, $comment); | ||
| 791 | } | ||
| 792 | if (@done) { | ||
| 793 | unshift(@done, <<EOS); | ||
| 794 | <TABLE><TR> | ||
| 795 | <TH>Ref</TH> | ||
| 796 | <TH><I>Real</I> Link</TH> | ||
| 797 | <TH><I>Stage</I> Link</TH> | ||
| 798 | <TH>Comment</TH></TR> | ||
| 799 | EOS | ||
| 800 | push @done, "</TABLE>\n"; | ||
| 801 | } | ||
| 802 | my $vreport = sprintf "%d Error", $cntf; | ||
| 803 | $vreport .= sprintf ", %d Links not tested", $cntn if $cntn; | ||
| 804 | $vreport .= sprintf ", %d Anchor not tested", $cnta if $cnta; | ||
| 805 | unshift( | ||
| 806 | @done, | ||
| 807 | sprintf( | ||
| 808 | qq{<TABLE BORDER><TR><TH>File <A HREF="%s%s">%s%s</A> }. | ||
| 809 | qq{%s</TH></TR><TR><TD>}, | ||
| 810 | $stageuri, | ||
| 811 | $f, | ||
| 812 | $stageuri, | ||
| 813 | $f, | ||
| 814 | $vreport | ||
| 815 | )); | ||
| 816 | push @done, "</TABLE>\n"; | ||
| 817 | |||
| 818 | if ($display_method eq "as_line") { | ||
| 819 | return sprintf( | ||
| 820 | qq{<A HREF="/perl/user/up?SUBMITaction=1&}. | ||
| 821 | qq{ACTION=linkcheck&STAGEDFILES=%s">%s%s</A> %s<BR>}, | ||
| 822 | $f, | ||
| 823 | $stageuri, | ||
| 824 | $f, | ||
| 825 | $vreport); | ||
| 826 | } | ||
| 827 | return join "\n", @done; | ||
| 828 | } | ||
| 829 | |||
| 830 | 1; | ||
| 831 | |||
| 832 | =head1 NAME | ||
| 833 | |||
| 834 | Apache::UploadSvr - A Lightweight Publishing System for Apache | ||
| 835 | |||
| 836 | =head1 SYNOPSIS | ||
| 837 | |||
| 838 | |||
| 839 | |||
| 840 | =head1 DESCRIPTION | ||
| 841 | |||
| 842 | This module implements a small publishing system for a web server with | ||
| 843 | authentication, simple security, preview, directory viewer and an interface | ||
| 844 | to delete files. The whole system is actually running software on | ||
| 845 | www.kulturbox.de at the time of publishing (i.e. Summer 1998). | ||
| 846 | |||
| 847 | The author is looking for somebody to take this code over for | ||
| 848 | maintainance. | ||
| 849 | |||
| 850 | =head1 CONFIGURATION | ||
| 851 | |||
| 852 | httpd.conf: | ||
| 853 | |||
| 854 | PerlSetVar Auth_DBI_data_source dbi:mSQL:authen | ||
| 855 | PerlSetVar Auth_DBI_pwd_table usertable | ||
| 856 | PerlSetVar Auth_DBI_grp_table grouptable | ||
| 857 | PerlSetVar Auth_DBI_uid_field user | ||
| 858 | PerlSetVar Auth_DBI_grp_field group | ||
| 859 | PerlSetVar Auth_DBI_pwd_field password | ||
| 860 | PerlSetVar stageuri /STAGE | ||
| 861 | PerlSetVar Apache_UploadSvr_Usermgr "Apache::UploadSvr::User" | ||
| 862 | PerlSetVar Apache_UploadSvr_myuri /perl/user/up | ||
| 863 | PerlSetVar Apache_UploadSvr_transdir /usr/local/apache/trans | ||
| 864 | PerlSetVar Apache_UploadSvr_trashdir /usr/local/apache/trash | ||
| 865 | |||
| 866 | |||
| 867 | <Files "..."> | ||
| 868 | PerlSetVar DirCache .dircache | ||
| 869 | SetHandler perl-script | ||
| 870 | PerlHandler Apache::UploadSvr::Directory | ||
| 871 | AuthName stadtplandienst | ||
| 872 | AuthType Basic | ||
| 873 | PerlAuthenHandler Apache::AuthenDBI | ||
| 874 | require valid-user | ||
| 875 | </Files> | ||
| 876 | |||
| 877 | <Location /perl/user/up> | ||
| 878 | PerlHandler Apache::UploadSvr | ||
| 879 | </Location> | ||
| 880 | |||
| 881 | Change the permissions for the whole document tree to give the server | ||
| 882 | write access. | ||
| 883 | |||
| 884 | =head1 SECURITY | ||
| 885 | |||
| 886 | |||
| 887 | |||
| 888 | =head1 EXPORT | ||
| 889 | |||
| 890 | |||
| 891 | |||
| 892 | =head1 BUGS | ||
| 893 | |||
| 894 | |||
| 895 | |||
| 896 | =head1 COPYRIGHT | ||
| 897 | |||
| 898 | The application and accompanying modules are Copyright KULTURBOX, Berlin. | ||
| 899 | It is free software and can be used, copied and redistributed at the same | ||
| 900 | terms as perl itself. | ||
| 901 | |||
| 902 | =head1 AUTHOR | ||
| 903 | |||
| 904 | Andreas Koenig <koenig@kulturbox.de> | ||
| 905 | |||
| 906 | =cut |