]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2004-07-03
20 use POSIX qw(strftime);
30 my $script = $ENV{'SCRIPT_NAME'};
32 my %dispatch = ('show' => \
&exec_show
,
33 'beginedit' => \
&exec_beginedit
,
34 'canceledit' => \
&exec_canceledit
,
35 'endedit' => \
&exec_endedit
,
36 'mkdir' => \
&exec_mkdir
,
37 'mkfile' => \
&exec_mkfile
,
38 'upload' => \
&exec_upload
,
39 'copy' => \
&exec_copy
,
40 'rename' => \
&exec_rename
,
41 'remove' => \
&exec_remove
,
42 'unlock' => \
&exec_unlock
,
43 'about' => \
&exec_about
48 use base
qw(Exporter);
50 @EXPORT = qw(exec_command);
54 # Execute the specified command
56 # Params: 1. Command to execute
57 # 2. Reference to user input hash
58 # 3. Reference to config hash
60 # Return: Output of the command (Scalar Reference)
64 my ($command,$data,$config) = @_;
66 return error
($config->{'errors'}->{'cmd_unknown'},'/',{COMMAND
=> $command}) unless($dispatch{$command});
68 my $output = &{$dispatch{$command}}($data,$config);
74 # View a directory or a file
76 # Params: 1. Reference to user input hash
77 # 2. Reference to config hash
79 # Return: Output of the command (Scalar Reference)
83 my ($data,$config) = @_;
84 my $physical = $data->{'physical'};
85 my $virtual = $data->{'virtual'};
87 my $tpl = new Template
;
91 # Create directory listing
93 my $direntries = dir_read
($physical);
94 return error
($config->{'dir_read_failed'},upper_path
($virtual),{DIR
=> '$virtual'}) unless($direntries);
96 my $files = $direntries->{'files'};
97 my $dirs = $direntries->{'dirs'};
101 # Create the link to the upper directory
102 # (only if we are not in the root directory)
104 unless($virtual eq "/")
106 my @stat = stat($physical."/..");
108 my $udtpl = new Template
;
109 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
111 $udtpl->fillin("UPPER_DIR",encode_entities
(upper_path
($virtual)));
112 $udtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
114 $dirlist .= $udtpl->get_template;
119 foreach my $dir(@
$dirs)
121 my @stat = stat($physical."/".$dir);
122 my $virt_path = encode_entities
($virtual.$dir."/");
124 my $dtpl = new Template
;
125 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
127 $dtpl->fillin("DIR",$virt_path);
128 $dtpl->fillin("DIR_NAME",$dir);
129 $dtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
130 $dtpl->fillin("URL",equal_url
($config->{'httproot'},$virt_path));
132 $dirlist .= $dtpl->get_template;
137 foreach my $file(@
$files)
139 my $phys_path = $physical."/".$file;
140 my $virt_path = encode_entities
($virtual.$file);
142 my @stat = stat($phys_path);
143 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
145 my $ftpl = new Template
;
146 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
148 $ftpl->fillin("FILE",$virt_path);
149 $ftpl->fillin("FILE_NAME",$file);
150 $ftpl->fillin("SIZE",$stat[7]);
151 $ftpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
152 $ftpl->fillin("URL",equal_url
($config->{'httproot'},$virt_path));
154 $ftpl->parse_if_block("not_readable",not -r
$phys_path);
155 $ftpl->parse_if_block("binary",-B
$phys_path);
156 $ftpl->parse_if_block("readonly",not -w
$phys_path);
158 $ftpl->parse_if_block("viewable",-r
$phys_path && -T
$phys_path && not ($config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'}));
160 $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);
162 $ftpl->parse_if_block("in_use",$in_use);
163 $ftpl->parse_if_block("unused",not $in_use);
165 $ftpl->parse_if_block("too_large",$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
167 $dirlist .= $ftpl->get_template;
170 $tpl->read_file($config->{'templates'}->{'dirlist'});
172 $tpl->fillin("DIRLIST",$dirlist);
173 $tpl->fillin("DIR",$virtual);
174 $tpl->fillin("SCRIPT",$script);
175 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
181 return error
($config->{'errors'}->{'noview'},upper_path
($virtual)) unless(-r
$physical);
183 # Check on binary files
184 # We have to do it in this way, or empty files
185 # will be recognized as binary files
191 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
197 if($config->{'max_file_size'} && (stat($physical))[7] > $config->{'max_file_size'})
199 return error
($config->{'errors'}->{'file_too_large'},upper_path
($virtual),{SIZE
=> $config->{'max_file_size'}})
203 my $content = file_read
($physical);
204 $$content =~ s/\015\012|\012|\015/\n/g;
206 $tpl->read_file($config->{'templates'}->{'viewfile'});
208 $tpl->fillin("FILE",$virtual);
209 $tpl->fillin("DIR",upper_path
($virtual));
210 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
211 $tpl->fillin("SCRIPT",$script);
212 $tpl->fillin("CONTENT",encode_entities
($$content));
217 my $output = header
(-type
=> "text/html");
218 $output .= $tpl->get_template;
225 # Lock a file and display a form to edit it
227 # Params: 1. Reference to user input hash
228 # 2. Reference to config hash
230 # Return: Output of the command (Scalar Reference)
232 sub exec_beginedit
($$)
234 my ($data,$config) = @_;
235 my $physical = $data->{'physical'};
236 my $virtual = $data->{'virtual'};
237 my $uselist = $data->{'uselist'};
239 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
240 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
241 return error
($config->{'errors'}->{'noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
243 # Check on binary files
249 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
253 if($config->{'max_file_size'} && (stat($physical))[7] > $config->{'max_file_size'})
255 return error
($config->{'errors'}->{'file_too_large'},upper_path
($virtual),{SIZE
=> $config->{'max_file_size'}})
261 $uselist->add_file($virtual);
264 my $content = file_read
($physical);
265 $$content =~ s/\015\012|\012|\015/\n/g;
267 my $tpl = new Template
;
268 $tpl->read_file($config->{'templates'}->{'editfile'});
270 $tpl->fillin("FILE",$virtual);
271 $tpl->fillin("DIR",upper_path
($virtual));
272 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
273 $tpl->fillin("SCRIPT",$script);
274 $tpl->fillin("CONTENT",encode_entities
($$content));
276 my $output = header
(-type
=> "text/html");
277 $output .= $tpl->get_template;
288 # Params: 1. Reference to user input hash
289 # 2. Reference to config hash
291 # Return: Output of the command (Scalar Reference)
293 sub exec_canceledit
($$)
295 my ($data,$config) = @_;
296 my $virtual = $data->{'virtual'};
298 file_unlock
($data->{'uselist'},$virtual);
299 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
304 # Save a file, unlock it and return to directory view
306 # Params: 1. Reference to user input hash
307 # 2. Reference to config hash
309 # Return: Output of the command (Scalar Reference)
313 my ($data,$config) = @_;
314 my $physical = $data->{'physical'};
315 my $virtual = $data->{'virtual'};
316 my $content = $data->{'cgi'}->param('filecontent');
317 my $uselist = $data->{'uselist'};
321 $content =~ s/\015\012|\012|\015/\n/g;
323 if($data->{'cgi'}->param('encode_iso'))
325 # Encode all ISO-8859-1 special chars
327 $content = encode_entities
($content,"\200-\377");
330 if($data->{'cgi'}->param('saveas'))
332 # Create the new filename
334 $physical = $data->{'new_physical'};
335 $virtual = $data->{'new_virtual'};
337 # Check if someone else is editing the new file
339 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
342 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
343 return error
($config->{'errors'}->{'noedit'}, upper_path
($virtual)) unless(-r
$physical && -w
$physical);
345 if(file_save
($physical,\
$content))
347 # Saving of the file was successful - so unlock it!
349 file_unlock
($uselist,$data->{'virtual'});
351 # Maybe the user saved the file using another filename...
352 # But we have to unlock the original file!
354 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
358 return error
($config->{'errors'}->{'edit_failed'},upper_path
($virtual),{FILE
=> $virtual});
364 # Create a file and return to directory view
366 # Params: 1. Reference to user input hash
367 # 2. Reference to config hash
369 # Return: Output of the command (Scalar Reference)
373 my ($data,$config) = @_;
374 my $new_physical = $data->{'new_physical'};
375 my $new_virtual = $data->{'new_virtual'};
376 my $dir = upper_path
($new_virtual);
377 $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});
387 # Create a directory and return to directory view
389 # Params: 1. Reference to user input hash
390 # 2. Reference to config hash
392 # Return: Output of the command (Scalar Reference)
396 my ($data,$config) = @_;
397 my $new_physical = $data->{'new_physical'};
398 my $new_virtual = $data->{'new_virtual'};
399 my $dir = upper_path
($new_virtual);
400 $new_virtual = encode_entities
($new_virtual);
402 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
404 mkdir($new_physical,0777) or return error
($config->{'errors'}->{'mkdir_failed'},$dir,{DIR
=> $new_virtual});
405 return devedit_reload
({command
=> 'show', file
=> $dir});
412 # Params: 1. Reference to user input hash
413 # 2. Reference to config hash
415 # Return: Output of the command (Scalar Reference)
419 my ($data,$config) = @_;
420 my $physical = $data->{'physical'};
421 my $virtual = $data->{'virtual'};
422 my $cgi = $data->{'cgi'};
424 if(my $uploaded_file = $cgi->param('uploaded_file'))
426 # Process file upload
428 my $filename = file_name
($uploaded_file);
429 my $file_phys = $physical."/".$filename;
430 my $file_virt = $virtual."".$filename;
432 return error
($config->{'errors'}->{'file_exists'},$virtual,{FILE
=> $file_virt}) if(-e
$file_phys);
434 my $ascii = $cgi->param('ascii');
435 my $handle = $cgi->upload('uploaded_file');
439 open(FILE
,">$file_phys") or return error
($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE
=> $file_virt});
440 binmode(FILE
) unless($ascii);
442 # Read transferred file and write it to disk
444 read($handle, my $data, -s
$handle);
445 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
450 return devedit_reload
({command
=> "show", file
=> $virtual});
454 my $tpl = new Template
;
455 $tpl->read_file($config->{'templates'}->{'upload'});
457 $tpl->fillin("DIR",$virtual);
458 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
459 $tpl->fillin("SCRIPT",$script);
461 my $output = header
(-type
=> "text/html");
462 $output .= $tpl->get_template;
470 # Copy a file and return to directory view
472 # Params: 1. Reference to user input hash
473 # 2. Reference to config hash
475 # Return: Output of the command (Scalar Reference)
479 my ($data,$config) = @_;
480 my $physical = $data->{'physical'};
481 my $virtual = encode_entities
($data->{'virtual'});
482 my $new_physical = $data->{'new_physical'};
484 return error
($config->{'errors'}->{'nocopy'}) unless(-r
$physical);
488 my $new_virtual = $data->{'new_virtual'};
489 my $dir = upper_path
($new_virtual);
490 $new_virtual = encode_entities
($new_virtual);
494 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
498 return error
($config->{'errors'}->{'dircopy'});
500 elsif(not $data->{'cgi'}->param('confirmed'))
502 my $tpl = new Template
;
503 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
505 $tpl->fillin("FILE",$virtual);
506 $tpl->fillin("NEW_FILE",$new_virtual);
507 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
508 $tpl->fillin("NEW_DIR",$dir);
509 $tpl->fillin("DIR",upper_path
($virtual));
511 $tpl->fillin("COMMAND","copy");
512 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
513 $tpl->fillin("SCRIPT",$script);
515 my $output = header
(-type
=> "text/html");
516 $output .= $tpl->get_template;
522 copy
($physical,$new_physical) or return error
($config->{'errors'}->{'copy_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
523 return devedit_reload
({command
=> 'show', file
=> $dir});
527 my $tpl = new Template
;
528 $tpl->read_file($config->{'templates'}->{'copyfile'});
530 $tpl->fillin("FILE",$virtual);
531 $tpl->fillin("DIR",upper_path
($virtual));
532 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
533 $tpl->fillin("SCRIPT",$script);
535 my $output = header
(-type
=> "text/html");
536 $output .= $tpl->get_template;
544 # Rename/move a file and return to directory view
546 # Params: 1. Reference to user input hash
547 # 2. Reference to config hash
549 # Return: Output of the command (Scalar Reference)
553 my ($data,$config) = @_;
554 my $physical = $data->{'physical'};
555 my $virtual = $data->{'virtual'};
556 my $new_physical = $data->{'new_physical'};
558 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
562 my $new_virtual = $data->{'new_virtual'};
563 my $dir = upper_path
($new_virtual);
564 $new_virtual = encode_entities
($new_virtual);
568 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
572 return error
($config->{'errors'}->{'dircopy'});
574 elsif(not $data->{'cgi'}->param('confirmed'))
576 my $tpl = new Template
;
577 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
579 $tpl->fillin("FILE",$virtual);
580 $tpl->fillin("NEW_FILE",$new_virtual);
581 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
582 $tpl->fillin("NEW_DIR",$dir);
583 $tpl->fillin("DIR",upper_path
($virtual));
585 $tpl->fillin("COMMAND","rename");
586 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
587 $tpl->fillin("SCRIPT",$script);
589 my $output = header
(-type
=> "text/html");
590 $output .= $tpl->get_template;
596 rename($physical,$new_physical) or return error
($config->{'errors'}->{'rename_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
597 return devedit_reload
({command
=> 'show', file
=> $dir});
601 my $tpl = new Template
;
602 $tpl->read_file($config->{'templates'}->{'renamefile'});
604 $tpl->fillin("FILE",$virtual);
605 $tpl->fillin("DIR",upper_path
($virtual));
606 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
607 $tpl->fillin("SCRIPT",$script);
609 my $output = header
(-type
=> "text/html");
610 $output .= $tpl->get_template;
618 # Remove a file or a directory and return to directory view
620 # Params: 1. Reference to user input hash
621 # 2. Reference to config hash
623 # Return: Output of the command (Scalar Reference)
627 my ($data,$config) = @_;
628 my $physical = $data->{'physical'};
629 my $virtual = $data->{'virtual'};
635 if($data->{'cgi'}->param('confirmed'))
638 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
642 my $tpl = new Template
;
643 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
645 $tpl->fillin("DIR",$virtual);
646 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
647 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
648 $tpl->fillin("SCRIPT",$script);
650 my $output = header
(-type
=> "text/html");
651 $output .= $tpl->get_template;
660 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
662 if($data->{'cgi'}->param('confirmed'))
664 unlink($physical) or return error
($config->{'errors'}->{'delete_failed'},upper_path
($virtual),{FILE
=> $virtual});
665 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
669 my $tpl = new Template
;
670 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
672 $tpl->fillin("FILE",$virtual);
673 $tpl->fillin("DIR",upper_path
($virtual));
674 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
675 $tpl->fillin("SCRIPT",$script);
677 my $output = header
(-type
=> "text/html");
678 $output .= $tpl->get_template;
687 # Remove a file from the list of used files and
688 # return to directory view
690 # Params: 1. Reference to user input hash
691 # 2. Reference to config hash
693 # Return: Output of the command (Scalar Reference)
697 my ($data,$config) = @_;
698 my $virtual = $data->{'virtual'};
700 if($data->{'cgi'}->param('confirmed'))
702 file_unlock
($data->{'uselist'},$virtual);
703 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
707 my $tpl = new Template
;
708 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
710 $tpl->fillin("FILE",$virtual);
711 $tpl->fillin("DIR",upper_path
($virtual));
712 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
713 $tpl->fillin("SCRIPT",$script);
715 my $output = header
(-type
=> "text/html");
716 $output .= $tpl->get_template;
724 # Display some information about Dev-Editor
726 # Params: 1. Reference to user input hash
727 # 2. Reference to config hash
729 # Return: Output of the command (Scalar Reference)
733 my ($data,$config) = @_;
735 my $tpl = new Template
;
736 $tpl->read_file($config->{'templates'}->{'about'});
738 $tpl->fillin("SCRIPT",$script);
740 # Dev-Editor's version number
742 $tpl->fillin("VERSION",$data->{'version'});
744 # Some path information
746 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
747 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
748 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
749 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
753 $tpl->fillin("PERL_PROG",$^X
);
754 $tpl->fillin("PERL_VER",sprintf("%vd",$^V
));
756 # Information about the server
758 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
759 $tpl->fillin("OS",$^O
);
760 $tpl->fillin("TIME",strftime
($config->{'timeformat'},localtime));
762 # Process information
764 $tpl->fillin("PID",$$);
766 # Check if the functions getpwuid() and getgrgid() are available
768 if(eval("getpwuid(0)") && eval("getgrgid(0)"))
770 # Dev-Editor is running on a system which allows users and groups
771 # So we display the user and the group of our process
773 $tpl->parse_if_block("users",1);
775 # ID's of user and group
777 $tpl->fillin("UID",$<);
778 $tpl->fillin("GID",$();
780 # Names of user and group
782 $tpl->fillin("USER",getpwuid($<));
783 $tpl->fillin("GROUP",getgrgid($());
787 $tpl->parse_if_block("users",0);
790 my $output = header
(-type
=> "text/html");
791 $output .= $tpl->get_template;
796 # it's true, baby ;-)
patrick-canterino.de