]>
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-30
22 use POSIX qw(strftime) ;
25 $script = $ENV { 'SCRIPT_NAME' };
29 use base
qw(Exporter) ;
31 @EXPORT = qw(exec_show
44 # View a directory or a file
46 # Params: 1. Reference to user input hash
47 # 2. Reference to config hash
49 # Return: Output of the command (Scalar Reference)
53 my ( $data , $config ) = @_ ;
54 my $physical = $data ->{ 'physical' };
55 my $virtual = $data ->{ 'virtual' };
60 # Create directory listing
62 my $direntries = dir_read
( $physical );
63 return error
( "Reading of directory $virtual failed." , upper_path
( $virtual )) unless ( $direntries );
65 my $files = $direntries ->{ 'files' };
66 my $dirs = $direntries ->{ 'dirs' };
68 $output .= htmlhead
( "Directory listing of $virtual " );
69 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
70 $output .= "<hr> \n\n <pre> \n " ;
72 # Create the link to the upper directory
73 # (only if we are not in the root directory)
75 unless ( $virtual eq "/" )
77 my $upper = $physical . "/.." ;
78 my @stat = stat ( $upper );
80 $output .= " [SUBDIR] " ;
81 $output .= strftime
( " %d . %m . %Y %H : %M " , localtime ( $stat [ 9 ]));
83 $output .= "<a href= \" $script ?command=show&file=" . encode_entities
( upper_path
( $virtual )). " \" >../</a> \n " ;
86 # Get the length of the longest file/directory name
90 foreach ( @
$dirs , @
$files )
92 my $length = length ( $_ );
93 $max_name_len = $length if ( $length > $max_name_len );
98 foreach my $dir ( @
$dirs )
100 my @stat = stat ( $physical . "/" . $dir );
103 $output .= "[SUBDIR] " ;
104 $output .= strftime
( $config ->{ 'timeformat' }, localtime ( $stat [ 9 ]));
106 $output .= "<a href= \" $script ?command=show&file=" . encode_entities
( $virtual . $dir ). "/ \" >" . encode_entities
( $dir ). "/</a> \n " ;
111 foreach my $file ( @
$files )
113 my $phys_path = $physical . "/" . $file ;
114 my $virt_path = encode_entities
( $virtual . $file );
116 my @stat = stat ( $phys_path );
117 my $in_use = $data ->{ 'uselist' }-> in_use ( $virtual . $file );
119 $output .= " " x
( 10 - length ( $stat [ 7 ]));
122 $output .= strftime
( $config ->{ 'timeformat' }, localtime ( $stat [ 9 ]));
123 $output .= ( $in_use ) ?
" (IN USE) " : ( not - T
$phys_path ) ?
" (BINARY) " : " " x
10 ;
124 $output .= encode_entities
( $file );
125 $output .= " " x
( $max_name_len - length ( $file )). " \t (" ;
127 $output .= (- r
$phys_path && - T
$phys_path )
128 ?
"<a href= \" $script ?command=show&file= $virt_path \" >View</a>"
129 : '<span style="color:#C0C0C0">View</span>' ;
133 $output .= (- w
$phys_path && - r
$phys_path && - T
$phys_path && not $in_use )
134 ?
"<a href= \" $script ?command=beginedit&file= $virt_path \" >Edit</a>"
135 : '<span style="color:#C0C0C0">Edit</span>' ;
137 $output .= " | <a href= \" $script ?command=workwithfile&file= $virt_path \" >Do other stuff</a>) \n " ;
140 $output .= "</pre> \n\n <hr> \n\n " ;
142 # Bottom of directory listing
143 # (Fields for creating files and directories)
148 <form action=" $script ">
149 <input type="hidden" name="command" value="mkdir">
150 <input type="hidden" name="curdir" value=" $virtual ">
151 <td>Create new directory:</td>
152 <td> $virtual <input type="text" name="newfile"> <input type="submit" value="Create!"></td>
156 <td>Create new file:</td>
157 <form action=" $script ">
158 <input type="hidden" name="command" value="mkfile">
159 <input type="hidden" name="curdir" value=" $virtual ">
160 <td> $virtual <input type="text" name="newfile"> <input type="submit" value="Create!"></td>
173 return error
( "You have not enough permissions to view this file." , upper_path
( $virtual )) unless (- r
$physical );
175 # Check on binary files
176 # We have to do it in this way, or empty files
177 # will be recognized as binary files
183 return error
( "This editor is not able to view/edit binary files." , upper_path
( $virtual ));
189 $output = htmlhead
( "Contents of file " . encode_entities
( $virtual ));
190 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
191 $output .= dir_link
( $virtual );
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>" ;
207 # Lock a file and display a form to edit it
209 # Params: 1. Reference to user input hash
210 # 2. Reference to config hash
212 # Return: Output of the command (Scalar Reference)
214 sub exec_beginedit
($$)
216 my ( $data , $config ) = @_ ;
217 my $physical = $data ->{ 'physical' };
218 my $virtual = $data ->{ 'virtual' };
219 my $uselist = $data ->{ 'uselist' };
221 return error
( "You cannot edit directories." , upper_path
( $virtual )) if (- d
$physical );
222 return error_in_use
( $virtual ) if ( $uselist -> in_use ( $virtual ));
223 return error
( "You have not enough permissions to edit this file." , upper_path
( $virtual )) unless (- r
$physical && - w
$physical );
225 # Check on binary files
231 return error
( "This editor is not able to view/edit binary files." , upper_path
( $virtual ));
237 $uselist -> add_file ( $virtual );
240 my $dir = upper_path
( $virtual );
241 my $content = encode_entities
(${ file_read
( $physical )});
243 my $equal_url = equal_url
( $config ->{ 'httproot' }, $virtual );
245 $virtual = encode_entities
( $virtual );
247 my $output = htmlhead
( "Edit file $virtual " );
248 $output .= $equal_url ;
250 <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>
252 <form action=" $script " method="get">
253 <input type="hidden" name="command" value="canceledit">
254 <input type="hidden" name="file" value=" $virtual ">
255 <p><input type="submit" value="Exit WITHOUT saving"></p>
258 <form action=" $script " method="post">
259 <input type="hidden" name="command" value="endedit">
260 <input type="hidden" name="file" value=" $virtual ">
262 <table width="100%" border="1">
264 <td width="50%" align="center">
265 <input type="hidden" name="file" value=" $virtual ">
266 <input type="checkbox" name="saveas" value="1"> Save as new file: $dir <input type=text name="newfile" value=""></td>
267 <td width="50%" align="center"><input type="checkbox" name="encode_iso" value="1"> Encode ISO-8859-1 special chars</td>
270 <td align="center"><input type="reset" value="Reset form"></td>
271 <td align="center"><input type="submit" value="Save and exit"></td>
275 <textarea name="filecontent" rows="25" cols="120"> $content </textarea>
287 # Save a file, unlock it and return to directory view
289 # Params: 1. Reference to user input hash
290 # 2. Reference to config hash
292 # Return: Output of the command (Scalar Reference)
296 my ( $data , $config ) = @_ ;
297 my $physical = $data ->{ 'physical' };
298 my $virtual = $data ->{ 'virtual' };
299 my $content = $data ->{ 'cgi' }-> param ( 'filecontent' );
301 return error
( "You cannot edit directories." ) if (- d
$physical );
302 return error
( "You have not enough permissions to edit this file." , upper_path
( $virtual )) unless (- r
$physical && - w
$physical );
306 $content =~ s/\015\012|\012|\015/\n/g ;
308 if ( $data ->{ 'cgi' }-> param ( 'encode_iso' ))
310 # Encode all ISO-8859-1 special chars
312 $content = encode_entities
( $content , " \200 - \377 " );
315 if ( $data ->{ 'cgi' }-> param ( 'saveas' ))
317 # Create the new filename
319 $physical = $data ->{ 'new_physical' };
320 $virtual = $data ->{ 'new_virtual' };
323 if ( file_save
( $physical , \
$content ))
325 # Saving of the file was successful - so unlock it!
327 return exec_unlock
( $data , $config );
331 return error
( "Saving of file '" . encode_entities
( $virtual ). "' failed'." , upper_path
( $virtual ));
337 # Create a file and return to directory view
339 # Params: 1. Reference to user input hash
340 # 2. Reference to config hash
342 # Return: Output of the command (Scalar Reference)
346 my ( $data , $config ) = @_ ;
347 my $new_physical = $data ->{ 'new_physical' };
348 my $new_virtual = $data ->{ 'new_virtual' };
349 my $dir = upper_path
( $new_virtual );
350 $new_virtual = encode_entities
( $new_virtual );
352 return error
( "A file or directory called ' $new_virtual ' already exists." , $dir ) if (- e
$new_physical );
354 file_create
( $new_physical ) or return error
( "Could not create file ' $new_virtual '." , $dir );
355 return devedit_reload
({ command
=> 'show' , file
=> $dir });
360 # Create a directory and return to directory view
362 # Params: 1. Reference to user input hash
363 # 2. Reference to config hash
365 # Return: Output of the command (Scalar Reference)
369 my ( $data , $config ) = @_ ;
370 my $new_physical = $data ->{ 'new_physical' };
371 my $new_virtual = $data ->{ 'new_virtual' };
372 my $dir = upper_path
( $new_virtual );
373 $new_virtual = encode_entities
( $new_virtual );
375 return error
( "A file or directory called ' $new_virtual ' already exists." , $dir ) if (- e
$new_physical );
377 mkdir ( $new_physical ) or return error
( "Could not create directory ' $new_virtual '." , $dir );
378 return devedit_reload
({ command
=> 'show' , file
=> $dir });
381 # exec_workwithfile()
383 # Display a form for renaming/copying/deleting/unlocking a file
385 # Params: 1. Reference to user input hash
386 # 2. Reference to config hash
388 # Return: Output of the command (Scalar Reference)
390 sub exec_workwithfile
($$)
392 my ( $data , $config ) = @_ ;
393 my $physical = $data ->{ 'physical' };
394 my $virtual = $data ->{ 'virtual' };
395 my $unused = $data ->{ 'uselist' }-> unused ( $virtual );
397 my $dir = encode_entities
( upper_path
( $virtual ));
399 my $output = htmlhead
( "Work with file " . encode_entities
( $virtual ));
400 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
402 $virtual = encode_entities
( $virtual );
404 $output .= dir_link
( $virtual );
405 $output .= "<p><b>Note:</b> On UNIX systems, filenames are <b>case-sensitive</b>!</p> \n\n " ;
407 $output .= "<p>Someone else is currently editing this file. So not all features are available.</p> \n\n " unless ( $unused );
409 $output .= "<hr> \n\n " ;
411 # Copying of the file is always allowed - but we need read access
418 <form action=" $script ">
419 <input type="hidden" name="command" value="copy">
420 <input type="hidden" name="file" value=" $virtual ">
421 <p>Copy file ' $virtual ' to: $dir <input type="text" name="newfile" size="50"> <input type="submit" value="Copy!"></p>
432 # Allow renaming and deleting the file
437 <form action=" $script ">
438 <input type="hidden" name="command" value="rename">
439 <input type="hidden" name="file" value=" $virtual ">
440 <p>Move/Rename file ' $virtual ' to: $dir <input type="text" name="newfile" size="50"> <input type="submit" value="Move/Rename!"></p>
447 <form action=" $script " method="get">
448 <input type="hidden" name="file" value=" $virtual ">
449 <input type="hidden" name="command" value="remove">
450 <p><input type="submit" value="Delete file ' $virtual '!"></p>
457 # Just display a button for unlocking it
462 <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>
464 <form action=" $script " method="get">
465 <input type="hidden" name="file" value=" $virtual ">
466 <input type="hidden" name="command" value="unlock">
467 <p><input type="submit" value="Unlock file ' $virtual '"></p>
480 # Copy a file and 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 = encode_entities
( $data ->{ 'virtual' });
492 my $new_physical = $data ->{ 'new_physical' };
493 my $new_virtual = $data ->{ 'new_virtual' };
494 my $dir = upper_path
( $new_virtual );
495 $new_virtual = encode_entities
( $new_virtual );
497 return error
( "This editor is not able to copy directories." ) if (- d
$physical );
498 return error
( "You have not enough permissions to copy this file." ) unless (- r
$physical );
502 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 ));
505 copy
( $physical , $new_physical ) or return error
( "Could not copy ' $virtual ' to ' $new_virtual '" , upper_path
( $virtual ));
506 return devedit_reload
({ command
=> 'show' , file
=> $dir });
511 # Rename/move a file and return to directory view
513 # Params: 1. Reference to user input hash
514 # 2. Reference to config hash
516 # Return: Output of the command (Scalar Reference)
520 my ( $data , $config ) = @_ ;
521 my $physical = $data ->{ 'physical' };
522 my $virtual = $data ->{ 'virtual' };
523 my $new_physical = $data ->{ 'new_physical' };
524 my $new_virtual = $data ->{ 'new_virtual' };
525 my $dir = upper_path
( $new_virtual );
526 $new_virtual = encode_entities
( $new_virtual );
528 return error_in_use
( $virtual ) if ( $data ->{ 'uselist' }-> in_use ( $virtual ));
532 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 ));
535 rename ( $physical , $new_physical ) or return error
( "Could not move/rename '" . encode_entities
( $virtual ). "' to ' $new_virtual '." , upper_path
( $virtual ));
536 return devedit_reload
({ command
=> 'show' , file
=> $dir });
541 # Remove a file and return to directory view
543 # Params: 1. Reference to user input hash
544 # 2. Reference to config hash
546 # Return: Output of the command (Scalar Reference)
550 my ( $data , $config ) = @_ ;
551 my $physical = $data ->{ 'physical' };
552 my $virtual = $data ->{ 'virtual' };
554 return error
( "Deleting directories is currently unsupported." , upper_path
( $virtual )) if (- d
$physical );
555 return error_in_use
( $virtual ) if ( $data ->{ 'uselist' }-> in_use ( $virtual ));
557 unlink ( $physical ) or return error
( "Could not delete file '" . encode_entities
( $virtual ). "'." , upper_path
( $virtual ));
558 return devedit_reload
({ command
=> 'show' , file
=> upper_path
( $virtual )});
563 # Remove a file from the list of used files and
564 # return to directory view
566 # Params: 1. Reference to user input hash
567 # 2. Reference to config hash
569 # Return: Output of the command (Scalar Reference)
573 my ( $data , $config ) = @_ ;
574 my $virtual = $data ->{ 'virtual' };
575 my $uselist = $data ->{ 'uselist' };
577 $uselist -> remove_file ( $virtual );
580 return devedit_reload
({ command
=> 'show' , file
=> upper_path
( $virtual )});
583 # it's true, baby ;-)
patrick-canterino.de