From f6ceaa03052e6878afd53a4bbb7f4429849fe25a Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 19 May 2014 15:30:59 +0200 Subject: OO style modules --- index.php | 85 +++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 24 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index d50d1d71..ae03e86c 100644 --- a/index.php +++ b/index.php @@ -1,51 +1,89 @@ doPreprocess(); } + public static function render() { self::$instance->doRender(); } + public static function ajax() { self::$instance->doAjax(); } + /** + * + * @var \Page + */ + private static $instance = false; + public static function set($name) + { + $name = preg_replace('/[^A-Za-z]/', '', $name); + $modulePath = 'modules/' . strtolower($name) . '.inc.php'; + if (!file_exists($modulePath)) { + Util::traceError('Invalid module file: ' . $modulePath); + } + require_once $modulePath; + $className = 'Page_' . $name; + if (!class_exists($className) || get_parent_class($className) !== 'Page') { + Util::traceError('Module not found: ' . $name); + } + self::$instance = new $className(); + } +} + +// Error reporting (hopefully goind to stderr, not being printed on pages) error_reporting(E_ALL); +// Set variable if this is an ajax request +$isAsync = (isset($_REQUEST['async'])) + || (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'); + // Autoload classes from ./inc which adhere to naming scheme .inc.php function slxAutoloader($class) { $file = 'inc/' . preg_replace('/[^a-z0-9]/', '', mb_strtolower($class)) . '.inc.php'; if (!file_exists($file)) return; require_once $file; } - spl_autoload_register('slxAutoloader'); -if (empty($_REQUEST['do'])) { - // No specific module - set default - $moduleName = 'main'; -} else { - $moduleName = preg_replace('/[^a-z]/', '', $_REQUEST['do']); -} +// Now determine which module to run +Page::set(empty($_REQUEST['do']) ? 'Main' : $_REQUEST['do']); -$modulePath = 'modules/' . $moduleName . '.inc.php'; - -if (!file_exists($modulePath)) { - Util::traceError('Invalid module: ' . $moduleName); +// Deserialize any messages to display +if (!$isAsync && isset($_REQUEST['message'])) { + Message::fromRequest(); } -// Deserialize any messages -if (isset($_REQUEST['message'])) { - Message::fromRequest(); +// CSRF/XSS check +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + User::load(); + if (!Util::verifyToken()) { + if ($isAsync) { + die('CSRF/XSS? Missing token in POST request!'); + } else { + Util::redirect('?do=Main'); + } + } } -// CSRF/XSS -if ($_SERVER['REQUEST_METHOD'] === 'POST' && !Util::verifyToken()) { - Util::redirect('?do=' . $moduleName); +// AJAX Stuff? Just do so. Otherwise, run preprocessing +if ($isAsync) { + Page::ajax(); + exit(0); } -// Load module - it will execute pre-processing, or act upon request parameters -require_once($modulePath); -unset($modulePath); +// Normal mode - preprocess first.... +Page::preprocess(); -// Main menu +// Generate Main menu $menu = new Menu; Render::addTemplate('main-menu', $menu); Message::renderList(); -// Render module. If the module wants to output anything, it will be done here -render_module(); +// Render page. If the module wants to output anything, it will be done here... +Page::render(); if (defined('CONFIG_DEBUG') && CONFIG_DEBUG) { Message::addWarning('debug-mode'); @@ -53,4 +91,3 @@ if (defined('CONFIG_DEBUG') && CONFIG_DEBUG) { // Send page to client. Render::output(); - -- cgit v1.2.3-55-g7522