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

patrick-canterino.de