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

patrick-canterino.de