]> git.p6c8.net - devedit.git/blob - modules/Command.pm
- Do not write new file content if the user did not change it
[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 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);
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
366 my $md5 = new Digest::MD5;
367 $md5->addfile(*FILE);
368
369 my $md5file = $md5->hexdigest;
370 my $md5data = md5_hex($content);
371
372 if($md5file ne $md5sum && $md5data ne $md5file && 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',$md5file);
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 if($md5data ne $md5file)
400 {
401 seek(FILE,0,0);
402 truncate(FILE,0);
403
404 print FILE $content;
405 }
406
407 $output = devedit_reload({command => 'show', file => $dir});
408
409 #return error($config->{'errors'}->{'edit_failed'},$dir,{FILE => $virtual});
410 }
411
412 close(FILE);
413
414 return $output;
415 }
416
417 return devedit_reload({command => 'beginedit', file => $virtual});
418 }
419
420 # exec_mkfile()
421 #
422 # Create a file and return to directory view
423 #
424 # Params: 1. Reference to user input hash
425 # 2. Reference to config hash
426 #
427 # Return: Output of the command (Scalar Reference)
428
429 sub exec_mkfile($$)
430 {
431 my ($data,$config) = @_;
432 my $new_physical = $data->{'new_physical'};
433 my $new_virtual = $data->{'new_virtual'};
434 my $dir = upper_path($new_virtual);
435 $new_virtual = encode_entities($new_virtual);
436
437 if($new_physical)
438 {
439 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
440
441 file_create($new_physical) or return error($config->{'errors'}->{'mkfile_failed'},$dir,{FILE => $new_virtual});
442 return devedit_reload({command => 'show', file => $dir});
443 }
444 else
445 {
446 my $tpl = new Template;
447 $tpl->read_file($config->{'templates'}->{'mkfile'});
448
449 $tpl->fillin('DIR','/');
450 $tpl->fillin('SCRIPT',$script);
451
452 my $output = header(-type => 'text/html');
453 $output .= $tpl->get_template;
454
455 return \$output;
456 }
457 }
458
459 # exec_mkdir()
460 #
461 # Create a directory and return to directory view
462 #
463 # Params: 1. Reference to user input hash
464 # 2. Reference to config hash
465 #
466 # Return: Output of the command (Scalar Reference)
467
468 sub exec_mkdir($$)
469 {
470 my ($data,$config) = @_;
471 my $new_physical = $data->{'new_physical'};
472 my $new_virtual = $data->{'new_virtual'};
473 my $dir = upper_path($new_virtual);
474 $new_virtual = encode_entities($new_virtual);
475
476 if($new_physical)
477 {
478 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
479
480 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
481 return devedit_reload({command => 'show', file => $dir});
482 }
483 else
484 {
485 my $tpl = new Template;
486 $tpl->read_file($config->{'templates'}->{'mkdir'});
487
488 $tpl->fillin('DIR','/');
489 $tpl->fillin('SCRIPT',$script);
490
491 my $output = header(-type => 'text/html');
492 $output .= $tpl->get_template;
493
494 return \$output;
495 }
496 }
497
498 # exec_upload()
499 #
500 # Process a file upload
501 #
502 # Params: 1. Reference to user input hash
503 # 2. Reference to config hash
504 #
505 # Return: Output of the command (Scalar Reference)
506
507 sub exec_upload($$)
508 {
509 my ($data,$config) = @_;
510 my $physical = $data->{'physical'};
511 my $virtual = $data->{'virtual'};
512 my $cgi = $data->{'cgi'};
513
514 return error($config->{'errors'}->{'no_directory'},upper_path($virtual),{FILE => $virtual}) unless(-d $physical && not -l $physical);
515 return error($config->{'errors'}->{'dir_no_create'},$virtual,{DIR => $virtual}) unless(-w $physical);
516
517 if(my $uploaded_file = $cgi->param('uploaded_file'))
518 {
519 # Process file upload
520
521 my $filename = file_name($uploaded_file);
522 my $file_phys = $physical.'/'.$filename;
523 my $file_virt = $virtual.$filename;
524
525 if(-e $file_phys)
526 {
527 return error($config->{'errors'}->{'link_replace'},$virtual) if(-l $file_phys);
528 return error($config->{'errors'}->{'dir_replace'},$virtual) if(-d $file_phys);
529 return error($config->{'errors'}->{'exist_no_write'},$virtual,{FILE => $file_virt}) unless(-w $file_phys);
530 return error($config->{'errors'}->{'file_exists'},$virtual,{FILE => $file_virt}) unless($cgi->param('overwrite'));
531 }
532
533 my $ascii = $cgi->param('ascii');
534 my $handle = $cgi->upload('uploaded_file');
535
536 return error($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
537
538 # Read transferred file and write it to disk
539
540 read($handle, my $data, -s $handle);
541 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
542 file_save($file_phys,\$data,not $ascii) or return error($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE => $file_virt});
543
544 return devedit_reload({command => 'show', file => $virtual});
545 }
546 else
547 {
548 my $tpl = new Template;
549 $tpl->read_file($config->{'templates'}->{'upload'});
550
551 $tpl->fillin('DIR',$virtual);
552 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
553 $tpl->fillin('SCRIPT',$script);
554
555 my $output = header(-type => 'text/html');
556 $output .= $tpl->get_template;
557
558 return \$output;
559 }
560 }
561
562 # exec_copy()
563 #
564 # Copy a file and return to directory view
565 #
566 # Params: 1. Reference to user input hash
567 # 2. Reference to config hash
568 #
569 # Return: Output of the command (Scalar Reference)
570
571 sub exec_copy($$)
572 {
573 my ($data,$config) = @_;
574 my $physical = $data->{'physical'};
575 my $virtual = encode_entities($data->{'virtual'});
576 my $dir = upper_path($virtual);
577 my $new_physical = $data->{'new_physical'};
578
579 return error($config->{'errors'}->{'link_copy'},$dir) if(-l $physical);
580 return error($config->{'errors'}->{'dir_copy'},$dir) if(-d $physical);
581 return error($config->{'errors'}->{'no_copy'},$dir) unless(-r $physical);
582
583 if($new_physical)
584 {
585 my $new_virtual = $data->{'new_virtual'};
586 my $new_dir = upper_path($new_virtual);
587 $new_virtual = encode_entities($new_virtual);
588
589 if(-e $new_physical)
590 {
591 return error($config->{'errors'}->{'link_replace'},$new_dir) if(-l $new_physical);
592 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical);
593 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual}) unless(-w $new_physical);
594
595 if(not $data->{'cgi'}->param('confirmed'))
596 {
597 my $tpl = new Template;
598 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
599
600 $tpl->fillin('FILE',$virtual);
601 $tpl->fillin('NEW_FILE',$new_virtual);
602 $tpl->fillin('NEW_FILENAME',file_name($new_virtual));
603 $tpl->fillin('NEW_DIR',$new_dir);
604 $tpl->fillin('DIR',$dir);
605
606 $tpl->fillin('COMMAND','copy');
607 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
608 $tpl->fillin('SCRIPT',$script);
609
610 my $output = header(-type => 'text/html');
611 $output .= $tpl->get_template;
612
613 return \$output;
614 }
615 }
616
617 copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => $virtual, NEW_FILE => $new_virtual});
618 return devedit_reload({command => 'show', file => $new_dir});
619 }
620 else
621 {
622 my $tpl = new Template;
623 $tpl->read_file($config->{'templates'}->{'copyfile'});
624
625 $tpl->fillin('FILE',$virtual);
626 $tpl->fillin('DIR',$dir);
627 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
628 $tpl->fillin('SCRIPT',$script);
629
630 my $output = header(-type => 'text/html');
631 $output .= $tpl->get_template;
632
633 return \$output;
634 }
635 }
636
637 # exec_rename()
638 #
639 # Rename/move a file and return to directory view
640 #
641 # Params: 1. Reference to user input hash
642 # 2. Reference to config hash
643 #
644 # Return: Output of the command (Scalar Reference)
645
646 sub exec_rename($$)
647 {
648 my ($data,$config) = @_;
649 my $physical = $data->{'physical'};
650 my $virtual = $data->{'virtual'};
651 my $dir = upper_path($virtual);
652 my $new_physical = $data->{'new_physical'};
653
654 return error($config->{'errors'}->{'rename_root'},'/') if($virtual eq '/');
655 return error($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path($physical));
656
657 if($new_physical)
658 {
659 my $new_virtual = $data->{'new_virtual'};
660 my $new_dir = upper_path($new_virtual);
661 $new_virtual = encode_entities($new_virtual);
662
663 if(-e $new_physical)
664 {
665 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical && not -l $new_physical);
666 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual}) unless(-w $new_physical);
667
668 if(not $data->{'cgi'}->param('confirmed'))
669 {
670 my $tpl = new Template;
671 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
672
673 $tpl->fillin('FILE',$virtual);
674 $tpl->fillin('NEW_FILE',$new_virtual);
675 $tpl->fillin('NEW_FILENAME',file_name($new_virtual));
676 $tpl->fillin('NEW_DIR',$new_dir);
677 $tpl->fillin('DIR',$dir);
678
679 $tpl->fillin('COMMAND','rename');
680 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
681 $tpl->fillin('SCRIPT',$script);
682
683 my $output = header(-type => 'text/html');
684 $output .= $tpl->get_template;
685
686 return \$output;
687 }
688 }
689
690 move($physical,$new_physical) or return error($config->{'errors'}->{'rename_failed'},$dir,{FILE => $virtual, NEW_FILE => $new_virtual});
691 return devedit_reload({command => 'show', file => $new_dir});
692 }
693 else
694 {
695 my $tpl = new Template;
696 $tpl->read_file($config->{'templates'}->{'renamefile'});
697
698 $tpl->fillin('FILE',$virtual);
699 $tpl->fillin('DIR',$dir);
700 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
701 $tpl->fillin('SCRIPT',$script);
702
703 my $output = header(-type => 'text/html');
704 $output .= $tpl->get_template;
705
706 return \$output;
707 }
708 }
709
710 # exec_remove()
711 #
712 # Remove a file or a directory and return to directory view
713 #
714 # Params: 1. Reference to user input hash
715 # 2. Reference to config hash
716 #
717 # Return: Output of the command (Scalar Reference)
718
719 sub exec_remove($$)
720 {
721 my ($data,$config) = @_;
722 my $physical = $data->{'physical'};
723 my $virtual = $data->{'virtual'};
724 my $dir = upper_path($virtual);
725
726 return error($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
727 return error($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path($physical));
728
729 if(-d $physical && not -l $physical)
730 {
731 # Remove a directory
732
733 if($data->{'cgi'}->param('confirmed'))
734 {
735 rmtree($physical);
736 return devedit_reload({command => 'show', file => $dir});
737 }
738 else
739 {
740 my $tpl = new Template;
741 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
742
743 $tpl->fillin('DIR',$virtual);
744 $tpl->fillin('UPPER_DIR',$dir);
745 $tpl->fillin('URL',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 else
755 {
756 # Remove a file
757
758 if($data->{'cgi'}->param('confirmed'))
759 {
760 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},$dir,{FILE => $virtual});
761 return devedit_reload({command => 'show', file => $dir});
762 }
763 else
764 {
765 my $tpl = new Template;
766 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
767
768 $tpl->fillin('FILE',$virtual);
769 $tpl->fillin('DIR',$dir);
770 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
771 $tpl->fillin('SCRIPT',$script);
772
773 my $output = header(-type => 'text/html');
774 $output .= $tpl->get_template;
775
776 return \$output;
777 }
778 }
779 }
780
781 # exec_chprop()
782 #
783 # Change the mode and the group of a file or a directory
784 #
785 # Params: 1. Reference to user input hash
786 # 2. Reference to config hash
787 #
788 # Return: Output of the command (Scalar Reference)
789
790 sub exec_chprop($$)
791 {
792 my ($data,$config) = @_;
793 my $physical = $data->{'physical'};
794 my $virtual = $data->{'virtual'};
795 my $dir = upper_path($virtual);
796
797 return error($config->{'errors'}->{'no_users'},$dir,{FILE => $virtual}) unless($users);
798 return error($config->{'errors'}->{'chprop_root'},'/') if($virtual eq '/');
799 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => $virtual}) unless(-o $physical);
800 return error($config->{'errors'}->{'chprop_link'},$dir) if(-l $physical);
801
802 my $cgi = $data->{'cgi'};
803 my $mode = $cgi->param('mode');
804 my $group = $cgi->param('group');
805
806 if($mode || $group)
807 {
808 if($mode)
809 {
810 # Change the mode
811
812 chmod(oct($mode),$physical);
813 }
814
815 if($group)
816 {
817 # Change the group using the `chgrp` system command
818
819 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => encode_entities($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
820 system('chgrp',$group,$physical);
821 }
822
823 return devedit_reload({command => 'show', file => $dir});
824 }
825 else
826 {
827 # Display the form
828
829 my @stat = stat($physical);
830 my $mode = $stat[2];
831 my $gid = $stat[5];
832
833 my $tpl = new Template;
834 $tpl->read_file($config->{'templates'}->{'chprop'});
835
836 # Insert file properties into the template
837
838 $tpl->fillin('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
839 $tpl->fillin('MODE_STRING',mode_string($mode));
840 $tpl->fillin('GID',$gid);
841
842 if(my $group = getgrgid($gid))
843 {
844 $tpl->fillin('GROUP',encode_entities($group));
845 $tpl->parse_if_block('group_detected',1);
846 }
847 else
848 {
849 $tpl->parse_if_block('group_detected',0);
850 }
851
852 # Insert other information
853
854 $tpl->fillin('FILE',$virtual);
855 $tpl->fillin('DIR',$dir);
856 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
857 $tpl->fillin('SCRIPT',$script);
858
859 my $output = header(-type => 'text/html');
860 $output .= $tpl->get_template;
861
862 return \$output;
863 }
864 }
865
866 # exec_about()
867 #
868 # Display some information about Dev-Editor
869 #
870 # Params: 1. Reference to user input hash
871 # 2. Reference to config hash
872 #
873 # Return: Output of the command (Scalar Reference)
874
875 sub exec_about($$)
876 {
877 my ($data,$config) = @_;
878
879 my $tpl = new Template;
880 $tpl->read_file($config->{'templates'}->{'about'});
881
882 $tpl->fillin('SCRIPT',$script);
883
884 # Dev-Editor's version number
885
886 $tpl->fillin('VERSION',$data->{'version'});
887
888 # Some path information
889
890 $tpl->fillin('SCRIPT_PHYS',encode_entities($ENV{'SCRIPT_FILENAME'}));
891 $tpl->fillin('CONFIG_PATH',encode_entities($data->{'configfile'}));
892 $tpl->fillin('FILE_ROOT', encode_entities($config->{'fileroot'}));
893 $tpl->fillin('HTTP_ROOT', encode_entities($config->{'httproot'}));
894
895 # Perl
896
897 $tpl->fillin('PERL_PROG',encode_entities($^X));
898 $tpl->fillin('PERL_VER', sprintf('%vd',$^V));
899
900 # Information about the server
901
902 $tpl->fillin('HTTPD',encode_entities($ENV{'SERVER_SOFTWARE'}));
903 $tpl->fillin('OS', encode_entities($^O));
904 $tpl->fillin('TIME', encode_entities(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime : localtime)));
905
906 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
907
908 # Process information
909
910 $tpl->fillin('PID',$$);
911
912 # The following information is only available on systems supporting
913 # users and groups
914
915 if($users)
916 {
917 # Dev-Editor is running on a system which allows users and groups
918 # So we display the user and the group of our process
919
920 my $uid = POSIX::getuid;
921 my $gid = POSIX::getgid;
922
923 $tpl->parse_if_block('users',1);
924
925 # ID's of user and group
926
927 $tpl->fillin('UID',$uid);
928 $tpl->fillin('GID',$gid);
929
930 # Names of user and group
931
932 if(my $user = getpwuid($uid))
933 {
934 $tpl->fillin('USER',encode_entities($user));
935 $tpl->parse_if_block('user_detected',1);
936 }
937 else
938 {
939 $tpl->parse_if_block('user_detected',0);
940 }
941
942 if(my $group = getgrgid($gid))
943 {
944 $tpl->fillin('GROUP',encode_entities($group));
945 $tpl->parse_if_block('group_detected',1);
946 }
947 else
948 {
949 $tpl->parse_if_block('group_detected',0);
950 }
951
952 # Process umask
953
954 $tpl->fillin('UMASK',sprintf('%04o',umask));
955 }
956 else
957 {
958 $tpl->parse_if_block('users',0);
959 }
960
961 my $output = header(-type => 'text/html');
962 $output .= $tpl->get_template;
963
964 return \$output;
965 }
966
967 # it's true, baby ;-)
968
969 1;
970
971 #
972 ### End ###

patrick-canterino.de