From 0db0dadef71b2c0155f491cba5723bcb090e4efa Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 06:24:58 -0700 Subject: [PATCH 01/17] Improve request URI determination. --- system/request.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/system/request.php b/system/request.php index 671de97b..dd3be314 100644 --- a/system/request.php +++ b/system/request.php @@ -36,7 +36,12 @@ public static function uri() // ------------------------------------------------------- elseif (isset($_SERVER['REQUEST_URI'])) { - $uri = str_replace('/index.php', '', $_SERVER['REQUEST_URI']); + $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); + + if ($uri === false) + { + throw new \Exception("Malformed request URI. Request terminated."); + } } // ------------------------------------------------------- // Neither PATH_INFO or REQUEST_URI are available. From 3cffaf589d5d1e3408312d5e4b6af26e987d90d6 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 06:25:44 -0700 Subject: [PATCH 02/17] Changed (:any) wildcard to more restrictive. --- system/router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/router.php b/system/router.php index 7026411c..8a2a6a30 100644 --- a/system/router.php +++ b/system/router.php @@ -55,7 +55,7 @@ public static function route($method, $uri) // -------------------------------------------------------------- // Convert the route wild-cards to regular expressions. // -------------------------------------------------------------- - $route = str_replace(':num', '[0-9]+', str_replace(':any', '.+', $route)); + $route = str_replace(':num', '[0-9]+', str_replace(':any', '[a-zA-Z0-9\-_]+', $route)); if (preg_match('#^'.$route.'$#', $method.' '.$uri)) { From afd00260159c267a0831f5c7c00c1a7d43a62df4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 10:22:58 -0700 Subject: [PATCH 03/17] Edited session class to use new Cookie send method. --- system/session.php | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/system/session.php b/system/session.php index 695b7131..7a745149 100644 --- a/system/session.php +++ b/system/session.php @@ -17,15 +17,13 @@ class Session { private static $session = array(); /** - * Get the session driver instance. + * Get the session driver. If the driver has already been instantiated, that + * instance will be returned. * * @return Session\Driver */ public static function driver() { - // ----------------------------------------------------- - // Create the session driver if we haven't already. - // ----------------------------------------------------- if (is_null(static::$driver)) { static::$driver = Session\Factory::make(Config::get('session.driver')); @@ -59,7 +57,7 @@ public static function load() } // ----------------------------------------------------- - // Generate a CSRF token if one does not exist. + // Create a CSRF token for the session if necessary. // ----------------------------------------------------- if ( ! static::has('csrf_token')) { @@ -70,7 +68,7 @@ public static function load() } /** - * Determine if the session contains an item. + * Determine if the session or flash data contains an item. * * @param string $key * @return bool @@ -83,7 +81,7 @@ public static function has($key) } /** - * Get an item from the session. + * Get an item from the session or flash data. * * @param string $key * @return mixed @@ -96,9 +94,6 @@ public static function get($key, $default = null) { return static::$session['data'][$key]; } - // ----------------------------------------------------- - // Check the flash data for the item. - // ----------------------------------------------------- elseif (array_key_exists(':old:'.$key, static::$session['data'])) { return static::$session['data'][':old:'.$key]; @@ -125,7 +120,7 @@ public static function put($key, $value) } /** - * Write a flash item to the session. + * Write an item to the session flash data. * * @param string $key * @param mixed $value @@ -176,17 +171,14 @@ public static function regenerate() public static function close() { // ----------------------------------------------------- - // Flash the old input data to the session. + // Flash the old input to the session and age the flash. // ----------------------------------------------------- - static::flash('laravel_old_input', Input::get()); + static::flash('laravel_old_input', Input::get()); - // ----------------------------------------------------- - // Age the flash data. - // ----------------------------------------------------- static::age_flash(); // ----------------------------------------------------- - // Save the session data to storage. + // Write the session data to storage. // ----------------------------------------------------- static::driver()->save(static::$session); @@ -195,8 +187,18 @@ public static function close() // ----------------------------------------------------- if ( ! headers_sent()) { - $lifetime = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime'); - Cookie::put('laravel_session', static::$session['id'], $lifetime, Config::get('session.path'), Config::get('session.domain'), Config::get('session.https')); + $cookie = new Cookie('laravel_session', static::$session['id']); + + if ( ! Config::get('session.expire_on_close')) + { + $cookie->lifetime = Config::get('session.lifetime'); + } + + $cookie->path = Config::get('session.path'); + $cookie->domain = Config::get('session.domain'); + $cookie->secure = Config::get('session.https'); + + $cookie->send(); } // ----------------------------------------------------- @@ -204,7 +206,7 @@ public static function close() // ----------------------------------------------------- if (mt_rand(1, 100) <= 2) { - static::driver()->sweep(time() - (Config::get('session.lifetime') * 60)); + static::driver()->sweep(time() - ($config['lifetime'] * 60)); } } @@ -234,12 +236,12 @@ private static function age_flash() if (strpos($key, ':new:') === 0) { // ----------------------------------------------------- - // Create an :old: flash item. + // Create an :old: item for the :new: item. // ----------------------------------------------------- static::put(':old:'.substr($key, 5), $value); // ----------------------------------------------------- - // Forget the :new: flash item. + // Forget the :new: item. // ----------------------------------------------------- static::forget($key); } From 5a0a230b3dffb90f8b369c1211b76b3421c71956 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 10:24:30 -0700 Subject: [PATCH 04/17] Added ability to create cookies using properties. --- system/cookie.php | 112 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 96 insertions(+), 16 deletions(-) diff --git a/system/cookie.php b/system/cookie.php index e004c807..22a06b6a 100644 --- a/system/cookie.php +++ b/system/cookie.php @@ -3,47 +3,127 @@ class Cookie { /** - * Determine if a cookie exists. + * The cookie name. + * + * @var string + */ + public $name; + + /** + * The cookie value. + * + * @var mixed + */ + public $value; + + /** + * The number of minutes the cookie should live. + * + * @var int + */ + public $lifetime = 0; + + /** + * The path for which the cookie is available. + * + * @var string + */ + public $path = '/'; + + /** + * The domain for which the cookie is available. + * + * @var string + */ + public $domain = null; + + /** + * Indicates if the cookie should only be sent over HTTPS. + * + * @var bool + */ + public $secure = false; + + /** + * Create a new Cookie instance. + * + * @param string $name + * @return void + */ + public function __construct($name, $value = null) + { + $this->name = $name; + $this->value = $value; + } + + /** + * Create a new Cookie instance. + * + * @param string $name + * @return Cookie + */ + public static function make($name, $value = null) + { + return new static($name, $value); + } + + /** + * Send the current cookie instance to the user's machine. * - * @param string $key * @return bool */ - public static function has($key) + public function send() { - return ( ! is_null(static::get($key))); + if (is_null($this->name)) + { + throw new \Exception("Error sending cookie. The cookie does not have a name."); + } + + return static::put($this->name, $this->value, $this->lifetime, $this->path, $this->domain, $this->secure); + } + + /** + * Determine if a cookie exists. + * + * @param string $name + * @return bool + */ + public static function has($name) + { + return ( ! is_null(static::get($name))); } /** * Get the value of a cookie. * - * @param string $key + * @param string $name * @param mixed $default * @return string */ - public static function get($key, $default = null) + public static function get($name, $default = null) { - return (array_key_exists($key, $_COOKIE)) ? $_COOKIE[$key] : $default; + return (array_key_exists($name, $_COOKIE)) ? $_COOKIE[$name] : $default; } /** * Set a "permanent" cookie. The cookie will last 5 years. * - * @param string $key + * @param string $name * @param string $value * @param string $path * @param string $domain * @param bool $secure * @return bool */ - public static function forever($key, $value, $path = '/', $domain = null, $secure = false) + public static function forever($name, $value, $path = '/', $domain = null, $secure = false) { - return static::put($key, $value, 2628000, $path, $domain, $secure); + return static::put($name, $value, 2628000, $path, $domain, $secure); } /** * Set the value of a cookie. * - * @param string $key + * @param string $name * @param string $value * @param int $minutes * @param string $path @@ -51,23 +131,23 @@ public static function forever($key, $value, $path = '/', $domain = null, $secur * @param bool $secure * @return bool */ - public static function put($key, $value, $minutes = 0, $path = '/', $domain = null, $secure = false) + public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false) { if ($minutes < 0) { - unset($_COOKIE[$key]); + unset($_COOKIE[$name]); } - return setcookie($key, $value, ($minutes != 0) ? time() + ($minutes * 60) : 0, $path, $domain, $secure); + return setcookie($name, $value, ($minutes != 0) ? time() + ($minutes * 60) : 0, $path, $domain, $secure); } /** * Delete a cookie. * - * @param string $key + * @param string $name * @return bool */ - public static function forget($key) + public static function forget($name) { return static::put($key, null, -60); } From aee154c12fcbd13937fd2e2008f2eed16bf8d6b9 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 10:48:57 -0700 Subject: [PATCH 05/17] Tweaked Input::has method to accept multiple arguments and to return false if an argument is an empty string. --- system/input.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/system/input.php b/system/input.php index 513ca658..873f1f79 100644 --- a/system/input.php +++ b/system/input.php @@ -10,14 +10,21 @@ class Input { public static $input; /** - * Determine if the input data contains an item. + * Determine if the input data contains an item or set of items. * - * @param string $key * @return bool */ - public static function has($key) + public static function has() { - return ( ! is_null(static::get($key))); + foreach (func_get_args() as $key) + { + if (is_null($value = static::get($key)) or trim((string) $value) == '') + { + return false; + } + } + + return true; } /** From f00f3d5ea1e1db51a9c81b783ae3b7992828d5b4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 10:49:33 -0700 Subject: [PATCH 06/17] removed extraneous comment from Input class. --- system/input.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/system/input.php b/system/input.php index 873f1f79..20f593bb 100644 --- a/system/input.php +++ b/system/input.php @@ -36,9 +36,6 @@ public static function has() */ public static function get($key = null, $default = null) { - // ----------------------------------------------------- - // Has the input data been hydrated for the request? - // ----------------------------------------------------- if (is_null(static::$input)) { static::hydrate(); From f48d76abcaace996a7cf6698e3eb23145e5fadd5 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 11:07:54 -0700 Subject: [PATCH 07/17] Fix bug in session garbage collection. --- system/session.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/session.php b/system/session.php index 7a745149..4f85251f 100644 --- a/system/session.php +++ b/system/session.php @@ -206,7 +206,7 @@ public static function close() // ----------------------------------------------------- if (mt_rand(1, 100) <= 2) { - static::driver()->sweep(time() - ($config['lifetime'] * 60)); + static::driver()->sweep(time() - (Config::get('session.lifetime') * 60)); } } From 2ef32608f23552e326cb2d45df139a1990d93714 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 11:17:47 -0700 Subject: [PATCH 08/17] Allow multiple items to be passed to Session::has. --- system/session.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/system/session.php b/system/session.php index 4f85251f..00692f45 100644 --- a/system/session.php +++ b/system/session.php @@ -68,16 +68,24 @@ public static function load() } /** - * Determine if the session or flash data contains an item. + * Determine if the session or flash data contains an item or set of items. * * @param string $key * @return bool */ public static function has($key) { - return array_key_exists($key, static::$session['data']) or - array_key_exists(':old:'.$key, static::$session['data']) or - array_key_exists(':new:'.$key, static::$session['data']); + foreach (func_get_args() as $key) + { + if ( ! array_key_exists($key, static::$session['data']) and + ! array_key_exists(':old:'.$key, static::$session['data']) and + ! array_key_exists(':new:'.$key, static::$session['data'])) + { + return false; + } + } + + return true; } /** From 525f53b54c0b60c19bde9a05d0c5f7f7949b9eaf Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 11:19:52 -0700 Subject: [PATCH 09/17] Fix formatting on Session::has method. --- system/session.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/system/session.php b/system/session.php index 00692f45..63faa6fd 100644 --- a/system/session.php +++ b/system/session.php @@ -77,12 +77,12 @@ public static function has($key) { foreach (func_get_args() as $key) { - if ( ! array_key_exists($key, static::$session['data']) and - ! array_key_exists(':old:'.$key, static::$session['data']) and - ! array_key_exists(':new:'.$key, static::$session['data'])) - { - return false; - } + if ( ! array_key_exists($key, static::$session['data']) and + ! array_key_exists(':old:'.$key, static::$session['data']) and + ! array_key_exists(':new:'.$key, static::$session['data'])) + { + return false; + } } return true; From 55eb2ecb086cf72965f107c46d41b19e00e2aaad Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 11:21:39 -0700 Subject: [PATCH 10/17] Fixed parameters on Session::has. --- system/session.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/system/session.php b/system/session.php index 63faa6fd..8b7de15b 100644 --- a/system/session.php +++ b/system/session.php @@ -70,10 +70,9 @@ public static function load() /** * Determine if the session or flash data contains an item or set of items. * - * @param string $key * @return bool */ - public static function has($key) + public static function has() { foreach (func_get_args() as $key) { From 65dca2493926b569a3323aeb8b5e4b817dcd46db Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 11:22:40 -0700 Subject: [PATCH 11/17] Added ability to pass multiple parameters to Cookie::has. --- system/cookie.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/system/cookie.php b/system/cookie.php index 22a06b6a..ab48f988 100644 --- a/system/cookie.php +++ b/system/cookie.php @@ -83,14 +83,21 @@ public function send() } /** - * Determine if a cookie exists. + * Determine if a cookie or set of cookies exists. * - * @param string $name * @return bool */ - public static function has($name) + public static function has() { - return ( ! is_null(static::get($name))); + foreach (func_get_args() as $key) + { + if (is_null(static::get($key))) + { + return false; + } + } + + return true; } /** From 52390c4a63f1a1d48b01a0fae24127d2f7658fec Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 11:34:09 -0700 Subject: [PATCH 12/17] Added ability to pass multiple items to Input::has_old. --- system/input.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/system/input.php b/system/input.php index 20f593bb..095ca2e5 100644 --- a/system/input.php +++ b/system/input.php @@ -45,14 +45,21 @@ public static function get($key = null, $default = null) } /** - * Determine if the old input data contains an item. + * Determine if the old input data contains an item or set of items. * - * @param string $key * @return bool */ - public static function has_old($key) + public static function has_old() { - return ( ! is_null(static::old($key))); + foreach (func_get_args() as $key) + { + if (is_null($value = static::old($key)) or trim((string) $value) == '') + { + return false; + } + } + + return true; } /** From 324845a855d9506a14a2d823c71274eafe966049 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 12:42:52 -0700 Subject: [PATCH 13/17] Added Input::filled, Input::had, and Input::was_filled. --- system/input.php | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/system/input.php b/system/input.php index 095ca2e5..91b7862a 100644 --- a/system/input.php +++ b/system/input.php @@ -18,7 +18,25 @@ public static function has() { foreach (func_get_args() as $key) { - if (is_null($value = static::get($key)) or trim((string) $value) == '') + if (is_null(static::get($key))) + { + return false; + } + } + + return true; + } + + /** + * Determine if the input data contains an item or set of items that are not empty. + * + * @return bool + */ + public static function filled() + { + foreach (func_get_args() as $key) + { + if ( ! static::has($key) or trim((string) static::get($key)) == '') { return false; } @@ -49,11 +67,29 @@ public static function get($key = null, $default = null) * * @return bool */ - public static function has_old() + public static function had() { foreach (func_get_args() as $key) { - if (is_null($value = static::old($key)) or trim((string) $value) == '') + if (is_null(static::old($key))) + { + return false; + } + } + + return true; + } + + /** + * Determine if the old input data contains an item or set of items that are not empty. + * + * @return bool + */ + public static function was_filled() + { + foreach (func_get_args() as $key) + { + if ( ! static::had($key) or trim((string) static::old($key)) == '') { return false; } From 2413b9cc630ac616231ea4587fbfd3fd86922134 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 13:42:01 -0700 Subject: [PATCH 14/17] Fixed bug in route finder. --- system/route/finder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/route/finder.php b/system/route/finder.php index fb932a7e..b5b12c1c 100644 --- a/system/route/finder.php +++ b/system/route/finder.php @@ -53,7 +53,7 @@ public static function find($name) { $route = $recursiveIterator->getSubIterator(); - if ($route['name'] == $name) + if (isset($route['name'] and $route['name'] == $name) { return static::$names[$name] = array($arrayIterator->key() => iterator_to_array($route)); } From cd7e1eb6645f603aae157f9c24c6bae6667191f8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 13:43:09 -0700 Subject: [PATCH 15/17] Added missing paren from previous commit. --- system/route/finder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/route/finder.php b/system/route/finder.php index b5b12c1c..2b459e48 100644 --- a/system/route/finder.php +++ b/system/route/finder.php @@ -53,7 +53,7 @@ public static function find($name) { $route = $recursiveIterator->getSubIterator(); - if (isset($route['name'] and $route['name'] == $name) + if (isset($route['name']) and $route['name'] == $name) { return static::$names[$name] = array($arrayIterator->key() => iterator_to_array($route)); } From 5ddab2bf9666d46c1b2dd034275041d3c29d827c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 23:21:51 -0500 Subject: [PATCH 16/17] fixed typo in form class. --- system/form.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/form.php b/system/form.php index b303c0ed..7b594fc8 100644 --- a/system/form.php +++ b/system/form.php @@ -56,7 +56,7 @@ public static function open($action = null, $method = 'POST', $attributes = arra /** * Close a HTML form. * - * @return void + * @return string */ public static function close() { From 04fb81367fd98c5eda0009b22b4202f6694ab388 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 23:29:56 -0500 Subject: [PATCH 17/17] added support for alpha only random strings in Str::random. --- system/str.php | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/system/str.php b/system/str.php index 17370e7f..7ab40159 100644 --- a/system/str.php +++ b/system/str.php @@ -47,20 +47,43 @@ public static function title($value) } /** - * Generate a random alpha-numeric string. + * Generate a random alpha or alpha-numeric string. + * + * Supported types: 'alnum' and 'alpha'. * * @param int $length + * @param string $type * @return string */ - public static function random($length = 16) + public static function random($length = 16, $type = 'alnum') { - $pool = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 1); - $value = ''; + // ----------------------------------------------------- + // Get the proper character pool for the type. + // ----------------------------------------------------- + switch ($type) + { + case 'alpha': + $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + break; + + default: + $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + } + + // ----------------------------------------------------- + // Get the pool length and split the pool into an array. + // ----------------------------------------------------- + $pool_length = strlen($pool) - 1; + $pool = str_split($pool, 1); + + // ----------------------------------------------------- + // Build the random string to the specified length. + // ----------------------------------------------------- for ($i = 0; $i < $length; $i++) { - $value .= $pool[mt_rand(0, 61)]; + $value .= $pool[mt_rand(0, $pool_length)]; } return $value;