]>
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: 09-25-2003
25 use POSIX
qw(strftime) ;
28 $script = $ENV { 'SCRIPT_NAME' };
32 use base
qw(Exporter) ;
34 @EXPORT = qw(exec_show
47 # View a directory or a file
49 # Params: 1. Reference to user input hash
50 # 2. Reference to config hash
52 # Return: Output of the command (Scalar Reference)
56 my ( $data , $config ) = @_ ;
57 my $physical = $data ->{ 'physical' };
58 my $virtual = $data ->{ 'virtual' };
63 # Create directory listing
65 my $direntries = dir_read
( $physical );
66 return error
( "Reading of directory $virtual failed" ) unless ( $direntries );
68 my $files = $direntries ->{ 'files' };
69 my $dirs = $direntries ->{ 'dirs' };
71 $output .= htmlhead
( "Directory listing of $virtual " );
72 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
73 $output .= "<hr> \n\n <pre> \n " ;
75 # Create the link to the upper directory
76 # (only if we are not in the root directory)
78 unless ( $virtual eq "/" )
80 my $upper = $physical . "/.." ;
81 my @stat = stat ( $upper );
83 $output .= " [SUBDIR] " ;
84 $output .= strftime
( " %d . %m . %Y %H : %M " , localtime ( $stat [ 9 ]));
86 $output .= "<a href= \" $script ?command=show&file=" . encode_entities
( upper_path
( $virtual )). " \" >../</a> \n " ;
89 # Get the longest file/directory name
93 foreach ( @
$dirs , @
$files )
95 my $length = length ( $_ );
96 $max_name_len = $length if ( $length > $max_name_len );
101 foreach my $dir ( @
$dirs )
103 my @stat = stat ( $physical . "/" . $dir );
106 $output .= "[SUBDIR] " ;
107 $output .= strftime
( " %d . %m . %Y %H : %M " , localtime ( $stat [ 9 ]));
109 $output .= "<a href= \" $script ?command=show&file=" . encode_entities
( $virtual . $dir ). "/ \" >" . encode_entities
( $dir ). "/</a> \n " ;
114 foreach my $file ( @
$files )
116 my $phys_path = $physical . "/" . $file ; # Not exactly...
117 my $virt_path = encode_entities
( $virtual . $file );
119 my @stat = stat ( $phys_path );
120 my $in_use = $data ->{ 'uselist' }-> in_use ( $virtual . $file );
122 $output .= " " x
( 10 - length ( $stat [ 7 ]));
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 (" ;
130 $output .= (- T
$phys_path )
131 ?
"<a href= \" $script ?command=show&file= $virt_path \" >View</a>"
132 : '<span style="color:#C0C0C0">View</span>' ;
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>" ;
140 $output .= " | <a href= \" $script ?command=workwithfile&file= $virt_path \" >Do other stuff</a>) \n " ;
143 $output .= "</pre> \n\n <hr> \n\n " ;
145 # Bottom of directory listing
146 # (Fields for creating files and directories)
151 <td>Create new directory:</td>
152 <td> $virtual <input type="text" name="newdirname"> <input type="submit" value="Create!"></td>
155 <td>Create new file:</td>
156 <td> $virtual <input type="text" name="newfilename"> <input type="submit" value="Create!"></td>
168 # Check on binary files
169 # We have to do it in this way, or empty files
170 # will be recognized as binary files
176 return error
( "This editor is not able to view/edit binary files." );
182 $output = htmlhead
( "Contents of file " . encode_entities
( $virtual ));
183 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
184 $output .= dir_link
( $virtual );
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>" ;
200 # Lock a file and display a form to edit it
202 # Params: 1. Reference to user input hash
203 # 2. Reference to config hash
205 # Return: Output of the command (Scalar Reference)
207 sub exec_beginedit
($$)
209 my ( $data , $config ) = @_ ;
210 my $physical = $data ->{ 'physical' };
211 my $virtual = $data ->{ 'virtual' };
212 my $uselist = $data ->{ 'uselist' };
214 return error
( "You cannot edit directories." ) if (- d
$physical );
215 return error_in_use
( $virtual ) if ( $uselist -> in_use ( $virtual ));
217 # Check on binary files
223 return error
( "This editor is not able to view/edit binary files." );
229 $uselist -> add_file ( $virtual );
232 my $dir = upper_path
( $virtual );
233 my $content = encode_entities
(${ file_read
( $physical )});
235 my $output = htmlhead
( "Edit file " . encode_entities
( $virtual ));
236 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
238 $virtual = encode_entities
( $virtual );
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>
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>
249 <form action=" $ENV {'SCRIPT_NAME'}" method="post">
250 <input type="hidden" name="command" value="endedit">
251 <input type="hidden" name="file" value=" $virtual ">
253 <table width="100%" border="1">
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>
259 <td align="center"><input type="reset" value="Reset form"></td>
260 <td align="center"><input type="submit" value="Save and exit"></td>
264 <textarea name="filecontent" rows="25" cols="120"> $content </textarea>
276 # Save a file, unlock it and return to directory view
278 # Params: 1. Reference to user input hash
279 # 2. Reference to config hash
281 # Return: Output of the command (Scalar Reference)
285 my ( $data , $config ) = @_ ;
286 my $physical = $data ->{ 'physical' };
287 my $virtual = $data ->{ 'virtual' };
288 my $content = $data ->{ 'cgi' }-> param ( 'filecontent' );
290 return error
( "You cannot edit directories." ) if (- d
$physical );
292 if ( $data ->{ 'cgi' }-> param ( 'encode_iso' ))
294 # Encode all ISO-8859-1 special chars
296 $content = encode_entities
( $content , " \200 - \377 " );
299 if ( file_save
( $physical , \
$content ))
301 # Saving of the file was successfull - so unlock it!
303 return exec_unlock
( $data , $config );
307 return error
( "Saving of file '" . encode_entities
( $virtual ). "' failed'" );
313 # Create a file and return to directory view
315 # Params: 1. Reference to user input hash
316 # 2. Reference to config hash
318 # Return: Output of the command (Scalar Reference)
327 # Create a directory and return to directory view
329 # Params: 1. Reference to user input hash
330 # 2. Reference to config hash
332 # Return: Output of the command (Scalar Reference)
339 # exec_workwithfile()
341 # Display a form for renaming/copying/deleting/unlocking a file
343 # Params: 1. Reference to user input hash
344 # 2. Reference to config hash
346 # Return: Output of the command (Scalar Reference)
348 sub exec_workwithfile
($$)
350 my ( $data , $config ) = @_ ;
351 my $physical = $data ->{ 'physical' };
352 my $virtual = $data ->{ 'virtual' };
353 my $unused = $data ->{ 'uselist' }-> unused ( $virtual );
355 my $output = htmlhead
( "Work with file " . encode_entities
( $virtual ));
356 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
358 $virtual = encode_entities
( $virtual );
360 $output .= dir_link
( $virtual );
361 $output .= "<p><b>Note:</b> On UNIX systems, filenames are <b>case-sensitive</b>!</p> \n\n " ;
363 $output .= "<p>Someone else is currently editing this file. So not all features are available.</p> \n\n " unless ( $unused );
365 # Copying of the file as always allowed
372 <p>Copy file ' $virtual ' to: <input type="text" name="newfilename" size="50"> <input type="submit" value="Copy!"></p>
381 # Allow renaming and deleting the file
386 <p>Move/Rename file ' $virtual ' to: <input type="text" name="newfilename" size="50"> <input type="submit" value="Move/Rename!"></p>
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>
402 # Just display a button for unlocking it
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>
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>
425 # Copy a file and return to directory view
427 # Params: 1. Reference to user input hash
428 # 2. Reference to config hash
430 # Return: Output of the command (Scalar Reference)
439 # Rename/move a file and return to directory view
441 # Params: 1. Reference to user input hash
442 # 2. Reference to config hash
444 # Return: Output of the command (Scalar Reference)
453 # Remove a file and return to directory view
455 # Params: 1. Reference to user input hash
456 # 2. Reference to config hash
458 # Return: Output of the command (Scalar Reference)
462 my ( $data , $config ) = @_ ;
463 my $physical = $data ->{ 'physical' };
464 my $virtual = $data ->{ 'virtual' };
466 return error
( "Deleting of directories is currently unsupported" ) if (- d
$physical );
467 return error_in_use
( $virtual ) if ( $data ->{ 'uselist' }-> in_use ( $virtual ));
469 my $dir = upper_path
( $virtual );
471 unlink ( $physical ) or return error
( "Could not delete file '" . encode_entities
( $virtual ). "'." );
473 my $output = redirect
( "http:// $ENV {'HTTP_HOST'} $script ?command=show&file= $dir " );
479 # Remove a file from the list of used files and
480 # return to directory view
482 # Params: 1. Reference to user input hash
483 # 2. Reference to config hash
485 # Return: Output of the command (Scalar Reference)
489 my ( $data , $config ) = @_ ;
490 my $physical = $data ->{ 'physical' };
491 my $virtual = $data ->{ 'virtual' };
492 my $uselist = $data ->{ 'uselist' };
494 my $dir = upper_path
( $virtual );
496 $uselist -> remove_file ( $virtual );
499 my $output = redirect
( "http:// $ENV {'HTTP_HOST'} $script ?command=show&file= $dir " );
503 # it's true, baby ;-)
patrick-canterino.de