]> git.p6c8.net - devedit.git/blob - modules/Command.pm
9f6b970aff283f58cee28af30da52b7c19ec64d5
[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 <patshaping@gmx.net>
9 # Last modified: 2004-03-09
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT);
15
16 use File::Access;
17 use File::Copy;
18 use File::Path;
19
20 use POSIX qw(strftime);
21 use Tool;
22
23 use CGI qw(header);
24 use HTML::Entities;
25 use Output;
26 use Template;
27
28 my $script = $ENV{'SCRIPT_NAME'};
29
30 my %dispatch = ('show' => \&exec_show,
31 'beginedit' => \&exec_beginedit,
32 'canceledit' => \&exec_canceledit,
33 'endedit' => \&exec_endedit,
34 'mkdir' => \&exec_mkdir,
35 'mkfile' => \&exec_mkfile,
36 'upload' => \&exec_upload,
37 'copy' => \&exec_copy,
38 'rename' => \&exec_rename,
39 'remove' => \&exec_remove,
40 'unlock' => \&exec_unlock,
41 'about' => \&exec_about
42 );
43
44 ### Export ###
45
46 use base qw(Exporter);
47
48 @EXPORT = qw(exec_command);
49
50 # exec_command()
51 #
52 # Execute the specified command
53 #
54 # Params: 1. Command to execute
55 # 2. Reference to user input hash
56 # 3. Reference to config hash
57 #
58 # Return: Output of the command (Scalar Reference)
59
60 sub exec_command($$$)
61 {
62 my ($command,$data,$config) = @_;
63
64 return error($config->{'err_cmd_unknown'},'/',{COMMAND => $command}) unless($dispatch{$command});
65
66 my $output = &{$dispatch{$command}}($data,$config);
67 return $output;
68 }
69
70 # exec_show()
71 #
72 # View a directory or a file
73 #
74 # Params: 1. Reference to user input hash
75 # 2. Reference to config hash
76 #
77 # Return: Output of the command (Scalar Reference)
78
79 sub exec_show($$)
80 {
81 my ($data,$config) = @_;
82 my $physical = $data->{'physical'};
83 my $virtual = $data->{'virtual'};
84
85 my $tpl = new Template;
86
87 if(-d $physical)
88 {
89 # Create directory listing
90
91 my $direntries = dir_read($physical);
92 return error($config->{'dir_read_failed'},upper_path($virtual),{DIR => '$virtual'}) unless($direntries);
93
94 my $files = $direntries->{'files'};
95 my $dirs = $direntries->{'dirs'};
96
97 my $dirlist = "";
98
99 # Create the link to the upper directory
100 # (only if we are not in the root directory)
101
102 unless($virtual eq "/")
103 {
104 my @stat = stat($physical."/..");
105
106 my $udtpl = new Template;
107 $udtpl->read_file($config->{'tpl_dirlist_up'});
108
109 $udtpl->fillin("UPPER_DIR",encode_entities(upper_path($virtual)));
110 $udtpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
111
112 $dirlist .= $udtpl->get_template;
113 }
114
115 # Directories
116
117 foreach my $dir(@$dirs)
118 {
119 my @stat = stat($physical."/".$dir);
120 my $virt_path = encode_entities($virtual.$dir."/");
121
122 my $dtpl = new Template;
123 $dtpl->read_file($config->{'tpl_dirlist_dir'});
124
125 $dtpl->fillin("DIR",$virt_path);
126 $dtpl->fillin("DIR_NAME",$dir);
127 $dtpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
128
129 $dirlist .= $dtpl->get_template;
130 }
131
132 # Files
133
134 foreach my $file(@$files)
135 {
136 my $phys_path = $physical."/".$file;
137 my $virt_path = encode_entities($virtual.$file);
138
139 my @stat = stat($phys_path);
140 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
141
142 my $ftpl = new Template;
143 $ftpl->read_file($config->{'tpl_dirlist_file'});
144
145 $ftpl->fillin("FILE",$virt_path);
146 $ftpl->fillin("FILE_NAME",$file);
147 $ftpl->fillin("SIZE",$stat[7]);
148 $ftpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
149
150 $ftpl->parse_if_block("not_readable",not -r $phys_path);
151 $ftpl->parse_if_block("binary",-B $phys_path);
152 $ftpl->parse_if_block("readonly",not -w $phys_path);
153
154 $ftpl->parse_if_block("viewable",-r $phys_path && -T $phys_path);
155 $ftpl->parse_if_block("editable",-w $phys_path && -r $phys_path && -T $phys_path && not $in_use);
156
157 $ftpl->parse_if_block("in_use",$in_use);
158 $ftpl->parse_if_block("unused",not $in_use);
159
160 $dirlist .= $ftpl->get_template;
161 }
162
163 $tpl->read_file($config->{'tpl_dirlist'});
164
165 $tpl->fillin("DIRLIST",$dirlist);
166 $tpl->fillin("DIR",$virtual);
167 $tpl->fillin("SCRIPT",$script);
168 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
169 }
170 else
171 {
172 # View a file
173
174 return error($config->{'err_noview'},upper_path($virtual)) unless(-r $physical);
175
176 # Check on binary files
177 # We have to do it in this way, or empty files
178 # will be recognized as binary files
179
180 unless(-T $physical)
181 {
182 # Binary file
183
184 return error($config->{'err_binary'},upper_path($virtual));
185 }
186 else
187 {
188 # Text file
189
190 my $content = file_read($physical);
191 $$content =~ s/\015\012|\012|\015/\n/g;
192
193 $tpl->read_file($config->{'tpl_viewfile'});
194
195 $tpl->fillin("FILE",$virtual);
196 $tpl->fillin("DIR",upper_path($virtual));
197 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
198 $tpl->fillin("SCRIPT",$script);
199 $tpl->fillin("CONTENT",encode_entities($$content));
200 }
201 }
202
203 my $output = header(-type => "text/html");
204 $output .= $tpl->get_template;
205
206 return \$output;
207 }
208
209 # exec_beginedit
210 #
211 # Lock a file and display a form to edit it
212 #
213 # Params: 1. Reference to user input hash
214 # 2. Reference to config hash
215 #
216 # Return: Output of the command (Scalar Reference)
217
218 sub exec_beginedit($$)
219 {
220 my ($data,$config) = @_;
221 my $physical = $data->{'physical'};
222 my $virtual = $data->{'virtual'};
223 my $uselist = $data->{'uselist'};
224
225 return error($config->{'err_editdir'},upper_path($virtual)) if(-d $physical);
226 return error($config->{'err_in_use'},upper_path($virtual),{FILE => $virtual}) if($uselist->in_use($virtual));
227 return error($config->{'err_noedit'},upper_path($virtual)) unless(-r $physical && -w $physical);
228
229 # Check on binary files
230
231 unless(-T $physical)
232 {
233 # Binary file
234
235 return error($config->{'err_binary'},upper_path($virtual));
236 }
237 else
238 {
239 # Text file
240
241 $uselist->add_file($virtual);
242 $uselist->save;
243
244 my $content = file_read($physical);
245 $$content =~ s/\015\012|\012|\015/\n/g;
246
247 my $tpl = new Template;
248 $tpl->read_file($config->{'tpl_editfile'});
249
250 $tpl->fillin("FILE",$virtual);
251 $tpl->fillin("DIR",upper_path($virtual));
252 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
253 $tpl->fillin("SCRIPT",$script);
254 $tpl->fillin("CONTENT",encode_entities($$content));
255
256 my $output = header(-type => "text/html");
257 $output .= $tpl->get_template;
258
259 return \$output;
260 }
261 }
262
263 # exec_canceledit()
264 #
265 # Abort file editing
266 #
267 # Params: 1. Reference to user input hash
268 # 2. Reference to config hash
269 #
270 # Return: Output of the command (Scalar Reference)
271
272 sub exec_canceledit($$)
273 {
274 my ($data,$config) = @_;
275 my $virtual = $data->{'virtual'};
276
277 file_unlock($data->{'uselist'},$virtual);
278 return devedit_reload({command => 'show', file => upper_path($virtual)});
279 }
280
281 # exec_endedit()
282 #
283 # Save a file, unlock it and return to directory view
284 #
285 # Params: 1. Reference to user input hash
286 # 2. Reference to config hash
287 #
288 # Return: Output of the command (Scalar Reference)
289
290 sub exec_endedit($$)
291 {
292 my ($data,$config) = @_;
293 my $physical = $data->{'physical'};
294 my $virtual = $data->{'virtual'};
295 my $content = $data->{'cgi'}->param('filecontent');
296 my $uselist = $data->{'uselist'};
297
298 # Normalize newlines
299
300 $content =~ s/\015\012|\012|\015/\n/g;
301
302 if($data->{'cgi'}->param('encode_iso'))
303 {
304 # Encode all ISO-8859-1 special chars
305
306 $content = encode_entities($content,"\200-\377");
307 }
308
309 if($data->{'cgi'}->param('saveas'))
310 {
311 # Create the new filename
312
313 $physical = $data->{'new_physical'};
314 $virtual = $data->{'new_virtual'};
315
316 # Check if someone else is editing the new file
317
318 return error($config->{'err_in_use'},upper_path($virtual),{FILE => $virtual}) if($uselist->in_use($virtual));
319 }
320
321 return error($config->{'err_editdir'},upper_path($virtual)) if(-d $physical);
322 return error($config->{'err_noedit'}, upper_path($virtual)) unless(-r $physical && -w $physical);
323
324 if(file_save($physical,\$content))
325 {
326 # Saving of the file was successful - so unlock it!
327
328 file_unlock($uselist,$data->{'virtual'});
329 # ^^^^^^^^^^^^^^^^^^
330 # Maybe the user saved the file using another filename...
331 # But we have to unlock the original file!
332
333 return devedit_reload({command => 'show', file => upper_path($virtual)});
334 }
335 else
336 {
337 return error($config->{'err_edit_failed'},upper_path($virtual),{FILE => $virtual});
338 }
339 }
340
341 # exec_mkfile()
342 #
343 # Create a file and return to directory view
344 #
345 # Params: 1. Reference to user input hash
346 # 2. Reference to config hash
347 #
348 # Return: Output of the command (Scalar Reference)
349
350 sub exec_mkfile($$)
351 {
352 my ($data,$config) = @_;
353 my $new_physical = $data->{'new_physical'};
354 my $new_virtual = $data->{'new_virtual'};
355 my $dir = upper_path($new_virtual);
356 $new_virtual = encode_entities($new_virtual);
357
358 return error($config->{'err_file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
359
360 file_create($new_physical) or return error($config->{'err_mkfile_failed'},$dir,{FILE => $new_virtual});
361 return devedit_reload({command => 'show', file => $dir});
362 }
363
364 # exec_mkdir()
365 #
366 # Create a directory and return to directory view
367 #
368 # Params: 1. Reference to user input hash
369 # 2. Reference to config hash
370 #
371 # Return: Output of the command (Scalar Reference)
372
373 sub exec_mkdir($$)
374 {
375 my ($data,$config) = @_;
376 my $new_physical = $data->{'new_physical'};
377 my $new_virtual = $data->{'new_virtual'};
378 my $dir = upper_path($new_virtual);
379 $new_virtual = encode_entities($new_virtual);
380
381 return error($config->{'err_file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
382
383 mkdir($new_physical,0777) or return error($config->{'err_mkdir_failed'},$dir,{DIR => $new_virtual});
384 return devedit_reload({command => 'show', file => $dir});
385 }
386
387 # exec_upload()
388 #
389 # Upload a file
390 #
391 # Params: 1. Reference to user input hash
392 # 2. Reference to config hash
393 #
394 # Return: Output of the command (Scalar Reference)
395
396 sub exec_upload($$)
397 {
398 my ($data,$config) = @_;
399 my $physical = $data->{'physical'};
400 my $virtual = $data->{'virtual'};
401 my $cgi = $data->{'cgi'};
402
403 if(my $uploaded_file = $cgi->param('uploaded_file'))
404 {
405 # Process file upload
406
407 my $filename = file_name($uploaded_file);
408 my $file_phys = $physical."/".$filename;
409 my $file_virt = $virtual."".$filename;
410
411 return error($config->{'err_file_exists'},$virtual,{FILE => $file_virt}) if(-e $file_phys);
412
413 my $ascii = $cgi->param('ascii');
414 my $handle = $cgi->upload('uploaded_file');
415
416 local *FILE;
417
418 open(FILE,">$file_phys") or return error($config->{'err_mkfile_failed'},$virtual,{FILE => $file_virt});
419 binmode(FILE) unless($ascii);
420
421 my $data;
422
423 while(read($handle,$data,1024))
424 {
425 $data =~ s/\015\012|\012|\015/\n/g if($ascii);
426 print FILE $data;
427 }
428
429 close(FILE);
430
431 return devedit_reload({command => "show", file => $virtual});
432 }
433 else
434 {
435 my $tpl = new Template;
436 $tpl->read_file($config->{'tpl_upload'});
437
438 $tpl->fillin("DIR",$virtual);
439 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
440 $tpl->fillin("SCRIPT",$script);
441
442 my $output = header(-type => "text/html");
443 $output .= $tpl->get_template;
444
445 return \$output;
446 }
447 }
448
449 # exec_copy()
450 #
451 # Copy a file and return to directory view
452 #
453 # Params: 1. Reference to user input hash
454 # 2. Reference to config hash
455 #
456 # Return: Output of the command (Scalar Reference)
457
458 sub exec_copy($$)
459 {
460 my ($data,$config) = @_;
461 my $physical = $data->{'physical'};
462 my $virtual = encode_entities($data->{'virtual'});
463 my $new_physical = $data->{'new_physical'};
464
465 return error($config->{'err_nocopy'}) unless(-r $physical);
466
467 if($new_physical)
468 {
469 my $new_virtual = $data->{'new_virtual'};
470 my $dir = upper_path($new_virtual);
471 $new_virtual = encode_entities($new_virtual);
472
473 if(-e $new_physical)
474 {
475 return error($config->{'err_exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
476
477 if(-d $new_physical)
478 {
479 return error($config->{'err_dircopy'});
480 }
481 elsif(not $data->{'cgi'}->param('confirmed'))
482 {
483 my $tpl = new Template;
484 $tpl->read_file($config->{'tpl_confirm_replace'});
485
486 $tpl->fillin("FILE",$virtual);
487 $tpl->fillin("NEW_FILE",$new_virtual);
488 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
489 $tpl->fillin("NEW_DIR",$dir);
490 $tpl->fillin("DIR",upper_path($virtual));
491
492 $tpl->fillin("COMMAND","copy");
493 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
494 $tpl->fillin("SCRIPT",$script);
495
496 my $output = header(-type => "text/html");
497 $output .= $tpl->get_template;
498
499 return \$output;
500 }
501 }
502
503 copy($physical,$new_physical) or return error($config->{'err_copy_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
504 return devedit_reload({command => 'show', file => $dir});
505 }
506 else
507 {
508 my $tpl = new Template;
509 $tpl->read_file($config->{'tpl_copyfile'});
510
511 $tpl->fillin("FILE",$virtual);
512 $tpl->fillin("DIR",upper_path($virtual));
513 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
514 $tpl->fillin("SCRIPT",$script);
515
516 my $output = header(-type => "text/html");
517 $output .= $tpl->get_template;
518
519 return \$output;
520 }
521 }
522
523 # exec_rename()
524 #
525 # Rename/move a file and return to directory view
526 #
527 # Params: 1. Reference to user input hash
528 # 2. Reference to config hash
529 #
530 # Return: Output of the command (Scalar Reference)
531
532 sub exec_rename($$)
533 {
534 my ($data,$config) = @_;
535 my $physical = $data->{'physical'};
536 my $virtual = $data->{'virtual'};
537 my $new_physical = $data->{'new_physical'};
538
539 return error($config->{'err_in_use'},upper_path($virtual),{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
540
541 if($new_physical)
542 {
543 my $new_virtual = $data->{'new_virtual'};
544 my $dir = upper_path($new_virtual);
545 $new_virtual = encode_entities($new_virtual);
546
547 if(-e $new_physical)
548 {
549 return error($config->{'err_exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
550
551 if(-d $new_physical)
552 {
553 return error($config->{'err_dircopy'});
554 }
555 elsif(not $data->{'cgi'}->param('confirmed'))
556 {
557 my $tpl = new Template;
558 $tpl->read_file($config->{'tpl_confirm_replace'});
559
560 $tpl->fillin("FILE",$virtual);
561 $tpl->fillin("NEW_FILE",$new_virtual);
562 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
563 $tpl->fillin("NEW_DIR",$dir);
564 $tpl->fillin("DIR",upper_path($virtual));
565
566 $tpl->fillin("COMMAND","rename");
567 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
568 $tpl->fillin("SCRIPT",$script);
569
570 my $output = header(-type => "text/html");
571 $output .= $tpl->get_template;
572
573 return \$output;
574 }
575 }
576
577 rename($physical,$new_physical) or return error($config->{'err_rename_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
578 return devedit_reload({command => 'show', file => $dir});
579 }
580 else
581 {
582 my $tpl = new Template;
583 $tpl->read_file($config->{'tpl_renamefile'});
584
585 $tpl->fillin("FILE",$virtual);
586 $tpl->fillin("DIR",upper_path($virtual));
587 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
588 $tpl->fillin("SCRIPT",$script);
589
590 my $output = header(-type => "text/html");
591 $output .= $tpl->get_template;
592
593 return \$output;
594 }
595 }
596
597 # exec_remove()
598 #
599 # Remove a file or a directory and return to directory view
600 #
601 # Params: 1. Reference to user input hash
602 # 2. Reference to config hash
603 #
604 # Return: Output of the command (Scalar Reference)
605
606 sub exec_remove($$)
607 {
608 my ($data,$config) = @_;
609 my $physical = $data->{'physical'};
610 my $virtual = $data->{'virtual'};
611
612 if(-d $physical)
613 {
614 # Remove a directory
615
616 if($data->{'cgi'}->param('confirmed'))
617 {
618 rmtree($physical);
619 return devedit_reload({command => 'show', file => upper_path($virtual)});
620 }
621 else
622 {
623 my $tpl = new Template;
624 $tpl->read_file($config->{'tpl_confirm_rmdir'});
625
626 $tpl->fillin("DIR",$virtual);
627 $tpl->fillin("UPPER_DIR",upper_path($virtual));
628 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
629 $tpl->fillin("SCRIPT",$script);
630
631 my $output = header(-type => "text/html");
632 $output .= $tpl->get_template;
633
634 return \$output;
635 }
636 }
637 else
638 {
639 # Remove a file
640
641 return error($config->{'err_in_use'},upper_path($virtual),{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
642
643 if($data->{'cgi'}->param('confirmed'))
644 {
645 unlink($physical) or return error($config->{'err_delete_failed'},upper_path($virtual),{FILE => $virtual});
646 return devedit_reload({command => 'show', file => upper_path($virtual)});
647 }
648 else
649 {
650 my $tpl = new Template;
651 $tpl->read_file($config->{'tpl_confirm_rmfile'});
652
653 $tpl->fillin("FILE",$virtual);
654 $tpl->fillin("DIR",upper_path($virtual));
655 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
656 $tpl->fillin("SCRIPT",$script);
657
658 my $output = header(-type => "text/html");
659 $output .= $tpl->get_template;
660
661 return \$output;
662 }
663 }
664 }
665
666 # exec_unlock()
667 #
668 # Remove a file from the list of used files and
669 # return to directory view
670 #
671 # Params: 1. Reference to user input hash
672 # 2. Reference to config hash
673 #
674 # Return: Output of the command (Scalar Reference)
675
676 sub exec_unlock($$)
677 {
678 my ($data,$config) = @_;
679 my $virtual = $data->{'virtual'};
680
681 if($data->{'cgi'}->param('confirmed'))
682 {
683 file_unlock($data->{'uselist'},$virtual);
684 return devedit_reload({command => 'show', file => upper_path($virtual)});
685 }
686 else
687 {
688 my $tpl = new Template;
689 $tpl->read_file($config->{'tpl_confirm_unlock'});
690
691 $tpl->fillin("FILE",$virtual);
692 $tpl->fillin("DIR",upper_path($virtual));
693 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
694 $tpl->fillin("SCRIPT",$script);
695
696 my $output = header(-type => "text/html");
697 $output .= $tpl->get_template;
698
699 return \$output;
700 }
701 }
702
703 # exec_about()
704 #
705 # Display some information about Dev-Editor
706 #
707 # Params: 1. Reference to user input hash
708 # 2. Reference to config hash
709 #
710 # Return: Output of the command (Scalar Reference)
711
712 sub exec_about($$)
713 {
714 my ($data,$config) = @_;
715
716 my $tpl = new Template;
717 $tpl->read_file($config->{'tpl_about'});
718
719 $tpl->fillin("SCRIPT",$script);
720
721 # Dev-Editor's version number
722
723 $tpl->fillin("VERSION",$data->{'version'});
724
725 # Some path information
726
727 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
728 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
729 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
730 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
731
732 # Perl
733
734 $tpl->fillin("PERL_PROG",$^X);
735 $tpl->fillin("PERL_VER",sprintf("%vd",$^V));
736
737 # Information about the server
738
739 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
740 $tpl->fillin("OS",$^O);
741 $tpl->fillin("TIME",strftime($config->{'timeformat'},localtime));
742
743 # Process information
744
745 $tpl->fillin("PID",$$);
746
747 # Check if the functions getpwuid() and getgrgid() are available
748
749 if(eval("getpwuid(0)") && eval("getgrgid(0)"))
750 {
751 # Dev-Editor is running on a system which allows users and groups
752 # So we display the user and the group of our process
753
754 $tpl->parse_if_block("users",1);
755
756 # ID's of user and group
757
758 $tpl->fillin("UID",$<);
759 $tpl->fillin("GID",$();
760
761 # Names of user and group
762
763 $tpl->fillin("USER",getpwuid($<));
764 $tpl->fillin("GROUP",getgrgid($());
765 }
766 else
767 {
768 $tpl->parse_if_block("users",0);
769 }
770
771 my $output = header(-type => "text/html");
772 $output .= $tpl->get_template;
773
774 return \$output;
775 }
776
777 # it's true, baby ;-)
778
779 1;
780
781 #
782 ### End ###

patrick-canterino.de