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

patrick-canterino.de