2008-08-09 05:19:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a HTTP-request, including a URL that is tokenised for parsing, and a request method (GET/POST/PUT/DELETE).
|
|
|
|
* This is used by {@link RequestHandlingData} objects to decide what to do.
|
|
|
|
*
|
|
|
|
* The intention is that a single HTTPRequest object can be passed from one object to another, each object calling
|
|
|
|
* match() to get the information that they need out of the URL. This is generally handled by
|
|
|
|
* {@link RequestHandlingData::handleRequest()}.
|
2008-08-09 09:03:24 +02:00
|
|
|
*
|
|
|
|
* @todo Accept X_HTTP_METHOD_OVERRIDE http header and $_REQUEST['_method'] to override request types (useful for webclients
|
|
|
|
* not supporting PUT and DELETE)
|
2008-10-06 00:16:07 +02:00
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage control
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2008-08-09 05:29:30 +02:00
|
|
|
class HTTPRequest extends Object implements ArrayAccess {
|
2008-10-06 00:16:07 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
2008-10-06 00:35:14 +02:00
|
|
|
* The non-extension parts of the passed URL as an array, originally exploded by the "/" separator.
|
2008-10-06 00:16:07 +02:00
|
|
|
* All elements of the URL are loaded in here,
|
|
|
|
* and subsequently popped out of the array by {@link shift()}.
|
2008-10-06 00:35:14 +02:00
|
|
|
* Only use this structure for internal request handling purposes.
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
|
|
|
protected $dirParts;
|
2008-10-06 00:16:07 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* The URL extension
|
|
|
|
*/
|
|
|
|
protected $extension;
|
|
|
|
|
|
|
|
/**
|
2008-08-09 09:03:24 +02:00
|
|
|
* The HTTP method: GET/PUT/POST/DELETE/HEAD
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
|
|
|
protected $httpMethod;
|
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
|
|
|
* @var array $getVars Contains alls HTTP GET parameters passed into this request.
|
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
protected $getVars = array();
|
2008-08-09 09:03:24 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
|
|
|
* @var array $postVars Contains alls HTTP POST parameters passed into this request.
|
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
protected $postVars = array();
|
2008-08-09 09:03:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP Headers like "Content-Type: text/xml"
|
|
|
|
*
|
|
|
|
* @see http://en.wikipedia.org/wiki/List_of_HTTP_headers
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $headers = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw HTTP body, used by PUT and POST requests.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $body;
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
|
|
|
* @var array $allParams Contains an assiciative array of all
|
|
|
|
* arguments matched in all calls to {@link RequestHandlingData->handleRequest()}.
|
|
|
|
* Its a "historical record" thats specific to the current call of
|
|
|
|
* {@link handleRequest()}, and only complete once the last call is made.
|
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
protected $allParams = array();
|
2008-08-09 09:03:24 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
|
|
|
* @var array $latestParams Contains an associative array of all
|
|
|
|
* arguments matched in the current call from {@link RequestHandlingData->handleRequest()},
|
|
|
|
* as denoted with a "$"-prefix in the $url_handler definitions.
|
|
|
|
* Contains different states throughout its lifespan, so just useful
|
|
|
|
* while processed in {@link RequestHandlingData} and to get the last
|
|
|
|
* processes arguments.
|
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
protected $latestParams = array();
|
|
|
|
|
|
|
|
protected $unshiftedButParsedParts = 0;
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
function isGET() {
|
|
|
|
return $this->httpMethod == 'GET';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPOST() {
|
|
|
|
return $this->httpMethod == 'POST';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPUT() {
|
|
|
|
return $this->httpMethod == 'PUT';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDELETE() {
|
|
|
|
return $this->httpMethod == 'DELETE';
|
|
|
|
}
|
2008-09-12 06:49:15 +02:00
|
|
|
|
|
|
|
function isHEAD() {
|
|
|
|
return $this->httpMethod == 'HEAD';
|
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
function setBody($body) {
|
|
|
|
$this->body = $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBody() {
|
|
|
|
return $this->body;
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
function getVars() {
|
|
|
|
return $this->getVars;
|
|
|
|
}
|
|
|
|
function postVars() {
|
|
|
|
return $this->postVars;
|
|
|
|
}
|
2008-10-06 00:16:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all combined HTTP GET and POST parameters
|
|
|
|
* passed into this request. If a parameter with the same
|
|
|
|
* name exists in both arrays, the POST value is returned.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
function requestVars() {
|
|
|
|
return array_merge($this->getVars, $this->postVars);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVar($name) {
|
|
|
|
if(isset($this->getVars[$name])) return $this->getVars[$name];
|
|
|
|
}
|
2008-10-06 00:16:07 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
function postVar($name) {
|
|
|
|
if(isset($this->postVars[$name])) return $this->postVars[$name];
|
|
|
|
}
|
2008-10-06 00:16:07 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
function requestVar($name) {
|
|
|
|
if(isset($this->postVars[$name])) return $this->postVars[$name];
|
|
|
|
if(isset($this->getVars[$name])) return $this->getVars[$name];
|
|
|
|
}
|
2008-08-09 05:29:30 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
|
|
|
* Returns a possible file extension found in parsing the URL
|
|
|
|
* as denoted by a "."-character near the end of the URL.
|
|
|
|
* Doesn't necessarily have to belong to an existing file,
|
|
|
|
* for example used for {@link RestfulServer} content-type-switching.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
function getExtension() {
|
|
|
|
return $this->extension;
|
|
|
|
}
|
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
/**
|
|
|
|
* Add a HTTP header to the response, replacing any header of the same name.
|
|
|
|
*
|
|
|
|
* @param string $header Example: "Content-Type"
|
|
|
|
* @param string $value Example: "text/xml"
|
|
|
|
*/
|
|
|
|
function addHeader($header, $value) {
|
|
|
|
$this->headers[$header] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getHeaders() {
|
|
|
|
return $this->headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an existing HTTP header
|
|
|
|
*
|
|
|
|
* @param string $header
|
|
|
|
*/
|
|
|
|
function getHeader($header) {
|
|
|
|
return (isset($this->headers[$header])) ? $this->headers[$header] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an existing HTTP header by its name,
|
|
|
|
* e.g. "Content-Type".
|
|
|
|
*
|
|
|
|
* @param string $header
|
|
|
|
*/
|
|
|
|
function removeHeader($header) {
|
|
|
|
if(isset($this->headers[$header])) unset($this->headers[$header]);
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:29:30 +02:00
|
|
|
/**
|
|
|
|
* Enables the existence of a key-value pair in the request to be checked using
|
|
|
|
* array syntax, so isset($request['title']) will check for $_POST['title'] and $_GET['title]
|
|
|
|
*
|
|
|
|
* @param unknown_type $offset
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function offsetExists($offset) {
|
|
|
|
if(isset($this->postVars[$offset])) return true;
|
|
|
|
if(isset($this->getVars[$offset])) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access a request variable using array syntax. eg: $request['title'] instead of $request->postVar('title')
|
|
|
|
*
|
|
|
|
* @param unknown_type $offset
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function offsetGet($offset) {
|
|
|
|
return $this->requestVar($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
function offsetSet($offset, $value) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
function offsetUnset($offset) {}
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a HTTPRequest from a URL relative to the site root.
|
|
|
|
*/
|
2008-08-09 09:03:24 +02:00
|
|
|
function __construct($httpMethod, $url, $getVars = array(), $postVars = array(), $body = null) {
|
2008-08-09 05:19:54 +02:00
|
|
|
$this->httpMethod = $httpMethod;
|
|
|
|
|
|
|
|
$url = preg_replace(array('/\/+/','/^\//', '/\/$/'),array('/','',''), $url);
|
|
|
|
|
|
|
|
if(preg_match('/^(.*)\.([A-Za-z][A-Za-z0-9]*)$/', $url, $matches)) {
|
|
|
|
$url = $matches[1];
|
|
|
|
$this->extension = $matches[2];
|
|
|
|
}
|
|
|
|
if($url) $this->dirParts = split('/+', $url);
|
|
|
|
else $this->dirParts = array();
|
|
|
|
|
|
|
|
$this->getVars = (array)$getVars;
|
|
|
|
$this->postVars = (array)$postVars;
|
2008-08-09 09:03:24 +02:00
|
|
|
$this->body = $body;
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2008-10-03 04:23:11 +02:00
|
|
|
/**
|
|
|
|
* Construct an HTTPResponse that will deliver a file to the client
|
|
|
|
*/
|
|
|
|
static function send_file($fileData, $fileName, $mimeType = null) {
|
|
|
|
if(!$mimeType) $mimeType = HTTP::getMimeType($fileName);
|
|
|
|
|
|
|
|
$response = new HTTPResponse($fileData);
|
|
|
|
$response->addHeader("Content-Type", "$mimeType; name=\"" . addslashes($fileName) . "\"");
|
|
|
|
$response->addHeader("Content-disposition", "attachment; filename=" . addslashes($fileName));
|
|
|
|
$response->addHeader("Content-Length", strlen($fileData));
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Matches a URL pattern
|
|
|
|
* The pattern can contain a number of segments, separted by / (and an extension indicated by a .)
|
|
|
|
*
|
|
|
|
* The parts can be either literals, or, if they start with a $ they are interpreted as variables.
|
|
|
|
* - Literals must be provided in order to match
|
|
|
|
* - $Variables are optional
|
|
|
|
* - However, if you put ! at the end of a variable, then it becomes mandatory.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
* - admin/crm/list will match admin/crm/$Action/$ID/$OtherID, but it won't match admin/crm/$Action!/$ClassName!
|
|
|
|
*
|
|
|
|
* The pattern can optionally start with an HTTP method and a space. For example, "POST $Controller/$Action".
|
|
|
|
* This is used to define a rule that only matches on a specific HTTP method.
|
|
|
|
*/
|
|
|
|
function match($pattern, $shiftOnSuccess = false) {
|
|
|
|
// Check if a specific method is required
|
|
|
|
if(preg_match('/^([A-Za-z]+) +(.*)$/', $pattern, $matches)) {
|
|
|
|
$requiredMethod = $matches[1];
|
|
|
|
if($requiredMethod != $this->httpMethod) return false;
|
|
|
|
|
|
|
|
// If we get this far, we can match the URL pattern as usual.
|
|
|
|
$pattern = $matches[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case for the root URL controller
|
|
|
|
if(!$pattern) {
|
|
|
|
return ($this->dirParts == array()) ? array('Matched' => true) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the '//' marker that represents the "shifting point"
|
|
|
|
$doubleSlashPoint = strpos($pattern, '//');
|
|
|
|
if($doubleSlashPoint !== false) {
|
|
|
|
$shiftCount = substr_count($pattern, '/', 0, $doubleSlashPoint) + 1;
|
|
|
|
$pattern = str_replace('//', '/', $pattern);
|
|
|
|
$patternParts = explode('/', $pattern);
|
|
|
|
|
2008-09-16 22:37:46 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
} else {
|
|
|
|
$patternParts = explode('/', $pattern);
|
|
|
|
$shiftCount = sizeof($patternParts);
|
|
|
|
}
|
|
|
|
|
|
|
|
$matched = true;
|
|
|
|
$arguments = array();
|
|
|
|
foreach($patternParts as $i => $part) {
|
|
|
|
$part = trim($part);
|
2008-09-16 22:37:46 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// Match a variable
|
|
|
|
if(isset($part[0]) && $part[0] == '$') {
|
|
|
|
// A variable ending in ! is required
|
|
|
|
if(substr($part,-1) == '!') {
|
|
|
|
$varRequired = true;
|
|
|
|
$varName = substr($part,1,-1);
|
|
|
|
} else {
|
|
|
|
$varRequired = false;
|
|
|
|
$varName = substr($part,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if a required variable isn't populated
|
|
|
|
if($varRequired && !isset($this->dirParts[$i])) return false;
|
|
|
|
|
|
|
|
$arguments[$varName] = isset($this->dirParts[$i]) ? $this->dirParts[$i] : null;
|
|
|
|
if($part == '$Controller' && !class_exists($arguments['Controller'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-16 22:37:46 +02:00
|
|
|
|
|
|
|
// Literal parts with extension
|
|
|
|
} else if(isset($this->dirParts[$i]) && $this->dirParts[$i] . '.' . $this->extension == $part) {
|
|
|
|
continue;
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// Literal parts must always be there
|
|
|
|
} else if(!isset($this->dirParts[$i]) || $this->dirParts[$i] != $part) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-06 00:16:07 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2008-10-06 00:16:07 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
if($shiftOnSuccess) {
|
|
|
|
$this->shift($shiftCount);
|
|
|
|
// We keep track of pattern parts that we looked at but didn't shift off.
|
|
|
|
// This lets us say that we have *parsed* the whole URL even when we haven't *shifted* it all
|
|
|
|
$this->unshiftedButParsedParts = sizeof($patternParts) - $shiftCount;
|
|
|
|
}
|
2008-10-06 00:16:07 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
$this->latestParams = $arguments;
|
|
|
|
|
|
|
|
// Load the arguments that actually have a value into $this->allParams
|
|
|
|
// This ensures that previous values aren't overridden with blanks
|
|
|
|
foreach($arguments as $k => $v) {
|
2008-08-09 05:29:30 +02:00
|
|
|
if($v || !isset($this->allParams[$k])) $this->allParams[$k] = $v;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
if($arguments === array()) $arguments['_matched'] = true;
|
2008-08-09 05:19:54 +02:00
|
|
|
return $arguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
function allParams() {
|
|
|
|
return $this->allParams;
|
|
|
|
}
|
|
|
|
function latestParams() {
|
|
|
|
return $this->latestParams;
|
|
|
|
}
|
|
|
|
function latestParam($name) {
|
|
|
|
if(isset($this->latestParams[$name]))
|
|
|
|
return $this->latestParams[$name];
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
2008-10-06 00:16:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a named URL parameter (denoted by "$"-prefix in $url_handlers)
|
|
|
|
* from the full URL.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string Value of the URL parameter (if found)
|
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
function param($name) {
|
|
|
|
if(isset($this->allParams[$name]))
|
|
|
|
return $this->allParams[$name];
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
|
|
|
* Returns the unparsed part of the original URL
|
|
|
|
* separated by commas. This is used by {@link RequestHandlingData->handleRequest()}
|
|
|
|
* to determine if further URL processing is necessary.
|
|
|
|
*
|
|
|
|
* @return string Partial URL
|
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
function remaining() {
|
|
|
|
return implode("/", $this->dirParts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-08-09 05:54:55 +02:00
|
|
|
* Returns true if this is a URL that will match without shifting off any of the URL.
|
|
|
|
* This is used by the request handler to prevent infinite parsing loops.
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
|
|
|
function isEmptyPattern($pattern) {
|
|
|
|
if(preg_match('/^([A-Za-z]+) +(.*)$/', $pattern, $matches)) {
|
|
|
|
$pattern = $matches[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(trim($pattern) == "") return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shift one or more parts off the beginning of the URL.
|
|
|
|
* If you specify shifting more than 1 item off, then the items will be returned as an array
|
|
|
|
*/
|
|
|
|
function shift($count = 1) {
|
|
|
|
if($count == 1) return array_shift($this->dirParts);
|
|
|
|
else for($i=0;$i<$count;$i++) $return[] = array_shift($this->dirParts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the URL has been completely parsed.
|
|
|
|
* This will respect parsed but unshifted directory parts.
|
|
|
|
*/
|
|
|
|
function allParsed() {
|
|
|
|
return sizeof($this->dirParts) <= $this->unshiftedButParsedParts;
|
|
|
|
}
|
2008-08-11 02:14:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the client IP address which
|
|
|
|
* originated this request.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getIP() {
|
|
|
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
|
|
|
//check ip from share internet
|
|
|
|
return $_SERVER['HTTP_CLIENT_IP'];
|
|
|
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
|
|
//to check ip is pass from proxy
|
|
|
|
return $_SERVER['HTTP_X_FORWARDED_FOR'];
|
2008-08-13 03:43:49 +02:00
|
|
|
} elseif(isset($_SERVER['REMOTE_ADDR'])) {
|
2008-08-11 02:14:48 +02:00
|
|
|
return $_SERVER['REMOTE_ADDR'];
|
|
|
|
}
|
|
|
|
}
|
2008-08-11 04:57:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all mimetypes from the HTTP "Accept" header
|
|
|
|
* as an array.
|
|
|
|
*
|
|
|
|
* @param boolean $includeQuality Don't strip away optional "quality indicators", e.g. "application/xml;q=0.9" (Default: false)
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getAcceptMimetypes($includeQuality = false) {
|
|
|
|
$mimetypes = array();
|
|
|
|
$mimetypesWithQuality = explode(',',$this->getHeader('Accept'));
|
|
|
|
foreach($mimetypesWithQuality as $mimetypeWithQuality) {
|
|
|
|
$mimetypes[] = ($includeQuality) ? $mimetypeWithQuality : preg_replace('/;.*/', '', $mimetypeWithQuality);
|
|
|
|
}
|
|
|
|
return $mimetypes;
|
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|