]> git.p6c8.net - devedit.git/blob - modules/Command.pm
Added single quotes around the "COMMAND" variable in the "cmd_unknown" error
[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-10
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",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",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",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 $uselist = $data->{'uselist'};
259
260 return error($config->{'errors'}->{'editdir'},upper_path($virtual)) if(-d $physical);
261 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($uselist->in_use($virtual));
262 return error($config->{'errors'}->{'noedit'},upper_path($virtual)) unless(-r $physical && -w $physical);
263
264 # Check on binary files
265
266 unless(-T $physical)
267 {
268 # Binary file
269
270 return error($config->{'errors'}->{'binary'},upper_path($virtual));
271 }
272 else
273 {
274 if($config->{'max_file_size'} && (stat($physical))[7] > $config->{'max_file_size'})
275 {
276 return error($config->{'errors'}->{'file_too_large'},upper_path($virtual),{SIZE => $config->{'max_file_size'}})
277 }
278 else
279 {
280 # Text file
281
282 $uselist->add_file($virtual);
283 $uselist->save;
284
285 my $content = file_read($physical);
286 $$content =~ s/\015\012|\012|\015/\n/g;
287
288 my $tpl = new Template;
289 $tpl->read_file($config->{'templates'}->{'editfile'});
290
291 $tpl->fillin("FILE",$virtual);
292 $tpl->fillin("DIR",upper_path($virtual));
293 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
294 $tpl->fillin("SCRIPT",$script);
295 $tpl->fillin("CONTENT",encode_entities($$content));
296
297 my $output = header(-type => "text/html");
298 $output .= $tpl->get_template;
299
300 return \$output;
301 }
302 }
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 $content = $data->{'cgi'}->param('filecontent');
338 my $uselist = $data->{'uselist'};
339
340 # We already unlock the file at the beginning of the
341 # subroutine, because if we have to abort this routine,
342 # the file keeps locked.
343 # Nobody else will access the file during this routine
344 # 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'},upper_path($virtual),{FILE => $virtual}) if($uselist->in_use($virtual));
369 }
370
371 return error($config->{'errors'}->{'text_to_binary'},upper_path($virtual)) unless(-T $physical);
372 return error($config->{'errors'}->{'editdir'},upper_path($virtual)) if(-d $physical);
373 return error($config->{'errors'}->{'noedit'}, upper_path($virtual)) if(-e $physical && !(-r $physical && -w $physical));
374
375 if(file_save($physical,\$content))
376 {
377 # Saving of the file was successful - so unlock it!
378
379 return devedit_reload({command => 'show', file => upper_path($virtual)});
380 }
381 else
382 {
383 return error($config->{'errors'}->{'edit_failed'},upper_path($virtual),{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 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
444
445 if($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 if(my $uploaded_file = $cgi->param('uploaded_file'))
482 {
483 # Process file upload
484
485 my $filename = file_name($uploaded_file);
486 my $file_phys = $physical."/".$filename;
487 my $file_virt = $virtual."".$filename;
488
489 return error($config->{'errors'}->{'file_exists'},$virtual,{FILE => $file_virt}) if(-e $file_phys && not $cgi->param('overwrite'));
490
491 my $ascii = $cgi->param('ascii');
492 my $handle = $cgi->upload('uploaded_file');
493
494 local *FILE;
495
496 open(FILE,">$file_phys") or return error($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE => $file_virt});
497 binmode(FILE) unless($ascii);
498
499 # Read transferred file and write it to disk
500
501 read($handle, my $data, -s $handle);
502 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
503 print FILE $data;
504
505 close(FILE);
506
507 return devedit_reload({command => "show", file => $virtual});
508 }
509 else
510 {
511 my $tpl = new Template;
512 $tpl->read_file($config->{'templates'}->{'upload'});
513
514 $tpl->fillin("DIR",$virtual);
515 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
516 $tpl->fillin("SCRIPT",$script);
517
518 my $output = header(-type => "text/html");
519 $output .= $tpl->get_template;
520
521 return \$output;
522 }
523 }
524
525 # exec_copy()
526 #
527 # Copy a file and return to directory view
528 #
529 # Params: 1. Reference to user input hash
530 # 2. Reference to config hash
531 #
532 # Return: Output of the command (Scalar Reference)
533
534 sub exec_copy($$)
535 {
536 my ($data,$config) = @_;
537 my $physical = $data->{'physical'};
538 my $virtual = encode_entities($data->{'virtual'});
539 my $new_physical = $data->{'new_physical'};
540
541 return error($config->{'errors'}->{'dircopy'}) if(-d $physical);
542 return error($config->{'errors'}->{'nocopy'}) unless(-r $physical);
543
544 if($new_physical)
545 {
546 my $new_virtual = $data->{'new_virtual'};
547 my $dir = upper_path($new_virtual);
548 $new_virtual = encode_entities($new_virtual);
549
550 if(-e $new_physical)
551 {
552 return error($config->{'errors'}->{'exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
553
554 if(-d $new_physical)
555 {
556 return error($config->{'errors'}->{'dir_replace'},$dir);
557 }
558 elsif(not $data->{'cgi'}->param('confirmed'))
559 {
560 my $tpl = new Template;
561 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
562
563 $tpl->fillin("FILE",$virtual);
564 $tpl->fillin("NEW_FILE",$new_virtual);
565 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
566 $tpl->fillin("NEW_DIR",$dir);
567 $tpl->fillin("DIR",upper_path($virtual));
568
569 $tpl->fillin("COMMAND","copy");
570 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
571 $tpl->fillin("SCRIPT",$script);
572
573 my $output = header(-type => "text/html");
574 $output .= $tpl->get_template;
575
576 return \$output;
577 }
578 }
579
580 copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
581 return devedit_reload({command => 'show', file => $dir});
582 }
583 else
584 {
585 my $tpl = new Template;
586 $tpl->read_file($config->{'templates'}->{'copyfile'});
587
588 $tpl->fillin("FILE",$virtual);
589 $tpl->fillin("DIR",upper_path($virtual));
590 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
591 $tpl->fillin("SCRIPT",$script);
592
593 my $output = header(-type => "text/html");
594 $output .= $tpl->get_template;
595
596 return \$output;
597 }
598 }
599
600 # exec_rename()
601 #
602 # Rename/move a file and return to directory view
603 #
604 # Params: 1. Reference to user input hash
605 # 2. Reference to config hash
606 #
607 # Return: Output of the command (Scalar Reference)
608
609 sub exec_rename($$)
610 {
611 my ($data,$config) = @_;
612 my $physical = $data->{'physical'};
613 my $virtual = $data->{'virtual'};
614 my $new_physical = $data->{'new_physical'};
615
616 return error($config->{'errors'}->{'rename_root'},"/") if($virtual eq "/");
617 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
618
619 if($new_physical)
620 {
621 my $new_virtual = $data->{'new_virtual'};
622 my $dir = upper_path($new_virtual);
623 $new_virtual = encode_entities($new_virtual);
624
625 if(-e $new_physical)
626 {
627 return error($config->{'errors'}->{'exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
628
629 if(-d $new_physical)
630 {
631 return error($config->{'errors'}->{'dir_replace'},$dir);
632 }
633 elsif(not $data->{'cgi'}->param('confirmed'))
634 {
635 my $tpl = new Template;
636 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
637
638 $tpl->fillin("FILE",$virtual);
639 $tpl->fillin("NEW_FILE",$new_virtual);
640 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
641 $tpl->fillin("NEW_DIR",$dir);
642 $tpl->fillin("DIR",upper_path($virtual));
643
644 $tpl->fillin("COMMAND","rename");
645 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
646 $tpl->fillin("SCRIPT",$script);
647
648 my $output = header(-type => "text/html");
649 $output .= $tpl->get_template;
650
651 return \$output;
652 }
653 }
654
655 rename($physical,$new_physical) or return error($config->{'errors'}->{'rename_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
656 return devedit_reload({command => 'show', file => $dir});
657 }
658 else
659 {
660 my $tpl = new Template;
661 $tpl->read_file($config->{'templates'}->{'renamefile'});
662
663 $tpl->fillin("FILE",$virtual);
664 $tpl->fillin("DIR",upper_path($virtual));
665 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
666 $tpl->fillin("SCRIPT",$script);
667
668 my $output = header(-type => "text/html");
669 $output .= $tpl->get_template;
670
671 return \$output;
672 }
673 }
674
675 # exec_remove()
676 #
677 # Remove a file or a directory and return to directory view
678 #
679 # Params: 1. Reference to user input hash
680 # 2. Reference to config hash
681 #
682 # Return: Output of the command (Scalar Reference)
683
684 sub exec_remove($$)
685 {
686 my ($data,$config) = @_;
687 my $physical = $data->{'physical'};
688 my $virtual = $data->{'virtual'};
689
690 return error($config->{'errors'}->{'remove_root'},"/") if($virtual eq "/");
691
692 if(-d $physical)
693 {
694 # Remove a directory
695
696 if($data->{'cgi'}->param('confirmed'))
697 {
698 rmtree($physical);
699 return devedit_reload({command => 'show', file => upper_path($virtual)});
700 }
701 else
702 {
703 my $tpl = new Template;
704 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
705
706 $tpl->fillin("DIR",$virtual);
707 $tpl->fillin("UPPER_DIR",upper_path($virtual));
708 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
709 $tpl->fillin("SCRIPT",$script);
710
711 my $output = header(-type => "text/html");
712 $output .= $tpl->get_template;
713
714 return \$output;
715 }
716 }
717 else
718 {
719 # Remove a file
720
721 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
722
723 if($data->{'cgi'}->param('confirmed'))
724 {
725 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},upper_path($virtual),{FILE => $virtual});
726 return devedit_reload({command => 'show', file => upper_path($virtual)});
727 }
728 else
729 {
730 my $tpl = new Template;
731 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
732
733 $tpl->fillin("FILE",$virtual);
734 $tpl->fillin("DIR",upper_path($virtual));
735 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
736 $tpl->fillin("SCRIPT",$script);
737
738 my $output = header(-type => "text/html");
739 $output .= $tpl->get_template;
740
741 return \$output;
742 }
743 }
744 }
745
746 # exec_chprop()
747 #
748 # Change the mode and the group of a file or a directory
749 #
750 # Params: 1. Reference to user input hash
751 # 2. Reference to config hash
752 #
753 # Return: Output of the command (Scalar Reference)
754
755 sub exec_chprop($$)
756 {
757 my ($data,$config) = @_;
758 my $physical = $data->{'physical'};
759 my $virtual = $data->{'virtual'};
760 my $dir = upper_path($virtual);
761 my $cgi = $data->{'cgi'};
762 my $mode = $cgi->param('mode');
763 my $group = $cgi->param('group');
764
765 if($users)
766 {
767 if(-o $physical)
768 {
769 if($mode || $group)
770 {
771 if($mode)
772 {
773 my $oct_mode = $mode;
774 $oct_mode = "0".$oct_mode if(length($oct_mode) == 3);
775 $oct_mode = oct($oct_mode);
776
777 chmod($oct_mode,$physical);
778 }
779
780 if($group)
781 {
782 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => $group}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
783 system("chgrp",$group,$physical);
784 }
785
786 return devedit_reload({command => 'show', file => $dir});
787 }
788 else
789 {
790 my @stat = stat($physical);
791
792 my $mode = $stat[2];
793 my $mode_oct = substr(sprintf("%04o",$mode),-4);
794 my $gid = $stat[5];
795 my $group = getgrgid($gid);
796
797 my $tpl = new Template;
798 $tpl->read_file($config->{'templates'}->{'chprop'});
799
800 $tpl->fillin("MODE_OCTAL",$mode_oct);
801 $tpl->fillin("MODE_STRING",mode_string($mode));
802 $tpl->fillin("GID",$gid);
803 $tpl->fillin("GROUP",$group);
804
805 $tpl->fillin("FILE",$virtual);
806 $tpl->fillin("DIR",$dir);
807 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
808 $tpl->fillin("SCRIPT",$script);
809
810 my $output = header(-type => "text/html");
811 $output .= $tpl->get_template;
812
813 return \$output;
814 }
815 }
816 else
817 {
818 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => $virtual});
819 }
820 }
821 else
822 {
823 return error($config->{'errors'}->{'no_users'},$dir,{FILE => $virtual});
824 }
825 }
826
827 # exec_unlock()
828 #
829 # Remove a file from the list of used files and
830 # return to directory view
831 #
832 # Params: 1. Reference to user input hash
833 # 2. Reference to config hash
834 #
835 # Return: Output of the command (Scalar Reference)
836
837 sub exec_unlock($$)
838 {
839 my ($data,$config) = @_;
840 my $virtual = $data->{'virtual'};
841 my $uselist = $data->{'uselist'};
842
843 return devedit_reload({command => 'show', file => upper_path($virtual)}) if($uselist->unused($virtual));
844
845 if($data->{'cgi'}->param('confirmed'))
846 {
847 file_unlock($uselist,$virtual);
848 return devedit_reload({command => 'show', file => upper_path($virtual)});
849 }
850 else
851 {
852 my $tpl = new Template;
853 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
854
855 $tpl->fillin("FILE",$virtual);
856 $tpl->fillin("DIR",upper_path($virtual));
857 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
858 $tpl->fillin("SCRIPT",$script);
859
860 my $output = header(-type => "text/html");
861 $output .= $tpl->get_template;
862
863 return \$output;
864 }
865 }
866
867 # exec_about()
868 #
869 # Display some information about Dev-Editor
870 #
871 # Params: 1. Reference to user input hash
872 # 2. Reference to config hash
873 #
874 # Return: Output of the command (Scalar Reference)
875
876 sub exec_about($$)
877 {
878 my ($data,$config) = @_;
879
880 my $tpl = new Template;
881 $tpl->read_file($config->{'templates'}->{'about'});
882
883 $tpl->fillin("SCRIPT",$script);
884
885 # Dev-Editor's version number
886
887 $tpl->fillin("VERSION",$data->{'version'});
888
889 # Some path information
890
891 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
892 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
893 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
894 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
895
896 # Perl
897
898 $tpl->fillin("PERL_PROG",$^X);
899 $tpl->fillin("PERL_VER",sprintf("%vd",$^V));
900
901 # Information about the server
902
903 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
904 $tpl->fillin("OS",$^O);
905 $tpl->fillin("TIME",strftime($config->{'timeformat'},localtime));
906
907 # Process information
908
909 $tpl->fillin("PID",$$);
910
911 # Check if the functions getpwuid() and getgrgid() are available
912
913 if($users)
914 {
915 # Dev-Editor is running on a system which allows users and groups
916 # So we display the user and the group of our process
917
918 my $uid = POSIX::getuid;
919 my $gid = POSIX::getgid;
920
921 $tpl->parse_if_block("users",1);
922
923 # ID's of user and group
924
925 $tpl->fillin("UID",$uid);
926 $tpl->fillin("GID",$gid);
927
928 # Names of user and group
929
930 $tpl->fillin("USER",getpwuid($uid));
931 $tpl->fillin("GROUP",getgrgid($gid));
932
933 # Process umask
934
935 $tpl->fillin("UMASK",sprintf("%04o",umask));
936 }
937 else
938 {
939 $tpl->parse_if_block("users",0);
940 }
941
942 my $output = header(-type => "text/html");
943 $output .= $tpl->get_template;
944
945 return \$output;
946 }
947
948 # it's true, baby ;-)
949
950 1;
951
952 #
953 ### End ###

patrick-canterino.de