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

patrick-canterino.de