]> git.p6c8.net - devedit.git/blob - modules/Command.pm
34a25416d8ad3830ddb507a64a2271e115fc7353
[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-10
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->set_var('DIR','/');
477 $tpl->set_var('SCRIPT',$script);
478
479 $tpl->parse;
480
481 my $output = header(-type => 'text/html');
482 $output .= $tpl->get_template;
483
484 return \$output;
485 }
486 }
487
488 # exec_mkdir()
489 #
490 # Create a directory and return to directory view
491 #
492 # Params: 1. Reference to user input hash
493 # 2. Reference to config hash
494 #
495 # Return: Output of the command (Scalar Reference)
496
497 sub exec_mkdir($$)
498 {
499 my ($data,$config) = @_;
500 my $new_physical = $data->{'new_physical'};
501 my $new_virtual = $data->{'new_virtual'};
502 my $dir = upper_path($new_virtual);
503 $new_virtual = encode_html($new_virtual);
504
505 if($new_physical)
506 {
507 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
508
509 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
510 return devedit_reload({command => 'show', file => $dir});
511 }
512 else
513 {
514 my $tpl = new Template;
515 $tpl->read_file($config->{'templates'}->{'mkdir'});
516
517 $tpl->set_var('DIR','/');
518 $tpl->set_var('SCRIPT',$script);
519
520 $tpl->parse;
521
522 my $output = header(-type => 'text/html');
523 $output .= $tpl->get_template;
524
525 return \$output;
526 }
527 }
528
529 # exec_upload()
530 #
531 # Process a file upload
532 #
533 # Params: 1. Reference to user input hash
534 # 2. Reference to config hash
535 #
536 # Return: Output of the command (Scalar Reference)
537
538 sub exec_upload($$)
539 {
540 my ($data,$config) = @_;
541 my $physical = $data->{'physical'};
542 my $virtual = $data->{'virtual'};
543 my $cgi = $data->{'cgi'};
544
545 return error($config->{'errors'}->{'no_directory'},upper_path($virtual),{FILE => encode_html($virtual)}) unless(-d $physical && not -l $physical);
546 return error($config->{'errors'}->{'dir_no_create'},$virtual,{DIR => encode_html($virtual)}) unless(-w $physical);
547
548 if(my $uploaded_file = $cgi->param('uploaded_file'))
549 {
550 if($cgi->param('remote_file'))
551 {
552 $uploaded_file = $cgi->param('remote_file');
553
554 $uploaded_file =~ s!/!!g;
555 $uploaded_file =~ s!\\!!g;
556 }
557
558 # Process file upload
559
560 my $filename = file_name($uploaded_file);
561 my $file_phys = $physical.'/'.$filename;
562 my $file_virt = encode_html($virtual.$filename);
563
564 if(-e $file_phys)
565 {
566 return error($config->{'errors'}->{'link_replace'},$virtual) if(-l $file_phys);
567 return error($config->{'errors'}->{'dir_replace'},$virtual) if(-d $file_phys);
568 return error($config->{'errors'}->{'exist_no_write'},$virtual,{FILE => $file_virt}) unless(-w $file_phys);
569 return error($config->{'errors'}->{'file_exists'},$virtual,{FILE => $file_virt}) unless($cgi->param('overwrite'));
570 }
571
572 my $ascii = $cgi->param('ascii');
573 my $handle = $cgi->upload('uploaded_file');
574
575 return error($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
576
577 # Read transferred file and write it to disk
578
579 read($handle, my $data, -s $handle);
580 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
581 file_save($file_phys,\$data,not $ascii) or return error($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE => $file_virt});
582
583 return devedit_reload({command => 'show', file => $virtual});
584 }
585 else
586 {
587 my $tpl = new Template;
588 $tpl->read_file($config->{'templates'}->{'upload'});
589
590 $tpl->set_var('DIR',encode_html($virtual));
591 $tpl->set_var('DIR_URL',escape($virtual));
592 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
593 $tpl->set_var('SCRIPT',$script);
594
595 $tpl->parse;
596
597 my $output = header(-type => 'text/html');
598 $output .= $tpl->get_template;
599
600 return \$output;
601 }
602 }
603
604 # exec_copy()
605 #
606 # Copy a file and return to directory view
607 #
608 # Params: 1. Reference to user input hash
609 # 2. Reference to config hash
610 #
611 # Return: Output of the command (Scalar Reference)
612
613 sub exec_copy($$)
614 {
615 my ($data,$config) = @_;
616 my $physical = $data->{'physical'};
617 my $virtual = $data->{'virtual'};
618 my $dir = upper_path($virtual);
619 my $new_physical = $data->{'new_physical'};
620
621 return error($config->{'errors'}->{'link_copy'},$dir) if(-l $physical);
622 return error($config->{'errors'}->{'no_copy'},$dir) unless(-r $physical);
623
624 if($new_physical)
625 {
626 my $new_virtual = multi_string($data->{'new_virtual'});
627 my $new_dir = upper_path($new_virtual->{'normal'});
628
629 if(-d $physical)
630 {
631 return error($config->{'errors'}->{'no_copy'},$dir) unless(-x $physical);
632 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual->{'html'}}) if(-e $new_physical);
633 return error($config->{'errors'}->{'dir_copy_self'},$dir) if(index($new_virtual->{'normal'},$virtual) == 0);
634
635 dir_copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
636 return devedit_reload({command => 'show', file => $new_dir});
637 }
638 else
639 {
640 if(-e $new_physical)
641 {
642 return error($config->{'errors'}->{'link_replace'},$new_dir) if(-l $new_physical);
643 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical);
644 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual->{'html'}}) unless(-w $new_physical);
645
646 if(not $data->{'cgi'}->param('confirmed'))
647 {
648 my $tpl = new Template;
649 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
650
651 $tpl->set_var('FILE',encode_html($virtual));
652 $tpl->set_var('NEW_FILE',$new_virtual->{'html'});
653 $tpl->set_var('NEW_FILENAME',file_name($new_virtual->{'html'}));
654 $tpl->set_var('NEW_DIR',encode_html($new_dir));
655 $tpl->set_var('DIR',encode_html($dir));
656 $tpl->set_var('DIR_URL',escape($dir));
657
658 $tpl->set_var('COMMAND','copy');
659 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
660 $tpl->set_var('SCRIPT',$script);
661
662 $tpl->parse;
663
664 my $output = header(-type => 'text/html');
665 $output .= $tpl->get_template;
666
667 return \$output;
668 }
669 }
670
671 copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
672 return devedit_reload({command => 'show', file => $new_dir});
673 }
674 }
675 else
676 {
677 if(-d $physical)
678 {
679 my $tpl = new Template;
680 $tpl->read_file($config->{'templates'}->{'copydir'});
681
682 $tpl->set_var('FILE',encode_html($virtual));
683 $tpl->set_var('DIR',encode_html($dir));
684 $tpl->set_var('DIR_URL',escape($dir));
685 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
686 $tpl->set_var('SCRIPT',$script);
687
688 $tpl->parse;
689
690 my $output = header(-type => 'text/html');
691 $output .= $tpl->get_template;
692
693 return \$output;
694 }
695 else
696 {
697 my $tpl = new Template;
698 $tpl->read_file($config->{'templates'}->{'copyfile'});
699
700 $tpl->set_var('FILE',encode_html($virtual));
701 $tpl->set_var('DIR',encode_html($dir));
702 $tpl->set_var('DIR_URL',escape($dir));
703 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
704 $tpl->set_var('SCRIPT',$script);
705
706 $tpl->parse;
707
708 my $output = header(-type => 'text/html');
709 $output .= $tpl->get_template;
710
711 return \$output;
712 }
713 }
714 }
715
716 # exec_rename()
717 #
718 # Rename/move a file and return to directory view
719 #
720 # Params: 1. Reference to user input hash
721 # 2. Reference to config hash
722 #
723 # Return: Output of the command (Scalar Reference)
724
725 sub exec_rename($$)
726 {
727 my ($data,$config) = @_;
728 my $physical = $data->{'physical'};
729 my $virtual = $data->{'virtual'};
730 my $dir = upper_path($virtual);
731 my $new_physical = $data->{'new_physical'};
732
733 return error($config->{'errors'}->{'rename_root'},'/') if($virtual eq '/');
734 return error($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path($physical));
735
736 if($new_physical)
737 {
738 my $new_virtual = multi_string($data->{'new_virtual'});
739 my $new_dir = upper_path($new_virtual->{'normal'});
740
741 if(-e $new_physical)
742 {
743 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical && not -l $new_physical);
744 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual}) unless(-w $new_physical);
745
746 if(not $data->{'cgi'}->param('confirmed'))
747 {
748 my $tpl = new Template;
749 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
750
751 $tpl->set_var('FILE',encode_html($virtual));
752 $tpl->set_var('NEW_FILE',$new_virtual->{'html'});
753 $tpl->set_var('NEW_FILENAME',file_name($new_virtual->{'html'}));
754 $tpl->set_var('NEW_DIR',encode_html($new_dir));
755 $tpl->set_var('DIR',encode_html($dir));
756
757 $tpl->set_var('COMMAND','rename');
758 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
759 $tpl->set_var('SCRIPT',$script);
760
761 $tpl->parse;
762
763 my $output = header(-type => 'text/html');
764 $output .= $tpl->get_template;
765
766 return \$output;
767 }
768 }
769
770 move($physical,$new_physical) or return error($config->{'errors'}->{'rename_failed'},$dir,{FILE => encode_html($virtual), NEW_FILE => $new_virtual->{'html'}});
771 return devedit_reload({command => 'show', file => $new_dir});
772 }
773 else
774 {
775 my $tpl = new Template;
776 $tpl->read_file($config->{'templates'}->{'renamefile'});
777
778 $tpl->set_var('FILE',encode_html($virtual));
779 $tpl->set_var('DIR',encode_html($dir));
780 $tpl->set_var('DIR_URL',escape($dir));
781 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
782 $tpl->set_var('SCRIPT',$script);
783
784 $tpl->parse;
785
786 my $output = header(-type => 'text/html');
787 $output .= $tpl->get_template;
788
789 return \$output;
790 }
791 }
792
793 # exec_remove()
794 #
795 # Remove a file or a directory and return to directory view
796 #
797 # Params: 1. Reference to user input hash
798 # 2. Reference to config hash
799 #
800 # Return: Output of the command (Scalar Reference)
801
802 sub exec_remove($$)
803 {
804 my ($data,$config) = @_;
805 my $physical = $data->{'physical'};
806 my $virtual = $data->{'virtual'};
807 my $dir = upper_path($virtual);
808
809 return error($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
810 return error($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path($physical));
811
812 if(-d $physical && not -l $physical)
813 {
814 # Remove a directory
815
816 if($data->{'cgi'}->param('confirmed'))
817 {
818 rmtree($physical);
819 return devedit_reload({command => 'show', file => $dir});
820 }
821 else
822 {
823 my $tpl = new Template;
824 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
825
826 $tpl->set_var('DIR',encode_html($virtual));
827 $tpl->set_var('DIR_URL',escape($virtual));
828 $tpl->set_var('UPPER_DIR',encode_html($dir));
829 $tpl->set_var('UPPER_DIR_URL',escape($dir));
830 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
831 $tpl->set_var('SCRIPT',$script);
832
833 $tpl->parse;
834
835 my $output = header(-type => 'text/html');
836 $output .= $tpl->get_template;
837
838 return \$output;
839 }
840 }
841 else
842 {
843 # Remove a file
844
845 if($data->{'cgi'}->param('confirmed'))
846 {
847 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},$dir,{FILE => $virtual});
848 return devedit_reload({command => 'show', file => $dir});
849 }
850 else
851 {
852 my $tpl = new Template;
853 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
854
855 $tpl->set_var('FILE',encode_html($virtual));
856 $tpl->set_var('FILE_URL',escape($virtual));
857 $tpl->set_var('DIR',encode_html($dir));
858 $tpl->set_var('DIR_URL',escape($dir));
859 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
860 $tpl->set_var('SCRIPT',$script);
861
862 $tpl->parse;
863
864 my $output = header(-type => 'text/html');
865 $output .= $tpl->get_template;
866
867 return \$output;
868 }
869 }
870 }
871
872 # exec_remove_multi()
873 #
874 # Remove a file or a directory and return to directory view
875 #
876 # Params: 1. Reference to user input hash
877 # 2. Reference to config hash
878 #
879 # Return: Output of the command (Scalar Reference)
880
881 sub exec_remove_multi($$)
882 {
883 my ($data,$config) = @_;
884 my $physical = $data->{'physical'};
885 my $virtual = $data->{'virtual'};
886 my $cgi = $data->{'cgi'};
887
888 my @files = $cgi->param('files');
889 my @new_files;
890
891 if(@files)
892 {
893 foreach my $file(@files)
894 {
895 # Filter out some "bad" files (e.g. files going up in the
896 # directory hierarchy or files containing slashes (it's too
897 # dangerous...)
898
899 next if($file =~ m!^\.+$!);
900 next if($file =~ m!/!);
901 next if($file =~ m!\\!);
902
903 push(@new_files,$file);
904 }
905 }
906
907 if(@new_files)
908 {
909 if($cgi->param('confirmed'))
910 {
911 my @success;
912 my @failed;
913
914 foreach my $file(@new_files)
915 {
916 my $file_path = clean_path($physical.'/'.$file);
917
918 if(-e $file_path)
919 {
920 if(-d $file_path && not -l $file_path)
921 {
922 # Remove a directory
923
924 if(rmtree($file_path))
925 {
926 push(@success,clean_path($file));
927 }
928 else
929 {
930 push(@failed,clean_path($file));
931 }
932 }
933 else
934 {
935 # Remove a file
936
937 if(unlink($file_path))
938 {
939 push(@success,clean_path($file));
940 }
941 else
942 {
943 push(@failed,clean_path($file));
944 }
945 }
946 }
947 else
948 {
949 push(@failed,clean_path($file));
950 }
951 }
952
953 my $tpl = new Template;
954 $tpl->read_file($config->{'templates'}->{'rmmulti'});
955
956 if(scalar(@success) > 0)
957 {
958 if(scalar(@success) == scalar(@new_files) && scalar(@failed) == 0)
959 {
960 return devedit_reload({command => 'show', file => $virtual});
961 }
962 else
963 {
964 $tpl->parse_if_block('success',1);
965
966 foreach my $file_success(@success)
967 {
968 $tpl->add_loop_data('SUCCESS',{FILE => encode_html($file_success),
969 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_success))});
970 }
971 }
972 }
973 else
974 {
975 $tpl->parse_if_block('success',0);
976 }
977
978 if(scalar(@failed) > 0)
979 {
980 $tpl->parse_if_block('failed',1);
981
982 foreach my $file_failed(@failed)
983 {
984 $tpl->add_loop_data('FAILED',{FILE => encode_html($file_failed),
985 FILE_PATH => encode_html(clean_path($virtual.'/'.$file_failed))});
986 }
987 }
988 else
989 {
990 $tpl->parse_if_block('failed',0);
991 }
992
993
994 $tpl->set_var('DIR',encode_html($virtual));
995 $tpl->set_var('SCRIPT',$script);
996
997 $tpl->parse;
998
999 my $output = header(-type => 'text/html');
1000 $output .= $tpl->get_template;
1001
1002 return \$output;
1003 }
1004 else
1005 {
1006 my $tpl = new Template;
1007 $tpl->read_file($config->{'templates'}->{'confirm_rmmulti'});
1008
1009 foreach my $file(@new_files)
1010 {
1011 $tpl->add_loop_data('FILES',{FILE => encode_html($file),
1012 FILE_PATH => encode_html(clean_path($virtual.'/'.$file))});
1013 }
1014
1015 $tpl->set_var('COUNT',scalar(@new_files));
1016
1017 $tpl->set_var('DIR',encode_html($virtual));
1018 $tpl->set_var('SCRIPT',$script);
1019
1020 $tpl->parse;
1021
1022 my $output = header(-type => 'text/html');
1023 $output .= $tpl->get_template;
1024
1025 return \$output;
1026 }
1027 }
1028 else
1029 {
1030 return devedit_reload({command => 'show', file => $virtual});
1031 }
1032 }
1033
1034 # exec_chprop()
1035 #
1036 # Change the mode and the group of a file or a directory
1037 #
1038 # Params: 1. Reference to user input hash
1039 # 2. Reference to config hash
1040 #
1041 # Return: Output of the command (Scalar Reference)
1042
1043 sub exec_chprop($$)
1044 {
1045 my ($data,$config) = @_;
1046 my $physical = $data->{'physical'};
1047 my $virtual = $data->{'virtual'};
1048 my $dir = upper_path($virtual);
1049
1050 return error($config->{'errors'}->{'no_users'},$dir,{FILE => encode_html($virtual)}) unless($users);
1051 return error($config->{'errors'}->{'chprop_root'},'/') if($virtual eq '/');
1052 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => encode_html($virtual)}) unless(-o $physical);
1053 return error($config->{'errors'}->{'chprop_link'},$dir) if(-l $physical);
1054
1055 my $cgi = $data->{'cgi'};
1056 my $mode = $cgi->param('mode');
1057 my $group = $cgi->param('group');
1058
1059 if($mode || $group)
1060 {
1061 if($mode)
1062 {
1063 # Change the mode
1064
1065 return error($config->{'errors'}->{'invalid_mode'},$dir) unless($mode =~ /^[0-7]{3,}$/);
1066 chmod(oct($mode),$physical);
1067 }
1068
1069 if($group)
1070 {
1071 # Change the group using the `chgrp` system command
1072
1073 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => encode_html($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
1074 system('chgrp',$group,$physical);
1075 }
1076
1077 return devedit_reload({command => 'show', file => $dir});
1078 }
1079 else
1080 {
1081 # Display the form
1082
1083 my @stat = stat($physical);
1084 my $mode = $stat[2];
1085 my $gid = $stat[5];
1086
1087 my $tpl = new Template;
1088 $tpl->read_file($config->{'templates'}->{'chprop'});
1089
1090 # Insert file properties into the template
1091
1092 $tpl->set_var('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
1093 $tpl->set_var('MODE_STRING',mode_string($mode));
1094 $tpl->set_var('GID',$gid);
1095
1096 if(my $group = getgrgid($gid))
1097 {
1098 $tpl->set_var('GROUP',encode_html($group));
1099 $tpl->parse_if_block('group_detected',1);
1100 }
1101 else
1102 {
1103 $tpl->parse_if_block('group_detected',0);
1104 }
1105
1106 # Insert other information
1107
1108 $tpl->set_var('FILE',encode_html($virtual));
1109 $tpl->set_var('FILE_URL',escape($virtual));
1110 $tpl->set_var('DIR',encode_html($dir));
1111 $tpl->set_var('DIR_URL',escape($dir));
1112 $tpl->set_var('URL',encode_html(equal_url($config->{'httproot'},$virtual)));
1113 $tpl->set_var('SCRIPT',$script);
1114
1115 $tpl->parse;
1116
1117 my $output = header(-type => 'text/html');
1118 $output .= $tpl->get_template;
1119
1120 return \$output;
1121 }
1122 }
1123
1124 # exec_about()
1125 #
1126 # Display some information about Dev-Editor
1127 #
1128 # Params: 1. Reference to user input hash
1129 # 2. Reference to config hash
1130 #
1131 # Return: Output of the command (Scalar Reference)
1132
1133 sub exec_about($$)
1134 {
1135 my ($data,$config) = @_;
1136
1137 my $tpl = new Template;
1138 $tpl->read_file($config->{'templates'}->{'about'});
1139
1140 $tpl->set_var('SCRIPT',$script);
1141
1142 # Dev-Editor's version number
1143
1144 $tpl->set_var('VERSION',$data->{'version'});
1145
1146 # Some path information
1147
1148 $tpl->set_var('SCRIPT_PHYS',encode_html($ENV{'SCRIPT_FILENAME'}));
1149 $tpl->set_var('CONFIG_PATH',encode_html($data->{'configfile'}));
1150 $tpl->set_var('FILE_ROOT', encode_html($config->{'fileroot'}));
1151 $tpl->set_var('HTTP_ROOT', encode_html($config->{'httproot'}));
1152
1153 # Perl
1154
1155 $tpl->set_var('PERL_PROG',encode_html($^X));
1156 $tpl->set_var('PERL_VER', sprintf('%vd',$^V));
1157
1158 # Information about the server
1159
1160 $tpl->set_var('HTTPD',encode_html($ENV{'SERVER_SOFTWARE'}));
1161 $tpl->set_var('OS', encode_html($^O));
1162 $tpl->set_var('TIME', encode_html(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime : localtime)));
1163
1164 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
1165
1166 # Process information
1167
1168 $tpl->set_var('PID',$$);
1169
1170 # The following information is only available on systems supporting
1171 # users and groups
1172
1173 if($users)
1174 {
1175 # Dev-Editor is running on a system which allows users and groups
1176 # So we display the user and the group of our process
1177
1178 my $uid = POSIX::getuid;
1179 my $gid = POSIX::getgid;
1180
1181 $tpl->parse_if_block('users',1);
1182
1183 # IDs of user and group
1184
1185 $tpl->set_var('UID',$uid);
1186 $tpl->set_var('GID',$gid);
1187
1188 # Names of user and group
1189
1190 if(my $user = getpwuid($uid))
1191 {
1192 $tpl->set_var('USER',encode_html($user));
1193 $tpl->parse_if_block('user_detected',1);
1194 }
1195 else
1196 {
1197 $tpl->parse_if_block('user_detected',0);
1198 }
1199
1200 if(my $group = getgrgid($gid))
1201 {
1202 $tpl->set_var('GROUP',encode_html($group));
1203 $tpl->parse_if_block('group_detected',1);
1204 }
1205 else
1206 {
1207 $tpl->parse_if_block('group_detected',0);
1208 }
1209
1210 # Process umask
1211
1212 $tpl->set_var('UMASK',sprintf('%04o',umask));
1213 }
1214 else
1215 {
1216 $tpl->parse_if_block('users',0);
1217 }
1218
1219 $tpl->parse;
1220
1221 my $output = header(-type => 'text/html');
1222 $output .= $tpl->get_template;
1223
1224 return \$output;
1225 }
1226
1227 # it's true, baby ;-)
1228
1229 1;
1230
1231 #
1232 ### End ###

patrick-canterino.de