|
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->template = 'page/config'; |
|
87
|
|
|
|
|
88
|
|
|
$this->getPost(); |
|
89
|
|
|
|
|
90
|
|
|
$this->languages = Trans::getAvailableLanguages(); |
|
91
|
|
|
$this->themes = Functions::getThemes(); |
|
92
|
|
|
$this->dbtypes = Database::getDbDrivers(); |
|
93
|
|
|
|
|
94
|
|
|
Trans::setLang($this->config->main->language ?? Trans::FALLBACK_LANG); |
|
95
|
|
|
|
|
96
|
|
|
$this->checkDatabaseStatus(); |
|
97
|
|
|
|
|
98
|
|
|
return parent::beforeAction(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets $data with the information sent by POST |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getPost(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->data = Config::getConfig(); |
|
109
|
|
|
if (!isset($this->data)) { |
|
110
|
|
|
$this->data = new stdClass(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
foreach (Config::CONFIG_STRUCTURE as $section => $values) { |
|
114
|
|
|
if (!isset($this->data->{$section})) { |
|
115
|
|
|
$this->data->{$section} = new stdClass(); |
|
116
|
|
|
} |
|
117
|
|
|
foreach ($values as $variable) { |
|
118
|
|
|
$value = Functions::getIfIsset($variable, $this->data->{$section}->{$variable} ?? ''); |
|
119
|
|
|
if (!isset($value)) { |
|
120
|
|
|
continue; |
|
121
|
|
|
} |
|
122
|
|
|
$this->data->{$section}->{$variable} = $value; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->db_create = filter_input(INPUT_POST, 'db_create'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
private function checkDatabaseStatus() |
|
130
|
|
|
{ |
|
131
|
|
|
$this->pdo_connection = Database::checkConnection($this->data->db); |
|
132
|
|
|
$this->pdo_db_exists = Database::checkIfDatabaseExists($this->data->db); |
|
133
|
|
|
if (!$this->pdo_connection) { |
|
134
|
|
|
Messages::addAdvice(Trans::_('pdo_connection_error')); |
|
135
|
|
|
} elseif (!$this->pdo_db_exists) { |
|
136
|
|
|
Messages::addAdvice(Trans::_('pdo_db_connection_error', ['db' => $this->data->db->name])); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* The 'index' action: Default action |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public function doIndex(): bool |
|
146
|
|
|
{ |
|
147
|
|
|
/** |
|
148
|
|
|
* TODO: The value of this variable will be filled in when the roles |
|
149
|
|
|
* are correctly implemented. |
|
150
|
|
|
*/ |
|
151
|
|
|
$restricted_access = false; |
|
152
|
|
|
|
|
153
|
|
|
if (isset($this->config) && $restricted_access) { |
|
|
|
|
|
|
154
|
|
|
$this->template = 'page/forbidden'; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return true; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* The 'createDatabase' action: Creates the database |
|
162
|
|
|
* |
|
163
|
|
|
* @return bool |
|
164
|
|
|
*/ |
|
165
|
|
|
public function doCreateDatabase(): bool |
|
166
|
|
|
{ |
|
167
|
|
|
Functions::httpRedirect(MigrationController::url()); |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
if (!Database::createDatabaseIfNotExists($this->data->db)) { |
|
171
|
|
|
Messages::addError(Trans::_('error_connecting_database', ['db' => $this->data->db->name])); |
|
172
|
|
|
return true; |
|
173
|
|
|
} |
|
174
|
|
|
Messages::addMessage(Trans::_('successful_connection_database', ['db' => $this->data->db->name])); |
|
175
|
|
|
|
|
176
|
|
|
return static::doRunMigrationsAndSeeders(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function doRunMigrationsAndSeeders(): bool |
|
180
|
|
|
{ |
|
181
|
|
|
new Database($this->data->db); |
|
182
|
|
|
|
|
183
|
|
|
Config::runMigrations(); |
|
184
|
|
|
Config::runSeeders(); |
|
185
|
|
|
|
|
186
|
|
|
return true; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* CheckConnection action. |
|
191
|
|
|
* |
|
192
|
|
|
* @return bool |
|
193
|
|
|
* @throws DebugBarException |
|
194
|
|
|
*/ |
|
195
|
|
|
public function doCheckConnection(): bool |
|
196
|
|
|
{ |
|
197
|
|
|
$ok = Database::checkDatabaseConnection($this->data->db, $this->db_create); |
|
198
|
|
|
if (!$ok) { |
|
199
|
|
|
// $messages = Messages::getMessages(); |
|
200
|
|
|
// foreach ($messages as $message) { |
|
201
|
|
|
// Messages::addAdvice($message); |
|
202
|
|
|
// } |
|
203
|
|
|
Messages::addError(Trans::_('error_connecting_database', ['db' => $this->data->db->name])); |
|
204
|
|
|
return true; |
|
205
|
|
|
} |
|
206
|
|
|
Messages::addMessage(Trans::_('successful_connection_database', ['db' => $this->data->db->name])); |
|
207
|
|
|
|
|
208
|
|
|
return static::doRunMigrationsAndSeeders(); |
|
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Save action. |
|
213
|
|
|
* |
|
214
|
|
|
* @return bool |
|
215
|
|
|
* @throws DebugBarException |
|
216
|
|
|
*/ |
|
217
|
|
|
public function doSave(): bool |
|
218
|
|
|
{ |
|
219
|
|
|
if (!config::setConfig($this->data)) { |
|
220
|
|
|
Messages::addError(Trans::_('error_saving_settings')); |
|
221
|
|
|
return false; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
Messages::addMessage(Trans::_('settings_saved_successfully')); |
|
225
|
|
|
return true; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function doExit() |
|
229
|
|
|
{ |
|
230
|
|
|
/** |
|
231
|
|
|
* TODO: Loads public page |
|
232
|
|
|
*/ |
|
233
|
|
|
$this->template = 'page/public'; |
|
234
|
|
|
return true; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function doRegenerate() |
|
238
|
|
|
{ |
|
239
|
|
|
ModuleManager::regenerate(); |
|
240
|
|
|
return true; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|