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(); } } }