blib/lib/Apache2/FileManager.pm untested

filter: Statementstmt 0.0% 0 / 362 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 51 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 607 CC 47 SCAR 36.3
CC
47
Cov
0%
html_file_list
182.0
cmd_paste
72.0
cmd_rsync
42.0
CRAP 37.9
1 package Apache2::FileManager;
2  
3 =head1 NAME
4  
5 Apache2::FileManager - Apache2 mod_perl File Manager
6  
7 =head1 SYNOPSIS
8  
9 # Install in mod_perl enabled apache conf file
10 <Location /FileManager>
11 SetHandler perl-script
12 PerlHandler Apache2::FileManager
13 </Location>
14  
15 (Then point your browser to http://www.yourwebsite.com/FileManager)
16  
17 # Or call from your own mod_perl script
18 use Apache2::FileManager;
19 my $obj = Apache2::FileManager->new();
20 $obj->print();
21  
22 # Or create your own custom MyFileManager subclass
23  
24 package MyFileManager;
25 use strict;
26 use Apache2::FileManager;
27  
28 our @ISA = ('Apache2::FileManager');
29  
30 sub handler {
31 my $r = shift;
32 my $obj = __PACKAGE__->new();
33 $r->content_type('text/html');
34 $r->print ("
35 <HTML>
36 <HEAD>
37 <TITLE>".$r->hostname." File Manager</TITLE>
38 </HEAD>
39 ");
40 $obj->print();
41 $r->print("</HTML>");
42 }
43  
44 # .. overload the methods ..
45  
46  
47 =head1 DESCRIPTION
48  
49 The Apache2::FileManager module is a simple HTML file manager. It provides
50 file manipulations such as cut, copy, paste, delete, rename, extract archive,
51 create directory, create file, edit file, and upload files.
52  
53 Apache2::FileManager also has the ability to rsync the server htdocs tree to
54 another server with the click of a button.
55  
56  
57 =head1 PREREQUISITES
58  
59 The following (non-core) perl modules must be installed before installing
60 Apache2::FileManager.
61  
62 Apache/mod_perl => 2.0
63 Archive::Any => 0.03
64 CGI::Cookie => 1.20
65 File::NCopy => 0.32
66 File::Remove => 0.20
67  
68 =head1 SPECIAL NOTES
69  
70 Make sure the web server has read, write, and execute access access to the
71 directory you want to manage files in. Typically you are going to want to
72 run the following commands before you begin.
73  
74 chown -R nobody /web/xyz/htdocs
75 chmod -R 755 /web/xyz/htdocs
76  
77 The extract functionality only works with *.tar.gz and *.zip files.
78  
79 =head1 RSYNC FEATURE
80  
81 To use the rync functionality you must have ssh, rsync, and the L<File::Rsync>
82 perl module installed on the development server. You also must have an sshd
83 running on the production server.
84  
85 Make sure you always fully qualify your server names so you don't have
86 different values in your known hosts file.
87  
88 For Example:
89 ssh my-machine - wrong
90 ssh my-machine.subnet.com - right
91  
92 Note: If the ip address of the production_server changes you will need to
93 create a new known_hosts file.
94  
95 To get the rsync feature to work do the following:
96  
97 #1 log onto the production server
98  
99 #2 become root
100  
101 #3 give web server user (typically nobody) a home area
102 I made mine /usr/local/apache/nobody
103 - production_server> mkdir /usr/local/apache/nobody
104 - edit passwd file and set new home area for nobody
105 - production_server> mkdir /usr/local/apache/nobody/.ssh
106  
107 #4 log onto the development server
108  
109 #5 become root
110  
111 #6 give web server user (typically nobody) a home area
112 - dev_server> mkdir /usr/local/apache/nobody
113 - dev_server> chown -R nobody.nobody /usr/local/apache/nobody
114 - edit passwd file and set new home area for nobody
115 - dev_server> su - nobody
116 - dev_server> ssh-keygen -t dsa (don't use passphrase)
117 - dev_server> ssh production_server
118 (will fail but will make known_hosts file)
119 - log out from user nobody back to root user
120 - dev_server> cd /usr/local/apache/nobody/.ssh
121 - dev_server> scp id_dsa.pub production_server:/usr/local/apache/nobody/.ssh/authorized_keys
122 - dev_server> chown -R nobody.nobody /usr/local/apache/nobody
123 - dev_server> chmod -R 700 /usr/local/apache/nobody
124  
125 #7 log back into the production server
126  
127 #8 become root
128  
129 #9 Do the following commands:
130 - production_server> chown -R nobody.nobody /usr/local/apache/nobody
131 - production_server> chmod -R 700 /usr/local/apache/nobody
132  
133 You also need to specify the production server in the development server's
134 web conf file. So your conf file should look like this:
135  
136 <Location /FileManager>
137 SetHandler perl-script
138 PerlHandler Apache2::FileManager
139 PerlSetVar RSYNC_TO production_server:/web/xyz
140 </Location>
141  
142 If your ssh path is not /usr/bin/ssh or /usr/local/bin/ssh, you also need to
143 specify the path in the conf file or in the contructor with the directive
144 SSH_PATH.
145  
146 You can also specify RSYNC_TO in the constructor:
147  
148 my $obj = Apache2::FileManager->new({
149 RSYNC_TO => "production_server:/web/xyz"
150 });
151  
152 Also make sure /web/xyz and all files in the tree are readable, writeable, and
153 executable by nobody on both the production server AND the development server.
154  
155 =head1 USING DIFFERENT DOCUMENT ROOT
156  
157 You can specify a different document root as long as the new document root
158 falls inside of the orginal document root. For example if the document root
159 of a web server is /web/project/htdocs, you could assign the document root to
160 also be /web/project/htdocs/newroot. The directory `newroot` must exist.
161  
162 # Specify different document root in apache conf file
163 <Location /FileManager>
164 SetHandler perl-script
165 PerlHandler Apache2::FileManager
166 PerlSetVar DOCUMENT_ROOT /web/project/htdocs/newroot
167 </Location>
168  
169 # Or specify different document root in your own mod_perl script
170 use Apache2::FileManager;
171 my $obj = Apache2::FileManager->new({
172 DOCUMENT_ROOT => '/web/project/htdocs/newroot'
173 });
174 $obj->print();
175  
176 =head1 SUBCLASSING Apache2::FileManager
177  
178 # Create a new file with the following code:
179  
180 package MyProject::MyFileManager;
181 use strict;
182 use Apache2::FileManager;
183 our @ISA = ('Apache2::FileManager');
184  
185 #Add your own methods here
186  
187 1;
188  
189 The best way to subclass the filemanager would be to copy the methods you want
190 to overload from the Apache2::FileManager file to your new subclass. Then change
191 the methods to your liking.
192  
193 =head1 BUGS
194  
195 There is a bug in L<File::NCopy> that occurs when trying to paste an empty
196 directory. The directory is copied but reports back as 0 directories pasted.
197 The author is in the process of fixing the problem.
198  
199 =head1 AUTHOR
200  
201 L<Apache::FileManager> was written by
202 Philip Collins E<lt>pmc@cpan.orgE<gt>.
203  
204 L<Apache2::FileManager> was adapted for Apache2 by
205 David Aguilar E<lt>davvid@cpan.orgE<gt>.
206  
207 =cut
208  
209 use strict;
210 use warnings;
211 use Apache2::Log ();
212 use Apache2::Util ();
213 use Apache2::Const -compile => qw(OK DECLINED);
214 use Apache2::Request ();
215 use Apache2::RequestIO ();
216 use Apache2::RequestRec ();
217 use Apache2::RequestUtil ();
218 use Apache2::ServerUtil ();
219 use Apache2::Upload;
220 use IO::File;
221 use File::NCopy qw(copy);
222 use File::Copy qw(move);
223 use File::Remove qw(remove);
224 use File::stat;
225 use Archive::Any;
226 use POSIX qw(strftime);
227 use CGI::Cookie;
228 #use Data::Dumper;
229  
230 require 5.005_62;
231  
232 our $VERSION = '0.20';
233  
234 sub r { return Apache2::Request->new(Apache2::RequestUtil->request) }
235  
236 # ---------- Object Constructor -----------------------------------------
237 sub new {
238 my $package = shift;
239 my $attribs = shift || {};
240 my $o = bless $attribs, $package;
241 $o->intialize();
242 $o->execute_cmds();
243 return $o;
244 }
245  
246  
247 # ---- If this was called directly via a perl content handler by apache -------
248 sub handler {
249 my $r = Apache2::Request->new(@_);
250 return Apache2::Const::DECLINED if defined r->param('nossi');
251 my $package = __PACKAGE__;
252 my $obj = $package->new();
253 r->content_type('text/html');
254 r->print("<HTML><HEAD><TITLE>"
255 .r->hostname." File Manager $VERSION</TITLE></HEAD>");
256 $obj->print();
257 r->print("</HTML>");
258 return Apache2::Const::OK;
259 }
260  
261  
262 # ---- Call the view ----------------------------------------------
263 sub print {
264 my $o = shift;
265  
266 my $view = "view_".$$o{'view'};
267 $o->$view();
268 }
269  
270  
271 # ------------ Intialize object -----------------------------------------
272 sub intialize {
273 my $o = shift;
274  
275 $$o{MESSAGE} = "";
276 $$o{JS} = "";
277 $$o{EDIT_COLS} ||= 75;
278 $$o{EDIT_ROWS} ||= 22;
279  
280  
281 # Is this filemanager rsync capable?
282 $$o{RSYNC_TO} ||= r->dir_config('RSYNC_TO') || undef;
283  
284 #set some defaults (for warnings sake)
285  
286 $$o{FILEMANAGER_cmd} = r->param('FILEMANAGER_cmd') || "";
287 $$o{FILEMANAGER_arg} = r->param('FILEMANAGER_arg') || "";
288 $$o{FILEMANAGER_curr_dir} = r->param('FILEMANAGER_curr_dir') || "";
289 $$o{FILEMANAGER_sel_files} => r->param('FILEMANAGER_sel_files') || [];
290  
291 #document root
292 my $dr = r->document_root;
293 $$o{DR} ||= r->dir_config('DOCUMENT_ROOT') || r->document_root;
294  
295 #does user defined document root lie inside real doc root?
296 if ($$o{DR} !~ /^$dr/) {
297 $$o{DR} = r->document_root;
298 r->log_error("Warning: Document root changed to $dr.".
299 " Custom document root must lie inside of ".
300 "real document root.");
301 }
302  
303 #verify current working directory
304 $_ = r->param('FILEMANAGER_curr_dir');
305 s/\.\.//g; s/^\///; s/\/$//;
306 my $curr_dir = $_;
307  
308 #set current directory
309 if (! chdir $$o{DR}."/$curr_dir") {
310 chdir $$o{DR};
311 $curr_dir = "";
312 }
313  
314 $$o{FILEMANAGER_curr_dir} = $curr_dir;
315  
316 #set default view method
317 $$o{'view'} = "filemanager";
318  
319 return undef;
320 }
321  
322  
323  
324  
325  
326 ###############################################################################
327 # ----- Views --------------------------------------------------------------- #
328 ###############################################################################
329  
330 #after upload files - view
331 sub view_post_upload {
332 r->print(q{
333 <SCRIPT>
334 window.opener.document.FileManager.submit();
335 window.opener.focus();
336 window.close();
337 </SCRIPT>
338 });
339 return undef;
340 }
341  
342  
343 #after rsync transacation - view
344 sub view_post_rsync {
345 my $o = shift;
346 r->print(qq{
347 <CENTER>
348 <TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>
349 <TR><TD>$$o{MESSAGE}</TD></TR>
350 <TR>
351 <FORM>
352 <TD ALIGN=RIGHT>
353 <INPUT TYPE=BUTTON VALUE='close'
354 onclick="window.close();">
355 </TD>
356 </FORM>
357 </TR>
358 </TABLE>
359 </CENTER>
360 });
361 return undef;
362 }
363  
364  
365 sub view_filemanager {
366 my $o = shift;
367  
368 my ($location, $up_a_href) = $o->html_location_toolbar();
369 $up_a_href ||= "";
370  
371 my $message = "<I><FONT COLOR=#990000>".$$o{MESSAGE}."</FONT></I>";
372 r->print("
373 <!-- Scripts -->
374 ".$o->html_javascript(r->hostname())."
375  
376 <!-- Styles -->
377 ".$o->html_style_sheet()."
378  
379 <FORM NAME=FileManager ACTION='".r->uri."' METHOD=POST>
380 ".$o->html_hidden_fields()."
381  
382 <!-- Header -->
383 ".$o->html_top()."
384  
385 <!-- Special message -->
386 $message
387  
388 <TABLE CELLPADDING=2 CELLSPACING=2
389 BORDER=0 WIDTH=100% BGCOLOR=#606060>
390 <TR><TD>".$o->html_cmd_toolbar()."</TD></TR>
391 <TR BGCOLOR=WHITE><TD>$location</TD></TR>
392 <TR><TD>".$o->html_file_list($up_a_href)."</TD></TR>
393 </TABLE>
394  
395 <!-- Footer -->
396 ".$o->html_bottom()."
397  
398 </FORM>
399 ");
400  
401 return undef;
402 }
403  
404  
405 sub view_pre_editfile {
406 my $o = shift;
407  
408 my $editfile = r->param('FILEMANAGER_editfile');
409 my $base = "http://".r->hostname."/$editfile";
410 $editfile =~ /([^\/]+)$/;
411 my $filename = $1;
412  
413 my $fh;
414 if (-T $filename && -w $filename) {
415 $fh = IO::File->new("< ".$filename);
416 }
417  
418 my $message = "";
419 if ($$o{MESSAGE}) {
420 $message =
421 "<I><FONT COLOR=#990000>".$$o{MESSAGE}."</FONT></I><BR>";
422 }
423  
424 if (! $fh) {
425 r->print("
426 <HTML>
427 <HEAD>
428 <BODY>
429 <CENTER><BR>$message
430  
431 <TABLE BORDER=1 CELLPADDING=10 CELLSPACING=0
432 BGCOLOR=#606060>
433 <TR BGCOLOR=WHITE>
434 <TD ALIGN=CENTER>
435 could not open file: <I>$base</I> in text writing mode
436 </TD>
437 </TR>
438 <TR BGCOLOR=#efefef>
439 <TD ALIGN=RIGHT>
440 <FORM>
441 <INPUT TYPE=BUTTON VALUE=close
442 onclick=\"
443 window.close();
444 return false;\">
445 </FORM>
446 </TD>
447 </TR>
448 </TABLE>
449  
450 <BR>
451 </CENTER>
452 </BODY>
453 </HTML>");
454 }
455  
456 else {
457  
458 my $data;
459 {
460 local $/=undef;
461 $data = scalar(<$fh>);
462 }
463  
464 r->print("
465 <SCRIPT>
466 function show_preview () {
467 var f = window.document.FileManagerEditFile;
468 var w = window.open('',
469 'FileManagerPreviewEditFile',
470 'scrollbars,resizable,menubar,status,toolbar');
471 var d = w.document.open();
472 d.write('<HTML><HEAD><BASE HREF=\"$base\"></HEAD>');
473 d.write(f.FILEMANAGER_filedata.value);
474 d.write('</HTML>');
475 d.close();
476 w.focus();
477 }
478 $$o{JS}
479 </SCRIPT>
480  
481 <!-- Styles -->
482 ".$o->html_style_sheet()."
483  
484 <FORM NAME=FileManagerEditFile
485 ACTION='".r->uri."'
486 METHOD=POST>
487  
488 ".$o->html_hidden_fields()."
489  
490 <INPUT TYPE=HIDDEN NAME=FILEMANAGER_editfile
491 VALUE=\"".r->param('FILEMANAGER_editfile')."\">
492  
493 <!-- Header -->
494 <TABLE WIDTH=100% CELLPADDING=0 CELLSPAING=0>
495 <TR>
496 <TD>
497 <FONT COLOR=#3a3a3a><B>$base</B></FONT>
498 </TD>
499 </TR>
500 </TABLE>
501  
502 <!-- Special message -->
503 $message
504  
505 <TABLE CELLPADDING=2 CELLSPACING=2
506 BORDER=0 WIDTH=100% BGCOLOR=#606060>
507  
508 <!-- Toolbar -->
509 <TR>
510 <TD ALIGN=CENTER>
511 <TABLE CELLPADDING=0 CELLSPACING=0
512 BORDER=0 WIDTH=90%>
513 <TR ALIGN=CENTER>
514 <TD ALIGN=CENTER>
515 <INPUT TYPE=BUTTON VALUE='cancel'
516 onclick=\"
517 window.close();
518 return false;\">
519 </TD>
520  
521 <TD ALIGN=CENTER>
522 <INPUT TYPE=BUTTON VALUE='preview'
523 onclick=\"
524 window.show_preview();
525 return false;\">
526 </TD>
527  
528 <TD ALIGN=CENTER>
529 <INPUT TYPE=BUTTON VALUE='save'
530 onclick=\"
531 var f = window.document.FileManagerEditFile;
532 f.FILEMANAGER_cmd.value = 'savefiledata';
533 f.submit();
534 return false;\">
535 </TD>
536 </TR>
537 </TABLE>
538 </TD>
539 </TR>
540  
541 <!-- file edit box -->
542 <TR>
543 <TD ALIGN=CENTER BGCOLOR=#efefef>
544 <TEXTAREA NAME=FILEMANAGER_filedata
545 COLS=$$o{EDIT_COLS}
546 ROWS=$$o{EDIT_ROWS}>$data</TEXTAREA>
547 </TD>
548 </TR>
549  
550 <!-- Toolbar -->
551 <TR>
552 <TD ALIGN=CENTER>
553 <TABLE CELLPADDING=0 CELLSPACING=0
554 BORDER=0 WIDTH=90%>
555 <TR ALIGN=CENTER>
556 <TD ALIGN=CENTER>
557 <INPUT TYPE=BUTTON VALUE='cancel'
558 onclick=\"
559 window.close();
560 return false;\">
561 </TD>
562  
563 <TD ALIGN=CENTER>
564 <INPUT TYPE=BUTTON VALUE='preview'
565 onclick=\"
566 window.show_preview();
567 return false;\">
568 </TD>
569  
570 <TD ALIGN=CENTER>
571 <INPUT TYPE=BUTTON VALUE='save'
572 onclick=\"
573 var f = window.document.FileManagerEditFile;
574 f.FILEMANAGER_cmd.value = 'savefiledata';
575 f.submit();
576 return false;\">
577 </TD>
578 </TR>
579 </TABLE>
580 </TD>
581 </TR>
582 </TABLE>
583 <!-- Footer -->
584 ".$o->html_bottom()."
585 </FORM>
586 ");
587 }
588  
589 return undef;
590 }
591  
592  
593  
594  
595  
596 ###############################################################################
597 # ---- HTML Component Output ------------------------------------------------ #
598 ###############################################################################
599  
600 sub html_javascript {
601 my $o = shift;
602  
603 my $hostname = r->hostname();
604 my $cookie_name = uc($hostname);
605 $cookie_name =~ s/[^A-Z]//g;
606 $cookie_name .= "_FM";
607  
608 #start return literal
609 return "
610 <NOSCRIPT>
611 <H1><FONT COLOR=#990000>please enable javascript</FONT></H1>
612 </NOSCRIPT>
613 <SCRIPT>
614 <!--
615 var cookie_name = '$cookie_name';
616  
617 function select_all () {
618 var f = window.document.FileManager;
619 var ck_stat;
620 var ar = get_ckbox_array();
621  
622 if (ar.length > 0) {
623 if (f.FILEMANAGER_last_select_all.value == '1') {
624 ck_stat = false;
625 f.FILEMANAGER_last_select_all.value = '0';
626 } else {
627 ck_stat = true;
628 f.FILEMANAGER_last_select_all.value = '1';
629 }
630 }
631  
632 for (var i=0; i < ar.length; i++) {
633 ar[i].checked = ck_stat;
634 }
635 }
636  
637 function display_help () {
638 var w=window.open('', 'help',
639 'resizable=yes,scrollbars=yes,width=650,height=650');
640 var d = w.document.open();
641 d.write(
642 \"<HTML> <UL><B><U><FONT SIZE=+1>Help</FONT></U></B><BR><BR>\"+
643  
644 \"<LI><A NAME=upload><B>How do I upload files?</B></A><BR>\"+
645 \"Click on the upload menu item. After the <I>Upload Files</I>\"+
646 \"window opens, click the <I>Browse</I> button. \"+
647 \"This will pop open another window showing files on your computer. \"+
648 \"Select a file you want to upload. You can not upload directories. \"+
649 \"If you want to upload a directory, archive it first into a \"+
650 \"<I>zip</I> file or a tarball. You will then be able to extract \"+
651 \"it on the server. You can upload up to 10 files at a time. \"+
652 \"After selecting the files you want to upload, click the \"+
653 \"<I>upload</I> button to transfer the files from your machine to \"+
654 \"the server.<BR><BR>\"+
655  
656 \"<LI><A NAME=move><B>How do I copy or move files?</B></A><BR>\"+
657 \"First click the check boxes next to the file names that you would \"+
658 \"like to copy or paste. Next click the <I>copy</I> or <I>paste</I> \"+
659 \"button. Then go to the directory you would like them pasted in. \"+
660 \"Finally, click <I>paste</I>.<BR><BR>\"+
661  
662 \"<LI><A NAME=move><B>Why does the file manager seem broken in \"+
663 \"certain directories or when copying or pasting certain files?\"+
664 \"</B></A><BR>\"+
665 \"This occurs when the file manager does not have permission to \"+
666 \"access these files. To fix the problem, contact your system \"+
667 \"administrator and ask them to grant the webserver \"+
668 \"READ, WRITE, and EXECUTE access to your files.<BR><BR>\"+
669  
670 \"</UL><CENTER>\"+
671 \"<FORM><INPUT TYPE=BUTTON VALUE='close' onclick='window.close();'>\"+
672 \"</FORM></CENTER></HTML>\");
673 d.close();
674 w.focus();
675 }
676  
677 function getexpirydate(nodays){
678 var UTCstring;
679 Today = new Date();
680 nomilli=Date.parse(Today);
681 Today.setTime(nomilli+nodays*24*60*60*1000);
682 UTCstring = Today.toUTCString();
683 return UTCstring;
684 }
685  
686 function getcookie(cookiename) {
687 var cookiestring=''+document.cookie;
688 var index1=cookiestring.indexOf(cookiename);
689 if (index1==-1 || cookiename=='') return '';
690 var index2=cookiestring.indexOf(';',index1);
691 if (index2==-1) index2=cookiestring.length;
692 escsubstring = cookiestring.substring(index1+cookiename.length+1, index2)
693 return unescape(escsubstring);
694 }
695  
696 function setcookie(name,value,duration){
697 cookiestring=name+'='+escape(value)+';EXPIRES='+getexpirydate(duration);
698 document.cookie=cookiestring;
699 if (!getcookie(name)) {
700 return false;
701 } else{
702 return true;
703 }
704 }
705  
706 function print_upload () {
707 var w = window.open('','FileManagerUpload',
708 'scrollbars=yes,resizable=yes,width=500,height=440');
709 var d = w.document.open();
710 d.write(\"<HTML><BODY><CENTER><H1>Upload Files</H1>\"+
711 \"<FORM NAME=UploadForm ACTION='".r->uri."' \"+
712 \" METHOD=POST onsubmit='window.opener.focus();' \"+
713 \" ENCTYPE=multipart/form-data>\"+
714 \" <INPUT TYPE=HIDDEN NAME=FILEMANAGER_curr_dir \"+
715 \" VALUE='".r->param('FILEMANAGER_curr_dir')."'>\");
716  
717 for (var i=1; i <= 10; i++) {
718 d.write(\"<INPUT TYPE=FILE SIZE=40 NAME=FILEMANAGER_file\"+i+\"><BR>\");
719 }
720 d.write(\"<INPUT TYPE=BUTTON VALUE='cancel' onclick='window.close();'>\"+
721 \"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"+
722 \"<INPUT TYPE=SUBMIT NAME=FILEMANAGER_cmd\"+
723 \" VALUE=upload></CENTER></BODY></HTML>\");
724 d.close();
725 w.focus();
726 }
727  
728 // make input check box form elements into an array ALL the time
729 function get_ckbox_array() {
730 var ar;
731 sel_files = window.document.FileManager.FILEMANAGER_sel_files;
732  
733 // no files
734 if (sel_files == null) {
735 ar = new Array();
736 }
737  
738 // 1 file (no length)
739 else if (sel_files.length == null) {
740 ar = [ sel_files ];
741 }
742  
743 // more than one file
744 else {
745 ar = sel_files;
746 }
747 return ar;
748 }
749  
750  
751 // make array of check box objects that are selected
752 function get_sel_ckbox_array() {
753 var sel_ar = new Array();
754 var ar = window.get_ckbox_array();
755  
756 for (var i=0; i < ar.length; i++) {
757 if (ar[i].checked == true) {
758 sel_ar.push(ar[i]);
759 }
760 }
761  
762 return sel_ar;
763 }
764  
765  
766  
767  
768 // get the number checked
769 function get_num_checked() {
770 var sel_ar = get_sel_ckbox_array();
771 return sel_ar.length;
772 }
773  
774 //function to edit file
775 function edit_file () {
776 var sel_ar = get_sel_ckbox_array();
777  
778 //make sure there is 1 and only 1 selected file
779 if (sel_ar.length != 1) {
780 window.alert('Please select ONE file to edit by clicking on a '+
781 'check box with the mouse.');
782 }
783  
784 else {
785 var f= window.document.FileManager;
786 var cd = escape(f.FILEMANAGER_curr_dir.value);
787 var editfile = escape(sel_ar[0].value);
788 var w = window.open('".r->uri."?FILEMANAGER_cmd=editfile".
789 "&FILEMANAGER_curr_dir='+cd+'".
790 "&FILEMANAGER_editfile='+editfile,
791 'FileManagerEditFile',
792 'scrollbars,resizable');
793 sel_ar[0].checked = false;
794 w.focus();
795 }
796 }
797  
798 // make cookie for checked filenames
799 function save_names (type) {
800 var cb = get_sel_ckbox_array();
801 var ac = '';
802 for (var i=0; i < cb.length; i++) {
803 ac = ac + cb[i].value + '|';
804 cb[i].checked = false;
805 }
806 if (ac == '') {
807 window.alert('Please select file(s) by clicking on the '+
808 'check boxes with the mouse.');
809 } else {
810 ac = ac + type;
811 window.setcookie(cookie_name,ac,1);
812 }
813 }
814  
815 //test if browser cookies are enabled
816 if (! window.document.cookie ) {
817 window.setcookie(cookie_name,'test',1);
818 if (! window.document.cookie) {
819 document.write('<H1><FONT COLOR=#990000>'+
820 'please enable cookies</FONT></H1>');
821 }
822 window.setcookie(cookie_name,'',-1);
823 }
824 $$o{JS}
825 --></SCRIPT> "; #end return literal
826 }
827  
828  
829 sub html_style_sheet {
830 return "
831 <STYLE TYPE='text/css'>
832 <!--
833 BODY {
834 background-color: white;
835 font-family: serif;
836 margin-top: 0;
837 margin-right: 0;
838 margin-bottom: 0;
839 margin-left: 0;
840 padding-top: 0;
841 padding-bottom: 0;
842 padding-right: 1;
843 padding-left: 1;
844 border-top: 0;
845 border-bottom: 0;
846 border-right: 0;
847 border-left: 0;
848 border-style: 0;
849 }
850  
851 A:link {
852 color: #990000;
853 text-decoration: none;
854 }
855  
856 A:visited {
857 color: #990000;
858 text-decoration: none;
859 }
860  
861 A:hover {
862 color: #990000;
863 text-decoration: underline;
864 }
865 --> </STYLE>";
866 }
867  
868  
869 sub html_hidden_fields {
870 return "
871 <INPUT TYPE=HIDDEN NAME=FILEMANAGER_curr_dir
872 VALUE='".r->param('FILEMANAGER_curr_dir')."'>
873 <INPUT TYPE=HIDDEN NAME=FILEMANAGER_cmd VALUE=''>
874 <INPUT TYPE=HIDDEN NAME=FILEMANAGER_arg VALUE=''>
875 <INPUT TYPE=HIDDEN NAME=FILEMANAGER_last_select_all
876 VALUE='".r->param('FILEMANAGER_last_select_all')."'>
877 ";
878 }
879  
880  
881 sub html_location_toolbar {
882 my $o = shift;
883  
884 my @loc = split /\//, r->param('FILEMANAGER_curr_dir');
885  
886 #already in base directory?
887 return "<B>location: / </B>" if ($#loc == -1);
888  
889 #for all elements in the loc except the last one
890 my @ac;
891 my $up_a_href = "
892 <A HREF=#
893 onclick=\"
894 var f=window.document.FileManager;
895 f.FILEMANAGER_curr_dir.value='';
896 f.submit();
897 return false;\">
898 <FONT COLOR=#006699 SIZE=+1><B>..</B></FONT></A>
899 &nbsp;
900 ";
901  
902 for (my $i = 0; $i < $#loc; $i++) {
903 push @ac, $loc[$i];
904 my $url = join("/", @ac);
905  
906 $loc[$i] = "
907 <A HREF=#
908 onclick=\"
909 var f=window.document.FileManager;
910 f.FILEMANAGER_curr_dir.value='$url';
911 f.submit();
912 return false;\">
913 <FONT COLOR=#006699 SIZE=+1><B>".$loc[$i]."</B></FONT></A>
914 ";
915  
916 if ($i == ($#loc - 1)) {
917 $up_a_href = "
918 <A HREF=#
919 onclick=\"
920 var f=window.document.FileManager;
921 f.FILEMANAGER_curr_dir.value='$url';
922 f.submit();
923 return false;\">
924 <FONT COLOR=#006699 SIZE=+1><B>..</B></FONT></A>
925 &nbsp;
926 ";
927 }
928 }
929  
930 $loc[$#loc] = "<FONT SIZE=+1><B>".$loc[$#loc]."</B></FONT>";
931  
932 my $location = "
933 <B>location: </B>
934 <A HREF=#
935 onclick=\"
936 var f=window.document.FileManager;
937 f.FILEMANAGER_curr_dir.value='';
938 f.submit();
939 return false;\">
940 <FONT COLOR=#006699 SIZE=+1><B>/</B></FONT></A>
941 &nbsp;
942 ".join("&nbsp;<FONT SIZE=+1><B>/</B></FONT>&nbsp;", @loc);
943  
944 return ($location, $up_a_href);
945 }
946  
947  
948 sub html_cmd_toolbar {
949 my $o = shift;
950  
951 my @cmds = (
952  
953 #Refresh
954 "<A HREF=# onclick=\"
955 var f=window.document.FileManager;
956 f.submit();
957 return false;\"
958 ><FONT COLOR=WHITE><B>refresh</B></FONT></A>",
959  
960 #Edit
961 "<A HREF=# onclick=\"
962 window.edit_file(); return false;\"
963 ><FONT COLOR=WHITE><B>edit</B></FONT></A>",
964  
965 #Cut
966 "<A HREF=# onclick=\"
967 window.save_names('cut'); return false;\"
968 ><FONT COLOR=WHITE><B>cut</B></FONT></A>",
969  
970 #Copy
971 "<A HREF=# onclick=\"
972 window.save_names('copy'); return false;\"
973 ><FONT COLOR=WHITE><B>copy</B></FONT></A>",
974  
975 #Paste
976 "<A HREF=# onclick=\"
977 if (window.getcookie(cookie_name) != '') {
978 var f=window.document.FileManager;
979 f.FILEMANAGER_cmd.value='paste'; f.submit();
980 } else {
981 window.alert('Please select file(s) to paste by checking '+
982 'the file(s) first and clicking copy or cut.');
983 }
984 return false;\"
985 ><FONT COLOR=WHITE><B>paste</B></FONT></A>",
986  
987 #Delete
988 "<A HREF=# onclick=\"
989 var f=window.document.FileManager;
990 if (get_num_checked() == 0) {
991 window.alert('Please select a file to delete by clicking'+
992 ' on a check box with the mouse.');
993 } else {
994 var msg = '\\n Are you sure?\\n' +
995 '\\n' + 'Click OK to delete selected ' +
996 'files & directories\\n' +
997 ' ***including*** files in those directories';
998  
999 if (window.confirm(msg)) {
1000 f.FILEMANAGER_cmd.value='delete'; f.submit();
1001 }
1002 }
1003 return false; \"
1004 ><FONT COLOR=WHITE><B>delete</B></FONT></A>",
1005  
1006 #Rename
1007 "<A HREF=# onclick=\"
1008 var f=window.document.FileManager;
1009 if (get_num_checked() != 1) {
1010 window.alert('Please select ONE file to rename by clicking on a '+
1011 'check box with the mouse.');
1012 } else {
1013 var rv=window.prompt('enter new name','');
1014 if ((rv != null) && (rv != '')) {
1015 f.FILEMANAGER_cmd.value='rename';
1016 f.FILEMANAGER_arg.value=rv;
1017 f.submit();
1018 }
1019 }
1020 return false;\"
1021 ><FONT COLOR=WHITE><B>rename</B></FONT></A>",
1022  
1023 #Extract
1024 "<A HREF=# onclick=\"
1025 var f=window.document.FileManager;
1026 if (get_num_checked() == 0) {
1027 window.alert('Please select a file to extract by clicking on a '+
1028 'check box with the mouse.');
1029 } else {
1030 f.FILEMANAGER_cmd.value='extract';
1031 f.submit();
1032 }
1033 return false;\"
1034 ><FONT COLOR=WHITE><B>extract</B></FONT></A>",
1035  
1036 #New File
1037 "<A HREF=# onclick=\"
1038 var f=window.document.FileManager;
1039 var rv = window.prompt('new file name','');
1040 var cd = f.FILEMANAGER_curr_dir.value;
1041 if (cd != '') {
1042 rv = cd+'/'+rv;
1043 }
1044 if ((rv != null) && (rv != '')) {
1045 var w = window.open('".r->uri."?FILEMANAGER_cmd=editfile".
1046 "&FILEMANAGER_curr_dir='+escape(cd)+'".
1047 "&FILEMANAGER_editfile='+escape(rv),
1048 'FileManagerEditFile',
1049 'scrollbars,resizable');
1050 w.focus();
1051  
1052 } else if (rv == '') {
1053 window.alert('can not create blank file names');
1054 }
1055 return false; \">
1056 <FONT COLOR=WHITE><B>new file</B></FONT></A>",
1057  
1058 #New Directory
1059 "<A HREF=# onclick=\"
1060 var f=window.document.FileManager;
1061 var rv=window.prompt('new directory name','');
1062 if ((rv != null) && (rv != '')) {
1063 f.FILEMANAGER_arg.value=rv;
1064 f.FILEMANAGER_cmd.value='mkdir';
1065 f.submit();
1066  
1067 } else if (rv == '') {
1068 window.alert('can not create blank directory names');
1069 }
1070 return false;\"
1071 ><FONT COLOR=WHITE><B>new directory</B></FONT></A>",
1072  
1073 #Upload
1074 "<A HREF=# onclick=\"
1075 window.print_upload();
1076 return false;\"
1077 ><FONT COLOR=WHITE><B>upload<B></FONT></A>"
1078 );
1079  
1080 #Rsync
1081 my $rsync = "";
1082 if ($$o{'RSYNC_TO'}) {
1083 push @cmds,
1084 "<TD><A HREF=# onclick=\"
1085 if (window.confirm('Are you sure you want to synchronize with ".
1086 "the production server?')) {
1087 var w=window.open('','RSYNC',
1088 'scrollbars=yes,resizables=yes,width=400,height=500');
1089 w.focus();
1090 var d=w.document.open();
1091 d.write('<HTML><BODY><BR><BR><BR>".
1092 "<CENTER>Please wait synchronizing production server.".
1093 "<BR>This could take several minutes.".
1094 "</CENTER></BODY></HTML>');
1095 d.close();
1096 w.location.replace('".r->uri."?FILEMANAGER_cmd=rsync',
1097 'RSYNC',
1098 'scrollbars=yes,resizables=yes,width=400,height=500');
1099 } return false;\"
1100 ><FONT COLOR=WHITE><B>synchronize</B></FONT></A>";
1101 }
1102  
1103 return "
1104 <!-- Actions Tool bar -->
1105 <TABLE CELLPADDING=0 CELLSPACING=0
1106 BORDER=0>
1107 <TR ALIGN=CENTER>
1108 <TD ALIGN=CENTER>"
1109 .join("</TD><TD>&nbsp;<B><FONT COLOR=#bcbcbc SIZE=+2>|</FONT>&nbsp;</B>"
1110 ."</TD><TD>",
1111 @cmds)
1112 ."</TD></TR></TABLE>";
1113 }
1114  
1115  
1116 sub html_file_list {
1117 my $o = shift;
1118 my $up_a_href = shift || "";
1119  
1120 my $bgcolor = "efefef";
1121  
1122 #get the list in this directory
1123 my $curr_dir = "";
1124 $curr_dir = r->param('FILEMANAGER_curr_dir')."/"
1125 if (r->param('FILEMANAGER_curr_dir') ne "");
1126  
1127 #if there is a value for the ".." directory, then add a row for that link
1128 #at the *top* of the list
1129 my $acum = "";
1130 if ($up_a_href ne "") {
1131 $acum = "
1132 <TR BGCOLOR=#$bgcolor>
1133 <TD>&nbsp;</TD>
1134 <TD>$up_a_href</TD>
1135 <TD ALIGN=CENTER>--</TD>
1136 <TD ALIGN=CENTER>--</TD>
1137 </TR>";
1138 $bgcolor = "ffffff";
1139 }
1140  
1141 my $ct_rows = 0;
1142  
1143 foreach my $file (sort <*>) {
1144  
1145 my ($link,$last_modified,$size,$type);
1146 $ct_rows++;
1147  
1148 #if directory?
1149 if (-d $file) {
1150 $last_modified = "--";
1151 $size = "<TD ALIGN=CENTER>--</TD>";
1152 $type = "/"; # "/" designates "directory"
1153 $link = "<A HREF=#
1154 onclick=\"
1155 var f=window.document.FileManager;
1156 f.FILEMANAGER_curr_dir.value='"
1157 .Apache2::Util::escape_path($curr_dir.$file, r->pool)."';
1158 f.submit();
1159 return false;\">
1160 <FONT COLOR=#006699>$file$type</FONT></A>";
1161 }
1162  
1163 #must be a file
1164 elsif (-f $file) {
1165  
1166 #get file size
1167 my $stat = stat($file);
1168 $size = $stat->size;
1169 if ($size > 1024000) {
1170 $size = sprintf("%0.2f",$size/1024000) . " <I>M</I>";
1171 } elsif ($stat->size > 1024) {
1172 $size = sprintf("%0.2f",$size/1024). " <I>K</I>";
1173 } else {
1174 $size = sprintf("%.2f",$size). " <I>b</I>";
1175 }
1176 $size =~ s/\.0{1,2}//;
1177 $size = "<TD NOWRAP ALIGN=RIGHT>$size</TD>";
1178  
1179 #get last modified
1180 $last_modified = $o->formated_date($stat->mtime);
1181  
1182 #get file type
1183 if (-S $file) {
1184 $type = "="; # "=" designates "socket"
1185 }
1186 elsif (-l $file) {
1187 $type = "@"; # "@" designates "link"
1188 }
1189 elsif (-x $file) {
1190 $type = "*"; # "*" designates "executable"
1191 }
1192  
1193 my $true_doc_root = r->document_root;
1194 my $fake_doc_root = $$o{DR};
1195 $fake_doc_root =~ s/^$true_doc_root//;
1196 $fake_doc_root =~ s/^\///; $fake_doc_root =~ s/\/$//;
1197  
1198 my $href = $curr_dir;
1199 $href = $fake_doc_root."/".$href if $fake_doc_root;
1200  
1201 $link = "
1202 <A HREF=\"/$href"."$file?nossi=1\"
1203 TARGET=_blank><FONT COLOR=BLACK>"
1204 .Apache2::Util::escape_path($file.$type, r->pool).
1205 "</FONT>
1206 </A>";
1207 }
1208  
1209 $acum .= "
1210 <TR BGCOLOR=#$bgcolor>
1211 <TD><INPUT TYPE=CHECKBOX NAME=FILEMANAGER_sel_files
1212 VALUE='$curr_dir"."$file'></TD>
1213 <TD>$link</TD>
1214 <TD ALIGN=CENTER NOWRAP>$last_modified</TD>
1215 $size
1216 </TR>";
1217  
1218 #alternate bgcolor so it is easier to read
1219 $bgcolor = ( ($bgcolor eq "ffffff") ? "efefef" : "ffffff" );
1220 }
1221  
1222 #print a message if there were no files in this directory
1223 if ($ct_rows == 0) {
1224 $acum .= "
1225 <TR ALIGN=CENTER><TD COLSPAN=3>
1226 <TABLE BORDER=1 WIDTH=100% BGCOLOR=WHITE>
1227 <TR>
1228 <TD ALIGN=CENTER><BR><I>no files found</I><BR><BR></TD>
1229 </TR>
1230 </TABLE>
1231 </TD>
1232 </TR>";
1233 }
1234  
1235 return "
1236 <!-- Files list -->
1237 <TABLE CELLPADDING=3 CELLSPACING=0 WIDTH=100% BORDER=0>
1238  
1239 <!-- Headers -->
1240 <TR BGCOLOR=#606060>
1241 <TD WIDTH=1% ALIGN=CENTER>
1242 <A HREF=# onclick=\"
1243 window.select_all();
1244 return false;\"
1245 ><FONT COLOR=WHITE>+</FONT> </A>
1246 </TD>
1247 <TD WIDTH=80%>
1248 <FONT COLOR=WHITE><B>filename</B></FONT>
1249 </TD>
1250 <TD WIDTH=15% ALIGN=CENTER NOWRAP>
1251 <FONT COLOR=WHITE><B>last modified</B></FONT>
1252 </TD>
1253 <TD WIDTH=4% ALIGN=CENTER>
1254 <FONT COLOR=WHITE><B>size</B></FONT>
1255 </TD>
1256 </TR>
1257  
1258 <! -- Files -->
1259 $acum
1260 </TD></TR></TABLE>";
1261 }
1262  
1263  
1264 sub html_top {
1265 return "
1266 <TABLE WIDTH=100% CELLPADDING=0 CELLSPAING=0>
1267 <TR>
1268 <TD>
1269 <FONT SIZE=+2 COLOR=#3a3a3a>
1270 <B>".r->hostname." - file manager</B></FONT>
1271 </TD>
1272 <TD ALIGN=RIGHT VALIGN=TOP>
1273 <A HREF=# onclick=\"
1274 window.display_help();
1275 return false;\"
1276 ><FONT COLOR=#3a3a3a>help</FONT></A>
1277 </TD>
1278 </TR>
1279 </TABLE>
1280 ";
1281 }
1282  
1283  
1284 sub html_bottom {
1285 return "
1286 <TABLE WIDTH=100% CELLPADDING=0 CELLSPAING=0><TR>
1287 <TD ALIGN=RIGHT VALIGN=TOP>
1288 <A HREF=http://www.cpan.org/modules/by-module/Apache/PMC
1289 TARGET=CPAN
1290 ><FONT SIZE=-1 COLOR=BLACK>Apache2-FileManager-$VERSION</FONT></A>
1291 </TD>
1292 </TR>
1293 </TABLE>";
1294 }
1295  
1296  
1297  
1298  
1299  
1300 ##############################################################################
1301 # -------------- Utility Methods ------------------------------------------- #
1302 ##############################################################################
1303  
1304 sub execute_cmds {
1305 my $o = shift;
1306 my $cmd = r->param('FILEMANAGER_cmd');
1307 my $arg = r->param('FILEMANAGER_arg');
1308 my $method = "cmd_$cmd";
1309 if ($o->can($method)) {
1310 $o->$method($arg);
1311 }
1312 }
1313  
1314  
1315 sub get_selected_files {
1316 my @sel_files = r->param('FILEMANAGER_sel_files');
1317 return \@sel_files;
1318 }
1319  
1320  
1321 sub filename_esc {
1322 #escape spaces in filename
1323 my $o = shift;
1324 my $f = shift;
1325 $f =~ s/\ /\\\ /g;
1326 return $f;
1327 }
1328  
1329  
1330 sub formated_date {
1331 my $o = shift;
1332 my $date = shift || time;
1333 return strftime "%D %l:%M %P", localtime($date);
1334 }
1335  
1336  
1337 sub get_clip_board {
1338 my $o = shift;
1339  
1340 #get copy and cut file arrays
1341 my $buffer_type = "";
1342 my $buffer_filenames = [];
1343  
1344 if (defined(r->headers_in->{'Cookie'})) {
1345 my $cookie_name = uc(r->hostname());
1346 $cookie_name =~ s/[^A-Z]//g;
1347 $cookie_name .= "_FM";
1348 my %cookies = CGI::Cookie->parse(r->headers_in->{'Cookie'});
1349 if (exists $cookies{$cookie_name}) {
1350 my $data = $cookies{$cookie_name}->value;
1351 my @ar = split /\|/, $data;
1352  
1353 #is there something in buffer
1354 if ($#ar > 0) {
1355 $buffer_type = pop @ar;
1356 $buffer_filenames = \@ar;
1357 }
1358 }
1359 }
1360 return ($buffer_type, $buffer_filenames);
1361 }
1362  
1363  
1364  
1365  
1366  
1367 ###############################################################################
1368 # -- Commands (called via form input from method execute_cmds or manually) -- #
1369 ###############################################################################
1370  
1371 sub cmd_savefiledata {
1372 my $o = shift;
1373  
1374 my $base = r->param('FILEMANAGER_editfile');
1375 $base =~ /([^\/]+)$/;
1376 my $filename = $1;
1377 remove $filename;
1378 my $fh = IO::File->new("> ".$filename);
1379 print $fh scalar(r->param('FILEMANAGER_filedata'));
1380 $$o{MESSAGE} = "file saved";
1381 $$o{view} = "pre_editfile";
1382 return undef;
1383 }
1384  
1385  
1386 sub cmd_editfile {
1387 my $o = shift;
1388  
1389 my $base = r->param('FILEMANAGER_editfile');
1390 $base =~ /([^\/]+)$/;
1391 my $filename = $1;
1392  
1393 if (! -e $filename) {
1394 my $fh = IO::File->new("> ".$filename);
1395 if ($fh) {
1396 $$o{JS} .= "
1397 if (window.opener && window.opener.document.FileManager) {
1398 window.opener.document.FileManager.submit();
1399 }";
1400 }
1401 }
1402 $$o{view} = "pre_editfile";
1403 }
1404  
1405  
1406 sub cmd_paste {
1407 my $o = shift;
1408 my $arg1 = shift;
1409 my ($buffer_type, $files) = $o->get_clip_board();
1410  
1411 my $count = 0;
1412  
1413 if ($buffer_type eq "copy") {
1414 my @files = map { $o->filename_esc($$o{DR}."/".$_) } @{ $files };
1415 $count = copy \1, @files, ".";
1416 } elsif ($buffer_type eq "cut") {
1417 for (@{ $files }) {
1418 my $file = $$o{DR}."/".$_;
1419 if (-d $file) {
1420 my $file = $o->filename_esc($file);
1421 my $tmp = copy \1, $file, ".";
1422 if ($tmp) {
1423 remove \1, $file and $count++;
1424 }
1425 } elsif (-f $file) {
1426 move($file, ".") and $count++;
1427 }
1428 }
1429 }
1430  
1431 if ($count == 0) {
1432 $$o{MESSAGE} = "0 files and directories pasted";
1433 } elsif ($count == 1) {
1434 $$o{MESSAGE} = "1 file or directory pasted";
1435 } else {
1436 $$o{MESSAGE} = "$count files or directories pasted";
1437 }
1438 $$o{JS} = "window.setcookie(cookie_name,'',-1);";
1439 return undef;
1440 }
1441  
1442  
1443 sub cmd_delete {
1444 my $o = shift;
1445 my $arg1 = shift;
1446 my $sel_files = $o->get_selected_files();
1447 my @files = map { $o->filename_esc($$o{DR}."/".$_) } @{ $sel_files };
1448 my $count = remove \1, @files;
1449 if ($count == 0) {
1450 $$o{MESSAGE} = "0 files and directories deleted";
1451 } elsif ($count == 1) {
1452 $$o{MESSAGE} = "1 file or directory deleted";
1453 } else {
1454 $$o{MESSAGE} = "$count files or directories deleted";
1455 }
1456 return undef;
1457 }
1458  
1459  
1460 sub cmd_extract {
1461 my $o = shift;
1462 my $arg1 = shift;
1463 my $sel_files = $o->get_selected_files();
1464 foreach my $f (@{ $sel_files }) {
1465 my $esc = $o->filename_esc($$o{DR}."/".$f);
1466 my $archive = Archive::Any->new($esc);
1467 $archive->extract if defined $archive;
1468 $$o{MESSAGE} = "Files extracted.";
1469 }
1470 return undef;
1471 }
1472  
1473  
1474 sub cmd_upload {
1475 my $o = shift;
1476 my $arg1 = shift;
1477 my $count = 0;
1478  
1479 foreach my $i (1 .. 10) {
1480 my @ar = split /\/|\\/, r->param("FILEMANAGER_file$i");
1481 next if ($#ar == -1);
1482 my $filename = pop @ar;
1483 $filename =~ s/[^\w\ \d\.\-]//g;
1484 next if ($filename eq "");
1485  
1486 $count++;
1487  
1488 my $up = r->upload("FILEMANAGER_file$i");
1489 #next if not defined $up;
1490  
1491 #my $in_fh = $up->fh;
1492 #next if !defined $in_fh;
1493  
1494 my $path = $$o{DR}."/".r->param('FILEMANAGER_curr_dir')."/".$filename;
1495 $up->link($path);
1496  
1497 #my $arg = "> ".$$o{DR}."/".r->param('FILEMANAGER_curr_dir')."/".$filename;
1498 #my $out_fh;
1499 #open($out_fh, $arg)
1500 # or die "ERROR: cannot open '$arg'";
1501  
1502 #next if not defined $out_fh;
1503  
1504 #while (<$in_fh>) {
1505 # print $out_fh $_;
1506 #}
1507 #close($out_fh);
1508  
1509 }
1510 #$$o{MESSAGE} = "$count file(s) uploaded.";
1511 $$o{'view'} = "post_upload";
1512 return undef;
1513 }
1514  
1515  
1516 sub cmd_rename {
1517 my $o = shift;
1518 my $arg1 = shift;
1519 my $sel_files = $o->get_selected_files();
1520 my $file = $$o{DR}."/".$sel_files->[0];
1521 my $bool = move($file, $arg1);
1522 if ($bool) {
1523 $$o{MESSAGE} = "File renamed.";
1524 } else {
1525 $$o{MESSAGE} = "File could not be renamed.";
1526 }
1527 return undef;
1528 }
1529  
1530  
1531 sub cmd_rsync {
1532 my $o = shift;
1533 my $arg1 = shift;
1534 $$o{'SSH_PATH'} ||= r->dir_config('SSH_PATH');
1535  
1536 #try some default paths for ssh if we can't find ssh
1537 for (qw(/usr/bin/ssh /usr/local/bin/ssh)) {
1538 last if $$o{'SSH_PATH'};
1539 $$o{'SSH_PATH'} = $_ if (-f $_);
1540 }
1541  
1542 eval "require File::Rsync";
1543 if ($@) {
1544 r->log_error($@);
1545 $$o{MESSAGE} = "Module File::Rsync not installed.";
1546 } else {
1547 my $obj = File::Rsync->new( {
1548 'archive' => 1,
1549 'compress' => 1,
1550 'rsh' => $$o{'SSH_PATH'},
1551 'delete' => 1,
1552 'stats' => 1
1553 } );
1554  
1555 $obj->exec( { src => r->document_root(),
1556 dest => $$o{'RSYNC_TO'} } )
1557 or warn "rsync failed\n";
1558 $$o{MESSAGE} = join("<BR>", @{ $obj->out }) if ($obj->out);
1559 $$o{MESSAGE} = join("<BR>", @{ $obj->err }) if ($obj->err);
1560 }
1561 $$o{'view'} = "post_upload";
1562 return undef;
1563 }
1564  
1565  
1566 sub cmd_mkdir {
1567 my $o = shift;
1568 my $arg1 = shift;
1569 my $bool = mkdir $arg1;
1570 if ($bool) {
1571 $$o{MESSAGE} = "New directory added.";
1572 } else {
1573 $$o{MESSAGE} = "Could not make directory.";
1574 }
1575 return undef;
1576 }
1577  
1578  
1579 "righty-o";