trunk 20080912
[gitenigma.git] / src / enigma_dyn_conf.cpp
blob86dc17bbddd1e895ac55db816681586f5c4cc5db
1 /*
2 * $Id: enigma_dyn_conf.cpp,v 1.23 2008/01/30 19:01:42 dbluelle Exp $
4 * (C) 2005 by digi_casi <digi_casi@tuxbox.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifdef ENABLE_EXPERT_WEBIF
24 #include <map>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <dirent.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #include <net/if.h>
36 #include <arpa/inet.h>
37 #include <linux/if_ether.h>
38 #include <iomanip>
39 #include <iostream>
40 #include <fstream>
42 #include <enigma.h>
43 #include <enigma_main.h>
44 #include <enigma_standby.h>
45 #include <timer.h>
46 #include <lib/driver/eavswitch.h>
47 #include <lib/dvb/dvb.h>
48 #include <lib/dvb/edvb.h>
49 #include <lib/dvb/epgcache.h>
50 #include <lib/dvb/servicestructure.h>
51 #include <lib/dvb/decoder.h>
52 #include <lib/dvb/dvbservice.h>
53 #include <lib/dvb/service.h>
54 #include <lib/dvb/record.h>
55 #include <lib/dvb/serviceplaylist.h>
57 #include <lib/system/info.h>
58 #include <lib/system/http_dyn.h>
59 #include <lib/system/econfig.h>
60 #include <enigma_dyn.h>
61 #include <enigma_dyn_utils.h>
62 #include <enigma_dyn_conf.h>
63 #include <configfile.h>
65 using namespace std;
67 eString getConfigSwapFile(void)
69 eString result;
70 eString th1, th2, th3, th4, th5;
71 eString td1, td2, td3, td4, td5;
73 int swapfile = 0;
74 eString procswaps = readFile("/proc/swaps");
75 if (procswaps)
77 result = readFile(TEMPLATE_DIR + "configSwapFile.tmp");
78 std::stringstream tmp;
79 tmp.str(procswaps);
80 tmp >> th1 >> th2 >> th3 >> th4 >> th5 >> td1 >> td2 >> td3 >> td4 >> td5;
82 result.strReplace("#TH1#", th1);
83 result.strReplace("#TH2#", th2);
84 result.strReplace("#TH3#", th3);
85 result.strReplace("#TH4#", th4);
86 result.strReplace("#TH5#", th5);
87 result.strReplace("#TD1#", td1);
88 result.strReplace("#TD2#", td2);
89 result.strReplace("#TD3#", td3);
90 result.strReplace("#TD4#", td4);
91 result.strReplace("#TD5#", td5);
93 else
94 result = "No swap file active.";
96 result += readFile(TEMPLATE_DIR + "configSwapFileMenu.tmp");
97 eConfig::getInstance()->getKey("/extras/swapfile", swapfile);
98 char *swapfilename = 0;
99 eConfig::getInstance()->getKey("/extras/swapfilename", swapfilename);
100 result.strReplace("#SWAP#", (swapfile == 1) ? "checked" : "");
101 eString rpl;
102 if (swapfilename)
104 rpl = swapfilename;
105 free(swapfilename);
107 result.strReplace("#SWAPFILE#", rpl);
108 result.strReplace("#SWAPFILEBUTTON#", button(100, "Configure", NOCOLOR, "javascript:configSwapFile()", "#000000"));
110 return result;
113 void deactivateSwapFile(eString swapFile)
115 eString cmd;
116 cmd = "swapoff " + swapFile;
117 system(cmd.c_str());
120 void activateSwapFile(eString swapFile)
122 eString procswaps = readFile("/proc/swaps");
123 eString th1, th2, th3, th4, th5;
124 eString td1, td2, td3, td4, td5;
125 std::stringstream tmp;
126 tmp.str(procswaps);
127 tmp >> th1 >> th2 >> th3 >> th4 >> th5 >> td1 >> td2 >> td3 >> td4 >> td5;
128 if ((td1 != "") && (td1 != swapFile))
129 deactivateSwapFile(td1);
130 if ((td1 == "") || (td1 != swapFile))
132 eString cmd = "mkswap " + swapFile;
133 system(cmd.c_str());
134 cmd = "swapon " + swapFile;
135 system(cmd.c_str());
137 system("echo 0 > /proc/sys/vm/swappiness");
140 void setSwapFile(int nextswapfile, eString nextswapfilename)
142 eConfig::getInstance()->setKey("/extras/swapfile", nextswapfile);
143 if (nextswapfile == 1)
145 eConfig::getInstance()->setKey("/extras/swapfilename", nextswapfilename.c_str());
146 activateSwapFile(nextswapfilename);
148 else
149 deactivateSwapFile(nextswapfilename);
152 eString setConfigSwapFile(eString request, eString dirpath, eString opts, eHTTPConnection *content)
154 std::map<eString, eString> opt = getRequestOptions(opts, '&');
155 eString swap = opt["swap"];
156 eString swapFile = opt["swapfile"];
158 if (swap == "on")
160 if (access(swapFile.c_str(), W_OK) != 0)
161 system(eString("dd if=/dev/zero of=" + swapFile + " bs=1024 count=32768").c_str());
164 setSwapFile((swap == "on") ? 1 : 0, swapFile);
166 return closeWindow(content, "", 500);
169 void initHDDparms(void)
171 #ifndef DISABLE_FILE
172 eString cmd;
173 int ti = 0, ac = 0;
175 eConfig::getInstance()->getKey("/extras/hdparm-s", ti);
176 eConfig::getInstance()->getKey("/extras/hdparm-m", ac);
177 if (ti)
179 cmd.sprintf("hdparm -S %d /dev/ide/host0/bus0/target0/lun0/disc", ti);
180 system(cmd.c_str());
182 if (ac)
184 cmd.sprintf("hdparm -M %d /dev/ide/host0/bus0/target0/lun0/disc", ac);
185 system(cmd.c_str());
187 #endif
190 eString getConfigSettings(void)
192 eString result = readFile(TEMPLATE_DIR + "configSettings.tmp");
193 int fastshutdown = 0;
194 eConfig::getInstance()->getKey("/extras/fastshutdown", fastshutdown);
195 int showSatPos = 1;
196 eConfig::getInstance()->getKey("/extras/showSatPos", showSatPos);
197 result.strReplace("#SHOWSATPOS#", (showSatPos == 1) ? "checked" : "");
198 int timeroffset = 0;
199 eConfig::getInstance()->getKey("/enigma/timeroffset", timeroffset);
200 result.strReplace("#TIMEROFFSET#", eString().sprintf("%d", timeroffset));
201 int maxmtu = 1500;
202 eConfig::getInstance()->getKey("/elitedvb/network/maxmtu", maxmtu);
203 result.strReplace("#MAXMTU#", eString().sprintf("%d", maxmtu));
204 int samba = 1;
205 eConfig::getInstance()->getKey("/elitedvb/network/samba", samba);
206 result.strReplace("#SAMBA#", (samba == 1) ? "checked" : "");
207 int webLock = 1;
208 eConfig::getInstance()->getKey("/ezap/webif/lockWebIf", webLock);
209 result.strReplace("#WEBIFLOCK#", (webLock == 1) ? "checked" : "");
210 int hddti = 24;
211 eConfig::getInstance()->getKey("/extras/hdparm-s", hddti);
212 result.strReplace("#HDDSTANDBY#", eString().sprintf("%d", hddti / 12));
213 int hddac = 160;
214 eConfig::getInstance()->getKey("/extras/hdparm-m", hddac);
215 result.strReplace("#HDDACOUSTICS#", eString().sprintf("%d", hddac));
216 char *audiochannelspriority=0;
217 eConfig::getInstance()->getKey("/extras/audiochannelspriority", audiochannelspriority);
218 eString rpl="";
219 if ( audiochannelspriority )
221 rpl = audiochannelspriority;
222 free(audiochannelspriority);
224 result.strReplace("#AUDIOCHANNELSPRIORITY#", rpl);
225 char *trustedhosts=NULL;
226 eConfig::getInstance()->getKey("/ezap/webif/trustedhosts", trustedhosts);
227 rpl = "";
228 if ( trustedhosts )
230 rpl = trustedhosts;
231 free(trustedhosts);
233 result.strReplace("#TRUSTEDHOSTS#", rpl);
234 char *epgcachepath=NULL;
235 eConfig::getInstance()->getKey("/extras/epgcachepath", epgcachepath);
236 rpl = "/hdd";
237 if ( epgcachepath )
239 rpl = epgcachepath;
240 free(epgcachepath);
242 result.strReplace("#EPGCACHEPATH#", rpl);
243 return result;
246 eString setConfigSettings(eString request, eString dirpath, eString opts, eHTTPConnection *content)
248 std::map<eString, eString> opt = getRequestOptions(opts, '&');
249 eString maxmtu = opt["maxmtu"];
250 eString samba = opt["samba"];
251 eString hddti = opt["hddstandby"];
252 eString hddac = opt["hddacoustics"];
253 eString timeroffset = opt["timeroffset"];
254 eString showsatpos = opt["showsatpos"];
255 eString webiflock = opt["webiflock"];
256 eString audiochannelspriority = opt["audiochannelspriority"];
257 eString trustedhosts = opt["trustedhosts"];
258 eString epgcachepath = opt["epgcachepath"];
259 eConfig::getInstance()->setKey("/ezap/webif/trustedhosts", trustedhosts.c_str());
260 eConfig::getInstance()->setKey("/extras/epgcachepath", epgcachepath.c_str());
262 int oldti = 0;
263 eConfig::getInstance()->getKey("/extras/hdparm-s", oldti);
264 int oldac = 0;
265 eConfig::getInstance()->getKey("/extras/hdparm-m", oldac);
267 eConfig::getInstance()->setKey("/elitedvb/network/samba", (samba == "on" ? 1 : 0));
269 int webLock1 = 0;
270 eConfig::getInstance()->getKey("/ezap/webif/lockWebIf", webLock1);
271 eConfig::getInstance()->setKey("/ezap/webif/lockWebIf", (webiflock == "on" ? 1 : 0));
272 int webLock2 = 0;
273 eConfig::getInstance()->getKey("/ezap/webif/lockWebIf", webLock2);
274 if (webLock1 != webLock2)
275 eZap::getInstance()->reconfigureHTTPServer();
276 eConfig::getInstance()->setKey("/extras/showSatPos", (showsatpos == "on" ? 1 : 0));
277 eConfig::getInstance()->setKey("/enigma/timeroffset", atoi(timeroffset.c_str()));
278 eConfig::getInstance()->setKey("/elitedvb/network/maxmtu", atoi(maxmtu.c_str()));
279 system(eString("/sbin/ifconfig eth0 mtu " + maxmtu).c_str());
280 if ((atoi(hddti.c_str()) * 12) != oldti)
281 eConfig::getInstance()->setKey("/extras/hdparm-s", atoi(hddti.c_str()) * 12);
282 if (atoi(hddac.c_str()) != oldac)
283 eConfig::getInstance()->setKey("/extras/hdparm-m", atoi(hddac.c_str()));
284 initHDDparms();
285 eConfig::getInstance()->setKey("/extras/audiochannelspriority", audiochannelspriority.c_str());
287 return closeWindow(content, "", 500);
290 void ezapConfInitializeDyn(eHTTPDynPathResolver *dyn_resolver, bool lockWeb)
292 dyn_resolver->addDyn("GET", "/cgi-bin/setConfigSwapFile", setConfigSwapFile, lockWeb);
293 dyn_resolver->addDyn("GET", "/cgi-bin/setConfigSettings", setConfigSettings, lockWeb);
295 #endif