Option to show command output raw with selectable MIME type
authorJamie Cameron <jcameron@webmin.com>
Tue, 21 Dec 2010 21:55:27 +0000 (13:55 -0800)
committerJamie Cameron <jcameron@webmin.com>
Tue, 21 Dec 2010 21:55:27 +0000 (13:55 -0800)
custom/CHANGELOG
custom/custom-lib.pl
custom/edit_cmd.cgi
custom/lang/en
custom/run.cgi
custom/save_cmd.cgi

index bea1a6c..c1c2431 100644 (file)
@@ -30,3 +30,5 @@ The sort order of commands can now be set on the Module Config page, and is resp
 ---- Changes since 1.510 ----
 Fixed a bug that broke remote command execution with parameters.
 Added a new parameter type for selecting multiple items from a menu.
+---- Changes since 1.530 ----
+Added an option for custom commands to have their output displayed without any Webmin UI, in a selectable MIME type.
index 506ed6d..34414a5 100755 (executable)
@@ -32,6 +32,7 @@ while($f = readdir(DIR)) {
                $cmd{'usermin'} = int($o[5]);
                $cmd{'timeout'} = int($o[6]);
                $cmd{'clear'} = int($o[7]);
+               $cmd{'format'} = $o[8] eq '-' ? undef : $o[8];
                }
        elsif ($f =~ /^(\d+)\.edit$/) {
                # Read file-editor file
@@ -172,7 +173,7 @@ else {
                   $c->{'user'}," ",int($c->{'raw'})," ",int($c->{'su'})," ",
                   int($c->{'order'})," ",int($c->{'noshow'})," ",
                   int($c->{'usermin'})," ",int($c->{'timeout'})," ",
-                  int($c->{'clear'}),"\n");
+                  int($c->{'clear'})," ",($c->{'format'} || "-"),"\n");
        }
 
 
index 82ad344..1a3bcc7 100755 (executable)
@@ -73,13 +73,18 @@ print &ui_table_row(&hlink($text{'edit_usermin'},"usermin"),
 
 # Command timeout
 print &ui_table_row(&hlink($text{'edit_timeout'},"timeout"),
-       &ui_opt_textbox("timeout", $cmd->{'timeout'}, 6, $text{'default'}).
-       " ".$text{'edit_secs'});
+       &ui_opt_textbox("timeout", $cmd->{'timeout'} || undef, 6,
+         $text{'default'})." ".$text{'edit_secs'});
 
 # Clear environment?
 print &ui_table_row(&hlink($text{'edit_clear'},"clear"),
        &ui_yesno_radio("clear", $cmd->{'clear'}));
 
+# Output format
+print &ui_table_row(&hlink($text{'edit_format'}, "format"),
+       &ui_opt_textbox("format", $cmd->{'format'}, 20, $text{'edit_format0'},
+                       $text{'edit_format1'}));
+
 # Show Webmin servers to run on
 @servers = &list_servers();
 if (@servers > 1) {
index 0fc0f5d..eda085a 100644 (file)
@@ -53,6 +53,9 @@ edit_timeout=Maximum time to wait for command?
 edit_timeoutdef=Forever
 edit_secs=seconds
 edit_clear=Clear environment variables?
+edit_format=Output style
+edit_format0=Show in Webmin UI
+edit_format1=Output with MIME type
 edit_servers=Run on Webmin servers
 edit_this=this server
 edit_clone=Clone
@@ -66,6 +69,7 @@ save_euser=Missing or invalid user
 save_eorder=Ordering on main page must be a number
 save_etimeout=Missing or invalid maximum time to wait
 save_eusermin=Commands that run on other servers cannot be run from Usermin
+save_eformat=Invalid output MIME type
 
 run_err=Failed to execute command
 run_ecannot=You are not allowed to run this command
index ba4340d..0707fe2 100755 (executable)
@@ -34,7 +34,14 @@ else {
 @servers = &list_servers();
 
 # Run and display output
-&ui_print_unbuffered_header($cmd->{'desc'}, $text{'run_title'}, "", -d "help" ? "run" : undef);
+if ($cmd->{'format'}) {
+       print "Content-type: ",$cmd->{'format'},"\n";
+       print "\n";
+       }
+else {
+       &ui_print_unbuffered_header($cmd->{'desc'}, $text{'run_title'}, "",
+                                   -d "help" ? "run" : undef);
+       }
 
 &remote_error_setup(\&remote_custom_handler);
 
@@ -45,9 +52,11 @@ foreach $h (@hosts) {
        if (@{$cmd->{'hosts'}}) {
                $txt .= 'on';
                }
-       print &text($txt, "<tt>".&html_escape($displaystr)."</tt>",
-                 $server->{'desc'} || "<tt>$server->{'host'}</tt>"),"\n";
-       print "<pre>" if (!$cmd->{'raw'});
+       if (!$cmd->{'format'}) {
+               print &text($txt, "<tt>".&html_escape($displaystr)."</tt>",
+                   $server->{'desc'} || "<tt>$server->{'host'}</tt>"),"\n";
+               print "<pre>" if (!$cmd->{'raw'});
+               }
        $remote_custom_error = undef;
        if ($h == 0) {
                # Run locally
@@ -70,16 +79,20 @@ foreach $h (@hosts) {
                }
        if (!$remote_custom_error) {
                print $out if ($h != 0);
-               if (!$got) {
+               if (!$got && !$cmd->{'format'}) {
                        print "<i>$text{'run_noout'}</i>\n";
                        }
                }
-       print "</pre>\n" if (!$cmd->{'raw'});
-       if ($remote_custom_error) {
-               print "<b>$remote_custom_error</b><p>\n";
-               }
-       elsif ($timeout) {
-               print "<b>",&text('run_timeout', $cmd->{'timeout'}),"</b><p>\n";
+
+       if (!$cmd->{'format'}) {
+               print "</pre>\n" if (!$cmd->{'raw'});
+               if ($remote_custom_error) {
+                       print "<b>$remote_custom_error</b><p>\n";
+                       }
+               elsif ($timeout) {
+                       print "<b>",&text('run_timeout',
+                                         $cmd->{'timeout'}),"</b><p>\n";
+                       }
                }
 
        # Only log non-upload inputs
@@ -93,7 +106,9 @@ foreach $h (@hosts) {
        }
 &webmin_log("exec", "command", $cmd->{'id'}, \%cmdin);
 unlink(@unlink) if (@unlink);
-&ui_print_footer("", $text{'index_return'});
+if (!$cmd->{'format'}) {
+       &ui_print_footer("", $text{'index_return'});
+       }
 
 sub remote_custom_handler
 {
index 93f0a50..8df2f29 100755 (executable)
@@ -54,6 +54,14 @@ else {
        $cmd->{'order'} = $in{'order_def'} ? 0 : int($in{'order'});
        $cmd->{'timeout'} = $in{'timeout_def'} ? 0 : int($in{'timeout'});
        $cmd->{'clear'} = $in{'clear'};
+       if ($in{'format_def'}) {
+               delete($cmd->{'format'});
+               }
+       else {
+               $in{'format'} =~ /^[a-z0-9\.\_\-]+\/[a-z0-9\.\_\-]+/i ||
+                       &error($text{'save_eformat'});
+               $cmd->{'format'} = $in{'format'};
+               }
        $cmd->{'noshow'} = $in{'noshow'};
        $cmd->{'usermin'} = $in{'usermin'};
        @hosts = split(/\0/, $in{'hosts'});