System.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. class System extends PluginStrategy
  12. {
  13. /**
  14. * Normal F3 session file cache.
  15. * Every session will be client related
  16. *
  17. * @var Cache
  18. */
  19. private $cache;
  20. /**
  21. * The shared cache, cache file will be created with prefix 'OMLINK_SHARED_CACHE.xxxxxx' where 'xxxxxx' is the variable name.
  22. * Every session will be server related
  23. *
  24. * @var Cache
  25. */
  26. private $sharedCache;
  27. public function __construct()
  28. {
  29. global $f3;
  30. // Set Error Handler
  31. $f3->set('ONERROR', __NAMESPACE__ . '\ErrorHandlers->Handler');
  32. $this->cache = Cache::instance();
  33. $this->sharedCache = new Cache();
  34. $this->sharedCache->load($f3->get('CACHE'), 'OMLINK_SHARED_CACHE');
  35. // Set Database connection
  36. try {
  37. $dbMode = (strtolower(PHP_OS_FAMILY) == 'linux') ? TRUE : FALSE;
  38. if ($f3->get('CLI')) // from cli, always persistent mode
  39. $dbMode = TRUE;
  40. $db = new SQL(
  41. $f3->get('DSN'),
  42. $f3->get('dbuser'),
  43. $f3->get('dbpass'),
  44. array(
  45. \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
  46. \PDO::ATTR_PERSISTENT => $dbMode // To avoid massive new persistent connections due to php-cgi processes
  47. ));
  48. // This fix memory leak in ->exec() and all methods of F3 that use this function like load, find, findone etc...
  49. $db->log(FALSE);
  50. $f3->set('DB', $db);
  51. } catch (PDOException $e) {
  52. die($e->getMessage());
  53. }
  54. $this->setSystemRoutes();
  55. }
  56. private function setSystemRoutes()
  57. {
  58. $this->AddRoute('GET /api.system.getserverstatus', __NAMESPACE__ . '\System->getServerStatus');
  59. $this->AddRoute('POST /api.system.removenotification/@code', __NAMESPACE__ . '\System->NotificationDelete');
  60. }
  61. /**
  62. * Get cache cache
  63. *
  64. * @return Cache
  65. */
  66. public function getCache(): Cache
  67. {
  68. return $this->cache;
  69. }
  70. public function getSharedCache(): Cache
  71. {
  72. return $this->sharedCache;
  73. }
  74. /**
  75. * AJAX web service for bell notifications and system checking
  76. *
  77. * @param Base $f3
  78. */
  79. public function getServerStatus($f3)
  80. {
  81. $qs = new QueueSystem('FindBoxService_isAlive');
  82. if (!$qs->check()) {
  83. $lastUpdate = time() - $qs->command;
  84. if ($lastUpdate > $f3->get('FINDBOXSERVICE.GRACE_PERIOD_SEC')) {
  85. $omlNotifications = new Notifications();
  86. $omlNotifications->notify(Notifications::FB_NOTALIVE, 'Errore: Servizio di FindBox non disponibile');
  87. } else {
  88. $omlNotifications = new Notifications();
  89. $omlNotifications->Remove(Notifications::FB_NOTALIVE);
  90. }
  91. }
  92. $ret = [];
  93. $notifications = new Notifications();
  94. $notifications->count = 'count(caption)';
  95. $sqlGroupBy = array(
  96. 'order' => 'timestamp DESC',
  97. 'group' => 'caption'
  98. );
  99. $ret['notify'] = array_map(array($notifications, 'cast'), $notifications->find(NULL, $sqlGroupBy));
  100. echo json_encode($ret);
  101. }
  102. public function NotificationDelete($f3, $args)
  103. {
  104. if ($args['code'] != 0)
  105. (new Notifications())->Remove($args['code']);
  106. else
  107. (new Notifications())->emptyAll();
  108. }
  109. /**
  110. * Store in F3 cache the session id, usefull for CLI call
  111. *
  112. * @param string $cacheName
  113. * @see System::RestoreWebSession()
  114. */
  115. public Function StoreWebSession(string $cacheName = 'WEBSESSIONID')
  116. {
  117. $this->cache->set($cacheName, session_id());
  118. }
  119. /**
  120. * Restore the web session for CLI calls
  121. *
  122. * @param string $cacheName
  123. * @see System::StoreWebSession()
  124. */
  125. public function RestoreWebSession(string $cacheName = 'WEBSESSIONID')
  126. {
  127. if ($this->cache->get($cacheName)) {
  128. $id = $this->cache->get($cacheName);
  129. if (isset($_SESSION)) { // if there is already a session running
  130. session_write_close(); // save and close it
  131. }
  132. session_id($id);
  133. session_start();
  134. }
  135. }
  136. }