]> git.p6c8.net - devedit.git/blob - modules/Command.pm
check_path() was cleaned up. It now uses upper_path() and file_name() instead of...
[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: 10-04-2003
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT
15 $script);
16
17 use CGI qw(redirect);
18
19 use File::Access;
20 use File::Copy;
21
22 use HTML::Entities;
23 use Output;
24 use POSIX qw(strftime);
25 use Tool;
26
27 $script = $ENV{'SCRIPT_NAME'};
28
29 ### Export ###
30
31 use base qw(Exporter);
32
33 @EXPORT = qw(exec_show
34 exec_beginedit
35 exec_endedit
36 exec_mkfile
37 exec_mkdir
38 exec_workwithfile
39 exec_copy
40 exec_rename
41 exec_remove
42 exec_unlock);
43
44 # exec_show()
45 #
46 # View a directory or a file
47 #
48 # Params: 1. Reference to user input hash
49 # 2. Reference to config hash
50 #
51 # Return: Output of the command (Scalar Reference)
52
53 sub exec_show($$$)
54 {
55 my ($data,$config) = @_;
56 my $physical = $data->{'physical'};
57 my $virtual = $data->{'virtual'};
58 my $output;
59
60 if(-d $physical)
61 {
62 # Create directory listing
63
64 my $direntries = dir_read($physical);
65 return error("Reading of directory $virtual failed") unless($direntries);
66
67 my $files = $direntries->{'files'};
68 my $dirs = $direntries->{'dirs'};
69
70 $output .= htmlhead("Directory listing of $virtual");
71 $output .= equal_url($config->{'httproot'},$virtual);
72 $output .= "<hr>\n\n<pre>\n";
73
74 # Create the link to the upper directory
75 # (only if we are not in the root directory)
76
77 unless($virtual eq "/")
78 {
79 my $upper = $physical."/..";
80 my @stat = stat($upper);
81
82 $output .= " [SUBDIR] ";
83 $output .= strftime("%d.%m.%Y %H:%M",localtime($stat[9]));
84 $output .= " " x 10;
85 $output .= "<a href=\"$script?command=show&file=".encode_entities(upper_path($virtual))."\">../</a>\n";
86 }
87
88 # Get the length of the longest file/directory name
89
90 my $max_name_len = 0;
91
92 foreach(@$dirs,@$files)
93 {
94 my $length = length($_);
95 $max_name_len = $length if($length > $max_name_len);
96 }
97
98 # Directories
99
100 foreach my $dir(@$dirs)
101 {
102 my @stat = stat($physical."/".$dir);
103
104 $output .= " ";
105 $output .= "[SUBDIR] ";
106 $output .= strftime($config->{'timeformat'},localtime($stat[9]));
107 $output .= " " x 10;
108 $output .= "<a href=\"$script?command=show&file=".encode_entities($virtual.$dir)."/\">".encode_entities($dir)."/</a>\n";
109 }
110
111 # Files
112
113 foreach my $file(@$files)
114 {
115 my $phys_path = $physical."/".$file; # Not exactly...
116 my $virt_path = encode_entities($virtual.$file);
117
118 my @stat = stat($phys_path);
119 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
120
121 $output .= " " x (10 - length($stat[7]));
122 $output .= $stat[7];
123 $output .= " ";
124 $output .= strftime($config->{'timeformat'},localtime($stat[9]));
125 $output .= ($in_use) ? " (IN USE) " : (not -T $phys_path) ? " (BINARY) " : " " x 10;
126 $output .= encode_entities($file);
127 $output .= " " x ($max_name_len - length($file))."\t (";
128
129 $output .= (-T $phys_path)
130 ? "<a href=\"$script?command=show&file=$virt_path\">View</a>"
131 : '<span style="color:#C0C0C0">View</span>';
132
133 $output .= " | ";
134
135 $output .= ($in_use || not -T $phys_path)
136 ? '<span style="color:#C0C0C0">Edit</span>'
137 : "<a href=\"$script?command=beginedit&file=$virt_path\">Edit</a>";
138
139 $output .= " | <a href=\"$script?command=workwithfile&file=$virt_path\">Do other stuff</a>)\n";
140 }
141
142 $output .= "</pre>\n\n<hr>\n\n";
143
144 # Bottom of directory listing
145 # (Fields for creating files and directories)
146
147 $output .= <<END;
148 <table border="0">
149 <tr>
150 <form action="$script">
151 <input type="hidden" name="command" value="mkdir">
152 <input type="hidden" name="curdir" value="$virtual">
153 <td>Create new directory:</td>
154 <td>$virtual <input type="text" name="newfile"> <input type="submit" value="Create!"></td>
155 </form>
156 </tr>
157 <tr>
158 <td>Create new file:</td>
159 <form action="$script">
160 <input type="hidden" name="command" value="mkfile">
161 <input type="hidden" name="curdir" value="$virtual">
162 <td>$virtual <input type="text" name="newfile"> <input type="submit" value="Create!"></td>
163 </form>
164 </tr>
165 </table>
166
167 <hr>
168 END
169 $output .= htmlfoot;
170 }
171 else
172 {
173 # View a file
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("This editor is not able to view/edit binary files.");
184 }
185 else
186 {
187 # Text file
188
189 $output = htmlhead("Contents of file ".encode_entities($virtual));
190 $output .= equal_url($config->{'httproot'},$virtual);
191 $output .= dir_link($virtual);
192
193 $output .= '<div style="background-color:#FFFFE0;border:1px solid black;margin-top:10px;width:100%">'."\n";
194 $output .= '<pre style="color:#0000C0;">'."\n";
195 $output .= encode_entities(${file_read($physical)});
196 $output .= "\n</pre>\n</div>";
197
198 $output .= htmlfoot;
199 }
200 }
201
202 return \$output;
203 }
204
205 # exec_beginedit
206 #
207 # Lock a file and display a form to edit it
208 #
209 # Params: 1. Reference to user input hash
210 # 2. Reference to config hash
211 #
212 # Return: Output of the command (Scalar Reference)
213
214 sub exec_beginedit($$)
215 {
216 my ($data,$config) = @_;
217 my $physical = $data->{'physical'};
218 my $virtual = $data->{'virtual'};
219 my $uselist = $data->{'uselist'};
220
221 return error("You cannot edit directories.") if(-d $physical);
222 return error_in_use($virtual) if($uselist->in_use($virtual));
223
224 # Check on binary files
225
226 unless(-T $physical)
227 {
228 # Binary file
229
230 return error("This editor is not able to view/edit binary files.");
231 }
232 else
233 {
234 # Text file
235
236 $uselist->add_file($virtual);
237 $uselist->save;
238
239 my $dir = upper_path($virtual);
240 my $content = encode_entities(${file_read($physical)});
241
242 my $equal_url = equal_url($config->{'httproot'},$virtual);
243
244 $virtual = encode_entities($virtual);
245
246 my $output = htmlhead("Edit file $virtual");
247 $output .= $equal_url;
248 $output .= <<END;
249 <p><b style="color:#FF0000">Caution!</b> This file is locked for other users while you are editing it. To unlock it, click <i>Save and exit</i> or <i>Exit WITHOUT saving</i>. Please <b>don't</b> click the <i>Reload</i> button in your browser! This will confuse the editor.</p>
250
251 <form action="$script" method="get">
252 <input type="hidden" name="command" value="canceledit">
253 <input type="hidden" name="file" value="$virtual">
254 <p><input type="submit" value="Exit WITHOUT saving"></p>
255 </form>
256
257 <form action="$script" method="post">
258 <input type="hidden" name="command" value="endedit">
259 <input type="hidden" name="file" value="$virtual">
260
261 <table width="100%" border="1">
262 <tr>
263 <td width="50%" align="center">
264 <input type="hidden" name="file" value="$virtual">
265 <input type="checkbox" name="saveas" value="1"> Save as new file: $dir <input type=text name="newfile" value=""></td>
266 <td width="50%" align="center"><input type="checkbox" name="encode_iso" value="1"> Encode ISO-8859-1 special chars</td>
267 </tr>
268 <tr>
269 <td align="center"><input type="reset" value="Reset form"></td>
270 <td align="center"><input type="submit" value="Save and exit"></td>
271 </tr>
272 </table>
273
274 <textarea name="filecontent" rows="25" cols="120">$content</textarea>
275 </form>
276 END
277
278 $output .= htmlfoot;
279
280 return \$output;
281 }
282 }
283
284 # exec_endedit()
285 #
286 # Save a file, unlock it and return to directory view
287 #
288 # Params: 1. Reference to user input hash
289 # 2. Reference to config hash
290 #
291 # Return: Output of the command (Scalar Reference)
292
293 sub exec_endedit($$)
294 {
295 my ($data,$config) = @_;
296 my $physical = $data->{'physical'};
297 my $virtual = $data->{'virtual'};
298 my $content = $data->{'cgi'}->param('filecontent');
299
300 return error("You cannot edit directories.") if(-d $physical);
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 return exec_unlock($data,$config);
322 }
323 else
324 {
325 return error("Saving of file '".encode_entities($virtual)."' failed'.");
326 }
327 }
328
329 # exec_mkfile()
330 #
331 # Create a file and return to directory view
332 #
333 # Params: 1. Reference to user input hash
334 # 2. Reference to config hash
335 #
336 # Return: Output of the command (Scalar Reference)
337
338 sub exec_mkfile($$)
339 {
340 my ($data,$config) = @_;
341 my $new_physical = $data->{'new_physical'};
342 my $new_virtual = $data->{'new_virtual'};
343 my $dir = upper_path($new_virtual);
344 $new_virtual = encode_entities($new_virtual);
345
346 return error("A file or directory called '$new_virtual' does already exist.") if(-e $new_physical);
347
348 file_create($new_physical) or return error("Could not create file '$new_virtual'.");
349
350 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=$dir");
351 return \$output;
352 }
353
354 # exec_mkdir()
355 #
356 # Create a directory and return to directory view
357 #
358 # Params: 1. Reference to user input hash
359 # 2. Reference to config hash
360 #
361 # Return: Output of the command (Scalar Reference)
362
363 sub exec_mkdir($$)
364 {
365 my ($data,$config) = @_;
366 my $new_physical = $data->{'new_physical'};
367 my $new_virtual = $data->{'new_virtual'};
368 my $dir = upper_path($new_virtual);
369 $new_virtual = encode_entities($new_virtual);
370
371 return error("A file or directory called '$new_virtual' does already exist.") if(-e $new_physical);
372
373 mkdir($new_physical) or return error("Could not create directory '$new_virtual'.");
374
375 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=$dir");
376 return \$output;
377 }
378
379 # exec_workwithfile()
380 #
381 # Display a form for renaming/copying/deleting/unlocking a file
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_workwithfile($$)
389 {
390 my ($data,$config) = @_;
391 my $physical = $data->{'physical'};
392 my $virtual = $data->{'virtual'};
393 my $unused = $data->{'uselist'}->unused($virtual);
394
395 my $dir = encode_entities(upper_path($virtual));
396
397 my $output = htmlhead("Work with file ".encode_entities($virtual));
398 $output .= equal_url($config->{'httproot'},$virtual);
399
400 $virtual = encode_entities($virtual);
401
402 $output .= dir_link($virtual);
403 $output .= "<p><b>Note:</b> On UNIX systems, filenames are <b>case-sensitive</b>!</p>\n\n";
404
405 $output .= "<p>Someone else is currently editing this file. So not all features are available.</p>\n\n" unless($unused);
406
407 # Copying of the file as always allowed
408
409 $output .= <<END;
410 <hr>
411
412 <h2>Copy</h2>
413
414 <form action="$script">
415 <input type="hidden" name="command" value="copy">
416 <input type="hidden" name="file" value="$virtual">
417 <p>Copy file '$virtual' to: $dir <input type="text" name="newfile" size="50"> <input type="submit" value="Copy!"></p>
418 </form>
419
420 <hr>
421
422 END
423
424 if($unused)
425 {
426 # File is not locked
427 # Allow renaming and deleting the file
428
429 $output .= <<END;
430 <h2>Move/rename</h2>
431
432 <form action="$script">
433 <input type="hidden" name="command" value="rename">
434 <input type="hidden" name="file" value="$virtual">
435 <p>Move/Rename file '$virtual' to: $dir <input type="text" name="newfile" size="50"> <input type="submit" value="Move/Rename!"></p>
436 </form>
437
438 <hr>
439
440 <h2>Delete</h2>
441
442 <form action="$script" method="get">
443 <input type="hidden" name="file" value="$virtual">
444 <input type="hidden" name="command" value="remove">
445 <p><input type="submit" value="Delete file '$virtual'!"></p>
446 </form>
447 END
448 }
449 else
450 {
451 # File is locked
452 # Just display a button for unlocking it
453
454 $output .= <<END;
455 <h2>Unlock file</h2>
456
457 <p>Someone else is currently editing this file. At least, the file is marked so. Maybe, someone who was editing the file has forgotten to unlock it. In this case (and <b>only</b> in this case) you can unlock the file using this button:</p>
458
459 <form action="$script" method="get">
460 <input type="hidden" name="file" value="$virtual">
461 <input type="hidden" name="command" value="unlock">
462 <p><input type="submit" value="Unlock file '$virtual'"></p>
463 </form>
464 END
465 }
466
467 $output .= "\n<hr>";
468 $output .= htmlfoot;
469
470 return \$output;
471 }
472
473 # exec_copy()
474 #
475 # Copy a file and return to directory view
476 #
477 # Params: 1. Reference to user input hash
478 # 2. Reference to config hash
479 #
480 # Return: Output of the command (Scalar Reference)
481
482 sub exec_copy($$)
483 {
484 my ($data,$config) = @_;
485 my $physical = $data->{'physical'};
486 my $virtual = encode_entities($data->{'virtual'});
487 my $new_physical = $data->{'new_physical'};
488 my $new_virtual = $data->{'new_virtual'};
489 my $dir = upper_path($new_virtual);
490 $new_virtual = encode_entities($new_virtual);
491
492 return error("This editor is not able to copy directories.") if(-d $physical);
493
494 if(-e $new_physical)
495 {
496 return error("A file or directory called '$new_virtual' does already exists and this editor is currently not able to ask to overwrite the existing file or directory.");
497 }
498
499 copy($physical,$new_physical) or return error("Could not copy '$virtual' to '$new_virtual'");
500
501 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=$dir");
502 return \$output;
503 }
504
505 # exec_rename()
506 #
507 # Rename/move a file and return to directory view
508 #
509 # Params: 1. Reference to user input hash
510 # 2. Reference to config hash
511 #
512 # Return: Output of the command (Scalar Reference)
513
514 sub exec_rename($$)
515 {
516 my ($data,$config) = @_;
517 my $physical = $data->{'physical'};
518 my $virtual = $data->{'virtual'};
519 my $new_physical = $data->{'new_physical'};
520 my $new_virtual = $data->{'new_virtual'};
521 my $dir = upper_path($new_virtual);
522 $new_virtual = encode_entities($new_virtual);
523
524 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
525
526 if(-e $new_physical)
527 {
528 return error("A file or directory called '$new_virtual' does already exists and this editor is currently not able to ask to overwrite the existing file or directory.");
529 }
530
531 rename($physical,$new_physical) or return error("Could not move/rename '".encode_entities($virtual)."' to '$new_virtual'.");
532
533 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=$dir");
534 return \$output;
535 }
536
537 # exec_remove()
538 #
539 # Remove a file and return to directory view
540 #
541 # Params: 1. Reference to user input hash
542 # 2. Reference to config hash
543 #
544 # Return: Output of the command (Scalar Reference)
545
546 sub exec_remove($$)
547 {
548 my ($data,$config) = @_;
549 my $physical = $data->{'physical'};
550 my $virtual = $data->{'virtual'};
551
552 return error("Deleting directories is currently unsupported") if(-d $physical);
553 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
554
555 unlink($physical) or return error("Could not delete file '".encode_entities($virtual)."'.");
556
557 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=".upper_path($virtual));
558 return \$output;
559 }
560
561 # exec_unlock()
562 #
563 # Remove a file from the list of used files and
564 # return to directory view
565 #
566 # Params: 1. Reference to user input hash
567 # 2. Reference to config hash
568 #
569 # Return: Output of the command (Scalar Reference)
570
571 sub exec_unlock($$)
572 {
573 my ($data,$config) = @_;
574 my $virtual = $data->{'virtual'};
575 my $uselist = $data->{'uselist'};
576
577 $uselist->remove_file($virtual);
578 $uselist->save;
579
580 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=".upper_path($virtual));
581 return \$output;
582 }
583
584 # it's true, baby ;-)
585
586 1;
587
588 #
589 ### End ###

patrick-canterino.de