]> git.p6c8.net - devedit.git/blob - modules/Command.pm
In some parts of file "Command.pm", make use of new methods defined by the
[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-05-09
10 #
11 # Copyright (C) 1999-2000 Roland Bluethgen, Frank Schoenmann
12 # Copyright (C) 2003-2009 Patrick Canterino
13 # All Rights Reserved.
14 #
15 # This file can be distributed and/or modified under the terms of
16 # of the Artistic License 1.0 (see also the LICENSE file found at
17 # the top level of the Dev-Editor distribution).
18 #
19
20 use strict;
21
22 use vars qw(@EXPORT);
23
24 use Fcntl;
25 use File::Access;
26 use File::Copy;
27 use File::Path;
28
29 use Digest::MD5 qw(md5_hex);
30 use POSIX qw(strftime);
31 use Tool;
32
33 use CGI qw(header
34 escape);
35
36 use Output;
37 use Template;
38
39 my $script = encode_html($ENV{'SCRIPT_NAME'});
40 my $users = eval('getpwuid(0)') && eval('getgrgid(0)');
41
42 my %dispatch = ('show' => \&exec_show,
43 'beginedit' => \&exec_beginedit,
44 'endedit' => \&exec_endedit,
45 'mkdir' => \&exec_mkdir,
46 'mkfile' => \&exec_mkfile,
47 'upload' => \&exec_upload,
48 'copy' => \&exec_copy,
49 'rename' => \&exec_rename,
50 'remove' => \&exec_remove,
51 'remove_multi' => \&exec_remove_multi,
52 'chprop' => \&exec_chprop,
53 'about' => \&exec_about
54 );
55
56 ### Export ###
57
58 use base qw(Exporter);
59
60 @EXPORT = qw(exec_command);
61
62 # exec_command()
63 #
64 # Execute the specified command
65 #
66 # Params: 1. Command to execute
67 # 2. Reference to user input hash
68 # 3. Reference to config hash
69 #
70 # Return: Output of the command (Scalar Reference)
71
72 sub exec_command($$$)
73 {
74 my ($command,$data,$config) = @_;
75
76 foreach(keys(%dispatch))
77 {
78 if(lc($_) eq lc($command))
79 {
80 my $output = &{$dispatch{$_}}($data,$config);
81 return $output;
82 }
83 }
84
85 return error($config->{'errors'}->{'command_unknown'},'/',{COMMAND => encode_html($command)});
86 }
87
88 # exec_show()
89 #
90 # View a directory or a file
91 #
92 # Params: 1. Reference to user input hash
93 # 2. Reference to config hash
94 #
95 # Return: Output of the command (Scalar Reference)
96
97 sub exec_show($$)
98 {
99 my ($data,$config) = @_;
100 my $physical = $data->{'physical'};
101 my $virtual = $data->{'virtual'};
102 my $upper_path = multi_string(upper_path($virtual));
103
104 my $tpl = new Template;
105
106 if(-d $physical && not -l $physical)
107 {
108 # Create directory listing
109
110 return error($config->{'errors'}->{'no_dir_access'},$upper_path->{'normal'}) unless(-r $physical && -x $physical);
111
112 my $direntries = dir_read($physical);
113 return error($config->{'errors'}->{'dir_read_failed'},$upper_path->{'normal'},{DIR => encode_html($virtual)}) unless($direntries);
114
115 my $files = $direntries->{'files'};
116 my $dirs = $direntries->{'dirs'};
117
118 my $dirlist = '';
119
120 my $count = 0;
121
122 my $filter1 = $data->{'cgi'}->param('filter') || '*'; # The real wildcard
123 my $filter2 = ($filter1 && $filter1 ne '*') ? $filter1 : ''; # Wildcard for output
124
125 # Create the link to the upper directory
126 # (only if the current directory is not the root directory)
127
128 unless($virtual eq '/')
129 {
130 $count++;
131
132 my @stat = stat($physical.'/..');
133
134 my $udtpl = new Template;
135 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
136
137 $udtpl->fillin('UPPER_DIR',$upper_path->{'html'});
138 $udtpl->fillin('UPPER_DIR_URL',$upper_path->{'url'});
139 $udtpl->fillin('DATE',encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
140
141 $dirlist .= $udtpl->get_template;
142 }
143
144 # Directories
145
146 foreach my $dir(@$dirs)
147 {
148 next if($config->{'hide_dot_files'} && substr($dir,0,1) eq '.');
149 next unless(dos_wildcard_match($filter1,$dir));
150
151 $count++;
152
153 my $phys_path = $physical.'/'.$dir;
154 my $virt_path = multi_string($virtual.$dir.'/');
155
156 my @stat = stat($phys_path);
157
158 my $dtpl = new Template;
159 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
160
161 $dtpl->fillin('DIR',$virt_path->{'html'});
162 $dtpl->fillin('DIR_URL',$virt_path->{'url'});
163 $dtpl->fillin('DIR_NAME',encode_html($dir));
164 $dtpl->fillin('DATE',encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
165 $dtpl->fillin('URL',equal_url(encode_html($config->{'httproot'}),$virt_path->{'html'}));
166
167 $dtpl->parse_if_block('forbidden',is_forbidden_file($config->{'forbidden'},$virt_path->{'normal'}));
168 $dtpl->parse_if_block('readable',-r $phys_path && -x $phys_path);
169 $dtpl->parse_if_block('users',$users && -o $phys_path);
170 $dtpl->parse_if_block('even',($count % 2) == 0);
171
172 $dirlist .= $dtpl->get_template;
173 }
174
175 # Files
176
177 foreach my $file(@$files)
178 {
179 next if($config->{'hide_dot_files'} && substr($file,0,1) eq '.');
180 next unless(dos_wildcard_match($filter1,$file));
181
182 $count++;
183
184 my $phys_path = $physical.'/'.$file;
185 my $virt_path = multi_string($virtual.$file);
186
187 my @stat = lstat($phys_path);
188 my $too_large = $config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'};
189
190 my $ftpl = new Template;
191 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
192
193 $ftpl->fillin('FILE',$virt_path->{'html'});
194 $ftpl->fillin('FILE_URL',$virt_path->{'url'});
195 $ftpl->fillin('FILE_NAME',encode_html($file));
196 $ftpl->fillin('FILE_URL',$virt_path->{'url'});
197 $ftpl->fillin('SIZE',$stat[7]);
198 $ftpl->fillin('DATE',encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
199 $ftpl->fillin('URL',equal_url(encode_html($config->{'httproot'}),$virt_path->{'html'}));
200
201 $ftpl->parse_if_block('link',-l $phys_path);
202 $ftpl->parse_if_block('readable',-r $phys_path);
203 $ftpl->parse_if_block('writeable',-w $phys_path);
204 $ftpl->parse_if_block('binary',-B $phys_path);
205
206 $ftpl->parse_if_block('forbidden',is_forbidden_file($config->{'forbidden'},$virt_path->{'normal'}));
207 $ftpl->parse_if_block('viewable',(-r $phys_path && -T $phys_path && not $too_large) || -l $phys_path);
208 $ftpl->parse_if_block('editable',(-r $phys_path && -w $phys_path && -T $phys_path && not $too_large) && not -l $phys_path);
209
210 $ftpl->parse_if_block('too_large',$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
211
212 $ftpl->parse_if_block('users',$users && -o $phys_path);
213
214 $ftpl->parse_if_block('even',($count % 2) == 0);
215
216 $dirlist .= $ftpl->get_template;
217 }
218
219 $tpl->read_file($config->{'templates'}->{'dirlist'});
220
221 $tpl->fillin('DIRLIST',$dirlist);
222 $tpl->fillin('DIR',encode_html($virtual));
223 $tpl->fillin('DIR_URL',escape($virtual));
224 $tpl->fillin('SCRIPT',$script);
225 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
226
227 $tpl->fillin('FILTER',encode_html($filter2));
228 $tpl->fillin('FILTER_URL',escape($filter2));
229
230 $tpl->parse_if_block('empty',$dirlist eq '');
231 $tpl->parse_if_block('dir_writeable',-w $physical);
232 $tpl->parse_if_block('filter',$filter2);
233 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
234 }
235 elsif(-l $physical)
236 {
237 # Show the target of a symbolic link
238
239 my $link_target = readlink($physical);
240
241 $tpl->read_file($config->{'templates'}->{'viewlink'});
242
243 $tpl->fillin('FILE',encode_html($virtual));
244 $tpl->fillin('DIR',$upper_path->{'html'});
245 $tpl->fillin('DIR_URL',$upper_path->{'url'});
246 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
247 $tpl->fillin('SCRIPT',$script);
248
249 $tpl->fillin('LINK_TARGET',encode_html($link_target));
250 }
251 else
252 {
253 # View a file
254
255 return error($config->{'errors'}->{'no_view'},$upper_path->{'normal'}) unless(-r $physical);
256
257 # Check on binary files
258 # We have to do it in this way or empty files will be recognized
259 # as binary files
260
261 return error($config->{'errors'}->{'binary_file'},$upper_path->{'normal'}) unless(-T $physical);
262
263 # Is the file too large?
264
265 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'});
266
267 # View the file
268
269 my $content = file_read($physical);
270 $$content =~ s/\015\012|\012|\015/\n/g;
271
272 $tpl->read_file($config->{'templates'}->{'viewfile'});
273
274 $tpl->fillin('FILE',encode_html($virtual));
275 $tpl->fillin('FILE_URL',escape($virtual));
276 $tpl->fillin('DIR',$upper_path->{'html'});
277 $tpl->fillin('DIR_URL',$upper_path->{'url'});
278 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
279 $tpl->fillin('SCRIPT',$script);
280
281 $tpl->parse_if_block('editable',-w $physical);
282
283 $tpl->fillin('CONTENT',encode_html($$content));
284 }
285
286 my $output = header(-type => 'text/html');
287 $output .= $tpl->get_template;
288
289 return \$output;
290 }
291
292 # exec_beginedit
293 #
294 # Lock a file and display a form to edit it
295 #
296 # Params: 1. Reference to user input hash
297 # 2. Reference to config hash
298 #
299 # Return: Output of the command (Scalar Reference)
300
301 sub exec_beginedit($$)
302 {
303 my ($data,$config) = @_;
304 my $physical = $data->{'physical'};
305 my $virtual = $data->{'virtual'};
306 my $dir = upper_path($virtual);
307
308 return error($config->{'errors'}->{'link_edit'},$dir) if(-l $physical);
309 return error($config->{'errors'}->{'dir_edit'}, $dir) if(-d $physical);
310 return error($config->{'errors'}->{'no_edit'}, $dir) unless(-r $physical && -w $physical);
311
312 # Check on binary files
313
314 return error($config->{'errors'}->{'binary_file'},$dir) unless(-T $physical);
315
316 # Is the file too large?
317
318 return error($config->{'errors'}->{'file_too_large'},$dir,{SIZE => $config->{'max_file_size'}}) if($config->{'max_file_size'} && -s $physical > $config->{'max_file_size'});
319
320 # Show the editing form
321
322 my $content = file_read($physical);
323 my $md5sum = md5_hex($$content);
324 $$content =~ s/\015\012|\012|\015/\n/g;
325
326 my $tpl = new Template;
327 $tpl->read_file($config->{'templates'}->{'editfile'});
328
329 $tpl->fillin('FILE',encode_html($virtual));
330 $tpl->fillin('FILE_URL',escape($virtual));
331 $tpl->fillin('DIR',encode_html($dir));
332 $tpl->fillin('DIR_URL',escape($dir));
333 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
334 $tpl->fillin('SCRIPT',$script);
335 $tpl->fillin('MD5SUM',$md5sum);
336 $tpl->fillin('CONTENT',encode_html($$content));
337
338 $tpl->parse_if_block('error',0);
339
340 my $output = header(-type => 'text/html');
341 $output .= $tpl->get_template;
342
343 return \$output;
344 }
345
346 # exec_endedit()
347 #
348 # Save a file, unlock it and return to directory view
349 #
350 # Params: 1. Reference to user input hash
351 # 2. Reference to config hash
352 #
353 # Return: Output of the command (Scalar Reference)
354
355 sub exec_endedit($$)
356 {
357 my ($data,$config) = @_;
358 my $physical = $data->{'physical'};
359 my $virtual = $data->{'virtual'};
360 my $dir = upper_path($virtual);
361 my $cgi = $data->{'cgi'};
362 my $content = $cgi->param('filecontent');
363 my $md5sum = $cgi->param('md5sum');
364 my $output;
365
366 if(defined $content && $md5sum)
367 {
368 # Normalize newlines
369
370 $content =~ s/\015\012|\012|\015/\n/g;
371
372 if($cgi->param('saveas') && $data->{'new_physical'} ne '' && $data->{'new_virtual'} ne '')
373 {
374 # Create the new filename
375
376 $physical = $data->{'new_physical'};
377 $virtual = $data->{'new_virtual'};
378 }
379
380 return error($config->{'errors'}->{'link_edit'},$dir) if(-l $physical);
381 return error($config->{'errors'}->{'dir_edit'},$dir) if(-d $physical);
382 return error($config->{'errors'}->{'no_edit'},$dir) if(-e $physical && !(-r $physical && -w $physical));
383 return error($config->{'errors'}->{'text_to_binary'},$dir) if(-e $physical && not -T $physical);
384
385 # For technical reasons, we can't use file_save() for
386 # saving the file...
387
388 local *FILE;
389
390 sysopen(FILE,$physical,O_RDWR | O_CREAT) or return error($config->{'errors'}->{'edit_failed'},$dir,{FILE => encode_html($virtual)});
391 file_lock(*FILE,LOCK_EX) or do { close(FILE); return error($config->{'errors'}->{'edit_failed'},$dir,{FILE => encode_html($virtual)}) };
392
393 my $md5 = new Digest::MD5;
394 $md5->addfile(*FILE);
395
396 my $md5file = $md5->hexdigest;
397 my $md5data = md5_hex($content);
398
399 if($md5file ne $md5sum && $md5data ne $md5file && not $cgi->param('saveas'))
400 {
401 # The file changed meanwhile
402
403 my $tpl = new Template;
404 $tpl->read_file($config->{'templates'}->{'editfile'});
405
406 $tpl->fillin('ERROR',$config->{'errors'}->{'edit_file_changed'});
407
408 $tpl->fillin('FILE',encode_html($virtual));
409 $tpl->fillin('FILE_URL',escape($virtual));
410 $tpl->fillin('DIR',encode_html($dir));
411 $tpl->fillin('DIR_URL',escape($dir));
412 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
413 $tpl->fillin('SCRIPT',$script);
414 $tpl->fillin('MD5SUM',$md5file);
415 $tpl->fillin('CONTENT',encode_html($content));
416
417 $tpl->parse_if_block('error',1);
418
419 my $data = header(-type => 'text/html');
420 $data .= $tpl->get_template;
421
422 $output = \$data;
423 }
424 else
425 {
426 if($md5data ne $md5file)
427 {
428 seek(FILE,0,0);
429 truncate(FILE,0);
430
431 print FILE $content;
432 }
433
434 $output = ($cgi->param('continue'))
435 ? devedit_reload({command => 'beginedit', file => $virtual})
436 : devedit_reload({command => 'show', file => $dir});
437 }
438
439 close(FILE);
440
441 return $output;
442 }
443
444 return devedit_reload({command => 'beginedit', file => $virtual});
445 }
446
447 # exec_mkfile()
448 #
449 # Create a file and return to directory view
450 #
451 # Params: 1. Reference to user input hash
452 # 2. Reference to config hash
453 #
454 # Return: Output of the command (Scalar Reference)
455
456 sub exec_mkfile($$)
457 {
458 my ($data,$config) = @_;
459 my $new_physical = $data->{'new_physical'};
460 my $new_virtual = $data->{'new_virtual'};
461 my $dir = upper_path($new_virtual);
462 $new_virtual = encode_html($new_virtual);
463
464 if($new_physical)
465 {
466 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
467
468 file_create($new_physical) or return error($config->{'errors'}->{'mkfile_failed'},$dir,{FILE => $new_virtual});
469 return devedit_reload({command => 'show', file => $dir});
470 }
471 else
472 {
473 my $tpl = new Template;
474 $tpl->read_file($config->{'templates'}->{'mkfile'});
475
476 $tpl->fillin('DIR','/');
477 $tpl->fillin('SCRIPT',$script);
478
479 my $output = header(-type => 'text/html');
480 $output .= $tpl->get_template;
481
482 return \$output;
483 }
484 }
485
486 # exec_mkdir()
487 #
488 # Create a directory and return to directory view
489 #
490 # Params: 1. Reference to user input hash
491 # 2. Reference to config hash
492 #
493 # Return: Output of the command (Scalar Reference)
494
495 sub exec_mkdir($$)
496 {
497 my ($data,$config) = @_;
498 my $new_physical = $data->{'new_physical'};
499 my $new_virtual = $data->{'new_virtual'};
500 my $dir = upper_path($new_virtual);
501 $new_virtual = encode_html($new_virtual);
502
503 if($new_physical)
504 {
505 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
506
507 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
508 return devedit_reload({command => 'show', file => $dir});
509 }
510 else
511 {
512 my $tpl = new Template;
513 $tpl->read_file($config->{'templates'}->{'mkdir'});
514
515 $tpl->fillin('DIR','/');
516 $tpl->fillin('SCRIPT',$script);
517
518 my $output = header(-type => 'text/html');
519 $output .= $tpl->get_template;
520
521 return \$output;
522 }
523 }
524
525 # exec_upload()
526 #
527 # Process a file upload
528 #
529 # Params: 1. Reference to user input hash
530 # 2. Reference to config hash
531 #
532 # Return: Output of the command (Scalar Reference)
533
534 sub exec_upload($$)
535 {
536 my ($data,$config) = @_;
537 my $physical = $data->{'physical'};
538 my $virtual = $data->{'virtual'};
539 my $cgi = $data->{'cgi'};
540
541 return error($config->{'errors'}->{'no_directory'},upper_path($virtual),{FILE => encode_html($virtual)}) unless(-d $physical && not -l $physical);
542 return error($config->{'errors'}->{'dir_no_create'},$virtual,{DIR => encode_html($virtual)}) unless(-w $physical);
543
544 if(my $uploaded_file = $cgi->param('uploaded_file'))
545 {
546 if($cgi->param('remote_file'))
547 {
548 $uploaded_file = $cgi->param('remote_file');
549
550 $uploaded_file =~ s!/!!g;
551 $uploaded_file =~ s!\\!!g;
552 }
553
554 # Process file upload
555
556 my $filename = file_name($uploaded_file);
557 my $file_phys = $physical.'/'.$filename;
558 my $file_virt = encode_html($virtual.$filename);
559
560 if(-e $file_phys)
561 {
562 return error($config->{'errors'}->{'link_replace'},$virtual) if(-l $file_phys);
563 return error($config->{'errors'}->{'dir_replace'},$virtual) if(-d $file_phys);
564 return error($config->{'errors'}->{'exist_no_write'},$virtual,{FILE => $file_virt}) unless(-w $file_phys);
565 return error($config->{'errors'}->{'file_exists'},$virtual,{FILE => $file_virt}) unless($cgi->param('overwrite'));
566 }
567
568 my $ascii = $cgi->param('ascii');
569 my $handle = $cgi->upload('uploaded_file');
570
571 return error($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
572
573 # Read transferred file and write it to disk
574
575 read($handle, my $data, -s $handle);
576 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
577 file_save($file_phys,\$data,not $ascii) or return error($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE => $file_virt});
578
579 return devedit_reload({command => 'show', file => $virtual});
580 }
581 else
582 {
583 my $tpl = new Template;
584 $tpl->read_file($config->{'templates'}->{'upload'});
585
586 $tpl->fillin('DIR',encode_html($virtual));
587 $tpl->fillin('DIR_URL',escape($virtual));
588 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
589 $tpl->fillin('SCRIPT',$script);
590
591 my $output = header(-type => 'text/html');
592 $output .= $tpl->get_template;
593
594 return \$output;
595 }
596 }
597
598 # exec_copy()
599 #
600 # Copy a file and return to directory view
601 #
602 # Params: 1. Reference to user input hash
603 # 2. Reference to config hash
604 #
605 # Return: Output of the command (Scalar Reference)
606
607 sub exec_copy($$)
608 {
609 my ($data,$config) = @_;
610 my $physical = $data->{'physical'};
611 my $virtual = $data->{'virtual'};
612 my $dir = upper_path($virtual);
613 my $new_physical = $data->{'new_physical'};
614
615 return error($config->{'errors'}->{'link_copy'},$dir) if(-l $physical);
616 return error($config->{'errors'}->{'no_copy'},$dir) unless(-r $physical);
617
618 if($new_physical)
619 {
620 my $new_virtual = multi_string($data->{'new_virtual'});
621 my $new_dir = upper_path($new_virtual->{'normal'});
622
623 if(-d $physical)
624 {
625 return error($config->{'errors'}->{'no_copy'},$dir) unless(-x $physical);
626 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual->{'html'}}) if(-e $new_physical);
627 return error($config->{'errors'}->{'dir_copy_self'},$dir) if(index($new_virtual->{'normal'},$virtual) == 0);
628
629 dir_copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
630 return devedit_reload({command => 'show', file => $new_dir});
631 }
632 else
633 {
634 if(-e $new_physical)
635 {
636 return error($config->{'errors'}->{'link_replace'},$new_dir) if(-l $new_physical);
637 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical);
638 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual->{'html'}}) unless(-w $new_physical);
639
640 if(not $data->{'cgi'}->param('confirmed'))
641 {
642 my $tpl = new Template;
643 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
644
645 $tpl->fillin('FILE',encode_html($virtual));
646 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
647 $tpl->fillin('NEW_FILENAME',file_name($new_virtual->{'html'}));
648 $tpl->fillin('NEW_DIR',encode_html($new_dir));
649 $tpl->fillin('DIR',encode_html($dir));
650 $tpl->fillin('DIR_URL',escape($dir));
651
652 $tpl->fillin('COMMAND','copy');
653 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
654 $tpl->fillin('SCRIPT',$script);
655
656 my $output = header(-type => 'text/html');
657 $output .= $tpl->get_template;
658
659 return \$output;
660 }
661 }
662
663 copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
664 return devedit_reload({command => 'show', file => $new_dir});
665 }
666 }
667 else
668 {
669 if(-d $physical)
670 {
671 my $tpl = new Template;
672 $tpl->read_file($config->{'templates'}->{'copydir'});
673
674 $tpl->fillin('FILE',encode_html($virtual));
675 $tpl->fillin('DIR',encode_html($dir));
676 $tpl->fillin('DIR_URL',escape($dir));
677 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
678 $tpl->fillin('SCRIPT',$script);
679
680 my $output = header(-type => 'text/html');
681 $output .= $tpl->get_template;
682
683 return \$output;
684 }
685 else
686 {
687 my $tpl = new Template;
688 $tpl->read_file($config->{'templates'}->{'copyfile'});
689
690 $tpl->fillin('FILE',encode_html($virtual));
691 $tpl->fillin('DIR',encode_html($dir));
692 $tpl->fillin('DIR_URL',escape($dir));
693 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
694 $tpl->fillin('SCRIPT',$script);
695
696 my $output = header(-type => 'text/html');
697 $output .= $tpl->get_template;
698
699 return \$output;
700 }
701 }
702 }
703
704 # exec_rename()
705 #
706 # Rename/move a file and return to directory view
707 #
708 # Params: 1. Reference to user input hash
709 # 2. Reference to config hash
710 #
711 # Return: Output of the command (Scalar Reference)
712
713 sub exec_rename($$)
714 {
715 my ($data,$config) = @_;
716 my $physical = $data->{'physical'};
717 my $virtual = $data->{'virtual'};
718 my $dir = upper_path($virtual);
719 my $new_physical = $data->{'new_physical'};
720
721 return error($config->{'errors'}->{'rename_root'},'/') if($virtual eq '/');
722 return error($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path($physical));
723
724 if($new_physical)
725 {
726 my $new_virtual = multi_string($data->{'new_virtual'});
727 my $new_dir = upper_path($new_virtual->{'normal'});
728
729 if(-e $new_physical)
730 {
731 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical && not -l $new_physical);
732 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual}) unless(-w $new_physical);
733
734 if(not $data->{'cgi'}->param('confirmed'))
735 {
736 my $tpl = new Template;
737 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
738
739 $tpl->fillin('FILE',encode_html($virtual));
740 $tpl->fillin('NEW_FILE',$new_virtual->{'html'});
741 $tpl->fillin('NEW_FILENAME',file_name($new_virtual->{'html'}));
742 $tpl->fillin('NEW_DIR',encode_html($new_dir));
743 $tpl->fillin('DIR',encode_html($dir));
744
745 $tpl->fillin('COMMAND','rename');
746 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
747 $tpl->fillin('SCRIPT',$script);
748
749 my $output = header(-type => 'text/html');
750 $output .= $tpl->get_template;
751
752 return \$output;
753 }
754 }
755
756 move($physical,$new_physical) or return error($config->{'errors'}->{'rename_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
757 return devedit_reload({command => 'show', file => $new_dir});
758 }
759 else
760 {
761 my $tpl = new Template;
762 $tpl->read_file($config->{'templates'}->{'renamefile'});
763
764 $tpl->fillin('FILE',encode_html($virtual));
765 $tpl->fillin('DIR',encode_html($dir));
766 $tpl->fillin('DIR_URL',escape($dir));
767 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
768 $tpl->fillin('SCRIPT',$script);
769
770 my $output = header(-type => 'text/html');
771 $output .= $tpl->get_template;
772
773 return \$output;
774 }
775 }
776
777 # exec_remove()
778 #
779 # Remove a file or a directory and return to directory view
780 #
781 # Params: 1. Reference to user input hash
782 # 2. Reference to config hash
783 #
784 # Return: Output of the command (Scalar Reference)
785
786 sub exec_remove($$)
787 {
788 my ($data,$config) = @_;
789 my $physical = $data->{'physical'};
790 my $virtual = $data->{'virtual'};
791 my $dir = upper_path($virtual);
792
793 return error($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
794 return error($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path($physical));
795
796 if(-d $physical && not -l $physical)
797 {
798 # Remove a directory
799
800 if($data->{'cgi'}->param('confirmed'))
801 {
802 rmtree($physical);
803 return devedit_reload({command => 'show', file => $dir});
804 }
805 else
806 {
807 my $tpl = new Template;
808 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
809
810 $tpl->set_var('DIR',encode_html($virtual));
811 $tpl->set_var('DIR_URL',escape($virtual));
812 $tpl->set_var('UPPER_DIR',encode_html($dir));
813 $tpl->set_var('UPPER_DIR_URL',escape($dir));
814 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
815 $tpl->set_var('SCRIPT',$script);
816
817 $tpl->parse;
818
819 my $output = header(-type => 'text/html');
820 $output .= $tpl->get_template;
821
822 return \$output;
823 }
824 }
825 else
826 {
827 # Remove a file
828
829 if($data->{'cgi'}->param('confirmed'))
830 {
831 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},$dir,{FILE => $virtual});
832 return devedit_reload({command => 'show', file => $dir});
833 }
834 else
835 {
836 my $tpl = new Template;
837 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
838
839 $tpl->set_var('FILE',encode_html($virtual));
840 $tpl->set_var('FILE_URL',escape($virtual));
841 $tpl->set_var('DIR',encode_html($dir));
842 $tpl->set_var('DIR_URL',escape($dir));
843 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
844 $tpl->set_var('SCRIPT',$script);
845
846 $tpl->parse;
847
848 my $output = header(-type => 'text/html');
849 $output .= $tpl->get_template;
850
851 return \$output;
852 }
853 }
854 }
855
856 # exec_remove_multi()
857 #
858 # Remove a file or a directory and return to directory view
859 #
860 # Params: 1. Reference to user input hash
861 # 2. Reference to config hash
862 #
863 # Return: Output of the command (Scalar Reference)
864
865 sub exec_remove_multi($$)
866 {
867 my ($data,$config) = @_;
868 my $physical = $data->{'physical'};
869 my $virtual = $data->{'virtual'};
870 my $cgi = $data->{'cgi'};
871
872 my @files = $cgi->param('files');
873 my @new_files;
874
875 if(@files)
876 {
877 foreach my $file(@files)
878 {
879 # Filter out some "bad" files (e.g. files going up in the
880 # directory hierarchy or files containing slashes (it's too
881 # dangerous...)
882
883 next if($file =~ m!^\.+$!);
884 next if($file =~ m!/!);
885 next if($file =~ m!\\!);
886
887 push(@new_files,$file);
888 }
889 }
890
891 if(@new_files)
892 {
893 if($cgi->param('confirmed'))
894 {
895 my @success;
896 my @failed;
897
898 foreach my $file(@new_files)
899 {
900 my $file_path = clean_path($physical.'/'.$file);
901
902 if(-e $file_path)
903 {
904 if(-d $file_path && not -l $file_path)
905 {
906 # Remove a directory
907
908 if(rmtree($file_path))
909 {
910 push(@success,clean_path($file));
911 }
912 else
913 {
914 push(@failed,clean_path($file));
915 }
916 }
917 else
918 {
919 # Remove a file
920
921 if(unlink($file_path))
922 {
923 push(@success,clean_path($file));
924 }
925 else
926 {
927 push(@failed,clean_path($file));
928 }
929 }
930 }
931 else
932 {
933 push(@failed,clean_path($file));
934 }
935 }
936
937 my $tpl = new Template;
938 $tpl->read_file($config->{'templates'}->{'rmmulti'});
939
940 if(scalar(@success) > 0)
941 {
942 if(scalar(@success) == scalar(@new_files) && scalar(@failed) == 0)
943 {
944 return devedit_reload({command => 'show', file => $virtual});
945 }
946 else
947 {
948 $tpl->parse_if_block('success',1);
949
950 foreach my $file_success(@success)
951 {
952 $tpl->add_loop_data('SUCCESS',{FILE => encode_html($file_success),
953 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_success))});
954 }
955 }
956 }
957 else
958 {
959 $tpl->parse_if_block('success',0);
960 }
961
962 if(scalar(@failed) > 0)
963 {
964 $tpl->parse_if_block('failed',1);
965
966 foreach my $file_failed(@failed)
967 {
968 $tpl->add_loop_data('FAILED',{FILE => encode_html($file_failed),
969 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_failed))});
970 }
971 }
972 else
973 {
974 $tpl->parse_if_block('failed',0);
975 }
976
977
978 $tpl->set_var('DIR',encode_html($virtual));
979 $tpl->set_var('SCRIPT',$script);
980
981 $tpl->parse;
982
983 my $output = header(-type => 'text/html');
984 $output .= $tpl->get_template;
985
986 return \$output;
987 }
988 else
989 {
990 my $tpl = new Template;
991 $tpl->read_file($config->{'templates'}->{'confirm_rmmulti'});
992
993 foreach my $file(@new_files)
994 {
995 $tpl->add_loop_data('FILES',{FILE => encode_html($file),
996 FILE_PATH => encode_html(clean_path($virtual.'/'.$file))});
997 }
998
999 $tpl->set_var('COUNT',scalar(@new_files));
1000
1001 $tpl->set_var('DIR',encode_html($virtual));
1002 $tpl->set_var('SCRIPT',$script);
1003
1004 $tpl->parse;
1005
1006 my $output = header(-type => 'text/html');
1007 $output .= $tpl->get_template;
1008
1009 return \$output;
1010 }
1011 }
1012 else
1013 {
1014 return devedit_reload({command => 'show', file => $virtual});
1015 }
1016 }
1017
1018 # exec_chprop()
1019 #
1020 # Change the mode and the group of a file or a directory
1021 #
1022 # Params: 1. Reference to user input hash
1023 # 2. Reference to config hash
1024 #
1025 # Return: Output of the command (Scalar Reference)
1026
1027 sub exec_chprop($$)
1028 {
1029 my ($data,$config) = @_;
1030 my $physical = $data->{'physical'};
1031 my $virtual = $data->{'virtual'};
1032 my $dir = upper_path($virtual);
1033
1034 return error($config->{'errors'}->{'no_users'},$dir,{FILE => encode_html($virtual)}) unless($users);
1035 return error($config->{'errors'}->{'chprop_root'},'/') if($virtual eq '/');
1036 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => encode_html($virtual)}) unless(-o $physical);
1037 return error($config->{'errors'}->{'chprop_link'},$dir) if(-l $physical);
1038
1039 my $cgi = $data->{'cgi'};
1040 my $mode = $cgi->param('mode');
1041 my $group = $cgi->param('group');
1042
1043 if($mode || $group)
1044 {
1045 if($mode)
1046 {
1047 # Change the mode
1048
1049 return error($config->{'errors'}->{'invalid_mode'},$dir) unless($mode =~ /^[0-7]{3,}$/);
1050 chmod(oct($mode),$physical);
1051 }
1052
1053 if($group)
1054 {
1055 # Change the group using the `chgrp` system command
1056
1057 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => encode_html($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
1058 system('chgrp',$group,$physical);
1059 }
1060
1061 return devedit_reload({command => 'show', file => $dir});
1062 }
1063 else
1064 {
1065 # Display the form
1066
1067 my @stat = stat($physical);
1068 my $mode = $stat[2];
1069 my $gid = $stat[5];
1070
1071 my $tpl = new Template;
1072 $tpl->read_file($config->{'templates'}->{'chprop'});
1073
1074 # Insert file properties into the template
1075
1076 $tpl->set_var('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
1077 $tpl->set_var('MODE_STRING',mode_string($mode));
1078 $tpl->set_var('GID',$gid);
1079
1080 if(my $group = getgrgid($gid))
1081 {
1082 $tpl->set_var('GROUP',encode_html($group));
1083 $tpl->parse_if_block('group_detected',1);
1084 }
1085 else
1086 {
1087 $tpl->parse_if_block('group_detected',0);
1088 }
1089
1090 # Insert other information
1091
1092 $tpl->set_var('FILE',encode_html($virtual));
1093 $tpl->set_var('FILE_URL',escape($virtual));
1094 $tpl->set_var('DIR',encode_html($dir));
1095 $tpl->set_var('DIR_URL',escape($dir));
1096 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
1097 $tpl->set_var('SCRIPT',$script);
1098
1099 $tpl->parse;
1100
1101 my $output = header(-type => 'text/html');
1102 $output .= $tpl->get_template;
1103
1104 return \$output;
1105 }
1106 }
1107
1108 # exec_about()
1109 #
1110 # Display some information about Dev-Editor
1111 #
1112 # Params: 1. Reference to user input hash
1113 # 2. Reference to config hash
1114 #
1115 # Return: Output of the command (Scalar Reference)
1116
1117 sub exec_about($$)
1118 {
1119 my ($data,$config) = @_;
1120
1121 my $tpl = new Template;
1122 $tpl->read_file($config->{'templates'}->{'about'});
1123
1124 $tpl->set_var('SCRIPT',$script);
1125
1126 # Dev-Editor's version number
1127
1128 $tpl->set_var('VERSION',$data->{'version'});
1129
1130 # Some path information
1131
1132 $tpl->set_var('SCRIPT_PHYS',encode_html($ENV{'SCRIPT_FILENAME'}));
1133 $tpl->set_var('CONFIG_PATH',encode_html($data->{'configfile'}));
1134 $tpl->set_var('FILE_ROOT', encode_html($config->{'fileroot'}));
1135 $tpl->set_var('HTTP_ROOT', encode_html($config->{'httproot'}));
1136
1137 # Perl
1138
1139 $tpl->set_var('PERL_PROG',encode_html($^X));
1140 $tpl->set_var('PERL_VER', sprintf('%vd',$^V));
1141
1142 # Information about the server
1143
1144 $tpl->set_var('HTTPD',encode_html($ENV{'SERVER_SOFTWARE'}));
1145 $tpl->set_var('OS', encode_html($^O));
1146 $tpl->set_var('TIME', encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime : localtime)));
1147
1148 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
1149
1150 # Process information
1151
1152 $tpl->set_var('PID',$$);
1153
1154 # The following information is only available on systems supporting
1155 # users and groups
1156
1157 if($users)
1158 {
1159 # Dev-Editor is running on a system which allows users and groups
1160 # So we display the user and the group of our process
1161
1162 my $uid = POSIX::getuid;
1163 my $gid = POSIX::getgid;
1164
1165 $tpl->parse_if_block('users',1);
1166
1167 # IDs of user and group
1168
1169 $tpl->set_var('UID',$uid);
1170 $tpl->set_var('GID',$gid);
1171
1172 # Names of user and group
1173
1174 if(my $user = getpwuid($uid))
1175 {
1176 $tpl->set_var('USER',encode_html($user));
1177 $tpl->parse_if_block('user_detected',1);
1178 }
1179 else
1180 {
1181 $tpl->parse_if_block('user_detected',0);
1182 }
1183
1184 if(my $group = getgrgid($gid))
1185 {
1186 $tpl->set_var('GROUP',encode_html($group));
1187 $tpl->parse_if_block('group_detected',1);
1188 }
1189 else
1190 {
1191 $tpl->parse_if_block('group_detected',0);
1192 }
1193
1194 # Process umask
1195
1196 $tpl->set_var('UMASK',sprintf('%04o',umask));
1197 }
1198 else
1199 {
1200 $tpl->parse_if_block('users',0);
1201 }
1202
1203 $tpl->parse;
1204
1205 my $output = header(-type => 'text/html');
1206 $output .= $tpl->get_template;
1207
1208 return \$output;
1209 }
1210
1211 # it's true, baby ;-)
1212
1213 1;
1214
1215 #
1216 ### End ###

patrick-canterino.de