diff --git a/HandBrakeCluster/Exceptions.class.php b/HandBrakeCluster/Exceptions.class.php index 6e02620..73a98d8 100644 --- a/HandBrakeCluster/Exceptions.class.php +++ b/HandBrakeCluster/Exceptions.class.php @@ -10,4 +10,8 @@ class HandBrakeCluster_Exception_ResultCountMismatch extends Exception {}; class HandBrakeCluster_Exception_UnknownSetting extends Exception {}; +class HandBrakeCluster_Exception_TemplateException extends Exception {}; +class HandBrakeCluster_Exception_Unauthorized extends HandBrakeCluster_Exception_TemplateException {}; +class HandBrakeCluster_Exception_FileNotFound extends HandBrakeCluster_Exception_TemplateException {}; + ?> diff --git a/HandBrakeCluster/Main.class.php b/HandBrakeCluster/Main.class.php index 216e5cb..469e4a3 100644 --- a/HandBrakeCluster/Main.class.php +++ b/HandBrakeCluster/Main.class.php @@ -29,7 +29,7 @@ class HandBrakeCluster_Main { $this->smarty->config_fir = './config'; $this->smarty->assign('version', '0.1'); - $this->smarty->assign('base_uri', '/handbrake/'); + $this->smarty->assign('base_uri', dirname($_SERVER['SCRIPT_NAME']) . '/'); } diff --git a/HandBrakeCluster/Page.class.php b/HandBrakeCluster/Page.class.php index 16d4431..7920daf 100644 --- a/HandBrakeCluster/Page.class.php +++ b/HandBrakeCluster/Page.class.php @@ -18,22 +18,43 @@ class HandBrakeCluster_Page { } public function template_filename() { - $template_filename = $this->page . '.tpl'; - - if (!$this->smarty->template_exists($template_filename)) { - $template_filename = 'home.tpl'; - } - - return $template_filename; + return $this->page . '.tpl'; } - public function evaluate() { - $code_filename = 'pages/' . $this->page . '.php'; + public function evaluate($template_variables = array()) { + $code_filename = $this->page . '.php'; + $template_filename = $this->template_filename(); - if (file_exists($code_filename)) { - eval("include '$code_filename';"); + try { + $this->render($template_filename, $code_filename, $template_variables); + } catch (HandBrakeCluster_Exception_FileNotFound $e) { + $this->render('errors/404.tpl', 'errors/404.php'); + } catch (HandBrakeCluster_Exception_TemplateException $e) { + $this->render('errors/unhandled-exception.tpl', 'errors/unhandled-exception.php', array( + 'exception' => $e, + )); + } + } + + protected function render($template_filename, $code_filename = null, $template_variables = array()) { + if ( ! $this->smarty->template_exists($template_filename)) { + throw new HandBrakeCluster_Exception_FileNotFound($template_filename); } + // Copy all the template variables into the namespace for this function, + // so that they are readily available to the template + foreach ($template_variables as $__k => $__v) { + $$__k = $__v; + } + + // Include the template code file, which will do all the work for this page + $real_code_filename = 'pages/' . $code_filename; + if ($code_filename && file_exists($real_code_filename)) { + include $real_code_filename; + } + + // Now execute the template itself, which will render the results of the code file + $this->smarty->assign('page_content', $this->smarty->fetch($template_filename)); } }; diff --git a/HandBrakeCluster/RequestParser.class.php b/HandBrakeCluster/RequestParser.class.php index 24e4f39..34371dc 100644 --- a/HandBrakeCluster/RequestParser.class.php +++ b/HandBrakeCluster/RequestParser.class.php @@ -42,6 +42,10 @@ class HandBrakeCluster_RequestParser { return null; } + + public function request_string() { + return $this->request_string; + } }; diff --git a/index.php b/index.php index 2f1b400..c8a82e3 100644 --- a/index.php +++ b/index.php @@ -8,8 +8,6 @@ try { $page = new HandBrakeCluster_Page($smarty, $main->request()); $page->evaluate(); - $smarty->assign('page_content', $smarty->fetch($page->template_filename())); - $smarty->display('index.tpl'); } catch (HandBrakeCluster_Exception $e) { die("Uncaught Exception: " . $e->getMessage()); diff --git a/pages/errors/404.php b/pages/errors/404.php new file mode 100644 index 0000000..d5bc834 --- /dev/null +++ b/pages/errors/404.php @@ -0,0 +1,9 @@ +request(); + +$this->smarty->assign('requested_page', htmlspecialchars($req->request_string())); + + +?> \ No newline at end of file diff --git a/pages/errors/unhandled-exception.php b/pages/errors/unhandled-exception.php new file mode 100644 index 0000000..1427536 --- /dev/null +++ b/pages/errors/unhandled-exception.php @@ -0,0 +1,10 @@ +config(); + +$this->smarty->assign('display_exceptions', $config->get('debug.display_exceptions')); +$this->smarty->assign('exception', $exception); +$this->smarty->assign('exception_type', get_class($exception)); + +?> \ No newline at end of file diff --git a/templates/errors/404.tpl b/templates/errors/404.tpl new file mode 100644 index 0000000..607707a --- /dev/null +++ b/templates/errors/404.tpl @@ -0,0 +1,6 @@ +

The requested page could not be found

+

+ The file you requested ({$requested_page}) could not be found. + If you typed in the address manually, check that you have spelled it correctly, + or if you followed a link, let us know and we'll look into it. +

\ No newline at end of file diff --git a/templates/errors/unhandled-exception.tpl b/templates/errors/unhandled-exception.tpl new file mode 100644 index 0000000..f1e02c9 --- /dev/null +++ b/templates/errors/unhandled-exception.tpl @@ -0,0 +1,36 @@ +

An unhandled error has occurred

+

+ There was a problem trying to complete the requested action. Please try again and if the problem persists, let us know. +

+ +{if $display_exceptions} +

+ An unhandled exception was caught during the page template processing. The full details are shown below: +

+ + + + + + + + + + + + + + + + + + + + + + + + + +
Exception{$exception_type}
File{$exception->getFile()}
Line{$exception->getLine()}
Message{$exception->getMessage()}
+{/if} \ No newline at end of file