]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
cddb5ca37a2e3559fd8c1d7c060fb6cbb1fa905c
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2004-02-23
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
($config->{'err_cmd_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;
161 $tpl->read_file($config->{'tpl_dirlist'});
163 $tpl->fillin("DIRLIST",$dirlist);
164 $tpl->fillin("DIR",$virtual);
165 $tpl->fillin("SCRIPT",$script);
166 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
172 return error
($config->{'err_noview'},upper_path
($virtual)) unless(-r
$physical);
174 # Check on binary files
175 # We have to do it in this way, or empty files
176 # will be recognized as binary files
182 return error
($config->{'err_binary'},upper_path
($virtual));
188 my $content = file_read
($physical);
189 $$content =~ s/\015\012|\012|\015/\n/g;
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);
243 $$content =~ s/\015\012|\012|\015/\n/g;
245 my $tpl = new Template
;
246 $tpl->read_file($config->{'tpl_editfile'});
248 $tpl->fillin("FILE",$virtual);
249 $tpl->fillin("DIR",upper_path
($virtual));
250 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
251 $tpl->fillin("SCRIPT",$script);
252 $tpl->fillin("CONTENT",encode_entities
($$content));
254 my $output = header
(-type
=> "text/html");
255 $output .= $tpl->get_template;
265 # Params: 1. Reference to user input hash
266 # 2. Reference to config hash
268 # Return: Output of the command (Scalar Reference)
270 sub exec_canceledit
($$)
272 my ($data,$config) = @_;
273 my $virtual = $data->{'virtual'};
275 file_unlock
($data->{'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');
294 my $uselist = $data->{'uselist'};
298 $content =~ s/\015\012|\012|\015/\n/g;
300 if($data->{'cgi'}->param('encode_iso'))
302 # Encode all ISO-8859-1 special chars
304 $content = encode_entities
($content,"\200-\377");
307 if($data->{'cgi'}->param('saveas'))
309 # Create the new filename
311 $physical = $data->{'new_physical'};
312 $virtual = $data->{'new_virtual'};
314 # Check if someone else is editing the new file
316 return error_in_use
($virtual) if($uselist->in_use($virtual));
319 return error
($config->{'err_editdir'},upper_path
($virtual)) if(-d
$physical);
320 return error
($config->{'err_noedit'}, upper_path
($virtual)) unless(-r
$physical && -w
$physical);
322 if(file_save
($physical,\
$content))
324 # Saving of the file was successful - so unlock it!
326 file_unlock
($uselist,$data->{'virtual'});
328 # Maybe the user saved the file using another filename...
329 # But we have to unlock the original file!
331 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
335 return error
($config->{'err_edit_failed'},upper_path
($virtual),{FILE
=> $virtual});
341 # Create a file and return to directory view
343 # Params: 1. Reference to user input hash
344 # 2. Reference to config hash
346 # Return: Output of the command (Scalar Reference)
350 my ($data,$config) = @_;
351 my $new_physical = $data->{'new_physical'};
352 my $new_virtual = $data->{'new_virtual'};
353 my $dir = upper_path
($new_virtual);
354 $new_virtual = encode_entities
($new_virtual);
356 return error
($config->{'err_file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
358 file_create
($new_physical) or return error
($config->{'err_mkfile_failed'},$dir,{FILE
=> $new_virtual});
359 return devedit_reload
({command
=> 'show', file
=> $dir});
364 # Create a directory and return to directory view
366 # Params: 1. Reference to user input hash
367 # 2. Reference to config hash
369 # Return: Output of the command (Scalar Reference)
373 my ($data,$config) = @_;
374 my $new_physical = $data->{'new_physical'};
375 my $new_virtual = $data->{'new_virtual'};
376 my $dir = upper_path
($new_virtual);
377 $new_virtual = encode_entities
($new_virtual);
379 return error
($config->{'err_file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
381 mkdir($new_physical,0777) or return error
($config->{'err_mkdir_failed'},$dir,{DIR
=> $new_virtual});
382 return devedit_reload
({command
=> 'show', file
=> $dir});
387 # Copy a file and return to directory view
389 # Params: 1. Reference to user input hash
390 # 2. Reference to config hash
392 # Return: Output of the command (Scalar Reference)
396 my ($data,$config) = @_;
397 my $physical = $data->{'physical'};
398 my $virtual = encode_entities
($data->{'virtual'});
399 my $new_physical = $data->{'new_physical'};
401 return error
($config->{'err_nocopy'}) unless(-r
$physical);
405 my $new_virtual = $data->{'new_virtual'};
406 my $dir = upper_path
($new_virtual);
407 $new_virtual = encode_entities
($new_virtual);
411 return error
($config->{'err_exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
415 return error
($config->{'err_dircopy'});
417 elsif(not $data->{'cgi'}->param('confirmed'))
419 my $tpl = new Template
;
420 $tpl->read_file($config->{'tpl_confirm_replace'});
422 $tpl->fillin("FILE",$virtual);
423 $tpl->fillin("NEW_FILE",$new_virtual);
424 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
425 $tpl->fillin("NEW_DIR",$dir);
426 $tpl->fillin("DIR",upper_path
($virtual));
427 $tpl->fillin("COMMAND","copy");
428 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
429 $tpl->fillin("SCRIPT",$script);
431 my $output = header
(-type
=> "text/html");
432 $output .= $tpl->get_template;
438 copy
($physical,$new_physical) or return error
($config->{'err_copy_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
439 return devedit_reload
({command
=> 'show', file
=> $dir});
443 my $tpl = new Template
;
444 $tpl->read_file($config->{'tpl_copyfile'});
446 $tpl->fillin("FILE",$virtual);
447 $tpl->fillin("DIR",upper_path
($virtual));
448 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
449 $tpl->fillin("SCRIPT",$script);
451 my $output = header
(-type
=> "text/html");
452 $output .= $tpl->get_template;
460 # Rename/move a file and return to directory view
462 # Params: 1. Reference to user input hash
463 # 2. Reference to config hash
465 # Return: Output of the command (Scalar Reference)
469 my ($data,$config) = @_;
470 my $physical = $data->{'physical'};
471 my $virtual = $data->{'virtual'};
472 my $new_physical = $data->{'new_physical'};
474 return error_in_use
($virtual) if($data->{'uselist'}->in_use($virtual));
478 my $new_virtual = $data->{'new_virtual'};
479 my $dir = upper_path
($new_virtual);
480 $new_virtual = encode_entities
($new_virtual);
484 return error
($config->{'err_exist_edited'},$dir,{FILE
=> $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
488 return error
($config->{'err_dircopy'});
490 elsif(not $data->{'cgi'}->param('confirmed'))
492 my $tpl = new Template
;
493 $tpl->read_file($config->{'tpl_confirm_replace'});
495 $tpl->fillin("FILE",$virtual);
496 $tpl->fillin("NEW_FILE",$new_virtual);
497 $tpl->fillin("NEW_FILENAME",file_name
($new_virtual));
498 $tpl->fillin("NEW_DIR",$dir);
499 $tpl->fillin("DIR",upper_path
($virtual));
500 $tpl->fillin("COMMAND","rename");
501 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
502 $tpl->fillin("SCRIPT",$script);
504 my $output = header
(-type
=> "text/html");
505 $output .= $tpl->get_template;
511 rename($physical,$new_physical) or return error
($config->{'err_rename_failed'},upper_path
($virtual),{FILE
=> $virtual, NEW_FILE
=> $new_virtual});
512 return devedit_reload
({command
=> 'show', file
=> $dir});
516 my $tpl = new Template
;
517 $tpl->read_file($config->{'tpl_renamefile'});
519 $tpl->fillin("FILE",$virtual);
520 $tpl->fillin("DIR",upper_path
($virtual));
521 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
522 $tpl->fillin("SCRIPT",$script);
524 my $output = header
(-type
=> "text/html");
525 $output .= $tpl->get_template;
533 # Remove a file or a directory and return to directory view
535 # Params: 1. Reference to user input hash
536 # 2. Reference to config hash
538 # Return: Output of the command (Scalar Reference)
542 my ($data,$config) = @_;
543 my $physical = $data->{'physical'};
544 my $virtual = $data->{'virtual'};
550 if($data->{'cgi'}->param('confirmed'))
553 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
557 my $tpl = new Template
;
558 $tpl->read_file($config->{'tpl_confirm_rmdir'});
560 $tpl->fillin("DIR",$virtual);
561 $tpl->fillin("UPPER_DIR",upper_path
($virtual));
562 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
563 $tpl->fillin("SCRIPT",$script);
565 my $output = header
(-type
=> "text/html");
566 $output .= $tpl->get_template;
575 return error_in_use
($virtual) if($data->{'uselist'}->in_use($virtual));
577 if($data->{'cgi'}->param('confirmed'))
579 unlink($physical) or return error
($config->{'err_delete_failed'},upper_path
($virtual),{FILE
=> $virtual});
580 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
584 my $tpl = new Template
;
585 $tpl->read_file($config->{'tpl_confirm_rmfile'});
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;
602 # Remove a file from the list of used files and
603 # return to directory view
605 # Params: 1. Reference to user input hash
606 # 2. Reference to config hash
608 # Return: Output of the command (Scalar Reference)
612 my ($data,$config) = @_;
613 my $virtual = $data->{'virtual'};
615 if($data->{'cgi'}->param('confirmed'))
617 file_unlock
($data->{'uselist'},$virtual);
618 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
622 my $tpl = new Template
;
623 $tpl->read_file($config->{'tpl_confirm_unlock'});
625 $tpl->fillin("FILE",$virtual);
626 $tpl->fillin("DIR",upper_path
($virtual));
627 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
628 $tpl->fillin("SCRIPT",$script);
630 my $output = header
(-type
=> "text/html");
631 $output .= $tpl->get_template;
637 # it's true, baby ;-)
patrick-canterino.de