-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
system_database.php
259 lines (226 loc) · 11.9 KB
/
system_database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/*
part-db version 0.1
Copyright (C) 2005 Christoph Lechner
http://www.cl-projects.de/
part-db version 0.2+
Copyright (C) 2009 K. Jacobs and others (see authors.php)
http://code.google.com/p/part-db/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
include_once __DIR__ . '/start_session.php';
use PartDB\Database;
use PartDB\HTML;
use PartDB\Permissions\DatabasePermission;
use PartDB\Permissions\PermissionManager;
use PartDB\User;
$messages = array();
$fatal_error = false; // if a fatal error occurs, only the $messages will be printed, but not the site content
/********************************************************************************
*
* Evaluate $_REQUEST
*
*********************************************************************************/
$db_type = isset($_POST['db_type']) ? (string)$_POST['db_type'] : 'mysql';
$db_host = isset($_POST['db_host']) ? (string)$_POST['db_host'] : 'localhost';
$db_name = isset($_POST['db_name']) ? (string)$_POST['db_name'] : '';
$db_user = isset($_POST['db_user']) ? (string)$_POST['db_user'] : '';
$db_password = isset($_POST['db_password']) ? trim((string)$_POST['db_password']) : '';
$automatic_updates_enabled = isset($_POST['automatic_updates_enabled']);
$action = 'default';
if (isset($_POST['apply_connection_settings'])) {
$action = 'apply_connection_settings';
}
if (isset($_POST['apply_auto_updates'])) {
$action = 'apply_auto_updates';
}
if (isset($_POST['make_update'])) {
$action = 'make_update';
}
if (isset($_POST['make_new_update'])) {
$action = 'make_new_update';
}
/********************************************************************************
*
* Initialize Objects
*
*********************************************************************************/
$html = new HTML($config['html']['theme'], $user_config['theme'], 'Datenbank');
try {
$database = new Database();
$log = new \PartDB\Log($database);
$current_user = User::getLoggedInUser($database, $log);
} catch (Exception $e) {
$messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red');
$fatal_error = true;
}
/********************************************************************************
*
* Execute actions
*
*********************************************************************************/
if (true) { //Allow to save connection settings, even when a error happened.
switch ($action) {
case 'apply_connection_settings':
$config_old = $config;
try {
if (isset($current_user)) {
$current_user->tryDo(PermissionManager::DATABASE, DatabasePermission::WRITE_DB_SETTINGS);
}
if ($config['is_online_demo']) {
break;
}
$config['db']['type'] = $db_type;
//$config['db']['charset'] = $db_charset; // temporarly deactivated
$config['db']['host'] = $db_host;
$config['db']['name'] = $db_name;
$config['db']['user'] = $db_user;
$config['db']['password'] = $db_password;
saveConfig();
header('Location: system_database.php'); // Reload the page that we can see if the new settings are stored successfully
} catch (Exception $e) {
$config = $config_old; // reload the old config
$messages[] = array('text' => _('Die neuen Werte konnten nicht gespeichert werden!'), 'strong' => true, 'color' => 'red');
$messages[] = array('text' => _('Fehlermeldung: ') . nl2br($e->getMessage()), 'color' => 'red');
}
break;
case 'apply_auto_updates':
$config_old = $config;
try {
$current_user->tryDo(PermissionManager::DATABASE, DatabasePermission::WRITE_DB_SETTINGS);
$config['db']['auto_update'] = $automatic_updates_enabled;
saveConfig();
//header('Location: system_database.php'); // Reload the page that we can see if the new settings are stored successfully --> does not work correctly?!
} catch (Exception $e) {
$config = $config_old; // reload the old config
$messages[] = array('text' => _('Die neuen Werte konnten nicht gespeichert werden!'), 'strong' => true, 'color' => 'red');
$messages[] = array('text' => _('Fehlermeldung: ') . nl2br($e->getMessage()), 'color' => 'red');
}
break;
/** @noinspection PhpMissingBreakStatementInspection */
case 'make_new_update':
// start the next update attempt from the beginning, not from the step of the last error
$config['db']['update_error']['version'] = -1;
$config['db']['update_error']['next_step'] = 0;
// no break
case 'make_update':
try {
$current_user->tryDo(PermissionManager::DATABASE, DatabasePermission::UPDATE_DB);
if (!is_object($database)) {
throw new Exception(_('Es konnte keine Verbindung mit der Datenbank hergestellt werden!'));
}
$database_update_executed = true;
$update_log = $database->update();
foreach ($update_log as $message) {
$messages[] = array('text' => nl2br($message['text']), 'color' => $message['error'] == true ? 'red' : 'black');
}
} catch (Exception $e) {
$messages[] = array('text' => _('Es trat ein Fehler auf!'), 'strong' => true, 'color' => 'red');
$messages[] = array('text' => _('Fehlermeldung: ') . nl2br($e->getMessage()), 'color' => 'red');
}
$messages[count($messages) - 1]['text'] .= nl2br("\n");
break;
}
}
/********************************************************************************
*
* Set all HTML variables
*
*********************************************************************************/
try {
$html->setVariable('is_online_demo', $config['is_online_demo'], 'boolean');
$html->setVariable('db_type_loop', arrayToTemplateLoop($config['db_types'], $config['db']['type']));
$html->setVariable('db_host', $config['db']['host'], 'string');
$html->setVariable('db_name', $config['db']['name'], 'string');
$html->setVariable('db_user', $config['db']['user'], 'string');
$html->setVariable('automatic_updates_enabled', $config['db']['auto_update'], 'boolean');
$html->setVariable('refresh_navigation_frame', isset($database_update_executed) && $database_update_executed, 'boolean');
} catch (Exception $e) {
$messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red');
$fatal_error = true;
}
if (! $fatal_error) {
try {
$current = $database->getCurrentVersion();
$latest = $database->getLatestVersion();
$html->setVariable('current_version', $current, 'integer');
$html->setVariable('latest_version', $latest, 'integer');
$html->setVariable('update_required', $latest > $current, 'boolean');
$html->setVariable('last_update_failed', $config['db']['update_error']['version'] == $current, 'boolean');
if (($current > 0) && ($current < 13) && ($latest >= 13)) { // v12 to v13 was a huge update! show warning!
$messages[] = array('text' => _('Achtung!<br><br>'.
'Das Datenbankupdate auf Version 13 ist sehr umfangreich, es finden sehr viele Veränderungen statt.<br>'.
'Es wird dringend empfohlen, vor dem Update eine Sicherung der Datenbank anzulegen, '.
'damit diese im Fehlerfall wiederhergestellt, und so ein Datenverlust verhindert werden kann.<br>'.
'Die Entwickler von Part-DB übernehmen keinerlei Haftung für Schäden, die durch fehlgeschlagene Updates, '.
'Fehler in der Software oder durch andere Ursachen hervorgerufen werden.'),
'strong' => true, 'color' => 'red');
} elseif (($current > 0) && ($latest > $current)) { // normal update...we will show a hint
$messages[] = array('text' => _('Hinweis:<br>'.
'Es wird dringend empfohlen, vor jedem Datenbankupdate eine Sicherung der Datenbank anzulegen.<br>'.
'Die Entwickler von Part-DB übernehmen keinerlei Haftung für Schäden, die durch fehlgeschlagene Updates, '.
'Fehler in der Software oder durch andere Ursachen hervorgerufen werden.'),
'strong' => true, 'color' => 'red');
}
if (!$current_user->canDo(PermissionManager::DATABASE, DatabasePermission::READ_DB_SETTINGS)
&& !$current_user->canDo(PermissionManager::DATABASE, DatabasePermission::SEE_STATUS)) {
//Throw message.
$current_user->tryDo(PermissionManager::DATABASE, DatabasePermission::SEE_STATUS);
}
} catch (Exception $e) {
$messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red');
$fatal_error = true;
}
}
if (!$fatal_error) {
try {
$size = $database->getDatabaseSize();
$html->setVariable('db_size', $size);
} catch (Exception $e) {
$messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red');
}
}
try {
$html->setVariable('hide_status', $fatal_error, 'boolean');
if (isset($current_user) && is_object($current_user)) {
$html->setVariable('can_status', $current_user->canDo(PermissionManager::DATABASE, DatabasePermission::SEE_STATUS));
$html->setVariable('can_read_db_settings', $current_user->canDo(PermissionManager::DATABASE, DatabasePermission::READ_DB_SETTINGS));
$html->setVariable('can_update', $current_user->canDo(PermissionManager::DATABASE, DatabasePermission::UPDATE_DB));
$html->setVariable('can_edit_db_settings', $current_user->canDo(PermissionManager::DATABASE, DatabasePermission::WRITE_DB_SETTINGS));
} else { //In case no user could be created (DB Error), then allow everything.
$html->setVariable('can_status', true);
$html->setVariable('can_read_db_settings', true);
$html->setVariable('can_update', true);
$html->setVariable('can_edit_db_settings', true);
}
} catch (Exception $e) {
$messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red');
$fatal_error = true;
}
/********************************************************************************
*
* Generate HTML Output
*
*********************************************************************************/
//If a ajax version is requested, say this the template engine.
if (isset($_REQUEST['ajax'])) {
$html->setVariable('ajax_request', true);
}
// an empty $reload_link means that the reload-button won't be visible
$reload_link = ($fatal_error || isset($database_update_executed)) ? 'system_database.php' : '';
$html->printHeader($messages, $reload_link);
// we don't hide the site content if no user could be created, so the db connection could be repaired.
if (!$fatal_error || !isset($current_user)) {
$html->printTemplate('system_database');
}
$html->printFooter();