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

patrick-canterino.de