Renamed slim directory to avoid error on loading.
This commit is contained in:
181
src/html/api/Slim/Http/Headers.php
Normal file
181
src/html/api/Slim/Http/Headers.php
Normal file
@ -0,0 +1,181 @@
|
||||
<?php
|
||||
/**
|
||||
* Slim - a micro PHP 5 framework
|
||||
*
|
||||
* @author Josh Lockhart <info@slimframework.com>
|
||||
* @copyright 2011 Josh Lockhart
|
||||
* @link http://www.slimframework.com
|
||||
* @license http://www.slimframework.com/license
|
||||
* @version 2.2.0
|
||||
* @package Slim
|
||||
*
|
||||
* MIT LICENSE
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
namespace Slim\Http;
|
||||
|
||||
/**
|
||||
* HTTP Headers
|
||||
*
|
||||
* This class is an abstraction of the HTTP response headers and
|
||||
* provides array access to the header list while automatically
|
||||
* stores and retrieves headers with lowercase canonical keys regardless
|
||||
* of the input format.
|
||||
*
|
||||
* This class also implements the `Iterator` and `Countable`
|
||||
* interfaces for even more convenient usage.
|
||||
*
|
||||
* @package Slim
|
||||
* @author Josh Lockhart
|
||||
* @since 1.6.0
|
||||
*/
|
||||
class Headers implements \ArrayAccess, \Iterator, \Countable
|
||||
{
|
||||
/**
|
||||
* @var array HTTP headers
|
||||
*/
|
||||
protected $headers;
|
||||
|
||||
/**
|
||||
* @var array Map canonical header name to original header name
|
||||
*/
|
||||
protected $map;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param array $headers
|
||||
*/
|
||||
public function __construct($headers = array())
|
||||
{
|
||||
$this->merge($headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge Headers
|
||||
* @param array $headers
|
||||
*/
|
||||
public function merge($headers)
|
||||
{
|
||||
foreach ($headers as $name => $value) {
|
||||
$this[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform header name into canonical form
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
protected function canonical($name)
|
||||
{
|
||||
return strtolower(trim($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Access: Offset Exists
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->headers[$this->canonical($offset)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Access: Offset Get
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
$canonical = $this->canonical($offset);
|
||||
if (isset($this->headers[$canonical])) {
|
||||
return $this->headers[$canonical];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Access: Offset Set
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$canonical = $this->canonical($offset);
|
||||
$this->headers[$canonical] = $value;
|
||||
$this->map[$canonical] = $offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Access: Offset Unset
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
$canonical = $this->canonical($offset);
|
||||
unset($this->headers[$canonical], $this->map[$canonical]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Countable: Count
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: Rewind
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
reset($this->headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: Current
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return current($this->headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: Key
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
$key = key($this->headers);
|
||||
|
||||
return $this->map[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: Next
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
return next($this->headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: Valid
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return current($this->headers) !== false;
|
||||
}
|
||||
}
|
585
src/html/api/Slim/Http/Request.php
Normal file
585
src/html/api/Slim/Http/Request.php
Normal file
@ -0,0 +1,585 @@
|
||||
<?php
|
||||
/**
|
||||
* Slim - a micro PHP 5 framework
|
||||
*
|
||||
* @author Josh Lockhart <info@slimframework.com>
|
||||
* @copyright 2011 Josh Lockhart
|
||||
* @link http://www.slimframework.com
|
||||
* @license http://www.slimframework.com/license
|
||||
* @version 2.2.0
|
||||
* @package Slim
|
||||
*
|
||||
* MIT LICENSE
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
namespace Slim\Http;
|
||||
|
||||
/**
|
||||
* Slim HTTP Request
|
||||
*
|
||||
* This class provides a human-friendly interface to the Slim environment variables;
|
||||
* environment variables are passed by reference and will be modified directly.
|
||||
*
|
||||
* @package Slim
|
||||
* @author Josh Lockhart
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Request
|
||||
{
|
||||
const METHOD_HEAD = 'HEAD';
|
||||
const METHOD_GET = 'GET';
|
||||
const METHOD_POST = 'POST';
|
||||
const METHOD_PUT = 'PUT';
|
||||
const METHOD_DELETE = 'DELETE';
|
||||
const METHOD_OPTIONS = 'OPTIONS';
|
||||
const METHOD_OVERRIDE = '_METHOD';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $formDataMediaTypes = array('application/x-www-form-urlencoded');
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $env;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param array $env
|
||||
* @see \Slim\Environment
|
||||
*/
|
||||
public function __construct($env)
|
||||
{
|
||||
$this->env = $env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get HTTP method
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod()
|
||||
{
|
||||
return $this->env['REQUEST_METHOD'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a GET request?
|
||||
* @return bool
|
||||
*/
|
||||
public function isGet()
|
||||
{
|
||||
return $this->getMethod() === self::METHOD_GET;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a POST request?
|
||||
* @return bool
|
||||
*/
|
||||
public function isPost()
|
||||
{
|
||||
return $this->getMethod() === self::METHOD_POST;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a PUT request?
|
||||
* @return bool
|
||||
*/
|
||||
public function isPut()
|
||||
{
|
||||
return $this->getMethod() === self::METHOD_PUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a DELETE request?
|
||||
* @return bool
|
||||
*/
|
||||
public function isDelete()
|
||||
{
|
||||
return $this->getMethod() === self::METHOD_DELETE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a HEAD request?
|
||||
* @return bool
|
||||
*/
|
||||
public function isHead()
|
||||
{
|
||||
return $this->getMethod() === self::METHOD_HEAD;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a OPTIONS request?
|
||||
* @return bool
|
||||
*/
|
||||
public function isOptions()
|
||||
{
|
||||
return $this->getMethod() === self::METHOD_OPTIONS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this an AJAX request?
|
||||
* @return bool
|
||||
*/
|
||||
public function isAjax()
|
||||
{
|
||||
if ($this->params('isajax')) {
|
||||
return true;
|
||||
} elseif (isset($this->env['X_REQUESTED_WITH']) && $this->env['X_REQUESTED_WITH'] === 'XMLHttpRequest') {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this an XHR request? (alias of Slim_Http_Request::isAjax)
|
||||
* @return bool
|
||||
*/
|
||||
public function isXhr()
|
||||
{
|
||||
return $this->isAjax();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch GET and POST data
|
||||
*
|
||||
* This method returns a union of GET and POST data as a key-value array, or the value
|
||||
* of the array key if requested; if the array key does not exist, NULL is returned.
|
||||
*
|
||||
* @param string $key
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function params($key = null)
|
||||
{
|
||||
$union = array_merge($this->get(), $this->post());
|
||||
if ($key) {
|
||||
if (isset($union[$key])) {
|
||||
return $union[$key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return $union;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch GET data
|
||||
*
|
||||
* This method returns a key-value array of data sent in the HTTP request query string, or
|
||||
* the value of the array key if requested; if the array key does not exist, NULL is returned.
|
||||
*
|
||||
* @param string $key
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function get($key = null)
|
||||
{
|
||||
if (!isset($this->env['slim.request.query_hash'])) {
|
||||
$output = array();
|
||||
if (function_exists('mb_parse_str') && !isset($this->env['slim.tests.ignore_multibyte'])) {
|
||||
mb_parse_str($this->env['QUERY_STRING'], $output);
|
||||
} else {
|
||||
parse_str($this->env['QUERY_STRING'], $output);
|
||||
}
|
||||
$this->env['slim.request.query_hash'] = Util::stripSlashesIfMagicQuotes($output);
|
||||
}
|
||||
if ($key) {
|
||||
if (isset($this->env['slim.request.query_hash'][$key])) {
|
||||
return $this->env['slim.request.query_hash'][$key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return $this->env['slim.request.query_hash'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch POST data
|
||||
*
|
||||
* This method returns a key-value array of data sent in the HTTP request body, or
|
||||
* the value of a hash key if requested; if the array key does not exist, NULL is returned.
|
||||
*
|
||||
* @param string $key
|
||||
* @return array|mixed|null
|
||||
* @throws \RuntimeException If environment input is not available
|
||||
*/
|
||||
public function post($key = null)
|
||||
{
|
||||
if (!isset($this->env['slim.input'])) {
|
||||
throw new \RuntimeException('Missing slim.input in environment variables');
|
||||
}
|
||||
if (!isset($this->env['slim.request.form_hash'])) {
|
||||
$this->env['slim.request.form_hash'] = array();
|
||||
if ($this->isFormData() && is_string($this->env['slim.input'])) {
|
||||
$output = array();
|
||||
if (function_exists('mb_parse_str') && !isset($this->env['slim.tests.ignore_multibyte'])) {
|
||||
mb_parse_str($this->env['slim.input'], $output);
|
||||
} else {
|
||||
parse_str($this->env['slim.input'], $output);
|
||||
}
|
||||
$this->env['slim.request.form_hash'] = Util::stripSlashesIfMagicQuotes($output);
|
||||
} else {
|
||||
$this->env['slim.request.form_hash'] = Util::stripSlashesIfMagicQuotes($_POST);
|
||||
}
|
||||
}
|
||||
if ($key) {
|
||||
if (isset($this->env['slim.request.form_hash'][$key])) {
|
||||
return $this->env['slim.request.form_hash'][$key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return $this->env['slim.request.form_hash'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch PUT data (alias for \Slim\Http\Request::post)
|
||||
* @param string $key
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function put($key = null)
|
||||
{
|
||||
return $this->post($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch DELETE data (alias for \Slim\Http\Request::post)
|
||||
* @param string $key
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function delete($key = null)
|
||||
{
|
||||
return $this->post($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch COOKIE data
|
||||
*
|
||||
* This method returns a key-value array of Cookie data sent in the HTTP request, or
|
||||
* the value of a array key if requested; if the array key does not exist, NULL is returned.
|
||||
*
|
||||
* @param string $key
|
||||
* @return array|string|null
|
||||
*/
|
||||
public function cookies($key = null)
|
||||
{
|
||||
if (!isset($this->env['slim.request.cookie_hash'])) {
|
||||
$cookieHeader = isset($this->env['COOKIE']) ? $this->env['COOKIE'] : '';
|
||||
$this->env['slim.request.cookie_hash'] = Util::parseCookieHeader($cookieHeader);
|
||||
}
|
||||
if ($key) {
|
||||
if (isset($this->env['slim.request.cookie_hash'][$key])) {
|
||||
return $this->env['slim.request.cookie_hash'][$key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return $this->env['slim.request.cookie_hash'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the Request body contain parseable form data?
|
||||
* @return bool
|
||||
*/
|
||||
public function isFormData()
|
||||
{
|
||||
$method = isset($this->env['slim.method_override.original_method']) ? $this->env['slim.method_override.original_method'] : $this->getMethod();
|
||||
|
||||
return ($method === self::METHOD_POST && is_null($this->getContentType())) || in_array($this->getMediaType(), self::$formDataMediaTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Headers
|
||||
*
|
||||
* This method returns a key-value array of headers sent in the HTTP request, or
|
||||
* the value of a hash key if requested; if the array key does not exist, NULL is returned.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default The default value returned if the requested header is not available
|
||||
* @return mixed
|
||||
*/
|
||||
public function headers($key = null, $default = null)
|
||||
{
|
||||
if ($key) {
|
||||
$key = strtoupper($key);
|
||||
$key = str_replace('-', '_', $key);
|
||||
$key = preg_replace('@^HTTP_@', '', $key);
|
||||
if (isset($this->env[$key])) {
|
||||
return $this->env[$key];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
} else {
|
||||
$headers = array();
|
||||
foreach ($this->env as $key => $value) {
|
||||
if (strpos($key, 'slim.') !== 0) {
|
||||
$headers[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $headers;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Body
|
||||
* @return string
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
return $this->env['slim.input'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Content Type
|
||||
* @return string
|
||||
*/
|
||||
public function getContentType()
|
||||
{
|
||||
if (isset($this->env['CONTENT_TYPE'])) {
|
||||
return $this->env['CONTENT_TYPE'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Media Type (type/subtype within Content Type header)
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMediaType()
|
||||
{
|
||||
$contentType = $this->getContentType();
|
||||
if ($contentType) {
|
||||
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
|
||||
|
||||
return strtolower($contentTypeParts[0]);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Media Type Params
|
||||
* @return array
|
||||
*/
|
||||
public function getMediaTypeParams()
|
||||
{
|
||||
$contentType = $this->getContentType();
|
||||
$contentTypeParams = array();
|
||||
if ($contentType) {
|
||||
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
|
||||
$contentTypePartsLength = count($contentTypeParts);
|
||||
for ($i = 1; $i < $contentTypePartsLength; $i++) {
|
||||
$paramParts = explode('=', $contentTypeParts[$i]);
|
||||
$contentTypeParams[strtolower($paramParts[0])] = $paramParts[1];
|
||||
}
|
||||
}
|
||||
|
||||
return $contentTypeParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Content Charset
|
||||
* @return string|null
|
||||
*/
|
||||
public function getContentCharset()
|
||||
{
|
||||
$mediaTypeParams = $this->getMediaTypeParams();
|
||||
if (isset($mediaTypeParams['charset'])) {
|
||||
return $mediaTypeParams['charset'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Content-Length
|
||||
* @return int
|
||||
*/
|
||||
public function getContentLength()
|
||||
{
|
||||
if (isset($this->env['CONTENT_LENGTH'])) {
|
||||
return (int) $this->env['CONTENT_LENGTH'];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Host
|
||||
* @return string
|
||||
*/
|
||||
public function getHost()
|
||||
{
|
||||
if (isset($this->env['HOST'])) {
|
||||
if (strpos($this->env['HOST'], ':') !== false) {
|
||||
$hostParts = explode(':', $this->env['HOST']);
|
||||
|
||||
return $hostParts[0];
|
||||
}
|
||||
|
||||
return $this->env['HOST'];
|
||||
} else {
|
||||
return $this->env['SERVER_NAME'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Host with Port
|
||||
* @return string
|
||||
*/
|
||||
public function getHostWithPort()
|
||||
{
|
||||
return sprintf('%s:%s', $this->getHost(), $this->getPort());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Port
|
||||
* @return int
|
||||
*/
|
||||
public function getPort()
|
||||
{
|
||||
return (int) $this->env['SERVER_PORT'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Scheme (https or http)
|
||||
* @return string
|
||||
*/
|
||||
public function getScheme()
|
||||
{
|
||||
return $this->env['slim.url_scheme'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Script Name (physical path)
|
||||
* @return string
|
||||
*/
|
||||
public function getScriptName()
|
||||
{
|
||||
return $this->env['SCRIPT_NAME'];
|
||||
}
|
||||
|
||||
/**
|
||||
* LEGACY: Get Root URI (alias for Slim_Http_Request::getScriptName)
|
||||
* @return string
|
||||
*/
|
||||
public function getRootUri()
|
||||
{
|
||||
return $this->getScriptName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Path (physical path + virtual path)
|
||||
* @return string
|
||||
*/
|
||||
public function getPath()
|
||||
{
|
||||
return $this->getScriptName() . $this->getPathInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Path Info (virtual path)
|
||||
* @return string
|
||||
*/
|
||||
public function getPathInfo()
|
||||
{
|
||||
return $this->env['PATH_INFO'];
|
||||
}
|
||||
|
||||
/**
|
||||
* LEGACY: Get Resource URI (alias for Slim_Http_Request::getPathInfo)
|
||||
* @return string
|
||||
*/
|
||||
public function getResourceUri()
|
||||
{
|
||||
return $this->getPathInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL (scheme + host [ + port if non-standard ])
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
$url = $this->getScheme() . '://' . $this->getHost();
|
||||
if (($this->getScheme() === 'https' && $this->getPort() !== 443) || ($this->getScheme() === 'http' && $this->getPort() !== 80)) {
|
||||
$url .= sprintf(':%s', $this->getPort());
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get IP
|
||||
* @return string
|
||||
*/
|
||||
public function getIp()
|
||||
{
|
||||
if (isset($this->env['X_FORWARDED_FOR'])) {
|
||||
return $this->env['X_FORWARDED_FOR'];
|
||||
} elseif (isset($this->env['CLIENT_IP'])) {
|
||||
return $this->env['CLIENT_IP'];
|
||||
}
|
||||
|
||||
return $this->env['REMOTE_ADDR'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Referrer
|
||||
* @return string|null
|
||||
*/
|
||||
public function getReferrer()
|
||||
{
|
||||
if (isset($this->env['REFERER'])) {
|
||||
return $this->env['REFERER'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Referer (for those who can't spell)
|
||||
* @return string|null
|
||||
*/
|
||||
public function getReferer()
|
||||
{
|
||||
return $this->getReferrer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get User Agent
|
||||
* @return string|null
|
||||
*/
|
||||
public function getUserAgent()
|
||||
{
|
||||
if (isset($this->env['USER_AGENT'])) {
|
||||
return $this->env['USER_AGENT'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
459
src/html/api/Slim/Http/Response.php
Normal file
459
src/html/api/Slim/Http/Response.php
Normal file
@ -0,0 +1,459 @@
|
||||
<?php
|
||||
/**
|
||||
* Slim - a micro PHP 5 framework
|
||||
*
|
||||
* @author Josh Lockhart <info@slimframework.com>
|
||||
* @copyright 2011 Josh Lockhart
|
||||
* @link http://www.slimframework.com
|
||||
* @license http://www.slimframework.com/license
|
||||
* @version 2.2.0
|
||||
* @package Slim
|
||||
*
|
||||
* MIT LICENSE
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
namespace Slim\Http;
|
||||
|
||||
/**
|
||||
* Response
|
||||
*
|
||||
* This is a simple abstraction over top an HTTP response. This
|
||||
* provides methods to set the HTTP status, the HTTP headers,
|
||||
* and the HTTP body.
|
||||
*
|
||||
* @package Slim
|
||||
* @author Josh Lockhart
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Response implements \ArrayAccess, \Countable, \IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* @var int HTTP status code
|
||||
*/
|
||||
protected $status;
|
||||
|
||||
/**
|
||||
* @var \Slim\Http\Headers List of HTTP response headers
|
||||
*/
|
||||
protected $header;
|
||||
|
||||
/**
|
||||
* @var string HTTP response body
|
||||
*/
|
||||
protected $body;
|
||||
|
||||
/**
|
||||
* @var int Length of HTTP response body
|
||||
*/
|
||||
protected $length;
|
||||
|
||||
/**
|
||||
* @var array HTTP response codes and messages
|
||||
*/
|
||||
protected static $messages = array(
|
||||
//Informational 1xx
|
||||
100 => '100 Continue',
|
||||
101 => '101 Switching Protocols',
|
||||
//Successful 2xx
|
||||
200 => '200 OK',
|
||||
201 => '201 Created',
|
||||
202 => '202 Accepted',
|
||||
203 => '203 Non-Authoritative Information',
|
||||
204 => '204 No Content',
|
||||
205 => '205 Reset Content',
|
||||
206 => '206 Partial Content',
|
||||
//Redirection 3xx
|
||||
300 => '300 Multiple Choices',
|
||||
301 => '301 Moved Permanently',
|
||||
302 => '302 Found',
|
||||
303 => '303 See Other',
|
||||
304 => '304 Not Modified',
|
||||
305 => '305 Use Proxy',
|
||||
306 => '306 (Unused)',
|
||||
307 => '307 Temporary Redirect',
|
||||
//Client Error 4xx
|
||||
400 => '400 Bad Request',
|
||||
401 => '401 Unauthorized',
|
||||
402 => '402 Payment Required',
|
||||
403 => '403 Forbidden',
|
||||
404 => '404 Not Found',
|
||||
405 => '405 Method Not Allowed',
|
||||
406 => '406 Not Acceptable',
|
||||
407 => '407 Proxy Authentication Required',
|
||||
408 => '408 Request Timeout',
|
||||
409 => '409 Conflict',
|
||||
410 => '410 Gone',
|
||||
411 => '411 Length Required',
|
||||
412 => '412 Precondition Failed',
|
||||
413 => '413 Request Entity Too Large',
|
||||
414 => '414 Request-URI Too Long',
|
||||
415 => '415 Unsupported Media Type',
|
||||
416 => '416 Requested Range Not Satisfiable',
|
||||
417 => '417 Expectation Failed',
|
||||
422 => '422 Unprocessable Entity',
|
||||
423 => '423 Locked',
|
||||
//Server Error 5xx
|
||||
500 => '500 Internal Server Error',
|
||||
501 => '501 Not Implemented',
|
||||
502 => '502 Bad Gateway',
|
||||
503 => '503 Service Unavailable',
|
||||
504 => '504 Gateway Timeout',
|
||||
505 => '505 HTTP Version Not Supported'
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param string $body The HTTP response body
|
||||
* @param int $status The HTTP response status
|
||||
* @param \Slim\Http\Headers|array $header The HTTP response headers
|
||||
*/
|
||||
public function __construct($body = '', $status = 200, $header = array())
|
||||
{
|
||||
$this->status = (int) $status;
|
||||
$headers = array();
|
||||
foreach ($header as $key => $value) {
|
||||
$headers[$key] = $value;
|
||||
}
|
||||
$this->header = new Headers(array_merge(array('Content-Type' => 'text/html'), $headers));
|
||||
$this->body = '';
|
||||
$this->write($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and set status
|
||||
* @param int|null $status
|
||||
* @return int
|
||||
*/
|
||||
public function status($status = null)
|
||||
{
|
||||
if (!is_null($status)) {
|
||||
$this->status = (int) $status;
|
||||
}
|
||||
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and set header
|
||||
* @param string $name Header name
|
||||
* @param string|null $value Header value
|
||||
* @return string Header value
|
||||
*/
|
||||
public function header($name, $value = null)
|
||||
{
|
||||
if (!is_null($value)) {
|
||||
$this[$name] = $value;
|
||||
}
|
||||
|
||||
return $this[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get headers
|
||||
* @return \Slim\Http\Headers
|
||||
*/
|
||||
public function headers()
|
||||
{
|
||||
return $this->header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and set body
|
||||
* @param string|null $body Content of HTTP response body
|
||||
* @return string
|
||||
*/
|
||||
public function body($body = null)
|
||||
{
|
||||
if (!is_null($body)) {
|
||||
$this->write($body, true);
|
||||
}
|
||||
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and set length
|
||||
* @param int|null $length
|
||||
* @return int
|
||||
*/
|
||||
public function length($length = null)
|
||||
{
|
||||
if (!is_null($length)) {
|
||||
$this->length = (int) $length;
|
||||
}
|
||||
|
||||
return $this->length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append HTTP response body
|
||||
* @param string $body Content to append to the current HTTP response body
|
||||
* @param bool $replace Overwrite existing response body?
|
||||
* @return string The updated HTTP response body
|
||||
*/
|
||||
public function write($body, $replace = false)
|
||||
{
|
||||
if ($replace) {
|
||||
$this->body = $body;
|
||||
} else {
|
||||
$this->body .= (string) $body;
|
||||
}
|
||||
$this->length = strlen($this->body);
|
||||
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finalize
|
||||
*
|
||||
* This prepares this response and returns an array
|
||||
* of [status, headers, body]. This array is passed to outer middleware
|
||||
* if available or directly to the Slim run method.
|
||||
*
|
||||
* @return array[int status, array headers, string body]
|
||||
*/
|
||||
public function finalize()
|
||||
{
|
||||
if (in_array($this->status, array(204, 304))) {
|
||||
unset($this['Content-Type'], $this['Content-Length']);
|
||||
|
||||
return array($this->status, $this->header, '');
|
||||
} else {
|
||||
return array($this->status, $this->header, $this->body);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cookie
|
||||
*
|
||||
* Instead of using PHP's `setcookie()` function, Slim manually constructs the HTTP `Set-Cookie`
|
||||
* header on its own and delegates this responsibility to the `Slim_Http_Util` class. This
|
||||
* response's header is passed by reference to the utility class and is directly modified. By not
|
||||
* relying on PHP's native implementation, Slim allows middleware the opportunity to massage or
|
||||
* analyze the raw header before the response is ultimately delivered to the HTTP client.
|
||||
*
|
||||
* @param string $name The name of the cookie
|
||||
* @param string|array $value If string, the value of cookie; if array, properties for
|
||||
* cookie including: value, expire, path, domain, secure, httponly
|
||||
*/
|
||||
public function setCookie($name, $value)
|
||||
{
|
||||
Util::setCookieHeader($this->header, $name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete cookie
|
||||
*
|
||||
* Instead of using PHP's `setcookie()` function, Slim manually constructs the HTTP `Set-Cookie`
|
||||
* header on its own and delegates this responsibility to the `Slim_Http_Util` class. This
|
||||
* response's header is passed by reference to the utility class and is directly modified. By not
|
||||
* relying on PHP's native implementation, Slim allows middleware the opportunity to massage or
|
||||
* analyze the raw header before the response is ultimately delivered to the HTTP client.
|
||||
*
|
||||
* This method will set a cookie with the given name that has an expiration time in the past; this will
|
||||
* prompt the HTTP client to invalidate and remove the client-side cookie. Optionally, you may
|
||||
* also pass a key/value array as the second argument. If the "domain" key is present in this
|
||||
* array, only the Cookie with the given name AND domain will be removed. The invalidating cookie
|
||||
* sent with this response will adopt all properties of the second argument.
|
||||
*
|
||||
* @param string $name The name of the cookie
|
||||
* @param array $value Properties for cookie including: value, expire, path, domain, secure, httponly
|
||||
*/
|
||||
public function deleteCookie($name, $value = array())
|
||||
{
|
||||
Util::deleteCookieHeader($this->header, $name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect
|
||||
*
|
||||
* This method prepares this response to return an HTTP Redirect response
|
||||
* to the HTTP client.
|
||||
*
|
||||
* @param string $url The redirect destination
|
||||
* @param int $status The redirect HTTP status code
|
||||
*/
|
||||
public function redirect ($url, $status = 302)
|
||||
{
|
||||
$this->status = $status;
|
||||
$this['Location'] = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: Empty?
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty()
|
||||
{
|
||||
return in_array($this->status, array(201, 204, 304));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: Informational?
|
||||
* @return bool
|
||||
*/
|
||||
public function isInformational()
|
||||
{
|
||||
return $this->status >= 100 && $this->status < 200;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: OK?
|
||||
* @return bool
|
||||
*/
|
||||
public function isOk()
|
||||
{
|
||||
return $this->status === 200;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: Successful?
|
||||
* @return bool
|
||||
*/
|
||||
public function isSuccessful()
|
||||
{
|
||||
return $this->status >= 200 && $this->status < 300;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: Redirect?
|
||||
* @return bool
|
||||
*/
|
||||
public function isRedirect()
|
||||
{
|
||||
return in_array($this->status, array(301, 302, 303, 307));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: Redirection?
|
||||
* @return bool
|
||||
*/
|
||||
public function isRedirection()
|
||||
{
|
||||
return $this->status >= 300 && $this->status < 400;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: Forbidden?
|
||||
* @return bool
|
||||
*/
|
||||
public function isForbidden()
|
||||
{
|
||||
return $this->status === 403;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: Not Found?
|
||||
* @return bool
|
||||
*/
|
||||
public function isNotFound()
|
||||
{
|
||||
return $this->status === 404;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: Client error?
|
||||
* @return bool
|
||||
*/
|
||||
public function isClientError()
|
||||
{
|
||||
return $this->status >= 400 && $this->status < 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helpers: Server Error?
|
||||
* @return bool
|
||||
*/
|
||||
public function isServerError()
|
||||
{
|
||||
return $this->status >= 500 && $this->status < 600;
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Access: Offset Exists
|
||||
*/
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset($this->header[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Access: Offset Get
|
||||
*/
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
if (isset($this->header[$offset])) {
|
||||
return $this->header[$offset];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Access: Offset Set
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->header[$offset] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Access: Offset Unset
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->header[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Countable: Count
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Iterator
|
||||
*
|
||||
* This returns the contained `\Slim\Http\Headers` instance which
|
||||
* is itself iterable.
|
||||
*
|
||||
* @return \Slim\Http\Headers
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return $this->header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get message for HTTP status code
|
||||
* @return string|null
|
||||
*/
|
||||
public static function getMessageForCode($status)
|
||||
{
|
||||
if (isset(self::$messages[$status])) {
|
||||
return self::$messages[$status];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
389
src/html/api/Slim/Http/Util.php
Normal file
389
src/html/api/Slim/Http/Util.php
Normal file
@ -0,0 +1,389 @@
|
||||
<?php
|
||||
/**
|
||||
* Slim - a micro PHP 5 framework
|
||||
*
|
||||
* @author Josh Lockhart <info@slimframework.com>
|
||||
* @copyright 2011 Josh Lockhart
|
||||
* @link http://www.slimframework.com
|
||||
* @license http://www.slimframework.com/license
|
||||
* @version 2.2.0
|
||||
* @package Slim
|
||||
*
|
||||
* MIT LICENSE
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
namespace Slim\Http;
|
||||
|
||||
/**
|
||||
* Slim HTTP Utilities
|
||||
*
|
||||
* This class provides useful methods for handling HTTP requests.
|
||||
*
|
||||
* @package Slim
|
||||
* @author Josh Lockhart
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Util
|
||||
{
|
||||
/**
|
||||
* Strip slashes from string or array
|
||||
*
|
||||
* This method strips slashes from its input. By default, this method will only
|
||||
* strip slashes from its input if magic quotes are enabled. Otherwise, you may
|
||||
* override the magic quotes setting with either TRUE or FALSE as the send argument
|
||||
* to force this method to strip or not strip slashes from its input.
|
||||
*
|
||||
* @var array|string $rawData
|
||||
* @return array|string
|
||||
*/
|
||||
public static function stripSlashesIfMagicQuotes($rawData, $overrideStripSlashes = null)
|
||||
{
|
||||
$strip = is_null($overrideStripSlashes) ? get_magic_quotes_gpc() : $overrideStripSlashes;
|
||||
if ($strip) {
|
||||
return self::_stripSlashes($rawData);
|
||||
} else {
|
||||
return $rawData;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip slashes from string or array
|
||||
* @param array|string $rawData
|
||||
* @return array|string
|
||||
*/
|
||||
protected static function _stripSlashes($rawData)
|
||||
{
|
||||
return is_array($rawData) ? array_map(array('self', '_stripSlashes'), $rawData) : stripslashes($rawData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt data
|
||||
*
|
||||
* This method will encrypt data using a given key, vector, and cipher.
|
||||
* By default, this will encrypt data using the RIJNDAEL/AES 256 bit cipher. You
|
||||
* may override the default cipher and cipher mode by passing your own desired
|
||||
* cipher and cipher mode as the final key-value array argument.
|
||||
*
|
||||
* @param string $data The unencrypted data
|
||||
* @param string $key The encryption key
|
||||
* @param string $iv The encryption initialization vector
|
||||
* @param array $settings Optional key-value array with custom algorithm and mode
|
||||
* @return string
|
||||
*/
|
||||
public static function encrypt($data, $key, $iv, $settings = array())
|
||||
{
|
||||
if ($data === '' || !extension_loaded('mcrypt')) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
//Merge settings with defaults
|
||||
$settings = array_merge(array(
|
||||
'algorithm' => MCRYPT_RIJNDAEL_256,
|
||||
'mode' => MCRYPT_MODE_CBC
|
||||
), $settings);
|
||||
|
||||
//Get module
|
||||
$module = mcrypt_module_open($settings['algorithm'], '', $settings['mode'], '');
|
||||
|
||||
//Validate IV
|
||||
$ivSize = mcrypt_enc_get_iv_size($module);
|
||||
if (strlen($iv) > $ivSize) {
|
||||
$iv = substr($iv, 0, $ivSize);
|
||||
}
|
||||
|
||||
//Validate key
|
||||
$keySize = mcrypt_enc_get_key_size($module);
|
||||
if (strlen($key) > $keySize) {
|
||||
$key = substr($key, 0, $keySize);
|
||||
}
|
||||
|
||||
//Encrypt value
|
||||
mcrypt_generic_init($module, $key, $iv);
|
||||
$res = @mcrypt_generic($module, $data);
|
||||
mcrypt_generic_deinit($module);
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt data
|
||||
*
|
||||
* This method will decrypt data using a given key, vector, and cipher.
|
||||
* By default, this will decrypt data using the RIJNDAEL/AES 256 bit cipher. You
|
||||
* may override the default cipher and cipher mode by passing your own desired
|
||||
* cipher and cipher mode as the final key-value array argument.
|
||||
*
|
||||
* @param string $data The encrypted data
|
||||
* @param string $key The encryption key
|
||||
* @param string $iv The encryption initialization vector
|
||||
* @param array $settings Optional key-value array with custom algorithm and mode
|
||||
* @return string
|
||||
*/
|
||||
public static function decrypt($data, $key, $iv, $settings = array())
|
||||
{
|
||||
if ($data === '' || !extension_loaded('mcrypt')) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
//Merge settings with defaults
|
||||
$settings = array_merge(array(
|
||||
'algorithm' => MCRYPT_RIJNDAEL_256,
|
||||
'mode' => MCRYPT_MODE_CBC
|
||||
), $settings);
|
||||
|
||||
//Get module
|
||||
$module = mcrypt_module_open($settings['algorithm'], '', $settings['mode'], '');
|
||||
|
||||
//Validate IV
|
||||
$ivSize = mcrypt_enc_get_iv_size($module);
|
||||
if (strlen($iv) > $ivSize) {
|
||||
$iv = substr($iv, 0, $ivSize);
|
||||
}
|
||||
|
||||
//Validate key
|
||||
$keySize = mcrypt_enc_get_key_size($module);
|
||||
if (strlen($key) > $keySize) {
|
||||
$key = substr($key, 0, $keySize);
|
||||
}
|
||||
|
||||
//Decrypt value
|
||||
mcrypt_generic_init($module, $key, $iv);
|
||||
$decryptedData = @mdecrypt_generic($module, $data);
|
||||
$res = str_replace("\x0", '', $decryptedData);
|
||||
mcrypt_generic_deinit($module);
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode secure cookie value
|
||||
*
|
||||
* This method will create the secure value of an HTTP cookie. The
|
||||
* cookie value is encrypted and hashed so that its value is
|
||||
* secure and checked for integrity when read in subsequent requests.
|
||||
*
|
||||
* @param string $value The unsecure HTTP cookie value
|
||||
* @param int $expires The UNIX timestamp at which this cookie will expire
|
||||
* @param string $secret The secret key used to hash the cookie value
|
||||
* @param int $algorithm The algorithm to use for encryption
|
||||
* @param int $mode The algorithm mode to use for encryption
|
||||
* @param string
|
||||
*/
|
||||
public static function encodeSecureCookie($value, $expires, $secret, $algorithm, $mode)
|
||||
{
|
||||
$key = hash_hmac('sha1', $expires, $secret);
|
||||
$iv = self::get_iv($expires, $secret);
|
||||
$secureString = base64_encode(self::encrypt($value, $key, $iv, array(
|
||||
'algorithm' => $algorithm,
|
||||
'mode' => $mode
|
||||
)));
|
||||
$verificationString = hash_hmac('sha1', $expires . $value, $key);
|
||||
|
||||
return implode('|', array($expires, $secureString, $verificationString));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode secure cookie value
|
||||
*
|
||||
* This method will decode the secure value of an HTTP cookie. The
|
||||
* cookie value is encrypted and hashed so that its value is
|
||||
* secure and checked for integrity when read in subsequent requests.
|
||||
*
|
||||
* @param string $value The secure HTTP cookie value
|
||||
* @param int $expires The UNIX timestamp at which this cookie will expire
|
||||
* @param string $secret The secret key used to hash the cookie value
|
||||
* @param int $algorithm The algorithm to use for encryption
|
||||
* @param int $mode The algorithm mode to use for encryption
|
||||
* @param string
|
||||
*/
|
||||
public static function decodeSecureCookie($value, $secret, $algorithm, $mode)
|
||||
{
|
||||
if ($value) {
|
||||
$value = explode('|', $value);
|
||||
if (count($value) === 3 && ((int) $value[0] === 0 || (int) $value[0] > time())) {
|
||||
$key = hash_hmac('sha1', $value[0], $secret);
|
||||
$iv = self::get_iv($value[0], $secret);
|
||||
$data = self::decrypt(base64_decode($value[1]), $key, $iv, array(
|
||||
'algorithm' => $algorithm,
|
||||
'mode' => $mode
|
||||
));
|
||||
$verificationString = hash_hmac('sha1', $value[0] . $data, $key);
|
||||
if ($verificationString === $value[2]) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set HTTP cookie header
|
||||
*
|
||||
* This method will construct and set the HTTP `Set-Cookie` header. Slim
|
||||
* uses this method instead of PHP's native `setcookie` method. This allows
|
||||
* more control of the HTTP header irrespective of the native implementation's
|
||||
* dependency on PHP versions.
|
||||
*
|
||||
* This method accepts the Slim_Http_Headers object by reference as its
|
||||
* first argument; this method directly modifies this object instead of
|
||||
* returning a value.
|
||||
*
|
||||
* @param array $header
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
*/
|
||||
public static function setCookieHeader(&$header, $name, $value)
|
||||
{
|
||||
//Build cookie header
|
||||
if (is_array($value)) {
|
||||
$domain = '';
|
||||
$path = '';
|
||||
$expires = '';
|
||||
$secure = '';
|
||||
$httponly = '';
|
||||
if (isset($value['domain']) && $value['domain']) {
|
||||
$domain = '; domain=' . $value['domain'];
|
||||
}
|
||||
if (isset($value['path']) && $value['path']) {
|
||||
$path = '; path=' . $value['path'];
|
||||
}
|
||||
if (isset($value['expires'])) {
|
||||
if (is_string($value['expires'])) {
|
||||
$timestamp = strtotime($value['expires']);
|
||||
} else {
|
||||
$timestamp = (int) $value['expires'];
|
||||
}
|
||||
if ($timestamp !== 0) {
|
||||
$expires = '; expires=' . gmdate('D, d-M-Y H:i:s e', $timestamp);
|
||||
}
|
||||
}
|
||||
if (isset($value['secure']) && $value['secure']) {
|
||||
$secure = '; secure';
|
||||
}
|
||||
if (isset($value['httponly']) && $value['httponly']) {
|
||||
$httponly = '; HttpOnly';
|
||||
}
|
||||
$cookie = sprintf('%s=%s%s', urlencode($name), urlencode((string) $value['value']), $domain . $path . $expires . $secure . $httponly);
|
||||
} else {
|
||||
$cookie = sprintf('%s=%s', urlencode($name), urlencode((string) $value));
|
||||
}
|
||||
|
||||
//Set cookie header
|
||||
if (!isset($header['Set-Cookie']) || $header['Set-Cookie'] === '') {
|
||||
$header['Set-Cookie'] = $cookie;
|
||||
} else {
|
||||
$header['Set-Cookie'] = implode("\n", array($header['Set-Cookie'], $cookie));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete HTTP cookie header
|
||||
*
|
||||
* This method will construct and set the HTTP `Set-Cookie` header to invalidate
|
||||
* a client-side HTTP cookie. If a cookie with the same name (and, optionally, domain)
|
||||
* is already set in the HTTP response, it will also be removed. Slim uses this method
|
||||
* instead of PHP's native `setcookie` method. This allows more control of the HTTP header
|
||||
* irrespective of PHP's native implementation's dependency on PHP versions.
|
||||
*
|
||||
* This method accepts the Slim_Http_Headers object by reference as its
|
||||
* first argument; this method directly modifies this object instead of
|
||||
* returning a value.
|
||||
*
|
||||
* @param array $header
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
*/
|
||||
public static function deleteCookieHeader(&$header, $name, $value = array())
|
||||
{
|
||||
//Remove affected cookies from current response header
|
||||
$cookiesOld = array();
|
||||
$cookiesNew = array();
|
||||
if (isset($header['Set-Cookie'])) {
|
||||
$cookiesOld = explode("\n", $header['Set-Cookie']);
|
||||
}
|
||||
foreach ($cookiesOld as $c) {
|
||||
if (isset($value['domain']) && $value['domain']) {
|
||||
$regex = sprintf('@%s=.*domain=%s@', urlencode($name), preg_quote($value['domain']));
|
||||
} else {
|
||||
$regex = sprintf('@%s=@', urlencode($name));
|
||||
}
|
||||
if (preg_match($regex, $c) === 0) {
|
||||
$cookiesNew[] = $c;
|
||||
}
|
||||
}
|
||||
if ($cookiesNew) {
|
||||
$header['Set-Cookie'] = implode("\n", $cookiesNew);
|
||||
} else {
|
||||
unset($header['Set-Cookie']);
|
||||
}
|
||||
|
||||
//Set invalidating cookie to clear client-side cookie
|
||||
self::setCookieHeader($header, $name, array_merge(array('value' => '', 'path' => null, 'domain' => null, 'expires' => time() - 100), $value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse cookie header
|
||||
*
|
||||
* This method will parse the HTTP requst's `Cookie` header
|
||||
* and extract cookies into an associative array.
|
||||
*
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public static function parseCookieHeader($header)
|
||||
{
|
||||
$cookies = array();
|
||||
$header = rtrim($header, "\r\n");
|
||||
$headerPieces = preg_split('@\s*[;,]\s*@', $header);
|
||||
foreach ($headerPieces as $c) {
|
||||
$cParts = explode('=', $c);
|
||||
if (count($cParts) === 2) {
|
||||
$key = urldecode($cParts[0]);
|
||||
$value = urldecode($cParts[1]);
|
||||
if (!isset($cookies[$key])) {
|
||||
$cookies[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $cookies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random IV
|
||||
*
|
||||
* This method will generate a non-predictable IV for use with
|
||||
* the cookie encryption
|
||||
*
|
||||
* @param int $expires The UNIX timestamp at which this cookie will expire
|
||||
* @param string $secret The secret key used to hash the cookie value
|
||||
* @return binary string with length 40
|
||||
*/
|
||||
private static function get_iv($expires, $secret)
|
||||
{
|
||||
$data1 = hash_hmac('sha1', 'a'.$expires.'b', $secret);
|
||||
$data2 = hash_hmac('sha1', 'z'.$expires.'y', $secret);
|
||||
|
||||
return pack("h*", $data1.$data2);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user