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

patrick-canterino.de