]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2009-12-29
11 # Copyright (C) 1999-2000 Roland Bluethgen, Frank Schoenmann
12 # Copyright (C) 2003-2009 Patrick Canterino
13 # All Rights Reserved.
15 # This file can be distributed and/or modified under the terms of
16 # of the Artistic License 1.0 (see also the LICENSE file found at
17 # the top level of the Dev-Editor distribution).
29 use Digest::MD5 qw(md5_hex);
30 use POSIX
qw(strftime);
39 my $script = encode_html
($ENV{'SCRIPT_NAME'});
40 my $users = eval('getpwuid(0)') && eval('getgrgid(0)');
42 my %dispatch = ('show' => \
&exec_show
,
43 'beginedit' => \
&exec_beginedit
,
44 'endedit' => \
&exec_endedit
,
45 'download' => \
&exec_download
,
46 'mkdir' => \
&exec_mkdir
,
47 'mkfile' => \
&exec_mkfile
,
48 'upload' => \
&exec_upload
,
49 'copy' => \
&exec_copy
,
50 'rename' => \
&exec_rename
,
51 'remove' => \
&exec_remove
,
52 'remove_multi' => \
&exec_remove_multi
,
53 'chprop' => \
&exec_chprop
,
54 'about' => \
&exec_about
59 use base
qw(Exporter);
61 @EXPORT = qw(exec_command);
65 # Execute the specified command
67 # Params: 1. Command to execute
68 # 2. Reference to user input hash
69 # 3. Reference to config hash
71 # Return: Output of the command (Scalar Reference)
75 my ($command,$data,$config) = @_;
77 foreach(keys(%dispatch))
79 if(lc($_) eq lc($command))
81 my $output = &{$dispatch{$_}}($data,$config);
86 return error
($config->{'errors'}->{'command_unknown'},'/',{COMMAND
=> encode_html
($command)});
91 # View a directory or a file
93 # Params: 1. Reference to user input hash
94 # 2. Reference to config hash
96 # Return: Output of the command (Scalar Reference)
100 my ($data,$config) = @_;
101 my $physical = $data->{'physical'};
102 my $virtual = $data->{'virtual'};
103 my $upper_path = multi_string
(upper_path
($virtual));
105 my $tpl = new Template
;
107 if(-d
$physical && not -l
$physical)
109 # Create directory listing
111 return error
($config->{'errors'}->{'no_dir_access'},$upper_path->{'normal'}) unless(-r
$physical && -x
$physical);
113 my $direntries = dir_read
($physical);
114 return error
($config->{'errors'}->{'dir_read_failed'},$upper_path->{'normal'},{DIR
=> encode_html
($virtual)}) unless($direntries);
116 my $files = $direntries->{'files'};
117 my $dirs = $direntries->{'dirs'};
123 my $filter1 = $data->{'cgi'}->param('filter') || '*'; # The real wildcard
124 my $filter2 = ($filter1 && $filter1 ne '*') ?
$filter1 : ''; # Wildcard for output
126 # Create the link to the upper directory
127 # (only if the current directory is not the root directory)
129 unless($virtual eq '/')
133 my @stat = stat($physical.'/..');
135 my $udtpl = new Template
;
136 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
138 $udtpl->fillin('UPPER_DIR',$upper_path->{'html'});
139 $udtpl->fillin('UPPER_DIR_URL',$upper_path->{'url'});
140 $udtpl->fillin('DATE',encode_html
(strftime
($config->{'timeformat'},($config->{'use_gmt'}) ?
gmtime($stat[9]) : localtime($stat[9]))));
142 $dirlist .= $udtpl->get_template;
147 foreach my $dir(@
$dirs)
149 next if($config->{'hide_dot_files'} && substr($dir,0,1) eq '.');
150 next unless(dos_wildcard_match
($filter1,$dir));
154 my $phys_path = $physical.'/'.$dir;
155 my $virt_path = multi_string
($virtual.$dir.'/');
157 my @stat = stat($phys_path);
159 my $dtpl = new Template
;
160 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
162 $dtpl->fillin('DIR',$virt_path->{'html'});
163 $dtpl->fillin('DIR_URL',$virt_path->{'url'});
164 $dtpl->fillin('DIR_NAME',encode_html
($dir));
165 $dtpl->fillin('DATE',encode_html
(strftime
($config->{'timeformat'},($config->{'use_gmt'}) ?
gmtime($stat[9]) : localtime($stat[9]))));
166 $dtpl->fillin('URL',equal_url
(encode_html
($config->{'httproot'}),$virt_path->{'html'}));
168 $dtpl->parse_if_block('forbidden',is_forbidden_file
($config->{'forbidden'},$virt_path->{'normal'}));
169 $dtpl->parse_if_block('readable',-r
$phys_path && -x
$phys_path);
170 $dtpl->parse_if_block('users',$users && -o
$phys_path);
171 $dtpl->parse_if_block('even',($count % 2) == 0);
173 $dirlist .= $dtpl->get_template;
178 foreach my $file(@
$files)
180 next if($config->{'hide_dot_files'} && substr($file,0,1) eq '.');
181 next unless(dos_wildcard_match
($filter1,$file));
185 my $phys_path = $physical.'/'.$file;
186 my $virt_path = multi_string
($virtual.$file);
188 my @stat = lstat($phys_path);
189 my $too_large = $config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'};
191 my $ftpl = new Template
;
192 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
194 $ftpl->fillin('FILE',$virt_path->{'html'});
195 $ftpl->fillin('FILE_URL',$virt_path->{'url'});
196 $ftpl->fillin('FILE_NAME',encode_html
($file));
197 $ftpl->fillin('FILE_URL',$virt_path->{'url'});
198 $ftpl->fillin('SIZE',$stat[7]);
199 $ftpl->fillin('DATE',encode_html
(strftime
($config->{'timeformat'},($config->{'use_gmt'}) ?
gmtime($stat[9]) : localtime($stat[9]))));
200 $ftpl->fillin('URL',equal_url
(encode_html
($config->{'httproot'}),$virt_path->{'html'}));
202 $ftpl->parse_if_block('link',-l
$phys_path);
203 $ftpl->parse_if_block('readable',-r
$phys_path);
204 $ftpl->parse_if_block('writeable',-w
$phys_path);
205 $ftpl->parse_if_block('binary',-B
$phys_path);
207 $ftpl->parse_if_block('forbidden',is_forbidden_file
($config->{'forbidden'},$virt_path->{'normal'}));
208 $ftpl->parse_if_block('viewable',(-r
$phys_path && -T
$phys_path && not $too_large) || -l
$phys_path);
209 $ftpl->parse_if_block('editable',(-r
$phys_path && -w
$phys_path && -T
$phys_path && not $too_large) && not -l
$phys_path);
211 $ftpl->parse_if_block('too_large',$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
213 $ftpl->parse_if_block('users',$users && -o
$phys_path);
215 $ftpl->parse_if_block('even',($count % 2) == 0);
217 $dirlist .= $ftpl->get_template;
220 $tpl->read_file($config->{'templates'}->{'dirlist'});
222 $tpl->fillin('DIRLIST',$dirlist);
223 $tpl->fillin('DIR',encode_html
($virtual));
224 $tpl->fillin('DIR_URL',escape
($virtual));
225 $tpl->fillin('SCRIPT',$script);
226 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
228 $tpl->fillin('FILTER',encode_html
($filter2));
229 $tpl->fillin('FILTER_URL',escape
($filter2));
231 $tpl->parse_if_block('empty',$dirlist eq '');
232 $tpl->parse_if_block('dir_writeable',-w
$physical);
233 $tpl->parse_if_block('filter',$filter2);
234 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
238 # Show the target of a symbolic link
240 my $link_target = readlink($physical);
242 $tpl->read_file($config->{'templates'}->{'viewlink'});
244 $tpl->fillin('FILE',encode_html
($virtual));
245 $tpl->fillin('DIR',$upper_path->{'html'});
246 $tpl->fillin('DIR_URL',$upper_path->{'url'});
247 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
248 $tpl->fillin('SCRIPT',$script);
250 $tpl->fillin('LINK_TARGET',encode_html
($link_target));
256 return error
($config->{'errors'}->{'no_view'},$upper_path->{'normal'}) unless(-r
$physical);
258 # Check on binary files
259 # We have to do it in this way or empty files will be recognized
262 return error
($config->{'errors'}->{'binary_file'},$upper_path->{'normal'}) unless(-T
$physical);
264 # Is the file too large?
266 return error
($config->{'errors'}->{'file_too_large'},$upper_path->{'normal'},{SIZE
=> $config->{'max_file_size'}}) if($config->{'max_file_size'} && -s
$physical > $config->{'max_file_size'});
270 my $content = file_read
($physical);
271 $$content =~ s/\015\012|\012|\015/\n/g;
273 $tpl->read_file($config->{'templates'}->{'viewfile'});
275 $tpl->fillin('FILE',encode_html
($virtual));
276 $tpl->fillin('FILE_URL',escape
($virtual));
277 $tpl->fillin('DIR',$upper_path->{'html'});
278 $tpl->fillin('DIR_URL',$upper_path->{'url'});
279 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
280 $tpl->fillin('SCRIPT',$script);
282 $tpl->parse_if_block('editable',-w
$physical);
284 $tpl->fillin('CONTENT',encode_html
($$content));
287 my $output = header
(-type
=> 'text/html');
288 $output .= $tpl->get_template;
295 # Lock a file and display a form to edit it
297 # Params: 1. Reference to user input hash
298 # 2. Reference to config hash
300 # Return: Output of the command (Scalar Reference)
302 sub exec_beginedit
($$)
304 my ($data,$config) = @_;
305 my $physical = $data->{'physical'};
306 my $virtual = $data->{'virtual'};
307 my $dir = upper_path
($virtual);
309 return error
($config->{'errors'}->{'link_edit'},$dir) if(-l
$physical);
310 return error
($config->{'errors'}->{'dir_edit'}, $dir) if(-d
$physical);
311 return error
($config->{'errors'}->{'no_edit'}, $dir) unless(-r
$physical && -w
$physical);
313 # Check on binary files
315 return error
($config->{'errors'}->{'binary_file'},$dir) unless(-T
$physical);
317 # Is the file too large?
319 return error
($config->{'errors'}->{'file_too_large'},$dir,{SIZE
=> $config->{'max_file_size'}}) if($config->{'max_file_size'} && -s
$physical > $config->{'max_file_size'});
321 # Show the editing form
323 my $content = file_read
($physical);
324 my $md5sum = md5_hex
($$content);
325 $$content =~ s/\015\012|\012|\015/\n/g;
327 my $tpl = new Template
;
328 $tpl->read_file($config->{'templates'}->{'editfile'});
330 $tpl->fillin('FILE',encode_html
($virtual));
331 $tpl->fillin('FILE_URL',escape
($virtual));
332 $tpl->fillin('DIR',encode_html
($dir));
333 $tpl->fillin('DIR_URL',escape
($dir));
334 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
335 $tpl->fillin('SCRIPT',$script);
336 $tpl->fillin('MD5SUM',$md5sum);
337 $tpl->fillin('CONTENT',encode_html
($$content));
339 $tpl->parse_if_block('error',0);
341 my $output = header
(-type
=> 'text/html');
342 $output .= $tpl->get_template;
349 # Save a file, unlock it and return to directory view
351 # Params: 1. Reference to user input hash
352 # 2. Reference to config hash
354 # Return: Output of the command (Scalar Reference)
358 my ($data,$config) = @_;
359 my $physical = $data->{'physical'};
360 my $virtual = $data->{'virtual'};
361 my $dir = upper_path
($virtual);
362 my $cgi = $data->{'cgi'};
363 my $content = $cgi->param('filecontent');
364 my $md5sum = $cgi->param('md5sum');
367 if(defined $content && $md5sum)
371 $content =~ s/\015\012|\012|\015/\n/g;
373 if($cgi->param('saveas') && $data->{'new_physical'} ne '' && $data->{'new_virtual'} ne '')
375 # Create the new filename
377 $physical = $data->{'new_physical'};
378 $virtual = $data->{'new_virtual'};
381 return error
($config->{'errors'}->{'link_edit'},$dir) if(-l
$physical);
382 return error
($config->{'errors'}->{'dir_edit'},$dir) if(-d
$physical);
383 return error
($config->{'errors'}->{'no_edit'},$dir) if(-e
$physical && !(-r
$physical && -w
$physical));
384 return error
($config->{'errors'}->{'text_to_binary'},$dir) if(-e
$physical && not -T
$physical);
386 # For technical reasons, we can't use file_save() for
391 sysopen(FILE
,$physical,O_RDWR
| O_CREAT
) or return error
($config->{'errors'}->{'edit_failed'},$dir,{FILE
=> encode_html
($virtual)});
392 file_lock
(*FILE
,LOCK_EX
) or do { close(FILE
); return error
($config->{'errors'}->{'edit_failed'},$dir,{FILE
=> encode_html
($virtual)}) };
394 my $md5 = new Digest
::MD5
;
395 $md5->addfile(*FILE
);
397 my $md5file = $md5->hexdigest;
398 my $md5data = md5_hex
($content);
400 if($md5file ne $md5sum && $md5data ne $md5file && not $cgi->param('saveas'))
402 # The file changed meanwhile
404 my $tpl = new Template
;
405 $tpl->read_file($config->{'templates'}->{'editfile'});
407 $tpl->fillin('ERROR',$config->{'errors'}->{'edit_file_changed'});
409 $tpl->fillin('FILE',encode_html
($virtual));
410 $tpl->fillin('FILE_URL',escape
($virtual));
411 $tpl->fillin('DIR',encode_html
($dir));
412 $tpl->fillin('DIR_URL',escape
($dir));
413 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
414 $tpl->fillin('SCRIPT',$script);
415 $tpl->fillin('MD5SUM',$md5file);
416 $tpl->fillin('CONTENT',encode_html
($content));
418 $tpl->parse_if_block('error',1);
420 my $data = header
(-type
=> 'text/html');
421 $data .= $tpl->get_template;
427 if($md5data ne $md5file)
435 $output = ($cgi->param('continue'))
436 ? devedit_reload
({command
=> 'beginedit', file
=> $virtual})
437 : devedit_reload
({command
=> 'show', file
=> $dir});
445 return devedit_reload
({command
=> 'beginedit', file
=> $virtual});
450 # Execute a HTTP download of a file
452 # Params: 1. Reference to user input hash
453 # 2. Reference to config hash
455 # Return: Output of the command (Scalar Reference)
457 sub exec_download
($$)
459 my ($data,$config) = @_;
460 my $physical = $data->{'physical'};
461 my $virtual = $data->{'virtual'};
462 my $dir = upper_path
($virtual);
464 return return error
($config->{'errors'}->{'no_download'},$dir,{FILE
=> $virtual}) if(-d
$physical || -l
$physical);
466 my $filename = file_name
($virtual);
468 my $output = header
(-type
=> 'application/octet-stream', -attachment
=> $filename);
469 $output .= ${ file_read
($physical,1) };
476 # Create a file and return to directory view
478 # Params: 1. Reference to user input hash
479 # 2. Reference to config hash
481 # Return: Output of the command (Scalar Reference)
485 my ($data,$config) = @_;
486 my $new_physical = $data->{'new_physical'};
487 my $new_virtual = $data->{'new_virtual'};
488 my $dir = upper_path
($new_virtual);
489 $new_virtual = encode_html
($new_virtual);
493 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
495 file_create
($new_physical) or return error
($config->{'errors'}->{'mkfile_failed'},$dir,{FILE
=> $new_virtual});
496 return devedit_reload
({command
=> 'show', file
=> $dir});
500 my $tpl = new Template
;
501 $tpl->read_file($config->{'templates'}->{'mkfile'});
503 $tpl->fillin('DIR','/');
504 $tpl->fillin('SCRIPT',$script);
506 my $output = header
(-type
=> 'text/html');
507 $output .= $tpl->get_template;
515 # Create a directory and return to directory view
517 # Params: 1. Reference to user input hash
518 # 2. Reference to config hash
520 # Return: Output of the command (Scalar Reference)
524 my ($data,$config) = @_;
525 my $new_physical = $data->{'new_physical'};
526 my $new_virtual = $data->{'new_virtual'};
527 my $dir = upper_path
($new_virtual);
528 $new_virtual = encode_html
($new_virtual);
532 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
534 mkdir($new_physical,0777) or return error
($config->{'errors'}->{'mkdir_failed'},$dir,{DIR
=> $new_virtual});
535 return devedit_reload
({command
=> 'show', file
=> $dir});
539 my $tpl = new Template
;
540 $tpl->read_file($config->{'templates'}->{'mkdir'});
542 $tpl->fillin('DIR','/');
543 $tpl->fillin('SCRIPT',$script);
545 my $output = header
(-type
=> 'text/html');
546 $output .= $tpl->get_template;
554 # Process a file upload
556 # Params: 1. Reference to user input hash
557 # 2. Reference to config hash
559 # Return: Output of the command (Scalar Reference)
563 my ($data,$config) = @_;
564 my $physical = $data->{'physical'};
565 my $virtual = $data->{'virtual'};
566 my $cgi = $data->{'cgi'};
568 return error
($config->{'errors'}->{'no_directory'},upper_path
($virtual),{FILE
=> encode_html
($virtual)}) unless(-d
$physical && not -l
$physical);
569 return error
($config->{'errors'}->{'dir_no_create'},$virtual,{DIR
=> encode_html
($virtual)}) unless(-w
$physical);
571 if(my $uploaded_file = $cgi->param('uploaded_file'))
573 if($cgi->param('remote_file'))
575 $uploaded_file = $cgi->param('remote_file');
577 $uploaded_file =~ s!/!!g;
578 $uploaded_file =~ s!\\!!g;
581 # Process file upload
583 my $filename = file_name
($uploaded_file);
584 my $file_phys = $physical.'/'.$filename;
585 my $file_virt = encode_html
($virtual.$filename);
589 return error
($config->{'errors'}->{'link_replace'},$virtual) if(-l
$file_phys);
590 return error
($config->{'errors'}->{'dir_replace'},$virtual) if(-d
$file_phys);
591 return error
($config->{'errors'}->{'exist_no_write'},$virtual,{FILE
=> $file_virt}) unless(-w
$file_phys);
592 return error
($config->{'errors'}->{'file_exists'},$virtual,{FILE
=> $file_virt}) unless($cgi->param('overwrite'));
595 my $ascii = $cgi->param('ascii');
596 my $handle = $cgi->upload('uploaded_file');
598 return error
($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
600 # Read transferred file and write it to disk
602 read($handle, my $data, -s
$handle);
603 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
604 file_save
($file_phys,\
$data,not $ascii) or return error
($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE
=> $file_virt});
606 return devedit_reload
({command
=> 'show', file
=> $virtual});
610 my $tpl = new Template
;
611 $tpl->read_file($config->{'templates'}->{'upload'});
613 $tpl->fillin('DIR',encode_html
($virtual));
614 $tpl->fillin('DIR_URL',escape
($virtual));
615 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
616 $tpl->fillin('SCRIPT',$script);
618 my $output = header
(-type
=> 'text/html');
619 $output .= $tpl->get_template;
627 # Copy a file and return to directory view
629 # Params: 1. Reference to user input hash
630 # 2. Reference to config hash
632 # Return: Output of the command (Scalar Reference)
636 my ($data,$config) = @_;
637 my $physical = $data->{'physical'};
638 my $virtual = $data->{'virtual'};
639 my $dir = upper_path
($virtual);
640 my $new_physical = $data->{'new_physical'};
642 return error
($config->{'errors'}->{'link_copy'},$dir) if(-l
$physical);
643 return error
($config->{'errors'}->{'no_copy'},$dir) unless(-r
$physical);
647 my $new_virtual = multi_string
($data->{'new_virtual'});
648 my $new_dir = upper_path
($new_virtual->{'normal'});
652 return error
($config->{'errors'}->{'no_copy'},$dir) unless(-x
$physical);
653 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual->{'html'}}) if(-e
$new_physical);
654 return error
($config->{'errors'}->{'dir_copy_self'},$dir) if(index($new_virtual->{'normal'},$virtual) == 0);
656 dir_copy
($physical,$new_physical) or return error
($config->{'errors'}->{'copy_failed'},$dir,{FILE
=> encode_html
($virtual), NEW_FILE
=> $new_virtual->{'html'}});
657 return devedit_reload
({command
=> 'show', file
=> $new_dir});
663 return error
($config->{'errors'}->{'link_replace'},$new_dir) if(-l
$new_physical);
664 return error
($config->{'errors'}->{'dir_replace'},$new_dir) if(-d
$new_physical);
665 return error
($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE
=> $new_virtual->{'html'}}) unless(-w
$new_physical);
667 if(not $data->{'cgi'}->param('confirmed'))
669 my $tpl = new Template
;
670 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
672 $tpl->fillin('FILE',encode_html
($virtual));
673 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
674 $tpl->fillin('NEW_FILENAME',file_name
($new_virtual->{'html'}));
675 $tpl->fillin('NEW_DIR',encode_html
($new_dir));
676 $tpl->fillin('DIR',encode_html
($dir));
677 $tpl->fillin('DIR_URL',escape
($dir));
679 $tpl->fillin('COMMAND','copy');
680 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
681 $tpl->fillin('SCRIPT',$script);
683 my $output = header
(-type
=> 'text/html');
684 $output .= $tpl->get_template;
690 copy
($physical,$new_physical) or return error
($config->{'errors'}->{'copy_failed'},$dir,{FILE
=> encode_html
($virtual), NEW_FILE
=> $new_virtual->{'html'}});
691 return devedit_reload
({command
=> 'show', file
=> $new_dir});
698 my $tpl = new Template
;
699 $tpl->read_file($config->{'templates'}->{'copydir'});
701 $tpl->fillin('FILE',encode_html
($virtual));
702 $tpl->fillin('DIR',encode_html
($dir));
703 $tpl->fillin('DIR_URL',escape
($dir));
704 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
705 $tpl->fillin('SCRIPT',$script);
707 my $output = header
(-type
=> 'text/html');
708 $output .= $tpl->get_template;
714 my $tpl = new Template
;
715 $tpl->read_file($config->{'templates'}->{'copyfile'});
717 $tpl->fillin('FILE',encode_html
($virtual));
718 $tpl->fillin('DIR',encode_html
($dir));
719 $tpl->fillin('DIR_URL',escape
($dir));
720 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
721 $tpl->fillin('SCRIPT',$script);
723 my $output = header
(-type
=> 'text/html');
724 $output .= $tpl->get_template;
733 # Rename/move a file and return to directory view
735 # Params: 1. Reference to user input hash
736 # 2. Reference to config hash
738 # Return: Output of the command (Scalar Reference)
742 my ($data,$config) = @_;
743 my $physical = $data->{'physical'};
744 my $virtual = $data->{'virtual'};
745 my $dir = upper_path
($virtual);
746 my $new_physical = $data->{'new_physical'};
748 return error
($config->{'errors'}->{'rename_root'},'/') if($virtual eq '/');
749 return error
($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path
($physical));
753 my $new_virtual = multi_string
($data->{'new_virtual'});
754 my $new_dir = upper_path
($new_virtual->{'normal'});
758 return error
($config->{'errors'}->{'dir_replace'},$new_dir) if(-d
$new_physical && not -l
$new_physical);
759 return error
($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE
=> $new_virtual}) unless(-w
$new_physical);
761 if(not $data->{'cgi'}->param('confirmed'))
763 my $tpl = new Template
;
764 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
766 $tpl->fillin('FILE',encode_html
($virtual));
767 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
768 $tpl->fillin('NEW_FILENAME',file_name
($new_virtual->{'html'}));
769 $tpl->fillin('NEW_DIR',encode_html
($new_dir));
770 $tpl->fillin('DIR',encode_html
($dir));
772 $tpl->fillin('COMMAND','rename');
773 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
774 $tpl->fillin('SCRIPT',$script);
776 my $output = header
(-type
=> 'text/html');
777 $output .= $tpl->get_template;
783 move
($physical,$new_physical) or return error
($config->{'errors'}->{'rename_failed'},$dir,{FILE
=> encode_html
($virtual), NEW_FILE
=> $new_virtual->{'html'}});
784 return devedit_reload
({command
=> 'show', file
=> $new_dir});
788 my $tpl = new Template
;
789 $tpl->read_file($config->{'templates'}->{'renamefile'});
791 $tpl->fillin('FILE',encode_html
($virtual));
792 $tpl->fillin('DIR',encode_html
($dir));
793 $tpl->fillin('DIR_URL',escape
($dir));
794 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
795 $tpl->fillin('SCRIPT',$script);
797 my $output = header
(-type
=> 'text/html');
798 $output .= $tpl->get_template;
806 # Remove a file or a directory and return to directory view
808 # Params: 1. Reference to user input hash
809 # 2. Reference to config hash
811 # Return: Output of the command (Scalar Reference)
815 my ($data,$config) = @_;
816 my $physical = $data->{'physical'};
817 my $virtual = $data->{'virtual'};
818 my $dir = upper_path
($virtual);
820 return error
($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
821 return error
($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path
($physical));
823 if(-d
$physical && not -l
$physical)
827 if($data->{'cgi'}->param('confirmed'))
830 return devedit_reload
({command
=> 'show', file
=> $dir});
834 my $tpl = new Template
;
835 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
837 $tpl->fillin('DIR',encode_html
($virtual));
838 $tpl->fillin('DIR_URL',escape
($virtual));
839 $tpl->fillin('UPPER_DIR',encode_html
($dir));
840 $tpl->fillin('UPPER_DIR_URL',escape
($dir));
841 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
842 $tpl->fillin('SCRIPT',$script);
844 my $output = header
(-type
=> 'text/html');
845 $output .= $tpl->get_template;
854 if($data->{'cgi'}->param('confirmed'))
856 unlink($physical) or return error
($config->{'errors'}->{'delete_failed'},$dir,{FILE
=> $virtual});
857 return devedit_reload
({command
=> 'show', file
=> $dir});
861 my $tpl = new Template
;
862 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
864 $tpl->fillin('FILE',encode_html
($virtual));
865 $tpl->fillin('FILE_URL',escape
($virtual));
866 $tpl->fillin('DIR',encode_html
($dir));
867 $tpl->fillin('DIR_URL',escape
($dir));
868 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
869 $tpl->fillin('SCRIPT',$script);
871 my $output = header
(-type
=> 'text/html');
872 $output .= $tpl->get_template;
879 # exec_remove_multi()
881 # Remove a file or a directory and return to directory view
883 # Params: 1. Reference to user input hash
884 # 2. Reference to config hash
886 # Return: Output of the command (Scalar Reference)
888 sub exec_remove_multi
($$)
890 my ($data,$config) = @_;
891 my $physical = $data->{'physical'};
892 my $virtual = $data->{'virtual'};
893 my $cgi = $data->{'cgi'};
895 my @files = $cgi->param('files');#
900 foreach my $file(@files)
902 # Filter out some "bad" files (e.g. files going up in the
903 # directory hierarchy or files containing slashes (it's too
906 next if($file =~ m!^\.+$!);
907 next if($file =~ m!/!);
908 next if($file =~ m!\\!);
910 push(@new_files,$file);
916 if($cgi->param('confirmed'))
921 foreach my $file(@new_files)
923 my $file_path = clean_path
($physical.'/'.$file);
927 if(-d
$file_path && not -l
$file_path)
931 if(rmtree
($file_path))
933 push(@success,clean_path
($file));
937 push(@failed,clean_path
($file));
944 if(unlink($file_path))
946 push(@success,clean_path
($file));
950 push(@failed,clean_path
($file));
956 push(@failed,clean_path
($file));
960 my $tpl = new Template
;
961 $tpl->read_file($config->{'templates'}->{'rmmulti'});
963 if(scalar(@success) > 0)
965 if(scalar(@success) == scalar(@new_files) && scalar(@failed) == 0)
967 return devedit_reload
({command
=> 'show', file
=> $virtual});
971 $tpl->parse_if_block('success',1);
973 foreach my $file_success(@success)
975 $tpl->add_loop_data('SUCCESS',{FILE
=> encode_html
($file_success),
976 FILE_PATH
=> encode_html
(clean_path
($virtual.'/'.$file_success))});
979 $tpl->parse_loop('SUCCESS');
984 $tpl->parse_if_block('success',0);
987 if(scalar(@failed) > 0)
989 $tpl->parse_if_block('failed',1);
991 foreach my $file_failed(@failed)
993 $tpl->add_loop_data('FAILED',{FILE
=> encode_html
($file_failed),
994 FILE_PATH
=> encode_html
(clean_path
($virtual.'/'.$file_failed))});
997 $tpl->parse_loop('FAILED');
1001 $tpl->parse_if_block('failed',0);
1005 $tpl->fillin('DIR',encode_html
($virtual));
1006 $tpl->fillin('SCRIPT',$script);
1008 my $output = header
(-type
=> 'text/html');
1009 $output .= $tpl->get_template;
1015 my $tpl = new Template
;
1016 $tpl->read_file($config->{'templates'}->{'confirm_rmmulti'});
1018 foreach my $file(@new_files)
1020 $tpl->add_loop_data('FILES',{FILE
=> encode_html
($file),
1021 FILE_PATH
=> encode_html
(clean_path
($virtual.'/'.$file))});
1024 $tpl->parse_loop('FILES');
1026 $tpl->fillin('COUNT',scalar(@new_files));
1028 $tpl->fillin('DIR',encode_html
($virtual));
1029 $tpl->fillin('SCRIPT',$script);
1031 my $output = header
(-type
=> 'text/html');
1032 $output .= $tpl->get_template;
1039 return devedit_reload
({command
=> 'show', file
=> $virtual});
1045 # Change the mode and the group of a file or a directory
1047 # Params: 1. Reference to user input hash
1048 # 2. Reference to config hash
1050 # Return: Output of the command (Scalar Reference)
1054 my ($data,$config) = @_;
1055 my $physical = $data->{'physical'};
1056 my $virtual = $data->{'virtual'};
1057 my $dir = upper_path
($virtual);
1059 return error
($config->{'errors'}->{'no_users'},$dir,{FILE
=> encode_html
($virtual)}) unless($users);
1060 return error
($config->{'errors'}->{'chprop_root'},'/') if($virtual eq '/');
1061 return error
($config->{'errors'}->{'not_owner'},$dir,{FILE
=> encode_html
($virtual)}) unless(-o
$physical);
1062 return error
($config->{'errors'}->{'chprop_link'},$dir) if(-l
$physical);
1064 my $cgi = $data->{'cgi'};
1065 my $mode = $cgi->param('mode');
1066 my $group = $cgi->param('group');
1074 return error
($config->{'errors'}->{'invalid_mode'},$dir) unless($mode =~ /^[0-7]{3,}$/);
1075 chmod(oct($mode),$physical);
1080 # Change the group using the `chgrp` system command
1082 return error
($config->{'errors'}->{'invalid_group'},$dir,{GROUP
=> encode_html
($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
1083 system('chgrp',$group,$physical);
1086 return devedit_reload
({command
=> 'show', file
=> $dir});
1092 my @stat = stat($physical);
1093 my $mode = $stat[2];
1096 my $tpl = new Template
;
1097 $tpl->read_file($config->{'templates'}->{'chprop'});
1099 # Insert file properties into the template
1101 $tpl->fillin('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
1102 $tpl->fillin('MODE_STRING',mode_string
($mode));
1103 $tpl->fillin('GID',$gid);
1105 if(my $group = getgrgid($gid))
1107 $tpl->fillin('GROUP',encode_html
($group));
1108 $tpl->parse_if_block('group_detected',1);
1112 $tpl->parse_if_block('group_detected',0);
1115 # Insert other information
1117 $tpl->fillin('FILE',encode_html
($virtual));
1118 $tpl->fillin('FILE_URL',escape
($virtual));
1119 $tpl->fillin('DIR',encode_html
($dir));
1120 $tpl->fillin('DIR_URL',escape
($dir));
1121 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
1122 $tpl->fillin('SCRIPT',$script);
1124 my $output = header
(-type
=> 'text/html');
1125 $output .= $tpl->get_template;
1133 # Display some information about Dev-Editor
1135 # Params: 1. Reference to user input hash
1136 # 2. Reference to config hash
1138 # Return: Output of the command (Scalar Reference)
1142 my ($data,$config) = @_;
1144 my $tpl = new Template
;
1145 $tpl->read_file($config->{'templates'}->{'about'});
1147 $tpl->fillin('SCRIPT',$script);
1149 # Dev-Editor's version number
1151 $tpl->fillin('VERSION',$data->{'version'});
1153 # Some path information
1155 $tpl->fillin('SCRIPT_PHYS',encode_html
($ENV{'SCRIPT_FILENAME'}));
1156 $tpl->fillin('CONFIG_PATH',encode_html
($data->{'configfile'}));
1157 $tpl->fillin('FILE_ROOT', encode_html
($config->{'fileroot'}));
1158 $tpl->fillin('HTTP_ROOT', encode_html
($config->{'httproot'}));
1162 $tpl->fillin('PERL_PROG',encode_html
($^X
));
1163 $tpl->fillin('PERL_VER', sprintf('%vd',$^V
));
1165 # Information about the server
1167 $tpl->fillin('HTTPD',encode_html
($ENV{'SERVER_SOFTWARE'}));
1168 $tpl->fillin('OS', encode_html
($^O
));
1169 $tpl->fillin('TIME', encode_html
(strftime
($config->{'timeformat'},($config->{'use_gmt'}) ?
gmtime : localtime)));
1171 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
1173 # Process information
1175 $tpl->fillin('PID',$$);
1177 # The following information is only available on systems supporting
1182 # Dev-Editor is running on a system which allows users and groups
1183 # So we display the user and the group of our process
1185 my $uid = POSIX
::getuid
;
1186 my $gid = POSIX
::getgid
;
1188 $tpl->parse_if_block('users',1);
1190 # IDs of user and group
1192 $tpl->fillin('UID',$uid);
1193 $tpl->fillin('GID',$gid);
1195 # Names of user and group
1197 if(my $user = getpwuid($uid))
1199 $tpl->fillin('USER',encode_html
($user));
1200 $tpl->parse_if_block('user_detected',1);
1204 $tpl->parse_if_block('user_detected',0);
1207 if(my $group = getgrgid($gid))
1209 $tpl->fillin('GROUP',encode_html
($group));
1210 $tpl->parse_if_block('group_detected',1);
1214 $tpl->parse_if_block('group_detected',0);
1219 $tpl->fillin('UMASK',sprintf('%04o',umask));
1223 $tpl->parse_if_block('users',0);
1226 my $output = header
(-type
=> 'text/html');
1227 $output .= $tpl->get_template;
1232 # it's true, baby ;-)
patrick-canterino.de