OMUtils.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 26/04/2018
  5. * @time 11:11
  6. */
  7. namespace OpenMind\System;
  8. use Exception;
  9. use FilesystemIterator;
  10. use Log;
  11. use OpenMind\Web\Web;
  12. use RecursiveDirectoryIterator;
  13. use RecursiveIteratorIterator;
  14. use Template;
  15. class OMUtils
  16. {
  17. private $Logger;
  18. public function __construct()
  19. {
  20. $this->Logger = new Log('Site.log');
  21. }
  22. /**
  23. * The Singleton Instance
  24. *
  25. * @return null|OMUtils
  26. */
  27. public static function getInstance(): ?OMUtils
  28. {
  29. static $instance = null;
  30. if ($instance === null) {
  31. $instance = new static(); // PHP 5+ lazy state building
  32. }
  33. return $instance;
  34. }
  35. /**
  36. * Get logger classe
  37. *
  38. * @return Log
  39. */
  40. public function getLogger(): Log
  41. {
  42. return $this->Logger;
  43. }
  44. /**
  45. * Load all modules in <$path> folder
  46. * @param string $path
  47. */
  48. public function PluginLoader(string $path)
  49. {
  50. // <class priority>
  51. // TODO if is necessary to give a priority to some classes before other, initialize here
  52. Global $System, $Web;
  53. $System = new System(); // we need System Base class first, so will force to load as first
  54. $Web = new Web();
  55. // </class priority>
  56. $Directory = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
  57. $Iterator = new RecursiveIteratorIterator($Directory, RecursiveIteratorIterator::CHILD_FIRST);
  58. foreach($Iterator as $item) {
  59. if (is_file($item->getPathname()))
  60. continue;
  61. $moduleName = $item->getFilename();
  62. $className = str_replace('/', '\\', ltrim("{$item->getPathname()}/{$moduleName}", $path));
  63. // Load only PHP file with same name of namespace ex.: \Lamborghini\Lamborghini.php
  64. $moduleInitPath = "{$item->getPathname()}/{$moduleName}.php";
  65. if (file_exists($moduleInitPath)) { // If already imported, skip
  66. if (!isset($$moduleName)) {
  67. $$moduleName = new $className(); // Instantiate the Module, all init procedure must reside in __constructor() method
  68. $$moduleName->setTsStartLoad();
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * Use F3 template resolve() to parse string like "{{@BASESITE}}"
  75. *
  76. * @param string $strTemplate the F3 original string
  77. * @return string converted string
  78. */
  79. public function getStringFromTemplate(string $strTemplate)
  80. {
  81. return Template::instance()->resolve($strTemplate);
  82. }
  83. /**
  84. * 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()
  85. *
  86. * @param string $path
  87. * @throws Exception
  88. */
  89. public function LoadEncryptedConfig(string $path)
  90. {
  91. global $f3;
  92. if (!file_exists($path))
  93. throw new Exception('Config file not found');
  94. $tmpFilepath = tempnam(sys_get_temp_dir(), 'om');
  95. $config = $this->Decrypt(file_get_contents($path));
  96. file_put_contents($tmpFilepath, $config);
  97. $f3->config($tmpFilepath, true);
  98. unlink($tmpFilepath);
  99. }
  100. /**
  101. * Decrypt $data using OM-Link algorithm
  102. *
  103. * @param $binaryData
  104. * @return string
  105. */
  106. public function Decrypt($binaryData)
  107. {
  108. return gzuncompress(base64_decode($binaryData));
  109. }
  110. /**
  111. * Encrypt $data using OM-Link algorithm
  112. *
  113. * @param $data
  114. * @return string
  115. */
  116. public function Encrypt($data)
  117. {
  118. return base64_encode(gzcompress($data));
  119. }
  120. /*
  121. * Exit from APP if the OMLINK requirement aren't meet
  122. */
  123. public function DieByOldPhpVersion()
  124. {
  125. if (version_compare(PHP_VERSION, '7.2') != 1)
  126. throw new Exception('PHP 7.2+ is need to run the project');
  127. }
  128. /**
  129. * Check if a port server is opened, return $errno code of fsockopen function
  130. *
  131. * @param string $ip
  132. * @param int $port
  133. * @param int $timeout_sec
  134. * @return int
  135. */
  136. function isServerAlive(string $ip, int $port, int $timeout_sec = 5): int
  137. {
  138. try {
  139. @fclose($sck = @fsockopen($ip, $port, $errno, $errstr, $timeout_sec));
  140. } catch (Exception $e) {
  141. die($e->getMessage());
  142. }
  143. return $errno;
  144. }
  145. /**
  146. * Recurse delete folder
  147. *
  148. * @param string $dir /path/for/the/directory/
  149. */
  150. function rrmdir($dir)
  151. {
  152. if (is_dir($dir)) {
  153. $files = glob($dir . '*', GLOB_MARK); //GLOB_MARK adds a slash to directories returned
  154. foreach($files as $file) {
  155. $this->rrmdir($file);
  156. }
  157. @rmdir($dir);
  158. } elseif (is_file($dir)) {
  159. @unlink($dir);
  160. }
  161. }
  162. }