]> git.p6c8.net - devedit.git/blob - modules/Command.pm
c4e7f120d94d60827d3e6c7583b2875c8e206aca
[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-04
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->fillin('DIR',encode_html($virtual));
811 $tpl->fillin('DIR_URL',escape($virtual));
812 $tpl->fillin('UPPER_DIR',encode_html($dir));
813 $tpl->fillin('UPPER_DIR_URL',escape($dir));
814 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
815 $tpl->fillin('SCRIPT',$script);
816
817 my $output = header(-type => 'text/html');
818 $output .= $tpl->get_template;
819
820 return \$output;
821 }
822 }
823 else
824 {
825 # Remove a file
826
827 if($data->{'cgi'}->param('confirmed'))
828 {
829 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},$dir,{FILE => $virtual});
830 return devedit_reload({command => 'show', file => $dir});
831 }
832 else
833 {
834 my $tpl = new Template;
835 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
836
837 $tpl->fillin('FILE',encode_html($virtual));
838 $tpl->fillin('FILE_URL',escape($virtual));
839 $tpl->fillin('DIR',encode_html($dir));
840 $tpl->fillin('DIR_URL',escape($dir));
841 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
842 $tpl->fillin('SCRIPT',$script);
843
844 my $output = header(-type => 'text/html');
845 $output .= $tpl->get_template;
846
847 return \$output;
848 }
849 }
850 }
851
852 # exec_remove_multi()
853 #
854 # Remove a file or a directory and return to directory view
855 #
856 # Params: 1. Reference to user input hash
857 # 2. Reference to config hash
858 #
859 # Return: Output of the command (Scalar Reference)
860
861 sub exec_remove_multi($$)
862 {
863 my ($data,$config) = @_;
864 my $physical = $data->{'physical'};
865 my $virtual = $data->{'virtual'};
866 my $cgi = $data->{'cgi'};
867
868 my @files = $cgi->param('files');#
869 my @new_files;
870
871 if(@files)
872 {
873 foreach my $file(@files)
874 {
875 # Filter out some "bad" files (e.g. files going up in the
876 # directory hierarchy or files containing slashes (it's too
877 # dangerous...)
878
879 next if($file =~ m!^\.+$!);
880 next if($file =~ m!/!);
881 next if($file =~ m!\\!);
882
883 push(@new_files,$file);
884 }
885 }
886
887 if(@new_files)
888 {
889 if($cgi->param('confirmed'))
890 {
891 my @success;
892 my @failed;
893
894 foreach my $file(@new_files)
895 {
896 my $file_path = clean_path($physical.'/'.$file);
897
898 if(-e $file_path)
899 {
900 if(-d $file_path && not -l $file_path)
901 {
902 # Remove a directory
903
904 if(rmtree($file_path))
905 {
906 push(@success,clean_path($file));
907 }
908 else
909 {
910 push(@failed,clean_path($file));
911 }
912 }
913 else
914 {
915 # Remove a file
916
917 if(unlink($file_path))
918 {
919 push(@success,clean_path($file));
920 }
921 else
922 {
923 push(@failed,clean_path($file));
924 }
925 }
926 }
927 else
928 {
929 push(@failed,clean_path($file));
930 }
931 }
932
933 my $tpl = new Template;
934 $tpl->read_file($config->{'templates'}->{'rmmulti'});
935
936 if(scalar(@success) > 0)
937 {
938 $tpl->parse_if_block('success',1);
939
940 foreach my $file_success(@success)
941 {
942 $tpl->add_loop_data('SUCCESS',{FILE => encode_html($file_success),
943 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_success))});
944 }
945 }
946 else
947 {
948 $tpl->parse_if_block('success',0);
949 }
950
951 if(scalar(@failed) > 0)
952 {
953 $tpl->parse_if_block('failed',1);
954
955 foreach my $file_failed(@failed)
956 {
957 $tpl->add_loop_data('FAILED',{FILE => encode_html($file_failed),
958 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_failed))});
959 }
960 }
961 else
962 {
963 $tpl->parse_if_block('failed',0);
964 }
965
966
967 $tpl->set_var('DIR',encode_html($virtual));
968 $tpl->set_var('SCRIPT',$script);
969
970 $tpl->parse;
971
972 my $output = header(-type => 'text/html');
973 $output .= $tpl->get_template;
974
975 return \$output;
976 }
977 else
978 {
979 my $tpl = new Template;
980 $tpl->read_file($config->{'templates'}->{'confirm_rmmulti'});
981
982 foreach my $file(@new_files)
983 {
984 $tpl->add_loop_data('FILES',{FILE => encode_html($file),
985 FILE_PATH => encode_html(clean_path($virtual.'/'.$file))});
986 }
987
988 $tpl->set_var('COUNT',scalar(@new_files));
989
990 $tpl->set_var('DIR',encode_html($virtual));
991 $tpl->set_var('SCRIPT',$script);
992
993 $tpl->parse;
994
995 my $output = header(-type => 'text/html');
996 $output .= $tpl->get_template;
997
998 return \$output;
999 }
1000 }
1001 else
1002 {
1003 return devedit_reload({command => 'show', file => $virtual});
1004 }
1005 }
1006
1007 # exec_chprop()
1008 #
1009 # Change the mode and the group of a file or a directory
1010 #
1011 # Params: 1. Reference to user input hash
1012 # 2. Reference to config hash
1013 #
1014 # Return: Output of the command (Scalar Reference)
1015
1016 sub exec_chprop($$)
1017 {
1018 my ($data,$config) = @_;
1019 my $physical = $data->{'physical'};
1020 my $virtual = $data->{'virtual'};
1021 my $dir = upper_path($virtual);
1022
1023 return error($config->{'errors'}->{'no_users'},$dir,{FILE => encode_html($virtual)}) unless($users);
1024 return error($config->{'errors'}->{'chprop_root'},'/') if($virtual eq '/');
1025 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => encode_html($virtual)}) unless(-o $physical);
1026 return error($config->{'errors'}->{'chprop_link'},$dir) if(-l $physical);
1027
1028 my $cgi = $data->{'cgi'};
1029 my $mode = $cgi->param('mode');
1030 my $group = $cgi->param('group');
1031
1032 if($mode || $group)
1033 {
1034 if($mode)
1035 {
1036 # Change the mode
1037
1038 return error($config->{'errors'}->{'invalid_mode'},$dir) unless($mode =~ /^[0-7]{3,}$/);
1039 chmod(oct($mode),$physical);
1040 }
1041
1042 if($group)
1043 {
1044 # Change the group using the `chgrp` system command
1045
1046 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => encode_html($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
1047 system('chgrp',$group,$physical);
1048 }
1049
1050 return devedit_reload({command => 'show', file => $dir});
1051 }
1052 else
1053 {
1054 # Display the form
1055
1056 my @stat = stat($physical);
1057 my $mode = $stat[2];
1058 my $gid = $stat[5];
1059
1060 my $tpl = new Template;
1061 $tpl->read_file($config->{'templates'}->{'chprop'});
1062
1063 # Insert file properties into the template
1064
1065 $tpl->fillin('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
1066 $tpl->fillin('MODE_STRING',mode_string($mode));
1067 $tpl->fillin('GID',$gid);
1068
1069 if(my $group = getgrgid($gid))
1070 {
1071 $tpl->fillin('GROUP',encode_html($group));
1072 $tpl->parse_if_block('group_detected',1);
1073 }
1074 else
1075 {
1076 $tpl->parse_if_block('group_detected',0);
1077 }
1078
1079 # Insert other information
1080
1081 $tpl->fillin('FILE',encode_html($virtual));
1082 $tpl->fillin('FILE_URL',escape($virtual));
1083 $tpl->fillin('DIR',encode_html($dir));
1084 $tpl->fillin('DIR_URL',escape($dir));
1085 $tpl->fillin('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
1086 $tpl->fillin('SCRIPT',$script);
1087
1088 my $output = header(-type => 'text/html');
1089 $output .= $tpl->get_template;
1090
1091 return \$output;
1092 }
1093 }
1094
1095 # exec_about()
1096 #
1097 # Display some information about Dev-Editor
1098 #
1099 # Params: 1. Reference to user input hash
1100 # 2. Reference to config hash
1101 #
1102 # Return: Output of the command (Scalar Reference)
1103
1104 sub exec_about($$)
1105 {
1106 my ($data,$config) = @_;
1107
1108 my $tpl = new Template;
1109 $tpl->read_file($config->{'templates'}->{'about'});
1110
1111 $tpl->fillin('SCRIPT',$script);
1112
1113 # Dev-Editor's version number
1114
1115 $tpl->fillin('VERSION',$data->{'version'});
1116
1117 # Some path information
1118
1119 $tpl->fillin('SCRIPT_PHYS',encode_html($ENV{'SCRIPT_FILENAME'}));
1120 $tpl->fillin('CONFIG_PATH',encode_html($data->{'configfile'}));
1121 $tpl->fillin('FILE_ROOT', encode_html($config->{'fileroot'}));
1122 $tpl->fillin('HTTP_ROOT', encode_html($config->{'httproot'}));
1123
1124 # Perl
1125
1126 $tpl->fillin('PERL_PROG',encode_html($^X));
1127 $tpl->fillin('PERL_VER', sprintf('%vd',$^V));
1128
1129 # Information about the server
1130
1131 $tpl->fillin('HTTPD',encode_html($ENV{'SERVER_SOFTWARE'}));
1132 $tpl->fillin('OS', encode_html($^O));
1133 $tpl->fillin('TIME', encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime : localtime)));
1134
1135 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
1136
1137 # Process information
1138
1139 $tpl->fillin('PID',$$);
1140
1141 # The following information is only available on systems supporting
1142 # users and groups
1143
1144 if($users)
1145 {
1146 # Dev-Editor is running on a system which allows users and groups
1147 # So we display the user and the group of our process
1148
1149 my $uid = POSIX::getuid;
1150 my $gid = POSIX::getgid;
1151
1152 $tpl->parse_if_block('users',1);
1153
1154 # IDs of user and group
1155
1156 $tpl->fillin('UID',$uid);
1157 $tpl->fillin('GID',$gid);
1158
1159 # Names of user and group
1160
1161 if(my $user = getpwuid($uid))
1162 {
1163 $tpl->fillin('USER',encode_html($user));
1164 $tpl->parse_if_block('user_detected',1);
1165 }
1166 else
1167 {
1168 $tpl->parse_if_block('user_detected',0);
1169 }
1170
1171 if(my $group = getgrgid($gid))
1172 {
1173 $tpl->fillin('GROUP',encode_html($group));
1174 $tpl->parse_if_block('group_detected',1);
1175 }
1176 else
1177 {
1178 $tpl->parse_if_block('group_detected',0);
1179 }
1180
1181 # Process umask
1182
1183 $tpl->fillin('UMASK',sprintf('%04o',umask));
1184 }
1185 else
1186 {
1187 $tpl->parse_if_block('users',0);
1188 }
1189
1190 my $output = header(-type => 'text/html');
1191 $output .= $tpl->get_template;
1192
1193 return \$output;
1194 }
1195
1196 # it's true, baby ;-)
1197
1198 1;
1199
1200 #
1201 ### End ###

patrick-canterino.de