]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
d84d42f0ab2dba1579ca3077aa11918adbfa7f2c
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2004-02-06
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'};
86 # Create directory listing
88 my $direntries = dir_read
($physical);
89 return error
("Reading of directory $virtual failed.",upper_path
($virtual)) unless($direntries);
91 my $files = $direntries->{'files'};
92 my $dirs = $direntries->{'dirs'};
96 # Create the link to the upper directory
97 # (only if we are not in the root directory)
99 unless($virtual eq "/")
101 my @stat = stat($physical."/..");
103 my $udtpl = new Template
;
104 $udtpl->read_file($config->{'tpl_dirlist_up'});
106 $udtpl->fillin("UPPER_DIR",encode_entities
(upper_path
($virtual)));
107 $udtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
109 $dirlist .= $udtpl->get_template;
114 foreach my $dir(@
$dirs)
116 my @stat = stat($physical."/".$dir);
117 my $virt_path = encode_entities
($virtual.$dir."/");
119 my $dtpl = new Template
;
120 $dtpl->read_file($config->{'tpl_dirlist_dir'});
122 $dtpl->fillin("DIR",$virt_path);
123 $dtpl->fillin("DIR_NAME",$dir);
124 $dtpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
126 $dirlist .= $dtpl->get_template;
131 foreach my $file(@
$files)
133 my $phys_path = $physical."/".$file;
134 my $virt_path = encode_entities
($virtual.$file);
136 my @stat = stat($phys_path);
137 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
139 my $ftpl = new Template
;
140 $ftpl->read_file($config->{'tpl_dirlist_file'});
142 $ftpl->fillin("FILE",$virt_path);
143 $ftpl->fillin("FILE_NAME",$file);
144 $ftpl->fillin("SIZE",$stat[7]);
145 $ftpl->fillin("DATE",strftime
($config->{'timeformat'},localtime($stat[9])));
147 $ftpl->parse_if_block("not_readable",not -r
$phys_path);
148 $ftpl->parse_if_block("binary",-B
$phys_path);
149 $ftpl->parse_if_block("readonly",not -w
$phys_path);
151 $ftpl->parse_if_block("viewable",-r
$phys_path && -T
$phys_path);
152 $ftpl->parse_if_block("editable",-w
$phys_path && -r
$phys_path && -T
$phys_path && not $in_use);
154 $ftpl->parse_if_block("in_use",$in_use);
155 $ftpl->parse_if_block("unused",not $in_use);
157 $dirlist .= $ftpl->get_template;
160 my $tpl = new 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));
168 $output = header
(-type
=> "text/html");
169 $output .= $tpl->get_template;
175 return error
("You have not enough permissions to view this file.",upper_path
($virtual)) unless(-r
$physical);
177 # Check on binary files
178 # We have to do it in this way, or empty files
179 # will be recognized as binary files
185 return error
($config->{'err_binary'},upper_path
($virtual));
191 my $content = file_read
($physical);
193 my $tpl = new Template
;
194 $tpl->read_file($config->{'tpl_viewfile'});
196 $tpl->fillin("FILE",$virtual);
197 $tpl->fillin("DIR",upper_path
($virtual));
198 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
199 $tpl->fillin("SCRIPT",$script);
200 $tpl->fillin("CONTENT",encode_entities
($$content));
202 $output = header
(-type
=> "text/html");
203 $output .= $tpl->get_template;
212 # Lock a file and display a form to edit it
214 # Params: 1. Reference to user input hash
215 # 2. Reference to config hash
217 # Return: Output of the command (Scalar Reference)
219 sub exec_beginedit
($$)
221 my ($data,$config) = @_;
222 my $physical = $data->{'physical'};
223 my $virtual = $data->{'virtual'};
224 my $uselist = $data->{'uselist'};
226 return error
($config->{'err_editdir'},upper_path
($virtual)) if(-d
$physical);
227 return error_in_use
($virtual) if($uselist->in_use($virtual));
228 return error
($config->{'err_noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
230 # Check on binary files
236 return error
($config->{'err_binary'},upper_path
($virtual));
242 $uselist->add_file($virtual);
245 my $content = file_read
($physical);
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'};
276 my $uselist = $data->{'uselist'};
278 file_unlock
($uselist,$virtual);
279 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
284 # Save a file, unlock it and return to directory view
286 # Params: 1. Reference to user input hash
287 # 2. Reference to config hash
289 # Return: Output of the command (Scalar Reference)
293 my ($data,$config) = @_;
294 my $physical = $data->{'physical'};
295 my $virtual = $data->{'virtual'};
296 my $content = $data->{'cgi'}->param('filecontent');
298 return error
($config->{'err_editdir'},upper_path
($virtual)) if(-d
$physical);
299 return error
($config->{'err_noedit'},upper_path
($virtual)) unless(-r
$physical && -w
$physical);
303 $content =~ s/\015\012|\012|\015/\n/g;
305 if($data->{'cgi'}->param('encode_iso'))
307 # Encode all ISO-8859-1 special chars
309 $content = encode_entities
($content,"\200-\377");
312 if($data->{'cgi'}->param('saveas'))
314 # Create the new filename
316 $physical = $data->{'new_physical'};
317 $virtual = $data->{'new_virtual'};
320 if(file_save
($physical,\
$content))
322 # Saving of the file was successful - so unlock it!
324 file_unlock
($data->{'uselist'},$virtual);
325 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
329 return error
($config->{'err_editfailed'},upper_path
($virtual),{FILE
=> $virtual});
335 # Create a file and return to directory view
337 # Params: 1. Reference to user input hash
338 # 2. Reference to config hash
340 # Return: Output of the command (Scalar Reference)
344 my ($data,$config) = @_;
345 my $new_physical = $data->{'new_physical'};
346 my $new_virtual = $data->{'new_virtual'};
347 my $dir = upper_path
($new_virtual);
348 $new_virtual = encode_entities
($new_virtual);
350 return error
("A file or directory called '$new_virtual' already exists.",$dir) if(-e
$new_physical);
352 file_create
($new_physical) or return error
("Could not create file '$new_virtual'.",$dir);
353 return devedit_reload
({command
=> 'show', file
=> $dir});
358 # Create a directory and return to directory view
360 # Params: 1. Reference to user input hash
361 # 2. Reference to config hash
363 # Return: Output of the command (Scalar Reference)
367 my ($data,$config) = @_;
368 my $new_physical = $data->{'new_physical'};
369 my $new_virtual = $data->{'new_virtual'};
370 my $dir = upper_path
($new_virtual);
371 $new_virtual = encode_entities
($new_virtual);
373 return error
("A file or directory called '$new_virtual' already exists.",$dir) if(-e
$new_physical);
375 mkdir($new_physical,0777) or return error
("Could not create directory '$new_virtual'.",$dir);
376 return devedit_reload
({command
=> 'show', file
=> $dir});
381 # Copy a file and return to directory view
383 # Params: 1. Reference to user input hash
384 # 2. Reference to config hash
386 # Return: Output of the command (Scalar Reference)
390 my ($data,$config) = @_;
391 my $physical = $data->{'physical'};
392 my $virtual = encode_entities
($data->{'virtual'});
393 my $new_physical = $data->{'new_physical'};
394 my $new_virtual = $data->{'new_virtual'};
395 my $dir = upper_path
($new_virtual);
396 $new_virtual = encode_entities
($new_virtual);
398 return error
("This editor is not able to copy directories.") if(-d
$physical);
399 return error
("You have not enough permissions to copy this file.") unless(-r
$physical);
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("DIR",upper_path
($virtual));
415 $tpl->fillin("COMMAND","copy");
416 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
417 $tpl->fillin("SCRIPT",$script);
419 my $output = header
(-type
=> "text/html");
420 $output .= $tpl->get_template;
426 if($data->{'uselist'}->in_use($data->{'new_virtual'}))
428 return error
("The target file '$new_virtual' already exists and it is edited by someone else.",$dir);
431 copy
($physical,$new_physical) or return error
("Could not copy '$virtual' to '$new_virtual'",upper_path
($virtual));
432 return devedit_reload
({command
=> 'show', file
=> $dir});
437 # Rename/move a file and return to directory view
439 # Params: 1. Reference to user input hash
440 # 2. Reference to config hash
442 # Return: Output of the command (Scalar Reference)
446 my ($data,$config) = @_;
447 my $physical = $data->{'physical'};
448 my $virtual = $data->{'virtual'};
449 my $new_physical = $data->{'new_physical'};
450 my $new_virtual = $data->{'new_virtual'};
451 my $dir = upper_path
($new_virtual);
452 $new_virtual = encode_entities
($new_virtual);
454 return error_in_use
($virtual) if($data->{'uselist'}->in_use($virtual));
460 return error
("A directory called '$new_virtual' already exists. You cannot replace a directory!",upper_path
($virtual));
462 elsif(not $data->{'cgi'}->param('confirmed'))
464 my $tpl = new Template
;
465 $tpl->read_file($config->{'tpl_confirm_replace'});
467 $tpl->fillin("FILE",$virtual);
468 $tpl->fillin("NEW_FILE",$new_virtual);
469 $tpl->fillin("DIR",upper_path
($virtual));
470 $tpl->fillin("COMMAND","rename");
471 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
472 $tpl->fillin("SCRIPT",$script);
474 my $output = header
(-type
=> "text/html");
475 $output .= $tpl->get_template;
481 if($data->{'uselist'}->in_use($data->{'new_virtual'}))
483 return error
("The target file '$new_virtual' already exists and it is edited by someone else.",$dir);
486 rename($physical,$new_physical) or return error
("Could not move/rename '".encode_entities
($virtual)."' to '$new_virtual'.",upper_path
($virtual));
487 return devedit_reload
({command
=> 'show', file
=> $dir});
492 # Remove a file or a directory and return to directory view
494 # Params: 1. Reference to user input hash
495 # 2. Reference to config hash
497 # Return: Output of the command (Scalar Reference)
501 my ($data,$config) = @_;
502 my $physical = $data->{'physical'};
503 my $virtual = $data->{'virtual'};
509 if($data->{'cgi'}->param('confirmed'))
512 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
516 my $tpl = new Template
;
517 $tpl->read_file($config->{'tpl_confirm_rmdir'});
519 $tpl->fillin("DIR",$virtual);
520 $tpl->fillin("UPPER_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;
534 return error_in_use
($virtual) if($data->{'uselist'}->in_use($virtual));
536 if($data->{'cgi'}->param('confirmed'))
538 unlink($physical) or return error
($config->{'err_editfailed'},upper_path
($virtual),{FILE
=> $virtual});
539 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
543 my $tpl = new Template
;
544 $tpl->read_file($config->{'tpl_confirm_rmfile'});
546 $tpl->fillin("FILE",$virtual);
547 $tpl->fillin("DIR",upper_path
($virtual));
548 $tpl->fillin("URL",equal_url
($config->{'httproot'},$virtual));
549 $tpl->fillin("SCRIPT",$script);
551 my $output = header
(-type
=> "text/html");
552 $output .= $tpl->get_template;
561 # Remove a file from the list of used files and
562 # return to directory view
564 # Params: 1. Reference to user input hash
565 # 2. Reference to config hash
567 # Return: Output of the command (Scalar Reference)
571 my ($data,$config) = @_;
572 my $virtual = $data->{'virtual'};
573 my $uselist = $data->{'uselist'};
575 if($data->{'cgi'}->param('confirmed'))
577 file_unlock
($uselist,$virtual);
578 return devedit_reload
({command
=> 'show', file
=> upper_path
($virtual)});
582 my $tpl = new Template
;
583 $tpl->read_file($config->{'tpl_confirm_unlock'});
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;
597 # it's true, baby ;-)
patrick-canterino.de