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

patrick-canterino.de