]> git.p6c8.net - devedit.git/blob - modules/Command.pm
d84d42f0ab2dba1579ca3077aa11918adbfa7f2c
[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-06
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 my $output;
83
84 if(-d $physical)
85 {
86 # Create directory listing
87
88 my $direntries = dir_read($physical);
89 return error("Reading of directory $virtual failed.",upper_path($virtual)) unless($direntries);
90
91 my $files = $direntries->{'files'};
92 my $dirs = $direntries->{'dirs'};
93
94 my $dirlist = "";
95
96 # Create the link to the upper directory
97 # (only if we are not in the root directory)
98
99 unless($virtual eq "/")
100 {
101 my @stat = stat($physical."/..");
102
103 my $udtpl = new Template;
104 $udtpl->read_file($config->{'tpl_dirlist_up'});
105
106 $udtpl->fillin("UPPER_DIR",encode_entities(upper_path($virtual)));
107 $udtpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
108
109 $dirlist .= $udtpl->get_template;
110 }
111
112 # Directories
113
114 foreach my $dir(@$dirs)
115 {
116 my @stat = stat($physical."/".$dir);
117 my $virt_path = encode_entities($virtual.$dir."/");
118
119 my $dtpl = new Template;
120 $dtpl->read_file($config->{'tpl_dirlist_dir'});
121
122 $dtpl->fillin("DIR",$virt_path);
123 $dtpl->fillin("DIR_NAME",$dir);
124 $dtpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
125
126 $dirlist .= $dtpl->get_template;
127 }
128
129 # Files
130
131 foreach my $file(@$files)
132 {
133 my $phys_path = $physical."/".$file;
134 my $virt_path = encode_entities($virtual.$file);
135
136 my @stat = stat($phys_path);
137 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
138
139 my $ftpl = new Template;
140 $ftpl->read_file($config->{'tpl_dirlist_file'});
141
142 $ftpl->fillin("FILE",$virt_path);
143 $ftpl->fillin("FILE_NAME",$file);
144 $ftpl->fillin("SIZE",$stat[7]);
145 $ftpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
146
147 $ftpl->parse_if_block("not_readable",not -r $phys_path);
148 $ftpl->parse_if_block("binary",-B $phys_path);
149 $ftpl->parse_if_block("readonly",not -w $phys_path);
150
151 $ftpl->parse_if_block("viewable",-r $phys_path && -T $phys_path);
152 $ftpl->parse_if_block("editable",-w $phys_path && -r $phys_path && -T $phys_path && not $in_use);
153
154 $ftpl->parse_if_block("in_use",$in_use);
155 $ftpl->parse_if_block("unused",not $in_use);
156
157 $dirlist .= $ftpl->get_template;
158 }
159
160 my $tpl = new Template;
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 $output = header(-type => "text/html");
169 $output .= $tpl->get_template;
170 }
171 else
172 {
173 # View a file
174
175 return error("You have not enough permissions to view this file.",upper_path($virtual)) unless(-r $physical);
176
177 # Check on binary files
178 # We have to do it in this way, or empty files
179 # will be recognized as binary files
180
181 unless(-T $physical)
182 {
183 # Binary file
184
185 return error($config->{'err_binary'},upper_path($virtual));
186 }
187 else
188 {
189 # Text file
190
191 my $content = file_read($physical);
192
193 my $tpl = new Template;
194 $tpl->read_file($config->{'tpl_viewfile'});
195
196 $tpl->fillin("FILE",$virtual);
197 $tpl->fillin("DIR",upper_path($virtual));
198 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
199 $tpl->fillin("SCRIPT",$script);
200 $tpl->fillin("CONTENT",encode_entities($$content));
201
202 $output = header(-type => "text/html");
203 $output .= $tpl->get_template;
204 }
205 }
206
207 return \$output;
208 }
209
210 # exec_beginedit
211 #
212 # Lock a file and display a form to edit it
213 #
214 # Params: 1. Reference to user input hash
215 # 2. Reference to config hash
216 #
217 # Return: Output of the command (Scalar Reference)
218
219 sub exec_beginedit($$)
220 {
221 my ($data,$config) = @_;
222 my $physical = $data->{'physical'};
223 my $virtual = $data->{'virtual'};
224 my $uselist = $data->{'uselist'};
225
226 return error($config->{'err_editdir'},upper_path($virtual)) if(-d $physical);
227 return error_in_use($virtual) if($uselist->in_use($virtual));
228 return error($config->{'err_noedit'},upper_path($virtual)) unless(-r $physical && -w $physical);
229
230 # Check on binary files
231
232 unless(-T $physical)
233 {
234 # Binary file
235
236 return error($config->{'err_binary'},upper_path($virtual));
237 }
238 else
239 {
240 # Text file
241
242 $uselist->add_file($virtual);
243 $uselist->save;
244
245 my $content = file_read($physical);
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 my $uselist = $data->{'uselist'};
277
278 file_unlock($uselist,$virtual);
279 return devedit_reload({command => 'show', file => upper_path($virtual)});
280 }
281
282 # exec_endedit()
283 #
284 # Save a file, unlock it and return to directory view
285 #
286 # Params: 1. Reference to user input hash
287 # 2. Reference to config hash
288 #
289 # Return: Output of the command (Scalar Reference)
290
291 sub exec_endedit($$)
292 {
293 my ($data,$config) = @_;
294 my $physical = $data->{'physical'};
295 my $virtual = $data->{'virtual'};
296 my $content = $data->{'cgi'}->param('filecontent');
297
298 return error($config->{'err_editdir'},upper_path($virtual)) if(-d $physical);
299 return error($config->{'err_noedit'},upper_path($virtual)) unless(-r $physical && -w $physical);
300
301 # Normalize newlines
302
303 $content =~ s/\015\012|\012|\015/\n/g;
304
305 if($data->{'cgi'}->param('encode_iso'))
306 {
307 # Encode all ISO-8859-1 special chars
308
309 $content = encode_entities($content,"\200-\377");
310 }
311
312 if($data->{'cgi'}->param('saveas'))
313 {
314 # Create the new filename
315
316 $physical = $data->{'new_physical'};
317 $virtual = $data->{'new_virtual'};
318 }
319
320 if(file_save($physical,\$content))
321 {
322 # Saving of the file was successful - so unlock it!
323
324 file_unlock($data->{'uselist'},$virtual);
325 return devedit_reload({command => 'show', file => upper_path($virtual)});
326 }
327 else
328 {
329 return error($config->{'err_editfailed'},upper_path($virtual),{FILE => $virtual});
330 }
331 }
332
333 # exec_mkfile()
334 #
335 # Create a file and return to directory view
336 #
337 # Params: 1. Reference to user input hash
338 # 2. Reference to config hash
339 #
340 # Return: Output of the command (Scalar Reference)
341
342 sub exec_mkfile($$)
343 {
344 my ($data,$config) = @_;
345 my $new_physical = $data->{'new_physical'};
346 my $new_virtual = $data->{'new_virtual'};
347 my $dir = upper_path($new_virtual);
348 $new_virtual = encode_entities($new_virtual);
349
350 return error("A file or directory called '$new_virtual' already exists.",$dir) if(-e $new_physical);
351
352 file_create($new_physical) or return error("Could not create file '$new_virtual'.",$dir);
353 return devedit_reload({command => 'show', file => $dir});
354 }
355
356 # exec_mkdir()
357 #
358 # Create a directory and return to directory view
359 #
360 # Params: 1. Reference to user input hash
361 # 2. Reference to config hash
362 #
363 # Return: Output of the command (Scalar Reference)
364
365 sub exec_mkdir($$)
366 {
367 my ($data,$config) = @_;
368 my $new_physical = $data->{'new_physical'};
369 my $new_virtual = $data->{'new_virtual'};
370 my $dir = upper_path($new_virtual);
371 $new_virtual = encode_entities($new_virtual);
372
373 return error("A file or directory called '$new_virtual' already exists.",$dir) if(-e $new_physical);
374
375 mkdir($new_physical,0777) or return error("Could not create directory '$new_virtual'.",$dir);
376 return devedit_reload({command => 'show', file => $dir});
377 }
378
379 # exec_copy()
380 #
381 # Copy a file and return to directory view
382 #
383 # Params: 1. Reference to user input hash
384 # 2. Reference to config hash
385 #
386 # Return: Output of the command (Scalar Reference)
387
388 sub exec_copy($$)
389 {
390 my ($data,$config) = @_;
391 my $physical = $data->{'physical'};
392 my $virtual = encode_entities($data->{'virtual'});
393 my $new_physical = $data->{'new_physical'};
394 my $new_virtual = $data->{'new_virtual'};
395 my $dir = upper_path($new_virtual);
396 $new_virtual = encode_entities($new_virtual);
397
398 return error("This editor is not able to copy directories.") if(-d $physical);
399 return error("You have not enough permissions to copy this file.") unless(-r $physical);
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("DIR",upper_path($virtual));
415 $tpl->fillin("COMMAND","copy");
416 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
417 $tpl->fillin("SCRIPT",$script);
418
419 my $output = header(-type => "text/html");
420 $output .= $tpl->get_template;
421
422 return \$output;
423 }
424 }
425
426 if($data->{'uselist'}->in_use($data->{'new_virtual'}))
427 {
428 return error("The target file '$new_virtual' already exists and it is edited by someone else.",$dir);
429 }
430
431 copy($physical,$new_physical) or return error("Could not copy '$virtual' to '$new_virtual'",upper_path($virtual));
432 return devedit_reload({command => 'show', file => $dir});
433 }
434
435 # exec_rename()
436 #
437 # Rename/move a file and return to directory view
438 #
439 # Params: 1. Reference to user input hash
440 # 2. Reference to config hash
441 #
442 # Return: Output of the command (Scalar Reference)
443
444 sub exec_rename($$)
445 {
446 my ($data,$config) = @_;
447 my $physical = $data->{'physical'};
448 my $virtual = $data->{'virtual'};
449 my $new_physical = $data->{'new_physical'};
450 my $new_virtual = $data->{'new_virtual'};
451 my $dir = upper_path($new_virtual);
452 $new_virtual = encode_entities($new_virtual);
453
454 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
455
456 if(-e $new_physical)
457 {
458 if(-d $new_physical)
459 {
460 return error("A directory called '$new_virtual' already exists. You cannot replace a directory!",upper_path($virtual));
461 }
462 elsif(not $data->{'cgi'}->param('confirmed'))
463 {
464 my $tpl = new Template;
465 $tpl->read_file($config->{'tpl_confirm_replace'});
466
467 $tpl->fillin("FILE",$virtual);
468 $tpl->fillin("NEW_FILE",$new_virtual);
469 $tpl->fillin("DIR",upper_path($virtual));
470 $tpl->fillin("COMMAND","rename");
471 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
472 $tpl->fillin("SCRIPT",$script);
473
474 my $output = header(-type => "text/html");
475 $output .= $tpl->get_template;
476
477 return \$output;
478 }
479 }
480
481 if($data->{'uselist'}->in_use($data->{'new_virtual'}))
482 {
483 return error("The target file '$new_virtual' already exists and it is edited by someone else.",$dir);
484 }
485
486 rename($physical,$new_physical) or return error("Could not move/rename '".encode_entities($virtual)."' to '$new_virtual'.",upper_path($virtual));
487 return devedit_reload({command => 'show', file => $dir});
488 }
489
490 # exec_remove()
491 #
492 # Remove a file or a directory and return to directory view
493 #
494 # Params: 1. Reference to user input hash
495 # 2. Reference to config hash
496 #
497 # Return: Output of the command (Scalar Reference)
498
499 sub exec_remove($$)
500 {
501 my ($data,$config) = @_;
502 my $physical = $data->{'physical'};
503 my $virtual = $data->{'virtual'};
504
505 if(-d $physical)
506 {
507 # Remove a directory
508
509 if($data->{'cgi'}->param('confirmed'))
510 {
511 rmtree($physical);
512 return devedit_reload({command => 'show', file => upper_path($virtual)});
513 }
514 else
515 {
516 my $tpl = new Template;
517 $tpl->read_file($config->{'tpl_confirm_rmdir'});
518
519 $tpl->fillin("DIR",$virtual);
520 $tpl->fillin("UPPER_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 else
531 {
532 # Remove a file
533
534 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
535
536 if($data->{'cgi'}->param('confirmed'))
537 {
538 unlink($physical) or return error($config->{'err_editfailed'},upper_path($virtual),{FILE => $virtual});
539 return devedit_reload({command => 'show', file => upper_path($virtual)});
540 }
541 else
542 {
543 my $tpl = new Template;
544 $tpl->read_file($config->{'tpl_confirm_rmfile'});
545
546 $tpl->fillin("FILE",$virtual);
547 $tpl->fillin("DIR",upper_path($virtual));
548 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
549 $tpl->fillin("SCRIPT",$script);
550
551 my $output = header(-type => "text/html");
552 $output .= $tpl->get_template;
553
554 return \$output;
555 }
556 }
557 }
558
559 # exec_unlock()
560 #
561 # Remove a file from the list of used files and
562 # return to directory view
563 #
564 # Params: 1. Reference to user input hash
565 # 2. Reference to config hash
566 #
567 # Return: Output of the command (Scalar Reference)
568
569 sub exec_unlock($$)
570 {
571 my ($data,$config) = @_;
572 my $virtual = $data->{'virtual'};
573 my $uselist = $data->{'uselist'};
574
575 if($data->{'cgi'}->param('confirmed'))
576 {
577 file_unlock($uselist,$virtual);
578 return devedit_reload({command => 'show', file => upper_path($virtual)});
579 }
580 else
581 {
582 my $tpl = new Template;
583 $tpl->read_file($config->{'tpl_confirm_unlock'});
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 # it's true, baby ;-)
598
599 1;
600
601 #
602 ### End ###

patrick-canterino.de