diff --git a/README.md b/README.md index 0aeb668..0b90709 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,20 @@ location / { Nothing special is required for Apache to work. We've include the `.htaccess` file in the `public` folder. If rewriting is not working for you, please check that the `mod_rewrite` module (htaccess support) is enabled in the Apache configuration. +#### .htaccess example + +Below is an example of an working `.htaccess` file used by simple-php-router. + +Simply create a new `.htaccess` file in your projects `public` directory and paste the contents below in your newly created file. This will redirect all requests to your `index.php` file (see Configuration section below). + +``` +RewriteEngine on +RewriteCond %{SCRIPT_FILENAME} !-f +RewriteCond %{SCRIPT_FILENAME} !-d +RewriteCond %{SCRIPT_FILENAME} !-l +RewriteRule ^(.*)$ index.php/$1 +``` + ### Configuration Create a new file, name it `routes.php` and place it in your library folder. This will be the file where you define all the routes for your project. diff --git a/src/Pecee/CsrfToken.php b/src/Pecee/CsrfToken.php index d161974..6080827 100644 --- a/src/Pecee/CsrfToken.php +++ b/src/Pecee/CsrfToken.php @@ -10,6 +10,7 @@ class CsrfToken /** * Generate random identifier for CSRF token * + * @throws \RuntimeException * @return string */ public static function generateToken() @@ -18,7 +19,14 @@ class CsrfToken return bin2hex(random_bytes(32)); } - return bin2hex(openssl_random_pseudo_bytes(32)); + $isSourceStrong = false; + + $random = openssl_random_pseudo_bytes(32, $isSourceStrong); + if ($isSourceStrong === false || $random === false) { + throw new \RuntimeException('IV generation failed'); + } + + return $random; } /** diff --git a/src/Pecee/Http/Input/Input.php b/src/Pecee/Http/Input/Input.php index 57825fd..c3732dd 100644 --- a/src/Pecee/Http/Input/Input.php +++ b/src/Pecee/Http/Input/Input.php @@ -60,7 +60,7 @@ class Input { $list = []; - foreach ($_FILES as $key => $value) { + foreach ((array)$_FILES as $key => $value) { // Handle array input if (is_array($value['name']) === false) { diff --git a/src/Pecee/SimpleRouter/Route/RouteController.php b/src/Pecee/SimpleRouter/Route/RouteController.php index 2347d2c..6ef22e8 100644 --- a/src/Pecee/SimpleRouter/Route/RouteController.php +++ b/src/Pecee/SimpleRouter/Route/RouteController.php @@ -66,7 +66,7 @@ class RouteController extends LoadableRoute implements IControllerRoute foreach (static::$requestTypes as $requestType) { if (stripos($method, $requestType) === 0) { - $method = substr($method, strlen($requestType)); + $method = (string)substr($method, strlen($requestType)); break; } } diff --git a/src/Pecee/SimpleRouter/Route/RouteResource.php b/src/Pecee/SimpleRouter/Route/RouteResource.php index 050e998..2bf54cf 100644 --- a/src/Pecee/SimpleRouter/Route/RouteResource.php +++ b/src/Pecee/SimpleRouter/Route/RouteResource.php @@ -53,7 +53,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute /* Remove method/type */ if (strpos($name, '.') !== false) { - $name = substr($name, 0, strrpos($name, '.')); + $name = (string)substr($name, 0, strrpos($name, '.')); } return (strtolower($this->name) === strtolower($name)); diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index dd4de09..c085032 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -33,6 +33,10 @@ class SimpleRouter */ protected static $response; + /** + * Router instance + * @var Router + */ protected static $router; /** @@ -214,7 +218,7 @@ class SimpleRouter * @param string $url * @param string|\Closure $callback * @param array|null $settings - * @return RouteUrl + * @return RouteUrl|IRoute */ public static function match(array $requestMethods, $url, $callback, array $settings = null) { @@ -237,7 +241,7 @@ class SimpleRouter * @param string $url * @param string|\Closure $callback * @param array|null $settings - * @return RouteUrl + * @return RouteUrl|IRoute */ public static function all($url, $callback, array $settings = null) { @@ -259,7 +263,7 @@ class SimpleRouter * @param string $url * @param string $controller * @param array|null $settings - * @return RouteController + * @return RouteController|IRoute */ public static function controller($url, $controller, array $settings = null) { @@ -281,7 +285,7 @@ class SimpleRouter * @param string $url * @param string $controller * @param array|null $settings - * @return RouteResource + * @return RouteResource|IRoute */ public static function resource($url, $controller, array $settings = null) { diff --git a/test/RouterRouteTest.php b/test/RouterRouteTest.php index ae045ee..d913248 100644 --- a/test/RouterRouteTest.php +++ b/test/RouterRouteTest.php @@ -77,10 +77,10 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase public function testPathParamRegex() { - TestRouter::get('/test/path/{myParam}', 'DummyController@param', ['where' => ['myParam' => '([0-9]+)']]); - $response = TestRouter::debugOutput('/test/path/123123', 'get'); + TestRouter::get('/{lang}/productscategories/{name}', 'DummyController@param', ['where' => ['lang' => '[a-z]+', 'name' => '[A-Za-z0-9\-]+']]); + $response = TestRouter::debugOutput('/it/productscategories/system', 'get'); - $this->assertEquals('123123', $response); + $this->assertEquals('it, system', $response); } public function testDomainAllowedRoute()