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