]> git.p6c8.net - devedit.git/blob - modules/Command.pm
Version 1.1:
[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: 09-25-2003
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT
15 $script);
16
17 use CGI qw(header
18 redirect);
19
20 use File::Access;
21 use File::Copy;
22
23 use HTML::Entities;
24 use Output;
25 use POSIX qw(strftime);
26 use Tool;
27
28 $script = $ENV{'SCRIPT_NAME'};
29
30 ### Export ###
31
32 use base qw(Exporter);
33
34 @EXPORT = qw(exec_show
35 exec_beginedit
36 exec_endedit
37 exec_mkfile
38 exec_mkdir
39 exec_workwithfile
40 exec_copy
41 exec_rename
42 exec_remove
43 exec_unlock);
44
45 # exec_show()
46 #
47 # View a directory or a file
48 #
49 # Params: 1. Reference to user input hash
50 # 2. Reference to config hash
51 #
52 # Return: Output of the command (Scalar Reference)
53
54 sub exec_show($$$)
55 {
56 my ($data,$config) = @_;
57 my $physical = $data->{'physical'};
58 my $virtual = $data->{'virtual'};
59 my $output;
60
61 if(-d $physical)
62 {
63 # Create directory listing
64
65 my $direntries = dir_read($physical);
66 return error("Reading of directory $virtual failed") unless($direntries);
67
68 my $files = $direntries->{'files'};
69 my $dirs = $direntries->{'dirs'};
70
71 $output .= htmlhead("Directory listing of $virtual");
72 $output .= equal_url($config->{'httproot'},$virtual);
73 $output .= "<hr>\n\n<pre>\n";
74
75 # Create the link to the upper directory
76 # (only if we are not in the root directory)
77
78 unless($virtual eq "/")
79 {
80 my $upper = $physical."/..";
81 my @stat = stat($upper);
82
83 $output .= " [SUBDIR] ";
84 $output .= strftime("%d.%m.%Y %H:%M",localtime($stat[9]));
85 $output .= " " x 10;
86 $output .= "<a href=\"$script?command=show&file=".encode_entities(upper_path($virtual))."\">../</a>\n";
87 }
88
89 # Get the longest file/directory name
90
91 my $max_name_len = 0;
92
93 foreach(@$dirs,@$files)
94 {
95 my $length = length($_);
96 $max_name_len = $length if($length > $max_name_len);
97 }
98
99 # Directories
100
101 foreach my $dir(@$dirs)
102 {
103 my @stat = stat($physical."/".$dir);
104
105 $output .= " ";
106 $output .= "[SUBDIR] ";
107 $output .= strftime("%d.%m.%Y %H:%M",localtime($stat[9]));
108 $output .= " " x 10;
109 $output .= "<a href=\"$script?command=show&file=".encode_entities($virtual.$dir)."/\">".encode_entities($dir)."/</a>\n";
110 }
111
112 # Files
113
114 foreach my $file(@$files)
115 {
116 my $phys_path = $physical."/".$file; # Not exactly...
117 my $virt_path = encode_entities($virtual.$file);
118
119 my @stat = stat($phys_path);
120 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
121
122 $output .= " " x (10 - length($stat[7]));
123 $output .= $stat[7];
124 $output .= " ";
125 $output .= strftime("%d.%m.%Y %H:%M",localtime($stat[9]));
126 $output .= ($in_use) ? " (IN USE) " : (not -T $phys_path) ? " (BINARY) " : " " x 10;
127 $output .= encode_entities($file);
128 $output .= " " x ($max_name_len - length($file))."\t (";
129
130 $output .= (-T $phys_path)
131 ? "<a href=\"$script?command=show&file=$virt_path\">View</a>"
132 : '<span style="color:#C0C0C0">View</span>';
133
134 $output .= " | ";
135
136 $output .= ($in_use || not -T $phys_path)
137 ? '<span style="color:#C0C0C0">Edit</span>'
138 : "<a href=\"$script?command=beginedit&file=$virt_path\">Edit</a>";
139
140 $output .= " | <a href=\"$script?command=workwithfile&file=$virt_path\">Do other stuff</a>)\n";
141 }
142
143 $output .= "</pre>\n\n<hr>\n\n";
144
145 # Bottom of directory listing
146 # (Fields for creating files and directories)
147
148 $output .= <<END;
149 <table border="0">
150 <tr>
151 <td>Create new directory:</td>
152 <td>$virtual <input type="text" name="newdirname"> <input type="submit" value="Create!"></td>
153 </tr>
154 <tr>
155 <td>Create new file:</td>
156 <td>$virtual <input type="text" name="newfilename"> <input type="submit" value="Create!"></td>
157 </tr>
158 </table>
159
160 <hr>
161 END
162 $output .= htmlfoot;
163 }
164 else
165 {
166 # View a file
167
168 # Check on binary files
169 # We have to do it in this way, or empty files
170 # will be recognized as binary files
171
172 unless(-T $physical)
173 {
174 # Binary file
175
176 return error("This editor is not able to view/edit binary files.");
177 }
178 else
179 {
180 # Text file
181
182 $output = htmlhead("Contents of file ".encode_entities($virtual));
183 $output .= equal_url($config->{'httproot'},$virtual);
184 $output .= dir_link($virtual);
185
186 $output .= '<div style="background-color:#FFFFE0;border:1px solid black;margin-top:10px;width:100%">'."\n";
187 $output .= '<pre style="color:#0000C0;">'."\n";
188 $output .= encode_entities(${file_read($physical)});
189 $output .= "\n</pre>\n</div>";
190
191 $output .= htmlfoot;
192 }
193 }
194
195 return \$output
196 }
197
198 # exec_beginedit
199 #
200 # Lock a file and display a form to edit it
201 #
202 # Params: 1. Reference to user input hash
203 # 2. Reference to config hash
204 #
205 # Return: Output of the command (Scalar Reference)
206
207 sub exec_beginedit($$)
208 {
209 my ($data,$config) = @_;
210 my $physical = $data->{'physical'};
211 my $virtual = $data->{'virtual'};
212 my $uselist = $data->{'uselist'};
213
214 return error("You cannot edit directories.") if(-d $physical);
215 return error_in_use($virtual) if($uselist->in_use($virtual));
216
217 # Check on binary files
218
219 unless(-T $physical)
220 {
221 # Binary file
222
223 return error("This editor is not able to view/edit binary files.");
224 }
225 else
226 {
227 # Text file
228
229 $uselist->add_file($virtual);
230 $uselist->save;
231
232 my $dir = upper_path($virtual);
233 my $content = encode_entities(${file_read($physical)});
234
235 my $output = htmlhead("Edit file ".encode_entities($virtual));
236 $output .= equal_url($config->{'httproot'},$virtual);
237
238 $virtual = encode_entities($virtual);
239
240 $output .= <<END;
241 <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>
242
243 <form action="$ENV{'SCRIPT_NAME'}" method="get">
244 <input type="hidden" name="command" value="canceledit">
245 <input type="hidden" name="file" value="$virtual">
246 <p><input type="submit" value="Exit WITHOUT saving"></p>
247 </form>
248
249 <form action="$ENV{'SCRIPT_NAME'}" method="post">
250 <input type="hidden" name="command" value="endedit">
251 <input type="hidden" name="file" value="$virtual">
252
253 <table width="100%" border="1">
254 <tr>
255 <td width="50%" align="center"><input type="checkbox" name="save_as_new_file" value="1"> Save as new file: $dir <input type=text name="new_filename" value=""></td>
256 <td width="50%" align="center"><input type="checkbox" name="encode_iso" value="1"> Encode ISO-8859-1 special chars</td>
257 </tr>
258 <tr>
259 <td align="center"><input type="reset" value="Reset form"></td>
260 <td align="center"><input type="submit" value="Save and exit"></td>
261 </tr>
262 </table>
263
264 <textarea name="filecontent" rows="25" cols="120">$content</textarea>
265 </form>
266 END
267
268 $output .= htmlfoot;
269
270 return \$output;
271 }
272 }
273
274 # exec_endedit()
275 #
276 # Save a file, unlock it and return to directory view
277 #
278 # Params: 1. Reference to user input hash
279 # 2. Reference to config hash
280 #
281 # Return: Output of the command (Scalar Reference)
282
283 sub exec_endedit($$)
284 {
285 my ($data,$config) = @_;
286 my $physical = $data->{'physical'};
287 my $virtual = $data->{'virtual'};
288 my $content = $data->{'cgi'}->param('filecontent');
289
290 return error("You cannot edit directories.") if(-d $physical);
291
292 if($data->{'cgi'}->param('encode_iso'))
293 {
294 # Encode all ISO-8859-1 special chars
295
296 $content = encode_entities($content,"\200-\377");
297 }
298
299 if(file_save($physical,\$content))
300 {
301 # Saving of the file was successfull - so unlock it!
302
303 return exec_unlock($data,$config);
304 }
305 else
306 {
307 return error("Saving of file '".encode_entities($virtual)."' failed'");
308 }
309 }
310
311 # exec_mkfile()
312 #
313 # Create a file and return to directory view
314 #
315 # Params: 1. Reference to user input hash
316 # 2. Reference to config hash
317 #
318 # Return: Output of the command (Scalar Reference)
319
320 sub exec_mkfile($$)
321 {
322 1;
323 }
324
325 # exec_mkdir()
326 #
327 # Create a directory and return to directory view
328 #
329 # Params: 1. Reference to user input hash
330 # 2. Reference to config hash
331 #
332 # Return: Output of the command (Scalar Reference)
333
334 sub exec_mkdir($$)
335 {
336 1;
337 }
338
339 # exec_workwithfile()
340 #
341 # Display a form for renaming/copying/deleting/unlocking a file
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_workwithfile($$)
349 {
350 my ($data,$config) = @_;
351 my $physical = $data->{'physical'};
352 my $virtual = $data->{'virtual'};
353 my $unused = $data->{'uselist'}->unused($virtual);
354
355 my $output = htmlhead("Work with file ".encode_entities($virtual));
356 $output .= equal_url($config->{'httproot'},$virtual);
357
358 $virtual = encode_entities($virtual);
359
360 $output .= dir_link($virtual);
361 $output .= "<p><b>Note:</b> On UNIX systems, filenames are <b>case-sensitive</b>!</p>\n\n";
362
363 $output .= "<p>Someone else is currently editing this file. So not all features are available.</p>\n\n" unless($unused);
364
365 # Copying of the file as always allowed
366
367 $output .= <<END;
368 <hr>
369
370 <h2>Copy</h2>
371
372 <p>Copy file '$virtual' to: <input type="text" name="newfilename" size="50"> <input type="submit" value="Copy!"></p>
373
374 <hr>
375
376 END
377
378 if($unused)
379 {
380 # File is not locked
381 # Allow renaming and deleting the file
382
383 $output .= <<END;
384 <h2>Move/rename</h2>
385
386 <p>Move/Rename file '$virtual' to: <input type="text" name="newfilename" size="50"> <input type="submit" value="Move/Rename!"></p>
387
388 <hr>
389
390 <h2>Delete</h2>
391
392 <form action="$script" method="get">
393 <input type="hidden" name="file" value="$virtual">
394 <input type="hidden" name="command" value="remove">
395 <p><input type="submit" value="Delete file '$virtual'!"></p>
396 </form>
397 END
398 }
399 else
400 {
401 # File is locked
402 # Just display a button for unlocking it
403
404 $output .= <<END;
405 <h2>Unlock file</h2>
406
407 <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>
408
409 <form action="$script" method="get">
410 <input type="hidden" name="file" value="$virtual">
411 <input type="hidden" name="command" value="unlock">
412 <p><input type="submit" value="Unlock file '$virtual'"></p>
413 </form>
414 END
415 }
416
417 $output .= "\n<hr>";
418 $output .= htmlfoot;
419
420 return \$output;
421 }
422
423 # exec_copy()
424 #
425 # Copy a file and return to directory view
426 #
427 # Params: 1. Reference to user input hash
428 # 2. Reference to config hash
429 #
430 # Return: Output of the command (Scalar Reference)
431
432 sub exec_copy($$)
433 {
434 1;
435 }
436
437 # exec_rename()
438 #
439 # Rename/move a file and return to directory view
440 #
441 # Params: 1. Reference to user input hash
442 # 2. Reference to config hash
443 #
444 # Return: Output of the command (Scalar Reference)
445
446 sub exec_rename($$)
447 {
448 1;
449 }
450
451 # exec_remove()
452 #
453 # Remove a file and return to directory view
454 #
455 # Params: 1. Reference to user input hash
456 # 2. Reference to config hash
457 #
458 # Return: Output of the command (Scalar Reference)
459
460 sub exec_remove($$)
461 {
462 my ($data,$config) = @_;
463 my $physical = $data->{'physical'};
464 my $virtual = $data->{'virtual'};
465
466 return error("Deleting of directories is currently unsupported") if(-d $physical);
467 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
468
469 my $dir = upper_path($virtual);
470
471 unlink($physical) or return error("Could not delete file '".encode_entities($virtual)."'.");
472
473 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=$dir");
474 return \$output;
475 }
476
477 # exec_unlock()
478 #
479 # Remove a file from the list of used files and
480 # return to directory view
481 #
482 # Params: 1. Reference to user input hash
483 # 2. Reference to config hash
484 #
485 # Return: Output of the command (Scalar Reference)
486
487 sub exec_unlock($$)
488 {
489 my ($data,$config) = @_;
490 my $physical = $data->{'physical'};
491 my $virtual = $data->{'virtual'};
492 my $uselist = $data->{'uselist'};
493
494 my $dir = upper_path($virtual);
495
496 $uselist->remove_file($virtual);
497 $uselist->save;
498
499 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=$dir");
500 return \$output;
501 }
502
503 # it's true, baby ;-)
504
505 1;
506
507 #
508 ### End ###

patrick-canterino.de