]> git.p6c8.net - devedit.git/blob - modules/Command.pm
Allow to execute a HTTP download of a file
[devedit.git] / modules / Command.pm
1 package Command;
2
3 #
4 # Dev-Editor - Module Command
5 #
6 # Execute Dev-Editor's commands
7 #
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2009-12-29
10 #
11 # Copyright (C) 1999-2000 Roland Bluethgen, Frank Schoenmann
12 # Copyright (C) 2003-2009 Patrick Canterino
13 # All Rights Reserved.
14 #
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).
18 #
19
20 use strict;
21
22 use vars qw(@EXPORT);
23
24 use Fcntl;
25 use File::Access;
26 use File::Copy;
27 use File::Path;
28
29 use Digest::MD5 qw(md5_hex);
30 use POSIX qw(strftime);
31 use Tool;
32
33 use CGI qw(header
34 escape);
35
36 use Output;
37 use Template;
38
39 my $script = encode_html($ENV{'SCRIPT_NAME'});
40 my $users = eval('getpwuid(0)') && eval('getgrgid(0)');
41
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
55 );
56
57 ### Export ###
58
59 use base qw(Exporter);
60
61 @EXPORT = qw(exec_command);
62
63 # exec_command()
64 #
65 # Execute the specified command
66 #
67 # Params: 1. Command to execute
68 # 2. Reference to user input hash
69 # 3. Reference to config hash
70 #
71 # Return: Output of the command (Scalar Reference)
72
73 sub exec_command($$$)
74 {
75 my ($command,$data,$config) = @_;
76
77 foreach(keys(%dispatch))
78 {
79 if(lc($_) eq lc($command))
80 {
81 my $output = &{$dispatch{$_}}($data,$config);
82 return $output;
83 }
84 }
85
86 return error($config->{'errors'}->{'command_unknown'},'/',{COMMAND => encode_html($command)});
87 }
88
89 # exec_show()
90 #
91 # View a directory or a file
92 #
93 # Params: 1. Reference to user input hash
94 # 2. Reference to config hash
95 #
96 # Return: Output of the command (Scalar Reference)
97
98 sub exec_show($$)
99 {
100 my ($data,$config) = @_;
101 my $physical = $data->{'physical'};
102 my $virtual = $data->{'virtual'};
103 my $upper_path = multi_string(upper_path($virtual));
104
105 my $tpl = new Template;
106
107 if(-d $physical && not -l $physical)
108 {
109 # Create directory listing
110
111 return error($config->{'errors'}->{'no_dir_access'},$upper_path->{'normal'}) unless(-r $physical && -x $physical);
112
113 my $direntries = dir_read($physical);
114 return error($config->{'errors'}->{'dir_read_failed'},$upper_path->{'normal'},{DIR => encode_html($virtual)}) unless($direntries);
115
116 my $files = $direntries->{'files'};
117 my $dirs = $direntries->{'dirs'};
118
119 my $dirlist = '';
120
121 my $count = 0;
122
123 my $filter1 = $data->{'cgi'}->param('filter') || '*'; # The real wildcard
124 my $filter2 = ($filter1 && $filter1 ne '*') ? $filter1 : ''; # Wildcard for output
125
126 # Create the link to the upper directory
127 # (only if the current directory is not the root directory)
128
129 unless($virtual eq '/')
130 {
131 $count++;
132
133 my @stat = stat($physical.'/..');
134
135 my $udtpl = new Template;
136 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
137
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]))));
141
142 $dirlist .= $udtpl->get_template;
143 }
144
145 # Directories
146
147 foreach my $dir(@$dirs)
148 {
149 next if($config->{'hide_dot_files'} && substr($dir,0,1) eq '.');
150 next unless(dos_wildcard_match($filter1,$dir));
151
152 $count++;
153
154 my $phys_path = $physical.'/'.$dir;
155 my $virt_path = multi_string($virtual.$dir.'/');
156
157 my @stat = stat($phys_path);
158
159 my $dtpl = new Template;
160 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
161
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'}));
167
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);
172
173 $dirlist .= $dtpl->get_template;
174 }
175
176 # Files
177
178 foreach my $file(@$files)
179 {
180 next if($config->{'hide_dot_files'} && substr($file,0,1) eq '.');
181 next unless(dos_wildcard_match($filter1,$file));
182
183 $count++;
184
185 my $phys_path = $physical.'/'.$file;
186 my $virt_path = multi_string($virtual.$file);
187
188 my @stat = lstat($phys_path);
189 my $too_large = $config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'};
190
191 my $ftpl = new Template;
192 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
193
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'}));
201
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);
206
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);
210
211 $ftpl->parse_if_block('too_large',$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
212
213 $ftpl->parse_if_block('users',$users && -o $phys_path);
214
215 $ftpl->parse_if_block('even',($count % 2) == 0);
216
217 $dirlist .= $ftpl->get_template;
218 }
219
220 $tpl->read_file($config->{'templates'}->{'dirlist'});
221
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)));
227
228 $tpl->fillin('FILTER',encode_html($filter2));
229 $tpl->fillin('FILTER_URL',escape($filter2));
230
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'});
235 }
236 elsif(-l $physical)
237 {
238 # Show the target of a symbolic link
239
240 my $link_target = readlink($physical);
241
242 $tpl->read_file($config->{'templates'}->{'viewlink'});
243
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);
249
250 $tpl->fillin('LINK_TARGET',encode_html($link_target));
251 }
252 else
253 {
254 # View a file
255
256 return error($config->{'errors'}->{'no_view'},$upper_path->{'normal'}) unless(-r $physical);
257
258 # Check on binary files
259 # We have to do it in this way or empty files will be recognized
260 # as binary files
261
262 return error($config->{'errors'}->{'binary_file'},$upper_path->{'normal'}) unless(-T $physical);
263
264 # Is the file too large?
265
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'});
267
268 # View the file
269
270 my $content = file_read($physical);
271 $$content =~ s/\015\012|\012|\015/\n/g;
272
273 $tpl->read_file($config->{'templates'}->{'viewfile'});
274
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);
281
282 $tpl->parse_if_block('editable',-w $physical);
283
284 $tpl->fillin('CONTENT',encode_html($$content));
285 }
286
287 my $output = header(-type => 'text/html');
288 $output .= $tpl->get_template;
289
290 return \$output;
291 }
292
293 # exec_beginedit
294 #
295 # Lock a file and display a form to edit it
296 #
297 # Params: 1. Reference to user input hash
298 # 2. Reference to config hash
299 #
300 # Return: Output of the command (Scalar Reference)
301
302 sub exec_beginedit($$)
303 {
304 my ($data,$config) = @_;
305 my $physical = $data->{'physical'};
306 my $virtual = $data->{'virtual'};
307 my $dir = upper_path($virtual);
308
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);
312
313 # Check on binary files
314
315 return error($config->{'errors'}->{'binary_file'},$dir) unless(-T $physical);
316
317 # Is the file too large?
318
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'});
320
321 # Show the editing form
322
323 my $content = file_read($physical);
324 my $md5sum = md5_hex($$content);
325 $$content =~ s/\015\012|\012|\015/\n/g;
326
327 my $tpl = new Template;
328 $tpl->read_file($config->{'templates'}->{'editfile'});
329
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));
338
339 $tpl->parse_if_block('error',0);
340
341 my $output = header(-type => 'text/html');
342 $output .= $tpl->get_template;
343
344 return \$output;
345 }
346
347 # exec_endedit()
348 #
349 # Save a file, unlock it and return to directory view
350 #
351 # Params: 1. Reference to user input hash
352 # 2. Reference to config hash
353 #
354 # Return: Output of the command (Scalar Reference)
355
356 sub exec_endedit($$)
357 {
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');
365 my $output;
366
367 if(defined $content && $md5sum)
368 {
369 # Normalize newlines
370
371 $content =~ s/\015\012|\012|\015/\n/g;
372
373 if($cgi->param('saveas') && $data->{'new_physical'} ne '' && $data->{'new_virtual'} ne '')
374 {
375 # Create the new filename
376
377 $physical = $data->{'new_physical'};
378 $virtual = $data->{'new_virtual'};
379 }
380
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);
385
386 # For technical reasons, we can't use file_save() for
387 # saving the file...
388
389 local *FILE;
390
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)}) };
393
394 my $md5 = new Digest::MD5;
395 $md5->addfile(*FILE);
396
397 my $md5file = $md5->hexdigest;
398 my $md5data = md5_hex($content);
399
400 if($md5file ne $md5sum && $md5data ne $md5file && not $cgi->param('saveas'))
401 {
402 # The file changed meanwhile
403
404 my $tpl = new Template;
405 $tpl->read_file($config->{'templates'}->{'editfile'});
406
407 $tpl->fillin('ERROR',$config->{'errors'}->{'edit_file_changed'});
408
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));
417
418 $tpl->parse_if_block('error',1);
419
420 my $data = header(-type => 'text/html');
421 $data .= $tpl->get_template;
422
423 $output = \$data;
424 }
425 else
426 {
427 if($md5data ne $md5file)
428 {
429 seek(FILE,0,0);
430 truncate(FILE,0);
431
432 print FILE $content;
433 }
434
435 $output = ($cgi->param('continue'))
436 ? devedit_reload({command => 'beginedit', file => $virtual})
437 : devedit_reload({command => 'show', file => $dir});
438 }
439
440 close(FILE);
441
442 return $output;
443 }
444
445 return devedit_reload({command => 'beginedit', file => $virtual});
446 }
447
448 # exec_download()
449 #
450 # Execute a HTTP download of a file
451 #
452 # Params: 1. Reference to user input hash
453 # 2. Reference to config hash
454 #
455 # Return: Output of the command (Scalar Reference)
456
457 sub exec_download($$)
458 {
459 my ($data,$config) = @_;
460 my $physical = $data->{'physical'};
461 my $virtual = $data->{'virtual'};
462 my $dir = upper_path($virtual);
463
464 return return error($config->{'errors'}->{'no_download'},$dir,{FILE => $virtual}) if(-d $physical || -l $physical);
465
466 my $filename = file_name($virtual);
467
468 my $output = header(-type => 'application/octet-stream', -attachment => $filename);
469 $output .= ${ file_read($physical,1) };
470
471 return \$output;
472 }
473
474 # exec_mkfile()
475 #
476 # Create a file and return to directory view
477 #
478 # Params: 1. Reference to user input hash
479 # 2. Reference to config hash
480 #
481 # Return: Output of the command (Scalar Reference)
482
483 sub exec_mkfile($$)
484 {
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);
490
491 if($new_physical)
492 {
493 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
494
495 file_create($new_physical) or return error($config->{'errors'}->{'mkfile_failed'},$dir,{FILE => $new_virtual});
496 return devedit_reload({command => 'show', file => $dir});
497 }
498 else
499 {
500 my $tpl = new Template;
501 $tpl->read_file($config->{'templates'}->{'mkfile'});
502
503 $tpl->fillin('DIR','/');
504 $tpl->fillin('SCRIPT',$script);
505
506 my $output = header(-type => 'text/html');
507 $output .= $tpl->get_template;
508
509 return \$output;
510 }
511 }
512
513 # exec_mkdir()
514 #
515 # Create a directory and return to directory view
516 #
517 # Params: 1. Reference to user input hash
518 # 2. Reference to config hash
519 #
520 # Return: Output of the command (Scalar Reference)
521
522 sub exec_mkdir($$)
523 {
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);
529
530 if($new_physical)
531 {
532 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
533
534 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
535 return devedit_reload({command => 'show', file => $dir});
536 }
537 else
538 {
539 my $tpl = new Template;
540 $tpl->read_file($config->{'templates'}->{'mkdir'});
541
542 $tpl->fillin('DIR','/');
543 $tpl->fillin('SCRIPT',$script);
544
545 my $output = header(-type => 'text/html');
546 $output .= $tpl->get_template;
547
548 return \$output;
549 }
550 }
551
552 # exec_upload()
553 #
554 # Process a file upload
555 #
556 # Params: 1. Reference to user input hash
557 # 2. Reference to config hash
558 #
559 # Return: Output of the command (Scalar Reference)
560
561 sub exec_upload($$)
562 {
563 my ($data,$config) = @_;
564 my $physical = $data->{'physical'};
565 my $virtual = $data->{'virtual'};
566 my $cgi = $data->{'cgi'};
567
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);
570
571 if(my $uploaded_file = $cgi->param('uploaded_file'))
572 {
573 if($cgi->param('remote_file'))
574 {
575 $uploaded_file = $cgi->param('remote_file');
576
577 $uploaded_file =~ s!/!!g;
578 $uploaded_file =~ s!\\!!g;
579 }
580
581 # Process file upload
582
583 my $filename = file_name($uploaded_file);
584 my $file_phys = $physical.'/'.$filename;
585 my $file_virt = encode_html($virtual.$filename);
586
587 if(-e $file_phys)
588 {
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'));
593 }
594
595 my $ascii = $cgi->param('ascii');
596 my $handle = $cgi->upload('uploaded_file');
597
598 return error($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
599
600 # Read transferred file and write it to disk
601
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});
605
606 return devedit_reload({command => 'show', file => $virtual});
607 }
608 else
609 {
610 my $tpl = new Template;
611 $tpl->read_file($config->{'templates'}->{'upload'});
612
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);
617
618 my $output = header(-type => 'text/html');
619 $output .= $tpl->get_template;
620
621 return \$output;
622 }
623 }
624
625 # exec_copy()
626 #
627 # Copy a file and return to directory view
628 #
629 # Params: 1. Reference to user input hash
630 # 2. Reference to config hash
631 #
632 # Return: Output of the command (Scalar Reference)
633
634 sub exec_copy($$)
635 {
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'};
641
642 return error($config->{'errors'}->{'link_copy'},$dir) if(-l $physical);
643 return error($config->{'errors'}->{'no_copy'},$dir) unless(-r $physical);
644
645 if($new_physical)
646 {
647 my $new_virtual = multi_string($data->{'new_virtual'});
648 my $new_dir = upper_path($new_virtual->{'normal'});
649
650 if(-d $physical)
651 {
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);
655
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});
658 }
659 else
660 {
661 if(-e $new_physical)
662 {
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);
666
667 if(not $data->{'cgi'}->param('confirmed'))
668 {
669 my $tpl = new Template;
670 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
671
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));
678
679 $tpl->fillin('COMMAND','copy');
680 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
681 $tpl->fillin('SCRIPT',$script);
682
683 my $output = header(-type => 'text/html');
684 $output .= $tpl->get_template;
685
686 return \$output;
687 }
688 }
689
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});
692 }
693 }
694 else
695 {
696 if(-d $physical)
697 {
698 my $tpl = new Template;
699 $tpl->read_file($config->{'templates'}->{'copydir'});
700
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);
706
707 my $output = header(-type => 'text/html');
708 $output .= $tpl->get_template;
709
710 return \$output;
711 }
712 else
713 {
714 my $tpl = new Template;
715 $tpl->read_file($config->{'templates'}->{'copyfile'});
716
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);
722
723 my $output = header(-type => 'text/html');
724 $output .= $tpl->get_template;
725
726 return \$output;
727 }
728 }
729 }
730
731 # exec_rename()
732 #
733 # Rename/move a file and return to directory view
734 #
735 # Params: 1. Reference to user input hash
736 # 2. Reference to config hash
737 #
738 # Return: Output of the command (Scalar Reference)
739
740 sub exec_rename($$)
741 {
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'};
747
748 return error($config->{'errors'}->{'rename_root'},'/') if($virtual eq '/');
749 return error($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path($physical));
750
751 if($new_physical)
752 {
753 my $new_virtual = multi_string($data->{'new_virtual'});
754 my $new_dir = upper_path($new_virtual->{'normal'});
755
756 if(-e $new_physical)
757 {
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);
760
761 if(not $data->{'cgi'}->param('confirmed'))
762 {
763 my $tpl = new Template;
764 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
765
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));
771
772 $tpl->fillin('COMMAND','rename');
773 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
774 $tpl->fillin('SCRIPT',$script);
775
776 my $output = header(-type => 'text/html');
777 $output .= $tpl->get_template;
778
779 return \$output;
780 }
781 }
782
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});
785 }
786 else
787 {
788 my $tpl = new Template;
789 $tpl->read_file($config->{'templates'}->{'renamefile'});
790
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);
796
797 my $output = header(-type => 'text/html');
798 $output .= $tpl->get_template;
799
800 return \$output;
801 }
802 }
803
804 # exec_remove()
805 #
806 # Remove a file or a directory and return to directory view
807 #
808 # Params: 1. Reference to user input hash
809 # 2. Reference to config hash
810 #
811 # Return: Output of the command (Scalar Reference)
812
813 sub exec_remove($$)
814 {
815 my ($data,$config) = @_;
816 my $physical = $data->{'physical'};
817 my $virtual = $data->{'virtual'};
818 my $dir = upper_path($virtual);
819
820 return error($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
821 return error($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path($physical));
822
823 if(-d $physical && not -l $physical)
824 {
825 # Remove a directory
826
827 if($data->{'cgi'}->param('confirmed'))
828 {
829 rmtree($physical);
830 return devedit_reload({command => 'show', file => $dir});
831 }
832 else
833 {
834 my $tpl = new Template;
835 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
836
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);
843
844 my $output = header(-type => 'text/html');
845 $output .= $tpl->get_template;
846
847 return \$output;
848 }
849 }
850 else
851 {
852 # Remove a file
853
854 if($data->{'cgi'}->param('confirmed'))
855 {
856 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},$dir,{FILE => $virtual});
857 return devedit_reload({command => 'show', file => $dir});
858 }
859 else
860 {
861 my $tpl = new Template;
862 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
863
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);
870
871 my $output = header(-type => 'text/html');
872 $output .= $tpl->get_template;
873
874 return \$output;
875 }
876 }
877 }
878
879 # exec_remove_multi()
880 #
881 # Remove a file or a directory and return to directory view
882 #
883 # Params: 1. Reference to user input hash
884 # 2. Reference to config hash
885 #
886 # Return: Output of the command (Scalar Reference)
887
888 sub exec_remove_multi($$)
889 {
890 my ($data,$config) = @_;
891 my $physical = $data->{'physical'};
892 my $virtual = $data->{'virtual'};
893 my $cgi = $data->{'cgi'};
894
895 my @files = $cgi->param('files');#
896 my @new_files;
897
898 if(@files)
899 {
900 foreach my $file(@files)
901 {
902 # Filter out some "bad" files (e.g. files going up in the
903 # directory hierarchy or files containing slashes (it's too
904 # dangerous...)
905
906 next if($file =~ m!^\.+$!);
907 next if($file =~ m!/!);
908 next if($file =~ m!\\!);
909
910 push(@new_files,$file);
911 }
912 }
913
914 if(@new_files)
915 {
916 if($cgi->param('confirmed'))
917 {
918 my @success;
919 my @failed;
920
921 foreach my $file(@new_files)
922 {
923 my $file_path = clean_path($physical.'/'.$file);
924
925 if(-e $file_path)
926 {
927 if(-d $file_path && not -l $file_path)
928 {
929 # Remove a directory
930
931 if(rmtree($file_path))
932 {
933 push(@success,clean_path($file));
934 }
935 else
936 {
937 push(@failed,clean_path($file));
938 }
939 }
940 else
941 {
942 # Remove a file
943
944 if(unlink($file_path))
945 {
946 push(@success,clean_path($file));
947 }
948 else
949 {
950 push(@failed,clean_path($file));
951 }
952 }
953 }
954 else
955 {
956 push(@failed,clean_path($file));
957 }
958 }
959
960 my $tpl = new Template;
961 $tpl->read_file($config->{'templates'}->{'rmmulti'});
962
963 if(scalar(@success) > 0)
964 {
965 if(scalar(@success) == scalar(@new_files) && scalar(@failed) == 0)
966 {
967 return devedit_reload({command => 'show', file => $virtual});
968 }
969 else
970 {
971 $tpl->parse_if_block('success',1);
972
973 foreach my $file_success(@success)
974 {
975 $tpl->add_loop_data('SUCCESS',{FILE => encode_html($file_success),
976 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_success))});
977 }
978
979 $tpl->parse_loop('SUCCESS');
980 }
981 }
982 else
983 {
984 $tpl->parse_if_block('success',0);
985 }
986
987 if(scalar(@failed) > 0)
988 {
989 $tpl->parse_if_block('failed',1);
990
991 foreach my $file_failed(@failed)
992 {
993 $tpl->add_loop_data('FAILED',{FILE => encode_html($file_failed),
994 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_failed))});
995 }
996
997 $tpl->parse_loop('FAILED');
998 }
999 else
1000 {
1001 $tpl->parse_if_block('failed',0);
1002 }
1003
1004
1005 $tpl->fillin('DIR',encode_html($virtual));
1006 $tpl->fillin('SCRIPT',$script);
1007
1008 my $output = header(-type => 'text/html');
1009 $output .= $tpl->get_template;
1010
1011 return \$output;
1012 }
1013 else
1014 {
1015 my $tpl = new Template;
1016 $tpl->read_file($config->{'templates'}->{'confirm_rmmulti'});
1017
1018 foreach my $file(@new_files)
1019 {
1020 $tpl->add_loop_data('FILES',{FILE => encode_html($file),
1021 FILE_PATH => encode_html(clean_path($virtual.'/'.$file))});
1022 }
1023
1024 $tpl->parse_loop('FILES');
1025
1026 $tpl->fillin('COUNT',scalar(@new_files));
1027
1028 $tpl->fillin('DIR',encode_html($virtual));
1029 $tpl->fillin('SCRIPT',$script);
1030
1031 my $output = header(-type => 'text/html');
1032 $output .= $tpl->get_template;
1033
1034 return \$output;
1035 }
1036 }
1037 else
1038 {
1039 return devedit_reload({command => 'show', file => $virtual});
1040 }
1041 }
1042
1043 # exec_chprop()
1044 #
1045 # Change the mode and the group of a file or a directory
1046 #
1047 # Params: 1. Reference to user input hash
1048 # 2. Reference to config hash
1049 #
1050 # Return: Output of the command (Scalar Reference)
1051
1052 sub exec_chprop($$)
1053 {
1054 my ($data,$config) = @_;
1055 my $physical = $data->{'physical'};
1056 my $virtual = $data->{'virtual'};
1057 my $dir = upper_path($virtual);
1058
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);
1063
1064 my $cgi = $data->{'cgi'};
1065 my $mode = $cgi->param('mode');
1066 my $group = $cgi->param('group');
1067
1068 if($mode || $group)
1069 {
1070 if($mode)
1071 {
1072 # Change the mode
1073
1074 return error($config->{'errors'}->{'invalid_mode'},$dir) unless($mode =~ /^[0-7]{3,}$/);
1075 chmod(oct($mode),$physical);
1076 }
1077
1078 if($group)
1079 {
1080 # Change the group using the `chgrp` system command
1081
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);
1084 }
1085
1086 return devedit_reload({command => 'show', file => $dir});
1087 }
1088 else
1089 {
1090 # Display the form
1091
1092 my @stat = stat($physical);
1093 my $mode = $stat[2];
1094 my $gid = $stat[5];
1095
1096 my $tpl = new Template;
1097 $tpl->read_file($config->{'templates'}->{'chprop'});
1098
1099 # Insert file properties into the template
1100
1101 $tpl->fillin('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
1102 $tpl->fillin('MODE_STRING',mode_string($mode));
1103 $tpl->fillin('GID',$gid);
1104
1105 if(my $group = getgrgid($gid))
1106 {
1107 $tpl->fillin('GROUP',encode_html($group));
1108 $tpl->parse_if_block('group_detected',1);
1109 }
1110 else
1111 {
1112 $tpl->parse_if_block('group_detected',0);
1113 }
1114
1115 # Insert other information
1116
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);
1123
1124 my $output = header(-type => 'text/html');
1125 $output .= $tpl->get_template;
1126
1127 return \$output;
1128 }
1129 }
1130
1131 # exec_about()
1132 #
1133 # Display some information about Dev-Editor
1134 #
1135 # Params: 1. Reference to user input hash
1136 # 2. Reference to config hash
1137 #
1138 # Return: Output of the command (Scalar Reference)
1139
1140 sub exec_about($$)
1141 {
1142 my ($data,$config) = @_;
1143
1144 my $tpl = new Template;
1145 $tpl->read_file($config->{'templates'}->{'about'});
1146
1147 $tpl->fillin('SCRIPT',$script);
1148
1149 # Dev-Editor's version number
1150
1151 $tpl->fillin('VERSION',$data->{'version'});
1152
1153 # Some path information
1154
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'}));
1159
1160 # Perl
1161
1162 $tpl->fillin('PERL_PROG',encode_html($^X));
1163 $tpl->fillin('PERL_VER', sprintf('%vd',$^V));
1164
1165 # Information about the server
1166
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)));
1170
1171 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
1172
1173 # Process information
1174
1175 $tpl->fillin('PID',$$);
1176
1177 # The following information is only available on systems supporting
1178 # users and groups
1179
1180 if($users)
1181 {
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
1184
1185 my $uid = POSIX::getuid;
1186 my $gid = POSIX::getgid;
1187
1188 $tpl->parse_if_block('users',1);
1189
1190 # IDs of user and group
1191
1192 $tpl->fillin('UID',$uid);
1193 $tpl->fillin('GID',$gid);
1194
1195 # Names of user and group
1196
1197 if(my $user = getpwuid($uid))
1198 {
1199 $tpl->fillin('USER',encode_html($user));
1200 $tpl->parse_if_block('user_detected',1);
1201 }
1202 else
1203 {
1204 $tpl->parse_if_block('user_detected',0);
1205 }
1206
1207 if(my $group = getgrgid($gid))
1208 {
1209 $tpl->fillin('GROUP',encode_html($group));
1210 $tpl->parse_if_block('group_detected',1);
1211 }
1212 else
1213 {
1214 $tpl->parse_if_block('group_detected',0);
1215 }
1216
1217 # Process umask
1218
1219 $tpl->fillin('UMASK',sprintf('%04o',umask));
1220 }
1221 else
1222 {
1223 $tpl->parse_if_block('users',0);
1224 }
1225
1226 my $output = header(-type => 'text/html');
1227 $output .= $tpl->get_template;
1228
1229 return \$output;
1230 }
1231
1232 # it's true, baby ;-)
1233
1234 1;
1235
1236 #
1237 ### End ###

patrick-canterino.de