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

patrick-canterino.de