| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- /**
- * @author Open Mind
- * @date 26/04/2018
- * @time 11:11
- */
- namespace OpenMind\System;
- use Exception;
- use FilesystemIterator;
- use Log;
- use OpenMind\Web\Web;
- use RecursiveDirectoryIterator;
- use RecursiveIteratorIterator;
- use Template;
- class OMUtils
- {
- private $Logger;
- public function __construct()
- {
- $this->Logger = new Log('Site.log');
- }
- /**
- * The Singleton Instance
- *
- * @return null|OMUtils
- */
- public static function getInstance(): ?OMUtils
- {
- static $instance = null;
- if ($instance === null) {
- $instance = new static(); // PHP 5+ lazy state building
- }
- return $instance;
- }
- /**
- * Get logger classe
- *
- * @return Log
- */
- public function getLogger(): Log
- {
- return $this->Logger;
- }
- /**
- * Load all modules in <$path> folder
- * @param string $path
- */
- public function PluginLoader(string $path)
- {
- // <class priority>
- // TODO if is necessary to give a priority to some classes before other, initialize here
- Global $System, $Web;
- $System = new System(); // we need System Base class first, so will force to load as first
- $Web = new Web();
- // </class priority>
- $Directory = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
- $Iterator = new RecursiveIteratorIterator($Directory, RecursiveIteratorIterator::CHILD_FIRST);
- foreach($Iterator as $item) {
- if (is_file($item->getPathname()))
- continue;
- $moduleName = $item->getFilename();
- $className = str_replace('/', '\\', ltrim("{$item->getPathname()}/{$moduleName}", $path));
- // Load only PHP file with same name of namespace ex.: \Lamborghini\Lamborghini.php
- $moduleInitPath = "{$item->getPathname()}/{$moduleName}.php";
- if (file_exists($moduleInitPath)) { // If already imported, skip
- if (!isset($$moduleName)) {
- $$moduleName = new $className(); // Instantiate the Module, all init procedure must reside in __constructor() method
- $$moduleName->setTsStartLoad();
- }
- }
- }
- }
- /**
- * Use F3 template resolve() to parse string like "{{@BASESITE}}"
- *
- * @param string $strTemplate the F3 original string
- * @return string converted string
- */
- public function getStringFromTemplate(string $strTemplate)
- {
- return Template::instance()->resolve($strTemplate);
- }
- /**
- * Load an encrypted file with the method "base64_encode(gzcompress(<path to config.ini>))", save it in a temporary file in temp folder of system and load with F3::config()
- *
- * @param string $path
- * @throws Exception
- */
- public function LoadEncryptedConfig(string $path)
- {
- global $f3;
- if (!file_exists($path))
- throw new Exception('Config file not found');
- $tmpFilepath = tempnam(sys_get_temp_dir(), 'om');
- $config = $this->Decrypt(file_get_contents($path));
- file_put_contents($tmpFilepath, $config);
- $f3->config($tmpFilepath, true);
- unlink($tmpFilepath);
- }
- /**
- * Decrypt $data using OM-Link algorithm
- *
- * @param $binaryData
- * @return string
- */
- public function Decrypt($binaryData)
- {
- return gzuncompress(base64_decode($binaryData));
- }
- /**
- * Encrypt $data using OM-Link algorithm
- *
- * @param $data
- * @return string
- */
- public function Encrypt($data)
- {
- return base64_encode(gzcompress($data));
- }
- /*
- * Exit from APP if the OMLINK requirement aren't meet
- */
- public function DieByOldPhpVersion()
- {
- if (version_compare(PHP_VERSION, '7.2') != 1)
- throw new Exception('PHP 7.2+ is need to run the project');
- }
- /**
- * Check if a port server is opened, return $errno code of fsockopen function
- *
- * @param string $ip
- * @param int $port
- * @param int $timeout_sec
- * @return int
- */
- function isServerAlive(string $ip, int $port, int $timeout_sec = 5): int
- {
- try {
- @fclose($sck = @fsockopen($ip, $port, $errno, $errstr, $timeout_sec));
- } catch (Exception $e) {
- die($e->getMessage());
- }
- return $errno;
- }
- /**
- * Recurse delete folder
- *
- * @param string $dir /path/for/the/directory/
- */
- function rrmdir($dir)
- {
- if (is_dir($dir)) {
- $files = glob($dir . '*', GLOB_MARK); //GLOB_MARK adds a slash to directories returned
- foreach($files as $file) {
- $this->rrmdir($file);
- }
- @rmdir($dir);
- } elseif (is_file($dir)) {
- @unlink($dir);
- }
- }
- }
|