a4a07a672a1138e966df7faf35d782cb2c7853d9
[project/luci.git] / modules / admin-mini / luasrc / controller / mini / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 module("luci.controller.mini.system", package.seeall)
17
18 function index()
19         luci.i18n.loadc("admin-core")
20         local i18n = luci.i18n.translate
21
22         entry({"mini", "system"}, alias("mini", "system", "index"), i18n("system"), 40)
23         entry({"mini", "system", "index"}, cbi("mini/system"), i18n("general"), 1)
24         entry({"mini", "system", "passwd"}, call("action_passwd"), i18n("a_s_changepw"), 10)
25         entry({"mini", "system", "upgrade"}, call("action_upgrade"), i18n("a_s_flash"), 90)
26         entry({"mini", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 100)
27 end
28
29 function action_reboot()
30         local reboot = luci.http.formvalue("reboot")
31         luci.template.render("mini/reboot", {reboot=reboot})
32         if reboot then
33                 luci.sys.reboot()
34         end
35 end
36
37 function action_upgrade()
38         require("luci.model.uci")
39
40         local ret  = nil
41         local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
42         local tmpfile = "/tmp/firmware.img"
43
44         local file
45         luci.http.setfilehandler(
46                 function(meta, chunk, eof)
47                         if not file then
48                                 file = io.open(tmpfile, "w")
49                         end
50                         if chunk then
51                                 file:write(chunk)
52                         end
53                         if eof then
54                                 file:close()
55                         end
56                 end
57         )
58
59         local fname   = luci.http.formvalue("image")
60         local keepcfg = luci.http.formvalue("keepcfg")
61
62         if plat and fname then
63                 local kpattern = nil
64                 if keepcfg then
65                         local files = luci.model.uci.get_all("luci", "flash_keep")
66                         if files.luci and files.luci.flash_keep then
67                                 kpattern = ""
68                                 for k,v in pairs(files.luci.flash_keep) do
69                                         kpattern = kpattern .. " " ..  v
70                                 end
71                         end
72                 end
73                 ret = luci.sys.flash(tmpfile, kpattern)
74         end
75
76         luci.template.render("mini/upgrade", {sysupgrade=plat, ret=ret})
77 end
78
79 function action_passwd()
80         local p1 = luci.http.formvalue("pwd1")
81         local p2 = luci.http.formvalue("pwd2")
82         local stat = nil
83
84         if p1 or p2 then
85                 if p1 == p2 then
86                         stat = luci.sys.user.setpasswd("root", p1)
87                 else
88                         stat = 10
89                 end
90         end
91
92         luci.template.render("mini/passwd", {stat=stat})
93 end