From a01232d374e2554e210ae9153799671b3b625631 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sat, 16 Apr 2022 02:30:24 -0300 Subject: [PATCH] Fixes and add Header and INPUT to Request class --- system/engine/exception.php | 4 ++-- system/engine/response.php | 2 +- system/request/autoload.php | 43 +++++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/system/engine/exception.php b/system/engine/exception.php index fd969c5..d92b156 100644 --- a/system/engine/exception.php +++ b/system/engine/exception.php @@ -27,8 +27,8 @@ class Exception extends \Exception * @param \Exception $object * @return $this */ - public function setObject(\Exception $object){ - $this->message = $object->getMessage(); + public function setObject($object){ + $this->message = get_class($object).": ".$object->getMessage(); $this->line = $object->getLine(); $this->heritageTrace = $object->getTrace(); $this->file = $object->getFile(); diff --git a/system/engine/response.php b/system/engine/response.php index c319fa0..f8d59a1 100644 --- a/system/engine/response.php +++ b/system/engine/response.php @@ -162,7 +162,7 @@ final class Response { * @param string|null $description * @return void */ - public function code(int $code, string $description = null){ + public function code($code, $description = null){ $this->addHeader("HTTP/1.1 ".$code.(($description) ? " ". $description : "")); $this->addHeader("Status: ".$code.""); } diff --git a/system/request/autoload.php b/system/request/autoload.php index e5bf2b3..a2f6973 100644 --- a/system/request/autoload.php +++ b/system/request/autoload.php @@ -93,6 +93,12 @@ final class Request { */ static protected $_SERVER = []; + /** + * + * @var array|string + */ + static protected $_HEADERS = []; + /** * * @var array|string|false @@ -107,6 +113,7 @@ final class Request { self::$_COOKIE = $this->clean($_COOKIE); self::$_FILES = $this->clean($_FILES); self::$_SERVER = $this->clean($_SERVER); + self::$_HEADERS = $this->clean(getallheaders()); self::$_METHOD = (isset(self::$_SERVER['REQUEST_METHOD'])) ? $this->clean(self::$_SERVER['REQUEST_METHOD']) : false; $this->get =& self::$_GET; @@ -124,12 +131,12 @@ final class Request { * * @since 1.0.0 */ - public function clean($data) { + static public function clean($data) { if (is_array($data)) { foreach ($data as $key => $value) { unset($data[$key]); - $data[$this->clean($key)] = $this->clean($value); + $data[self::clean($key)] = self::clean($value); } } else { $data = htmlspecialchars($data, ENT_COMPAT); @@ -154,6 +161,22 @@ final class Request { return $key ? (isset(self::$_POST[$key]) ? self::$_POST[$key] : null) : self::$_POST; } + /** + * Return php://input from POST or PUT requests. + * + * @param string $key (optional) + * @return string|array + * + * @since 2.0.0 + */ + static public function INPUT($key = null){ + if (self::HEADER("Content-Type") == 'application/json'){ + $data = (JSON::decode(file_get_contents('php://input'))); + } + + return (self::HEADER("Content-Type") == 'application/json') ? (($key) ? $data[$key] : $data) : self::clean(file_get_contents('php://input')); + } + /** * Return GET values from $_GET. * @@ -234,6 +257,22 @@ final class Request { return $key ? (isset(self::$_SERVER[$key]) ? self::$_SERVER[$key] : null) : self::$_SERVER; } + /** + * Return HEADERS values from getallheaders(). + * + * @param string $key + * @param mixed $value + * @return mixed + * + * @since 2.0.0 + */ + static public function HEADER($key = null, $value = null){ + if ($value !== null) + self::$_HEADERS[$key] = $value; + + return $key ? (isset(self::$_HEADERS[$key]) ? self::$_HEADERS[$key] : null) : self::$_HEADERS; + } + /** * Return HTTP method from $_SERVER['REQUEST_METHOD']. *