Skip to content

Commit b937b61

Browse files
authored
Merge pull request #651 from skipperbent/v5-development
Version 5.3.0.0
2 parents fadb783 + 5ac7473 commit b937b61

File tree

5 files changed

+47
-31
lines changed

5 files changed

+47
-31
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,17 @@ class CustomExceptionHandler implements IExceptionHandler
10361036
return;
10371037

10381038
}
1039+
1040+
/* Other error */
1041+
if($error instanceof MyCustomException) {
1042+
1043+
$request->setRewriteRoute(
1044+
// Add new route based on current url (minus query-string) and add custom parameters.
1045+
(new RouteUrl(url(null, null, []), 'PageController@error'))->setParameters(['exception' => $error])
1046+
);
1047+
return;
1048+
1049+
}
10391050

10401051
throw $error;
10411052

src/Pecee/Http/Middleware/BaseCsrfVerifier.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ class BaseCsrfVerifier implements IMiddleware
1717
* For example: /admin/*
1818
* @var array|null
1919
*/
20-
protected ?array $except = null;
20+
protected array $except = [];
2121

2222
/**
2323
* Urls to include. Can be used to include urls from a certain path.
2424
* @var array|null
2525
*/
26-
protected ?array $include = null;
26+
protected array $include = [];
2727

2828
/**
2929
* @var ITokenProvider
@@ -38,18 +38,35 @@ public function __construct()
3838
$this->tokenProvider = new CookieTokenProvider();
3939
}
4040

41+
protected function isIncluded(Request $request): bool
42+
{
43+
if (count($this->include) > 0) {
44+
foreach ($this->include as $includeUrl) {
45+
$includeUrl = rtrim($includeUrl, '/');
46+
if ($includeUrl[strlen($includeUrl) - 1] === '*') {
47+
$includeUrl = rtrim($includeUrl, '*');
48+
return $request->getUrl()->contains($includeUrl);
49+
}
50+
51+
return ($includeUrl === rtrim($request->getUrl()->getRelativeUrl(false), '/'));
52+
}
53+
}
54+
55+
return false;
56+
}
57+
4158
/**
4259
* Check if the url matches the urls in the except property
4360
* @param Request $request
4461
* @return bool
4562
*/
4663
protected function skip(Request $request): bool
4764
{
48-
if ($this->except === null || count($this->except) === 0) {
65+
if (count($this->except) === 0) {
4966
return false;
5067
}
5168

52-
foreach($this->except as $url) {
69+
foreach ($this->except as $url) {
5370
$url = rtrim($url, '/');
5471
if ($url[strlen($url) - 1] === '*') {
5572
$url = rtrim($url, '*');
@@ -60,20 +77,9 @@ protected function skip(Request $request): bool
6077

6178
if ($skip === true) {
6279

63-
if(is_array($this->include) === true && count($this->include) > 0) {
64-
foreach($this->include as $includeUrl) {
65-
$includeUrl = rtrim($includeUrl, '/');
66-
if ($includeUrl[strlen($includeUrl) - 1] === '*') {
67-
$includeUrl = rtrim($includeUrl, '*');
68-
$skip = !$request->getUrl()->contains($includeUrl);
69-
break;
70-
}
71-
72-
$skip = !($includeUrl === rtrim($request->getUrl()->getRelativeUrl(false), '/'));
73-
}
74-
}
80+
$skip = !$this->isIncluded($request);
7581

76-
if($skip === false) {
82+
if ($skip === false) {
7783
continue;
7884
}
7985

@@ -92,12 +98,11 @@ protected function skip(Request $request): bool
9298
*/
9399
public function handle(Request $request): void
94100
{
95-
if ($this->skip($request) === false && $request->isPostBack() === true) {
101+
if ($this->skip($request) === false && ($request->isPostBack() === true || $this->isIncluded($request) === true)) {
96102

97103
$token = $request->getInputHandler()->value(
98104
static::POST_KEY,
99105
$request->getHeader(static::HEADER_KEY),
100-
Request::$requestTypesPost
101106
);
102107

103108
if ($this->tokenProvider->validate((string)$token) === false) {

src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ public function loadClass(string $class)
2727
* @param object $class
2828
* @param string $method
2929
* @param array $parameters
30-
* @return mixed
30+
* @return string
3131
*/
32-
public function loadClassMethod($class, string $method, array $parameters)
32+
public function loadClassMethod($class, string $method, array $parameters): string
3333
{
34-
return call_user_func_array([$class, $method], array_values($parameters));
34+
return (string)call_user_func_array([$class, $method], array_values($parameters));
3535
}
3636

3737
/**
3838
* Load closure
3939
*
4040
* @param Callable $closure
4141
* @param array $parameters
42-
* @return mixed
42+
* @return string
4343
*/
44-
public function loadClosure(Callable $closure, array $parameters)
44+
public function loadClosure(callable $closure, array $parameters): string
4545
{
46-
return call_user_func_array($closure, array_values($parameters));
46+
return (string)call_user_func_array($closure, array_values($parameters));
4747
}
4848

4949
}

src/Pecee/SimpleRouter/Router.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public function start(): ?string
346346
/* Verify csrf token for request */
347347
$this->csrfVerifier->handle($this->request);
348348
} catch(Exception $e) {
349-
$this->handleException($e);
349+
return $this->handleException($e);
350350
}
351351
}
352352

@@ -427,7 +427,7 @@ public function routeRequest(): ?string
427427
$routeOutput = $route->renderRoute($this->request, $this);
428428

429429
if ($this->renderMultipleRoutes === true) {
430-
if ($routeOutput !== null) {
430+
if ($routeOutput !== '') {
431431
return $routeOutput;
432432
}
433433

@@ -444,12 +444,12 @@ public function routeRequest(): ?string
444444
}
445445

446446
} catch (Exception $e) {
447-
$this->handleException($e);
447+
return $this->handleException($e);
448448
}
449449

450450
if ($methodNotAllowed === true) {
451451
$message = sprintf('Route "%s" or method "%s" not allowed.', $this->request->getUrl()->getPath(), $this->request->getMethod());
452-
$this->handleException(new NotFoundHttpException($message, 403));
452+
return $this->handleException(new NotFoundHttpException($message, 403));
453453
}
454454

455455
if (count($this->request->getLoadedRoutes()) === 0) {

tests/Pecee/SimpleRouter/Dummy/CsrfVerifier/DummyCsrfVerifier.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
class DummyCsrfVerifier extends \Pecee\Http\Middleware\BaseCsrfVerifier {
44

5-
protected ?array $except = [
5+
protected array $except = [
66
'/exclude-page',
77
'/exclude-all/*',
88
];
99

10-
protected ?array $include = [
10+
protected array $include = [
1111
'/exclude-all/include-page',
1212
];
1313

0 commit comments

Comments
 (0)