end
end
-function action_run(...)
- local fs = require "nixio.fs"
+function execute_command(callback, ...)
+ local fs = require "nixio.fs"
local argv = parse_cmdline(...)
if argv then
local outfile = os.tmpname()
local binary = not not (stdout:match("[%z\1-\8\14-\31]"))
- luci.http.prepare_content("application/json")
- luci.http.write_json({
+ callback({
+ ok = true,
command = table.concat(argv, " "),
stdout = not binary and stdout,
stderr = stderr,
binary = binary
})
else
- luci.http.status(404, "No such command")
+ callback({
+ ok = false,
+ code = 404,
+ reason = "No such command"
+ })
+ end
+end
+
+function return_json(result)
+ if result.ok then
+ luci.http.prepare_content("application/json")
+ luci.http.write_json(result)
+ else
+ luci.http.status(result.code, result.reason)
end
end
+function action_run(...)
+ execute_command(return_json, ...)
+end
+
+function return_html(result)
+ if result.ok then
+ require("luci.template")
+ luci.template.render("commands_public", {
+ exitcode = result.exitcode,
+ stdout = result.stdout,
+ stderr = result.stderr
+ })
+ else
+ luci.http.status(result.code, result.reason)
+ end
+
+end
+
function action_download(...)
local fs = require "nixio.fs"
local argv = parse_cmdline(...)
local name
if chunk:match("[%z\1-\8\14-\31]") then
luci.http.header("Content-Disposition", "attachment; filename=%s"
- % fs.basename(argv[1]):gsub("%W+", ".") .. ".bin")
+ % fs.basename(argv[1]):gsub("%W+", ".") .. ".bin")
luci.http.prepare_content("application/octet-stream")
else
luci.http.header("Content-Disposition", "attachment; filename=%s"
- % fs.basename(argv[1]):gsub("%W+", ".") .. ".txt")
+ % fs.basename(argv[1]):gsub("%W+", ".") .. ".txt")
luci.http.prepare_content("text/plain")
end
end
end
+
function action_public(cmdid, args)
+ local disp = false
+ if string.sub(cmdid, -1) == "s" then
+ disp = true
+ cmdid = string.sub(cmdid, 1, -2)
+ end
local uci = require "luci.model.uci".cursor()
if cmdid and
- uci:get("luci", cmdid) == "command" and
- uci:get("luci", cmdid, "public") == "1"
- then
- action_download(cmdid, args)
- else
- luci.http.status(403, "Access to command denied")
+ uci:get("luci", cmdid) == "command" and
+ uci:get("luci", cmdid, "public") == "1"
+ then
+ if disp then
+ execute_command(return_html, cmdid, args)
+ else
+ action_download(cmdid, args)
+ end
+ else
+ luci.http.status(403, "Access to command denied")
+ end
end
-end
if (legend && output)
{
- var link = location.protocol + '//' + location.hostname +
+ var prefix = location.protocol + '//' + location.hostname +
(location.port ? ':' + location.port : '') +
- location.pathname.split(';')[0] + 'command/' +
- id + (args ? '/' + args : '');
-
+ location.pathname.split(';')[0] + 'command/';
+ var suffix = (args ? '/' + args : '');
+
+ var link = prefix + id + suffix;
+ var link_nodownload = prefix + id + "s" + suffix;
+
legend.style.display = 'none';
output.parentNode.style.display = 'block';
output.innerHTML = String.format(
- '<div class="alert-message"><%:Access command with%> <a href="%s">%s</a></div>',
- link, link
+ '<div class="alert-message"><p><%:Download execution result%> <a href="%s">%s</a></p><p><%:Or display result%> <a href="%s">%s</a></p></div>',
+ link, link, link_nodownload, link_nodownload
);
location.hash = '#output';
--- /dev/null
+<%#
+ Copyright 2016 t123yh <t123yh@outlook.com>
+ Licensed to the public under the Apache License 2.0.
+-%>
+
+<% css = [[
+.alert-success {
+ color: #3c763d;
+ background-color: #dff0d8;
+ border-color: #d6e9c6;
+}
+
+.alert {
+ padding: 15px;
+ margin-bottom: 20px;
+ border: 1px solid transparent;
+ border-radius: 4px;
+}
+
+.alert-warning {
+ color: #8a6d3b;
+ background-color: #fcf8e3;
+ border-color: #faebcc;
+}
+]] -%>
+
+<%+header%>
+
+<% if exitcode == 0 then %>
+ <div class="alert alert-success" role="alert"> <%:Command executed successfully.%> </div>
+<% else %>
+ <div class="alert alert-warning" role="alert"> <%:Command exited with status code %> <%= exitcode %> </div>
+<% end %>
+
+<% if stdout ~= "" then %>
+ <h3><%:Standard Output%></h3>
+ <pre><%= stdout %></pre>
+<% end %>
+
+<% if stderr ~= "" then %>
+ <h3><%:Standard Error%></h3>
+ <pre><%= stderr %></pre>
+<% end %>
+
+<script>
+ <%# Display top bar on mobile devices -%>
+ document.getElementsByClassName('brand')[0].style.setProperty("display", "block", "important");
+</script>
+
+<%+footer%>
\ No newline at end of file
msgid "A short textual description of the configured command"
msgstr "A short textual description of the configured command"
-msgid "Access command with"
-msgstr "Access command with"
-
msgid ""
"Allow executing the command and downloading its output without prior "
"authentication"
msgid "Waiting for command to complete..."
msgstr "Waiting for command to complete..."
+
+msgid "Command executed successfully."
+msgstr "Command executed successfully."
+
+msgid "Command exited with status code "
+msgstr "Command exited with status code "
+
+msgid "Standard Output"
+msgstr "Standard Output"
+
+msgid "Standard Error"
+msgstr "Standard Error"
+
+msgid "Download execution result"
+msgstr "Download execution result"
+
+msgid "Or display result"
+msgstr "Or display result"
\ No newline at end of file