set('UI', 'ui/admin/'); } /** * 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 { $f3->set('PAGENAME', 'Admin'); echo Template::instance()->render('masterpage.tpl'); } /** * 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; $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'); } /** * 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; $request = json_decode($f3->get('BODY')); if (!is_object($request) || !isset($request->method)) { $this->sendJson(['err' => 'Invalid configuration command']); return; } switch ($request->method) { case self::SAVE_CONFIGURATION_METHOD: if (!isset($request->value) || !is_string($request->value)) { $this->sendJson(['err' => 'Configuration value is required']); return; } $this->saveConfiguration($frameworkUtils->Encrypt($request->value)); $this->sendJson(['err' => 'Configuration saved']); return; case self::LOAD_CONFIGURATION_METHOD: $this->sendJson([ 'err' => 'Configuration reloaded successfully', 'value' => $this->readConfiguration($frameworkUtils), ]); return; default: $this->sendJson(['err' => 'Unsupported configuration 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 { $users = new User(''); $f3->set('USERLIST', $users->find()); $f3->set('PAGENAME', 'User Management'); $f3->set('CONTENT', 'users.tpl'); echo Template::instance()->render('masterpage.tpl'); } /** * 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'); } return $frameworkUtils->Decrypt($config); } /** * 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'); } 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'); } /** * Sends a JSON response. * * @param array $response Response data. * @return void * @sideEffects Writes JSON to the HTTP response. */ private function sendJson(array $response): void { echo json_encode($response); } }