first version
This commit is contained in:
79
kirby/src/Http/Request/Auth/BasicAuth.php
Executable file
79
kirby/src/Http/Request/Auth/BasicAuth.php
Executable file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Http\Request\Auth;
|
||||
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
* Basic Authentication
|
||||
*/
|
||||
class BasicAuth extends BearerAuth
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $credentials;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $password;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $username;
|
||||
|
||||
/**
|
||||
* @param string $token
|
||||
*/
|
||||
public function __construct(string $token)
|
||||
{
|
||||
parent::__construct($token);
|
||||
|
||||
$this->credentials = base64_decode($token);
|
||||
$this->username = Str::before($this->credentials, ':');
|
||||
$this->password = Str::after($this->credentials, ':');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entire unencoded credentials string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function credentials(): string
|
||||
{
|
||||
return $this->credentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the password
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function password(): ?string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the authentication type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function type(): string
|
||||
{
|
||||
return 'basic';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the username
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function username(): ?string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
}
|
55
kirby/src/Http/Request/Auth/BearerAuth.php
Executable file
55
kirby/src/Http/Request/Auth/BearerAuth.php
Executable file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Http\Request\Auth;
|
||||
|
||||
/**
|
||||
* Bearer Auth
|
||||
*/
|
||||
class BearerAuth
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $token;
|
||||
|
||||
/**
|
||||
* Creates a new Bearer Auth object
|
||||
*
|
||||
* @param string $token
|
||||
*/
|
||||
public function __construct(string $token)
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the object to a string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return ucfirst($this->type()) . ' ' . $this->token();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the authentication token
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function token(): string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the auth type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function type(): string
|
||||
{
|
||||
return 'bearer';
|
||||
}
|
||||
}
|
129
kirby/src/Http/Request/Body.php
Executable file
129
kirby/src/Http/Request/Body.php
Executable file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Http\Request;
|
||||
|
||||
/**
|
||||
* The Body class parses the
|
||||
* request body and provides a nice
|
||||
* interface to get values from
|
||||
* structured bodies (json encoded or form data)
|
||||
*
|
||||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Body
|
||||
{
|
||||
use Data;
|
||||
|
||||
/**
|
||||
* The raw body content
|
||||
*
|
||||
* @var string|array
|
||||
*/
|
||||
protected $contents;
|
||||
|
||||
/**
|
||||
* The parsed content as array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Creates a new request body object.
|
||||
* You can pass your own array or string.
|
||||
* If null is being passed, the class will
|
||||
* fetch the body either from the $_POST global
|
||||
* or from php://input.
|
||||
*
|
||||
* @param array|string|null $contents
|
||||
*/
|
||||
public function __construct($contents = null)
|
||||
{
|
||||
$this->contents = $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the raw contents for the body
|
||||
* or uses the passed contents.
|
||||
*
|
||||
* @return string|array
|
||||
*/
|
||||
public function contents()
|
||||
{
|
||||
if ($this->contents === null) {
|
||||
if (empty($_POST) === false) {
|
||||
$this->contents = $_POST;
|
||||
} else {
|
||||
$this->contents = file_get_contents('php://input');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the raw contents once and caches
|
||||
* the result. The parser will try to convert
|
||||
* the body with the json decoder first and
|
||||
* then run parse_str to get some results
|
||||
* if the json decoder failed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data(): array
|
||||
{
|
||||
if (is_array($this->data) === true) {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
$contents = $this->contents();
|
||||
|
||||
// return content which is already in array form
|
||||
if (is_array($contents) === true) {
|
||||
return $this->data = $contents;
|
||||
}
|
||||
|
||||
// try to convert the body from json
|
||||
$json = json_decode($contents, true);
|
||||
|
||||
if (is_array($json) === true) {
|
||||
return $this->data = $json;
|
||||
}
|
||||
|
||||
if (strstr($contents, '=') !== false) {
|
||||
// try to parse the body as query string
|
||||
parse_str($contents, $parsed);
|
||||
|
||||
if (is_array($parsed)) {
|
||||
return $this->data = $parsed;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->data = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the data array back
|
||||
* to a http query string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toString(): string
|
||||
{
|
||||
return http_build_query($this->data());
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic string converter
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
}
|
85
kirby/src/Http/Request/Data.php
Executable file
85
kirby/src/Http/Request/Data.php
Executable file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Http\Request;
|
||||
|
||||
/**
|
||||
* The Data Trait is being used in
|
||||
* Query, Files and Body classes to
|
||||
* provide unified get and data methods.
|
||||
* Especially the get method is a bit more
|
||||
* complex with the option to fetch single keys
|
||||
* or entire arrays.
|
||||
*
|
||||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
trait Data
|
||||
{
|
||||
|
||||
/**
|
||||
* Improved var_dump output
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __debuginfo(): array
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* The data provider method has to be
|
||||
* implemented by each class using this Trait
|
||||
* and has to return an associative array
|
||||
* for the get method
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function data(): array;
|
||||
|
||||
/**
|
||||
* The get method is the heart and soul of this
|
||||
* Trait. You can use it to fetch a single value
|
||||
* of the data array by key or multiple values by
|
||||
* passing an array of keys.
|
||||
*
|
||||
* @param string|array $key
|
||||
* @param mixed|null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
if (is_array($key) === true) {
|
||||
$result = [];
|
||||
foreach ($key as $k) {
|
||||
$result[$k] = $this->get($k);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->data()[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array.
|
||||
* This is basically an alias for Data::data()
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->data();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the data array to json
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toJson(): string
|
||||
{
|
||||
return json_encode($this->data());
|
||||
}
|
||||
}
|
73
kirby/src/Http/Request/Files.php
Executable file
73
kirby/src/Http/Request/Files.php
Executable file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Http\Request;
|
||||
|
||||
/**
|
||||
* The Files object sanitizes
|
||||
* the input coming from the $_FILES
|
||||
* global. Especially for multiple uploads
|
||||
* for the same key, it will produce a more
|
||||
* usable array.
|
||||
*
|
||||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Files
|
||||
{
|
||||
use Data;
|
||||
|
||||
/**
|
||||
* Sanitized array of all received files
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $files;
|
||||
|
||||
/**
|
||||
* Creates a new Files object
|
||||
* Pass your own array to mock
|
||||
* uploads.
|
||||
*
|
||||
* @param array|null $files
|
||||
*/
|
||||
public function __construct($files = null)
|
||||
{
|
||||
if ($files === null) {
|
||||
$files = $_FILES;
|
||||
}
|
||||
|
||||
$this->files = [];
|
||||
|
||||
foreach ($files as $key => $file) {
|
||||
if (is_array($file['name'])) {
|
||||
foreach ($file['name'] as $i => $name) {
|
||||
$this->files[$key][] = [
|
||||
'name' => $file['name'][$i] ?? null,
|
||||
'type' => $file['type'][$i] ?? null,
|
||||
'tmp_name' => $file['tmp_name'][$i] ?? null,
|
||||
'error' => $file['error'][$i] ?? null,
|
||||
'size' => $file['size'][$i] ?? null,
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$this->files[$key] = $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The data method returns the files
|
||||
* array. This is only needed to make
|
||||
* the Data trait work for the Files::get($key)
|
||||
* method.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data(): array
|
||||
{
|
||||
return $this->files;
|
||||
}
|
||||
}
|
78
kirby/src/Http/Request/Query.php
Executable file
78
kirby/src/Http/Request/Query.php
Executable file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Http\Request;
|
||||
|
||||
/**
|
||||
* The Query class helps to
|
||||
* parse and inspect URL queries
|
||||
* as part of the Request object
|
||||
*
|
||||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Query
|
||||
{
|
||||
use Data;
|
||||
|
||||
/**
|
||||
* The Query data array
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Creates a new Query object.
|
||||
* The passed data can be an array
|
||||
* or a parsable query string. If
|
||||
* null is passed, the current Query
|
||||
* will be taken from $_GET
|
||||
*
|
||||
* @param array|string|null $data
|
||||
*/
|
||||
public function __construct($data = null)
|
||||
{
|
||||
if ($data === null) {
|
||||
$this->data = $_GET;
|
||||
} elseif (is_array($data)) {
|
||||
$this->data = $data;
|
||||
} else {
|
||||
parse_str($data, $parsed);
|
||||
$this->data = $parsed;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Query data as array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the query data array
|
||||
* back to a query string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toString(): string
|
||||
{
|
||||
return http_build_query($this->data());
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic string converter
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user