cryptonas-branches/pythonrewrite/bin2/CryptoBoxWebserverSites.py
2006-08-27 08:38:48 +00:00

250 lines
8.7 KiB
Python
Executable file

import CryptoBox
import CryptoBoxWebserverSettings
import CryptoBoxWebserverRender
website = CryptoBoxWebserverRender.CryptoBoxWebserverRender()
# is it necessary to inherit these both classes?
# for clarity they should be just instanciated - or not?
class CryptoBoxWebserverSites(CryptoBox.CryptoBoxProps, CryptoBoxWebserverSettings.CryptoBoxWebserverSettings):
'''
url2func = {'index':'show_status','doc':'show_doc','logs':'show_log'}
'''
def __prepare(self, action="show_status"):
'''handles stuff that all sites need as preparation
The default site to render is 'show_status'.
Self.settings is filled by the following methodcall
thus every rendered site will get actual values from the configfile.
After that the corresponding site-method (e.g. doc) may set individual values.
'''
# RFC: the following line somehow implicitly calls 'setSettings(self, self)'
# should it be that way? [l]
self.setSettings(self)
#self.settings
self.settings["Data.Action"] = action
#TODO: check ssl status
def __sanitize_input(self, evilparams):
'''mistrusts every given parameter and wipes crap out
Every single site method has to call this before even looking
at url-given parameters.
This has to be called manually, since I don't see any other way of
sanitizing input automatically for all sites.
# RFC: why shouldn't it be called in __init__? [l]
'''
niceparams = { 'weblang': tuple(self.__getAvailableLanguages()),
'loglevel': ('','info', 'warn', 'debug', 'error'),
'type': ('reboot', 'poweroff')
}
for evilkey in evilparams.keys():
## if the param isn't in the niceparams list, ignore it
if not niceparams.get(evilkey):
self.log.warn('ignoring "%s"' % evilkey)
# RFC: why "return False"? - just ignoring is sufficient
return False
#TODO: until now only a warning message is printed
## if the param has no such value, set it to a default (the first in the list)
if evilparams.get(evilkey) not in niceparams.get(evilkey):
self.log.warn('"%s" not in weblang %s' % (evilkey, niceparams.get(evilkey)))
# RFC: why "weblang"?
#TODO: set it to: niceparams.get(evilkey)[0]))
return
def __check_config(self):
#TODO
pass
def __check_init_running(self):
#TODO
pass
def __getAvailableLanguages(self):
import re, os
regpat = re.compile(r"^\w+\.hdf$")
try:
lang_dir = self.cbxPrefs["Locations"]["LangDir"]
except KeyError:
self.log.error("Could not find a configuration setting: [Locations]->LangDir - please check the config file")
return []
if not os.path.exists(lang_dir):
self.log.error("The configured language directory (%s) does not exist" % (lang_dir, ))
return []
try:
return [ e[:-4] for e in os.listdir(lang_dir) if regpat.search(e)]
except OSError:
self.log.error("Could not access the language directory (%s)" % (lang_dir,))
return []
def __getAvailableDocLanguages(self):
import re, os
regpat = re.compile(r"^\w+$")
try:
doc_dir = self.cbxPrefs["Locations"]["DocDir"]
except KeyError:
self.log.error("Could not find a configuration setting: [Locations]->DocDir - please check the config file")
return []
if not os.path.exists(doc_dir):
self.log.error("The configured documentation directory (%s) does not exist" % (doc_dir, ))
return []
try:
return [ e for e in os.listdir(doc_dir)
if regpat.search(e) and os.path.isdir(os.path.join(doc_dir, e))]
except OSError:
self.log.error("Could not access the documentations directory (%s)" % (doc_dir,))
return []
######################################################################
## put real sites down here and don't forget to expose them at the end
def logs(self, loglevel=""):
'''displays a HTML version of the logfile
The loglevel has to be set and nothing else, as we just log in english.
RFC: what does this mean? We still have to save the current language - or not?
Be aware not to name this method just "log" as it seems to be a
reserved word.
# RFC: maybe it conflicts with CryptoBoxProps.log - which we inherited?
'''
self.__sanitize_input({"loglevel":loglevel})
self.__prepare("show_log")
self.settings["Data.Log"] = "<br/>".join(self.getLogData(lines=30, maxSize=2000))
return website.render(self)
def status(self, weblang=""):
'''shows the current status of the box
'''
self.__sanitize_input({"weblang":weblang})
self.__prepare("show_status")
if not self.__check_config():
self.settings["Data.Warning"] = "NotInitialized"
self.settings["Data.Action"] = "form_init"
elif self.__check_init_running():
self.settings["Data.Warning"] = "InitNotFinished"
self.settings["Data.Action"] = "empty"
self.settings["Data.Redirect.Action"] = "form_config"
self.settings["Data.Redirect.Delay"] = "30"
else:
self.settings["Data.Action"] = "show_status"
self.settings["Data.Redirect.Delay"] = "60"
return website.render(self)
def config(self,weblang=""):
pass
def doc(self,action="",page="",weblang=""):
'''prints the offline wikipage
TODO: action is unnessessary, remove here and from all html
files in doc/html/[de|en]/*
'''
# RFC: sanitize?
self.__prepare("show_doc")
if page:
self.settings["Data.Doc.Page"] = page
else:
self.settings["Data.Doc.Page"] ="CryptoBoxUser"
if weblang:
if weblang in self.__getAvailableDocLanguages():
self.settings["Settings.DocLang"] = weblang
else:
self.log.warn("invalid documentation language selected: %s", % (weblang, ))
return website.render(self)
def system(self,typeOfShutdown=""):
self.__prepare("form_system")
if typeOfShutdown == "reboot":
self.settings["Data.Success"] = "ReBoot"
self.settings["Data.Redirect.Action"] = "show_status"
self.settings["Data.Redirect.Delay"] = "180"
self.log.info("TODO: call function for system reboot")
elif typeOfShutdown == "poweroff":
self.settings["Data.Success"] = "PowerOff"
self.log.info("TODO: call function for system shutdown")
else:
self.log.warn("someone tried to shutdown the system in a broken way (%s)" % typeOfShutdown)
return website.render(self)
def index(self):
self.__prepare("show_status")
return website.render(self)
'''
## DONE: these functions are pythonized
#################### show_log #######################
##################### doc ############################
##################### poweroff ######################
##################### reboot ########################
## but there are even more TODO
##################### 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 not device:
self.log.debug("invalid device chosen: %s" 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)
else:
self.log.debug("invalid device chosen: %s" 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
'''
############################################################################
## to make the sites visible through the webserver they must be exposed here
index.exposed = True
doc.exposed = True
logs.exposed = True
system.exposed = True
status.exposed = True