mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 01:52:16 +00:00
Added support for PHP7
This commit is contained in:
@@ -5,17 +5,17 @@ namespace Pecee\Http\Input;
|
||||
interface IInputItem
|
||||
{
|
||||
|
||||
public function getIndex();
|
||||
public function getIndex(): string;
|
||||
|
||||
public function setIndex($index);
|
||||
public function setIndex($index): self;
|
||||
|
||||
public function getName();
|
||||
public function getName(): string;
|
||||
|
||||
public function setName($name);
|
||||
public function setName($name): self;
|
||||
|
||||
public function getValue();
|
||||
public function getValue(): string;
|
||||
|
||||
public function setValue($value);
|
||||
public function setValue($value): self;
|
||||
|
||||
public function __toString();
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ class InputFile implements IInputItem
|
||||
* @throws InvalidArgumentException
|
||||
* @return static
|
||||
*/
|
||||
public static function createFromArray(array $values)
|
||||
public static function createFromArray(array $values): self
|
||||
{
|
||||
if (isset($values['index']) === false) {
|
||||
throw new InvalidArgumentException('Index key is required');
|
||||
@@ -59,7 +59,7 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIndex()
|
||||
public function getIndex(): string
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
@@ -67,9 +67,9 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* Set input index
|
||||
* @param string $index
|
||||
* @return static $this
|
||||
* @return static
|
||||
*/
|
||||
public function setIndex($index)
|
||||
public function setIndex($index): IInputItem
|
||||
{
|
||||
$this->index = $index;
|
||||
|
||||
@@ -79,7 +79,7 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSize()
|
||||
public function getSize(): string
|
||||
{
|
||||
return $this->size;
|
||||
}
|
||||
@@ -87,9 +87,9 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* Set file size
|
||||
* @param int $size
|
||||
* @return static $this
|
||||
* @return static
|
||||
*/
|
||||
public function setSize($size)
|
||||
public function setSize($size): IInputItem
|
||||
{
|
||||
$this->size = $size;
|
||||
|
||||
@@ -100,7 +100,7 @@ class InputFile implements IInputItem
|
||||
* Get mime-type of file
|
||||
* @return string
|
||||
*/
|
||||
public function getMime()
|
||||
public function getMime(): string
|
||||
{
|
||||
return $this->getType();
|
||||
}
|
||||
@@ -108,7 +108,7 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
@@ -116,9 +116,9 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* Set type
|
||||
* @param string $type
|
||||
* @return static $this
|
||||
* @return static
|
||||
*/
|
||||
public function setType($type)
|
||||
public function setType($type): IInputItem
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
@@ -130,7 +130,7 @@ class InputFile implements IInputItem
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getExtension()
|
||||
public function getExtension(): string
|
||||
{
|
||||
return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
|
||||
}
|
||||
@@ -140,7 +140,7 @@ class InputFile implements IInputItem
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
@@ -150,9 +150,9 @@ class InputFile implements IInputItem
|
||||
* Useful for adding validation etc.
|
||||
*
|
||||
* @param string $name
|
||||
* @return static $this
|
||||
* @return static
|
||||
*/
|
||||
public function setName($name)
|
||||
public function setName($name): IInputItem
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
@@ -163,9 +163,9 @@ class InputFile implements IInputItem
|
||||
* Set filename
|
||||
*
|
||||
* @param string $name
|
||||
* @return static $this
|
||||
* @return static
|
||||
*/
|
||||
public function setFilename($name)
|
||||
public function setFilename($name): IInputItem
|
||||
{
|
||||
$this->filename = $name;
|
||||
|
||||
@@ -177,7 +177,7 @@ class InputFile implements IInputItem
|
||||
*
|
||||
* @return string mixed
|
||||
*/
|
||||
public function getFilename()
|
||||
public function getFilename(): string
|
||||
{
|
||||
return $this->filename;
|
||||
}
|
||||
@@ -188,7 +188,7 @@ class InputFile implements IInputItem
|
||||
* @param string $destination
|
||||
* @return bool
|
||||
*/
|
||||
public function move($destination)
|
||||
public function move($destination): bool
|
||||
{
|
||||
return move_uploaded_file($this->tmpName, $destination);
|
||||
}
|
||||
@@ -198,7 +198,7 @@ class InputFile implements IInputItem
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContents()
|
||||
public function getContents(): string
|
||||
{
|
||||
return file_get_contents($this->tmpName);
|
||||
}
|
||||
@@ -208,7 +208,7 @@ class InputFile implements IInputItem
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasError()
|
||||
public function hasError(): bool
|
||||
{
|
||||
return ($this->getError() !== 0);
|
||||
}
|
||||
@@ -218,7 +218,7 @@ class InputFile implements IInputItem
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getError()
|
||||
public function getError(): string
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
@@ -227,9 +227,9 @@ class InputFile implements IInputItem
|
||||
* Set error
|
||||
*
|
||||
* @param int $error
|
||||
* @return static $this
|
||||
* @return static
|
||||
*/
|
||||
public function setError($error)
|
||||
public function setError($error): IInputItem
|
||||
{
|
||||
$this->errors = (int)$error;
|
||||
|
||||
@@ -239,7 +239,7 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTmpName()
|
||||
public function getTmpName(): string
|
||||
{
|
||||
return $this->tmpName;
|
||||
}
|
||||
@@ -247,9 +247,9 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* Set file temp. name
|
||||
* @param string $name
|
||||
* @return static $this
|
||||
* @return static
|
||||
*/
|
||||
public function setTmpName($name)
|
||||
public function setTmpName($name): IInputItem
|
||||
{
|
||||
$this->tmpName = $name;
|
||||
|
||||
@@ -261,7 +261,7 @@ class InputFile implements IInputItem
|
||||
return $this->getTmpName();
|
||||
}
|
||||
|
||||
public function getValue()
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->getFilename();
|
||||
}
|
||||
@@ -270,14 +270,14 @@ class InputFile implements IInputItem
|
||||
* @param string $value
|
||||
* @return static
|
||||
*/
|
||||
public function setValue($value)
|
||||
public function setValue($value): IInputItem
|
||||
{
|
||||
$this->filename = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'tmp_name' => $this->tmpName,
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Pecee\Http\Input;
|
||||
use Pecee\Exceptions\InvalidArgumentException;
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class Input
|
||||
class InputHandler
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
@@ -42,26 +42,26 @@ class Input
|
||||
* Parse input values
|
||||
*
|
||||
*/
|
||||
public function parseInputs()
|
||||
public function parseInputs() : void
|
||||
{
|
||||
/* Parse get requests */
|
||||
if (count($_GET) !== 0) {
|
||||
if (\count($_GET) !== 0) {
|
||||
$this->get = $this->handleGetPost($_GET);
|
||||
}
|
||||
|
||||
/* Parse post requests */
|
||||
$postVars = $_POST;
|
||||
|
||||
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete'], false) === true) {
|
||||
if (\in_array($this->request->getMethod(), ['put', 'patch', 'delete'], false) === true) {
|
||||
parse_str(file_get_contents('php://input'), $postVars);
|
||||
}
|
||||
|
||||
if (count($postVars) !== 0) {
|
||||
if (\count($postVars) !== 0) {
|
||||
$this->post = $this->handleGetPost($postVars);
|
||||
}
|
||||
|
||||
/* Parse get requests */
|
||||
if (count($_FILES) !== 0) {
|
||||
if (\count($_FILES) !== 0) {
|
||||
$this->file = $this->parseFiles();
|
||||
}
|
||||
}
|
||||
@@ -69,14 +69,14 @@ class Input
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function parseFiles()
|
||||
public function parseFiles() : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ((array)$_FILES as $key => $value) {
|
||||
|
||||
// Handle array input
|
||||
if (is_array($value['name']) === false) {
|
||||
if (\is_array($value['name']) === false) {
|
||||
$values['index'] = $key;
|
||||
try {
|
||||
$list[$key] = InputFile::createFromArray($values + $value);
|
||||
@@ -101,7 +101,7 @@ class Input
|
||||
return $list;
|
||||
}
|
||||
|
||||
protected function rearrangeFiles(array $values, &$index, $original)
|
||||
protected function rearrangeFiles(array $values, &$index, $original) : array
|
||||
{
|
||||
|
||||
$originalIndex = $index[0];
|
||||
@@ -111,7 +111,7 @@ class Input
|
||||
|
||||
foreach ($values as $key => $value) {
|
||||
|
||||
if (is_array($original['name'][$key]) === false) {
|
||||
if (\is_array($original['name'][$key]) === false) {
|
||||
|
||||
try {
|
||||
|
||||
@@ -152,14 +152,14 @@ class Input
|
||||
return $output;
|
||||
}
|
||||
|
||||
protected function handleGetPost(array $array)
|
||||
protected function handleGetPost(array $array) : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
|
||||
// Handle array input
|
||||
if (is_array($value) === false) {
|
||||
if (\is_array($value) === false) {
|
||||
$list[$key] = new InputItem($key, $value);
|
||||
continue;
|
||||
}
|
||||
@@ -181,7 +181,7 @@ class Input
|
||||
*/
|
||||
public function findPost($index, $defaultValue = null)
|
||||
{
|
||||
return isset($this->post[$index]) ? $this->post[$index] : $defaultValue;
|
||||
return $this->post[$index] ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -193,7 +193,7 @@ class Input
|
||||
*/
|
||||
public function findFile($index, $defaultValue = null)
|
||||
{
|
||||
return isset($this->file[$index]) ? $this->file[$index] : $defaultValue;
|
||||
return $this->file[$index] ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,7 +205,7 @@ class Input
|
||||
*/
|
||||
public function findGet($index, $defaultValue = null)
|
||||
{
|
||||
return isset($this->get[$index]) ? $this->get[$index] : $defaultValue;
|
||||
return $this->get[$index] ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,25 +218,25 @@ class Input
|
||||
*/
|
||||
public function getObject($index, $defaultValue = null, $methods = null)
|
||||
{
|
||||
if ($methods !== null && is_string($methods) === true) {
|
||||
if ($methods !== null && \is_string($methods) === true) {
|
||||
$methods = [$methods];
|
||||
}
|
||||
|
||||
$element = null;
|
||||
|
||||
if ($methods === null || in_array('get', $methods, false) === true) {
|
||||
if ($methods === null || \in_array('get', $methods, true) === true) {
|
||||
$element = $this->findGet($index);
|
||||
}
|
||||
|
||||
if (($element === null && $methods === null) || ($methods !== null && in_array('post', $methods, false) === true)) {
|
||||
if (($element === null && $methods === null) || ($methods !== null && \in_array('post', $methods, true) === true)) {
|
||||
$element = $this->findPost($index);
|
||||
}
|
||||
|
||||
if (($element === null && $methods === null) || ($methods !== null && in_array('file', $methods, false) === true)) {
|
||||
if (($element === null && $methods === null) || ($methods !== null && \in_array('file', $methods, true) === true)) {
|
||||
$element = $this->findFile($index);
|
||||
}
|
||||
|
||||
return ($element !== null) ? $element : $defaultValue;
|
||||
return $element ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -264,7 +264,7 @@ class Input
|
||||
* @param string $index
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($index)
|
||||
public function exists($index) : bool
|
||||
{
|
||||
return ($this->getObject($index) !== null);
|
||||
}
|
||||
@@ -274,7 +274,7 @@ class Input
|
||||
* @param array|null $filter Only take items in filter
|
||||
* @return array
|
||||
*/
|
||||
public function all(array $filter = null)
|
||||
public function all(array $filter = null) : array
|
||||
{
|
||||
$output = $_GET + $_POST;
|
||||
|
||||
@@ -20,12 +20,12 @@ class InputItem implements IInputItem
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIndex()
|
||||
public function getIndex(): string
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
public function setIndex($index)
|
||||
public function setIndex($index): IInputItem
|
||||
{
|
||||
$this->index = $index;
|
||||
|
||||
@@ -35,7 +35,7 @@ class InputItem implements IInputItem
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
@@ -45,7 +45,7 @@ class InputItem implements IInputItem
|
||||
* @param string $name
|
||||
* @return static $this
|
||||
*/
|
||||
public function setName($name)
|
||||
public function setName($name): IInputItem
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
@@ -55,7 +55,7 @@ class InputItem implements IInputItem
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getValue()
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
@@ -65,7 +65,7 @@ class InputItem implements IInputItem
|
||||
* @param string $value
|
||||
* @return static $this
|
||||
*/
|
||||
public function setValue($value)
|
||||
public function setValue($value): IInputItem
|
||||
{
|
||||
$this->value = $value;
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@ use Pecee\Http\Security\ITokenProvider;
|
||||
|
||||
class BaseCsrfVerifier implements IMiddleware
|
||||
{
|
||||
const POST_KEY = 'csrf-token';
|
||||
const HEADER_KEY = 'X-CSRF-TOKEN';
|
||||
public const POST_KEY = 'csrf-token';
|
||||
public const HEADER_KEY = 'X-CSRF-TOKEN';
|
||||
|
||||
protected $except;
|
||||
protected $tokenProvider;
|
||||
@@ -29,21 +29,21 @@ class BaseCsrfVerifier implements IMiddleware
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
protected function skip(Request $request)
|
||||
protected function skip(Request $request): bool
|
||||
{
|
||||
if ($this->except === null || count($this->except) === 0) {
|
||||
if ($this->except === null || \count($this->except) === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$max = count($this->except) - 1;
|
||||
$max = \count($this->except) - 1;
|
||||
|
||||
for ($i = $max; $i >= 0; $i--) {
|
||||
$url = $this->except[$i];
|
||||
|
||||
$url = rtrim($url, '/');
|
||||
if ($url[strlen($url) - 1] === '*') {
|
||||
if ($url[\strlen($url) - 1] === '*') {
|
||||
$url = rtrim($url, '*');
|
||||
$skip = (stripos($request->getUrl()->getOriginalUrl(), $url) === 0);
|
||||
$skip = $request->getUrl()->contains($url);
|
||||
} else {
|
||||
$skip = ($url === $request->getUrl()->getOriginalUrl());
|
||||
}
|
||||
@@ -62,12 +62,12 @@ class BaseCsrfVerifier implements IMiddleware
|
||||
* @param Request $request
|
||||
* @throws TokenMismatchException
|
||||
*/
|
||||
public function handle(Request $request)
|
||||
public function handle(Request $request): void
|
||||
{
|
||||
|
||||
if ($this->skip($request) === false && in_array($request->getMethod(), ['post', 'put', 'delete'], false) === true) {
|
||||
if ($this->skip($request) === false && \in_array($request->getMethod(), ['post', 'put', 'delete'], true) === true) {
|
||||
|
||||
$token = $request->getInput()->get(static::POST_KEY, null, 'post');
|
||||
$token = $request->getInputHandler()->get(static::POST_KEY, null, 'post');
|
||||
|
||||
// If the token is not posted, check headers for valid x-csrf-token
|
||||
if ($token === null) {
|
||||
@@ -85,7 +85,7 @@ class BaseCsrfVerifier implements IMiddleware
|
||||
|
||||
}
|
||||
|
||||
public function getTokenProvider()
|
||||
public function getTokenProvider(): ITokenProvider
|
||||
{
|
||||
return $this->tokenProvider;
|
||||
}
|
||||
@@ -94,7 +94,7 @@ class BaseCsrfVerifier implements IMiddleware
|
||||
* Set token provider
|
||||
* @param ITokenProvider $provider
|
||||
*/
|
||||
public function setTokenProvider(ITokenProvider $provider)
|
||||
public function setTokenProvider(ITokenProvider $provider): void
|
||||
{
|
||||
$this->tokenProvider = $provider;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\Http\Middleware;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
@@ -7,8 +8,7 @@ interface IMiddleware
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return Request|null
|
||||
*/
|
||||
public function handle(Request $request);
|
||||
public function handle(Request $request): void;
|
||||
|
||||
}
|
||||
+76
-53
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Pecee\Http;
|
||||
|
||||
use Pecee\Http\Input\Input;
|
||||
use Pecee\Http\Input\InputHandler;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
use Pecee\SimpleRouter\Route\RouteUrl;
|
||||
use Pecee\SimpleRouter\SimpleRouter;
|
||||
@@ -14,7 +14,7 @@ class Request
|
||||
protected $host;
|
||||
protected $url;
|
||||
protected $method;
|
||||
protected $input;
|
||||
protected $inputHandler;
|
||||
|
||||
protected $hasRewrite = false;
|
||||
|
||||
@@ -25,9 +25,9 @@ class Request
|
||||
protected $rewriteUrl;
|
||||
|
||||
/**
|
||||
* @var ILoadableRoute|null
|
||||
* @var array
|
||||
*/
|
||||
protected $loadedRoute;
|
||||
protected $loadedRoutes = [];
|
||||
|
||||
/**
|
||||
* Request constructor.
|
||||
@@ -41,11 +41,11 @@ class Request
|
||||
// Check if special IIS header exist, otherwise use default.
|
||||
$this->setUrl($this->getHeader('unencoded-url', $this->getHeader('request-uri')));
|
||||
|
||||
$this->input = new Input($this);
|
||||
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method')));
|
||||
$this->inputHandler = new InputHandler($this);
|
||||
$this->method = strtolower($this->inputHandler->get('_method', $this->getHeader('request-method')));
|
||||
}
|
||||
|
||||
protected function parseHeaders()
|
||||
protected function parseHeaders(): void
|
||||
{
|
||||
$this->headers = [];
|
||||
|
||||
@@ -56,7 +56,7 @@ class Request
|
||||
|
||||
}
|
||||
|
||||
public function isSecure()
|
||||
public function isSecure(): bool
|
||||
{
|
||||
return $this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || $this->getHeader('server-port') === 443;
|
||||
}
|
||||
@@ -64,23 +64,23 @@ class Request
|
||||
/**
|
||||
* @return Url
|
||||
*/
|
||||
public function getUrl()
|
||||
public function getUrl(): Url
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHost()
|
||||
public function getHost(): ?string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMethod()
|
||||
public function getMethod(): ?string
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
@@ -89,7 +89,7 @@ class Request
|
||||
* Get http basic auth user
|
||||
* @return string|null
|
||||
*/
|
||||
public function getUser()
|
||||
public function getUser(): ?string
|
||||
{
|
||||
return $this->getHeader('php-auth-user');
|
||||
}
|
||||
@@ -98,7 +98,7 @@ class Request
|
||||
* Get http basic auth password
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPassword()
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return $this->getHeader('php-auth-pw');
|
||||
}
|
||||
@@ -107,16 +107,16 @@ class Request
|
||||
* Get all headers
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders()
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get id address
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function getIp()
|
||||
public function getIp(): ?string
|
||||
{
|
||||
if ($this->getHeader('http-cf-connecting-ip') !== null) {
|
||||
return $this->getHeader('http-cf-connecting-ip');
|
||||
@@ -133,27 +133,27 @@ class Request
|
||||
* Get remote address/ip
|
||||
*
|
||||
* @alias static::getIp
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRemoteAddr()
|
||||
public function getRemoteAddr(): ?string
|
||||
{
|
||||
return $this->getIp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get referer
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function getReferer()
|
||||
public function getReferer(): ?string
|
||||
{
|
||||
return $this->getHeader('http-referer');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user agent
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function getUserAgent()
|
||||
public function getUserAgent(): ?string
|
||||
{
|
||||
return $this->getHeader('http-user-agent');
|
||||
}
|
||||
@@ -166,18 +166,18 @@ class Request
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHeader($name, $defaultValue = null)
|
||||
public function getHeader($name, $defaultValue = null): ?string
|
||||
{
|
||||
return isset($this->headers[strtolower($name)]) ? $this->headers[strtolower($name)] : $defaultValue;
|
||||
return $this->headers[strtolower($name)] ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get input class
|
||||
* @return Input
|
||||
* @return InputHandler
|
||||
*/
|
||||
public function getInput()
|
||||
public function getInputHandler(): InputHandler
|
||||
{
|
||||
return $this->input;
|
||||
return $this->inputHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,9 +187,9 @@ class Request
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isFormatAccepted($format)
|
||||
public function isFormatAccepted($format): bool
|
||||
{
|
||||
return ($this->getHeader('http-accept') !== null && stripos($this->getHeader('http-accept'), $format) > -1);
|
||||
return ($this->getHeader('http-accept') !== null && stripos($this->getHeader('http-accept'), $format) !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,7 +197,7 @@ class Request
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isAjax()
|
||||
public function isAjax(): bool
|
||||
{
|
||||
return (strtolower($this->getHeader('http-x-requested-with')) === 'xmlhttprequest');
|
||||
}
|
||||
@@ -206,7 +206,7 @@ class Request
|
||||
* Get accept formats
|
||||
* @return array
|
||||
*/
|
||||
public function getAcceptFormats()
|
||||
public function getAcceptFormats(): array
|
||||
{
|
||||
return explode(',', $this->getHeader('http-accept'));
|
||||
}
|
||||
@@ -215,7 +215,7 @@ class Request
|
||||
* @param string|Url $url
|
||||
* @throws \Pecee\Http\Exceptions\MalformedUrlException
|
||||
*/
|
||||
public function setUrl($url)
|
||||
public function setUrl($url): void
|
||||
{
|
||||
$this->url = ($url instanceof Url) ? $url : new Url($url);
|
||||
}
|
||||
@@ -223,7 +223,7 @@ class Request
|
||||
/**
|
||||
* @param string $host
|
||||
*/
|
||||
public function setHost($host)
|
||||
public function setHost($host): void
|
||||
{
|
||||
$this->host = $host;
|
||||
}
|
||||
@@ -231,7 +231,7 @@ class Request
|
||||
/**
|
||||
* @param string $method
|
||||
*/
|
||||
public function setMethod($method)
|
||||
public function setMethod($method): void
|
||||
{
|
||||
$this->method = $method;
|
||||
}
|
||||
@@ -242,7 +242,7 @@ class Request
|
||||
* @param ILoadableRoute $route
|
||||
* @return static
|
||||
*/
|
||||
public function setRewriteRoute(ILoadableRoute $route)
|
||||
public function setRewriteRoute(ILoadableRoute $route): self
|
||||
{
|
||||
$this->hasRewrite = true;
|
||||
$this->rewriteRoute = SimpleRouter::addDefaultNamespace($route);
|
||||
@@ -255,7 +255,7 @@ class Request
|
||||
*
|
||||
* @return ILoadableRoute|null
|
||||
*/
|
||||
public function getRewriteRoute()
|
||||
public function getRewriteRoute(): ?ILoadableRoute
|
||||
{
|
||||
return $this->rewriteRoute;
|
||||
}
|
||||
@@ -263,9 +263,9 @@ class Request
|
||||
/**
|
||||
* Get rewrite url
|
||||
*
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRewriteUrl()
|
||||
public function getRewriteUrl(): ?string
|
||||
{
|
||||
return $this->rewriteUrl;
|
||||
}
|
||||
@@ -276,7 +276,7 @@ class Request
|
||||
* @param string $rewriteUrl
|
||||
* @return static
|
||||
*/
|
||||
public function setRewriteUrl($rewriteUrl)
|
||||
public function setRewriteUrl(string $rewriteUrl): self
|
||||
{
|
||||
$this->hasRewrite = true;
|
||||
$this->rewriteUrl = rtrim($rewriteUrl, '/') . '/';
|
||||
@@ -286,10 +286,10 @@ class Request
|
||||
|
||||
/**
|
||||
* Set rewrite callback
|
||||
* @param string $callback
|
||||
* @param string|\Closure $callback
|
||||
* @return static
|
||||
*/
|
||||
public function setRewriteCallback($callback)
|
||||
public function setRewriteCallback($callback): self
|
||||
{
|
||||
$this->hasRewrite = true;
|
||||
|
||||
@@ -300,37 +300,60 @@ class Request
|
||||
* Get loaded route
|
||||
* @return ILoadableRoute|null
|
||||
*/
|
||||
public function getLoadedRoute()
|
||||
public function getLoadedRoute(): ?ILoadableRoute
|
||||
{
|
||||
return $this->loadedRoute;
|
||||
return (\count($this->loadedRoutes) > 0) ? end($this->loadedRoutes) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set loaded route
|
||||
* Get all loaded routes
|
||||
*
|
||||
* @param ILoadableRoute $route
|
||||
* @return array
|
||||
*/
|
||||
public function getLoadedRoutes(): array
|
||||
{
|
||||
return $this->loadedRoutes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set loaded routes
|
||||
*
|
||||
* @param array $routes
|
||||
* @return static
|
||||
*/
|
||||
public function setLoadedRoute(ILoadableRoute $route)
|
||||
public function setLoadedRoutes(array $routes): self
|
||||
{
|
||||
$this->loadedRoute = $route;
|
||||
$this->loadedRoutes = $routes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasRewrite()
|
||||
/**
|
||||
* Added loaded route
|
||||
*
|
||||
* @param ILoadableRoute $route
|
||||
* @return static
|
||||
*/
|
||||
public function addLoadedRoute(ILoadableRoute $route): self
|
||||
{
|
||||
$this->loadedRoutes[] = $route;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasRewrite(): bool
|
||||
{
|
||||
return $this->hasRewrite;
|
||||
}
|
||||
|
||||
public function setHasRewrite($value)
|
||||
public function setHasRewrite($value): self
|
||||
{
|
||||
$this->hasRewrite = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isRewrite($url)
|
||||
public function isRewrite($url): bool
|
||||
{
|
||||
return ($this->rewriteUrl === $url);
|
||||
}
|
||||
@@ -347,7 +370,7 @@ class Request
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
return isset($this->data[$name]) ? $this->data[$name] : null;
|
||||
return $this->data[$name] ?? null;
|
||||
}
|
||||
|
||||
}
|
||||
+10
-11
@@ -19,7 +19,7 @@ class Response
|
||||
* @param int $code
|
||||
* @return static
|
||||
*/
|
||||
public function httpCode($code)
|
||||
public function httpCode($code): self
|
||||
{
|
||||
http_response_code($code);
|
||||
|
||||
@@ -32,7 +32,7 @@ class Response
|
||||
* @param string $url
|
||||
* @param int $httpCode
|
||||
*/
|
||||
public function redirect($url, $httpCode = null)
|
||||
public function redirect($url, $httpCode = null): void
|
||||
{
|
||||
if ($httpCode !== null) {
|
||||
$this->httpCode($httpCode);
|
||||
@@ -42,7 +42,7 @@ class Response
|
||||
exit(0);
|
||||
}
|
||||
|
||||
public function refresh()
|
||||
public function refresh(): void
|
||||
{
|
||||
$this->redirect($this->request->getUrl()->getOriginalUrl());
|
||||
}
|
||||
@@ -52,7 +52,7 @@ class Response
|
||||
* @param string $name
|
||||
* @return static
|
||||
*/
|
||||
public function auth($name = '')
|
||||
public function auth($name = ''): self
|
||||
{
|
||||
$this->headers([
|
||||
'WWW-Authenticate: Basic realm="' . $name . '"',
|
||||
@@ -62,7 +62,7 @@ class Response
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function cache($eTag, $lastModified = 2592000)
|
||||
public function cache($eTag, $lastModified = 2592000): self
|
||||
{
|
||||
|
||||
$this->headers([
|
||||
@@ -77,8 +77,7 @@ class Response
|
||||
if (($httpIfNoneMatch !== null && $httpIfNoneMatch === $eTag) || ($httpModified !== null && strtotime($httpModified) === $lastModified)) {
|
||||
|
||||
$this->header('HTTP/1.1 304 Not Modified');
|
||||
|
||||
exit();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
return $this;
|
||||
@@ -91,9 +90,9 @@ class Response
|
||||
* @param int $dept JSON debt.
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function json($value, $options = null, $dept = 512)
|
||||
public function json($value, $options = null, $dept = 512): void
|
||||
{
|
||||
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
||||
if (($value instanceof \JsonSerializable) === false && \is_array($value) === false) {
|
||||
throw new InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
||||
}
|
||||
|
||||
@@ -107,7 +106,7 @@ class Response
|
||||
* @param string $value
|
||||
* @return static
|
||||
*/
|
||||
public function header($value)
|
||||
public function header($value): self
|
||||
{
|
||||
header($value);
|
||||
|
||||
@@ -119,7 +118,7 @@ class Response
|
||||
* @param array $headers
|
||||
* @return static
|
||||
*/
|
||||
public function headers(array $headers)
|
||||
public function headers(array $headers): self
|
||||
{
|
||||
foreach ($headers as $header) {
|
||||
$this->header($header);
|
||||
|
||||
@@ -6,7 +6,7 @@ use Pecee\Http\Security\Exceptions\SecurityException;
|
||||
|
||||
class CookieTokenProvider implements ITokenProvider
|
||||
{
|
||||
const CSRF_KEY = 'CSRF-TOKEN';
|
||||
public const CSRF_KEY = 'CSRF-TOKEN';
|
||||
|
||||
protected $token;
|
||||
protected $cookieTimeoutMinutes = 120;
|
||||
@@ -30,24 +30,13 @@ class CookieTokenProvider implements ITokenProvider
|
||||
* @return string
|
||||
* @throws SecurityException
|
||||
*/
|
||||
public function generateToken()
|
||||
public function generateToken(): string
|
||||
{
|
||||
if (function_exists('random_bytes') === true) {
|
||||
try {
|
||||
return bin2hex(random_bytes(32));
|
||||
} catch(\Exception $e) {
|
||||
throw new SecurityException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
|
||||
}
|
||||
try {
|
||||
return bin2hex(random_bytes(32));
|
||||
} catch (\Exception $e) {
|
||||
throw new SecurityException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
|
||||
}
|
||||
|
||||
$isSourceStrong = false;
|
||||
|
||||
$random = openssl_random_pseudo_bytes(32, $isSourceStrong);
|
||||
if ($isSourceStrong === false || $random === false) {
|
||||
throw new SecurityException('IV generation failed');
|
||||
}
|
||||
|
||||
return $random;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +45,7 @@ class CookieTokenProvider implements ITokenProvider
|
||||
* @param string $token
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($token)
|
||||
public function validate($token): bool
|
||||
{
|
||||
if ($token !== null && $this->getToken() !== null) {
|
||||
return hash_equals($token, $this->getToken());
|
||||
@@ -71,7 +60,7 @@ class CookieTokenProvider implements ITokenProvider
|
||||
*
|
||||
* @param string $token
|
||||
*/
|
||||
public function setToken($token)
|
||||
public function setToken($token): void
|
||||
{
|
||||
$this->token = $token;
|
||||
setcookie(static::CSRF_KEY, $token, time() + 60 * $this->cookieTimeoutMinutes, '/');
|
||||
@@ -82,17 +71,17 @@ class CookieTokenProvider implements ITokenProvider
|
||||
* @param string|null $defaultValue
|
||||
* @return string|null
|
||||
*/
|
||||
public function getToken($defaultValue = null)
|
||||
public function getToken($defaultValue = null): ?string
|
||||
{
|
||||
$this->token = ($this->hasToken() === true) ? $_COOKIE[static::CSRF_KEY] : null;
|
||||
|
||||
return ($this->token !== null) ? $this->token : $defaultValue;
|
||||
return $this->token ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh existing token
|
||||
*/
|
||||
public function refresh()
|
||||
public function refresh(): void
|
||||
{
|
||||
if ($this->token !== null) {
|
||||
$this->setToken($this->token);
|
||||
@@ -103,7 +92,7 @@ class CookieTokenProvider implements ITokenProvider
|
||||
* Returns whether the csrf token has been defined
|
||||
* @return bool
|
||||
*/
|
||||
public function hasToken()
|
||||
public function hasToken(): bool
|
||||
{
|
||||
return isset($_COOKIE[static::CSRF_KEY]);
|
||||
}
|
||||
@@ -112,7 +101,7 @@ class CookieTokenProvider implements ITokenProvider
|
||||
* Get timeout for cookie in minutes
|
||||
* @return int
|
||||
*/
|
||||
public function getCookieTimeoutMinutes()
|
||||
public function getCookieTimeoutMinutes(): int
|
||||
{
|
||||
return $this->cookieTimeoutMinutes;
|
||||
}
|
||||
@@ -121,7 +110,7 @@ class CookieTokenProvider implements ITokenProvider
|
||||
* Set cookie timeout in minutes
|
||||
* @param $minutes
|
||||
*/
|
||||
public function setCookieTimeoutMinutes($minutes)
|
||||
public function setCookieTimeoutMinutes($minutes): void
|
||||
{
|
||||
$this->cookieTimeoutMinutes = $minutes;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ interface ITokenProvider
|
||||
/**
|
||||
* Refresh existing token
|
||||
*/
|
||||
public function refresh();
|
||||
public function refresh(): void;
|
||||
|
||||
/**
|
||||
* Validate valid CSRF token
|
||||
@@ -16,6 +16,14 @@ interface ITokenProvider
|
||||
* @param string $token
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($token);
|
||||
public function validate($token): bool;
|
||||
|
||||
/**
|
||||
* Get token token
|
||||
*
|
||||
* @param string|null $defaultValue
|
||||
* @return string|null
|
||||
*/
|
||||
public function getToken($defaultValue = null);
|
||||
|
||||
}
|
||||
+18
-13
@@ -38,7 +38,7 @@ class Url
|
||||
* Check if url is using a secure protocol like https
|
||||
* @return bool
|
||||
*/
|
||||
public function isSecure()
|
||||
public function isSecure(): bool
|
||||
{
|
||||
return (strtolower($this->getScheme()) === 'https');
|
||||
}
|
||||
@@ -47,7 +47,7 @@ class Url
|
||||
* Checks if url is relative
|
||||
* @return bool
|
||||
*/
|
||||
public function isRelative()
|
||||
public function isRelative(): bool
|
||||
{
|
||||
return ($this->getHost() === null);
|
||||
}
|
||||
@@ -56,7 +56,7 @@ class Url
|
||||
* Get url scheme
|
||||
* @return string|null
|
||||
*/
|
||||
public function getScheme()
|
||||
public function getScheme(): ?string
|
||||
{
|
||||
return $this->data['scheme'];
|
||||
}
|
||||
@@ -65,7 +65,7 @@ class Url
|
||||
* Get url host
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHost()
|
||||
public function getHost(): ?string
|
||||
{
|
||||
return $this->data['host'];
|
||||
}
|
||||
@@ -74,7 +74,7 @@ class Url
|
||||
* Get url port
|
||||
* @return int|null
|
||||
*/
|
||||
public function getPort()
|
||||
public function getPort(): ?int
|
||||
{
|
||||
return ($this->data['port'] !== null) ? (int)$this->data['port'] : null;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ class Url
|
||||
* Parse username from url
|
||||
* @return string|null
|
||||
*/
|
||||
public function getUserName()
|
||||
public function getUserName(): ?string
|
||||
{
|
||||
return $this->data['user'];
|
||||
}
|
||||
@@ -92,7 +92,7 @@ class Url
|
||||
* Parse password from url
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPassword()
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return $this->data['pass'];
|
||||
}
|
||||
@@ -101,7 +101,7 @@ class Url
|
||||
* Get path from url
|
||||
* @return string
|
||||
*/
|
||||
public function getPath()
|
||||
public function getPath(): ?string
|
||||
{
|
||||
return $this->data['path'];
|
||||
}
|
||||
@@ -110,7 +110,7 @@ class Url
|
||||
* Get querystring from url
|
||||
* @return string|null
|
||||
*/
|
||||
public function getQueryString()
|
||||
public function getQueryString(): ?string
|
||||
{
|
||||
return $this->data['query'];
|
||||
}
|
||||
@@ -119,7 +119,7 @@ class Url
|
||||
* Get fragment from url (everything after #)
|
||||
* @return string|null
|
||||
*/
|
||||
public function getFragment()
|
||||
public function getFragment(): ?string
|
||||
{
|
||||
return $this->data['fragment'];
|
||||
}
|
||||
@@ -127,7 +127,7 @@ class Url
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOriginalUrl()
|
||||
public function getOriginalUrl(): string
|
||||
{
|
||||
return $this->originalUrl;
|
||||
}
|
||||
@@ -139,7 +139,7 @@ class Url
|
||||
* @throws MalformedUrlException
|
||||
* @return array
|
||||
*/
|
||||
public function parseUrl($url, $component = -1)
|
||||
public function parseUrl($url, $component = -1): array
|
||||
{
|
||||
$encodedUrl = preg_replace_callback(
|
||||
'/[^:\/@?&=#]+/u',
|
||||
@@ -158,11 +158,16 @@ class Url
|
||||
return array_map('urldecode', $parts);
|
||||
}
|
||||
|
||||
public function contains($value): bool
|
||||
{
|
||||
return (stripos($this->getOriginalUrl(), $value) === false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns data array with information about the url
|
||||
* @return array
|
||||
*/
|
||||
public function getData()
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user