2017-03-19 02:30:36 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-19 02:30:36 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\REST;
|
2017-03-19 02:30:36 +00:00
|
|
|
|
|
|
|
class Request {
|
2017-04-07 01:41:21 +00:00
|
|
|
public $method = "GET";
|
2017-09-29 21:40:07 +00:00
|
|
|
public $head = false;
|
2017-04-07 01:41:21 +00:00
|
|
|
public $url = "";
|
|
|
|
public $path ="";
|
2017-05-20 03:52:26 +00:00
|
|
|
public $paths = [];
|
2017-04-07 01:41:21 +00:00
|
|
|
public $query = "";
|
|
|
|
public $type ="";
|
|
|
|
public $body = "";
|
2017-03-19 02:30:36 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function __construct(string $method = null, string $url = null, string $body = null, string $contentType = null) {
|
2017-09-05 23:35:14 +00:00
|
|
|
$method = $method ?? $_SERVER['REQUEST_METHOD'];
|
|
|
|
$url = $url ?? $_SERVER['REQUEST_URI'];
|
|
|
|
$body = $body ?? file_get_contents("php://input");
|
2017-08-29 14:50:31 +00:00
|
|
|
if (is_null($contentType)) {
|
|
|
|
if (isset($_SERVER['HTTP_CONTENT_TYPE'])) {
|
2017-04-07 01:41:21 +00:00
|
|
|
$contentType = $_SERVER['HTTP_CONTENT_TYPE'];
|
|
|
|
} else {
|
|
|
|
$contentType = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->method = strtoupper($method);
|
|
|
|
$this->url = $url;
|
|
|
|
$this->body = $body;
|
|
|
|
$this->type = $contentType;
|
2017-09-30 16:05:57 +00:00
|
|
|
if ($this->method=="HEAD") {
|
2017-09-29 21:40:07 +00:00
|
|
|
$this->head = true;
|
|
|
|
$this->method = "GET";
|
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->refreshURL();
|
|
|
|
}
|
2017-04-02 03:06:52 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
public function refreshURL() {
|
|
|
|
$url = $this->parseURL($this->url);
|
|
|
|
$this->path = $url['path'];
|
2017-05-20 03:52:26 +00:00
|
|
|
$this->paths = $url['paths'];
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->query = $url['query'];
|
|
|
|
}
|
2017-04-02 03:06:52 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
protected function parseURL(string $url): array {
|
|
|
|
// split the query string from the path
|
|
|
|
$parts = explode("?", $url);
|
2017-05-20 03:52:26 +00:00
|
|
|
$out = ['path' => $parts[0], 'paths' => [''], 'query' => []];
|
2017-04-07 01:41:21 +00:00
|
|
|
// if there is a query string, parse it
|
2017-08-29 14:50:31 +00:00
|
|
|
if (isset($parts[1])) {
|
2017-04-07 01:41:21 +00:00
|
|
|
// split along & to get key-value pairs
|
|
|
|
$query = explode("&", $parts[1]);
|
2017-08-29 14:50:31 +00:00
|
|
|
for ($a = 0; $a < sizeof($query); $a++) {
|
2017-04-07 01:41:21 +00:00
|
|
|
// split each pair, into no more than two parts
|
|
|
|
$data = explode("=", $query[$a], 2);
|
|
|
|
// decode the key
|
|
|
|
$key = rawurldecode($data[0]);
|
|
|
|
// decode the value if there is one
|
|
|
|
$value = "";
|
2017-08-29 14:50:31 +00:00
|
|
|
if (isset($data[1])) {
|
2017-04-07 01:41:21 +00:00
|
|
|
$value = rawurldecode($data[1]);
|
|
|
|
}
|
|
|
|
// add the pair to the query output, overwriting earlier values for the same key, is present
|
|
|
|
$out['query'][$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
2017-05-20 03:52:26 +00:00
|
|
|
// also include the path as a set of decoded elements
|
|
|
|
// if the path is an empty string or just / nothing needs be done
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!in_array($out['path'], ["/",""])) {
|
2017-05-20 03:52:26 +00:00
|
|
|
$paths = explode("/", $out['path']);
|
2017-05-20 12:57:24 +00:00
|
|
|
// remove the first and last empty elements, if present (they are artefacts of the splitting; others should remain)
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!strlen($paths[0])) {
|
2017-07-21 02:40:09 +00:00
|
|
|
array_shift($paths);
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!strlen($paths[sizeof($paths)-1])) {
|
2017-07-21 02:40:09 +00:00
|
|
|
array_pop($paths);
|
|
|
|
}
|
2017-05-20 03:52:26 +00:00
|
|
|
// %-decode each path element
|
2017-08-29 14:50:31 +00:00
|
|
|
$paths = array_map(function ($v) {
|
|
|
|
return rawurldecode($v);
|
|
|
|
}, $paths);
|
2017-05-20 03:52:26 +00:00
|
|
|
$out['paths'] = $paths;
|
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
return $out;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|