]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
0ae3416e57611e9f766ab6dcd29e849ca142d0a3
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2004-07-20
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 return error
($config->{'errors'}->{'cmd_unknown'},'/',{COMMAND
=> $command}) unless($dispatch{$command});
66 my $output = &{$dispatch{$command}}($data,$config);
72 # View a directory or a file
74 # Params: 1. Reference to user input hash
75 # 2. Reference to config hash
77 # Return: Output of the command (Scalar Reference)
81 my ($data,$config) = @_;
82 my $physical = $data->{'physical'};
83 my $virtual = $data->{'virtual'};
84 my $uselist = $data->{'uselist'};
86 my $tpl = new Template
;
90 # Create directory listing
92 my $direntries = dir_read
($physical);
93 return error
($config->{'dir_read_failed'},upper_path
($virtual),{DIR
=> '$virtual'}) unless($direntries);
95 my $files = $direntries->{'files'};
96 my $dirs = $direntries->{'dirs'};
100 # Create the link to the upper directory
101 # (only if we are not in the root directory)
103 unless($virtual eq "/")
105 my @stat = stat($physical."/..");
107 my $udtpl = new Template
;
108 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
110 $udtpl->fillin("UPPER_DIR",encode_entities
(upper_path
($virtual)));
111 $udtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
113 $dirlist .= $udtpl->get_template;
118 foreach my $dir(@
$dirs)
120 my @stat = stat($physical."/".$dir);
121 my $virt_path = encode_entities
($virtual.$dir."/");
123 my $dtpl = new Template
;
124 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
126 $dtpl->fillin("DIR",$virt_path);
127 $dtpl->fillin("DIR_NAME",$dir);
128 $dtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
129 $dtpl->fillin("URL",equal_url
($config->{'httproot'},$virt_path));
131 $dirlist .= $dtpl->get_template;
136 foreach my $file(@
$files)
138 my $phys_path = $physical."/".$file;
139 my $virt_path = encode_entities
($virtual.$file);
141 my @stat = stat($phys_path);
142 my $in_use = $uselist->in_use($virtual.$file);
144 my $ftpl = new Template
;
145 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
147 $ftpl->fillin("FILE",$virt_path);
148 $ftpl->fillin("FILE_NAME",$file);
149 $ftpl->fillin("SIZE",$stat[7]);
150 $ftpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
151 $ftpl->fillin("URL",equal_url
($config->{'httproot'},$virt_path));
153 $ftpl->parse_if_block("not_readable",not -r
$phys_path);
154 $ftpl->parse_if_block("binary",-B
$phys_path);
155 $ftpl->parse_if_block("readonly",not -w
$phys_path);
157 $ftpl->parse_if_block("viewable",-r
$phys_path && -T
$phys_path && not ($config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'}));
159 $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);
161 $ftpl->parse_if_block("in_use",$in_use);
162 $ftpl->parse_if_block("unused",not $in_use);
164 $ftpl->parse_if_block("too_large",$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
166 $dirlist .= $ftpl->get_template;
169 $tpl->read_file($config->{'templates'}->{'dirlist'});
171 $tpl->fillin("DIRLIST",$dirlist);
172 $tpl->fillin("DIR",$virtual);
173 $tpl->fillin("SCRIPT",$script);
174 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
180 return error
($config->{'errors'}->{'noview'},upper_path
($virtual)) unless(-r
$physical);
182 # Check on binary files
183 # We have to do it in this way, or empty files
184 # will be recognized as binary files
190 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
196 my $size = (stat($physical))[7];
198 if($config->{'max_file_size'} && $size > $config->{'max_file_size'})
200 return error
($config->{'errors'}->{'file_too_large'},upper_path
($virtual),{SIZE
=> $config->{'max_file_size'}})
204 my $content = file_read
($physical);
205 $$content =~ s/\015\012|\012|\015/\n/g;
207 $tpl->read_file($config->{'templates'}->{'viewfile'});
209 $tpl->fillin("FILE",$virtual);
210 $tpl->fillin("DIR",upper_path
($virtual));
211 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
212 $tpl->fillin("SCRIPT",$script);
213 $tpl->fillin("CONTENT",encode_entities
($$content));
215 $tpl->parse_if_block("editable",-r
$physical && -w
$physical && -T
$physical && not ($config->{'max_file_size'} && $size > $config->{'max_file_size'}) && $uselist->unused($virtual));
220 my $output = header
(-type
=> "text/html");
221 $output .= $tpl->get_template;
228 # Lock a file and display a form to edit it
230 # Params: 1. Reference to user input hash
231 # 2. Reference to config hash
233 # Return: Output of the command (Scalar Reference)
235 sub exec_beginedit
($$)
237 my ($data,$config) = @_;
238 my $physical = $data->{'physical'};
239 my $virtual = $data->{'virtual'};
240 my $uselist = $data->{'uselist'};
242 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
243 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
244 return error
($config->{'errors'}->{'noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
246 # Check on binary files
252 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
256 if($config->{'max_file_size'} && (stat($physical))[7] > $config->{'max_file_size'})
258 return error
($config->{'errors'}->{'file_too_large'},upper_path
($virtual),{SIZE
=> $config->{'max_file_size'}})
264 $uselist->add_file($virtual);
267 my $content = file_read
($physical);
268 $$content =~ s/\015\012|\012|\015/\n/g;
270 my $tpl = new Template
;
271 $tpl->read_file($config->{'templates'}->{'editfile'});
273 $tpl->fillin("FILE",$virtual);
274 $tpl->fillin("DIR",upper_path
($virtual));
275 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
276 $tpl->fillin("SCRIPT",$script);
277 $tpl->fillin("CONTENT",encode_entities
($$content));
279 my $output = header
(-type
=> "text/html");
280 $output .= $tpl->get_template;
291 # Params: 1. Reference to user input hash
292 # 2. Reference to config hash
294 # Return: Output of the command (Scalar Reference)
296 sub exec_canceledit
($$)
298 my ($data,$config) = @_;
299 my $virtual = $data->{'virtual'};
301 file_unlock
($data->{'uselist'},$virtual);
302 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
307 # Save a file, unlock it and return to directory view
309 # Params: 1. Reference to user input hash
310 # 2. Reference to config hash
312 # Return: Output of the command (Scalar Reference)
316 my ($data,$config) = @_;
317 my $physical = $data->{'physical'};
318 my $virtual = $data->{'virtual'};
319 my $content = $data->{'cgi'}->param('filecontent');
320 my $uselist = $data->{'uselist'};
324 $content =~ s/\015\012|\012|\015/\n/g;
326 if($data->{'cgi'}->param('encode_iso'))
328 # Encode all ISO-8859-1 special chars
330 $content = encode_entities
($content,"\200-\377");
333 if($data->{'cgi'}->param('saveas'))
335 # Create the new filename
337 $physical = $data->{'new_physical'};
338 $virtual = $data->{'new_virtual'};
340 # Check if someone else is editing the new file
342 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
345 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
346 return error
($config->{'errors'}->{'noedit'}, upper_path
($virtual)) unless(-r
$physical && -w
$physical);
348 if(file_save
($physical,\
$content))
350 # Saving of the file was successful - so unlock it!
352 file_unlock
($uselist,$data->{'virtual'});
354 # Maybe the user saved the file using another filename...
355 # But we have to unlock the original file!
357 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
361 return error
($config->{'errors'}->{'edit_failed'},upper_path
($virtual),{FILE
=> $virtual});
367 # Create a file and return to directory view
369 # Params: 1. Reference to user input hash
370 # 2. Reference to config hash
372 # Return: Output of the command (Scalar Reference)
376 my ($data,$config) = @_;
377 my $new_physical = $data->{'new_physical'};
378 my $new_virtual = $data->{'new_virtual'};
379 my $dir = upper_path
($new_virtual);
380 $new_virtual = encode_entities
($new_virtual);
384 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
386 file_create
($new_physical) or return error
($config->{'errors'}->{'mkfile_failed'},$dir,{FILE
=> $new_virtual});
387 return devedit_reload
({command
=> 'show', file
=> $dir});
391 my $tpl = new Template
;
392 $tpl->read_file($config->{'templates'}->{'mkfile'});
394 $tpl->fillin("DIR","/");
395 $tpl->fillin("SCRIPT",$script);
397 my $output = header
(-type
=> "text/html");
398 $output .= $tpl->get_template;
406 # Create a directory and return to directory view
408 # Params: 1. Reference to user input hash
409 # 2. Reference to config hash
411 # Return: Output of the command (Scalar Reference)
415 my ($data,$config) = @_;
416 my $new_physical = $data->{'new_physical'};
417 my $new_virtual = $data->{'new_virtual'};
418 my $dir = upper_path
($new_virtual);
419 $new_virtual = encode_entities
($new_virtual);
421 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
425 mkdir($new_physical,0777) or return error
($config->{'errors'}->{'mkdir_failed'},$dir,{DIR
=> $new_virtual});
426 return devedit_reload
({command
=> 'show', file
=> $dir});
430 my $tpl = new Template
;
431 $tpl->read_file($config->{'templates'}->{'mkdir'});
433 $tpl->fillin("DIR","/");
434 $tpl->fillin("SCRIPT",$script);
436 my $output = header
(-type
=> "text/html");
437 $output .= $tpl->get_template;
447 # Params: 1. Reference to user input hash
448 # 2. Reference to config hash
450 # Return: Output of the command (Scalar Reference)
454 my ($data,$config) = @_;
455 my $physical = $data->{'physical'};
456 my $virtual = $data->{'virtual'};
457 my $cgi = $data->{'cgi'};
459 if(my $uploaded_file = $cgi->param('uploaded_file'))
461 # Process file upload
463 my $filename = file_name
($uploaded_file);
464 my $file_phys = $physical."/".$filename;
465 my $file_virt = $virtual."".$filename;
467 return error
($config->{'errors'}->{'file_exists'},$virtual,{FILE
=> $file_virt}) if(-e
$file_phys);
469 my $ascii = $cgi->param('ascii');
470 my $handle = $cgi->upload('uploaded_file');
474 open(FILE
,">$file_phys") or return error
($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE
=> $file_virt});
475 binmode(FILE
) unless($ascii);
477 # Read transferred file and write it to disk
479 read($handle, my $data, -s
$handle);
480 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
485 return devedit_reload
({command
=> "show", file
=> $virtual});
489 my $tpl = new Template
;
490 $tpl->read_file($config->{'templates'}->{'upload'});
492 $tpl->fillin("DIR",$virtual);
493 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
494 $tpl->fillin("SCRIPT",$script);
496 my $output = header
(-type
=> "text/html");
497 $output .= $tpl->get_template;
505 # Copy a file and return to directory view
507 # Params: 1. Reference to user input hash
508 # 2. Reference to config hash
510 # Return: Output of the command (Scalar Reference)
514 my ($data,$config) = @_;
515 my $physical = $data->{'physical'};
516 my $virtual = encode_entities
($data->{'virtual'});
517 my $new_physical = $data->{'new_physical'};
519 return error
($config->{'errors'}->{'nocopy'}) unless(-r
$physical);
523 my $new_virtual = $data->{'new_virtual'};
524 my $dir = upper_path
($new_virtual);
525 $new_virtual = encode_entities
($new_virtual);
529 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
533 return error
($config->{'errors'}->{'dircopy'});
535 elsif(not $data->{'cgi'}->param('confirmed'))
537 my $tpl = new Template
;
538 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
540 $tpl->fillin("FILE",$virtual);
541 $tpl->fillin("NEW_FILE",$new_virtual);
542 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
543 $tpl->fillin("NEW_DIR",$dir);
544 $tpl->fillin("DIR",upper_path
($virtual));
546 $tpl->fillin("COMMAND","copy");
547 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
548 $tpl->fillin("SCRIPT",$script);
550 my $output = header
(-type
=> "text/html");
551 $output .= $tpl->get_template;
557 copy
($physical,$new_physical) or return error
($config->{'errors'}->{'copy_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
558 return devedit_reload
({command
=> 'show', file
=> $dir});
562 my $tpl = new Template
;
563 $tpl->read_file($config->{'templates'}->{'copyfile'});
565 $tpl->fillin("FILE",$virtual);
566 $tpl->fillin("DIR",upper_path
($virtual));
567 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
568 $tpl->fillin("SCRIPT",$script);
570 my $output = header
(-type
=> "text/html");
571 $output .= $tpl->get_template;
579 # Rename/move a file and return to directory view
581 # Params: 1. Reference to user input hash
582 # 2. Reference to config hash
584 # Return: Output of the command (Scalar Reference)
588 my ($data,$config) = @_;
589 my $physical = $data->{'physical'};
590 my $virtual = $data->{'virtual'};
591 my $new_physical = $data->{'new_physical'};
593 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
597 my $new_virtual = $data->{'new_virtual'};
598 my $dir = upper_path
($new_virtual);
599 $new_virtual = encode_entities
($new_virtual);
603 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
607 return error
($config->{'errors'}->{'dircopy'});
609 elsif(not $data->{'cgi'}->param('confirmed'))
611 my $tpl = new Template
;
612 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
614 $tpl->fillin("FILE",$virtual);
615 $tpl->fillin("NEW_FILE",$new_virtual);
616 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
617 $tpl->fillin("NEW_DIR",$dir);
618 $tpl->fillin("DIR",upper_path
($virtual));
620 $tpl->fillin("COMMAND","rename");
621 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
622 $tpl->fillin("SCRIPT",$script);
624 my $output = header
(-type
=> "text/html");
625 $output .= $tpl->get_template;
631 rename($physical,$new_physical) or return error
($config->{'errors'}->{'rename_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
632 return devedit_reload
({command
=> 'show', file
=> $dir});
636 my $tpl = new Template
;
637 $tpl->read_file($config->{'templates'}->{'renamefile'});
639 $tpl->fillin("FILE",$virtual);
640 $tpl->fillin("DIR",upper_path
($virtual));
641 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
642 $tpl->fillin("SCRIPT",$script);
644 my $output = header
(-type
=> "text/html");
645 $output .= $tpl->get_template;
653 # Remove a file or a directory and return to directory view
655 # Params: 1. Reference to user input hash
656 # 2. Reference to config hash
658 # Return: Output of the command (Scalar Reference)
662 my ($data,$config) = @_;
663 my $physical = $data->{'physical'};
664 my $virtual = $data->{'virtual'};
670 if($data->{'cgi'}->param('confirmed'))
673 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
677 my $tpl = new Template
;
678 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
680 $tpl->fillin("DIR",$virtual);
681 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
682 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
683 $tpl->fillin("SCRIPT",$script);
685 my $output = header
(-type
=> "text/html");
686 $output .= $tpl->get_template;
695 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
697 if($data->{'cgi'}->param('confirmed'))
699 unlink($physical) or return error
($config->{'errors'}->{'delete_failed'},upper_path
($virtual),{FILE
=> $virtual});
700 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
704 my $tpl = new Template
;
705 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
707 $tpl->fillin("FILE",$virtual);
708 $tpl->fillin("DIR",upper_path
($virtual));
709 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
710 $tpl->fillin("SCRIPT",$script);
712 my $output = header
(-type
=> "text/html");
713 $output .= $tpl->get_template;
722 # Remove a file from the list of used files and
723 # return to directory view
725 # Params: 1. Reference to user input hash
726 # 2. Reference to config hash
728 # Return: Output of the command (Scalar Reference)
732 my ($data,$config) = @_;
733 my $virtual = $data->{'virtual'};
735 if($data->{'cgi'}->param('confirmed'))
737 file_unlock
($data->{'uselist'},$virtual);
738 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
742 my $tpl = new Template
;
743 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
745 $tpl->fillin("FILE",$virtual);
746 $tpl->fillin("DIR",upper_path
($virtual));
747 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
748 $tpl->fillin("SCRIPT",$script);
750 my $output = header
(-type
=> "text/html");
751 $output .= $tpl->get_template;
759 # Display some information about Dev-Editor
761 # Params: 1. Reference to user input hash
762 # 2. Reference to config hash
764 # Return: Output of the command (Scalar Reference)
768 my ($data,$config) = @_;
770 my $tpl = new Template
;
771 $tpl->read_file($config->{'templates'}->{'about'});
773 $tpl->fillin("SCRIPT",$script);
775 # Dev-Editor's version number
777 $tpl->fillin("VERSION",$data->{'version'});
779 # Some path information
781 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
782 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
783 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
784 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
788 $tpl->fillin("PERL_PROG",$^X
);
789 $tpl->fillin("PERL_VER",sprintf("%vd",$^V
));
791 # Information about the server
793 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
794 $tpl->fillin("OS",$^O
);
795 $tpl->fillin("TIME",strftime
($config->{'timeformat'},localtime));
797 # Process information
799 $tpl->fillin("PID",$$);
801 # Check if the functions getpwuid() and getgrgid() are available
803 if(eval("getpwuid(0)") && eval("getgrgid(0)"))
805 # Dev-Editor is running on a system which allows users and groups
806 # So we display the user and the group of our process
808 $tpl->parse_if_block("users",1);
810 # ID's of user and group
812 $tpl->fillin("UID",$<);
813 $tpl->fillin("GID",$();
815 # Names of user and group
817 $tpl->fillin("USER",getpwuid($<));
818 $tpl->fillin("GROUP",getgrgid($());
822 $tpl->parse_if_block("users",0);
825 my $output = header
(-type
=> "text/html");
826 $output .= $tpl->get_template;
831 # it's true, baby ;-)
patrick-canterino.de