-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
start_session.php
341 lines (282 loc) · 14.1 KB
/
start_session.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php /** @noinspection PhpIncludeInspection */
/*
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
*/
/**
* @file start_session.php
*
* @brief This file must be included in every PHP file which produces HTML output!
* This files set various things needed for Part-DB
*/
// set HTTP charset to UTF-8
header('Content-type: text/html; charset=utf-8');
$BASE_tmp = str_replace('\\', '/', __DIR__); // temporary base path of Part-DB, without slash at the end
include_once $BASE_tmp.'/inc/lib.start_session.php';
/********************************************************************************
*
* define an exception handler for uncaught exceptions
*
*********************************************************************************/
set_exception_handler('exception_handler');
/********************************************************************************
*
* include config files
*
*********************************************************************************/
include_once $BASE_tmp.'/inc/config_defaults.php'; // first, we load all default values of the $config array...
if (file_exists($BASE_tmp.'/data/config.php') && is_readable($BASE_tmp.'/data/config.php')) {
include_once $BASE_tmp.'/data/config.php';
} // ...and then we overwrite them with the user settings, if they exist
if (!empty($manual_config)) { // $manual_config is defined in "config_defaults.php" and can be filled in "config.php"
$config = array_replace_recursive($config, $manual_config);
} // if there are manual configs, add them to $config
/********************************************************************************
*
* define directory constants of the part-db installation
*
* please note: we always use slashes, even if the script runs on Windows!
*
* If the paths BASE, DOCUMENT_ROOT or BASE_RELATIVE are not correct,
* the user can set them manually in his config.php.
* Example how to define it in the config.php: $manual_config['BASE'] = '/my/base';
*
*********************************************************************************/
// directory to the part-db installation, without slash at the end
// Example (UNIX/Linux): "/var/www/part-db"
// Example (Windows): "C:/wamp/www/part-db"
if (isset($config['BASE'])) {
define('BASE', $config['BASE']);
} else {
define('BASE', $BASE_tmp);
}
/** @const BASE Directory to the part-db installation, without slash at the end
* Example (UNIX/Linux): "/var/www/part-db"
* Example (Windows): "C:/wamp/www/part-db"
*/
// server-directory without slash at the end
// Example (UNIX/Linux): "/var/www"
// Example (Windows): "C:/wamp/www"
if (isset($config['DOCUMENT_ROOT'])) {
define('DOCUMENT_ROOT', $config['DOCUMENT_ROOT']);
} elseif (isset($_SERVER['DOCUMENT_ROOT'])) {
define('DOCUMENT_ROOT', rtrim(str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']), '/'));
} elseif (isset($_SERVER['SCRIPT_FILENAME'], $_SERVER['PHP_SELF'])) {
define('DOCUMENT_ROOT', rtrim(str_replace('\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF'])))));
} elseif (isset($_SERVER['PATH_TRANSLATED'], $_SERVER['PHP_SELF'])) {
define('DOCUMENT_ROOT', rtrim(str_replace('\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0-strlen($_SERVER['PHP_SELF'])))));
} else {
$messages = _('Die Konstante "DOCUMENT_ROOT" konnte auf Ihrem Server nicht ermittelt werden.<br>'.
'Bitte definieren Sie diese Konstante manuell in Ihrer Konfigurationsdatei "data/config.php".');
printMessagesWithoutTemplate(_('Part-DB'), _('DOCUMENT_ROOT kann nicht ermittelt werden'), $messages);
exit;
}
/** @const DOCUMENT_ROOT Server-directory without slash at the end
* Example (UNIX/Linux): "/var/www"
* Example (Windows): "C:/wamp/www"
*/
// the part-db installation directory without document root, without slash at the end
// Example (UNIX/Linux): "/part-db"
// Example (Windows): "/part-db"
if (isset($config['BASE_RELATIVE'])) {
define('BASE_RELATIVE', $config['BASE_RELATIVE']);
} elseif (mb_strpos(BASE, DOCUMENT_ROOT) === false) { // workaround for STRATO servers, see german post on uC.net:
define('BASE_RELATIVE', '.');
} // http://www.mikrocontroller.net/topic/269289#3152928
else {
define('BASE_RELATIVE', str_replace(DOCUMENT_ROOT, '', BASE));
}
/**
* @const BASE_DATA The location of the data folder, where Part-DB can write data (without slash)
*/
define('BASE_DATA', BASE . '/data');
/** @const BASE_RELATIVE Server-directory without slash at the end
* Example (UNIX/Linux): "/part-db"
* Example (Windows): "/part-db"
*/
// for debugging uncomment these lines:
//print 'BASE = "'.BASE.'"<br>';
//print 'DOCUMENT_ROOT = "'.DOCUMENT_ROOT.'"<br>';
//print 'BASE_RELATIVE = "'.BASE_RELATIVE.'"<br>';
//print 'DIRECTORY_SEPARATOR = "'.DIRECTORY_SEPARATOR.'"<br>';
//exit;
mb_internal_encoding(/*$config['html']['http_charset']*/ 'UTF-8');
mb_regex_encoding('UTF-8');
date_default_timezone_set($config['timezone']);
ownSetlocale(LC_ALL, $config['language']);
//Set gettext locale for PHP
$domain = 'php';
bindtextdomain($domain, BASE . '/locale');
textdomain($domain);
/********************************************************************************
*
* make some checks
*
*********************************************************************************/
$messages = checkRequirements();
if (count($messages) > 0) {
printMessagesWithoutTemplate(
'Part-DB',
_('Mindestanforderungen von Part-DB nicht erfüllt!'),
'<span><strong>•' .implode('<br>•', $messages). '</strong></span><br><br>' .
sprintf(_('Nähere Informationen gibt es in der <a target="_blank" href="%s">Dokumentation</a>.'), _('https://github.com/jbtronics/Part-DB/wiki/Anforderungen'))
);
exit;
}
$messages = checkFilePermissions();
if (count($messages) > 0) {
$message = '<strong><span class="text-danger">';
foreach ($messages as $msg) {
$message .= '•'.$msg.'<br>';
}
$message .= '</span></strong><br><br>';
$message .= sprintf(_('Nähere Informationen gibt es in der <a target="_blank" href="%s">Dokumentation</a>.'), _('https://github.com/jbtronics/Part-DB/wiki/Installation'));
$message .= '<br><br>';
$message .= _('<form action="" method="post"><button class="btn btn-primary" type="submit" value="Seite neu laden">Seite neu laden</button></form>');
printMessagesWithoutTemplate('Part-DB', _('Anpassung der Rechte von Verzeichnissen und Dateien'), $message);
exit;
// please note: the messages and the "exit;" here are very important, we mustn't continue the script!
// the reasen is: if the config.php is not readable, the array $config is now not loaded successfully.
}
$message = checkIfConfigIsValid();
if (is_string($message)) {
printMessagesWithoutTemplate(
'Part-DB',
_('Ihre config.php ist fehlerhaft!'),
sprintf(_('Nähere Informationen gibt es in der <a target="_blank" href="%s">Dokumentation</a>.'), _('https://github.com/jbtronics/Part-DB/wiki/Installation')).
_('<form action="" method="post"><button class="btn btn-primary" type="submit" value="Seite neu laden">Seite neu laden</button></form>')
);
exit;
}
$messages = checkComposerFolder();
if (count($messages) > 0) {
$message = _("<b>Part-DB benutzt den PHP Abhängikeitsmanager <a href='https://getcomposer.org/' target='_blank'>Composer</a>" .
' um benötigte Bibliotheken bereitzustellen.<br> Bevor sie Part-DB nutzen können müssen sie diese' .
' Bibliotheken mit <code>php composer.phar install</code> im Hauptverzeichnis von Part-DB' .
' installiert werden. <br> Sollten sie keine Möglichkeit haben, auf ihrem Server Konsolenbefehle' .
' auszuführen, dann benutzen kopieren sie den vendor/ Ordner, aus einem mit composer eingerichteten ' .
' Part-DB oder ein speziellen Release benutzen, der die Abhängikeiten mitliefert.</b><br>');
$message .= sprintf(_('Nähere Informationen gibt es in der <a target="_blank" href="%s">Dokumentation</a>.'), _('https://github.com/jbtronics/Part-DB/wiki/Installation'));
$message .= '<br><ul>';
foreach ($messages as $msg) {
$message .= '<li>'.$msg.'</li>';
}
$message .= '</ul>';
//$message .= 'Nähere Informationen zu den Dateirechten gibt es in der <a target="_blank" href="' .
// 'https://github.com/jbtronics/Part-DB/wiki/Installation">Dokumentation</a>.<br><br>';
$message .= _('<form action="" method="post"><button class="btn btn-primary" type="submit" value="Seite neu laden">Seite neu laden</button></form>');
printMessagesWithoutTemplate('Part-DB', 'Benötigte Bibliotheken fehlen!', $message);
exit;
// please note: the messages and the "exit;" here are very important, we mustn't continue the script!
// the reasen is: if the config.php is not readable, the array $config is now not loaded successfully.
}
/********************************************************************************
*
* update the config.php if the system is newer than the user's config.php
*
*********************************************************************************/
if (($config['system']['current_config_version'] < $config['system']['latest_config_version'])
&& file_exists(BASE.'/data/config.php') && is_readable(BASE.'/data/config.php')
&& (filesize(BASE.'/data/config.php') > 0)) {
include_once BASE.'/updates/config_update_steps.php';
try {
$update_messages = update_users_config_php();
$message = '<strong><span style="color: darkgreen; ">'._('Ihre config.php wurde erfolgreich aktualisiert!').'</span></strong><br><br>' .
_('Es kann sein, dass jetzt der Installationsassistent startet, '.
'um noch einige neue Einstellungen zu tätigen.') . '<br><br>';
if (count($update_messages) > 0) {
$message .= '<strong><span style="color: red; ">';
foreach ($update_messages as $text) {
$message .= '•'.$text.'<br>';
}
$message .= '</span></strong><br>';
}
} catch (Exception $e) {
$message = '<strong><span class="text-danger">'._('Es gab ein Fehler bei der Aktualisierung ihrer config.php:').'<br><br>' .
nl2br($e->getMessage()). '</span></strong><br><br>';
}
$message .= _('<form action="" method="post"><button class="btn btn-primary" type="submit" value="Seite neu laden">Seite neu laden</button></form>');
printMessagesWithoutTemplate('Part-DB', _('Aktualisierung ihrer config.php'), $message);
exit;
}
$config['html']['http_charset'] = 'utf-8'; ///< @todo remove this later; see config_defaults.php
/********************************************************************************
*
* start session
*
*********************************************************************************/
if (isset($config['user']['gc_maxlifetime']) && $config['user']['gc_maxlifetime'] > 0) {
@ini_set('session.gc_maxlifetime', $config['user']['gc_maxlifetime']);
}
session_name('Part-DB');
session_start();
session_write_close();
/********************************************************************************
*
* set internal encoding / timezone / locale / error reporting
*
*********************************************************************************/
if ($config['debug']['enable'] && (! $config['debug']['template_debugging_enable'])) { // template debugging produces a lot of warnings!
error_reporting(E_ALL & ~E_STRICT);
@ini_set('display_errors', 1);
//Dont show errors because of function override in php7
if (PHP_MAJOR_VERSION >= 7) {
set_error_handler(function ($errno, $errstr) {
return strncmp($errstr, 'Declaration of', 14) === 0;
}, E_WARNING);
}
} else {
@ini_set('display_errors', 0);
}
/********************************************************************************
*
* include libraries
*
*********************************************************************************/
include_once BASE.'/inc/lib.functions.php';
include_once BASE.'/inc/lib.debug.php';
include_once BASE.'/inc/lib.php';
/********************************************************************************
*
* Register Autoloaders
*
********************************************************************************/
//Include Composer autoloader
require_once BASE . '/vendor/autoload.php';
//Check if Klass exists, and debugging is enabled, then activate Whoops Handler
if (class_exists("\Whoops\Run") && $config['debug']['enable'] &&
(PHP_MAJOR_VERSION >= 7 || PHP_MINOR_VERSION >= 6)) {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();
}
/**************************************************************************************
* Try to set settings that are only apply for the current user.
**************************************************************************************/
global $user_config;
$user_config = array();
$user_config['theme'] = '';
try {
$current_user = \PartDB\User::getLoggedInUser();
$user_config['theme'] = $current_user->getTheme() ?? '';
ownSetlocale(LC_ALL, $current_user->getLanguage());
date_default_timezone_set($config['timezone']);
} catch (Exception $ex) {
//Ignore all errors.
//TODO: Log the errors
}