]>
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-03-04
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
45 use base
qw(Exporter);
47 @EXPORT = qw(exec_command);
51 # Execute the specified command
53 # Params: 1. Command to execute
54 # 2. Reference to user input hash
55 # 3. Reference to config hash
57 # Return: Output of the command (Scalar Reference)
61 my ($command,$data,$config) = @_;
63 return error
($config->{'err_cmd_unknown'},'/',{COMMAND
=> $command}) unless($dispatch{$command});
65 my $output = &{$dispatch{$command}}($data,$config);
71 # View a directory or a file
73 # Params: 1. Reference to user input hash
74 # 2. Reference to config hash
76 # Return: Output of the command (Scalar Reference)
80 my ($data,$config) = @_;
81 my $physical = $data->{'physical'};
82 my $virtual = $data->{'virtual'};
84 my $tpl = new Template
;
88 # Create directory listing
90 my $direntries = dir_read
($physical);
91 return error
($config->{'dir_read_failed'},upper_path
($virtual),{DIR
=> '$virtual'}) unless($direntries);
93 my $files = $direntries->{'files'};
94 my $dirs = $direntries->{'dirs'};
98 # Create the link to the upper directory
99 # (only if we are not in the root directory)
101 unless($virtual eq "/")
103 my @stat = stat($physical."/..");
105 my $udtpl = new Template
;
106 $udtpl->read_file($config->{'tpl_dirlist_up'});
108 $udtpl->fillin("UPPER_DIR",encode_entities
(upper_path
($virtual)));
109 $udtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
111 $dirlist .= $udtpl->get_template;
116 foreach my $dir(@
$dirs)
118 my @stat = stat($physical."/".$dir);
119 my $virt_path = encode_entities
($virtual.$dir."/");
121 my $dtpl = new Template
;
122 $dtpl->read_file($config->{'tpl_dirlist_dir'});
124 $dtpl->fillin("DIR",$virt_path);
125 $dtpl->fillin("DIR_NAME",$dir);
126 $dtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
128 $dirlist .= $dtpl->get_template;
133 foreach my $file(@
$files)
135 my $phys_path = $physical."/".$file;
136 my $virt_path = encode_entities
($virtual.$file);
138 my @stat = stat($phys_path);
139 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
141 my $ftpl = new Template
;
142 $ftpl->read_file($config->{'tpl_dirlist_file'});
144 $ftpl->fillin("FILE",$virt_path);
145 $ftpl->fillin("FILE_NAME",$file);
146 $ftpl->fillin("SIZE",$stat[7]);
147 $ftpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
149 $ftpl->parse_if_block("not_readable",not -r
$phys_path);
150 $ftpl->parse_if_block("binary",-B
$phys_path);
151 $ftpl->parse_if_block("readonly",not -w
$phys_path);
153 $ftpl->parse_if_block("viewable",-r
$phys_path && -T
$phys_path);
154 $ftpl->parse_if_block("editable",-w
$phys_path && -r
$phys_path && -T
$phys_path && not $in_use);
156 $ftpl->parse_if_block("in_use",$in_use);
157 $ftpl->parse_if_block("unused",not $in_use);
159 $dirlist .= $ftpl->get_template;
162 $tpl->read_file($config->{'tpl_dirlist'});
164 $tpl->fillin("DIRLIST",$dirlist);
165 $tpl->fillin("DIR",$virtual);
166 $tpl->fillin("SCRIPT",$script);
167 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
173 return error
($config->{'err_noview'},upper_path
($virtual)) unless(-r
$physical);
175 # Check on binary files
176 # We have to do it in this way, or empty files
177 # will be recognized as binary files
183 return error
($config->{'err_binary'},upper_path
($virtual));
189 my $content = file_read
($physical);
190 $$content =~ s/\015\012|\012|\015/\n/g;
192 $tpl->read_file($config->{'tpl_viewfile'});
194 $tpl->fillin("FILE",$virtual);
195 $tpl->fillin("DIR",upper_path
($virtual));
196 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
197 $tpl->fillin("SCRIPT",$script);
198 $tpl->fillin("CONTENT",encode_entities
($$content));
202 my $output = header
(-type
=> "text/html");
203 $output .= $tpl->get_template;
210 # Lock a file and display a form to edit it
212 # Params: 1. Reference to user input hash
213 # 2. Reference to config hash
215 # Return: Output of the command (Scalar Reference)
217 sub exec_beginedit
($$)
219 my ($data,$config) = @_;
220 my $physical = $data->{'physical'};
221 my $virtual = $data->{'virtual'};
222 my $uselist = $data->{'uselist'};
224 return error
($config->{'err_editdir'},upper_path
($virtual)) if(-d
$physical);
225 return error
($config->{'err_in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($uselist->in_use($virtual));
226 return error
($config->{'err_noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
228 # Check on binary files
234 return error
($config->{'err_binary'},upper_path
($virtual));
240 $uselist->add_file($virtual);
243 my $content = file_read
($physical);
244 $$content =~ s/\015\012|\012|\015/\n/g;
246 my $tpl = new Template
;
247 $tpl->read_file($config->{'tpl_editfile'});
249 $tpl->fillin("FILE",$virtual);
250 $tpl->fillin("DIR",upper_path
($virtual));
251 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
252 $tpl->fillin("SCRIPT",$script);
253 $tpl->fillin("CONTENT",encode_entities
($$content));
255 my $output = header
(-type
=> "text/html");
256 $output .= $tpl->get_template;
266 # Params: 1. Reference to user input hash
267 # 2. Reference to config hash
269 # Return: Output of the command (Scalar Reference)
271 sub exec_canceledit
($$)
273 my ($data,$config) = @_;
274 my $virtual = $data->{'virtual'};
276 file_unlock
($data->{'uselist'},$virtual);
277 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
282 # Save a file, unlock it and return to directory view
284 # Params: 1. Reference to user input hash
285 # 2. Reference to config hash
287 # Return: Output of the command (Scalar Reference)
291 my ($data,$config) = @_;
292 my $physical = $data->{'physical'};
293 my $virtual = $data->{'virtual'};
294 my $content = $data->{'cgi'}->param('filecontent');
295 my $uselist = $data->{'uselist'};
299 $content =~ s/\015\012|\012|\015/\n/g;
301 if($data->{'cgi'}->param('encode_iso'))
303 # Encode all ISO-8859-1 special chars
305 $content = encode_entities
($content,"\200-\377");
308 if($data->{'cgi'}->param('saveas'))
310 # Create the new filename
312 $physical = $data->{'new_physical'};
313 $virtual = $data->{'new_virtual'};
315 # Check if someone else is editing the new file
317 return error_in_use
($virtual) if($uselist->in_use($virtual));
320 return error
($config->{'err_editdir'},upper_path
($virtual)) if(-d
$physical);
321 return error
($config->{'err_noedit'}, upper_path
($virtual)) unless(-r
$physical && -w
$physical);
323 if(file_save
($physical,\
$content))
325 # Saving of the file was successful - so unlock it!
327 file_unlock
($uselist,$data->{'virtual'});
329 # Maybe the user saved the file using another filename...
330 # But we have to unlock the original file!
332 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
336 return error
($config->{'err_edit_failed'},upper_path
($virtual),{FILE
=> $virtual});
342 # Create a file and return to directory view
344 # Params: 1. Reference to user input hash
345 # 2. Reference to config hash
347 # Return: Output of the command (Scalar Reference)
351 my ($data,$config) = @_;
352 my $new_physical = $data->{'new_physical'};
353 my $new_virtual = $data->{'new_virtual'};
354 my $dir = upper_path
($new_virtual);
355 $new_virtual = encode_entities
($new_virtual);
357 return error
($config->{'err_file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
359 file_create
($new_physical) or return error
($config->{'err_mkfile_failed'},$dir,{FILE
=> $new_virtual});
360 return devedit_reload
({command
=> 'show', file
=> $dir});
365 # Create a directory and return to directory view
367 # Params: 1. Reference to user input hash
368 # 2. Reference to config hash
370 # Return: Output of the command (Scalar Reference)
374 my ($data,$config) = @_;
375 my $new_physical = $data->{'new_physical'};
376 my $new_virtual = $data->{'new_virtual'};
377 my $dir = upper_path
($new_virtual);
378 $new_virtual = encode_entities
($new_virtual);
380 return error
($config->{'err_file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
382 mkdir($new_physical,0777) or return error
($config->{'err_mkdir_failed'},$dir,{DIR
=> $new_virtual});
383 return devedit_reload
({command
=> 'show', file
=> $dir});
390 # Params: 1. Reference to user input hash
391 # 2. Reference to config hash
393 # Return: Output of the command (Scalar Reference)
397 my ($data,$config) = @_;
398 my $physical = $data->{'physical'};
399 my $virtual = $data->{'virtual'};
400 my $cgi = $data->{'cgi'};
402 if(my $uploaded_file = $cgi->param('uploaded_file'))
404 # Process file upload
406 my $filename = file_name
($uploaded_file);
407 my $file_phys = $physical."/".$filename;
408 my $file_virt = $virtual."".$filename;
410 return error
($config->{'err_file_exists'},$virtual,{FILE
=> $file_virt}) if(-e
$file_phys);
412 my $ascii = $cgi->param('ascii');
413 my $handle = $cgi->upload('uploaded_file');
417 open(FILE
,">$file_phys") or return error
($config->{'err_mkfile_failed'},$virtual,{FILE
=> $file_virt});
418 binmode(FILE
) unless($ascii);
422 while(read($handle,$data,1024))
424 $data =~ s/\015\012|\012|\015/\n/g if($ascii);
430 return devedit_reload
({command
=> "show", file
=> $virtual});
434 my $tpl = new Template
;
435 $tpl->read_file($config->{'tpl_upload'});
437 $tpl->fillin("DIR",$virtual);
438 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
439 $tpl->fillin("SCRIPT",$script);
441 my $output = header
(-type
=> "text/html");
442 $output .= $tpl->get_template;
450 # Copy a file and return to directory view
452 # Params: 1. Reference to user input hash
453 # 2. Reference to config hash
455 # Return: Output of the command (Scalar Reference)
459 my ($data,$config) = @_;
460 my $physical = $data->{'physical'};
461 my $virtual = encode_entities
($data->{'virtual'});
462 my $new_physical = $data->{'new_physical'};
464 return error
($config->{'err_nocopy'}) unless(-r
$physical);
468 my $new_virtual = $data->{'new_virtual'};
469 my $dir = upper_path
($new_virtual);
470 $new_virtual = encode_entities
($new_virtual);
474 return error
($config->{'err_exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
478 return error
($config->{'err_dircopy'});
480 elsif(not $data->{'cgi'}->param('confirmed'))
482 my $tpl = new Template
;
483 $tpl->read_file($config->{'tpl_confirm_replace'});
485 $tpl->fillin("FILE",$virtual);
486 $tpl->fillin("NEW_FILE",$new_virtual);
487 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
488 $tpl->fillin("NEW_DIR",$dir);
489 $tpl->fillin("DIR",upper_path
($virtual));
490 $tpl->fillin("COMMAND","copy");
491 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
492 $tpl->fillin("SCRIPT",$script);
494 my $output = header
(-type
=> "text/html");
495 $output .= $tpl->get_template;
501 copy
($physical,$new_physical) or return error
($config->{'err_copy_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
502 return devedit_reload
({command
=> 'show', file
=> $dir});
506 my $tpl = new Template
;
507 $tpl->read_file($config->{'tpl_copyfile'});
509 $tpl->fillin("FILE",$virtual);
510 $tpl->fillin("DIR",upper_path
($virtual));
511 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
512 $tpl->fillin("SCRIPT",$script);
514 my $output = header
(-type
=> "text/html");
515 $output .= $tpl->get_template;
523 # Rename/move a file and return to directory view
525 # Params: 1. Reference to user input hash
526 # 2. Reference to config hash
528 # Return: Output of the command (Scalar Reference)
532 my ($data,$config) = @_;
533 my $physical = $data->{'physical'};
534 my $virtual = $data->{'virtual'};
535 my $new_physical = $data->{'new_physical'};
537 return error
($config->{'err_in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
541 my $new_virtual = $data->{'new_virtual'};
542 my $dir = upper_path
($new_virtual);
543 $new_virtual = encode_entities
($new_virtual);
547 return error
($config->{'err_exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
551 return error
($config->{'err_dircopy'});
553 elsif(not $data->{'cgi'}->param('confirmed'))
555 my $tpl = new Template
;
556 $tpl->read_file($config->{'tpl_confirm_replace'});
558 $tpl->fillin("FILE",$virtual);
559 $tpl->fillin("NEW_FILE",$new_virtual);
560 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
561 $tpl->fillin("NEW_DIR",$dir);
562 $tpl->fillin("DIR",upper_path
($virtual));
563 $tpl->fillin("COMMAND","rename");
564 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
565 $tpl->fillin("SCRIPT",$script);
567 my $output = header
(-type
=> "text/html");
568 $output .= $tpl->get_template;
574 rename($physical,$new_physical) or return error
($config->{'err_rename_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
575 return devedit_reload
({command
=> 'show', file
=> $dir});
579 my $tpl = new Template
;
580 $tpl->read_file($config->{'tpl_renamefile'});
582 $tpl->fillin("FILE",$virtual);
583 $tpl->fillin("DIR",upper_path
($virtual));
584 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
585 $tpl->fillin("SCRIPT",$script);
587 my $output = header
(-type
=> "text/html");
588 $output .= $tpl->get_template;
596 # Remove a file or a directory and return to directory view
598 # Params: 1. Reference to user input hash
599 # 2. Reference to config hash
601 # Return: Output of the command (Scalar Reference)
605 my ($data,$config) = @_;
606 my $physical = $data->{'physical'};
607 my $virtual = $data->{'virtual'};
613 if($data->{'cgi'}->param('confirmed'))
616 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
620 my $tpl = new Template
;
621 $tpl->read_file($config->{'tpl_confirm_rmdir'});
623 $tpl->fillin("DIR",$virtual);
624 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
625 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
626 $tpl->fillin("SCRIPT",$script);
628 my $output = header
(-type
=> "text/html");
629 $output .= $tpl->get_template;
638 return error
($config->{'err_in_use'},upper_path
($virtual),{FILE
=> $virtual}) if($data->{'uselist'}->in_use($virtual));
640 if($data->{'cgi'}->param('confirmed'))
642 unlink($physical) or return error
($config->{'err_delete_failed'},upper_path
($virtual),{FILE
=> $virtual});
643 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
647 my $tpl = new Template
;
648 $tpl->read_file($config->{'tpl_confirm_rmfile'});
650 $tpl->fillin("FILE",$virtual);
651 $tpl->fillin("DIR",upper_path
($virtual));
652 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
653 $tpl->fillin("SCRIPT",$script);
655 my $output = header
(-type
=> "text/html");
656 $output .= $tpl->get_template;
665 # Remove a file from the list of used files and
666 # return to directory view
668 # Params: 1. Reference to user input hash
669 # 2. Reference to config hash
671 # Return: Output of the command (Scalar Reference)
675 my ($data,$config) = @_;
676 my $virtual = $data->{'virtual'};
678 if($data->{'cgi'}->param('confirmed'))
680 file_unlock
($data->{'uselist'},$virtual);
681 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
685 my $tpl = new Template
;
686 $tpl->read_file($config->{'tpl_confirm_unlock'});
688 $tpl->fillin("FILE",$virtual);
689 $tpl->fillin("DIR",upper_path
($virtual));
690 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
691 $tpl->fillin("SCRIPT",$script);
693 my $output = header
(-type
=> "text/html");
694 $output .= $tpl->get_template;
700 # it's true, baby ;-)
patrick-canterino.de