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

patrick-canterino.de