class IPF_Admin_LoginRequired extends IPF_Router_Shortcut
{
- public function response($request)
+ public function response($container, $request)
{
return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL::urlForView(array('IPF_Admin_User_Controller', 'login')));
}
class IPF_Admin_AccessDenied extends IPF_Router_Shortcut
{
- public function response($request)
+ public function response($container, $request)
{
- return new IPF_HTTP_Response_NotFound($request);
+ return new IPF_HTTP_Response_NotFound($container, $request);
}
}
$this->checkPermissions('view', 'change');
if (!isset($this->request->POST['ids']) || !is_array($this->request->POST['ids']))
- return new IPF_HTTP_Response_NotFound($this->request);
+ return new IPF_HTTP_Response_NotFound($this->container, $this->request);
if ($this->component->reorder($this->request->POST['ids']))
return new IPF_HTTP_Response_Json("Ok");
if (!$this->request->user->isAnonymous() && $this->request->user->is_superuser) {
$user = $this->authApp()->findUser($this->params[1]);
if (!$user)
- return new IPF_HTTP_Response_NotFound($this->request);
+ return new IPF_HTTP_Response_NotFound($this->container, $this->request);
$this->authApp()->login($this->request, $user);
}
<h1>Error 404</h1>
<h2>Page not found</h2>
<p>Sorry, it appears the page you were looking for does not exist anymore or might have been moved.</p>
-<p>Go to <a href="<?php echo IPF::get('app_base') ?>/">home page</a> or go back to <a href="#" onclick="javascript:history.back();">previous page</a>.</p>
-
+<p>Go to <a href="<?php echo $this->app_base ?>/">home page</a> or go back to <a href="#" onclick="javascript:history.back();">previous page</a>.</p>
<?php
-class IPF_Not_Found_Error_Page extends IPF_Error_Page
+namespace IPF\Error;
+
+use IPF_Settings;
+use PFF\NativeTemplate;
+
+class NotFoundPage extends NativeTemplate
{
- protected function templateFile()
+ public static function renderPage(IPF_Settings $settings, $request)
{
- return '404.html';
+ $page = new NativeTemplate(array(dirname(__FILE__)));
+ return $page->render('404.html', array(
+ 'request' => $request,
+ 'app_base' => $settings->get('app_base'),
+ ));
}
}
-
<h1>Error 500</h1>
<h2>Server error</h2>
<p>Our apologies…</p>
-<p>Go to <a href="<?php echo IPF::get('app_base') ?>/">home page</a> or go back to <a href="#" onclick="javascript:history.back();">previous page</a>.</p>
-
+<p>Go to <a href="<?php echo $this->app_base ?>/">home page</a> or go back to <a href="#" onclick="javascript:history.back();">previous page</a>.</p>
<?php
-class IPF_Server_Error_Page extends IPF_Error_Page
+namespace IPF\Error;
+
+use IPF_Settings;
+use PFF\NativeTemplate;
+
+class ServerErrorPage extends NativeTemplate
{
- protected function templateFile()
+ public static function renderPage(IPF_Settings $settings, $request, $exception = null)
{
- return '500.html';
+ $page = new static(array(dirname(__FILE__)));
+ return $page->render('500.html', array(
+ 'request' => $request,
+ 'exception' => $exception,
+ 'app_base' => $settings->get('app_base'),
+ ));
}
}
-
+++ /dev/null
-<?php
-
-class IPF_Server_Error_Page_Debug extends IPF_Error_Page
-{
- protected function templateFile()
- {
- return '500_debug.html';
- }
-
- function debug_print_r($v, $indent=0)
- {
- $result = array();
-
- if (is_object($v)) {
- $props = method_exists($v, '__debugInfo') ? $v->__debugInfo() : get_object_vars($v);
- $result[] = get_class($v) . ' Object';
- $result[] = '{';
- foreach ($props as $key => $val) {
- $result[] = ' [' . $key . '] => ' . $this->debug_print_r($val, $indent+2);
- }
- $result[] = '}';
- } elseif ($v instanceof stdClass) {
- $result[] = 'stdClass';
- $result[] = '{';
- foreach ($v as $k => $v) {
- $result[] = ' ' . $k . ' => ' . $this->debug_print_r($v, $indent+2);
- }
- $result[] = '}';
- } elseif (is_array($v)) {
- if ($v) {
- $result[] = 'Array';
- $result[] = '{';
- foreach ($v as $k => $v) {
- $result[] = ' [' . $k . '] => ' . $this->debug_print_r($v, $indent+2);
- }
- $result[] = '}';
- } else {
- $result[] = 'Array()';
- }
- } elseif (is_bool($v)) {
- return $v ? 'true' : 'false';
- } else {
- $result = explode("\n", print_r($v, true));
- }
-
- $i = str_repeat(' ', $indent);
- foreach ($result as &$line)
- $line = $i . $line;
- return ltrim(implode("\n", $result));
- }
-
- function parse_headers($list)
- {
- $result = array();
- foreach ($list as $line) {
- list($name, $value) = explode(':', $line, 2);
- $result[$name] = $value;
- }
- return $result;
- }
-
- function values($name, $values)
- {
- if ($values) {
- ?><h3><?php echo $name ?></h3>
- <table class="values">
- <?php foreach ($values as $name => $value): ?>
- <tr>
- <th><?php $this->e($name) ?></th>
- <td><?php $this->e($this->debug_print_r($value)) ?></td>
- </tr>
- <?php endforeach; ?>
- </table><?php
- }
- }
-
- function frames($exception)
- {
- $result = array();
-
- $file = $exception->getFile();
- $line = $exception->getLine();
- foreach ($exception->getTrace() as $frame) {
- $params = array();
- if (isset($frame["function"])) {
- try {
- if (isset($frame["class"])) {
- $r = new ReflectionMethod($frame["class"]."::".$frame["function"]);
- } else {
- $r = new ReflectionFunction($frame["function"]);
- }
- foreach ($r->getParameters() as $p) {
- $params[$p->getPosition()] = $p->getName();
- }
- } catch (ReflectionException $e) {
- }
- }
-
- $args = array();
- if (isset($frame['args'])) {
- foreach ($frame['args'] as $index => $arg) {
- $name = array_key_exists($index, $params) ? $params[$index] : "<arg-{$index}>";
- $args[$name] = $arg;
- }
- }
-
- $result[] = array(
- 'file' => $file,
- 'line' => $line,
- 'class' => @$frame['class'],
- 'type' => @$frame['type'],
- 'function' => @$frame['function'],
- 'func' => @$frame['class'].@$frame['type'].@$frame['function'],
- 'args' => $args,
- );
-
- $file = @$frame['file'];
- $line = @$frame['line'];
- }
-
- if ($file) {
- $result[] = array(
- 'file' => $file,
- 'line' => $line,
- 'class' => '',
- 'type' => '',
- 'function' => '',
- 'func' => '',
- 'args' => array(),
- );
- }
-
- return $result;
- }
-
- function sourceFile($file, $line)
- {
- $start = max($line - 10, 0);
- $ol = "<ol start={$start} class=source>";
-
- $rows = explode('<br />', nl2br(highlight_file($file, true)));
- $rows = array_slice($rows, $start, 20);
-
- foreach ($rows as $k => $row) {
- $attrs = ($k + $start === $line) ? ' class=current-line' : '';
- $ol .= "<li{$attrs}>{$row}</li>";
- }
- $ol .= '</ol>';
- return $ol;
- }
-}
-
--- /dev/null
+<?php
+
+namespace IPF\Error;
+
+use Exception;
+use PFF\NativeTemplate;
+use ReflectionException;
+use ReflectionFunction;
+use ReflectionMethod;
+use stdClass;
+
+class ServerErrorDebugPage extends NativeTemplate
+{
+ public static function renderPage($request, $exception)
+ {
+ $page = new static(array(dirname(__FILE__)));
+ return $page->render('500_debug.html', array(
+ 'request' => $request,
+ 'exception' => $exception,
+ ));
+ }
+
+ function debug_print_r($v, $indent = 0)
+ {
+ $result = array();
+
+ if (is_object($v)) {
+ $props = method_exists($v, '__debugInfo') ? $v->__debugInfo() : get_object_vars($v);
+ $result[] = get_class($v) . ' Object';
+ $result[] = '{';
+ foreach ($props as $key => $val) {
+ $result[] = ' [' . $key . '] => ' . $this->debug_print_r($val, $indent + 2);
+ }
+ $result[] = '}';
+ } elseif ($v instanceof stdClass) {
+ $result[] = 'stdClass';
+ $result[] = '{';
+ foreach ($v as $k => $v1) {
+ $result[] = ' ' . $k . ' => ' . $this->debug_print_r($v1, $indent + 2);
+ }
+ $result[] = '}';
+ } elseif (is_array($v)) {
+ if ($v) {
+ $result[] = 'Array';
+ $result[] = '{';
+ foreach ($v as $k => $v1) {
+ $result[] = ' [' . $k . '] => ' . $this->debug_print_r($v1, $indent + 2);
+ }
+ $result[] = '}';
+ } else {
+ $result[] = 'Array()';
+ }
+ } elseif (is_bool($v)) {
+ return $v ? 'true' : 'false';
+ } else {
+ $result = explode("\n", print_r($v, true));
+ }
+
+ $i = str_repeat(' ', $indent);
+ foreach ($result as &$line)
+ $line = $i . $line;
+ return ltrim(implode("\n", $result));
+ }
+
+ function parse_headers($list)
+ {
+ $result = array();
+ foreach ($list as $line) {
+ list($name, $value) = explode(':', $line, 2);
+ $result[$name] = $value;
+ }
+ return $result;
+ }
+
+ function values($name, $values)
+ {
+ if ($values) {
+ ?><h3><?php echo $name ?></h3>
+ <table class="values">
+ <?php foreach ($values as $name => $value): ?>
+ <tr>
+ <th><?php $this->e($name) ?></th>
+ <td><?php $this->e($this->debug_print_r($value)) ?></td>
+ </tr>
+ <?php endforeach; ?>
+ </table><?php
+ }
+ }
+
+ function frames(Exception $exception)
+ {
+ $result = array();
+
+ $file = $exception->getFile();
+ $line = $exception->getLine();
+ foreach ($exception->getTrace() as $frame) {
+ $params = array();
+ if (isset($frame["function"])) {
+ try {
+ if (isset($frame["class"])) {
+ $r = new ReflectionMethod($frame["class"] . "::" . $frame["function"]);
+ } else {
+ $r = new ReflectionFunction($frame["function"]);
+ }
+ foreach ($r->getParameters() as $p) {
+ $params[$p->getPosition()] = $p->getName();
+ }
+ } catch (ReflectionException $e) {
+ }
+ }
+
+ $args = array();
+ if (isset($frame['args'])) {
+ foreach ($frame['args'] as $index => $arg) {
+ $name = array_key_exists($index, $params) ? $params[$index] : "<arg-{$index}>";
+ $args[$name] = $arg;
+ }
+ }
+
+ $result[] = array(
+ 'file' => $file,
+ 'line' => $line,
+ 'class' => @$frame['class'],
+ 'type' => @$frame['type'],
+ 'function' => @$frame['function'],
+ 'func' => @$frame['class'] . @$frame['type'] . @$frame['function'],
+ 'args' => $args,
+ );
+
+ $file = @$frame['file'];
+ $line = @$frame['line'];
+ }
+
+ if ($file) {
+ $result[] = array(
+ 'file' => $file,
+ 'line' => $line,
+ 'class' => '',
+ 'type' => '',
+ 'function' => '',
+ 'func' => '',
+ 'args' => array(),
+ );
+ }
+
+ return $result;
+ }
+
+ function sourceFile($file, $line)
+ {
+ $start = max($line - 10, 0);
+ $ol = "<ol start={$start} class=source>";
+
+ $rows = explode('<br />', nl2br(highlight_file($file, true)));
+ $rows = array_slice($rows, $start, 20);
+
+ foreach ($rows as $k => $row) {
+ $attrs = ($k + $start === $line) ? ' class=current-line' : '';
+ $ol .= "<li{$attrs}>{$row}</li>";
+ }
+ $ol .= '</ol>';
+ return $ol;
+ }
+}
+++ /dev/null
-<?php
-
-abstract class IPF_Error_Page extends \PFF\NativeTemplate
-{
- public static function render404($request)
- {
- if (IPF::get('render404')) {
- $func = IPF::get('render404');
- return IPF::callFunction($func, array($request));
- } else {
- return IPF_Not_Found_Error_Page::renderPage($request);
- }
- }
-
- public static function render500($request, $exception)
- {
- if (IPF::get('debug')) {
- return IPF_Server_Error_Page_Debug::renderPage($request, $exception);
- } elseif (IPF::get('render500')) {
- $func = IPF::get('render500');
- return IPF::callFunction($func, array($request, $exception));
- } else {
- return IPF_Server_Error_Page::renderPage($request, $exception);
- }
- }
-
- protected static function renderPage($request, $exception=null)
- {
- $page = new static(array(dirname(__FILE__)));
- return $page->render($page->templateFile(), array('request' => $request, 'exception' => $exception));
- }
-
- abstract protected function templateFile();
-}
-
--- /dev/null
+<?php
+
+namespace IPF\Error;
+
+use Pimple\Container;
+use Pimple\ServiceProviderInterface;
+
+class ErrorPageProvider implements ServiceProviderInterface
+{
+ public function register(Container $container)
+ {
+ $container['render404'] = function ($c) {
+ $settings = $c['settings'];
+ return function ($request) use ($settings) {
+ return NotFoundPage::renderPage($settings, $request);
+ };
+ };
+ $container['render500'] = function ($c) {
+ $settings = $c['settings'];
+ return function ($request, $exception) use ($settings) {
+ $isDebug = $settings->get('debug');
+ if ($isDebug) {
+ return ServerErrorDebugPage::renderPage($request, $exception);
+ } else {
+ return ServerErrorPage::renderPage($settings, $request, $exception);
+ }
+ };
+ };
+ }
+}
<?php
+use Pimple\Container;
+
class IPF_HTTP_Response_NotFound extends IPF_HTTP_Response
{
- function __construct($request=null)
+ function __construct(Container $container, IPF_HTTP_Request $request)
{
- parent::__construct(IPF_Error_Page::render404($request));
+ $render404 = $container['render404'];
+ $content = call_user_func($render404, $request);
+
+ parent::__construct($content);
$this->status_code = 404;
}
}
-
+++ /dev/null
-<?php
-
-class IPF_HTTP_Response_ServerError extends IPF_HTTP_Response
-{
- function __construct($request, $exception, $mimetype=null)
- {
- parent::__construct(IPF_Error_Page::render500($request, $exception), $mimetype);
- $this->status_code = 500;
- }
-}
{
$m = $this->getRouter()->match($request);
if (!$m) {
- return new IPF_HTTP_Response_NotFound($request);
+ return new IPF_HTTP_Response_NotFound($this->container, $request);
}
list($route, $match) = $m;
$controller = new $controllerClass($this->container);
return $controller->process($route->action(), $request, $match);
} catch (IPF_Router_Shortcut $e) {
- return $e->response($request);
+ return $e->response($this->container, $request);
}
}
throw new \Exception('Response is not a IPF_HTTP_Response');
} catch (\Exception $exception) {
error_log($exception);
- $response = new IPF_HTTP_Response_ServerError($request, $exception);
+ $response = $this->getServerErrorResponse($request, $exception);
}
if ($this->previous_error_handler)
set_error_handler($this->previous_error_handler);
$error = error_get_last();
if ($error) {
$exception = new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
- $response = new IPF_HTTP_Response_ServerError($request, $exception);
+ $response = $this->getServerErrorResponse($request, $exception);
$response->render();
}
}
-}
+ /**
+ * @param IPF_HTTP_Request $request
+ * @param Exception $exception
+ * @return IPF_HTTP_Response
+ */
+ private function getServerErrorResponse(IPF_HTTP_Request $request, Exception $exception)
+ {
+ $render500 = $this->container['render500'];
+ $content = call_user_func($render500, $request, $exception);
+
+ $response = new IPF_HTTP_Response($content, null);
+ $response->status_code = 500;
+ return $response;
+ }
+}
<?php
use IPF\CoreServicesProvider;
+use IPF\Error\ErrorPageProvider;
use IPF\Template\TemplateProvider;
use Pimple\Container;
}
$this->container['apps'] = $apps;
$this->container['router'] = function ($c) {
- return new IPF_Router;
+ $settings = $c['settings'];
+ return new IPF_Router(
+ $settings->get('urls'),
+ $settings->get('app_base')
+ );
};
$this->router = $this->container['router'];
}
{
$this->container->register(new CoreServicesProvider());
$this->container->register(new TemplateProvider());
+ $this->container->register(new ErrorPageProvider());
$this->container['databaseConnection'] = function ($c) {
return IPF_Database::connect();
{
/** @var IPF_Route[] */
private $routes = array();
+ /** @var string */
+ private $app_base;
- public function __construct()
+ public function __construct(array $urls, $app_base)
{
- $this->flattenRoutes(IPF::get('urls'));
+ $this->app_base = $app_base;
+ $this->flattenRoutes($urls);
}
private function flattenRoutes($routes, $prefix='', $common=array())
{
foreach ($this->routes as $route)
if ($route->routesTo($view))
- return IPF::get('app_base') . $route->matcher()->reverse($params);
+ return $this->app_base . $route->matcher()->reverse($params);
throw new IPF_Exception('Error, the view: '.print_r($view, true).' has not been found.');
}
}
abstract class IPF_Router_Shortcut extends Exception
{
- public abstract function response($request);
+ public abstract function response($container, $request);
}
class IPF_HTTP_Error404 extends IPF_Router_Shortcut
{
- public function response($request)
+ public function response($container, $request)
{
- return new IPF_HTTP_Response_NotFound($request);
+ return new IPF_HTTP_Response_NotFound($container, $request);
}
}
-