2010-04-26 04:50:02 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A class that helps wrap Request information and particulars about a single request.
|
|
|
|
*
|
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)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
class CakeRequest {
|
|
|
|
/**
|
|
|
|
* Array of parameters parsed from the url.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $params = array();
|
|
|
|
|
|
|
|
/**
|
2010-04-26 05:33:40 +00:00
|
|
|
* Array of POST data. Will contain form data as well as uploaded files.
|
|
|
|
* Will only contain data from inputs that start with 'data'. So
|
|
|
|
* `<input name="some_input" />` will not end up in data. However,
|
|
|
|
* `<input name="data[something]" />`
|
2010-04-26 04:50:02 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $data = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of querystring arguments
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $url = array();
|
|
|
|
|
2010-04-27 03:23:35 +00:00
|
|
|
/**
|
|
|
|
* The built in detectors used with `is()` can be modified with `addDetector()`.
|
|
|
|
*
|
|
|
|
* @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'),
|
|
|
|
'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(
|
|
|
|
'Android',
|
|
|
|
'AvantGo',
|
|
|
|
'BlackBerry',
|
|
|
|
'DoCoMo',
|
|
|
|
'iPod',
|
|
|
|
'iPhone',
|
|
|
|
'J2ME',
|
|
|
|
'MIDP',
|
|
|
|
'NetFront',
|
|
|
|
'Nokia',
|
|
|
|
'Opera Mini',
|
|
|
|
'PalmOS',
|
|
|
|
'PalmSource',
|
|
|
|
'portalmmm',
|
|
|
|
'Plucker',
|
|
|
|
'ReqwirelessWeb',
|
|
|
|
'SonyEricsson',
|
|
|
|
'Symbian',
|
|
|
|
'UP\.Browser',
|
|
|
|
'webOS',
|
|
|
|
'Windows CE',
|
|
|
|
'Xiino'
|
|
|
|
))
|
|
|
|
);
|
2010-04-26 04:50:02 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
if (isset($_POST)) {
|
|
|
|
$this->_processPost();
|
|
|
|
}
|
|
|
|
if (isset($_GET)) {
|
|
|
|
$this->_processGet();
|
|
|
|
}
|
|
|
|
$this->_processFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* process the post data and set what is there into the object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _processPost() {
|
|
|
|
$this->params['form'] = $_POST;
|
|
|
|
if (ini_get('magic_quotes_gpc') === '1') {
|
|
|
|
$this->params['form'] = stripslashes_deep($this->params['form']);
|
|
|
|
}
|
|
|
|
if (env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
|
|
|
|
$this->params['form']['_method'] = env('HTTP_X_HTTP_METHOD_OVERRIDE');
|
|
|
|
}
|
|
|
|
if (isset($this->params['form']['_method'])) {
|
|
|
|
if (!empty($_SERVER)) {
|
|
|
|
$_SERVER['REQUEST_METHOD'] = $this->params['form']['_method'];
|
|
|
|
} else {
|
|
|
|
$_ENV['REQUEST_METHOD'] = $this->params['form']['_method'];
|
|
|
|
}
|
|
|
|
unset($this->params['form']['_method']);
|
|
|
|
}
|
2010-04-26 04:56:54 +00:00
|
|
|
if (isset($this->params['form']['data'])) {
|
|
|
|
$this->data = $this->params['form']['data'];
|
|
|
|
unset($this->params['form']['data']);
|
|
|
|
}
|
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') {
|
|
|
|
$url = stripslashes_deep($_GET);
|
|
|
|
} else {
|
|
|
|
$url = $_GET;
|
|
|
|
}
|
|
|
|
if (isset($this->params['url'])) {
|
2010-04-26 04:56:54 +00:00
|
|
|
$this->url = array_merge($this->url, $url);
|
2010-04-26 04:50:02 +00:00
|
|
|
} else {
|
2010-04-26 04:56:54 +00:00
|
|
|
$this->url = $url;
|
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) {
|
|
|
|
foreach ($data as $model => $fields) {
|
|
|
|
if (is_array($fields)) {
|
|
|
|
foreach ($fields as $field => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $k => $v) {
|
2010-04-26 05:20:05 +00:00
|
|
|
$this->data[$model][$field][$k][$key] = $v;
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-04-26 05:20:05 +00:00
|
|
|
$this->data[$model][$field][$key] = $value;
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-04-26 05:20:05 +00:00
|
|
|
$this->data[$model][$key] = $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
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getClientIp($safe = true) {
|
|
|
|
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');
|
|
|
|
$base = '';
|
|
|
|
if (defined('FULL_BASE_URL')) {
|
|
|
|
$base = FULL_BASE_URL;
|
|
|
|
}
|
|
|
|
if (!empty($ref)) {
|
|
|
|
if ($local && strpos($ref, $base) === 0) {
|
|
|
|
$ref = substr($ref, strlen($base));
|
|
|
|
if ($ref[0] != '/') {
|
|
|
|
$ref = '/' . $ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ref;
|
|
|
|
}
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
|
2010-04-27 03:23:35 +00:00
|
|
|
/**
|
|
|
|
* Check whether or not a Request is a certain type. Uses the built in detection rules
|
|
|
|
* as well as additional rules defined with CakeRequest::addDetector(). Any detector can be called
|
|
|
|
* with `is($type)` or `is$Type()`.
|
|
|
|
*
|
|
|
|
* @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];
|
|
|
|
if (isset($detect['env']) && isset($detect['value'])) {
|
|
|
|
return env($detect['env']) == $detect['value'];
|
|
|
|
}
|
|
|
|
if (isset($detect['env']) && isset($detect['pattern'])) {
|
|
|
|
return (bool)preg_match($detect['pattern'], env($detect['env']));
|
|
|
|
}
|
|
|
|
if (isset($detect['env']) && isset($detect['options'])) {
|
|
|
|
$pattern = '/' . implode('|', $detect['options']) . '/i';
|
|
|
|
return (bool)preg_match($pattern, env($detect['env']));
|
|
|
|
}
|
|
|
|
if (isset($detect['callback']) && is_callable($detect['callback'])) {
|
|
|
|
return call_user_func($detect['callback'], $this);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-26 04:50:02 +00:00
|
|
|
}
|