1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2024 Rafael San José <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace CoreModules\Admin\Controller; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Base\Config; |
22
|
|
|
use Alxarafe\Base\Controller\ViewController; |
23
|
|
|
use Alxarafe\Base\Database; |
24
|
|
|
use Alxarafe\Lib\Functions; |
25
|
|
|
use Alxarafe\Lib\Messages; |
26
|
|
|
use Alxarafe\Lib\Trans; |
27
|
|
|
use Alxarafe\Tools\ModuleManager; |
28
|
|
|
use DebugBar\DebugBarException; |
29
|
|
|
use stdClass; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class ConfigController. App settings controller. |
33
|
|
|
*/ |
34
|
|
|
class ConfigController extends ViewController |
35
|
|
|
{ |
36
|
|
|
const MENU = 'admin|config'; |
37
|
|
|
const SIDEBAR_MENU = [ |
38
|
|
|
['option' => 'admin|auth|logout', 'url' => 'index.php?module=Admin&controller=Auth&method=logout'], |
39
|
|
|
['option' => 'general', 'url' => 'index.php?module=Admin&controller=Config&method=general'], |
40
|
|
|
['option' => 'appearance', 'url' => 'index.php?module=Admin&controller=Config&method=appearance'], |
41
|
|
|
['option' => 'security', 'url' => 'index.php?module=Admin&controller=Config&method=security'] |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Configuration file information |
46
|
|
|
* |
47
|
|
|
* @var stdClass |
48
|
|
|
*/ |
49
|
|
|
public $data; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Array with availables languages |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
public $languages; |
57
|
|
|
public $themes; |
58
|
|
|
public $dbtypes; |
59
|
|
|
|
60
|
|
|
public $db_create; |
61
|
|
|
public bool $pdo_connection; |
62
|
|
|
public bool $pdo_db_exists; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the module name for use in url function |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public static function getModuleName(): string |
70
|
|
|
{ |
71
|
|
|
return 'Admin'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the controller name for use in url function |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public static function getControllerName(): string |
80
|
|
|
{ |
81
|
|
|
return 'Config'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function beforeAction(): bool |
85
|
|
|
{ |
86
|
|
|
$this->getPost(); |
87
|
|
|
|
88
|
|
|
$this->languages = Trans::getAvailableLanguages(); |
89
|
|
|
$this->themes = Functions::getThemes(); |
90
|
|
|
$this->dbtypes = Database::getDbDrivers(); |
91
|
|
|
|
92
|
|
|
Trans::setLang($this->config->main->language ?? Trans::FALLBACK_LANG); |
93
|
|
|
|
94
|
|
|
$this->checkDatabaseStatus(); |
95
|
|
|
|
96
|
|
|
return parent::beforeAction(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sets $data with the information sent by POST |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
private function getPost(): void |
105
|
|
|
{ |
106
|
|
|
$this->data = Config::getConfig(); |
107
|
|
|
if (!isset($this->data)) { |
108
|
|
|
$this->data = new stdClass(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach (Config::CONFIG_STRUCTURE as $section => $values) { |
112
|
|
|
if (!isset($this->data->{$section})) { |
113
|
|
|
$this->data->{$section} = new stdClass(); |
114
|
|
|
} |
115
|
|
|
foreach ($values as $variable) { |
116
|
|
|
$value = Functions::getIfIsset($variable, $this->data->{$section}->{$variable} ?? ''); |
117
|
|
|
if (!isset($value)) { |
118
|
|
|
continue; |
119
|
|
|
} |
120
|
|
|
$this->data->{$section}->{$variable} = $value; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->db_create = filter_input(INPUT_POST, 'db_create'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function checkDatabaseStatus() |
128
|
|
|
{ |
129
|
|
|
$this->pdo_connection = Database::checkConnection($this->data->db); |
130
|
|
|
$this->pdo_db_exists = Database::checkIfDatabaseExists($this->data->db); |
131
|
|
|
if (!$this->pdo_connection) { |
132
|
|
|
Messages::addAdvice(Trans::_('pdo_connection_error')); |
133
|
|
|
} elseif (!$this->pdo_db_exists) { |
134
|
|
|
Messages::addAdvice(Trans::_('pdo_db_connection_error', ['db' => $this->data->db->name])); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* The 'index' action: Default action |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function doIndex(): bool |
144
|
|
|
{ |
145
|
|
|
/** |
146
|
|
|
* TODO: The value of this variable will be filled in when the roles |
147
|
|
|
* are correctly implemented. |
148
|
|
|
*/ |
149
|
|
|
$restricted_access = false; |
150
|
|
|
|
151
|
|
|
if (isset($this->config) && $restricted_access) { |
|
|
|
|
152
|
|
|
$this->template = 'page/forbidden'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* The 'createDatabase' action: Creates the database |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function doCreateDatabase(): bool |
164
|
|
|
{ |
165
|
|
|
if (!Database::createDatabaseIfNotExists($this->data->db)) { |
166
|
|
|
Messages::addError(Trans::_('error_connecting_database', ['db' => $this->data->db->name])); |
167
|
|
|
return true; |
168
|
|
|
} |
169
|
|
|
Messages::addMessage(Trans::_('successful_connection_database', ['db' => $this->data->db->name])); |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function doGoMigrations(): void |
174
|
|
|
{ |
175
|
|
|
Functions::httpRedirect(MigrationController::url()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Save action. |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
* @throws DebugBarException |
183
|
|
|
*/ |
184
|
|
|
public function doSave(): bool |
185
|
|
|
{ |
186
|
|
|
if (!config::setConfig($this->data)) { |
187
|
|
|
Messages::addError(Trans::_('error_saving_settings')); |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
Messages::addMessage(Trans::_('settings_saved_successfully')); |
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function doExit(): void |
196
|
|
|
{ |
197
|
|
|
Functions::httpRedirect(PublicController::url()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function doRegenerate(): bool |
201
|
|
|
{ |
202
|
|
|
ModuleManager::regenerate(); |
203
|
|
|
return true; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function doRunMigrations(): void |
207
|
|
|
{ |
208
|
|
|
Functions::httpRedirect(MigrationController::url()); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|