Pages.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * @author Giancarlo Voci
  4. * @date 04/12/2018
  5. * @time 15:22
  6. */
  7. namespace OpenMind\Admin;
  8. use Base;
  9. use Exception;
  10. use OpenMind\System\OMUtils;
  11. use OpenMind\System\User;
  12. use OpenMind\Web\Controller;
  13. use Template;
  14. class Pages extends Controller
  15. {
  16. private const CONFIGURATION_BACKUP_FILE = 'cfg/config.bin.old';
  17. private const CONFIGURATION_FILE = 'cfg/config.bin';
  18. private const LOAD_CONFIGURATION_METHOD = 'loadconfig';
  19. private const SAVE_CONFIGURATION_METHOD = 'saveconfig';
  20. /**
  21. * Initializes the admin controller and configures its template directory.
  22. *
  23. * @param Base $f3 Framework instance.
  24. * @return void
  25. * @sideEffects Changes the framework UI directory.
  26. */
  27. public function __construct(Base $f3)
  28. {
  29. parent::__construct($f3);
  30. $f3->set('UI', 'ui/admin/');
  31. }
  32. /**
  33. * Renders the admin landing page.
  34. *
  35. * @param Base $f3 Framework instance.
  36. * @return void
  37. * @sideEffects Sends the rendered template to the response.
  38. */
  39. public function Admin(Base $f3): void
  40. {
  41. $f3->set('PAGENAME', 'Admin');
  42. echo Template::instance()->render('masterpage.tpl');
  43. }
  44. /**
  45. * Renders the encrypted configuration editor.
  46. *
  47. * @param Base $f3 Framework instance.
  48. * @return void
  49. * @sideEffects Reads the configuration file and sends the rendered template.
  50. * @throws Exception When the configuration file cannot be read.
  51. */
  52. public function OML_Settings(Base $f3): void
  53. {
  54. global $frameworkUtils;
  55. $config = $this->readConfiguration($frameworkUtils);
  56. $f3->set('PAGENAME', 'OM-Link Settings');
  57. $f3->set('CONTENT', 'omlsettings.tpl');
  58. $f3->set('CONFIG_TEXT', $config);
  59. echo Template::instance()->render('masterpage.tpl');
  60. }
  61. /**
  62. * Loads or saves configuration data requested by the admin UI.
  63. *
  64. * @param Base $f3 Framework instance.
  65. * @return void
  66. * @sideEffects Reads or replaces the encrypted configuration file and sends JSON.
  67. */
  68. public function Config(Base $f3): void
  69. {
  70. global $frameworkUtils;
  71. $request = json_decode($f3->get('BODY'));
  72. if (!is_object($request) || !isset($request->method)) {
  73. $this->sendJson(['err' => 'Invalid configuration command']);
  74. return;
  75. }
  76. switch ($request->method) {
  77. case self::SAVE_CONFIGURATION_METHOD:
  78. if (!isset($request->value) || !is_string($request->value)) {
  79. $this->sendJson(['err' => 'Configuration value is required']);
  80. return;
  81. }
  82. $this->saveConfiguration($frameworkUtils->Encrypt($request->value));
  83. $this->sendJson(['err' => 'Configuration saved']);
  84. return;
  85. case self::LOAD_CONFIGURATION_METHOD:
  86. $this->sendJson([
  87. 'err' => 'Configuration reloaded successfully',
  88. 'value' => $this->readConfiguration($frameworkUtils),
  89. ]);
  90. return;
  91. default:
  92. $this->sendJson(['err' => 'Unsupported configuration command']);
  93. }
  94. }
  95. /**
  96. * Renders the user-management page.
  97. *
  98. * @param Base $f3 Framework instance.
  99. * @return void
  100. * @sideEffects Reads users from the database and sends the rendered template.
  101. */
  102. public function Users(Base $f3): void
  103. {
  104. $users = new User('');
  105. $f3->set('USERLIST', $users->find());
  106. $f3->set('PAGENAME', 'User Management');
  107. $f3->set('CONTENT', 'users.tpl');
  108. echo Template::instance()->render('masterpage.tpl');
  109. }
  110. /**
  111. * Reads and decrypts the application configuration.
  112. *
  113. * @param OMUtils $frameworkUtils Configuration utility service.
  114. * @return string Decrypted configuration.
  115. * @sideEffects Reads the configuration file.
  116. * @throws Exception When the configuration file cannot be read.
  117. */
  118. private function readConfiguration(OMUtils $frameworkUtils): string
  119. {
  120. $config = file_get_contents(self::CONFIGURATION_FILE);
  121. if ($config === false) {
  122. throw new Exception('Configuration file could not be read');
  123. }
  124. return $frameworkUtils->Decrypt($config);
  125. }
  126. /**
  127. * Replaces the configuration file while retaining the previous version as backup.
  128. *
  129. * @param string $configuration Encrypted configuration.
  130. * @return void
  131. * @sideEffects Renames and writes configuration files.
  132. * @throws Exception When the backup or write fails.
  133. */
  134. private function saveConfiguration(string $configuration): void
  135. {
  136. if (!rename(self::CONFIGURATION_FILE, self::CONFIGURATION_BACKUP_FILE)) {
  137. throw new Exception('Configuration backup could not be created');
  138. }
  139. if (file_put_contents(self::CONFIGURATION_FILE, $configuration) !== false) {
  140. return;
  141. }
  142. rename(self::CONFIGURATION_BACKUP_FILE, self::CONFIGURATION_FILE);
  143. throw new Exception('Configuration file could not be written');
  144. }
  145. /**
  146. * Sends a JSON response.
  147. *
  148. * @param array<string, string> $response Response data.
  149. * @return void
  150. * @sideEffects Writes JSON to the HTTP response.
  151. */
  152. private function sendJson(array $response): void
  153. {
  154. echo json_encode($response);
  155. }
  156. }