adding SSL Encryption to all WebInterface Ports with Python OpenSSL
[enigma2-plugins.git] / webinterface / src / WebIfConfig.py
1 Version = '$Header$';
2
3 from enigma import  eListboxPythonMultiContent, gFont
4 from Screens.Screen import Screen
5 from Screens.MessageBox import MessageBox
6
7 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigInteger,ConfigYesNo,ConfigText
8 from Components.ConfigList import ConfigListScreen
9 from Components.Label import Label
10 from Components.Button import Button
11 from Components.MenuList import MenuList
12 from Components.MultiContent import MultiContentEntryText
13
14 from Components.ActionMap import ActionMap
15 from Plugins.Extensions.WebInterface import addInterfaceConfig
16
17 class WebIfConfigScreen(ConfigListScreen,Screen):
18     skin = """
19         <screen position="100,100" size="550,400" title="Webinterface: Main Setup" >
20             <widget name="config" position="0,0" size="550,360" scrollbarMode="showOnDemand" />
21             
22             <widget name="key_red" position="0,360" size="140,40" valign="center" halign="center" zPosition="4"  foregroundColor="white" font="Regular;18"/> 
23             <widget name="key_green" position="140,360" size="140,40" valign="center" halign="center" zPosition="4"  foregroundColor="white" font="Regular;18"/> 
24             <widget name="key_yellow" position="280,360" size="140,40" valign="center" halign="center" zPosition="4"  foregroundColor="white" font="Regular;18"/>
25             
26             <ePixmap name="red"    position="0,360"   zPosition="2" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
27             <ePixmap name="green"  position="140,360" zPosition="2" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
28             <ePixmap name="yellow" position="280,360" zPosition="2" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" /> 
29         </screen>"""
30     def __init__(self, session, args = 0):
31         self.session = session
32         Screen.__init__(self, session)
33         self.list = []
34         self.list.append(getConfigListEntry(_("start Webinterface"), config.plugins.Webinterface.enable))
35         self.list.append(getConfigListEntry(_("enable /hdd"), config.plugins.Webinterface.includehdd))
36         self.list.append(getConfigListEntry(_("allow zapping via webif"), config.plugins.Webinterface.allowzapping))
37         self.list.append(getConfigListEntry(_("autowrite timer"), config.plugins.Webinterface.autowritetimer))
38         self.list.append(getConfigListEntry(_("load movie-length"), config.plugins.Webinterface.loadmovielength))
39         
40         for i in range(0, config.plugins.Webinterface.interfacecount.value):
41             c = config.plugins.Webinterface.interfaces[i]
42             
43         ConfigListScreen.__init__(self, self.list)
44         self["key_red"] = Button(_("Cancel"))
45         self["key_green"] = Button(_("Ok"))
46         self["key_yellow"] = Button(_("Interfaces"))
47         self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
48         {
49             "red": self.cancel,
50             "green": self.save,
51             "yellow": self.openIfacesConfig,
52             "save": self.save,
53             "cancel": self.cancel,
54             "ok": self.save,
55         }, -2)
56         
57     def openIfacesConfig(self):
58         print "yellow"
59         self.session.open(WebIfInterfaceListConfigScreen)
60         
61     def save(self):
62         print "saving"
63         for x in self["config"].list:
64             x[1].save()
65         self.close(True,self.session)
66
67     def cancel(self):
68         print "cancel"
69         for x in self["config"].list:
70             x[1].cancel()
71         self.close(False,self.session)
72
73 class WebIfInterfaceListConfigScreen(Screen):
74     skin = """
75         <screen position="100,100" size="550,400" title="Webinterface: list of configured Interfaces" >
76             <widget name="adress" position="5,0" size="150,50"   font="Regular;20" halign="left"/>
77             <widget name="port" position="160,0" size="50,50"   font="Regular;20" halign="left"/>
78             <widget name="auth" position="215,0" size="200,50"   font="Regular;20" halign="left"/>
79             <widget name="disabled" position="420,0" size="130,50"   font="Regular;20" halign="left"/>
80             <widget name="ifacelist" position="0,50" size="550,300"  scrollbarMode="showOnDemand"/>
81             
82             <widget name="key_red" position="0,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
83                 <widget name="key_yellow" position="280,350" size="140,40"  zPosition="5" valign="center" halign="center" backgroundColor="yellow" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
84             
85             <ePixmap name="red"    position="0,350"   zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
86             <ePixmap name="yellow" position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />    
87         </screen>"""
88     def __init__(self, session):
89         
90         Screen.__init__(self, session)
91         self["adress"] = Button(_("Adress"))
92         self["port"] = Button(_("Port"))
93         self["auth"] = Button(_("use Authorization"))
94         self["disabled"] = Button(_("disabled"))
95         self["key_red"] = Button(_("add"))
96         self["key_yellow"] = Button(_("change"))
97         self["ifacelist"] = WebIfInterfaceList([])
98         self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
99             {
100              "ok"       :       self.keyGreen,
101              "back"     :       self.close,
102              "red"      :       self.keyRed,
103              "green"    :       self.keyGreen,
104              "yellow"   :       self.keyYellow,
105              "up"       :       self.up,
106              "down"     :       self.down,
107              "left"     :       self.left,
108              "right"    :       self.right,
109              }, -1)
110         self.updateList()
111         
112     def updateList(self):
113         ifaceguilist = []
114         for i in range(0, config.plugins.Webinterface.interfacecount.value):
115             c= config.plugins.Webinterface.interfaces[i]
116             res = [ i ] #550,400
117             res.append(MultiContentEntryText(pos=(5, 0), size=(150, 25), font=0, text=c.adress.value))
118             res.append(MultiContentEntryText(pos=(160, 0),size=(50, 25), font=0,text=str(c.port.value)))
119             
120             if c.useauth.value:
121                 res.append(MultiContentEntryText(pos=(215, 0),size=(200, 25), font=0,text=_("yes"),color=0x0000FF00))
122             else:
123                 res.append(MultiContentEntryText(pos=(215, 0),size=(200, 25), font=0,text=_("no"),color=0x00FF0000))
124                 
125             if c.disabled.value:
126                 res.append(MultiContentEntryText(pos=(420, 0),size=(130, 25), font=0,text=_("yes"),color=0x0000FF00))
127             else:
128                 res.append(MultiContentEntryText(pos=(420, 0),size=(130, 25), font=0,text=_("no"),color=0x00FF0000))
129             ifaceguilist.append(res)
130         ifaceguilist.sort()
131         self["ifacelist"].l.setList(ifaceguilist)
132
133     
134     def keyRed(self):
135         print "KEYRED"
136         self.session.openWithCallback(self.updateList,WebIfInterfaceConfigScreen,None)
137         
138     def keyGreen(self):
139         print "KEYGREEN"
140         
141     def keyYellow(self):
142         x = self["ifacelist"].getCurrent()[0]
143         print "current list index",x
144         self.session.openWithCallback(self.updateList,WebIfInterfaceConfigScreen,int(x))
145
146     def up(self):
147         self["ifacelist"].up()
148
149     def down(self):
150         self["ifacelist"].down()
151
152     def left(self):
153         self["ifacelist"].pageUp()
154
155     def right(self):
156         self["ifacelist"].pageDown()
157
158
159 class WebIfInterfaceList(MenuList):
160     def __init__(self, list, enableWrapAround = False):
161         MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
162         self.l.setFont(0, gFont("Regular", 20))
163         #self.l.setFont(1, gFont("Regular", 25))
164
165     def postWidgetCreate(self, instance):
166         MenuList.postWidgetCreate(self, instance)
167         instance.setItemHeight(25)
168     
169         
170 class WebIfInterfaceConfigScreen(Screen, ConfigListScreen):
171     skin = """
172         <screen name="Interface Config" position="80,148" size="560,280" title="Webinterface: edit Interface">
173             <widget name="config" position="10,10" size="520,210" scrollbarMode="showOnDemand" />
174             <ePixmap name="red"    position="0,240"   zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
175             <ePixmap name="green"  position="140,240" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />            
176             <ePixmap name="blue"   position="420,240" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
177             
178             <widget name="key_red" position="0,240" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
179             <widget name="key_green" position="140,240" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
180             <widget name="key_blue" position="420,240" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
181         </screen>"""
182     
183     def __init__(self, session, ifacenum):
184         Screen.__init__(self, session)
185         self["actions"] = ActionMap(["SetupActions", "ColorActions"],
186         {
187             "green": self.keySave,
188             "red": self.keyCancel,
189             "blue": self.keyDelete,
190             "cancel": self.keyCancel
191         }, -2)
192
193         self["key_red"] = Button(_("Cancel"))
194         self["key_green"] = Button(_("OK"))
195         #self["key_yellow"] = Button("")
196         self["key_blue"] = Button(_("Delete"))
197
198         if ifacenum is None:
199             i = addInterfaceConfig()
200         else:
201             i = ifacenum
202         cfglist = []
203         try:
204             current = config.plugins.Webinterface.interfaces[i]
205         except IndexError,e:
206             print "[WebIf] iface config %i not found, adding it and setting default values"%i
207             addInterfaceConfig()
208             current = config.plugins.Webinterface.interfaces[ifacenum]
209             
210         cfglist.append(getConfigListEntry(_("disabled"), current.disabled))
211         cfglist.append(getConfigListEntry(_("Adress"), current.adress))
212         cfglist.append(getConfigListEntry(_("Port"), current.port))
213         cfglist.append(getConfigListEntry(_("use Authorization"), current.useauth))
214         cfglist.append(getConfigListEntry(_("use SSL Encryption"), current.usessl))
215         ConfigListScreen.__init__(self, cfglist, session)
216         self.ifacenum = i
217
218     def keySave(self):
219         config.plugins.Webinterface.interfacecount.save()
220         for x in self["config"].list:
221             if isinstance(x[1].value, str):
222                 x[1].value = x[1].value.strip()
223             x[1].save()
224         self.close()
225         config.save()
226
227     def cancelConfirm(self, result):
228         if result:
229             config.plugins.Webinterface.interfacecount.cancel()
230         self.callback = None
231         ConfigListScreen.cancelConfirm(self, result)
232
233     def keyDelete(self):
234         self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this Interface?"))
235
236     def deleteConfirm(self, result):
237         if not result:
238             return
239         del(config.plugins.Webinterface.interfaces[self.ifacenum])
240         config.plugins.Webinterface.interfaces.save()
241         config.plugins.Webinterface.interfacecount.value = config.plugins.Webinterface.interfacecount.value - 1;
242         config.plugins.Webinterface.interfacecount.save()
243         config.save()
244         self.close()
245