fc31ea22b598ba90d118229110e3fb14a914d41d
[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         info = info or {}
110         local pkgs = {}
111         
112         -- Sort after status and name
113         for k, v in pairs(info) do
114                 local x = 0
115                 for i, j in pairs(pkgs) do
116                         local vins = (v.Status and v.Status.installed)
117                         local jins = (j.Status and j.Status.installed)
118                         if vins ~= jins then
119                                 if vins then
120                                         break
121                                 end
122                         else
123                                 if j.Package > v.Package then
124                                         break
125                                 end
126                         end
127                         x = i
128                 end
129                 table.insert(pkgs, x+1, v)
130         end 
131         
132         ffluci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
133          install=install, remove=remove, update=update, upgrade=upgrade})       
134 end
135
136 function action_passwd()
137         local p1 = ffluci.http.formvalue("pwd1")
138         local p2 = ffluci.http.formvalue("pwd2")
139         local stat = nil
140         
141         if p1 or p2 then
142                 if p1 == p2 then
143                         stat = ffluci.sys.user.setpasswd("root", p1)
144                 else
145                         stat = 10
146                 end
147         end
148         
149         ffluci.template.render("admin_system/passwd", {stat=stat})
150 end
151
152 function action_reboot()
153         local reboot = ffluci.http.formvalue("reboot")
154         ffluci.template.render("admin_system/reboot", {reboot=reboot})
155         if reboot then
156                 ffluci.sys.reboot()
157         end
158 end
159
160 function action_sshkeys()
161         local file = "/etc/dropbear/authorized_keys"
162         local data = ffluci.http.formvalue("data")
163         local stat = nil
164         local err  = nil
165         
166         if data then
167                 stat, err = ffluci.fs.writefile(file, data)
168         end     
169         
170         local cnt  = ffluci.fs.readfile(file)   
171         if cnt then
172                 cnt = ffluci.util.pcdata(cnt)
173         end
174         
175         ffluci.template.render("admin_system/sshkeys", {cnt=cnt, msg=err})      
176 end