|
|
@@ -9,237 +9,168 @@ namespace OpenMind\Admin;
|
|
|
|
|
|
use Base;
|
|
|
use Exception;
|
|
|
-use OpenMind\FindBox\FindBoxCoreSystem;
|
|
|
-use OpenMind\FindBox\SmartESL;
|
|
|
-use OpenMind\System\Notifications;
|
|
|
+use OpenMind\System\OMUtils;
|
|
|
use OpenMind\System\User;
|
|
|
use OpenMind\Web\Controller;
|
|
|
use Template;
|
|
|
|
|
|
class Pages extends Controller
|
|
|
{
|
|
|
- private $findBoxCore = null;
|
|
|
+ private const CONFIGURATION_BACKUP_FILE = 'cfg/config.bin.old';
|
|
|
+ private const CONFIGURATION_FILE = 'cfg/config.bin';
|
|
|
+ private const LOAD_CONFIGURATION_METHOD = 'loadconfig';
|
|
|
+ private const SAVE_CONFIGURATION_METHOD = 'saveconfig';
|
|
|
|
|
|
/**
|
|
|
- * Pages constructor.
|
|
|
- * @param Base $f3
|
|
|
+ * Initializes the admin controller and configures its template directory.
|
|
|
+ *
|
|
|
+ * @param Base $f3 Framework instance.
|
|
|
+ * @return void
|
|
|
+ * @sideEffects Changes the framework UI directory.
|
|
|
*/
|
|
|
- public function __construct($f3)
|
|
|
+ public function __construct(Base $f3)
|
|
|
{
|
|
|
parent::__construct($f3);
|
|
|
-
|
|
|
- try {
|
|
|
- $this->findBoxCore = new FindBoxCoreSystem(...array_values($f3->get('CORE.fb')));
|
|
|
- } catch (Exception $e) {
|
|
|
- $ns = new Notifications();
|
|
|
- $ns->notify(Notifications::FB_ADMIN, $f3->clean($e->getMessage()));
|
|
|
- // IF can't connect to FINDBOX, reroute to OMLINK Page Settings to fast change settings
|
|
|
- if (!$f3->get('AJAX') && ($f3->get('PATTERN') != '/admin/omlsettings'))
|
|
|
- $f3->reroute('/admin/omlsettings');
|
|
|
- }
|
|
|
-
|
|
|
- // Set Admin F3 UI folder
|
|
|
$f3->set('UI', 'ui/admin/');
|
|
|
}
|
|
|
|
|
|
- public function Admin($f3)
|
|
|
- {
|
|
|
- echo Template::instance()->render('masterpage.tpl');
|
|
|
- }
|
|
|
-
|
|
|
- public function ESLStatus(Base $f3)
|
|
|
+ /**
|
|
|
+ * Renders the admin landing page.
|
|
|
+ *
|
|
|
+ * @param Base $f3 Framework instance.
|
|
|
+ * @return void
|
|
|
+ * @sideEffects Sends the rendered template to the response.
|
|
|
+ */
|
|
|
+ public function Admin(Base $f3): void
|
|
|
{
|
|
|
- $eslStatus = $this->findBoxCore->GetAllESLStatus();
|
|
|
-
|
|
|
- $f3->set('STATUS_BADGE_COLOR', [
|
|
|
- 'OFFLINE' => 'warning',
|
|
|
- 'BLOCKED,FC' => 'danger',
|
|
|
- 'NO_STATUS' => 'light',
|
|
|
- 'OK' => 'success'
|
|
|
- ]);
|
|
|
-
|
|
|
- $f3->set('BATLEV_ICONS', [
|
|
|
- 'GOOD' => 'full', // 100% - 60%
|
|
|
- 'LOW' => 'half', // 60% - 30%
|
|
|
- 'BAD' => 'empty', // 30% - 0%
|
|
|
- ]);
|
|
|
-
|
|
|
- $f3->set('ESL_STATUS', $eslStatus);
|
|
|
- $f3->set('PAGENAME', 'Dashboard');
|
|
|
- $f3->set('CONTENT', 'labelstatus.tpl');
|
|
|
+ $f3->set('PAGENAME', 'Admin');
|
|
|
echo Template::instance()->render('masterpage.tpl');
|
|
|
}
|
|
|
|
|
|
- public function OML_Settings(Base $f3)
|
|
|
+ /**
|
|
|
+ * Renders the encrypted configuration editor.
|
|
|
+ *
|
|
|
+ * @param Base $f3 Framework instance.
|
|
|
+ * @return void
|
|
|
+ * @sideEffects Reads the configuration file and sends the rendered template.
|
|
|
+ * @throws Exception When the configuration file cannot be read.
|
|
|
+ */
|
|
|
+ public function OML_Settings(Base $f3): void
|
|
|
{
|
|
|
global $frameworkUtils;
|
|
|
|
|
|
- $path = 'cfg/config.bin';
|
|
|
- if (!file_exists($path))
|
|
|
- throw new Exception('Config file not found');
|
|
|
-
|
|
|
- $data = file_get_contents($path);
|
|
|
- $config = $frameworkUtils->Decrypt($data);
|
|
|
-
|
|
|
+ $config = $this->readConfiguration($frameworkUtils);
|
|
|
$f3->set('PAGENAME', 'OM-Link Settings');
|
|
|
$f3->set('CONTENT', 'omlsettings.tpl');
|
|
|
$f3->set('CONFIG_TEXT', $config);
|
|
|
echo Template::instance()->render('masterpage.tpl');
|
|
|
}
|
|
|
|
|
|
- public function Config(Base $f3)
|
|
|
+ /**
|
|
|
+ * Loads or saves configuration data requested by the admin UI.
|
|
|
+ *
|
|
|
+ * @param Base $f3 Framework instance.
|
|
|
+ * @return void
|
|
|
+ * @sideEffects Reads or replaces the encrypted configuration file and sends JSON.
|
|
|
+ */
|
|
|
+ public function Config(Base $f3): void
|
|
|
{
|
|
|
global $frameworkUtils;
|
|
|
- $config = $f3->get('BODY');
|
|
|
- if (strlen($config) == 0) {
|
|
|
- echo json_encode([
|
|
|
- "err" => "Empty body command"
|
|
|
- ]);
|
|
|
+
|
|
|
+ $request = json_decode($f3->get('BODY'));
|
|
|
+ if (!is_object($request) || !isset($request->method)) {
|
|
|
+ $this->sendJson(['err' => 'Invalid configuration command']);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- $configOBJ = json_decode($config);
|
|
|
- switch ($configOBJ->method) {
|
|
|
-
|
|
|
- case 'saveconfig':
|
|
|
- $newConfig = $frameworkUtils->Encrypt($configOBJ->value);
|
|
|
- @rename('cfg/config.bin', 'cfg/config.bin.old');
|
|
|
- @file_put_contents('cfg/config.bin', $newConfig);
|
|
|
- echo json_encode([
|
|
|
- "err" => "Configuration saved"
|
|
|
- ]);
|
|
|
- break;
|
|
|
-
|
|
|
- case 'loadconfig':
|
|
|
- $config = $frameworkUtils->Decrypt(file_get_contents('cfg/config.bin'));
|
|
|
- echo json_encode([
|
|
|
- "err" => "Configuration reloaded successfully",
|
|
|
- "value" => $config
|
|
|
- ]);
|
|
|
- break;
|
|
|
-
|
|
|
- case 'geteslstatus':
|
|
|
-
|
|
|
- $labelID = $configOBJ->label_id;
|
|
|
- $esl = new SmartESL($this->findBoxCore, [$labelID]);
|
|
|
- $status = $esl->GetStatus(true);
|
|
|
-
|
|
|
- $svgPath = $this->findBoxCore->GetTemplateImage($status[$configOBJ->label_id]['data']['template_id']);
|
|
|
-
|
|
|
- echo json_encode([
|
|
|
- "err" => "Status reloaded successfully",
|
|
|
- "value" => $status[$configOBJ->label_id],
|
|
|
- "svgPath" => $svgPath
|
|
|
- ]);
|
|
|
-
|
|
|
- break;
|
|
|
-
|
|
|
- case 'blinklabels':
|
|
|
- $esl = new SmartESL($this->findBoxCore, [$configOBJ->labelid]);
|
|
|
- $ret = $esl->BlinkSpecial(2, 1, 1000, 5, 0);
|
|
|
-
|
|
|
- if ($ret['err'] == 0 && $ret['value']['code'] == 0) {
|
|
|
- if ($ret['value']['value'][$configOBJ->labelid])
|
|
|
- echo json_encode([
|
|
|
- "err" => 0,
|
|
|
- "value" => "{$configOBJ->labelid} blink successfull"
|
|
|
- ]);
|
|
|
- else
|
|
|
- echo json_encode([
|
|
|
- "err" => 1,
|
|
|
- "value" => "{$configOBJ->labelid} blink error"
|
|
|
- ]);
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
- case 'unbind':
|
|
|
- $ret = $this->findBoxCore->UnBind($configOBJ->labelid);
|
|
|
- if ($ret['err'] != 0) {
|
|
|
- echo json_encode($ret);
|
|
|
+ switch ($request->method) {
|
|
|
+ case self::SAVE_CONFIGURATION_METHOD:
|
|
|
+ if (!isset($request->value) || !is_string($request->value)) {
|
|
|
+ $this->sendJson(['err' => 'Configuration value is required']);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- echo json_encode([
|
|
|
- "err" => 0,
|
|
|
- "value" => "{$configOBJ->labelid} unbind successfull"
|
|
|
- ]);
|
|
|
- break;
|
|
|
-
|
|
|
- case 'resetfailcounter':
|
|
|
- $URI = "/1.0.0/index.php/smartesl.resetfailcounter/1.0.0/[[\"{$configOBJ->labelid}\"]]";
|
|
|
- $this->findBoxCore->CallAPI($URI, "");
|
|
|
+ $this->saveConfiguration($frameworkUtils->Encrypt($request->value));
|
|
|
+ $this->sendJson(['err' => 'Configuration saved']);
|
|
|
+ return;
|
|
|
|
|
|
- echo json_encode([
|
|
|
- "err" => 0,
|
|
|
- "value" => "{$configOBJ->labelid} Reset Fail Counter successfull"
|
|
|
+ case self::LOAD_CONFIGURATION_METHOD:
|
|
|
+ $this->sendJson([
|
|
|
+ 'err' => 'Configuration reloaded successfully',
|
|
|
+ 'value' => $this->readConfiguration($frameworkUtils),
|
|
|
]);
|
|
|
+ return;
|
|
|
|
|
|
- break;
|
|
|
-
|
|
|
- case 'TemplateSelector':
|
|
|
- $this->TemplateSelector($f3, $configOBJ);
|
|
|
- break;
|
|
|
-
|
|
|
- case 'SetDisplay':
|
|
|
- foreach($configOBJ->labels as $labelID) {
|
|
|
- $esl = new SmartESL($this->findBoxCore, [$labelID]);
|
|
|
- $esl->Display($configOBJ->id, $labelID);
|
|
|
- }
|
|
|
- echo json_encode([
|
|
|
- "err" => "Display command successfull"
|
|
|
- ]);
|
|
|
- break;
|
|
|
-
|
|
|
+ default:
|
|
|
+ $this->sendJson(['err' => 'Unsupported configuration command']);
|
|
|
}
|
|
|
- return;
|
|
|
}
|
|
|
|
|
|
- private function TemplateSelector(Base $f3, $Command)
|
|
|
+ /**
|
|
|
+ * Renders the user-management page.
|
|
|
+ *
|
|
|
+ * @param Base $f3 Framework instance.
|
|
|
+ * @return void
|
|
|
+ * @sideEffects Reads users from the database and sends the rendered template.
|
|
|
+ */
|
|
|
+ public function Users(Base $f3): void
|
|
|
{
|
|
|
- global $frameworkUtils;
|
|
|
-
|
|
|
- $fb_template = $this->findBoxCore->CallAPI('/1.0.0/index.php/smartesl.gettemplates/1.0.0/', '[]');
|
|
|
- $templates = $this->findBoxCore->getLabelsFromResponse($fb_template);
|
|
|
-
|
|
|
- if ($Command->clearCache == 1)
|
|
|
- $frameworkUtils->rrmdir("fbtpl");
|
|
|
-
|
|
|
- $tplFileNames = [];
|
|
|
- if ($templates != null) {
|
|
|
- foreach($templates as $item) {
|
|
|
+ $users = new User('');
|
|
|
+ $f3->set('USERLIST', $users->find());
|
|
|
+ $f3->set('PAGENAME', 'User Management');
|
|
|
+ $f3->set('CONTENT', 'users.tpl');
|
|
|
+ echo Template::instance()->render('masterpage.tpl');
|
|
|
+ }
|
|
|
|
|
|
- if (!in_array($item['resolution'], $Command->resolution))
|
|
|
- continue;
|
|
|
+ /**
|
|
|
+ * Reads and decrypts the application configuration.
|
|
|
+ *
|
|
|
+ * @param OMUtils $frameworkUtils Configuration utility service.
|
|
|
+ * @return string Decrypted configuration.
|
|
|
+ * @sideEffects Reads the configuration file.
|
|
|
+ * @throws Exception When the configuration file cannot be read.
|
|
|
+ */
|
|
|
+ private function readConfiguration(OMUtils $frameworkUtils): string
|
|
|
+ {
|
|
|
+ $config = file_get_contents(self::CONFIGURATION_FILE);
|
|
|
+ if ($config === false) {
|
|
|
+ throw new Exception('Configuration file could not be read');
|
|
|
+ }
|
|
|
|
|
|
- if (!file_exists("fbtpl/{$item['template_id']}.svg"))
|
|
|
- $this->findBoxCore->GetTemplateImage($item['template_id']);
|
|
|
+ return $frameworkUtils->Decrypt($config);
|
|
|
+ }
|
|
|
|
|
|
- $tplFileNames[] = [
|
|
|
- "id" => $item['template_id'],
|
|
|
- "name" => $item['name'],
|
|
|
- "resolution" => $item['resolution'],
|
|
|
- "title" => $item['name'],
|
|
|
- "svg" => file_get_contents("fbtpl/{$item['template_id']}.svg")
|
|
|
- ];
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * Replaces the configuration file while retaining the previous version as backup.
|
|
|
+ *
|
|
|
+ * @param string $configuration Encrypted configuration.
|
|
|
+ * @return void
|
|
|
+ * @sideEffects Renames and writes configuration files.
|
|
|
+ * @throws Exception When the backup or write fails.
|
|
|
+ */
|
|
|
+ private function saveConfiguration(string $configuration): void
|
|
|
+ {
|
|
|
+ if (!rename(self::CONFIGURATION_FILE, self::CONFIGURATION_BACKUP_FILE)) {
|
|
|
+ throw new Exception('Configuration backup could not be created');
|
|
|
}
|
|
|
|
|
|
- $f3->set('TPLOBJ', $tplFileNames);
|
|
|
- $tpl = Template::instance()->render('templates.tpl');
|
|
|
-
|
|
|
- echo json_encode([
|
|
|
- "err" => 0,
|
|
|
- "value" => $tpl
|
|
|
- ]);
|
|
|
+ if (file_put_contents(self::CONFIGURATION_FILE, $configuration) !== false) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
+ rename(self::CONFIGURATION_BACKUP_FILE, self::CONFIGURATION_FILE);
|
|
|
+ throw new Exception('Configuration file could not be written');
|
|
|
}
|
|
|
|
|
|
- public function Users(Base $f3)
|
|
|
+ /**
|
|
|
+ * Sends a JSON response.
|
|
|
+ *
|
|
|
+ * @param array<string, string> $response Response data.
|
|
|
+ * @return void
|
|
|
+ * @sideEffects Writes JSON to the HTTP response.
|
|
|
+ */
|
|
|
+ private function sendJson(array $response): void
|
|
|
{
|
|
|
- $listaUtenti = new User('');
|
|
|
- $f3->set('USERLIST', $listaUtenti->find());
|
|
|
- $f3->set('PAGENAME', 'User Management');
|
|
|
- $f3->set('CONTENT', 'users.tpl');
|
|
|
- echo Template::instance()->render('masterpage.tpl');
|
|
|
+ echo json_encode($response);
|
|
|
}
|
|
|
}
|