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

patrick-canterino.de