* >$enum->START
* $enum->STARTToString (return 'START')
*
* @package OpenMind\System
*/
abstract class Enum
{
public function __construct()
{
$counter = 0;
foreach((new ReflectionClass($this))->getProperties() as $property) {
$propertyValue = $property->getValue($this);
switch (gettype($propertyValue)) {
case 'integer':
$counter = $propertyValue + 1;
break;
case 'NULL':
$propertyValue = $counter++;
break;
}
$property->setValue($this, $propertyValue);
$this->{"{$property->name}ToString"} = $property->name;
}
}
/**
* Get the variable name as string by its value
*
* @param $value
* @return string|null the variable name
* @throws ReflectionException
*/
public function getNameByValue($value): ?string
{
foreach((new ReflectionClass($this))->getProperties() as $property) {
if ($property->getValue($this) === $value) {
return $property->name;
}
}
return null;
}
}
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();
if (!$qs->send('')) {
$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();
}
}
}