Commit 730808fb authored by Taylor Otwell's avatar Taylor Otwell

converting cookies to use http foundation.

parent ad41be0e
<?php namespace Laravel; defined('DS') or die('No direct script access.'); <?php namespace Laravel; use Closure;
use Closure;
class Cookie { class Cookie {
...@@ -22,52 +20,6 @@ class Cookie { ...@@ -22,52 +20,6 @@ class Cookie {
return ! is_null(static::get($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. * Get the value of a cookie.
* *
...@@ -85,27 +37,9 @@ class Cookie { ...@@ -85,27 +37,9 @@ class Cookie {
*/ */
public static function get($name, $default = null) 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); return array_get(Request::foundation()->cookies->all(), $name, $default);
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);
} }
/** /**
...@@ -121,15 +55,20 @@ class Cookie { ...@@ -121,15 +55,20 @@ class Cookie {
* *
* @param string $name * @param string $name
* @param string $value * @param string $value
* @param int $minutes * @param int $expiration
* @param string $path * @param string $path
* @param string $domain * @param string $domain
* @param bool $secure * @param bool $secure
* @return void * @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 @@ class Cookie { ...@@ -152,30 +91,6 @@ class Cookie {
return static::put($name, $value, 525600, $path, $domain, $secure); 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. * Delete a cookie.
* *
......
...@@ -136,39 +136,15 @@ class Input { ...@@ -136,39 +136,15 @@ class Input {
* <code> * <code>
* // Get the array of information for the "picture" upload * // Get the array of information for the "picture" upload
* $picture = Input::file('picture'); * $picture = Input::file('picture');
*
* // Get a specific element from within the file's data array
* $size = Input::file('picture.size');
* </code> * </code>
* *
* @param string $key * @param string $key
* @param mixed $default * @param mixed $default
* @return array * @return UploadedFile
*/ */
public static function file($key = null, $default = null) public static function file($key = null, $default = null)
{ {
return array_get($_FILES, $key, $default); return array_get(Request::foundation()->files->all(), $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);
} }
/** /**
......
...@@ -208,19 +208,6 @@ if (Config::get('session.driver') !== '') ...@@ -208,19 +208,6 @@ if (Config::get('session.driver') !== '')
Session::save(); 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 | Send The Response To The Browser
......
...@@ -78,7 +78,7 @@ class Request { ...@@ -78,7 +78,7 @@ class Request {
*/ */
public static function ip($default = '0.0.0.0') 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 @@ class Request { ...@@ -98,7 +98,7 @@ class Request {
*/ */
public static function accept() public static function accept()
{ {
return static::$foundation->getAcceptableContentTypes(); return static::foundation()->getAcceptableContentTypes();
} }
/** /**
...@@ -118,7 +118,7 @@ class Request { ...@@ -118,7 +118,7 @@ class Request {
*/ */
public static function secure() public static function secure()
{ {
return static::$foundation->isSecure(); return static::foundation()->isSecure();
} }
/** /**
...@@ -140,7 +140,7 @@ class Request { ...@@ -140,7 +140,7 @@ class Request {
*/ */
public static function ajax() public static function ajax()
{ {
return static::$foundation->isXmlHttpRequest(); return static::foundation()->isXmlHttpRequest();
} }
/** /**
...@@ -194,6 +194,16 @@ class Request { ...@@ -194,6 +194,16 @@ class Request {
return static::$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. * Pass any other methods to the Symfony request.
* *
...@@ -203,7 +213,7 @@ class Request { ...@@ -203,7 +213,7 @@ class Request {
*/ */
public static function __callStatic($method, $parameters) 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);
} }
} }
\ No newline at end of file
...@@ -175,9 +175,9 @@ class Response { ...@@ -175,9 +175,9 @@ class Response {
$this->content = (string) $this->content; $this->content = (string) $this->content;
} }
// Once we have the string content, we can set the content on // Once we obtain the string content, we can set the content on
// the HttpFoundation Response instance in preparation for // the HttpFoundation's Response instance in preparation for
// sending it back to client browser when all is done. // sending it back to client browser when all is finished.
$this->foundation->setContent($this->content); $this->foundation->setContent($this->content);
return $this->content; return $this->content;
...@@ -190,7 +190,9 @@ class Response { ...@@ -190,7 +190,9 @@ class Response {
*/ */
public function send() public function send()
{ {
$this->foundation->prepare(Request::$foundation); $this->cookies();
$this->foundation->prepare(Request::foundation());
$this->foundation->send(); $this->foundation->send();
} }
...@@ -202,11 +204,31 @@ class Response { ...@@ -202,11 +204,31 @@ class Response {
*/ */
public function send_headers() public function send_headers()
{ {
$this->foundation->prepare(Request::$foundation); $this->foundation->prepare(Request::foundation());
$this->foundation->sendHeaders(); $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. * Add a header to the array of response headers.
* *
...@@ -221,6 +243,16 @@ class Response { ...@@ -221,6 +243,16 @@ class Response {
return $this; return $this;
} }
/**
* Get the HttpFoundation Response headers.
*
* @return ResponseParameterBag
*/
public function headers()
{
return $this->foundation->headers;
}
/** /**
* Set the response status code. * Set the response status code.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment