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

patrick-canterino.de