From 32cc51de96a0d69bc9fc50ca781cc37fd50c39ec Mon Sep 17 00:00:00 2001 From: HungNA - Technical Manager Date: Tue, 20 Aug 2024 12:02:50 +0700 Subject: [PATCH] Add checkFunctionsRequirement --- helpers/helpers.php | 115 +++---- src/CheckSystem.php | 712 +++++++++++++++++++++++--------------------- src/FirewallIP.php | 480 ++++++++++++++--------------- src/Logging.php | 84 +++--- src/Output.php | 34 +-- 5 files changed, 736 insertions(+), 689 deletions(-) diff --git a/helpers/helpers.php b/helpers/helpers.php index 15911d1..724de3b 100644 --- a/helpers/helpers.php +++ b/helpers/helpers.php @@ -1,4 +1,5 @@ - * @copyright: 713uk13m - * @time : 09/24/2021 26:08 - */ - function register_error_handler($errno, $errstr, $errfile, $errline) - { - if (($errno & error_reporting()) > 0) { - throw new ErrorException($errstr, 500, $errno, $errfile, $errline); - } + /** + * Function register_error_handler + * + * @param $errno + * @param $errstr + * @param $errfile + * @param $errline + * + * @return bool + * @throws ErrorException + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/24/2021 26:08 + */ + function register_error_handler($errno, $errstr, $errfile, $errline) + { + if (($errno & error_reporting()) > 0) { + throw new ErrorException($errstr, 500, $errno, $errfile, $errline); + } - return false; - } + return false; + } } if (!function_exists('php_basic_firewall')) { - /** - * Function php_basic_firewall - * - * @param bool $defaultState - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 00:10 - */ - function php_basic_firewall(bool $defaultState = false) - { - $firewall = new nguyenanhung\PhpBasicFirewall\FirewallIP(); - $firewall->checkUserConnect($defaultState); - if (true !== $firewall->isAccess()) { - $firewall->accessDeniedResponse(); - } - } + /** + * Function php_basic_firewall + * + * @param bool $defaultState + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 00:10 + */ + function php_basic_firewall(bool $defaultState = false) + { + $firewall = new nguyenanhung\PhpBasicFirewall\FirewallIP(); + $firewall->checkUserConnect($defaultState); + if (true !== $firewall->isAccess()) { + $firewall->accessDeniedResponse(); + } + } } if (!function_exists('php_basic_firewall_save_log')) { - /** - * Function php_basic_firewall_save_log - * - * @param string $logFile - * @param bool $defaultState - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 17:22 - */ - function php_basic_firewall_save_log(string $logFile = '', bool $defaultState = false) - { - $firewall = new nguyenanhung\PhpBasicFirewall\FirewallIP(); - $firewall->setLogDestination($logFile)->checkUserConnect($defaultState); - if (true !== $firewall->isAccess()) { - $firewall->writeErrorLog($firewall->errorLogMessage()); - $firewall->accessDeniedResponse(); - } - } + /** + * Function php_basic_firewall_save_log + * + * @param string $logFile + * @param bool $defaultState + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 17:22 + */ + function php_basic_firewall_save_log(string $logFile = '', bool $defaultState = false) + { + $firewall = new nguyenanhung\PhpBasicFirewall\FirewallIP(); + $firewall->setLogDestination($logFile)->checkUserConnect($defaultState); + if (true !== $firewall->isAccess()) { + $firewall->writeErrorLog($firewall->errorLogMessage()); + $firewall->accessDeniedResponse(); + } + } } diff --git a/src/CheckSystem.php b/src/CheckSystem.php index d3ff4d8..ece060e 100644 --- a/src/CheckSystem.php +++ b/src/CheckSystem.php @@ -14,362 +14,408 @@ */ class CheckSystem { - protected $phpMinVersion = "5.4"; + protected $phpMinVersion = "5.4"; - /** - * Is CLI? - Test to see if a request was made from the command line. - * - * @return bool - */ - public static function isCLI(): bool - { - return (PHP_SAPI === 'cli' or defined('STDIN')); - } + /** + * Is CLI? - Test to see if a request was made from the command line. + * + * @return bool + */ + public static function isCLI(): bool + { + return (PHP_SAPI === 'cli' or defined('STDIN')); + } - /** - * Function getCurrentPhpVersion - * - * @return string - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 02:29 - */ - public function getCurrentPhpVersion(): string - { - return PHP_VERSION; - } + /** + * Function getCurrentPhpVersion + * + * @return string + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 02:29 + */ + public function getCurrentPhpVersion(): string + { + return PHP_VERSION; + } - /** - * Function getPhpMinVersion - * - * @return string - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 01:39 - */ - public function getPhpMinVersion(): string - { - return $this->phpMinVersion; - } + /** + * Function getPhpMinVersion + * + * @return string + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 01:39 + */ + public function getPhpMinVersion(): string + { + return $this->phpMinVersion; + } - /** - * Function setPhpMinVersion - * - * @param string $phpMinVersion - * - * @return $this - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 01:43 - */ - public function setPhpMinVersion(string $phpMinVersion): CheckSystem - { - $this->phpMinVersion = $phpMinVersion; + /** + * Function setPhpMinVersion + * + * @param string $phpMinVersion + * + * @return $this + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 01:43 + */ + public function setPhpMinVersion(string $phpMinVersion): CheckSystem + { + $this->phpMinVersion = $phpMinVersion; - return $this; - } + return $this; + } - /** - * Function checkPhpVersion - * - * @return array - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 01:55 - */ - public function checkPhpVersion(): array - { - $minVersion = $this->phpMinVersion; - $operator = '>='; + /** + * Function checkPhpVersion + * + * @return array + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 01:55 + */ + public function checkPhpVersion(): array + { + $minVersion = $this->phpMinVersion; + $operator = '>='; - $message = 'Current PHP Version: ' . PHP_VERSION . ' - Suggest PHP Version ' . $operator . ' ' . $minVersion; + $message = 'Current PHP Version: ' . PHP_VERSION . ' - Suggest PHP Version ' . $operator . ' ' . $minVersion; - if (version_compare(PHP_VERSION, $minVersion, $operator)) { - $code = true; - $status = 'OK'; - } else { - $code = false; - $status = 'NOK'; - } + if (version_compare(PHP_VERSION, $minVersion, $operator)) { + $code = true; + $status = 'OK'; + } else { + $code = false; + $status = 'NOK'; + } - return array( - 'code' => $code, - 'message' => $message, - 'status' => $status, - ); - } + return array( + 'code' => $code, + 'message' => $message, + 'status' => $status, + ); + } - /** - * Function phpVersion - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 02:40 - */ - public function phpVersion() - { - $result = $this->checkPhpVersion(); - Output::writeLn($result['message'] . ' -> ' . $result['status']); - } + /** + * Function phpVersion + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 02:40 + */ + public function phpVersion() + { + $result = $this->checkPhpVersion(); + Output::writeLn($result['message'] . ' -> ' . $result['status']); + } - /** - * Function connectUsePhpTelnet - * - * @param string $hostname - * @param string|int $port - * - * @return array - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 02:43 - */ - public function connectUsePhpTelnet(string $hostname = '', $port = ''): array - { - $message = 'Connection to server ' . $hostname . ':' . $port; - try { - $socket = fsockopen($hostname, $port); - if ($socket) { - $code = true; - } else { - $code = false; - } - $result = array( - 'code' => $code, - 'message' => $message, - 'status' => $code === true ? 'OK' : 'NOK' - ); - } catch (Exception $exception) { - $result = array( - 'code' => false, - 'message' => $message, - 'status' => 'NOK', - 'errorMessage' => $exception->getMessage() - ); - } + /** + * Function connectUsePhpTelnet + * + * @param string $hostname + * @param string|int $port + * + * @return array + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 02:43 + */ + public function connectUsePhpTelnet(string $hostname = '', $port = ''): array + { + $message = 'Connection to server ' . $hostname . ':' . $port; + try { + $socket = fsockopen($hostname, $port); + if ($socket) { + $code = true; + } else { + $code = false; + } + $result = array( + 'code' => $code, + 'message' => $message, + 'status' => $code === true ? 'OK' : 'NOK' + ); + } catch (Exception $exception) { + $result = array( + 'code' => false, + 'message' => $message, + 'status' => 'NOK', + 'errorMessage' => $exception->getMessage() + ); + } - return $result; - } + return $result; + } - /** - * Function phpTelnet - * - * @param string $hostname - * @param string|int $port - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 02:47 - */ - public function phpTelnet(string $hostname = '', $port = '') - { - $result = $this->connectUsePhpTelnet($hostname, $port); - Output::writeLn($result['message'] . ' -> ' . $result['status']); - } + /** + * Function phpTelnet + * + * @param string $hostname + * @param string|int $port + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 02:47 + */ + public function phpTelnet(string $hostname = '', $port = '') + { + $result = $this->connectUsePhpTelnet($hostname, $port); + Output::writeLn($result['message'] . ' -> ' . $result['status']); + } - /** - * Function checkExtensionRequirement - * - * @param string $extension - * - * @return array - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 02:50 - */ - public function checkExtensionRequirement(string $extension = ''): array - { - $message = 'Requirement Extension: ' . $extension; - $code = extension_loaded($extension); + /** + * Function checkExtensionRequirement + * + * @param string $extension + * + * @return array + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 02:50 + */ + public function checkExtensionRequirement(string $extension = ''): array + { + $message = 'Requirement Extension: ' . $extension; + $code = extension_loaded($extension); - return array( - 'code' => $code, - 'message' => $message, - 'status' => $code === true ? 'OK' : 'NOK' - ); + return array( + 'code' => $code, + 'message' => $message, + 'status' => $code === true ? 'OK' : 'NOK' + ); + } - } + /** + * Function checkExtension + * + * @param string $extension + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 02:54 + */ + public function checkExtension(string $extension = '') + { + $result = $this->checkExtensionRequirement($extension); + Output::writeLn($result['message'] . ' -> ' . $result['status']); + } - /** - * Function checkExtension - * - * @param string $extension - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 02:54 - */ - public function checkExtension(string $extension = '') - { - $result = $this->checkExtensionRequirement($extension); - Output::writeLn($result['message'] . ' -> ' . $result['status']); - } + /** + * Function checkFunctionsRequirement + * + * @param string $function_name + * + * @return array + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 02:50 + */ + public function checkFunctionsRequirement(string $function_name = ''): array + { + $message = 'Requirement Extension: ' . $function_name; + $code = function_exists($function_name); - /** - * Function checkFilePermission - * - * @param string $filename - * @param string $mode - * - * @return array - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 02:59 - */ - public function checkFilePermission(string $filename = '', string $mode = 'read'): array - { - $message = 'File ' . $filename; - if (!file_exists($filename)) { - $code = false; - $status = 'NOK'; - $checkStatus = 'File ' . $filename . ' not exists'; - } else { - $mode = function_exists('mb_strtolower') ? mb_strtolower($mode) : strtolower($mode); - switch ($mode) { - case "read": - $code = is_readable($filename); - $status = $code === true ? 'OK' : 'NOK'; - $checkStatus = $code === true ? 'Read OK' : 'Read NOK'; - break; - case "write": - $code = is_writable($filename); - $status = $code === true ? 'OK' : 'NOK'; - $checkStatus = $code === true ? 'Write OK' : 'Write NOK'; - break; - case "executable": - $code = is_executable($filename); - $status = $code === true ? 'OK' : 'NOK'; - $checkStatus = $code === true ? 'Executable OK' : 'Executable NOK'; - break; - default: - $code = false; - $status = 'NOK'; - $checkStatus = 'NOK'; - } - } + return array( + 'code' => $code, + 'message' => $message, + 'status' => $code === true ? 'OK' : 'NOK' + ); + } - return array( - 'code' => $code, - 'message' => $message, - 'status' => $status, - 'checkStatus' => $checkStatus, - ); - } + /** + * Function checkFunctions + * + * @param string $function_name + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 02:54 + */ + public function checkFunctions(string $function_name = '') + { + $result = $this->checkFunctionsRequirement($function_name); + Output::writeLn($result['message'] . ' -> ' . $result['status']); + } - /** - * Function checkWriteFile - * - * @param string $filename - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 03:02 - */ - public function checkWriteFile(string $filename = '') - { - $result = $this->checkFilePermission($filename, 'write'); - Output::writeLn($result['message'] . ' -> ' . $result['status']); - } + /** + * Function checkFilePermission + * + * @param string $filename + * @param string $mode + * + * @return array + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 02:59 + */ + public function checkFilePermission(string $filename = '', string $mode = 'read'): array + { + $message = 'File ' . $filename; + if (!file_exists($filename)) { + $code = false; + $status = 'NOK'; + $checkStatus = 'File ' . $filename . ' not exists'; + } else { + $mode = function_exists('mb_strtolower') ? mb_strtolower($mode) : strtolower($mode); + switch ($mode) { + case "read": + $code = is_readable($filename); + $status = $code === true ? 'OK' : 'NOK'; + $checkStatus = $code === true ? 'Read OK' : 'Read NOK'; + break; + case "write": + $code = is_writable($filename); + $status = $code === true ? 'OK' : 'NOK'; + $checkStatus = $code === true ? 'Write OK' : 'Write NOK'; + break; + case "executable": + $code = is_executable($filename); + $status = $code === true ? 'OK' : 'NOK'; + $checkStatus = $code === true ? 'Executable OK' : 'Executable NOK'; + break; + default: + $code = false; + $status = 'NOK'; + $checkStatus = 'NOK'; + } + } - /** - * Function checkReadFile - * - * @param string $filename - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 03:06 - */ - public function checkReadFile(string $filename = '') - { - $result = $this->checkFilePermission($filename); - Output::writeLn($result['message'] . ' -> ' . $result['status']); - } + return array( + 'code' => $code, + 'message' => $message, + 'status' => $status, + 'checkStatus' => $checkStatus, + ); + } - /** - * Function checkExecutableFile - * - * @param string $filename - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 03:09 - */ - public function checkExecutableFile(string $filename = '') - { - $result = $this->checkFilePermission($filename, 'executable'); - Output::writeLn($result['message'] . ' -> ' . $result['status']); - } + /** + * Function checkWriteFile + * + * @param string $filename + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 03:02 + */ + public function checkWriteFile(string $filename = '') + { + $result = $this->checkFilePermission($filename, 'write'); + Output::writeLn($result['message'] . ' -> ' . $result['status']); + } - /** - * Function checkConnectDatabaseWithPDO - * - * @param string $host - * @param string|int $port - * @param string $database - * @param string $username - * @param string $password - * - * @return array - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 03:12 - */ - public function checkConnectDatabaseWithPDO(string $host = '', $port = '', string $database = '', string $username = '', string $password = ''): array - { - $dsnString = "mysql:host=$host;port=$port;dbname=$database"; - try { - $conn = new PDO($dsnString, $username, $password); - // set the PDO error mode to exception - $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $result = array( - 'code' => true, - 'status' => 'OK', - 'message' => "Connected successfully to Database : " . $dsnString . " with username: " . $username . " and your input password", - 'data' => array( - 'dsnString' => $dsnString, - 'username' => $username, - 'status' => 'Connected successfully', - 'message' => "{{PDO_CONNECT_STATUS}} to Database : {{PDO_DSN_STRING}} with username: {{PDO_USERNAME}} and your input password" - ) - ); - $conn = null; - } catch (PDOException $e) { - $result = array( - 'code' => false, - 'status' => 'NOK', - 'message' => "Connection failed: " . $e->getMessage(), - 'error' => $e->getTraceAsString(), - 'data' => array( - 'dsnString' => $dsnString, - 'username' => $username, - 'status' => 'Connected failed', - 'message' => "{{PDO_CONNECT_STATUS}} to Database : {{PDO_DSN_STRING}} with username: {{PDO_USERNAME}} and your input password" - ) - ); - } + /** + * Function checkReadFile + * + * @param string $filename + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 03:06 + */ + public function checkReadFile(string $filename = '') + { + $result = $this->checkFilePermission($filename); + Output::writeLn($result['message'] . ' -> ' . $result['status']); + } - return $result; - } + /** + * Function checkExecutableFile + * + * @param string $filename + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 03:09 + */ + public function checkExecutableFile(string $filename = '') + { + $result = $this->checkFilePermission($filename, 'executable'); + Output::writeLn($result['message'] . ' -> ' . $result['status']); + } - /** - * Function checkConnectDatabase - * - * @param string $host - * @param string|int $port - * @param string $database - * @param string $username - * @param string $password - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 03:16 - */ - public function checkConnectDatabase(string $host = '', $port = '', string $database = '', string $username = '', string $password = '') - { - $result = $this->checkConnectDatabaseWithPDO($host, $port, $database, $username, $password); - Output::writeLn($result['message']); - if (isset($result['error'])) { - Output::writeLn($result['error']); - } - } + /** + * Function checkConnectDatabaseWithPDO + * + * @param string $host + * @param string|int $port + * @param string $database + * @param string $username + * @param string $password + * + * @return array + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 03:12 + */ + public function checkConnectDatabaseWithPDO( + string $host = '', + $port = '', + string $database = '', + string $username = '', + string $password = '' + ): array { + $dsnString = "mysql:host=$host;port=$port;dbname=$database"; + try { + $conn = new PDO($dsnString, $username, $password); + // set the PDO error mode to exception + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $result = array( + 'code' => true, + 'status' => 'OK', + 'message' => "Connected successfully to Database : " . $dsnString . " with username: " . $username . " and your input password", + 'data' => array( + 'dsnString' => $dsnString, + 'username' => $username, + 'status' => 'Connected successfully', + 'message' => "{{PDO_CONNECT_STATUS}} to Database : {{PDO_DSN_STRING}} with username: {{PDO_USERNAME}} and your input password" + ) + ); + $conn = null; + } catch (PDOException $e) { + $result = array( + 'code' => false, + 'status' => 'NOK', + 'message' => "Connection failed: " . $e->getMessage(), + 'error' => $e->getTraceAsString(), + 'data' => array( + 'dsnString' => $dsnString, + 'username' => $username, + 'status' => 'Connected failed', + 'message' => "{{PDO_CONNECT_STATUS}} to Database : {{PDO_DSN_STRING}} with username: {{PDO_USERNAME}} and your input password" + ) + ); + } + + return $result; + } + + /** + * Function checkConnectDatabase + * + * @param string $host + * @param string|int $port + * @param string $database + * @param string $username + * @param string $password + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 03:16 + */ + public function checkConnectDatabase( + string $host = '', + $port = '', + string $database = '', + string $username = '', + string $password = '' + ) { + $result = $this->checkConnectDatabaseWithPDO($host, $port, $database, $username, $password); + Output::writeLn($result['message']); + if (isset($result['error'])) { + Output::writeLn($result['error']); + } + } } diff --git a/src/FirewallIP.php b/src/FirewallIP.php index fb75f8a..573a036 100644 --- a/src/FirewallIP.php +++ b/src/FirewallIP.php @@ -19,272 +19,272 @@ */ class FirewallIP { - /** @var string $logDestination Log File */ - protected $logDestination; + /** @var string $logDestination Log File */ + protected $logDestination; - /** - * @var array Cấu hình các IP được phép truy cập vào hệ thống - */ - protected $ipWhiteList = array('127.0.0.1'); + /** + * @var array Cấu hình các IP được phép truy cập vào hệ thống + */ + protected $ipWhiteList = array('127.0.0.1'); - /** - * @var array Cấu hình các IP không được phép truy cập vào hệ thống - */ - protected $ipBlacklist = array(); + /** + * @var array Cấu hình các IP không được phép truy cập vào hệ thống + */ + protected $ipBlacklist = array(); - /** @var bool Access Roles */ - protected $access = false; + /** @var bool Access Roles */ + protected $access = false; - /** @var bool Bypass if Use CLI */ - protected $cliBypass = false; + /** @var bool Bypass if Use CLI */ + protected $cliBypass = false; - /** - * Function setCliBypass - * - * @param false $cliBypass - * - * @return $this - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 09:28 - */ - public function setCliBypass(bool $cliBypass = false): FirewallIP - { - $this->cliBypass = $cliBypass; + /** + * Function setCliBypass + * + * @param false $cliBypass + * + * @return $this + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 09:28 + */ + public function setCliBypass(bool $cliBypass = false): FirewallIP + { + $this->cliBypass = $cliBypass; - return $this; - } + return $this; + } - /** - * Function isCliBypass - * - * @return bool - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 13:47 - */ - public function isCliBypass(): bool - { - return $this->cliBypass; - } + /** + * Function isCliBypass + * + * @return bool + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 13:47 + */ + public function isCliBypass(): bool + { + return $this->cliBypass; + } - /** - * Function isCLI - * - * @return bool - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 14:09 - */ - public function isCLI(): bool - { - return CheckSystem::isCLI(); - } + /** + * Function isCLI + * + * @return bool + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 14:09 + */ + public function isCLI(): bool + { + return CheckSystem::isCLI(); + } - /** - * Function setLogDestination - * - * @param string $logDestination - * - * @return $this - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/01/2021 50:21 - */ - public function setLogDestination(string $logDestination = ''): FirewallIP - { - $this->logDestination = $logDestination; + /** + * Function setLogDestination + * + * @param string $logDestination + * + * @return $this + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/01/2021 50:21 + */ + public function setLogDestination(string $logDestination = ''): FirewallIP + { + $this->logDestination = $logDestination; - return $this; - } + return $this; + } - /** - * Function setIpWhiteList - * - * @param array $ipWhiteList - * - * @return $this - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 29:42 - */ - public function setIpWhiteList(array $ipWhiteList = array()): FirewallIP - { - $this->ipWhiteList = $ipWhiteList; + /** + * Function setIpWhiteList + * + * @param array $ipWhiteList + * + * @return $this + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 29:42 + */ + public function setIpWhiteList(array $ipWhiteList = array()): FirewallIP + { + $this->ipWhiteList = $ipWhiteList; - return $this; - } + return $this; + } - /** - * Function setIpBlackList - * - * @param array $ipBlacklist - * - * @return $this - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 29:46 - */ - public function setIpBlackList(array $ipBlacklist = array()): FirewallIP - { - $this->ipBlacklist = $ipBlacklist; + /** + * Function setIpBlackList + * + * @param array $ipBlacklist + * + * @return $this + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 29:46 + */ + public function setIpBlackList(array $ipBlacklist = array()): FirewallIP + { + $this->ipBlacklist = $ipBlacklist; - return $this; - } + return $this; + } - /** - * Function getIPAddress - * - * @param bool $convertToInteger - * - * @return bool|int|string - * @author: 713uk13m - * @time : 2019-01-10 16:24 - * - */ - public function getIPAddress(bool $convertToInteger = false) - { - $IPKeys = array( - 0 => 'HTTP_CF_CONNECTING_IP', - 1 => 'HTTP_X_FORWARDED_FOR', - 2 => 'HTTP_X_FORWARDED', - 3 => 'HTTP_X_IPADDRESS', - 4 => 'HTTP_X_CLUSTER_CLIENT_IP', - 5 => 'HTTP_FORWARDED_FOR', - 6 => 'HTTP_FORWARDED', - 7 => 'HTTP_CLIENT_IP', - 8 => 'HTTP_IP', - 9 => 'REMOTE_ADDR' - ); - foreach ($IPKeys as $key) { - if (array_key_exists($key, $_SERVER) === true) { - $ips = explode(',', $_SERVER[$key]); - foreach ($ips as $ip) { - $ip = trim($ip); - if ($convertToInteger === true) { - return ip2long($ip); - } + /** + * Function getIPAddress + * + * @param bool $convertToInteger + * + * @return bool|int|string + * @author: 713uk13m + * @time : 2019-01-10 16:24 + * + */ + public function getIPAddress(bool $convertToInteger = false) + { + $IPKeys = array( + 0 => 'HTTP_CF_CONNECTING_IP', + 1 => 'HTTP_X_FORWARDED_FOR', + 2 => 'HTTP_X_FORWARDED', + 3 => 'HTTP_X_IPADDRESS', + 4 => 'HTTP_X_CLUSTER_CLIENT_IP', + 5 => 'HTTP_FORWARDED_FOR', + 6 => 'HTTP_FORWARDED', + 7 => 'HTTP_CLIENT_IP', + 8 => 'HTTP_IP', + 9 => 'REMOTE_ADDR' + ); + foreach ($IPKeys as $key) { + if (array_key_exists($key, $_SERVER) === true) { + $ips = explode(',', $_SERVER[$key]); + foreach ($ips as $ip) { + $ip = trim($ip); + if ($convertToInteger === true) { + return ip2long($ip); + } - return $ip; - } - } - } + return $ip; + } + } + } - return false; - } + return false; + } - /** - * Function errorLogMessage - * - * @return array - * @author: 713uk13m - * @time : 2019-01-10 16:29 - * - */ - public function errorLogMessage(): array - { - $context = array( - 'IP' => $this->getIPAddress(), - 'COUNTRY' => $_SERVER['HTTP_CF_IPCOUNTRY'] ?? '', - 'PROTOCOL' => $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '', - 'METHOD' => $_SERVER['REQUEST_METHOD'] ?? '', - 'HOST' => $_SERVER['HTTP_HOST'] ?? '', - 'URI' => $_SERVER['REQUEST_URI'] ?? '', - 'QUERY_STRING' => $_SERVER['QUERY_STRING'] ?? '', - 'USER_AGENT' => $_SERVER['HTTP_USER_AGENT'] ?? '', - ); - $message = $this->getIPAddress() . ' -> Access Denied!'; + /** + * Function errorLogMessage + * + * @return array + * @author: 713uk13m + * @time : 2019-01-10 16:29 + * + */ + public function errorLogMessage(): array + { + $context = array( + 'IP' => $this->getIPAddress(), + 'COUNTRY' => $_SERVER['HTTP_CF_IPCOUNTRY'] ?? '', + 'PROTOCOL' => $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '', + 'METHOD' => $_SERVER['REQUEST_METHOD'] ?? '', + 'HOST' => $_SERVER['HTTP_HOST'] ?? '', + 'URI' => $_SERVER['REQUEST_URI'] ?? '', + 'QUERY_STRING' => $_SERVER['QUERY_STRING'] ?? '', + 'USER_AGENT' => $_SERVER['HTTP_USER_AGENT'] ?? '', + ); + $message = $this->getIPAddress() . ' -> Access Denied!'; - return array( - 'message' => $message, - 'context' => $context - ); - } + return array( + 'message' => $message, + 'context' => $context + ); + } - /** - * Function accessDenied - * - * @return string - * @author: 713uk13m - * @time : 2019-01-10 16:29 - * - */ - public function accessDenied(): string - { - return 'Access Denied!'; - } + /** + * Function accessDenied + * + * @return string + * @author: 713uk13m + * @time : 2019-01-10 16:29 + * + */ + public function accessDenied(): string + { + return 'Access Denied!'; + } - /** - * Function accessDeniedResponse - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 41:56 - */ - public function accessDeniedResponse() - { - http_response_code(403); - exit($this->accessDenied()); - } + /** + * Function accessDeniedResponse + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 41:56 + */ + public function accessDeniedResponse() + { + http_response_code(403); + exit($this->accessDenied()); + } - /** - * Function writeErrorLog - * - * @param array $data - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/24/2021 13:31 - */ - public function writeErrorLog(array $data = array()) - { - $log = new Logging(); - $log->setLogDestination($this->logDestination)->write($data['message'], $data['context']); - } + /** + * Function writeErrorLog + * + * @param array $data + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/24/2021 13:31 + */ + public function writeErrorLog(array $data = array()) + { + $log = new Logging(); + $log->setLogDestination($this->logDestination)->write($data['message'], $data['context']); + } - /** - * Function checkUserConnect - * - * @param bool $defaultState - * - * @return $this - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 59:34 - */ - public function checkUserConnect(bool $defaultState = false): FirewallIP - { - $firewall = new Firewall(); - $firewall->setDefaultState($defaultState); - if (defined('HUNGNG_IP_WHITELIST') && is_array(HUNGNG_IP_WHITELIST)) { - $firewall->addList(HUNGNG_IP_WHITELIST, 'local', true); - } elseif (!empty($this->ipWhiteList)) { - $firewall->addList($this->ipWhiteList, 'local', true); - } - if (defined('HUNGNG_IP_BLACKLIST') && is_array(HUNGNG_IP_BLACKLIST)) { - $firewall->addList(HUNGNG_IP_BLACKLIST, 'localBad', false); - } elseif (!empty($this->ipBlacklist)) { - $firewall->addList($this->ipBlacklist, 'localBad', false); - } - $this->access = $firewall->setIpAddress($this->getIPAddress())->handle(); + /** + * Function checkUserConnect + * + * @param bool $defaultState + * + * @return $this + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 59:34 + */ + public function checkUserConnect(bool $defaultState = false): FirewallIP + { + $firewall = new Firewall(); + $firewall->setDefaultState($defaultState); + if (defined('HUNGNG_IP_WHITELIST') && is_array(HUNGNG_IP_WHITELIST)) { + $firewall->addList(HUNGNG_IP_WHITELIST, 'local', true); + } elseif (!empty($this->ipWhiteList)) { + $firewall->addList($this->ipWhiteList, 'local', true); + } + if (defined('HUNGNG_IP_BLACKLIST') && is_array(HUNGNG_IP_BLACKLIST)) { + $firewall->addList(HUNGNG_IP_BLACKLIST, 'localBad', false); + } elseif (!empty($this->ipBlacklist)) { + $firewall->addList($this->ipBlacklist, 'localBad', false); + } + $this->access = $firewall->setIpAddress($this->getIPAddress())->handle(); - // Nếu chạy từ CLI -> bypass + // Nếu chạy từ CLI -> bypass - return $this; - } + return $this; + } - /** - * Function isAccess - * - * @return bool - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/18/2021 41:24 - */ - public function isAccess(): bool - { - return $this->access; - } + /** + * Function isAccess + * + * @return bool + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/18/2021 41:24 + */ + public function isAccess(): bool + { + return $this->access; + } } diff --git a/src/Logging.php b/src/Logging.php index 8b2c364..c54c23d 100644 --- a/src/Logging.php +++ b/src/Logging.php @@ -25,49 +25,49 @@ */ class Logging { - protected $logDestination = ''; + protected $logDestination = ''; - /** - * Function setLogDestination - * - * @param $logDestination - * - * @return $this - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/24/2021 57:40 - */ - public function setLogDestination($logDestination): Logging - { - $this->logDestination = $logDestination; + /** + * Function setLogDestination + * + * @param $logDestination + * + * @return $this + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/24/2021 57:40 + */ + public function setLogDestination($logDestination): Logging + { + $this->logDestination = $logDestination; - return $this; - } + return $this; + } - /** - * Function write - * - * @param string $message - * @param array $context - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/24/2021 23:18 - */ - public function write(string $message = '', array $context = array()) - { - if (file_exists($this->logDestination)) { - try { - $log = new Logger('firewall'); - $log->pushHandler(new StreamHandler($this->logDestination, Logger::WARNING)); - $log->warning($message, $context); - } catch (Exception $exception) { - if (!empty($this->logDestination)) { - @error_log($exception->getMessage() . PHP_EOL, 3, $this->logDestination); - } else { - @error_log($exception->getMessage() . PHP_EOL, 3); - } - } - } - } + /** + * Function write + * + * @param string $message + * @param array $context + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/24/2021 23:18 + */ + public function write(string $message = '', array $context = array()) + { + if (file_exists($this->logDestination)) { + try { + $log = new Logger('firewall'); + $log->pushHandler(new StreamHandler($this->logDestination, Logger::WARNING)); + $log->warning($message, $context); + } catch (Exception $exception) { + if (!empty($this->logDestination)) { + @error_log($exception->getMessage() . PHP_EOL, 3, $this->logDestination); + } else { + @error_log($exception->getMessage() . PHP_EOL, 3); + } + } + } + } } diff --git a/src/Output.php b/src/Output.php index 9705ca7..9c83f77 100644 --- a/src/Output.php +++ b/src/Output.php @@ -19,21 +19,21 @@ */ class Output { - /** - * Function writeLn - * - * @param $message - * @param string $newLine - * - * @author : 713uk13m - * @copyright: 713uk13m - * @time : 09/02/2021 42:50 - */ - public static function writeLn($message, string $newLine = "\n") - { - if (function_exists('json_encode') && (is_array($message) || is_object($message))) { - $message = json_encode($message); - } - echo $message . $newLine; - } + /** + * Function writeLn + * + * @param $message + * @param string $newLine + * + * @author : 713uk13m + * @copyright: 713uk13m + * @time : 09/02/2021 42:50 + */ + public static function writeLn($message, string $newLine = "\n") + { + if (function_exists('json_encode') && (is_array($message) || is_object($message))) { + $message = json_encode($message); + } + echo $message . $newLine; + } }