]> git.p6c8.net - devedit.git/blob - modules/Command.pm
Rewrote some sentences in the README 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: 2011-02-11
10 #
11 # Copyright (C) 1999-2000 Roland Bluethgen, Frank Schoenmann
12 # Copyright (C) 2003-2011 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 2.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 'unpack' => \&exec_unpack,
50 'copy' => \&exec_copy,
51 'rename' => \&exec_rename,
52 'remove' => \&exec_remove,
53 'remove_multi' => \&exec_remove_multi,
54 'chprop' => \&exec_chprop,
55 'about' => \&exec_about
56 );
57
58 ### Export ###
59
60 use base qw(Exporter);
61
62 @EXPORT = qw(exec_command);
63
64 # exec_command()
65 #
66 # Execute the specified command
67 #
68 # Params: 1. Command to execute
69 # 2. Reference to user input hash
70 # 3. Reference to config hash
71 #
72 # Return: Output of the command (Scalar Reference)
73
74 sub exec_command($$$)
75 {
76 my ($command,$data,$config) = @_;
77
78 foreach(keys(%dispatch))
79 {
80 if(lc($_) eq lc($command))
81 {
82 my $output = &{$dispatch{$_}}($data,$config);
83 return $output;
84 }
85 }
86
87 return error($config->{'errors'}->{'command_unknown'},'/',{COMMAND => encode_html($command)});
88 }
89
90 # exec_show()
91 #
92 # View a directory or a file
93 #
94 # Params: 1. Reference to user input hash
95 # 2. Reference to config hash
96 #
97 # Return: Output of the command (Scalar Reference)
98
99 sub exec_show($$)
100 {
101 my ($data,$config) = @_;
102 my $physical = $data->{'physical'};
103 my $virtual = $data->{'virtual'};
104 my $upper_path = multi_string(upper_path($virtual));
105
106 my $tpl = new Template;
107
108 if(-d $physical && not -l $physical)
109 {
110 # Create directory listing
111
112 return error($config->{'errors'}->{'no_dir_access'},$upper_path->{'normal'}) unless(-r $physical && -x $physical);
113
114 my $direntries = dir_read($physical);
115 return error($config->{'errors'}->{'dir_read_failed'},$upper_path->{'normal'},{DIR => encode_html($virtual)}) unless($direntries);
116
117 my $files = $direntries->{'files'};
118 my $dirs = $direntries->{'dirs'};
119
120 my $dirlist = '';
121
122 my $count = 0;
123
124 my $filter1 = $data->{'cgi'}->param('filter') || '*'; # The real wildcard
125 my $filter2 = ($filter1 && $filter1 ne '*') ? $filter1 : ''; # Wildcard for output
126
127 # Create the link to the upper directory
128 # (only if the current directory is not the root directory)
129
130 unless($virtual eq '/')
131 {
132 $count++;
133
134 my @stat = stat($physical.'/..');
135
136 my $udtpl = new Template;
137 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
138
139 $udtpl->fillin('UPPER_DIR',$upper_path->{'html'});
140 $udtpl->fillin('UPPER_DIR_URL',$upper_path->{'url'});
141 $udtpl->fillin('DATE',encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
142
143 $dirlist .= $udtpl->get_template;
144 }
145
146 # Directories
147
148 foreach my $dir(@$dirs)
149 {
150 next if($config->{'hide_dot_files'} && substr($dir,0,1) eq '.');
151 next unless(dos_wildcard_match($filter1,$dir));
152
153 $count++;
154
155 my $phys_path = $physical.'/'.$dir;
156 my $virt_path = multi_string($virtual.$dir.'/');
157
158 my @stat = stat($phys_path);
159
160 my $dtpl = new Template;
161 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
162
163 $dtpl->fillin('DIR',$virt_path->{'html'});
164 $dtpl->fillin('DIR_URL',$virt_path->{'url'});
165 $dtpl->fillin('DIR_NAME',encode_html($dir));
166 $dtpl->fillin('DATE',encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
167 $dtpl->fillin('URL',equal_url(encode_html($config->{'httproot'}),$virt_path->{'html'}));
168
169 $dtpl->parse_if_block('forbidden',is_forbidden_file($config->{'forbidden'},$virt_path->{'normal'}));
170 $dtpl->parse_if_block('readable',-r $phys_path && -x $phys_path);
171 $dtpl->parse_if_block('users',$users && -o $phys_path);
172 $dtpl->parse_if_block('even',($count % 2) == 0);
173
174 $dirlist .= $dtpl->get_template;
175 }
176
177 # Files
178
179 foreach my $file(@$files)
180 {
181 next if($config->{'hide_dot_files'} && substr($file,0,1) eq '.');
182 next unless(dos_wildcard_match($filter1,$file));
183
184 $count++;
185
186 my $phys_path = $physical.'/'.$file;
187 my $virt_path = multi_string($virtual.$file);
188
189 my @stat = lstat($phys_path);
190 my $too_large = $config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'};
191
192 my $ftpl = new Template;
193 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
194
195 $ftpl->fillin('FILE',$virt_path->{'html'});
196 $ftpl->fillin('FILE_URL',$virt_path->{'url'});
197 $ftpl->fillin('FILE_NAME',encode_html($file));
198 $ftpl->fillin('FILE_URL',$virt_path->{'url'});
199 $ftpl->fillin('SIZE',$stat[7]);
200 $ftpl->fillin('DATE',encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
201 $ftpl->fillin('URL',equal_url(encode_html($config->{'httproot'}),$virt_path->{'html'}));
202
203 $ftpl->parse_if_block('link',-l $phys_path);
204 $ftpl->parse_if_block('readable',-r $phys_path);
205 $ftpl->parse_if_block('writeable',-w $phys_path);
206 $ftpl->parse_if_block('binary',-B $phys_path);
207
208 $ftpl->parse_if_block('forbidden',is_forbidden_file($config->{'forbidden'},$virt_path->{'normal'}));
209 $ftpl->parse_if_block('viewable',(-r $phys_path && -T $phys_path && not $too_large) || -l $phys_path);
210 $ftpl->parse_if_block('editable',(-r $phys_path && -w $phys_path && -T $phys_path && not $too_large) && not -l $phys_path);
211
212 $ftpl->parse_if_block('too_large',$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
213
214 $ftpl->parse_if_block('users',$users && -o $phys_path);
215
216 $ftpl->parse_if_block('archive',$File::Access::has_archive_extract && is_archive($file));
217
218 $ftpl->parse_if_block('even',($count % 2) == 0);
219
220 $dirlist .= $ftpl->get_template;
221 }
222
223 $tpl->read_file($config->{'templates'}->{'dirlist'});
224
225 $tpl->fillin('DIRLIST',$dirlist);
226 $tpl->fillin('DIR',encode_html($virtual));
227 $tpl->fillin('DIR_URL',escape($virtual));
228 $tpl->fillin('SCRIPT',$script);
229 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
230
231 $tpl->fillin('FILTER',encode_html($filter2));
232 $tpl->fillin('FILTER_URL',escape($filter2));
233
234 $tpl->parse_if_block('empty',$dirlist eq '');
235 $tpl->parse_if_block('dir_writeable',-w $physical);
236 $tpl->parse_if_block('filter',$filter2);
237 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
238 }
239 elsif(-l $physical)
240 {
241 # Show the target of a symbolic link
242
243 my $link_target = readlink($physical);
244
245 $tpl->read_file($config->{'templates'}->{'viewlink'});
246
247 $tpl->fillin('FILE',encode_html($virtual));
248 $tpl->fillin('DIR',$upper_path->{'html'});
249 $tpl->fillin('DIR_URL',$upper_path->{'url'});
250 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
251 $tpl->fillin('SCRIPT',$script);
252
253 $tpl->fillin('LINK_TARGET',encode_html($link_target));
254 }
255 else
256 {
257 # View a file
258
259 return error($config->{'errors'}->{'no_view'},$upper_path->{'normal'}) unless(-r $physical);
260
261 # Check on binary files
262 # We have to do it in this way or empty files will be recognized
263 # as binary files
264
265 return error($config->{'errors'}->{'binary_file'},$upper_path->{'normal'}) unless(-T $physical);
266
267 # Is the file too large?
268
269 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
271 # View the file
272
273 my $content = file_read($physical);
274 $$content =~ s/\015\012|\012|\015/\n/g;
275
276 $tpl->read_file($config->{'templates'}->{'viewfile'});
277
278 $tpl->fillin('FILE',encode_html($virtual));
279 $tpl->fillin('FILE_URL',escape($virtual));
280 $tpl->fillin('DIR',$upper_path->{'html'});
281 $tpl->fillin('DIR_URL',$upper_path->{'url'});
282 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
283 $tpl->fillin('SCRIPT',$script);
284
285 $tpl->parse_if_block('editable',-w $physical);
286
287 $tpl->fillin('CONTENT',encode_html($$content));
288 }
289
290 my $output = header(-type => 'text/html');
291 $output .= $tpl->get_template;
292
293 return \$output;
294 }
295
296 # exec_beginedit
297 #
298 # Lock a file and display a form to edit it
299 #
300 # Params: 1. Reference to user input hash
301 # 2. Reference to config hash
302 #
303 # Return: Output of the command (Scalar Reference)
304
305 sub exec_beginedit($$)
306 {
307 my ($data,$config) = @_;
308 my $physical = $data->{'physical'};
309 my $virtual = $data->{'virtual'};
310 my $dir = upper_path($virtual);
311
312 return error($config->{'errors'}->{'link_edit'},$dir) if(-l $physical);
313 return error($config->{'errors'}->{'dir_edit'}, $dir) if(-d $physical);
314 return error($config->{'errors'}->{'no_edit'}, $dir) unless(-r $physical && -w $physical);
315
316 # Check on binary files
317
318 return error($config->{'errors'}->{'binary_file'},$dir) unless(-T $physical);
319
320 # Is the file too large?
321
322 return error($config->{'errors'}->{'file_too_large'},$dir,{SIZE => $config->{'max_file_size'}}) if($config->{'max_file_size'} && -s $physical > $config->{'max_file_size'});
323
324 # Show the editing form
325
326 my $content = file_read($physical);
327 my $md5sum = md5_hex($$content);
328 $$content =~ s/\015\012|\012|\015/\n/g;
329
330 my $tpl = new Template;
331 $tpl->read_file($config->{'templates'}->{'editfile'});
332
333 $tpl->fillin('FILE',encode_html($virtual));
334 $tpl->fillin('FILE_URL',escape($virtual));
335 $tpl->fillin('DIR',encode_html($dir));
336 $tpl->fillin('DIR_URL',escape($dir));
337 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
338 $tpl->fillin('SCRIPT',$script);
339 $tpl->fillin('MD5SUM',$md5sum);
340 $tpl->fillin('CONTENT',encode_html($$content));
341
342 $tpl->parse_if_block('error',0);
343
344 my $output = header(-type => 'text/html');
345 $output .= $tpl->get_template;
346
347 return \$output;
348 }
349
350 # exec_endedit()
351 #
352 # Save a file, unlock it and return to directory view
353 #
354 # Params: 1. Reference to user input hash
355 # 2. Reference to config hash
356 #
357 # Return: Output of the command (Scalar Reference)
358
359 sub exec_endedit($$)
360 {
361 my ($data,$config) = @_;
362 my $physical = $data->{'physical'};
363 my $virtual = $data->{'virtual'};
364 my $dir = upper_path($virtual);
365 my $cgi = $data->{'cgi'};
366 my $content = $cgi->param('filecontent');
367 my $md5sum = $cgi->param('md5sum');
368 my $output;
369
370 if(defined $content && $md5sum)
371 {
372 # Normalize newlines
373
374 $content =~ s/\015\012|\012|\015/\n/g;
375
376 if($cgi->param('saveas') && $data->{'new_physical'} ne '' && $data->{'new_virtual'} ne '')
377 {
378 # Create the new filename
379
380 $physical = $data->{'new_physical'};
381 $virtual = $data->{'new_virtual'};
382 }
383
384 return error($config->{'errors'}->{'link_edit'},$dir) if(-l $physical);
385 return error($config->{'errors'}->{'dir_edit'},$dir) if(-d $physical);
386 return error($config->{'errors'}->{'no_edit'},$dir) if(-e $physical && !(-r $physical && -w $physical));
387 return error($config->{'errors'}->{'text_to_binary'},$dir) if(-e $physical && not -T $physical);
388
389 # For technical reasons, we can't use file_save() for
390 # saving the file...
391
392 local *FILE;
393
394 sysopen(FILE,$physical,O_RDWR | O_CREAT) or return error($config->{'errors'}->{'edit_failed'},$dir,{FILE => encode_html($virtual)});
395 file_lock(*FILE,LOCK_EX) or do { close(FILE); return error($config->{'errors'}->{'edit_failed'},$dir,{FILE => encode_html($virtual)}) };
396
397 my $md5 = new Digest::MD5;
398 $md5->addfile(*FILE);
399
400 my $md5file = $md5->hexdigest;
401 my $md5data = md5_hex($content);
402
403 if($md5file ne $md5sum && $md5data ne $md5file && not $cgi->param('saveas'))
404 {
405 # The file changed meanwhile
406
407 my $tpl = new Template;
408 $tpl->read_file($config->{'templates'}->{'editfile'});
409
410 $tpl->fillin('ERROR',$config->{'errors'}->{'edit_file_changed'});
411
412 $tpl->fillin('FILE',encode_html($virtual));
413 $tpl->fillin('FILE_URL',escape($virtual));
414 $tpl->fillin('DIR',encode_html($dir));
415 $tpl->fillin('DIR_URL',escape($dir));
416 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
417 $tpl->fillin('SCRIPT',$script);
418 $tpl->fillin('MD5SUM',$md5file);
419 $tpl->fillin('CONTENT',encode_html($content));
420
421 $tpl->parse_if_block('error',1);
422
423 my $data = header(-type => 'text/html');
424 $data .= $tpl->get_template;
425
426 $output = \$data;
427 }
428 else
429 {
430 if($md5data ne $md5file)
431 {
432 seek(FILE,0,0);
433 truncate(FILE,0);
434
435 print FILE $content;
436 }
437
438 $output = ($cgi->param('continue'))
439 ? devedit_reload({command => 'beginedit', file => $virtual})
440 : devedit_reload({command => 'show', file => $dir});
441 }
442
443 close(FILE);
444
445 return $output;
446 }
447
448 return devedit_reload({command => 'beginedit', file => $virtual});
449 }
450
451 # exec_download()
452 #
453 # Execute a HTTP download of a file
454 #
455 # Params: 1. Reference to user input hash
456 # 2. Reference to config hash
457 #
458 # Return: Output of the command (Scalar Reference)
459
460 sub exec_download($$)
461 {
462 my ($data,$config) = @_;
463 my $physical = $data->{'physical'};
464 my $virtual = $data->{'virtual'};
465 my $dir = upper_path($virtual);
466
467 return return error($config->{'errors'}->{'no_download'},$dir,{FILE => $virtual}) if((not -r $physical) || (-d $physical || -l $physical));
468
469 my $filename = file_name($virtual);
470
471 my $output = header(-type => 'application/octet-stream', -attachment => $filename);
472 $output .= ${ file_read($physical,1) };
473
474 return \$output;
475 }
476
477 # exec_mkfile()
478 #
479 # Create a file and return to directory view
480 #
481 # Params: 1. Reference to user input hash
482 # 2. Reference to config hash
483 #
484 # Return: Output of the command (Scalar Reference)
485
486 sub exec_mkfile($$)
487 {
488 my ($data,$config) = @_;
489 my $new_physical = $data->{'new_physical'};
490 my $new_virtual = $data->{'new_virtual'};
491 my $dir = upper_path($new_virtual);
492 $new_virtual = encode_html($new_virtual);
493
494 if($new_physical)
495 {
496 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
497
498 file_create($new_physical) or return error($config->{'errors'}->{'mkfile_failed'},$dir,{FILE => $new_virtual});
499
500 if($data->{'cgi'}->param('edit'))
501 {
502 return devedit_reload({command => 'beginedit', file => $new_virtual});
503 }
504 else
505 {
506 return devedit_reload({command => 'show', file => $dir});
507 }
508 }
509 else
510 {
511 my $tpl = new Template;
512 $tpl->read_file($config->{'templates'}->{'mkfile'});
513
514 $tpl->fillin('DIR','/');
515 $tpl->fillin('SCRIPT',$script);
516
517 my $output = header(-type => 'text/html');
518 $output .= $tpl->get_template;
519
520 return \$output;
521 }
522 }
523
524 # exec_mkdir()
525 #
526 # Create a directory and return to directory view
527 #
528 # Params: 1. Reference to user input hash
529 # 2. Reference to config hash
530 #
531 # Return: Output of the command (Scalar Reference)
532
533 sub exec_mkdir($$)
534 {
535 my ($data,$config) = @_;
536 my $new_physical = $data->{'new_physical'};
537 my $new_virtual = $data->{'new_virtual'};
538 my $dir = upper_path($new_virtual);
539 $new_virtual = encode_html($new_virtual);
540
541 if($new_physical)
542 {
543 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
544
545 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
546 return devedit_reload({command => 'show', file => $dir});
547 }
548 else
549 {
550 my $tpl = new Template;
551 $tpl->read_file($config->{'templates'}->{'mkdir'});
552
553 $tpl->fillin('DIR','/');
554 $tpl->fillin('SCRIPT',$script);
555
556 my $output = header(-type => 'text/html');
557 $output .= $tpl->get_template;
558
559 return \$output;
560 }
561 }
562
563 # exec_upload()
564 #
565 # Process a file upload
566 #
567 # Params: 1. Reference to user input hash
568 # 2. Reference to config hash
569 #
570 # Return: Output of the command (Scalar Reference)
571
572 sub exec_upload($$)
573 {
574 my ($data,$config) = @_;
575 my $physical = $data->{'physical'};
576 my $virtual = $data->{'virtual'};
577 my $cgi = $data->{'cgi'};
578
579 return error($config->{'errors'}->{'no_directory'},upper_path($virtual),{FILE => encode_html($virtual)}) unless(-d $physical && not -l $physical);
580 return error($config->{'errors'}->{'dir_no_create'},$virtual,{DIR => encode_html($virtual)}) unless(-w $physical);
581
582 if(my $uploaded_file = $cgi->param('uploaded_file'))
583 {
584 if($cgi->param('remote_file'))
585 {
586 $uploaded_file = $cgi->param('remote_file');
587
588 $uploaded_file =~ s!/!!g;
589 $uploaded_file =~ s!\\!!g;
590 }
591
592 # Process file upload
593
594 my $filename = file_name($uploaded_file);
595 my $file_phys = $physical.'/'.$filename;
596 my $file_virt = encode_html($virtual.$filename);
597
598 if(-e $file_phys)
599 {
600 return error($config->{'errors'}->{'link_replace'},$virtual) if(-l $file_phys);
601 return error($config->{'errors'}->{'dir_replace'},$virtual) if(-d $file_phys);
602 return error($config->{'errors'}->{'exist_no_write'},$virtual,{FILE => $file_virt}) unless(-w $file_phys);
603 return error($config->{'errors'}->{'file_exists'},$virtual,{FILE => $file_virt}) unless($cgi->param('overwrite'));
604 }
605
606 my $ascii = $cgi->param('ascii');
607 my $handle = $cgi->upload('uploaded_file');
608
609 return error($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
610
611 # Read transferred file and write it to disk
612
613 read($handle, my $data, -s $handle);
614 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
615 file_save($file_phys,\$data,not $ascii) or return error($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE => $file_virt});
616
617 if($cgi->param('unpack') && $File::Access::has_archive_extract)
618 {
619 return error($config->{'errors'}->{'no_archive'},$virtual,{FILE => encode_html($file_virt)}) unless(is_archive($file_phys));
620
621 my $return_unpack = archive_unpack($file_phys,$physical);
622
623 return error($config->{'errors'}->{'unpack_failed'},$virtual,{FILE => encode_html($file_virt), AE_ERROR => ''}) unless($return_unpack);
624 }
625
626 return devedit_reload({command => 'show', file => $virtual});
627 }
628 else
629 {
630 my $tpl = new Template;
631 $tpl->read_file($config->{'templates'}->{'upload'});
632
633 $tpl->fillin('DIR',encode_html($virtual));
634 $tpl->fillin('DIR_URL',escape($virtual));
635 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
636 $tpl->fillin('SCRIPT',$script);
637
638 $tpl->parse_if_block('PERL_ARCHIVE_EXTRACT',$File::Access::has_archive_extract);
639
640 my $output = header(-type => 'text/html');
641 $output .= $tpl->get_template;
642
643 return \$output;
644 }
645 }
646
647 # exec_unpack()
648 #
649 # Unpack an archive
650 #
651 # Params: 1. Reference to user input hash
652 # 2. Reference to config hash
653 #
654 # Return: Output of the command (Scalar Reference)
655
656 sub exec_unpack($$)
657 {
658 my ($data,$config) = @_;
659 my $physical = $data->{'physical'};
660 my $virtual = $data->{'virtual'};
661 my $dir = upper_path($virtual);
662 my $new_physical = $data->{'new_physical'};
663 my $new_virtual = $data->{'new_virtual'};
664 my $cgi = $data->{'cgi'};
665
666 return error($config->{'errors'}->{'no_ae'},$dir) unless($File::Access::has_archive_extract);
667 return error($config->{'errors'}->{'no_archive'},$dir,{FILE => encode_html($virtual)}) unless(is_archive($physical));
668
669 if($new_physical)
670 {
671 return error($config->{'errors'}->{'unpack_no_dir'},$dir,{FILE => encode_html($virtual), NEW_FILE => encode_html($new_virtual)}) if(-l $new_physical || not -d $new_physical);
672
673 my $return_unpack = archive_unpack($physical,$new_physical);
674
675 return error($config->{'errors'}->{'unpack_failed'},$dir,{FILE => encode_html($virtual), AE_ERROR => encode_html($File::Access::archive_extract_error)}) unless($return_unpack);
676
677 return devedit_reload({command => 'show', file => $new_virtual});
678 }
679 else
680 {
681 my $tpl = new Template;
682 $tpl->read_file($config->{'templates'}->{'unpack'});
683
684 $tpl->fillin('FILE',encode_html($virtual));
685 $tpl->fillin('DIR',encode_html($dir));
686 $tpl->fillin('DIR_URL',escape($dir));
687 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
688 $tpl->fillin('SCRIPT',$script);
689
690 my $output = header(-type => 'text/html');
691 $output .= $tpl->get_template;
692
693 return \$output;
694 }
695 }
696
697 # exec_copy()
698 #
699 # Copy a file and return to directory view
700 #
701 # Params: 1. Reference to user input hash
702 # 2. Reference to config hash
703 #
704 # Return: Output of the command (Scalar Reference)
705
706 sub exec_copy($$)
707 {
708 my ($data,$config) = @_;
709 my $physical = $data->{'physical'};
710 my $virtual = $data->{'virtual'};
711 my $dir = upper_path($virtual);
712 my $new_physical = $data->{'new_physical'};
713
714 return error($config->{'errors'}->{'link_copy'},$dir) if(-l $physical);
715 return error($config->{'errors'}->{'no_copy'},$dir) unless(-r $physical);
716
717 if($new_physical)
718 {
719 my $new_virtual = multi_string($data->{'new_virtual'});
720 my $new_dir = upper_path($new_virtual->{'normal'});
721
722 if(-d $physical)
723 {
724 return error($config->{'errors'}->{'no_copy'},$dir) unless(-x $physical);
725 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual->{'html'}}) if(-e $new_physical);
726 return error($config->{'errors'}->{'dir_copy_self'},$dir) if(index($new_virtual->{'normal'},$virtual) == 0);
727
728 dir_copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
729 return devedit_reload({command => 'show', file => $new_dir});
730 }
731 else
732 {
733 if(-e $new_physical)
734 {
735 return error($config->{'errors'}->{'link_replace'},$new_dir) if(-l $new_physical);
736 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical);
737 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual->{'html'}}) unless(-w $new_physical);
738
739 if(not $data->{'cgi'}->param('confirmed'))
740 {
741 my $tpl = new Template;
742 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
743
744 $tpl->fillin('FILE',encode_html($virtual));
745 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
746 $tpl->fillin('NEW_FILENAME',file_name($new_virtual->{'html'}));
747 $tpl->fillin('NEW_DIR',encode_html($new_dir));
748 $tpl->fillin('DIR',encode_html($dir));
749 $tpl->fillin('DIR_URL',escape($dir));
750
751 $tpl->fillin('COMMAND','copy');
752 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
753 $tpl->fillin('SCRIPT',$script);
754
755 my $output = header(-type => 'text/html');
756 $output .= $tpl->get_template;
757
758 return \$output;
759 }
760 }
761
762 copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
763 return devedit_reload({command => 'show', file => $new_dir});
764 }
765 }
766 else
767 {
768 if(-d $physical)
769 {
770 my $tpl = new Template;
771 $tpl->read_file($config->{'templates'}->{'copydir'});
772
773 $tpl->fillin('FILE',encode_html($virtual));
774 $tpl->fillin('DIR',encode_html($dir));
775 $tpl->fillin('DIR_URL',escape($dir));
776 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
777 $tpl->fillin('SCRIPT',$script);
778
779 my $output = header(-type => 'text/html');
780 $output .= $tpl->get_template;
781
782 return \$output;
783 }
784 else
785 {
786 my $tpl = new Template;
787 $tpl->read_file($config->{'templates'}->{'copyfile'});
788
789 $tpl->fillin('FILE',encode_html($virtual));
790 $tpl->fillin('DIR',encode_html($dir));
791 $tpl->fillin('DIR_URL',escape($dir));
792 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
793 $tpl->fillin('SCRIPT',$script);
794
795 my $output = header(-type => 'text/html');
796 $output .= $tpl->get_template;
797
798 return \$output;
799 }
800 }
801 }
802
803 # exec_rename()
804 #
805 # Rename/move a file and return to directory view
806 #
807 # Params: 1. Reference to user input hash
808 # 2. Reference to config hash
809 #
810 # Return: Output of the command (Scalar Reference)
811
812 sub exec_rename($$)
813 {
814 my ($data,$config) = @_;
815 my $physical = $data->{'physical'};
816 my $virtual = $data->{'virtual'};
817 my $dir = upper_path($virtual);
818 my $new_physical = $data->{'new_physical'};
819
820 return error($config->{'errors'}->{'rename_root'},'/') if($virtual eq '/');
821 return error($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path($physical));
822
823 if($new_physical)
824 {
825 my $new_virtual = multi_string($data->{'new_virtual'});
826 my $new_dir = upper_path($new_virtual->{'normal'});
827
828 if(-e $new_physical)
829 {
830 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical && not -l $new_physical);
831 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual}) unless(-w $new_physical);
832
833 if(not $data->{'cgi'}->param('confirmed'))
834 {
835 my $tpl = new Template;
836 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
837
838 $tpl->fillin('FILE',encode_html($virtual));
839 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
840 $tpl->fillin('NEW_FILENAME',file_name($new_virtual->{'html'}));
841 $tpl->fillin('NEW_DIR',encode_html($new_dir));
842 $tpl->fillin('DIR',encode_html($dir));
843
844 $tpl->fillin('COMMAND','rename');
845 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
846 $tpl->fillin('SCRIPT',$script);
847
848 my $output = header(-type => 'text/html');
849 $output .= $tpl->get_template;
850
851 return \$output;
852 }
853 }
854
855 move($physical,$new_physical) or return error($config->{'errors'}->{'rename_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
856 return devedit_reload({command => 'show', file => $new_dir});
857 }
858 else
859 {
860 my $tpl = new Template;
861 $tpl->read_file($config->{'templates'}->{'renamefile'});
862
863 $tpl->fillin('FILE',encode_html($virtual));
864 $tpl->fillin('DIR',encode_html($dir));
865 $tpl->fillin('DIR_URL',escape($dir));
866 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
867 $tpl->fillin('SCRIPT',$script);
868
869 my $output = header(-type => 'text/html');
870 $output .= $tpl->get_template;
871
872 return \$output;
873 }
874 }
875
876 # exec_remove()
877 #
878 # Remove a file or a directory and return to directory view
879 #
880 # Params: 1. Reference to user input hash
881 # 2. Reference to config hash
882 #
883 # Return: Output of the command (Scalar Reference)
884
885 sub exec_remove($$)
886 {
887 my ($data,$config) = @_;
888 my $physical = $data->{'physical'};
889 my $virtual = $data->{'virtual'};
890 my $dir = upper_path($virtual);
891
892 return error($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
893 return error($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path($physical));
894
895 if(-d $physical && not -l $physical)
896 {
897 # Remove a directory
898
899 if($data->{'cgi'}->param('confirmed'))
900 {
901 rmtree($physical);
902 return devedit_reload({command => 'show', file => $dir});
903 }
904 else
905 {
906 my $tpl = new Template;
907 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
908
909 $tpl->fillin('DIR',encode_html($virtual));
910 $tpl->fillin('DIR_URL',escape($virtual));
911 $tpl->fillin('UPPER_DIR',encode_html($dir));
912 $tpl->fillin('UPPER_DIR_URL',escape($dir));
913 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
914 $tpl->fillin('SCRIPT',$script);
915
916 my $output = header(-type => 'text/html');
917 $output .= $tpl->get_template;
918
919 return \$output;
920 }
921 }
922 else
923 {
924 # Remove a file
925
926 if($data->{'cgi'}->param('confirmed'))
927 {
928 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},$dir,{FILE => $virtual});
929 return devedit_reload({command => 'show', file => $dir});
930 }
931 else
932 {
933 my $tpl = new Template;
934 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
935
936 $tpl->fillin('FILE',encode_html($virtual));
937 $tpl->fillin('FILE_URL',escape($virtual));
938 $tpl->fillin('DIR',encode_html($dir));
939 $tpl->fillin('DIR_URL',escape($dir));
940 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
941 $tpl->fillin('SCRIPT',$script);
942
943 my $output = header(-type => 'text/html');
944 $output .= $tpl->get_template;
945
946 return \$output;
947 }
948 }
949 }
950
951 # exec_remove_multi()
952 #
953 # Remove a file or a directory and return to directory view
954 #
955 # Params: 1. Reference to user input hash
956 # 2. Reference to config hash
957 #
958 # Return: Output of the command (Scalar Reference)
959
960 sub exec_remove_multi($$)
961 {
962 my ($data,$config) = @_;
963 my $physical = $data->{'physical'};
964 my $virtual = $data->{'virtual'};
965 my $cgi = $data->{'cgi'};
966
967 my @files = $cgi->param('files');#
968 my @new_files;
969
970 if(@files)
971 {
972 foreach my $file(@files)
973 {
974 # Filter out some "bad" files (e.g. files going up in the
975 # directory hierarchy or files containing slashes (it's too
976 # dangerous...)
977
978 next if($file =~ m!^\.+$!);
979 next if($file =~ m!/!);
980 next if($file =~ m!\\!);
981
982 push(@new_files,$file);
983 }
984 }
985
986 if(@new_files)
987 {
988 if($cgi->param('confirmed'))
989 {
990 my @success;
991 my @failed;
992
993 foreach my $file(@new_files)
994 {
995 my $file_path = clean_path($physical.'/'.$file);
996
997 if(-e $file_path)
998 {
999 if(-d $file_path && not -l $file_path)
1000 {
1001 # Remove a directory
1002
1003 if(rmtree($file_path))
1004 {
1005 push(@success,clean_path($file));
1006 }
1007 else
1008 {
1009 push(@failed,clean_path($file));
1010 }
1011 }
1012 else
1013 {
1014 # Remove a file
1015
1016 if(unlink($file_path))
1017 {
1018 push(@success,clean_path($file));
1019 }
1020 else
1021 {
1022 push(@failed,clean_path($file));
1023 }
1024 }
1025 }
1026 else
1027 {
1028 push(@failed,clean_path($file));
1029 }
1030 }
1031
1032 my $tpl = new Template;
1033 $tpl->read_file($config->{'templates'}->{'rmmulti'});
1034
1035 if(scalar(@success) > 0)
1036 {
1037 if(scalar(@success) == scalar(@new_files) && scalar(@failed) == 0)
1038 {
1039 return devedit_reload({command => 'show', file => $virtual});
1040 }
1041 else
1042 {
1043 $tpl->parse_if_block('success',1);
1044
1045 foreach my $file_success(@success)
1046 {
1047 $tpl->add_loop_data('SUCCESS',{FILE => encode_html($file_success),
1048 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_success))});
1049 }
1050
1051 $tpl->parse_loop('SUCCESS');
1052 }
1053 }
1054 else
1055 {
1056 $tpl->parse_if_block('success',0);
1057 }
1058
1059 if(scalar(@failed) > 0)
1060 {
1061 $tpl->parse_if_block('failed',1);
1062
1063 foreach my $file_failed(@failed)
1064 {
1065 $tpl->add_loop_data('FAILED',{FILE => encode_html($file_failed),
1066 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_failed))});
1067 }
1068
1069 $tpl->parse_loop('FAILED');
1070 }
1071 else
1072 {
1073 $tpl->parse_if_block('failed',0);
1074 }
1075
1076
1077 $tpl->fillin('DIR',encode_html($virtual));
1078 $tpl->fillin('SCRIPT',$script);
1079
1080 my $output = header(-type => 'text/html');
1081 $output .= $tpl->get_template;
1082
1083 return \$output;
1084 }
1085 else
1086 {
1087 my $tpl = new Template;
1088 $tpl->read_file($config->{'templates'}->{'confirm_rmmulti'});
1089
1090 foreach my $file(@new_files)
1091 {
1092 $tpl->add_loop_data('FILES',{FILE => encode_html($file),
1093 FILE_PATH => encode_html(clean_path($virtual.'/'.$file))});
1094 }
1095
1096 $tpl->parse_loop('FILES');
1097
1098 $tpl->fillin('COUNT',scalar(@new_files));
1099
1100 $tpl->fillin('DIR',encode_html($virtual));
1101 $tpl->fillin('SCRIPT',$script);
1102
1103 my $output = header(-type => 'text/html');
1104 $output .= $tpl->get_template;
1105
1106 return \$output;
1107 }
1108 }
1109 else
1110 {
1111 return devedit_reload({command => 'show', file => $virtual});
1112 }
1113 }
1114
1115 # exec_chprop()
1116 #
1117 # Change the mode and the group of a file or a directory
1118 #
1119 # Params: 1. Reference to user input hash
1120 # 2. Reference to config hash
1121 #
1122 # Return: Output of the command (Scalar Reference)
1123
1124 sub exec_chprop($$)
1125 {
1126 my ($data,$config) = @_;
1127 my $physical = $data->{'physical'};
1128 my $virtual = $data->{'virtual'};
1129 my $dir = upper_path($virtual);
1130
1131 return error($config->{'errors'}->{'no_users'},$dir,{FILE => encode_html($virtual)}) unless($users);
1132 return error($config->{'errors'}->{'chprop_root'},'/') if($virtual eq '/');
1133 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => encode_html($virtual)}) unless(-o $physical);
1134 return error($config->{'errors'}->{'chprop_link'},$dir) if(-l $physical);
1135
1136 my $cgi = $data->{'cgi'};
1137 my $mode = $cgi->param('mode');
1138 my $group = $cgi->param('group');
1139
1140 if($mode || $group)
1141 {
1142 if($mode)
1143 {
1144 # Change the mode
1145
1146 return error($config->{'errors'}->{'invalid_mode'},$dir) unless($mode =~ /^[0-7]{3,}$/);
1147 chmod(oct($mode),$physical);
1148 }
1149
1150 if($group)
1151 {
1152 # Change the group using the `chgrp` system command
1153
1154 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => encode_html($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
1155 system('chgrp',$group,$physical);
1156 }
1157
1158 return devedit_reload({command => 'show', file => $dir});
1159 }
1160 else
1161 {
1162 # Display the form
1163
1164 my @stat = stat($physical);
1165 my $mode = $stat[2];
1166 my $gid = $stat[5];
1167
1168 my $tpl = new Template;
1169 $tpl->read_file($config->{'templates'}->{'chprop'});
1170
1171 # Insert file properties into the template
1172
1173 $tpl->fillin('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
1174 $tpl->fillin('MODE_STRING',mode_string($mode));
1175 $tpl->fillin('GID',$gid);
1176
1177 if(my $group = getgrgid($gid))
1178 {
1179 $tpl->fillin('GROUP',encode_html($group));
1180 $tpl->parse_if_block('group_detected',1);
1181 }
1182 else
1183 {
1184 $tpl->parse_if_block('group_detected',0);
1185 }
1186
1187 # Insert other information
1188
1189 $tpl->fillin('FILE',encode_html($virtual));
1190 $tpl->fillin('FILE_URL',escape($virtual));
1191 $tpl->fillin('DIR',encode_html($dir));
1192 $tpl->fillin('DIR_URL',escape($dir));
1193 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
1194 $tpl->fillin('SCRIPT',$script);
1195
1196 my $output = header(-type => 'text/html');
1197 $output .= $tpl->get_template;
1198
1199 return \$output;
1200 }
1201 }
1202
1203 # exec_about()
1204 #
1205 # Display some information about Dev-Editor
1206 #
1207 # Params: 1. Reference to user input hash
1208 # 2. Reference to config hash
1209 #
1210 # Return: Output of the command (Scalar Reference)
1211
1212 sub exec_about($$)
1213 {
1214 my ($data,$config) = @_;
1215
1216 my $tpl = new Template;
1217 $tpl->read_file($config->{'templates'}->{'about'});
1218
1219 $tpl->fillin('SCRIPT',$script);
1220
1221 # Dev-Editor's version number
1222
1223 $tpl->fillin('VERSION',$data->{'version'});
1224
1225 # Some path information
1226
1227 $tpl->fillin('SCRIPT_PHYS',encode_html($ENV{'SCRIPT_FILENAME'}));
1228 $tpl->fillin('CONFIG_PATH',encode_html($data->{'configfile'}));
1229 $tpl->fillin('FILE_ROOT', encode_html($config->{'fileroot'}));
1230 $tpl->fillin('HTTP_ROOT', encode_html($config->{'httproot'}));
1231
1232 # Perl
1233
1234 $tpl->fillin('PERL_PROG',encode_html($^X));
1235 $tpl->fillin('PERL_VER', sprintf('%vd',$^V));
1236
1237 $tpl->parse_if_block('PERL_ARCHIVE_EXTRACT',$File::Access::has_archive_extract);
1238
1239 # Information about the server
1240
1241 $tpl->fillin('HTTPD',encode_html($ENV{'SERVER_SOFTWARE'}));
1242 $tpl->fillin('OS', encode_html($^O));
1243 $tpl->fillin('TIME', encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime : localtime)));
1244
1245 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
1246
1247 # Process information
1248
1249 $tpl->fillin('PID',$$);
1250
1251 # The following information is only available on systems supporting
1252 # users and groups
1253
1254 if($users)
1255 {
1256 # Dev-Editor is running on a system which allows users and groups
1257 # So we display the user and the group of our process
1258
1259 my $uid = POSIX::getuid;
1260 my $gid = POSIX::getgid;
1261
1262 $tpl->parse_if_block('users',1);
1263
1264 # IDs of user and group
1265
1266 $tpl->fillin('UID',$uid);
1267 $tpl->fillin('GID',$gid);
1268
1269 # Names of user and group
1270
1271 if(my $user = getpwuid($uid))
1272 {
1273 $tpl->fillin('USER',encode_html($user));
1274 $tpl->parse_if_block('user_detected',1);
1275 }
1276 else
1277 {
1278 $tpl->parse_if_block('user_detected',0);
1279 }
1280
1281 if(my $group = getgrgid($gid))
1282 {
1283 $tpl->fillin('GROUP',encode_html($group));
1284 $tpl->parse_if_block('group_detected',1);
1285 }
1286 else
1287 {
1288 $tpl->parse_if_block('group_detected',0);
1289 }
1290
1291 # Process umask
1292
1293 $tpl->fillin('UMASK',sprintf('%04o',umask));
1294 }
1295 else
1296 {
1297 $tpl->parse_if_block('users',0);
1298 }
1299
1300 my $output = header(-type => 'text/html');
1301 $output .= $tpl->get_template;
1302
1303 return \$output;
1304 }
1305
1306 # it's true, baby ;-)
1307
1308 1;
1309
1310 #
1311 ### End ###

patrick-canterino.de