2010-04-26 04:50:02 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* CakeRequest
|
2010-04-26 04:50:02 +00:00
|
|
|
*
|
2010-04-27 02:33:56 +00:00
|
|
|
* PHP 5
|
2010-04-26 04:50:02 +00:00
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-04-26 04:50:02 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-04-26 04:50:02 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2010-04-30 03:08:01 +00:00
|
|
|
|
2010-12-07 03:45:18 +00:00
|
|
|
App::uses('Set', 'Utility');
|
2010-04-30 03:08:01 +00:00
|
|
|
|
2010-09-12 18:05:57 +00:00
|
|
|
/**
|
|
|
|
* A class that helps wrap Request information and particulars about a single request.
|
|
|
|
* Provides methods commonly used to introspect on the request headers and request body.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2010-09-12 18:05:57 +00:00
|
|
|
* Has both an Array and Object interface. You can access framework parameters using indexes:
|
|
|
|
*
|
|
|
|
* `$request['controller']` or `$request->controller`.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Network
|
2010-09-12 18:05:57 +00:00
|
|
|
*/
|
2010-04-27 03:46:47 +00:00
|
|
|
class CakeRequest implements ArrayAccess {
|
2012-03-04 16:18:20 +00:00
|
|
|
|
2010-04-26 04:50:02 +00:00
|
|
|
/**
|
|
|
|
* Array of parameters parsed from the url.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-10-22 22:48:31 +00:00
|
|
|
public $params = array(
|
|
|
|
'plugin' => null,
|
|
|
|
'controller' => null,
|
|
|
|
'action' => null,
|
2012-04-13 13:58:45 +00:00
|
|
|
'named' => array(),
|
|
|
|
'pass' => array(),
|
2011-10-22 22:48:31 +00:00
|
|
|
);
|
2010-04-26 04:50:02 +00:00
|
|
|
|
|
|
|
/**
|
2010-04-26 05:33:40 +00:00
|
|
|
* Array of POST data. Will contain form data as well as uploaded files.
|
2011-06-01 01:48:09 +00:00
|
|
|
* Inputs prefixed with 'data' will have the data prefix removed. If there is
|
|
|
|
* overlap between an input prefixed with data and one without, the 'data' prefixed
|
|
|
|
* value will take precedence.
|
2010-04-26 04:50:02 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $data = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of querystring arguments
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-30 03:20:31 +00:00
|
|
|
public $query = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The url string used for the request.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $url;
|
2010-04-26 04:50:02 +00:00
|
|
|
|
2010-04-30 04:13:24 +00:00
|
|
|
/**
|
|
|
|
* Base url path.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $base = false;
|
|
|
|
|
|
|
|
/**
|
2010-05-01 04:32:43 +00:00
|
|
|
* webroot path segment for the request.
|
2010-04-30 04:13:24 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $webroot = '/';
|
|
|
|
|
2010-05-01 05:43:06 +00:00
|
|
|
/**
|
|
|
|
* The full address to the current request
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $here = null;
|
|
|
|
|
2010-04-27 03:23:35 +00:00
|
|
|
/**
|
|
|
|
* The built in detectors used with `is()` can be modified with `addDetector()`.
|
|
|
|
*
|
2011-08-16 03:55:08 +00:00
|
|
|
* There are several ways to specify a detector, see CakeRequest::addDetector() for the
|
2010-04-27 03:34:30 +00:00
|
|
|
* various formats and ways to define detectors.
|
|
|
|
*
|
2010-04-27 03:23:35 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_detectors = array(
|
|
|
|
'get' => array('env' => 'REQUEST_METHOD', 'value' => 'GET'),
|
|
|
|
'post' => array('env' => 'REQUEST_METHOD', 'value' => 'POST'),
|
|
|
|
'put' => array('env' => 'REQUEST_METHOD', 'value' => 'PUT'),
|
|
|
|
'delete' => array('env' => 'REQUEST_METHOD', 'value' => 'DELETE'),
|
|
|
|
'head' => array('env' => 'REQUEST_METHOD', 'value' => 'HEAD'),
|
2010-06-29 04:24:29 +00:00
|
|
|
'options' => array('env' => 'REQUEST_METHOD', 'value' => 'OPTIONS'),
|
2010-04-27 03:23:35 +00:00
|
|
|
'ssl' => array('env' => 'HTTPS', 'value' => 1),
|
|
|
|
'ajax' => array('env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'),
|
|
|
|
'flash' => array('env' => 'HTTP_USER_AGENT', 'pattern' => '/^(Shockwave|Adobe) Flash/'),
|
|
|
|
'mobile' => array('env' => 'HTTP_USER_AGENT', 'options' => array(
|
2012-01-10 02:33:13 +00:00
|
|
|
'Android', 'AvantGo', 'BlackBerry', 'DoCoMo', 'Fennec', 'iPod', 'iPhone', 'iPad',
|
2011-10-25 01:12:06 +00:00
|
|
|
'J2ME', 'MIDP', 'NetFront', 'Nokia', 'Opera Mini', 'Opera Mobi', 'PalmOS', 'PalmSource',
|
2010-05-02 03:46:10 +00:00
|
|
|
'portalmmm', 'Plucker', 'ReqwirelessWeb', 'SonyEricsson', 'Symbian', 'UP\\.Browser',
|
2012-01-02 02:29:13 +00:00
|
|
|
'webOS', 'Windows CE', 'Windows Phone OS', 'Xiino'
|
2011-10-21 01:11:42 +00:00
|
|
|
)),
|
|
|
|
'requested' => array('param' => 'requested', 'value' => 1)
|
2010-04-27 03:23:35 +00:00
|
|
|
);
|
2011-05-01 13:59:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy of php://input. Since this stream can only be read once in most SAPI's
|
|
|
|
* keep a copy of it so users don't need to know about that detail.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_input = '';
|
2011-05-01 13:59:47 +00:00
|
|
|
|
2010-04-26 04:50:02 +00:00
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* Constructor
|
2010-04-26 04:50:02 +00:00
|
|
|
*
|
2010-09-12 18:05:57 +00:00
|
|
|
* @param string $url Trimmed url string to use. Should not contain the application base path.
|
2010-05-04 02:50:38 +00:00
|
|
|
* @param boolean $parseEnvironment Set to false to not auto parse the environment. ie. GET, POST and FILES.
|
2010-04-26 04:50:02 +00:00
|
|
|
*/
|
2010-05-04 02:50:38 +00:00
|
|
|
public function __construct($url = null, $parseEnvironment = true) {
|
2011-02-19 18:17:05 +00:00
|
|
|
$this->_base();
|
2010-04-30 04:13:24 +00:00
|
|
|
if (empty($url)) {
|
2010-05-01 03:37:16 +00:00
|
|
|
$url = $this->_url();
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
2010-05-02 22:04:13 +00:00
|
|
|
if ($url[0] == '/') {
|
2010-05-02 02:45:31 +00:00
|
|
|
$url = substr($url, 1);
|
|
|
|
}
|
2010-04-30 04:13:24 +00:00
|
|
|
$this->url = $url;
|
2010-05-01 15:04:41 +00:00
|
|
|
|
2010-05-04 02:50:38 +00:00
|
|
|
if ($parseEnvironment) {
|
|
|
|
$this->_processPost();
|
|
|
|
$this->_processGet();
|
|
|
|
$this->_processFiles();
|
|
|
|
}
|
2010-05-01 05:43:06 +00:00
|
|
|
$this->here = $this->base . '/' . $this->url;
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* process the post data and set what is there into the object.
|
2012-02-19 03:18:16 +00:00
|
|
|
* processed data is available at `$this->data`
|
|
|
|
*
|
2012-02-22 21:49:42 +00:00
|
|
|
* Will merge POST vars prefixed with `data`, and ones without
|
2012-02-19 03:18:16 +00:00
|
|
|
* into a single array. Variables prefixed with `data` will overwrite those without.
|
|
|
|
*
|
|
|
|
* If you have mixed POST values be careful not to make any top level keys numeric
|
2012-03-11 01:57:18 +00:00
|
|
|
* containing arrays. Hash::merge() is used to merge data, and it has possibly
|
2012-02-19 03:18:16 +00:00
|
|
|
* unexpected behavior in this situation.
|
2010-04-26 04:50:02 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _processPost() {
|
2012-06-06 20:58:27 +00:00
|
|
|
if ($_POST) {
|
|
|
|
$this->data = $_POST;
|
2012-06-18 00:03:45 +00:00
|
|
|
} elseif ($this->is('put') || $this->is('delete')) {
|
2012-06-06 20:58:27 +00:00
|
|
|
$this->data = $this->_readInput();
|
2012-08-14 16:40:33 +00:00
|
|
|
if (strpos(env('CONTENT_TYPE'), 'application/x-www-form-urlencoded') === 0) {
|
2012-06-06 20:58:27 +00:00
|
|
|
parse_str($this->data, $this->data);
|
|
|
|
}
|
|
|
|
}
|
2010-04-26 04:50:02 +00:00
|
|
|
if (ini_get('magic_quotes_gpc') === '1') {
|
2011-04-30 18:12:51 +00:00
|
|
|
$this->data = stripslashes_deep($this->data);
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
|
|
|
if (env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
|
2011-04-30 18:12:51 +00:00
|
|
|
$this->data['_method'] = env('HTTP_X_HTTP_METHOD_OVERRIDE');
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
2012-07-02 22:18:12 +00:00
|
|
|
$isArray = is_array($this->data);
|
|
|
|
if ($isArray && isset($this->data['_method'])) {
|
2010-04-26 04:50:02 +00:00
|
|
|
if (!empty($_SERVER)) {
|
2011-04-30 18:12:51 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = $this->data['_method'];
|
2010-04-26 04:50:02 +00:00
|
|
|
} else {
|
2011-04-30 18:12:51 +00:00
|
|
|
$_ENV['REQUEST_METHOD'] = $this->data['_method'];
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
2011-04-30 18:12:51 +00:00
|
|
|
unset($this->data['_method']);
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
2012-07-02 22:18:12 +00:00
|
|
|
if ($isArray && isset($this->data['data'])) {
|
2011-04-29 02:42:50 +00:00
|
|
|
$data = $this->data['data'];
|
2012-03-07 01:22:13 +00:00
|
|
|
if (count($this->data) <= 1) {
|
|
|
|
$this->data = $data;
|
|
|
|
} else {
|
|
|
|
unset($this->data['data']);
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->data = Hash::merge($this->data, $data);
|
2012-03-07 01:22:13 +00:00
|
|
|
}
|
2010-04-26 04:56:54 +00:00
|
|
|
}
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the GET parameters and move things into the object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _processGet() {
|
|
|
|
if (ini_get('magic_quotes_gpc') === '1') {
|
2010-05-01 15:04:41 +00:00
|
|
|
$query = stripslashes_deep($_GET);
|
2010-04-26 04:50:02 +00:00
|
|
|
} else {
|
2010-05-01 15:04:41 +00:00
|
|
|
$query = $_GET;
|
|
|
|
}
|
2011-02-21 02:52:20 +00:00
|
|
|
|
2012-02-18 10:35:52 +00:00
|
|
|
unset($query['/' . str_replace('.', '_', urldecode($this->url))]);
|
2010-05-01 15:04:41 +00:00
|
|
|
if (strpos($this->url, '?') !== false) {
|
|
|
|
list(, $querystr) = explode('?', $this->url);
|
|
|
|
parse_str($querystr, $queryArgs);
|
2011-02-21 02:52:20 +00:00
|
|
|
$query += $queryArgs;
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
|
|
|
if (isset($this->params['url'])) {
|
2010-05-01 15:04:41 +00:00
|
|
|
$query = array_merge($this->params['url'], $query);
|
2010-04-30 03:20:31 +00:00
|
|
|
}
|
2010-05-01 15:04:41 +00:00
|
|
|
$this->query = $query;
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-19 23:20:54 +00:00
|
|
|
* Get the request uri. Looks in PATH_INFO first, as this is the exact value we need prepared
|
|
|
|
* by PHP. Following that, REQUEST_URI, PHP_SELF, HTTP_X_REWRITE_URL and argv are checked in that order.
|
|
|
|
* Each of these server variables have the base path, and query strings stripped off
|
2010-04-30 04:13:24 +00:00
|
|
|
*
|
2011-02-19 23:20:54 +00:00
|
|
|
* @return string URI The CakePHP request path that is being accessed.
|
2010-04-30 04:13:24 +00:00
|
|
|
*/
|
2011-02-20 03:26:12 +00:00
|
|
|
protected function _url() {
|
2011-02-22 03:30:01 +00:00
|
|
|
if (!empty($_SERVER['PATH_INFO'])) {
|
|
|
|
return $_SERVER['PATH_INFO'];
|
|
|
|
} elseif (isset($_SERVER['REQUEST_URI'])) {
|
|
|
|
$uri = $_SERVER['REQUEST_URI'];
|
|
|
|
} elseif (isset($_SERVER['PHP_SELF']) && isset($_SERVER['SCRIPT_NAME'])) {
|
|
|
|
$uri = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['PHP_SELF']);
|
|
|
|
} elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
|
|
|
|
$uri = $_SERVER['HTTP_X_REWRITE_URL'];
|
|
|
|
} elseif ($var = env('argv')) {
|
|
|
|
$uri = $var[0];
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
2011-02-22 03:30:01 +00:00
|
|
|
|
2011-02-19 23:20:54 +00:00
|
|
|
$base = $this->base;
|
2010-05-01 04:18:17 +00:00
|
|
|
|
2011-02-26 20:37:08 +00:00
|
|
|
if (strlen($base) > 0 && strpos($uri, $base) === 0) {
|
2011-02-19 23:20:54 +00:00
|
|
|
$uri = substr($uri, strlen($base));
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
2011-02-19 23:20:54 +00:00
|
|
|
if (strpos($uri, '?') !== false) {
|
2011-12-09 02:48:40 +00:00
|
|
|
list($uri) = explode('?', $uri, 2);
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
|
|
|
if (empty($uri) || $uri == '/' || $uri == '//') {
|
2011-02-19 23:20:54 +00:00
|
|
|
return '/';
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
2011-02-19 23:20:54 +00:00
|
|
|
return $uri;
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a base URL and sets the proper webroot
|
|
|
|
*
|
|
|
|
* @return string Base URL
|
|
|
|
*/
|
2010-05-01 04:32:43 +00:00
|
|
|
protected function _base() {
|
2010-04-30 04:13:24 +00:00
|
|
|
$dir = $webroot = null;
|
|
|
|
$config = Configure::read('App');
|
|
|
|
extract($config);
|
|
|
|
|
2011-10-04 19:16:25 +00:00
|
|
|
if (!isset($base)) {
|
2010-04-30 04:13:24 +00:00
|
|
|
$base = $this->base;
|
|
|
|
}
|
|
|
|
if ($base !== false) {
|
|
|
|
$this->webroot = $base . '/';
|
2011-02-05 17:20:09 +00:00
|
|
|
return $this->base = $base;
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
2011-02-22 02:58:30 +00:00
|
|
|
|
2010-04-30 04:13:24 +00:00
|
|
|
if (!$baseUrl) {
|
2012-01-05 02:10:29 +00:00
|
|
|
$base = dirname(env('PHP_SELF'));
|
2010-04-30 04:13:24 +00:00
|
|
|
|
|
|
|
if ($webroot === 'webroot' && $webroot === basename($base)) {
|
|
|
|
$base = dirname($base);
|
|
|
|
}
|
|
|
|
if ($dir === 'app' && $dir === basename($base)) {
|
|
|
|
$base = dirname($base);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($base === DS || $base === '.') {
|
|
|
|
$base = '';
|
|
|
|
}
|
|
|
|
|
2012-03-04 16:18:20 +00:00
|
|
|
$this->webroot = $base . '/';
|
2011-02-19 18:17:05 +00:00
|
|
|
return $this->base = $base;
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$file = '/' . basename($baseUrl);
|
|
|
|
$base = dirname($baseUrl);
|
|
|
|
|
|
|
|
if ($base === DS || $base === '.') {
|
|
|
|
$base = '';
|
|
|
|
}
|
2011-02-05 17:20:09 +00:00
|
|
|
$this->webroot = $base . '/';
|
|
|
|
|
|
|
|
$docRoot = env('DOCUMENT_ROOT');
|
|
|
|
$docRootContainsWebroot = strpos($docRoot, $dir . '/' . $webroot);
|
2010-04-30 04:13:24 +00:00
|
|
|
|
2011-02-05 17:20:09 +00:00
|
|
|
if (!empty($base) || !$docRootContainsWebroot) {
|
2011-12-03 04:39:33 +00:00
|
|
|
if (strpos($this->webroot, '/' . $dir . '/') === false) {
|
2012-03-04 16:18:20 +00:00
|
|
|
$this->webroot .= $dir . '/';
|
2010-04-30 04:13:24 +00:00
|
|
|
}
|
2011-12-03 04:39:33 +00:00
|
|
|
if (strpos($this->webroot, '/' . $webroot . '/') === false) {
|
2010-04-30 04:13:24 +00:00
|
|
|
$this->webroot .= $webroot . '/';
|
|
|
|
}
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
2011-02-19 18:17:05 +00:00
|
|
|
return $this->base = $base . $file;
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process $_FILES and move things into the object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _processFiles() {
|
|
|
|
if (isset($_FILES) && is_array($_FILES)) {
|
|
|
|
foreach ($_FILES as $name => $data) {
|
|
|
|
if ($name != 'data') {
|
|
|
|
$this->params['form'][$name] = $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_FILES['data'])) {
|
|
|
|
foreach ($_FILES['data'] as $key => $data) {
|
2012-04-18 02:28:08 +00:00
|
|
|
$this->_processFileData('', $data, $key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively walks the FILES array restructuring the data
|
|
|
|
* into something sane and useable.
|
|
|
|
*
|
|
|
|
* @param string $path The dot separated path to insert $data into.
|
|
|
|
* @param array $data The data to traverse/insert.
|
|
|
|
* @param string $field The terminal field name, which is the top level key in $_FILES.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _processFileData($path, $data, $field) {
|
|
|
|
foreach ($data as $key => $fields) {
|
|
|
|
$newPath = $key;
|
|
|
|
if (!empty($path)) {
|
|
|
|
$newPath = $path . '.' . $key;
|
|
|
|
}
|
|
|
|
if (is_array($fields)) {
|
|
|
|
$this->_processFileData($newPath, $fields, $field);
|
|
|
|
} else {
|
|
|
|
$newPath .= '.' . $field;
|
|
|
|
$this->data = Set::insert($this->data, $newPath, $fields);
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-26 05:08:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the IP the client is using, or says they are using.
|
|
|
|
*
|
|
|
|
* @param boolean $safe Use safe = false when you think the user might manipulate their HTTP_CLIENT_IP
|
|
|
|
* header. Setting $safe = false will will also look at HTTP_X_FORWARDED_FOR
|
2010-09-14 03:06:15 +00:00
|
|
|
* @return string The client IP.
|
2010-04-26 05:08:33 +00:00
|
|
|
*/
|
2010-05-01 15:08:58 +00:00
|
|
|
public function clientIp($safe = true) {
|
2010-04-26 05:08:33 +00:00
|
|
|
if (!$safe && env('HTTP_X_FORWARDED_FOR') != null) {
|
|
|
|
$ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR'));
|
|
|
|
} else {
|
|
|
|
if (env('HTTP_CLIENT_IP') != null) {
|
|
|
|
$ipaddr = env('HTTP_CLIENT_IP');
|
|
|
|
} else {
|
|
|
|
$ipaddr = env('REMOTE_ADDR');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env('HTTP_CLIENTADDRESS') != null) {
|
|
|
|
$tmpipaddr = env('HTTP_CLIENTADDRESS');
|
|
|
|
|
|
|
|
if (!empty($tmpipaddr)) {
|
|
|
|
$ipaddr = preg_replace('/(?:,.*)/', '', $tmpipaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trim($ipaddr);
|
|
|
|
}
|
2010-04-27 02:33:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the referer that referred this request.
|
|
|
|
*
|
|
|
|
* @param boolean $local Attempt to return a local address. Local addresses do not contain hostnames.
|
|
|
|
* @return string The referring address for this request.
|
|
|
|
*/
|
|
|
|
public function referer($local = false) {
|
|
|
|
$ref = env('HTTP_REFERER');
|
2010-05-08 04:36:03 +00:00
|
|
|
$forwarded = env('HTTP_X_FORWARDED_HOST');
|
|
|
|
if ($forwarded) {
|
|
|
|
$ref = $forwarded;
|
|
|
|
}
|
|
|
|
|
2010-04-27 02:33:56 +00:00
|
|
|
$base = '';
|
|
|
|
if (defined('FULL_BASE_URL')) {
|
2010-05-15 04:53:15 +00:00
|
|
|
$base = FULL_BASE_URL . $this->webroot;
|
2010-04-27 02:33:56 +00:00
|
|
|
}
|
2010-05-15 04:53:15 +00:00
|
|
|
if (!empty($ref) && !empty($base)) {
|
2010-04-27 02:33:56 +00:00
|
|
|
if ($local && strpos($ref, $base) === 0) {
|
|
|
|
$ref = substr($ref, strlen($base));
|
|
|
|
if ($ref[0] != '/') {
|
|
|
|
$ref = '/' . $ref;
|
|
|
|
}
|
2010-05-15 04:53:15 +00:00
|
|
|
return $ref;
|
|
|
|
} elseif (!$local) {
|
|
|
|
return $ref;
|
2010-04-27 02:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
|
2010-04-27 03:34:30 +00:00
|
|
|
/**
|
|
|
|
* Missing method handler, handles wrapping older style isAjax() type methods
|
|
|
|
*
|
2010-04-27 03:46:47 +00:00
|
|
|
* @param string $name The method called
|
|
|
|
* @param array $params Array of parameters for the method call
|
|
|
|
* @return mixed
|
2011-07-31 20:55:52 +00:00
|
|
|
* @throws CakeException when an invalid method is called.
|
2010-04-27 03:34:30 +00:00
|
|
|
*/
|
|
|
|
public function __call($name, $params) {
|
|
|
|
if (strpos($name, 'is') === 0) {
|
|
|
|
$type = strtolower(substr($name, 2));
|
|
|
|
return $this->is($type);
|
|
|
|
}
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'Method %s does not exist', $name));
|
2010-04-27 03:34:30 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 03:37:36 +00:00
|
|
|
/**
|
|
|
|
* Magic get method allows access to parsed routing parameters directly on the object.
|
|
|
|
*
|
2010-08-28 04:59:07 +00:00
|
|
|
* Allows access to `$this->params['controller']` via `$this->controller`
|
|
|
|
*
|
2010-04-27 03:46:47 +00:00
|
|
|
* @param string $name The property being accessed.
|
|
|
|
* @return mixed Either the value of the parameter or null.
|
2010-04-27 03:37:36 +00:00
|
|
|
*/
|
|
|
|
public function __get($name) {
|
|
|
|
if (isset($this->params[$name])) {
|
|
|
|
return $this->params[$name];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-11-17 01:20:50 +00:00
|
|
|
/**
|
|
|
|
* Magic isset method allows isset/empty checks
|
|
|
|
* on routing parameters.
|
|
|
|
*
|
|
|
|
* @param string $name The property being accessed.
|
|
|
|
* @return bool Existence
|
|
|
|
*/
|
|
|
|
public function __isset($name) {
|
|
|
|
return isset($this->params[$name]);
|
|
|
|
}
|
|
|
|
|
2010-04-27 03:23:35 +00:00
|
|
|
/**
|
|
|
|
* Check whether or not a Request is a certain type. Uses the built in detection rules
|
2011-08-16 03:55:08 +00:00
|
|
|
* as well as additional rules defined with CakeRequest::addDetector(). Any detector can be called
|
2010-08-28 04:59:07 +00:00
|
|
|
* as `is($type)` or `is$Type()`.
|
2010-04-27 03:23:35 +00:00
|
|
|
*
|
|
|
|
* @param string $type The type of request you want to check.
|
|
|
|
* @return boolean Whether or not the request is the type you are checking.
|
|
|
|
*/
|
|
|
|
public function is($type) {
|
|
|
|
$type = strtolower($type);
|
|
|
|
if (!isset($this->_detectors[$type])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$detect = $this->_detectors[$type];
|
2010-04-27 03:34:30 +00:00
|
|
|
if (isset($detect['env'])) {
|
|
|
|
if (isset($detect['value'])) {
|
|
|
|
return env($detect['env']) == $detect['value'];
|
|
|
|
}
|
|
|
|
if (isset($detect['pattern'])) {
|
|
|
|
return (bool)preg_match($detect['pattern'], env($detect['env']));
|
|
|
|
}
|
|
|
|
if (isset($detect['options'])) {
|
|
|
|
$pattern = '/' . implode('|', $detect['options']) . '/i';
|
|
|
|
return (bool)preg_match($pattern, env($detect['env']));
|
|
|
|
}
|
2010-04-27 03:23:35 +00:00
|
|
|
}
|
2011-10-21 01:11:42 +00:00
|
|
|
if (isset($detect['param'])) {
|
|
|
|
$key = $detect['param'];
|
|
|
|
$value = $detect['value'];
|
|
|
|
return isset($this->params[$key]) ? $this->params[$key] == $value : false;
|
|
|
|
}
|
2010-04-27 03:23:35 +00:00
|
|
|
if (isset($detect['callback']) && is_callable($detect['callback'])) {
|
|
|
|
return call_user_func($detect['callback'], $this);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-29 04:33:41 +00:00
|
|
|
/**
|
|
|
|
* Add a new detector to the list of detectors that a request can use.
|
|
|
|
* There are several different formats and types of detectors that can be set.
|
|
|
|
*
|
|
|
|
* ### Environment value comparison
|
|
|
|
*
|
|
|
|
* An environment value comparison, compares a value fetched from `env()` to a known value
|
|
|
|
* the environment value is equality checked against the provided value.
|
|
|
|
*
|
|
|
|
* e.g `addDetector('post', array('env' => 'REQUEST_METHOD', 'value' => 'POST'))`
|
|
|
|
*
|
|
|
|
* ### Pattern value comparison
|
|
|
|
*
|
|
|
|
* Pattern value comparison allows you to compare a value fetched from `env()` to a regular expression.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2010-07-31 18:43:21 +00:00
|
|
|
* e.g `addDetector('iphone', array('env' => 'HTTP_USER_AGENT', 'pattern' => '/iPhone/i'));`
|
2010-04-29 04:33:41 +00:00
|
|
|
*
|
|
|
|
* ### Option based comparison
|
|
|
|
*
|
|
|
|
* Option based comparisons use a list of options to create a regular expression. Subsequent calls
|
|
|
|
* to add an already defined options detector will merge the options.
|
|
|
|
*
|
|
|
|
* e.g `addDetector('mobile', array('env' => 'HTTP_USER_AGENT', 'options' => array('Fennec')));`
|
|
|
|
*
|
|
|
|
* ### Callback detectors
|
|
|
|
*
|
|
|
|
* Callback detectors allow you to provide a 'callback' type to handle the check. The callback will
|
2011-12-02 05:58:09 +00:00
|
|
|
* receive the request object as its only parameter.
|
2010-04-29 04:33:41 +00:00
|
|
|
*
|
|
|
|
* e.g `addDetector('custom', array('callback' => array('SomeClass', 'somemethod')));`
|
|
|
|
*
|
2011-10-21 01:11:42 +00:00
|
|
|
* ### Request parameter detectors
|
|
|
|
*
|
|
|
|
* Allows for custom detectors on the request parameters.
|
|
|
|
*
|
|
|
|
* e.g `addDetector('post', array('param' => 'requested', 'value' => 1)`
|
|
|
|
*
|
2010-04-29 04:33:41 +00:00
|
|
|
* @param string $name The name of the detector.
|
|
|
|
* @param array $options The options for the detector definition. See above.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addDetector($name, $options) {
|
2012-02-28 16:05:24 +00:00
|
|
|
$name = strtolower($name);
|
2010-04-30 03:08:01 +00:00
|
|
|
if (isset($this->_detectors[$name]) && isset($options['options'])) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$options = Hash::merge($this->_detectors[$name], $options);
|
2010-04-30 03:08:01 +00:00
|
|
|
}
|
|
|
|
$this->_detectors[$name] = $options;
|
2010-04-29 04:33:41 +00:00
|
|
|
}
|
|
|
|
|
2010-05-01 15:04:41 +00:00
|
|
|
/**
|
2010-12-22 03:36:37 +00:00
|
|
|
* Add parameters to the request's parsed parameter set. This will overwrite any existing parameters.
|
|
|
|
* This modifies the parameters available through `$request->params`.
|
2010-05-01 15:04:41 +00:00
|
|
|
*
|
|
|
|
* @param array $params Array of parameters to merge in
|
2010-05-02 05:22:34 +00:00
|
|
|
* @return The current object, you can chain this method.
|
2010-05-01 15:04:41 +00:00
|
|
|
*/
|
|
|
|
public function addParams($params) {
|
2011-05-05 03:08:10 +00:00
|
|
|
$this->params = array_merge($this->params, (array)$params);
|
2010-05-02 05:22:34 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-28 04:59:07 +00:00
|
|
|
* Add paths to the requests' paths vars. This will overwrite any existing paths.
|
2010-12-22 03:36:37 +00:00
|
|
|
* Provides an easy way to modify, here, webroot and base.
|
2010-05-02 05:22:34 +00:00
|
|
|
*
|
|
|
|
* @param array $paths Array of paths to merge in
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeRequest the current object, you can chain this method.
|
2010-05-02 05:22:34 +00:00
|
|
|
*/
|
|
|
|
public function addPaths($paths) {
|
|
|
|
foreach (array('webroot', 'here', 'base') as $element) {
|
|
|
|
if (isset($paths[$element])) {
|
|
|
|
$this->{$element} = $paths[$element];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this;
|
2010-05-01 15:04:41 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 02:46:28 +00:00
|
|
|
/**
|
|
|
|
* Get the value of the current requests url. Will include named parameters and querystring arguments.
|
|
|
|
*
|
|
|
|
* @param boolean $base Include the base path, set to false to trim the base path off.
|
|
|
|
* @return string the current request url including query string args.
|
|
|
|
*/
|
|
|
|
public function here($base = true) {
|
|
|
|
$url = $this->here;
|
|
|
|
if (!empty($this->query)) {
|
2012-02-24 14:35:50 +00:00
|
|
|
$url .= '?' . http_build_query($this->query, null, '&');
|
2011-03-03 02:46:28 +00:00
|
|
|
}
|
|
|
|
if (!$base) {
|
|
|
|
$url = preg_replace('/^' . preg_quote($this->base, '/') . '/', '', $url, 1);
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2010-06-29 04:24:29 +00:00
|
|
|
/**
|
2010-07-01 16:00:53 +00:00
|
|
|
* Read an HTTP header from the Request information.
|
2010-06-29 04:24:29 +00:00
|
|
|
*
|
|
|
|
* @param string $name Name of the header you want.
|
|
|
|
* @return mixed Either false on no header being set or the value of the header.
|
|
|
|
*/
|
2010-11-07 05:35:36 +00:00
|
|
|
public static function header($name) {
|
2010-06-29 04:24:29 +00:00
|
|
|
$name = 'HTTP_' . strtoupper(str_replace('-', '_', $name));
|
|
|
|
if (!empty($_SERVER[$name])) {
|
|
|
|
return $_SERVER[$name];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-01 03:25:45 +00:00
|
|
|
|
2010-08-22 04:15:13 +00:00
|
|
|
/**
|
|
|
|
* Get the HTTP method used for this request.
|
2011-08-16 03:55:08 +00:00
|
|
|
* There are a few ways to specify a method.
|
2011-04-29 02:42:50 +00:00
|
|
|
*
|
|
|
|
* - If your client supports it you can use native HTTP methods.
|
2011-08-16 03:55:08 +00:00
|
|
|
* - You can set the HTTP-X-Method-Override header.
|
2011-04-29 02:42:50 +00:00
|
|
|
* - You can submit an input with the name `_method`
|
|
|
|
*
|
|
|
|
* Any of these 3 approaches can be used to set the HTTP method used
|
|
|
|
* by CakePHP internally, and will effect the result of this method.
|
2010-08-22 04:15:13 +00:00
|
|
|
*
|
|
|
|
* @return string The name of the HTTP method used.
|
|
|
|
*/
|
|
|
|
public function method() {
|
|
|
|
return env('REQUEST_METHOD');
|
|
|
|
}
|
|
|
|
|
2010-08-22 04:44:05 +00:00
|
|
|
/**
|
|
|
|
* Get the host that the request was handled on.
|
|
|
|
*
|
2012-05-23 23:52:29 +00:00
|
|
|
* @return string
|
2010-08-22 04:44:05 +00:00
|
|
|
*/
|
|
|
|
public function host() {
|
|
|
|
return env('HTTP_HOST');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the domain name and include $tldLength segments of the tld.
|
|
|
|
*
|
2011-08-16 03:55:08 +00:00
|
|
|
* @param integer $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld.
|
2011-03-03 02:00:53 +00:00
|
|
|
* While `example.co.uk` contains 2.
|
2010-08-22 04:44:05 +00:00
|
|
|
* @return string Domain name without subdomains.
|
|
|
|
*/
|
2010-11-07 05:36:37 +00:00
|
|
|
public function domain($tldLength = 1) {
|
2010-08-22 04:44:05 +00:00
|
|
|
$segments = explode('.', $this->host());
|
|
|
|
$domain = array_slice($segments, -1 * ($tldLength + 1));
|
|
|
|
return implode('.', $domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the subdomains for a host.
|
|
|
|
*
|
2011-08-16 03:55:08 +00:00
|
|
|
* @param integer $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld.
|
2011-03-03 02:00:53 +00:00
|
|
|
* While `example.co.uk` contains 2.
|
2010-08-22 04:44:05 +00:00
|
|
|
* @return array of subdomains.
|
|
|
|
*/
|
2010-11-07 05:36:37 +00:00
|
|
|
public function subdomains($tldLength = 1) {
|
2010-08-22 04:44:05 +00:00
|
|
|
$segments = explode('.', $this->host());
|
|
|
|
return array_slice($segments, 0, -1 * ($tldLength + 1));
|
|
|
|
}
|
|
|
|
|
2010-07-01 03:25:45 +00:00
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* Find out which content types the client accepts or check if they accept a
|
2010-07-01 03:25:45 +00:00
|
|
|
* particular type of content.
|
|
|
|
*
|
2010-08-28 04:59:07 +00:00
|
|
|
* #### Get all types:
|
|
|
|
*
|
2010-12-22 03:36:37 +00:00
|
|
|
* `$this->request->accepts();`
|
2010-08-28 04:59:07 +00:00
|
|
|
*
|
|
|
|
* #### Check for a single type:
|
|
|
|
*
|
2012-06-26 20:08:07 +00:00
|
|
|
* `$this->request->accepts('application/json');`
|
2010-08-28 04:59:07 +00:00
|
|
|
*
|
2011-08-27 15:39:22 +00:00
|
|
|
* This method will order the returned content types by the preference values indicated
|
|
|
|
* by the client.
|
|
|
|
*
|
2010-07-01 03:25:45 +00:00
|
|
|
* @param string $type The content type to check for. Leave null to get all types a client accepts.
|
|
|
|
* @return mixed Either an array of all the types the client accepts or a boolean if they accept the
|
|
|
|
* provided type.
|
|
|
|
*/
|
|
|
|
public function accepts($type = null) {
|
2011-08-31 23:15:27 +00:00
|
|
|
$raw = $this->parseAccept();
|
|
|
|
$accept = array();
|
|
|
|
foreach ($raw as $value => $types) {
|
|
|
|
$accept = array_merge($accept, $types);
|
|
|
|
}
|
|
|
|
if ($type === null) {
|
|
|
|
return $accept;
|
|
|
|
}
|
|
|
|
return in_array($type, $accept);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the HTTP_ACCEPT header and return a sorted array with content types
|
|
|
|
* as the keys, and pref values as the values.
|
2011-10-04 19:16:25 +00:00
|
|
|
*
|
2011-08-31 23:15:27 +00:00
|
|
|
* Generally you want to use CakeRequest::accept() to get a simple list
|
|
|
|
* of the accepted content types.
|
|
|
|
*
|
|
|
|
* @return array An array of prefValue => array(content/types)
|
|
|
|
*/
|
|
|
|
public function parseAccept() {
|
2011-08-27 15:39:22 +00:00
|
|
|
$accept = array();
|
|
|
|
$header = explode(',', $this->header('accept'));
|
2011-08-31 23:15:27 +00:00
|
|
|
foreach (array_filter($header) as $value) {
|
2011-08-27 15:39:22 +00:00
|
|
|
$prefPos = strpos($value, ';');
|
|
|
|
if ($prefPos !== false) {
|
2011-08-31 23:15:27 +00:00
|
|
|
$prefValue = substr($value, strpos($value, '=') + 1);
|
2011-08-27 15:39:22 +00:00
|
|
|
$value = trim(substr($value, 0, $prefPos));
|
|
|
|
} else {
|
2011-08-31 23:15:27 +00:00
|
|
|
$prefValue = '1.0';
|
2011-08-27 15:39:22 +00:00
|
|
|
$value = trim($value);
|
|
|
|
}
|
2011-08-31 23:15:27 +00:00
|
|
|
if (!isset($accept[$prefValue])) {
|
|
|
|
$accept[$prefValue] = array();
|
|
|
|
}
|
2011-08-27 15:39:22 +00:00
|
|
|
if ($prefValue) {
|
2011-08-31 23:15:27 +00:00
|
|
|
$accept[$prefValue][] = $value;
|
2010-07-01 03:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-31 23:15:27 +00:00
|
|
|
krsort($accept);
|
|
|
|
return $accept;
|
2010-07-01 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
2010-11-07 05:35:36 +00:00
|
|
|
/**
|
2011-09-05 10:23:26 +00:00
|
|
|
* Get the languages accepted by the client, or check if a specific language is accepted.
|
2010-11-07 05:35:36 +00:00
|
|
|
*
|
2010-12-22 03:36:37 +00:00
|
|
|
* Get the list of accepted languages:
|
|
|
|
*
|
|
|
|
* {{{ CakeRequest::acceptLanguage(); }}}
|
|
|
|
*
|
|
|
|
* Check if a specific language is accepted:
|
|
|
|
*
|
|
|
|
* {{{ CakeRequest::acceptLanguage('es-es'); }}}
|
|
|
|
*
|
2010-11-07 05:35:36 +00:00
|
|
|
* @param string $language The language to test.
|
|
|
|
* @return If a $language is provided, a boolean. Otherwise the array of accepted languages.
|
|
|
|
*/
|
|
|
|
public static function acceptLanguage($language = null) {
|
|
|
|
$accepts = preg_split('/[;,]/', self::header('Accept-Language'));
|
|
|
|
foreach ($accepts as &$accept) {
|
|
|
|
$accept = strtolower($accept);
|
|
|
|
if (strpos($accept, '_') !== false) {
|
|
|
|
$accept = str_replace('_', '-', $accept);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($language === null) {
|
|
|
|
return $accepts;
|
|
|
|
}
|
|
|
|
return in_array($language, $accepts);
|
|
|
|
}
|
|
|
|
|
2010-09-12 18:05:57 +00:00
|
|
|
/**
|
|
|
|
* Provides a read/write accessor for `$this->data`. Allows you
|
|
|
|
* to use a syntax similar to `CakeSession` for reading post data.
|
|
|
|
*
|
|
|
|
* ## Reading values.
|
|
|
|
*
|
|
|
|
* `$request->data('Post.title');`
|
|
|
|
*
|
|
|
|
* When reading values you will get `null` for keys/values that do not exist.
|
|
|
|
*
|
|
|
|
* ## Writing values
|
|
|
|
*
|
|
|
|
* `$request->data('Post.title', 'New post!');`
|
|
|
|
*
|
|
|
|
* You can write to any value, even paths/keys that do not exist, and the arrays
|
|
|
|
* will be created for you.
|
|
|
|
*
|
2011-08-22 01:45:34 +00:00
|
|
|
* @param string $name,... Dot separated name of the value to read/write
|
2010-09-12 18:05:57 +00:00
|
|
|
* @return mixed Either the value being read, or this so you can chain consecutive writes.
|
|
|
|
*/
|
|
|
|
public function data($name) {
|
|
|
|
$args = func_get_args();
|
|
|
|
if (count($args) == 2) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->data = Hash::insert($this->data, $name, $args[1]);
|
2010-09-12 18:05:57 +00:00
|
|
|
return $this;
|
|
|
|
}
|
2012-03-11 01:57:18 +00:00
|
|
|
return Hash::get($this->data, $name);
|
2010-09-12 18:05:57 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 03:03:17 +00:00
|
|
|
/**
|
2011-09-22 02:30:02 +00:00
|
|
|
* Read data from `php://input`. Useful when interacting with XML or JSON
|
2011-04-27 03:03:17 +00:00
|
|
|
* request body content.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2011-04-30 17:05:10 +00:00
|
|
|
* Getting input with a decoding function:
|
|
|
|
*
|
|
|
|
* `$this->request->input('json_decode');`
|
|
|
|
*
|
|
|
|
* Getting input using a decoding function, and additional params:
|
|
|
|
*
|
|
|
|
* `$this->request->input('Xml::build', array('return' => 'DOMDocument'));`
|
|
|
|
*
|
|
|
|
* Any additional parameters are applied to the callback in the order they are given.
|
2011-04-27 03:03:17 +00:00
|
|
|
*
|
|
|
|
* @param string $callback A decoding callback that will convert the string data to another
|
2011-04-30 17:05:10 +00:00
|
|
|
* representation. Leave empty to access the raw input data. You can also
|
|
|
|
* supply additional parameters for the decoding callback using var args, see above.
|
2011-04-27 03:03:17 +00:00
|
|
|
* @return The decoded/processed request data.
|
|
|
|
*/
|
|
|
|
public function input($callback = null) {
|
2011-09-25 01:56:37 +00:00
|
|
|
$input = $this->_readInput();
|
2011-04-30 17:05:10 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
if (!empty($args)) {
|
|
|
|
$callback = array_shift($args);
|
|
|
|
array_unshift($args, $input);
|
|
|
|
return call_user_func_array($callback, $args);
|
2011-04-27 03:03:17 +00:00
|
|
|
}
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2012-08-09 14:11:32 +00:00
|
|
|
/**
|
|
|
|
* Only allow certain HTTP request methods, if the request method does not match
|
|
|
|
* a 405 error will be shown and the required "Allow" response header will be set.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* $this->request->onlyAllow('post', 'delete');
|
|
|
|
* or
|
|
|
|
* $this->request->onlyAllow(array('post', 'delete'));
|
|
|
|
*
|
|
|
|
* If the request would be GET, response header "Allow: POST, DELETE" will be set
|
|
|
|
* and a 405 error will be returned
|
|
|
|
*
|
|
|
|
* @param string|array $methods Allowed HTTP request methods
|
|
|
|
* @return boolean true
|
|
|
|
* @throws MethodNotAllowedException
|
|
|
|
*/
|
|
|
|
public function onlyAllow($methods) {
|
|
|
|
if (!is_array($methods)) {
|
|
|
|
$methods = func_get_args();
|
|
|
|
}
|
|
|
|
foreach ($methods as $method) {
|
|
|
|
if ($this->is($method)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$allowed = strtoupper(implode(', ', $methods));
|
|
|
|
$e = new MethodNotAllowedException();
|
|
|
|
$e->responseHeader('Allow', $allowed);
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:03:17 +00:00
|
|
|
/**
|
2011-09-22 02:30:02 +00:00
|
|
|
* Read data from php://input, mocked in tests.
|
2011-04-27 03:03:17 +00:00
|
|
|
*
|
2011-09-25 01:56:37 +00:00
|
|
|
* @return string contents of php://input
|
2011-04-27 03:03:17 +00:00
|
|
|
*/
|
2011-09-25 01:56:37 +00:00
|
|
|
protected function _readInput() {
|
2011-08-20 04:43:34 +00:00
|
|
|
if (empty($this->_input)) {
|
2011-05-01 13:59:47 +00:00
|
|
|
$fh = fopen('php://input', 'r');
|
|
|
|
$content = stream_get_contents($fh);
|
|
|
|
fclose($fh);
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_input = $content;
|
2011-05-01 13:59:47 +00:00
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
return $this->_input;
|
2011-04-27 03:03:17 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 03:46:47 +00:00
|
|
|
/**
|
|
|
|
* Array access read implementation
|
|
|
|
*
|
|
|
|
* @param string $name Name of the key being accessed.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function offsetGet($name) {
|
|
|
|
if (isset($this->params[$name])) {
|
|
|
|
return $this->params[$name];
|
|
|
|
}
|
2010-05-01 15:04:41 +00:00
|
|
|
if ($name == 'url') {
|
|
|
|
return $this->query;
|
|
|
|
}
|
2010-05-02 03:46:10 +00:00
|
|
|
if ($name == 'data') {
|
|
|
|
return $this->data;
|
|
|
|
}
|
2010-04-27 03:46:47 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array access write implementation
|
|
|
|
*
|
|
|
|
* @param string $name Name of the key being written
|
|
|
|
* @param mixed $value The value being written.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function offsetSet($name, $value) {
|
|
|
|
$this->params[$name] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array access isset() implementation
|
|
|
|
*
|
|
|
|
* @param string $name thing to check.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function offsetExists($name) {
|
|
|
|
return isset($this->params[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array access unset() implementation
|
|
|
|
*
|
2011-07-29 03:56:10 +00:00
|
|
|
* @param string $name Name to unset.
|
2010-04-27 03:46:47 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function offsetUnset($name) {
|
|
|
|
unset($this->params[$name]);
|
|
|
|
}
|
2012-03-04 16:18:20 +00:00
|
|
|
|
2011-04-27 03:03:17 +00:00
|
|
|
}
|