Mercurial > SimpleWebPresenter
diff Options.inc.php @ 134:b6b4a58c7625
Using .inc.php rather than just .inc for include files.
| author | Tom Fredrik Blenning <bfg@bfgconsult.no> |
|---|---|
| date | Sun, 22 Jan 2023 19:22:00 +0100 |
| parents | Options.inc@14959382c901 |
| children | 2fe6713ccd64 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Options.inc.php Sun Jan 22 19:22:00 2023 +0100 @@ -0,0 +1,307 @@ +<?php +/** + */ +abstract class Cacheable { + const NO = 0; + const YES = 1; + const UNDETERMINED = -1; +} + +/** + * Contains alle configurable parameters, and "globals" + * + * @author Tom Fredrik Blenning Klaussen + */ +class Options +{ + private $defaultLang; + private $lang; + private $name; + private $acceptedLanguages = array(); + private $inputDefaults = array(); + private $cache; + private $urlParams = array(); + private $basePath; + private $flagUrl; + private $baseUrl; + private $cacheable = Cacheable::YES; + + /** + * Gets the default language + * + * @return two letter code for the language + */ + function getDefaultLang() + { + return $this->defaultLang; + } + + /** + * Gets the selected language + * + * @return two letter code for the language + */ + function getLang() + { + return $this->lang; + } + + /** + * Sets wether or not this page may be cached + * + * @param $cacheable Cacheable + */ + function setCacheable($cacheable) + { + $this->cacheable = $cacheable; + } + + /** + * Gets wether or not this page may be cached + * + * @return Cacheable, default is YES + */ + function getCacheable() + { + return $this->cacheable; + } + + /** + * Get the base url, or if non set, extracts it from _SERVER + * + * @return the baseurl for the scripts + */ + function getBaseUrl() + { + if ($this->baseUrl) + return $this->baseUrl; + + $request = $_SERVER['REQUEST_URI']; + + $l = strpos($request, '?'); + $base = ($l) ? substr($request, 0 , $l) : $request; + $base = rtrim($base, '/'); + + return "//" . $_SERVER['HTTP_HOST'] . $base; + } + + /** + * Replaces placeholder variables, with actual values. + * + * Currently supported values: + * @li \%HOST\% + * + * @param $value string to replace values in + * @return the processed string + */ + static function replacePlaceholders($value) + { + $value = preg_replace('/%HOST%/', $_SERVER['HTTP_HOST'], $value); + return $value; + } + + /** + * Sets the base url where scripts are located + * + * @param $baseUrl the url where scripts are located + */ + function setBaseUrl($baseUrl) + { + $baseUrl = self::replacePlaceholders($baseUrl); + $this->baseUrl = $baseUrl; + } + + /** + * Sets the url for the flag script + * + * @param $flagUrl for flag script + */ + function setFlagUrl($flagUrl) + { + $flagUrl = self::replacePlaceholders($flagUrl); + $this->flagUrl = $flagUrl; + } + + /** + * Gets the url for the flag script, or autogenerate if not set + * + * @return url for flag script + */ + function getFlagUrl() + { + if ($this->flagUrl) + return $this->flagUrl; + + return $this->getBaseUrl() . "/flag.php"; + } + + /** + * Sets the selected language + * + * @param $lang two letter code for the language + */ + function setLang($lang) + { + $this->lang = $lang; + } + + /** + * Gets the path where the scripts are located + * + * @return path where scripts are located + */ + function getBasePath() + { + return $this->basePath; + } + + /** + * Sets the path where the scripts are located + * + * @param $basePath path where scripts are located + */ + function setBasePath($basePath) + { + $this->basePath = $basePath; + } + + /** + * Sets a set of urlparameters + * + * @param $urlParams list of parameters to get from the URL + */ + function setUrlParams($urlParams) + { + foreach($urlParams as $key) { + $value = array_key_exists($key, $_GET) ? $_GET[$key] : ''; + $this->urlParams[$key] = $value; + } + } + + /** + * Gets the default language + * + * @return associative array of key and value for the url parameters + */ + function getUrlParams() + { + return $this->urlParams; + } + + /** + * Sets the name(identity) for this page + * + * @param $name name(identity) + */ + function setName($name) + { + $this->name = $name; + } + + /** + * Gets the name(identity) for this page + * + * @return name(identity) + */ + function getName() + { + return $this->name; + } + + /** + * Sets a cache object + * + * @param $cache CacheTimeCheck object + */ + function setCache($cache) + { + $this->cache = $cache; + } + + /** + * Gets the cache object + * + * @return cache object + */ + function getCache() + { + return $this->cache; + } + + /** + * A list of languages which this configuration supports. + * + * @return array of two letter language codes + */ + function getAcceptedLanguages() + { + return $this->acceptedLanguages; + } + + /** + * Gets the default value associated whith the key + * + * @param $key as specified in master xml file. + * @return associated default + */ + function getInputDefault($key) + { + return $this->inputDefaults[$key]; + } + + /** + * Constructs an options object + * + * This contstructor will consume any tag with the type option, and + * extract values from any tag with type input + * + * @include master.xml + * + * @param $baseDocument An open xml file + */ + function __construct($baseDocument) + { + $params = $baseDocument->getElementsByTagName("param"); + $toRemove = array(); + foreach ($params as $param) { + if ($param->getAttribute("type") == "option") { + $id = $param->getAttribute("id"); + if ($id == "lang") { + $this->defaultLang = $param->getAttribute("default"); + $accepts = $param->getElementsByTagName("accept_value"); + foreach($accepts as $accept) { + foreach($accept->childNodes as $child) { + array_push($this->acceptedLanguages, $child->nodeValue); + } + } + } + elseif ($id == "baseUrl") { + $value = $param->getAttribute("value"); + if($value) + $this->setBaseUrl($value); + } + elseif ($id == "flagUrl") { + $value = $param->getAttribute("value"); + if($value) + $this->setFlagUrl($value); + } + else { + warn("Invalid option: ${id}"); + } + //We need to iterate in the opposite direction when removing, + //so best shifting. + array_unshift($toRemove, $param); + } + elseif ($param->getAttribute("type") == "input") { + $id = $param->getAttribute("id"); + $default = $param->getAttribute("default"); + $this->inputDefaults[$id] = $default; + } + } + foreach($toRemove as $param) { + $parent = $param->parentNode; + $parent->removeChild($param); + } + } +} +?>
