2006-09-07 13:21:56 +02:00
|
|
|
import os
|
|
|
|
import CryptoBoxContainer
|
2006-11-02 14:55:31 +01:00
|
|
|
import CryptoBoxTools
|
2006-09-07 13:21:56 +02:00
|
|
|
|
2006-09-12 10:55:20 +02:00
|
|
|
## useful constant for some functions
|
2006-09-07 13:21:56 +02:00
|
|
|
CONT_TYPES = CryptoBoxContainer.CryptoBoxContainer.Types
|
|
|
|
|
|
|
|
class WebInterfaceDataset(dict):
|
|
|
|
"""this class contains all data that should be available for the clearsilver
|
|
|
|
templates
|
|
|
|
"""
|
|
|
|
|
2006-09-25 14:22:41 +02:00
|
|
|
def __init__(self, cbox, prefs, plugins):
|
2006-09-07 13:21:56 +02:00
|
|
|
self.prefs = prefs
|
|
|
|
self.cbox = cbox
|
|
|
|
self.__setConfigValues()
|
2006-10-11 17:51:28 +02:00
|
|
|
self.plugins = plugins
|
2006-11-06 07:16:27 +01:00
|
|
|
self.setCryptoBoxState()
|
2006-10-11 17:51:28 +02:00
|
|
|
self.setPluginData()
|
2006-11-06 07:16:27 +01:00
|
|
|
self.setContainersState()
|
2006-10-11 17:51:28 +02:00
|
|
|
|
|
|
|
|
2006-11-06 07:16:27 +01:00
|
|
|
def setCryptoBoxState(self):
|
|
|
|
import cherrypy
|
|
|
|
self["Data.Version"] = self.cbox.VERSION
|
|
|
|
langs = self.cbox.getAvailableLanguages()
|
|
|
|
langs.sort()
|
|
|
|
for (index, lang) in enumerate(langs):
|
|
|
|
self.cbox.log.info("language loaded: %s" % lang)
|
|
|
|
self["Data.Languages.%d.name" % index] = lang
|
|
|
|
self["Data.Languages.%d.link" % index] = self.__getLanguageName(lang)
|
|
|
|
try:
|
|
|
|
self["Data.ScriptURL.Prot"] = cherrypy.request.scheme
|
|
|
|
host = cherrypy.request.headers["Host"]
|
|
|
|
self["Data.ScriptURL.Host"] = host.split(":",1)[0]
|
|
|
|
complete_url = "%s://%s" % (self["Data.ScriptURL.Prot"], self["Data.ScriptURL.Host"])
|
|
|
|
try:
|
|
|
|
port = int(host.split(":",1)[1])
|
|
|
|
complete_url += ":%s" % port
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
if cherrypy.request.scheme == "http":
|
|
|
|
port = 80
|
|
|
|
elif cherrypy.request.scheme == "https":
|
|
|
|
port = 443
|
|
|
|
else:
|
|
|
|
## unknown scheme -> port 0
|
|
|
|
self.cbox.log.info("unknown protocol scheme used: %s" % (cherrypy.request.scheme,))
|
|
|
|
port = 0
|
|
|
|
self["Data.ScriptURL.Port"] = port
|
|
|
|
## retrieve the relative address of the CGI (or the cherrypy base address)
|
|
|
|
## remove the last part of the url and add a slash
|
|
|
|
path = "/".join(cherrypy.request.path.split("/")[:-1]) + "/"
|
|
|
|
self["Data.ScriptURL.Path"] = path
|
|
|
|
complete_url += path
|
|
|
|
self["Data.ScriptURL"] = complete_url
|
|
|
|
except AttributeError:
|
|
|
|
self["Data.ScriptURL"] = ""
|
2006-09-12 10:55:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
def setCurrentDiskState(self, device):
|
|
|
|
for container in self.cbox.getContainerList():
|
|
|
|
if container.getDevice() == device:
|
|
|
|
isEncrypted = (container.getType() == CONT_TYPES["luks"]) and 1 or 0
|
|
|
|
isPlain = (container.getType() == CONT_TYPES["plain"]) and 1 or 0
|
|
|
|
isMounted = container.isMounted() and 1 or 0
|
|
|
|
self["Data.CurrentDisk.device"] = container.getDevice()
|
|
|
|
self["Data.CurrentDisk.name"] = container.getName()
|
|
|
|
self["Data.CurrentDisk.encryption"] = isEncrypted
|
|
|
|
self["Data.CurrentDisk.plaintext"] = isPlain
|
|
|
|
self["Data.CurrentDisk.active"] = isMounted
|
2006-11-02 14:55:31 +01:00
|
|
|
self["Data.CurrentDisk.size"] = CryptoBoxTools.getBlockDeviceSizeHumanly(container.getDevice())
|
2006-09-12 10:55:20 +02:00
|
|
|
if isMounted:
|
|
|
|
(size, avail, used) = container.getCapacity()
|
|
|
|
percent = used / size
|
|
|
|
self["Data.CurrentDisk.capacity.used"] = used
|
|
|
|
self["Data.CurrentDisk.capacity.free"] = avail
|
|
|
|
self["Data.CurrentDisk.capacity.size"] = size
|
|
|
|
self["Data.CurrentDisk.capacity.percent"] = percent
|
2006-11-06 07:16:27 +01:00
|
|
|
self["Settings.LinkAttrs.device"] = device
|
2006-09-12 10:55:20 +02:00
|
|
|
|
|
|
|
|
2006-09-18 12:16:05 +02:00
|
|
|
def setContainersState(self):
|
2006-09-07 13:21:56 +02:00
|
|
|
avail_counter = 0
|
|
|
|
active_counter = 0
|
|
|
|
for container in self.cbox.getContainerList():
|
2006-09-25 14:22:41 +02:00
|
|
|
## useful if the container was changed during an action
|
|
|
|
container.resetObject()
|
2006-09-07 13:21:56 +02:00
|
|
|
isEncrypted = (container.getType() == CONT_TYPES["luks"]) and 1 or 0
|
|
|
|
isPlain = (container.getType() == CONT_TYPES["plain"]) and 1 or 0
|
|
|
|
isMounted = container.isMounted() and 1 or 0
|
|
|
|
self["Data.Disks.%d.device" % avail_counter] = container.getDevice()
|
|
|
|
self["Data.Disks.%d.name" % avail_counter] = container.getName()
|
|
|
|
self["Data.Disks.%d.encryption" % avail_counter] = isEncrypted
|
|
|
|
self["Data.Disks.%d.plaintext" % avail_counter] = isPlain
|
|
|
|
self["Data.Disks.%d.active" % avail_counter] = isMounted
|
2006-11-02 14:55:31 +01:00
|
|
|
self["Data.Disks.%d.size" % avail_counter] = CryptoBoxTools.getBlockDeviceSizeHumanly(container.getDevice())
|
2006-09-07 13:21:56 +02:00
|
|
|
if isMounted: active_counter += 1
|
|
|
|
avail_counter += 1
|
|
|
|
self["Data.activeDisksCount"] = active_counter
|
2006-09-18 12:16:05 +02:00
|
|
|
|
|
|
|
|
2006-11-06 07:16:27 +01:00
|
|
|
def setPluginData(self):
|
|
|
|
for p in self.plugins:
|
|
|
|
lang_data = p.getLanguageData()
|
|
|
|
entryName = "Settings.PluginList." + p.getName()
|
|
|
|
self[entryName] = p.getName()
|
|
|
|
self[entryName + ".Link"] = lang_data.getValue("Link", p.getName())
|
|
|
|
self[entryName + ".Rank"] = p.getRank()
|
|
|
|
self[entryName + ".RequestAuth"] = p.isAuthRequired() and "1" or "0"
|
|
|
|
self[entryName + ".Enabled"] = p.isEnabled() and "1" or "0"
|
|
|
|
for a in p.pluginCapabilities:
|
|
|
|
self[entryName + ".Types." + a] = "1"
|
|
|
|
|
|
|
|
|
2006-09-18 12:16:05 +02:00
|
|
|
def __setConfigValues(self):
|
|
|
|
self["Settings.TemplateDir"] = os.path.abspath(self.prefs["Locations"]["TemplateDir"])
|
|
|
|
self["Settings.LanguageDir"] = os.path.abspath(self.prefs["Locations"]["LangDir"])
|
|
|
|
self["Settings.DocDir"] = os.path.abspath(self.prefs["Locations"]["DocDir"])
|
|
|
|
self["Settings.Stylesheet"] = self.prefs["WebSettings"]["Stylesheet"]
|
|
|
|
self["Settings.Language"] = self.prefs["WebSettings"]["Language"]
|
|
|
|
self["Settings.PluginDir"] = self.prefs["Locations"]["PluginDir"]
|
2006-10-11 17:51:28 +02:00
|
|
|
self["Settings.SettingsDir"] = self.prefs["Locations"]["SettingsDir"]
|
2006-09-18 12:16:05 +02:00
|
|
|
|
|
|
|
|
2006-09-07 13:21:56 +02:00
|
|
|
def __getLanguageName(self, lang):
|
2006-10-31 16:07:13 +01:00
|
|
|
try:
|
|
|
|
import neo_cgi, neo_util, neo_cs
|
|
|
|
except:
|
|
|
|
raise CryptoBoxExceptions.CBEnvironmentError("couldn't import 'neo_*'! Try 'apt-get install python-clearsilver'.")
|
2006-09-07 13:21:56 +02:00
|
|
|
hdf_path = os.path.join(self.prefs["Locations"]["LangDir"], lang + ".hdf")
|
|
|
|
hdf = neo_util.HDF()
|
|
|
|
hdf.readFile(hdf_path)
|
2006-09-25 14:22:41 +02:00
|
|
|
return hdf.getValue("Name",lang)
|
|
|
|
|
|
|
|
|
2006-09-07 13:21:56 +02:00
|
|
|
|