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

patrick-canterino.de