| 1 | --[[ |
|---|
| 2 | LuCI - Lua Configuration Interface |
|---|
| 3 | |
|---|
| 4 | Copyright 2008 Steven Barth <steven@midlink.org> |
|---|
| 5 | |
|---|
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | You may obtain a copy of the License at |
|---|
| 9 | |
|---|
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 11 | |
|---|
| 12 | $Id$ |
|---|
| 13 | ]]-- |
|---|
| 14 | |
|---|
| 15 | local os = os |
|---|
| 16 | local io = io |
|---|
| 17 | local require = require |
|---|
| 18 | local pairs = pairs |
|---|
| 19 | local string = string |
|---|
| 20 | local luci = require("luci") |
|---|
| 21 | module "luci.controller.fon_admin.fon_index" |
|---|
| 22 | |
|---|
| 23 | function index() |
|---|
| 24 | local root = node() |
|---|
| 25 | root.lock = true |
|---|
| 26 | root.target = call("root") |
|---|
| 27 | |
|---|
| 28 | node("fon_dashboard").target = call("dashboard") |
|---|
| 29 | entry({"fon_dashboard", "_status"}, template("fon/admin_fon_summary")) |
|---|
| 30 | entry({"fon_dashboard", "_attached"}, template("fon/admin_fon_attached")) |
|---|
| 31 | assign({"_maincontent"}, {}) |
|---|
| 32 | |
|---|
| 33 | local page = node("fon_plugins") |
|---|
| 34 | page.target = call("action_plugin") |
|---|
| 35 | page.title = "Plugins" |
|---|
| 36 | page.sysauth = "root" |
|---|
| 37 | page.sysauth_authenticator = "htmlauth" |
|---|
| 38 | |
|---|
| 39 | local page = node("fon_status") |
|---|
| 40 | page.target = template("fon_status/main") |
|---|
| 41 | |
|---|
| 42 | local page = node("splash") |
|---|
| 43 | page.title = "Splash" |
|---|
| 44 | page.target = template("fon/splash") |
|---|
| 45 | end |
|---|
| 46 | |
|---|
| 47 | function dashboard() |
|---|
| 48 | local util = require "luci.util" |
|---|
| 49 | local uci = require "luci.model.uci".cursor() |
|---|
| 50 | local tpl = require "luci.template" |
|---|
| 51 | local dsp = require "luci.dispatcher" |
|---|
| 52 | local plugins = uci:get_all("plugfons") |
|---|
| 53 | local objects = {} |
|---|
| 54 | |
|---|
| 55 | for k, v in util.spairs(plugins, function (a,b) |
|---|
| 56 | return (plugins[a].order or 100) < (plugins[b].order or 100) |
|---|
| 57 | end) do |
|---|
| 58 | local obj = {} |
|---|
| 59 | if(v.dashicon ~= nil) |
|---|
| 60 | then |
|---|
| 61 | obj.csscl = v.name |
|---|
| 62 | obj.iconl = luci.config.main.resourcebase.."/"..v.dashicon |
|---|
| 63 | else |
|---|
| 64 | obj.csscl = "%s" % v.id |
|---|
| 65 | end |
|---|
| 66 | obj.target = dsp.build_url(v.href) |
|---|
| 67 | obj.title = v.name |
|---|
| 68 | objects[#objects+1] = obj |
|---|
| 69 | end |
|---|
| 70 | |
|---|
| 71 | tpl.render("fon/master_dashboard", {objects=objects}) |
|---|
| 72 | tpl.render("fon/admin_fon") |
|---|
| 73 | end |
|---|
| 74 | |
|---|
| 75 | function root() |
|---|
| 76 | local uci = require "luci.model.uci".cursor() |
|---|
| 77 | local dsp = require "luci.dispatcher" |
|---|
| 78 | |
|---|
| 79 | local i = 0 |
|---|
| 80 | local pluginname = nil |
|---|
| 81 | uci:foreach("plugfons", "plugin", function(s) |
|---|
| 82 | pluginname = s[".name"] |
|---|
| 83 | i = i + 1 |
|---|
| 84 | end) |
|---|
| 85 | |
|---|
| 86 | -- No plugins => /fon_status |
|---|
| 87 | if i == 1 then |
|---|
| 88 | dsp.dispatch({pluginname}) |
|---|
| 89 | else |
|---|
| 90 | dsp.dispatch({"fon_dashboard"}) |
|---|
| 91 | end |
|---|
| 92 | end |
|---|
| 93 | |
|---|
| 94 | function action_plugin() |
|---|
| 95 | require("luci") |
|---|
| 96 | |
|---|
| 97 | local ret = nil |
|---|
| 98 | local tmpfile = "/tmp/update.img" |
|---|
| 99 | local keep_avail = true |
|---|
| 100 | |
|---|
| 101 | local file |
|---|
| 102 | local first = true |
|---|
| 103 | local type |
|---|
| 104 | luci.http.setfilehandler( |
|---|
| 105 | function(meta, chunk, eof) |
|---|
| 106 | if not chunk then |
|---|
| 107 | return |
|---|
| 108 | end |
|---|
| 109 | if first then |
|---|
| 110 | os.remove(tmpfile) |
|---|
| 111 | os.remove(tmpfile) |
|---|
| 112 | if chunk:sub(1,2) == "\031\139" then |
|---|
| 113 | type = "tgz" |
|---|
| 114 | elseif chunk:sub(1,2) == "FO" then |
|---|
| 115 | type = "fon" |
|---|
| 116 | else |
|---|
| 117 | ret = "Invalid plugin" |
|---|
| 118 | end |
|---|
| 119 | end |
|---|
| 120 | first = false |
|---|
| 121 | if type then |
|---|
| 122 | if not file then |
|---|
| 123 | file = io.open(tmpfile, "w") |
|---|
| 124 | end |
|---|
| 125 | if chunk then |
|---|
| 126 | file:write(chunk) |
|---|
| 127 | end |
|---|
| 128 | if eof then |
|---|
| 129 | file:close() |
|---|
| 130 | end |
|---|
| 131 | end |
|---|
| 132 | end |
|---|
| 133 | ) |
|---|
| 134 | |
|---|
| 135 | local del = luci.http.formvalue("del") |
|---|
| 136 | if del then |
|---|
| 137 | local pkg = require("luci.fon.pkg") |
|---|
| 138 | local p = pkg.Plugin(del) |
|---|
| 139 | p:load() |
|---|
| 140 | p:delete() |
|---|
| 141 | end |
|---|
| 142 | |
|---|
| 143 | local fname = luci.http.formvalue("image") |
|---|
| 144 | if fname and type then |
|---|
| 145 | ret = "Installing plugin." |
|---|
| 146 | if type == "tgz" or type == "fon" then |
|---|
| 147 | local verify = require("luci.fon.pkg.verify") |
|---|
| 148 | local str, key, err = verify.fonidentify(tmpfile) |
|---|
| 149 | local uci = require("luci.model.uci").cursor() |
|---|
| 150 | local allow_unsigned = false |
|---|
| 151 | local dev = uci:get("registered", "fonreg", "dev") |
|---|
| 152 | if dev == "1" then |
|---|
| 153 | allow_unsigned = true |
|---|
| 154 | end |
|---|
| 155 | if str ~= "hotfix" and str ~= "plugin" and str ~= "unsigned" then |
|---|
| 156 | ret = "Failed to identify upload." |
|---|
| 157 | else |
|---|
| 158 | local dir, str = verify.fonverify(tmpfile, "/etc/fon/keyring/", allow_unsigned) |
|---|
| 159 | if dir == nil then |
|---|
| 160 | ret = "Failed to verify plugin." |
|---|
| 161 | else |
|---|
| 162 | local res, str = verify.fonupgrade(dir) |
|---|
| 163 | if res == 0 then |
|---|
| 164 | ret = "Installed plugin." |
|---|
| 165 | else |
|---|
| 166 | ret = str |
|---|
| 167 | end |
|---|
| 168 | end |
|---|
| 169 | end |
|---|
| 170 | else |
|---|
| 171 | ret = "No handler for this file" |
|---|
| 172 | end |
|---|
| 173 | end |
|---|
| 174 | |
|---|
| 175 | luci.http.prepare_content("text/html") |
|---|
| 176 | local webadmin = require "luci.tools.webadmin" |
|---|
| 177 | webadmin.foreach_uci("plugfons", "plugin", "fon_plugin/head", "fon_plugin/plugin", "fon_plugin/tail", {ret=ret}) |
|---|
| 178 | end |
|---|