]>
git.p6c8.net - devedit.git/blob - modules/Command.pm
0a368e95db7777ef68bad27dbe2330a3f8c62029
4 # Dev-Editor - Module Command
6 # Execute Dev-Editor's commands
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2003-11-10
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 ]));
124 $output .= encode_entities
( $file );
125 $output .= " " x
( $max_name_len - length ( $file )). " \t (" ;
129 if (- r
$phys_path && - T
$phys_path )
131 $output .= "<a href= \" $script ?command=show&file= $virt_path \" >View</a>" ;
135 $output .= '<span style="color:#C0C0C0" title="' ;
137 $output .= ( not - r
$phys_path ) ?
"Not readable" :
138 (- B
$phys_path ) ?
"Binary file" : "" ;
140 $output .= '">View</span>' ;
147 if (- w
$phys_path && - r
$phys_path && - T
$phys_path && not $in_use )
149 $output .= "<a href= \" $script ?command=beginedit&file= $virt_path \" >Edit</a>" ;
153 $output .= '<span style="color:#C0C0C0" title="' ;
155 $output .= ( not - r
$phys_path ) ?
"Not readable" :
156 ( not - w
$phys_path ) ?
"Read only" :
157 (- B
$phys_path ) ?
"Binary file" :
158 ( $in_use ) ?
"In use" : "" ;
160 $output .= '">Edit</span>' ;
163 # Link "Do other stuff"
165 $output .= " | <a href= \" $script ?command=workwithfile&file= $virt_path \" >Do other stuff</a>) \n " ;
168 $output .= "</pre> \n\n <hr> \n\n " ;
170 # Bottom of directory listing
171 # (Fields for creating files and directories)
176 <form action=" $script ">
177 <input type="hidden" name="command" value="mkdir">
178 <input type="hidden" name="curdir" value=" $virtual ">
179 <td>Create new directory:</td>
180 <td> $virtual <input type="text" name="newfile"> <input type="submit" value="Create!"></td>
184 <td>Create new file:</td>
185 <form action=" $script ">
186 <input type="hidden" name="command" value="mkfile">
187 <input type="hidden" name="curdir" value=" $virtual ">
188 <td> $virtual <input type="text" name="newfile"> <input type="submit" value="Create!"></td>
201 return error
( "You have not enough permissions to view this file." , upper_path
( $virtual )) unless (- r
$physical );
203 # Check on binary files
204 # We have to do it in this way, or empty files
205 # will be recognized as binary files
211 return error
( "This editor is not able to view/edit binary files." , upper_path
( $virtual ));
217 $output = htmlhead
( "Contents of file " . encode_entities
( $virtual ));
218 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
219 $output .= dir_link
( $virtual );
221 $output .= '<div style="background-color:#FFFFE0;border:1px solid black;margin-top:10px;width:100%">' . " \n " ;
222 $output .= '<pre style="color:#0000C0;">' . " \n " ;
223 $output .= encode_entities
(${ file_read
( $physical )});
224 $output .= " \n </pre> \n </div>" ;
235 # Lock a file and display a form to edit it
237 # Params: 1. Reference to user input hash
238 # 2. Reference to config hash
240 # Return: Output of the command (Scalar Reference)
242 sub exec_beginedit
($$)
244 my ( $data , $config ) = @_ ;
245 my $physical = $data ->{ 'physical' };
246 my $virtual = $data ->{ 'virtual' };
247 my $uselist = $data ->{ 'uselist' };
249 return error
( "You cannot edit directories." , upper_path
( $virtual )) if (- d
$physical );
250 return error_in_use
( $virtual ) if ( $uselist -> in_use ( $virtual ));
251 return error
( "You have not enough permissions to edit this file." , upper_path
( $virtual )) unless (- r
$physical && - w
$physical );
253 # Check on binary files
259 return error
( "This editor is not able to view/edit binary files." , upper_path
( $virtual ));
265 $uselist -> add_file ( $virtual );
268 my $dir = upper_path
( $virtual );
269 my $content = encode_entities
(${ file_read
( $physical )});
271 my $equal_url = equal_url
( $config ->{ 'httproot' }, $virtual );
273 $virtual = encode_entities
( $virtual );
275 my $output = htmlhead
( "Edit file $virtual " );
276 $output .= $equal_url ;
278 <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>
280 <form action=" $script " method="get">
281 <input type="hidden" name="command" value="canceledit">
282 <input type="hidden" name="file" value=" $virtual ">
283 <p><input type="submit" value="Exit WITHOUT saving"></p>
286 <form action=" $script " method="post">
287 <input type="hidden" name="command" value="endedit">
288 <input type="hidden" name="file" value=" $virtual ">
290 <table width="100%" border="1">
292 <td width="50%" align="center">
293 <input type="hidden" name="file" value=" $virtual ">
294 <input type="checkbox" name="saveas" value="1"> Save as new file: $dir <input type=text name="newfile" value=""></td>
295 <td width="50%" align="center"><input type="checkbox" name="encode_iso" value="1"> Encode ISO-8859-1 special chars</td>
298 <td align="center"><input type="reset" value="Reset form"></td>
299 <td align="center"><input type="submit" value="Save and exit"></td>
303 <textarea name="filecontent" rows="25" cols="120"> $content </textarea>
315 # Save a file, unlock it and return to directory view
317 # Params: 1. Reference to user input hash
318 # 2. Reference to config hash
320 # Return: Output of the command (Scalar Reference)
324 my ( $data , $config ) = @_ ;
325 my $physical = $data ->{ 'physical' };
326 my $virtual = $data ->{ 'virtual' };
327 my $content = $data ->{ 'cgi' }-> param ( 'filecontent' );
329 return error
( "You cannot edit directories." ) if (- d
$physical );
330 return error
( "You have not enough permissions to edit this file." , upper_path
( $virtual )) unless (- r
$physical && - w
$physical );
334 $content =~ s/\015\012|\012|\015/\n/g ;
336 if ( $data ->{ 'cgi' }-> param ( 'encode_iso' ))
338 # Encode all ISO-8859-1 special chars
340 $content = encode_entities
( $content , " \200 - \377 " );
343 if ( $data ->{ 'cgi' }-> param ( 'saveas' ))
345 # Create the new filename
347 $physical = $data ->{ 'new_physical' };
348 $virtual = $data ->{ 'new_virtual' };
351 if ( file_save
( $physical , \
$content ))
353 # Saving of the file was successful - so unlock it!
355 return exec_unlock
( $data , $config );
359 return error
( "Saving of file '" . encode_entities
( $virtual ). "' failed'." , upper_path
( $virtual ));
365 # Create a file and return to directory view
367 # Params: 1. Reference to user input hash
368 # 2. Reference to config hash
370 # Return: Output of the command (Scalar Reference)
374 my ( $data , $config ) = @_ ;
375 my $new_physical = $data ->{ 'new_physical' };
376 my $new_virtual = $data ->{ 'new_virtual' };
377 my $dir = upper_path
( $new_virtual );
378 $new_virtual = encode_entities
( $new_virtual );
380 return error
( "A file or directory called ' $new_virtual ' already exists." , $dir ) if (- e
$new_physical );
382 file_create
( $new_physical ) or return error
( "Could not create file ' $new_virtual '." , $dir );
383 return devedit_reload
({ command
=> 'show' , file
=> $dir });
388 # Create a directory and return to directory view
390 # Params: 1. Reference to user input hash
391 # 2. Reference to config hash
393 # Return: Output of the command (Scalar Reference)
397 my ( $data , $config ) = @_ ;
398 my $new_physical = $data ->{ 'new_physical' };
399 my $new_virtual = $data ->{ 'new_virtual' };
400 my $dir = upper_path
( $new_virtual );
401 $new_virtual = encode_entities
( $new_virtual );
403 return error
( "A file or directory called ' $new_virtual ' already exists." , $dir ) if (- e
$new_physical );
405 mkdir ( $new_physical ) or return error
( "Could not create directory ' $new_virtual '." , $dir );
406 return devedit_reload
({ command
=> 'show' , file
=> $dir });
409 # exec_workwithfile()
411 # Display a form for renaming/copying/deleting/unlocking a file
413 # Params: 1. Reference to user input hash
414 # 2. Reference to config hash
416 # Return: Output of the command (Scalar Reference)
418 sub exec_workwithfile
($$)
420 my ( $data , $config ) = @_ ;
421 my $physical = $data ->{ 'physical' };
422 my $virtual = $data ->{ 'virtual' };
423 my $unused = $data ->{ 'uselist' }-> unused ( $virtual );
425 my $dir = encode_entities
( upper_path
( $virtual ));
427 my $output = htmlhead
( "Work with file " . encode_entities
( $virtual ));
428 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
430 $virtual = encode_entities
( $virtual );
432 $output .= dir_link
( $virtual );
433 $output .= "<p><b>Note:</b> On UNIX systems, filenames are <b>case-sensitive</b>!</p> \n\n " ;
435 $output .= "<p>Someone else is currently editing this file. So not all features are available.</p> \n\n " unless ( $unused );
437 $output .= "<hr> \n\n " ;
439 # Copying of the file is always allowed - but we need read access
446 <form action=" $script ">
447 <input type="hidden" name="command" value="copy">
448 <input type="hidden" name="file" value=" $virtual ">
449 <p>Copy file ' $virtual ' to: $dir <input type="text" name="newfile" size="50"> <input type="submit" value="Copy!"></p>
460 # Allow renaming and deleting the file
465 <form action=" $script ">
466 <input type="hidden" name="command" value="rename">
467 <input type="hidden" name="file" value=" $virtual ">
468 <p>Move/Rename file ' $virtual ' to: $dir <input type="text" name="newfile" size="50"> <input type="submit" value="Move/Rename!"></p>
475 <form action=" $script " method="get">
476 <input type="hidden" name="file" value=" $virtual ">
477 <input type="hidden" name="command" value="remove">
478 <p><input type="submit" value="Delete file ' $virtual '!"></p>
485 # Just display a button for unlocking it
490 <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>
492 <form action=" $script " method="get">
493 <input type="hidden" name="file" value=" $virtual ">
494 <input type="hidden" name="command" value="unlock">
495 <p><input type="submit" value="Unlock file ' $virtual '"></p>
508 # Copy a file and return to directory view
510 # Params: 1. Reference to user input hash
511 # 2. Reference to config hash
513 # Return: Output of the command (Scalar Reference)
517 my ( $data , $config ) = @_ ;
518 my $physical = $data ->{ 'physical' };
519 my $virtual = encode_entities
( $data ->{ 'virtual' });
520 my $new_physical = $data ->{ 'new_physical' };
521 my $new_virtual = $data ->{ 'new_virtual' };
522 my $dir = upper_path
( $new_virtual );
523 $new_virtual = encode_entities
( $new_virtual );
525 return error
( "This editor is not able to copy directories." ) if (- d
$physical );
526 return error
( "You have not enough permissions to copy this file." ) unless (- r
$physical );
530 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 ));
533 copy
( $physical , $new_physical ) or return error
( "Could not copy ' $virtual ' to ' $new_virtual '" , upper_path
( $virtual ));
534 return devedit_reload
({ command
=> 'show' , file
=> $dir });
539 # Rename/move a file and return to directory view
541 # Params: 1. Reference to user input hash
542 # 2. Reference to config hash
544 # Return: Output of the command (Scalar Reference)
548 my ( $data , $config ) = @_ ;
549 my $physical = $data ->{ 'physical' };
550 my $virtual = $data ->{ 'virtual' };
551 my $new_physical = $data ->{ 'new_physical' };
552 my $new_virtual = $data ->{ 'new_virtual' };
553 my $dir = upper_path
( $new_virtual );
554 $new_virtual = encode_entities
( $new_virtual );
556 return error_in_use
( $virtual ) if ( $data ->{ 'uselist' }-> in_use ( $virtual ));
560 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 ));
563 rename ( $physical , $new_physical ) or return error
( "Could not move/rename '" . encode_entities
( $virtual ). "' to ' $new_virtual '." , upper_path
( $virtual ));
564 return devedit_reload
({ command
=> 'show' , file
=> $dir });
569 # Remove a file and return to directory view
571 # Params: 1. Reference to user input hash
572 # 2. Reference to config hash
574 # Return: Output of the command (Scalar Reference)
578 my ( $data , $config ) = @_ ;
579 my $physical = $data ->{ 'physical' };
580 my $virtual = $data ->{ 'virtual' };
582 return error
( "Deleting directories is currently unsupported." , upper_path
( $virtual )) if (- d
$physical );
583 return error_in_use
( $virtual ) if ( $data ->{ 'uselist' }-> in_use ( $virtual ));
585 unlink ( $physical ) or return error
( "Could not delete file '" . encode_entities
( $virtual ). "'." , upper_path
( $virtual ));
586 return devedit_reload
({ command
=> 'show' , file
=> upper_path
( $virtual )});
591 # Remove a file from the list of used files and
592 # return to directory view
594 # Params: 1. Reference to user input hash
595 # 2. Reference to config hash
597 # Return: Output of the command (Scalar Reference)
601 my ( $data , $config ) = @_ ;
602 my $virtual = $data ->{ 'virtual' };
603 my $uselist = $data ->{ 'uselist' };
605 $uselist -> remove_file ( $virtual );
608 return devedit_reload
({ command
=> 'show' , file
=> upper_path
( $virtual )});
611 # it's true, baby ;-)
patrick-canterino.de