]> git.p6c8.net - devedit.git/blob - modules/Command.pm
4a5e7681b6ca4a42a8c72705897f96c8c52cb517
[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-02-20
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT);
15
16 use File::Access;
17 use File::Copy;
18 use File::Path;
19
20 use POSIX qw(strftime);
21 use Tool;
22
23 use CGI qw(header);
24 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 'copy' => \&exec_copy,
37 'rename' => \&exec_rename,
38 'remove' => \&exec_remove,
39 'unlock' => \&exec_unlock
40 );
41
42 ### Export ###
43
44 use base qw(Exporter);
45
46 @EXPORT = qw(exec_command);
47
48 # exec_command()
49 #
50 # Execute the specified command
51 #
52 # Params: 1. Command to execute
53 # 2. Reference to user input hash
54 # 3. Reference to config hash
55 #
56 # Return: Output of the command (Scalar Reference)
57
58 sub exec_command($$$)
59 {
60 my ($command,$data,$config) = @_;
61
62 return error("Unknown command: $command") unless($dispatch{$command});
63
64 my $output = &{$dispatch{$command}}($data,$config);
65 return $output;
66 }
67
68 # exec_show()
69 #
70 # View a directory or a file
71 #
72 # Params: 1. Reference to user input hash
73 # 2. Reference to config hash
74 #
75 # Return: Output of the command (Scalar Reference)
76
77 sub exec_show($$)
78 {
79 my ($data,$config) = @_;
80 my $physical = $data->{'physical'};
81 my $virtual = $data->{'virtual'};
82
83 my $tpl = new Template;
84
85 if(-d $physical)
86 {
87 # Create directory listing
88
89 my $direntries = dir_read($physical);
90 return error($config->{'dir_read_failed'},upper_path($virtual),{DIR => '$virtual'}) unless($direntries);
91
92 my $files = $direntries->{'files'};
93 my $dirs = $direntries->{'dirs'};
94
95 my $dirlist = "";
96
97 # Create the link to the upper directory
98 # (only if we are not in the root directory)
99
100 unless($virtual eq "/")
101 {
102 my @stat = stat($physical."/..");
103
104 my $udtpl = new Template;
105 $udtpl->read_file($config->{'tpl_dirlist_up'});
106
107 $udtpl->fillin("UPPER_DIR",encode_entities(upper_path($virtual)));
108 $udtpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
109
110 $dirlist .= $udtpl->get_template;
111 }
112
113 # Directories
114
115 foreach my $dir(@$dirs)
116 {
117 my @stat = stat($physical."/".$dir);
118 my $virt_path = encode_entities($virtual.$dir."/");
119
120 my $dtpl = new Template;
121 $dtpl->read_file($config->{'tpl_dirlist_dir'});
122
123 $dtpl->fillin("DIR",$virt_path);
124 $dtpl->fillin("DIR_NAME",$dir);
125 $dtpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
126
127 $dirlist .= $dtpl->get_template;
128 }
129
130 # Files
131
132 foreach my $file(@$files)
133 {
134 my $phys_path = $physical."/".$file;
135 my $virt_path = encode_entities($virtual.$file);
136
137 my @stat = stat($phys_path);
138 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
139
140 my $ftpl = new Template;
141 $ftpl->read_file($config->{'tpl_dirlist_file'});
142
143 $ftpl->fillin("FILE",$virt_path);
144 $ftpl->fillin("FILE_NAME",$file);
145 $ftpl->fillin("SIZE",$stat[7]);
146 $ftpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
147
148 $ftpl->parse_if_block("not_readable",not -r $phys_path);
149 $ftpl->parse_if_block("binary",-B $phys_path);
150 $ftpl->parse_if_block("readonly",not -w $phys_path);
151
152 $ftpl->parse_if_block("viewable",-r $phys_path && -T $phys_path);
153 $ftpl->parse_if_block("editable",-w $phys_path && -r $phys_path && -T $phys_path && not $in_use);
154
155 $ftpl->parse_if_block("in_use",$in_use);
156 $ftpl->parse_if_block("unused",not $in_use);
157
158 $dirlist .= $ftpl->get_template;
159 }
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
191 $tpl->read_file($config->{'tpl_viewfile'});
192
193 $tpl->fillin("FILE",$virtual);
194 $tpl->fillin("DIR",upper_path($virtual));
195 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
196 $tpl->fillin("SCRIPT",$script);
197 $tpl->fillin("CONTENT",encode_entities($$content));
198 }
199 }
200
201 my $output = header(-type => "text/html");
202 $output .= $tpl->get_template;
203
204 return \$output;
205 }
206
207 # exec_beginedit
208 #
209 # Lock a file and display a form to edit it
210 #
211 # Params: 1. Reference to user input hash
212 # 2. Reference to config hash
213 #
214 # Return: Output of the command (Scalar Reference)
215
216 sub exec_beginedit($$)
217 {
218 my ($data,$config) = @_;
219 my $physical = $data->{'physical'};
220 my $virtual = $data->{'virtual'};
221 my $uselist = $data->{'uselist'};
222
223 return error($config->{'err_editdir'},upper_path($virtual)) if(-d $physical);
224 return error_in_use($virtual) if($uselist->in_use($virtual));
225 return error($config->{'err_noedit'},upper_path($virtual)) unless(-r $physical && -w $physical);
226
227 # Check on binary files
228
229 unless(-T $physical)
230 {
231 # Binary file
232
233 return error($config->{'err_binary'},upper_path($virtual));
234 }
235 else
236 {
237 # Text file
238
239 $uselist->add_file($virtual);
240 $uselist->save;
241
242 my $content = file_read($physical);
243
244 my $tpl = new Template;
245 $tpl->read_file($config->{'tpl_editfile'});
246
247 $tpl->fillin("FILE",$virtual);
248 $tpl->fillin("DIR",upper_path($virtual));
249 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
250 $tpl->fillin("SCRIPT",$script);
251 $tpl->fillin("CONTENT",encode_entities($$content));
252
253 my $output = header(-type => "text/html");
254 $output .= $tpl->get_template;
255
256 return \$output;
257 }
258 }
259
260 # exec_canceledit()
261 #
262 # Abort file editing
263 #
264 # Params: 1. Reference to user input hash
265 # 2. Reference to config hash
266 #
267 # Return: Output of the command (Scalar Reference)
268
269 sub exec_canceledit($$)
270 {
271 my ($data,$config) = @_;
272 my $virtual = $data->{'virtual'};
273 my $uselist = $data->{'uselist'};
274
275 file_unlock($uselist,$virtual);
276 return devedit_reload({command => 'show', file => upper_path($virtual)});
277 }
278
279 # exec_endedit()
280 #
281 # Save a file, unlock it and return to directory view
282 #
283 # Params: 1. Reference to user input hash
284 # 2. Reference to config hash
285 #
286 # Return: Output of the command (Scalar Reference)
287
288 sub exec_endedit($$)
289 {
290 my ($data,$config) = @_;
291 my $physical = $data->{'physical'};
292 my $virtual = $data->{'virtual'};
293 my $content = $data->{'cgi'}->param('filecontent');
294
295 return error($config->{'err_editdir'},upper_path($virtual)) if(-d $physical);
296 return error($config->{'err_noedit'}, upper_path($virtual)) unless(-r $physical && -w $physical);
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
317 if(file_save($physical,\$content))
318 {
319 # Saving of the file was successful - so unlock it!
320
321 file_unlock($data->{'uselist'},$virtual);
322 return devedit_reload({command => 'show', file => upper_path($virtual)});
323 }
324 else
325 {
326 return error($config->{'err_editfailed'},upper_path($virtual),{FILE => $virtual});
327 }
328 }
329
330 # exec_mkfile()
331 #
332 # Create a file and return to directory view
333 #
334 # Params: 1. Reference to user input hash
335 # 2. Reference to config hash
336 #
337 # Return: Output of the command (Scalar Reference)
338
339 sub exec_mkfile($$)
340 {
341 my ($data,$config) = @_;
342 my $new_physical = $data->{'new_physical'};
343 my $new_virtual = $data->{'new_virtual'};
344 my $dir = upper_path($new_virtual);
345 $new_virtual = encode_entities($new_virtual);
346
347 return error("A file or directory called '$new_virtual' already exists.",$dir) if(-e $new_physical);
348
349 file_create($new_physical) or return error("Could not create file '$new_virtual'.",$dir);
350 return devedit_reload({command => 'show', file => $dir});
351 }
352
353 # exec_mkdir()
354 #
355 # Create a directory and return to directory view
356 #
357 # Params: 1. Reference to user input hash
358 # 2. Reference to config hash
359 #
360 # Return: Output of the command (Scalar Reference)
361
362 sub exec_mkdir($$)
363 {
364 my ($data,$config) = @_;
365 my $new_physical = $data->{'new_physical'};
366 my $new_virtual = $data->{'new_virtual'};
367 my $dir = upper_path($new_virtual);
368 $new_virtual = encode_entities($new_virtual);
369
370 return error("A file or directory called '$new_virtual' already exists.",$dir) if(-e $new_physical);
371
372 mkdir($new_physical,0777) or return error("Could not create directory '$new_virtual'.",$dir);
373 return devedit_reload({command => 'show', file => $dir});
374 }
375
376 # exec_copy()
377 #
378 # Copy a file and return to directory view
379 #
380 # Params: 1. Reference to user input hash
381 # 2. Reference to config hash
382 #
383 # Return: Output of the command (Scalar Reference)
384
385 sub exec_copy($$)
386 {
387 my ($data,$config) = @_;
388 my $physical = $data->{'physical'};
389 my $virtual = encode_entities($data->{'virtual'});
390 my $new_physical = $data->{'new_physical'};
391
392 return error($config->{'err_dircopy'}) if(-d $physical);
393 return error($config->{'err_nocopy'}) unless(-r $physical);
394
395 if($new_physical)
396 {
397 my $new_virtual = $data->{'new_virtual'};
398 my $dir = upper_path($new_virtual);
399 $new_virtual = encode_entities($new_virtual);
400
401 if(-e $new_physical)
402 {
403 if(-d $new_physical)
404 {
405 return error("A directory called '$new_virtual' already exists. You cannot replace a directory by a file!",$dir);
406 }
407 elsif(not $data->{'cgi'}->param('confirmed'))
408 {
409 my $tpl = new Template;
410 $tpl->read_file($config->{'tpl_confirm_replace'});
411
412 $tpl->fillin("FILE",$virtual);
413 $tpl->fillin("NEW_FILE",$new_virtual);
414 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
415 $tpl->fillin("NEW_DIR",$dir);
416 $tpl->fillin("DIR",upper_path($virtual));
417 $tpl->fillin("COMMAND","copy");
418 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
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 if($data->{'uselist'}->in_use($data->{'new_virtual'}))
429 {
430 return error("The target file '$new_virtual' already exists and it is edited by someone else.",$dir);
431 }
432
433 copy($physical,$new_physical) or return error("Could not copy '$virtual' to '$new_virtual'",upper_path($virtual));
434 return devedit_reload({command => 'show', file => $dir});
435 }
436 else
437 {
438 my $tpl = new Template;
439 $tpl->read_file($config->{'tpl_copyfile'});
440
441 $tpl->fillin("FILE",$virtual);
442 $tpl->fillin("DIR",upper_path($virtual));
443 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
444 $tpl->fillin("SCRIPT",$script);
445
446 my $output = header(-type => "text/html");
447 $output .= $tpl->get_template;
448
449 return \$output;
450 }
451 }
452
453 # exec_rename()
454 #
455 # Rename/move a file and return to directory view
456 #
457 # Params: 1. Reference to user input hash
458 # 2. Reference to config hash
459 #
460 # Return: Output of the command (Scalar Reference)
461
462 sub exec_rename($$)
463 {
464 my ($data,$config) = @_;
465 my $physical = $data->{'physical'};
466 my $virtual = $data->{'virtual'};
467 my $new_physical = $data->{'new_physical'};
468 my $new_virtual = $data->{'new_virtual'};
469 my $dir = upper_path($new_virtual);
470 $new_virtual = encode_entities($new_virtual);
471
472 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
473
474 if($new_physical)
475 {
476 my $new_virtual = $data->{'new_virtual'};
477 my $dir = upper_path($new_virtual);
478 $new_virtual = encode_entities($new_virtual);
479
480 if(-e $new_physical)
481 {
482 if(-d $new_physical)
483 {
484 return error("A directory called '$new_virtual' already exists. You cannot replace a directory by a file!",$dir);
485 }
486 elsif(not $data->{'cgi'}->param('confirmed'))
487 {
488 my $tpl = new Template;
489 $tpl->read_file($config->{'tpl_confirm_replace'});
490
491 $tpl->fillin("FILE",$virtual);
492 $tpl->fillin("NEW_FILE",$new_virtual);
493 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
494 $tpl->fillin("NEW_DIR",$dir);
495 $tpl->fillin("DIR",upper_path($virtual));
496 $tpl->fillin("COMMAND","rename");
497 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
498 $tpl->fillin("SCRIPT",$script);
499
500 my $output = header(-type => "text/html");
501 $output .= $tpl->get_template;
502
503 return \$output;
504 }
505 }
506
507 if($data->{'uselist'}->in_use($data->{'new_virtual'}))
508 {
509 return error("The target file '$new_virtual' already exists and it is edited by someone else.",$dir);
510 }
511
512 rename($physical,$new_physical) or return error("Could not move/rename '$virtual' to '$new_virtual'",upper_path($virtual));
513 return devedit_reload({command => 'show', file => $dir});
514 }
515 else
516 {
517 my $tpl = new Template;
518 $tpl->read_file($config->{'tpl_renamefile'});
519
520 $tpl->fillin("FILE",$virtual);
521 $tpl->fillin("DIR",upper_path($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_remove()
533 #
534 # Remove a file or a directory 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_remove($$)
542 {
543 my ($data,$config) = @_;
544 my $physical = $data->{'physical'};
545 my $virtual = $data->{'virtual'};
546
547 if(-d $physical)
548 {
549 # Remove a directory
550
551 if($data->{'cgi'}->param('confirmed'))
552 {
553 rmtree($physical);
554 return devedit_reload({command => 'show', file => upper_path($virtual)});
555 }
556 else
557 {
558 my $tpl = new Template;
559 $tpl->read_file($config->{'tpl_confirm_rmdir'});
560
561 $tpl->fillin("DIR",$virtual);
562 $tpl->fillin("UPPER_DIR",upper_path($virtual));
563 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
564 $tpl->fillin("SCRIPT",$script);
565
566 my $output = header(-type => "text/html");
567 $output .= $tpl->get_template;
568
569 return \$output;
570 }
571 }
572 else
573 {
574 # Remove a file
575
576 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
577
578 if($data->{'cgi'}->param('confirmed'))
579 {
580 unlink($physical) or return error($config->{'err_editfailed'},upper_path($virtual),{FILE => $virtual});
581 return devedit_reload({command => 'show', file => upper_path($virtual)});
582 }
583 else
584 {
585 my $tpl = new Template;
586 $tpl->read_file($config->{'tpl_confirm_rmfile'});
587
588 $tpl->fillin("FILE",$virtual);
589 $tpl->fillin("DIR",upper_path($virtual));
590 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
591 $tpl->fillin("SCRIPT",$script);
592
593 my $output = header(-type => "text/html");
594 $output .= $tpl->get_template;
595
596 return \$output;
597 }
598 }
599 }
600
601 # exec_unlock()
602 #
603 # Remove a file from the list of used files and
604 # return to directory view
605 #
606 # Params: 1. Reference to user input hash
607 # 2. Reference to config hash
608 #
609 # Return: Output of the command (Scalar Reference)
610
611 sub exec_unlock($$)
612 {
613 my ($data,$config) = @_;
614 my $virtual = $data->{'virtual'};
615 my $uselist = $data->{'uselist'};
616
617 if($data->{'cgi'}->param('confirmed'))
618 {
619 file_unlock($uselist,$virtual);
620 return devedit_reload({command => 'show', file => upper_path($virtual)});
621 }
622 else
623 {
624 my $tpl = new Template;
625 $tpl->read_file($config->{'tpl_confirm_unlock'});
626
627 $tpl->fillin("FILE",$virtual);
628 $tpl->fillin("DIR",upper_path($virtual));
629 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
630 $tpl->fillin("SCRIPT",$script);
631
632 my $output = header(-type => "text/html");
633 $output .= $tpl->get_template;
634
635 return \$output;
636 }
637 }
638
639 # it's true, baby ;-)
640
641 1;
642
643 #
644 ### End ###

patrick-canterino.de