|
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\Lib\Functions; |
|
24
|
|
|
use stdClass; |
|
25
|
|
|
|
|
26
|
|
|
class ConfigController extends ViewController |
|
27
|
|
|
{ |
|
28
|
|
|
public $data; |
|
29
|
|
|
|
|
30
|
|
|
public function doIndex(): bool |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* TODO: The value of this variable will be filled in when the roles |
|
34
|
|
|
* are correctly implemented. |
|
35
|
|
|
*/ |
|
36
|
|
|
$restricted_access = false; |
|
37
|
|
|
|
|
38
|
|
|
$this->template = 'page/config'; |
|
39
|
|
|
if (isset($this->config) && $restricted_access) { |
|
|
|
|
|
|
40
|
|
|
$this->template = 'page/forbidden'; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->getPost(); |
|
44
|
|
|
|
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function getPost() |
|
49
|
|
|
{ |
|
50
|
|
|
if (!isset($this->data)) { |
|
51
|
|
|
$this->data = new stdClass(); |
|
52
|
|
|
} |
|
53
|
|
|
foreach (Config::getConfig() as $section => $values) { |
|
54
|
|
|
if (!isset($this->data->{$section})) { |
|
55
|
|
|
$this->data->{$section} = new stdClass(); |
|
56
|
|
|
} |
|
57
|
|
|
foreach ($values as $variable => $value) { |
|
58
|
|
|
$this->data->{$section}->{$variable} = Functions::getIfIsset($variable, $value); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function doLogin() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->template = 'page/admin/login'; |
|
66
|
|
|
$login = filter_input(INPUT_POST, 'login'); |
|
67
|
|
|
if (!$login) { |
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$username = filter_input(INPUT_POST, 'username'); |
|
72
|
|
|
$password = filter_input(INPUT_POST, 'password'); |
|
73
|
|
|
if (!Auth::login($username, $password)) { |
|
74
|
|
|
$this->advice[] = $this->langs->trans('ErrorBadLoginPassword'); |
|
|
|
|
|
|
75
|
|
|
dump([ |
|
76
|
|
|
'ConfigController' => $this |
|
77
|
|
|
]); |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
$this->template = 'page/admin/info'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function doLogout() |
|
84
|
|
|
{ |
|
85
|
|
|
Auth::logout(true); |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function doCheckConnection() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->template = 'page/config'; |
|
92
|
|
|
|
|
93
|
|
|
static::getPost(); |
|
|
|
|
|
|
94
|
|
|
var_dump($this->data->db); |
|
|
|
|
|
|
95
|
|
|
$ok = Config::checkDatabaseConnection($this->data->db); |
|
96
|
|
|
if (!$ok) { |
|
97
|
|
|
static::addError('Error al conectar a la base de datos "' . $this->data->db->name . '".'); |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
static::addMessage('Conexión satisfactoria con la base de datos "' . $this->data->db->name . '".'); |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function doSave(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
static::getPost(); |
|
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Converts the stdClass to an array |
|
109
|
|
|
*/ |
|
110
|
|
|
$data = json_decode(json_encode($this->data), true); |
|
111
|
|
|
|
|
112
|
|
|
$result = config::setConfig($data); |
|
113
|
|
|
if (!$result) { |
|
114
|
|
|
$this->template = 'page/config'; |
|
115
|
|
|
static::addError('Error al guardar la configuración'); |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
/** |
|
119
|
|
|
* TODO: Loads public page |
|
120
|
|
|
*/ |
|
121
|
|
|
$this->template = 'page/public'; |
|
122
|
|
|
static::addMessage('Configuración guardada correctamente'); |
|
123
|
|
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|