]>
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-04-25
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);
157 $ftpl->parse_if_block("editable",-w
$phys_path && -r
$phys_path && -T
$phys_path && not $in_use);
159 $ftpl->parse_if_block("in_use",$in_use);
160 $ftpl->parse_if_block("unused",not $in_use);
162 $dirlist .= $ftpl->get_template;
165 $tpl->read_file($config->{'templates'}->{'dirlist'});
167 $tpl->fillin("DIRLIST",$dirlist);
168 $tpl->fillin("DIR",$virtual);
169 $tpl->fillin("SCRIPT",$script);
170 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
176 return error
($config->{'errors'}->{'noview'},upper_path
($virtual)) unless(-r
$physical);
178 # Check on binary files
179 # We have to do it in this way, or empty files
180 # will be recognized as binary files
186 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
192 my $content = file_read
($physical);
193 $$content =~ s/\015\012|\012|\015/\n/g;
195 $tpl->read_file($config->{'templates'}->{'viewfile'});
197 $tpl->fillin("FILE",$virtual);
198 $tpl->fillin("DIR",upper_path
($virtual));
199 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
200 $tpl->fillin("SCRIPT",$script);
201 $tpl->fillin("CONTENT",encode_entities
($$content));
205 my $output = header
(-type
=> "text/html");
206 $output .= $tpl->get_template;
213 # Lock a file and display a form to edit it
215 # Params: 1. Reference to user input hash
216 # 2. Reference to config hash
218 # Return: Output of the command (Scalar Reference)
220 sub exec_beginedit
($$)
222 my ($data,$config) = @_;
223 my $physical = $data->{'physical'};
224 my $virtual = $data->{'virtual'};
225 my $uselist = $data->{'uselist'};
227 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
228 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
229 return error
($config->{'errors'}->{'noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
231 # Check on binary files
237 return error
($config->{'errors'}->{'binary'},upper_path
($virtual));
243 $uselist->add_file($virtual);
246 my $content = file_read
($physical);
247 $$content =~ s/\015\012|\012|\015/\n/g;
249 my $tpl = new Template
;
250 $tpl->read_file($config->{'templates'}->{'editfile'});
252 $tpl->fillin("FILE",$virtual);
253 $tpl->fillin("DIR",upper_path
($virtual));
254 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
255 $tpl->fillin("SCRIPT",$script);
256 $tpl->fillin("CONTENT",encode_entities
($$content));
258 my $output = header
(-type
=> "text/html");
259 $output .= $tpl->get_template;
269 # Params: 1. Reference to user input hash
270 # 2. Reference to config hash
272 # Return: Output of the command (Scalar Reference)
274 sub exec_canceledit
($$)
276 my ($data,$config) = @_;
277 my $virtual = $data->{'virtual'};
279 file_unlock
($data->{'uselist'},$virtual);
280 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
285 # Save a file, unlock it and return to directory view
287 # Params: 1. Reference to user input hash
288 # 2. Reference to config hash
290 # Return: Output of the command (Scalar Reference)
294 my ($data,$config) = @_;
295 my $physical = $data->{'physical'};
296 my $virtual = $data->{'virtual'};
297 my $content = $data->{'cgi'}->param('filecontent');
298 my $uselist = $data->{'uselist'};
302 $content =~ s/\015\012|\012|\015/\n/g;
304 if($data->{'cgi'}->param('encode_iso'))
306 # Encode all ISO-8859-1 special chars
308 $content = encode_entities
($content,"\200-\377");
311 if($data->{'cgi'}->param('saveas'))
313 # Create the new filename
315 $physical = $data->{'new_physical'};
316 $virtual = $data->{'new_virtual'};
318 # Check if someone else is editing the new file
320 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
323 return error
($config->{'errors'}->{'editdir'},upper_path
($virtual)) if(-d
$physical);
324 return error
($config->{'errors'}->{'noedit'}, upper_path
($virtual)) unless(-r
$physical && -w
$physical);
326 if(file_save
($physical,\
$content))
328 # Saving of the file was successful - so unlock it!
330 file_unlock
($uselist,$data->{'virtual'});
332 # Maybe the user saved the file using another filename...
333 # But we have to unlock the original file!
335 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
339 return error
($config->{'errors'}->{'edit_failed'},upper_path
($virtual),{FILE
=> $virtual});
345 # Create a file and return to directory view
347 # Params: 1. Reference to user input hash
348 # 2. Reference to config hash
350 # Return: Output of the command (Scalar Reference)
354 my ($data,$config) = @_;
355 my $new_physical = $data->{'new_physical'};
356 my $new_virtual = $data->{'new_virtual'};
357 my $dir = upper_path
($new_virtual);
358 $new_virtual = encode_entities
($new_virtual);
360 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
362 file_create
($new_physical) or return error
($config->{'errors'}->{'mkfile_failed'},$dir,{FILE
=> $new_virtual});
363 return devedit_reload
({command
=> 'show', file
=> $dir});
368 # Create a directory and return to directory view
370 # Params: 1. Reference to user input hash
371 # 2. Reference to config hash
373 # Return: Output of the command (Scalar Reference)
377 my ($data,$config) = @_;
378 my $new_physical = $data->{'new_physical'};
379 my $new_virtual = $data->{'new_virtual'};
380 my $dir = upper_path
($new_virtual);
381 $new_virtual = encode_entities
($new_virtual);
383 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
385 mkdir($new_physical,0777) or return error
($config->{'errors'}->{'mkdir_failed'},$dir,{DIR
=> $new_virtual});
386 return devedit_reload
({command
=> 'show', file
=> $dir});
393 # Params: 1. Reference to user input hash
394 # 2. Reference to config hash
396 # Return: Output of the command (Scalar Reference)
400 my ($data,$config) = @_;
401 my $physical = $data->{'physical'};
402 my $virtual = $data->{'virtual'};
403 my $cgi = $data->{'cgi'};
405 if(my $uploaded_file = $cgi->param('uploaded_file'))
407 # Process file upload
409 my $filename = file_name
($uploaded_file);
410 my $file_phys = $physical."/".$filename;
411 my $file_virt = $virtual."".$filename;
413 return error
($config->{'errors'}->{'file_exists'},$virtual,{FILE
=> $file_virt}) if(-e
$file_phys);
415 my $ascii = $cgi->param('ascii');
416 my $handle = $cgi->upload('uploaded_file');
420 open(FILE
,">$file_phys") or return error
($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE
=> $file_virt});
421 binmode(FILE
) unless($ascii);
425 while(read($handle,$data,1024))
427 $data =~ s/\015\012|\012|\015/\n/g if($ascii);
433 return devedit_reload
({command
=> "show", file
=> $virtual});
437 my $tpl = new Template
;
438 $tpl->read_file($config->{'templates'}->{'upload'});
440 $tpl->fillin("DIR",$virtual);
441 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
442 $tpl->fillin("SCRIPT",$script);
444 my $output = header
(-type
=> "text/html");
445 $output .= $tpl->get_template;
453 # Copy a file and return to directory view
455 # Params: 1. Reference to user input hash
456 # 2. Reference to config hash
458 # Return: Output of the command (Scalar Reference)
462 my ($data,$config) = @_;
463 my $physical = $data->{'physical'};
464 my $virtual = encode_entities
($data->{'virtual'});
465 my $new_physical = $data->{'new_physical'};
467 return error
($config->{'errors'}->{'nocopy'}) unless(-r
$physical);
471 my $new_virtual = $data->{'new_virtual'};
472 my $dir = upper_path
($new_virtual);
473 $new_virtual = encode_entities
($new_virtual);
477 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
481 return error
($config->{'errors'}->{'dircopy'});
483 elsif(not $data->{'cgi'}->param('confirmed'))
485 my $tpl = new Template
;
486 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
488 $tpl->fillin("FILE",$virtual);
489 $tpl->fillin("NEW_FILE",$new_virtual);
490 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
491 $tpl->fillin("NEW_DIR",$dir);
492 $tpl->fillin("DIR",upper_path
($virtual));
494 $tpl->fillin("COMMAND","copy");
495 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
496 $tpl->fillin("SCRIPT",$script);
498 my $output = header
(-type
=> "text/html");
499 $output .= $tpl->get_template;
505 copy
($physical,$new_physical) or return error
($config->{'errors'}->{'copy_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
506 return devedit_reload
({command
=> 'show', file
=> $dir});
510 my $tpl = new Template
;
511 $tpl->read_file($config->{'templates'}->{'copyfile'});
513 $tpl->fillin("FILE",$virtual);
514 $tpl->fillin("DIR",upper_path
($virtual));
515 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
516 $tpl->fillin("SCRIPT",$script);
518 my $output = header
(-type
=> "text/html");
519 $output .= $tpl->get_template;
527 # Rename/move a file and return to directory view
529 # Params: 1. Reference to user input hash
530 # 2. Reference to config hash
532 # Return: Output of the command (Scalar Reference)
536 my ($data,$config) = @_;
537 my $physical = $data->{'physical'};
538 my $virtual = $data->{'virtual'};
539 my $new_physical = $data->{'new_physical'};
541 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
545 my $new_virtual = $data->{'new_virtual'};
546 my $dir = upper_path
($new_virtual);
547 $new_virtual = encode_entities
($new_virtual);
551 return error
($config->{'errors'}->{'exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
555 return error
($config->{'errors'}->{'dircopy'});
557 elsif(not $data->{'cgi'}->param('confirmed'))
559 my $tpl = new Template
;
560 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
562 $tpl->fillin("FILE",$virtual);
563 $tpl->fillin("NEW_FILE",$new_virtual);
564 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
565 $tpl->fillin("NEW_DIR",$dir);
566 $tpl->fillin("DIR",upper_path
($virtual));
568 $tpl->fillin("COMMAND","rename");
569 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
570 $tpl->fillin("SCRIPT",$script);
572 my $output = header
(-type
=> "text/html");
573 $output .= $tpl->get_template;
579 rename($physical,$new_physical) or return error
($config->{'errors'}->{'rename_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
580 return devedit_reload
({command
=> 'show', file
=> $dir});
584 my $tpl = new Template
;
585 $tpl->read_file($config->{'templates'}->{'renamefile'});
587 $tpl->fillin("FILE",$virtual);
588 $tpl->fillin("DIR",upper_path
($virtual));
589 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
590 $tpl->fillin("SCRIPT",$script);
592 my $output = header
(-type
=> "text/html");
593 $output .= $tpl->get_template;
601 # Remove a file or a directory and return to directory view
603 # Params: 1. Reference to user input hash
604 # 2. Reference to config hash
606 # Return: Output of the command (Scalar Reference)
610 my ($data,$config) = @_;
611 my $physical = $data->{'physical'};
612 my $virtual = $data->{'virtual'};
618 if($data->{'cgi'}->param('confirmed'))
621 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
625 my $tpl = new Template
;
626 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
628 $tpl->fillin("DIR",$virtual);
629 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
630 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
631 $tpl->fillin("SCRIPT",$script);
633 my $output = header
(-type
=> "text/html");
634 $output .= $tpl->get_template;
643 return error
($config->{'errors'}->{'in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
645 if($data->{'cgi'}->param('confirmed'))
647 unlink($physical) or return error
($config->{'errors'}->{'delete_failed'},upper_path
($virtual),{FILE
=> $virtual});
648 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
652 my $tpl = new Template
;
653 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
655 $tpl->fillin("FILE",$virtual);
656 $tpl->fillin("DIR",upper_path
($virtual));
657 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
658 $tpl->fillin("SCRIPT",$script);
660 my $output = header
(-type
=> "text/html");
661 $output .= $tpl->get_template;
670 # Remove a file from the list of used files and
671 # return to directory view
673 # Params: 1. Reference to user input hash
674 # 2. Reference to config hash
676 # Return: Output of the command (Scalar Reference)
680 my ($data,$config) = @_;
681 my $virtual = $data->{'virtual'};
683 if($data->{'cgi'}->param('confirmed'))
685 file_unlock
($data->{'uselist'},$virtual);
686 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
690 my $tpl = new Template
;
691 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
693 $tpl->fillin("FILE",$virtual);
694 $tpl->fillin("DIR",upper_path
($virtual));
695 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
696 $tpl->fillin("SCRIPT",$script);
698 my $output = header
(-type
=> "text/html");
699 $output .= $tpl->get_template;
707 # Display some information about Dev-Editor
709 # Params: 1. Reference to user input hash
710 # 2. Reference to config hash
712 # Return: Output of the command (Scalar Reference)
716 my ($data,$config) = @_;
718 my $tpl = new Template
;
719 $tpl->read_file($config->{'templates'}->{'about'});
721 $tpl->fillin("SCRIPT",$script);
723 # Dev-Editor's version number
725 $tpl->fillin("VERSION",$data->{'version'});
727 # Some path information
729 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
730 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
731 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
732 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
736 $tpl->fillin("PERL_PROG",$^X
);
737 $tpl->fillin("PERL_VER",sprintf("%vd",$^V
));
739 # Information about the server
741 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
742 $tpl->fillin("OS",$^O
);
743 $tpl->fillin("TIME",strftime
($config->{'timeformat'},localtime));
745 # Process information
747 $tpl->fillin("PID",$$);
749 # Check if the functions getpwuid() and getgrgid() are available
751 if(eval("getpwuid(0)") && eval("getgrgid(0)"))
753 # Dev-Editor is running on a system which allows users and groups
754 # So we display the user and the group of our process
756 $tpl->parse_if_block("users",1);
758 # ID's of user and group
760 $tpl->fillin("UID",$<);
761 $tpl->fillin("GID",$();
763 # Names of user and group
765 $tpl->fillin("USER",getpwuid($<));
766 $tpl->fillin("GROUP",getgrgid($());
770 $tpl->parse_if_block("users",0);
773 my $output = header
(-type
=> "text/html");
774 $output .= $tpl->get_template;
779 # it's true, baby ;-)
patrick-canterino.de