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

patrick-canterino.de