2008-08-09 05:19:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is the base class of any Sapphire object that can be used to handle HTTP requests.
|
|
|
|
*
|
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
|
2008-10-06 00:16:07 +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 HTTPRequest} that contains the remainder of the URL to be parsed.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* In Sapphire, URL parsing is distributed throughout the object graph. For example, suppose that we have a search form
|
2008-10-06 00:16:07 +02:00
|
|
|
* that contains a {@link TreeMultiSelectField} named "Groups". We want to use ajax to load segments of this tree as they are needed
|
2008-08-09 05:19:54 +02:00
|
|
|
* 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)
|
2008-08-09 05:19:54 +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)
|
2008-10-06 00:16:07 +02:00
|
|
|
* - Form will determine that field/Groups is controlled by the Groups field, a TreeMultiselectField, and pass control to that.
|
|
|
|
* Matching $url_handlers: 'field/$FieldName!' => 'handleField' (defined in Form class)
|
2008-08-09 05:19:54 +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.
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2008-10-30 23:03:21 +01:00
|
|
|
class RequestHandler extends ViewableData {
|
2008-08-09 05:54:55 +02:00
|
|
|
protected $request = null;
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* The keys of this array are parse rules. See {@link HTTPRequest::match()} for a description of the rules available.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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(
|
|
|
|
* 'someaction', // someaction can be accessed by anyone, any time
|
|
|
|
* 'otheraction' => true, // So can otheraction
|
|
|
|
* 'restrictedaction' => 'ADMIN', // restrictedaction can only be people with ADMIN privilege
|
|
|
|
* 'complexaction' '->canComplexAction' // complexaction can only be accessed if $this->canComplexAction() returns true
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*/
|
|
|
|
static $allowed_actions = null;
|
|
|
|
/**
|
|
|
|
* Handles URL requests.
|
|
|
|
*
|
|
|
|
* - ViewableData::handleRequest() iterates through each rule in {@link self::$url_handlers}.
|
|
|
|
* - If the rule matches, the named method will be called.
|
|
|
|
* - If there is still more URL to be processed, then handleRequest() is called on the object that that method returns.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* customise the controller.
|
|
|
|
*
|
|
|
|
* @param $params The parameters taken from the parsed URL of the parent url handler
|
|
|
|
* @param $request The {@link HTTPRequest} object that is reponsible for distributing URL parsing
|
|
|
|
* @uses HTTPRequest
|
2008-10-06 00:16:07 +02:00
|
|
|
* @uses HTTPRequest->match()
|
2008-10-30 23:03:21 +01:00
|
|
|
* @return HTTPResponse|RequestHandler|string|array
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
|
|
|
function handleRequest($request) {
|
2008-08-09 05:54:55 +02:00
|
|
|
$this->request = $request;
|
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
// $handlerClass is used to step up the class hierarchy to implement url_handlers inheritance
|
|
|
|
$handlerClass = $this->class;
|
|
|
|
// We stop after RequestHandler; in other words, at ViewableData
|
|
|
|
while($handlerClass != 'ViewableData') {
|
|
|
|
// Todo: ajshort's stat rewriting could be useful here.
|
|
|
|
$urlHandlers = eval("return $handlerClass::\$url_handlers;");
|
|
|
|
|
|
|
|
if($urlHandlers) foreach($urlHandlers as $rule => $action) {
|
|
|
|
if(isset($_REQUEST['debug_request'])) Debug::message("Testing '$rule' with '" . $request->remaining() . "' on $this->class");
|
|
|
|
if($params = $request->match($rule, true)) {
|
|
|
|
// FIXME: This unnecessary coupling was added to fix a bug in Image_Uploader.
|
|
|
|
if($this instanceof Controller) $this->urlParams = $request->allParams();
|
2008-08-15 00:20:32 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
if(isset($_REQUEST['debug_request'])) {
|
|
|
|
Debug::message("Rule '$rule' matched to action '$action' on $this->class. Latest request params: " . var_export($request->latestParams(), true));
|
|
|
|
}
|
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) {
|
|
|
|
if(isset($_REQUEST['debug_request'])) Debug::message("Action not set; using default action method name 'index'");
|
|
|
|
$action = "index";
|
|
|
|
} else if(!is_string($action)) {
|
|
|
|
user_error("Non-string method name: " . var_export($action, true), E_USER_ERROR);
|
|
|
|
}
|
|
|
|
$result = $this->$action($request);
|
|
|
|
} else {
|
|
|
|
return $this->httpError(403, "Action '$action' isn't allowed on class $this->class");
|
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2008-10-30 23:28:01 +01:00
|
|
|
if($result instanceof HTTPResponse && $result->isError()) {
|
|
|
|
if(isset($_REQUEST['debug_request'])) Debug::message("Rule resulted in HTTP error; breaking");
|
|
|
|
return $result;
|
|
|
|
}
|
2008-08-11 02:03:57 +02:00
|
|
|
|
2008-10-30 23:28:01 +01: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
|
|
|
|
if(!$request->isEmptyPattern($rule) && is_object($result) && $result instanceof RequestHandler) {
|
|
|
|
$returnValue = $result->handleRequest($request);
|
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 {
|
|
|
|
return $this->httpError(400, "I can't handle sub-URLs of a $this->class object.");
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the given action is allowed to be called from a URL.
|
|
|
|
* It will interrogate {@link self::$allowed_actions} to determine this.
|
|
|
|
*/
|
|
|
|
function checkAccessAction($action) {
|
|
|
|
// Collate self::$allowed_actions from this class and all parent classes
|
|
|
|
$access = null;
|
|
|
|
$className = $this->class;
|
2008-10-30 23:03:21 +01:00
|
|
|
while($className != 'RequestHandler') {
|
2008-08-09 05:19:54 +02:00
|
|
|
// Merge any non-null parts onto $access.
|
|
|
|
$accessPart = eval("return $className::\$allowed_actions;");
|
|
|
|
if($accessPart !== null) $access = array_merge((array)$access, $accessPart);
|
|
|
|
|
|
|
|
// Build an array of parts for checking if part[0] == part[1], which means that this class doesn't directly define it.
|
|
|
|
$accessParts[] = $accessPart;
|
|
|
|
|
|
|
|
$className = get_parent_class($className);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add $allowed_actions from extensions
|
|
|
|
if($this->extension_instances) {
|
|
|
|
foreach($this->extension_instances as $inst) {
|
|
|
|
$accessPart = $inst->stat('allowed_actions');
|
|
|
|
if($accessPart !== null) $access = array_merge((array)$access, $accessPart);
|
|
|
|
}
|
|
|
|
}
|
2008-10-31 03:16:25 +01:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
if($action == 'index') return true;
|
|
|
|
|
2008-09-11 02:15:31 +02:00
|
|
|
// Make checkAccessAction case-insensitive
|
|
|
|
$action = strtolower($action);
|
2008-10-31 03:16:25 +01:00
|
|
|
if($access) {
|
|
|
|
foreach($access as $k => $v) $newAccess[strtolower($k)] = strtolower($v);
|
|
|
|
$access = $newAccess;
|
2008-09-11 02:15:31 +02:00
|
|
|
|
2008-10-31 03:16:25 +01:00
|
|
|
if(isset($access[$action])) {
|
|
|
|
$test = $access[$action];
|
|
|
|
if($test === true) return true;
|
|
|
|
if(substr($test,0,2) == '->') {
|
|
|
|
$funcName = substr($test,2);
|
|
|
|
return $this->$funcName();
|
|
|
|
}
|
|
|
|
if(Permission::check($test)) return true;
|
|
|
|
} else if((($key = array_search($action, $access)) !== false) && is_numeric($key)) {
|
|
|
|
return true;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
|
|
|
}
|
2008-10-31 03:16:25 +01:00
|
|
|
|
|
|
|
if($access === null || (isset($accessParts[1]) && $accessParts[0] === $accessParts[1])) {
|
|
|
|
// 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 and comparing it to the magic $this->hasMethod(). This will
|
|
|
|
// still let through actions that are handled by templates.
|
|
|
|
return method_exists($this, $action) || !$this->hasMethod($action);
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw an HTTP error instead of performing the normal processing
|
|
|
|
* @todo This doesn't work properly right now. :-(
|
|
|
|
*/
|
|
|
|
function httpError($errorCode, $errorMessage = null) {
|
|
|
|
$r = new HTTPResponse();
|
|
|
|
$r->setBody($errorMessage);
|
|
|
|
$r->setStatuscode($errorCode);
|
|
|
|
return $r;
|
|
|
|
}
|
2008-08-11 02:14:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the HTTPRequest object that this controller is using.
|
|
|
|
*
|
|
|
|
* @return HTTPRequest
|
|
|
|
*/
|
|
|
|
function getRequest() {
|
|
|
|
return $this->request;
|
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|