converting cookies to use http foundation.
This commit is contained in:
parent
ad41be0eed
commit
730808fb02
|
@ -1,6 +1,4 @@
|
|||
<?php namespace Laravel; defined('DS') or die('No direct script access.');
|
||||
|
||||
use Closure;
|
||||
<?php namespace Laravel; use Closure;
|
||||
|
||||
class Cookie {
|
||||
|
||||
|
@ -22,52 +20,6 @@ public static function has($name)
|
|||
return ! is_null(static::get($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send all of the cookies to the browser.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function send()
|
||||
{
|
||||
if (headers_sent()) return false;
|
||||
|
||||
// All cookies are stored in the "jar" when set and not sent directly to
|
||||
// the browser. This simply makes testing all of the cookie stuff very
|
||||
// easy since the jar can be inspected by tests.
|
||||
foreach (static::$jar as $cookie)
|
||||
{
|
||||
static::set($cookie);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a cookie from the cookie jar back to the browser.
|
||||
*
|
||||
* @param array $cookie
|
||||
* @return void
|
||||
*/
|
||||
protected static function set($cookie)
|
||||
{
|
||||
extract($cookie);
|
||||
|
||||
$time = ($minutes !== 0) ? time() + ($minutes * 60) : 0;
|
||||
|
||||
$value = static::sign($name, $value);
|
||||
|
||||
// A cookie payload can't exceed 4096 bytes, so if the cookie payload
|
||||
// is greater than that, we'll raise an error to warn the developer
|
||||
// since it could cause cookie session problems.
|
||||
if (strlen($value) > 4000)
|
||||
{
|
||||
throw new \Exception("Payload too large for cookie.");
|
||||
}
|
||||
else
|
||||
{
|
||||
setcookie($name, $value, $time, $path, $domain, $secure);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the value of a cookie.
|
||||
*
|
||||
|
@ -85,27 +37,9 @@ protected static function set($cookie)
|
|||
*/
|
||||
public static function get($name, $default = null)
|
||||
{
|
||||
if (isset(static::$jar[$name])) return static::$jar[$name]['value'];
|
||||
if (isset(static::$jar[$name])) return static::$jar[$name];
|
||||
|
||||
$value = array_get($_COOKIE, $name);
|
||||
|
||||
if ( ! is_null($value) and isset($value[40]) and $value[40] == '~')
|
||||
{
|
||||
// The hash signature and the cookie value are separated by a tilde
|
||||
// character for convenience. To separate the hash and the payload
|
||||
// we can simply expode on that character.
|
||||
list($hash, $value) = explode('~', $value, 2);
|
||||
|
||||
// By re-feeding the cookie value into the "hash" method we should
|
||||
// be able to generate a hash that matches the one taken from the
|
||||
// cookie. If they don't, we return null.
|
||||
if (static::hash($name, $value) === $hash)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return value($default);
|
||||
return array_get(Request::foundation()->cookies->all(), $name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,15 +55,20 @@ public static function get($name, $default = null)
|
|||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @param int $minutes
|
||||
* @param int $expiration
|
||||
* @param string $path
|
||||
* @param string $domain
|
||||
* @param bool $secure
|
||||
* @return void
|
||||
*/
|
||||
public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
|
||||
public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
|
||||
{
|
||||
static::$jar[$name] = compact('name', 'value', 'minutes', 'path', 'domain', 'secure');
|
||||
if ($expiration !== 0)
|
||||
{
|
||||
$expiration = time() + ($expiration * 60);
|
||||
}
|
||||
|
||||
static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -152,30 +91,6 @@ public static function forever($name, $value, $path = '/', $domain = null, $secu
|
|||
return static::put($name, $value, 525600, $path, $domain, $secure);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a cookie signature based on the contents.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function sign($name, $value)
|
||||
{
|
||||
return static::hash($name, $value).'~'.$value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a cookie hash based on the contents.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
protected static function hash($name, $value)
|
||||
{
|
||||
return sha1($name.$value.Config::get('application.key'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a cookie.
|
||||
*
|
||||
|
|
|
@ -136,39 +136,15 @@ public static function old($key = null, $default = null)
|
|||
* <code>
|
||||
* // Get the array of information for the "picture" upload
|
||||
* $picture = Input::file('picture');
|
||||
*
|
||||
* // Get a specific element from within the file's data array
|
||||
* $size = Input::file('picture.size');
|
||||
* </code>
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return array
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return UploadedFile
|
||||
*/
|
||||
public static function file($key = null, $default = null)
|
||||
{
|
||||
return array_get($_FILES, $key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move an uploaded file to permanent storage.
|
||||
*
|
||||
* This method is simply a convenient wrapper around move_uploaded_file.
|
||||
*
|
||||
* <code>
|
||||
* // Move the "picture" file to a permanent location on disk
|
||||
* Input::upload('picture', 'path/to/photos/picture.jpg');
|
||||
* </code>
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
public static function upload($key, $path)
|
||||
{
|
||||
if (is_null(static::file($key))) return false;
|
||||
|
||||
return move_uploaded_file(static::file("{$key}.tmp_name"), $path);
|
||||
return array_get(Request::foundation()->files->all(), $key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -208,19 +208,6 @@
|
|||
Session::save();
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Let's Eat Cookies
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| All cookies set during the request are actually stored in a cookie jar
|
||||
| until the end of the request so they can be expected by unit tests or
|
||||
| the developer. Here, we'll push them out to the browser.
|
||||
|
|
||||
*/
|
||||
|
||||
Cookie::send();
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Send The Response To The Browser
|
||||
|
|
|
@ -78,7 +78,7 @@ public static function spoofed()
|
|||
*/
|
||||
public static function ip($default = '0.0.0.0')
|
||||
{
|
||||
return value(static::$foundation->getClientIp(), $default);
|
||||
return value(static::foundation()->getClientIp(), $default);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,7 +98,7 @@ public static function protocol()
|
|||
*/
|
||||
public static function accept()
|
||||
{
|
||||
return static::$foundation->getAcceptableContentTypes();
|
||||
return static::foundation()->getAcceptableContentTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -118,7 +118,7 @@ public static function accepts($type)
|
|||
*/
|
||||
public static function secure()
|
||||
{
|
||||
return static::$foundation->isSecure();
|
||||
return static::foundation()->isSecure();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -140,7 +140,7 @@ public static function forged()
|
|||
*/
|
||||
public static function ajax()
|
||||
{
|
||||
return static::$foundation->isXmlHttpRequest();
|
||||
return static::foundation()->isXmlHttpRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -194,6 +194,16 @@ public static function route()
|
|||
return static::$route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Symfony HttpFoundation Request instance.
|
||||
*
|
||||
* @return HttpFoundation\Request
|
||||
*/
|
||||
public static function foundation()
|
||||
{
|
||||
return static::$foundation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass any other methods to the Symfony request.
|
||||
*
|
||||
|
@ -203,7 +213,7 @@ public static function route()
|
|||
*/
|
||||
public static function __callStatic($method, $parameters)
|
||||
{
|
||||
return call_user_func_array(array(static::$foundation, $method), $parameters);
|
||||
return call_user_func_array(array(static::foundation(), $method), $parameters);
|
||||
}
|
||||
|
||||
}
|
|
@ -175,9 +175,9 @@ public function render()
|
|||
$this->content = (string) $this->content;
|
||||
}
|
||||
|
||||
// Once we have the string content, we can set the content on
|
||||
// the HttpFoundation Response instance in preparation for
|
||||
// sending it back to client browser when all is done.
|
||||
// Once we obtain the string content, we can set the content on
|
||||
// the HttpFoundation's Response instance in preparation for
|
||||
// sending it back to client browser when all is finished.
|
||||
$this->foundation->setContent($this->content);
|
||||
|
||||
return $this->content;
|
||||
|
@ -190,7 +190,9 @@ public function render()
|
|||
*/
|
||||
public function send()
|
||||
{
|
||||
$this->foundation->prepare(Request::$foundation);
|
||||
$this->cookies();
|
||||
|
||||
$this->foundation->prepare(Request::foundation());
|
||||
|
||||
$this->foundation->send();
|
||||
}
|
||||
|
@ -202,11 +204,31 @@ public function send()
|
|||
*/
|
||||
public function send_headers()
|
||||
{
|
||||
$this->foundation->prepare(Request::$foundation);
|
||||
$this->foundation->prepare(Request::foundation());
|
||||
|
||||
$this->foundation->sendHeaders();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cookies on the HttpFoundation Response.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function cookies()
|
||||
{
|
||||
$ref = new \ReflectionClass('Symfony\Component\HttpFoundation\Cookie');
|
||||
|
||||
// All of the cookies for the response are actually stored on the
|
||||
// Cookie class until we're ready to send the response back to
|
||||
// the browser. This allows a cookies to be set easily.
|
||||
foreach (Cookie::$jar as $name => $cookie)
|
||||
{
|
||||
$config = array_values($cookie);
|
||||
|
||||
$this->headers()->setCookie($ref->newInstanceArgs($config));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a header to the array of response headers.
|
||||
*
|
||||
|
@ -221,6 +243,16 @@ public function header($name, $value)
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the HttpFoundation Response headers.
|
||||
*
|
||||
* @return ResponseParameterBag
|
||||
*/
|
||||
public function headers()
|
||||
{
|
||||
return $this->foundation->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the response status code.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue