]> git.p6c8.net - devedit.git/blob - modules/Command.pm
- Tool.pm: Be consistent (lc instead of uc)
[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: 2010-05-24
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
497 if($data->{'cgi'}->param('edit'))
498 {
499 return devedit_reload({command => 'beginedit', file => $new_virtual});
500 }
501 else
502 {
503 return devedit_reload({command => 'show', file => $dir});
504 }
505 }
506 else
507 {
508 my $tpl = new Template;
509 $tpl->read_file($config->{'templates'}->{'mkfile'});
510
511 $tpl->fillin('DIR','/');
512 $tpl->fillin('SCRIPT',$script);
513
514 my $output = header(-type => 'text/html');
515 $output .= $tpl->get_template;
516
517 return \$output;
518 }
519 }
520
521 # exec_mkdir()
522 #
523 # Create a directory and return to directory view
524 #
525 # Params: 1. Reference to user input hash
526 # 2. Reference to config hash
527 #
528 # Return: Output of the command (Scalar Reference)
529
530 sub exec_mkdir($$)
531 {
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);
537
538 if($new_physical)
539 {
540 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
541
542 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
543 return devedit_reload({command => 'show', file => $dir});
544 }
545 else
546 {
547 my $tpl = new Template;
548 $tpl->read_file($config->{'templates'}->{'mkdir'});
549
550 $tpl->fillin('DIR','/');
551 $tpl->fillin('SCRIPT',$script);
552
553 my $output = header(-type => 'text/html');
554 $output .= $tpl->get_template;
555
556 return \$output;
557 }
558 }
559
560 # exec_upload()
561 #
562 # Process a file upload
563 #
564 # Params: 1. Reference to user input hash
565 # 2. Reference to config hash
566 #
567 # Return: Output of the command (Scalar Reference)
568
569 sub exec_upload($$)
570 {
571 my ($data,$config) = @_;
572 my $physical = $data->{'physical'};
573 my $virtual = $data->{'virtual'};
574 my $cgi = $data->{'cgi'};
575
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);
578
579 if(my $uploaded_file = $cgi->param('uploaded_file'))
580 {
581 if($cgi->param('remote_file'))
582 {
583 $uploaded_file = $cgi->param('remote_file');
584
585 $uploaded_file =~ s!/!!g;
586 $uploaded_file =~ s!\\!!g;
587 }
588
589 # Process file upload
590
591 my $filename = file_name($uploaded_file);
592 my $file_phys = $physical.'/'.$filename;
593 my $file_virt = encode_html($virtual.$filename);
594
595 if(-e $file_phys)
596 {
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'));
601 }
602
603 my $ascii = $cgi->param('ascii');
604 my $handle = $cgi->upload('uploaded_file');
605
606 return error($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
607
608 # Read transferred file and write it to disk
609
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});
613
614 return devedit_reload({command => 'show', file => $virtual});
615 }
616 else
617 {
618 my $tpl = new Template;
619 $tpl->read_file($config->{'templates'}->{'upload'});
620
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);
625
626 my $output = header(-type => 'text/html');
627 $output .= $tpl->get_template;
628
629 return \$output;
630 }
631 }
632
633 # exec_copy()
634 #
635 # Copy a file and return to directory view
636 #
637 # Params: 1. Reference to user input hash
638 # 2. Reference to config hash
639 #
640 # Return: Output of the command (Scalar Reference)
641
642 sub exec_copy($$)
643 {
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'};
649
650 return error($config->{'errors'}->{'link_copy'},$dir) if(-l $physical);
651 return error($config->{'errors'}->{'no_copy'},$dir) unless(-r $physical);
652
653 if($new_physical)
654 {
655 my $new_virtual = multi_string($data->{'new_virtual'});
656 my $new_dir = upper_path($new_virtual->{'normal'});
657
658 if(-d $physical)
659 {
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);
663
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});
666 }
667 else
668 {
669 if(-e $new_physical)
670 {
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);
674
675 if(not $data->{'cgi'}->param('confirmed'))
676 {
677 my $tpl = new Template;
678 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
679
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));
686
687 $tpl->fillin('COMMAND','copy');
688 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
689 $tpl->fillin('SCRIPT',$script);
690
691 my $output = header(-type => 'text/html');
692 $output .= $tpl->get_template;
693
694 return \$output;
695 }
696 }
697
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});
700 }
701 }
702 else
703 {
704 if(-d $physical)
705 {
706 my $tpl = new Template;
707 $tpl->read_file($config->{'templates'}->{'copydir'});
708
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);
714
715 my $output = header(-type => 'text/html');
716 $output .= $tpl->get_template;
717
718 return \$output;
719 }
720 else
721 {
722 my $tpl = new Template;
723 $tpl->read_file($config->{'templates'}->{'copyfile'});
724
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);
730
731 my $output = header(-type => 'text/html');
732 $output .= $tpl->get_template;
733
734 return \$output;
735 }
736 }
737 }
738
739 # exec_rename()
740 #
741 # Rename/move a file and return to directory view
742 #
743 # Params: 1. Reference to user input hash
744 # 2. Reference to config hash
745 #
746 # Return: Output of the command (Scalar Reference)
747
748 sub exec_rename($$)
749 {
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'};
755
756 return error($config->{'errors'}->{'rename_root'},'/') if($virtual eq '/');
757 return error($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path($physical));
758
759 if($new_physical)
760 {
761 my $new_virtual = multi_string($data->{'new_virtual'});
762 my $new_dir = upper_path($new_virtual->{'normal'});
763
764 if(-e $new_physical)
765 {
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);
768
769 if(not $data->{'cgi'}->param('confirmed'))
770 {
771 my $tpl = new Template;
772 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
773
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));
779
780 $tpl->fillin('COMMAND','rename');
781 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
782 $tpl->fillin('SCRIPT',$script);
783
784 my $output = header(-type => 'text/html');
785 $output .= $tpl->get_template;
786
787 return \$output;
788 }
789 }
790
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});
793 }
794 else
795 {
796 my $tpl = new Template;
797 $tpl->read_file($config->{'templates'}->{'renamefile'});
798
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);
804
805 my $output = header(-type => 'text/html');
806 $output .= $tpl->get_template;
807
808 return \$output;
809 }
810 }
811
812 # exec_remove()
813 #
814 # Remove a file or a directory and return to directory view
815 #
816 # Params: 1. Reference to user input hash
817 # 2. Reference to config hash
818 #
819 # Return: Output of the command (Scalar Reference)
820
821 sub exec_remove($$)
822 {
823 my ($data,$config) = @_;
824 my $physical = $data->{'physical'};
825 my $virtual = $data->{'virtual'};
826 my $dir = upper_path($virtual);
827
828 return error($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
829 return error($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path($physical));
830
831 if(-d $physical && not -l $physical)
832 {
833 # Remove a directory
834
835 if($data->{'cgi'}->param('confirmed'))
836 {
837 rmtree($physical);
838 return devedit_reload({command => 'show', file => $dir});
839 }
840 else
841 {
842 my $tpl = new Template;
843 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
844
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);
851
852 my $output = header(-type => 'text/html');
853 $output .= $tpl->get_template;
854
855 return \$output;
856 }
857 }
858 else
859 {
860 # Remove a file
861
862 if($data->{'cgi'}->param('confirmed'))
863 {
864 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},$dir,{FILE => $virtual});
865 return devedit_reload({command => 'show', file => $dir});
866 }
867 else
868 {
869 my $tpl = new Template;
870 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
871
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);
878
879 my $output = header(-type => 'text/html');
880 $output .= $tpl->get_template;
881
882 return \$output;
883 }
884 }
885 }
886
887 # exec_remove_multi()
888 #
889 # Remove a file or a directory and return to directory view
890 #
891 # Params: 1. Reference to user input hash
892 # 2. Reference to config hash
893 #
894 # Return: Output of the command (Scalar Reference)
895
896 sub exec_remove_multi($$)
897 {
898 my ($data,$config) = @_;
899 my $physical = $data->{'physical'};
900 my $virtual = $data->{'virtual'};
901 my $cgi = $data->{'cgi'};
902
903 my @files = $cgi->param('files');#
904 my @new_files;
905
906 if(@files)
907 {
908 foreach my $file(@files)
909 {
910 # Filter out some "bad" files (e.g. files going up in the
911 # directory hierarchy or files containing slashes (it's too
912 # dangerous...)
913
914 next if($file =~ m!^\.+$!);
915 next if($file =~ m!/!);
916 next if($file =~ m!\\!);
917
918 push(@new_files,$file);
919 }
920 }
921
922 if(@new_files)
923 {
924 if($cgi->param('confirmed'))
925 {
926 my @success;
927 my @failed;
928
929 foreach my $file(@new_files)
930 {
931 my $file_path = clean_path($physical.'/'.$file);
932
933 if(-e $file_path)
934 {
935 if(-d $file_path && not -l $file_path)
936 {
937 # Remove a directory
938
939 if(rmtree($file_path))
940 {
941 push(@success,clean_path($file));
942 }
943 else
944 {
945 push(@failed,clean_path($file));
946 }
947 }
948 else
949 {
950 # Remove a file
951
952 if(unlink($file_path))
953 {
954 push(@success,clean_path($file));
955 }
956 else
957 {
958 push(@failed,clean_path($file));
959 }
960 }
961 }
962 else
963 {
964 push(@failed,clean_path($file));
965 }
966 }
967
968 my $tpl = new Template;
969 $tpl->read_file($config->{'templates'}->{'rmmulti'});
970
971 if(scalar(@success) > 0)
972 {
973 if(scalar(@success) == scalar(@new_files) && scalar(@failed) == 0)
974 {
975 return devedit_reload({command => 'show', file => $virtual});
976 }
977 else
978 {
979 $tpl->parse_if_block('success',1);
980
981 foreach my $file_success(@success)
982 {
983 $tpl->add_loop_data('SUCCESS',{FILE => encode_html($file_success),
984 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_success))});
985 }
986
987 $tpl->parse_loop('SUCCESS');
988 }
989 }
990 else
991 {
992 $tpl->parse_if_block('success',0);
993 }
994
995 if(scalar(@failed) > 0)
996 {
997 $tpl->parse_if_block('failed',1);
998
999 foreach my $file_failed(@failed)
1000 {
1001 $tpl->add_loop_data('FAILED',{FILE => encode_html($file_failed),
1002 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_failed))});
1003 }
1004
1005 $tpl->parse_loop('FAILED');
1006 }
1007 else
1008 {
1009 $tpl->parse_if_block('failed',0);
1010 }
1011
1012
1013 $tpl->fillin('DIR',encode_html($virtual));
1014 $tpl->fillin('SCRIPT',$script);
1015
1016 my $output = header(-type => 'text/html');
1017 $output .= $tpl->get_template;
1018
1019 return \$output;
1020 }
1021 else
1022 {
1023 my $tpl = new Template;
1024 $tpl->read_file($config->{'templates'}->{'confirm_rmmulti'});
1025
1026 foreach my $file(@new_files)
1027 {
1028 $tpl->add_loop_data('FILES',{FILE => encode_html($file),
1029 FILE_PATH => encode_html(clean_path($virtual.'/'.$file))});
1030 }
1031
1032 $tpl->parse_loop('FILES');
1033
1034 $tpl->fillin('COUNT',scalar(@new_files));
1035
1036 $tpl->fillin('DIR',encode_html($virtual));
1037 $tpl->fillin('SCRIPT',$script);
1038
1039 my $output = header(-type => 'text/html');
1040 $output .= $tpl->get_template;
1041
1042 return \$output;
1043 }
1044 }
1045 else
1046 {
1047 return devedit_reload({command => 'show', file => $virtual});
1048 }
1049 }
1050
1051 # exec_chprop()
1052 #
1053 # Change the mode and the group of a file or a directory
1054 #
1055 # Params: 1. Reference to user input hash
1056 # 2. Reference to config hash
1057 #
1058 # Return: Output of the command (Scalar Reference)
1059
1060 sub exec_chprop($$)
1061 {
1062 my ($data,$config) = @_;
1063 my $physical = $data->{'physical'};
1064 my $virtual = $data->{'virtual'};
1065 my $dir = upper_path($virtual);
1066
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);
1071
1072 my $cgi = $data->{'cgi'};
1073 my $mode = $cgi->param('mode');
1074 my $group = $cgi->param('group');
1075
1076 if($mode || $group)
1077 {
1078 if($mode)
1079 {
1080 # Change the mode
1081
1082 return error($config->{'errors'}->{'invalid_mode'},$dir) unless($mode =~ /^[0-7]{3,}$/);
1083 chmod(oct($mode),$physical);
1084 }
1085
1086 if($group)
1087 {
1088 # Change the group using the `chgrp` system command
1089
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);
1092 }
1093
1094 return devedit_reload({command => 'show', file => $dir});
1095 }
1096 else
1097 {
1098 # Display the form
1099
1100 my @stat = stat($physical);
1101 my $mode = $stat[2];
1102 my $gid = $stat[5];
1103
1104 my $tpl = new Template;
1105 $tpl->read_file($config->{'templates'}->{'chprop'});
1106
1107 # Insert file properties into the template
1108
1109 $tpl->fillin('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
1110 $tpl->fillin('MODE_STRING',mode_string($mode));
1111 $tpl->fillin('GID',$gid);
1112
1113 if(my $group = getgrgid($gid))
1114 {
1115 $tpl->fillin('GROUP',encode_html($group));
1116 $tpl->parse_if_block('group_detected',1);
1117 }
1118 else
1119 {
1120 $tpl->parse_if_block('group_detected',0);
1121 }
1122
1123 # Insert other information
1124
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);
1131
1132 my $output = header(-type => 'text/html');
1133 $output .= $tpl->get_template;
1134
1135 return \$output;
1136 }
1137 }
1138
1139 # exec_about()
1140 #
1141 # Display some information about Dev-Editor
1142 #
1143 # Params: 1. Reference to user input hash
1144 # 2. Reference to config hash
1145 #
1146 # Return: Output of the command (Scalar Reference)
1147
1148 sub exec_about($$)
1149 {
1150 my ($data,$config) = @_;
1151
1152 my $tpl = new Template;
1153 $tpl->read_file($config->{'templates'}->{'about'});
1154
1155 $tpl->fillin('SCRIPT',$script);
1156
1157 # Dev-Editor's version number
1158
1159 $tpl->fillin('VERSION',$data->{'version'});
1160
1161 # Some path information
1162
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'}));
1167
1168 # Perl
1169
1170 $tpl->fillin('PERL_PROG',encode_html($^X));
1171 $tpl->fillin('PERL_VER', sprintf('%vd',$^V));
1172
1173 # Information about the server
1174
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)));
1178
1179 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
1180
1181 # Process information
1182
1183 $tpl->fillin('PID',$$);
1184
1185 # The following information is only available on systems supporting
1186 # users and groups
1187
1188 if($users)
1189 {
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
1192
1193 my $uid = POSIX::getuid;
1194 my $gid = POSIX::getgid;
1195
1196 $tpl->parse_if_block('users',1);
1197
1198 # IDs of user and group
1199
1200 $tpl->fillin('UID',$uid);
1201 $tpl->fillin('GID',$gid);
1202
1203 # Names of user and group
1204
1205 if(my $user = getpwuid($uid))
1206 {
1207 $tpl->fillin('USER',encode_html($user));
1208 $tpl->parse_if_block('user_detected',1);
1209 }
1210 else
1211 {
1212 $tpl->parse_if_block('user_detected',0);
1213 }
1214
1215 if(my $group = getgrgid($gid))
1216 {
1217 $tpl->fillin('GROUP',encode_html($group));
1218 $tpl->parse_if_block('group_detected',1);
1219 }
1220 else
1221 {
1222 $tpl->parse_if_block('group_detected',0);
1223 }
1224
1225 # Process umask
1226
1227 $tpl->fillin('UMASK',sprintf('%04o',umask));
1228 }
1229 else
1230 {
1231 $tpl->parse_if_block('users',0);
1232 }
1233
1234 my $output = header(-type => 'text/html');
1235 $output .= $tpl->get_template;
1236
1237 return \$output;
1238 }
1239
1240 # it's true, baby ;-)
1241
1242 1;
1243
1244 #
1245 ### End ###

patrick-canterino.de