Development

- Ensure that request-method is always lowercase.
- Fixed spaces instead of tabs to comply with PSR-2.
This commit is contained in:
Simon Sessingø
2016-11-25 12:51:45 +01:00
parent 2dd2d95af5
commit 1c515119b4
24 changed files with 3052 additions and 3066 deletions
+2 -1
View File
@@ -1,7 +1,8 @@
<?php <?php
namespace Pecee\Controllers; namespace Pecee\Controllers;
interface IRestController { interface IRestController
{
/** /**
* @return void * @return void
+3
View File
@@ -17,6 +17,7 @@ class CsrfToken
if (function_exists('mcrypt_create_iv')) { if (function_exists('mcrypt_create_iv')) {
return bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)); return bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
} }
return bin2hex(openssl_random_pseudo_bytes(32)); return bin2hex(openssl_random_pseudo_bytes(32));
} }
@@ -31,6 +32,7 @@ class CsrfToken
if ($token !== null && $this->getToken() !== null) { if ($token !== null && $this->getToken() !== null) {
return hash_equals($token, $this->getToken()); return hash_equals($token, $this->getToken());
} }
return false; return false;
} }
@@ -53,6 +55,7 @@ class CsrfToken
if ($this->hasToken()) { if ($this->hasToken()) {
return $_COOKIE[static::CSRF_KEY]; return $_COOKIE[static::CSRF_KEY];
} }
return null; return null;
} }
-27
View File
@@ -25,40 +25,13 @@ class Input
*/ */
protected $request; protected $request;
protected $invalidContentType = false;
protected $invalidContentTypes = [
'text/plain',
'application/x-www-form-urlencoded',
];
public function __construct(Request $request) public function __construct(Request $request)
{ {
$this->request = $request; $this->request = $request;
if ($request->getMethod() !== 'get') {
$requestContentType = $request->getHeader('http-content-type');
$max = count($this->invalidContentTypes) - 1;
for ($i = $max; $i >= 0; $i--) {
$contentType = $this->invalidContentType[$i];
if (stripos($requestContentType, $contentType) === 0) {
$this->invalidContentType = true;
break;
}
}
}
if ($this->invalidContentType === false) {
$this->parseInputs(); $this->parseInputs();
} }
}
protected function parseInputs() protected function parseInputs()
{ {
/* Parse get requests */ /* Parse get requests */
+2
View File
@@ -48,6 +48,7 @@ class InputItem implements IInputItem
public function setName($name) public function setName($name)
{ {
$this->name = $name; $this->name = $name;
return $this; return $this;
} }
@@ -59,6 +60,7 @@ class InputItem implements IInputItem
public function setValue($value) public function setValue($value)
{ {
$this->value = $value; $this->value = $value;
return $this; return $this;
} }
@@ -79,6 +79,7 @@ class BaseCsrfVerifier implements IMiddleware
{ {
$token = $this->csrfToken->generateToken(); $token = $this->csrfToken->generateToken();
$this->csrfToken->setToken($token); $this->csrfToken->setToken($token);
return $token; return $token;
} }
+1 -1
View File
@@ -18,7 +18,7 @@ class Request
$this->host = $this->getHeader('http-host');; $this->host = $this->getHeader('http-host');;
$this->uri = $this->getHeader('request-uri'); $this->uri = $this->getHeader('request-uri');
$this->input = new Input($this); $this->input = new Input($this);
$this->method = $this->input->get('_method', strtolower($this->getHeader('request-method'))); $this->method = strtolower($this->input->get('_method', $this->getHeader('request-method')));
} }
protected function parseHeaders() protected function parseHeaders()
+6 -2
View File
@@ -19,6 +19,7 @@ class Response
public function httpCode($code) public function httpCode($code)
{ {
http_response_code($code); http_response_code($code);
return $this; return $this;
} }
@@ -52,8 +53,9 @@ class Response
{ {
$this->headers([ $this->headers([
'WWW-Authenticate: Basic realm="' . $name . '"', 'WWW-Authenticate: Basic realm="' . $name . '"',
'HTTP/1.0 401 Unauthorized' 'HTTP/1.0 401 Unauthorized',
]); ]);
return $this; return $this;
} }
@@ -63,7 +65,7 @@ class Response
$this->headers([ $this->headers([
'Cache-Control: public', 'Cache-Control: public',
'Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastModified) . ' GMT', 'Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastModified) . ' GMT',
'Etag: ' . $eTag 'Etag: ' . $eTag,
]); ]);
$httpModified = $this->request->getHeader('http-if-modified-since'); $httpModified = $this->request->getHeader('http-if-modified-since');
@@ -98,6 +100,7 @@ class Response
public function header($value) public function header($value)
{ {
header($value); header($value);
return $this; return $this;
} }
@@ -112,6 +115,7 @@ class Response
for ($i = 0; $i < $max; $i++) { for ($i = 0; $i < $max; $i++) {
$this->header($headers[$i]); $this->header($headers[$i]);
} }
return $this; return $this;
} }
+2
View File
@@ -422,6 +422,7 @@ class Router
/* Return current route if no options has been specified */ /* Return current route if no options has been specified */
if ($name === null && $parameters === null) { if ($name === null && $parameters === null) {
$url = rtrim(parse_url($this->request->getUri(), PHP_URL_PATH), '/'); $url = rtrim(parse_url($this->request->getUri(), PHP_URL_PATH), '/');
return (($url === '') ? '/' : $url . '/') . $this->arrayToParams($getParams); return (($url === '') ? '/' : $url . '/') . $this->arrayToParams($getParams);
} }
@@ -465,6 +466,7 @@ class Router
/* No result so we assume that someone is using a hardcoded url and join everything together. */ /* No result so we assume that someone is using a hardcoded url and join everything together. */
$url = trim(join('/', array_merge((array)$name, (array)$parameters)), '/'); $url = trim(join('/', array_merge((array)$name, (array)$parameters)), '/');
return (($url === '') ? '/' : '/' . $url . '/') . $this->arrayToParams($getParams); return (($url === '') ? '/' : '/' . $url . '/') . $this->arrayToParams($getParams);
} }