]> git.p6c8.net - devedit.git/blob - modules/Command.pm
ef15a77a2ec10f4d9ebdf35ab6cc748c23a6b85f
[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-03-31
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 'mkdir' => \&exec_mkdir,
46 'mkfile' => \&exec_mkfile,
47 'upload' => \&exec_upload,
48 'copy' => \&exec_copy,
49 'rename' => \&exec_rename,
50 'remove' => \&exec_remove,
51 'chprop' => \&exec_chprop,
52 'about' => \&exec_about
53 );
54
55 ### Export ###
56
57 use base qw(Exporter);
58
59 @EXPORT = qw(exec_command);
60
61 # exec_command()
62 #
63 # Execute the specified command
64 #
65 # Params: 1. Command to execute
66 # 2. Reference to user input hash
67 # 3. Reference to config hash
68 #
69 # Return: Output of the command (Scalar Reference)
70
71 sub exec_command($$$)
72 {
73 my ($command,$data,$config) = @_;
74
75 foreach(keys(%dispatch))
76 {
77 if(lc($_) eq lc($command))
78 {
79 my $output = &{$dispatch{$_}}($data,$config);
80 return $output;
81 }
82 }
83
84 return error($config->{'errors'}->{'command_unknown'},'/',{COMMAND => encode_html($command)});
85 }
86
87 # exec_show()
88 #
89 # View a directory or a file
90 #
91 # Params: 1. Reference to user input hash
92 # 2. Reference to config hash
93 #
94 # Return: Output of the command (Scalar Reference)
95
96 sub exec_show($$)
97 {
98 my ($data,$config) = @_;
99 my $physical = $data->{'physical'};
100 my $virtual = $data->{'virtual'};
101 my $upper_path = multi_string(upper_path($virtual));
102
103 my $tpl = new Template;
104
105 if(-d $physical && not -l $physical)
106 {
107 # Create directory listing
108
109 return error($config->{'errors'}->{'no_dir_access'},$upper_path->{'normal'}) unless(-r $physical && -x $physical);
110
111 my $direntries = dir_read($physical);
112 return error($config->{'errors'}->{'dir_read_failed'},$upper_path->{'normal'},{DIR => encode_html($virtual)}) unless($direntries);
113
114 my $files = $direntries->{'files'};
115 my $dirs = $direntries->{'dirs'};
116
117 my $dirlist = '';
118
119 my $count = 0;
120
121 my $filter1 = $data->{'cgi'}->param('filter') || '*'; # The real wildcard
122 my $filter2 = ($filter1 && $filter1 ne '*') ? $filter1 : ''; # Wildcard for output
123
124 # Create the link to the upper directory
125 # (only if the current directory is not the root directory)
126
127 unless($virtual eq '/')
128 {
129 $count++;
130
131 my @stat = stat($physical.'/..');
132
133 my $udtpl = new Template;
134 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
135
136 $udtpl->fillin('UPPER_DIR',$upper_path->{'html'});
137 $udtpl->fillin('UPPER_DIR_URL',$upper_path->{'url'});
138 $udtpl->fillin('DATE',encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
139
140 $dirlist .= $udtpl->get_template;
141 }
142
143 # Directories
144
145 foreach my $dir(@$dirs)
146 {
147 next if($config->{'hide_dot_files'} && substr($dir,0,1) eq '.');
148 next unless(dos_wildcard_match($filter1,$dir));
149
150 $count++;
151
152 my $phys_path = $physical.'/'.$dir;
153 my $virt_path = multi_string($virtual.$dir.'/');
154
155 my @stat = stat($phys_path);
156
157 my $dtpl = new Template;
158 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
159
160 $dtpl->fillin('DIR',$virt_path->{'html'});
161 $dtpl->fillin('DIR_URL',$virt_path->{'url'});
162 $dtpl->fillin('DIR_NAME',encode_html($dir));
163 $dtpl->fillin('DATE',encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
164 $dtpl->fillin('URL',equal_url(encode_html($config->{'httproot'}),$virt_path->{'html'}));
165
166 $dtpl->parse_if_block('forbidden',is_forbidden_file($config->{'forbidden'},$virt_path->{'normal'}));
167 $dtpl->parse_if_block('readable',-r $phys_path && -x $phys_path);
168 $dtpl->parse_if_block('users',$users && -o $phys_path);
169 $dtpl->parse_if_block('even',($count % 2) == 0);
170
171 $dirlist .= $dtpl->get_template;
172 }
173
174 # Files
175
176 foreach my $file(@$files)
177 {
178 next if($config->{'hide_dot_files'} && substr($file,0,1) eq '.');
179 next unless(dos_wildcard_match($filter1,$file));
180
181 $count++;
182
183 my $phys_path = $physical.'/'.$file;
184 my $virt_path = multi_string($virtual.$file);
185
186 my @stat = lstat($phys_path);
187 my $too_large = $config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'};
188
189 my $ftpl = new Template;
190 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
191
192 $ftpl->fillin('FILE',$virt_path->{'html'});
193 $ftpl->fillin('FILE_URL',$virt_path->{'url'});
194 $ftpl->fillin('FILE_NAME',encode_html($file));
195 $ftpl->fillin('FILE_URL',$virt_path->{'url'});
196 $ftpl->fillin('SIZE',$stat[7]);
197 $ftpl->fillin('DATE',encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
198 $ftpl->fillin('URL',equal_url(encode_html($config->{'httproot'}),$virt_path->{'html'}));
199
200 $ftpl->parse_if_block('link',-l $phys_path);
201 $ftpl->parse_if_block('readable',-r $phys_path);
202 $ftpl->parse_if_block('writeable',-w $phys_path);
203 $ftpl->parse_if_block('binary',-B $phys_path);
204
205 $ftpl->parse_if_block('forbidden',is_forbidden_file($config->{'forbidden'},$virt_path->{'normal'}));
206 $ftpl->parse_if_block('viewable',(-r $phys_path && -T $phys_path && not $too_large) || -l $phys_path);
207 $ftpl->parse_if_block('editable',(-r $phys_path && -w $phys_path && -T $phys_path && not $too_large) && not -l $phys_path);
208
209 $ftpl->parse_if_block('too_large',$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
210
211 $ftpl->parse_if_block('users',$users && -o $phys_path);
212
213 $ftpl->parse_if_block('even',($count % 2) == 0);
214
215 $dirlist .= $ftpl->get_template;
216 }
217
218 $tpl->read_file($config->{'templates'}->{'dirlist'});
219
220 $tpl->fillin('DIRLIST',$dirlist);
221 $tpl->fillin('DIR',encode_html($virtual));
222 $tpl->fillin('DIR_URL',escape($virtual));
223 $tpl->fillin('SCRIPT',$script);
224 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
225
226 $tpl->fillin('FILTER',encode_html($filter2));
227 $tpl->fillin('FILTER_URL',escape($filter2));
228
229 $tpl->parse_if_block('empty',$dirlist eq '');
230 $tpl->parse_if_block('dir_writeable',-w $physical);
231 $tpl->parse_if_block('filter',$filter2);
232 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
233 }
234 elsif(-l $physical)
235 {
236 # Show the target of a symbolic link
237
238 my $link_target = readlink($physical);
239
240 $tpl->read_file($config->{'templates'}->{'viewlink'});
241
242 $tpl->fillin('FILE',encode_html($virtual));
243 $tpl->fillin('DIR',$upper_path->{'html'});
244 $tpl->fillin('DIR_URL',$upper_path->{'url'});
245 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
246 $tpl->fillin('SCRIPT',$script);
247
248 $tpl->fillin('LINK_TARGET',encode_html($link_target));
249 }
250 else
251 {
252 # View a file
253
254 return error($config->{'errors'}->{'no_view'},$upper_path->{'normal'}) unless(-r $physical);
255
256 # Check on binary files
257 # We have to do it in this way or empty files will be recognized
258 # as binary files
259
260 return error($config->{'errors'}->{'binary_file'},$upper_path->{'normal'}) unless(-T $physical);
261
262 # Is the file too large?
263
264 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'});
265
266 # View the file
267
268 my $content = file_read($physical);
269 $$content =~ s/\015\012|\012|\015/\n/g;
270
271 $tpl->read_file($config->{'templates'}->{'viewfile'});
272
273 $tpl->fillin('FILE',encode_html($virtual));
274 $tpl->fillin('FILE_URL',escape($virtual));
275 $tpl->fillin('DIR',$upper_path->{'html'});
276 $tpl->fillin('DIR_URL',$upper_path->{'url'});
277 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
278 $tpl->fillin('SCRIPT',$script);
279
280 $tpl->parse_if_block('editable',-w $physical);
281
282 $tpl->fillin('CONTENT',encode_html($$content));
283 }
284
285 my $output = header(-type => 'text/html');
286 $output .= $tpl->get_template;
287
288 return \$output;
289 }
290
291 # exec_beginedit
292 #
293 # Lock a file and display a form to edit it
294 #
295 # Params: 1. Reference to user input hash
296 # 2. Reference to config hash
297 #
298 # Return: Output of the command (Scalar Reference)
299
300 sub exec_beginedit($$)
301 {
302 my ($data,$config) = @_;
303 my $physical = $data->{'physical'};
304 my $virtual = $data->{'virtual'};
305 my $dir = upper_path($virtual);
306
307 return error($config->{'errors'}->{'link_edit'},$dir) if(-l $physical);
308 return error($config->{'errors'}->{'dir_edit'}, $dir) if(-d $physical);
309 return error($config->{'errors'}->{'no_edit'}, $dir) unless(-r $physical && -w $physical);
310
311 # Check on binary files
312
313 return error($config->{'errors'}->{'binary_file'},$dir) unless(-T $physical);
314
315 # Is the file too large?
316
317 return error($config->{'errors'}->{'file_too_large'},$dir,{SIZE => $config->{'max_file_size'}}) if($config->{'max_file_size'} && -s $physical > $config->{'max_file_size'});
318
319 # Show the editing form
320
321 my $content = file_read($physical);
322 my $md5sum = md5_hex($$content);
323 $$content =~ s/\015\012|\012|\015/\n/g;
324
325 my $tpl = new Template;
326 $tpl->read_file($config->{'templates'}->{'editfile'});
327
328 $tpl->fillin('FILE',encode_html($virtual));
329 $tpl->fillin('FILE_URL',escape($virtual));
330 $tpl->fillin('DIR',encode_html($dir));
331 $tpl->fillin('DIR_URL',escape($dir));
332 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
333 $tpl->fillin('SCRIPT',$script);
334 $tpl->fillin('MD5SUM',$md5sum);
335 $tpl->fillin('CONTENT',encode_html($$content));
336
337 $tpl->parse_if_block('error',0);
338
339 my $output = header(-type => 'text/html');
340 $output .= $tpl->get_template;
341
342 return \$output;
343 }
344
345 # exec_endedit()
346 #
347 # Save a file, unlock it and return to directory view
348 #
349 # Params: 1. Reference to user input hash
350 # 2. Reference to config hash
351 #
352 # Return: Output of the command (Scalar Reference)
353
354 sub exec_endedit($$)
355 {
356 my ($data,$config) = @_;
357 my $physical = $data->{'physical'};
358 my $virtual = $data->{'virtual'};
359 my $dir = upper_path($virtual);
360 my $cgi = $data->{'cgi'};
361 my $content = $cgi->param('filecontent');
362 my $md5sum = $cgi->param('md5sum');
363 my $output;
364
365 if(defined $content && $md5sum)
366 {
367 # Normalize newlines
368
369 $content =~ s/\015\012|\012|\015/\n/g;
370
371 if($cgi->param('saveas') && $data->{'new_physical'} ne '' && $data->{'new_virtual'} ne '')
372 {
373 # Create the new filename
374
375 $physical = $data->{'new_physical'};
376 $virtual = $data->{'new_virtual'};
377 }
378
379 return error($config->{'errors'}->{'link_edit'},$dir) if(-l $physical);
380 return error($config->{'errors'}->{'dir_edit'},$dir) if(-d $physical);
381 return error($config->{'errors'}->{'no_edit'},$dir) if(-e $physical && !(-r $physical && -w $physical));
382 return error($config->{'errors'}->{'text_to_binary'},$dir) if(-e $physical && not -T $physical);
383
384 # For technical reasons, we can't use file_save() for
385 # saving the file...
386
387 local *FILE;
388
389 sysopen(FILE,$physical,O_RDWR | O_CREAT) or return error($config->{'errors'}->{'edit_failed'},$dir,{FILE => encode_html($virtual)});
390 file_lock(*FILE,LOCK_EX) or do { close(FILE); return error($config->{'errors'}->{'edit_failed'},$dir,{FILE => encode_html($virtual)}) };
391
392 my $md5 = new Digest::MD5;
393 $md5->addfile(*FILE);
394
395 my $md5file = $md5->hexdigest;
396 my $md5data = md5_hex($content);
397
398 if($md5file ne $md5sum && $md5data ne $md5file && not $cgi->param('saveas'))
399 {
400 # The file changed meanwhile
401
402 my $tpl = new Template;
403 $tpl->read_file($config->{'templates'}->{'editfile'});
404
405 $tpl->fillin('ERROR',$config->{'errors'}->{'edit_file_changed'});
406
407 $tpl->fillin('FILE',encode_html($virtual));
408 $tpl->fillin('FILE_URL',escape($virtual));
409 $tpl->fillin('DIR',encode_html($dir));
410 $tpl->fillin('DIR_URL',escape($dir));
411 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
412 $tpl->fillin('SCRIPT',$script);
413 $tpl->fillin('MD5SUM',$md5file);
414 $tpl->fillin('CONTENT',encode_html($content));
415
416 $tpl->parse_if_block('error',1);
417
418 my $data = header(-type => 'text/html');
419 $data .= $tpl->get_template;
420
421 $output = \$data;
422 }
423 else
424 {
425 if($md5data ne $md5file)
426 {
427 seek(FILE,0,0);
428 truncate(FILE,0);
429
430 print FILE $content;
431 }
432
433 $output = ($cgi->param('continue'))
434 ? devedit_reload({command => 'beginedit', file => $virtual})
435 : devedit_reload({command => 'show', file => $dir});
436 }
437
438 close(FILE);
439
440 return $output;
441 }
442
443 return devedit_reload({command => 'beginedit', file => $virtual});
444 }
445
446 # exec_mkfile()
447 #
448 # Create a file and return to directory view
449 #
450 # Params: 1. Reference to user input hash
451 # 2. Reference to config hash
452 #
453 # Return: Output of the command (Scalar Reference)
454
455 sub exec_mkfile($$)
456 {
457 my ($data,$config) = @_;
458 my $new_physical = $data->{'new_physical'};
459 my $new_virtual = $data->{'new_virtual'};
460 my $dir = upper_path($new_virtual);
461 $new_virtual = encode_html($new_virtual);
462
463 if($new_physical)
464 {
465 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
466
467 file_create($new_physical) or return error($config->{'errors'}->{'mkfile_failed'},$dir,{FILE => $new_virtual});
468 return devedit_reload({command => 'show', file => $dir});
469 }
470 else
471 {
472 my $tpl = new Template;
473 $tpl->read_file($config->{'templates'}->{'mkfile'});
474
475 $tpl->fillin('DIR','/');
476 $tpl->fillin('SCRIPT',$script);
477
478 my $output = header(-type => 'text/html');
479 $output .= $tpl->get_template;
480
481 return \$output;
482 }
483 }
484
485 # exec_mkdir()
486 #
487 # Create a directory and return to directory view
488 #
489 # Params: 1. Reference to user input hash
490 # 2. Reference to config hash
491 #
492 # Return: Output of the command (Scalar Reference)
493
494 sub exec_mkdir($$)
495 {
496 my ($data,$config) = @_;
497 my $new_physical = $data->{'new_physical'};
498 my $new_virtual = $data->{'new_virtual'};
499 my $dir = upper_path($new_virtual);
500 $new_virtual = encode_html($new_virtual);
501
502 if($new_physical)
503 {
504 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
505
506 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
507 return devedit_reload({command => 'show', file => $dir});
508 }
509 else
510 {
511 my $tpl = new Template;
512 $tpl->read_file($config->{'templates'}->{'mkdir'});
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_upload()
525 #
526 # Process a file upload
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_upload($$)
534 {
535 my ($data,$config) = @_;
536 my $physical = $data->{'physical'};
537 my $virtual = $data->{'virtual'};
538 my $cgi = $data->{'cgi'};
539
540 return error($config->{'errors'}->{'no_directory'},upper_path($virtual),{FILE => encode_html($virtual)}) unless(-d $physical && not -l $physical);
541 return error($config->{'errors'}->{'dir_no_create'},$virtual,{DIR => encode_html($virtual)}) unless(-w $physical);
542
543 if(my $uploaded_file = $cgi->param('uploaded_file'))
544 {
545 if($cgi->param('remote_file'))
546 {
547 $uploaded_file = $cgi->param('remote_file');
548
549 $uploaded_file =~ s!/!!g;
550 $uploaded_file =~ s!\\!!g;
551 }
552
553 # Process file upload
554
555 my $filename = file_name($uploaded_file);
556 my $file_phys = $physical.'/'.$filename;
557 my $file_virt = encode_html($virtual.$filename);
558
559 if(-e $file_phys)
560 {
561 return error($config->{'errors'}->{'link_replace'},$virtual) if(-l $file_phys);
562 return error($config->{'errors'}->{'dir_replace'},$virtual) if(-d $file_phys);
563 return error($config->{'errors'}->{'exist_no_write'},$virtual,{FILE => $file_virt}) unless(-w $file_phys);
564 return error($config->{'errors'}->{'file_exists'},$virtual,{FILE => $file_virt}) unless($cgi->param('overwrite'));
565 }
566
567 my $ascii = $cgi->param('ascii');
568 my $handle = $cgi->upload('uploaded_file');
569
570 return error($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
571
572 # Read transferred file and write it to disk
573
574 read($handle, my $data, -s $handle);
575 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
576 file_save($file_phys,\$data,not $ascii) or return error($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE => $file_virt});
577
578 return devedit_reload({command => 'show', file => $virtual});
579 }
580 else
581 {
582 my $tpl = new Template;
583 $tpl->read_file($config->{'templates'}->{'upload'});
584
585 $tpl->fillin('DIR',encode_html($virtual));
586 $tpl->fillin('DIR_URL',escape($virtual));
587 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
588 $tpl->fillin('SCRIPT',$script);
589
590 my $output = header(-type => 'text/html');
591 $output .= $tpl->get_template;
592
593 return \$output;
594 }
595 }
596
597 # exec_copy()
598 #
599 # Copy a file and return to directory view
600 #
601 # Params: 1. Reference to user input hash
602 # 2. Reference to config hash
603 #
604 # Return: Output of the command (Scalar Reference)
605
606 sub exec_copy($$)
607 {
608 my ($data,$config) = @_;
609 my $physical = $data->{'physical'};
610 my $virtual = $data->{'virtual'};
611 my $dir = upper_path($virtual);
612 my $new_physical = $data->{'new_physical'};
613
614 return error($config->{'errors'}->{'link_copy'},$dir) if(-l $physical);
615 return error($config->{'errors'}->{'no_copy'},$dir) unless(-r $physical);
616
617 if($new_physical)
618 {
619 my $new_virtual = multi_string($data->{'new_virtual'});
620 my $new_dir = upper_path($new_virtual->{'normal'});
621
622 if(-d $physical)
623 {
624 return error($config->{'errors'}->{'no_copy'},$dir) unless(-x $physical);
625 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual->{'html'}}) if(-e $new_physical);
626 return error($config->{'errors'}->{'dir_copy_self'},$dir) if(index($new_virtual->{'normal'},$virtual) == 0);
627
628 dir_copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
629 return devedit_reload({command => 'show', file => $new_dir});
630 }
631 else
632 {
633 if(-e $new_physical)
634 {
635 return error($config->{'errors'}->{'link_replace'},$new_dir) if(-l $new_physical);
636 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical);
637 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual->{'html'}}) unless(-w $new_physical);
638
639 if(not $data->{'cgi'}->param('confirmed'))
640 {
641 my $tpl = new Template;
642 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
643
644 $tpl->fillin('FILE',encode_html($virtual));
645 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
646 $tpl->fillin('NEW_FILENAME',file_name($new_virtual->{'html'}));
647 $tpl->fillin('NEW_DIR',encode_html($new_dir));
648 $tpl->fillin('DIR',encode_html($dir));
649 $tpl->fillin('DIR_URL',escape($dir));
650
651 $tpl->fillin('COMMAND','copy');
652 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
653 $tpl->fillin('SCRIPT',$script);
654
655 my $output = header(-type => 'text/html');
656 $output .= $tpl->get_template;
657
658 return \$output;
659 }
660 }
661
662 copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
663 return devedit_reload({command => 'show', file => $new_dir});
664 }
665 }
666 else
667 {
668 if(-d $physical)
669 {
670 my $tpl = new Template;
671 $tpl->read_file($config->{'templates'}->{'copydir'});
672
673 $tpl->fillin('FILE',encode_html($virtual));
674 $tpl->fillin('DIR',encode_html($dir));
675 $tpl->fillin('DIR_URL',escape($dir));
676 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
677 $tpl->fillin('SCRIPT',$script);
678
679 my $output = header(-type => 'text/html');
680 $output .= $tpl->get_template;
681
682 return \$output;
683 }
684 else
685 {
686 my $tpl = new Template;
687 $tpl->read_file($config->{'templates'}->{'copyfile'});
688
689 $tpl->fillin('FILE',encode_html($virtual));
690 $tpl->fillin('DIR',encode_html($dir));
691 $tpl->fillin('DIR_URL',escape($dir));
692 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
693 $tpl->fillin('SCRIPT',$script);
694
695 my $output = header(-type => 'text/html');
696 $output .= $tpl->get_template;
697
698 return \$output;
699 }
700 }
701 }
702
703 # exec_rename()
704 #
705 # Rename/move a file and return to directory view
706 #
707 # Params: 1. Reference to user input hash
708 # 2. Reference to config hash
709 #
710 # Return: Output of the command (Scalar Reference)
711
712 sub exec_rename($$)
713 {
714 my ($data,$config) = @_;
715 my $physical = $data->{'physical'};
716 my $virtual = $data->{'virtual'};
717 my $dir = upper_path($virtual);
718 my $new_physical = $data->{'new_physical'};
719
720 return error($config->{'errors'}->{'rename_root'},'/') if($virtual eq '/');
721 return error($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path($physical));
722
723 if($new_physical)
724 {
725 my $new_virtual = multi_string($data->{'new_virtual'});
726 my $new_dir = upper_path($new_virtual->{'normal'});
727
728 if(-e $new_physical)
729 {
730 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical && not -l $new_physical);
731 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual}) unless(-w $new_physical);
732
733 if(not $data->{'cgi'}->param('confirmed'))
734 {
735 my $tpl = new Template;
736 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
737
738 $tpl->fillin('FILE',encode_html($virtual));
739 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
740 $tpl->fillin('NEW_FILENAME',file_name($new_virtual->{'html'}));
741 $tpl->fillin('NEW_DIR',encode_html($new_dir));
742 $tpl->fillin('DIR',encode_html($dir));
743
744 $tpl->fillin('COMMAND','rename');
745 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
746 $tpl->fillin('SCRIPT',$script);
747
748 my $output = header(-type => 'text/html');
749 $output .= $tpl->get_template;
750
751 return \$output;
752 }
753 }
754
755 move($physical,$new_physical) or return error($config->{'errors'}->{'rename_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
756 return devedit_reload({command => 'show', file => $new_dir});
757 }
758 else
759 {
760 my $tpl = new Template;
761 $tpl->read_file($config->{'templates'}->{'renamefile'});
762
763 $tpl->fillin('FILE',encode_html($virtual));
764 $tpl->fillin('DIR',encode_html($dir));
765 $tpl->fillin('DIR_URL',escape($dir));
766 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
767 $tpl->fillin('SCRIPT',$script);
768
769 my $output = header(-type => 'text/html');
770 $output .= $tpl->get_template;
771
772 return \$output;
773 }
774 }
775
776 # exec_remove()
777 #
778 # Remove a file or a directory and return to directory view
779 #
780 # Params: 1. Reference to user input hash
781 # 2. Reference to config hash
782 #
783 # Return: Output of the command (Scalar Reference)
784
785 sub exec_remove($$)
786 {
787 my ($data,$config) = @_;
788 my $physical = $data->{'physical'};
789 my $virtual = $data->{'virtual'};
790 my $dir = upper_path($virtual);
791
792 return error($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
793 return error($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path($physical));
794
795 if(-d $physical && not -l $physical)
796 {
797 # Remove a directory
798
799 if($data->{'cgi'}->param('confirmed'))
800 {
801 rmtree($physical);
802 return devedit_reload({command => 'show', file => $dir});
803 }
804 else
805 {
806 my $tpl = new Template;
807 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
808
809 $tpl->fillin('DIR',encode_html($virtual));
810 $tpl->fillin('DIR_URL',escape($virtual));
811 $tpl->fillin('UPPER_DIR',encode_html($dir));
812 $tpl->fillin('UPPER_DIR_URL',escape($dir));
813 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
814 $tpl->fillin('SCRIPT',$script);
815
816 my $output = header(-type => 'text/html');
817 $output .= $tpl->get_template;
818
819 return \$output;
820 }
821 }
822 else
823 {
824 # Remove a file
825
826 if($data->{'cgi'}->param('confirmed'))
827 {
828 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},$dir,{FILE => $virtual});
829 return devedit_reload({command => 'show', file => $dir});
830 }
831 else
832 {
833 my $tpl = new Template;
834 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
835
836 $tpl->fillin('FILE',encode_html($virtual));
837 $tpl->fillin('FILE_URL',escape($virtual));
838 $tpl->fillin('DIR',encode_html($dir));
839 $tpl->fillin('DIR_URL',escape($dir));
840 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
841 $tpl->fillin('SCRIPT',$script);
842
843 my $output = header(-type => 'text/html');
844 $output .= $tpl->get_template;
845
846 return \$output;
847 }
848 }
849 }
850
851 # exec_chprop()
852 #
853 # Change the mode and the group of a file or a directory
854 #
855 # Params: 1. Reference to user input hash
856 # 2. Reference to config hash
857 #
858 # Return: Output of the command (Scalar Reference)
859
860 sub exec_chprop($$)
861 {
862 my ($data,$config) = @_;
863 my $physical = $data->{'physical'};
864 my $virtual = $data->{'virtual'};
865 my $dir = upper_path($virtual);
866
867 return error($config->{'errors'}->{'no_users'},$dir,{FILE => encode_html($virtual)}) unless($users);
868 return error($config->{'errors'}->{'chprop_root'},'/') if($virtual eq '/');
869 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => encode_html($virtual)}) unless(-o $physical);
870 return error($config->{'errors'}->{'chprop_link'},$dir) if(-l $physical);
871
872 my $cgi = $data->{'cgi'};
873 my $mode = $cgi->param('mode');
874 my $group = $cgi->param('group');
875
876 if($mode || $group)
877 {
878 if($mode)
879 {
880 # Change the mode
881
882 return error($config->{'errors'}->{'invalid_mode'},$dir) unless($mode =~ /^[0-7]{3,}$/);
883 chmod(oct($mode),$physical);
884 }
885
886 if($group)
887 {
888 # Change the group using the `chgrp` system command
889
890 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => encode_html($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
891 system('chgrp',$group,$physical);
892 }
893
894 return devedit_reload({command => 'show', file => $dir});
895 }
896 else
897 {
898 # Display the form
899
900 my @stat = stat($physical);
901 my $mode = $stat[2];
902 my $gid = $stat[5];
903
904 my $tpl = new Template;
905 $tpl->read_file($config->{'templates'}->{'chprop'});
906
907 # Insert file properties into the template
908
909 $tpl->fillin('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
910 $tpl->fillin('MODE_STRING',mode_string($mode));
911 $tpl->fillin('GID',$gid);
912
913 if(my $group = getgrgid($gid))
914 {
915 $tpl->fillin('GROUP',encode_html($group));
916 $tpl->parse_if_block('group_detected',1);
917 }
918 else
919 {
920 $tpl->parse_if_block('group_detected',0);
921 }
922
923 # Insert other information
924
925 $tpl->fillin('FILE',encode_html($virtual));
926 $tpl->fillin('FILE_URL',escape($virtual));
927 $tpl->fillin('DIR',encode_html($dir));
928 $tpl->fillin('DIR_URL',escape($dir));
929 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
930 $tpl->fillin('SCRIPT',$script);
931
932 my $output = header(-type => 'text/html');
933 $output .= $tpl->get_template;
934
935 return \$output;
936 }
937 }
938
939 # exec_about()
940 #
941 # Display some information about Dev-Editor
942 #
943 # Params: 1. Reference to user input hash
944 # 2. Reference to config hash
945 #
946 # Return: Output of the command (Scalar Reference)
947
948 sub exec_about($$)
949 {
950 my ($data,$config) = @_;
951
952 my $tpl = new Template;
953 $tpl->read_file($config->{'templates'}->{'about'});
954
955 $tpl->fillin('SCRIPT',$script);
956
957 # Dev-Editor's version number
958
959 $tpl->fillin('VERSION',$data->{'version'});
960
961 # Some path information
962
963 $tpl->fillin('SCRIPT_PHYS',encode_html($ENV{'SCRIPT_FILENAME'}));
964 $tpl->fillin('CONFIG_PATH',encode_html($data->{'configfile'}));
965 $tpl->fillin('FILE_ROOT', encode_html($config->{'fileroot'}));
966 $tpl->fillin('HTTP_ROOT', encode_html($config->{'httproot'}));
967
968 # Perl
969
970 $tpl->fillin('PERL_PROG',encode_html($^X));
971 $tpl->fillin('PERL_VER', sprintf('%vd',$^V));
972
973 # Information about the server
974
975 $tpl->fillin('HTTPD',encode_html($ENV{'SERVER_SOFTWARE'}));
976 $tpl->fillin('OS', encode_html($^O));
977 $tpl->fillin('TIME', encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime : localtime)));
978
979 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
980
981 # Process information
982
983 $tpl->fillin('PID',$$);
984
985 # The following information is only available on systems supporting
986 # users and groups
987
988 if($users)
989 {
990 # Dev-Editor is running on a system which allows users and groups
991 # So we display the user and the group of our process
992
993 my $uid = POSIX::getuid;
994 my $gid = POSIX::getgid;
995
996 $tpl->parse_if_block('users',1);
997
998 # IDs of user and group
999
1000 $tpl->fillin('UID',$uid);
1001 $tpl->fillin('GID',$gid);
1002
1003 # Names of user and group
1004
1005 if(my $user = getpwuid($uid))
1006 {
1007 $tpl->fillin('USER',encode_html($user));
1008 $tpl->parse_if_block('user_detected',1);
1009 }
1010 else
1011 {
1012 $tpl->parse_if_block('user_detected',0);
1013 }
1014
1015 if(my $group = getgrgid($gid))
1016 {
1017 $tpl->fillin('GROUP',encode_html($group));
1018 $tpl->parse_if_block('group_detected',1);
1019 }
1020 else
1021 {
1022 $tpl->parse_if_block('group_detected',0);
1023 }
1024
1025 # Process umask
1026
1027 $tpl->fillin('UMASK',sprintf('%04o',umask));
1028 }
1029 else
1030 {
1031 $tpl->parse_if_block('users',0);
1032 }
1033
1034 my $output = header(-type => 'text/html');
1035 $output .= $tpl->get_template;
1036
1037 return \$output;
1038 }
1039
1040 # it's true, baby ;-)
1041
1042 1;
1043
1044 #
1045 ### End ###

patrick-canterino.de