]> git.p6c8.net - devedit.git/blob - modules/Command.pm
Parse the {IF} block "gmt" only once
[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: 2005-01-21
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT);
15
16 use File::Access;
17 use File::Copy;
18 use File::Path;
19
20 use POSIX qw(strftime);
21 use Tool;
22
23 use CGI qw(header
24 escape);
25
26 use HTML::Entities;
27 use Output;
28 use Template;
29
30 my $script = encode_entities($ENV{'SCRIPT_NAME'});
31 my $users = eval('getpwuid(0)') && eval('getgrgid(0)');
32
33 my %dispatch = ('show' => \&exec_show,
34 'beginedit' => \&exec_beginedit,
35 'canceledit' => \&exec_canceledit,
36 'endedit' => \&exec_endedit,
37 'mkdir' => \&exec_mkdir,
38 'mkfile' => \&exec_mkfile,
39 'upload' => \&exec_upload,
40 'copy' => \&exec_copy,
41 'rename' => \&exec_rename,
42 'remove' => \&exec_remove,
43 'chprop' => \&exec_chprop,
44 'unlock' => \&exec_unlock,
45 'about' => \&exec_about
46 );
47
48 ### Export ###
49
50 use base qw(Exporter);
51
52 @EXPORT = qw(exec_command);
53
54 # exec_command()
55 #
56 # Execute the specified command
57 #
58 # Params: 1. Command to execute
59 # 2. Reference to user input hash
60 # 3. Reference to config hash
61 #
62 # Return: Output of the command (Scalar Reference)
63
64 sub exec_command($$$)
65 {
66 my ($command,$data,$config) = @_;
67
68 foreach(keys(%dispatch))
69 {
70 if(lc($_) eq lc($command))
71 {
72 my $output = &{$dispatch{$_}}($data,$config);
73 return $output;
74 }
75 }
76
77 return error($config->{'errors'}->{'cmd_unknown'},'/',{COMMAND => encode_entities($command)});
78 }
79
80 # exec_show()
81 #
82 # View a directory or a file
83 #
84 # Params: 1. Reference to user input hash
85 # 2. Reference to config hash
86 #
87 # Return: Output of the command (Scalar Reference)
88
89 sub exec_show($$)
90 {
91 my ($data,$config) = @_;
92 my $physical = $data->{'physical'};
93 my $virtual = $data->{'virtual'};
94 my $upper_path = encode_entities(upper_path($virtual));
95 my $uselist = $data->{'uselist'};
96
97 my $tpl = new Template;
98
99 if(-d $physical)
100 {
101 # Create directory listing
102
103 return error($config->{'errors'}->{'no_dir_access'},$upper_path) unless(-r $physical && -x $physical);
104
105 my $direntries = dir_read($physical);
106 return error($config->{'dir_read_fail'},$upper_path,{DIR => encode_entities($virtual)}) unless($direntries);
107
108 my $files = $direntries->{'files'};
109 my $dirs = $direntries->{'dirs'};
110
111 my $dir_writeable = -w $physical;
112
113 my $dirlist = '';
114
115 my $filter1 = $data->{'cgi'}->param('filter') || '*'; # The real wildcard
116 my $filter2 = ($filter1 && $filter1 ne '*') ? $filter1 : ''; # Wildcard for output
117
118 # Create the link to the upper directory
119 # (only if we are not in the root directory)
120
121 unless($virtual eq '/')
122 {
123 my @stat = stat($physical.'/..');
124
125 my $udtpl = new Template;
126 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
127
128 $udtpl->fillin('UPPER_DIR',$upper_path);
129 $udtpl->fillin('DATE',encode_entities(strftime($config->{'timeformat'},localtime($stat[9]))));
130
131 $dirlist .= $udtpl->get_template;
132 }
133
134 # Directories
135
136 foreach my $dir(@$dirs)
137 {
138 next unless(dos_wildcard_match($filter1,$dir));
139
140 my $phys_path = $physical.'/'.$dir;
141 my $virt_path = encode_entities($virtual.$dir.'/');
142
143 my @stat = stat($phys_path);
144
145 my $dtpl = new Template;
146 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
147
148 $dtpl->fillin('DIR',$virt_path);
149 $dtpl->fillin('DIR_NAME',encode_entities($dir));
150 $dtpl->fillin('DATE',encode_entities(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
151 $dtpl->fillin('URL',equal_url(encode_entities($config->{'httproot'}),$virt_path));
152
153 $dtpl->parse_if_block('readable',-r $phys_path && -x $phys_path);
154 $dtpl->parse_if_block('users',$users && -o $phys_path);
155
156 $dirlist .= $dtpl->get_template;
157 }
158
159 # Files
160
161 foreach my $file(@$files)
162 {
163 next unless(dos_wildcard_match($filter1,$file));
164
165 my $phys_path = $physical.'/'.$file;
166 my $virt_path = encode_entities($virtual.$file);
167
168 my @stat = stat($phys_path);
169 my $in_use = $uselist->in_use($virtual.$file);
170 my $too_large = $config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'};
171
172 my $ftpl = new Template;
173 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
174
175 $ftpl->fillin('FILE',$virt_path);
176 $ftpl->fillin('FILE_NAME',encode_entities($file));
177 $ftpl->fillin('SIZE',$stat[7]);
178 $ftpl->fillin('DATE',encode_entities(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime($stat[9]) : localtime($stat[9]))));
179 $ftpl->fillin('URL',equal_url(encode_entities($config->{'httproot'}),$virt_path));
180
181 $ftpl->parse_if_block('not_readable',not -r $phys_path);
182 $ftpl->parse_if_block('binary',-B $phys_path);
183 $ftpl->parse_if_block('readonly',not -w $phys_path);
184
185 $ftpl->parse_if_block('viewable',-r $phys_path && -T $phys_path && not $too_large);
186 $ftpl->parse_if_block('editable',(-r $phys_path && -w $phys_path && -T $phys_path && not $too_large) && not $in_use);
187
188 $ftpl->parse_if_block('in_use',$in_use);
189 $ftpl->parse_if_block('unused',not $in_use);
190
191 $ftpl->parse_if_block('too_large',$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
192
193 $ftpl->parse_if_block('users',$users && -o $phys_path);
194
195 $dirlist .= $ftpl->get_template;
196 }
197
198 $tpl->read_file($config->{'templates'}->{'dirlist'});
199
200 $tpl->fillin('DIRLIST',$dirlist);
201 $tpl->fillin('DIR',encode_entities($virtual));
202 $tpl->fillin('SCRIPT',$script);
203 $tpl->fillin('URL',encode_entities(equal_url($config->{'httproot'},$virtual)));
204
205 $tpl->fillin('FILTER',encode_entities($filter2));
206 $tpl->fillin('FILTER_URL',escape($filter2));
207
208 $tpl->parse_if_block('dir_writeable',$dir_writeable);
209 $tpl->parse_if_block('filter',$filter2);
210 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
211 }
212 else
213 {
214 # View a file
215
216 return error($config->{'errors'}->{'no_view'},$upper_path) unless(-r $physical);
217
218 # Check on binary files
219 # We have to do it in this way or empty files will be recognized
220 # as binary files
221
222 return error($config->{'errors'}->{'binary'},$upper_path) unless(-T $physical);
223
224 # Is the file too large?
225
226 return error($config->{'errors'}->{'file_too_large'},$upper_path,{SIZE => $config->{'max_file_size'}}) if($config->{'max_file_size'} && -s $physical > $config->{'max_file_size'});
227
228 # View the file
229
230 my $content = file_read($physical);
231 $$content =~ s/\015\012|\012|\015/\n/g;
232
233 $tpl->read_file($config->{'templates'}->{'viewfile'});
234
235 $tpl->fillin('FILE',encode_entities($virtual));
236 $tpl->fillin('DIR',$upper_path);
237 $tpl->fillin('URL',encode_entities(equal_url($config->{'httproot'},$virtual)));
238 $tpl->fillin('SCRIPT',$script);
239
240 $tpl->parse_if_block('editable',-w $physical && $uselist->unused($virtual));
241
242 $tpl->fillin('CONTENT',encode_entities($$content));
243 }
244
245 my $output = header(-type => 'text/html');
246 $output .= $tpl->get_template;
247
248 return \$output;
249 }
250
251 # exec_beginedit
252 #
253 # Lock a file and display a form to edit it
254 #
255 # Params: 1. Reference to user input hash
256 # 2. Reference to config hash
257 #
258 # Return: Output of the command (Scalar Reference)
259
260 sub exec_beginedit($$)
261 {
262 my ($data,$config) = @_;
263 my $physical = $data->{'physical'};
264 my $virtual = $data->{'virtual'};
265 my $dir = upper_path($virtual);
266 my $uselist = $data->{'uselist'};
267
268 return error($config->{'errors'}->{'editdir'},$dir) if(-d $physical);
269 return error($config->{'errors'}->{'in_use'}, $dir,{FILE => $virtual}) if($uselist->in_use($virtual));
270 return error($config->{'errors'}->{'no_edit'},$dir) unless(-r $physical && -w $physical);
271
272 # Check on binary files
273
274 return error($config->{'errors'}->{'binary'},$dir) unless(-T $physical);
275
276 # Is the file too large?
277
278 return error($config->{'errors'}->{'file_too_large'},$dir,{SIZE => $config->{'max_file_size'}}) if($config->{'max_file_size'} && -s $physical > $config->{'max_file_size'});
279
280 # Lock the file...
281
282 $uselist->add_file($virtual);
283 $uselist->save;
284
285 # ... and show the editing form
286
287 my $content = file_read($physical);
288 $$content =~ s/\015\012|\012|\015/\n/g;
289
290 my $tpl = new Template;
291 $tpl->read_file($config->{'templates'}->{'editfile'});
292
293 $tpl->fillin('FILE',$virtual);
294 $tpl->fillin('DIR',$dir);
295 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
296 $tpl->fillin('SCRIPT',$script);
297 $tpl->fillin('CONTENT',encode_entities($$content));
298
299 my $output = header(-type => 'text/html');
300 $output .= $tpl->get_template;
301
302 return \$output;
303 }
304
305 # exec_canceledit()
306 #
307 # Abort file editing
308 #
309 # Params: 1. Reference to user input hash
310 # 2. Reference to config hash
311 #
312 # Return: Output of the command (Scalar Reference)
313
314 sub exec_canceledit($$)
315 {
316 my ($data,$config) = @_;
317 my $virtual = $data->{'virtual'};
318
319 file_unlock($data->{'uselist'},$virtual);
320 return devedit_reload({command => 'show', file => upper_path($virtual)});
321 }
322
323 # exec_endedit()
324 #
325 # Save a file, unlock it and return to directory view
326 #
327 # Params: 1. Reference to user input hash
328 # 2. Reference to config hash
329 #
330 # Return: Output of the command (Scalar Reference)
331
332 sub exec_endedit($$)
333 {
334 my ($data,$config) = @_;
335 my $physical = $data->{'physical'};
336 my $virtual = $data->{'virtual'};
337 my $dir = upper_path($virtual);
338 my $content = $data->{'cgi'}->param('filecontent');
339 my $uselist = $data->{'uselist'};
340
341 # We already unlock the file at the beginning of the subroutine,
342 # because if we have to abort this routine, the file keeps locked.
343 # No other user of Dev-Editor will access the file during this
344 # routine because of the concept of File::UseList.
345
346 file_unlock($uselist,$virtual);
347
348 # Normalize newlines
349
350 $content =~ s/\015\012|\012|\015/\n/g;
351
352 if($data->{'cgi'}->param('encode_iso'))
353 {
354 # Encode all ISO-8859-1 special chars
355
356 $content = encode_entities($content,"\200-\377");
357 }
358
359 if($data->{'cgi'}->param('saveas') && $data->{'new_physical'} ne '' && $data->{'new_virtual'} ne '')
360 {
361 # Create the new filename
362
363 $physical = $data->{'new_physical'};
364 $virtual = $data->{'new_virtual'};
365
366 # Check if someone else is editing the new file
367
368 return error($config->{'errors'}->{'in_use'},$dir,{FILE => $virtual}) if($uselist->in_use($virtual));
369 }
370
371 return error($config->{'errors'}->{'editdir'},$dir) if(-d $physical);
372 return error($config->{'errors'}->{'no_edit'},$dir) if(-e $physical && !(-r $physical && -w $physical));
373 return error($config->{'errors'}->{'text_to_binary'},$dir) unless(-T $physical);
374
375 if(file_save($physical,\$content))
376 {
377 # Saving of the file was successful!
378
379 return devedit_reload({command => 'show', file => $dir});
380 }
381 else
382 {
383 return error($config->{'errors'}->{'edit_failed'},$dir,{FILE => $virtual});
384 }
385 }
386
387 # exec_mkfile()
388 #
389 # Create a file and return to directory view
390 #
391 # Params: 1. Reference to user input hash
392 # 2. Reference to config hash
393 #
394 # Return: Output of the command (Scalar Reference)
395
396 sub exec_mkfile($$)
397 {
398 my ($data,$config) = @_;
399 my $new_physical = $data->{'new_physical'};
400 my $new_virtual = $data->{'new_virtual'};
401 my $dir = upper_path($new_virtual);
402 $new_virtual = encode_entities($new_virtual);
403
404 if($new_physical)
405 {
406 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
407
408 file_create($new_physical) or return error($config->{'errors'}->{'mkfile_failed'},$dir,{FILE => $new_virtual});
409 return devedit_reload({command => 'show', file => $dir});
410 }
411 else
412 {
413 my $tpl = new Template;
414 $tpl->read_file($config->{'templates'}->{'mkfile'});
415
416 $tpl->fillin('DIR','/');
417 $tpl->fillin('SCRIPT',$script);
418
419 my $output = header(-type => 'text/html');
420 $output .= $tpl->get_template;
421
422 return \$output;
423 }
424 }
425
426 # exec_mkdir()
427 #
428 # Create a directory and return to directory view
429 #
430 # Params: 1. Reference to user input hash
431 # 2. Reference to config hash
432 #
433 # Return: Output of the command (Scalar Reference)
434
435 sub exec_mkdir($$)
436 {
437 my ($data,$config) = @_;
438 my $new_physical = $data->{'new_physical'};
439 my $new_virtual = $data->{'new_virtual'};
440 my $dir = upper_path($new_virtual);
441 $new_virtual = encode_entities($new_virtual);
442
443 if($new_physical)
444 {
445 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
446
447 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
448 return devedit_reload({command => 'show', file => $dir});
449 }
450 else
451 {
452 my $tpl = new Template;
453 $tpl->read_file($config->{'templates'}->{'mkdir'});
454
455 $tpl->fillin('DIR','/');
456 $tpl->fillin('SCRIPT',$script);
457
458 my $output = header(-type => 'text/html');
459 $output .= $tpl->get_template;
460
461 return \$output;
462 }
463 }
464
465 # exec_upload()
466 #
467 # Upload a file
468 #
469 # Params: 1. Reference to user input hash
470 # 2. Reference to config hash
471 #
472 # Return: Output of the command (Scalar Reference)
473
474 sub exec_upload($$)
475 {
476 my ($data,$config) = @_;
477 my $physical = $data->{'physical'};
478 my $virtual = $data->{'virtual'};
479 my $cgi = $data->{'cgi'};
480
481 return error($config->{'errors'}->{'no_directory'},upper_path($virtual),{FILE => $virtual}) unless(-d $physical);
482 return error($config->{'errors'}->{'dir_no_create'},$virtual,{DIR => $virtual}) unless(-w $physical);
483
484 if(my $uploaded_file = $cgi->param('uploaded_file'))
485 {
486 # Process file upload
487
488 my $filename = file_name($uploaded_file);
489 my $file_phys = $physical.'/'.$filename;
490 my $file_virt = $virtual.$filename;
491
492 return error($config->{'errors'}->{'in_use'},$virtual,{FILE => $file_virt}) if($data->{'uselist'}->in_use($file_virt));
493
494 if(-e $file_phys)
495 {
496 return error($config->{'errors'}->{'dir_replace'},$virtual) if(-d $file_phys);
497 return error($config->{'errors'}->{'exist_no_write'},$virtual,{FILE => $file_virt}) unless(-w $file_phys);
498 return error($config->{'errors'}->{'file_exists'},$virtual,{FILE => $file_virt}) unless($cgi->param('overwrite'));
499 }
500
501 my $ascii = $cgi->param('ascii');
502 my $handle = $cgi->upload('uploaded_file');
503
504 return error($config->{'errors'}->{'invalid_upload'},$virtual) unless($handle);
505
506 # Read transferred file and write it to disk
507
508 read($handle, my $data, -s $handle);
509 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
510 file_save($file_phys,\$data,not $ascii) or return error($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE => $file_virt});
511
512 return devedit_reload({command => 'show', file => $virtual});
513 }
514 else
515 {
516 my $tpl = new Template;
517 $tpl->read_file($config->{'templates'}->{'upload'});
518
519 $tpl->fillin('DIR',$virtual);
520 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
521 $tpl->fillin('SCRIPT',$script);
522
523 my $output = header(-type => 'text/html');
524 $output .= $tpl->get_template;
525
526 return \$output;
527 }
528 }
529
530 # exec_copy()
531 #
532 # Copy a file and return to directory view
533 #
534 # Params: 1. Reference to user input hash
535 # 2. Reference to config hash
536 #
537 # Return: Output of the command (Scalar Reference)
538
539 sub exec_copy($$)
540 {
541 my ($data,$config) = @_;
542 my $physical = $data->{'physical'};
543 my $virtual = encode_entities($data->{'virtual'});
544 my $dir = upper_path($virtual);
545 my $new_physical = $data->{'new_physical'};
546
547 return error($config->{'errors'}->{'dircopy'},$dir) if(-d $physical);
548 return error($config->{'errors'}->{'no_copy'},$dir) unless(-r $physical);
549
550 if($new_physical)
551 {
552 my $new_virtual = $data->{'new_virtual'};
553 my $new_dir = upper_path($new_virtual);
554 $new_virtual = encode_entities($new_virtual);
555
556 if(-e $new_physical)
557 {
558 return error($config->{'errors'}->{'exist_edited'},$new_dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
559 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical);
560 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual}) unless(-w $new_physical);
561
562 if(not $data->{'cgi'}->param('confirmed'))
563 {
564 my $tpl = new Template;
565 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
566
567 $tpl->fillin('FILE',$virtual);
568 $tpl->fillin('NEW_FILE',$new_virtual);
569 $tpl->fillin('NEW_FILENAME',file_name($new_virtual));
570 $tpl->fillin('NEW_DIR',$new_dir);
571 $tpl->fillin('DIR',$dir);
572
573 $tpl->fillin('COMMAND','copy');
574 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
575 $tpl->fillin('SCRIPT',$script);
576
577 my $output = header(-type => 'text/html');
578 $output .= $tpl->get_template;
579
580 return \$output;
581 }
582 }
583
584 copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},$dir,{FILE => $virtual, NEW_FILE => $new_virtual});
585 return devedit_reload({command => 'show', file => $new_dir});
586 }
587 else
588 {
589 my $tpl = new Template;
590 $tpl->read_file($config->{'templates'}->{'copyfile'});
591
592 $tpl->fillin('FILE',$virtual);
593 $tpl->fillin('DIR',$dir);
594 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
595 $tpl->fillin('SCRIPT',$script);
596
597 my $output = header(-type => 'text/html');
598 $output .= $tpl->get_template;
599
600 return \$output;
601 }
602 }
603
604 # exec_rename()
605 #
606 # Rename/move 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_rename($$)
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'}->{'rename_root'},'/') if($virtual eq '/');
622 return error($config->{'errors'}->{'no_rename'},$dir) unless(-w upper_path($physical));
623 return error($config->{'errors'}->{'in_use'},$dir,{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
624
625 if($new_physical)
626 {
627 my $new_virtual = $data->{'new_virtual'};
628 my $new_dir = upper_path($new_virtual);
629 $new_virtual = encode_entities($new_virtual);
630
631 if(-e $new_physical)
632 {
633 return error($config->{'errors'}->{'exist_edited'},$new_dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
634 return error($config->{'errors'}->{'dir_replace'},$new_dir) if(-d $new_physical);
635 return error($config->{'errors'}->{'exist_no_write'},$new_dir,{FILE => $new_virtual}) unless(-w $new_physical);
636
637 if(not $data->{'cgi'}->param('confirmed'))
638 {
639 my $tpl = new Template;
640 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
641
642 $tpl->fillin('FILE',$virtual);
643 $tpl->fillin('NEW_FILE',$new_virtual);
644 $tpl->fillin('NEW_FILENAME',file_name($new_virtual));
645 $tpl->fillin('NEW_DIR',$new_dir);
646 $tpl->fillin('DIR',$dir);
647
648 $tpl->fillin('COMMAND','rename');
649 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
650 $tpl->fillin('SCRIPT',$script);
651
652 my $output = header(-type => 'text/html');
653 $output .= $tpl->get_template;
654
655 return \$output;
656 }
657 }
658
659 rename($physical,$new_physical) or return error($config->{'errors'}->{'rename_failed'},$dir,{FILE => $virtual, NEW_FILE => $new_virtual});
660 return devedit_reload({command => 'show', file => $new_dir});
661 }
662 else
663 {
664 my $tpl = new Template;
665 $tpl->read_file($config->{'templates'}->{'renamefile'});
666
667 $tpl->fillin('FILE',$virtual);
668 $tpl->fillin('DIR',$dir);
669 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
670 $tpl->fillin('SCRIPT',$script);
671
672 my $output = header(-type => 'text/html');
673 $output .= $tpl->get_template;
674
675 return \$output;
676 }
677 }
678
679 # exec_remove()
680 #
681 # Remove a file or a directory and return to directory view
682 #
683 # Params: 1. Reference to user input hash
684 # 2. Reference to config hash
685 #
686 # Return: Output of the command (Scalar Reference)
687
688 sub exec_remove($$)
689 {
690 my ($data,$config) = @_;
691 my $physical = $data->{'physical'};
692 my $virtual = $data->{'virtual'};
693 my $dir = upper_path($virtual);
694
695 return error($config->{'errors'}->{'remove_root'},'/') if($virtual eq '/');
696 return error($config->{'errors'}->{'no_delete'},$dir) unless(-w upper_path($physical));
697
698 if(-d $physical)
699 {
700 # Remove a directory
701
702 if($data->{'cgi'}->param('confirmed'))
703 {
704 rmtree($physical);
705 return devedit_reload({command => 'show', file => $dir});
706 }
707 else
708 {
709 my $tpl = new Template;
710 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
711
712 $tpl->fillin('DIR',$virtual);
713 $tpl->fillin('UPPER_DIR',$dir);
714 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
715 $tpl->fillin('SCRIPT',$script);
716
717 my $output = header(-type => 'text/html');
718 $output .= $tpl->get_template;
719
720 return \$output;
721 }
722 }
723 else
724 {
725 # Remove a file
726
727 return error($config->{'errors'}->{'in_use'},$dir,{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
728
729 if($data->{'cgi'}->param('confirmed'))
730 {
731 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},$dir,{FILE => $virtual});
732 return devedit_reload({command => 'show', file => $dir});
733 }
734 else
735 {
736 my $tpl = new Template;
737 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
738
739 $tpl->fillin('FILE',$virtual);
740 $tpl->fillin('DIR',$dir);
741 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
742 $tpl->fillin('SCRIPT',$script);
743
744 my $output = header(-type => 'text/html');
745 $output .= $tpl->get_template;
746
747 return \$output;
748 }
749 }
750 }
751
752 # exec_chprop()
753 #
754 # Change the mode and the group of a file or a directory
755 #
756 # Params: 1. Reference to user input hash
757 # 2. Reference to config hash
758 #
759 # Return: Output of the command (Scalar Reference)
760
761 sub exec_chprop($$)
762 {
763 my ($data,$config) = @_;
764 my $physical = $data->{'physical'};
765 my $virtual = $data->{'virtual'};
766 my $dir = upper_path($virtual);
767
768 return error($config->{'errors'}->{'no_users'},$dir,{FILE => $virtual}) unless($users);
769 return error($config->{'errors'}->{'chprop_root'},'/') if($virtual eq '/');
770 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => $virtual}) unless(-o $physical);
771 return error($config->{'errors'}->{'in_use'},$dir,{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
772
773 my $cgi = $data->{'cgi'};
774 my $mode = $cgi->param('mode');
775 my $group = $cgi->param('group');
776
777 if($mode || $group)
778 {
779 if($mode)
780 {
781 # Change the mode
782
783 chmod(oct($mode),$physical);
784 }
785
786 if($group)
787 {
788 # Change the group using the `chgrp` system command
789
790 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => encode_entities($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
791 system('chgrp',$group,$physical);
792 }
793
794 return devedit_reload({command => 'show', file => $dir});
795 }
796 else
797 {
798 # Display the form
799
800 my @stat = stat($physical);
801 my $mode = $stat[2];
802 my $gid = $stat[5];
803
804 my $tpl = new Template;
805 $tpl->read_file($config->{'templates'}->{'chprop'});
806
807 # Insert file properties into the template
808
809 $tpl->fillin('MODE_OCTAL',substr(sprintf('%04o',$mode),-4));
810 $tpl->fillin('MODE_STRING',mode_string($mode));
811 $tpl->fillin('GID',$gid);
812
813 if(my $group = getgrgid($gid))
814 {
815 $tpl->fillin('GROUP',encode_entities($group));
816 $tpl->parse_if_block('group_detected',1);
817 }
818 else
819 {
820 $tpl->parse_if_block('group_detected',0);
821 }
822
823 # Insert other information
824
825 $tpl->fillin('FILE',$virtual);
826 $tpl->fillin('DIR',$dir);
827 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
828 $tpl->fillin('SCRIPT',$script);
829
830 my $output = header(-type => 'text/html');
831 $output .= $tpl->get_template;
832
833 return \$output;
834 }
835 }
836
837 # exec_unlock()
838 #
839 # Remove a file from the list of used files and
840 # return to directory view
841 #
842 # Params: 1. Reference to user input hash
843 # 2. Reference to config hash
844 #
845 # Return: Output of the command (Scalar Reference)
846
847 sub exec_unlock($$)
848 {
849 my ($data,$config) = @_;
850 my $virtual = $data->{'virtual'};
851 my $uselist = $data->{'uselist'};
852 my $dir = upper_path($virtual);
853
854 return devedit_reload({command => 'show', file => $dir}) if($uselist->unused($virtual));
855
856 if($data->{'cgi'}->param('confirmed'))
857 {
858 file_unlock($uselist,$virtual);
859 return devedit_reload({command => 'show', file => $dir});
860 }
861 else
862 {
863 my $tpl = new Template;
864 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
865
866 $tpl->fillin('FILE',$virtual);
867 $tpl->fillin('DIR',$dir);
868 $tpl->fillin('URL',equal_url($config->{'httproot'},$virtual));
869 $tpl->fillin('SCRIPT',$script);
870
871 my $output = header(-type => 'text/html');
872 $output .= $tpl->get_template;
873
874 return \$output;
875 }
876 }
877
878 # exec_about()
879 #
880 # Display some information about Dev-Editor
881 #
882 # Params: 1. Reference to user input hash
883 # 2. Reference to config hash
884 #
885 # Return: Output of the command (Scalar Reference)
886
887 sub exec_about($$)
888 {
889 my ($data,$config) = @_;
890
891 my $tpl = new Template;
892 $tpl->read_file($config->{'templates'}->{'about'});
893
894 $tpl->fillin('SCRIPT',$script);
895
896 # Dev-Editor's version number
897
898 $tpl->fillin('VERSION',$data->{'version'});
899
900 # Some path information
901
902 $tpl->fillin('SCRIPT_PHYS',encode_entities($ENV{'SCRIPT_FILENAME'}));
903 $tpl->fillin('CONFIG_PATH',encode_entities($data->{'configfile'}));
904 $tpl->fillin('FILE_ROOT', encode_entities($config->{'fileroot'}));
905 $tpl->fillin('HTTP_ROOT', encode_entities($config->{'httproot'}));
906
907 # Perl
908
909 $tpl->fillin('PERL_PROG',encode_entities($^X));
910 $tpl->fillin('PERL_VER', sprintf('%vd',$^V));
911
912 # Information about the server
913
914 $tpl->fillin('HTTPD',encode_entities($ENV{'SERVER_SOFTWARE'}));
915 $tpl->fillin('OS', encode_entities($^O));
916 $tpl->fillin('TIME', encode_entities(strftime($config->{'timeformat'},($config->{'use_gmt'}) ? gmtime : localtime)));
917
918 $tpl->parse_if_block('gmt',$config->{'use_gmt'});
919
920 # Process information
921
922 $tpl->fillin('PID',$$);
923
924 # Check if the functions getpwuid() and getgrgid() are available
925
926 if($users)
927 {
928 # Dev-Editor is running on a system which allows users and groups
929 # So we display the user and the group of our process
930
931 my $uid = POSIX::getuid;
932 my $gid = POSIX::getgid;
933
934 $tpl->parse_if_block('users',1);
935
936 # ID's of user and group
937
938 $tpl->fillin('UID',$uid);
939 $tpl->fillin('GID',$gid);
940
941 # Names of user and group
942
943 if(my $user = getpwuid($uid))
944 {
945 $tpl->fillin('USER',encode_entities($user));
946 $tpl->parse_if_block('user_detected',1);
947 }
948 else
949 {
950 $tpl->parse_if_block('user_detected',0);
951 }
952
953 if(my $group = getgrgid($gid))
954 {
955 $tpl->fillin('GROUP',encode_entities($group));
956 $tpl->parse_if_block('group_detected',1);
957 }
958 else
959 {
960 $tpl->parse_if_block('group_detected',0);
961 }
962
963 # Process umask
964
965 $tpl->fillin('UMASK',sprintf('%04o',umask));
966 }
967 else
968 {
969 $tpl->parse_if_block('users',0);
970 }
971
972 my $output = header(-type => 'text/html');
973 $output .= $tpl->get_template;
974
975 return \$output;
976 }
977
978 # it's true, baby ;-)
979
980 1;
981
982 #
983 ### End ###

patrick-canterino.de