]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
6db5d07b5429b8dc83d8da99dd9f4d6d87a7f64e
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2004-07-11
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'};
85 my $tpl = new Template
;
89 # Create directory listing
91 my $direntries = dir_read
($physical);
92 return error
($config->{'dir_read_failed'},upper_path
($virtual),{DIR
=> '$virtual'}) unless($direntries);
94 my $files = $direntries->{'files'};
95 my $dirs = $direntries->{'dirs'};
99 # Create the link to the upper directory
100 # (only if we are not in the root directory)
102 unless($virtual eq "/")
104 my @stat = stat($physical."/..");
106 my $udtpl = new Template
;
107 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
109 $udtpl->fillin("UPPER_DIR",encode_entities
(upper_path
($virtual)));
110 $udtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
112 $dirlist .= $udtpl->get_template;
117 foreach my $dir(@
$dirs)
119 my @stat = stat($physical."/".$dir);
120 my $virt_path = encode_entities
($virtual.$dir."/");
122 my $dtpl = new Template
;
123 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
125 $dtpl->fillin("DIR",$virt_path);
126 $dtpl->fillin("DIR_NAME",$dir);
127 $dtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
128 $dtpl->fillin("URL",equal_url
($config->{'httproot'},$virt_path));
130 $dirlist .= $dtpl->get_template;
135 foreach my $file(@
$files)
137 my $phys_path = $physical."/".$file;
138 my $virt_path = encode_entities
($virtual.$file);
140 my @stat = stat($phys_path);
141 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
143 my $ftpl = new Template
;
144 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
146 $ftpl->fillin("FILE",$virt_path);
147 $ftpl->fillin("FILE_NAME",$file);
148 $ftpl->fillin("SIZE",$stat[7]);
149 $ftpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
150 $ftpl->fillin("URL",equal_url
($config->{'httproot'},$virt_path));
152 $ftpl->parse_if_block("not_readable",not -r
$phys_path);
153 $ftpl->parse_if_block("binary",-B
$phys_path);
154 $ftpl->parse_if_block("readonly",not -w
$phys_path);
156 $ftpl->parse_if_block("viewable",-r
$phys_path && -T
$phys_path && not ($config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'}));
158 $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);
160 $ftpl->parse_if_block("in_use",$in_use);
161 $ftpl->parse_if_block("unused",not $in_use);
163 $ftpl->parse_if_block("too_large",$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
165 $dirlist .= $ftpl->get_template;
168 $tpl->read_file($config->{'templates'}->{'dirlist'});
170 $tpl->fillin("DIRLIST",$dirlist);
171 $tpl->fillin("DIR",$virtual);
172 $tpl->fillin("SCRIPT",$script);
173 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
179 return error
($config->{'errors'}->{'noview'},upper_path
($virtual)) unless(-r
$physical);
181 # Check on binary files
182 # We have to do it in this way, or empty files
183 # will be recognized as binary files
189 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
195 if($config->{'max_file_size'} && (stat($physical))[7] > $config->{'max_file_size'})
197 return error
($config->{'errors'}->{'file_too_large'},upper_path
($virtual),{SIZE
=> $config->{'max_file_size'}})
201 my $content = file_read
($physical);
202 $$content =~ s/\015\012|\012|\015/\n/g;
204 $tpl->read_file($config->{'templates'}->{'viewfile'});
206 $tpl->fillin("FILE",$virtual);
207 $tpl->fillin("DIR",upper_path
($virtual));
208 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
209 $tpl->fillin("SCRIPT",$script);
210 $tpl->fillin("CONTENT",encode_entities
($$content));
215 my $output = header
(-type
=> "text/html");
216 $output .= $tpl->get_template;
223 # Lock a file and display a form to edit it
225 # Params: 1. Reference to user input hash
226 # 2. Reference to config hash
228 # Return: Output of the command (Scalar Reference)
230 sub exec_beginedit
($$)
232 my ($data,$config) = @_;
233 my $physical = $data->{'physical'};
234 my $virtual = $data->{'virtual'};
235 my $uselist = $data->{'uselist'};
237 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
238 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
239 return error
($config->{'errors'}->{'noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
241 # Check on binary files
247 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
251 if($config->{'max_file_size'} && (stat($physical))[7] > $config->{'max_file_size'})
253 return error
($config->{'errors'}->{'file_too_large'},upper_path
($virtual),{SIZE
=> $config->{'max_file_size'}})
259 $uselist->add_file($virtual);
262 my $content = file_read
($physical);
263 $$content =~ s/\015\012|\012|\015/\n/g;
265 my $tpl = new Template
;
266 $tpl->read_file($config->{'templates'}->{'editfile'});
268 $tpl->fillin("FILE",$virtual);
269 $tpl->fillin("DIR",upper_path
($virtual));
270 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
271 $tpl->fillin("SCRIPT",$script);
272 $tpl->fillin("CONTENT",encode_entities
($$content));
274 my $output = header
(-type
=> "text/html");
275 $output .= $tpl->get_template;
286 # Params: 1. Reference to user input hash
287 # 2. Reference to config hash
289 # Return: Output of the command (Scalar Reference)
291 sub exec_canceledit
($$)
293 my ($data,$config) = @_;
294 my $virtual = $data->{'virtual'};
296 file_unlock
($data->{'uselist'},$virtual);
297 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
302 # Save a file, unlock it and return to directory view
304 # Params: 1. Reference to user input hash
305 # 2. Reference to config hash
307 # Return: Output of the command (Scalar Reference)
311 my ($data,$config) = @_;
312 my $physical = $data->{'physical'};
313 my $virtual = $data->{'virtual'};
314 my $content = $data->{'cgi'}->param('filecontent');
315 my $uselist = $data->{'uselist'};
319 $content =~ s/\015\012|\012|\015/\n/g;
321 if($data->{'cgi'}->param('encode_iso'))
323 # Encode all ISO-8859-1 special chars
325 $content = encode_entities
($content,"\200-\377");
328 if($data->{'cgi'}->param('saveas'))
330 # Create the new filename
332 $physical = $data->{'new_physical'};
333 $virtual = $data->{'new_virtual'};
335 # Check if someone else is editing the new file
337 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
340 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
341 return error
($config->{'errors'}->{'noedit'}, upper_path
($virtual)) unless(-r
$physical && -w
$physical);
343 if(file_save
($physical,\
$content))
345 # Saving of the file was successful - so unlock it!
347 file_unlock
($uselist,$data->{'virtual'});
349 # Maybe the user saved the file using another filename...
350 # But we have to unlock the original file!
352 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
356 return error
($config->{'errors'}->{'edit_failed'},upper_path
($virtual),{FILE
=> $virtual});
362 # Create a file and return to directory view
364 # Params: 1. Reference to user input hash
365 # 2. Reference to config hash
367 # Return: Output of the command (Scalar Reference)
371 my ($data,$config) = @_;
372 my $new_physical = $data->{'new_physical'};
373 my $new_virtual = $data->{'new_virtual'};
374 my $dir = upper_path
($new_virtual);
375 $new_virtual = encode_entities
($new_virtual);
379 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
381 file_create
($new_physical) or return error
($config->{'errors'}->{'mkfile_failed'},$dir,{FILE
=> $new_virtual});
382 return devedit_reload
({command
=> 'show', file
=> $dir});
386 my $tpl = new Template
;
387 $tpl->read_file($config->{'templates'}->{'mkfile'});
389 $tpl->fillin("DIR","/");
390 $tpl->fillin("SCRIPT",$script);
392 my $output = header
(-type
=> "text/html");
393 $output .= $tpl->get_template;
401 # Create a directory and return to directory view
403 # Params: 1. Reference to user input hash
404 # 2. Reference to config hash
406 # Return: Output of the command (Scalar Reference)
410 my ($data,$config) = @_;
411 my $new_physical = $data->{'new_physical'};
412 my $new_virtual = $data->{'new_virtual'};
413 my $dir = upper_path
($new_virtual);
414 $new_virtual = encode_entities
($new_virtual);
416 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
420 mkdir($new_physical,0777) or return error
($config->{'errors'}->{'mkdir_failed'},$dir,{DIR
=> $new_virtual});
421 return devedit_reload
({command
=> 'show', file
=> $dir});
425 my $tpl = new Template
;
426 $tpl->read_file($config->{'templates'}->{'mkdir'});
428 $tpl->fillin("DIR","/");
429 $tpl->fillin("SCRIPT",$script);
431 my $output = header
(-type
=> "text/html");
432 $output .= $tpl->get_template;
442 # Params: 1. Reference to user input hash
443 # 2. Reference to config hash
445 # Return: Output of the command (Scalar Reference)
449 my ($data,$config) = @_;
450 my $physical = $data->{'physical'};
451 my $virtual = $data->{'virtual'};
452 my $cgi = $data->{'cgi'};
454 if(my $uploaded_file = $cgi->param('uploaded_file'))
456 # Process file upload
458 my $filename = file_name
($uploaded_file);
459 my $file_phys = $physical."/".$filename;
460 my $file_virt = $virtual."".$filename;
462 return error
($config->{'errors'}->{'file_exists'},$virtual,{FILE
=> $file_virt}) if(-e
$file_phys);
464 my $ascii = $cgi->param('ascii');
465 my $handle = $cgi->upload('uploaded_file');
469 open(FILE
,">$file_phys") or return error
($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE
=> $file_virt});
470 binmode(FILE
) unless($ascii);
472 # Read transferred file and write it to disk
474 read($handle, my $data, -s
$handle);
475 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
480 return devedit_reload
({command
=> "show", file
=> $virtual});
484 my $tpl = new Template
;
485 $tpl->read_file($config->{'templates'}->{'upload'});
487 $tpl->fillin("DIR",$virtual);
488 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
489 $tpl->fillin("SCRIPT",$script);
491 my $output = header
(-type
=> "text/html");
492 $output .= $tpl->get_template;
500 # Copy a file and return to directory view
502 # Params: 1. Reference to user input hash
503 # 2. Reference to config hash
505 # Return: Output of the command (Scalar Reference)
509 my ($data,$config) = @_;
510 my $physical = $data->{'physical'};
511 my $virtual = encode_entities
($data->{'virtual'});
512 my $new_physical = $data->{'new_physical'};
514 return error
($config->{'errors'}->{'nocopy'}) unless(-r
$physical);
518 my $new_virtual = $data->{'new_virtual'};
519 my $dir = upper_path
($new_virtual);
520 $new_virtual = encode_entities
($new_virtual);
524 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
528 return error
($config->{'errors'}->{'dircopy'});
530 elsif(not $data->{'cgi'}->param('confirmed'))
532 my $tpl = new Template
;
533 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
535 $tpl->fillin("FILE",$virtual);
536 $tpl->fillin("NEW_FILE",$new_virtual);
537 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
538 $tpl->fillin("NEW_DIR",$dir);
539 $tpl->fillin("DIR",upper_path
($virtual));
541 $tpl->fillin("COMMAND","copy");
542 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
543 $tpl->fillin("SCRIPT",$script);
545 my $output = header
(-type
=> "text/html");
546 $output .= $tpl->get_template;
552 copy
($physical,$new_physical) or return error
($config->{'errors'}->{'copy_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
553 return devedit_reload
({command
=> 'show', file
=> $dir});
557 my $tpl = new Template
;
558 $tpl->read_file($config->{'templates'}->{'copyfile'});
560 $tpl->fillin("FILE",$virtual);
561 $tpl->fillin("DIR",upper_path
($virtual));
562 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
563 $tpl->fillin("SCRIPT",$script);
565 my $output = header
(-type
=> "text/html");
566 $output .= $tpl->get_template;
574 # Rename/move a file and return to directory view
576 # Params: 1. Reference to user input hash
577 # 2. Reference to config hash
579 # Return: Output of the command (Scalar Reference)
583 my ($data,$config) = @_;
584 my $physical = $data->{'physical'};
585 my $virtual = $data->{'virtual'};
586 my $new_physical = $data->{'new_physical'};
588 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
592 my $new_virtual = $data->{'new_virtual'};
593 my $dir = upper_path
($new_virtual);
594 $new_virtual = encode_entities
($new_virtual);
598 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
602 return error
($config->{'errors'}->{'dircopy'});
604 elsif(not $data->{'cgi'}->param('confirmed'))
606 my $tpl = new Template
;
607 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
609 $tpl->fillin("FILE",$virtual);
610 $tpl->fillin("NEW_FILE",$new_virtual);
611 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
612 $tpl->fillin("NEW_DIR",$dir);
613 $tpl->fillin("DIR",upper_path
($virtual));
615 $tpl->fillin("COMMAND","rename");
616 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
617 $tpl->fillin("SCRIPT",$script);
619 my $output = header
(-type
=> "text/html");
620 $output .= $tpl->get_template;
626 rename($physical,$new_physical) or return error
($config->{'errors'}->{'rename_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
627 return devedit_reload
({command
=> 'show', file
=> $dir});
631 my $tpl = new Template
;
632 $tpl->read_file($config->{'templates'}->{'renamefile'});
634 $tpl->fillin("FILE",$virtual);
635 $tpl->fillin("DIR",upper_path
($virtual));
636 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
637 $tpl->fillin("SCRIPT",$script);
639 my $output = header
(-type
=> "text/html");
640 $output .= $tpl->get_template;
648 # Remove a file or a directory and return to directory view
650 # Params: 1. Reference to user input hash
651 # 2. Reference to config hash
653 # Return: Output of the command (Scalar Reference)
657 my ($data,$config) = @_;
658 my $physical = $data->{'physical'};
659 my $virtual = $data->{'virtual'};
665 if($data->{'cgi'}->param('confirmed'))
668 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
672 my $tpl = new Template
;
673 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
675 $tpl->fillin("DIR",$virtual);
676 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
677 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
678 $tpl->fillin("SCRIPT",$script);
680 my $output = header
(-type
=> "text/html");
681 $output .= $tpl->get_template;
690 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
692 if($data->{'cgi'}->param('confirmed'))
694 unlink($physical) or return error
($config->{'errors'}->{'delete_failed'},upper_path
($virtual),{FILE
=> $virtual});
695 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
699 my $tpl = new Template
;
700 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
702 $tpl->fillin("FILE",$virtual);
703 $tpl->fillin("DIR",upper_path
($virtual));
704 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
705 $tpl->fillin("SCRIPT",$script);
707 my $output = header
(-type
=> "text/html");
708 $output .= $tpl->get_template;
717 # Remove a file from the list of used files and
718 # return to directory view
720 # Params: 1. Reference to user input hash
721 # 2. Reference to config hash
723 # Return: Output of the command (Scalar Reference)
727 my ($data,$config) = @_;
728 my $virtual = $data->{'virtual'};
730 if($data->{'cgi'}->param('confirmed'))
732 file_unlock
($data->{'uselist'},$virtual);
733 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
737 my $tpl = new Template
;
738 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
740 $tpl->fillin("FILE",$virtual);
741 $tpl->fillin("DIR",upper_path
($virtual));
742 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
743 $tpl->fillin("SCRIPT",$script);
745 my $output = header
(-type
=> "text/html");
746 $output .= $tpl->get_template;
754 # Display some information about Dev-Editor
756 # Params: 1. Reference to user input hash
757 # 2. Reference to config hash
759 # Return: Output of the command (Scalar Reference)
763 my ($data,$config) = @_;
765 my $tpl = new Template
;
766 $tpl->read_file($config->{'templates'}->{'about'});
768 $tpl->fillin("SCRIPT",$script);
770 # Dev-Editor's version number
772 $tpl->fillin("VERSION",$data->{'version'});
774 # Some path information
776 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
777 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
778 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
779 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
783 $tpl->fillin("PERL_PROG",$^X
);
784 $tpl->fillin("PERL_VER",sprintf("%vd",$^V
));
786 # Information about the server
788 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
789 $tpl->fillin("OS",$^O
);
790 $tpl->fillin("TIME",strftime
($config->{'timeformat'},localtime));
792 # Process information
794 $tpl->fillin("PID",$$);
796 # Check if the functions getpwuid() and getgrgid() are available
798 if(eval("getpwuid(0)") && eval("getgrgid(0)"))
800 # Dev-Editor is running on a system which allows users and groups
801 # So we display the user and the group of our process
803 $tpl->parse_if_block("users",1);
805 # ID's of user and group
807 $tpl->fillin("UID",$<);
808 $tpl->fillin("GID",$();
810 # Names of user and group
812 $tpl->fillin("USER",getpwuid($<));
813 $tpl->fillin("GROUP",getgrgid($());
817 $tpl->parse_if_block("users",0);
820 my $output = header
(-type
=> "text/html");
821 $output .= $tpl->get_template;
826 # it's true, baby ;-)
patrick-canterino.de