5d3150aa2abebc38d2d2e2d17374d85e8b02bb49
[project/luci.git] / src / ffluci / controller / admin / system.lua
1 module("ffluci.controller.admin.system", package.seeall)
2
3 require("ffluci.sys")
4 require("ffluci.http")
5 require("ffluci.util")
6 require("ffluci.fs")
7 require("ffluci.model.ipkg")
8
9 function action_editor()
10 local file = ffluci.http.formvalue("file", "")
11 local data = ffluci.http.formvalue("data")
12 local err = nil
13 local msg = nil
14 local stat = true
15
16 if file and data then
17 stat, err = ffluci.fs.writefile(file, data)
18 end
19
20 if not stat then
21 err = ffluci.util.split(err, " ")
22 table.remove(err, 1)
23 msg = table.concat(err, " ")
24 end
25
26 local cnt, err = ffluci.fs.readfile(file)
27 if cnt then
28 cnt = ffluci.util.pcdata(cnt)
29 end
30 ffluci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg})
31 end
32
33 function action_ipkg()
34 local file = "/etc/ipkg.conf"
35 local data = ffluci.http.formvalue("data")
36 local stat = nil
37 local err = nil
38
39 if data then
40 stat, err = ffluci.fs.writefile(file, data)
41 end
42
43 local cnt = ffluci.fs.readfile(file)
44 if cnt then
45 cnt = ffluci.util.pcdata(cnt)
46 end
47
48 ffluci.template.render("admin_system/ipkg", {cnt=cnt, msg=err})
49 end
50
51 function action_packages()
52 local ipkg = ffluci.model.ipkg
53 local void = nil
54 local submit = ffluci.http.formvalue("submit")
55
56
57 -- Search query
58 local query = ffluci.http.formvalue("query")
59 query = (query ~= '') and query or nil
60
61
62 -- Packets to be installed
63 local install = ffluci.http.formvalue("install")
64 install = (type(install) == "table" and submit) and install or nil
65
66 -- Install from URL
67 local url = ffluci.http.formvalue("url")
68 if url and url ~= '' and submit then
69 if not install then
70 install = {}
71 end
72 install[url] = 1
73 end
74
75 -- Do install
76 if install then
77 for k, v in pairs(install) do
78 void, install[k] = ipkg.install(k)
79 end
80 end
81
82
83 -- Remove packets
84 local remove = ffluci.http.formvalue("remove")
85 remove = (type(remove) == "table" and submit) and remove or nil
86 if remove then
87 for k, v in pairs(remove) do
88 void, remove[k] = ipkg.remove(k)
89 end
90 end
91
92
93 -- Update all packets
94 local update = ffluci.http.formvalue("update")
95 if update then
96 void, update = ipkg.update()
97 end
98
99
100 -- Upgrade all packets
101 local upgrade = ffluci.http.formvalue("upgrade")
102 if upgrade then
103 void, upgrade = ipkg.upgrade()
104 end
105
106
107 -- Package info
108 local info = ffluci.model.ipkg.info(query)
109 local pkgs = {}
110
111 -- Sort after status and name
112 for k, v in pairs(info) do
113 local x = 0
114 for i, j in pairs(pkgs) do
115 local vins = (v.Status and v.Status.installed)
116 local jins = (j.Status and j.Status.installed)
117 if vins ~= jins then
118 if vins then
119 break
120 end
121 else
122 if j.Package > v.Package then
123 break
124 end
125 end
126 x = i
127 end
128 table.insert(pkgs, x+1, v)
129 end
130
131 ffluci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
132 install=install, remove=remove, update=update, upgrade=upgrade})
133 end
134
135 function action_passwd()
136 local p1 = ffluci.http.formvalue("pwd1")
137 local p2 = ffluci.http.formvalue("pwd2")
138 local stat = nil
139
140 if p1 or p2 then
141 if p1 == p2 then
142 stat = ffluci.sys.user.setpasswd("root", p1)
143 else
144 stat = 10
145 end
146 end
147
148 ffluci.template.render("admin_system/passwd", {stat=stat})
149 end
150
151 function action_reboot()
152 local reboot = ffluci.http.formvalue("reboot")
153 ffluci.template.render("admin_system/reboot", {reboot=reboot})
154 if reboot then
155 ffluci.sys.reboot()
156 end
157 end
158
159 function action_sshkeys()
160 local file = "/etc/dropbear/authorized_keys"
161 local data = ffluci.http.formvalue("data")
162 local stat = nil
163 local err = nil
164
165 if data then
166 stat, err = ffluci.fs.writefile(file, data)
167 end
168
169 local cnt = ffluci.fs.readfile(file)
170 if cnt then
171 cnt = ffluci.util.pcdata(cnt)
172 end
173
174 ffluci.template.render("admin_system/sshkeys", {cnt=cnt, msg=err})
175 end