System.php 3.5 KB

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