| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- /**
- * @author Open Mind
- * @date 21/06/2018
- * @time 18:28
- */
- namespace OpenMind\System;
- use Base;
- use Cache;
- use DB\SQL;
- class System extends PluginStrategy
- {
- /**
- * Normal F3 session file cache.
- * Every session will be client related
- *
- * @var Cache
- */
- private $cache;
- /**
- * The shared cache, cache file will be created with prefix 'OMLINK_SHARED_CACHE.xxxxxx' where 'xxxxxx' is the variable name.
- * Every session will be server related
- *
- * @var Cache
- */
- private $sharedCache;
- public function __construct()
- {
- global $f3;
- // Set Error Handler
- $f3->set('ONERROR', __NAMESPACE__ . '\ErrorHandlers->Handler');
- $this->cache = Cache::instance();
- $this->sharedCache = new Cache();
- $this->sharedCache->load($f3->get('CACHE'), 'OMLINK_SHARED_CACHE');
- // Set Database connection
- try {
- $dbMode = (strtolower(PHP_OS_FAMILY) == 'linux') ? TRUE : FALSE;
- if ($f3->get('CLI')) // from cli, always persistent mode
- $dbMode = TRUE;
- $db = new SQL(
- $f3->get('DSN'),
- $f3->get('dbuser'),
- $f3->get('dbpass'),
- array(
- \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
- \PDO::ATTR_PERSISTENT => $dbMode // To avoid massive new persistent connections due to php-cgi processes
- ));
- // This fix memory leak in ->exec() and all methods of F3 that use this function like load, find, findone etc...
- $db->log(FALSE);
- $f3->set('DB', $db);
- } catch (PDOException $e) {
- die($e->getMessage());
- }
- $this->setSystemRoutes();
- }
- private function setSystemRoutes()
- {
- $this->AddRoute('GET /api.system.getserverstatus', __NAMESPACE__ . '\System->getServerStatus');
- $this->AddRoute('POST /api.system.removenotification/@code', __NAMESPACE__ . '\System->NotificationDelete');
- }
- /**
- * Get cache cache
- *
- * @return Cache
- */
- public function getCache(): Cache
- {
- return $this->cache;
- }
- public function getSharedCache(): Cache
- {
- return $this->sharedCache;
- }
- /**
- * AJAX web service for bell notifications and system checking
- *
- * @param Base $f3
- */
- public function getServerStatus($f3)
- {
- $qs = new QueueSystem('FindBoxService_isAlive');
- if (!$qs->check()) {
- $lastUpdate = time() - $qs->command;
- if ($lastUpdate > $f3->get('FINDBOXSERVICE.GRACE_PERIOD_SEC')) {
- $omlNotifications = new Notifications();
- $omlNotifications->notify(Notifications::FB_NOTALIVE, 'Errore: Servizio di FindBox non disponibile');
- } else {
- $omlNotifications = new Notifications();
- $omlNotifications->Remove(Notifications::FB_NOTALIVE);
- }
- }
- $ret = [];
- $notifications = new Notifications();
- $notifications->count = 'count(caption)';
- $sqlGroupBy = array(
- 'order' => 'timestamp DESC',
- 'group' => 'caption'
- );
- $ret['notify'] = array_map(array($notifications, 'cast'), $notifications->find(NULL, $sqlGroupBy));
- echo json_encode($ret);
- }
- public function NotificationDelete($f3, $args)
- {
- if ($args['code'] != 0)
- (new Notifications())->Remove($args['code']);
- else
- (new Notifications())->emptyAll();
- }
- /**
- * Store in F3 cache the session id, usefull for CLI call
- *
- * @param string $cacheName
- * @see System::RestoreWebSession()
- */
- public Function StoreWebSession(string $cacheName = 'WEBSESSIONID')
- {
- $this->cache->set($cacheName, session_id());
- }
- /**
- * Restore the web session for CLI calls
- *
- * @param string $cacheName
- * @see System::StoreWebSession()
- */
- public function RestoreWebSession(string $cacheName = 'WEBSESSIONID')
- {
- if ($this->cache->get($cacheName)) {
- $id = $this->cache->get($cacheName);
- if (isset($_SESSION)) { // if there is already a session running
- session_write_close(); // save and close it
- }
- session_id($id);
- session_start();
- }
- }
- }
|