]>
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-22
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'}->{'rename_root'},"/") if($virtual eq "/");
594 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
598 my $new_virtual = $data->{'new_virtual'};
599 my $dir = upper_path
($new_virtual);
600 $new_virtual = encode_entities
($new_virtual);
604 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
608 return error
($config->{'errors'}->{'dircopy'});
610 elsif(not $data->{'cgi'}->param('confirmed'))
612 my $tpl = new Template
;
613 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
615 $tpl->fillin("FILE",$virtual);
616 $tpl->fillin("NEW_FILE",$new_virtual);
617 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
618 $tpl->fillin("NEW_DIR",$dir);
619 $tpl->fillin("DIR",upper_path
($virtual));
621 $tpl->fillin("COMMAND","rename");
622 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
623 $tpl->fillin("SCRIPT",$script);
625 my $output = header
(-type
=> "text/html");
626 $output .= $tpl->get_template;
632 rename($physical,$new_physical) or return error
($config->{'errors'}->{'rename_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
633 return devedit_reload
({command
=> 'show', file
=> $dir});
637 my $tpl = new Template
;
638 $tpl->read_file($config->{'templates'}->{'renamefile'});
640 $tpl->fillin("FILE",$virtual);
641 $tpl->fillin("DIR",upper_path
($virtual));
642 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
643 $tpl->fillin("SCRIPT",$script);
645 my $output = header
(-type
=> "text/html");
646 $output .= $tpl->get_template;
654 # Remove a file or a directory and return to directory view
656 # Params: 1. Reference to user input hash
657 # 2. Reference to config hash
659 # Return: Output of the command (Scalar Reference)
663 my ($data,$config) = @_;
664 my $physical = $data->{'physical'};
665 my $virtual = $data->{'virtual'};
667 return error
($config->{'errors'}->{'remove_root'},"/") if($virtual eq "/");
673 if($data->{'cgi'}->param('confirmed'))
676 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
680 my $tpl = new Template
;
681 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
683 $tpl->fillin("DIR",$virtual);
684 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
685 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
686 $tpl->fillin("SCRIPT",$script);
688 my $output = header
(-type
=> "text/html");
689 $output .= $tpl->get_template;
698 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
700 if($data->{'cgi'}->param('confirmed'))
702 unlink($physical) or return error
($config->{'errors'}->{'delete_failed'},upper_path
($virtual),{FILE
=> $virtual});
703 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
707 my $tpl = new Template
;
708 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
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;
725 # Remove a file from the list of used files and
726 # return to directory view
728 # Params: 1. Reference to user input hash
729 # 2. Reference to config hash
731 # Return: Output of the command (Scalar Reference)
735 my ($data,$config) = @_;
736 my $virtual = $data->{'virtual'};
738 if($data->{'cgi'}->param('confirmed'))
740 file_unlock
($data->{'uselist'},$virtual);
741 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
745 my $tpl = new Template
;
746 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
748 $tpl->fillin("FILE",$virtual);
749 $tpl->fillin("DIR",upper_path
($virtual));
750 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
751 $tpl->fillin("SCRIPT",$script);
753 my $output = header
(-type
=> "text/html");
754 $output .= $tpl->get_template;
762 # Display some information about Dev-Editor
764 # Params: 1. Reference to user input hash
765 # 2. Reference to config hash
767 # Return: Output of the command (Scalar Reference)
771 my ($data,$config) = @_;
773 my $tpl = new Template
;
774 $tpl->read_file($config->{'templates'}->{'about'});
776 $tpl->fillin("SCRIPT",$script);
778 # Dev-Editor's version number
780 $tpl->fillin("VERSION",$data->{'version'});
782 # Some path information
784 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
785 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
786 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
787 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
791 $tpl->fillin("PERL_PROG",$^X
);
792 $tpl->fillin("PERL_VER",sprintf("%vd",$^V
));
794 # Information about the server
796 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
797 $tpl->fillin("OS",$^O
);
798 $tpl->fillin("TIME",strftime
($config->{'timeformat'},localtime));
800 # Process information
802 $tpl->fillin("PID",$$);
804 # Check if the functions getpwuid() and getgrgid() are available
806 if(eval("getpwuid(0)") && eval("getgrgid(0)"))
808 # Dev-Editor is running on a system which allows users and groups
809 # So we display the user and the group of our process
811 $tpl->parse_if_block("users",1);
813 # ID's of user and group
815 $tpl->fillin("UID",$<);
816 $tpl->fillin("GID",$();
818 # Names of user and group
820 $tpl->fillin("USER",getpwuid($<));
821 $tpl->fillin("GROUP",getgrgid($());
825 $tpl->parse_if_block("users",0);
828 my $output = header
(-type
=> "text/html");
829 $output .= $tpl->get_template;
834 # it's true, baby ;-)
patrick-canterino.de