Commit 17f1cfcf by pupi1985

Add an exception handler, a small exception hierarchy and middleware support

parent 963f4156
......@@ -23,8 +23,9 @@ use Q2A\Controllers\User\UserMessages;
use Q2A\Controllers\User\UserPosts;
use Q2A\Controllers\User\UserProfile;
use Q2A\Controllers\User\UsersList;
use Q2A\Http\Route;
use Q2A\Http\Router;
use Q2A\Exceptions\ExceptionHandler;
use Q2A\Http\Routing\Route;
use Q2A\Http\Routing\Router;
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../');
......@@ -193,8 +194,11 @@ function qa_get_request_content()
qa_set_template($route->getId());
$controllerClass = $route->getController();
$ctrl = new $controllerClass();
$qa_content = $ctrl->executeAction($route->getAction(), $route->getParameters());
try {
$qa_content = $ctrl->executeAction($route->getAction(), $route->getParameters());
} catch (Exception $e) {
$qa_content = (new ExceptionHandler())->handle($e);
}
} elseif (isset($routing[$requestlower])) {
qa_set_template($firstlower);
$qa_content = require QA_INCLUDE_DIR . $routing[$requestlower];
......
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Auth;
use Q2A\Exceptions\FatalErrorException;
class InternalUsersOnlyException extends FatalErrorException
{
/**
* InternalUsersOnlyException constructor.
*
* @param string $message
*/
public function __construct($message = 'Feature only supported by internal users.')
{
parent::__construct($message);
}
}
......@@ -18,12 +18,53 @@
namespace Q2A\Controllers;
use Q2A\Middleware\BaseMiddleware;
abstract class BaseController
{
// TODO: constructor taking Database class parameter
/** @var BaseMiddleware[string] */
private $middleware;
public function __construct()
{
// TODO: constructor taking Database class parameter
$this->middleware = array();
}
/**
* Execute the given action with the given parameters on this controller.
* Attach a middleware to one action or an array of actions. Use '*' to match all actions.
*
* @param BaseMiddleware $middleware
* @param array|string $actions
*/
public function addMiddleware(BaseMiddleware $middleware, $actions = '*')
{
if (is_array($actions)) {
foreach ($actions as $action) {
$this->addMiddlewareToAction($middleware, $action);
}
} else { // If it is a string
$this->addMiddlewareToAction($middleware, $actions);
}
}
/**
* @param BaseMiddleware $middleware
* @param string $action
*/
private function addMiddlewareToAction(BaseMiddleware $middleware, $action)
{
if (!isset($this->middleware[$action])) {
$this->middleware[$action] = array();
}
$this->middleware[$action][] = $middleware;
}
/**
* Execute the given action with the given parameters on this controller. after running all
* middleware for the action. This method is expected to return a qa_content array or throw an
* exception.
*
* @param string $action Action to execute
* @param array $parameters Parameters to send to the action
......@@ -32,6 +73,23 @@ abstract class BaseController
*/
public function executeAction($action, $parameters)
{
$this->executeMiddlewareForAction('*');
$this->executeMiddlewareForAction($action);
return call_user_func_array(array($this, $action), $parameters);
}
/**
* @param string $action
*/
private function executeMiddlewareForAction($action)
{
if (!isset($this->middleware[$action])) {
return;
}
foreach ($this->middleware[$action] as $middleware) {
$middleware->handle();
}
}
}
......@@ -18,34 +18,36 @@
namespace Q2A\Controllers\User;
use Q2A\Http\PageNotFoundException;
use Q2A\Middleware\Auth\InternalUsersOnly;
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/messages.php';
class UserMessages extends \Q2A\Controllers\BaseController
{
public function wall($handle)
public function __construct()
{
if (QA_FINAL_EXTERNAL_USERS) {
// Check we're not using single-sign on integration, which doesn't allow walls
qa_fatal_error('User accounts are handled by external code');
return;
}
parent::__construct();
$this->addMiddleware(new InternalUsersOnly());
}
public function wall($handle)
{
$userhtml = qa_html($handle);
$start = qa_get_start();
// Find the questions for this user
list($useraccount, $usermessages) = qa_db_select_with_pending(
qa_db_user_account_selectspec($handle, false),
qa_db_recent_messages_selectspec(null, null, $handle, false, qa_opt_if_loaded('page_size_wall'), $start)
);
if (!is_array($useraccount)) // check the user exists
return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
if (!is_array($useraccount)) { // check the user exists
throw new PageNotFoundException();
}
// Perform pagination
......@@ -149,5 +151,4 @@ class UserMessages extends \Q2A\Controllers\BaseController
return $qa_content;
}
}
......@@ -18,6 +18,8 @@
namespace Q2A\Controllers\User;
use Q2A\Http\PageNotFoundException;
require_once QA_INCLUDE_DIR . 'db/users.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/users.php';
......@@ -45,10 +47,9 @@ class UserPosts extends \Q2A\Controllers\BaseController
qa_db_user_recent_edit_qs_selectspec($loginuserid, $identifier)
);
if (!QA_FINAL_EXTERNAL_USERS && !is_array($useraccount)) // check the user exists
return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
if (!QA_FINAL_EXTERNAL_USERS && !is_array($useraccount)) { // check the user exists
throw new PageNotFoundException();
}
// Get information on user references
$questions = qa_any_sort_and_dedupe(array_merge($questions, $answerqs, $commentqs, $editqs));
......@@ -115,8 +116,9 @@ class UserPosts extends \Q2A\Controllers\BaseController
qa_db_user_recent_qs_selectspec($loginuserid, $identifier, qa_opt_if_loaded('page_size_qs'), $start)
);
if (!QA_FINAL_EXTERNAL_USERS && !is_array($useraccount)) // check the user exists
return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
if (!QA_FINAL_EXTERNAL_USERS && !is_array($useraccount)) { // check the user exists
throw new PageNotFoundException();
}
// Get information on user questions
......@@ -188,8 +190,9 @@ class UserPosts extends \Q2A\Controllers\BaseController
qa_db_user_recent_a_qs_selectspec($loginuserid, $identifier, qa_opt_if_loaded('page_size_activity'), $start)
);
if (!QA_FINAL_EXTERNAL_USERS && !is_array($useraccount)) // check the user exists
return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
if (!QA_FINAL_EXTERNAL_USERS && !is_array($useraccount)) { // check the user exists
throw new PageNotFoundException();
}
// Get information on user questions
......@@ -254,12 +257,12 @@ class UserPosts extends \Q2A\Controllers\BaseController
if (QA_FINAL_EXTERNAL_USERS) {
$this->userid = qa_handle_to_userid($handle);
if (!isset($this->userid))
return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
if (!isset($this->userid)) { // check the user exists
throw new PageNotFoundException();
}
$usershtml = qa_get_users_html(array($this->userid), false, qa_path_to_root(), true);
$this->userhtml = @$usershtml[$this->userid];
} else
$this->userhtml = qa_html($handle);
}
......
......@@ -18,6 +18,8 @@
namespace Q2A\Controllers\User;
use Q2A\Http\PageNotFoundException;
require_once QA_INCLUDE_DIR . 'db/users.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/users.php';
......@@ -40,8 +42,9 @@ class UserProfile extends \Q2A\Controllers\BaseController
if (QA_FINAL_EXTERNAL_USERS) {
$userid = qa_handle_to_userid($handle);
if (!isset($userid))
return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
if (!isset($userid)) { // check the user exists
throw new PageNotFoundException();
}
$usershtml = qa_get_users_html(array($userid), false, qa_path_to_root(), true);
$userhtml = @$usershtml[$userid];
......@@ -90,8 +93,9 @@ class UserProfile extends \Q2A\Controllers\BaseController
if (!QA_FINAL_EXTERNAL_USERS) { // if we're using integrated user management, we can know and show more
require_once QA_INCLUDE_DIR . 'app/messages.php';
if (!is_array($userpoints) && !is_array($useraccount))
return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
if (!is_array($userpoints) && !is_array($useraccount)) { // check the user exists
throw new PageNotFoundException();
}
$userid = $useraccount['userid'];
$fieldseditable = false;
......
......@@ -100,7 +100,7 @@ class DbConnection
}
}
public function query($query, $params=array())
public function query($query, $params = array())
{
try {
if (QA_DEBUG_PERFORMANCE) {
......@@ -127,7 +127,7 @@ class DbConnection
}
}
protected function execute($query, $params=array())
protected function execute($query, $params = array())
{
$stmt = $this->pdo->prepare($query);
......
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Exceptions;
use Exception;
class ErrorMessageException extends Exception
{
/**
* ErrorMessageException constructor.
*
* @param string $message
*/
public function __construct($message = 'Error message exception.')
{
parent::__construct($message);
}
}
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Exceptions;
use Exception;
use Q2A\Http\PageNotFoundException;
require_once QA_INCLUDE_DIR . 'app/format.php';
class ExceptionHandler
{
public function __construct()
{
}
public function handle(Exception $exception)
{
if ($exception instanceof FatalErrorException) {
$this->handleFatalErrorException($exception);
} elseif ($exception instanceof PageNotFoundException) {
return $this->handlePageNotFoundException($exception);
} elseif ($exception instanceof ErrorMessageException) {
return $this->handleErrorMessageException($exception);
} else {
return $this->handleUnknownException($exception);
}
}
private function handleFatalErrorException(FatalErrorException $exception)
{
qa_fatal_error($exception->getMessage());
}
private function handlePageNotFoundException(PageNotFoundException $exception)
{
header('HTTP/1.0 404 Not Found');
$qa_content = $this->handleErrorMessageException($exception);
$qa_content['suggest_next'] = qa_html_suggest_qs_tags(qa_using_tags());
return $qa_content;
}
private function handleErrorMessageException(ErrorMessageException $exception)
{
$qa_content = qa_content_prepare();
$qa_content['error'] = $exception->getMessage();
return $qa_content;
}
private function handleUnknownException(Exception $exception)
{
return $this->handleErrorMessageException(new ErrorMessageException($exception->getMessage()));
}
}
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Exceptions;
use Exception;
class FatalErrorException extends Exception
{
/**
* FatalErrorException constructor.
*
* @param string $message
*/
public function __construct($message = 'Fatal error exception.')
{
parent::__construct($message);
}
}
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Http;
use Q2A\Exceptions\ErrorMessageException;
class PageNotFoundException extends ErrorMessageException
{
/**
* PageNotFoundException constructor.
*
* @param string $message
*/
public function __construct($message = null)
{
if (is_null($message)) {
$message = qa_lang_html('main/page_not_found');
}
parent::__construct($message);
}
}
......@@ -16,7 +16,7 @@
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Http;
namespace Q2A\Http\Routing;
class Route
{
......
......@@ -16,7 +16,7 @@
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Http;
namespace Q2A\Http\Routing;
class Router
{
......
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Middleware\Auth;
use Q2A\Auth\InternalUsersOnlyException;
use Q2A\Middleware\BaseMiddleware;
class InternalUsersOnly extends BaseMiddleware
{
/**
* Throw an exception if the current configuration is set to external users.
*
* @throws InternalUsersOnlyException
*/
public function handle()
{
if (QA_FINAL_EXTERNAL_USERS) {
throw new InternalUsersOnlyException('User accounts are handled by external code');
}
}
}
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Middleware;
abstract class BaseMiddleware
{
abstract public function handle();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment