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

patrick-canterino.de