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

patrick-canterino.de