diff --git a/README.md b/README.md index f42d237..fbf6665 100644 --- a/README.md +++ b/README.md @@ -726,6 +726,36 @@ When you've created your CSRF-verifier you need to tell simple-php-router that i Router::csrfVerifier(new \Demo\Middlewares\CsrfVerifier()); ``` +## Getting CSRF-token + +When posting to any of the urls that has CSRF-verification enabled, you need post your CSRF-token or else the request will get rejected. + +You can get the CSRF-token by calling the helper method: + +```php +csrf_token(); +``` + +You can also get the token directly: + +```php +return Router::router()->getCsrfVerifier()->getTokenProvider()->getToken(); +``` + +The default name/key for the input-field is `csrf_token` and is defined in the `POST_KEY` constant in the `BaseCsrfVerifier` class. +You can change the key by overwriting the constant in your own CSRF-verifier class. + +**Example:** + +The example below will post to the current url with a hidden field "`csrf_token`". + +```html +
+``` + ## Custom CSRF-verifier Create a new class and extend the `BaseCsrfVerifier` middleware class provided by default with the simple-php-router library. @@ -790,36 +820,6 @@ $verifier->setTokenProvider(new SessionTokenProvider()); Router::csrfVerifier($verifier); ``` -## Getting CSRF-token - -When posting to any of the urls that has CSRF-verification enabled, you need post your CSRF-token or else the request will get rejected. - -You can get the CSRF-token by calling the helper method: - -```php -csrf_token(); -``` - -You can also get the token directly: - -```php -return Router::router()->getCsrfVerifier()->getTokenProvider()->getToken(); -``` - -The default name/key for the input-field is `csrf_token` and is defined in the `POST_KEY` constant in the `BaseCsrfVerifier` class. -You can change the key by overwriting the constant in your own CSRF-verifier class. - -**Example:** - -The example below will post to the current url with a hidden field "`csrf_token`". - -```html - -``` - --- # Middlewares diff --git a/src/Pecee/Http/Input/InputFile.php b/src/Pecee/Http/Input/InputFile.php index c65eb1b..56faef5 100644 --- a/src/Pecee/Http/Input/InputFile.php +++ b/src/Pecee/Http/Input/InputFile.php @@ -28,7 +28,7 @@ class InputFile implements IInputItem */ public static function createFromArray(array $values) { - if (array_key_exists('index', $values) === false) { + if (isset('index', $values) === false) { throw new \InvalidArgumentException('Index key is required'); } diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 5c124d0..2fb8068 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -227,26 +227,7 @@ class Request */ public function setRewriteRoute(ILoadableRoute $route) { - $this->rewriteRoute = $route; - - $callback = $route->getCallback(); - - /* Only add default namespace on relative callbacks */ - if ($callback === null || $callback[0] !== '\\') { - - $namespace = SimpleRouter::getDefaultNamespace(); - - if ($namespace !== null) { - - if ($this->rewriteRoute->getNamespace() !== null) { - $namespace .= '\\' . $this->rewriteRoute->getNamespace(); - } - - $this->rewriteRoute->setDefaultNamespace($namespace); - - } - - } + $this->rewriteRoute = SimpleRouter::addDefaultNamespace($route); return $this; } diff --git a/src/Pecee/Http/Uri.php b/src/Pecee/Http/Uri.php index 64a7798..0ab926c 100644 --- a/src/Pecee/Http/Uri.php +++ b/src/Pecee/Http/Uri.php @@ -6,20 +6,20 @@ class Uri { private $originalUrl; private $data = [ - 'scheme', - 'host', - 'port', - 'user', - 'pass', - 'path', - 'query', - 'fragment', + 'scheme' => '', + 'host' => '', + 'port' => '', + 'user' => '', + 'pass' => '', + 'path' => '', + 'query' => '', + 'fragment' => '', ]; public function __construct($url) { $this->originalUrl = $url; - $this->data = array_merge($this->data, $this->parseUrl(urldecode($url))); + $this->data = $this->parseUrl($url) + $this->data; if (isset($this->data['path']) === true && $this->data['path'] !== '/') { $this->data['path'] = rtrim($this->data['path'], '/') . '/'; @@ -134,15 +134,7 @@ class Uri */ public function parseUrl($url, $component = -1) { - $encodedUrl = preg_replace_callback( - '%[^:/@?&=#]+%u', - function ($matches) { - return urlencode($matches[0]); - }, - $url - ); - - $parts = parse_url($encodedUrl, $component); + $parts = parse_url(urlencode($url), $component); if ($parts === false) { throw new \InvalidArgumentException('Malformed URL: ' . $url); diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index c14e47e..5b9e287 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -420,7 +420,7 @@ class SimpleRouter * @param IRoute $route * @return IRoute */ - protected static function addDefaultNamespace(IRoute $route) + public static function addDefaultNamespace(IRoute $route) { if (static::$defaultNamespace !== null) {