]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2003-10-20
24 use POSIX
qw(strftime) ;
27 $script = $ENV { 'SCRIPT_NAME' };
31 use base
qw(Exporter) ;
33 @EXPORT = qw(exec_show
46 # View a directory or a file
48 # Params: 1. Reference to user input hash
49 # 2. Reference to config hash
51 # Return: Output of the command (Scalar Reference)
55 my ( $data , $config ) = @_ ;
56 my $physical = $data ->{ 'physical' };
57 my $virtual = $data ->{ 'virtual' };
62 # Create directory listing
64 my $direntries = dir_read
( $physical );
65 return error
( "Reading of directory $virtual failed" ) unless ( $direntries );
67 my $files = $direntries ->{ 'files' };
68 my $dirs = $direntries ->{ 'dirs' };
70 $output .= htmlhead
( "Directory listing of $virtual " );
71 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
72 $output .= "<hr> \n\n <pre> \n " ;
74 # Create the link to the upper directory
75 # (only if we are not in the root directory)
77 unless ( $virtual eq "/" )
79 my $upper = $physical . "/.." ;
80 my @stat = stat ( $upper );
82 $output .= " [SUBDIR] " ;
83 $output .= strftime
( " %d . %m . %Y %H : %M " , localtime ( $stat [ 9 ]));
85 $output .= "<a href= \" $script ?command=show&file=" . encode_entities
( upper_path
( $virtual )). " \" >../</a> \n " ;
88 # Get the length of the longest file/directory name
92 foreach ( @
$dirs , @
$files )
94 my $length = length ( $_ );
95 $max_name_len = $length if ( $length > $max_name_len );
100 foreach my $dir ( @
$dirs )
102 my @stat = stat ( $physical . "/" . $dir );
105 $output .= "[SUBDIR] " ;
106 $output .= strftime
( $config ->{ 'timeformat' }, localtime ( $stat [ 9 ]));
108 $output .= "<a href= \" $script ?command=show&file=" . encode_entities
( $virtual . $dir ). "/ \" >" . encode_entities
( $dir ). "/</a> \n " ;
113 foreach my $file ( @
$files )
115 my $phys_path = $physical . "/" . $file ;
116 my $virt_path = encode_entities
( $virtual . $file );
118 my @stat = stat ( $phys_path );
119 my $in_use = $data ->{ 'uselist' }-> in_use ( $virtual . $file );
121 $output .= " " x
( 10 - length ( $stat [ 7 ]));
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 (" ;
129 $output .= (- r
$phys_path && - T
$phys_path )
130 ?
"<a href= \" $script ?command=show&file= $virt_path \" >View</a>"
131 : '<span style="color:#C0C0C0">View</span>' ;
135 $output .= (- w
$phys_path && - r
$phys_path && - T
$phys_path && not $in_use )
136 ?
"<a href= \" $script ?command=beginedit&file= $virt_path \" >Edit</a>"
137 : '<span style="color:#C0C0C0">Edit</span>' ;
139 $output .= " | <a href= \" $script ?command=workwithfile&file= $virt_path \" >Do other stuff</a>) \n " ;
142 $output .= "</pre> \n\n <hr> \n\n " ;
144 # Bottom of directory listing
145 # (Fields for creating files and directories)
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>
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>
175 return error
( "You have not enough permissions to view this file." ) unless (- r
$physical );
177 # Check on binary files
178 # We have to do it in this way, or empty files
179 # will be recognized as binary files
185 return error
( "This editor is not able to view/edit binary files." );
191 $output = htmlhead
( "Contents of file " . encode_entities
( $virtual ));
192 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
193 $output .= dir_link
( $virtual );
195 $output .= '<div style="background-color:#FFFFE0;border:1px solid black;margin-top:10px;width:100%">' . " \n " ;
196 $output .= '<pre style="color:#0000C0;">' . " \n " ;
197 $output .= encode_entities
(${ file_read
( $physical )});
198 $output .= " \n </pre> \n </div>" ;
209 # Lock a file and display a form to edit it
211 # Params: 1. Reference to user input hash
212 # 2. Reference to config hash
214 # Return: Output of the command (Scalar Reference)
216 sub exec_beginedit
($$)
218 my ( $data , $config ) = @_ ;
219 my $physical = $data ->{ 'physical' };
220 my $virtual = $data ->{ 'virtual' };
221 my $uselist = $data ->{ 'uselist' };
223 return error
( "You cannot edit directories." ) if (- d
$physical );
224 return error_in_use
( $virtual ) if ( $uselist -> in_use ( $virtual ));
225 return error
( "You have not enough permissions to edit this file." ) unless (- r
$physical && - w
$physical );
227 # Check on binary files
233 return error
( "This editor is not able to view/edit binary files." );
239 $uselist -> add_file ( $virtual );
242 my $dir = upper_path
( $virtual );
243 my $content = encode_entities
(${ file_read
( $physical )});
245 my $equal_url = equal_url
( $config ->{ 'httproot' }, $virtual );
247 $virtual = encode_entities
( $virtual );
249 my $output = htmlhead
( "Edit file $virtual " );
250 $output .= $equal_url ;
252 <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>
254 <form action=" $script " method="get">
255 <input type="hidden" name="command" value="canceledit">
256 <input type="hidden" name="file" value=" $virtual ">
257 <p><input type="submit" value="Exit WITHOUT saving"></p>
260 <form action=" $script " method="post">
261 <input type="hidden" name="command" value="endedit">
262 <input type="hidden" name="file" value=" $virtual ">
264 <table width="100%" border="1">
266 <td width="50%" align="center">
267 <input type="hidden" name="file" value=" $virtual ">
268 <input type="checkbox" name="saveas" value="1"> Save as new file: $dir <input type=text name="newfile" value=""></td>
269 <td width="50%" align="center"><input type="checkbox" name="encode_iso" value="1"> Encode ISO-8859-1 special chars</td>
272 <td align="center"><input type="reset" value="Reset form"></td>
273 <td align="center"><input type="submit" value="Save and exit"></td>
277 <textarea name="filecontent" rows="25" cols="120"> $content </textarea>
289 # Save a file, unlock it and return to directory view
291 # Params: 1. Reference to user input hash
292 # 2. Reference to config hash
294 # Return: Output of the command (Scalar Reference)
298 my ( $data , $config ) = @_ ;
299 my $physical = $data ->{ 'physical' };
300 my $virtual = $data ->{ 'virtual' };
301 my $content = $data ->{ 'cgi' }-> param ( 'filecontent' );
303 return error
( "You cannot edit directories." ) if (- d
$physical );
304 return error
( "You have not enough permissions to edit this file." ) unless (- r
$physical && - w
$physical );
308 $content =~ s/\015\012|\012|\015/\n/g ;
310 if ( $data ->{ 'cgi' }-> param ( 'encode_iso' ))
312 # Encode all ISO-8859-1 special chars
314 $content = encode_entities
( $content , " \200 - \377 " );
317 if ( $data ->{ 'cgi' }-> param ( 'saveas' ))
319 # Create the new filename
321 $physical = $data ->{ 'new_physical' };
322 $virtual = $data ->{ 'new_virtual' };
325 if ( file_save
( $physical , \
$content ))
327 # Saving of the file was successful - so unlock it!
329 return exec_unlock
( $data , $config );
333 return error
( "Saving of file '" . encode_entities
( $virtual ). "' failed'." , upper_path
( $virtual ));
339 # Create a file and return to directory view
341 # Params: 1. Reference to user input hash
342 # 2. Reference to config hash
344 # Return: Output of the command (Scalar Reference)
348 my ( $data , $config ) = @_ ;
349 my $new_physical = $data ->{ 'new_physical' };
350 my $new_virtual = $data ->{ 'new_virtual' };
351 my $dir = upper_path
( $new_virtual );
352 $new_virtual = encode_entities
( $new_virtual );
354 return error
( "A file or directory called ' $new_virtual ' already exists." , $dir ) if (- e
$new_physical );
356 file_create
( $new_physical ) or return error
( "Could not create file ' $new_virtual '." , $dir );
358 my $output = redirect
( "http:// $ENV {'HTTP_HOST'} $script ?command=show&file= $dir " );
364 # Create a directory and return to directory view
366 # Params: 1. Reference to user input hash
367 # 2. Reference to config hash
369 # Return: Output of the command (Scalar Reference)
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 );
379 return error
( "A file or directory called ' $new_virtual ' already exists." , $dir ) if (- e
$new_physical );
381 mkdir ( $new_physical ) or return error
( "Could not create directory ' $new_virtual '." , $dir );
383 my $output = redirect
( "http:// $ENV {'HTTP_HOST'} $script ?command=show&file= $dir " );
387 # exec_workwithfile()
389 # Display a form for renaming/copying/deleting/unlocking a file
391 # Params: 1. Reference to user input hash
392 # 2. Reference to config hash
394 # Return: Output of the command (Scalar Reference)
396 sub exec_workwithfile
($$)
398 my ( $data , $config ) = @_ ;
399 my $physical = $data ->{ 'physical' };
400 my $virtual = $data ->{ 'virtual' };
401 my $unused = $data ->{ 'uselist' }-> unused ( $virtual );
403 my $dir = encode_entities
( upper_path
( $virtual ));
405 my $output = htmlhead
( "Work with file " . encode_entities
( $virtual ));
406 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
408 $virtual = encode_entities
( $virtual );
410 $output .= dir_link
( $virtual );
411 $output .= "<p><b>Note:</b> On UNIX systems, filenames are <b>case-sensitive</b>!</p> \n\n " ;
413 $output .= "<p>Someone else is currently editing this file. So not all features are available.</p> \n\n " unless ( $unused );
415 # Copying of the file as always allowed
422 <form action=" $script ">
423 <input type="hidden" name="command" value="copy">
424 <input type="hidden" name="file" value=" $virtual ">
425 <p>Copy file ' $virtual ' to: $dir <input type="text" name="newfile" size="50"> <input type="submit" value="Copy!"></p>
435 # Allow renaming and deleting the file
440 <form action=" $script ">
441 <input type="hidden" name="command" value="rename">
442 <input type="hidden" name="file" value=" $virtual ">
443 <p>Move/Rename file ' $virtual ' to: $dir <input type="text" name="newfile" size="50"> <input type="submit" value="Move/Rename!"></p>
450 <form action=" $script " method="get">
451 <input type="hidden" name="file" value=" $virtual ">
452 <input type="hidden" name="command" value="remove">
453 <p><input type="submit" value="Delete file ' $virtual '!"></p>
460 # Just display a button for unlocking it
465 <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>
467 <form action=" $script " method="get">
468 <input type="hidden" name="file" value=" $virtual ">
469 <input type="hidden" name="command" value="unlock">
470 <p><input type="submit" value="Unlock file ' $virtual '"></p>
483 # Copy a file and return to directory view
485 # Params: 1. Reference to user input hash
486 # 2. Reference to config hash
488 # Return: Output of the command (Scalar Reference)
492 my ( $data , $config ) = @_ ;
493 my $physical = $data ->{ 'physical' };
494 my $virtual = encode_entities
( $data ->{ 'virtual' });
495 my $new_physical = $data ->{ 'new_physical' };
496 my $new_virtual = $data ->{ 'new_virtual' };
497 my $dir = upper_path
( $new_virtual );
498 $new_virtual = encode_entities
( $new_virtual );
500 return error
( "This editor is not able to copy directories." ) if (- d
$physical );
504 return error
( "A file or directory called ' $new_virtual ' already exists and this editor is currently not able to ask to overwrite the existing file or directory." , upper_path
( $virtual ));
507 copy
( $physical , $new_physical ) or return error
( "Could not copy ' $virtual ' to ' $new_virtual '" , upper_path
( $virtual ));
509 my $output = redirect
( "http:// $ENV {'HTTP_HOST'} $script ?command=show&file= $dir " );
515 # Rename/move a file and return to directory view
517 # Params: 1. Reference to user input hash
518 # 2. Reference to config hash
520 # Return: Output of the command (Scalar Reference)
524 my ( $data , $config ) = @_ ;
525 my $physical = $data ->{ 'physical' };
526 my $virtual = $data ->{ 'virtual' };
527 my $new_physical = $data ->{ 'new_physical' };
528 my $new_virtual = $data ->{ 'new_virtual' };
529 my $dir = upper_path
( $new_virtual );
530 $new_virtual = encode_entities
( $new_virtual );
532 return error_in_use
( $virtual ) if ( $data ->{ 'uselist' }-> in_use ( $virtual ));
536 return error
( "A file or directory called ' $new_virtual ' already exists and this editor is currently not able to ask to overwrite the existing file or directory." , upper_path
( $virtual ));
539 rename ( $physical , $new_physical ) or return error
( "Could not move/rename '" . encode_entities
( $virtual ). "' to ' $new_virtual '." , upper_path
( $virtual ));
541 my $output = redirect
( "http:// $ENV {'HTTP_HOST'} $script ?command=show&file= $dir " );
547 # Remove a file and return to directory view
549 # Params: 1. Reference to user input hash
550 # 2. Reference to config hash
552 # Return: Output of the command (Scalar Reference)
556 my ( $data , $config ) = @_ ;
557 my $physical = $data ->{ 'physical' };
558 my $virtual = $data ->{ 'virtual' };
560 return error
( "Deleting directories is currently unsupported." ) if (- d
$physical );
561 return error_in_use
( $virtual ) if ( $data ->{ 'uselist' }-> in_use ( $virtual ));
563 unlink ( $physical ) or return error
( "Could not delete file '" . encode_entities
( $virtual ). "'." , upper_path
( $virtual ));
565 my $output = redirect
( "http:// $ENV {'HTTP_HOST'} $script ?command=show&file=" . upper_path
( $virtual ));
571 # Remove a file from the list of used files and
572 # return to directory view
574 # Params: 1. Reference to user input hash
575 # 2. Reference to config hash
577 # Return: Output of the command (Scalar Reference)
581 my ( $data , $config ) = @_ ;
582 my $virtual = $data ->{ 'virtual' };
583 my $uselist = $data ->{ 'uselist' };
585 $uselist -> remove_file ( $virtual );
588 my $output = redirect
( "http:// $ENV {'HTTP_HOST'} $script ?command=show&file=" . upper_path
( $virtual ));
592 # it's true, baby ;-)
patrick-canterino.de