From 127bd75fd5fdc2417628996563edba8326c0f688 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 15 Apr 2022 00:32:37 -0300 Subject: [PATCH] New Requests methods --- system/engine/controller.php | 4 +- system/login/autoload.php | 6 +- system/request/autoload.php | 265 ++++++++++++++++++++++++++++++---- system/system.php | 8 +- system/translate/autoload.php | 38 ++--- system/url/autoload.php | 3 +- system/url/seo_url.php | 21 +-- 7 files changed, 275 insertions(+), 70 deletions(-) diff --git a/system/engine/controller.php b/system/engine/controller.php index 18fd596..4371730 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -256,7 +256,7 @@ abstract class Controller { $tpl = new \Phacil\Framework\Render($this->registry); - $pegRout = explode("/", ($this->registry->routeOrig)?: $this->request->get['route']); + $pegRout = explode("/", ($this->registry->routeOrig)?: Request::GET('route')); $pegRoutWithoutLast = $pegRout; array_pop($pegRoutWithoutLast); $pegRoutWithoutPenultimate = $pegRoutWithoutLast; @@ -328,7 +328,7 @@ abstract class Controller { * @throws Exception * @final */ - final protected function out ($commonChildren = true) { + protected function out ($commonChildren = true) { if($commonChildren === true){ $this->children = array_merge(array( 'common/footer', diff --git a/system/login/autoload.php b/system/login/autoload.php index 7514066..c0e6eea 100644 --- a/system/login/autoload.php +++ b/system/login/autoload.php @@ -99,10 +99,10 @@ class Login implements \Phacil\Framework\Login\Interfaces\Login { if (!((isset($this->engine->session->data['MM_Username'])) && ($this->isAuthorized("",$this->MM_authorizedUsers, $this->engine->session->data['MM_Username'], $this->engine->session->data['MM_UserGroup'])))) { $MM_qsChar = "?"; - $MM_referrer = $this->engine->request->server['PHP_SELF']; + $MM_referrer = Request::SERVER('PHP_SELF'); if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; - if (isset($this->engine->request->server['QUERY_STRING']) && strlen($this->engine->request->server['QUERY_STRING']) > 0) - $MM_referrer .= "?" . $this->engine->request->server['QUERY_STRING']; + if (Request::SERVER('QUERY_STRING') && strlen(Request::SERVER('QUERY_STRING')) > 0) + $MM_referrer .= "?" . Request::SERVER('QUERY_STRING'); $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; diff --git a/system/request/autoload.php b/system/request/autoload.php index dd4b3fd..e5bf2b3 100644 --- a/system/request/autoload.php +++ b/system/request/autoload.php @@ -8,36 +8,121 @@ namespace Phacil\Framework; -/** @package Phacil\Framework */ +/** + * The Request class with escaped values. + * + * @package Phacil\Framework + * @since 1.0.0 + */ final class Request { + /** + * @deprecated since version 2.0, to be removed in 3.0. + * @var array|string + */ public $get = array(); + + /** + * @deprecated since version 2.0, to be removed in 3.0. + * @var array|string + */ public $post = array(); + + /** + * @deprecated since version 2.0, to be removed in 3.0. + * @var array|string + */ public $cookie = array(); + + /** + * @deprecated since version 2.0, to be removed in 3.0. + * @var array|string + */ public $files = array(); + + /** + * @deprecated since version 2.0, to be removed in 3.0. + * @var array|string + */ public $server = array(); + + /** + * @deprecated since version 2.0, to be removed in 3.0. + * @var array|string + */ + public $request = []; + + /** + * @deprecated since version 2.0, to be removed in 3.0. + * @var array|string|false + */ public $method; + /** + * + * @var array|string + */ + static protected $_GET = []; + + /** + * + * @var array|string + */ + static protected $_POST = []; + + /** + * + * @var array|string + */ + static protected $_REQUEST = []; + + /** + * + * @var array|string + */ + static protected $_COOKIE = []; + + /** + * + * @var array|string + */ + static protected $_FILES = []; + + /** + * + * @var array|string + */ + static protected $_SERVER = []; + + /** + * + * @var array|string|false + */ + static protected $_METHOD; + /** @return void */ public function __construct() { - $_GET = $this->clean($_GET); - $_POST = $this->clean($_POST); - $_REQUEST = $this->clean($_REQUEST); - $_COOKIE = $this->clean($_COOKIE); - $_FILES = $this->clean($_FILES); - $_SERVER = $this->clean($_SERVER); + self::$_GET = $this->clean($_GET); + self::$_POST = $this->clean($_POST); + self::$_REQUEST = $this->clean($_REQUEST); + self::$_COOKIE = $this->clean($_COOKIE); + self::$_FILES = $this->clean($_FILES); + self::$_SERVER = $this->clean($_SERVER); + self::$_METHOD = (isset(self::$_SERVER['REQUEST_METHOD'])) ? $this->clean(self::$_SERVER['REQUEST_METHOD']) : false; - $this->get = $_GET; - $this->post = $_POST; - $this->request = $_REQUEST; - $this->cookie = $_COOKIE; - $this->files = $_FILES; - $this->server = $_SERVER; - $this->method = (isset($this->server['REQUEST_METHOD'])) ? $this->clean($this->server['REQUEST_METHOD']) : false; + $this->get =& self::$_GET; + $this->post =& self::$_POST; + $this->request =& self::$_REQUEST; + $this->cookie =& self::$_COOKIE; + $this->files =& self::$_FILES; + $this->server =& self::$_SERVER; + $this->method =& self::$_METHOD; } /** * @param string|array $data * @return array|string + * + * @since 1.0.0 */ public function clean($data) { if (is_array($data)) { @@ -53,17 +138,130 @@ final class Request { return $data; } - /** @return bool */ + /** + * Return POST values from $_POST or $_FILES. + * + * @param string $key (optional) + * @param mixed $value (optional) + * @return mixed + * + * @since 2.0.0 + */ + static public function POST($key = null, $value = null){ + if($value !== null) + self::$_POST[$key] = $value; + + return $key ? (isset(self::$_POST[$key]) ? self::$_POST[$key] : null) : self::$_POST; + } + + /** + * Return GET values from $_GET. + * + * @param string $key (optional) + * @param mixed $value (optional) + * @return mixed + * + * @since 2.0.0 + */ + static public function GET($key = null, $value = null){ + if ($value !== null) + self::$_GET[$key] = $value; + + return $key ? (isset(self::$_GET[$key]) ? self::$_GET[$key] : null) : self::$_GET; + } + + /** + * Return REQUEST values from $_REQUEST. + * + * @param string $key (optional) + * @param mixed $value (optional) + * @return mixed + * + * @since 2.0.0 + */ + static public function REQUEST($key = null, $value = null){ + if ($value !== null) + self::$_REQUEST[$key] = $value; + + return $key ? (isset(self::$_REQUEST[$key]) ? self::$_REQUEST[$key] : null) : self::$_REQUEST; + } + + /** + * Return FILES values from $_FILES. + * + * @param string $key (optional) If null return all values. + * @param mixed $value (optional) If not null, set the value for this key. + * @return mixed + * + * @since 2.0.0 + */ + static public function FILES($key = null, $value = null){ + if ($value !== null) + self::$_FILES[$key] = $value; + + return $key ? (isset(self::$_FILES[$key]) ? self::$_FILES[$key] : null) : self::$_FILES; + } + + /** + * Return COOKIE values from $_COOKIE. + * + * @param string $key + * @param mixed $value + * @return mixed + * + * @since 2.0.0 + */ + static public function COOKIE($key = null, $value = null){ + if ($value !== null) + self::$_COOKIE[$key] = $value; + + return $key ? (isset(self::$_COOKIE[$key]) ? self::$_COOKIE[$key] : null) : self::$_COOKIE; + } + + /** + * Return SERVER values from $_SERVER. + * + * @param string $key + * @param mixed $value + * @return mixed + * + * @since 2.0.0 + */ + static public function SERVER($key = null, $value = null){ + if ($value !== null) + self::$_SERVER[$key] = $value; + + return $key ? (isset(self::$_SERVER[$key]) ? self::$_SERVER[$key] : null) : self::$_SERVER; + } + + /** + * Return HTTP method from $_SERVER['REQUEST_METHOD']. + * + * @return string|false|null + * + * @since 2.0.0 + */ + static public function METHOD(){ + return self::$_METHOD; + } + + /** @return bool + * @since 1.5.0 + */ public function isPOST() { return $this->is('POST'); } - /** @return bool */ + /** @return bool + * @since 1.5.0 + */ public function isGET() { return $this->is('GET'); } - /** @return bool */ + /** @return bool + * @since 1.5.0 + */ public function isHEAD() { return $this->is('HEAD'); } @@ -71,44 +269,55 @@ final class Request { /** * * @return bool + * @since 1.5.0 */ public function isPUT() { return $this->is('PUT'); } - + /** * * @return bool + * @since 1.5.0 */ public function isDELETE() { return $this->is('DELETE'); } - - /** @return bool */ + + /** @return bool + * @since 1.5.0 + */ public function isCONNECT() { return $this->is('CONNECT') ; } - - /** @return bool */ + + /** @return bool + * @since 1.5.0 + */ public function isOPTIONS() { return $this->is('OPTIONS') ; } - - /** @return bool */ + + /** @return bool + * @since 1.5.0 + */ public function isTRACE() { return $this->is('TRACE'); } - - /** @return bool */ + + /** @return bool + * @since 1.5.0 + */ public function isPATCH() { return $this->is('PATCH'); } - + /** * @param string $method * @return bool + * @since 1.5.0 */ public function is($method){ - return ($this->method == $method) ? true : false; + return (self::$_METHOD == $method); } } diff --git a/system/system.php b/system/system.php index 38a8831..389f3c4 100644 --- a/system/system.php +++ b/system/system.php @@ -412,7 +412,7 @@ set_exception_handler(function ($e) use (&$engine) { } }); -if(defined('DB_DRIVER')) +if(\Phacil\Framework\Config::DB_DRIVER()) $engine->db = new Database(\Phacil\Framework\Config::DB_DRIVER(), \Phacil\Framework\Config::DB_HOSTNAME(), \Phacil\Framework\Config::DB_USERNAME(), \Phacil\Framework\Config::DB_PASSWORD(), \Phacil\Framework\Config::DB_DATABASE()); // Settings @@ -552,11 +552,11 @@ if($engine->controllerPreActions()){ } // Router -if (isset($engine->request->get['route'])) { - $action = new Action($engine->request->get['route']); +if (Request::GET('route')) { + $action = new Action(Request::GET('route')); } else { $default = (\Phacil\Framework\Config::DEFAULT_ROUTE()) ? \Phacil\Framework\Config::DEFAULT_ROUTE() : \Phacil\Framework\Config::DEFAULT_ROUTE('common/home'); - $engine->request->get['route'] = $default; + Request::GET('route', $default); $action = new Action($default); } diff --git a/system/translate/autoload.php b/system/translate/autoload.php index c01eac1..e049b51 100644 --- a/system/translate/autoload.php +++ b/system/translate/autoload.php @@ -9,6 +9,7 @@ namespace Phacil\Framework; use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; +use Phacil\Framework\Registry; /** @package Phacil\Framework */ @@ -22,38 +23,37 @@ final class Translate { /** * - * @var Request + * @var \Phacil\Framework\Session */ - private $request; + private $session; /** * - * @var Session + * @var \Phacil\Framework\Database */ - private $session; + private $db; /** * - * @var Caches + * @var \Phacil\Framework\Caches */ protected $cache; public function __construct(){ - $this->request = new Request(); - - $this->session = new Session(); + $this->session = Registry::getInstance()->session; $this->autoLang = (isset($this->session->data['lang'])) ? $this->session->data['lang'] : NULL; - $this->cookie = (isset($this->request->cookie['lang'])) ? $this->request->cookie['lang'] : NULL; + $this->cookie = (Request::COOKIE(['lang'])) ?: NULL; - $this->cache = new Caches(); + $this->cache = Registry::getInstance()->cache; + + $this->db = Registry::getInstance()->db; if($this->autoLang != NULL) { setcookie("lang", ($this->autoLang), strtotime( '+90 days' )); } - } @@ -64,19 +64,15 @@ final class Translate { * @throws PhpfastcacheInvalidArgumentException */ public function translation ($value, $lang = NULL) { - - global $db; - - //$cache = new Caches(); - + $lang = ($lang != NULL) ? $lang : $this->autoLang; if($this->cache->check("lang_".$lang."_".md5($value))) { return $this->cache->get("lang_".$lang."_".md5($value)); } else { - $sql = "SELECT * FROM translate WHERE text = '".$db->escape($value)."'"; - $result = $db->query($sql, false); + $sql = "SELECT * FROM translate WHERE text = '".$this->db->escape($value)."'"; + $result = $this->db->query($sql, false); if ($result->num_rows == 1) { if(isset($result->row[$lang]) and $result->row[$lang] != "") { @@ -101,9 +97,7 @@ final class Translate { * @param string $value * @return void */ - public function insertBaseText ($value){ - global $db; - - $db->query("INSERT INTO translate SET text='".$db->escape($value)."'"); + public function insertBaseText ($value){ + $this->db->query("INSERT INTO translate SET text='".$this->db->escape($value)."'"); } } \ No newline at end of file diff --git a/system/url/autoload.php b/system/url/autoload.php index ced64fd..cacf16e 100644 --- a/system/url/autoload.php +++ b/system/url/autoload.php @@ -9,6 +9,7 @@ namespace Phacil\Framework; use Phacil\Framework\Config; +use Phacil\Framework\Request; class Url { @@ -52,7 +53,7 @@ class Url { $this->ssl = $ssl; $this->cdn = Config::CDN() ?: false; - if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) { + if ((Request::SERVER('HTTPS') == 'on') || (Request::SERVER('HTTPS') == '1')) { $this->baseurl = $ssl; } else { $this->baseurl = $url; diff --git a/system/url/seo_url.php b/system/url/seo_url.php index 09b5812..669fcff 100644 --- a/system/url/seo_url.php +++ b/system/url/seo_url.php @@ -11,6 +11,7 @@ use Phacil\Framework\Action; use Phacil\Framework\Controller as ControllerController; use Phacil\Framework\Registry; use Phacil\Framework\Config; +use Phacil\Framework\Request; class SystemUrlSeoUrl extends ControllerController { @@ -52,9 +53,9 @@ class SystemUrlSeoUrl extends ControllerController { $match = array(); // Decode URL - if (isset($this->request->get['_route_'])) { - $parts_count = explode('/', $this->request->get['_route_']); - $parts = array($this->request->get['_route_']); + if (Request::GET('_route_')) { + $parts_count = explode('/', Request::GET('_route_')); + $parts = array(Request::GET('_route_')); foreach ($parts as $part) { if(Config::USE_DB_CONFIG()) @@ -73,13 +74,13 @@ class SystemUrlSeoUrl extends ControllerController { } } - $this->request->get['route'] = $query->row['query']; + Request::GET('route', $query->row['query']); } elseif (Config::ROUTES() && is_array(Config::ROUTES())) { $rotas = Config::ROUTES(); if(isset($rotas[$part])){ - $this->request->get['route'] = $rotas[$part]; + Request::GET('route', $rotas[$part]); } else { foreach($rotas as $key => $page) { @@ -110,20 +111,20 @@ class SystemUrlSeoUrl extends ControllerController { } if(isset($pagina)) { - $this->request->get['route'] = $pagina; + Request::GET('route', $pagina); } else { - $this->request->get['route'] = $this->notfound; + Request::GET('route', $this->notfound); } } } else { - $this->request->get['route'] = $this->notfound; + Request::GET('route', $this->notfound); } } - if (isset($this->request->get['route'])) { - return $this->forward($this->request->get['route'], $match); + if (Request::GET('route')) { + return $this->forward(Request::GET('route'), $match); } } }