]> git.p6c8.net - devedit.git/blob - modules/Command.pm
f2b1a04f4b9031c038129b674f2580ccb1d65d1c
[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-25
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 $upper_path = 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 => $virtual}) unless($direntries);
105
106 my $files = $direntries->{'files'};
107 my $dirs = $direntries->{'dirs'};
108
109 my $dirlist = "";
110
111 # Create the link to the upper directory
112 # (only if we are not in the root directory)
113
114 unless($virtual eq "/")
115 {
116 my @stat = stat($physical."/..");
117
118 my $udtpl = new Template;
119 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
120
121 $udtpl->fillin("UPPER_DIR",encode_entities($upper_path));
122 $udtpl->fillin("DATE",encode_entities(strftime($config->{'timeformat'},localtime($stat[9]))));
123
124 $dirlist .= $udtpl->get_template;
125 }
126
127 # Directories
128
129 foreach my $dir(@$dirs)
130 {
131 my $phys_path = $physical."/".$dir;
132 my $virt_path = encode_entities($virtual.$dir."/");
133
134 my @stat = stat($phys_path);
135
136 my $dtpl = new Template;
137 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
138
139 $dtpl->fillin("DIR",$virt_path);
140 $dtpl->fillin("DIR_NAME",$dir);
141 $dtpl->fillin("DATE",encode_entities(strftime($config->{'timeformat'},localtime($stat[9]))));
142 $dtpl->fillin("URL",equal_url($config->{'httproot'},$virt_path));
143
144 $dtpl->parse_if_block("readable",-r $phys_path && -x $phys_path);
145 $dtpl->parse_if_block("users",$users && -o $phys_path);
146
147 $dirlist .= $dtpl->get_template;
148 }
149
150 # Files
151
152 foreach my $file(@$files)
153 {
154 my $phys_path = $physical."/".$file;
155 my $virt_path = encode_entities($virtual.$file);
156
157 my @stat = stat($phys_path);
158 my $in_use = $uselist->in_use($virtual.$file);
159
160 my $ftpl = new Template;
161 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
162
163 $ftpl->fillin("FILE",$virt_path);
164 $ftpl->fillin("FILE_NAME",$file);
165 $ftpl->fillin("SIZE",$stat[7]);
166 $ftpl->fillin("DATE",encode_entities(strftime($config->{'timeformat'},localtime($stat[9]))));
167 $ftpl->fillin("URL",equal_url($config->{'httproot'},$virt_path));
168
169 $ftpl->parse_if_block("not_readable",not -r $phys_path);
170 $ftpl->parse_if_block("binary",-B $phys_path);
171 $ftpl->parse_if_block("readonly",not -w $phys_path);
172
173 $ftpl->parse_if_block("viewable",-r $phys_path && -T $phys_path && not ($config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'}));
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) 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);
208 }
209 else
210 {
211 # Text file
212
213 if($config->{'max_file_size'} && -s $physical > $config->{'max_file_size'})
214 {
215 return error($config->{'errors'}->{'file_too_large'},$upper_path,{SIZE => $config->{'max_file_size'}})
216 }
217 else
218 {
219 my $content = file_read($physical);
220 $$content =~ s/\015\012|\012|\015/\n/g;
221
222 $tpl->read_file($config->{'templates'}->{'viewfile'});
223
224 $tpl->fillin("FILE",$virtual);
225 $tpl->fillin("DIR",$upper_path);
226 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
227 $tpl->fillin("SCRIPT",$script);
228
229 $tpl->parse_if_block("editable",-w $physical && $uselist->unused($virtual));
230
231 $tpl->fillin("CONTENT",encode_entities($$content));
232 }
233 }
234 }
235
236 my $output = header(-type => "text/html");
237 $output .= $tpl->get_template;
238
239 return \$output;
240 }
241
242 # exec_beginedit
243 #
244 # Lock a file and display a form to edit it
245 #
246 # Params: 1. Reference to user input hash
247 # 2. Reference to config hash
248 #
249 # Return: Output of the command (Scalar Reference)
250
251 sub exec_beginedit($$)
252 {
253 my ($data,$config) = @_;
254 my $physical = $data->{'physical'};
255 my $virtual = $data->{'virtual'};
256 my $dir = upper_path($virtual);
257 my $uselist = $data->{'uselist'};
258
259 return error($config->{'errors'}->{'editdir'},$dir) if(-d $physical);
260 return error($config->{'errors'}->{'in_use'}, $dir,{FILE => $virtual}) if($uselist->in_use($virtual));
261 return error($config->{'errors'}->{'noedit'}, $dir) unless(-r $physical && -w $physical);
262
263 # Check on binary files
264
265 unless(-T $physical)
266 {
267 # Binary file
268
269 return error($config->{'errors'}->{'binary'},$dir);
270 }
271 else
272 {
273 if($config->{'max_file_size'} && -s $physical > $config->{'max_file_size'})
274 {
275 return error($config->{'errors'}->{'file_too_large'},$dir,{SIZE => $config->{'max_file_size'}})
276 }
277 else
278 {
279 # Text file
280
281 $uselist->add_file($virtual);
282 $uselist->save;
283
284 my $content = file_read($physical);
285 $$content =~ s/\015\012|\012|\015/\n/g;
286
287 my $tpl = new Template;
288 $tpl->read_file($config->{'templates'}->{'editfile'});
289
290 $tpl->fillin("FILE",$virtual);
291 $tpl->fillin("DIR",$dir);
292 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
293 $tpl->fillin("SCRIPT",$script);
294 $tpl->fillin("CONTENT",encode_entities($$content));
295
296 my $output = header(-type => "text/html");
297 $output .= $tpl->get_template;
298
299 return \$output;
300 }
301 }
302 }
303
304 # exec_canceledit()
305 #
306 # Abort file editing
307 #
308 # Params: 1. Reference to user input hash
309 # 2. Reference to config hash
310 #
311 # Return: Output of the command (Scalar Reference)
312
313 sub exec_canceledit($$)
314 {
315 my ($data,$config) = @_;
316 my $virtual = $data->{'virtual'};
317
318 file_unlock($data->{'uselist'},$virtual);
319 return devedit_reload({command => 'show', file => upper_path($virtual)});
320 }
321
322 # exec_endedit()
323 #
324 # Save a file, unlock it and return to directory view
325 #
326 # Params: 1. Reference to user input hash
327 # 2. Reference to config hash
328 #
329 # Return: Output of the command (Scalar Reference)
330
331 sub exec_endedit($$)
332 {
333 my ($data,$config) = @_;
334 my $physical = $data->{'physical'};
335 my $virtual = $data->{'virtual'};
336 my $dir = upper_path($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'},$dir,{FILE => $virtual}) if($uselist->in_use($virtual));
369 }
370
371 return error($config->{'errors'}->{'text_to_binary'},$dir) unless(-T $physical);
372 return error($config->{'errors'}->{'editdir'},$dir) if(-d $physical);
373 return error($config->{'errors'}->{'noedit'}, $dir) 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 => $dir});
380 }
381 else
382 {
383 return error($config->{'errors'}->{'edit_failed'},$dir,{FILE => $virtual});
384 }
385 }
386
387 # exec_mkfile()
388 #
389 # Create a file and return to directory view
390 #
391 # Params: 1. Reference to user input hash
392 # 2. Reference to config hash
393 #
394 # Return: Output of the command (Scalar Reference)
395
396 sub exec_mkfile($$)
397 {
398 my ($data,$config) = @_;
399 my $new_physical = $data->{'new_physical'};
400 my $new_virtual = $data->{'new_virtual'};
401 my $dir = upper_path($new_virtual);
402 $new_virtual = encode_entities($new_virtual);
403
404 if($new_physical)
405 {
406 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
407
408 file_create($new_physical) or return error($config->{'errors'}->{'mkfile_failed'},$dir,{FILE => $new_virtual});
409 return devedit_reload({command => 'show', file => $dir});
410 }
411 else
412 {
413 my $tpl = new Template;
414 $tpl->read_file($config->{'templates'}->{'mkfile'});
415
416 $tpl->fillin("DIR","/");
417 $tpl->fillin("SCRIPT",$script);
418
419 my $output = header(-type => "text/html");
420 $output .= $tpl->get_template;
421
422 return \$output;
423 }
424 }
425
426 # exec_mkdir()
427 #
428 # Create a directory and return to directory view
429 #
430 # Params: 1. Reference to user input hash
431 # 2. Reference to config hash
432 #
433 # Return: Output of the command (Scalar Reference)
434
435 sub exec_mkdir($$)
436 {
437 my ($data,$config) = @_;
438 my $new_physical = $data->{'new_physical'};
439 my $new_virtual = $data->{'new_virtual'};
440 my $dir = upper_path($new_virtual);
441 $new_virtual = encode_entities($new_virtual);
442
443 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 return error($config->{'errors'}->{'no_directory'},upper_path($virtual),{FILE => $virtual}) unless(-d $physical);
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'},upper_path($virtual)) if(-d $physical);
544 return error($config->{'errors'}->{'nocopy'},upper_path($virtual)) 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 # System supports user and groups
770
771 if(-o $physical)
772 {
773 # We own this file
774
775 if($mode || $group)
776 {
777 if($mode)
778 {
779 # Change the mode
780
781 my $oct_mode = $mode;
782 $oct_mode = "0".$oct_mode if(length($oct_mode) == 3);
783 $oct_mode = oct($oct_mode);
784
785 chmod($oct_mode,$physical);
786 }
787
788 if($group)
789 {
790 # Change the group using the `chgrp` system command
791
792 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => encode_entities($group)}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
793 system("chgrp",$group,$physical);
794 }
795
796 return devedit_reload({command => 'show', file => $dir});
797 }
798 else
799 {
800 # Display the form
801
802 my @stat = stat($physical);
803
804 my $mode = $stat[2];
805 my $mode_oct = substr(sprintf("%04o",$mode),-4);
806 my $gid = $stat[5];
807
808 my $tpl = new Template;
809 $tpl->read_file($config->{'templates'}->{'chprop'});
810
811 # Insert file properties into the template
812
813 $tpl->fillin("MODE_OCTAL",$mode_oct);
814 $tpl->fillin("MODE_STRING",mode_string($mode));
815 $tpl->fillin("GID",$gid);
816
817 if(my $group = getgrgid($gid))
818 {
819 $tpl->fillin("GROUP",encode_entities($group));
820 $tpl->parse_if_block("group_detected",1);
821 }
822 else
823 {
824 $tpl->parse_if_block("group_detected",0);
825 }
826
827 # Insert other information
828
829 $tpl->fillin("FILE",$virtual);
830 $tpl->fillin("DIR",$dir);
831 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
832 $tpl->fillin("SCRIPT",$script);
833
834 my $output = header(-type => "text/html");
835 $output .= $tpl->get_template;
836
837 return \$output;
838 }
839 }
840 else
841 {
842 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => $virtual});
843 }
844 }
845 else
846 {
847 return error($config->{'errors'}->{'no_users'},$dir,{FILE => $virtual});
848 }
849 }
850
851 # exec_unlock()
852 #
853 # Remove a file from the list of used files and
854 # return to directory view
855 #
856 # Params: 1. Reference to user input hash
857 # 2. Reference to config hash
858 #
859 # Return: Output of the command (Scalar Reference)
860
861 sub exec_unlock($$)
862 {
863 my ($data,$config) = @_;
864 my $virtual = $data->{'virtual'};
865 my $uselist = $data->{'uselist'};
866
867 return devedit_reload({command => 'show', file => upper_path($virtual)}) if($uselist->unused($virtual));
868
869 if($data->{'cgi'}->param('confirmed'))
870 {
871 file_unlock($uselist,$virtual);
872 return devedit_reload({command => 'show', file => upper_path($virtual)});
873 }
874 else
875 {
876 my $tpl = new Template;
877 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
878
879 $tpl->fillin("FILE",$virtual);
880 $tpl->fillin("DIR",upper_path($virtual));
881 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
882 $tpl->fillin("SCRIPT",$script);
883
884 my $output = header(-type => "text/html");
885 $output .= $tpl->get_template;
886
887 return \$output;
888 }
889 }
890
891 # exec_about()
892 #
893 # Display some information about Dev-Editor
894 #
895 # Params: 1. Reference to user input hash
896 # 2. Reference to config hash
897 #
898 # Return: Output of the command (Scalar Reference)
899
900 sub exec_about($$)
901 {
902 my ($data,$config) = @_;
903
904 my $tpl = new Template;
905 $tpl->read_file($config->{'templates'}->{'about'});
906
907 $tpl->fillin("SCRIPT",$script);
908
909 # Dev-Editor's version number
910
911 $tpl->fillin("VERSION",$data->{'version'});
912
913 # Some path information
914
915 $tpl->fillin("SCRIPT_PHYS",encode_entities($ENV{'SCRIPT_FILENAME'}));
916 $tpl->fillin("CONFIG_PATH",encode_entities($data->{'configfile'}));
917 $tpl->fillin("FILE_ROOT", encode_entities($config->{'fileroot'}));
918 $tpl->fillin("HTTP_ROOT", encode_entities($config->{'httproot'}));
919
920 # Perl
921
922 $tpl->fillin("PERL_PROG",encode_entities($^X));
923 $tpl->fillin("PERL_VER",sprintf("%vd",$^V));
924
925 # Information about the server
926
927 $tpl->fillin("HTTPD",encode_entities($ENV{'SERVER_SOFTWARE'}));
928 $tpl->fillin("OS",$^O);
929 $tpl->fillin("TIME",encode_entities(strftime($config->{'timeformat'},localtime)));
930
931 # Process information
932
933 $tpl->fillin("PID",$$);
934
935 # Check if the functions getpwuid() and getgrgid() are available
936
937 if($users)
938 {
939 # Dev-Editor is running on a system which allows users and groups
940 # So we display the user and the group of our process
941
942 my $uid = POSIX::getuid;
943 my $gid = POSIX::getgid;
944
945 $tpl->parse_if_block("users",1);
946
947 # ID's of user and group
948
949 $tpl->fillin("UID",$uid);
950 $tpl->fillin("GID",$gid);
951
952 # Names of user and group
953
954 if(my $user = getpwuid($uid))
955 {
956 $tpl->fillin("USER",encode_entities($user));
957 $tpl->parse_if_block("user_detected",1);
958 }
959 else
960 {
961 $tpl->parse_if_block("user_detected",0);
962 }
963
964 if(my $group = getgrgid($gid))
965 {
966 $tpl->fillin("GROUP",encode_entities($group));
967 $tpl->parse_if_block("group_detected",1);
968 }
969 else
970 {
971 $tpl->parse_if_block("group_detected",0);
972 }
973
974 # Process umask
975
976 $tpl->fillin("UMASK",sprintf("%04o",umask));
977 }
978 else
979 {
980 $tpl->parse_if_block("users",0);
981 }
982
983 my $output = header(-type => "text/html");
984 $output .= $tpl->get_template;
985
986 return \$output;
987 }
988
989 # it's true, baby ;-)
990
991 1;
992
993 #
994 ### End ###

patrick-canterino.de