diff --git a/application/config/aliases.php b/application/config/aliases.php index 707ff07b..58d33a60 100644 --- a/application/config/aliases.php +++ b/application/config/aliases.php @@ -39,6 +39,7 @@ 'IoC' => 'Laravel\\IoC', 'Lang' => 'Laravel\\Facades\\Lang', 'Loader' => 'Laravel\\Facades\\Loader', + 'Messages' => 'Laravel\\Validation\\Messages', 'Package' => 'Laravel\\Facades\\Package', 'URL' => 'Laravel\\Facades\\URL', 'Redirect' => 'Laravel\\Facades\\Redirect', diff --git a/application/filters.php b/application/filters.php index d7db2678..5150cf5c 100644 --- a/application/filters.php +++ b/application/filters.php @@ -62,7 +62,7 @@ 'csrf' => function() { - return (Input::get('csrf_token') !== Form::raw_token()) ? new Error('500') : null; + return (Input::get('csrf_token') !== Form::raw_token()) ? Response::error('500') : null; }, ); \ No newline at end of file diff --git a/laravel/arr.php b/laravel/arr.php index ca88753c..af2d6407 100644 --- a/laravel/arr.php +++ b/laravel/arr.php @@ -110,4 +110,29 @@ public static function first($array, $callback, $default = null) return ($default instanceof \Closure) ? call_user_func($default) : $default; } + /** + * Remove all values in the array that are contained within a given array of values. + * + * + * // Remove all empty string values from an array + * $array = Arr::without($array, array('')); + * + * // Remove all array values that are "3", "2", or "1" + * $array = Arr::without($array, array(3, 2, 1)); + * + * + * @param array $array + * @param array $without + * @return array + */ + public static function without($array, $without = array()) + { + foreach ($array as $key => $value) + { + if (in_array($value, $without)) unset($array[$key]); + } + + return $array; + } + } \ No newline at end of file diff --git a/laravel/config/container.php b/laravel/config/container.php index c8152e94..cc179d80 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -79,7 +79,7 @@ 'laravel.hasher' => array('singleton' => true, 'resolver' => function($container) { - return new Security\Hashing\Bcrypt(8); + return new Security\Hashing\Bcrypt(8, false); }), @@ -158,9 +158,15 @@ }), - 'laravel.routing.router' => array('singleton' => true, 'resolver' => function($container) + 'laravel.routing.router' => array('singleton' => true, 'resolver' => function($c) { - return new Routing\Router(require APP_PATH.'routes'.EXT, CONTROLLER_PATH); + return new Routing\Router($c->resolve('laravel.routing.loader'), CONTROLLER_PATH); + }), + + + 'laravel.routing.loader' => array('singleton' => true, 'resolver' => function($container) + { + return new Routing\Loader(APP_PATH, ROUTE_PATH); }), diff --git a/laravel/bootstrap.php b/laravel/core.php similarity index 97% rename from laravel/bootstrap.php rename to laravel/core.php index 5dba2621..7962a561 100644 --- a/laravel/bootstrap.php +++ b/laravel/core.php @@ -1,71 +1,72 @@ -resolve('laravel.loader'), 'load')); - -// -------------------------------------------------------------- -// Set the application environment configuration option. -// -------------------------------------------------------------- +resolve('laravel.loader'), 'load')); + +// -------------------------------------------------------------- +// Set the application environment configuration option. +// -------------------------------------------------------------- $container->resolve('laravel.config')->set('application.env', $env); \ No newline at end of file diff --git a/laravel/laravel.php b/laravel/laravel.php index ba754276..36cf0ec2 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -3,7 +3,7 @@ // -------------------------------------------------------------- // Bootstrap the core framework components. // -------------------------------------------------------------- -require 'bootstrap.php'; +require 'core.php'; // -------------------------------------------------------------- // Get an instance of the configuration manager. diff --git a/laravel/routing/caller.php b/laravel/routing/caller.php index 549a2660..9bcb68dc 100644 --- a/laravel/routing/caller.php +++ b/laravel/routing/caller.php @@ -4,6 +4,28 @@ use Laravel\Response; use Laravel\Container; +class Delegate { + + /** + * The destination of the route delegate. + * + * @var string + */ + public $destination; + + /** + * Create a new route delegate instance. + * + * @param string $destination + * @return void + */ + public function __construct($destination) + { + $this->destination = $destination; + } + +} + class Caller { /** @@ -60,10 +82,10 @@ public function call(Route $route) if ( ! is_null($response = $route->call())) { - // If a route returns a string, it also means the route is delegating the - // handling of the request to a controller method. So, we will pass the - // string to the route delegator, exploding on "@". - if (is_string($response)) $response = $this->delegate($route, $response); + // If a route returns a Delegate, it also means the route is delegating the + // handling of the request to a controller method. So, we will pass the string + // to the route delegator, exploding on "@". + if ($response instanceof Delegate) $response = $this->delegate($route, $response->destination); return $this->finish($route, $response); } @@ -98,6 +120,11 @@ protected function before(Route $route) */ protected function delegate(Route $route, $delegate) { + if (strpos($delegate, '@') === false) + { + throw new \Exception("Route delegate [$delegate] has an invalid format."); + } + list($controller, $method) = explode('@', $delegate); $controller = $this->resolve($controller); diff --git a/laravel/routing/loader.php b/laravel/routing/loader.php new file mode 100644 index 00000000..ac8196df --- /dev/null +++ b/laravel/routing/loader.php @@ -0,0 +1,107 @@ +base = $base; + $this->nest = $nest; + } + + /** + * Load the applicable routes for a given URI. + * + * @param string $uri + * @return array + */ + public function load($uri) + { + $routes = (file_exists($path = $this->base.'routes'.EXT)) ? require $path : array(); + + return array_merge($this->nested(Arr::without(explode('/', $uri), array(''))), $routes); + } + + /** + * Get the appropriate routes from the routes directory for a given URI. + * + * @param array $segments + * @return array + */ + protected function nested($segments) + { + // Work backwards through the URI segments until we find the deepest possible + // matching route directory. Once we find it, we will return those routes. + foreach (array_reverse($segments, true) as $key => $value) + { + if (file_exists($path = $this->nest.implode('/', array_slice($segments, 0, $key + 1)).EXT)) + { + return require $path; + } + elseif (file_exists($path = str_replace('.php', '/routes.php', $path))) + { + return require $path; + } + } + + return array(); + } + + /** + * Get every route defined for the application. + * + * @return array + */ + public function everything() + { + if ( ! is_null($this->everything)) return $this->everything; + + $routes = array(); + + // Since route files can be nested deep within the route directory, we need to + // recursively spin through the directory to find every file. + $directoryIterator = new \RecursiveDirectoryIterator($this->nest); + + $recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST); + + foreach ($recursiveIterator as $file) + { + if (filetype($file) === 'file') + { + $routes = array_merge(require $file, $routes); + } + } + + return $this->everything = $routes; + } + +} \ No newline at end of file diff --git a/laravel/routing/route.php b/laravel/routing/route.php index c54f3867..149a87bc 100644 --- a/laravel/routing/route.php +++ b/laravel/routing/route.php @@ -95,11 +95,13 @@ public function call() } // Otherwise, we will assume the route is an array and will return the first value with - // a key of "do", or the first instance of a Closure. If the value is a string, the route - // is delegating the responsibility for handling the request to a controller. + // a key of "delegate", or the first instance of a Closure. If the value is a string, the + // route is delegating the responsibility for handling the request to a controller. elseif (is_array($this->callback)) { - return Arr::first($this->callback, function($key, $value) {return $key == 'do' or $value instanceof Closure;}); + $callback = Arr::first($this->callback, function($key, $value) {return $key == 'delegate' or $value instanceof Closure;}); + + return ($callback instanceof Closure) ? call_user_func_array($callback, $this->parameters) : new Delegate($callback); } // If a value defined for a route is a string, it means the route is delegating control @@ -107,7 +109,7 @@ public function call() // for the route caller to parse and delegate. elseif (is_string($this->callback)) { - return $this->callback; + return new Delegate($this->callback); } } diff --git a/laravel/routing/router.php b/laravel/routing/router.php index 6f2155cf..3e7e8a74 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -5,11 +5,11 @@ class Router { /** - * All of the routes available to the router. + * The route loader instance. * - * @var array + * @var Loader */ - public $routes; + public $loader; /** * The named routes that have been found so far. @@ -28,13 +28,13 @@ class Router { /** * Create a new router for a request method and URI. * - * @param array $routes + * @param Loader $loader * @param string $controllers * @return void */ - public function __construct($routes, $controllers) + public function __construct(Loader $loader, $controllers) { - $this->routes = $routes; + $this->loader = $loader; $this->controllers = $controllers; } @@ -50,7 +50,7 @@ public function find($name) { if (array_key_exists($name, $this->names)) return $this->names[$name]; - $arrayIterator = new \RecursiveArrayIterator($this->routes); + $arrayIterator = new \RecursiveArrayIterator($this->loader->everything()); $recursiveIterator = new \RecursiveIteratorIterator($arrayIterator); @@ -75,18 +75,20 @@ public function find($name) */ public function route(Request $request) { + $routes = $this->loader->load($request->uri()); + // Put the request method and URI in route form. Routes begin with // the request method and a forward slash. $destination = $request->method().' /'.trim($request->uri(), '/'); // Check for a literal route match first. If we find one, there is // no need to spin through all of the routes. - if (isset($this->routes[$destination])) + if (isset($routes[$destination])) { - return $request->route = new Route($destination, $this->routes[$destination], array()); + return $request->route = new Route($destination, $routes[$destination], array()); } - foreach ($this->routes as $keys => $callback) + foreach ($routes as $keys => $callback) { // Only check routes that have multiple URIs or wildcards. // Other routes would have been caught by the check for literal matches. diff --git a/laravel/security/hashing/bcrypt.php b/laravel/security/hashing/bcrypt.php index a385e3b4..b2e61157 100644 --- a/laravel/security/hashing/bcrypt.php +++ b/laravel/security/hashing/bcrypt.php @@ -1,53 +1,254 @@ in 2004-2006 and placed in +# the public domain. Revised in subsequent years, still public domain. +# +# There's absolutely no warranty. +# +# The homepage URL for this framework is: +# +# http://www.openwall.com/phpass/ +# +# Please be sure to update the Version line if you edit this file in any way. +# It is suggested that you leave the main version number intact, but indicate +# your project name (after the slash) and add your own revision information. +# +# Please do not change the "private" password hashing method implemented in +# here, thereby making your hashes incompatible. However, if you must, please +# change the hash type identifier (the "$P$") to something different. +# +# Obviously, since this code is in the public domain, the above are not +# requirements (there can be none), but merely suggestions. +# class Bcrypt implements Engine { + var $itoa64; + var $iteration_count_log2; + var $portable_hashes; + var $random_state; - /** - * The number of iterations that should be performed. - * - * @var int - */ - protected $rounds; - - /** - * Create a new bcrypt hashing engine. - * - * @param int $rounds - * @return void - */ - public function __construct($rounds) + public function __construct($iteration_count_log2, $portable_hashes) { - $this->rounds = $rounds; + $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; - if ( ! function_exists('openssl_random_pseudo_bytes')) - { - throw new \Exception("The openssl PHP extension is required to perform bcrypt hashing."); + if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31) + $iteration_count_log2 = 8; + $this->iteration_count_log2 = $iteration_count_log2; + + $this->portable_hashes = $portable_hashes; + + $this->random_state = microtime(); + if (function_exists('getmypid')) + $this->random_state .= getmypid(); + } + + protected function get_random_bytes($count) + { + $output = ''; + if (is_readable('/dev/urandom') && + ($fh = @fopen('/dev/urandom', 'rb'))) { + $output = fread($fh, $count); + fclose($fh); } + + if (strlen($output) < $count) { + $output = ''; + for ($i = 0; $i < $count; $i += 16) { + $this->random_state = + md5(microtime() . $this->random_state); + $output .= + pack('H*', md5($this->random_state)); + } + $output = substr($output, 0, $count); + } + + return $output; } - /** - * Perform a one-way hash on a string using bcrypt. - * - * @param string $value - * @return string - */ - public function hash($value) + protected function encode64($input, $count) { - $salt = sprintf('$2a$%02d$', $this->rounds).substr(base64_encode(openssl_random_pseudo_bytes(16)), 0, 22); + $output = ''; + $i = 0; + do { + $value = ord($input[$i++]); + $output .= $this->itoa64[$value & 0x3f]; + if ($i < $count) + $value |= ord($input[$i]) << 8; + $output .= $this->itoa64[($value >> 6) & 0x3f]; + if ($i++ >= $count) + break; + if ($i < $count) + $value |= ord($input[$i]) << 16; + $output .= $this->itoa64[($value >> 12) & 0x3f]; + if ($i++ >= $count) + break; + $output .= $this->itoa64[($value >> 18) & 0x3f]; + } while ($i < $count); - return crypt($value, str_replace('+', '.', $salt)); + return $output; } - /** - * Determine if an unhashed value matches a given hash. - * - * @param string $value - * @param string $hash - * @return bool - */ - public function check($value, $hash) + protected function gensalt_private($input) { - return crypt($value, $hash) === $hash; + $output = '$P$'; + $output .= $this->itoa64[min($this->iteration_count_log2 + + ((PHP_VERSION >= '5') ? 5 : 3), 30)]; + $output .= $this->encode64($input, 6); + + return $output; } -} \ No newline at end of file + protected function crypt_private($password, $setting) + { + $output = '*0'; + if (substr($setting, 0, 2) == $output) + $output = '*1'; + + $id = substr($setting, 0, 3); + # We use "$P$", phpBB3 uses "$H$" for the same thing + if ($id != '$P$' && $id != '$H$') + return $output; + + $count_log2 = strpos($this->itoa64, $setting[3]); + if ($count_log2 < 7 || $count_log2 > 30) + return $output; + + $count = 1 << $count_log2; + + $salt = substr($setting, 4, 8); + if (strlen($salt) != 8) + return $output; + + # We're kind of forced to use MD5 here since it's the only + # cryptographic primitive available in all versions of PHP + # currently in use. To implement our own low-level crypto + # in PHP would result in much worse performance and + # consequently in lower iteration counts and hashes that are + # quicker to crack (by non-PHP code). + if (PHP_VERSION >= '5') { + $hash = md5($salt . $password, TRUE); + do { + $hash = md5($hash . $password, TRUE); + } while (--$count); + } else { + $hash = pack('H*', md5($salt . $password)); + do { + $hash = pack('H*', md5($hash . $password)); + } while (--$count); + } + + $output = substr($setting, 0, 12); + $output .= $this->encode64($hash, 16); + + return $output; + } + + protected function gensalt_extended($input) + { + $count_log2 = min($this->iteration_count_log2 + 8, 24); + # This should be odd to not reveal weak DES keys, and the + # maximum valid value is (2**24 - 1) which is odd anyway. + $count = (1 << $count_log2) - 1; + + $output = '_'; + $output .= $this->itoa64[$count & 0x3f]; + $output .= $this->itoa64[($count >> 6) & 0x3f]; + $output .= $this->itoa64[($count >> 12) & 0x3f]; + $output .= $this->itoa64[($count >> 18) & 0x3f]; + + $output .= $this->encode64($input, 3); + + return $output; + } + + protected function gensalt_blowfish($input) + { + # This one needs to use a different order of characters and a + # different encoding scheme from the one in encode64() above. + # We care because the last character in our encoded string will + # only represent 2 bits. While two known implementations of + # bcrypt will happily accept and correct a salt string which + # has the 4 unused bits set to non-zero, we do not want to take + # chances and we also do not want to waste an additional byte + # of entropy. + $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + + $output = '$2a$'; + $output .= chr(ord('0') + $this->iteration_count_log2 / 10); + $output .= chr(ord('0') + $this->iteration_count_log2 % 10); + $output .= '$'; + + $i = 0; + do { + $c1 = ord($input[$i++]); + $output .= $itoa64[$c1 >> 2]; + $c1 = ($c1 & 0x03) << 4; + if ($i >= 16) { + $output .= $itoa64[$c1]; + break; + } + + $c2 = ord($input[$i++]); + $c1 |= $c2 >> 4; + $output .= $itoa64[$c1]; + $c1 = ($c2 & 0x0f) << 2; + + $c2 = ord($input[$i++]); + $c1 |= $c2 >> 6; + $output .= $itoa64[$c1]; + $output .= $itoa64[$c2 & 0x3f]; + } while (1); + + return $output; + } + + public function hash($password) + { + $random = ''; + + if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) { + $random = $this->get_random_bytes(16); + $hash = + crypt($password, $this->gensalt_blowfish($random)); + if (strlen($hash) == 60) + return $hash; + } + + if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) { + if (strlen($random) < 3) + $random = $this->get_random_bytes(3); + $hash = + crypt($password, $this->gensalt_extended($random)); + if (strlen($hash) == 20) + return $hash; + } + + if (strlen($random) < 6) + $random = $this->get_random_bytes(6); + $hash = + $this->crypt_private($password, + $this->gensalt_private($random)); + if (strlen($hash) == 34) + return $hash; + + # Returning '*' on error is safe here, but would _not_ be safe + # in a crypt(3)-like function used _both_ for generating new + # hashes and for validating passwords against existing hashes. + return '*'; + } + + public function check($password, $stored_hash) + { + $hash = $this->crypt_private($password, $stored_hash); + if ($hash[0] == '*') + $hash = crypt($password, $stored_hash); + + return $hash == $stored_hash; + } +} + +?> diff --git a/laravel/session/transporters/cookie.php b/laravel/session/transporters/cookie.php index 98074d85..1590b272 100644 --- a/laravel/session/transporters/cookie.php +++ b/laravel/session/transporters/cookie.php @@ -33,12 +33,9 @@ public function get($config) */ public function put($id, $config) { - if ( ! headers_sent()) - { - $minutes = ($config['expire_on_close']) ? 0 : $config['lifetime']; + $minutes = ($config['expire_on_close']) ? 0 : $config['lifetime']; - $this->cookie->put('laravel_session', $id, $minutes, $config['path'], $config['domain']); - } + $this->cookie->put('laravel_session', $id, $minutes, $config['path'], $config['domain']); } } \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 888560fe..8c33c957 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -23,4 +23,4 @@ $public = 'public'; -require realpath($laravel).'/bootstrap.php'; \ No newline at end of file +require realpath($laravel).'/core.php'; \ No newline at end of file