]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
9f6b970aff283f58cee28af30da52b7c19ec64d5
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2004-03-09
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->{'err_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->{'tpl_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->{'tpl_dirlist_dir'});
125 $dtpl->fillin("DIR",$virt_path);
126 $dtpl->fillin("DIR_NAME",$dir);
127 $dtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
129 $dirlist .= $dtpl->get_template;
134 foreach my $file(@
$files)
136 my $phys_path = $physical."/".$file;
137 my $virt_path = encode_entities
($virtual.$file);
139 my @stat = stat($phys_path);
140 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
142 my $ftpl = new Template
;
143 $ftpl->read_file($config->{'tpl_dirlist_file'});
145 $ftpl->fillin("FILE",$virt_path);
146 $ftpl->fillin("FILE_NAME",$file);
147 $ftpl->fillin("SIZE",$stat[7]);
148 $ftpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
150 $ftpl->parse_if_block("not_readable",not -r
$phys_path);
151 $ftpl->parse_if_block("binary",-B
$phys_path);
152 $ftpl->parse_if_block("readonly",not -w
$phys_path);
154 $ftpl->parse_if_block("viewable",-r
$phys_path && -T
$phys_path);
155 $ftpl->parse_if_block("editable",-w
$phys_path && -r
$phys_path && -T
$phys_path && not $in_use);
157 $ftpl->parse_if_block("in_use",$in_use);
158 $ftpl->parse_if_block("unused",not $in_use);
160 $dirlist .= $ftpl->get_template;
163 $tpl->read_file($config->{'tpl_dirlist'});
165 $tpl->fillin("DIRLIST",$dirlist);
166 $tpl->fillin("DIR",$virtual);
167 $tpl->fillin("SCRIPT",$script);
168 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
174 return error
($config->{'err_noview'},upper_path
($virtual)) unless(-r
$physical);
176 # Check on binary files
177 # We have to do it in this way, or empty files
178 # will be recognized as binary files
184 return error
($config->{'err_binary'},upper_path
($virtual));
190 my $content = file_read
($physical);
191 $$content =~ s/\015\012|\012|\015/\n/g;
193 $tpl->read_file($config->{'tpl_viewfile'});
195 $tpl->fillin("FILE",$virtual);
196 $tpl->fillin("DIR",upper_path
($virtual));
197 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
198 $tpl->fillin("SCRIPT",$script);
199 $tpl->fillin("CONTENT",encode_entities
($$content));
203 my $output = header
(-type
=> "text/html");
204 $output .= $tpl->get_template;
211 # Lock a file and display a form to edit it
213 # Params: 1. Reference to user input hash
214 # 2. Reference to config hash
216 # Return: Output of the command (Scalar Reference)
218 sub exec_beginedit
($$)
220 my ($data,$config) = @_;
221 my $physical = $data->{'physical'};
222 my $virtual = $data->{'virtual'};
223 my $uselist = $data->{'uselist'};
225 return error
($config->{'err_editdir'},upper_path
($virtual)) if(-d
$physical);
226 return error
($config->{'err_in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
227 return error
($config->{'err_noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
229 # Check on binary files
235 return error
($config->{'err_binary'},upper_path
($virtual));
241 $uselist->add_file($virtual);
244 my $content = file_read
($physical);
245 $$content =~ s/\015\012|\012|\015/\n/g;
247 my $tpl = new Template
;
248 $tpl->read_file($config->{'tpl_editfile'});
250 $tpl->fillin("FILE",$virtual);
251 $tpl->fillin("DIR",upper_path
($virtual));
252 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
253 $tpl->fillin("SCRIPT",$script);
254 $tpl->fillin("CONTENT",encode_entities
($$content));
256 my $output = header
(-type
=> "text/html");
257 $output .= $tpl->get_template;
267 # Params: 1. Reference to user input hash
268 # 2. Reference to config hash
270 # Return: Output of the command (Scalar Reference)
272 sub exec_canceledit
($$)
274 my ($data,$config) = @_;
275 my $virtual = $data->{'virtual'};
277 file_unlock
($data->{'uselist'},$virtual);
278 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
283 # Save a file, unlock it and return to directory view
285 # Params: 1. Reference to user input hash
286 # 2. Reference to config hash
288 # Return: Output of the command (Scalar Reference)
292 my ($data,$config) = @_;
293 my $physical = $data->{'physical'};
294 my $virtual = $data->{'virtual'};
295 my $content = $data->{'cgi'}->param('filecontent');
296 my $uselist = $data->{'uselist'};
300 $content =~ s/\015\012|\012|\015/\n/g;
302 if($data->{'cgi'}->param('encode_iso'))
304 # Encode all ISO-8859-1 special chars
306 $content = encode_entities
($content,"\200-\377");
309 if($data->{'cgi'}->param('saveas'))
311 # Create the new filename
313 $physical = $data->{'new_physical'};
314 $virtual = $data->{'new_virtual'};
316 # Check if someone else is editing the new file
318 return error
($config->{'err_in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
321 return error
($config->{'err_editdir'},upper_path
($virtual)) if(-d
$physical);
322 return error
($config->{'err_noedit'}, upper_path
($virtual)) unless(-r
$physical && -w
$physical);
324 if(file_save
($physical,\
$content))
326 # Saving of the file was successful - so unlock it!
328 file_unlock
($uselist,$data->{'virtual'});
330 # Maybe the user saved the file using another filename...
331 # But we have to unlock the original file!
333 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
337 return error
($config->{'err_edit_failed'},upper_path
($virtual),{FILE
=> $virtual});
343 # Create a file and return to directory view
345 # Params: 1. Reference to user input hash
346 # 2. Reference to config hash
348 # Return: Output of the command (Scalar Reference)
352 my ($data,$config) = @_;
353 my $new_physical = $data->{'new_physical'};
354 my $new_virtual = $data->{'new_virtual'};
355 my $dir = upper_path
($new_virtual);
356 $new_virtual = encode_entities
($new_virtual);
358 return error
($config->{'err_file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
360 file_create
($new_physical) or return error
($config->{'err_mkfile_failed'},$dir,{FILE
=> $new_virtual});
361 return devedit_reload
({command
=> 'show', file
=> $dir});
366 # Create a directory and return to directory view
368 # Params: 1. Reference to user input hash
369 # 2. Reference to config hash
371 # Return: Output of the command (Scalar Reference)
375 my ($data,$config) = @_;
376 my $new_physical = $data->{'new_physical'};
377 my $new_virtual = $data->{'new_virtual'};
378 my $dir = upper_path
($new_virtual);
379 $new_virtual = encode_entities
($new_virtual);
381 return error
($config->{'err_file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
383 mkdir($new_physical,0777) or return error
($config->{'err_mkdir_failed'},$dir,{DIR
=> $new_virtual});
384 return devedit_reload
({command
=> 'show', file
=> $dir});
391 # Params: 1. Reference to user input hash
392 # 2. Reference to config hash
394 # Return: Output of the command (Scalar Reference)
398 my ($data,$config) = @_;
399 my $physical = $data->{'physical'};
400 my $virtual = $data->{'virtual'};
401 my $cgi = $data->{'cgi'};
403 if(my $uploaded_file = $cgi->param('uploaded_file'))
405 # Process file upload
407 my $filename = file_name
($uploaded_file);
408 my $file_phys = $physical."/".$filename;
409 my $file_virt = $virtual."".$filename;
411 return error
($config->{'err_file_exists'},$virtual,{FILE
=> $file_virt}) if(-e
$file_phys);
413 my $ascii = $cgi->param('ascii');
414 my $handle = $cgi->upload('uploaded_file');
418 open(FILE
,">$file_phys") or return error
($config->{'err_mkfile_failed'},$virtual,{FILE
=> $file_virt});
419 binmode(FILE
) unless($ascii);
423 while(read($handle,$data,1024))
425 $data =~ s/\015\012|\012|\015/\n/g if($ascii);
431 return devedit_reload
({command
=> "show", file
=> $virtual});
435 my $tpl = new Template
;
436 $tpl->read_file($config->{'tpl_upload'});
438 $tpl->fillin("DIR",$virtual);
439 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
440 $tpl->fillin("SCRIPT",$script);
442 my $output = header
(-type
=> "text/html");
443 $output .= $tpl->get_template;
451 # Copy a file and return to directory view
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 = encode_entities
($data->{'virtual'});
463 my $new_physical = $data->{'new_physical'};
465 return error
($config->{'err_nocopy'}) unless(-r
$physical);
469 my $new_virtual = $data->{'new_virtual'};
470 my $dir = upper_path
($new_virtual);
471 $new_virtual = encode_entities
($new_virtual);
475 return error
($config->{'err_exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
479 return error
($config->{'err_dircopy'});
481 elsif(not $data->{'cgi'}->param('confirmed'))
483 my $tpl = new Template
;
484 $tpl->read_file($config->{'tpl_confirm_replace'});
486 $tpl->fillin("FILE",$virtual);
487 $tpl->fillin("NEW_FILE",$new_virtual);
488 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
489 $tpl->fillin("NEW_DIR",$dir);
490 $tpl->fillin("DIR",upper_path
($virtual));
492 $tpl->fillin("COMMAND","copy");
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;
503 copy
($physical,$new_physical) or return error
($config->{'err_copy_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
504 return devedit_reload
({command
=> 'show', file
=> $dir});
508 my $tpl = new Template
;
509 $tpl->read_file($config->{'tpl_copyfile'});
511 $tpl->fillin("FILE",$virtual);
512 $tpl->fillin("DIR",upper_path
($virtual));
513 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
514 $tpl->fillin("SCRIPT",$script);
516 my $output = header
(-type
=> "text/html");
517 $output .= $tpl->get_template;
525 # Rename/move a file and return to directory view
527 # Params: 1. Reference to user input hash
528 # 2. Reference to config hash
530 # Return: Output of the command (Scalar Reference)
534 my ($data,$config) = @_;
535 my $physical = $data->{'physical'};
536 my $virtual = $data->{'virtual'};
537 my $new_physical = $data->{'new_physical'};
539 return error
($config->{'err_in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
543 my $new_virtual = $data->{'new_virtual'};
544 my $dir = upper_path
($new_virtual);
545 $new_virtual = encode_entities
($new_virtual);
549 return error
($config->{'err_exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
553 return error
($config->{'err_dircopy'});
555 elsif(not $data->{'cgi'}->param('confirmed'))
557 my $tpl = new Template
;
558 $tpl->read_file($config->{'tpl_confirm_replace'});
560 $tpl->fillin("FILE",$virtual);
561 $tpl->fillin("NEW_FILE",$new_virtual);
562 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
563 $tpl->fillin("NEW_DIR",$dir);
564 $tpl->fillin("DIR",upper_path
($virtual));
566 $tpl->fillin("COMMAND","rename");
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;
577 rename($physical,$new_physical) or return error
($config->{'err_rename_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
578 return devedit_reload
({command
=> 'show', file
=> $dir});
582 my $tpl = new Template
;
583 $tpl->read_file($config->{'tpl_renamefile'});
585 $tpl->fillin("FILE",$virtual);
586 $tpl->fillin("DIR",upper_path
($virtual));
587 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
588 $tpl->fillin("SCRIPT",$script);
590 my $output = header
(-type
=> "text/html");
591 $output .= $tpl->get_template;
599 # Remove a file or a directory and return to directory view
601 # Params: 1. Reference to user input hash
602 # 2. Reference to config hash
604 # Return: Output of the command (Scalar Reference)
608 my ($data,$config) = @_;
609 my $physical = $data->{'physical'};
610 my $virtual = $data->{'virtual'};
616 if($data->{'cgi'}->param('confirmed'))
619 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
623 my $tpl = new Template
;
624 $tpl->read_file($config->{'tpl_confirm_rmdir'});
626 $tpl->fillin("DIR",$virtual);
627 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
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;
641 return error
($config->{'err_in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
643 if($data->{'cgi'}->param('confirmed'))
645 unlink($physical) or return error
($config->{'err_delete_failed'},upper_path
($virtual),{FILE
=> $virtual});
646 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
650 my $tpl = new Template
;
651 $tpl->read_file($config->{'tpl_confirm_rmfile'});
653 $tpl->fillin("FILE",$virtual);
654 $tpl->fillin("DIR",upper_path
($virtual));
655 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
656 $tpl->fillin("SCRIPT",$script);
658 my $output = header
(-type
=> "text/html");
659 $output .= $tpl->get_template;
668 # Remove a file from the list of used files and
669 # return to directory view
671 # Params: 1. Reference to user input hash
672 # 2. Reference to config hash
674 # Return: Output of the command (Scalar Reference)
678 my ($data,$config) = @_;
679 my $virtual = $data->{'virtual'};
681 if($data->{'cgi'}->param('confirmed'))
683 file_unlock
($data->{'uselist'},$virtual);
684 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
688 my $tpl = new Template
;
689 $tpl->read_file($config->{'tpl_confirm_unlock'});
691 $tpl->fillin("FILE",$virtual);
692 $tpl->fillin("DIR",upper_path
($virtual));
693 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
694 $tpl->fillin("SCRIPT",$script);
696 my $output = header
(-type
=> "text/html");
697 $output .= $tpl->get_template;
705 # Display some information about Dev-Editor
707 # Params: 1. Reference to user input hash
708 # 2. Reference to config hash
710 # Return: Output of the command (Scalar Reference)
714 my ($data,$config) = @_;
716 my $tpl = new Template
;
717 $tpl->read_file($config->{'tpl_about'});
719 $tpl->fillin("SCRIPT",$script);
721 # Dev-Editor's version number
723 $tpl->fillin("VERSION",$data->{'version'});
725 # Some path information
727 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
728 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
729 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
730 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
734 $tpl->fillin("PERL_PROG",$^X
);
735 $tpl->fillin("PERL_VER",sprintf("%vd",$^V
));
737 # Information about the server
739 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
740 $tpl->fillin("OS",$^O
);
741 $tpl->fillin("TIME",strftime
($config->{'timeformat'},localtime));
743 # Process information
745 $tpl->fillin("PID",$$);
747 # Check if the functions getpwuid() and getgrgid() are available
749 if(eval("getpwuid(0)") && eval("getgrgid(0)"))
751 # Dev-Editor is running on a system which allows users and groups
752 # So we display the user and the group of our process
754 $tpl->parse_if_block("users",1);
756 # ID's of user and group
758 $tpl->fillin("UID",$<);
759 $tpl->fillin("GID",$();
761 # Names of user and group
763 $tpl->fillin("USER",getpwuid($<));
764 $tpl->fillin("GROUP",getgrgid($());
768 $tpl->parse_if_block("users",0);
771 my $output = header
(-type
=> "text/html");
772 $output .= $tpl->get_template;
777 # it's true, baby ;-)
patrick-canterino.de