Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,17 @@ class CustomExceptionHandler implements IExceptionHandler
return;

}

/* Other error */
if($error instanceof MyCustomException) {

$request->setRewriteRoute(
// Add new route based on current url (minus query-string) and add custom parameters.
(new RouteUrl(url(null, null, []), 'PageController@error'))->setParameters(['exception' => $error])
);
return;

}

throw $error;

Expand Down
43 changes: 24 additions & 19 deletions src/Pecee/Http/Middleware/BaseCsrfVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class BaseCsrfVerifier implements IMiddleware
* For example: /admin/*
* @var array|null
*/
protected ?array $except = null;
protected array $except = [];

/**
* Urls to include. Can be used to include urls from a certain path.
* @var array|null
*/
protected ?array $include = null;
protected array $include = [];

/**
* @var ITokenProvider
Expand All @@ -38,18 +38,35 @@ public function __construct()
$this->tokenProvider = new CookieTokenProvider();
}

protected function isIncluded(Request $request): bool
{
if (count($this->include) > 0) {
foreach ($this->include as $includeUrl) {
$includeUrl = rtrim($includeUrl, '/');
if ($includeUrl[strlen($includeUrl) - 1] === '*') {
$includeUrl = rtrim($includeUrl, '*');
return $request->getUrl()->contains($includeUrl);
}

return ($includeUrl === rtrim($request->getUrl()->getRelativeUrl(false), '/'));
}
}

return false;
}

/**
* Check if the url matches the urls in the except property
* @param Request $request
* @return bool
*/
protected function skip(Request $request): bool
{
if ($this->except === null || count($this->except) === 0) {
if (count($this->except) === 0) {
return false;
}

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

if ($skip === true) {

if(is_array($this->include) === true && count($this->include) > 0) {
foreach($this->include as $includeUrl) {
$includeUrl = rtrim($includeUrl, '/');
if ($includeUrl[strlen($includeUrl) - 1] === '*') {
$includeUrl = rtrim($includeUrl, '*');
$skip = !$request->getUrl()->contains($includeUrl);
break;
}

$skip = !($includeUrl === rtrim($request->getUrl()->getRelativeUrl(false), '/'));
}
}
$skip = !$this->isIncluded($request);

if($skip === false) {
if ($skip === false) {
continue;
}

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

$token = $request->getInputHandler()->value(
static::POST_KEY,
$request->getHeader(static::HEADER_KEY),
Request::$requestTypesPost
);

if ($this->tokenProvider->validate((string)$token) === false) {
Expand Down
12 changes: 6 additions & 6 deletions src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ public function loadClass(string $class)
* @param object $class
* @param string $method
* @param array $parameters
* @return mixed
* @return string
*/
public function loadClassMethod($class, string $method, array $parameters)
public function loadClassMethod($class, string $method, array $parameters): string
{
return call_user_func_array([$class, $method], array_values($parameters));
return (string)call_user_func_array([$class, $method], array_values($parameters));
}

/**
* Load closure
*
* @param Callable $closure
* @param array $parameters
* @return mixed
* @return string
*/
public function loadClosure(Callable $closure, array $parameters)
public function loadClosure(callable $closure, array $parameters): string
{
return call_user_func_array($closure, array_values($parameters));
return (string)call_user_func_array($closure, array_values($parameters));
}

}
8 changes: 4 additions & 4 deletions src/Pecee/SimpleRouter/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public function start(): ?string
/* Verify csrf token for request */
$this->csrfVerifier->handle($this->request);
} catch(Exception $e) {
$this->handleException($e);
return $this->handleException($e);
}
}

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

if ($this->renderMultipleRoutes === true) {
if ($routeOutput !== null) {
if ($routeOutput !== '') {
return $routeOutput;
}

Expand All @@ -444,12 +444,12 @@ public function routeRequest(): ?string
}

} catch (Exception $e) {
$this->handleException($e);
return $this->handleException($e);
}

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

if (count($this->request->getLoadedRoutes()) === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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

protected ?array $except = [
protected array $except = [
'/exclude-page',
'/exclude-all/*',
];

protected ?array $include = [
protected array $include = [
'/exclude-all/include-page',
];

Expand Down