1 -- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed under the Apache License, Version 2.0
4 module("luci.controller.privoxy", package.seeall)
6 local NX = require "nixio"
7 local NXFS = require "nixio.fs"
8 local HTTP = require "luci.http"
9 local UCI = require "luci.model.uci"
10 local SYS = require "luci.sys"
12 PRIVOXY_MIN = "3.0.22-0" -- minimum version of service required
15 local _sys = require "luci.sys"
16 local _verinst = _sys.exec([[opkg list-installed ]] .. "privoxy" .. [[ | cut -d " " -f 3 ]])
17 local _cmd = [[opkg compare-versions "]] .. _verinst .. [[" ">=" "]] .. "3.0.22-0" .. [["]]
18 local _verok = tonumber(_sys.call(_cmd))
20 -- check config file and version
21 if not nixio.fs.access("/etc/config/privoxy") or (_verok == 0) then
22 entry( {"admin", "services", "privoxy"}, cbi("privoxy/apperror",
23 {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true }), _("Privoxy WEB proxy"), 59)
25 entry( {"admin", "services", "privoxy"}, cbi("privoxy/detail"), _("Privoxy WEB proxy"), 59)
26 entry( {"admin", "services", "privoxy", "logview"}, call("logread") ).leaf = true
27 entry( {"admin", "services", "privoxy", "startstop"}, call("startstop") ).leaf = true
28 entry( {"admin", "services", "privoxy", "status"}, call("get_pid") ).leaf = true
32 -- called by XHR.get from detail_logview.htm
34 -- read application settings
35 local uci = UCI.cursor()
36 local logdir = uci:get("privoxy", "privoxy", "logdir") or "/var/log"
37 local logfile = uci:get("privoxy", "privoxy", "logfile") or "privoxy.log"
40 local lfile=logdir .. "/" .. logfile
41 local ldata=NXFS.readfile(lfile)
42 if not ldata or #ldata == 0 then
48 -- called by XHR.get from detail_startstop.htm
50 local pid = get_pid(true)
52 SYS.call("/etc/init.d/privoxy stop")
53 NX.nanosleep(1) -- sleep a second
54 if NX.kill(pid, 0) then -- still running
55 NX.kill(pid, 9) -- send SIGKILL
59 SYS.call("/etc/init.d/privoxy start")
60 NX.nanosleep(1) -- sleep a second
61 pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
62 if pid > 0 and not NX.kill(pid, 0) then
63 pid = 0 -- process did not start
66 HTTP.write(tostring(pid)) -- HTTP needs string not number
69 -- called by XHR.poll from detail_startstop.htm
70 -- and from lua (with parameter "true")
71 function get_pid(from_lua)
72 local pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
73 if pid > 0 and not NX.kill(pid, 0) then
79 HTTP.write(tostring(pid)) -- HTTP needs string not number
83 -- get the "name" of the current active theme
85 local _uci = UCI.cursor()
86 local _base = _uci:get("luci", "main", "mediaurlbase") -- only pathname
89 for k, v in pairs(luci.config.themes) do
90 if k:sub(1, 1) ~= "." and v == _base then
97 -- replacement of build-in Flag.parse of cbi.lua
98 -- modified to mark section as changed if value changes
99 -- current parse did not do this, but it is done AbstaractValue.parse()
100 function flag_parse(self, section)
101 local fexists = self.map:formvalue(
102 luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
105 local fvalue = self:formvalue(section) and self.enabled or self.disabled
106 local cvalue = self:cfgvalue(section)
107 if fvalue ~= self.default or (not self.optional and not self.rmempty) then
108 self:write(section, fvalue)
112 if (fvalue ~= cvalue) then self.section.changed = true end
115 self.section.changed = true