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

patrick-canterino.de