diff options
author | Simon Rettberg | 2013-10-15 19:24:01 +0200 |
---|---|---|
committer | Simon Rettberg | 2013-10-15 19:24:01 +0200 |
commit | 43e406068af8f2ae3d77301926bb5d31f392c961 (patch) | |
tree | a71f2fda66e789a6a1d2a9437bc1e37027e4ee93 /inc | |
download | slx-admin-43e406068af8f2ae3d77301926bb5d31f392c961.tar.gz slx-admin-43e406068af8f2ae3d77301926bb5d31f392c961.tar.xz slx-admin-43e406068af8f2ae3d77301926bb5d31f392c961.zip |
Initial commit
Diffstat (limited to 'inc')
-rw-r--r-- | inc/render.inc.php | 124 | ||||
-rw-r--r-- | inc/session.inc.php | 49 | ||||
-rw-r--r-- | inc/util.inc.php | 21 |
3 files changed, 194 insertions, 0 deletions
diff --git a/inc/render.inc.php b/inc/render.inc.php new file mode 100644 index 00000000..cf2b20b7 --- /dev/null +++ b/inc/render.inc.php @@ -0,0 +1,124 @@ +<?php + +define('RENDER_DEFAULT_TITLE', 'OpenSLX Admin'); + +require_once('inc/util.inc.php'); + +require_once('Mustache/Autoloader.php'); +Mustache_Autoloader::register(); + +/** + * HTML rendering helper class + */ +Render::init(); +class Render +{ + private static $mustache = false; + private static $body = ''; + private static $header = ''; + private static $title = ''; + private static $templateCache = array(); + private static $tags = array(); + + public static function init() + { + if (self::$mustache !== false) Util::traceError('Called Render::init() twice!'); + self::$mustache = new Mustache_Engine; + } + + /** + * Output the buffered, generated page + */ + public static function output() + { + Header('Content-Type: text/html; charset=utf-8'); + echo + '<!DOCTYPE html> + <html> + <head> + <title>', RENDER_DEFAULT_TITLE, self::$title, '</title> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <!-- Bootstrap --> + <link href="style/bootstrap.min.css" rel="stylesheet" media="screen"> + ', + self::$header + , + ' </head> + <body> + ', + self::$body + , + ' <script src="script/jquery.js"></script> + <script src="script/bootstrap.min.js"></script></body> + </html>' + ; + } + + /** + * Set the page title (title-tag) + */ + public static function setTitle($title) + { + self::$title = ' - ' . $title; + } + + /** + * Add raw html data to the header-section of the generated page + */ + public static function addHeader($html) + { + self::$header .= $html . "\n"; + } + + /** + * Add the given template to the output, using the given params for placeholders in the template + */ + public static function parse($template, $params) + { + self::$body .= self::$mustache->render(self::getTemplate($template), $params); + } + + /** + * Open the given html tag, optionally adding the passed assoc array of params + */ + public static function openTag($tag, $params = false) + { + array_push(self::$tags, $tag); + if (!is_array($params)) { + self::$body .= '<' . $tag . '>'; + } else { + self::$body .= '<' . $tag; + foreach ($params as $key => $val) { + self::$body .= ' ' . $key . '="' . htmlspecialchars($val) . '"'; + } + self::$body .= '>'; + } + } + + /** + * Close the given tag. Will check if it maches the tag last opened + */ + public static function closeTag($tag) + { + if (empty(self::$tags)) Util::traceError('Tried to close tag ' . $tag . ' when no open tags exist.'); + $last = array_pop(self::$tags); + if ($last !== $tag) Util::traceError('Tried to close tag ' . $tag . ' when last opened tag was ' . $last); + self::$body .= '</' . $tag . '>'; + } + + /** + * Private helper: Load the given template and return it + */ + private static function getTemplate($template) + { + if (isset(self::$templateCache[$template])) { + return self::$templateCache[$template]; + } + // Load from disk + $data = @file_get_contents('templates/' . $template . '.html'); + if ($data === false) $data = '<b>Non-existent template ' . $template . ' requested!</b>'; + self::$templateCache[$template] =& $data; + return $data; + } +} + diff --git a/inc/session.inc.php b/inc/session.inc.php new file mode 100644 index 00000000..a62c5cd3 --- /dev/null +++ b/inc/session.inc.php @@ -0,0 +1,49 @@ +<?php + +require_once('config.php'); + +@mkdir(CONFIG_SESSION_DIR, 0700); +@chmod(CONFIG_SESSION_DIR, 0700); + +session_set_save_handler('sh_open', 'sh_close', 'sh_read', 'sh_write', 'sh_destroy', 'sh_gc'); + +// Pretty much a reimplementation of the default session handler: Plain files +// Needs to be switched to db later + +function sh_open($path, $name) +{ + return true; +} + +function sh_close() +{ + return true; +} + +function sh_read($id) +{ + return (string)@file_get_contents(CONFIG_SESSION_DIR . '/slx-session-' . $id); +} + +function sh_write($id, $data) +{ + return @file_put_contents(CONFIG_SESSION_DIR . '/slx-session-' . $id, $data); +} + +function sh_destroy($id) +{ + return @unlink(CONFIG_SESSION_DIR . '/slx-session-' . $id); +} + +function sh_gc($maxAgeSeconds) +{ + $files = @glob(CONFIG_SESSION_DIR . '/slx-session-*'); + if (!is_array($files)) return false; + foreach ($files as $file) { + if (filemtime($file) + $maxAgeSeconds < time()) { + @unlink($file); + } + } + return true; +} + diff --git a/inc/util.inc.php b/inc/util.inc.php new file mode 100644 index 00000000..75cd914e --- /dev/null +++ b/inc/util.inc.php @@ -0,0 +1,21 @@ +<?php + +$verboseDebug = true; + +class Util +{ + public static function traceError($message) + { + global $verboseDebug; + Header('Content-Type: text/plain; charset=utf-8'); + echo "--------------------\nFlagrant system error:\n$message\n--------------------\n\n"; + if (isset($verboseDebug) && $verboseDebug) { + debug_print_backtrace(); + echo "\n\n"; + $vars = get_defined_vars(); + print_r($vars); + } + exit(0); + } +} + |