cryptonas-branches/pythonrewrite/bin2/CryptoBoxWebserver.py

197 lines
6.6 KiB
Python
Raw Normal View History

2006-08-16 14:52:23 +02:00
#!/usr/bin/env python
import os,sys
import CryptoBox
2006-08-20 18:33:52 +02:00
try:
import cherrypy
except:
print "could not import cherrypy module! Try apt-get install python-cherrypy."
sys.exit(1)
try:
import neo_cgi, neo_util, neo_cs
except:
print "could not import clearsilver modules! Try apt-get install python-clearsilver."
2006-08-20 18:33:52 +02:00
class CryptoBoxSite:
'''this is the motherclass of all cbx sites
2006-08-20 18:33:52 +02:00
put the stuff in her, that every site will need access to'''
2006-08-20 20:23:35 +02:00
def __init__(self):
pass
2006-08-20 18:33:52 +02:00
def print_foo(self):
'''a stupid demo method'''
self.cbx_inheritance_test()
print "fooooooooooooobaaaaaaaaaaaaaaaaaaaar"
2006-08-20 20:23:35 +02:00
#class CryptoBoxSites(CryptoBox.CryptoBoxProps, CryptoBoxSite):
class CryptoBoxSites(CryptoBox.CryptoBox, CryptoBox.CryptoBoxProps, CryptoBoxSite):
def index(self,action="show_status",page="",weblang="",device="", type=""):
#PROPOSAL:
#cherrypy could create nice paths like http://crypto.box/doc/,
#but that would require an update of the links in the templates.
2006-08-20 18:33:52 +02:00
#is the style worth it? - yes :)
2006-08-20 18:33:52 +02:00
self.print_foo() # this method was inherited by CryptoBoxSite, be careful not to overwrite methods unwanted..
#sanitize input
page = self.sanitize_input(page)
action = self.sanitize_input(action)
device = self.sanitize_input(device)
type = self.sanitize_input(type)
#setting settings ;)
settings={}
TemplateDir = self.cbxPrefs["Settings"]["TemplateDir"]
settings["Settings.TemplateDir"] = TemplateDir
settings["Settings.Stylesheet"] = self.cbxPrefs["Settings"]["Stylesheet"]
LangDir = self.cbxPrefs["Settings"]["LangDir"]
settings["Settings.LangDir"] = LangDir
Language = self.cbxPrefs["Settings"]["Language"]
settings["Settings.DocDir"] = self.cbxPrefs["Settings"]["DocDir"]
settings["Settings.DocLang"] = self.cbxPrefs["Settings"]["DocLang"]
# BEWARE: there are two kinds of actions:
# * some require a harddisk
# * some do not require a harddisk
# take care, that you put a new action into the appropriate block below
#TODO: check for ssl, see cryptobox.pl lines 483-9
#--------------------------------------------------------------#
# here you may define all cases that do not require a harddisk #
# put all other cases below the harddisk check #
#--------------------------------------------------------------#
#################### show_log #######################
if action == "show_log":
settings["Data.Action"] = "show_log"
##################### doc ############################
elif action == "doc":
settings["Data.Action"] = "show_doc"
if page != "":
settings["Data.Doc.Page"] = page
else:
settings["Data.Doc.Page"] ="CryptoBoxUser"
##################### poweroff ######################
elif action == "system_ask":
settings["Data.Action"] = "form_system"
##################### reboot ########################
elif action == "shutdown_do":
if type == "reboot":
#TODO: call function for system reboot
pass
settings["Data.Success"] = "ReBoot"
settings["Data.Redirect.Action"] = "show_status"
settings["Data.Redirect.Delay"] = "180"
else:
settings["Data.Success"] = "PowerOff"
settings["Data.Action"] = "empty"
##################### check for a harddisk ##########################
# catch this error, to prevent all following actions from execution #
#####################################################################
elif not True: #TODO: replace True with check for hard disk is_harddisk_available()
settings["Data.Error"] = "NoHardDisk"
#-------------------------------------------------------#
# here you may define all cases that require a harddisk #
#-------------------------------------------------------#
################ umount_do #######################
elif action == "unmount_do":
if device == "":
#TODO: debug message: "invalid device: "+ device
settings["Data.Warning"] = "InvalidDevice"
settings["Data.Action"] = "empty"
elif not True: #TODO: replace True with check_config()
settings["Data.Warning"] = "NotInitialized"
settings["Data.Action"] = "form_init"
elif True: #TODO: replace True with check_init_running()
settings["Data.Warning"] = "InitNotFinished"
settings["Data.Action"] = "empty"
settings["Data.Redirect.Action"] = "form_config"
settings["Data.Redirect.Delay"] = "30"
elif not True: #TODO: replace True with check_mounted(device)
settings["Data.Warning"] = "NotMounted"
settings["Data.Action"] = "show_volume"
else: #unmount
#TODO: replace this line with umount_vol(device)
if True: #TODO: replace True with check_mounted(device)
settings["Data.Warning"] = "UmountFailed"
settings["Data.Action"] = "show_volume"
else:
settings["Data.Action"] = "show_volume"
################ mount_do ########################
elif action == "mount_do":
if device != "":
pass #TODO: is_encrypted = check_device_encryption(device)
if device == "":
#TODO: debug_msg(DEBUG_INFO, "invalid device: " + device)
settings["Data.Warning"] = "InvalidDevice"
settings["Data.Action"] = "empty"
elif not True: #TODO: replace True with check_config()
settings["Data.Warning"] = "NotInitialized"
settings["Data.Action"] = "form_init"
#at cryptobox.pl line 568
#go render stuff
2006-08-20 18:33:52 +02:00
cs = CryptoBoxRenderSite()
2006-08-20 20:23:35 +02:00
self.log.debug("rendering site")
content = cs.render(self.cbxPrefs["Settings"]["TemplateDir"]+"/main.cs",settings, \
self.cbxPrefs["Settings"]["LangDir"]+"/"+Language+".hdf")
return content
def sanitize_input(self,data):
#TODO: do things
return data
2006-08-16 14:52:23 +02:00
index.exposed = True
2006-08-20 18:33:52 +02:00
class CryptoBoxRenderSite:
def render(self,cs_path,settings = {},hdf_path = ""):
"""
render a clearsilver template and return the result.
2006-08-20 20:23:35 +02:00
2006-08-20 18:33:52 +02:00
gets:
- path to clearsilver template
- dictionary with settings (optional)
- path to hdf dataset (optional)
"""
2006-08-20 20:23:35 +02:00
hdf = neo_util.HDF()
2006-08-20 18:33:52 +02:00
if hdf_path != "":
hdf.readFile(hdf_path)
for key in settings.keys():
hdf.setValue(key,str(settings[key]))
2006-08-20 20:23:35 +02:00
cs = neo_cs.CS(hdf)
2006-08-20 18:33:52 +02:00
cs.parseFile(cs_path)
return cs.render()
##############################################################
2006-08-20 18:33:52 +02:00
if __name__ == "__main__":
cherrypy.root = CryptoBoxSites()
#expose static content:
#I currently have no idea how to cleanly extract the stylesheet path from
#the config object without an extra CryptoBox.CryptoBoxProps instance.
#perhaps put config handling into a seperate class in CryptoBox.py?
cherrypy.config.configMap.update(
{
"/cryptobox.css": {
"staticFilter.on" : True,
"staticFilter.file": os.path.abspath("../www-data/cryptobox.css" )
}
})
2006-08-20 18:33:52 +02:00
cherrypy.server.start()
2006-08-16 14:52:23 +02:00