2008-08-09 05:19:54 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Control;
|
|
|
|
|
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\ORM\ArrayLib;
|
|
|
|
use ArrayAccess;
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* 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 RequestHandler} objects to decide what to do.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Caution: objects of this class are immutable, e.g. echo $request['a']; works as expected,
|
2014-03-05 01:21:54 +01:00
|
|
|
* but $request['a'] = '1'; has no effect.
|
2014-08-15 08:53:05 +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
|
|
|
* The intention is that a single SS_HTTPRequest object can be passed from one object to another, each object calling
|
2014-08-15 08:53:05 +02:00
|
|
|
* match() to get the information that they need out of the URL. This is generally handled by
|
2008-10-30 23:03:21 +01:00
|
|
|
* {@link RequestHandler::handleRequest()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +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-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
|
|
|
class SS_HTTPRequest implements ArrayAccess {
|
2008-10-06 00:16:07 +02:00
|
|
|
|
2008-10-14 20:47:38 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* @var string
|
2008-10-14 20:47:38 +02:00
|
|
|
*/
|
|
|
|
protected $url;
|
|
|
|
|
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.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
|
|
|
protected $dirParts;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* The URL extension (if present)
|
|
|
|
*
|
|
|
|
* @var string
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
|
|
|
protected $extension;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* The HTTP method in all uppercase: GET/PUT/POST/DELETE/HEAD
|
|
|
|
*
|
|
|
|
* @var string
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
|
|
|
protected $httpMethod;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* Contains alls HTTP GET parameters passed into this request.
|
|
|
|
*
|
|
|
|
* @var array
|
2008-10-06 00:16:07 +02:00
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
protected $getVars = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* Contains alls HTTP POST parameters passed into this request.
|
|
|
|
*
|
|
|
|
* @var array
|
2008-10-06 00:16:07 +02:00
|
|
|
*/
|
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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
/**
|
|
|
|
* Raw HTTP body, used by PUT and POST requests.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $body;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* Contains an associative array of all
|
2008-10-30 23:03:21 +01:00
|
|
|
* arguments matched in all calls to {@link RequestHandler->handleRequest()}.
|
2009-03-29 01:58:44 +01:00
|
|
|
* It's a "historical record" that's specific to the current call of
|
2008-10-06 16:58:01 +02:00
|
|
|
* {@link handleRequest()}, and is only complete once the "last call" to that method is made.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2008-10-06 00:16:07 +02:00
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
protected $allParams = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* Contains an associative array of all
|
2008-10-30 23:03:21 +01:00
|
|
|
* arguments matched in the current call from {@link RequestHandler->handleRequest()},
|
2008-10-06 16:58:01 +02:00
|
|
|
* as denoted with a "$"-prefix in the $url_handlers definitions.
|
2008-10-06 00:16:07 +02:00
|
|
|
* Contains different states throughout its lifespan, so just useful
|
2008-10-30 23:03:21 +01:00
|
|
|
* while processed in {@link RequestHandler} and to get the last
|
2008-10-06 00:16:07 +02:00
|
|
|
* processes arguments.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2008-10-06 00:16:07 +02:00
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
protected $latestParams = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-27 00:56:59 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* Contains an associative array of all arguments
|
2012-08-27 00:56:59 +02:00
|
|
|
* explicitly set in the route table for the current request.
|
|
|
|
* Useful for passing generic arguments via custom routes.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-08-27 00:56:59 +02:00
|
|
|
* E.g. The "Locale" parameter would be assigned "en_NZ" below
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-08-27 00:56:59 +02:00
|
|
|
* Director:
|
|
|
|
* rules:
|
|
|
|
* 'en_NZ/$URLSegment!//$Action/$ID/$OtherID':
|
|
|
|
* Controller: 'ModelAsController'
|
|
|
|
* Locale: 'en_NZ'
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2012-08-27 00:56:59 +02:00
|
|
|
*/
|
|
|
|
protected $routeParams = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-08-09 05:19:54 +02:00
|
|
|
protected $unshiftedButParsedParts = 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-15 02:53:09 +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
|
|
|
* Construct a SS_HTTPRequest from a URL relative to the site root.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param string $httpMethod
|
|
|
|
* @param string $url
|
|
|
|
* @param array $getVars
|
|
|
|
* @param array $postVars
|
|
|
|
* @param string $body
|
2008-10-15 02:53:09 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($httpMethod, $url, $getVars = array(), $postVars = array(), $body = null) {
|
2008-10-15 02:53:09 +02:00
|
|
|
$this->httpMethod = strtoupper(self::detect_method($httpMethod, $postVars));
|
2013-02-27 18:19:04 +01:00
|
|
|
$this->setUrl($url);
|
|
|
|
|
2015-07-27 09:17:37 +02:00
|
|
|
$this->getVars = (array) $getVars;
|
|
|
|
$this->postVars = (array) $postVars;
|
2013-02-27 18:19:04 +01:00
|
|
|
$this->body = $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow the setting of a URL
|
|
|
|
*
|
|
|
|
* This is here so that RootURLController can change the URL of the request
|
|
|
|
* without us loosing all the other info attached (like headers)
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $url The new URL
|
2013-02-27 18:19:04 +01:00
|
|
|
* @return SS_HTTPRequest The updated request
|
|
|
|
*/
|
|
|
|
public function setUrl($url) {
|
2008-10-15 02:53:09 +02:00
|
|
|
$this->url = $url;
|
2012-05-20 11:16:34 +02:00
|
|
|
|
|
|
|
// Normalize URL if its relative (strictly speaking), or has leading slashes
|
|
|
|
if(Director::is_relative_url($url) || preg_match('/^\//', $url)) {
|
2010-10-13 05:31:53 +02:00
|
|
|
$this->url = preg_replace(array('/\/+/','/^\//', '/\/$/'),array('/','',''), $this->url);
|
|
|
|
}
|
2008-10-15 02:53:09 +02:00
|
|
|
if(preg_match('/^(.*)\.([A-Za-z][A-Za-z0-9]*)$/', $this->url, $matches)) {
|
|
|
|
$this->url = $matches[1];
|
|
|
|
$this->extension = $matches[2];
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
if($this->url) {
|
|
|
|
$this->dirParts = preg_split('|/+|', $this->url);
|
|
|
|
} else {
|
|
|
|
$this->dirParts = array();
|
|
|
|
}
|
2013-02-27 18:19:04 +01:00
|
|
|
|
|
|
|
return $this;
|
2008-10-15 02:53:09 +02:00
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isGET() {
|
2008-08-09 05:54:55 +02:00
|
|
|
return $this->httpMethod == 'GET';
|
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isPOST() {
|
2008-08-09 05:54:55 +02:00
|
|
|
return $this->httpMethod == 'POST';
|
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isPUT() {
|
2008-08-09 05:54:55 +02:00
|
|
|
return $this->httpMethod == 'PUT';
|
|
|
|
}
|
|
|
|
|
2012-09-22 00:20:12 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isDELETE() {
|
2008-08-09 05:54:55 +02:00
|
|
|
return $this->httpMethod == 'DELETE';
|
2012-09-22 00:20:12 +02:00
|
|
|
}
|
2008-09-12 06:49:15 +02:00
|
|
|
|
2012-09-22 00:20:12 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isHEAD() {
|
2008-09-12 06:49:15 +02:00
|
|
|
return $this->httpMethod == 'HEAD';
|
2012-09-22 00:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $body
|
|
|
|
* @return SS_HTTPRequest $this
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setBody($body) {
|
2008-08-09 09:03:24 +02:00
|
|
|
$this->body = $body;
|
2012-09-22 00:20:12 +02:00
|
|
|
return $this;
|
2008-08-09 09:03:24 +02:00
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getBody() {
|
2008-08-09 09:03:24 +02:00
|
|
|
return $this->body;
|
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getVars() {
|
2008-08-09 05:19:54 +02:00
|
|
|
return $this->getVars;
|
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function postVars() {
|
2008-08-09 05:19:54 +02:00
|
|
|
return $this->postVars;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-06 00:16:07 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requestVars() {
|
2011-12-22 23:04:44 +01:00
|
|
|
return ArrayLib::array_merge_recursive($this->getVars, $this->postVars);
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getVar($name) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(isset($this->getVars[$name])) {
|
|
|
|
return $this->getVars[$name];
|
|
|
|
}
|
|
|
|
return null;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function postVar($name) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(isset($this->postVars[$name])) {
|
|
|
|
return $this->postVars[$name];
|
|
|
|
}
|
|
|
|
return null;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requestVar($name) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(isset($this->postVars[$name])) {
|
|
|
|
return $this->postVars[$name];
|
|
|
|
}
|
|
|
|
if(isset($this->getVars[$name])) {
|
|
|
|
return $this->getVars[$name];
|
|
|
|
}
|
|
|
|
return null;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +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,
|
2012-06-04 10:21:29 +02:00
|
|
|
* as extensions can be also used for content-type-switching.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-06 00:16:07 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getExtension() {
|
2008-08-09 05:54:55 +02:00
|
|
|
return $this->extension;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:02 +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
|
|
|
* Checks if the {@link SS_HTTPRequest->getExtension()} on this request matches one of the more common media types
|
2009-10-11 02:07:02 +02:00
|
|
|
* embedded into a webpage - e.g. css, png.
|
|
|
|
*
|
|
|
|
* This is useful for things like determining wether to display a fully rendered error page or not. Note that the
|
|
|
|
* media file types is not at all comprehensive.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isMedia() {
|
|
|
|
return in_array($this->getExtension(), array('css', 'js', 'jpg', 'jpeg', 'gif', 'png', 'bmp', 'ico'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
/**
|
|
|
|
* Add a HTTP header to the response, replacing any header of the same name.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-09 09:03:24 +02:00
|
|
|
* @param string $header Example: "Content-Type"
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $value Example: "text/xml"
|
2008-08-09 09:03:24 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function addHeader($header, $value) {
|
2008-08-09 09:03:24 +02:00
|
|
|
$this->headers[$header] = $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getHeaders() {
|
2008-08-09 09:03:24 +02:00
|
|
|
return $this->headers;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
/**
|
|
|
|
* Remove an existing HTTP header
|
|
|
|
*
|
|
|
|
* @param string $header
|
2012-09-22 00:20:12 +02:00
|
|
|
* @return mixed
|
2008-08-09 09:03:24 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getHeader($header) {
|
2014-08-15 08:53:05 +02:00
|
|
|
return (isset($this->headers[$header])) ? $this->headers[$header] : null;
|
2008-08-09 09:03:24 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
/**
|
|
|
|
* Remove an existing HTTP header by its name,
|
|
|
|
* e.g. "Content-Type".
|
|
|
|
*
|
|
|
|
* @param string $header
|
2012-09-22 00:20:12 +02:00
|
|
|
* @return SS_HTTPRequest $this
|
2008-08-09 09:03:24 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeHeader($header) {
|
2008-08-09 09:03:24 +02:00
|
|
|
if(isset($this->headers[$header])) unset($this->headers[$header]);
|
2012-09-22 00:20:12 +02:00
|
|
|
return $this;
|
2008-08-09 09:03:24 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-14 20:47:38 +02:00
|
|
|
/**
|
2012-06-29 12:02:30 +02:00
|
|
|
* Returns the URL used to generate the page
|
|
|
|
*
|
|
|
|
* @param bool $includeGetVars whether or not to include the get parameters\
|
2008-10-14 20:47:38 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getURL($includeGetVars = false) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$url = ($this->getExtension()) ? $this->url . '.' . $this->getExtension() : $this->url;
|
|
|
|
|
|
|
|
if ($includeGetVars) {
|
|
|
|
// if we don't unset $vars['url'] we end up with /my/url?url=my/url&foo=bar etc
|
2012-06-29 12:02:30 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$vars = $this->getVars();
|
|
|
|
unset($vars['url']);
|
2012-06-29 12:02:30 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
if (count($vars)) {
|
|
|
|
$url .= '?' . http_build_query($vars);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(strpos($url, "?") !== false) {
|
|
|
|
$url = substr($url, 0, strpos($url, "?"));
|
|
|
|
}
|
2012-06-29 12:02:30 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
return $url;
|
2008-10-14 20:47:38 +02:00
|
|
|
}
|
2012-04-05 14:44:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this request an ajax request,
|
|
|
|
* based on custom HTTP ajax added by common JavaScript libraries,
|
|
|
|
* or based on an explicit "ajax" request parameter.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-05 14:44:42 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isAjax() {
|
2012-04-05 14:44:42 +02:00
|
|
|
return (
|
|
|
|
$this->requestVar('ajax') ||
|
|
|
|
$this->getHeader('X-Requested-With') && $this->getHeader('X-Requested-With') == "XMLHttpRequest"
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:29:30 +02:00
|
|
|
/**
|
|
|
|
* Enables the existence of a key-value pair in the request to be checked using
|
2009-03-29 01:58:44 +01:00
|
|
|
* array syntax, so isset($request['title']) will check for $_POST['title'] and $_GET['title']
|
2008-08-09 05:29:30 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $offset
|
|
|
|
* @return bool
|
2008-08-09 05:29:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function offsetExists($offset) {
|
2016-08-19 00:51:35 +02:00
|
|
|
return isset($this->postVars[$offset]) || isset($this->getVars[$offset]);
|
2008-08-09 05:29:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:29:30 +02:00
|
|
|
/**
|
|
|
|
* Access a request variable using array syntax. eg: $request['title'] instead of $request->postVar('title')
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $offset
|
|
|
|
* @return mixed
|
2008-08-09 05:29:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function offsetGet($offset) {
|
2008-08-09 05:29:30 +02:00
|
|
|
return $this->requestVar($offset);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:29:30 +02:00
|
|
|
/**
|
|
|
|
* @ignore
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $offset
|
|
|
|
* @param mixed $value
|
2008-08-09 05:29:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function offsetSet($offset, $value) {}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:29:30 +02:00
|
|
|
/**
|
|
|
|
* @ignore
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param mixed $offset
|
2008-08-09 05:29:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function offsetUnset($offset) {}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 04:23:11 +02:00
|
|
|
/**
|
2013-04-05 13:50:17 +02:00
|
|
|
* Construct an SS_HTTPResponse that will deliver a file to the client.
|
|
|
|
* Caution: Since it requires $fileData to be passed as binary data (no stream support),
|
|
|
|
* it's only advisable to send small files through this method.
|
2012-09-22 00:20:12 +02:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param $fileData
|
|
|
|
* @param $fileName
|
|
|
|
* @param null $mimeType
|
|
|
|
* @return SS_HTTPResponse
|
2008-10-03 04:23:11 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function send_file($fileData, $fileName, $mimeType = null) {
|
2012-05-11 01:33:21 +02:00
|
|
|
if(!$mimeType) {
|
|
|
|
$mimeType = HTTP::get_mime_type($fileName);
|
|
|
|
}
|
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
|
|
|
$response = new SS_HTTPResponse($fileData);
|
2008-10-03 04:23:11 +02:00
|
|
|
$response->addHeader("Content-Type", "$mimeType; name=\"" . addslashes($fileName) . "\"");
|
2013-08-23 05:04:54 +02:00
|
|
|
// Note a IE-only fix that inspects this header in HTTP::add_cache_headers().
|
2014-08-13 18:28:36 +02:00
|
|
|
$response->addHeader("Content-Disposition", "attachment; filename=" . addslashes($fileName));
|
2008-10-03 04:23:11 +02:00
|
|
|
$response->addHeader("Content-Length", strlen($fileData));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 04:23:11 +02:00
|
|
|
return $response;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Matches a URL pattern
|
2009-03-29 01:58:44 +01:00
|
|
|
* The pattern can contain a number of segments, separated by / (and an extension indicated by a .)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-09 05:19:54 +02:00
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-09 05:19:54 +02:00
|
|
|
* For example:
|
|
|
|
* - admin/crm/list will match admin/crm/$Action/$ID/$OtherID, but it won't match admin/crm/$Action!/$ClassName!
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-09 05:19:54 +02:00
|
|
|
* 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.
|
2012-09-22 00:20:12 +02:00
|
|
|
*
|
|
|
|
* @param $pattern
|
|
|
|
* @param bool $shiftOnSuccess
|
|
|
|
* @return array|bool
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function match($pattern, $shiftOnSuccess = false) {
|
2008-08-09 05:19:54 +02:00
|
|
|
// Check if a specific method is required
|
|
|
|
if(preg_match('/^([A-Za-z]+) +(.*)$/', $pattern, $matches)) {
|
|
|
|
$requiredMethod = $matches[1];
|
|
|
|
if($requiredMethod != $this->httpMethod) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// If we get this far, we can match the URL pattern as usual.
|
|
|
|
$pattern = $matches[2];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// 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) {
|
2009-07-01 01:21:50 +02:00
|
|
|
$shiftCount = substr_count(substr($pattern,0,$doubleSlashPoint), '/') + 1;
|
2008-08-09 05:19:54 +02:00
|
|
|
$pattern = str_replace('//', '/', $pattern);
|
|
|
|
$patternParts = explode('/', $pattern);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
} else {
|
|
|
|
$patternParts = explode('/', $pattern);
|
|
|
|
$shiftCount = sizeof($patternParts);
|
|
|
|
}
|
|
|
|
|
2013-04-29 06:13:37 +02:00
|
|
|
// Filter out any "empty" matching parts - either from an initial / or a trailing /
|
|
|
|
$patternParts = array_values(array_filter($patternParts));
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
$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);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// Fail if a required variable isn't populated
|
|
|
|
if($varRequired && !isset($this->dirParts[$i])) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
|
|
|
$key = "Controller";
|
2008-08-09 05:19:54 +02:00
|
|
|
$arguments[$varName] = isset($this->dirParts[$i]) ? $this->dirParts[$i] : null;
|
2016-08-19 00:51:35 +02:00
|
|
|
if( $part == '$Controller'
|
|
|
|
&& (
|
|
|
|
!ClassInfo::exists($arguments[$key])
|
|
|
|
|| !is_subclass_of($arguments[$key], 'SilverStripe\\Control\\Controller')
|
|
|
|
)
|
|
|
|
) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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;
|
|
|
|
}
|
2014-08-15 08:53:05 +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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// 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-10-06 16:58:01 +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;
|
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function allParams() {
|
2008-08-09 05:19:54 +02:00
|
|
|
return $this->allParams;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:07 +02:00
|
|
|
/**
|
|
|
|
* Shift all the parameter values down a key space, and return the shifted value.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function shiftAllParams() {
|
2012-12-08 12:20:20 +01:00
|
|
|
$keys = array_keys($this->allParams);
|
|
|
|
$values = array_values($this->allParams);
|
|
|
|
$value = array_shift($values);
|
2009-12-18 06:19:28 +01:00
|
|
|
|
|
|
|
// push additional unparsed URL parts onto the parameter stack
|
|
|
|
if(array_key_exists($this->unshiftedButParsedParts, $this->dirParts)) {
|
|
|
|
$values[] = $this->dirParts[$this->unshiftedButParsedParts];
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:07 +02:00
|
|
|
foreach($keys as $position => $key) {
|
|
|
|
$this->allParams[$key] = isset($values[$position]) ? $values[$position] : null;
|
|
|
|
}
|
2009-12-18 06:19:28 +01:00
|
|
|
|
2009-10-11 02:07:07 +02:00
|
|
|
return $value;
|
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function latestParams() {
|
2008-08-09 05:19:54 +02:00
|
|
|
return $this->latestParams;
|
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function latestParam($name) {
|
2012-08-27 00:56:59 +02:00
|
|
|
if(isset($this->latestParams[$name])) return $this->latestParams[$name];
|
|
|
|
else return null;
|
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function routeParams() {
|
2012-08-27 00:56:59 +02:00
|
|
|
return $this->routeParams;
|
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $params
|
|
|
|
* @return SS_HTTPRequest $this
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setRouteParams($params) {
|
2012-08-27 00:56:59 +02:00
|
|
|
$this->routeParams = $params;
|
2012-09-22 00:20:12 +02:00
|
|
|
return $this;
|
2012-08-27 00:56:59 +02:00
|
|
|
}
|
2012-09-22 00:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function params() {
|
2012-08-27 00:56:59 +02:00
|
|
|
return array_merge($this->allParams, $this->routeParams);
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
|
|
|
* Finds a named URL parameter (denoted by "$"-prefix in $url_handlers)
|
2012-08-27 00:56:59 +02:00
|
|
|
* from the full URL, or a parameter specified in the route table
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-06 00:16:07 +02:00
|
|
|
* @param string $name
|
|
|
|
* @return string Value of the URL parameter (if found)
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function param($name) {
|
2012-08-27 00:56:59 +02:00
|
|
|
$params = $this->params();
|
|
|
|
if(isset($params[$name])) return $params[$name];
|
2008-12-04 23:38:32 +01:00
|
|
|
else return null;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 00:16:07 +02:00
|
|
|
/**
|
|
|
|
* Returns the unparsed part of the original URL
|
2008-10-30 23:03:21 +01:00
|
|
|
* separated by commas. This is used by {@link RequestHandler->handleRequest()}
|
2008-10-06 00:16:07 +02:00
|
|
|
* to determine if further URL processing is necessary.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-06 00:16:07 +02:00
|
|
|
* @return string Partial URL
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function remaining() {
|
2008-08-09 05:19:54 +02:00
|
|
|
return implode("/", $this->dirParts);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
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.
|
2012-09-22 00:20:12 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $pattern
|
2012-09-22 00:20:12 +02:00
|
|
|
* @return bool
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isEmptyPattern($pattern) {
|
2008-08-09 05:19:54 +02:00
|
|
|
if(preg_match('/^([A-Za-z]+) +(.*)$/', $pattern, $matches)) {
|
|
|
|
$pattern = $matches[2];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if(trim($pattern) == "") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2010-10-15 05:08:39 +02:00
|
|
|
*
|
|
|
|
* @param int $count Shift Count
|
2016-08-19 00:51:35 +02:00
|
|
|
* @return string|array
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function shift($count = 1) {
|
2010-10-15 05:08:39 +02:00
|
|
|
$return = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
if($count == 1) return array_shift($this->dirParts);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:08:39 +02:00
|
|
|
for($i=0;$i<$count;$i++) {
|
|
|
|
$value = array_shift($this->dirParts);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-02-27 18:19:04 +01:00
|
|
|
if($value === null) break;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:08:39 +02:00
|
|
|
$return[] = $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:08:39 +02:00
|
|
|
return $return;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2008-10-06 16:58:01 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the URL has been completely parsed.
|
|
|
|
* This will respect parsed but unshifted directory parts.
|
2012-09-22 00:20:12 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function allParsed() {
|
2008-08-09 05:19:54 +02:00
|
|
|
return sizeof($this->dirParts) <= $this->unshiftedButParsedParts;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 02:14:48 +02:00
|
|
|
/**
|
|
|
|
* Returns the client IP address which
|
|
|
|
* originated this request.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getIP() {
|
2016-02-17 11:19:19 +01:00
|
|
|
$headerOverrideIP = null;
|
|
|
|
if(TRUSTED_PROXY) {
|
|
|
|
$headers = (defined('SS_TRUSTED_PROXY_IP_HEADER')) ? array(SS_TRUSTED_PROXY_IP_HEADER) : null;
|
|
|
|
if(!$headers) {
|
|
|
|
// Backwards compatible defaults
|
|
|
|
$headers = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR');
|
|
|
|
}
|
|
|
|
foreach($headers as $header) {
|
|
|
|
if(!empty($_SERVER[$header])) {
|
|
|
|
$headerOverrideIP = $_SERVER[$header];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-08-11 02:14:48 +02:00
|
|
|
}
|
2016-02-17 11:19:19 +01:00
|
|
|
|
2016-02-25 02:51:59 +01:00
|
|
|
if ($headerOverrideIP && filter_var($headerOverrideIP, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
|
2016-03-01 13:56:34 +01:00
|
|
|
return $this->getIPFromHeaderValue($headerOverrideIP);
|
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'];
|
2016-02-17 11:19:19 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
2015-02-24 03:39:59 +01:00
|
|
|
}
|
2008-08-11 02:14:48 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-01 13:56:34 +01:00
|
|
|
/**
|
|
|
|
* Extract an IP address from a header value that has been obtained. Accepts single IP or comma separated string of
|
|
|
|
* IPs
|
|
|
|
*
|
|
|
|
* @param string $headerValue The value from a trusted header
|
|
|
|
* @return string The IP address
|
|
|
|
*/
|
|
|
|
protected function getIPFromHeaderValue($headerValue) {
|
|
|
|
if (strpos($headerValue, ',') !== false) {
|
|
|
|
//sometimes the IP from a load balancer could be "x.x.x.x, y.y.y.y, z.z.z.z" so we need to find the most
|
|
|
|
// likely candidate
|
|
|
|
$ips = explode(',', $headerValue);
|
|
|
|
foreach ($ips as $ip) {
|
|
|
|
$ip = trim($ip);
|
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
|
|
|
|
return $ip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $headerValue;
|
|
|
|
}
|
|
|
|
|
2008-08-11 04:57:59 +02:00
|
|
|
/**
|
|
|
|
* Returns all mimetypes from the HTTP "Accept" header
|
|
|
|
* as an array.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param boolean $includeQuality Don't strip away optional "quality indicators", e.g. "application/xml;q=0.9"
|
|
|
|
* (Default: false)
|
2008-08-11 04:57:59 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAcceptMimetypes($includeQuality = false) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$mimetypes = array();
|
|
|
|
$mimetypesWithQuality = explode(',',$this->getHeader('Accept'));
|
|
|
|
foreach($mimetypesWithQuality as $mimetypeWithQuality) {
|
|
|
|
$mimetypes[] = ($includeQuality) ? $mimetypeWithQuality : preg_replace('/;.*/', '', $mimetypeWithQuality);
|
|
|
|
}
|
|
|
|
return $mimetypes;
|
2008-08-11 04:57:59 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 16:58:01 +02:00
|
|
|
/**
|
|
|
|
* @return string HTTP method (all uppercase)
|
|
|
|
*/
|
|
|
|
public function httpMethod() {
|
|
|
|
return $this->httpMethod;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 16:58:01 +02:00
|
|
|
/**
|
|
|
|
* Gets the "real" HTTP method for a request.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-06 16:58:01 +02:00
|
|
|
* Used to work around browser limitations of form
|
|
|
|
* submissions to GET and POST, by overriding the HTTP method
|
|
|
|
* with a POST parameter called "_method" for PUT, DELETE, HEAD.
|
|
|
|
* Using GET for the "_method" override is not supported,
|
|
|
|
* as GET should never carry out state changes.
|
|
|
|
* Alternatively you can use a custom HTTP header 'X-HTTP-Method-Override'
|
2014-08-15 08:53:05 +02:00
|
|
|
* to override the original method in {@link Director::direct()}.
|
2008-10-06 16:58:01 +02:00
|
|
|
* The '_method' POST parameter overrules the custom HTTP header.
|
|
|
|
*
|
|
|
|
* @param string $origMethod Original HTTP method from the browser request
|
|
|
|
* @param array $postVars
|
|
|
|
* @return string HTTP method (all uppercase)
|
|
|
|
*/
|
|
|
|
public static function detect_method($origMethod, $postVars) {
|
|
|
|
if(isset($postVars['_method'])) {
|
|
|
|
if(!in_array(strtoupper($postVars['_method']), array('GET','POST','PUT','DELETE','HEAD'))) {
|
|
|
|
user_error('Director::direct(): Invalid "_method" parameter', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
return strtoupper($postVars['_method']);
|
|
|
|
} else {
|
|
|
|
return $origMethod;
|
|
|
|
}
|
|
|
|
}
|
2009-10-11 02:07:02 +02:00
|
|
|
}
|