]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
17f94709872e53531f78deb6f350fd6c77948af9
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2004-07-27
20 use POSIX qw(strftime);
28 my $script = $ENV{'SCRIPT_NAME'};
30 my %dispatch = ('show' => \
&exec_show
,
31 'beginedit' => \
&exec_beginedit
,
32 'canceledit' => \
&exec_canceledit
,
33 'endedit' => \
&exec_endedit
,
34 'mkdir' => \
&exec_mkdir
,
35 'mkfile' => \
&exec_mkfile
,
36 'upload' => \
&exec_upload
,
37 'copy' => \
&exec_copy
,
38 'rename' => \
&exec_rename
,
39 'remove' => \
&exec_remove
,
40 'unlock' => \
&exec_unlock
,
41 'about' => \
&exec_about
46 use base
qw(Exporter);
48 @EXPORT = qw(exec_command);
52 # Execute the specified command
54 # Params: 1. Command to execute
55 # 2. Reference to user input hash
56 # 3. Reference to config hash
58 # Return: Output of the command (Scalar Reference)
62 my ($command,$data,$config) = @_;
64 foreach(keys(%dispatch))
66 if(lc($_) eq lc($command))
68 my $output = &{$dispatch{$_}}($data,$config);
73 return error
($config->{'errors'}->{'cmd_unknown'},'/',{COMMAND
=> $command});
78 # View a directory or a file
80 # Params: 1. Reference to user input hash
81 # 2. Reference to config hash
83 # Return: Output of the command (Scalar Reference)
87 my ($data,$config) = @_;
88 my $physical = $data->{'physical'};
89 my $virtual = $data->{'virtual'};
90 my $uselist = $data->{'uselist'};
92 my $tpl = new Template
;
96 # Create directory listing
98 my $direntries = dir_read
($physical);
99 return error
($config->{'dir_read_failed'},upper_path
($virtual),{DIR
=> '$virtual'}) unless($direntries);
101 my $files = $direntries->{'files'};
102 my $dirs = $direntries->{'dirs'};
106 # Create the link to the upper directory
107 # (only if we are not in the root directory)
109 unless($virtual eq "/")
111 my @stat = stat($physical."/..");
113 my $udtpl = new Template
;
114 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
116 $udtpl->fillin("UPPER_DIR",encode_entities
(upper_path
($virtual)));
117 $udtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
119 $dirlist .= $udtpl->get_template;
124 foreach my $dir(@
$dirs)
126 my @stat = stat($physical."/".$dir);
127 my $virt_path = encode_entities
($virtual.$dir."/");
129 my $dtpl = new Template
;
130 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
132 $dtpl->fillin("DIR",$virt_path);
133 $dtpl->fillin("DIR_NAME",$dir);
134 $dtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
135 $dtpl->fillin("URL",equal_url
($config->{'httproot'},$virt_path));
137 $dirlist .= $dtpl->get_template;
142 foreach my $file(@
$files)
144 my $phys_path = $physical."/".$file;
145 my $virt_path = encode_entities
($virtual.$file);
147 my @stat = stat($phys_path);
148 my $in_use = $uselist->in_use($virtual.$file);
150 my $ftpl = new Template
;
151 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
153 $ftpl->fillin("FILE",$virt_path);
154 $ftpl->fillin("FILE_NAME",$file);
155 $ftpl->fillin("SIZE",$stat[7]);
156 $ftpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
157 $ftpl->fillin("URL",equal_url
($config->{'httproot'},$virt_path));
159 $ftpl->parse_if_block("not_readable",not -r
$phys_path);
160 $ftpl->parse_if_block("binary",-B
$phys_path);
161 $ftpl->parse_if_block("readonly",not -w
$phys_path);
163 $ftpl->parse_if_block("viewable",-r
$phys_path && -T
$phys_path && not ($config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'}));
165 $ftpl->parse_if_block("editable",-r
$phys_path && -w
$phys_path && -T
$phys_path && not ($config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'}) && not $in_use);
167 $ftpl->parse_if_block("in_use",$in_use);
168 $ftpl->parse_if_block("unused",not $in_use);
170 $ftpl->parse_if_block("too_large",$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
172 $dirlist .= $ftpl->get_template;
175 $tpl->read_file($config->{'templates'}->{'dirlist'});
177 $tpl->fillin("DIRLIST",$dirlist);
178 $tpl->fillin("DIR",$virtual);
179 $tpl->fillin("SCRIPT",$script);
180 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
186 return error
($config->{'errors'}->{'noview'},upper_path
($virtual)) unless(-r
$physical);
188 # Check on binary files
189 # We have to do it in this way, or empty files
190 # will be recognized as binary files
196 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
202 my $size = (stat($physical))[7];
204 if($config->{'max_file_size'} && $size > $config->{'max_file_size'})
206 return error
($config->{'errors'}->{'file_too_large'},upper_path
($virtual),{SIZE
=> $config->{'max_file_size'}})
210 my $content = file_read
($physical);
211 $$content =~ s/\015\012|\012|\015/\n/g;
213 $tpl->read_file($config->{'templates'}->{'viewfile'});
215 $tpl->fillin("FILE",$virtual);
216 $tpl->fillin("DIR",upper_path
($virtual));
217 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
218 $tpl->fillin("SCRIPT",$script);
219 $tpl->fillin("CONTENT",encode_entities
($$content));
221 $tpl->parse_if_block("editable",-r
$physical && -w
$physical && -T
$physical && not ($config->{'max_file_size'} && $size > $config->{'max_file_size'}) && $uselist->unused($virtual));
226 my $output = header
(-type
=> "text/html");
227 $output .= $tpl->get_template;
234 # Lock a file and display a form to edit it
236 # Params: 1. Reference to user input hash
237 # 2. Reference to config hash
239 # Return: Output of the command (Scalar Reference)
241 sub exec_beginedit
($$)
243 my ($data,$config) = @_;
244 my $physical = $data->{'physical'};
245 my $virtual = $data->{'virtual'};
246 my $uselist = $data->{'uselist'};
248 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
249 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
250 return error
($config->{'errors'}->{'noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
252 # Check on binary files
258 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
262 if($config->{'max_file_size'} && (stat($physical))[7] > $config->{'max_file_size'})
264 return error
($config->{'errors'}->{'file_too_large'},upper_path
($virtual),{SIZE
=> $config->{'max_file_size'}})
270 $uselist->add_file($virtual);
273 my $content = file_read
($physical);
274 $$content =~ s/\015\012|\012|\015/\n/g;
276 my $tpl = new Template
;
277 $tpl->read_file($config->{'templates'}->{'editfile'});
279 $tpl->fillin("FILE",$virtual);
280 $tpl->fillin("DIR",upper_path
($virtual));
281 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
282 $tpl->fillin("SCRIPT",$script);
283 $tpl->fillin("CONTENT",encode_entities
($$content));
285 my $output = header
(-type
=> "text/html");
286 $output .= $tpl->get_template;
297 # Params: 1. Reference to user input hash
298 # 2. Reference to config hash
300 # Return: Output of the command (Scalar Reference)
302 sub exec_canceledit
($$)
304 my ($data,$config) = @_;
305 my $virtual = $data->{'virtual'};
307 file_unlock
($data->{'uselist'},$virtual);
308 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
313 # Save a file, unlock it and return to directory view
315 # Params: 1. Reference to user input hash
316 # 2. Reference to config hash
318 # Return: Output of the command (Scalar Reference)
322 my ($data,$config) = @_;
323 my $physical = $data->{'physical'};
324 my $virtual = $data->{'virtual'};
325 my $content = $data->{'cgi'}->param('filecontent');
326 my $uselist = $data->{'uselist'};
330 $content =~ s/\015\012|\012|\015/\n/g;
332 if($data->{'cgi'}->param('encode_iso'))
334 # Encode all ISO-8859-1 special chars
336 $content = encode_entities
($content,"\200-\377");
339 if($data->{'cgi'}->param('saveas'))
341 # Create the new filename
343 $physical = $data->{'new_physical'};
344 $virtual = $data->{'new_virtual'};
346 # Check if someone else is editing the new file
348 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
351 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
352 return error
($config->{'errors'}->{'noedit'}, upper_path
($virtual)) unless(-r
$physical && -w
$physical);
354 if(file_save
($physical,\
$content))
356 # Saving of the file was successful - so unlock it!
358 file_unlock
($uselist,$data->{'virtual'});
360 # Maybe the user saved the file using another filename...
361 # But we have to unlock the original file!
363 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
367 return error
($config->{'errors'}->{'edit_failed'},upper_path
($virtual),{FILE
=> $virtual});
373 # Create a file and return to directory view
375 # Params: 1. Reference to user input hash
376 # 2. Reference to config hash
378 # Return: Output of the command (Scalar Reference)
382 my ($data,$config) = @_;
383 my $new_physical = $data->{'new_physical'};
384 my $new_virtual = $data->{'new_virtual'};
385 my $dir = upper_path
($new_virtual);
386 $new_virtual = encode_entities
($new_virtual);
390 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
392 file_create
($new_physical) or return error
($config->{'errors'}->{'mkfile_failed'},$dir,{FILE
=> $new_virtual});
393 return devedit_reload
({command
=> 'show', file
=> $dir});
397 my $tpl = new Template
;
398 $tpl->read_file($config->{'templates'}->{'mkfile'});
400 $tpl->fillin("DIR","/");
401 $tpl->fillin("SCRIPT",$script);
403 my $output = header
(-type
=> "text/html");
404 $output .= $tpl->get_template;
412 # Create a directory and return to directory view
414 # Params: 1. Reference to user input hash
415 # 2. Reference to config hash
417 # Return: Output of the command (Scalar Reference)
421 my ($data,$config) = @_;
422 my $new_physical = $data->{'new_physical'};
423 my $new_virtual = $data->{'new_virtual'};
424 my $dir = upper_path
($new_virtual);
425 $new_virtual = encode_entities
($new_virtual);
427 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
431 mkdir($new_physical,0777) or return error
($config->{'errors'}->{'mkdir_failed'},$dir,{DIR
=> $new_virtual});
432 return devedit_reload
({command
=> 'show', file
=> $dir});
436 my $tpl = new Template
;
437 $tpl->read_file($config->{'templates'}->{'mkdir'});
439 $tpl->fillin("DIR","/");
440 $tpl->fillin("SCRIPT",$script);
442 my $output = header
(-type
=> "text/html");
443 $output .= $tpl->get_template;
453 # Params: 1. Reference to user input hash
454 # 2. Reference to config hash
456 # Return: Output of the command (Scalar Reference)
460 my ($data,$config) = @_;
461 my $physical = $data->{'physical'};
462 my $virtual = $data->{'virtual'};
463 my $cgi = $data->{'cgi'};
465 if(my $uploaded_file = $cgi->param('uploaded_file'))
467 # Process file upload
469 my $filename = file_name
($uploaded_file);
470 my $file_phys = $physical."/".$filename;
471 my $file_virt = $virtual."".$filename;
473 return error
($config->{'errors'}->{'file_exists'},$virtual,{FILE
=> $file_virt}) if(-e
$file_phys);
475 my $ascii = $cgi->param('ascii');
476 my $handle = $cgi->upload('uploaded_file');
480 open(FILE
,">$file_phys") or return error
($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE
=> $file_virt});
481 binmode(FILE
) unless($ascii);
483 # Read transferred file and write it to disk
485 read($handle, my $data, -s
$handle);
486 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
491 return devedit_reload
({command
=> "show", file
=> $virtual});
495 my $tpl = new Template
;
496 $tpl->read_file($config->{'templates'}->{'upload'});
498 $tpl->fillin("DIR",$virtual);
499 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
500 $tpl->fillin("SCRIPT",$script);
502 my $output = header
(-type
=> "text/html");
503 $output .= $tpl->get_template;
511 # Copy a file and return to directory view
513 # Params: 1. Reference to user input hash
514 # 2. Reference to config hash
516 # Return: Output of the command (Scalar Reference)
520 my ($data,$config) = @_;
521 my $physical = $data->{'physical'};
522 my $virtual = encode_entities
($data->{'virtual'});
523 my $new_physical = $data->{'new_physical'};
525 return error
($config->{'errors'}->{'nocopy'}) unless(-r
$physical);
529 my $new_virtual = $data->{'new_virtual'};
530 my $dir = upper_path
($new_virtual);
531 $new_virtual = encode_entities
($new_virtual);
535 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
539 return error
($config->{'errors'}->{'dircopy'});
541 elsif(not $data->{'cgi'}->param('confirmed'))
543 my $tpl = new Template
;
544 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
546 $tpl->fillin("FILE",$virtual);
547 $tpl->fillin("NEW_FILE",$new_virtual);
548 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
549 $tpl->fillin("NEW_DIR",$dir);
550 $tpl->fillin("DIR",upper_path
($virtual));
552 $tpl->fillin("COMMAND","copy");
553 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
554 $tpl->fillin("SCRIPT",$script);
556 my $output = header
(-type
=> "text/html");
557 $output .= $tpl->get_template;
563 copy
($physical,$new_physical) or return error
($config->{'errors'}->{'copy_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
564 return devedit_reload
({command
=> 'show', file
=> $dir});
568 my $tpl = new Template
;
569 $tpl->read_file($config->{'templates'}->{'copyfile'});
571 $tpl->fillin("FILE",$virtual);
572 $tpl->fillin("DIR",upper_path
($virtual));
573 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
574 $tpl->fillin("SCRIPT",$script);
576 my $output = header
(-type
=> "text/html");
577 $output .= $tpl->get_template;
585 # Rename/move a file and return to directory view
587 # Params: 1. Reference to user input hash
588 # 2. Reference to config hash
590 # Return: Output of the command (Scalar Reference)
594 my ($data,$config) = @_;
595 my $physical = $data->{'physical'};
596 my $virtual = $data->{'virtual'};
597 my $new_physical = $data->{'new_physical'};
599 return error
($config->{'errors'}->{'rename_root'},"/") if($virtual eq "/");
600 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
604 my $new_virtual = $data->{'new_virtual'};
605 my $dir = upper_path
($new_virtual);
606 $new_virtual = encode_entities
($new_virtual);
610 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
614 return error
($config->{'errors'}->{'dircopy'});
616 elsif(not $data->{'cgi'}->param('confirmed'))
618 my $tpl = new Template
;
619 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
621 $tpl->fillin("FILE",$virtual);
622 $tpl->fillin("NEW_FILE",$new_virtual);
623 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
624 $tpl->fillin("NEW_DIR",$dir);
625 $tpl->fillin("DIR",upper_path
($virtual));
627 $tpl->fillin("COMMAND","rename");
628 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
629 $tpl->fillin("SCRIPT",$script);
631 my $output = header
(-type
=> "text/html");
632 $output .= $tpl->get_template;
638 rename($physical,$new_physical) or return error
($config->{'errors'}->{'rename_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
639 return devedit_reload
({command
=> 'show', file
=> $dir});
643 my $tpl = new Template
;
644 $tpl->read_file($config->{'templates'}->{'renamefile'});
646 $tpl->fillin("FILE",$virtual);
647 $tpl->fillin("DIR",upper_path
($virtual));
648 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
649 $tpl->fillin("SCRIPT",$script);
651 my $output = header
(-type
=> "text/html");
652 $output .= $tpl->get_template;
660 # Remove a file or a directory and return to directory view
662 # Params: 1. Reference to user input hash
663 # 2. Reference to config hash
665 # Return: Output of the command (Scalar Reference)
669 my ($data,$config) = @_;
670 my $physical = $data->{'physical'};
671 my $virtual = $data->{'virtual'};
673 return error
($config->{'errors'}->{'remove_root'},"/") if($virtual eq "/");
679 if($data->{'cgi'}->param('confirmed'))
682 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
686 my $tpl = new Template
;
687 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
689 $tpl->fillin("DIR",$virtual);
690 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
691 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
692 $tpl->fillin("SCRIPT",$script);
694 my $output = header
(-type
=> "text/html");
695 $output .= $tpl->get_template;
704 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
706 if($data->{'cgi'}->param('confirmed'))
708 unlink($physical) or return error
($config->{'errors'}->{'delete_failed'},upper_path
($virtual),{FILE
=> $virtual});
709 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
713 my $tpl = new Template
;
714 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
716 $tpl->fillin("FILE",$virtual);
717 $tpl->fillin("DIR",upper_path
($virtual));
718 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
719 $tpl->fillin("SCRIPT",$script);
721 my $output = header
(-type
=> "text/html");
722 $output .= $tpl->get_template;
731 # Remove a file from the list of used files and
732 # return to directory view
734 # Params: 1. Reference to user input hash
735 # 2. Reference to config hash
737 # Return: Output of the command (Scalar Reference)
741 my ($data,$config) = @_;
742 my $virtual = $data->{'virtual'};
744 if($data->{'cgi'}->param('confirmed'))
746 file_unlock
($data->{'uselist'},$virtual);
747 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
751 my $tpl = new Template
;
752 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
754 $tpl->fillin("FILE",$virtual);
755 $tpl->fillin("DIR",upper_path
($virtual));
756 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
757 $tpl->fillin("SCRIPT",$script);
759 my $output = header
(-type
=> "text/html");
760 $output .= $tpl->get_template;
768 # Display some information about Dev-Editor
770 # Params: 1. Reference to user input hash
771 # 2. Reference to config hash
773 # Return: Output of the command (Scalar Reference)
777 my ($data,$config) = @_;
779 my $tpl = new Template
;
780 $tpl->read_file($config->{'templates'}->{'about'});
782 $tpl->fillin("SCRIPT",$script);
784 # Dev-Editor's version number
786 $tpl->fillin("VERSION",$data->{'version'});
788 # Some path information
790 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
791 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
792 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
793 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
797 $tpl->fillin("PERL_PROG",$^X
);
798 $tpl->fillin("PERL_VER",sprintf("%vd",$^V
));
800 # Information about the server
802 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
803 $tpl->fillin("OS",$^O
);
804 $tpl->fillin("TIME",strftime
($config->{'timeformat'},localtime));
806 # Process information
808 $tpl->fillin("PID",$$);
810 # Check if the functions getpwuid() and getgrgid() are available
812 if(eval("getpwuid(0)") && eval("getgrgid(0)"))
814 # Dev-Editor is running on a system which allows users and groups
815 # So we display the user and the group of our process
817 $tpl->parse_if_block("users",1);
819 # ID's of user and group
821 $tpl->fillin("UID",$<);
822 $tpl->fillin("GID",$();
824 # Names of user and group
826 $tpl->fillin("USER",getpwuid($<));
827 $tpl->fillin("GROUP",getgrgid($());
831 $tpl->parse_if_block("users",0);
834 my $output = header
(-type
=> "text/html");
835 $output .= $tpl->get_template;
840 # it's true, baby ;-)
patrick-canterino.de