]>
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-12-02
22 use POSIX qw(strftime) ;
25 my $script = $ENV { 'SCRIPT_NAME' };
27 my %dispatch = ( 'show' => \
& exec_show
,
28 'beginedit' => \
& exec_beginedit
,
29 'canceledit' => \
& exec_unlock
,
30 'endedit' => \
& exec_endedit
,
31 'mkdir' => \
& exec_mkdir
,
32 'mkfile' => \
& exec_mkfile
,
33 'workwithfile' => \
& exec_workwithfile
,
34 'workwithdir' => \
& exec_workwithdir
,
35 'copy' => \
& exec_copy
,
36 'rename' => \
& exec_rename
,
37 'remove' => \
& exec_remove
,
38 'rmdir' => \
& exec_rmdir
,
39 'unlock' => \
& exec_unlock
44 use base
qw(Exporter) ;
46 @EXPORT = qw(exec_command) ;
50 # Execute the specified command
52 # Params: 1. Command to execute
53 # 2. Reference to user input hash
54 # 3. Reference to config hash
56 # Return: Output of the command (Scalar Reference)
60 my ( $command , $data , $config ) = @_ ;
62 return error
( "Unknown command: $command " ) unless ( $dispatch { $command });
64 my $output = &{ $dispatch { $command }}( $data , $config );
70 # View a directory or a file
72 # Params: 1. Reference to user input hash
73 # 2. Reference to config hash
75 # Return: Output of the command (Scalar Reference)
79 my ( $data , $config ) = @_ ;
80 my $physical = $data ->{ 'physical' };
81 my $virtual = $data ->{ 'virtual' };
86 # Create directory listing
88 my $direntries = dir_read
( $physical );
89 return error
( "Reading of directory $virtual failed." , upper_path
( $virtual )) unless ( $direntries );
91 my $files = $direntries ->{ 'files' };
92 my $dirs = $direntries ->{ 'dirs' };
94 $output .= htmlhead
( "Directory listing of $virtual " );
95 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
96 $output .= "<hr> \n\n <pre> \n " ;
98 # Create the link to the upper directory
99 # (only if we are not in the root directory)
101 unless ( $virtual eq "/" )
103 my $upper = $physical . "/.." ;
104 my @stat = stat ( $upper );
106 $output .= " [SUBDIR] " ;
107 $output .= strftime
( " %d . %m . %Y %H : %M " , localtime ( $stat [ 9 ]));
109 $output .= "<a href= \" $script ?command=show&file=" . encode_entities
( upper_path
( $virtual )). " \" >../</a> \n " ;
112 # Get the length of the longest file/directory name
114 my $max_name_len = 0 ;
116 foreach ( @
$dirs , @
$files )
118 my $length = length ( $_ );
119 $max_name_len = $length if ( $length > $max_name_len );
124 foreach my $dir ( @
$dirs )
126 my @stat = stat ( $physical . "/" . $dir );
127 my $virt_path = encode_entities
( $virtual . $dir . "/" );
130 $output .= "[SUBDIR] " ;
131 $output .= strftime
( $config ->{ 'timeformat' }, localtime ( $stat [ 9 ]));
133 $output .= "<a href= \" $script ?command=show&file= $virt_path \" >" . encode_entities
( $dir ). "/</a>" ;
134 $output .= " " x
( $max_name_len - length ( $dir ) - 1 ). " \t (" ;
135 $output .= "<a href= \" $script ?command=workwithdir&file= $virt_path \" >Work with directory</a>) \n " ;
140 foreach my $file ( @
$files )
142 my $phys_path = $physical . "/" . $file ;
143 my $virt_path = encode_entities
( $virtual . $file );
145 my @stat = stat ( $phys_path );
146 my $in_use = $data ->{ 'uselist' }-> in_use ( $virtual . $file );
148 $output .= " " x
( 10 - length ( $stat [ 7 ]));
151 $output .= strftime
( $config ->{ 'timeformat' }, localtime ( $stat [ 9 ]));
153 $output .= encode_entities
( $file );
154 $output .= " " x
( $max_name_len - length ( $file )). " \t (" ;
158 if (- r
$phys_path && - T
$phys_path )
160 $output .= "<a href= \" $script ?command=show&file= $virt_path \" >View</a>" ;
164 $output .= '<span style="color:#C0C0C0" title="' ;
166 $output .= ( not - r
$phys_path ) ?
"Not readable" :
167 (- B
$phys_path ) ?
"Binary file" : "" ;
169 $output .= '">View</span>' ;
176 if (- w
$phys_path && - r
$phys_path && - T
$phys_path && not $in_use )
178 $output .= "<a href= \" $script ?command=beginedit&file= $virt_path \" >Edit</a>" ;
182 $output .= '<span style="color:#C0C0C0" title="' ;
184 $output .= ( not - r
$phys_path ) ?
"Not readable" :
185 ( not - w
$phys_path ) ?
"Read only" :
186 (- B
$phys_path ) ?
"Binary file" :
187 ( $in_use ) ?
"In use" : "" ;
189 $output .= '">Edit</span>' ;
192 # Link "Do other stuff"
194 $output .= " | <a href= \" $script ?command=workwithfile&file= $virt_path \" >Work with file</a>) \n " ;
197 $output .= "</pre> \n\n <hr> \n\n " ;
199 # Bottom of directory listing
200 # (Fields for creating files and directories)
205 <form action=" $script ">
206 <input type="hidden" name="command" value="mkdir">
207 <input type="hidden" name="curdir" value=" $virtual ">
208 <td>Create new directory:</td>
209 <td> $virtual <input type="text" name="newfile"> <input type="submit" value="Create!"></td>
213 <td>Create new file:</td>
214 <form action=" $script ">
215 <input type="hidden" name="command" value="mkfile">
216 <input type="hidden" name="curdir" value=" $virtual ">
217 <td> $virtual <input type="text" name="newfile"> <input type="submit" value="Create!"></td>
230 return error
( "You have not enough permissions to view this file." , upper_path
( $virtual )) unless (- r
$physical );
232 # Check on binary files
233 # We have to do it in this way, or empty files
234 # will be recognized as binary files
240 return error
( "This editor is not able to view/edit binary files." , upper_path
( $virtual ));
246 $output = htmlhead
( "Contents of file " . encode_entities
( $virtual ));
247 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
248 $output .= dir_link
( $virtual );
250 $output .= '<div style="background-color:#FFFFE0;border:1px solid black;margin-top:10px;width:100%">' . " \n " ;
251 $output .= '<pre style="color:#0000C0;">' . " \n " ;
252 $output .= encode_entities
(${ file_read
( $physical )});
253 $output .= " \n </pre> \n </div>" ;
264 # Lock a file and display a form to edit it
266 # Params: 1. Reference to user input hash
267 # 2. Reference to config hash
269 # Return: Output of the command (Scalar Reference)
271 sub exec_beginedit
($$)
273 my ( $data , $config ) = @_ ;
274 my $physical = $data ->{ 'physical' };
275 my $virtual = $data ->{ 'virtual' };
276 my $uselist = $data ->{ 'uselist' };
278 return error
( "You cannot edit directories." , upper_path
( $virtual )) if (- d
$physical );
279 return error_in_use
( $virtual ) if ( $uselist -> in_use ( $virtual ));
280 return error
( "You have not enough permissions to edit this file." , upper_path
( $virtual )) unless (- r
$physical && - w
$physical );
282 # Check on binary files
288 return error
( "This editor is not able to view/edit binary files." , upper_path
( $virtual ));
294 $uselist -> add_file ( $virtual );
297 my $dir = upper_path
( $virtual );
298 my $content = encode_entities
(${ file_read
( $physical )});
300 my $equal_url = equal_url
( $config ->{ 'httproot' }, $virtual );
302 $virtual = encode_entities
( $virtual );
304 my $output = htmlhead
( "Edit file $virtual " );
305 $output .= $equal_url ;
307 <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>
309 <form action=" $script " method="get">
310 <input type="hidden" name="command" value="canceledit">
311 <input type="hidden" name="file" value=" $virtual ">
312 <p><input type="submit" value="Exit WITHOUT saving"></p>
315 <form action=" $script " method="post">
316 <input type="hidden" name="command" value="endedit">
317 <input type="hidden" name="file" value=" $virtual ">
319 <table width="100%" border="1">
321 <td width="50%" align="center">
322 <input type="hidden" name="file" value=" $virtual ">
323 <input type="checkbox" name="saveas" value="1"> Save as new file: $dir <input type=text name="newfile" value=""></td>
324 <td width="50%" align="center"><input type="checkbox" name="encode_iso" value="1"> Encode ISO-8859-1 special chars</td>
327 <td align="center"><input type="reset" value="Reset form"></td>
328 <td align="center"><input type="submit" value="Save and exit"></td>
332 <textarea name="filecontent" rows="25" cols="120"> $content </textarea>
344 # Save a file, unlock it and return to directory view
346 # Params: 1. Reference to user input hash
347 # 2. Reference to config hash
349 # Return: Output of the command (Scalar Reference)
353 my ( $data , $config ) = @_ ;
354 my $physical = $data ->{ 'physical' };
355 my $virtual = $data ->{ 'virtual' };
356 my $content = $data ->{ 'cgi' }-> param ( 'filecontent' );
358 return error
( "You cannot edit directories." ) if (- d
$physical );
359 return error
( "You have not enough permissions to edit this file." , upper_path
( $virtual )) unless (- r
$physical && - w
$physical );
363 $content =~ s/\015\012|\012|\015/\n/g ;
365 if ( $data ->{ 'cgi' }-> param ( 'encode_iso' ))
367 # Encode all ISO-8859-1 special chars
369 $content = encode_entities
( $content , " \200 - \377 " );
372 if ( $data ->{ 'cgi' }-> param ( 'saveas' ))
374 # Create the new filename
376 $physical = $data ->{ 'new_physical' };
377 $virtual = $data ->{ 'new_virtual' };
380 if ( file_save
( $physical , \
$content ))
382 # Saving of the file was successful - so unlock it!
384 return exec_unlock
( $data , $config );
388 return error
( "Saving of file '" . encode_entities
( $virtual ). "' failed'." , upper_path
( $virtual ));
394 # Create a file and return to directory view
396 # Params: 1. Reference to user input hash
397 # 2. Reference to config hash
399 # Return: Output of the command (Scalar Reference)
403 my ( $data , $config ) = @_ ;
404 my $new_physical = $data ->{ 'new_physical' };
405 my $new_virtual = $data ->{ 'new_virtual' };
406 my $dir = upper_path
( $new_virtual );
407 $new_virtual = encode_entities
( $new_virtual );
409 return error
( "A file or directory called ' $new_virtual ' already exists." , $dir ) if (- e
$new_physical );
411 file_create
( $new_physical ) or return error
( "Could not create file ' $new_virtual '." , $dir );
412 return devedit_reload
({ command
=> 'show' , file
=> $dir });
417 # Create a directory and return to directory view
419 # Params: 1. Reference to user input hash
420 # 2. Reference to config hash
422 # Return: Output of the command (Scalar Reference)
426 my ( $data , $config ) = @_ ;
427 my $new_physical = $data ->{ 'new_physical' };
428 my $new_virtual = $data ->{ 'new_virtual' };
429 my $dir = upper_path
( $new_virtual );
430 $new_virtual = encode_entities
( $new_virtual );
432 return error
( "A file or directory called ' $new_virtual ' already exists." , $dir ) if (- e
$new_physical );
434 mkdir ( $new_physical , 0777 ) or return error
( "Could not create directory ' $new_virtual '." , $dir );
435 return devedit_reload
({ command
=> 'show' , file
=> $dir });
438 # exec_workwithfile()
440 # Display a form for renaming/copying/deleting/unlocking a file
442 # Params: 1. Reference to user input hash
443 # 2. Reference to config hash
445 # Return: Output of the command (Scalar Reference)
447 sub exec_workwithfile
($$)
449 my ( $data , $config ) = @_ ;
450 my $physical = $data ->{ 'physical' };
451 my $virtual = $data ->{ 'virtual' };
452 my $unused = $data ->{ 'uselist' }-> unused ( $virtual );
454 my $dir = encode_entities
( upper_path
( $virtual ));
456 my $output = htmlhead
( "Work with file " . encode_entities
( $virtual ));
457 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
459 $virtual = encode_entities
( $virtual );
461 $output .= dir_link
( $virtual );
462 $output .= "<p><b>Note:</b> On UNIX systems, filenames are <b>case-sensitive</b>!</p> \n\n " ;
464 $output .= "<p>Someone else is currently editing this file. So not all features are available.</p> \n\n " unless ( $unused );
466 $output .= "<hr> \n\n " ;
468 # Copying of the file is always allowed - but we need read access
475 <form action=" $script ">
476 <input type="hidden" name="command" value="copy">
477 <input type="hidden" name="file" value=" $virtual ">
478 <p>Copy file ' $virtual ' to:<br> $dir <input type="text" name="newfile" size="30"> <input type="submit" value="Copy!"></p>
489 # Allow renaming and deleting the file
494 <form action=" $script ">
495 <input type="hidden" name="command" value="rename">
496 <input type="hidden" name="file" value=" $virtual ">
497 <p>Move/Rename file ' $virtual ' to:<br> $dir <input type="text" name="newfile" size="30"> <input type="submit" value="Move/Rename!"></p>
504 <p>Click on the button below to remove the file ' $virtual '.</p>
506 <form action=" $script " method="get">
507 <input type="hidden" name="file" value=" $virtual ">
508 <input type="hidden" name="command" value="remove">
509 <p><input type="submit" value="Delete file!"></p>
516 # Just display a button for unlocking it
521 <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>
523 <form action=" $script " method="get">
524 <input type="hidden" name="file" value=" $virtual ">
525 <input type="hidden" name="command" value="unlock">
526 <p><input type="submit" value="Unlock file ' $virtual '"></p>
539 # Display a form for renaming/deleting a directory
541 # Params: 1. Reference to user input hash
542 # 2. Reference to config hash
544 # Return: Output of the command (Scalar Reference)
546 sub exec_workwithdir
($$)
548 my ( $data , $config ) = @_ ;
549 my $physical = $data ->{ 'physical' };
550 my $virtual = $data ->{ 'virtual' };
552 my $dir = encode_entities
( upper_path
( $virtual ));
554 my $output = htmlhead
( "Work with directory " . encode_entities
( $virtual ));
555 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
557 $virtual = encode_entities
( $virtual );
559 $output .= dir_link
( $virtual );
560 $output .= "<p><b>Note:</b> On UNIX systems, filenames are <b>case-sensitive</b>!</p> \n\n " ;
561 $output .= "<hr> \n\n " ;
566 <form action=" $script ">
567 <input type="hidden" name="command" value="rename">
568 <input type="hidden" name="file" value=" $virtual ">
569 <p>Move/Rename directory ' $virtual ' to: $dir <input type="text" name="newfile" size="50"> <input type="submit" value="Move/Rename!"></p>
576 <p>Click on the button below to completely remove the directory ' $virtual ' and oll of it's files and sub directories.</p>
578 <form action=" $script " method="get">
579 <input type="hidden" name="file" value=" $virtual ">
580 <input type="hidden" name="command" value="rmdir">
581 <p><input type="submit" value="Delete!"></p>
593 # Copy a file and return to directory view
595 # Params: 1. Reference to user input hash
596 # 2. Reference to config hash
598 # Return: Output of the command (Scalar Reference)
602 my ( $data , $config ) = @_ ;
603 my $physical = $data ->{ 'physical' };
604 my $virtual = encode_entities
( $data ->{ 'virtual' });
605 my $new_physical = $data ->{ 'new_physical' };
606 my $new_virtual = $data ->{ 'new_virtual' };
607 my $dir = upper_path
( $new_virtual );
608 $new_virtual = encode_entities
( $new_virtual );
610 return error
( "This editor is not able to copy directories." ) if (- d
$physical );
611 return error
( "You have not enough permissions to copy this file." ) unless (- r
$physical );
615 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 ));
618 copy
( $physical , $new_physical ) or return error
( "Could not copy ' $virtual ' to ' $new_virtual '" , upper_path
( $virtual ));
619 return devedit_reload
({ command
=> 'show' , file
=> $dir });
624 # Rename/move a file and return to directory view
626 # Params: 1. Reference to user input hash
627 # 2. Reference to config hash
629 # Return: Output of the command (Scalar Reference)
633 my ( $data , $config ) = @_ ;
634 my $physical = $data ->{ 'physical' };
635 my $virtual = $data ->{ 'virtual' };
636 my $new_physical = $data ->{ 'new_physical' };
637 my $new_virtual = $data ->{ 'new_virtual' };
638 my $dir = upper_path
( $new_virtual );
639 $new_virtual = encode_entities
( $new_virtual );
641 return error_in_use
( $virtual ) if ( $data ->{ 'uselist' }-> in_use ( $virtual ));
645 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 ));
648 rename ( $physical , $new_physical ) or return error
( "Could not move/rename '" . encode_entities
( $virtual ). "' to ' $new_virtual '." , upper_path
( $virtual ));
649 return devedit_reload
({ command
=> 'show' , file
=> $dir });
654 # Remove a file and return to directory view
656 # Params: 1. Reference to user input hash
657 # 2. Reference to config hash
659 # Return: Output of the command (Scalar Reference)
663 my ( $data , $config ) = @_ ;
664 my $physical = $data ->{ 'physical' };
665 my $virtual = $data ->{ 'virtual' };
667 return exec_rmdir
( $data , $config ) if (- d
$physical );
668 return error_in_use
( $virtual ) if ( $data ->{ 'uselist' }-> in_use ( $virtual ));
670 unlink ( $physical ) or return error
( "Could not delete file '" . encode_entities
( $virtual ). "'." , upper_path
( $virtual ));
671 return devedit_reload
({ command
=> 'show' , file
=> upper_path
( $virtual )});
676 # Remove a directory and return to directory view
678 # Params: 1. Reference to user input hash
679 # 2. Reference to config hash
681 # Return: Output of the command (Scalar Reference)
685 my ( $data , $config ) = @_ ;
686 my $physical = $data ->{ 'physical' };
687 my $virtual = $data ->{ 'virtual' };
689 if ( $data ->{ 'cgi' }-> param ( 'confirmed' ))
692 return devedit_reload
({ command
=> 'show' , file
=> upper_path
( $virtual )});
696 my $dir = encode_entities
( upper_path
( $virtual ));
699 $output = htmlhead
( "Remove directory $virtual " );
700 $output .= equal_url
( $config ->{ 'httproot' }, $virtual );
702 $virtual = encode_entities
( $virtual );
704 $output .= dir_link
( $virtual );
707 <p>Do you really want to remove the directory ' $virtual ' and all of it's files and sub directories?</p>
709 <form action=" $script " method="get">
710 <input type="hidden" name="command" value="rmdir">
711 <input type="hidden" name="file" value=" $virtual ">
712 <input type="hidden" name="confirmed" value="1">
714 <input type="submit" value="Yes">
717 <form action=" $script " method="get">
718 <input type="hidden" name="command" value="show">
719 <input type="hidden" name="file" value=" $dir ">
721 <input type="submit" value="No">
733 # Remove a file from the list of used files and
734 # return to directory view
736 # Params: 1. Reference to user input hash
737 # 2. Reference to config hash
739 # Return: Output of the command (Scalar Reference)
743 my ( $data , $config ) = @_ ;
744 my $virtual = $data ->{ 'virtual' };
745 my $uselist = $data ->{ 'uselist' };
747 $uselist -> remove_file ( $virtual );
750 return devedit_reload
({ command
=> 'show' , file
=> upper_path
( $virtual )});
753 # it's true, baby ;-)
patrick-canterino.de