Command to run before editing a file
authorJamie Cameron <jcameron@webmin.com>
Sat, 13 Aug 2011 05:03:30 +0000 (22:03 -0700)
committerJamie Cameron <jcameron@webmin.com>
Sat, 13 Aug 2011 05:03:30 +0000 (22:03 -0700)
custom/CHANGELOG
custom/custom-lib.pl
custom/edit_file.cgi
custom/help/beforeedit.html [new file with mode: 0644]
custom/lang/en
custom/save.cgi
custom/save_file.cgi
custom/view.cgi

index a96f7d3..0e3a687 100644 (file)
@@ -35,3 +35,5 @@ Added an option for custom commands to have their output displayed without any W
 ---- Changes since 1.550 ----
 A default value for each custom command parameter can now be entered on the Edit Command form. Defaults can also be read from a file or shell command, if this behavior is enabled on the Module Config page. Thanks to Sart Cole for suggesting this feature.
 A command can now be configured to not display any output at all, and instead return to the module index after being run.
+---- Changes since 1.560 ----
+Added an option to the file editor to run a command before the file is displayed, thanks to a suggestion from Sart Cole.
index 90c1664..07c86f9 100755 (executable)
@@ -50,6 +50,7 @@ while($f = readdir(DIR)) {
                $cmd{'order'} = int($cmd{'order'});
                chop($cmd{'usermin'} = <FILE>);
                chop($cmd{'envs'} = <FILE>);
+               chop($cmd{'beforeedit'} = <FILE>);
                }
        elsif ($f =~ /^(\d+)\.sql$/) {
                # Read SQL file
@@ -150,6 +151,7 @@ if ($c->{'edit'}) {
        &print_tempfile(FILE, $c->{'order'},"\n");
        &print_tempfile(FILE, $c->{'usermin'},"\n");
        &print_tempfile(FILE, $c->{'envs'},"\n");
+       &print_tempfile(FILE, $c->{'beforeedit'},"\n");
        }
 elsif ($c->{'sql'}) {
        # Save an SQL command
index a829d6d..80b5fb5 100755 (executable)
@@ -53,6 +53,8 @@ print &ui_table_row(&hlink($text{'file_perms'}, "perms"),
                        $text{'file_set'}));
 
 # Commands to run before and after
+print &ui_table_row(&hlink($text{'file_beforeedit'}, "beforeedit"),
+       &ui_textbox("beforeedit", $edit->{'beforeedit'}, 60));
 print &ui_table_row(&hlink($text{'file_before'}, "before"),
        &ui_textbox("before", $edit->{'before'}, 60));
 print &ui_table_row(&hlink($text{'file_after'}, "after"),
diff --git a/custom/help/beforeedit.html b/custom/help/beforeedit.html
new file mode 100644 (file)
index 0000000..135df80
--- /dev/null
@@ -0,0 +1,9 @@
+<header>Command to run before editing</header>
+
+Whatever command is entered here will be run as <tt>root</tt> before
+the contents of the file are displayed for editing. This could be used for
+example to copy the file in from another system, and the <b>Command to run
+after saving</b> could be used to copy it back. <p>
+
+<hr>
+
index 20f4a09..d7b29a4 100644 (file)
@@ -115,6 +115,7 @@ file_group=Group
 file_leave=Leave as is
 file_perms=File permissions
 file_set=Set to octal
+file_beforeedit=Command to run before editing
 file_before=Command to run before saving
 file_after=Command to run after saving
 file_err=Failed to save file editor
index c3a4f9d..b7c38cb 100755 (executable)
@@ -26,7 +26,7 @@ if ($edit->{'envs'} || @{$edit->{'args'}}) {
 
 # Run the before-command
 if ($edit->{'before'}) {
-       &system_logged("$edit->{'before'} >/dev/null 2>&1 </dev/null");
+       &system_logged("($edit->{'before'}) >/dev/null 2>&1 </dev/null");
        }
 
 # Save the file
@@ -48,7 +48,7 @@ if ($edit->{'perms'}) {
 
 # Run the after-command
 if ($edit->{'after'}) {
-       &system_logged("$edit->{'after'} >/dev/null 2>&1 </dev/null");
+       &system_logged("($edit->{'after'}) >/dev/null 2>&1 </dev/null");
        }
 
 &webmin_log("save", "edit", $cmd->{'id'}, $edit);
index 428edb6..cd668c2 100755 (executable)
@@ -48,6 +48,7 @@ else {
                $in{'perms'} =~ /^[0-7]{3}$/ || &error($text{'file_eperms'});
                $edit->{'perms'} = $in{'perms'};
                }
+       $edit->{'beforeedit'} = $in{'beforeedit'};
        $edit->{'before'} = $in{'before'};
        $edit->{'after'} = $in{'after'};
        $edit->{'order'} = $in{'order_def'} ? 0 : int($in{'order'});
index b7330a5..5b7c063 100755 (executable)
@@ -24,6 +24,11 @@ if ($edit->{'envs'} || @{$edit->{'args'}}) {
        chop($file = `echo "$file"`);
        }
 
+# Run any before-edit command
+if ($edit->{'beforeedit'}) {
+       &system_logged("($edit->{'beforeedit'}) >/dev/null 2>&1 </dev/null");
+       }
+
 # Show the editor form
 &ui_print_header(undef, $text{'view_title'}, "");