2008-08-09 05:19:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2012-03-24 04:38:57 +01:00
|
|
|
* This class is the base class of any SilverStripe object that can be used to handle HTTP requests.
|
2008-08-09 05:19:54 +02:00
|
|
|
*
|
2008-10-30 23:03:21 +01:00
|
|
|
* Any RequestHandler object can be made responsible for handling its own segment of the URL namespace.
|
2008-08-09 05:19:54 +02:00
|
|
|
* The {@link Director} begins the URL parsing process; it will parse the beginning of the URL to identify which
|
2012-09-26 23:34:00 +02:00
|
|
|
* controller is being used. It will then call {@link handleRequest()} on that Controller, passing it the parameters
|
|
|
|
* that it parsed from the URL, and the {@link SS_HTTPRequest} that contains the remainder of the URL to be parsed.
|
2008-10-06 00:16:07 +02:00
|
|
|
*
|
|
|
|
* You can use ?debug_request=1 to view information about the different components and rule matches for a specific URL.
|
2008-08-09 05:19:54 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* In SilverStripe, URL parsing is distributed throughout the object graph. For example, suppose that we have a
|
|
|
|
* search form that contains a {@link TreeMultiSelectField} named "Groups". We want to use ajax to load segments of
|
|
|
|
* this tree as they are needed rather than downloading the tree right at the beginning. We could use this URL to get
|
|
|
|
* the tree segment that appears underneath
|
|
|
|
*
|
2008-10-06 00:16:07 +02:00
|
|
|
* Group #36: "admin/crm/SearchForm/field/Groups/treesegment/36"
|
2008-08-09 05:19:54 +02:00
|
|
|
* - Director will determine that admin/crm is controlled by a new ModelAdmin object, and pass control to that.
|
2008-10-06 00:16:07 +02:00
|
|
|
* Matching Director Rule: "admin/crm" => "ModelAdmin" (defined in mysite/_config.php)
|
2012-09-26 23:34:00 +02:00
|
|
|
* - ModelAdmin will determine that SearchForm is controlled by a Form object returned by $this->SearchForm(), and
|
|
|
|
* pass control to that.
|
2008-10-30 23:03:21 +01:00
|
|
|
* Matching $url_handlers: "$Action" => "$Action" (defined in RequestHandler class)
|
2012-09-26 23:34:00 +02:00
|
|
|
* - Form will determine that field/Groups is controlled by the Groups field, a TreeMultiselectField, and pass
|
|
|
|
* control to that.
|
2008-10-06 00:16:07 +02:00
|
|
|
* Matching $url_handlers: 'field/$FieldName!' => 'handleField' (defined in Form class)
|
2012-09-26 23:34:00 +02:00
|
|
|
* - TreeMultiselectField will determine that treesegment/36 is handled by its treesegment() method. This method
|
|
|
|
* will return an HTML fragment that is output to the screen.
|
2008-10-06 00:16:07 +02:00
|
|
|
* Matching $url_handlers: "$Action/$ID" => "handleItem" (defined in TreeMultiSelectField class)
|
2008-08-09 05:19:54 +02:00
|
|
|
*
|
2008-10-30 23:03:21 +01:00
|
|
|
* {@link RequestHandler::handleRequest()} is where this behaviour is implemented.
|
2009-03-22 23:59:14 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-03-22 23:59:14 +01:00
|
|
|
* @subpackage control
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2008-10-30 23:03:21 +01:00
|
|
|
class RequestHandler extends ViewableData {
|
2010-12-16 03:36:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SS_HTTPRequest $request The request object that the controller was called with.
|
|
|
|
* Set in {@link handleRequest()}. Useful to generate the {}
|
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
protected $request = null;
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* The DataModel for this request
|
|
|
|
*/
|
|
|
|
protected $model = null;
|
|
|
|
|
2009-04-29 09:28:53 +02:00
|
|
|
/**
|
|
|
|
* This variable records whether RequestHandler::__construct()
|
|
|
|
* was called or not. Useful for checking if subclasses have
|
|
|
|
* called parent::__construct()
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $brokenOnConstruct = true;
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* The default URL handling rules. This specifies that the next component of the URL corresponds to a method to
|
|
|
|
* be called on this RequestHandlingData object.
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* The keys of this array are parse rules. See {@link SS_HTTPRequest::match()} for a description of the rules
|
|
|
|
* available.
|
2008-08-09 05:19:54 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* The values of the array are the method to be called if the rule matches. If this value starts with a '$', then
|
|
|
|
* the named parameter of the parsed URL wil be used to determine the method name.
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
|
|
|
static $url_handlers = array(
|
|
|
|
'$Action' => '$Action',
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define a list of action handling methods that are allowed to be called directly by URLs.
|
|
|
|
* The variable should be an array of action names. This sample shows the different values that it can contain:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* array(
|
2012-09-26 23:34:00 +02:00
|
|
|
* // someaction can be accessed by anyone, any time
|
|
|
|
* 'someaction',
|
|
|
|
* // So can otheraction
|
|
|
|
* 'otheraction' => true,
|
|
|
|
* // restrictedaction can only be people with ADMIN privilege
|
|
|
|
* 'restrictedaction' => 'ADMIN',
|
|
|
|
* // complexaction can only be accessed if $this->canComplexAction() returns true
|
|
|
|
* 'complexaction' '->canComplexAction'
|
2008-08-09 05:19:54 +02:00
|
|
|
* );
|
|
|
|
* </code>
|
2010-12-20 01:00:38 +01:00
|
|
|
*
|
|
|
|
* Form getters count as URL actions as well, and should be included in allowed_actions.
|
|
|
|
* Form actions on the other handed (first argument to {@link FormAction()} shoudl NOT be included,
|
|
|
|
* these are handled separately through {@link Form->httpSubmission}. You can control access on form actions
|
|
|
|
* either by conditionally removing {@link FormAction} in the form construction,
|
|
|
|
* or by defining $allowed_actions in your {@link Form} class.
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
|
|
|
static $allowed_actions = null;
|
2010-12-16 05:06:13 +01:00
|
|
|
|
2009-04-29 09:28:53 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->brokenOnConstruct = false;
|
2010-12-16 05:06:13 +01:00
|
|
|
|
|
|
|
// Check necessary to avoid class conflicts before manifest is rebuilt
|
|
|
|
if(class_exists('NullHTTPRequest')) $this->request = new NullHTTPRequest();
|
|
|
|
|
2012-05-01 04:43:52 +02:00
|
|
|
// This will prevent bugs if setDataModel() isn't called.
|
2011-05-01 07:33:02 +02:00
|
|
|
$this->model = DataModel::inst();
|
|
|
|
|
2009-04-29 09:28:53 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* Set the DataModel for this request.
|
|
|
|
*/
|
2012-05-01 04:43:52 +02:00
|
|
|
public function setDataModel($model) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$this->model = $model;
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Handles URL requests.
|
|
|
|
*
|
|
|
|
* - ViewableData::handleRequest() iterates through each rule in {@link self::$url_handlers}.
|
|
|
|
* - If the rule matches, the named method will be called.
|
2009-09-07 02:14:11 +02:00
|
|
|
* - If there is still more URL to be processed, then handleRequest()
|
|
|
|
* is called on the object that that method returns.
|
2008-08-09 05:19:54 +02:00
|
|
|
*
|
2009-09-07 02:14:11 +02:00
|
|
|
* Once all of the URL has been processed, the final result is returned.
|
|
|
|
* However, if the final result is an array, this
|
|
|
|
* array is interpreted as being additional template data to customise the
|
|
|
|
* 2nd to last result with, rather than an object
|
|
|
|
* in its own right. This is most frequently used when a Controller's
|
|
|
|
* action will return an array of data with which to
|
2008-08-09 05:19:54 +02:00
|
|
|
* customise the controller.
|
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @param $request The {@link SS_HTTPRequest} object that is reponsible for distributing URL parsing
|
|
|
|
* @uses SS_HTTPRequest
|
|
|
|
* @uses SS_HTTPRequest->match()
|
|
|
|
* @return SS_HTTPResponse|RequestHandler|string|array
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
|
2008-10-30 23:28:01 +01:00
|
|
|
// $handlerClass is used to step up the class hierarchy to implement url_handlers inheritance
|
2008-11-05 02:59:27 +01:00
|
|
|
$handlerClass = ($this->class) ? $this->class : get_class($this);
|
2009-04-29 09:28:53 +02:00
|
|
|
|
|
|
|
if($this->brokenOnConstruct) {
|
|
|
|
user_error("parent::__construct() needs to be called on {$handlerClass}::__construct()", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->request = $request;
|
2012-05-01 04:43:52 +02:00
|
|
|
$this->setDataModel($model);
|
2009-04-29 09:28:53 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
// We stop after RequestHandler; in other words, at ViewableData
|
2008-11-05 02:59:27 +01:00
|
|
|
while($handlerClass && $handlerClass != 'ViewableData') {
|
2012-04-18 13:10:57 +02:00
|
|
|
$urlHandlers = Config::inst()->get($handlerClass, 'url_handlers', Config::FIRST_SET);
|
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
if($urlHandlers) foreach($urlHandlers as $rule => $action) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($_REQUEST['debug_request'])) {
|
|
|
|
Debug::message("Testing '$rule' with '" . $request->remaining() . "' on $this->class");
|
|
|
|
}
|
2008-10-30 23:28:01 +01:00
|
|
|
if($params = $request->match($rule, true)) {
|
2010-10-15 04:53:11 +02:00
|
|
|
// Backwards compatible setting of url parameters, please use SS_HTTPRequest->latestParam() instead
|
2010-10-15 04:53:31 +02:00
|
|
|
//Director::setUrlParams($request->latestParams());
|
2008-08-15 00:20:32 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
if(isset($_REQUEST['debug_request'])) {
|
2012-09-26 23:34:00 +02:00
|
|
|
Debug::message("Rule '$rule' matched to action '$action' on $this->class."
|
|
|
|
. " Latest request params: " . var_export($request->latestParams(), true));
|
2008-10-30 23:28:01 +01:00
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
// Actions can reference URL parameters, eg, '$Action/$ID/$OtherID' => '$Action',
|
|
|
|
if($action[0] == '$') $action = $params[substr($action,1)];
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
if($this->checkAccessAction($action)) {
|
|
|
|
if(!$action) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($_REQUEST['debug_request'])) {
|
|
|
|
Debug::message("Action not set; using default action method name 'index'");
|
|
|
|
}
|
2008-10-30 23:28:01 +01:00
|
|
|
$action = "index";
|
|
|
|
} else if(!is_string($action)) {
|
|
|
|
user_error("Non-string method name: " . var_export($action, true), E_USER_ERROR);
|
2009-06-27 10:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2012-03-19 00:30:15 +01:00
|
|
|
if(!$this->hasMethod($action)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
return $this->httpError(404, "Action '$action' isn't available on class "
|
|
|
|
. get_class($this) . ".");
|
2012-03-19 00:30:15 +01:00
|
|
|
}
|
2009-06-27 10:48:44 +02:00
|
|
|
$result = $this->$action($request);
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
} catch(SS_HTTPResponse_Exception $responseException) {
|
2009-06-27 10:48:44 +02:00
|
|
|
$result = $responseException->getResponse();
|
|
|
|
}
|
2008-10-30 23:28:01 +01:00
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
return $this->httpError(403, "Action '$action' isn't allowed on class " . get_class($this));
|
2008-10-30 23:28:01 +01:00
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
if($result instanceof SS_HTTPResponse && $result->isError()) {
|
2008-10-30 23:28:01 +01:00
|
|
|
if(isset($_REQUEST['debug_request'])) Debug::message("Rule resulted in HTTP error; breaking");
|
|
|
|
return $result;
|
|
|
|
}
|
2008-08-11 02:03:57 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// If we return a RequestHandler, call handleRequest() on that, even if there is no more URL to
|
|
|
|
// parse. It might have its own handler. However, we only do this if we haven't just parsed an
|
|
|
|
// empty rule ourselves, to prevent infinite loops. Also prevent further handling of controller
|
|
|
|
// actions which return themselves to avoid infinite loops.
|
|
|
|
if($this !== $result && !$request->isEmptyPattern($rule) && is_object($result)
|
|
|
|
&& $result instanceof RequestHandler) {
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
$returnValue = $result->handleRequest($request, $model);
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
// Array results can be used to handle
|
|
|
|
if(is_array($returnValue)) $returnValue = $this->customise($returnValue);
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
return $returnValue;
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
// If we return some other data, and all the URL is parsed, then return that
|
|
|
|
} else if($request->allParsed()) {
|
|
|
|
return $result;
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
// But if we have more content on the URL and we don't know what to do with it, return an error.
|
|
|
|
} else {
|
2009-04-29 03:20:24 +02:00
|
|
|
return $this->httpError(404, "I can't handle sub-URLs of a $this->class object.");
|
2008-10-30 23:28:01 +01:00
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2008-10-30 23:28:01 +01:00
|
|
|
|
|
|
|
$handlerClass = get_parent_class($handlerClass);
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If nothing matches, return this object
|
|
|
|
return $this;
|
|
|
|
}
|
2009-03-21 06:10:05 +01:00
|
|
|
|
2009-10-11 02:07:26 +02:00
|
|
|
/**
|
|
|
|
* Get a unified array of allowed actions on this controller (if such data is available) from both the controller
|
|
|
|
* ancestry and any extensions.
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function allowedActions() {
|
2011-12-22 03:31:56 +01:00
|
|
|
|
|
|
|
$actions = Config::inst()->get(get_class($this), 'allowed_actions');
|
2012-03-09 04:39:12 +01:00
|
|
|
|
2009-10-11 02:07:26 +02:00
|
|
|
if($actions) {
|
2009-10-15 23:50:02 +02:00
|
|
|
// convert all keys and values to lowercase to
|
|
|
|
// allow for easier comparison, unless it is a permission code
|
|
|
|
$actions = array_change_key_case($actions, CASE_LOWER);
|
2009-10-11 02:07:26 +02:00
|
|
|
|
|
|
|
foreach($actions as $key => $value) {
|
|
|
|
if(is_numeric($key)) $actions[$key] = strtolower($value);
|
|
|
|
}
|
2012-03-09 04:39:12 +01:00
|
|
|
|
2009-10-11 02:07:26 +02:00
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:23 +02:00
|
|
|
/**
|
|
|
|
* Checks if this request handler has a specific action (even if the current user cannot access it).
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasAction($action) {
|
|
|
|
if($action == 'index') return true;
|
|
|
|
|
|
|
|
$action = strtolower($action);
|
2009-10-11 02:07:26 +02:00
|
|
|
$actions = $this->allowedActions();
|
2012-06-14 08:45:12 +02:00
|
|
|
|
2010-10-13 05:30:54 +02:00
|
|
|
// Check if the action is defined in the allowed actions as either a
|
|
|
|
// key or value. Note that if the action is numeric, then keys are not
|
|
|
|
// searched for actions to prevent actual array keys being recognised
|
|
|
|
// as actions.
|
2009-10-11 02:07:23 +02:00
|
|
|
if(is_array($actions)) {
|
2010-10-13 05:30:54 +02:00
|
|
|
$isKey = !is_numeric($action) && array_key_exists($action, $actions);
|
2012-06-14 08:45:12 +02:00
|
|
|
$isValue = in_array($action, $actions, true);
|
|
|
|
$isWildcard = (in_array('*', $actions) && $this->checkAccessAction($action));
|
|
|
|
if($isKey || $isValue || $isWildcard) return true;
|
2009-10-11 02:07:23 +02:00
|
|
|
}
|
2012-06-14 08:45:12 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!is_array($actions) || !$this->config()->get('allowed_actions',
|
|
|
|
Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES)) {
|
|
|
|
|
2009-10-11 02:07:23 +02:00
|
|
|
if($action != 'init' && $action != 'run' && method_exists($this, $action)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Check that the given action is allowed to be called from a URL.
|
|
|
|
* It will interrogate {@link self::$allowed_actions} to determine this.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function checkAccessAction($action) {
|
2010-10-13 03:24:15 +02:00
|
|
|
$actionOrigCasing = $action;
|
2009-03-21 06:10:05 +01:00
|
|
|
$action = strtolower($action);
|
2009-10-11 02:07:26 +02:00
|
|
|
$allowedActions = $this->allowedActions();
|
2009-10-11 07:16:18 +02:00
|
|
|
|
2009-03-21 06:10:05 +01:00
|
|
|
if($allowedActions) {
|
2009-04-02 18:34:27 +02:00
|
|
|
// check for specific action rules first, and fall back to global rules defined by asterisk
|
|
|
|
foreach(array($action,'*') as $actionOrAll) {
|
|
|
|
// check if specific action is set
|
|
|
|
if(isset($allowedActions[$actionOrAll])) {
|
|
|
|
$test = $allowedActions[$actionOrAll];
|
2009-07-16 05:44:15 +02:00
|
|
|
if($test === true || $test === 1 || $test === '1') {
|
2009-04-02 18:34:27 +02:00
|
|
|
// Case 1: TRUE should always allow access
|
|
|
|
return true;
|
|
|
|
} elseif(substr($test, 0, 2) == '->') {
|
|
|
|
// Case 2: Determined by custom method with "->" prefix
|
2012-07-23 07:44:37 +02:00
|
|
|
list($method, $arguments) = Object::parse_class_spec(substr($test, 2));
|
|
|
|
return call_user_func_array(array($this, $method), $arguments);
|
2009-10-11 11:15:51 +02:00
|
|
|
} else {
|
2009-04-02 18:34:27 +02:00
|
|
|
// Case 3: Value is a permission code to check the current member against
|
2009-10-11 11:15:51 +02:00
|
|
|
return Permission::check($test);
|
2009-04-02 18:34:27 +02:00
|
|
|
}
|
2009-10-11 11:15:51 +02:00
|
|
|
|
2012-03-09 04:39:12 +01:00
|
|
|
} elseif((($key = array_search($actionOrAll, $allowedActions, true)) !== false) && is_numeric($key)) {
|
2009-04-02 18:34:27 +02:00
|
|
|
// Case 4: Allow numeric array notation (search for array value as action instead of key)
|
2009-03-21 06:10:05 +01:00
|
|
|
return true;
|
2008-10-31 03:16:25 +01:00
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-21 06:10:05 +01:00
|
|
|
|
2009-10-11 11:15:51 +02:00
|
|
|
// If we get here an the action is 'index', then it hasn't been specified, which means that
|
|
|
|
// it should be allowed.
|
2010-10-13 03:24:15 +02:00
|
|
|
if($action == 'index' || empty($action)) return true;
|
2009-10-11 11:15:51 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if($allowedActions === null || !$this->config()->get('allowed_actions',
|
|
|
|
Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES)) {
|
|
|
|
|
|
|
|
// If no allowed_actions are provided, then we should only let through actions that aren't handled by
|
|
|
|
// magic methods we test this by calling the unmagic method_exists.
|
2010-10-13 03:24:15 +02:00
|
|
|
if(method_exists($this, $action)) {
|
|
|
|
// Disallow any methods which aren't defined on RequestHandler or subclasses
|
|
|
|
// (e.g. ViewableData->getSecurityID())
|
|
|
|
$r = new ReflectionClass(get_class($this));
|
|
|
|
if($r->hasMethod($actionOrigCasing)) {
|
|
|
|
$m = $r->getMethod($actionOrigCasing);
|
2010-10-13 03:46:02 +02:00
|
|
|
return ($m && is_subclass_of($m->getDeclaringClass()->getName(), 'RequestHandler'));
|
2010-10-13 03:24:15 +02:00
|
|
|
} else {
|
|
|
|
throw new Exception("method_exists() true but ReflectionClass can't find method - PHP is b0kred");
|
|
|
|
}
|
2010-10-13 03:26:16 +02:00
|
|
|
} else if(!$this->hasMethod($action)){
|
2010-10-13 03:24:15 +02:00
|
|
|
// Return true so that a template can handle this action
|
|
|
|
return true;
|
|
|
|
}
|
2008-10-31 03:16:25 +01:00
|
|
|
}
|
2009-03-21 06:10:05 +01:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-03-21 06:10:05 +01:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Throws a HTTP error response encased in a {@link SS_HTTPResponse_Exception}, which is later caught in
|
2009-06-27 10:48:44 +02:00
|
|
|
* {@link RequestHandler::handleAction()} and returned to the user.
|
|
|
|
*
|
|
|
|
* @param int $errorCode
|
2010-12-02 09:03:17 +01:00
|
|
|
* @param string $errorMessage Plaintext error message
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @uses SS_HTTPResponse_Exception
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2009-06-27 10:48:44 +02:00
|
|
|
public function httpError($errorCode, $errorMessage = null) {
|
2012-09-27 02:26:25 +02:00
|
|
|
// Call a handler method such as onBeforeHTTPError404
|
|
|
|
$this->extend('onBeforeHTTPError' . $errorCode, $this->request);
|
2010-12-02 09:03:17 +01:00
|
|
|
|
2012-09-27 02:26:25 +02:00
|
|
|
// Call a handler method such as onBeforeHTTPError, passing 404 as the first arg
|
|
|
|
$this->extend('onBeforeHTTPError', $errorCode, $this->request);
|
2010-12-02 09:03:17 +01:00
|
|
|
|
2012-09-27 02:26:25 +02:00
|
|
|
// Throw a new exception
|
|
|
|
throw new SS_HTTPResponse_Exception($errorMessage, $errorCode);
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2012-04-05 14:44:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 3.0 Use SS_HTTPRequest->isAjax() instead (through Controller->getRequest())
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isAjax() {
|
2012-04-05 14:44:42 +02:00
|
|
|
Deprecation::notice('3.0', 'Use SS_HTTPRequest->isAjax() instead (through Controller->getRequest())');
|
|
|
|
return $this->request->isAjax();
|
|
|
|
}
|
|
|
|
|
2008-08-11 02:14:48 +02:00
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Returns the SS_HTTPRequest object that this controller is using.
|
2010-12-16 05:06:13 +01:00
|
|
|
* Returns a placeholder {@link NullHTTPRequest} object unless
|
|
|
|
* {@link handleAction()} or {@link handleRequest()} have been called,
|
|
|
|
* which adds a reference to an actual {@link SS_HTTPRequest} object.
|
2008-08-11 02:14:48 +02:00
|
|
|
*
|
2010-12-16 05:06:13 +01:00
|
|
|
* @return SS_HTTPRequest|NullHTTPRequest
|
2008-08-11 02:14:48 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getRequest() {
|
2008-08-11 02:14:48 +02:00
|
|
|
return $this->request;
|
|
|
|
}
|
2010-12-16 03:36:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Typically the request is set through {@link handleAction()}
|
|
|
|
* or {@link handleRequest()}, but in some based we want to set it manually.
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setRequest($request) {
|
2010-12-16 03:36:31 +01:00
|
|
|
$this->request = $request;
|
|
|
|
}
|
2009-06-27 10:48:44 +02:00
|
|
|
}
|