]>
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: 2010-10-26
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((not -r
$physical) || (-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});
497 if($data->{'cgi'}->param('edit'))
499 return devedit_reload
({command
=> 'beginedit', file
=> $new_virtual});
503 return devedit_reload
({command
=> 'show', file
=> $dir});
508 my $tpl = new Template
;
509 $tpl->read_file($config->{'templates'}->{'mkfile'});
511 $tpl->fillin('DIR','/');
512 $tpl->fillin('SCRIPT',$script);
514 my $output = header
(-type
=> 'text/html');
515 $output .= $tpl->get_template;
523 # Create a directory 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 $new_physical = $data->{'new_physical'};
534 my $new_virtual = $data->{'new_virtual'};
535 my $dir = upper_path
($new_virtual);
536 $new_virtual = encode_html
($new_virtual);
540 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual}) if(-e
$new_physical);
542 mkdir($new_physical,0777) or return error
($config->{'errors'}->{'mkdir_failed'},$dir,{DIR
=> $new_virtual});
543 return devedit_reload
({command
=> 'show', file
=> $dir});
547 my $tpl = new Template
;
548 $tpl->read_file($config->{'templates'}->{'mkdir'});
550 $tpl->fillin('DIR','/');
551 $tpl->fillin('SCRIPT',$script);
553 my $output = header
(-type
=> 'text/html');
554 $output .= $tpl->get_template;
562 # Process a file upload
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 $physical = $data->{'physical'};
573 my $virtual = $data->{'virtual'};
574 my $cgi = $data->{'cgi'};
576 return error
($config->{'errors'}->{'no_directory'},upper_path
($virtual),{FILE
=> encode_html
($virtual)}) unless(-d
$physical && not -l
$physical);
577 return error
($config->{'errors'}->{'dir_no_create'},$virtual,{DIR
=> encode_html
($virtual)}) unless(-w
$physical);
579 if(my $uploaded_file = $cgi->param('uploaded_file'))
581 if($cgi->param('remote_file'))
583 $uploaded_file = $cgi->param('remote_file');
585 $uploaded_file =~ s!/!!g;
586 $uploaded_file =~ s!\\!!g;
589 # Process file upload
591 my $filename = file_name
($uploaded_file);
592 my $file_phys = $physical.'/'.$filename;
593 my $file_virt = encode_html
($virtual.$filename);
597 return error
($config->{'errors'}->{'link_replace'},$virtual) if(-l
$file_phys);
598 return error
($config->{'errors'}->{'dir_replace'},$virtual) if(-d
$file_phys);
599 return error
($config->{'errors'}->{'exist_no_write'},$virtual,{FILE
=> $file_virt}) unless(-w
$file_phys);
600 return error
($config->{'errors'}->{'file_exists'},$virtual,{FILE
=> $file_virt}) unless($cgi->param('overwrite'));
603 my $ascii = $cgi->param('ascii');
604 my $handle = $cgi->upload('uploaded_file');
606 return error
($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
608 # Read transferred file and write it to disk
610 read($handle, my $data, -s
$handle);
611 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
612 file_save
($file_phys,\
$data,not $ascii) or return error
($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE
=> $file_virt});
614 return devedit_reload
({command
=> 'show', file
=> $virtual});
618 my $tpl = new Template
;
619 $tpl->read_file($config->{'templates'}->{'upload'});
621 $tpl->fillin('DIR',encode_html
($virtual));
622 $tpl->fillin('DIR_URL',escape
($virtual));
623 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
624 $tpl->fillin('SCRIPT',$script);
626 my $output = header
(-type
=> 'text/html');
627 $output .= $tpl->get_template;
635 # Copy a file and return to directory view
637 # Params: 1. Reference to user input hash
638 # 2. Reference to config hash
640 # Return: Output of the command (Scalar Reference)
644 my ($data,$config) = @_;
645 my $physical = $data->{'physical'};
646 my $virtual = $data->{'virtual'};
647 my $dir = upper_path
($virtual);
648 my $new_physical = $data->{'new_physical'};
650 return error
($config->{'errors'}->{'link_copy'},$dir) if(-l
$physical);
651 return error
($config->{'errors'}->{'no_copy'},$dir) unless(-r
$physical);
655 my $new_virtual = multi_string
($data->{'new_virtual'});
656 my $new_dir = upper_path
($new_virtual->{'normal'});
660 return error
($config->{'errors'}->{'no_copy'},$dir) unless(-x
$physical);
661 return error
($config->{'errors'}->{'file_exists'},$dir,{FILE
=> $new_virtual->{'html'}}) if(-e
$new_physical);
662 return error
($config->{'errors'}->{'dir_copy_self'},$dir) if(index($new_virtual->{'normal'},$virtual) == 0);
664 dir_copy
($physical,$new_physical) or return error
($config->{'errors'}->{'copy_failed'},$dir,{FILE
=> encode_html
($virtual), NEW_FILE
=> $new_virtual->{'html'}});
665 return devedit_reload
({command
=> 'show', file
=> $new_dir});
671 return error
($config->{'errors'}->{'link_replace'},$new_dir) if(-l
$new_physical);
672 return error
($config->{'errors'}->{'dir_replace'},$new_dir) if(-d
$new_physical);
673 return error
($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE
=> $new_virtual->{'html'}}) unless(-w
$new_physical);
675 if(not $data->{'cgi'}->param('confirmed'))
677 my $tpl = new Template
;
678 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
680 $tpl->fillin('FILE',encode_html
($virtual));
681 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
682 $tpl->fillin('NEW_FILENAME',file_name
($new_virtual->{'html'}));
683 $tpl->fillin('NEW_DIR',encode_html
($new_dir));
684 $tpl->fillin('DIR',encode_html
($dir));
685 $tpl->fillin('DIR_URL',escape
($dir));
687 $tpl->fillin('COMMAND','copy');
688 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
689 $tpl->fillin('SCRIPT',$script);
691 my $output = header
(-type
=> 'text/html');
692 $output .= $tpl->get_template;
698 copy
($physical,$new_physical) or return error
($config->{'errors'}->{'copy_failed'},$dir,{FILE
=> encode_html
($virtual), NEW_FILE
=> $new_virtual->{'html'}});
699 return devedit_reload
({command
=> 'show', file
=> $new_dir});
706 my $tpl = new Template
;
707 $tpl->read_file($config->{'templates'}->{'copydir'});
709 $tpl->fillin('FILE',encode_html
($virtual));
710 $tpl->fillin('DIR',encode_html
($dir));
711 $tpl->fillin('DIR_URL',escape
($dir));
712 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
713 $tpl->fillin('SCRIPT',$script);
715 my $output = header
(-type
=> 'text/html');
716 $output .= $tpl->get_template;
722 my $tpl = new Template
;
723 $tpl->read_file($config->{'templates'}->{'copyfile'});
725 $tpl->fillin('FILE',encode_html
($virtual));
726 $tpl->fillin('DIR',encode_html
($dir));
727 $tpl->fillin('DIR_URL',escape
($dir));
728 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
729 $tpl->fillin('SCRIPT',$script);
731 my $output = header
(-type
=> 'text/html');
732 $output .= $tpl->get_template;
741 # Rename/move a file and return to directory view
743 # Params: 1. Reference to user input hash
744 # 2. Reference to config hash
746 # Return: Output of the command (Scalar Reference)
750 my ($data,$config) = @_;
751 my $physical = $data->{'physical'};
752 my $virtual = $data->{'virtual'};
753 my $dir = upper_path
($virtual);
754 my $new_physical = $data->{'new_physical'};
756 return error
($config->{'errors'}->{'rename_root'},'/') if($virtual eq '/');
757 return error
($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path
($physical));
761 my $new_virtual = multi_string
($data->{'new_virtual'});
762 my $new_dir = upper_path
($new_virtual->{'normal'});
766 return error
($config->{'errors'}->{'dir_replace'},$new_dir) if(-d
$new_physical && not -l
$new_physical);
767 return error
($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE
=> $new_virtual}) unless(-w
$new_physical);
769 if(not $data->{'cgi'}->param('confirmed'))
771 my $tpl = new Template
;
772 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
774 $tpl->fillin('FILE',encode_html
($virtual));
775 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
776 $tpl->fillin('NEW_FILENAME',file_name
($new_virtual->{'html'}));
777 $tpl->fillin('NEW_DIR',encode_html
($new_dir));
778 $tpl->fillin('DIR',encode_html
($dir));
780 $tpl->fillin('COMMAND','rename');
781 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
782 $tpl->fillin('SCRIPT',$script);
784 my $output = header
(-type
=> 'text/html');
785 $output .= $tpl->get_template;
791 move
($physical,$new_physical) or return error
($config->{'errors'}->{'rename_failed'},$dir,{FILE
=> encode_html
($virtual), NEW_FILE
=> $new_virtual->{'html'}});
792 return devedit_reload
({command
=> 'show', file
=> $new_dir});
796 my $tpl = new Template
;
797 $tpl->read_file($config->{'templates'}->{'renamefile'});
799 $tpl->fillin('FILE',encode_html
($virtual));
800 $tpl->fillin('DIR',encode_html
($dir));
801 $tpl->fillin('DIR_URL',escape
($dir));
802 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
803 $tpl->fillin('SCRIPT',$script);
805 my $output = header
(-type
=> 'text/html');
806 $output .= $tpl->get_template;
814 # Remove a file or a directory and return to directory view
816 # Params: 1. Reference to user input hash
817 # 2. Reference to config hash
819 # Return: Output of the command (Scalar Reference)
823 my ($data,$config) = @_;
824 my $physical = $data->{'physical'};
825 my $virtual = $data->{'virtual'};
826 my $dir = upper_path
($virtual);
828 return error
($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
829 return error
($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path
($physical));
831 if(-d
$physical && not -l
$physical)
835 if($data->{'cgi'}->param('confirmed'))
838 return devedit_reload
({command
=> 'show', file
=> $dir});
842 my $tpl = new Template
;
843 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
845 $tpl->fillin('DIR',encode_html
($virtual));
846 $tpl->fillin('DIR_URL',escape
($virtual));
847 $tpl->fillin('UPPER_DIR',encode_html
($dir));
848 $tpl->fillin('UPPER_DIR_URL',escape
($dir));
849 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
850 $tpl->fillin('SCRIPT',$script);
852 my $output = header
(-type
=> 'text/html');
853 $output .= $tpl->get_template;
862 if($data->{'cgi'}->param('confirmed'))
864 unlink($physical) or return error
($config->{'errors'}->{'delete_failed'},$dir,{FILE
=> $virtual});
865 return devedit_reload
({command
=> 'show', file
=> $dir});
869 my $tpl = new Template
;
870 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
872 $tpl->fillin('FILE',encode_html
($virtual));
873 $tpl->fillin('FILE_URL',escape
($virtual));
874 $tpl->fillin('DIR',encode_html
($dir));
875 $tpl->fillin('DIR_URL',escape
($dir));
876 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
877 $tpl->fillin('SCRIPT',$script);
879 my $output = header
(-type
=> 'text/html');
880 $output .= $tpl->get_template;
887 # exec_remove_multi()
889 # Remove a file or a directory and return to directory view
891 # Params: 1. Reference to user input hash
892 # 2. Reference to config hash
894 # Return: Output of the command (Scalar Reference)
896 sub exec_remove_multi
($$)
898 my ($data,$config) = @_;
899 my $physical = $data->{'physical'};
900 my $virtual = $data->{'virtual'};
901 my $cgi = $data->{'cgi'};
903 my @files = $cgi->param('files');#
908 foreach my $file(@files)
910 # Filter out some "bad" files (e.g. files going up in the
911 # directory hierarchy or files containing slashes (it's too
914 next if($file =~ m!^\.+$!);
915 next if($file =~ m!/!);
916 next if($file =~ m!\\!);
918 push(@new_files,$file);
924 if($cgi->param('confirmed'))
929 foreach my $file(@new_files)
931 my $file_path = clean_path
($physical.'/'.$file);
935 if(-d
$file_path && not -l
$file_path)
939 if(rmtree
($file_path))
941 push(@success,clean_path
($file));
945 push(@failed,clean_path
($file));
952 if(unlink($file_path))
954 push(@success,clean_path
($file));
958 push(@failed,clean_path
($file));
964 push(@failed,clean_path
($file));
968 my $tpl = new Template
;
969 $tpl->read_file($config->{'templates'}->{'rmmulti'});
971 if(scalar(@success) > 0)
973 if(scalar(@success) == scalar(@new_files) && scalar(@failed) == 0)
975 return devedit_reload
({command
=> 'show', file
=> $virtual});
979 $tpl->parse_if_block('success',1);
981 foreach my $file_success(@success)
983 $tpl->add_loop_data('SUCCESS',{FILE
=> encode_html
($file_success),
984 FILE_PATH
=> encode_html
(clean_path
($virtual.'/'.$file_success))});
987 $tpl->parse_loop('SUCCESS');
992 $tpl->parse_if_block('success',0);
995 if(scalar(@failed) > 0)
997 $tpl->parse_if_block('failed',1);
999 foreach my $file_failed(@failed)
1001 $tpl->add_loop_data('FAILED',{FILE
=> encode_html
($file_failed),
1002 FILE_PATH
=> encode_html
(clean_path
($virtual.'/'.$file_failed))});
1005 $tpl->parse_loop('FAILED');
1009 $tpl->parse_if_block('failed',0);
1013 $tpl->fillin('DIR',encode_html
($virtual));
1014 $tpl->fillin('SCRIPT',$script);
1016 my $output = header
(-type
=> 'text/html');
1017 $output .= $tpl->get_template;
1023 my $tpl = new Template
;
1024 $tpl->read_file($config->{'templates'}->{'confirm_rmmulti'});
1026 foreach my $file(@new_files)
1028 $tpl->add_loop_data('FILES',{FILE
=> encode_html
($file),
1029 FILE_PATH
=> encode_html
(clean_path
($virtual.'/'.$file))});
1032 $tpl->parse_loop('FILES');
1034 $tpl->fillin('COUNT',scalar(@new_files));
1036 $tpl->fillin('DIR',encode_html
($virtual));
1037 $tpl->fillin('SCRIPT',$script);
1039 my $output = header
(-type
=> 'text/html');
1040 $output .= $tpl->get_template;
1047 return devedit_reload
({command
=> 'show', file
=> $virtual});
1053 # Change the mode and the group of a file or a directory
1055 # Params: 1. Reference to user input hash
1056 # 2. Reference to config hash
1058 # Return: Output of the command (Scalar Reference)
1062 my ($data,$config) = @_;
1063 my $physical = $data->{'physical'};
1064 my $virtual = $data->{'virtual'};
1065 my $dir = upper_path
($virtual);
1067 return error
($config->{'errors'}->{'no_users'},$dir,{FILE
=> encode_html
($virtual)}) unless($users);
1068 return error
($config->{'errors'}->{'chprop_root'},'/') if($virtual eq '/');
1069 return error
($config->{'errors'}->{'not_owner'},$dir,{FILE
=> encode_html
($virtual)}) unless(-o
$physical);
1070 return error
($config->{'errors'}->{'chprop_link'},$dir) if(-l
$physical);
1072 my $cgi = $data->{'cgi'};
1073 my $mode = $cgi->param('mode');
1074 my $group = $cgi->param('group');
1082 return error
($config->{'errors'}->{'invalid_mode'},$dir) unless($mode =~ /^[0-7]{3,}$/);
1083 chmod(oct($mode),$physical);
1088 # Change the group using the `chgrp` system command
1090 return error
($config->{'errors'}->{'invalid_group'},$dir,{GROUP
=> encode_html
($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
1091 system('chgrp',$group,$physical);
1094 return devedit_reload
({command
=> 'show', file
=> $dir});
1100 my @stat = stat($physical);
1101 my $mode = $stat[2];
1104 my $tpl = new Template
;
1105 $tpl->read_file($config->{'templates'}->{'chprop'});
1107 # Insert file properties into the template
1109 $tpl->fillin('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
1110 $tpl->fillin('MODE_STRING',mode_string
($mode));
1111 $tpl->fillin('GID',$gid);
1113 if(my $group = getgrgid($gid))
1115 $tpl->fillin('GROUP',encode_html
($group));
1116 $tpl->parse_if_block('group_detected',1);
1120 $tpl->parse_if_block('group_detected',0);
1123 # Insert other information
1125 $tpl->fillin('FILE',encode_html
($virtual));
1126 $tpl->fillin('FILE_URL',escape
($virtual));
1127 $tpl->fillin('DIR',encode_html
($dir));
1128 $tpl->fillin('DIR_URL',escape
($dir));
1129 $tpl->fillin('URL',encode_html
(equal_url
($config->{'httproot'},$virtual)));
1130 $tpl->fillin('SCRIPT',$script);
1132 my $output = header
(-type
=> 'text/html');
1133 $output .= $tpl->get_template;
1141 # Display some information about Dev-Editor
1143 # Params: 1. Reference to user input hash
1144 # 2. Reference to config hash
1146 # Return: Output of the command (Scalar Reference)
1150 my ($data,$config) = @_;
1152 my $tpl = new Template
;
1153 $tpl->read_file($config->{'templates'}->{'about'});
1155 $tpl->fillin('SCRIPT',$script);
1157 # Dev-Editor's version number
1159 $tpl->fillin('VERSION',$data->{'version'});
1161 # Some path information
1163 $tpl->fillin('SCRIPT_PHYS',encode_html
($ENV{'SCRIPT_FILENAME'}));
1164 $tpl->fillin('CONFIG_PATH',encode_html
($data->{'configfile'}));
1165 $tpl->fillin('FILE_ROOT', encode_html
($config->{'fileroot'}));
1166 $tpl->fillin('HTTP_ROOT', encode_html
($config->{'httproot'}));
1170 $tpl->fillin('PERL_PROG',encode_html
($^X
));
1171 $tpl->fillin('PERL_VER', sprintf('%vd',$^V
));
1173 # Information about the server
1175 $tpl->fillin('HTTPD',encode_html
($ENV{'SERVER_SOFTWARE'}));
1176 $tpl->fillin('OS', encode_html
($^O
));
1177 $tpl->fillin('TIME', encode_html
(strftime
($config->{'timeformat'},($config->{'use_gmt'}) ?
gmtime : localtime)));
1179 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
1181 # Process information
1183 $tpl->fillin('PID',$$);
1185 # The following information is only available on systems supporting
1190 # Dev-Editor is running on a system which allows users and groups
1191 # So we display the user and the group of our process
1193 my $uid = POSIX
::getuid
;
1194 my $gid = POSIX
::getgid
;
1196 $tpl->parse_if_block('users',1);
1198 # IDs of user and group
1200 $tpl->fillin('UID',$uid);
1201 $tpl->fillin('GID',$gid);
1203 # Names of user and group
1205 if(my $user = getpwuid($uid))
1207 $tpl->fillin('USER',encode_html
($user));
1208 $tpl->parse_if_block('user_detected',1);
1212 $tpl->parse_if_block('user_detected',0);
1215 if(my $group = getgrgid($gid))
1217 $tpl->fillin('GROUP',encode_html
($group));
1218 $tpl->parse_if_block('group_detected',1);
1222 $tpl->parse_if_block('group_detected',0);
1227 $tpl->fillin('UMASK',sprintf('%04o',umask));
1231 $tpl->parse_if_block('users',0);
1234 my $output = header
(-type
=> 'text/html');
1235 $output .= $tpl->get_template;
1240 # it's true, baby ;-)
patrick-canterino.de