]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
4a5e7681b6ca4a42a8c72705897f96c8c52cb517
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2004-02-20
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 'copy' => \
&exec_copy
,
37 'rename' => \
&exec_rename
,
38 'remove' => \
&exec_remove
,
39 'unlock' => \
&exec_unlock
44 use base
qw(Exporter);
46 @EXPORT = qw(exec_command);
50 # Execute the specified command
52 # Params: 1. Command to execute
53 # 2. Reference to user input hash
54 # 3. Reference to config hash
56 # Return: Output of the command (Scalar Reference)
60 my ($command,$data,$config) = @_;
62 return error
("Unknown command: $command") unless($dispatch{$command});
64 my $output = &{$dispatch{$command}}($data,$config);
70 # View a directory or a file
72 # Params: 1. Reference to user input hash
73 # 2. Reference to config hash
75 # Return: Output of the command (Scalar Reference)
79 my ($data,$config) = @_;
80 my $physical = $data->{'physical'};
81 my $virtual = $data->{'virtual'};
83 my $tpl = new Template
;
87 # Create directory listing
89 my $direntries = dir_read
($physical);
90 return error
($config->{'dir_read_failed'},upper_path
($virtual),{DIR
=> '$virtual'}) unless($direntries);
92 my $files = $direntries->{'files'};
93 my $dirs = $direntries->{'dirs'};
97 # Create the link to the upper directory
98 # (only if we are not in the root directory)
100 unless($virtual eq "/")
102 my @stat = stat($physical."/..");
104 my $udtpl = new Template
;
105 $udtpl->read_file($config->{'tpl_dirlist_up'});
107 $udtpl->fillin("UPPER_DIR",encode_entities
(upper_path
($virtual)));
108 $udtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
110 $dirlist .= $udtpl->get_template;
115 foreach my $dir(@
$dirs)
117 my @stat = stat($physical."/".$dir);
118 my $virt_path = encode_entities
($virtual.$dir."/");
120 my $dtpl = new Template
;
121 $dtpl->read_file($config->{'tpl_dirlist_dir'});
123 $dtpl->fillin("DIR",$virt_path);
124 $dtpl->fillin("DIR_NAME",$dir);
125 $dtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
127 $dirlist .= $dtpl->get_template;
132 foreach my $file(@
$files)
134 my $phys_path = $physical."/".$file;
135 my $virt_path = encode_entities
($virtual.$file);
137 my @stat = stat($phys_path);
138 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
140 my $ftpl = new Template
;
141 $ftpl->read_file($config->{'tpl_dirlist_file'});
143 $ftpl->fillin("FILE",$virt_path);
144 $ftpl->fillin("FILE_NAME",$file);
145 $ftpl->fillin("SIZE",$stat[7]);
146 $ftpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
148 $ftpl->parse_if_block("not_readable",not -r
$phys_path);
149 $ftpl->parse_if_block("binary",-B
$phys_path);
150 $ftpl->parse_if_block("readonly",not -w
$phys_path);
152 $ftpl->parse_if_block("viewable",-r
$phys_path && -T
$phys_path);
153 $ftpl->parse_if_block("editable",-w
$phys_path && -r
$phys_path && -T
$phys_path && not $in_use);
155 $ftpl->parse_if_block("in_use",$in_use);
156 $ftpl->parse_if_block("unused",not $in_use);
158 $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);
191 $tpl->read_file($config->{'tpl_viewfile'});
193 $tpl->fillin("FILE",$virtual);
194 $tpl->fillin("DIR",upper_path
($virtual));
195 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
196 $tpl->fillin("SCRIPT",$script);
197 $tpl->fillin("CONTENT",encode_entities
($$content));
201 my $output = header
(-type
=> "text/html");
202 $output .= $tpl->get_template;
209 # Lock a file and display a form to edit it
211 # Params: 1. Reference to user input hash
212 # 2. Reference to config hash
214 # Return: Output of the command (Scalar Reference)
216 sub exec_beginedit
($$)
218 my ($data,$config) = @_;
219 my $physical = $data->{'physical'};
220 my $virtual = $data->{'virtual'};
221 my $uselist = $data->{'uselist'};
223 return error
($config->{'err_editdir'},upper_path
($virtual)) if(-d
$physical);
224 return error_in_use
($virtual) if($uselist->in_use($virtual));
225 return error
($config->{'err_noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
227 # Check on binary files
233 return error
($config->{'err_binary'},upper_path
($virtual));
239 $uselist->add_file($virtual);
242 my $content = file_read
($physical);
244 my $tpl = new Template
;
245 $tpl->read_file($config->{'tpl_editfile'});
247 $tpl->fillin("FILE",$virtual);
248 $tpl->fillin("DIR",upper_path
($virtual));
249 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
250 $tpl->fillin("SCRIPT",$script);
251 $tpl->fillin("CONTENT",encode_entities
($$content));
253 my $output = header
(-type
=> "text/html");
254 $output .= $tpl->get_template;
264 # Params: 1. Reference to user input hash
265 # 2. Reference to config hash
267 # Return: Output of the command (Scalar Reference)
269 sub exec_canceledit
($$)
271 my ($data,$config) = @_;
272 my $virtual = $data->{'virtual'};
273 my $uselist = $data->{'uselist'};
275 file_unlock
($uselist,$virtual);
276 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
281 # Save a file, unlock it and return to directory view
283 # Params: 1. Reference to user input hash
284 # 2. Reference to config hash
286 # Return: Output of the command (Scalar Reference)
290 my ($data,$config) = @_;
291 my $physical = $data->{'physical'};
292 my $virtual = $data->{'virtual'};
293 my $content = $data->{'cgi'}->param('filecontent');
295 return error
($config->{'err_editdir'},upper_path
($virtual)) if(-d
$physical);
296 return error
($config->{'err_noedit'}, upper_path
($virtual)) unless(-r
$physical && -w
$physical);
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'};
317 if(file_save
($physical,\
$content))
319 # Saving of the file was successful - so unlock it!
321 file_unlock
($data->{'uselist'},$virtual);
322 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
326 return error
($config->{'err_editfailed'},upper_path
($virtual),{FILE
=> $virtual});
332 # Create a file and return to directory view
334 # Params: 1. Reference to user input hash
335 # 2. Reference to config hash
337 # Return: Output of the command (Scalar Reference)
341 my ($data,$config) = @_;
342 my $new_physical = $data->{'new_physical'};
343 my $new_virtual = $data->{'new_virtual'};
344 my $dir = upper_path
($new_virtual);
345 $new_virtual = encode_entities
($new_virtual);
347 return error
("A file or directory called '$new_virtual' already exists.",$dir) if(-e
$new_physical);
349 file_create
($new_physical) or return error
("Could not create file '$new_virtual'.",$dir);
350 return devedit_reload
({command
=> 'show', file
=> $dir});
355 # Create a directory and return to directory view
357 # Params: 1. Reference to user input hash
358 # 2. Reference to config hash
360 # Return: Output of the command (Scalar Reference)
364 my ($data,$config) = @_;
365 my $new_physical = $data->{'new_physical'};
366 my $new_virtual = $data->{'new_virtual'};
367 my $dir = upper_path
($new_virtual);
368 $new_virtual = encode_entities
($new_virtual);
370 return error
("A file or directory called '$new_virtual' already exists.",$dir) if(-e
$new_physical);
372 mkdir($new_physical,0777) or return error
("Could not create directory '$new_virtual'.",$dir);
373 return devedit_reload
({command
=> 'show', file
=> $dir});
378 # Copy a file and return to directory view
380 # Params: 1. Reference to user input hash
381 # 2. Reference to config hash
383 # Return: Output of the command (Scalar Reference)
387 my ($data,$config) = @_;
388 my $physical = $data->{'physical'};
389 my $virtual = encode_entities
($data->{'virtual'});
390 my $new_physical = $data->{'new_physical'};
392 return error
($config->{'err_dircopy'}) if(-d
$physical);
393 return error
($config->{'err_nocopy'}) unless(-r
$physical);
397 my $new_virtual = $data->{'new_virtual'};
398 my $dir = upper_path
($new_virtual);
399 $new_virtual = encode_entities
($new_virtual);
405 return error
("A directory called '$new_virtual' already exists. You cannot replace a directory by a file!",$dir);
407 elsif(not $data->{'cgi'}->param('confirmed'))
409 my $tpl = new Template
;
410 $tpl->read_file($config->{'tpl_confirm_replace'});
412 $tpl->fillin("FILE",$virtual);
413 $tpl->fillin("NEW_FILE",$new_virtual);
414 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
415 $tpl->fillin("NEW_DIR",$dir);
416 $tpl->fillin("DIR",upper_path
($virtual));
417 $tpl->fillin("COMMAND","copy");
418 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
419 $tpl->fillin("SCRIPT",$script);
421 my $output = header
(-type
=> "text/html");
422 $output .= $tpl->get_template;
428 if($data->{'uselist'}->in_use($data->{'new_virtual'}))
430 return error
("The target file '$new_virtual' already exists and it is edited by someone else.",$dir);
433 copy
($physical,$new_physical) or return error
("Could not copy '$virtual' to '$new_virtual'",upper_path
($virtual));
434 return devedit_reload
({command
=> 'show', file
=> $dir});
438 my $tpl = new Template
;
439 $tpl->read_file($config->{'tpl_copyfile'});
441 $tpl->fillin("FILE",$virtual);
442 $tpl->fillin("DIR",upper_path
($virtual));
443 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
444 $tpl->fillin("SCRIPT",$script);
446 my $output = header
(-type
=> "text/html");
447 $output .= $tpl->get_template;
455 # Rename/move a file and return to directory view
457 # Params: 1. Reference to user input hash
458 # 2. Reference to config hash
460 # Return: Output of the command (Scalar Reference)
464 my ($data,$config) = @_;
465 my $physical = $data->{'physical'};
466 my $virtual = $data->{'virtual'};
467 my $new_physical = $data->{'new_physical'};
468 my $new_virtual = $data->{'new_virtual'};
469 my $dir = upper_path
($new_virtual);
470 $new_virtual = encode_entities
($new_virtual);
472 return error_in_use
($virtual) if($data->{'uselist'}->in_use($virtual));
476 my $new_virtual = $data->{'new_virtual'};
477 my $dir = upper_path
($new_virtual);
478 $new_virtual = encode_entities
($new_virtual);
484 return error
("A directory called '$new_virtual' already exists. You cannot replace a directory by a file!",$dir);
486 elsif(not $data->{'cgi'}->param('confirmed'))
488 my $tpl = new Template
;
489 $tpl->read_file($config->{'tpl_confirm_replace'});
491 $tpl->fillin("FILE",$virtual);
492 $tpl->fillin("NEW_FILE",$new_virtual);
493 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
494 $tpl->fillin("NEW_DIR",$dir);
495 $tpl->fillin("DIR",upper_path
($virtual));
496 $tpl->fillin("COMMAND","rename");
497 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
498 $tpl->fillin("SCRIPT",$script);
500 my $output = header
(-type
=> "text/html");
501 $output .= $tpl->get_template;
507 if($data->{'uselist'}->in_use($data->{'new_virtual'}))
509 return error
("The target file '$new_virtual' already exists and it is edited by someone else.",$dir);
512 rename($physical,$new_physical) or return error
("Could not move/rename '$virtual' to '$new_virtual'",upper_path
($virtual));
513 return devedit_reload
({command
=> 'show', file
=> $dir});
517 my $tpl = new Template
;
518 $tpl->read_file($config->{'tpl_renamefile'});
520 $tpl->fillin("FILE",$virtual);
521 $tpl->fillin("DIR",upper_path
($virtual));
522 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
523 $tpl->fillin("SCRIPT",$script);
525 my $output = header
(-type
=> "text/html");
526 $output .= $tpl->get_template;
534 # Remove a file or a directory and return to directory view
536 # Params: 1. Reference to user input hash
537 # 2. Reference to config hash
539 # Return: Output of the command (Scalar Reference)
543 my ($data,$config) = @_;
544 my $physical = $data->{'physical'};
545 my $virtual = $data->{'virtual'};
551 if($data->{'cgi'}->param('confirmed'))
554 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
558 my $tpl = new Template
;
559 $tpl->read_file($config->{'tpl_confirm_rmdir'});
561 $tpl->fillin("DIR",$virtual);
562 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
563 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
564 $tpl->fillin("SCRIPT",$script);
566 my $output = header
(-type
=> "text/html");
567 $output .= $tpl->get_template;
576 return error_in_use
($virtual) if($data->{'uselist'}->in_use($virtual));
578 if($data->{'cgi'}->param('confirmed'))
580 unlink($physical) or return error
($config->{'err_editfailed'},upper_path
($virtual),{FILE
=> $virtual});
581 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
585 my $tpl = new Template
;
586 $tpl->read_file($config->{'tpl_confirm_rmfile'});
588 $tpl->fillin("FILE",$virtual);
589 $tpl->fillin("DIR",upper_path
($virtual));
590 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
591 $tpl->fillin("SCRIPT",$script);
593 my $output = header
(-type
=> "text/html");
594 $output .= $tpl->get_template;
603 # Remove a file from the list of used files and
604 # return to directory view
606 # Params: 1. Reference to user input hash
607 # 2. Reference to config hash
609 # Return: Output of the command (Scalar Reference)
613 my ($data,$config) = @_;
614 my $virtual = $data->{'virtual'};
615 my $uselist = $data->{'uselist'};
617 if($data->{'cgi'}->param('confirmed'))
619 file_unlock
($uselist,$virtual);
620 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
624 my $tpl = new Template
;
625 $tpl->read_file($config->{'tpl_confirm_unlock'});
627 $tpl->fillin("FILE",$virtual);
628 $tpl->fillin("DIR",upper_path
($virtual));
629 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
630 $tpl->fillin("SCRIPT",$script);
632 my $output = header
(-type
=> "text/html");
633 $output .= $tpl->get_template;
639 # it's true, baby ;-)
patrick-canterino.de