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

patrick-canterino.de