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)
{
//
// 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();
//
$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())", 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);
}
}
}