Unverified Commit ffb29023 by Scott Committed by GitHub

Merge pull request #581 from pupi1985/patch-95

A few controller's branch comments
parents 419c30ab 1b9cc3ed
......@@ -645,7 +645,7 @@ function qa_message_html_defaults()
/**
* Return $voteview parameter to pass to qa_post_html_fields() in /qa-include/app/format.php.
* @param $postorbasetype The post, or for compatibility just a basetype, i.e. 'Q', 'A' or 'C'
* @param string|array $postorbasetype The post, or for compatibility just a basetype, i.e. 'Q', 'A' or 'C'
* @param bool $full Whether full post is shown
* @param bool $enabledif Whether to do checks for voting buttons (i.e. will always disable voting if false)
* @return bool|string Possible values:
......
......@@ -19,6 +19,14 @@
More about this license: http://www.question2answer.org/license.php
*/
use Q2A\Controllers\User\UserMessages;
use Q2A\Controllers\User\UserPosts;
use Q2A\Controllers\User\UserProfile;
use Q2A\Controllers\User\UsersList;
use Q2A\Exceptions\ExceptionHandler;
use Q2A\Http\Route;
use Q2A\Http\Router;
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../');
exit;
......@@ -178,15 +186,19 @@ function qa_get_request_content()
$firstlower = strtolower($requestparts[0]);
$routing = qa_page_routing();
$router = new \Q2A\Http\Router(qa_routing_config());
$routeInfo = $router->match($requestlower);
$router = new Router(qa_routing_config());
$route = $router->match($requestlower);
if ($routeInfo !== false) {
if (isset($route)) {
// use new Controller system
qa_set_template($routeInfo['id']);
$ctrl = new $routeInfo['controller']();
$qa_content = call_user_func_array(array($ctrl, $routeInfo['function']), $routeInfo['params']);
qa_set_template($route->getId());
$controllerClass = $route->getController();
$ctrl = new $controllerClass();
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];
......@@ -451,22 +463,22 @@ function qa_page_routing()
/**
* [qa_routing_config description]
* @return [type] [description]
* @return Route[]
*/
function qa_routing_config()
{
return array(
'user' => array('get', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile'),
'user-self' => array('get', 'user', '\Q2A\Controllers\User\UserProfile', 'index'),
'user-wall' => array('get', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall'),
'user-activity' => array('get', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity'),
'user-questions' => array('get', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions'),
'user-answers' => array('get', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers'),
'users-top' => array('get', 'users', '\Q2A\Controllers\User\UsersList', 'top'),
'users-blocked' => array('get', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked'),
'users-new' => array('get', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest'),
'users-special' => array('get', 'users/special', '\Q2A\Controllers\User\UsersList', 'special'),
new Route('user', 'get', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile'),
new Route('user-self', 'get', 'user', '\Q2A\Controllers\User\UserProfile', 'index'),
new Route('user-wall', 'get', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall'),
new Route('user-activity', 'get', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity'),
new Route('user-questions', 'get', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions'),
new Route('user-answers', 'get', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers'),
new Route('user-top', 'get', 'users', '\Q2A\Controllers\User\UsersList', 'top'),
new Route('user-blocked', 'get', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked'),
new Route('user-new', 'get', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest'),
new Route('user-special', 'get', 'users/special', '\Q2A\Controllers\User\UsersList', 'special'),
);
}
......
<?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,7 +18,78 @@
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();
}
/**
* 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
*
* @return mixed
*/
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\Exceptions\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
......@@ -143,11 +145,10 @@ class UserMessages extends \Q2A\Controllers\BaseController
// Sub menu for navigation in user pages
$ismyuser = isset($loginuserid) && $loginuserid == (QA_FINAL_EXTERNAL_USERS ? $userid : $useraccount['userid']);
$ismyuser = isset($loginuserid) && $loginuserid == $useraccount['userid'];
$qa_content['navigation']['sub'] = qa_user_sub_navigation($handle, 'wall', $ismyuser);
return $qa_content;
}
}
......@@ -18,6 +18,8 @@
namespace Q2A\Controllers\User;
use Q2A\Http\Exceptions\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\Exceptions\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\Exceptions\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\Exceptions;
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);
}
}
<?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;
class Route
{
/** @var string */
private $id;
/** @var string */
private $httpMethod;
/** @var string */
private $routePath;
/** @var string */
private $controller;
/** @var string */
private $action;
/** @var array */
private $parameters;
public function __construct($id = null, $httpMethod = null, $routePath = null, $controller = null, $action = null)
{
$this->id = $id;
$this->httpMethod = strtoupper($httpMethod);
$this->routePath = $routePath;
$this->controller = $controller;
$this->action = $action;
$this->parameters = array();
}
/**
* Bind actual request parameters to the route, replacing any existing ones.
*
* @param array $parameters
*/
public function bindParameters($parameters)
{
$this->parameters = $parameters;
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getHttpMethod()
{
return $this->httpMethod;
}
/**
* @return string
*/
public function getRoutePath()
{
return $this->routePath;
}
/**
* @return string
*/
public function getController()
{
return $this->controller;
}
/**
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
}
......@@ -20,42 +20,78 @@ namespace Q2A\Http;
class Router
{
/** @var Route[] */
protected $routes;
/** @var array */
private $paramsConverter;
/** @var string */
private $httpMethod;
public function __construct($routeData = array())
{
$this->routes = $routeData;
$this->paramsConverter = array(
'{str}' => '([^/]+)',
'{int}' => '([0-9]+)',
);
$this->httpMethod = strtoupper($_SERVER['REQUEST_METHOD']);
}
public function addRoute($id, $httpMethod, $routePath, $class, $func)
{
$this->routes[$id] = array($httpMethod, $routePath, $class, $func);
$this->routes[] = new Route($id, $httpMethod, $routePath, $class, $func);
}
/**
* Return the route definition that matches the given request. If none is found then null is
* returned.
*
* @param string $request Request that will be looked for a match
*
* @return Route|null
*/
public function match($request)
{
foreach ($this->routes as $id=>$route) {
if (count($route) !== 4) {
continue;
}
list($httpMethod, $routePath, $callClass, $callFunc) = $route;
if (strtoupper($httpMethod) !== strtoupper($_SERVER['REQUEST_METHOD'])) {
foreach ($this->routes as $route) {
if ($route->getHttpMethod() !== $this->httpMethod) {
continue;
}
$pathRegex = '#^' . str_replace(array('{str}', '{int}'), array('([^/]+)', '([0-9]+)'), $routePath) . '$#';
$pathRegex = $this->buildPathRegex($route->getRoutePath());
if (preg_match($pathRegex, $request, $matches)) {
return array(
'id' => $id,
'controller' => $callClass,
'function' => $callFunc,
'params' => array_slice($matches, 1)
);
$route->bindParameters(array_slice($matches, 1));
return $route;
}
}
return false;
return null;
}
/**
* Build the final regular expression to match the request. This method replaces all the
* parameters' placeholders with the given regular expression.
*
* @param string $routePath Route that might have placeholders
*
* @return string
*/
private function buildPathRegex($routePath)
{
return '#^' . strtr($routePath, $this->paramsConverter) . '$#';
}
/**
* Return the HTTP method of the current request.
*
* @return string
*/
public function getHttpMethod()
{
return $this->httpMethod;
}
}
<?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