diff --git a/src/Ease/Anonym.php b/src/Ease/Anonym.php index e731180..da8dd2e 100644 --- a/src/Ease/Anonym.php +++ b/src/Ease/Anonym.php @@ -1,20 +1,20 @@ - * @copyright 2009-2021 Vitex@hippy.cz (G) - * + * @copyright 2009-2023 Vitex@hippy.cz (G) + * * @category User Classes * @package EasePHP - * + * * PHP 7 - * + * */ +declare(strict_types=1); + namespace Ease; /** @@ -22,7 +22,6 @@ */ class Anonym extends Brick implements Person { - /** * User type. * @@ -33,7 +32,7 @@ class Anonym extends Brick implements Person /** * An Anonymous user ID is always null * - * @var null + * @var int|null */ public $userID = null; @@ -169,5 +168,4 @@ public function logout() return true; } - } diff --git a/src/Ease/Atom.php b/src/Ease/Atom.php index 3ed6ad7..72080b9 100644 --- a/src/Ease/Atom.php +++ b/src/Ease/Atom.php @@ -1,24 +1,22 @@ - * @copyright 2009-2021 Vitex@hippy.cz (G) + * @copyright 2009-2023 Vitex@hippy.cz (G) * @license https://opensource.org/licenses/MIT - * + * * PHP 8 */ namespace Ease; /** - * Basic Class of EasePHP0 - * + * Basic Class of EasePHP + * * @author Vítězslav Dvořák */ class Atom @@ -59,5 +57,4 @@ public function draw() { return $this->__toString(); } - } diff --git a/src/Ease/Brick.php b/src/Ease/Brick.php index f28649f..2c0a175 100644 --- a/src/Ease/Brick.php +++ b/src/Ease/Brick.php @@ -1,23 +1,24 @@ * @copyright 2009-2023 Vitex@hippy.cz (G) - * + * * PHP 7 */ + +declare(strict_types=1); + +namespace Ease; + class Brick extends Sand { - use RecordKey; + /** - * Name Column - * @var string + * @var string Name Column */ public $nameColumn = null; @@ -86,7 +87,7 @@ public function howToProcess($identifer) $recognizedAs = 'reuse'; } break; - default : + default: case "boolean": case "NULL": $recognizedAs = 'unknown'; @@ -109,7 +110,7 @@ public function setObjectName($objectName = null) if (is_null($objectName)) { $key = $this->getMyKey($this->data); if ($key) { - $result = parent::setObjectName(get_class($this).'@'.$key); + $result = parent::setObjectName(get_class($this) . '@' . $key); } else { $result = parent::setObjectName(); } @@ -126,16 +127,14 @@ public function setObjectName($objectName = null) */ public function setInit($init) { - } /** * Set/override object properties stub - * + * * @param array $properties */ public function setProperties($properties) { - } -} \ No newline at end of file +} diff --git a/src/Ease/Exception.php b/src/Ease/Exception.php index d717478..c60db08 100644 --- a/src/Ease/Exception.php +++ b/src/Ease/Exception.php @@ -1,16 +1,16 @@ - * @copyright 2009-2022 Vitex@hippy.cz (G) - * + * @copyright 2009-2023 Vitex@hippy.cz (G) + * * PHP 7 */ +declare(strict_types=1); + namespace Ease; /** @@ -20,10 +20,9 @@ */ class Exception extends \Exception { - /** * Ease Framework Exception - * + * * @param string $message of exception * @param int $code error code * @param \Ease\Exception $previous @@ -41,5 +40,4 @@ public function __construct($message, $code = 0, Exception $previous = null) \Ease\Shared::logger()->addStatusObject(new Logger\Message($message, 'error', $caller)); parent::__construct($message, $code, $previous); } - } diff --git a/src/Ease/Functions.php b/src/Ease/Functions.php index 2558e72..cdd6251 100644 --- a/src/Ease/Functions.php +++ b/src/Ease/Functions.php @@ -1,20 +1,20 @@ * @copyright 2019-2023 Vitex@hippy.cz (G) * @license https://opensource.org/licenses/MIT GPL-2 - * + * * PHP 7 * PHP 8 */ +declare(strict_types=1); + namespace Ease; /** @@ -22,8 +22,8 @@ * * @author Vítězslav Dvořák */ -class Functions { - +class Functions +{ /** * Returns PATH modified for current operating system. * @@ -31,7 +31,8 @@ class Functions { * * @return string */ - public static function sysFilename($path) { + public static function sysFilename($path) + { return str_replace(['\\', '/'], constant('DIRECTORY_SEPARATOR'), $path); } @@ -41,11 +42,11 @@ public static function sysFilename($path) { * @param string $url originall url * @param array $addParams value to add * @param boolean $override replace already existing values ? - * + * * @return string url with parameters added */ - public static function addUrlParams($url, array $addParams, - $override = false) { + public static function addUrlParams($url, array $addParams, $override = false) + { $urlParts = parse_url($url); $urlFinal = ''; if (array_key_exists('scheme', $urlParts)) { @@ -59,8 +60,8 @@ public static function addUrlParams($url, array $addParams, } if (array_key_exists('query', $urlParts)) { parse_str($urlParts['query'], $queryUrlParams); - $urlParams = $override ? array_merge($queryUrlParams, $addParams) : array_merge($addParams, - $queryUrlParams); + $urlParams = $override ? array_merge($queryUrlParams, $addParams) : + array_merge($addParams, $queryUrlParams); } else { $urlParams = $addParams; } @@ -78,8 +79,8 @@ public static function addUrlParams($url, array $addParams, * @param array $destinationArray destination * @param string $columName item to move */ - public static function divDataArray(&$sourceArray, &$destinationArray, - $columName) { + public static function divDataArray(&$sourceArray, &$destinationArray, $columName) + { $result = false; if (array_key_exists($columName, $sourceArray)) { $destinationArray[$columName] = $sourceArray[$columName]; @@ -98,7 +99,8 @@ public static function divDataArray(&$sourceArray, &$destinationArray, * * @return bool */ - public static function isAssoc(array $arr) { + public static function isAssoc(array $arr) + { return array_keys($arr) !== range(0, count($arr) - 1); } @@ -107,7 +109,8 @@ public static function isAssoc(array $arr) { * * @param string $text */ - public static function rip($text) { + public static function rip($text) + { $convertTable = [ 'ä' => 'a', 'Ä' => 'A', @@ -207,7 +210,8 @@ public static function rip($text) { * * @return string encrypted text */ - public static function easeEncrypt($textToEncrypt, $encryptKey) { + public static function easeEncrypt($textToEncrypt, $encryptKey) + { $encryptionKey = base64_decode($encryptKey); $ivec = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); $encrypted = openssl_encrypt($textToEncrypt, 'aes-256-cbc', $encryptionKey, 0, $ivec); @@ -222,7 +226,8 @@ public static function easeEncrypt($textToEncrypt, $encryptKey) { * * @return string */ - public static function easeDecrypt($textToDecrypt, $encryptKey) { + public static function easeDecrypt($textToDecrypt, $encryptKey) + { $encryptionKey = base64_decode($encryptKey); list($encryptedData, $ivec ) = explode('::', base64_decode($textToDecrypt), 2); return openssl_decrypt($encryptedData, 'aes-256-cbc', $encryptionKey, 0, $ivec); @@ -236,7 +241,8 @@ public static function easeDecrypt($textToDecrypt, $encryptKey) { * * @return float */ - public static function randomNumber($minimal = null, $maximal = null) { + public static function randomNumber($minimal = null, $maximal = null) + { if (isset($minimal) && isset($maximal)) { if ($minimal >= $maximal) { throw new Exception('Minimum cannot be bigger than maximum'); @@ -257,9 +263,9 @@ public static function randomNumber($minimal = null, $maximal = null) { * * @return string */ - public static function randomString($length = 6) { - return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), - 0, $length); + public static function randomString($length = 6) + { + return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, $length); } /** @@ -271,13 +277,13 @@ public static function randomString($length = 6) { * * @return array array recoded */ - public static function recursiveIconv(string $inCharset, string $outCharset, $arr) { + public static function recursiveIconv(string $inCharset, string $outCharset, $arr) + { if (!is_array($arr)) { return \iconv($inCharset, $outCharset, $arr); } $ret = $arr; - array_walk_recursive($ret, '\Ease\Functions::arrayIconv', - [$inCharset, $outCharset, $arr]); + array_walk_recursive($ret, '\Ease\Functions::arrayIconv', [$inCharset, $outCharset, $arr]); return $ret; } @@ -291,8 +297,8 @@ public static function recursiveIconv(string $inCharset, string $outCharset, $ar * @param string $key * @param mixed $encodings */ - public static function arrayIconv(&$val, /** @scrutinizer ignore-unused */ - $key, $encodings) { + public static function arrayIconv(&$val, /** @scrutinizer ignore-unused */ $key, $encodings) + { $val = iconv($encodings[0], $encodings[1], $val); } @@ -303,7 +309,8 @@ public static function arrayIconv(&$val, /** @scrutinizer ignore-unused */ * * @return string */ - public static function humanFilesize(int $filesize) { + public static function humanFilesize(int $filesize) + { $decr = 1024; $step = 0; $prefix = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB']; @@ -324,15 +331,15 @@ public static function humanFilesize(int $filesize) { * * @return array */ - public static function reindexArrayBy(array $data, string $indexBy = null) { + public static function reindexArrayBy(array $data, string $indexBy = null) + { $reindexedData = []; foreach ($data as $dataRow) { if (array_key_exists($indexBy, $dataRow)) { $reindexedData[(string) $dataRow[$indexBy]] = $dataRow; } else { - throw new \Exception(sprintf('Data row does not contain column %s for reindexing', - $indexBy)); + throw new \Exception(sprintf('Data row does not contain column %s for reindexing', $indexBy)); } } @@ -341,22 +348,23 @@ public static function reindexArrayBy(array $data, string $indexBy = null) { /** * Filter Only letters from string. - * Pouze malé a velké písmena. * * @return string text bez zvláštních znaků */ - public static function lettersOnly($text): string { + public static function lettersOnly($text): string + { return preg_replace('/[^(a-zA-Z0-9)]*/', '', strval($text)); } /** * Confirm that string is serialized - * + * * @param string $data * * @return boolean */ - public static function isSerialized(string $data): bool { + public static function isSerialized(string $data): bool + { $data = trim($data); if ('N;' == $data) { return true; @@ -365,16 +373,16 @@ public static function isSerialized(string $data): bool { return false; } switch ($badions[1]) { - case 'a' : - case 'O' : - case 's' : + case 'a': + case 'O': + case 's': if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data)) { return true; } break; - case 'b' : - case 'i' : - case 'd' : + case 'b': + case 'i': + case 'd': if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data)) { return true; } @@ -385,25 +393,26 @@ public static function isSerialized(string $data): bool { /** * Get Classname without namespace prefix - * + * * @param object $object - * - * @return string + * + * @return string|null */ - static public function baseClassName($object) { - return is_object($object) ? basename(str_replace('\\', '/', - get_class($object))) : null; + public static function baseClassName($object) + { + return is_object($object) ? basename(str_replace('\\', '/', get_class($object))) : null; } /** * Human readable bytes repersentation - * - * @param int|double $bytes - * + * + * @param int|double $dbytes + * * @return string */ - public static function formatBytes($bytes) { - $bytes = doubleval($bytes); + public static function formatBytes($dbytes) + { + $bytes = doubleval($dbytes); if ($bytes < 1024) { $humanReadable = $bytes . ' B'; @@ -429,33 +438,35 @@ public static function formatBytes($bytes) { /** * Get configuration from constant or environment - * + * * @deprecated since version 1.40.1 use \Ease\Shared::cfg() instead - * + * * @param string $constant * @param mixed $cfg Default value - * + * * @return string|int|boolean|null */ - public static function cfg(/*string*/ $constant, $cfg = null) { + public static function cfg(/*string*/ $constant, $cfg = null) + { return \Ease\Shared::cfg($constant, $cfg); } /** * Get All Classes in namespace - * + * * @param string $namespace - * + * * @return array */ - public static function classesInNamespace($namespace) { + public static function classesInNamespace($namespace) + { $namespace .= '\\'; $myClasses = array_filter(get_declared_classes(), function ($item) use ($namespace) { return substr($item, 0, strlen($namespace)) === $namespace; }); $theClasses = []; - foreach ($myClasses AS $class): + foreach ($myClasses as $class) : $theParts = explode('\\', $class); $theClasses[] = end($theParts); endforeach; @@ -465,12 +476,13 @@ public static function classesInNamespace($namespace) { /** * Load all files found for given namespace * (based on composer files) - * + * * @param string $namespace - * + * * @return array of loaded files className=>filePath */ - public static function loadClassesInNamespace($namespace) { + public static function loadClassesInNamespace($namespace) + { $loaded = []; $autoloader = preg_grep('/autoload\.php$/', get_included_files()); if (!empty($autoloader)) { @@ -482,8 +494,8 @@ public static function loadClassesInNamespace($namespace) { if (is_file($modulePath . '/' . $entry) && (pathinfo($entry, PATHINFO_EXTENSION) == 'php')) { $classesBefore = get_declared_classes(); include_once $modulePath . '/' . $entry; - $diff = array_diff(get_declared_classes(),$classesBefore); - $load = preg_grep('/'. addslashes($namespace).'/', $diff); + $diff = array_diff(get_declared_classes(), $classesBefore); + $load = preg_grep('/' . addslashes($namespace) . '/', $diff); foreach ($load as $class) { $loaded[$class] = $modulePath . '/' . $entry; } @@ -495,5 +507,4 @@ public static function loadClassesInNamespace($namespace) { } return $loaded; } - } diff --git a/src/Ease/Locale.php b/src/Ease/Locale.php index 7fb7c20..ee3bf76 100644 --- a/src/Ease/Locale.php +++ b/src/Ease/Locale.php @@ -1,16 +1,16 @@ * @copyright 2019-2023 Vitex@hippy.cz (G) - * + * * PHP 7 */ +declare(strict_types=1); + namespace Ease; /** @@ -18,31 +18,31 @@ * * @author vitex */ -class Locale { - +class Locale +{ /** - * @var Locale Singleton is stored here + * @var Locale|null Singleton is stored here */ public static $instance; /** * Current Used locale code * - * @var string + * @var string */ public static $localeUsed = null; /** * i18n files location * - * @var string dirpath + * @var string dirpath */ public static $i18n = null; /** * GetText Domain * - * @var string + * @var string */ public static $textDomain = ''; @@ -490,14 +490,13 @@ class Locale { /** * Use of the localization preparation. - * + * * @param string $setLocale en_US|cs_CZ|.. * @param string $i18n directory ( /usr/lib/locale/ in Debian ) * @param string $textDomain we want use $i18n/$setLocale/LC_ALL/$textDomain.mo */ - public function __construct($setLocale = null, $i18n = '../i18n', - $textDomain = null - ) { + public function __construct($setLocale = null, $i18n = '../i18n', $textDomain = null) + { if (empty($setLocale)) { $setLocale = self::getPreferedLocale(); } @@ -514,15 +513,17 @@ public function __construct($setLocale = null, $i18n = '../i18n', /** * Prefered Locale Code - 1) Requested 2) Session 3) Browser for WebPage or * getenv('LC_ALL') for CLI - * + * * @param $allowCli boolean Allow use in cli mode (set false for testing) - * - * @return string locale code + * + * @return string locale code */ - public static function getPreferedLocale($allowCli = true) { - // $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); + public static function getPreferedLocale($allowCli = true) + { + // $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); if (\php_sapi_name() === 'cli' && $allowCli) { - $locale = (\Ease\Functions::cfg('LANG') ? \Ease\Functions::cfg('LANG') : (\Ease\Functions::cfg('LC_ALL') ? \Ease\Functions::cfg('LC_ALL') : null )); + $locale = (\Ease\Shared::cfg('LANG') ? \Ease\Shared::cfg('LANG') : + (\Ease\Shared::cfg('LC_ALL') ? \Ease\Shared::cfg('LC_ALL') : null )); } else { $reqLocale = self::requestLocale(); if (is_null($reqLocale)) { @@ -541,37 +542,41 @@ public static function getPreferedLocale($allowCli = true) { /** * Session by page GET or POST request with 'locale' field - * + * * @return string|null Locale Code */ - public static function requestLocale() { + public static function requestLocale() + { return array_key_exists('locale', $_REQUEST) ? $_REQUEST['locale'] : null; } /** * Locale code saved to session field $_SESSION['locale'] - * + * * @return string|null locale code */ - public static function sessionLocale() { + public static function sessionLocale() + { return isset($_SESSION) && array_key_exists('locale', $_SESSION) ? $_SESSION['locale'] : null; } /** * Locale code by browser's default language - * + * * @return string locale code */ - public static function browserLocale() { + public static function browserLocale() + { return self::langToLocale(self::autodetected()); } /** * List of availble locales - * + * * @return array locales availble */ - public function availble() { + public function availble() + { $locales = []; $directory = dir(self::$i18n); while (false !== ($entry = $directory->read())) { @@ -593,7 +598,8 @@ public function availble() { * * @param string $textDomain */ - public static function setTextDomain($textDomain) { + public static function setTextDomain($textDomain) + { self::$textDomain = $textDomain; } @@ -608,27 +614,30 @@ public static function setTextDomain($textDomain) { * * @return string code of locale used */ - public static function initializeGetText($appname, $defaultLocale = 'en_US', $i18n = '../i18n') { + public static function initializeGetText($appname, $defaultLocale = 'en_US', $i18n = '../i18n') + { self::$i18n = $i18n; self::setTextDomain($appname); - return self::useLocale( empty(strval($defaultLocale)) ? 'en_US' : strval($defaultLocale) ); + return self::useLocale(empty(strval($defaultLocale)) ? 'en_US' : strval($defaultLocale)); } /** * Locale Code language from browser language - * + * * @param string $lang browser lan en|cs|.. - * + * * @return string locale code */ - public static function langToLocale($lang) { + public static function langToLocale($lang) + { $defaultLocale = 'C'; $langs = []; foreach (self::$alllngs as $langCode => $language) { $langs[$langCode] = [strstr($langCode, '_') ? substr( - $langCode, 0, - strpos($langCode, '_') - ) : $langCode, $language]; + $langCode, + 0, + strpos($langCode, '_') + ) : $langCode, $language]; } foreach ($langs as $code => $langInfo) { if ($lang == $langInfo[0]) { @@ -641,12 +650,13 @@ public static function langToLocale($lang) { /** * Use Effective locale to requested - * + * * @param string $localeCode locale code to use - * + * * @return string used locale code */ - public static function useLocale(string $localeCode) { + public static function useLocale(string $localeCode) + { \putenv("LC_ALL=$localeCode"); \putenv("LANGUAGUE=$localeCode"); \putenv("LANG=$localeCode"); @@ -667,19 +677,23 @@ public static function useLocale(string $localeCode) { /** * Try to autodetect default language - * - * @return string lang code + * + * @return string lang code */ - public static function autodetected() { - return array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER) && function_exists('\locale_accept_from_http') ? \locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']) : null; + public static function autodetected() + { + return array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER) && + function_exists('\locale_accept_from_http') ? + \locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']) : null; } /** * obtain ISO 639-1 language code - * + * * @return string */ - public function get2Code() { + public function get2Code() + { $localeUsed = $this->getLocaleUsed(); if ($localeUsed && strstr($localeUsed, '_')) { list($code, ) = explode('_', $localeUsed); @@ -691,29 +705,28 @@ public function get2Code() { /** * get Current Used locale code - * + * * @return string */ - public static function getLocaleUsed() { + public static function getLocaleUsed() + { return isset(self::$localeUsed) ? self::$localeUsed : ''; } /** * Common instance of Locale class - * + * * @param string $setLocale en_US|cs_CZ|.. * @param string $i18n directory ( /usr/lib/locale/ in Debian ) * @param string $textDomain we want use $i18n/$setLocale/LC_ALL/$textDomain.mo - * + * * @return \Ease\Locale */ - public static function singleton($setLocale = null, $i18n = '../i18n', - $textDomain = null - ) { + public static function singleton($setLocale = null, $i18n = '../i18n', $textDomain = null) + { if (!isset(self::$instance)) { self::$instance = new self($setLocale, $i18n, $textDomain); } return self::$instance; } - } diff --git a/src/Ease/Logger/Logging.php b/src/Ease/Logger/Logging.php index c144f20..00c36af 100644 --- a/src/Ease/Logger/Logging.php +++ b/src/Ease/Logger/Logging.php @@ -1,18 +1,19 @@ * @copyright 2019 Vitex@hippy.cz (G) * @license https://opensource.org/licenses/MIT MIT - * + * * PHP 7 */ +declare(strict_types=1); + namespace Ease\Logger; /** @@ -21,11 +22,10 @@ */ trait Logging { - /** * Objekt pro logování. * - * @var Regent + * @var Regent|null */ public $logger = null; @@ -36,12 +36,12 @@ trait Logging * @param string $message text zpravy * @param string $type fronta * @param object $caller Message source name - * + * * @return boolean message added */ public function addStatusMessage($message, $type = 'info', $caller = null) { - return $this->getLogger()->addToLog((empty($caller) ? $this : $caller), $message, $type); + return boolval($this->getLogger()->addToLog((empty($caller) ? $this : $caller), $message, $type)); } /** @@ -56,7 +56,7 @@ public function getStatusMessages() /** * Erase all status messages - * + * * @return boolean */ public function cleanSatatusMessages() @@ -66,9 +66,9 @@ public function cleanSatatusMessages() /** * Provide logger object - * + * * @param string|array $options - * + * * @return Regent */ public function getLogger($options = null) @@ -87,8 +87,8 @@ public function getLogger($options = null) */ public function logBanner($prefix = '', $suffix = '') { - if (\Ease\Functions::cfg('DEBUG') === true) { - $suffix .= ' Loggers: ' . \Ease\Functions::cfg('EASE_LOGGER'); + if (\Ease\Shared::cfg('DEBUG') === true) { + $suffix .= ' Loggers: ' . \Ease\Shared::cfg('EASE_LOGGER'); } if (method_exists('Composer\InstalledVersions', 'getPrettyVersion')) { @@ -98,8 +98,9 @@ public function logBanner($prefix = '', $suffix = '') } $this->addStatusMessage( - trim(($prefix ? $prefix : \Ease\Shared::appName()) . ' PHP v' . phpversion() . ' EasePHP Framework v' . $version . ' ' . $suffix), - 'debug' + trim(($prefix ? $prefix : \Ease\Shared::appName()) . + ' PHP v' . phpversion() . ' EasePHP Framework v' . $version . ' ' . $suffix), + 'debug' ); } -} \ No newline at end of file +} diff --git a/src/Ease/Logger/Loggingable.php b/src/Ease/Logger/Loggingable.php index c104357..ae5b19f 100644 --- a/src/Ease/Logger/Loggingable.php +++ b/src/Ease/Logger/Loggingable.php @@ -1,19 +1,19 @@ * @copyright 2019-2021 Vitex@hippy.cz (G) * @license https://opensource.org/licenses/MIT MIT - * + * * PHP 7 */ +declare(strict_types=1); + namespace Ease\Logger; /** @@ -22,6 +22,5 @@ */ interface Loggingable { - public function addToLog($caller, $message, $type = 'message'); } diff --git a/src/Ease/Logger/Message.php b/src/Ease/Logger/Message.php index 52303e4..d98b53c 100644 --- a/src/Ease/Logger/Message.php +++ b/src/Ease/Logger/Message.php @@ -1,18 +1,19 @@ * @copyright 2019-2023 Vitex@hippy.cz (G) * @license https://opensource.org/licenses/MIT MIT - * + * * PHP 7 */ +declare(strict_types=1); + namespace Ease\Logger; /** @@ -22,7 +23,6 @@ */ class Message { - /** * Message body * @@ -38,7 +38,7 @@ class Message public $type; /** - * + * * @var \Ease\Atom */ public $caller; @@ -52,7 +52,7 @@ class Message /** * Basic Message class - * + * * @param string $message text * @param string $type One of info|notice|debug|error * @param \Ease\Atom|string $caller Origin of message @@ -74,7 +74,7 @@ public function __construct($message, $type = 'info', $caller = null, $when = nu * Unicode Symbol for given message type * * @param string $type - * + * * @return string */ public static function getTypeUnicodeSymbol($type, $color = true) @@ -133,9 +133,9 @@ public static function getTypeUnicodeSymbol($type, $color = true) /** * Obtain object name from caller object - * + * * @param object|string $caller - * + * * @return string */ public static function getCallerName($caller) @@ -151,4 +151,4 @@ public static function getCallerName($caller) } return $callerName; } -} \ No newline at end of file +} diff --git a/src/Ease/Logger/Regent.php b/src/Ease/Logger/Regent.php index fb9cf11..a18a541 100644 --- a/src/Ease/Logger/Regent.php +++ b/src/Ease/Logger/Regent.php @@ -1,7 +1,5 @@ loggers[$logger] = ToConsole::singleton(); break; @@ -101,7 +98,6 @@ public function __construct($logger = null) public function takeMessage() { - } /** @@ -110,7 +106,7 @@ public function takeMessage() * @param object $caller message provider * @param string $message message to log * @param string $type info|succes|warning|error|email|... - * + * * @return int How many loggers takes message */ public function addToLog($caller, $message, $type = 'info') @@ -120,7 +116,7 @@ public function addToLog($caller, $message, $type = 'info') /** * Stored messages array - * + * * @return array */ public function getMessages() @@ -139,17 +135,16 @@ public function cleanMessages() /** * Add Status Object to stack - * + * * @param \Ease\Logger\Message $message - * + * * @return int number of stored messages */ public function addStatusObject(Message $message) { $this->messages[] = $message; $logged = 0; - foreach ($this->loggers as $logger) - { + foreach ($this->loggers as $logger) { $logged += $logger->addToLog($message->caller, $message->body, $message->type); } return $logged; @@ -165,5 +160,4 @@ public static function singleton($logger = null) } return self::$instance; } - } diff --git a/src/Ease/Logger/ToConsole.php b/src/Ease/Logger/ToConsole.php index f502927..2b2b5fb 100644 --- a/src/Ease/Logger/ToConsole.php +++ b/src/Ease/Logger/ToConsole.php @@ -1,17 +1,18 @@ * @copyright 2016-2023 Vitex@hippy.cz (G) */ +declare(strict_types=1); + namespace Ease\Logger; /** @@ -21,7 +22,6 @@ */ class ToConsole extends ToMemory implements Loggingable { - /** * Saves obejct instace (singleton...). */ @@ -83,10 +83,10 @@ public function __construct() /** * Set Ansi Color - * + * * @param string $str string to colorize * @param string $color color name - * + * * @return string */ public static function set($str, $color) @@ -112,16 +112,19 @@ public static function set($str, $color) public function addToLog($caller, $message, $type = 'message') { $fmt = datefmt_create( - \Ease\Locale::$localeUsed, - \IntlDateFormatter::FULL, - \IntlDateFormatter::FULL, - date_default_timezone_get(), - \IntlDateFormatter::GREGORIAN, - 'MM/dd/yyyy' + \Ease\Locale::$localeUsed, + \IntlDateFormatter::FULL, + \IntlDateFormatter::FULL, + date_default_timezone_get(), + \IntlDateFormatter::GREGORIAN, + 'MM/dd/yyyy' ); - + $ansiMessage = $this->set(strip_tags(strval($message)), self::getTypeColor($type)); - $logLine = datefmt_format($fmt, new \DateTime) . ' ' . Message::getTypeUnicodeSymbol($type) . ' •' . Message::getCallerName($caller) . '‣ ' . $ansiMessage; + $logLine = datefmt_format($fmt, new \DateTime()) . ' ' . + Message::getTypeUnicodeSymbol($type) . ' •' . + Message::getCallerName($caller) . '‣ ' . + $ansiMessage; $written = 0; switch ($type) { case 'error': @@ -135,8 +138,8 @@ public function addToLog($caller, $message, $type = 'message') } /** - * Get color code for given message - * + * Get color code for given message + * * @param string $type mail|warning|error|debug|success|info */ public static function getTypeColor($type) @@ -182,4 +185,4 @@ public static function singleton() } return self::$instance; } -} \ No newline at end of file +} diff --git a/src/Ease/Logger/ToEmail.php b/src/Ease/Logger/ToEmail.php index 552a07d..28c2537 100644 --- a/src/Ease/Logger/ToEmail.php +++ b/src/Ease/Logger/ToEmail.php @@ -1,7 +1,5 @@ recipient = empty($recipient) ? \Ease\Functions::cfg('EASE_EMAILTO') : $recipient; + $this->recipient = empty($recipient) ? \Ease\Shared::cfg('EASE_EMAILTO') : $recipient; $this->subject = empty($subject) ? $_SERVER['PHP_SELF'] : $subject; $this->mailer = new \Ease\Mailer($this->recipient, $this->subject); $this->mailer->setUserNotification(false); @@ -112,19 +111,19 @@ public function __construct($recipient = '', $subject = null) */ public static function singleton() { - if (!isset(self::$_instance)) { + if (!isset(self::$instance)) { $class = __CLASS__; if (\Ease\Shared::appName()) { - self::$_instance = new $class( - \Ease\Functions::cfg('EASE_EMAILTO',''), - \Ease\Shared::appName() + self::$instance = new $class( + \Ease\Shared::cfg('EASE_EMAILTO', ''), + \Ease\Shared::appName() ); } else { - self::$_instance = new $class('EaseFramework'); + self::$instance = new $class('EaseFramework'); } } - return self::$_instance; + return self::$instance; } /** @@ -134,25 +133,24 @@ public static function singleton() * @param string $message body * @param string $type of message (success|info|error|warning|*) * - * @return null|boolean byl report zapsán ? + * @return int logged message size ? */ public function addToLog($caller, $message, $type = 'notice') { - ++$this->messageID; if (($this->logLevel == 'silent') && ($type != 'error')) { - return; + return 0; } if (($this->logLevel != 'debug') && ($type == 'debug')) { - return; + return 0; } self::$statusMessages[$type][$this->messageID] = $message; - $logLine = strftime("%D %T") . ' `' . is_object($caller) ? get_class($caller) : $caller . '`: ' . $message; + $logLine = strftime("%D %T") . ' `' . Message::getCallerName($caller) . '`: ' . $message; $this->mailer->textBody .= "\n" . $logLine; - return true; + return strlen($message); } /** @@ -164,5 +162,4 @@ public function __destruct() $this->mailer->send(); } } - } diff --git a/src/Ease/Logger/ToEventlog.php b/src/Ease/Logger/ToEventlog.php index 9fa975f..b21e8d3 100644 --- a/src/Ease/Logger/ToEventlog.php +++ b/src/Ease/Logger/ToEventlog.php @@ -1,7 +1,5 @@ - * @copyright 2009-2021 Vitex@hippy.cz (G) + * @copyright 2009-2023 Vitex@hippy.cz (G) */ +declare(strict_types=1); + namespace Ease\Logger; /** @@ -16,7 +16,6 @@ */ class ToFile extends ToMemory implements Loggingable { - /** * Adresář do kterého se zapisují logy. * @@ -43,7 +42,7 @@ class ToFile extends ToMemory implements Loggingable * * @var resource|boolean */ - private $_logFileHandle = null; + private $logFileHandle = null; /** * ID naposledy ulozene zpravy. @@ -76,9 +75,9 @@ public function __construct($baseLogDir = null) /** * Get instanece of File Logger - * + * * @param string $logdir - * + * * @return ToFile */ public static function singleton($logdir = null) @@ -92,25 +91,24 @@ public static function singleton($logdir = null) /** * Nastaví cesty logovacích souborů. - * + * * @param string $baseLogDir */ - public function setupLogFiles($baseLogDir = null) + public function setupLogFiles($baseLogDir = '') { - - $baseLogDir = is_null($baseLogDir) ? ( is_null($this->logPrefix) && defined('LOG_DIRECTORY') ? constant('LOG_DIRECTORY') : null ) : $baseLogDir; + $baseLogDir = empty($baseLogDir) ? \Ease\Shared::cfg('LOG_DIRECTORY') : $baseLogDir; if (empty($baseLogDir)) { $this->logType = 'none'; - $this->logPrefix = null; - $this->logFileName = null; + $this->logPrefix = ''; + $this->logFileName = ''; } else { $this->logPrefix = \Ease\Functions::sysFilename($baseLogDir); if ($this->testDirectory($this->logPrefix)) { $this->logFileName = $this->logPrefix . $this->logFileName; } else { - $this->logPrefix = null; - $this->logFileName = null; + $this->logPrefix = ''; + $this->logFileName = ''; } } } @@ -132,19 +130,19 @@ public function addToLog($caller, $message, $type = 'notice') $message = htmlspecialchars_decode(strip_tags(stripslashes($message))); - $logLine = date(DATE_ATOM) . ' (' . (is_object($caller) ? get_class($caller) : $caller) . ') ' . str_replace( - ['notice', 'message', - 'debug', 'error', 'warning', 'success', 'info', 'mail',], - ['**', '##', '@@', '::'], $type - ) . ' ' . $message . "\n"; + $logLine = date(DATE_ATOM) . ' (' . Message::getCallerName($caller) . ') ' . str_replace( + ['notice', 'message', 'debug', 'error', 'warning', 'success', 'info', 'mail'], + ['**', '##', '@@', '::'], + $type + ) . ' ' . $message . "\n"; if (!empty($this->logPrefix)) { if ($this->logType == 'file' || $this->logType == 'both') { if (!empty($this->logFileName)) { - if (!$this->_logFileHandle) { - $this->_logFileHandle = fopen($this->logFileName, 'a+'); + if (!$this->logFileHandle) { + $this->logFileHandle = fopen($this->logFileName, 'a+'); } - if ($this->_logFileHandle !== null) { - $written += fwrite($this->_logFileHandle, $logLine); + if ($this->logFileHandle !== null) { + $written += fwrite($this->logFileHandle, $logLine); } } } @@ -163,9 +161,7 @@ public function addToLog($caller, $message, $type = 'notice') * * @return bool konečný výsledek testu */ - public static function testDirectory($directoryPath, $isDir = true, - $isReadable = true, $isWritable = true - ) + public static function testDirectory($directoryPath, $isDir = true, $isReadable = true, $isWritable = true) { if ($isDir && !is_dir($directoryPath)) { throw new \Exception($directoryPath . _(' is not an folder. Current directory:') . ' ' . getcwd()); @@ -184,9 +180,8 @@ public static function testDirectory($directoryPath, $isDir = true, */ public function __destruct() { - if ($this->_logFileHandle && is_resource($this->_logFileHandle)) { - fclose($this->_logFileHandle); + if ($this->logFileHandle && is_resource($this->logFileHandle)) { + fclose($this->logFileHandle); } } - } diff --git a/src/Ease/Logger/ToMemory.php b/src/Ease/Logger/ToMemory.php index 681598f..81bd6ca 100644 --- a/src/Ease/Logger/ToMemory.php +++ b/src/Ease/Logger/ToMemory.php @@ -1,7 +1,5 @@ messageID; - self::$statusMessages[$type][$this->messageID] = $message; - return true; + self::$statusMessages[$type][Message::getCallerName($caller) . $this->messageID] = $message; + return strlen($message); } /** @@ -118,5 +117,4 @@ public function getLogStyle($logType = 'notice') return ''; } } - } diff --git a/src/Ease/Logger/ToStd.php b/src/Ease/Logger/ToStd.php index 0fe127a..f0680e3 100644 --- a/src/Ease/Logger/ToStd.php +++ b/src/Ease/Logger/ToStd.php @@ -1,7 +1,5 @@ logStyles[$type])) { $type = 'notice'; } - $this->output($type, $logLine); - - return true; + return $this->output($type, $logLine); } /** @@ -119,14 +117,13 @@ public function addToLog($caller, $message, $type = 'message') * * @param string $type message type 'error' or anything else * @param string $logLine message to output - * + * * @return int bytes written */ public function output($type, $logLine) { $written = 0; - switch ($type) - { + switch ($type) { case 'error': $stderr = fopen('php://stderr', 'w'); $written += fwrite($stderr, $this->logName . ': ' . $logLine); @@ -152,5 +149,4 @@ public function finalizeMessage($messageRaw) { return trim($messageRaw) . PHP_EOL; } - } diff --git a/src/Ease/Logger/ToSyslog.php b/src/Ease/Logger/ToSyslog.php index 2422791..f39e3f2 100644 --- a/src/Ease/Logger/ToSyslog.php +++ b/src/Ease/Logger/ToSyslog.php @@ -1,7 +1,5 @@ - * @copyright 2009-2021 Vitex@hippy.cz (G) + * @copyright 2009-2023 Vitex@hippy.cz (G) */ class ToSyslog extends ToStd implements Loggingable { - /** * Předvolená metoda logování. * @@ -34,7 +33,7 @@ class ToSyslog extends ToStd implements Loggingable /** * Logovací třída. - * + * * @see https://www.php.net/manual/en/function.openlog.php * * @param string $logName syslog log source identifier @@ -42,12 +41,13 @@ class ToSyslog extends ToStd implements Loggingable public function __construct($logName = null) { parent::__construct($logName); - openlog(empty($this->logName) ? \Ease\Shared::appName() : $this->logName, intval(\Ease\Functions::cfg('LOG_FLAG')), intval(\Ease\Functions::cfg('LOG_FACILITY'))); + openlog(empty($this->logName) ? \Ease\Shared::appName() : + $this->logName, intval(\Ease\Shared::cfg('LOG_FLAG')), intval(\Ease\Shared::cfg('LOG_FACILITY'))); } /** * Obtain instance of Syslog loger - * + * * @return ToSyslog */ public static function singleton() @@ -63,10 +63,13 @@ public static function singleton() * * @param string $type message type 'error' or anything else * @param string $logLine message to output + * + * @return int written message length */ public function output($type, $logLine) { - return syslog($type == 'error' ? constant('LOG_ERR') : constant('LOG_INFO'), $this->finalizeMessage($logLine)); + return syslog($type == 'error' ? constant('LOG_ERR') : constant('LOG_INFO'), $this->finalizeMessage($logLine)) ? + strlen($logLine) : 0; } /** @@ -88,5 +91,4 @@ public function __destruct() { closelog(); } - } diff --git a/src/Ease/Mailer.php b/src/Ease/Mailer.php index 57564cc..c9a6580 100644 --- a/src/Ease/Mailer.php +++ b/src/Ease/Mailer.php @@ -1,16 +1,16 @@ - * @copyright 2009-2012 Vitex@hippy.cz (G) - * + * @copyright 2009-2023 Vitex@hippy.cz (G) + * * PHP 7 */ +declare(strict_types=1); + namespace Ease; /** @@ -19,8 +19,8 @@ * * @author Vitex */ -class Mailer extends Sand { - +class Mailer extends Sand +{ /** * Object for mail sending. * @@ -31,20 +31,20 @@ class Mailer extends Sand { /** * MIME Helper * - * @var \Mail_mime + * @var \Mail_mime */ public $mimer = null; /** * - * @var string + * @var string */ public $textBody = null; /** * Mail Headers * - * @var array + * @var array */ public $mailHeaders = []; @@ -64,13 +64,13 @@ class Mailer extends Sand { /** * - * @var array + * @var array */ public $mailBody = null; /** - * - * @var boolean + * + * @var boolean */ public $finalized = false; @@ -84,7 +84,7 @@ class Mailer extends Sand { /** * Email subject holder * - * @var string + * @var string */ public $emailSubject = null; @@ -123,11 +123,10 @@ class Mailer extends Sand { * @param string $mailSubject subject * @param mixed $emailContents body - any text mix and EaseObjects */ - public function __construct(string $emailAddress, string $mailSubject, - $emailContents = null - ) { - if (\Ease\Functions::cfg('EASE_SMTP')) { - $this->parameters = json_decode(\Ease\Functions::cfg('EASE_SMTP'), true); + public function __construct(string $emailAddress, string $mailSubject, $emailContents = null) + { + if (\Ease\Shared::cfg('EASE_SMTP')) { + $this->parameters = json_decode(\Ease\Shared::cfg('EASE_SMTP'), true); } $this->setMailHeaders( @@ -154,12 +153,13 @@ public function __construct(string $emailAddress, string $mailSubject, /** * Sets mail's text body - * + * * @param string $text - * + * * @return boolean|\Pear_Err */ - public function setMailBody($text) { + public function setMailBody($text) + { return $this->mimer->setTXTBody($text); } @@ -170,7 +170,8 @@ public function setMailBody($text) { * * @return string|null requested header value */ - public function getMailHeader($headername) { + public function getMailHeader($headername) + { return array_key_exists($headername, $this->mailHeaders) ? $this->mailHeaders[$headername] : null; } @@ -181,7 +182,8 @@ public function getMailHeader($headername) { * * @return bool true if the headers have been set */ - public function setMailHeaders(array $mailHeaders) { + public function setMailHeaders(array $mailHeaders) + { $this->mailHeaders = array_merge($this->mailHeaders, $mailHeaders); if (isset($this->mailHeaders['To'])) { $this->emailAddress = $this->mailHeaders['To']; @@ -205,17 +207,19 @@ public function setMailHeaders(array $mailHeaders) { * * @param string $filename path / file name to attach * @param string $mimeType MIME attachment type - * + * * @return boolean|\PEAR_Error */ - public function addFile(string $filename, $mimeType = 'text/plain') { + public function addFile(string $filename, $mimeType = 'text/plain') + { return $this->mimer->addAttachment($filename, $mimeType); } /** * Sends mail. */ - public function send() { + public function send() + { $this->setMailBody($this->textBody); $oMail = new \Mail(); if (count($this->parameters)) { @@ -224,26 +228,31 @@ public function send() { $this->mailer = $oMail->factory('mail'); } $this->sendResult = $this->mailer->send( - $this->emailAddress, - $this->mailHeadersDone, $this->mailBody + $this->emailAddress, + $this->mailHeadersDone, + $this->mailBody ); if ($this->notify === true) { $mailStripped = str_replace(['<', '>'], '', $this->emailAddress); if ($this->sendResult === true) { $this->addStatusMessage( - sprintf( - _('Message %s was sent to %s'), - $this->emailSubject, $mailStripped - ), 'success' + sprintf( + _('Message %s was sent to %s'), + $this->emailSubject, + $mailStripped + ), + 'success' ); } else { $this->addStatusMessage( - sprintf( - _('Message %s, for %s was not sent because of %s'), - $this->emailSubject, $mailStripped, - $this->sendResult->message - ), 'warning' + sprintf( + _('Message %s, for %s was not sent because of %s'), + $this->emailSubject, + $mailStripped, + $this->sendResult->message + ), + 'warning' ); } } @@ -256,8 +265,8 @@ public function send() { * * @param bool $notify required notification status */ - public function setUserNotification(bool $notify) { + public function setUserNotification(bool $notify) + { $this->notify = $notify; } - } diff --git a/src/Ease/Molecule.php b/src/Ease/Molecule.php index 521b533..e5d7048 100644 --- a/src/Ease/Molecule.php +++ b/src/Ease/Molecule.php @@ -1,16 +1,16 @@ - * @copyright 2009-2021 Vitex@hippy.cz (G) - * - * PHP 7 + * @copyright 2009-2023 Vitex@hippy.cz (G) + * + * PHP 8 */ +declare(strict_types=1); + namespace Ease; use Ease\Atom; @@ -23,7 +23,6 @@ */ class Molecule extends Atom { - /** * Object name * @@ -78,5 +77,4 @@ public function setupProperty($options, $name, $constant = '') } } } - } diff --git a/src/Ease/Person.php b/src/Ease/Person.php index 579a047..2bd98d8 100644 --- a/src/Ease/Person.php +++ b/src/Ease/Person.php @@ -5,7 +5,7 @@ * * @author Vitex * @copyright 2021 Vitex@hippy.cz (G) - * + * * PHP 7 */ @@ -17,7 +17,6 @@ */ interface Person { - /** * Anonym má level. * diff --git a/src/Ease/RecordKey.php b/src/Ease/RecordKey.php index 2d84b2a..c1a6117 100644 --- a/src/Ease/RecordKey.php +++ b/src/Ease/RecordKey.php @@ -1,17 +1,18 @@ * @copyright 2019 Vitex@hippy.cz (G) - * + * * @package EasePHP - * + * * PHP 7 */ +declare(strict_types=1); + namespace Ease; /** @@ -32,7 +33,7 @@ trait RecordKey * * @return array */ - public abstract function getData(); + abstract public function getData(); /** * Set data filed value @@ -42,7 +43,7 @@ public abstract function getData(); * * @return bool Success */ - public abstract function setDataValue(string $columnName, $value); + abstract public function setDataValue(string $columnName, $value); /** * Gives you value of KEY Column @@ -53,8 +54,9 @@ public abstract function setDataValue(string $columnName, $value); */ public function getMyKey($data = null) { - return is_null($data) ? $this->getDataValue($this->getKeyColumn()) : (array_key_exists($this->getKeyColumn(), - $data) ? $data[$this->getKeyColumn()] : null); + return is_null($data) ? $this->getDataValue($this->getKeyColumn()) : + (array_key_exists($this->getKeyColumn(), $data) ? + $data[$this->getKeyColumn()] : null); } /** @@ -66,8 +68,7 @@ public function getMyKey($data = null) */ public function setMyKey($myKeyValue) { - return empty($this->getKeyColumn()) ? null : $this->setDataValue($this->getKeyColumn(), - $myKeyValue); + return empty($this->getKeyColumn()) ? null : $this->setDataValue($this->getKeyColumn(), $myKeyValue); } /** @@ -89,4 +90,4 @@ public function setKeyColumn(string $keyColumn) { $this->keyColumn = $keyColumn; } -} \ No newline at end of file +} diff --git a/src/Ease/Sand.php b/src/Ease/Sand.php index a5a8412..3cab413 100644 --- a/src/Ease/Sand.php +++ b/src/Ease/Sand.php @@ -1,27 +1,26 @@ * @copyright 2009-2021 Vitex@hippy.cz (G) - * + * * PHP 7 */ +declare(strict_types=1); + namespace Ease; /** * Zakladni objekt urceny k rodicovstvi vsem pouzivanym objektum. * * @author Vitex - * @copyright 2009-2012 Vitex@hippy.cz (G) + * @copyright 2009-2023 Vitex@hippy.cz (G) */ class Sand extends Molecule { - use Logger\Logging; /** @@ -111,7 +110,8 @@ public function getDataCount() */ public function getDataValue($columnName) { - return !empty($columnName) && is_array($this->data) && array_key_exists($columnName, $this->data) ? $this->data[$columnName] : null; + return !empty($columnName) && is_array($this->data) && + array_key_exists($columnName, $this->data) ? $this->data[$columnName] : null; } /** @@ -164,5 +164,4 @@ public function takeData($data) return empty($data) ? null : count($data); } - } diff --git a/src/Ease/Shared.php b/src/Ease/Shared.php index 315ec03..6a42524 100644 --- a/src/Ease/Shared.php +++ b/src/Ease/Shared.php @@ -1,14 +1,15 @@ * @copyright 2009-2023 Vitex@hippy.cz (G) - * + * * PHP 7 * PHP 8 */ +declare(strict_types=1); + namespace Ease; use Ease\Functions; @@ -58,7 +59,7 @@ class Shared extends Atom /** * Load required Initial Configuration - * + * * @param array $configKeys * @param string $envFile */ @@ -68,7 +69,7 @@ public static function init($configKeys = [], $envFile = '.env') \Ease\Shared::singleton()->loadConfig($envFile, true); } $configured = true; - if(array_key_exists('DB_CONNECTION', $configKeys) && strstr(self::cfg('DB_CONNECTION'),'sqlite')){ + if (array_key_exists('DB_CONNECTION', $configKeys) && strstr(self::cfg('DB_CONNECTION'), 'sqlite') ){ unset($configKeys['DB_PASSWORD']); unset($configKeys['DB_USERNAME']); unset($configKeys['DB_HOST']); @@ -87,17 +88,18 @@ public static function init($configKeys = [], $envFile = '.env') /** * Get configuration from constant or environment - * + * * @param string $constant * @param mixed $cfg Default value - * + * * @return string|int|boolean|null */ - public static function cfg(/*string*/ $constant, $cfg = null) { + public static function cfg(/*string*/ $constant, $cfg = null) + { if (!empty($constant) && defined($constant)) { $cfg = constant($constant); } elseif (array_key_exists($constant, $_ENV)) { - $cfg = getenv($constant,true); + $cfg = getenv($constant, true); } else { $env = getenv($constant); if (!empty($env)) { @@ -106,14 +108,13 @@ public static function cfg(/*string*/ $constant, $cfg = null) { } return $cfg; } - - + /** * Application name or "Composer project Name" fallback - * + * * @return string */ - static public function appName() + public static function appName() { if (method_exists('Composer\InstalledVersions', 'getRootPackage')) { $package = \Composer\InstalledVersions::getRootPackage(); @@ -125,10 +126,10 @@ static public function appName() /** * Application version or "0.0.0" fallback - * + * * @return string */ - static public function appVersion() + public static function appVersion() { if (method_exists('Composer\InstalledVersions', 'getRootPackage')) { $package = \Composer\InstalledVersions::getRootPackage(); @@ -154,9 +155,9 @@ public function __construct() /** * File with stored messages - * + * * @param string $sessID - * + * * @return string */ public static function msgFile($sessID = 'EaseStatusMessages') @@ -166,9 +167,10 @@ public static function msgFile($sessID = 'EaseStatusMessages') } /** - * + * Load status Messages from session + * * @param string $sessID - * + * * @return array */ public static function loadStatusMessages($sessID = 'EaseStatusMessages') @@ -184,7 +186,7 @@ public static function loadStatusMessages($sessID = 'EaseStatusMessages') /** * Write remaining messages to temporary file. - * + * * @return int bytes saved */ public function saveStatusMessages($sessID = 'EaseStatusMessages') @@ -266,14 +268,16 @@ public static function &user(Person $user = null, string $candidat = 'User', str if (empty($user) && isset($_SESSION[$efprefix][self::$userSessionName])) { return $_SESSION[$efprefix][self::$userSessionName]; } - if (!is_null($userSessionName)) { + if (!empty($userSessionName)) { self::$userSessionName = $userSessionName; } if (is_object($user)) { $_SESSION[$efprefix][self::$userSessionName] = clone $user; } else { if (!empty($candidat)) { - $_SESSION[$efprefix][self::$userSessionName] = method_exists($candidat, 'singleton') ? $candidat::singleton() : new $candidat(); + $_SESSION[$efprefix][self::$userSessionName] = method_exists($candidat, 'singleton') ? + $candidat::singleton() : + new $candidat(); } } return $_SESSION[$efprefix][self::$userSessionName]; @@ -324,7 +328,7 @@ public function loadConfig($configFile, $defineConstants = false) if (array_key_exists('debug', $this->configuration)) { $this->debug = boolval($this->configuration['debug']); if ($this->debug === true) { - $this->logger()->addToLog($this,'Loaded configuration from ' . $configFile, 'debug'); + $this->logger()->addToLog($this, 'Loaded configuration from ' . $configFile, 'debug'); } } return $this->configuration; diff --git a/src/Ease/User.php b/src/Ease/User.php index 9d2db42..07d3ba8 100644 --- a/src/Ease/User.php +++ b/src/Ease/User.php @@ -1,16 +1,16 @@ - * @copyright 2009-2021 Vitex@hippy.cz (G) - * + * @copyright 2009-2023 Vitex@hippy.cz (G) + * * PHP 7 */ +declare(strict_types=1); + namespace Ease; /** @@ -20,9 +20,8 @@ */ class User extends Anonym { - /** - * @var User Singleton is stored here + * @var User|null Singleton is stored here */ public static $instance; @@ -50,7 +49,7 @@ class User extends Anonym /** * Sloupeček s loginem. * - * @var string + * @var string|null */ public $loginColumn = 'login'; @@ -67,8 +66,7 @@ class User extends Anonym * @var string */ public $plaintextField = 'password'; - - + /** * Sloupecek pro docasne zablokovani uctu. * @@ -147,7 +145,49 @@ public function getIcon() if ($email) { return self::getGravatar($email, 800, 'mm', 'g'); } else { - return 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhLS0gQ3JlYXRlZCB3aXRoIElua3NjYXBlIChodHRwOi8vd3d3Lmlua3NjYXBlLm9yZy8pIC0tPgo8c3ZnIGlkPSJzdmcyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjAwIiB3aWR0aD0iMjAwIiB2ZXJzaW9uPSIxLjAiPgogPHBhdGggaWQ9InBhdGgyMzgyIiBkPSJtMTY1LjMzIDExMy40NGExMDMuNjEgMTAzLjYxIDAgMSAxIC0yMDcuMjIgMCAxMDMuNjEgMTAzLjYxIDAgMSAxIDIwNy4yMiAweiIgdHJhbnNmb3JtPSJtYXRyaXgoLjkzNzM5IDAgMCAuOTM3MzkgNDIuMTQzIC02LjMzOTIpIiBzdHJva2Utd2lkdGg9IjAiIGZpbGw9IiNmZmYiLz4KIDxnIGlkPSJsYXllcjEiPgogIDxwYXRoIGlkP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ODctMy43Mi0zLjcxOC00Ljk2LTQuMzk0LTEwLjY' . + '5LTcuMzUzLTE3LjU2LTkuMDk0LTQuMTktMS4wNjItOC4yMy0xLjYtMTM' . + 'uMzUtMS43NS0wLjc4LTAuMDIzLTEuNTktMC4wMzYtMi4zNy0wLjAzMnptLT' . + 'EwLjkwOCAxMDMuNnYyMmgyMS45OTh2LTIyaC0yMS45OTh6Ii8+CiA8L2c+C' . + 'jwvc3ZnPgo='; } } @@ -166,26 +206,24 @@ public function tryToLogin($formData) if (empty($login)) { $this->addStatusMessage(_('missing login'), 'error'); - return; + return null; } if (empty($password)) { $this->addStatusMessage(_('missing password'), 'error'); - return; + return null; } if ($this->authentize($password)) { return true; } else { - $this->addStatusMessage(sprintf(_('user %s does not exist'), $login, - 'error')); - + $this->addStatusMessage(sprintf(_('user %s does not exist'), $login, 'error')); return false; } } /** - * Try to authentize user - * + * Try to authentize user + * * @return boolean */ public function authentize($plaintext) @@ -211,9 +249,9 @@ public function authentize($plaintext) /** * Try to validate Password - * + * * @param string $password plaintext - * + * * @return boolean */ public function validatePassword($password) @@ -232,8 +270,10 @@ public function isAccountEnabled() return true; } if ($this->getDataValue($this->disableColumn)) { - $this->addStatusMessage(_('Sign in denied by administrator'), - 'warning'); + $this->addStatusMessage( + _('Sign in denied by administrator'), + 'warning' + ); return false; } @@ -244,7 +284,7 @@ public function isAccountEnabled() /** * Akce provedené po úspěšném přihlášení * pokud tam jeste neexistuje zaznam, vytvori se novy. - * + * * @return boolean */ public function loginSuccess() @@ -252,8 +292,7 @@ public function loginSuccess() $this->userID = (int) $this->getMyKey(); $this->setUserLogin($this->getDataValue($this->loginColumn)); $this->logged = true; - $this->addStatusMessage(sprintf(_('Signed in as %s'), $this->userLogin), - 'success'); + $this->addStatusMessage(sprintf(_('Signed in as %s'), $this->userLogin), 'success'); $this->setObjectName(); return true; } @@ -291,8 +330,7 @@ public static function passwordValidation($plainPassword, $encryptedPassword) public static function encryptPassword($plainTextPassword) { $encryptedPassword = ''; - for ($i = 0; $i < 10; ++$i) - { + for ($i = 0; $i < 10; ++$i) { $encryptedPassword .= \Ease\Functions::randomNumber(); } $passwordSalt = substr(md5($encryptedPassword), 0, 2); @@ -341,8 +379,7 @@ public function getUserLogin() public function setUserLogin($login) { $this->userLogin = $login; - return isset($this->loginColumn) ? $this->setDataValue($this->loginColumn, - $login) : true; + return isset($this->loginColumn) ? $this->setDataValue($this->loginColumn, $login) : true; } /** @@ -442,9 +479,7 @@ public function getName() * * @source http://gravatar.com/site/implement/images/php/ */ - public static function getGravatar( - $email, $size = 80, $default = 'mm', $maxRating = 'g' - ) + public static function getGravatar($email, $size = 80, $default = 'mm', $maxRating = 'g') { $url = 'http://www.gravatar.com/avatar/'; $url .= md5(strtolower(trim($email))); @@ -464,7 +499,8 @@ public function setObjectName($objectName = null) { if (empty($objectName) && isset($_SERVER['REMOTE_ADDR'])) { - $name = parent::setObjectName(get_class($this) . ':' . $this->getUserName() . '@' . self::remoteToIdentity()); + $name = parent::setObjectName(get_class($this) . ':' . $this->getUserName() . '@' . + self::remoteToIdentity()); } else { $name = parent::setObjectName($objectName); } @@ -473,7 +509,7 @@ public function setObjectName($objectName = null) /** * Common instance of User class - * + * * @return \Ease\User */ public static function singleton($user = null) @@ -493,5 +529,4 @@ public function __sleep() { return ['logged', 'data']; } - }