]> git.p6c8.net - devedit.git/blob - modules/Command.pm
cddb5ca37a2e3559fd8c1d7c060fb6cbb1fa905c
[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-23
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($config->{'err_cmd_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 $tpl->read_file($config->{'tpl_dirlist'});
162
163 $tpl->fillin("DIRLIST",$dirlist);
164 $tpl->fillin("DIR",$virtual);
165 $tpl->fillin("SCRIPT",$script);
166 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
167 }
168 else
169 {
170 # View a file
171
172 return error($config->{'err_noview'},upper_path($virtual)) unless(-r $physical);
173
174 # Check on binary files
175 # We have to do it in this way, or empty files
176 # will be recognized as binary files
177
178 unless(-T $physical)
179 {
180 # Binary file
181
182 return error($config->{'err_binary'},upper_path($virtual));
183 }
184 else
185 {
186 # Text file
187
188 my $content = file_read($physical);
189 $$content =~ s/\015\012|\012|\015/\n/g;
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 $$content =~ s/\015\012|\012|\015/\n/g;
244
245 my $tpl = new Template;
246 $tpl->read_file($config->{'tpl_editfile'});
247
248 $tpl->fillin("FILE",$virtual);
249 $tpl->fillin("DIR",upper_path($virtual));
250 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
251 $tpl->fillin("SCRIPT",$script);
252 $tpl->fillin("CONTENT",encode_entities($$content));
253
254 my $output = header(-type => "text/html");
255 $output .= $tpl->get_template;
256
257 return \$output;
258 }
259 }
260
261 # exec_canceledit()
262 #
263 # Abort file editing
264 #
265 # Params: 1. Reference to user input hash
266 # 2. Reference to config hash
267 #
268 # Return: Output of the command (Scalar Reference)
269
270 sub exec_canceledit($$)
271 {
272 my ($data,$config) = @_;
273 my $virtual = $data->{'virtual'};
274
275 file_unlock($data->{'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 my $uselist = $data->{'uselist'};
295
296 # Normalize newlines
297
298 $content =~ s/\015\012|\012|\015/\n/g;
299
300 if($data->{'cgi'}->param('encode_iso'))
301 {
302 # Encode all ISO-8859-1 special chars
303
304 $content = encode_entities($content,"\200-\377");
305 }
306
307 if($data->{'cgi'}->param('saveas'))
308 {
309 # Create the new filename
310
311 $physical = $data->{'new_physical'};
312 $virtual = $data->{'new_virtual'};
313
314 # Check if someone else is editing the new file
315
316 return error_in_use($virtual) if($uselist->in_use($virtual));
317 }
318
319 return error($config->{'err_editdir'},upper_path($virtual)) if(-d $physical);
320 return error($config->{'err_noedit'}, upper_path($virtual)) unless(-r $physical && -w $physical);
321
322 if(file_save($physical,\$content))
323 {
324 # Saving of the file was successful - so unlock it!
325
326 file_unlock($uselist,$data->{'virtual'});
327 # ^^^^^^^^^^^^^^^^^^
328 # Maybe the user saved the file using another filename...
329 # But we have to unlock the original file!
330
331 return devedit_reload({command => 'show', file => upper_path($virtual)});
332 }
333 else
334 {
335 return error($config->{'err_edit_failed'},upper_path($virtual),{FILE => $virtual});
336 }
337 }
338
339 # exec_mkfile()
340 #
341 # Create a file and return to directory view
342 #
343 # Params: 1. Reference to user input hash
344 # 2. Reference to config hash
345 #
346 # Return: Output of the command (Scalar Reference)
347
348 sub exec_mkfile($$)
349 {
350 my ($data,$config) = @_;
351 my $new_physical = $data->{'new_physical'};
352 my $new_virtual = $data->{'new_virtual'};
353 my $dir = upper_path($new_virtual);
354 $new_virtual = encode_entities($new_virtual);
355
356 return error($config->{'err_file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
357
358 file_create($new_physical) or return error($config->{'err_mkfile_failed'},$dir,{FILE => $new_virtual});
359 return devedit_reload({command => 'show', file => $dir});
360 }
361
362 # exec_mkdir()
363 #
364 # Create a directory and return to directory view
365 #
366 # Params: 1. Reference to user input hash
367 # 2. Reference to config hash
368 #
369 # Return: Output of the command (Scalar Reference)
370
371 sub exec_mkdir($$)
372 {
373 my ($data,$config) = @_;
374 my $new_physical = $data->{'new_physical'};
375 my $new_virtual = $data->{'new_virtual'};
376 my $dir = upper_path($new_virtual);
377 $new_virtual = encode_entities($new_virtual);
378
379 return error($config->{'err_file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
380
381 mkdir($new_physical,0777) or return error($config->{'err_mkdir_failed'},$dir,{DIR => $new_virtual});
382 return devedit_reload({command => 'show', file => $dir});
383 }
384
385 # exec_copy()
386 #
387 # Copy a file and return to directory view
388 #
389 # Params: 1. Reference to user input hash
390 # 2. Reference to config hash
391 #
392 # Return: Output of the command (Scalar Reference)
393
394 sub exec_copy($$)
395 {
396 my ($data,$config) = @_;
397 my $physical = $data->{'physical'};
398 my $virtual = encode_entities($data->{'virtual'});
399 my $new_physical = $data->{'new_physical'};
400
401 return error($config->{'err_nocopy'}) unless(-r $physical);
402
403 if($new_physical)
404 {
405 my $new_virtual = $data->{'new_virtual'};
406 my $dir = upper_path($new_virtual);
407 $new_virtual = encode_entities($new_virtual);
408
409 if(-e $new_physical)
410 {
411 return error($config->{'err_exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
412
413 if(-d $new_physical)
414 {
415 return error($config->{'err_dircopy'});
416 }
417 elsif(not $data->{'cgi'}->param('confirmed'))
418 {
419 my $tpl = new Template;
420 $tpl->read_file($config->{'tpl_confirm_replace'});
421
422 $tpl->fillin("FILE",$virtual);
423 $tpl->fillin("NEW_FILE",$new_virtual);
424 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
425 $tpl->fillin("NEW_DIR",$dir);
426 $tpl->fillin("DIR",upper_path($virtual));
427 $tpl->fillin("COMMAND","copy");
428 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
429 $tpl->fillin("SCRIPT",$script);
430
431 my $output = header(-type => "text/html");
432 $output .= $tpl->get_template;
433
434 return \$output;
435 }
436 }
437
438 copy($physical,$new_physical) or return error($config->{'err_copy_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
439 return devedit_reload({command => 'show', file => $dir});
440 }
441 else
442 {
443 my $tpl = new Template;
444 $tpl->read_file($config->{'tpl_copyfile'});
445
446 $tpl->fillin("FILE",$virtual);
447 $tpl->fillin("DIR",upper_path($virtual));
448 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
449 $tpl->fillin("SCRIPT",$script);
450
451 my $output = header(-type => "text/html");
452 $output .= $tpl->get_template;
453
454 return \$output;
455 }
456 }
457
458 # exec_rename()
459 #
460 # Rename/move a file and return to directory view
461 #
462 # Params: 1. Reference to user input hash
463 # 2. Reference to config hash
464 #
465 # Return: Output of the command (Scalar Reference)
466
467 sub exec_rename($$)
468 {
469 my ($data,$config) = @_;
470 my $physical = $data->{'physical'};
471 my $virtual = $data->{'virtual'};
472 my $new_physical = $data->{'new_physical'};
473
474 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
475
476 if($new_physical)
477 {
478 my $new_virtual = $data->{'new_virtual'};
479 my $dir = upper_path($new_virtual);
480 $new_virtual = encode_entities($new_virtual);
481
482 if(-e $new_physical)
483 {
484 return error($config->{'err_exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
485
486 if(-d $new_physical)
487 {
488 return error($config->{'err_dircopy'});
489 }
490 elsif(not $data->{'cgi'}->param('confirmed'))
491 {
492 my $tpl = new Template;
493 $tpl->read_file($config->{'tpl_confirm_replace'});
494
495 $tpl->fillin("FILE",$virtual);
496 $tpl->fillin("NEW_FILE",$new_virtual);
497 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
498 $tpl->fillin("NEW_DIR",$dir);
499 $tpl->fillin("DIR",upper_path($virtual));
500 $tpl->fillin("COMMAND","rename");
501 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
502 $tpl->fillin("SCRIPT",$script);
503
504 my $output = header(-type => "text/html");
505 $output .= $tpl->get_template;
506
507 return \$output;
508 }
509 }
510
511 rename($physical,$new_physical) or return error($config->{'err_rename_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
512 return devedit_reload({command => 'show', file => $dir});
513 }
514 else
515 {
516 my $tpl = new Template;
517 $tpl->read_file($config->{'tpl_renamefile'});
518
519 $tpl->fillin("FILE",$virtual);
520 $tpl->fillin("DIR",upper_path($virtual));
521 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
522 $tpl->fillin("SCRIPT",$script);
523
524 my $output = header(-type => "text/html");
525 $output .= $tpl->get_template;
526
527 return \$output;
528 }
529 }
530
531 # exec_remove()
532 #
533 # Remove a file or a directory and return to directory view
534 #
535 # Params: 1. Reference to user input hash
536 # 2. Reference to config hash
537 #
538 # Return: Output of the command (Scalar Reference)
539
540 sub exec_remove($$)
541 {
542 my ($data,$config) = @_;
543 my $physical = $data->{'physical'};
544 my $virtual = $data->{'virtual'};
545
546 if(-d $physical)
547 {
548 # Remove a directory
549
550 if($data->{'cgi'}->param('confirmed'))
551 {
552 rmtree($physical);
553 return devedit_reload({command => 'show', file => upper_path($virtual)});
554 }
555 else
556 {
557 my $tpl = new Template;
558 $tpl->read_file($config->{'tpl_confirm_rmdir'});
559
560 $tpl->fillin("DIR",$virtual);
561 $tpl->fillin("UPPER_DIR",upper_path($virtual));
562 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
563 $tpl->fillin("SCRIPT",$script);
564
565 my $output = header(-type => "text/html");
566 $output .= $tpl->get_template;
567
568 return \$output;
569 }
570 }
571 else
572 {
573 # Remove a file
574
575 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
576
577 if($data->{'cgi'}->param('confirmed'))
578 {
579 unlink($physical) or return error($config->{'err_delete_failed'},upper_path($virtual),{FILE => $virtual});
580 return devedit_reload({command => 'show', file => upper_path($virtual)});
581 }
582 else
583 {
584 my $tpl = new Template;
585 $tpl->read_file($config->{'tpl_confirm_rmfile'});
586
587 $tpl->fillin("FILE",$virtual);
588 $tpl->fillin("DIR",upper_path($virtual));
589 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
590 $tpl->fillin("SCRIPT",$script);
591
592 my $output = header(-type => "text/html");
593 $output .= $tpl->get_template;
594
595 return \$output;
596 }
597 }
598 }
599
600 # exec_unlock()
601 #
602 # Remove a file from the list of used files and
603 # return to directory view
604 #
605 # Params: 1. Reference to user input hash
606 # 2. Reference to config hash
607 #
608 # Return: Output of the command (Scalar Reference)
609
610 sub exec_unlock($$)
611 {
612 my ($data,$config) = @_;
613 my $virtual = $data->{'virtual'};
614
615 if($data->{'cgi'}->param('confirmed'))
616 {
617 file_unlock($data->{'uselist'},$virtual);
618 return devedit_reload({command => 'show', file => upper_path($virtual)});
619 }
620 else
621 {
622 my $tpl = new Template;
623 $tpl->read_file($config->{'tpl_confirm_unlock'});
624
625 $tpl->fillin("FILE",$virtual);
626 $tpl->fillin("DIR",upper_path($virtual));
627 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
628 $tpl->fillin("SCRIPT",$script);
629
630 my $output = header(-type => "text/html");
631 $output .= $tpl->get_template;
632
633 return \$output;
634 }
635 }
636
637 # it's true, baby ;-)
638
639 1;
640
641 #
642 ### End ###

patrick-canterino.de