| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- /**
- * @author Giancarlo Voci
- * @date 04/12/2018
- * @time 15:22
- */
- namespace OpenMind\Admin;
- use Base;
- use Exception;
- use OpenMind\System\OMUtils;
- use OpenMind\System\User;
- use OpenMind\Web\Controller;
- use Template;
- class Pages extends Controller
- {
- 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';
- /**
- * 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(Base $f3)
- {
- parent::__construct($f3);
- $f3->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<string, string> $response Response data.
- * @return void
- * @sideEffects Writes JSON to the HTTP response.
- */
- private function sendJson(array $response): void
- {
- echo json_encode($response);
- }
- }
|