From f56348f53ba5906609c8a9cccacbdb61520f7a5b Mon Sep 17 00:00:00 2001 From: nguyenanhung Date: Fri, 3 Feb 2023 12:52:28 +0700 Subject: [PATCH] Release version 3.0.8.9 --- composer.json | 4 + helpers/common_helper.php | 6 -- src/BoolType.php | 189 +------------------------------------- src/Common.php | 52 +++++------ src/Email.php | 26 +----- src/JWTHelper.php | 50 +--------- src/ProjectInterface.php | 2 +- src/SimpleCurl.php | 92 +++++++++++++++++-- src/SimpleVerifiedKey.php | 47 +--------- 9 files changed, 122 insertions(+), 346 deletions(-) diff --git a/composer.json b/composer.json index efabd19..82ecacb 100755 --- a/composer.json +++ b/composer.json @@ -17,9 +17,12 @@ "ext-mbstring": "*", "nguyenanhung/array-helper": "^2.0 || ^1.0", "nguyenanhung/base64-php-helper": "^2.0 || ^1.0", + "nguyenanhung/bool-type-helper": "^2.0 || ^1.0", "nguyenanhung/bbcode-helper": "^2.0 || ^1.0", "nguyenanhung/date-helper": "^2.0 || ^1.0", "nguyenanhung/escape-helper": "^2.0 || ^1.0", + "nguyenanhung/email-helper": "^2.0 || ^1.0", + "nguyenanhung/encryption-helper": "^2.0 || ^1.0", "nguyenanhung/filesystem-helper": "^2.0 || ^1.0", "nguyenanhung/filtered-helper": "^2.0 || ^1.0", "nguyenanhung/hashids-helper": "^2.0 || ^1.0", @@ -27,6 +30,7 @@ "nguyenanhung/http-download-helper": "^2.0 || ^1.0", "nguyenanhung/ip-helper": "^2.0 || ^1.0", "nguyenanhung/json-helper": "^2.0 || ^1.0", + "nguyenanhung/jwt-helper": "^2.0 || ^1.0", "nguyenanhung/math-helper": "^2.0 || ^1.0", "nguyenanhung/mobile-helper": "^2.0 || ^1.0", "nguyenanhung/nanoid-helper": "^2.0 || ^1.0", diff --git a/helpers/common_helper.php b/helpers/common_helper.php index cc6a6f0..5d36d75 100644 --- a/helpers/common_helper.php +++ b/helpers/common_helper.php @@ -30,12 +30,6 @@ function isEmpty($input = ''): bool return (new nguyenanhung\Classes\Helper\Common())->isEmpty($input); } } -if (!function_exists('isEmail')) { - function isEmail($mail = ''): bool - { - return \nguyenanhung\Classes\Helper\Email::validateEmail($mail); - } -} if (!function_exists('developer_fullname_copyright')) { function developer_name_copyright(): string { diff --git a/src/BoolType.php b/src/BoolType.php index 5235355..677b2f0 100755 --- a/src/BoolType.php +++ b/src/BoolType.php @@ -9,8 +9,7 @@ namespace nguyenanhung\Classes\Helper; -use InvalidArgumentException; -use BadMethodCallException; +use nguyenanhung\Libraries\BoolType\BoolType as BaseBoolType; if (!class_exists('nguyenanhung\Classes\Helper\BoolType')) { /** @@ -20,192 +19,8 @@ * @author 713uk13m * @copyright 713uk13m */ - class BoolType + class BoolType extends BaseBoolType { - /** - * Returns $bool value in the string $format - * - * I'll return a bool value as a true-false, yes-no, or on-off string. - * - * For example: - * - * BoolType::boolToString(true); // returns (string) 'true' - * BoolType::boolToString(true, 'yes-no'); // returns (string) 'true' - * BoolType::boolToString(false, 'on-off'); // returns (string) 'off' - * - * @since 0.1.0 - * - * @param bool $bool the boolean value to convert - * @param string $format the string format to convert to (possible values are - * 't[/-]f', true[/-]false', 'y[/-]n', 'yes[/-]no', 'o[/-o]', and 'on[/-]off') - * (case-insensitive) (optional; if omitted, defaults to 'true-false') - * - * @return string the string value - * - * @throws \BadMethodCallException if $bool is null - * @throws \InvalidArgumentException if $bool is not a (bool) value - * @throws \InvalidArgumentException if $format is not a string - * @throws \InvalidArgumentException if $format is not a valid format - */ - public static function boolToString($bool, $format = 'true-false'): string - { - $string = false; - // if $bool and format are not null - if ($bool !== null && $format !== null) { - // if $bool is actually a bool - if (is_bool($bool)) { - // if $format is a string - if (is_string($format)) { - // switch on the lower-case $format - switch (strtolower($format)) { - - case 'oo': - case 'o/o': - case 'o-o': - case 'onoff': - case 'on/off': - case 'on-off': - $string = $bool ? 'on' : 'off'; - break; - - case 'tf': - case 't/f': - case 't-f': - case 'truefalse': - case 'true/false': - case 'true-false': - $string = $bool ? 'true' : 'false'; - break; - - case 'yn': - case 'y/n': - case 'y-n': - case 'yesno': - case 'yes/no': - case 'yes-no': - $string = $bool ? 'yes' : 'no'; - break; - - default: - throw new InvalidArgumentException(__METHOD__ . "() expects parameter two, format, to be one of the following: " . "'t[/-]f', 'true[/-]false', 'y[/-]s', 'yes[/-]no', 'o[/-]o', or " . "'on[/-]off', '$format' given"); - } - } else { - throw new InvalidArgumentException(__METHOD__ . "() expects parameter two, format, to be a string"); - } - } else { - throw new InvalidArgumentException(__METHOD__ . "() expects parameter one, bool, to be a bool value given"); - } - } else { - throw new BadMethodCallException(__METHOD__ . "() expects one or two parameters, a bool value and a string format"); - } - - return $string; - } - - /** - * Returns the boolean value of $var - * - * PHP's native boolval() function is not available before PHP 5.5, and it does not - * support the strings 'yes', 'no', 'on', or 'off'. - * - * I follow the following rules: - * - * Strings - * The strings "yes", "true", "on", or "1" are considered true, and - * the strings "no", "false", "off", or "0" are considered false - * (case-insensitive). Any other non-empty string is true. - * - * Numbers - * The numbers 0 or 0.0 are considered false. Any other number is - * considered true. - * - * Array - * An empty array is considered false. Any other array (even an - * associative array with no values) is considered true. - * - * Object - * Any object is considered true. - * - * For example... - * - * BoolType::val(""); // returns (bool) false - * BoolType::val(true); // returns (bool) true - * BoolType::val(0); // returns (bool) false - * BoolType::val(0.0); // returns (bool) false - * BoolType::val('0'); // returns (bool) false - * BoolType::val('abc'); // returns (bool) true - * BoolType::val('true'); // returns (bool) true - * BoolType::val('on'); // returns (bool) true - * BoolType::val('yes'); // returns (bool) true - * BoolType::val('off'); // returns (bool) false - * BoolType::val([]); // returns (bool) false - * BoolType::val([1, 2]); // returns (bool) true - * BoolType::val(new StdClass()); // returns (bool) true - * - * @since 0.1.0 - * - * @param mixed $var the variable to test - * - * @return bool|null the bool value - * - * @see http://www.php.net/manual/en/function.boolval.php boolval() man page - */ - public static function val($var) - { - $value = null; - - // if $var is not empty - // any value considered empty by empty() is considered false - // for example, "0", array(), "", etc - // - if (!empty($var)) { - // if $var is not already a bool type - if (!is_bool($var)) { - // if $var is a string - if (is_string($var)) { - // switch on the string - // the strings '1', 'on', 'yes', and 'true' are considered true - // the strings '0', 'no', 'off', and 'false' are considered false - // any other non-empty string is true - // - switch (strtolower($var)) { - - case '1': - case 'on': - case 'yes': - case 'true': - $value = true; - break; - - case '0': - case 'no': - case 'off': - case 'false': - $value = false; - break; - - default: - $value = !empty($var); - } - } elseif (is_numeric($var)) { - // any non-zero integer or float is considered true - $value = ($var !== 0 && $var !== 0.0); - } elseif (is_object($var)) { - // any object is considered true - $value = true; - } elseif (is_array($var)) { - // any non-empty array is considered true - $value = !empty($var); - } - } else { - $value = $var; - } - } else { - $value = false; - } - - return $value; - } } } diff --git a/src/Common.php b/src/Common.php index 6a98af3..4c3ff51 100644 --- a/src/Common.php +++ b/src/Common.php @@ -27,37 +27,14 @@ class Common implements ProjectInterface use Version; - /** - * Function dump - * - * @param $str - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 30/12/2022 12:51 - */ public function dump($str) { - echo "
";
-            var_dump($str);
-            echo "
"; + return dump($str); } - /** - * Function dump_die - * - * @param $str - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 30/12/2022 13:17 - */ public function dump_die($str) { - echo "
";
-            var_dump($str);
-            echo "
"; - die; + dump_die($str); } /** @@ -220,9 +197,7 @@ public function directoryMap($source_dir, $directory_depth = 0, $hidden = false) */ public function newFolder($pathname = '', $mode = 0777): bool { - $file = new File(); - - return $file->createNewFolder($pathname, $mode); + return (new File())->createNewFolder($pathname, $mode); } /** @@ -237,9 +212,7 @@ public function newFolder($pathname = '', $mode = 0777): bool */ public function formatSizeUnits($bytes = 0): string { - $file = new File(); - - return $file->formatSizeUnits($bytes); + return (new File())->formatSizeUnits($bytes); } /** @@ -684,6 +657,23 @@ public function highlightPhrase($str = '', $phrase = '', $tag_open = '', $ return TextProcessor::highlightPhrase($str, $phrase, $tag_open, $tag_close); } + /** + * Keyword Highlighter + * + * Highlights a keyword within a text string + * + * @param string $str the text string + * @param string $phrase the phrase you'd like to highlight + * @param string $tag_open the opening tag to precede the phrase with + * @param string $tag_close the closing tag to end the phrase with + * + * @return string + */ + public function highlightKeyword($str = '', $phrase = '', $tag_open = '', $tag_close = ''): string + { + return TextProcessor::highlightKeyword($str, $phrase, $tag_open, $tag_close); + } + /** * Function convertStrToEn * diff --git a/src/Email.php b/src/Email.php index 5f33b03..10da09e 100644 --- a/src/Email.php +++ b/src/Email.php @@ -10,6 +10,8 @@ namespace nguyenanhung\Classes\Helper; +use nguyenanhung\Libraries\Email\Email as BaseEmail; + if (!class_exists('nguyenanhung\Classes\Helper\Email')) { /** * Class Email @@ -18,30 +20,8 @@ * @author 713uk13m * @copyright 713uk13m */ - class Email implements ProjectInterface + class Email extends BaseEmail implements ProjectInterface { use Version; - - /** - * Function validateEmail - * - * @param $email - * - * @return bool - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 07/28/2021 59:25 - */ - public static function validateEmail($email): bool - { - // Remove all illegal characters from email - $email = filter_var($email, FILTER_SANITIZE_EMAIL); - - if (filter_var($email, FILTER_VALIDATE_EMAIL)) { - return true; - } - - return false; - } } } diff --git a/src/JWTHelper.php b/src/JWTHelper.php index 9d64b85..7f5ea33 100644 --- a/src/JWTHelper.php +++ b/src/JWTHelper.php @@ -10,6 +10,8 @@ namespace nguyenanhung\Classes\Helper; +use nguyenanhung\Libraries\JWT\JWTHelper as BaseJWTHelper; + if (!class_exists('nguyenanhung\Classes\Helper\JWTHelper')) { /** * Class JWTHelper @@ -18,54 +20,8 @@ * @author 713uk13m * @copyright 713uk13m */ - class JWTHelper implements ProjectInterface + class JWTHelper extends BaseJWTHelper implements ProjectInterface { use Version; - - /** - * Function getAuthorizationHeader - * - * @return string|null - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 11/6/19 50:26 - */ - public static function getAuthorizationHeader() - { - $headers = null; - if (isset($_SERVER['Authorization'])) { - $headers = trim($_SERVER["Authorization"]); - } elseif (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI - $headers = trim($_SERVER["HTTP_AUTHORIZATION"]); - } elseif (function_exists('apache_request_headers')) { - $requestHeaders = apache_request_headers(); - // Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization) - $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders)); - if (isset($requestHeaders['Authorization'])) { - $headers = trim($requestHeaders['Authorization']); - } - } - - return $headers; - } - - /** - * Function getBearerToken - * - * @return mixed|null - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 11/6/19 50:30 - */ - public static function getBearerToken() - { - $headers = static::getAuthorizationHeader(); - // HEADER: Get the access token from the header - if (!empty($headers) && preg_match('/Bearer\s(\S+)/', $headers, $matches)) { - return $matches[1]; - } - - return null; - } } } diff --git a/src/ProjectInterface.php b/src/ProjectInterface.php index 71d95a8..a41c076 100644 --- a/src/ProjectInterface.php +++ b/src/ProjectInterface.php @@ -19,7 +19,7 @@ */ interface ProjectInterface { - const VERSION = '3.0.8.0'; + const VERSION = '3.0.8.9'; const LAST_MODIFIED = '2023-02-03'; const AUTHOR_NAME = 'Hung Nguyen'; const AUTHOR_EMAIL = 'dev@nguyenanhung.com'; diff --git a/src/SimpleCurl.php b/src/SimpleCurl.php index fda9fb3..777338c 100644 --- a/src/SimpleCurl.php +++ b/src/SimpleCurl.php @@ -15,18 +15,24 @@ class SimpleCurl implements ProjectInterface { use Version; - protected $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'; + protected $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'; protected $url; protected $data; protected $followLocation; protected $timeout; protected $maxRedirects; protected $cookieFileLocation; + protected $disableSslVerifyHost; + protected $disableSslVerifyPeer; + protected $sslVerifyHost; + protected $sslVerifyPeer; + protected $sslCaInfoFile; + protected $sslVersion; protected $xml; protected $json; protected $post; protected $postFields; - protected $referer = ""; + protected $referer = ""; protected $session; protected $webpage; protected $headers; @@ -34,13 +40,13 @@ class SimpleCurl implements ProjectInterface protected $includeHeader; protected $noBody; protected $status; - protected $isError = false; + protected $isError = false; protected $error; protected $binaryTransfer; protected $userOptions; protected $authentication = 0; - protected $authUsername = ''; - protected $authPassword = ''; + protected $authUsername = ''; + protected $authPassword = ''; /** * SimpleCurl constructor. @@ -147,7 +153,7 @@ public function setPost($postFields) public function setJson($postFields) { - if (is_array($postFields)) { + if (is_array($postFields) || is_object($postFields)) { $postFields = json_encode($postFields); } $this->json = true; @@ -182,11 +188,51 @@ public function setUserAgent($userAgent) return $this; } + /** + * @see https://curl.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html + */ + public function disabledSslVerifyHost() + { + $this->disableSslVerifyHost = true; + + return $this; + } + + /** + * @see https://curl.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html + * @see https://www.saotn.org/dont-turn-off-curlopt_ssl_verifypeer-fix-php-configuration/#gsc.tab=0 + */ + public function disabledSslVerifyPeer() + { + $this->disableSslVerifyPeer = true; + + return $this; + } + + public function enabledSslVerifyPeer($certificateFile) + { + $this->sslVerifyPeer = true; + $this->sslCaInfoFile = $certificateFile; + + return $this; + } + + /** + * @see https://www.php.net/manual/en/function.curl-setopt.php + */ + public function setSslVersion($sslVersion) + { + $this->sslVersion = $sslVersion; + + return $this; + } + public function createCurl($url = null) { if ($url !== null) { $this->url = $url; } + $s = curl_init(); curl_setopt($s, CURLOPT_URL, $this->url); curl_setopt($s, CURLOPT_HTTPHEADER, $this->headers); @@ -194,39 +240,69 @@ public function createCurl($url = null) curl_setopt($s, CURLOPT_MAXREDIRS, $this->maxRedirects); curl_setopt($s, CURLOPT_RETURNTRANSFER, true); curl_setopt($s, CURLOPT_FOLLOWLOCATION, $this->followLocation); + if (!empty($this->cookieFileLocation) && file_exists($this->cookieFileLocation)) { curl_setopt($s, CURLOPT_COOKIEJAR, $this->cookieFileLocation); curl_setopt($s, CURLOPT_COOKIEFILE, $this->cookieFileLocation); } - //curl_setopt($s, CURLOPT_SSL_VERIFYHOST, 0); - //curl_setopt($s, CURLOPT_SSL_VERIFYPEER, 0); + + if (!empty($this->sslVersion)) { + curl_setopt($s, CURLOPT_SSLVERSION, $this->sslVersion); + } + + if ($this->disableSslVerifyHost === true) { + curl_setopt($s, CURLOPT_SSL_VERIFYHOST, 0); + } + + if ($this->disableSslVerifyPeer === true) { + curl_setopt($s, CURLOPT_SSL_VERIFYPEER, 0); + } + + if ($this->sslVerifyHost === true) { + curl_setopt($s, CURLOPT_SSL_VERIFYHOST, 2); + } + + if ($this->sslVerifyPeer === true && file_exists($this->sslCaInfoFile)) { + curl_setopt($s, CURLOPT_SSL_VERIFYPEER, true); + curl_setopt($s, CURLOPT_CAINFO, $this->sslCaInfoFile); + } + curl_setopt($s, CURLINFO_HEADER_OUT, $this->headerOut); curl_setopt($s, CURLOPT_FILETIME, 1); + if ($this->authentication === 1) { curl_setopt($s, CURLOPT_USERPWD, $this->authUsername . ':' . $this->authPassword); } + if ($this->post || $this->json || $this->xml) { curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $this->postFields); } + if ($this->includeHeader) { curl_setopt($s, CURLOPT_HEADER, true); } + if ($this->noBody) { curl_setopt($s, CURLOPT_NOBODY, true); } + if ($this->binaryTransfer) { curl_setopt($s, CURLOPT_BINARYTRANSFER, true); } + curl_setopt($s, CURLOPT_USERAGENT, $this->userAgent); curl_setopt($s, CURLOPT_REFERER, $this->referer); curl_setopt_array($s, $this->userOptions); + $this->webpage = curl_exec($s); $this->status = curl_getinfo($s); $this->error = curl_error($s); + if ($this->error) { $this->isError = true; } + $this->session = $s; return $this; diff --git a/src/SimpleVerifiedKey.php b/src/SimpleVerifiedKey.php index c9b33a6..bc613ed 100644 --- a/src/SimpleVerifiedKey.php +++ b/src/SimpleVerifiedKey.php @@ -10,50 +10,11 @@ namespace nguyenanhung\Classes\Helper; -if (!class_exists('nguyenanhung\Classes\Helper\SimpleRestfulRequest')) { - class SimpleVerifiedKey implements ProjectInterface +use nguyenanhung\Libraries\Encryption\SimpleVerifiedKey as BaseSimpleVerifiedKey; + +if (!class_exists('nguyenanhung\Classes\Helper\SimpleVerifiedKey')) { + class SimpleVerifiedKey extends BaseSimpleVerifiedKey implements ProjectInterface { use Version; - - public $remotePublicKey = <<remotePrivateKey; - } - - public function getRemotePublicKey() - { - return $this->remotePublicKey; - } - - public function getClientPrivateKey() - { - return $this->clientPrivateKey; - } - - public function getClientPublicKey() - { - return $this->clientPublicKey; - } } }