]> git.p6c8.net - devedit.git/blob - modules/Command.pm
Initial version
[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-22-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=".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=$virtual$dir/\">".encode_entities($dir)."/</a>\n";
110 }
111
112 # Files
113
114 foreach my $file(@$files)
115 {
116 my @stat = stat($physical."/".$file);
117 my $virt_path = $virtual.$file;
118 my $in_use = $data->{'uselist'}->in_use($virtual.$file);
119
120 $output .= " " x (10 - length($stat[7]));
121 $output .= $stat[7];
122 $output .= " ";
123 $output .= strftime("%d.%m.%Y %H:%M",localtime($stat[9]));
124 $output .= ($in_use) ? " (IN USE) " : " " x 10;
125 $output .= encode_entities($file);
126 $output .= " " x ($max_name_len - length($file))."\t (";
127 $output .= "<a href=\"$script?command=show&file=$virt_path\">View</a> | ";
128
129 $output .= ($in_use)
130 ? '<span style="color:#C0C0C0">Edit</span>'
131 : "<a href=\"$script?command=beginedit&file=$virt_path\">Edit</a>";
132
133 $output .= " | <a href=\"$script?command=workwithfile&file=$virt_path\">Do other stuff</a>)\n";
134 }
135
136 $output .= "</pre>\n\n<hr>\n\n";
137 $output .= <<END;
138 <table border="0">
139 <tr>
140 <td>Create new directory:</td>
141 <td>$virtual <input type="text" name="newdirname"> <input type="submit" value="Create!"></td>
142 </tr>
143 <tr>
144 <td>Create new file:</td>
145 <td>$virtual <input type="text" name="newfilename"> <input type="submit" value="Create!"></td>
146 </tr>
147 </table>
148
149 <hr>
150 END
151 $output .= htmlfoot;
152 }
153 else
154 {
155 # View a file
156
157 # Check on binary files
158
159 if(-B $physical)
160 {
161 # Binary file
162
163 return error("This editor is not able to view/edit binary files.");
164 }
165 else
166 {
167 # Text file
168
169 $output = htmlhead("Contents of file $virtual");
170 $output .= equal_url($config->{'httproot'},$virtual);
171 $output .= dir_link($virtual);
172
173 $output .= '<div style="background-color:#FFFFE0;border:1px solid black;margin-top:10px;width:100%">'."\n";
174 $output .= '<pre style="color:#0000C0;">'."\n";
175 $output .= encode_entities(${file_read($physical)});
176 $output .= "\n</pre>\n</div>";
177
178 $output .= htmlfoot;
179 }
180 }
181
182 return \$output
183 }
184
185 # exec_beginedit
186 #
187 # Lock a file and display a form to edit it
188 #
189 # Params: 1. Reference to user input hash
190 # 2. Reference to config hash
191 #
192 # Return: Output of the command (Scalar Reference)
193
194 sub exec_beginedit($$)
195 {
196 my ($data,$config) = @_;
197 my $physical = $data->{'physical'};
198 my $virtual = $data->{'virtual'};
199 my $uselist = $data->{'uselist'};
200
201 return error("You cannot edit directories.") if(-d $physical);
202 return error_in_use($virtual) if($uselist->in_use($virtual));
203
204 # Check on binary files
205
206 if(-B $physical)
207 {
208 # Binary file
209
210 return error("This editor is not able to view/edit binary files.");
211 }
212 else
213 {
214 # Text file
215
216 $uselist->add_file($virtual);
217 $uselist->save;
218
219 my $dir = upper_path($virtual);
220 my $content = encode_entities(${file_read($physical)});
221
222 my $output = htmlhead("Edit file $virtual");
223 $output .= equal_url($config->{'httproot'},$virtual);
224 $output .= <<END;
225 <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>
226
227 <form action="$ENV{'SCRIPT_NAME'}" method="get">
228 <input type="hidden" name="command" value="canceledit">
229 <input type="hidden" name="file" value="$virtual">
230 <p><input type="submit" value="Exit WITHOUT saving"></p>
231 </form>
232
233 <form action="$ENV{'SCRIPT_NAME'}" method="post">
234 <input type="hidden" name="command" value="endedit">
235 <input type="hidden" name="file" value="$virtual">
236
237 <table width="100%" border="1">
238 <tr>
239 <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>
240 <td width="50%" align="center"><input type="checkbox" name="encode_iso" value="1"> Encode ISO-8859-1 special chars</td>
241 </tr>
242 <tr>
243 <td align="center"><input type="reset" value="Reset form"></td>
244 <td align="center"><input type="submit" value="Save and exit"></td>
245 </tr>
246 </table>
247
248 <textarea name="filecontent" rows="25" cols="120">$content</textarea>
249 </form>
250 END
251
252 $output .= htmlfoot;
253
254 return \$output;
255 }
256 }
257
258 # exec_endedit()
259 #
260 # Save a file, unlock it and return to directory view
261 #
262 # Params: 1. Reference to user input hash
263 # 2. Reference to config hash
264 #
265 # Return: Output of the command (Scalar Reference)
266
267 sub exec_endedit($$)
268 {
269 my ($data,$config) = @_;
270 my $physical = $data->{'physical'};
271 my $virtual = $data->{'virtual'};
272 my $content = $data->{'cgi'}->param('filecontent');
273
274 return error("You cannot edit directories.") if(-d $physical);
275
276 if($data->{'cgi'}->param('encode_iso'))
277 {
278 # Encode all ISO-8859-1 special chars
279
280 $content = encode_entities($content,"\200-\377");
281 }
282
283 if(file_save($physical,\$content))
284 {
285 # Saving of the file was successfull - so unlock it!
286
287 return exec_unlock($data,$config);
288 }
289 else
290 {
291 return error("Saving of file '$virtual' failed'");
292 }
293 }
294
295 # exec_mkfile()
296 #
297 # Create a file and return to directory view
298 #
299 # Params: 1. Reference to user input hash
300 # 2. Reference to config hash
301 #
302 # Return: Output of the command (Scalar Reference)
303
304 sub exec_mkfile($$)
305 {
306 1;
307 }
308
309 # exec_mkdir()
310 #
311 # Create a directory and return to directory view
312 #
313 # Params: 1. Reference to user input hash
314 # 2. Reference to config hash
315 #
316 # Return: Output of the command (Scalar Reference)
317
318 sub exec_mkdir($$)
319 {
320 1;
321 }
322
323 # exec_workwithfile()
324 #
325 # Display a form for renaming/copying/deleting/unlocking a file
326 #
327 # Params: 1. Reference to user input hash
328 # 2. Reference to config hash
329 #
330 # Return: Output of the command (Scalar Reference)
331
332 sub exec_workwithfile($$)
333 {
334 my ($data,$config) = @_;
335 my $physical = $data->{'physical'};
336 my $virtual = $data->{'virtual'};
337 my $unused = $data->{'uselist'}->unused($virtual);
338
339 my $output = htmlhead("Work with file $virtual");
340 $output .= equal_url($config->{'httproot'},$virtual);
341 $output .= dir_link($virtual);
342 $output .= "<p><b>Note:</b> On UNIX systems, filenames are <b>case-sensitive</b>!</p>\n\n";
343
344 $output .= "<p>Someone else is currently editing this file. So not all features are available.</p>\n\n" unless($unused);
345
346 $output .= <<END;
347 <hr>
348
349 <h2>Copy</h2>
350
351 <p>Copy file '$virtual' to: <input type="text" name="newfilename" size="50"> <input type="submit" value="Copy!"></p>
352
353 <hr>
354
355 END
356
357 if($unused)
358 {
359 $output .= <<END;
360 <h2>Move/rename</h2>
361
362 <p>Move/Rename file '$virtual' to: <input type="text" name="newfilename" size="50"> <input type="submit" value="Move/Rename!"></p>
363
364 <hr>
365
366 <h2>Delete</h2>
367
368 <form action="$script" method="get">
369 <input type="hidden" name="file" value="$virtual">
370 <input type="hidden" name="command" value="remove">
371 <p><input type="submit" value="Delete file '$virtual'!"></p>
372 </form>
373 END
374 }
375 else
376 {
377 $output .= <<END;
378 <h2>Unlock file</h2>
379
380 <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>
381
382 <form action="$script" method="get">
383 <input type="hidden" name="file" value="$virtual">
384 <input type="hidden" name="command" value="unlock">
385 <p><input type="submit" value="Unlock file '$virtual'"></p>
386 </form>
387 END
388 }
389
390 $output .= "\n<hr>";
391 $output .= htmlfoot;
392
393 return \$output;
394 }
395
396 # exec_copy()
397 #
398 # Copy a file and return to directory view
399 #
400 # Params: 1. Reference to user input hash
401 # 2. Reference to config hash
402 #
403 # Return: Output of the command (Scalar Reference)
404
405 sub exec_copy($$)
406 {
407 1;
408 }
409
410 # exec_rename()
411 #
412 # Rename/move a file and return to directory view
413 #
414 # Params: 1. Reference to user input hash
415 # 2. Reference to config hash
416 #
417 # Return: Output of the command (Scalar Reference)
418
419 sub exec_rename($$)
420 {
421 1;
422 }
423
424 # exec_remove()
425 #
426 # Remove a file and return to directory view
427 #
428 # Params: 1. Reference to user input hash
429 # 2. Reference to config hash
430 #
431 # Return: Output of the command (Scalar Reference)
432
433 sub exec_remove($$)
434 {
435 my ($data,$config) = @_;
436 my $physical = $data->{'physical'};
437 my $virtual = $data->{'virtual'};
438
439 return error_in_use($virtual) if($data->{'uselist'}->in_use($virtual));
440
441 my $dir = upper_path($virtual);
442
443 unlink($physical);
444
445 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=$dir");
446 return \$output;
447 }
448
449 # exec_unlock()
450 #
451 # Remove a file from the list of used files and
452 # return to directory view
453 #
454 # Params: 1. Reference to user input hash
455 # 2. Reference to config hash
456 #
457 # Return: Output of the command (Scalar Reference)
458
459 sub exec_unlock($$)
460 {
461 my ($data,$config) = @_;
462 my $physical = $data->{'physical'};
463 my $virtual = $data->{'virtual'};
464 my $uselist = $data->{'uselist'};
465
466 my $dir = upper_path($virtual);
467
468 $uselist->remove_file($virtual);
469 $uselist->save;
470
471 my $output = redirect("http://$ENV{'HTTP_HOST'}$script?command=show&file=$dir");
472 return \$output;
473 }
474
475 # it's true, baby ;-)
476
477 1;
478
479 #
480 ### End ###

patrick-canterino.de