Debian Codegraph @

cgi-bin/dox

raw
1 #!/usr/bin/perl -wT
2
3 # F*EX CGI for document exchange
4 #
5 # Author: Ulli Horlacher <[email protected]>
6 #
7
8 BEGIN { ($ENV{PERLINIT}||'') =~ /(.+)/s and eval $1 }
9
10 use strict qw(vars subs);
11
12 use File::Basename;
13 use Digest::MD5 qw(md5_hex);
14 use Cwd qw(abs_path getcwd);
15 use POSIX qw(strftime locale_h);
16 use Fcntl qw(:flock :seek :mode);
17
18 # POSIX time format needed for HTTP header
19 setlocale(LC_TIME,'POSIX');
20
21 # add fex lib
22 (our $FEXLIB) = $ENV{FEXLIB} =~ /(.+)/;
23 die "$0: no $FEXLIB\n" unless -d $FEXLIB;
24
25 our $error = 'F*EX document ERROR';
26
27 # import from fex.pp and fex.ph
28 our ($spooldir,$docdir,$tmpdir,$akeydir,$akey,$charset);
29 our ($admin,$hostname,$sender_from,$sendmail,$bcc,@remote_domains,@durl);
30 our ($vp,$mrx,$bs,$timeout,$mdomain,$document_exchange,$style);
31 our ($FEXHOME,$MB,$RA);
32 our (@extra_header,@download_hosts);
33
34 # load common code, local config : $HOME/lib/fex.ph
35 require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";
36
37 our $head = "$ENV{SERVER_NAME} F*EX DOcument eXchange";
38
39 my $log = 'dox.log';
40 my $QS = $ENV{QUERY_STRING} || '';
41 my $uri = $ENV{PATH_INFO} || '';
42 my $owner = '';
43 my $user = 'anonymous';
44 my $file = '';
45 my $folder = '';
46 my $http_auth = '';
47 my $realm = '';
48 my $submit = '';
49 my $action = '';
50 my $mirror = '';
51 my $directory = '';
52 my $parentdir = '';
53 my $uploaddir = '';
54 my $newdir = '';
55 my $newfolder = '';
56 my $destination = '';
57 my $allowpublish = '';
58 my $userupload = '';
59 my $filedata = '';
60 my $utypes = 'gif|jpg|png|avi|mp\d|flv|m4v|ogg|tar|tgz|zip|7z|gz|bz2|rar|iso';
61 my $back = '<a href="javascript:history.back()">back</a>';
62 my $scroll = '<script>window.scrollTo(0,document.body.scrollHeight);</script>';
63 my $delete = '<font color="red">delete</font>';
64 my $deleteall = '<font color="red">DELETE</font>';
65 my $stdn = 'style="text-decoration:none"';
66 my $al = 'align="left"';
67 my $ar = 'align="right"';
68 my @files = ();
69 my @renamefrom = ();
70 my @renameto = ();
71 my %exclude = ();
72 my %PARAM = ();
73 my $dox = $ENV{SCRIPT_NAME};
74 my $hosturl = "$ENV{PROTO}://$ENV{HTTP_HOST}";
75
76 $ENV{PATH} = $ENV{FEXHOME}.'/bin:'.$ENV{PATH};
77
78 # $QS =~ s/%([\dA-F]{2})/unpack("a",pack("H2",$1))/ge;
79 $QS = decode_utf8(urldecode($QS));
80
81 if (@download_hosts and not ipin($RA,@download_hosts)) {
82 http_die(
83 "Downloads from your host ($RA) are not allowed.",
84 "Contact $ENV{SERVER_ADMIN} for details."
85 );
86 }
87
88 if (($ENV{HTTP_AUTHORIZATION}||'') =~ /Basic\s+(\S+)/) {
89 $http_auth = decode_b64($1);
90 }
91
92 $uri =~ s:%3F:/?/:g; # escape '?' for URL-decoding
93 $uri =~ s/%([\dA-F]{2})/unpack("a",pack("H2",$1))/ge;
94 $uri =~ s:/\?/:%3F:g; # deescape '?'
95 $uri =~ s://+:/:;
96 $uri =~ s:^/::;
97
98 $akey = $ENV{AKEY}||'';
99
100 # look for CGI parameters
101 &parse_request;
102
103 $akey = '' unless -f "$akeydir/$akey/@";
104
105 if ($uri =~ m:(.+?)/(.+):) {
106 $owner = $1;
107 $file = $2;
108 $file =~ s:/+$::;
109 $folder = $file;
110 $folder =~ s:/.*::;
111 } else {
112 $owner = $uri;
113 $owner =~ s:/+$::;
114 }
115
116 $owner =~ s:/::g;
117
118 if ($file =~ m:(^|/)\.\.(/|$):) {
119 http_die('illegal file name');
120 }
121
122 if ($akey and readlink_("$akeydir/$akey") =~ m:.*/(.+):) {
123 my $a = $1;
124 if ($owner) {
125 if ($a ne $owner) {
126 redirect("/$dox");
127 }
128 } else {
129 $owner = $a;
130 }
131 }
132
133 my $doxo = "$dox/$owner";
134 my $doxurl = "$hosturl/$doxo/";
135
136 unless ($owner) {
137 redirect("/fup?forward=/$dox");
138 }
139
140 unless (-f "$owner/@") {
141 http_die("unknown user $owner");
142 }
143
144 # user client ip allowed?
145 if (length($folder) and not $akey) {
146 my $access = "$owner/DOX/$folder/.fexdox/access";
147 if (-s $access and open $access,$access) {
148 my $noaccess = 1;
149 while (<$access>) {
150 /^#/ and next;
151 s/\s//g;
152 if (ipin($RA,$_)) {
153 $noaccess = 0;
154 last;
155 }
156 }
157 close $access;
158 if ($noaccess) {
159 errorlog("access denied to $owner/DOX/$folder");
160 http_error(403);
161 }
162 }
163 }
164
165
166 # zip archive can be downloaded without authentification
167 if ($file =~ /^[^\/]+\.zip$/) {
168 chdir "$owner/DOX" or http_die("$owner : no documents available");
169 if (-s $file) {
170 http_output($file) and unlink $file;
171 } else {
172 http_die("$owner : $file not available");
173 }
174 exit;
175 }
176
177 my $useraccess = '';
178 if (length($folder) and
179 (not $akey or readlink_("$akeydir/$akey") !~ m:\Q/$owner\E$:))
180 {
181 if ((slurp("$owner/DOX/$folder/.fexdox/users")||'') =~ /^[^\s#]+:\S/m) {
182 $useraccess = 'yes';
183 }
184 if (open my $fdc,"$owner/DOX/$folder/.fexdox/config") {
185 while (<$fdc>) {
186 s/#.*//;
187 $useraccess = lc $1 if /^\s*useraccess\s*=\s*(\w+)/;
188 }
189 close $fdc;
190 }
191 if ($useraccess ne 'yes' and $useraccess ne 'anonymous') {
192 http_header('200 OK');
193 print html_header($head);
194 pq(qq(
195 '<h3>Access to folder /$folder is restricted to'
196 '<a href="/fup?from=$owner?forward=/$dox/$owner">$owner</a></h3>'
197 ));
198 exit;
199 }
200 }
201
202 &check_auth;
203
204 if ($akey) {
205 my $doxua = lc readlink_("$owner/\@DOCUMENT_EXCHANGE");
206 if ($doxua eq 'yes' or not $doxua and $document_exchange) {
207 mkdirp("$owner/DOX");
208 }
209 }
210
211 chdir "$owner/DOX" or http_die("DOX not available for $owner");
212
213 if ($file) {
214 if (not -d $file and -d "$file!backup") {
215 $file = "$file!backup";
216 } elsif ("/$file/" =~ m:\Q/$folder/UPLOAD/:) {
217 if ($akey) {
218 $file =~ s:/UPLOAD.*:/.upload:;
219 redirect("/$doxo/$file/");
220 } elsif (-d "$folder/.upload/$user") {
221 $file =~ s:\Q$folder\E/UPLOAD:$folder/.upload/$user:;
222 $userupload = $file;
223 mkdirp($userupload);
224 }
225 }
226 mkdir $file if $file =~ m:^\Q$folder/.upload\E$:;
227 if (not -e $file) {
228 $file =~ s:(_$vp)/:$1!backup/:;
229 unless (-e $file) {
230 $file =~ s:!backup::;
231 http_die("$owner : /$file does not exist");
232 }
233 }
234 }
235
236 if (length($folder)) {
237 mkfexdox($folder);
238 foreach ('.*','#*','*~') {
239 $exclude{$_} = shp2prx($_);
240 }
241 if (open my $fdx,"$folder/.fexdox/private") {
242 while (<$fdx>) {
243 next if /^#/;
244 s/[\r\n]//g;
245 s://+:/:;
246 s:^/\Q$folder:/:;
247 s:/$::;
248 $exclude{$_} = shp2prx($_) if length;
249 }
250 close $fdx;
251 }
252 if (open my $fdc,"$folder/.fexdox/config") {
253 while (<$fdc>) {
254 s/#.*//;
255 $charset = $1 if /^\s*charset\s*=\s*(\S+)/;
256 }
257 close $fdc;
258 }
259 }
260
261
262 if ($action eq 'okey' and -d $file) {
263 okey($file);
264 exit;
265 }
266
267 if ($action eq 'listit' and -d $file) {
268 list($file);
269 exit;
270 }
271
272 if ($action eq 'userlist' and -d $file) {
273 userlist($file);
274 exit;
275 }
276
277 if ($action eq 'zipit' and -d $file) {
278 zipdir($file);
279 exit;
280 }
281
282 if ($action eq 'syncit' and -d $file) {
283 syncdir($file);
284 exit;
285 }
286
287 if ($action eq 'streamit' and -d $file) {
288 streamdir($file);
289 exit;
290 }
291
292 if ($action eq 'fexit' and -d $file) {
293 fexdir($file,$user);
294 exit;
295 }
296
297 if ($submit eq 'fex it' and -d $file and @files) {
298 fexfiles(@files,$user);
299 exit;
300 }
301
302 if ($submit eq 'zip it' and -d $file and @files) {
303 zipfiles(@files);
304 exit;
305 }
306
307 if ($submit eq 'delete' and -d $file and @files) {
308 delfiles(@files);
309 doxdu($owner,$folder);
310 my $dir = $file;
311 unless ($akey) {
312 $dir =~ s:\Q/.upload/$user:/UPLOAD:;
313 }
314 # $dir = urlencode($dir);
315 redirect("$doxurl$dir/");
316 }
317
318 if ($submit eq 'publish' and @files) {
319 publish(@files);
320 my $dir = $file;
321 $dir =~ s:/\.upload/[^/]+::;
322 redirect("$doxurl$dir/");
323 }
324
325 if ($submit eq 'copy' and -d $file and -d $destination and @files) {
326 copyfiles(@files);
327 redirect("$doxurl$destination/");
328 }
329
330 if ($submit eq 'move' and -d $file and -d $destination and @files) {
331 movefiles(@files);
332 redirect($doxurl.urlencode($file).'/');
333 }
334
335 if ($submit eq 'rename' and -d $file and @files) {
336 queryrename(@files);
337 exit;
338 }
339
340 if (@renamefrom and @renameto) {
341 renamefiles(@files);
342 redirect($doxurl.urlencode($file).'/');
343 }
344
345 if ($submit eq 'save') {
346 $filedata .= "\n" if length($filedata) and $filedata !~ /\n$/;
347 savefile($file);
348 redirect($doxurl.urlencode(dirname($file)).'/');
349 }
350
351 if (-d $file and $user and $action eq 'sendfile') {
352 sendfile($file,$filedata);
353 &reexec;
354 }
355
356 if ($user and $action eq 'notify') {
357 notify_owner();
358 exit;
359 }
360
361 if ($akey and $mirror) {
362 mirror();
363 exit;
364 }
365
366 if ($parentdir and $newdir) {
367 if ($akey) {
368 my $dir = "$parentdir/$newdir";
369 mkdir $dir or http_die("cannot mkdir $dir - $!");
370 update_folder($1) if $dir =~ m:([^/]+):;
371 redirect("/$doxo/$parentdir/");
372 } else {
373 if ($parentdir !~ m:^\Q$folder/.upload/$user:) {
374 http_die("illegal parentdir $parentdir");
375 }
376 my $dir = "$parentdir/$newdir";
377 mkdir $dir or http_die("cannot mkdir $dir - $!");
378 $dir = $parentdir;
379 $dir =~ s:\Q/.upload/$user:/UPLOAD:;
380 redirect("/$doxo/$dir/");
381 }
382 }
383
384 if ($submit and $uploaddir) {
385 redirect("/$dox") unless $akey;
386 my $dir = "$folder/.upload/$uploaddir";
387 mkdirp($dir);
388 redirect("/$doxo/$folder/.upload/");
389 }
390
391 if ($allowpublish) {
392 redirect("/$dox") unless $akey;
393 my $user = $allowpublish;
394 my $dir = "$folder/.upload/$user";
395 unless (-d $dir) {
396 http_die("$dir does not exist");
397 }
398 my $publish = "$folder/.upload/$user!publish";
399 open $publish,'>',$publish or http_die("$publish - $!");
400 print {$publish} "# User $user can publish his uploads (move to /$folder/)\n";
401 close $publish;
402 redirect("/$doxo/$folder/.upload/");
403 }
404
405 if ($newfolder) {
406 redirect("/$dox") unless $akey;
407 if (-d $newfolder) {
408 http_die("$newfolder does already exist");
409 }
410 my @d = localtime time;
411 my $nv = sprintf(
412 '%s_%d%02d%02d_%02d%02d%02d',
413 $newfolder,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]
414 );
415 mkdir $nv or http_die("cannot mkdir $nv - $!");
416 symlink $nv,$newfolder or http_die("cannot symlink $nv - $!");
417 redirect("/$dox");
418 }
419
420 if ($QS =~ /ACTION=logout/) {
421 unlink "$akeydir/$akey" if $akey;
422 redirect("/$dox");
423 }
424
425 if ($akey and $QS =~ /ACTION=edit/) {
426 edit($file);
427 exit;
428 }
429
430 if ($akey and $QS =~ /ACTION=logview/) {
431 logview($file);
432 exit;
433 }
434
435 if ($file and $QS eq 'ACTION=publishall') {
436 publish_all($file);
437 redirect("/$doxo/$folder/");
438 exit;
439 }
440
441 if ($file and $QS eq 'ACTION=backup') {
442 redirect("/$dox") unless $akey;
443 $folder = basename($file);
444 if (-d $folder and my $ov = untaint(readlink_($folder))) {
445 my @d = localtime time;
446 my $nv = sprintf(
447 '%s_%d%02d%02d_%02d%02d%02d',
448 $folder,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]
449 );
450 my $backup = "$ov!backup";
451 unless (-d $backup) {
452 $_ = `cp -al $ov $backup 2>&1`;
453 http_die($_) if $?;
454 }
455 backup_cleanup($folder);
456 redirect("/$dox");
457 } else {
458 http_die("unknown folder /$folder");
459 }
460 } elsif ($file and $QS eq 'ACTION=restore') {
461 redirect("/$dox") unless $akey;
462 $file =~ s:/::;
463 if (-d $file and $file =~ /(.+)_$vp(!backup)?$/ and -l $1) {
464 $folder = $1;
465 if ($file =~ s/!backup$//) {
466 } else {
467 $_ = `cp -al $file $file!backup 2>&1`;
468 http_die($_) if $?;
469 }
470 unlink $folder;
471 symlink $file,$folder;
472 backup_cleanup($folder);
473 redirect("/$dox");
474 } else {
475 http_die("unknown backup /$file");
476 }
477 } elsif ($file and $QS eq 'ACTION=delete') {
478 redirect("/$dox") unless $akey;
479 if ($file !~ m:/:) {
480 if (-l $file) {
481 # delete all versions, backup and stream
482 if (my @v = grep /^\Q$file\E_$vp(!\S+)?$/,glob "${file}_*") {
483 foreach my $v (@v) {
484 rmrf(untaint($v));
485 }
486 }
487 } elsif (-d $file and $file =~ /(.+)_$vp$/) {
488 $folder = $1;
489 if (-l $folder and my @v = grep /^\Q$folder\E_$vp$/,glob "${folder}_*") {
490 if (scalar(@v) == 1) {
491 unlink $folder;
492 } elsif (readlink_($folder) eq $file) {
493 unlink $folder;
494 if ($v[-1] ne $file) {
495 symlink untaint($v[-1]),$folder;
496 } else {
497 symlink untaint($v[-2]),$folder;
498 }
499 }
500 }
501 }
502 }
503 rmrf($file);
504 doxdu($owner,$folder);
505 redirect("/$dox/".dirname($uri));
506 } elsif ($file =~ m:([^/]+): and -d $1) {
507 if ($akey and $file eq "$folder/.fexdox" and not -d $file) {
508 mkfexdox($folder);
509 }
510 if (-d $file) {
511 if ($uri !~ m:/$:) {
512 redirect("/$dox/$uri/");
513 } else {
514 showindex($file);
515 }
516 } elsif (-f $file) {
517 sleep(2);
518 http_output($file);
519 } else {
520 http_die("$owner : $file is not available");
521 }
522 } elsif ($akey) {
523 showindex('/');
524 } else {
525 http_die("$owner : $file is not available");
526 }
527
528 &reexec;
529 exit;
530
531 sub check_auth {
532 my $id = readline1("$spooldir/$owner/@")||'';
533 my ($users);
534 my (%pw,%access);
535
536 if ($akey) {
537 if (-f "$akeydir/$akey/@") {
538 $user = $owner;
539 return;
540 } else {
541 $akey = '';
542 }
543 }
544
545 if ($http_auth =~ /^(fexmaster.*):/) {
546 my $fm = $1;
547 $fm .= '@'.$mdomain if $mdomain and $fm !~ /@/;
548 $id = readline1("$spooldir/$fm/@")||'';
549 if ($http_auth =~ /:$id$/) {
550 $akey = 'fexmaster';
551 if ($folder) {
552 $realm = "/$owner/$folder";
553 } else {
554 $realm = "/$owner";
555 }
556 return;
557 }
558 }
559
560 if ($folder) {
561 $realm = "/$owner/$folder";
562 } else {
563 if ($QS =~ /^realm=([^\s\'\"]+)/) {
564 http_header(
565 '401 Authorization Required',
566 "WWW-Authenticate: Basic realm=\"$1\"",
567 'Content-Length: 0',
568 );
569 &reexec;
570 } else {
571 redirect("/fup?from=$owner?forward=/$dox/$owner");
572 }
573 }
574
575 if ($http_auth eq "$owner:$id") {
576 $user = $owner;
577 return;
578 }
579
580 return if $useraccess eq 'anonymous';
581
582 if (open $users,"$spooldir/$owner/DOX/$folder/.fexdox/users") {
583 while (<$users>) {
584 /^#/ and next;
585 s/\s//g;
586 my ($user,$pw) = split ':';
587 $pw{lc($user)} = $pw if $pw;
588 }
589 close $users;
590 }
591
592 if ($http_auth =~ /(.+):(.+)/) {
593 my $u = lc $1;
594 my $p = $2;
595 if ($pw{$u} and $pw{$u} eq $p) {
596 $user = $u;
597 return;
598 }
599 }
600
601 http_header(
602 '401 Authorization Required',
603 "WWW-Authenticate: Basic realm=\"$realm\"",
604 'Content-Length: 0',
605 );
606 &reexec;
607 }
608
609
610 sub http_output {
611 my $file = shift;
612 my ($filename,$size,$total_size,$range);
613 my ($data,$type);
614 my ($var,$env,$con);
615 my @files;
616 my @s;
617 my $s = 0;
618 my $b = 0;
619 my $seek = 0;
620 my $stop = 0;
621 local $_;
622
623 if (-l $file or not -f $file or $file =~ m:/\.: and not $akey) {
624 http_error(404);
625 }
626
627 # reget?
628 if ($range = $ENV{HTTP_RANGE}) {
629 $seek = $1 if $range =~ /^bytes=(\d+)-/i;
630 $stop = $1 if $range =~ /^bytes=\d*-(\d+)/i;
631 }
632
633 if (not $akey and excluded($file)) {
634 http_die("no access for /$file");
635 }
636
637 if ($file =~ m:^/: or "/$file/" =~ m:\Q/../:) {
638 http_die("illegal file name $file");
639 }
640
641 open $file,'<',$file or http_error(400);
642
643 $type = 'application/octet-stream';
644 if ($file =~ /\.html?$/i) { $type = 'text/html' }
645 elsif ($file =~ /\.css$/) { $type = 'text/css' }
646 elsif ($file =~ /\.js$/) { $type = 'text/javascript' }
647 elsif ($file =~ /\.txt$/i) { $type = 'text/plain' }
648 elsif ($file =~ /\.ps$/) { $type = 'application/postscript' }
649 elsif ($file =~ /\.pdf$/i) { $type = 'application/pdf' }
650 elsif ($file =~ /\.mp3$/i) { $type = 'audio/mpeg' }
651 elsif ($file =~ /\.mp4$/i) { $type = 'video/mp4' }
652 elsif ($file =~ /\.avi$/i) { $type = 'video/x-msvideo' }
653 elsif ($file =~ /\.jpg$/i) { $type = 'image/jpeg' }
654 elsif ($file =~ /\.png$/i) { $type = 'image/png' }
655 elsif ($file =~ /\.gif$/i) { $type = 'image/gif' }
656 elsif ($file =~ /\.bmp$/i) { $type = 'image/x-ms-bmp' }
657 elsif ($file =~ /\.ico$/i) { $type = 'image/vnd.microsoft.icon' }
658 elsif ($file !~ /\.(tar|tgz|zip|jar|rar|arj|7z|bz2?|gz)$/i) {
659 my $qfile = shellquote(untaint($file));
660 $_ = `file $qfile 2>/dev/null`;
661 if (/:.*HTML/) {
662 $type = 'text/html';
663 } elsif (/:.*text/i and not -x $file) {
664 $type = 'text/plain';
665 if (/\sASCII\s/) { $type .= "; charset=us-ascii" }
666 elsif (/(ISO-[\w-]+)/) { $type .= "; charset=".lc($1) }
667 else { $type .= "; charset=utf-8" }
668 }
669 }
670
671 $type = 'text/plain' if $type eq 'text/html';
672
673 $total_size = -s $file || 0;
674 $size = $total_size - $seek - ($stop ? $total_size-$stop-1 : 0);
675
676 if ($size < 0) {
677 http_header('416 Requested Range Not Satisfiable');
678 exit;
679 }
680
681 alarm($timeout*10);
682
683 if ($seek or $stop) {
684 my $range;
685 if ($stop) {
686 $range = sprintf("bytes %s-%s/%s",$seek,$stop,$total_size);
687 } else {
688 $range = sprintf("bytes %s-%s/%s",$seek,$total_size-1,$total_size);
689 }
690 nvt_print(
691 'HTTP/1.1 206 Partial Content',
692 'Server: fexsrv',
693 "Content-Length: $size",
694 "Content-Range: $range",
695 "Content-Type: $type",
696 );
697 } else {
698 my $date = http_date($file);
699 nvt_print(
700 'HTTP/1.1 200 OK',
701 'Server: fexsrv',
702 "Last-Modified: $date",
703 "Expires: 0",
704 "Content-Length: $size",
705 "Content-Type: $type",
706 );
707 }
708 nvt_print($_) foreach(@extra_header);
709 nvt_print('');
710
711 # binary data # can be stream!
712 seek $file,$seek,0 if $seek;
713 while ($bs and $b = read($file,$data,$bs)) {
714 if ($stop and $s+$b > $size) {
715 $b = $size-$s;
716 $data = substr($data,0,$b);
717 $bs = 0;
718 }
719 $s += $b;
720 alarm($timeout*10);
721 print $data or last;
722 }
723 doxlog(sprintf "%s %s/%s",hexencode($file),$s,$size) if $s;
724
725 alarm(0);
726 close $file;
727 return($s+$seek == $total_size);
728 }
729
730
731 # show directory index
732 sub showindex {
733 my $dir = shift;
734 my ($htmldoc,$size,$dirhe,$path);
735 my $pathhref = '';
736 my @links = ();
737 my @dirs = ();
738 my %versions = ();
739 my %folder = ();
740 my @files = ();
741 my @users = ();
742 my $allowed;
743 my $uid = randstring(8);
744 my $js = slurp("$docdir/DOX/dox.js");
745 my $showdel;
746 my $v;
747 local $_;
748
749 $dir =~ s://+:/:;
750 $dir =~ s:^/::;
751 $dir =~ s:/$::;
752 $dir ||= '.' if $akey;
753
754 my $udir = urlencode($dir);
755
756 $js =~ s/\$RANDOM\$/$uid/g;
757
758 $htmldoc = html_header($head);
759 $htmldoc .= qqq(qq(
760 '<script>'
761 'function toggle_checkboxes(cb) {'
762 ' for (let checkbox of document.getElementsByName("file"))'
763 ' checkbox.checked = cb.checked;'
764 '}'
765 '</script>'
766 ));
767
768 if ($akey and $dir eq '.') {
769
770 # toplevel folders view
771
772 opendir $dir,$dir or http_die("$dir - $!");
773 while (defined($_ = readdir $dir)) {
774 next if /^[.#]/;
775 if ($v = readlink and $v =~ /$vp$/) {
776 $folder{$v} = $_;
777 }
778 if (-d and /((.+_$vp)!backup)$/ and not -d $2) {
779 rename $1,$2 and s/!backup$//;
780 }
781 if (-d and /(.+)_($vp(!backup)?)$/) {
782 push @{$versions{$1}},$2;
783 }
784 }
785 closedir $dir;
786
787 $style = qqq(qq(
788 'table {'
789 ' border-collapse: collapse;'
790 '}'
791 'th,td {'
792 ' border: 1px solid black;'
793 ' padding: 10px;'
794 '}'
795 ));
796
797 $htmldoc .= qq'<script>\n$js</script>\n';
798 $htmldoc .= '<h2>';
799 if ($akey eq 'fexmaster') {
800 $htmldoc .= "DOX of $owner ($akey access)\n";
801 } else {
802 $htmldoc .= "DOX of $owner\n";
803 }
804 $htmldoc .= '</h2>';
805 my $logout = button("/$dox?ACTION=logout",'logout');
806 $htmldoc .= qqq(qq(
807 '<style>'
808 '$style'
809 '</style>'
810 '$logout'
811 '&nbsp;'
812 '[<a href="/DOX/index.html">documentation</a>]'
813 '<p>'
814 '<table border=1>'
815 '<tr><th>folder<th>size<th>last modification<th>backup versions<th>folder actions</tr>'
816 ));
817 foreach my $d (asort(keys %versions)) {
818 my $kb = readlink(".$d.du")||0;
819 my ($mb,$date);
820 $mb = int($kb/1024) || '<1';
821 $showdel = 'a';
822 if (open my $fdc,"$d/.fexdox/config") {
823 while (<$fdc>) {
824 s/#.*//;
825 $showdel = '' if /^\s*showdelete\s*=\s*no/;
826 $showdel = 'a' if /^\s*showdelete\s*=\s*all/;
827 $showdel = 'b' if /^\s*showdelete\s*=\s*backup/;
828 }
829 close $fdc;
830 }
831 if (readlink_($d) =~ /_(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)$/) {
832 $date = "$1-$2-$3 $4:$5:$6";
833 } else {
834 $date = "????-??-?? ??:??:??";
835 }
836 $htmldoc .= qq'<tr><td><a href="/$doxo/$d/"><b>/$d</b></a>';
837 $htmldoc .= qq'<td align="right">$mb MB<td>$date<td>';
838 @{$versions{$d}} = sort @{$versions{$d}};
839 foreach (reverse @{$versions{$d}}) {
840 my $vd = my $v = $_; local $_;
841 my $dv = $d.'_'.$v;
842 $vd = "$1-$2-$3 $4:$5:$6" if $v =~ /(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)/;
843 next if $folder{$dv};
844 if ($v =~ s/!backup//g) {
845 next if readlink_($d) !~ /$v$/;
846 my $href = "javascript:alert('folder $d version is already $v')";
847 my $sv = $d.'_'.$v;
848 $htmldoc .= qq'<a href="/$doxo/$sv/"><code>$v</code></a> '.
849 button($href,'restore');
850 } else {
851 my $sv = $d.'_'.$v;
852 $htmldoc .= qq'<a href="/$doxo/$sv/"><code>$v</code></a> '.
853 button("/$doxo/$dv?ACTION=restore",'restore');
854 }
855 if ($showdel =~ /^[ab]/) {
856 $htmldoc .= ' '.button("/$doxo/$dv?ACTION=delete",$delete);
857 }
858 $htmldoc .= '<br>';
859 }
860 $htmldoc .= qq'<td>';
861 if (-d readlink_($d).'!backup') {
862 my $href = "javascript:alert('backup of folder $d is up to date')";
863 $htmldoc .= button($href,'backup');
864 } else {
865 $htmldoc .= button("/$doxo/$d?ACTION=backup",'backup');
866 }
867 if ($showdel eq 'a') {
868 $htmldoc .= ' ' . button("/$doxo/$d?ACTION=delete",$deleteall);
869 }
870 $htmldoc .= "</tr>\n";
871 }
872 $htmldoc .= "</table>\n<p>\n";
873 $htmldoc .= qqq(qq(
874 '<p>'
875 '<ul>'
876 '<li><form name="newfolder"'
877 ' action="/$dox"'
878 ' method="POST"'
879 ' accept-charset="$charset"'
880 ' enctype="multipart/form-data">'
881 ' <input type="submit" value="Create">'
882 ' new (empty) folder <input type="text" name="newfolder" size="40">'
883 '</form>'
884 '<p>'
885 '<li><form name="upload"'
886 ' action="/fup"'
887 ' method="POST"'
888 ' accept-charset="$charset"'
889 ' enctype="multipart/form-data"'
890 ' onsubmit="return showstatus()">'
891 ' <input type="hidden" name="from" value="$owner">'
892 ' <input type="hidden" name="to" value=".">'
893 ' <input type="hidden" name="uid" value="$uid">'
894 ' <input type="hidden" name="comment" value="DOX:SAVE=/">'
895 ' <input type="hidden" name="filesize" value="">'
896 ' <a id="uploadselect">'
897 ' Select a'
898 ' <button onclick="showfileupload(\'.zip\')">local zip file for upload</button>'
899 ' or select a'
900 ' <button onclick="showdirectoryupload()">local directory for upload</button>'
901 ' to create a new folder with content.'
902 ' </a>'
903 ' <a id="submit"></a>'
904 '</form>'
905 '</ul>'
906 ));
907
908 } elsif ($akey and $dir eq "$folder/.upload") {
909
910 $htmldoc .= "<h2>";
911 $htmldoc .= qq'<a href="/$doxo">DOX of $owner</a> ';
912 $htmldoc .= $akey eq 'fexmaster' ?
913 "($akey access)":
914 "(logged in as owner)";
915 $htmldoc .= "</h2>\n";
916
917 opendir $dir,$dir or http_die("$dir - $!");
918 while (defined($_ = readdir $dir)) {
919 next if /^\.\.?$/;
920 next if /\n/;
921 next if -l "$dir/$_";
922 if (lstat "$dir/$_") {
923 if (-d _) { push @dirs,$_ }
924 elsif (-f _) { push @files,$_ }
925 }
926 }
927 closedir $dir;
928
929
930 my $action = "/$doxo/".urlencode($dir)."?ACTION";
931 my $pathhref = qq'<a href="/$doxo/$folder">$folder</a>/.upload';
932
933 if (@dirs or @files) {
934 $htmldoc .= button("$action=listit",'list it');
935 $htmldoc .= ' &nbsp; ' . button("$action=zipit",'zip it');
936 $htmldoc .= ' &nbsp; ' . button("$action=fexit",'fex it');
937 $htmldoc .= ' &nbsp; ' . button("$action=syncit",'sync it') if $akey;
938 $htmldoc .= ' &nbsp; ' . button("$action=streamit",'stream it');
939 }
940 $htmldoc .= ' &nbsp; ' . button("/$doxo/$file",'reload');
941 $htmldoc .= ' &nbsp; ' . button("/$doxo$realm",'folders overview');
942 $htmldoc .= ' &nbsp; ' . button("/$dox?ACTION=logout",'logout');
943 $htmldoc .= ' &nbsp; ';
944 $htmldoc .= qq'[<a href="/DOX/index.html">documentation</a>]\n';
945 $htmldoc .= qqq(qq(
946 '<h2><tt>/$pathhref/</tt></h2>'
947 '<p><hr><p>'
948 ));
949
950 $htmldoc .= "<ul><li>";
951 $htmldoc .= button("/$doxo/$folder/.fexdox/users?ACTION=edit",
952 'Add or edit');
953 $htmldoc .= " users</ul>";
954
955
956 my @users = ();
957 if (open my $users,"$folder/.fexdox/users") {
958 while (<$users>) {
959 s/#.*//;
960 s/\s//g;
961 push @users,$1 if /^([^\/]+?@[\w.-]+):./;
962 }
963 close $users;
964 }
965 my $selection = '<select name="uploaddir" size="1">';
966 foreach (@users) {
967 unless (-d "$folder/.upload/$_") {
968 $selection .= '<option>'.htmlquote($_).'</option>';
969 }
970 }
971 $selection .= "</select>";
972 if ($selection =~ /<option>/) {
973 $htmldoc .= qqq(qq(
974 '<p>'
975 '<ul><li><form name="newuploaddir"'
976 ' action="/$doxo/$file"'
977 ' method="POST"'
978 ' accept-charset="$charset"'
979 ' enctype="multipart/form-data">'
980 ' <input type="submit" name="submit" value="Create">'
981 ' an upload directory for user $selection'
982 '</form></ul>'
983 ));
984 }
985
986 # first the user upload directories
987 if (@dirs) {
988 $htmldoc .= qqq(qq(
989 '<h3>Upload directories of your users</h3>'
990 '<form name="files"'
991 ' action="/$doxo/$file"'
992 ' method="POST"'
993 ' accept-charset="$charset"'
994 ' enctype="multipart/form-data">'
995 '<pre>'
996 ));
997 my $format = "%9s files # <b><a href=\"%s\"><em>%s/</em></a></b>\n";
998 foreach my $d (asort(@dirs)) {
999 my $ud = urlencode($d);
1000 my $hd = htmlencode($d);
1001 $htmldoc .= qq'<input type="checkbox" name="file" value="$ud"> ';
1002 $htmldoc .= sprintf $format,d3(countfiles("$dir/$d")),$ud,$hd;
1003 }
1004 $htmldoc .= qqq(qq(
1005 '</pre>'
1006 '&nbsp; &#8627; Action for selected directories:'
1007 '<input type="submit" name="submit" value="delete">'
1008 '</form>'
1009 ));
1010 }
1011
1012 # then the files
1013 if (my $n = scalar(@files)) {
1014 $htmldoc .= qqq(qq(
1015 '<h3>Files ($n)</h3>'
1016 '<form name="files"'
1017 ' action="/$doxo/$file"'
1018 ' method="POST"'
1019 ' accept-charset="$charset"'
1020 ' enctype="multipart/form-data">'
1021 '<pre>'
1022 ));
1023 my $format = "%s %20s bytes # <a href=\"%s\"><em>%s</em></a>\n";
1024 foreach my $f (asort(@files)) {
1025 next if $f eq 'log';
1026 my $uf = urlencode($f);
1027 my $hf = htmlencode($f);
1028 $htmldoc .= qq'<input type="checkbox" name="file" value="$uf"> ';
1029 $htmldoc .= sprintf $format,
1030 isodate(mtime("$dir/$f")),d3(-s "$dir/$f"||0),$uf,$hf;
1031 }
1032 if (-f "$dir/log") {
1033 $htmldoc .= qq'\n<input type="checkbox"> ';
1034 $htmldoc .= sprintf $format,
1035 isodate(mtime("$dir/log")),d3(-s "$dir/log"||0),
1036 'log?ACTION=logview','log';
1037 }
1038 $htmldoc .= qqq(qq(
1039 '</pre>'
1040 '&nbsp; &#8627; Action for selected files:'
1041 '<input type="submit" name="submit" value="delete">'
1042 '</form>'
1043 ));
1044
1045 my $selection = '<select name="allowpublish" size="1">';
1046 foreach (@dirs) {
1047 if (/^$mrx$/ and not -f ("$dir/$_!publish")) {
1048 $selection .= '<option>'.htmlquote($_).'</option>';
1049 }
1050 }
1051 $selection .= "</select>";
1052 if ($selection =~ /<option>/) {
1053 $htmldoc .= qqq(qq(
1054 '<p>'
1055 '<ul><li><form name="allowpublish"'
1056 ' action="/$doxo/$folder"'
1057 ' method="POST"'
1058 ' accept-charset="$charset"'
1059 ' enctype="multipart/form-data">'
1060 ' <input type="submit" value="Allow">'
1061 ' user $selection to publish uploads'
1062 ' (move to <code>/$folder/</code>)'
1063 '</form></ul>'
1064 ));
1065 }
1066 }
1067
1068 my $url = $ENV{REQUEST_URL};
1069 $url =~ s:/\.upload.*::;
1070 $htmldoc .= qqq(qq(
1071 '<p><hr><p>'
1072 'URL for your users:<br>'
1073 '$url/'
1074 ));
1075
1076 } else {
1077
1078 if (not $akey and $file !~ /^\Q$userupload/ and excluded($dir)) {
1079 http_die("no access for /$dir");
1080 }
1081
1082 opendir $dir,$dir or http_die("$dir - $!");
1083 while (defined($_ = readdir $dir)) {
1084 next if /^\.\.?$/;
1085 next if /\n/;
1086 next if -l "$dir/$_";
1087 next if not ($akey or $dir =~ m:/\.upload($|/):) and excluded("$dir/$_");
1088 if (lstat "$dir/$_") {
1089 if (-d _) { push @dirs,$_ }
1090 elsif (-f _) { push @files,$_ }
1091 }
1092 }
1093 closedir $dir;
1094
1095 if ($akey and $dir !~ m:/: and not grep /^\.fexdox$/,@dirs) {
1096 push @dirs,'.fexdox';
1097 }
1098
1099 $showdel = 'a';
1100 if ($akey and open my $fdc,"$folder/.fexdox/config") {
1101 while (<$fdc>) {
1102 s/#.*//;
1103 $showdel = '' if /^\s*showdelete\s*=\s*no/;
1104 $showdel = 'a' if /^\s*showdelete\s*=\s*all/;
1105 $showdel = 'b' if /^\s*showdelete\s*=\s*backup/;
1106 $showdel = 'd' if /^\s*showdelete\s*=\s*dir/;
1107 }
1108 close $fdc;
1109 }
1110
1111 # $htmldoc .= "<h1>[$ENV{PATH_INFO}]</h1>\n";
1112 $path = urldecode($ENV{PATH_INFO});
1113 # $htmldoc .= "<h1>[$path]</h1>\n";
1114 $path =~ s:/.+?/::; # remove owner
1115 $path =~ s:!backup/:/:;
1116 $pathhref = htmlencode(basename($path));
1117
1118 while ($path =~ s:(.+)/.+:$1:) {
1119 my $dir = htmlencode(basename($path));
1120 my $upath = urlencode($path);
1121 $pathhref = qq'<a href="/$doxo/$upath">$dir</a>/$pathhref';
1122 }
1123
1124 $realm = "?realm=$realm" if $realm;
1125
1126 $htmldoc .= "<h2>";
1127 if ($akey) {
1128 $htmldoc .= qq'<a href="/$doxo$realm">DOX of $owner</a> ';
1129 $htmldoc .= $akey eq 'fexmaster' ? "($akey access)" : "(logged in as owner)";
1130 } else {
1131 $htmldoc .= "DOX of $owner (user: $user)";
1132 }
1133 $htmldoc .= "</h2>\n";
1134
1135 if ($userupload) {
1136 my $okey = randstring(8);
1137 my $okeyd = "$spooldir/$owner/\@OKEY";
1138 mkdir $okeyd;
1139 symlink $user,"$okeyd/$okey"
1140 or http_die("cannot create OKEY $okeyd/$okey - $!");
1141
1142 if (countfiles("$folder/.upload/$user")) {
1143 $htmldoc .= "<script>\n$js</script>\n";
1144 my $action = "/$doxo/".urlencode($file)."?ACTION";
1145 $htmldoc .= button("$action=notify",'notify owner');
1146 if (-f "$folder/.upload/$user!publish" and
1147 $file =~ m:(\Q$folder/.upload/\E[^/]+)$:)
1148 {
1149 $htmldoc .= ' &nbsp; ';
1150 $htmldoc .= button("$action=publishall",'publish all');
1151 }
1152 $htmldoc .= qq' &nbsp; ';
1153 $htmldoc .= qq'[<a href="/DOX/user.html">documentation</a>]\n'
1154 }
1155 $htmldoc .= qqq(qq(
1156 '<h2><tt>/$pathhref/</tt></h2>'
1157 '<p><hr><p>'
1158 '<ul><li><form name="newdir"'
1159 ' action="/$doxo/$file"'
1160 ' method="POST"'
1161 ' accept-charset="$charset"'
1162 ' enctype="multipart/form-data">'
1163 ' <input type="hidden" name="parentdir" value="$udir">'
1164 ' <input type="submit" value="Create"> new directory'
1165 ' <input type="text" name="newdir" size="40">'
1166 '</form></ul>'
1167 '<p>'
1168 '<form name="files"'
1169 ' action="/$doxo/$file"'
1170 ' method="POST"'
1171 ' accept-charset="$charset"'
1172 ' enctype="multipart/form-data">'
1173 '<pre>'
1174 ));
1175
1176 # first the (sub)directories
1177 foreach my $d (asort(@dirs)) {
1178 my $ud = urlencode($d);
1179 my $hd = htmlquote($d);
1180 $htmldoc .= qq'<input type="checkbox" name="file" value="$ud"> ';
1181 $htmldoc .= sprintf qq'%9s files <b><a href="%s">%s/</a></b>\n',
1182 d3(countfiles("$dir/$d")),$ud,$hd;
1183 }
1184 $htmldoc .= "\n";
1185
1186 # then the files
1187 foreach my $f (asort(@files)) {
1188 $htmldoc .= sprintf '<input type="checkbox" name="file" value="%s"> ',
1189 urlencode($f);
1190 $htmldoc .= sprintf "%s %20s bytes %s\n",
1191 isodate(mtime("$dir/$f")),d3(-s "$dir/$f"||0),
1192 htmlencode($f);
1193 }
1194
1195 $htmldoc .= "</pre>\n";
1196 if (@dirs or @files) {
1197 $htmldoc .= qqq(qq(
1198 '<input type="checkbox" onClick="toggle_checkboxes(this)">'
1199 '(de)select all files and directories<br>'
1200 '&nbsp; &#8627; Action for selected files and directories:'
1201 '<input type="submit" name="submit" value="delete">'
1202 ));
1203 }
1204 $htmldoc .= "</form>\n";
1205 $htmldoc .= qqq(qq(
1206 '<ul><li><form name="upload"'
1207 ' action="/fup"'
1208 ' method="POST"'
1209 ' accept-charset="$charset"'
1210 ' enctype="multipart/form-data"'
1211 ' onsubmit="return showstatus()">'
1212 ' <input type="hidden" name="from" value="$user">'
1213 ' <input type="hidden" name="to" value="$owner">'
1214 ' <input type="hidden" name="uid" value="$uid">'
1215 ' <input type="hidden" name="okey" value="$okey">'
1216 ' <input type="hidden" name="comment" value="DOX:SAVE=$udir">'
1217 ' <input type="hidden" name="filesize" value="">'
1218 ' <a id="uploadselect">'
1219 ' Select a'
1220 ' <button onclick="showfileupload()">local file for upload</button>'
1221 ' or select a'
1222 ' <button onclick="showdirectoryupload()">local directory for upload</button><br>'
1223 ' (If you upload a file named <code>dox.zip</code> it will be extracted, preserving the file attributes)'
1224 ' </a>'
1225 ' <a id="submit"></a>'
1226 '</form></ul>'
1227 ));
1228
1229 } else {
1230
1231 if ($akey and $folder !~ /_$vp$/) {
1232 $htmldoc .= "<script>\n$js</script>\n";
1233 }
1234
1235 $path = $ENV{PATH_INFO};
1236 $path =~ s:/+$::;
1237 my $action = "/$dox$path?ACTION";
1238 if ($akey and $file =~ m:(\Q$folder/.upload/\E[^/]+)$:) {
1239 if (countfiles($1)) {
1240 $htmldoc .= button("$action=publishall",'pubish all');
1241 $htmldoc .= ' &nbsp; ';
1242 }
1243 }
1244 if (@dirs or @files) {
1245 $htmldoc .= button("$action=listit",'list it');
1246 $htmldoc .= ' &nbsp; ' . button("$action=zipit",'zip it');
1247 if ($user =~ /@/ and checkaddress($user)) {
1248 $htmldoc .= ' &nbsp; ' . button("$action=fexit",'fex it');
1249 }
1250 if ($akey or $dir eq $folder) {
1251 $htmldoc .= ' &nbsp; ' . button("$action=syncit",'sync it');
1252 }
1253 $htmldoc .= ' &nbsp; ' . button("$action=streamit",'stream it');
1254 $htmldoc .= ' &nbsp; ' . button("/$doxo/$file",'reload');
1255 }
1256 if ($akey) {
1257 $htmldoc .= ' &nbsp; ' . button("/$doxo$realm",'folders overview');
1258 $htmldoc .= ' &nbsp; ' . button("/$dox?ACTION=logout",'logout');
1259 $htmldoc .= ' &nbsp; ';
1260 $htmldoc .= qq'[<a href="/DOX/index.html">documentation</a>]'
1261 } elsif ($user =~ /@/ and checkaddress($user)) {
1262 $htmldoc .= qq' &nbsp; ';
1263 my $wopen = "window.open(this.href,'fup','width=1024,height=768');".
1264 "return false;";
1265 $htmldoc .= qq'<a href="$action=sendfile" onclick="$wopen" $stdn>'.
1266 '<button>send file</button></a>';
1267 if (-d "$folder/.upload/$user") {
1268 $htmldoc .= qq' &nbsp; ';
1269 $htmldoc .= qq'<a href="/$doxo/$folder/UPLOAD">'.
1270 '<button>upload</button></a>';
1271 }
1272 $htmldoc .= qq' &nbsp; ';
1273 $htmldoc .= qq'[<a href="/DOX/user.html">documentation</a>]'
1274 } else {
1275 $htmldoc .= qq' &nbsp; ';
1276 $htmldoc .= qq'[<a href="/DOX/user.html">documentation</a>]'
1277 }
1278 $htmldoc .= "\n";
1279 $htmldoc .= "<h2><tt>/$pathhref/</tt></h2>\n";
1280 $htmldoc .= "<p><hr><p>\n";
1281
1282 $path = urldecode($ENV{PATH_INFO});
1283 # $htmldoc .= "<h1>[$ENV{PATH_INFO}]</h1>\n";
1284 $path =~ s:/.+?/::; # remove owner
1285 $path =~ s:/+$::;
1286 # $htmldoc .= "<h2>$path</h2>\n";
1287 if ($_ = slurp("$path/.#.html")||slurp("$path/#.html")) {
1288 s:</body.*::si;
1289 s:</html.*::si;
1290 s:<(script):&lt;$1:g;
1291 $htmldoc .= $_."\n<p><hr><p>\n";
1292 }
1293
1294 if ($akey and "$path/" !~ m:^\Q$folder/.fexdox/:) {
1295 $htmldoc .= qqq(qq(
1296 '<ul><li><form name="newdir"'
1297 ' action="/$dox"'
1298 ' method="POST"'
1299 ' accept-charset="$charset"'
1300 ' enctype="multipart/form-data">'
1301 ' <input type="hidden" name="parentdir" value="$udir">'
1302 ' <input type="submit" value="Create"> new directory'
1303 ' <input type="text" name="newdir" size="40">'
1304 '</form></ul>'
1305 '<p>'
1306 ));
1307 }
1308
1309 if (@files or @dirs) {
1310 if ($akey) {
1311 # $htmldoc .= "(see below for action on selected files or directories)\n";
1312 } elsif (@files) {
1313 # $htmldoc .= "(see below for action on selected files)\n";
1314 }
1315 $htmldoc .= qqq(qq(
1316 '<form name="files"'
1317 ' action="/$doxo/$file"'
1318 ' method="POST"'
1319 ' accept-charset="$charset"'
1320 ' enctype="multipart/form-data">'
1321 ));
1322 }
1323
1324 # first the (sub)directories
1325 if (my $n = scalar(@dirs)) {
1326 $htmldoc .= "<h3>Directories ($n)</h3>\n";
1327 $htmldoc .= "<pre>\n";
1328 $htmldoc .= "<table border=0>\n";
1329
1330 foreach my $d (asort(@dirs)) {
1331 $htmldoc .= "<tr><td>";
1332 if ($akey) {
1333 if ($dir eq $folder and ($d eq '.fexdox' or $d eq '.upload')) {
1334 } else {
1335 $htmldoc .= sprintf
1336 '<input type="checkbox" name="file" value="%s"> ',
1337 urlencode($d);
1338 }
1339 $htmldoc .= "<td>";
1340 }
1341 my $format = "&nbsp; <b><a href=\"%s\">%s/</a></b>\n";
1342 if (excluded("$path/$d")) {
1343 $format = "# <b><a href=\"%s\"><em>%s/</em></a></b>\n";
1344 }
1345 $htmldoc .= sprintf "<td $ar>%9s files<td>$format</tr>\n",
1346 d3(countfiles("$dir/$d")),
1347 urlencode($d),
1348 htmlencode($d);
1349 }
1350 $htmldoc .= "</table>\n";
1351 $htmldoc .= "</pre>\n";
1352 }
1353
1354 # then the files
1355 if (my $n = scalar(@files)) {
1356 $htmldoc .= "<h3>Files ($n)</h3>\n";
1357 }
1358 $htmldoc .= "<pre>\n";
1359 foreach my $f (asort(@files)) {
1360 my $format = " <a href=\"%s\">%s</a>\n";
1361 if (excluded("$path/$f")) {
1362 $format = "# <a href=\"%s\"><em>%s</em></a>\n";
1363 }
1364 $format = "%s %20s bytes $format";
1365 my $action = '';
1366 if ($akey) {
1367 if ($path eq "$folder/.fexdox") {
1368 $action = "?ACTION=edit";
1369 } elsif ("$dir/$f" eq "$folder/.upload/log") {
1370 $action = "?ACTION=logview";
1371 }
1372 }
1373 my $uf = urlencode($f);
1374 my $hf = htmlencode($f);
1375 $htmldoc .= qq'<input type="checkbox" name="file" value="$uf"> ';
1376 $htmldoc .= sprintf $format,
1377 isodate(mtime("$dir/$f")),d3(-s "$dir/$f"||0),
1378 $uf.$action,$hf;
1379 }
1380 $htmldoc .= "</pre>\n";
1381 if (@files or @dirs and $akey) {
1382 if ($path ne "$folder/.upload") {
1383 my $x = $akey ? 'and directories' : '';
1384 $htmldoc .= qqq(qq(
1385 '<input type="checkbox" onClick="toggle_checkboxes(this)">'
1386 '(de)select all files $x<br>\n'
1387 ));
1388 }
1389 $htmldoc .= "&nbsp; &#8627; Action for selected files and directories:\n";
1390 if ($akey) {
1391 if ($path eq "$folder/.upload") {
1392 $htmldoc .= qq'<input type="submit" name="submit" value="delete">\n';
1393 } else {
1394 my (@folders,@dirs,$selection);
1395 @folders = grep { -d and -l and $_ ne $folder } glob '*';
1396 $selection = '<select name="destination" size="1">';
1397 $selection .= "<option>$_</option>" foreach asort(@folders);
1398 $selection .= "</select>";
1399 $htmldoc .=
1400 qq'<script>\n'.
1401 qq'function showcopy() {'.
1402 qq" document.getElementById('actions').innerHTML = '".
1403 qq' <input type="submit" name="submit" value="copy">'.
1404 qq" to folder $selection'; ".
1405 qq"}\n".
1406 qq'</script>\n';
1407 foreach (find($folder)) {
1408 if (-d and not -l) {
1409 $_ .= '/';
1410 next if $_ eq "$file/";
1411 next if m:^\Q$folder/.\E(fexdox|upload)/|/\.versions/:;
1412 push @dirs,"/$_";
1413 }
1414 }
1415 $selection = '<select name="destination" size="1">'.
1416 "<option>/$folder/</option>";
1417 foreach (asort(@dirs)) {
1418 local $_ = htmlquote($_);
1419 $selection .= "<option>$_</option>";
1420 }
1421 $selection .= "</select>";
1422 $htmldoc .=
1423 qq'<script>\n'.
1424 qq'function showmove() {'.
1425 qq" document.getElementById('actions').innerHTML = '".
1426 qq' <input type="submit" name="submit" value="move">'.
1427 qq" to directory $selection'; ".
1428 qq"}\n".
1429 qq'</script>\n';
1430 $htmldoc .= qq'<a id="actions">\n';
1431 $htmldoc .= qq'<input type="submit" name="submit" value="zip it">\n';
1432 $htmldoc .= qq'<input type="submit" name="submit" value="fex it">\n';
1433 if (@folders) {
1434 $htmldoc .= qq'<button onclick="showcopy()">copy</button>\n';
1435 }
1436 if ($folder !~ /_$vp$/) {
1437 if (@dirs) {
1438 $htmldoc .= qq'<button onclick="showmove()">move</button>\n';
1439 }
1440 if ("$path/" =~ m:^\Q$folder/.upload/:) {
1441 $htmldoc .= qq'<input type="submit" name="submit" value="publish">\n';
1442 }
1443 if ("$path/" !~ m:^\Q$folder/.fexdox/:) {
1444 $htmldoc .= qq'<input type="submit" name="submit" value="rename">\n';
1445 }
1446 $htmldoc .= qq'<input type="submit" name="submit" value="delete">\n';
1447 }
1448 $htmldoc .= "</a>\n";
1449 }
1450 } else {
1451 $htmldoc .= qq'<input type="submit" name="submit" value="zip it">\n';
1452 if ($user =~ /@/ and checkaddress($user)) {
1453 $htmldoc .= qq'<input type="submit" name="submit" value="fex it">\n';
1454 }
1455 }
1456 $htmldoc .= "</form>\n";
1457 }
1458 if ($akey and $folder !~ /_$vp$/) {
1459 $htmldoc .= "<ul>\n";
1460 if ("$path/" !~ m:^\Q$folder/.upload/:) {
1461 $htmldoc .= qqq(qq(
1462 '<li><form name="upload"'
1463 ' action="/fup"'
1464 ' method="POST"'
1465 ' accept-charset="$charset"'
1466 ' enctype="multipart/form-data"'
1467 ' onsubmit="return showstatus()">'
1468 ' <input type="hidden" name="from" value="$owner">'
1469 ' <input type="hidden" name="to" value=".">'
1470 ' <input type="hidden" name="uid" value="$uid">'
1471 ' <input type="hidden" name="comment" value="DOX:SAVE=$udir">'
1472 ' <input type="hidden" name="filesize" value="">'
1473 ' <a id="uploadselect">'
1474 ' Select a'
1475 ' <button onclick="showfileupload()">local file for upload</button>'
1476 ));
1477 if ("$path/" !~ m:^\Q$folder/.fexdox/:) {
1478 $htmldoc .= qqq(qq(
1479 ' or select a'
1480 ' <button onclick="showdirectoryupload()">local directory for upload</button><br>'
1481 ' (If you upload a file named <code>dox.zip</code> it will be extracted, preserving the file attributes)'
1482 ));
1483 }
1484 $htmldoc .= qqq(qq(
1485 ' </a>'
1486 ' <a id="submit"></a>'
1487 '</form>'
1488 ));
1489 if ("$path/" !~ m:^\Q$folder/.fexdox/:) {
1490 $htmldoc .= qqq(qq(
1491 '<li><form name="mirror"'
1492 ' action="/$doxo/$dir"'
1493 ' method="POST"'
1494 ' accept-charset="$charset"'
1495 ' enctype="multipart/form-data">'
1496 ' <input type="submit" value="Mirror"> this URL:'
1497 ' <input type="text" name="mirror" size="80">'
1498 ' </form>'
1499 ));
1500 }
1501 }
1502 if ($path eq $folder or $path eq "$folder/.fexdox") {
1503 my @users = ();
1504 if (open my $users,"$folder/.fexdox/users") {
1505 while (<$users>) {
1506 /^#/ and next;
1507 s/\s//g;
1508 push @users,$1 if /^([^\/]+?@[\w.-]+):./;
1509 }
1510 close $users;
1511 }
1512 if (@users) {
1513 $htmldoc .= "<li>Manage your ";
1514 $htmldoc .= button("/$doxo/$folder/.upload",
1515 'user upload directories');
1516 $htmldoc .= "\n";
1517 }
1518 }
1519 $htmldoc .= "</ul>\n";
1520 if ("$path/" !~ m:^\Q$folder/.\E(fexdox|upload)/:) {
1521 $htmldoc .= qqq(qq(
1522 '<p><hr><p>'
1523 'URL of this page (for your users):<br>'
1524 ));
1525 foreach (@durl) {
1526 local $_ = $_;
1527 s:/fop.*:$ENV{REQUEST_URI}<br>\n:;
1528 if ($ENV{PROTO} eq 'https' and /^https/ or $ENV{PROTO} eq 'http') {
1529 $htmldoc .= $_;
1530 }
1531 }
1532 }
1533 }
1534 }
1535 }
1536
1537 if ($dir eq "$folder/.fexdox") {
1538 my $url = $ENV{REQUEST_URL};
1539 $url =~ s:/\.fexdox.*::;
1540 $htmldoc .= qqq(qq(
1541 '<p><hr><p>'
1542 'URL for your users:<br>'
1543 '$url/'
1544 ));
1545 }
1546
1547 $htmldoc .= "</body>\n</html>\n";
1548
1549 $size = length($htmldoc);
1550 # $size = length(encode_utf8($htmldoc));
1551 doxlog(hexencode($dir).'/ '.$size);
1552
1553 http_header(
1554 '200 OK',
1555 "Content-Length: $size",
1556 );
1557 if ($charset =~ /utf/i) {
1558 # binmode(STDOUT,':raw');
1559 print $htmldoc;
1560 } else {
1561 # binmode(STDOUT,':raw');
1562 print $htmldoc;
1563 }
1564 }
1565
1566
1567 sub countfiles {
1568 my $dir = shift;
1569 my $n = 0;
1570 local $_;
1571
1572 # return 0;
1573
1574 # $dir = shellquote($dir);
1575 # my @files = `find $dir -type f`;
1576 # return scalar(@files);
1577
1578 opendir $dir,$dir or return $n;
1579 while (defined($_ = readdir $dir)) {
1580 next if /^\.\.?$/;
1581 $_ = "$dir/$_";
1582 lstat;
1583 next if -l _;
1584 if (-d _) {
1585 $n += countfiles($_);
1586 } elsif (-f _) {
1587 $n++;
1588 }
1589 }
1590 closedir $dir;
1591 return $n;
1592 }
1593
1594
1595 sub edit {
1596 my $file = shift;
1597 my $path = dirname($file);
1598 my $text = '';
1599 my ($htmldoc);
1600 local $/;
1601
1602 open $file,$file or http_die("$file - $!\n");
1603 $text = <$file>;
1604 close $file;
1605 $text =~ s/\r//g;
1606 $text =~ s/&/&amp;/g;
1607 $text =~ s/</&lt;/g;
1608 $text =~ s/\n*$/\n/;
1609
1610 $htmldoc = html_header($head);
1611 $htmldoc .= qqq(qq(
1612 '<h2>DOX of $owner</h2>'
1613 '<h3>edit /$file :</h3>'
1614 '<form name="edit"'
1615 ' action="/$doxo/$file"'
1616 ' method="POST"'
1617 ' accept-charset="$charset"'
1618 ' enctype="multipart/form-data">'
1619 ' <textarea name="filedata" cols="80" rows="25">'
1620 ));
1621 $htmldoc .= $text;
1622 $htmldoc .= qqq(qq(
1623 '</textarea><br>'
1624 '<input type="submit" name="submit" value="save">'
1625 '</form>'
1626 ));
1627 $htmldoc .= button('javascript:history.back()','cancel');
1628
1629 my $size = length(encode_utf8($htmldoc));
1630 http_header('200 OK',"Content-Length: $size");
1631 print $htmldoc;
1632 exit;
1633 }
1634
1635
1636 sub logview {
1637 my $file = shift;
1638 my $path = dirname($file);
1639 my $text = '';
1640 my @log = ();
1641 my $htmldoc;
1642 local $/ = "\n\n";
1643 local $_;
1644
1645 open $file,$file or http_die("$file - $!\n");
1646 @log = <$file>;
1647 close $file;
1648
1649 $htmldoc = html_header($head);
1650 $htmldoc .= "<h2>$file</h2>\n";
1651 $htmldoc .= "<pre>\n";
1652
1653 while (defined($_ = pop @log)) {
1654 s/^\n*//;
1655 s/\n*$/\n\n/;
1656 $htmldoc .= htmlquote($_);
1657 }
1658
1659 $htmldoc .= qqq(qq(
1660 '</pre>'
1661 '$back'
1662 '</body></html>'
1663 ));
1664
1665 my $size = length($htmldoc);
1666 http_header('200 OK',"Content-Length: $size");
1667 # binmode(STDOUT,':raw');
1668 print $htmldoc;
1669 exit;
1670 }
1671
1672
1673 sub button {
1674 my $href = shift;
1675 my $button = shift;
1676 return qq'<a href="$href" $stdn><button>$button</button></a>';
1677 }
1678
1679
1680 sub mkfexdox {
1681 my $folder = shift;
1682 my $fexdox = "$folder/.fexdox";
1683 my ($config,$info);
1684
1685 unlink($fexdox) unless -d $fexdox;
1686 mkdirp($fexdox);
1687 mkdir "$folder/.upload";
1688
1689 $info = "$fexdox/#.html";
1690 if (not -f $info and open $info,'>',$info) {
1691 my $href = qq'<a href="$hosturl/DOX/#Folders_and_configuration">help</a>';
1692 print {$info} "<h3>Folder configuration, see $href</h3>\r\n";
1693 close $info;
1694 }
1695
1696 $config = "$fexdox/config";
1697 unless (-f $config) {
1698 open $config,'>',$config or http_die("cannot write $config - $!");
1699 print {$config}
1700 "# Lines beginning with # are comments.\r\n",
1701 "\r\n",
1702 "# access for other users\r\n",
1703 "#useraccess=no\r\n",
1704 "useraccess=yes\r\n",
1705 "#useraccess=anonymous\r\n",
1706 "\r\n",
1707 "# number of backups fexdox automatically creates and keeps\r\n",
1708 "backups=1\r\n",
1709 "\r\n",
1710 "# file name character set\r\n",
1711 "charset=utf-8\r\n",
1712 "\r\n",
1713 "# show delete button or not\r\n",
1714 "#showdelete=no\r\n",
1715 "#showdelete=backup\r\n",
1716 "showdelete=all\r\n",
1717 "";
1718 close $config;
1719 }
1720
1721 $config = "$fexdox/users";
1722 unless (-f $config) {
1723 open $config,'>',$config or http_die("cannot write $config - $!");
1724 print {$config}
1725 "# Lines beginning with # are comments.\r\n",
1726 "#\r\n",
1727 "# User accounts are USER:PASSWORD strings.\r\n",
1728 "# If the user shall be enabled to upload files, it must be an email address.\r\n",
1729 "# Example:\r\n",
1730 "# framstag\@rus.uni-stuttgart.de:letthereberock\r\n",
1731 "";
1732 close $config;
1733 }
1734
1735 $config = "$fexdox/access";
1736 unless (-f $config) {
1737 open $config,'>',$config or http_die("cannot write $config - $!");
1738 print {$config}
1739 "# Lines beginning with # are comments.\r\n",
1740 "#\r\n",
1741 "# Access is restricted to ips or ip ranges listed here.\r\n",
1742 "127.0.0.1\r\n",
1743 "0.0.0.0-255.255.255.255\r\n",
1744 "0:0:0:0:0:0:0:0-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\r\n",
1745 "";
1746 close $config;
1747 }
1748
1749 $config = "$fexdox/private";
1750 unless (-f $config) {
1751 open $config,'>',$config or http_die("cannot write $config - $!");
1752 print {$config}
1753 "# Lines beginning with # are comments.\r\n",
1754 "#\r\n",
1755 "# List of private files and directories to be invisible for users.\r\n",
1756 "# Files and directories with . or # as first character are private by default.\r\n",
1757 "# You can use the shell patterns * ? [...]\r\n",
1758 "# Examples:\r\n",
1759 "#\r\n",
1760 "# internal.pdf\r\n",
1761 "# *.tmp\r\n",
1762 "# /software/test/alpha\r\n",
1763 "";
1764 close $config;
1765 }
1766 }
1767
1768
1769 sub list {
1770 my $dir = shift;
1771 my $files = ();
1772 my $htmldoc = html_header($head);
1773 local $_;
1774
1775 foreach (find($dir)) {
1776 next if -l or not -f;
1777 next if not $akey and excluded($_);
1778 push @files,$_;
1779 }
1780
1781 $htmldoc .= qqq(qq(
1782 '<html>'
1783 '<body>'
1784 '<h1>/$dir/</h1>'
1785 '<pre>'
1786 ));
1787
1788 foreach (asort(@files)) {
1789 if (m:\Q$dir\E/(.+)/(.+):) {
1790 my $hd = htmlencode($1);
1791 my $ud = urlencode("/$doxo/$dir/$1");
1792 my $hf = htmlencode($2);
1793 my $uf = urlencode("/$doxo/$dir/$1/$2");
1794 $htmldoc .= sprintf
1795 qq'%s %20s bytes <a href="%s">%s</a>/<a href="%s">%s</a>\n',
1796 isodate(mtime($_)),d3(-s||0),$ud,$hd,$uf,$hf;
1797 } elsif (m:\Q$dir\E/(.+):) {
1798 my $hf = htmlencode($1);
1799 my $uf = urlencode("/$doxo/$dir/$1");
1800 $htmldoc .= sprintf
1801 qq'%s %20s bytes <a href="%s">%s</a>\n',
1802 isodate(mtime($_)),d3(-s||0),$uf,$hf;
1803 }
1804 }
1805
1806 $htmldoc .= qqq(qq(
1807 '<p>'
1808 '$back'
1809 '</body>'
1810 '</html>'
1811 ));
1812
1813 my $size = length($htmldoc);
1814 doxlog(hexencode($dir).'/ '.$size);
1815
1816 http_header(
1817 '200 OK',
1818 "Content-Length: $size",
1819 );
1820 # binmode(STDOUT,':raw');
1821 print $htmldoc;
1822 }
1823
1824
1825 sub userlist {
1826 my $dir = shift;
1827 my %files = ();
1828 local $_;
1829
1830 unless (-d "$dir/.upload/$user") {
1831 http_error(403);
1832 }
1833
1834 foreach (find($dir)) {
1835 next if -l or not -f;
1836 next if m:/[.#]|~$:;
1837 next if excluded($_);
1838 $files{$1} = $_ if m:\Q$dir/\E(.+):;
1839 }
1840
1841 foreach (find("$dir/.upload/$user")) {
1842 next if -l or not -f;
1843 $files{$1} = $_ if m:\Q$dir/.upload/$user/\E(.+):;
1844 }
1845
1846 http_header(
1847 '200 OK',
1848 "Content-Type: text/plain",
1849 );
1850
1851 # print "$dir/.upload/$user\n";
1852 foreach (asort(keys %files)) {
1853 printf "%d %s\n",mtime($files{$_}),hexencode($_);
1854 }
1855 }
1856
1857
1858 sub zipdir {
1859 my $dir = shift;
1860 my $zip = $dir;
1861 my @zip = qw'zip -r';
1862 my $uf = my $cf = 1;
1863 my @exclude = ();
1864
1865 $zip =~ s:[^\w\@%~^,+-]:_:g;
1866 $zip .= sprintf "_%s.zip",time;
1867
1868 foreach (find($dir)) {
1869 unless ($akey) {
1870 if (-l or excluded($_)) {
1871 $_ .= '/*' if -d and not -l;
1872 push @exclude,$_;
1873 next;
1874 }
1875 }
1876 next if -l or not -f;
1877 if (/\.($utypes)$/i) {
1878 $cf += -s;
1879 } else {
1880 $uf += -s;
1881 }
1882 }
1883
1884 if ($uf+$cf > 2**30 or $uf/$cf < 0.2 and $uf+$cf > 2**24) {
1885 push @zip,'-0';
1886 }
1887
1888 http_header('200 OK');
1889
1890 pq(qq(
1891 '<html>'
1892 '<body>'
1893 '<pre>'
1894 ));
1895 push @zip,$zip,$dir;
1896 push @zip,'-x',@exclude if not $akey and @exclude;
1897 doxlog("[@zip]");
1898 if ($akey) {
1899 print htmlquote("\$ @zip\n");
1900 }
1901 unlink $zip;
1902 if (open my $zip,'-|',@zip) {
1903 while (<$zip>) {
1904 # print $scroll,htmlencode(decode_utf8($_));
1905 print $scroll,htmlencode($_);
1906 }
1907 close $zip;
1908 }
1909 print "</pre>\n";
1910 if (my $s = -s $zip) {
1911 $s = int($s/$MB);
1912 print qq'Download <a href="/$dox/$owner/$zip">$zip</a> ($s MB)\n';
1913 }
1914 pq(qq(
1915 '<p>'
1916 '$back'
1917 '$scroll'
1918 '</body>'
1919 '</html>'
1920 ));
1921
1922 }
1923
1924
1925 sub zipfiles {
1926 my $files = @_;
1927 my $dir = $file;
1928 my $zip = basename($dir);
1929 my @zip = qw'zip';
1930 my $uf = my $cf = 1;
1931 local $_;
1932
1933 $zip =~ s:[^\w\@%~^,+-]:_:g;
1934 $zip .= sprintf "_%s.zip",time;
1935
1936 map { $_="$dir/$_" } @files;
1937
1938 unless ($akey) {
1939 foreach my $file (@files) {
1940 if (excluded($file)) { http_die("no access for $file") }
1941 if (-d $file) { http_die("cannot fex $file/") }
1942 }
1943 }
1944
1945 foreach (@files) {
1946 if (my $size = -s) {
1947 if (/\.($utypes)$/i) {
1948 $cf += $size;
1949 } else {
1950 $uf += $size;
1951 }
1952 }
1953 }
1954
1955 if ($uf+$cf > 2**30 or $uf/$cf < 0.2 and $uf+$cf > 2**24) {
1956 push @zip,'-0';
1957 }
1958
1959 http_header('200 OK');
1960
1961 pq(qq(
1962 '<html>'
1963 '<body>'
1964 '<pre>'
1965 ));
1966 if ($akey) { push @zip,'-r' }
1967 else { push @zip,'-j' }
1968 push @zip,$zip,@files;
1969 doxlog("[@zip]");
1970 if ($akey) {
1971 print htmlquote("\$ @zip\n");
1972 }
1973 unlink $zip;
1974 if (open $zip,'-|',@zip) {
1975 while (<$zip>) {
1976 print $scroll,htmlencode($_);
1977 }
1978 close $zip;
1979 }
1980 print "</pre>\n";
1981 if (my $s = -s $zip) {
1982 $s = int($s/$MB);
1983 pq(qq(
1984 'Download <a href="/$dox/$owner/$zip">$zip</a> ($s MB)'
1985 '$scroll\n'
1986 ));
1987 }
1988 pq(qq(
1989 '<p>'
1990 '$back'
1991 '</body>'
1992 '</html>'
1993 ));
1994 }
1995
1996
1997 sub fexdir {
1998 my $dir = shift;
1999 my $recipient = shift;
2000 my $fexsend = "fexsend -q -C 'DOX $owner'";
2001
2002 unless ($recipient =~ /@/ and checkaddress($recipient)) {
2003 http_die("$recipient is not a valid email address");
2004 }
2005
2006 nvt_print(
2007 'HTTP/1.1 200 OK',
2008 'Server: fexsrv',
2009 "Content-Type: text/html; charset=$charset",
2010 '',
2011 );
2012
2013 pq(qq(
2014 '<html>'
2015 '<body>'
2016 '<pre>'
2017 ));
2018 if ($akey) {
2019 # $fexsend .= " -A $dir.zip $dir/* $dir/.??*";
2020 $fexsend .= " -a $dir.zip $dir";
2021 } else {
2022 my $X = '.\*';
2023 foreach my $x (asort(keys %exclude)) {
2024 $x =~ s/([?*])/\\$1/g;
2025 $x =~ s/([^\w\/.-])/\\$1/g;
2026 $X .= "#$x";
2027 }
2028 $fexsend .= " -# $X -a $dir.zip $dir/*";
2029 }
2030 $fexsend .= " $recipient";
2031 doxlog("[$fexsend]");
2032 print "\$ $fexsend\n";
2033 if (open $fexsend,"$fexsend 2>&1|") {
2034 while (<$fexsend>) {
2035 print $scroll,htmlencode($_);
2036 }
2037 close $fexsend;
2038 }
2039 pq(qq(
2040 '</pre>'
2041 '$back'
2042 '$scroll'
2043 '</body>'
2044 '</html>'
2045 ));
2046 }
2047
2048
2049 sub fexfiles {
2050 my $recipient = pop;
2051 my @files = @_;
2052 my $dir = $file;
2053 my $zip = shellquote(basename($dir)).'.zip';
2054 my $fexsend;
2055
2056 if ($recipient =~ /@/ and checkaddress($recipient)) {
2057 http_header('200 OK');
2058 } else {
2059 http_die("$recipient is not a valid email address");
2060 }
2061
2062 chdir $dir or http_die("$dir - $!");
2063
2064 unless ($akey) {
2065 foreach my $file (@files) {
2066 if (excluded("$dir/$file")) { http_die("no access for $file") }
2067 if (-d $file) { http_die("cannot fex $file/") }
2068 }
2069 }
2070
2071 pq(qq(
2072 '<html>'
2073 '<body>'
2074 '<pre>'
2075 ));
2076 map { s/[\'\\]/_/g } @files;
2077 $fexsend = "fexsend -q -C 'DOX $owner : @files' ";
2078 $fexsend .= $akey ? '-A' : '-a';
2079 $fexsend .= " $zip @files $recipient";
2080 doxlog("[$fexsend]");
2081 print "\$ $fexsend\n";
2082 if (open $fexsend,"$fexsend 2>&1|") {
2083 while (<$fexsend>) {
2084 print $scroll,htmlencode($_);
2085 }
2086 close $fexsend;
2087 }
2088 pq(qq(
2089 '</pre>'
2090 '$back'
2091 '$scroll'
2092 '</body>'
2093 '</html>'
2094 ));
2095 }
2096
2097
2098 sub delfiles {
2099 my @files = @_;
2100 my $dir = $file;
2101 local $_;
2102
2103 unless ($akey or "$file/" =~ m:^\Q$folder/.upload/$user/:) {
2104 http_die("no permission");
2105 }
2106
2107 chdir $dir or http_die("$dir - $!");
2108
2109 foreach my $file (@files) {
2110 unless (-f $file or -d $file) {
2111 http_die("no such file or directory $dir/$file")
2112 }
2113 }
2114
2115 foreach my $file (@files) {
2116 if (-f $file) {
2117 unlink($file);
2118 } elsif (-d $file) {
2119 unlink("$file!publish") if $dir =~ m:/\.upload$: and $file =~ /@/;
2120 rmrf($file);
2121 }
2122 }
2123
2124 chdir "$spooldir/$owner/DOX" or die $!;
2125 update_folder($folder) if $file !~ m:^\Q$folder/.upload/:
2126 }
2127
2128
2129 sub movefiles {
2130 my @files = @_;
2131 my $dir = $file;
2132
2133 unless ($akey) { http_die("no permission") }
2134
2135 unless (-d $destination) {
2136 http_die("destination /$destination is not a directory")
2137 }
2138
2139 map { $_="$dir/$_" } @files;
2140
2141 my $errors = move(@files,$destination);
2142 update_folder($folder);
2143 if ($errors) {
2144 $errors = htmlquote($errors);
2145 http_header('200 OK');
2146 print html_header($head);
2147 pq(qq(
2148 '<h1>move error</h1>'
2149 '$errors'
2150 '</body>'
2151 '</html>'
2152 ));
2153 &reexec;
2154 }
2155 }
2156
2157
2158 sub copyfiles {
2159 my @files = @_;
2160 my $dir = $file;
2161
2162 unless ($akey) { http_die("no permission") }
2163
2164 foreach my $file (@files) {
2165 unless (-f "$dir/$file" or -d "$dir/$file") {
2166 http_die("no such file or directory $dir/$file")
2167 }
2168 }
2169
2170 if ($folder =~ /^\Q$destination\E_$vp$/) {
2171 # use same source directory if coming from same folder other version
2172 $destination = $dir;
2173 $destination =~ s/_$vp//;
2174 mkdirp($destination);
2175 } else {
2176 # add source folder name if coming from foreigner folder
2177 $destination .= "/$folder";
2178 $destination =~ s/!backup$//;
2179 unless (-d $destination) {
2180 mkdir $destination or http_die("cannot mkdir $destination - $!");
2181 }
2182 }
2183
2184 foreach my $file (@files) {
2185 rmrf("$destination/$file");
2186 if (-f "$dir/$file") {
2187 link "$dir/$file","$destination/$file";
2188 } else {
2189 system qw'cp -al',"$dir/$file","$destination/$file";
2190 }
2191 }
2192 update_folder($1) if $destination =~ m:([^/]+):;
2193 }
2194
2195
2196 sub savefile {
2197 my $file = shift;
2198
2199 unless ($akey) {
2200 http_die("no permission")
2201 }
2202
2203 if ($file =~ m:^/: or "/$file/" =~ m:\Q/../:) {
2204 http_die("illegal file name $file");
2205 }
2206
2207 unless (-f $file) {
2208 http_die("/$file does not exist");
2209 }
2210
2211 my $qfile = shellquote($file);
2212 system "$FEXHOME/bin/vv -s $qfile 2>/dev/null";
2213 open $file,'>',$file or http_die("cannot write $file - $!");
2214 print {$file} $filedata;
2215 close $file;
2216 }
2217
2218
2219 sub queryrename {
2220 my @files = @_;
2221 my $dir = urlencode($file);
2222 unless ($akey) { http_die("no permission") }
2223
2224 http_header('200 OK');
2225 print html_header($head);
2226 pq(qq(
2227 '<h1>/$dir/</h1>'
2228 '<form name="rename"'
2229 ' action="/$doxo/$dir"'
2230 ' method="POST"'
2231 ' accept-charset="$charset"'
2232 ' enctype="multipart/form-data">'
2233 '<table border=0>'
2234 '<tr><th align="left">old name<th>&rarr;<th align="left">new name</tr>'
2235 ));
2236
2237 foreach my $file (@files) {
2238 print "<tr>";
2239 printf '<td>%s<input type="hidden" name="renamefrom" value="%s">',
2240 htmlencode($file),htmlquote($file);
2241 print "<td>&rarr;";
2242 printf '<td><input type="text" name="renameto" value="%s" size="60">',
2243 htmlquote($file);
2244 print "</tr>\n";
2245 }
2246
2247 pq(qq(
2248 '</table>'
2249 '<input type="submit">'
2250 '</form>'
2251 '</body>'
2252 '</html>'
2253 ));
2254 exit;
2255 }
2256
2257
2258 sub renamefiles {
2259 my @files = @_;
2260 my $dir = decode_utf8($file);
2261 my $i = 0;
2262
2263 unless ($akey) { http_die("no permission") }
2264
2265 if (scalar(@renamefrom) != scalar(@renameto)) {
2266 http_die("rename: different parameter numbers")
2267 }
2268
2269 chdir $dir or http_die("$dir - $1");
2270
2271 # http_header('200 OK');
2272 # print html_header($head);
2273 # printf "<h1>/%s/</h1><pre>\n",htmlquote($dir);
2274 # foreach my $old (@renamefrom) {
2275 # printf "[%s] &rarr; [%s]\n",htmlquote($old),htmlquote($renameto[$i]);
2276 # $i++;
2277 # }
2278 # exit;
2279
2280 foreach my $rf (@renamefrom) {
2281 my $rt = $renameto[$i];
2282 if ($rf ne $rt and not lstat $rt and length($rt)) {
2283 rename($rf,$rt);
2284 }
2285 $i++;
2286 }
2287
2288 }
2289
2290
2291 sub update_folder {
2292 my $folder = shift;
2293 my @d = localtime time;
2294 my $lv = readlink($folder) or return;
2295 my $nv = sprintf(
2296 '%s_%d%02d%02d_%02d%02d%02d',
2297 $folder,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]
2298 );
2299
2300 rename untaint($lv),$nv or return;
2301 unlink $folder;
2302 symlink $nv,$folder;
2303 if (opendir my $dox,'.') {
2304 while (defined($_ = readdir $dox)) {
2305 if (-d and not -l and /$vp/) {
2306 if (/^((\Q$folder\E_$vp)!backup)$/ and -d $2) {
2307 rmrf($1);
2308 } elsif (/^((\Q$folder\E_$vp)!backup)$/) {
2309 rename $1,$2;
2310 }
2311 }
2312 }
2313 closedir $dox;
2314 }
2315 }
2316
2317
2318 sub sendfile {
2319 my $dir = shift;
2320 my $okey = randstring(8);
2321 my $okeyd = "$spooldir/$owner/\@OKEY";
2322 my $url = $ENV{REQUEST_URL};
2323
2324 unless ($user =~ /@/ and checkaddress($user)) {
2325 http_die("$user is not a valid email address");
2326 }
2327
2328 mkdir $okeyd;
2329 symlink $user,"$okeyd/$okey"
2330 or http_die("cannot create OKEY $okeyd/$okey - $!");
2331
2332 $url =~ s:\?.*::;
2333 my $comment = urlencode($url);
2334 $url =~ s:/$dox/.*:/fup?to=$owner&okey=$okey&comment=$comment:;
2335
2336 redirect($url);
2337 exit;
2338 }
2339
2340
2341 sub okey {
2342 my $dir = shift;
2343 my $okey = randstring(8);
2344 my $okeyd = "$spooldir/$owner/\@OKEY";
2345
2346 unless ($user =~ /@/ and checkaddress($user)) {
2347 http_die("$user is not a valid email address");
2348 }
2349
2350 mkdir $okeyd;
2351 symlink $user,"$okeyd/$okey"
2352 or http_die("cannot create OKEY $okeyd/$okey - $!");
2353
2354 nvt_print(
2355 'HTTP/1.0 200 OK',
2356 "Server: fexsrv",
2357 "X-okey: $okey",
2358 "Content-Type: text/plain",
2359 ''
2360 );
2361
2362 print "okey=$okey\n";
2363 exit;
2364 }
2365
2366
2367 sub notify_owner {
2368 my $upload = "$folder/.upload/$user";
2369 my @files = ();
2370 my ($mfrom,$hfrom) = mhfrom($user);
2371 my $header = qqq(qq(
2372 'From: <$mfrom> ($hfrom via F*EX service $hostname)'
2373 'To: $owner'
2374 'Subject: DOX upload /$folder/'
2375 'MIME-Version: 1.0'
2376 'Content-Type: text/plain; charset=UTF-8'
2377 'Content-Transfer-Encoding: 8bit'
2378 ));
2379 local $_;
2380
2381 chdir $upload or http_die("$owner : $upload - $!");
2382
2383 foreach (asort(find("."))) {
2384 next if -l or not -f;
2385 push @files,"$1\n" if m:^\./(.+):;
2386 }
2387
2388 # explicite sender set in fex.ph?
2389 if ($sender_from) {
2390 # obsoleted feature!
2391 map { s/^From: <\Q$user/From: <$sender_from/ } $header;
2392 open $sendmail,'|-',$sendmail,$owner,$bcc
2393 or http_die("cannot start sendmail - $!");
2394 } else {
2395 # for special remote domains do not use same domain in From,
2396 # because remote MTA will probably reject this e-mail
2397 my $dfrom = $1 if $user =~ /@(.+)/;
2398 my $dto = $1 if $owner =~ /@(.+)/;
2399 if ($dfrom and $dto and @remote_domains and
2400 grep { $dfrom =~ /(^|\.)$_$/ and $dto =~ /(^|\.)$_$/ } @remote_domains)
2401 {
2402 $header =~ s/(From: <)\Q$user\E(.*?)\n/$1$admin$2\nReply-To: $user\n/;
2403 open $sendmail,'|-',$sendmail,$owner,$bcc
2404 or http_die("cannot start sendmail - $!");
2405 } else {
2406 open $sendmail,'|-',$sendmail,'-f',$mfrom,$owner,$bcc
2407 or http_die("cannot start sendmail - $!");
2408 }
2409 }
2410
2411 my @body = (
2412 "$doxurl$folder/.upload/$user\n",
2413 "\n",
2414 @files
2415 );
2416 print {$sendmail} $header,"\n",@body;
2417
2418 if (close $sendmail) {
2419 http_header('200 OK');
2420 print html_header($head);
2421 pq(qq(
2422 '<h2>Notification email has been sent to $owner</h2>'
2423 '<a href="/$doxo/$folder/UPLOAD">back to DOX</a>'
2424 ));
2425 } else {
2426 http_die("cannot send notification email (sendmail error $!)");
2427 }
2428
2429 }
2430
2431
2432 sub mirror {
2433 my $bytes = 0;
2434 my ($quota,$du) = check_recipient_quota($owner);
2435 if ($quota and $du > $quota) {
2436 http_die("$owner is overquota");
2437 }
2438 chdir $file or http_die("$owner : $file - $!");
2439 my $wget='wget -e robots=off -w 1 -nv -nH -np -m';
2440 $mirror .= '/' if $mirror =~ m:.*/[^./]+$:;
2441 $mirror = untaint(shellquote($mirror));
2442 open $wget,"$wget $mirror 2>&1|" or http_die("cannot run wget - $!");
2443
2444 http_header('200 OK');
2445 print html_header($head);
2446 print "<h1>Mirroring to /$file/</h1>\n";
2447 print "<pre>\n";
2448
2449 while (<$wget>) {
2450 if (/URL:(.+(\d+)\]) -> /) {
2451 $bytes += $2;
2452 printf "%s%s\n",$scroll,htmlquote($1) or exit;
2453 if ($quota and $du+$bytes/$MB > $quota) {
2454 pq(qq(
2455 '</pre>'
2456 '<h1>Overquota! ($quota MB)</h1>'
2457 '$scroll'
2458 '</body></html>'
2459 ));
2460 exit;
2461 }
2462 }
2463 }
2464
2465 pq(qq(
2466 '</pre>'
2467 '<a href="/$doxo/$file/">back to DOX</a>'
2468 '</body>'
2469 '</html>'
2470 ));
2471 exit;
2472 }
2473
2474
2475 sub publish {
2476 my @files = @_;
2477 my $sdir = $file;
2478 my $ddir = $sdir;
2479
2480 unless ($akey) { http_die("no permission") }
2481 unless (-d $sdir) { http_die("unknown upload directory $sdir") }
2482
2483 map { $_ = "$sdir/$_" } @files;
2484 $ddir =~ s:^\Q$folder/.upload/\E[^/]+(.*):$folder$1:;
2485 mkdirp($ddir);
2486 publishlog(@files);
2487 my $errors = move(@files,$ddir);
2488 update_folder($folder);
2489 if ($errors) {
2490 $errors = htmlquote($errors);
2491 http_header('200 OK');
2492 print html_header($head);
2493 pq(qq(
2494 '<h1>move error</h1>'
2495 '$errors'
2496 '</body>'
2497 '</html>'
2498 ));
2499 &reexec;
2500 }
2501 }
2502
2503
2504 sub publish_all {
2505 my $dir = shift;
2506 my @files = ();
2507 my $user = '';
2508 local $_;
2509
2510 if (-d $dir and $dir =~ m:^\Q$folder/.upload/\E([^/]+)$:) {
2511 $user = $1;
2512 } else {
2513 http_die("unknown upload directory $dir");
2514 }
2515
2516 foreach (glob "$dir/*") {
2517 next if -l;
2518 foreach (asort(find($_))) {
2519 push @files,$_ if -f and not -l;
2520 }
2521 }
2522 publishlog(@files) if @files;
2523
2524 @files = glob "$dir/*" or return;
2525 map { /(.+)/; $_ = $1 } @files;
2526
2527 my $errors = move(@files,$folder);
2528 update_folder($folder);
2529
2530 if ($errors) {
2531 $errors = htmlquote($errors);
2532 http_header('200 OK');
2533 print html_header($head);
2534 pq(qq(
2535 '<h1>move error</h1>'
2536 '$errors'
2537 '</body>'
2538 '</html>'
2539 ));
2540 exit;
2541 }
2542 }
2543
2544
2545 sub syncdir {
2546 my $dir = shift;
2547 my @fexsync = ('fexsync');
2548
2549 unless ($akey) {
2550 if ($dir ne $folder) {
2551 http_die('syncing not allowed for users');
2552 }
2553 my $exclude = '\.[^/]*';
2554 foreach (asort(values %exclude)) {
2555 s:^/+::;
2556 $exclude .= '|'.$_;
2557 }
2558 foreach (find($dir)) {
2559 if (-l) {
2560 s:^$folder/:./:;
2561 $exclude .= '|'.quotemeta($_);
2562 }
2563 }
2564 push @fexsync,'-X',$exclude;
2565 }
2566
2567 nvt_print(
2568 'HTTP/1.1 200 OK',
2569 'Server: fexsrv',
2570 "Content-Type: text/html; charset=$charset",
2571 '',
2572 );
2573
2574 pq(qq(
2575 '<html>'
2576 '<body>'
2577 ));
2578 push @fexsync,$dir;
2579 doxlog("[@fexsync]");
2580 print "\$ @fexsync\n" if $akey;
2581 local $ENV{FEXHOME} = '';
2582 local $ENV{FEXCOMMENT} = "$owner/DOX/$dir";
2583 local $ENV{FEXID} = '';
2584 local $ENV{FEXXX} = '';
2585 local $ENV{FEXSERVER} = '';
2586 if (open my $fexsync,'-|',@fexsync) {
2587 while (<$fexsync>) {
2588 if (/to start sync receiving without FEXID/) {
2589 print "<h3>To start sync receiving run:</h3>\n";
2590 print "<pre>\n";
2591 $_ = <$fexsync>;
2592 s/:\d+/:$ENV{PORT}/;
2593 print;
2594 last;
2595 }
2596 }
2597 while (<$fexsync>) {
2598 s!^server/user:.*!!;
2599 last if /^Recipient: /;
2600 print $scroll,htmlencode($_);
2601 }
2602 while (<$fexsync>) { }
2603 close $fexsync;
2604 }
2605 pq(qq(
2606 '</pre>'
2607 '<script>history.back()</script>'
2608 '</body>'
2609 '</html>'
2610 ));
2611 }
2612
2613
2614 sub streamdir {
2615 my $dir = shift;
2616 my $stream = $dir;
2617 my @tar = qw'tar -c';
2618 my ($sex,$tar,$data,$sexsend);
2619
2620 $SIG{ALRM} = sub {
2621 $SIG{__DIE__} = 'DEFAULT';
2622 die "<h2>TIMEOUT<h2>\n";
2623 };
2624 alarm($timeout*10);
2625
2626 if ($akey) {
2627 if (-l $dir) {
2628 push @tar,qw'-f -',"$dir/.";
2629 } else {
2630 push @tar,qw'-f -',$dir;
2631 }
2632 } else {
2633 push @tar,"--exclude=.*";
2634 foreach (asort(keys %exclude)) {
2635 s:^/+::;
2636 push @tar,"--exclude=$_";
2637 }
2638 foreach (find($dir)) {
2639 push @tar,"--exclude=$_" if -l;
2640 }
2641 if (-l $dir) {
2642 push @tar,qw'-f -',map {$_=untaint($_)} glob("$dir/*");
2643 } else {
2644 push @tar,qw'-f -',$dir;
2645 }
2646 }
2647
2648 my $idf = "$ENV{HOME}/.fex/id";
2649 open $idf,$idf or http_die("no $idf");
2650 my $url = <$idf>;
2651 my $fexuser = <$idf>;
2652 close $idf;
2653 unless ($fexuser) {
2654 http_die("no fexuser in $idf");
2655 }
2656 chomp $url;
2657 chomp $fexuser;
2658
2659 nvt_print(
2660 'HTTP/1.1 200 OK',
2661 'Server: fexsrv',
2662 "Content-Type: text/html; charset=$charset",
2663 '',
2664 );
2665
2666 pq(qq(
2667 '<html>'
2668 '<body>'
2669 ));
2670 $stream =~ s/[^\w\@.+-]/_/g;
2671 $stream .= ':'.randstring(8);
2672 $sex = "@tar | sexsend public $stream";
2673 doxlog("[$sex]");
2674 my $sexurl = "$url/sex?user=$fexuser&ID=public&stream=$stream";
2675 print "\$ $sex\n" if $akey;
2676 pq(qq(
2677 "<h3>To start stream receiving run:</h3>"
2678 "<pre>"
2679 "sexget $url/$fexuser:public $stream | tar xvf -"
2680 ""
2681 "wget -O- '$sexurl' | tar xvf -"
2682 ""
2683 "curl '$sexurl' | tar xvf -"
2684 "</pre>"
2685 ));
2686 local $ENV{FEXHOME} = '';
2687 local $ENV{FEXID} = '';
2688 local $ENV{FEXXX} = '';
2689 local $ENV{FEXSERVER} = '';
2690 open $sexsend,"|sexsend public $stream >/dev/null" or exit;
2691 open $tar,'-|',@tar or exit;
2692 while (read($tar,$data,$bs)) {
2693 print {$sexsend} $data or exit;
2694 alarm($timeout*10);
2695 }
2696 close $sexsend;
2697 close $tar;
2698 pq(qq(
2699 '<script>history.back()</script>'
2700 '</body>'
2701 '</html>'
2702 ));
2703 }
2704
2705
2706 sub backup_cleanup {
2707 my $folder = shift;
2708 my $current = readlink($folder) or return;
2709
2710 foreach my $v (grep /^\Q$folder\E_$vp$/,glob "*") {
2711 if ($v ne $current and -d $v and -d "$v!backup") {
2712 rmrf(untaint("$v!backup"));
2713 }
2714 }
2715 }
2716
2717
2718 sub d3 {
2719 local $_ = shift;
2720 while (s/(\d)(\d\d\d\b)/$1,$2/) {};
2721 return $_;
2722 }
2723
2724
2725 sub http_date {
2726 my $file = shift;
2727 my @stat;
2728
2729 if (@stat = stat($file)) {
2730 return strftime("%a, %d %b %Y %T GMT",gmtime($stat[9]));
2731 } else {
2732 return 0;
2733 }
2734 }
2735
2736
2737 # test if file is excluded (private)
2738 sub excluded {
2739 my $file = shift;
2740 $file =~ s://+:/:;
2741 $file =~ s:^/::;
2742 $file =~ s:/$::;
2743 $file =~ s:^\Q$folder/::;
2744 foreach my $p (keys %exclude) {
2745 my $pp = $exclude{$p};
2746 $pp = '.*/'.$pp if $pp !~ m:^/:;
2747 return 1 if "/$file" =~ m:^$pp(/.*)?$:;
2748 }
2749 }
2750
2751
2752 sub reldir {
2753 local $_ = '/'.shift.'/';
2754 while (s:/\.\./:/__/:) {}
2755 s://+:/:g;
2756 m:/(.+)/:;
2757 return $1;
2758 }
2759
2760
2761 sub redirect {
2762 my $uri = urlencode(shift);
2763
2764 $uri =~ s/%3F/?/g;
2765 $uri =~ s/%26/&/g;
2766
2767 nvt_print(
2768 "HTTP/1.1 302 Found",
2769 "Location: $uri",
2770 'Content-Length: 0',
2771 ''
2772 );
2773 # control back to fexsrv for further HTTP handling
2774 &reexec;
2775 }
2776
2777
2778 sub doxlog {
2779 my $msg = shift;
2780 my $ra = $RA||'-';
2781
2782 $ra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
2783 $ra =~ s/\s//g;
2784 $msg = sprintf("%s %s %s %s %s\n",isodate(time),$ra,$owner,$user,$msg);
2785 writelog('dox.log',$msg);
2786 }
2787
2788
2789 sub publishlog {
2790 my @files = @_;
2791 my @d = localtime time;
2792 my $log;
2793 local $_;
2794
2795 mkdir "$folder/.upload";
2796 $log = "$folder/.upload/log";
2797 open $log,'>>',$log or return;
2798 flock($log,LOCK_EX);
2799
2800 printf {$log}
2801 "\nPUBLISH %d-%02d-%02d %02d:%02d:%02d %s %s %s\n",
2802 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0],
2803 $user,$ENV{REMOTE_HOST}||'-',$ENV{REMOTE_ADDR}||'.';
2804 foreach (asort(@files)) {
2805 s:/+:/:g;
2806 s:^\Q$folder/::;
2807 s:^\Q.upload/::;
2808 print {$log} "$1\n" if /(.+)/;
2809 }
2810 close $log;
2811 }
2812
2813
2814 sub showenv {
2815 http_header('200 OK');
2816 foreach my $v (asort(keys %ENV)) {
2817 printf "%s = '%s'\n",$v,$ENV{$v};
2818 }
2819 }
2820
2821
2822 sub shellquote {
2823 local $_ = shift;
2824 s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
2825 return $_;
2826 }
2827
2828
2829 # parse GET and POST requests
2830 sub parse_request {
2831 my ($boundary);
2832 my $QS = $ENV{QUERY_STRING};
2833 local $_;
2834
2835 # parse HTTP QUERY_STRING (parameter=value pairs)
2836 if ($QS) {
2837 $QS =~ s/\?/&/g if $QS !~ /&/ and $QS =~ /\?\w+=/;
2838 foreach (split '&',$QS) {
2839 if (s/^(\w+)=(.*)//) {
2840 my $p = uc($1);
2841 my $v = $2;
2842 # decode URL-encoding
2843 $v =~ s/%([a-f0-9]{2})/chr(hex($1))/gie;
2844 setparam($p,$v);
2845 }
2846 }
2847 }
2848
2849 if ($ENV{REQUEST_METHOD} eq 'POST') {
2850 $SIG{ALRM} = sub {
2851 $SIG{__DIE__} = 'DEFAULT';
2852 die "TIMEOUT\n";
2853 };
2854 alarm($timeout);
2855 # binmode(STDIN,':raw');
2856
2857 if (($ENV{CONTENT_TYPE}||'') =~ /boundary=\"?([^\s\";,]+)/) {
2858 $boundary = $1;
2859 } else {
2860 http_die("malformed HTTP POST (no boundary found)");
2861 }
2862
2863 while (&nvt_read) {
2864 alarm($timeout);
2865 if (/^Content-Disposition:\s*form-data;\s*name="([a-z]\w*)"/i) {
2866 my $name = $1;
2867 my $data = '';
2868 nvt_skip_to('^\s*$');
2869 while (&nvt_read) {
2870 alarm($timeout);
2871 last if /^--\Q$boundary/;
2872 $data .= $_."\n";
2873 }
2874 setparam($name,$data);
2875 }
2876 last if /^--\Q$boundary--/;
2877 }
2878 }
2879
2880 alarm(0);
2881 }
2882
2883
2884
2885 # set CGI parameter variables
2886 sub setparam {
2887 my ($v,$vv) = @_;
2888 my ($idf,$to);
2889 local $_;
2890
2891 $v = uc(despace($v));
2892
2893 chomp($vv);
2894 $PARAM{$v} = $vv;
2895 if ($v eq 'LOGOUT' and $akey) {
2896 unlink "$akeydir/$akey";
2897 my $login = $FEXHOME.'/cgi-bin/login';
2898 if (-x $login) {
2899 $login = readlink($login) || 'login';
2900 nvt_print(
2901 "HTTP/1.1 302 Found",
2902 "Location: $hosturl/$login",
2903 'Content-Length: 0',
2904 ""
2905 );
2906 } else {
2907 nvt_print(
2908 "HTTP/1.1 302 Found",
2909 "Location: $hosturl/fup",
2910 'Content-Length: 0',
2911 ""
2912 );
2913 }
2914 &reexec;
2915 } elsif ($v eq 'SUBMIT') {
2916 $submit = lc($vv);
2917 } elsif ($v eq 'DIRECTORY') {
2918 # $directory = reldir(decode_utf8($vv));
2919 $directory = reldir($vv);
2920 } elsif ($v eq 'DESTINATION') {
2921 # $destination = reldir(decode_utf8(urldecode($vv)));
2922 $destination = reldir(urldecode($vv));
2923 } elsif ($v eq 'PARENTDIR') {
2924 # $parentdir = reldir(decode_utf8(urldecode($vv)));
2925 $parentdir = reldir(urldecode($vv));
2926 } elsif ($v eq 'UPLOADDIR' and $vv =~ /^($mrx)$/) {
2927 $uploaddir = $1;
2928 } elsif ($v eq 'NEWDIR' and $vv =~ /^\s*([^\/]+?)\s*$/) {
2929 # $newdir = decode_utf8(urldecode($1));
2930 $_ = urldecode($1);
2931 s:[\\/]:_:g;
2932 $newdir = $_;
2933 } elsif ($v eq 'NEWFOLDER' and $vv =~ /^\s*(.+?)\s*$/) {
2934 $_ = $1;
2935 s/[^a-z_\d\@~^,.+-]/_/gi;
2936 s/^\./_/;
2937 $newfolder = $_;
2938 } elsif ($v eq 'ALLOWPUBLISH' and $vv =~ /^($mrx)$/) {
2939 $allowpublish = $1;
2940 } elsif ($v eq 'ACTION' and $vv =~ /^([\w:]+)$/) {
2941 $action = $1;
2942 } elsif ($v eq 'MIRROR' and $vv =~ /\.[a-z]/i) {
2943 $mirror = urldecode($vv);
2944 $mirror =~ s/\s//g;
2945 $mirror =~ s/[<>|]/_/g;
2946 } elsif ($v eq 'FILE' and $vv =~ /^([^\/]+)$/) {
2947 # push @files,decode_utf8(urldecode($1));
2948 $_ = urldecode($1);
2949 s:[\\/]:_:g;
2950 push @files,$_;
2951 } elsif ($v eq 'RENAMEFROM' and $vv =~ /^\s*(.*?)\s*$/) {
2952 local $_ = $1;
2953 s:/:_:g;
2954 # push @renamefrom,decode_utf8(urldecode($_));
2955 push @renamefrom,urldecode($_);
2956 } elsif ($v eq 'RENAMETO' and $vv =~ /^\s*(.*?)\s*$/) {
2957 $_ = urldecode($1);
2958 s:[\n\r]: :g;
2959 s:[\\/]:_:g;
2960 # push @renameto,decode_utf8($_);
2961 push @renameto,$_;
2962 } elsif ($v eq 'FILEDATA') {
2963 $filedata = $vv;
2964 } else {
2965 http_die("unknown or empty parameter $v");
2966 }
2967 }