diff --git a/system/Cookies/SameSite.php b/system/Cookies/SameSite.php new file mode 100644 index 0000000..4f57043 --- /dev/null +++ b/system/Cookies/SameSite.php @@ -0,0 +1,49 @@ +value = self::STRICT; + return $this; + } + + public function setLax(){ + $this->value = self::LAX; + return $this; + } + + public function setNone(){ + $this->value = self::NONE; + return $this; + } + + public function getValue() { + return $this->value; + } + + /** + * + * @return $this + */ + static public function getInstance() + { + $class = get_called_class(); + return \Phacil\Framework\Registry::getAutoInstance((new $class())); + } +} \ No newline at end of file diff --git a/system/Cookies/autoload.php b/system/Cookies/autoload.php new file mode 100644 index 0000000..7720a43 --- /dev/null +++ b/system/Cookies/autoload.php @@ -0,0 +1,190 @@ +expiry = $expiry; + $this->path = $path; + $this->domain = $domain; + $this->secure = $secure; + $this->httpOnly = $httpOnly; + $this->sameSite = $sameSite; + } + + /** + * + * @param string $name The name of the cookie. + * @param mixed $value The value of the cookie. This value is stored on the clients computer, do not store sensitive information like passwords, personal docs ids, etc... + * @param bool $isRaw (Optional) Send a cookie without urlencoding the cookie value + * @return bool If output exists prior to calling this function, setcookie will fail and return false. If setcookie successfully runs, it will return true. This does not indicate whether the user accepted the cookie. + */ + public function setCookie($name, $value, $isRaw = false) + { + if (version_compare(phpversion(), "7.3.0", "<")) { + if($isRaw){ + return setrawcookie($name, $value, $this->expiry, $this->path . "; samesite=".$this->sameSite, $this->domain, $this->secure, $this->httpOnly); + } + return setcookie($name, $value, $this->expiry, $this->path. "; samesite=".$this->sameSite, $this->domain, $this->secure, $this->httpOnly); + } else { + if ($isRaw) { + return setrawcookie($name, $value, [ + 'expires' => $this->expiry, + 'path' => $this->path, + 'domain' => $this->domain, + 'secure' => $this->secure, + 'httponly' => $this->httpOnly, + 'samesite' => $this->sameSite + ]); + } + return setcookie($name, $value, [ + 'expires' => $this->expiry, + 'path' => $this->path, + 'domain' => $this->domain, + 'secure' => $this->secure, + 'httponly' => $this->httpOnly, + 'samesite' => $this->sameSite + ]); + } + //return $this; + } + + /** + * + * @param string $name + * @return bool + */ + public function cookieExists($name) + { + return isset($_COOKIE[$name]); + } + + /** + * @param int $expires + * @return $this + */ + public function setExpires($expires) { + $this->expiry = $expires; + return $this; + } + + /** + * @param string $path + * @return $this + */ + public function setPath($path) { + $this->path = $path; + return $this; + } + + /** + * @param string $domain + * @return $this + */ + public function setDomain($domain) { + $this->domain = $domain; + return $this; + } + + /** + * @param bool $secure + * @return $this + */ + public function setSecure($secure) { + $this->secure = $secure; + return $this; + } + + /** + * @param bool $httpOnly + * @return $this + */ + public function setHttpOnly($httpOnly) { + $this->httpOnly = $httpOnly; + return $this; + } + + /** + * + * @param \Phacil\Framework\Cookies\SameSite $sameSite + * @return $this + */ + public function setSameSite(SameSite $sameSite) { + $this->sameSite = $sameSite->getValue(); + return $this; + } + + /** + * @param string $name + * @return void + */ + public function deleteCookie($name) { + if ($this->cookieExists($name)) { + setcookie($name, '', time() - 3600, $this->path, $this->domain, $this->secure, $this->httpOnly); + unset($_COOKIE[$name]); + } + } + + /** + * @param string $name + * @return void + */ + public function unsetCookie($name) { + return $this->deleteCookie($name); + } +} \ No newline at end of file diff --git a/system/database/Databases/MSSQL.php b/system/database/Databases/MSSQL.php index 748892d..d653808 100644 --- a/system/database/Databases/MSSQL.php +++ b/system/database/Databases/MSSQL.php @@ -58,7 +58,7 @@ class MSSQL implements \Phacil\Framework\Interfaces\Databases \mssql_free_result($resource); /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $query = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$data]); + $query = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]); $query->setNumRows($i); unset($data); diff --git a/system/database/Databases/MySQL_PDO.php b/system/database/Databases/MySQL_PDO.php index c9e8b02..03ef952 100644 --- a/system/database/Databases/MySQL_PDO.php +++ b/system/database/Databases/MySQL_PDO.php @@ -104,7 +104,7 @@ class MySQL_PDO implements Databases //$sth= $this->dbh->query($sql); $this->affectedRows = $sth->rowCount(); /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $data = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$sth ? $sth->fetchAll(\PDO::FETCH_ASSOC) : array()]); + $data = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$sth ? $sth->fetchAll(\PDO::FETCH_ASSOC) : array()]); $data->setNumRows($this->affectedRows); return $data; } @@ -196,7 +196,7 @@ class MySQL_PDO implements Databases if ($stmt->columnCount()) { /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $data = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$stmt->fetchAll(\PDO::FETCH_ASSOC)]); + $data = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$stmt->fetchAll(\PDO::FETCH_ASSOC)]); $data->setNumRows($stmt->rowCount()); $stmt->closeCursor(); diff --git a/system/database/Databases/MySQLi.php b/system/database/Databases/MySQLi.php index 8181ad9..96487da 100644 --- a/system/database/Databases/MySQLi.php +++ b/system/database/Databases/MySQLi.php @@ -91,7 +91,7 @@ class MySQLi implements Databases { if (!$this->connection->errno) { if ($query instanceof \mysqli_result) { /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $result = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$query->fetch_all(MYSQLI_ASSOC)]); + $result = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$query->fetch_all(MYSQLI_ASSOC)]); $result->setNumRows($query->num_rows); $query->close(); return $result; @@ -191,7 +191,7 @@ class MySQLi implements Databases { if ($result instanceof \mysqli_result) { //$resultObj = new \Phacil\Framework\Databases\Object\Result($result->fetch_all(MYSQLI_ASSOC)); /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $resultObj = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$result->fetch_all(MYSQLI_ASSOC)]); + $resultObj = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$result->fetch_all(MYSQLI_ASSOC)]); $resultObj->setNumRows($result->num_rows); $result->close(); diff --git a/system/database/Databases/Object/Result.php b/system/database/Databases/Object/Result.php index 96f1ea4..e84b663 100644 --- a/system/database/Databases/Object/Result.php +++ b/system/database/Databases/Object/Result.php @@ -173,7 +173,7 @@ class Result extends \ArrayIterator implements ResultInterface { $data = parent::offsetGet($index); /** @var \Phacil\Framework\Databases\Object\ItemInterface */ - $item = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ItemInterface", [$data]); + $item = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ItemInterface::class, [$data]); return $item; } @@ -184,7 +184,7 @@ class Result extends \ArrayIterator implements ResultInterface { public function current() { /** @var \Phacil\Framework\Databases\Object\ItemInterface */ - $item = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ItemInterface", [parent::current()]); + $item = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ItemInterface::class, [parent::current()]); return $item; } diff --git a/system/database/Databases/Object/ResultCacheIterator.php b/system/database/Databases/Object/ResultCacheIterator.php index 8228bf3..2f8e02c 100644 --- a/system/database/Databases/Object/ResultCacheIterator.php +++ b/system/database/Databases/Object/ResultCacheIterator.php @@ -184,7 +184,7 @@ class ResultCacheIterator extends \CachingIterator implements ResultInterface { if(!$data) return null; /** @var \Phacil\Framework\Databases\Object\ItemInterface */ - $item = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ItemInterface", [$data]); + $item = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ItemInterface::class, [$data]); return $item; } diff --git a/system/database/Databases/Oracle_PDO.php b/system/database/Databases/Oracle_PDO.php index 4dd504b..e1d11e5 100644 --- a/system/database/Databases/Oracle_PDO.php +++ b/system/database/Databases/Oracle_PDO.php @@ -104,7 +104,7 @@ class Oracle_PDO implements Databases $this->statement = null; /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $result = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$data]); + $result = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]); $result->setNumRows($this->rowCount); } } catch (\PDOException $e) { @@ -179,7 +179,7 @@ class Oracle_PDO implements Databases if ($stmt->columnCount()) { /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $data = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$stmt->fetchAll(\PDO::FETCH_ASSOC)]); + $data = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$stmt->fetchAll(\PDO::FETCH_ASSOC)]); $data->setNumRows($stmt->rowCount()); $stmt->closeCursor(); diff --git a/system/database/Databases/SQLSRV.php b/system/database/Databases/SQLSRV.php index e928a5e..8089d59 100644 --- a/system/database/Databases/SQLSRV.php +++ b/system/database/Databases/SQLSRV.php @@ -83,7 +83,7 @@ class SQLSRV implements Databases { \sqlsrv_free_stmt($resource); /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $query = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$data]); + $query = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]); $query->setNumRows($i); unset($data); @@ -164,7 +164,7 @@ class SQLSRV implements Databases { } /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $resultObj = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$data]); + $resultObj = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]); $resultObj->setNumRows(\sqlsrv_num_rows($stmt)); return $resultObj; diff --git a/system/database/Databases/SQLite3.php b/system/database/Databases/SQLite3.php index aad8d90..facdaf3 100644 --- a/system/database/Databases/SQLite3.php +++ b/system/database/Databases/SQLite3.php @@ -67,7 +67,7 @@ class SQLite3 implements Databases { } /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $result = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$data]); + $result = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]); $result->setNumRows((!empty($data)) ? count($data) : 0); $query->finalize(); @@ -148,7 +148,7 @@ class SQLite3 implements Databases { } /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $resultObj = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$data]); + $resultObj = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]); $resultObj->setNumRows(count($data)); $result->finalize(); diff --git a/system/database/Databases/mPDO.php b/system/database/Databases/mPDO.php index 49d54f2..e22e26a 100644 --- a/system/database/Databases/mPDO.php +++ b/system/database/Databases/mPDO.php @@ -86,7 +86,7 @@ class mPDO implements Databases { $this->statement->closeCursor(); $this->statement = null; /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $result = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$data]); + $result = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]); $result->setNumRows($this->rowCount); } @@ -157,7 +157,7 @@ class mPDO implements Databases { if ($stmt->columnCount()) { /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $data = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$stmt->fetchAll(\PDO::FETCH_ASSOC)]); + $data = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$stmt->fetchAll(\PDO::FETCH_ASSOC)]); $data->setNumRows($stmt->rowCount()); $stmt->closeCursor(); diff --git a/system/database/Databases/sqlsrvPDO.php b/system/database/Databases/sqlsrvPDO.php index 846b46c..86a87dc 100644 --- a/system/database/Databases/sqlsrvPDO.php +++ b/system/database/Databases/sqlsrvPDO.php @@ -100,7 +100,7 @@ class sqlsrvPDO implements Databases { } /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $result = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$data]); + $result = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]); $result->setNumRows($this->statement->rowCount()); } } catch (\PDOException $e) { @@ -182,7 +182,7 @@ class sqlsrvPDO implements Databases { if ($this->statement->columnCount()) { /** @var \Phacil\Framework\Databases\Object\ResultInterface */ - $data = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$this->statement->fetchAll(\PDO::FETCH_ASSOC)]); + $data = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$this->statement->fetchAll(\PDO::FETCH_ASSOC)]); $data->setNumRows($this->statement->rowCount()); $this->statement->closeCursor(); diff --git a/system/engine/controller.php b/system/engine/controller.php index f2ee97e..1a7a4c0 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -288,7 +288,7 @@ abstract class Controller implements \Phacil\Framework\Interfaces\Controller { /** * @var \Phacil\Framework\Render */ - $tpl = $this->registry->getInstance("Phacil\Framework\Render"); + $tpl = $this->registry->getInstance(\Phacil\Framework\Render::class); $pegRout = explode("/", ($this->registry->routeOrig)?: \Phacil\Framework\startEngineExacTI::getRoute()); diff --git a/system/engine/document.php b/system/engine/document.php index 9a5c266..38d080c 100644 --- a/system/engine/document.php +++ b/system/engine/document.php @@ -238,8 +238,5 @@ class Document { } else { return $ref; } - - } - } diff --git a/system/engine/registry.php b/system/engine/registry.php index 8ebb093..26f6764 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -17,7 +17,7 @@ */ final class Registry { - const FACTORY_CLASS = 'Phacil\Framework\Factory'; + const FACTORY_CLASS = \Phacil\Framework\Factory::class; const FACTORY_WORD_KEY = "Factory"; @@ -143,13 +143,10 @@ final class Registry { if(is_string($class)) { $classCreate = self::checkPreference($class); return self::getInstance()->injectionClass($classCreate, $args, true); - /* $reflector = new ReflectionClass($classCreate); - return self::setAutoInstance($reflector->newInstanceArgs($args), $class); */ } if (is_object($class)) { return self::setAutoInstance($class); - //return self::getInstance($class); } return null; @@ -386,19 +383,22 @@ final class Registry { } if ($param->getClass()) { $injectionClass = $param->getClass()->name; + $classAttr = self::checkPreference($injectionClass); - if (class_exists($injectionClass)) { + if (class_exists($classAttr)) { $argsToInject[$param->getPosition()] = $this->injectionClass($injectionClass); continue; } if (!$param->isOptional()) { - throw new \Phacil\Framework\Exception\ReflectionException("Error Processing Request: " . $injectionClass . "not exist"); + throw new \Phacil\Framework\Exception\ReflectionException("Error Processing Request: " . $injectionClass . " not exist"); } } } else { if ($param->getType()) { $injectionClass = $param->getType()->getName(); - if (class_exists($injectionClass)) { + $classAttr = self::checkPreference($injectionClass); + + if (class_exists($classAttr)) { $argsToInject[$param->getPosition()] = $this->injectionClass($injectionClass); continue; } elseif (substr($injectionClass, -(strlen(self::FACTORY_WORD_KEY))) === self::FACTORY_WORD_KEY) { @@ -409,7 +409,7 @@ final class Registry { continue; } if (!$param->isOptional()) { - throw new \Phacil\Framework\Exception\ReflectionException("Error Processing Request: " . $injectionClass . "not exist"); + throw new \Phacil\Framework\Exception\ReflectionException("Error Processing Request: " . $injectionClass . " not exist"); } } } diff --git a/system/engine/render.php b/system/engine/render.php index c0fa149..b3bb13e 100644 --- a/system/engine/render.php +++ b/system/engine/render.php @@ -8,7 +8,6 @@ namespace Phacil\Framework; - use Phacil\Framework\Translate; use Phacil\Framework\Registry; use Phacil\Framework\Config; @@ -219,7 +218,9 @@ /** * @var \Phacil\Framework\templateEngines\Twig\Api\Extension\TranslateInterface */ - $translateExtension = $this->registry->getInstance('Phacil\Framework\templateEngines\Twig\Api\Extension\TranslateInterface'); + $translateExtension = $this->registry->getInstance( + \Phacil\Framework\templateEngines\Twig\Api\Extension\TranslateInterface::class + ); $twig->addExtension($translateExtension); $twig->addFilter(new $Twig_SimpleFilter('translate', function ($str) use ($translateExtension){ @@ -259,9 +260,10 @@ 'cache_file_mode' => 0666, 'loader' => new \Mustache_Loader_FilesystemLoader($this->templatePath), 'helpers' => array('translate' => function ($text) { - if (class_exists('Translate')) { - $trans = new Translate(); - return ($trans->translation($text)); + if (class_exists('Phacil\Framework\Translate')) { + /** @var \Phacil\Framework\Translate */ + $trans = $this->registry->getInstance(\Phacil\Framework\Translate::class); + return $trans->translation($text); } else { return $text; } // do something translate here... @@ -291,8 +293,9 @@ $smarty->registerPlugin("block", "translate", function ($text) { if (class_exists('Phacil\Framework\Translate')) { - $trans = new Translate(); - return ($trans->translation($text)); + /** @var \Phacil\Framework\Translate */ + $trans = $this->registry->getInstance(\Phacil\Framework\Translate::class); + return $trans->translation($text); } else { return $text; } // do something translate here... diff --git a/system/etc/preferences.json b/system/etc/preferences.json index b2994c4..8f89966 100644 --- a/system/etc/preferences.json +++ b/system/etc/preferences.json @@ -4,6 +4,8 @@ "Phacil\\Framework\\Interfaces\\Loader": "Phacil\\Framework\\Loader", "Phacil\\Framework\\Interfaces\\Url": "Phacil\\Framework\\Url", "Phacil\\Framework\\Databases\\Object\\ItemInterface": "Phacil\\Framework\\Databases\\Object\\Item", - "Phacil\\Framework\\Api\\Database": "Phacil\\Framework\\Database" + "Phacil\\Framework\\Api\\Database": "Phacil\\Framework\\Database", + "Cm\\RedisSession\\Handler\\ConfigInterface": "Phacil\\Framework\\Session\\Redis\\Config", + "Cm\\RedisSession\\Handler\\LoggerInterface": "Phacil\\Framework\\Session\\Redis\\Logger" } } \ No newline at end of file diff --git a/system/request/autoload.php b/system/request/autoload.php index 0927967..74750ab 100644 --- a/system/request/autoload.php +++ b/system/request/autoload.php @@ -101,7 +101,7 @@ final class Request { /** * - * @var array|string|false + * @var string|false */ static protected $_METHOD; @@ -148,8 +148,8 @@ final class Request { /** * Return POST values from $_POST or $_FILES. * - * @param string $key (optional) - * @param mixed $value (optional) + * @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 @@ -172,9 +172,9 @@ final class Request { static public function INPUT($key = null){ if (self::HEADER("Content-Type") == 'application/json'){ try{ - $data = self::clean(JSON::decode(file_get_contents('php://input'))); + $data = self::clean(\Phacil\Framework\Json::decode(file_get_contents('php://input'))); } catch (\Exception $e){ - throw new \UnexpectedValueException($e->getMessage(), $e->getCode()); + throw new \Phacil\Framework\Exception\UnexpectedValueException($e->getMessage(), $e->getCode()); } } else { $data = self::clean(file_get_contents('php://input')); @@ -186,8 +186,8 @@ final class Request { /** * Return GET values from $_GET. * - * @param string $key (optional) - * @param mixed $value (optional) + * @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 @@ -202,8 +202,8 @@ final class Request { /** * Return REQUEST values from $_REQUEST. * - * @param string $key (optional) - * @param mixed $value (optional) + * @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 @@ -218,8 +218,8 @@ final class 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. + * @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 @@ -234,8 +234,8 @@ final class Request { /** * Return COOKIE values from $_COOKIE. * - * @param string $key - * @param mixed $value + * @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 @@ -250,8 +250,8 @@ final class Request { /** * Return SERVER values from $_SERVER. * - * @param string $key - * @param mixed $value + * @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 @@ -266,8 +266,8 @@ final class Request { /** * Return HEADERS values from getallheaders(). * - * @param string $key - * @param mixed $value + * @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 @@ -290,21 +290,24 @@ final class Request { return self::$_METHOD; } - /** @return bool + /** + * @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() { @@ -329,28 +332,32 @@ final class Request { 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() { @@ -365,4 +372,150 @@ final class Request { public function is($method){ return (self::$_METHOD == $method); } -} + + /** + * + * @param string $key (Optional) If null, return all data + * @return mixed + */ + public function getPost($key = null){ + return self::POST($key); + } + + /** + * + * @param string $key (Optional) If null, return all data + * @return mixed + */ + public function getParam($key = null){ + return self::GET($key); + } + + /** + * + * @param string $key (Optional) If null, return all data + * @return mixed + */ + public function getCookie($key = null){ + return self::COOKIE($key); + } + + /** + * + * @param string $key (Optional) If null, return all data + * @return mixed + */ + public function getInput($key = null){ + return self::INPUT($key); + } + + /** + * + * @param string $key (Optional) If null, return all data + * @return mixed + */ + public function getRequest($key = null){ + return self::REQUEST($key); + } + + /** + * + * @param string $key (Optional) If null, return all data + * @return mixed + */ + public function getFiles($key = null){ + return self::FILES($key); + } + + /** + * + * @param string $key (Optional) If null, return all data + * @return mixed + */ + public function getServer($key = null){ + return self::SERVER($key); + } + + /** + * + * @param string $key (Optional) If null, return all data + * @return mixed + */ + public function getHeader($key = null){ + return self::HEADER($key); + } + + /** + * + * @param string $key + * @param mixed $value + * @return $this + */ + public function setPostValue($key, $value){ + self::$_POST[$key] = $value; + return $this; + } + + /** + * + * @param string $key + * @param mixed $value + * @return $this + */ + public function setParamValue($key, $value){ + self::$_GET[$key] = $value; + return $this; + } + + /** + * + * @param string $key + * @param mixed $value + * @return $this + */ + public function setCookieValue($key, $value){ + self::$_COOKIE[$key] = $value; + return $this; + } + + /** + * + * @param string $key + * @param mixed $value + * @return $this + */ + public function setRequestValue($key, $value){ + self::$_REQUEST[$key] = $value; + return $this; + } + + /** + * + * @param string $key + * @param mixed $value + * @return $this + */ + public function setFilesValue($key, $value){ + self::$_FILES[$key] = $value; + return $this; + } + + /** + * + * @param string $key + * @param mixed $value + * @return $this + */ + public function setHeaderValue($key, $value){ + self::$_HEADERS[$key] = $value; + return $this; + } + + /** + * Return the HTTP method used + * @return string + */ + public function getMethod(){ + return self::METHOD(); + } +} \ No newline at end of file diff --git a/system/session/Redis/Config.php b/system/session/Redis/Config.php index 1b2b2ae..ad0ce25 100644 --- a/system/session/Redis/Config.php +++ b/system/session/Redis/Config.php @@ -63,23 +63,17 @@ class Config implements ConfigInterface { /** * - * @var \Phacil\Framework\Registry + * @var \Phacil\Framework\Config */ - protected $engine; + private $config; /** * Get the Framework instance * @return void */ - public function __construct(){ - $this->engine = \Phacil\Framework\Registry::getInstance(); + public function __construct(\Phacil\Framework\Config $config){ + $this->config = $config; $this->logLevel = \Phacil\Framework\Config::Debug() ? 6 : $this->logLevel; - $fg = []; - foreach (get_class_methods($this) as $key => $value) { - # code... - if ($value != '__construct' && $value != 'getLogLevel') - $fg[$value] = $this->$value(); - } return; } @@ -89,7 +83,7 @@ class Config implements ConfigInterface { * {@inheritdoc} */ function getLogLevel() { - return $this->engine->config->get('session_redis_log_level') ?: $this->logLevel; + return $this->config->get('session_redis_log_level') ?: $this->logLevel; } /** @@ -97,7 +91,7 @@ class Config implements ConfigInterface { */ public function getHost() { - return $this->engine->config->get('session_redis_dsn') ? : self::PARAM_HOST; + return $this->config->get('session_redis_dsn') ? : self::PARAM_HOST; } /** @@ -105,7 +99,7 @@ class Config implements ConfigInterface { */ public function getPort() { - return $this->engine->config->get('session_redis_port') ?: self::PARAM_PORT; + return $this->config->get('session_redis_port') ?: self::PARAM_PORT; } /** @@ -113,7 +107,7 @@ class Config implements ConfigInterface { */ public function getDatabase() { - return (string)$this->engine->config->get('session_redis_database') ?: self::PARAM_DATABASE; + return (string)$this->config->get('session_redis_database') ?: self::PARAM_DATABASE; } /** @@ -121,7 +115,7 @@ class Config implements ConfigInterface { */ public function getPassword() { - return $this->engine->config->get('session_redis_password') ?: ''; + return $this->config->get('session_redis_password') ?: ''; } /** @@ -129,7 +123,7 @@ class Config implements ConfigInterface { */ public function getTimeout() { - return $this->engine->config->get('session_redis_timeout') ? : self::PARAM_TIMEOUT; + return $this->config->get('session_redis_timeout') ? : self::PARAM_TIMEOUT; } /** @@ -137,7 +131,7 @@ class Config implements ConfigInterface { */ public function getPersistentIdentifier() { - return $this->engine->config->get('session_redis_persistent_id') ?: ''; + return $this->config->get('session_redis_persistent_id') ?: ''; } /** @@ -145,7 +139,7 @@ class Config implements ConfigInterface { */ public function getCompressionThreshold() { - return $this->engine->config->get('session_redis_compression_threshold') ?: self::DEFAULT_COMPRESSION_THRESHOLD ; + return $this->config->get('session_redis_compression_threshold') ?: self::DEFAULT_COMPRESSION_THRESHOLD ; } /** @@ -153,7 +147,7 @@ class Config implements ConfigInterface { */ public function getCompressionLibrary() { - return $this->engine->config->get('session_redis_compression_library') ?: (self::PARAM_COMPRESSION_LIBRARY); + return $this->config->get('session_redis_compression_library') ?: (self::PARAM_COMPRESSION_LIBRARY); } /** @@ -161,7 +155,7 @@ class Config implements ConfigInterface { */ public function getMaxConcurrency() { - return $this->engine->config->get('session_redis_max_concurrency') ?: (self::PARAM_MAX_CONCURRENCY); + return $this->config->get('session_redis_max_concurrency') ?: (self::PARAM_MAX_CONCURRENCY); } /** @@ -169,7 +163,7 @@ class Config implements ConfigInterface { */ public function getMaxLifetime() { - return $this->engine->config->get('session_redis_max_lifetime') ?: self::SESSION_MAX_LIFETIME; + return $this->config->get('session_redis_max_lifetime') ?: self::SESSION_MAX_LIFETIME; } /** @@ -177,7 +171,7 @@ class Config implements ConfigInterface { */ public function getMinLifetime() { - return (int)$this->engine->config->get('session_redis_min_lifetime') ?: (self::PARAM_MIN_LIFETIME); + return (int)$this->config->get('session_redis_min_lifetime') ?: (self::PARAM_MIN_LIFETIME); } /** @@ -185,7 +179,7 @@ class Config implements ConfigInterface { */ public function getDisableLocking() { - return (bool)$this->engine->config->get('session_redis_disable_locking') ?: (self::PARAM_DISABLE_LOCKING); + return (bool)$this->config->get('session_redis_disable_locking') ?: (self::PARAM_DISABLE_LOCKING); } /** @@ -193,7 +187,7 @@ class Config implements ConfigInterface { */ public function getBotLifetime() { - return (int) $this->engine->config->get('session_redis_bot_lifetime') ?: (self::PARAM_BOT_LIFETIME); + return (int) $this->config->get('session_redis_bot_lifetime') ?: (self::PARAM_BOT_LIFETIME); } /** @@ -201,7 +195,7 @@ class Config implements ConfigInterface { */ public function getBotFirstLifetime() { - return (string)$this->engine->config->get('session_redis_bot_first_lifetime') ?: (self::PARAM_BOT_FIRST_LIFETIME); + return (string)$this->config->get('session_redis_bot_first_lifetime') ?: (self::PARAM_BOT_FIRST_LIFETIME); } /** @@ -209,7 +203,7 @@ class Config implements ConfigInterface { */ public function getFirstLifetime() { - return (int)$this->engine->config->get('session_redis_first_lifetime') ?: (self::PARAM_FIRST_LIFETIME); + return (int)$this->config->get('session_redis_first_lifetime') ?: (self::PARAM_FIRST_LIFETIME); } /** @@ -217,7 +211,7 @@ class Config implements ConfigInterface { */ public function getBreakAfter() { - return (int)$this->engine->config->get('session_redis_break_after') ?: (self::PARAM_BREAK_AFTER); + return (int)$this->config->get('session_redis_break_after') ?: (self::PARAM_BREAK_AFTER); } /** @@ -225,7 +219,7 @@ class Config implements ConfigInterface { */ public function getLifetime() { - return (int)$this->engine->config->get('session_redis_expire') ?: self::PARAM_SESSION_LIFETIME; + return (int)$this->config->get('session_redis_expire') ?: self::PARAM_SESSION_LIFETIME; } /** @@ -233,7 +227,7 @@ class Config implements ConfigInterface { */ public function getSentinelServers() { - return $this->engine->config->get('session_redis_sentinel_servers') ?: null; + return $this->config->get('session_redis_sentinel_servers') ?: null; } /** @@ -241,7 +235,7 @@ class Config implements ConfigInterface { */ public function getSentinelMaster() { - return $this->engine->config->get('session_redis_sentinel_master') ?: null; + return $this->config->get('session_redis_sentinel_master') ?: null; } /** @@ -249,7 +243,7 @@ class Config implements ConfigInterface { */ public function getSentinelVerifyMaster() { - return $this->engine->config->get('session_redis_verify_master') ?: (null); + return $this->config->get('session_redis_verify_master') ?: (null); } /** @@ -257,7 +251,7 @@ class Config implements ConfigInterface { */ public function getSentinelConnectRetries() { - return $this->engine->config->get('session_redis_sentinel_connect_retries') ?: (self::PARAM_SENTINEL_CONNECT_RETRIES); + return $this->config->get('session_redis_sentinel_connect_retries') ?: (self::PARAM_SENTINEL_CONNECT_RETRIES); } /** @@ -265,7 +259,7 @@ class Config implements ConfigInterface { */ public function getFailAfter() { - return (int)$this->engine->config->get('session_redis_fail_after') ?: self::DEFAULT_FAIL_AFTER; + return (int)$this->config->get('session_redis_fail_after') ?: self::DEFAULT_FAIL_AFTER; } } \ No newline at end of file diff --git a/system/session/autoload.php b/system/session/autoload.php index c2d2a6f..b4ffedc 100644 --- a/system/session/autoload.php +++ b/system/session/autoload.php @@ -8,7 +8,6 @@ namespace Phacil\Framework; -use Phacil\Framework\Credis; use Phacil\Framework\Config; /** @@ -54,9 +53,19 @@ class Session * * @var \Phacil\Framework\Session\Redis\Handler */ - protected $saveHandler; + private $saveHandler; - protected $redisExpire; + /** + * + * @var \Phacil\Framework\Registry + */ + private $registry; + + /** + * + * @var \Phacil\Framework\Cookies\SameSite + */ + private $sameSite; /** * @@ -68,13 +77,20 @@ class Session * @param string $redis_prefix * @return void */ - public function __construct($redis = false) - { + public function __construct( + \Phacil\Framework\Registry $registry, + Config $config, + \Phacil\Framework\Cookies\SameSite $sameSite + ) { + $this->registry = $registry; + + $this->sameSite = $sameSite; + $this->name = (Config::SESSION_PREFIX() ?: 'SESS') . (isset($_SERVER['REMOTE_ADDR']) ? md5($_SERVER['REMOTE_ADDR']) : md5(date("dmY"))); //define('SESSION_PREFIX_INTERNAL_REDIS', Config::REDIS_SESSION_PREFIX() ?: 'phacil_'); - $this->redis($redis); + $this->redis($config->get('session_redis')); if (!session_id()) { $this->openSession(); @@ -104,7 +120,15 @@ class Session if ($this->isSecure()) ini_set('session.cookie_secure', 1); - session_set_cookie_params(0, '/'); + if (version_compare(phpversion(), "7.3.0", "<")) { + session_set_cookie_params(0, '/; samesite=' . $this->sameSite->getValue()); + } else { + session_set_cookie_params([ + 'lifetime' => 0, + 'path' => '/', + 'samesite' => $this->sameSite->getValue() + ]); + } //session_id(md5()); session_name($this->name); session_start(); @@ -125,13 +149,10 @@ class Session */ private function redis($redis = false) { - if (!$redis) return false; - $redisConfig = new \Phacil\Framework\Session\Redis\Config(); - - $this->saveHandler = new \Phacil\Framework\Session\Redis\Handler($redisConfig, new \Phacil\Framework\Session\Redis\Logger($redisConfig)); + $this->saveHandler = $this->registry->getInstance(\Phacil\Framework\Session\Redis\Handler::class); $this->saveHandler->setName($this->name); diff --git a/system/system.php b/system/system.php index c6e862b..d9ca58f 100644 --- a/system/system.php +++ b/system/system.php @@ -163,8 +163,9 @@ final class startEngineExacTI { * @return string|false */ private function checkPHPversion() { - if (version_compare(phpversion(), '5.4.40', '>') == false) { - trigger_error('PHP 5.4.40+ Required', E_ERROR); + if (version_compare(phpversion(), '5.6.20', '>') == false) { + trigger_error('PHP 5.6.20+ Required', E_USER_ERROR); + die('PHP 5.6.20+ Required'); } else { return phpversion(); } @@ -367,12 +368,12 @@ final class startEngineExacTI { // Translate if(!isset($this->registry->$key) && $key == 'translate'){ - $this->translate = new Translate(); + $this->translate = $this->registry->getInstance(\Phacil\Framework\Translate::class); } // Session if(!isset($this->registry->$key) && $key == 'session'){ - $this->session = new Session(); + $this->session = $this->registry->getInstance(\Phacil\Framework\Session::class); } return (isset($this->registry->$key)) ?: NULL; @@ -398,7 +399,7 @@ $engine->engine = $engine; /** * @var \Phacil\Framework\Interfaces\Loader */ -$engine->load = $engine->getRegistry()->create("Phacil\Framework\Interfaces\Loader", [$engine->registry]); +$engine->load = $engine->getRegistry()->create(\Phacil\Framework\Interfaces\Loader::class, [$engine->registry]); // Config /** @var Config */ @@ -422,7 +423,7 @@ set_exception_handler(function ($e) use (&$engine) { }); if(\Phacil\Framework\Config::DB_DRIVER()) - $engine->db = $engine->getRegistry()->create("Phacil\Framework\Api\Database", [ + $engine->db = $engine->getRegistry()->create(\Phacil\Framework\Api\Database::class, [ \Phacil\Framework\Config::DB_DRIVER(), \Phacil\Framework\Config::DB_HOSTNAME(), \Phacil\Framework\Config::DB_USERNAME(), @@ -470,7 +471,10 @@ if($engine->config->get('PatternSiteTitle') == true) { /** * @var \Phacil\Framework\Interfaces\Url */ -$engine->url = $engine->getRegistry()->create("Phacil\Framework\Interfaces\Url", [$engine->config->get('config_url'), $engine->config->get('config_use_ssl') ? $engine->config->get('config_ssl') : $engine->config->get('config_url')]); +$engine->url = $engine->getRegistry()->create(\Phacil\Framework\Interfaces\Url::class, [ + $engine->config->get('config_url'), + $engine->config->get('config_use_ssl') ? $engine->config->get('config_ssl') : $engine->config->get('config_url') +]); // Log if(!$engine->config->get('config_error_filename')){ @@ -531,16 +535,15 @@ $engine->cache = new Caches(); //$engine->request = new Request(); // Response -$engine->response = new Response(); +/** @var Response */ +$engine->response = $engine->registry->getInstance(\Phacil\Framework\Response::class); $engine->response->addHeader('Content-Type: text/html; charset=utf-8'); if($engine->config->get('config_compression')) $engine->response->setCompression($engine->config->get('config_compression')); // Session -$engine->session = new Session( - $engine->config->get('session_redis') -); +$engine->session = $engine->getRegistry()->create(\Phacil\Framework\Session::class); // Document $engine->document = new Document(); diff --git a/system/templateEngines/Twig/Extension/Legacy/Translate.php b/system/templateEngines/Twig/Extension/Legacy/Translate.php index 9617d3e..f32bb1c 100644 --- a/system/templateEngines/Twig/Extension/Legacy/Translate.php +++ b/system/templateEngines/Twig/Extension/Legacy/Translate.php @@ -50,7 +50,7 @@ class Translate extends \Twig_Extension implements TranslateInterface $body = array_shift($params); /** @var \Phacil\Framework\Translate */ - $trans = \Phacil\Framework\Registry::getInstance("Phacil\Framework\Translate"); + $trans = \Phacil\Framework\Registry::getInstance(\Phacil\Framework\Translate::class); echo ($trans->translation($body)); } @@ -62,7 +62,7 @@ class Translate extends \Twig_Extension implements TranslateInterface $body = array_shift($params); /** @var \Phacil\Framework\Translate */ - $trans = \Phacil\Framework\Registry::getInstance("Phacil\Framework\Translate"); + $trans = \Phacil\Framework\Registry::getInstance(\Phacil\Framework\Translate::class); echo ($trans->translation($body)); } } diff --git a/system/templateEngines/Twig/Extension/Translate.php b/system/templateEngines/Twig/Extension/Translate.php index ca9b977..f718efc 100644 --- a/system/templateEngines/Twig/Extension/Translate.php +++ b/system/templateEngines/Twig/Extension/Translate.php @@ -51,7 +51,7 @@ class Translate extends \Twig\Extension\AbstractExtension implements TranslateIn $body = array_shift($params); /** @var \Phacil\Framework\Translate */ - $trans = \Phacil\Framework\Registry::getInstance("Phacil\Framework\Translate"); + $trans = \Phacil\Framework\Registry::getInstance(\Phacil\Framework\Translate::class); echo ($trans->translation($body)); } @@ -64,7 +64,7 @@ class Translate extends \Twig\Extension\AbstractExtension implements TranslateIn $body = array_shift($params); /** @var \Phacil\Framework\Translate */ - $trans = \Phacil\Framework\Registry::getInstance("Phacil\Framework\Translate"); + $trans = \Phacil\Framework\Registry::getInstance(\Phacil\Framework\Translate::class); echo ($trans->translation($body)); } } \ No newline at end of file diff --git a/system/templateEngines/Twig/autoload.php b/system/templateEngines/Twig/autoload.php index 03b1399..c2614fe 100644 --- a/system/templateEngines/Twig/autoload.php +++ b/system/templateEngines/Twig/autoload.php @@ -1,6 +1,6 @@ ') == false) { define('TwigFolderLoad', 'Twig1x'); define('TwigLoaderFilesystem', 'Twig_Loader_Filesystem'); @@ -9,7 +9,7 @@ namespace { define('TwigExtensionDebug', 'Twig_Extension_Debug'); if(!\Phacil\Framework\Registry::checkPreferenceExist($preferenceDIObj)){ - \Phacil\Framework\Registry::addDIPreference($preferenceDIObj, "Phacil\\Framework\\templateEngines\\Twig\\Extension\\Legacy\\Translate"); + \Phacil\Framework\Registry::addDIPreference($preferenceDIObj, \Phacil\Framework\templateEngines\Twig\Extension\Legacy\Translate::class); } } else { define('TwigLoaderFilesystem', '\Twig\Loader\FilesystemLoader'); diff --git a/system/translate/autoload.php b/system/translate/autoload.php index 29f14d1..88d8dc0 100644 --- a/system/translate/autoload.php +++ b/system/translate/autoload.php @@ -52,17 +52,17 @@ class Translate { */ protected $table = 'translate'; - public function __construct(){ + public function __construct(Registry $registry){ - $this->session = Registry::getInstance()->session; + $this->session = $registry->session; $this->autoLang = (isset($this->session->data['lang'])) ? $this->session->data['lang'] : NULL; $this->cookie = (Request::COOKIE('lang')) ?: NULL; - $this->cache = Registry::getInstance()->cache; + $this->cache = $registry->cache; - $this->db = Registry::getInstance()->db; + $this->db = $registry->db; if($this->autoLang != NULL) { setcookie("lang", ($this->autoLang), strtotime( '+90 days' ));