Merge pull request #776 from cshore/pull-request-statistics-nut
[project/luci.git] / applications / luci-app-privoxy / luasrc / controller / privoxy.lua
1 -- Copyright 2014-2016 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed under the Apache License, Version 2.0
3
4 module("luci.controller.privoxy", package.seeall)
5
6 local NX   = require "nixio"
7 local NXFS = require "nixio.fs"
8 local DISP = require "luci.dispatcher"
9 local HTTP = require "luci.http"
10 local I18N = require "luci.i18n"        -- not globally avalible here
11 local IPKG = require "luci.model.ipkg"
12 local UCI  = require "luci.model.uci"
13 local UTIL = require "luci.util"
14 local SYS  = require "luci.sys"
15
16 local srv_name    = "privoxy"
17 local srv_ver_min = "3.0.23"                    -- minimum version of service required
18 local srv_ver_cmd = [[/usr/sbin/privoxy --version | awk {'print $3'}]]
19 local app_name    = "luci-app-privoxy"
20 local app_title   = "Privoxy WEB proxy"
21 local app_version = "1.0.6-1"
22
23 function index()
24         entry( {"admin", "services", "privoxy"}, cbi("privoxy"), _("Privoxy WEB proxy"), 59)
25         entry( {"admin", "services", "privoxy", "logview"}, call("logread") ).leaf = true
26         entry( {"admin", "services", "privoxy", "startstop"}, post("startstop") ).leaf = true
27         entry( {"admin", "services", "privoxy", "status"}, call("get_pid") ).leaf = true
28 end
29
30 -- Application specific information functions
31 function app_description()
32         return  I18N.translate("Privoxy is a non-caching web proxy with advanced filtering "
33                         .. "capabilities for enhancing privacy, modifying web page data and HTTP headers, "
34                         .. "controlling access, and removing ads and other obnoxious Internet junk.")
35                 .. [[<br /><strong>]]
36                 .. I18N.translate("For help use link at the relevant option")
37                 .. [[</strong>]]
38 end
39
40 -- Standardized application/service functions
41 function app_title_main()
42         return  [[<a href="javascript:alert(']]
43                         .. I18N.translate("Version Information")
44                         .. [[\n\n]] .. app_name
45                         .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]] .. app_version
46                         .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("required") .. [[:]]
47                         .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
48                                 .. srv_ver_min .. [[ ]] .. I18N.translate("or higher")
49                         .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("installed") .. [[:]]
50                         .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
51                                 .. (service_version() or I18N.translate("NOT installed"))
52                         .. [[\n\n]]
53                 .. [[')">]]
54                 .. I18N.translate(app_title)
55                 .. [[</a>]]
56 end
57 function service_version()
58         local ver = nil
59         IPKG.list_installed(srv_name, function(n, v, d)
60                         if v and (#v > 0) then ver = v end
61                 end
62         )
63         if not ver or (#ver == 0) then
64                 ver = UTIL.exec(srv_ver_cmd)
65                 if #ver == 0 then ver = nil end
66         end
67         return  ver
68 end
69 function service_ok()
70         return  IPKG.compare_versions((service_version() or "0"), ">=", srv_ver_min)
71 end
72 function service_update()
73         local url = DISP.build_url("admin", "system", "packages")
74         if not service_version() then
75                 return  [[<h3><strong><br /><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;]]
76                         .. I18N.translate("Software package '%s' is not installed." % srv_name)
77                         .. [[</font><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;]]
78                         .. I18N.translate("required") .. [[: ]] .. srv_name .. [[ ]] .. srv_ver_min .. " " .. I18N.translate("or higher")
79                         .. [[<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;]]
80                         .. [[<a href="]] .. url ..[[">]]
81                         .. I18N.translate("Please install current version !")
82                         .. [[</a><br />&nbsp;</strong></h3>]]
83         else
84                 return  [[<h3><strong><br /><br /><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;]]
85                         .. I18N.translate("Software package '%s' is outdated." % srv_name)
86                         .. [[</font><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;]]
87                         .. I18N.translate("installed") .. ": " .. service_version()
88                         .. [[<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;]]
89                         .. I18N.translate("required") .. ": " .. srv_ver_min .. " " .. I18N.translate("or higher")
90                         .. [[<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;]]
91                         .. [[<a href="]] .. url ..[[">]]
92                         .. I18N.translate("Please update to the current version!")
93                         .. [[</a><br /><br />&nbsp;</strong></h3>]]
94         end
95 end
96
97 -- called by XHR.get from detail_logview.htm
98 function logread()
99         -- read application settings
100         local uci     = UCI.cursor()
101         local logdir  = uci:get("privoxy", "privoxy", "logdir") or "/var/log"
102         local logfile = uci:get("privoxy", "privoxy", "logfile") or "privoxy.log"
103         uci:unload("privoxy")
104
105         local ldata=NXFS.readfile(logdir .. "/" .. logfile)
106         if not ldata or #ldata == 0 then
107                 ldata="_nodata_"
108         end
109         HTTP.write(ldata)
110 end
111
112 -- called by XHR.get from detail_startstop.htm
113 function startstop()
114         local pid = get_pid(true)
115         if pid > 0 then
116                 SYS.call("/etc/init.d/privoxy stop")
117                 NX.nanosleep(1)         -- sleep a second
118                 if NX.kill(pid, 0) then -- still running
119                         NX.kill(pid, 9) -- send SIGKILL
120                 end
121                 pid = 0
122         else
123                 SYS.call("/etc/init.d/privoxy start")
124                 NX.nanosleep(1)         -- sleep a second
125                 pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
126                 if pid > 0 and not NX.kill(pid, 0) then
127                         pid = 0         -- process did not start
128                 end
129         end
130         HTTP.write(tostring(pid))       -- HTTP needs string not number
131 end
132
133 -- called by XHR.poll from detail_startstop.htm
134 -- and from lua (with parameter "true")
135 function get_pid(from_lua)
136         local pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
137         if pid > 0 and not NX.kill(pid, 0) then
138                 pid = 0
139         end
140         if from_lua then
141                 return pid
142         else
143                 HTTP.write(tostring(pid))       -- HTTP needs string not number
144         end
145 end
146
147 -- replacement of build-in parse of UCI option
148 -- modified AbstractValue.parse(self, section, novld) from cbi.lua
149 -- validate is called if rmempty/optional true or false
150 -- write is called if rmempty/optional true or false
151 function value_parse(self, section, novld)
152         local fvalue = self:formvalue(section)
153         local fexist = ( fvalue and (#fvalue > 0) )     -- not "nil" and "not empty"
154         local cvalue = self:cfgvalue(section)
155         local rm_opt = ( self.rmempty or self.optional )
156         local eq_cfg                                    -- flag: equal cfgvalue
157
158         -- If favlue and cvalue are both tables and have the same content
159         -- make them identical
160         if type(fvalue) == "table" and type(cvalue) == "table" then
161                 eq_cfg = (#fvalue == #cvalue)
162                 if eq_cfg then
163                         for i=1, #fvalue do
164                                 if cvalue[i] ~= fvalue[i] then
165                                         eq_cfg = false
166                                 end
167                         end
168                 end
169                 if eq_cfg then
170                         fvalue = cvalue
171                 end
172         end
173
174         -- removed parameter "section" from function call because used/accepted nowhere
175         -- also removed call to function "transfer"
176         local vvalue, errtxt = self:validate(fvalue)
177
178         -- error handling; validate return "nil"
179         if not vvalue then
180                 if novld then           -- and "novld" set
181                         return          -- then exit without raising an error
182                 end
183
184                 if fexist then          -- and there is a formvalue
185                         self:add_error(section, "invalid", errtxt or self.title .. ": invalid")
186                         return          -- so data are invalid
187                 elseif not rm_opt then  -- and empty formvalue but NOT (rmempty or optional) set
188                         self:add_error(section, "missing", errtxt or self.title .. ": missing")
189                         return          -- so data is missing
190                 elseif errtxt then
191                         self:add_error(section, "invalid", errtxt)
192                         return
193                 end
194 --              error  ("\n option: " .. self.option ..
195 --                      "\n fvalue: " .. tostring(fvalue) ..
196 --                      "\n fexist: " .. tostring(fexist) ..
197 --                      "\n cvalue: " .. tostring(cvalue) ..
198 --                      "\n vvalue: " .. tostring(vvalue) ..
199 --                      "\n vexist: " .. tostring(vexist) ..
200 --                      "\n rm_opt: " .. tostring(rm_opt) ..
201 --                      "\n eq_cfg: " .. tostring(eq_cfg) ..
202 --                      "\n eq_def: " .. tostring(eq_def) ..
203 --                      "\n novld : " .. tostring(novld) ..
204 --                      "\n errtxt: " .. tostring(errtxt) )
205         end
206
207         -- lets continue with value returned from validate
208         eq_cfg  = ( vvalue == cvalue )                                  -- update equal_config flag
209         local vexist = ( vvalue and (#vvalue > 0) ) and true or false   -- not "nil" and "not empty"
210         local eq_def = ( vvalue == self.default )                       -- equal_default flag
211
212         -- (rmempty or optional) and (no data or equal_default)
213         if rm_opt and (not vexist or eq_def) then
214                 if self:remove(section) then            -- remove data from UCI
215                         self.section.changed = true     -- and push events
216                 end
217                 return
218         end
219
220         -- not forcewrite and no changes, so nothing to write
221         if not self.forcewrite and eq_cfg then
222                 return
223         end
224
225         -- we should have a valid value here
226         assert (vvalue, "\n option: " .. self.option ..
227                         "\n fvalue: " .. tostring(fvalue) ..
228                         "\n fexist: " .. tostring(fexist) ..
229                         "\n cvalue: " .. tostring(cvalue) ..
230                         "\n vvalue: " .. tostring(vvalue) ..
231                         "\n vexist: " .. tostring(vexist) ..
232                         "\n rm_opt: " .. tostring(rm_opt) ..
233                         "\n eq_cfg: " .. tostring(eq_cfg) ..
234                         "\n eq_def: " .. tostring(eq_def) ..
235                         "\n errtxt: " .. tostring(errtxt) )
236
237         -- write data to UCI; raise event only on changes
238         if self:write(section, vvalue) and not eq_cfg then
239                 self.section.changed = true
240         end
241 end