System.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 21/06/2018
  5. * @time 18:28
  6. */
  7. namespace OpenMind\System;
  8. use Base;
  9. use Cache;
  10. use DB\SQL;
  11. use PDO;
  12. use PDOException;
  13. use ReflectionClass;
  14. use ReflectionException;
  15. /**
  16. * Class Enum
  17. * ---
  18. * Implement basic ENUMERATORS, support auto numbering of integer default number and custom start number
  19. *
  20. * It's possible to get enum name by add suffix "ToString" to enum name:<br/>
  21. * >$enum->START<br/>
  22. * $enum->STARTToString (return 'START')<br/>
  23. *
  24. * @package OpenMind\System
  25. */
  26. abstract class Enum
  27. {
  28. public function __construct()
  29. {
  30. $counter = 0;
  31. foreach((new ReflectionClass($this))->getProperties() as $property) {
  32. $propertyValue = $property->getValue($this);
  33. switch (gettype($propertyValue)) {
  34. case 'integer':
  35. $counter = $propertyValue + 1;
  36. break;
  37. case 'NULL':
  38. $propertyValue = $counter++;
  39. break;
  40. }
  41. $property->setValue($this, $propertyValue);
  42. $this->{"{$property->name}ToString"} = $property->name;
  43. }
  44. }
  45. /**
  46. * Get the variable name as string by its value
  47. *
  48. * @param $value
  49. * @return string|null the variable name
  50. * @throws ReflectionException
  51. */
  52. public function getNameByValue($value): ?string
  53. {
  54. foreach((new ReflectionClass($this))->getProperties() as $property) {
  55. if ($property->getValue($this) === $value) {
  56. return $property->name;
  57. }
  58. }
  59. return null;
  60. }
  61. }
  62. class System extends PluginStrategy
  63. {
  64. /**
  65. * Normal F3 session file cache.
  66. * Every session will be client related
  67. *
  68. * @var Cache
  69. */
  70. private $cache;
  71. /**
  72. * The shared cache, cache file will be created with prefix 'OMLINK_SHARED_CACHE.xxxxxx' where 'xxxxxx' is the variable name.
  73. * Every session will be server related
  74. *
  75. * @var Cache
  76. */
  77. private $sharedCache;
  78. public function __construct()
  79. {
  80. global $f3;
  81. // Set Error Handler
  82. $f3->set('ONERROR', __NAMESPACE__ . '\ErrorHandlers->Handler');
  83. $this->cache = Cache::instance();
  84. $this->sharedCache = new Cache();
  85. $this->sharedCache->load($f3->get('CACHE'), 'OMLINK_SHARED_CACHE');
  86. // Set Database connection
  87. try {
  88. $dbMode = (strtolower(PHP_OS_FAMILY) == 'linux') ? TRUE : FALSE;
  89. if ($f3->get('CLI')) // from cli, always persistent mode
  90. $dbMode = TRUE;
  91. $db = new SQL(
  92. $f3->get('DSN'),
  93. $f3->get('dbuser'),
  94. $f3->get('dbpass'),
  95. array(
  96. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  97. PDO::ATTR_PERSISTENT => $dbMode // To avoid massive new persistent connections due to php-cgi processes
  98. ));
  99. // This fix memory leak in ->exec() and all methods of F3 that use this function like load, find, findone etc...
  100. $db->log(FALSE);
  101. $f3->set('DB', $db);
  102. } catch (PDOException $e) {
  103. die($e->getMessage());
  104. }
  105. $this->setSystemRoutes();
  106. }
  107. private function setSystemRoutes()
  108. {
  109. $this->AddRoute('GET /api.system.getserverstatus', __NAMESPACE__ . '\System->getServerStatus');
  110. $this->AddRoute('POST /api.system.removenotification/@code', __NAMESPACE__ . '\System->NotificationDelete');
  111. }
  112. /**
  113. * Get cache cache
  114. *
  115. * @return Cache
  116. */
  117. public function getCache(): Cache
  118. {
  119. return $this->cache;
  120. }
  121. public function getSharedCache(): Cache
  122. {
  123. return $this->sharedCache;
  124. }
  125. /**
  126. * AJAX web service for bell notifications and system checking
  127. *
  128. * @param Base $f3
  129. */
  130. public function getServerStatus($f3)
  131. {
  132. $qs = new QueueSystem();
  133. if (!$qs->send('')) {
  134. $omlNotifications = new Notifications();
  135. $omlNotifications->notify(Notifications::FB_NOTALIVE, 'Errore: Servizio di FindBox non disponibile');
  136. } else {
  137. $omlNotifications = new Notifications();
  138. $omlNotifications->Remove(Notifications::FB_NOTALIVE);
  139. }
  140. $ret = [];
  141. $notifications = new Notifications();
  142. $notifications->count = 'count(caption)';
  143. $sqlGroupBy = array(
  144. 'order' => 'timestamp DESC',
  145. 'group' => 'caption'
  146. );
  147. $ret['notify'] = array_map(array($notifications, 'cast'), $notifications->find(NULL, $sqlGroupBy));
  148. echo json_encode($ret);
  149. }
  150. public function NotificationDelete($f3, $args)
  151. {
  152. if ($args['code'] != 0)
  153. (new Notifications())->Remove($args['code']);
  154. else
  155. (new Notifications())->emptyAll();
  156. }
  157. /**
  158. * Store in F3 cache the session id, usefull for CLI call
  159. *
  160. * @param string $cacheName
  161. * @see System::RestoreWebSession()
  162. */
  163. public Function StoreWebSession(string $cacheName = 'WEBSESSIONID')
  164. {
  165. $this->cache->set($cacheName, session_id());
  166. }
  167. /**
  168. * Restore the web session for CLI calls
  169. *
  170. * @param string $cacheName
  171. * @see System::StoreWebSession()
  172. */
  173. public function RestoreWebSession(string $cacheName = 'WEBSESSIONID')
  174. {
  175. if ($this->cache->get($cacheName)) {
  176. $id = $this->cache->get($cacheName);
  177. if (isset($_SESSION)) { // if there is already a session running
  178. session_write_close(); // save and close it
  179. }
  180. session_id($id);
  181. session_start();
  182. }
  183. }
  184. }