1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Bootstrap phpMyFAQ. |
5
|
|
|
* |
6
|
|
|
* PHP Version 5.5 |
7
|
|
|
* |
8
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License, |
9
|
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can |
10
|
|
|
* obtain one at http://mozilla.org/MPL/2.0/. |
11
|
|
|
* |
12
|
|
|
* @category phpMyFAQ |
13
|
|
|
* @author Thorsten Rinne <[email protected]> |
14
|
|
|
* @copyright 2012-2016 phpMyFAQ Team |
15
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
16
|
|
|
* @link http://www.phpmyfaq.de |
17
|
|
|
* @since 2012-03-07 |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use Symfony\Component\ClassLoader\UniversalClassLoader; |
21
|
|
|
use Symfony\Component\ClassLoader\Psr4ClassLoader; |
22
|
|
|
use Elasticsearch\ClientBuilder; |
23
|
|
|
|
24
|
|
|
// |
25
|
|
|
// Debug mode: |
26
|
|
|
// - false debug mode disabled |
27
|
|
|
// - true debug mode enabled |
28
|
|
|
// |
29
|
|
|
define('DEBUG', false); |
30
|
|
|
if (DEBUG) { |
31
|
|
|
ini_set('display_errors', 1); |
32
|
|
|
ini_set('display_startup_errors', 1); |
33
|
|
|
error_reporting(E_ALL | E_STRICT); |
34
|
|
|
} else { |
35
|
|
|
error_reporting(0); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!defined('IS_VALID_PHPMYFAQ')) { |
39
|
|
|
exit(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// |
43
|
|
|
// Fix the PHP include path if PMF is running under a "strange" PHP configuration |
44
|
|
|
// |
45
|
|
|
$foundCurrPath = false; |
46
|
|
|
$includePaths = explode(PATH_SEPARATOR, ini_get('include_path')); |
47
|
|
|
$i = 0; |
48
|
|
|
while ((!$foundCurrPath) && ($i < count($includePaths))) { |
49
|
|
|
if ('.' == $includePaths[$i]) { |
50
|
|
|
$foundCurrPath = true; |
51
|
|
|
} |
52
|
|
|
++$i; |
53
|
|
|
} |
54
|
|
|
if (!$foundCurrPath) { |
55
|
|
|
ini_set('include_path', '.'.PATH_SEPARATOR.ini_get('include_path')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// |
59
|
|
|
// Tweak some PHP configuration values |
60
|
|
|
// Warning: be sure the server has enough memory and stack for PHP |
61
|
|
|
// |
62
|
|
|
ini_set('pcre.backtrack_limit', 100000000); |
63
|
|
|
ini_set('pcre.recursion_limit', 100000000); |
64
|
|
|
|
65
|
|
|
// |
66
|
|
|
// Check if multisite/multisite.php exist for Multisite support |
67
|
|
|
// |
68
|
|
|
if (file_exists(__DIR__.'/../multisite/multisite.php') && 'cli' !== PHP_SAPI) { |
69
|
|
|
require __DIR__.'/../multisite/multisite.php'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// |
73
|
|
|
// The root directory |
74
|
|
|
// |
75
|
|
|
if (!defined('PMF_ROOT_DIR')) { |
76
|
|
|
define('PMF_ROOT_DIR', dirname(__DIR__)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// |
80
|
|
|
// Read configuration and constants |
81
|
|
|
// |
82
|
|
|
if (!defined('PMF_MULTI_INSTANCE_CONFIG_DIR')) { |
83
|
|
|
// Single instance configuration |
84
|
|
|
define('PMF_CONFIG_DIR', dirname(__DIR__).'/config'); |
85
|
|
|
} else { |
86
|
|
|
// Multi instance configuration |
87
|
|
|
define('PMF_CONFIG_DIR', PMF_MULTI_INSTANCE_CONFIG_DIR); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// |
91
|
|
|
// Check if config/database.php exist -> if not, redirect to installer |
92
|
|
|
// |
93
|
|
View Code Duplication |
if (!file_exists(PMF_CONFIG_DIR.'/database.php') && !file_exists(PMF_ROOT_DIR.'/inc/data.php')) { |
94
|
|
|
header('Location: setup/index.php'); |
95
|
|
|
exit(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (file_exists(PMF_ROOT_DIR.'/inc/data.php')) { |
99
|
|
|
require PMF_ROOT_DIR.'/inc/data.php'; |
100
|
|
|
} else { |
101
|
|
|
require PMF_CONFIG_DIR.'/database.php'; |
102
|
|
|
} |
103
|
|
|
require PMF_CONFIG_DIR.'/constants.php'; |
104
|
|
|
|
105
|
|
|
/* |
106
|
|
|
* The include directory |
107
|
|
|
*/ |
108
|
|
|
define('PMF_INCLUDE_DIR', __DIR__); |
109
|
|
|
|
110
|
|
|
/* |
111
|
|
|
* The directory where the translations reside |
112
|
|
|
*/ |
113
|
|
|
define('PMF_LANGUAGE_DIR', dirname(__DIR__).'/lang'); |
114
|
|
|
|
115
|
|
|
// |
116
|
|
|
// Setting up PSR-0 autoloader for Symfony Components |
117
|
|
|
// |
118
|
|
|
require PMF_INCLUDE_DIR.'/libs/Symfony/Component/ClassLoader/UniversalClassLoader.php'; |
119
|
|
|
|
120
|
|
|
$loader = new UniversalClassLoader(); |
121
|
|
|
$loader->registerNamespace('Symfony', PMF_INCLUDE_DIR.'/libs'); |
122
|
|
|
$loader->registerPrefix('PMF_', PMF_INCLUDE_DIR); |
123
|
|
|
$loader->register(); |
124
|
|
|
|
125
|
|
|
require PMF_INCLUDE_DIR.'/libs/parsedown/Parsedown.php'; |
126
|
|
|
require PMF_INCLUDE_DIR.'/libs/parsedown/ParsedownExtra.php'; |
127
|
|
|
|
128
|
|
|
// |
129
|
|
|
// Set the error handler to our pmf_error_handler() function |
130
|
|
|
// |
131
|
|
|
set_error_handler('pmf_error_handler'); |
132
|
|
|
|
133
|
|
|
// |
134
|
|
|
// Create a database connection |
135
|
|
|
// |
136
|
|
|
try { |
137
|
|
|
PMF_Db::setTablePrefix($DB['prefix']); |
138
|
|
|
$db = PMF_Db::factory($DB['type']); |
139
|
|
|
$db->connect($DB['server'], $DB['user'], $DB['password'], $DB['db']); |
140
|
|
|
} catch (PMF_Exception $e) { |
141
|
|
|
PMF_Db::errorPage($e->getMessage()); |
142
|
|
|
exit(-1); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// |
146
|
|
|
// Fetch the configuration and add the database connection |
147
|
|
|
// |
148
|
|
|
$faqConfig = new PMF_Configuration($db); |
149
|
|
|
$faqConfig->getAll(); |
150
|
|
|
|
151
|
|
|
// |
152
|
|
|
// We always need a valid session! |
153
|
|
|
// |
154
|
|
|
ini_set('session.use_only_cookies', 1); // Avoid any PHP version to move sessions on URLs |
155
|
|
|
ini_set('session.auto_start', 0); // Prevent error to use session_start() if it's active in php.ini |
156
|
|
|
ini_set('session.use_trans_sid', 0); |
157
|
|
|
ini_set('url_rewriter.tags', ''); |
158
|
|
|
|
159
|
|
|
// |
160
|
|
|
// Start the PHP session |
161
|
|
|
// |
162
|
|
|
PMF_Init::cleanRequest(); |
163
|
|
|
if (defined('PMF_SESSION_SAVE_PATH') && !empty(PMF_SESSION_SAVE_PATH)) { |
164
|
|
|
session_save_path(PMF_SESSION_SAVE_PATH); |
165
|
|
|
} |
166
|
|
|
session_start(); |
167
|
|
|
|
168
|
|
|
// |
169
|
|
|
// Connect to LDAP server, when LDAP support is enabled |
170
|
|
|
// |
171
|
|
|
if ($faqConfig->get('security.ldapSupport') && file_exists(PMF_CONFIG_DIR.'/ldap.php') && extension_loaded('ldap')) { |
172
|
|
|
require PMF_CONFIG_DIR.'/constants_ldap.php'; |
173
|
|
|
require PMF_CONFIG_DIR.'/ldap.php'; |
174
|
|
|
$faqConfig->setLdapConfig($PMF_LDAP); |
175
|
|
|
} else { |
176
|
|
|
$ldap = null; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// |
180
|
|
|
// Connect to Elasticsearch if enabled |
181
|
|
|
// |
182
|
|
|
if ($faqConfig->get('search.enableElasticsearch')) { |
183
|
|
|
|
184
|
|
|
require PMF_CONFIG_DIR.'/elasticsearch.php'; |
185
|
|
|
require PMF_CONFIG_DIR.'/constants_elasticsearch.php'; |
186
|
|
|
require PMF_INCLUDE_DIR.'/libs/react/promise/src/functions.php'; |
187
|
|
|
|
188
|
|
|
$psr4Loader = new Psr4ClassLoader(); |
189
|
|
|
$psr4Loader->addPrefix('Elasticsearch', PMF_INCLUDE_DIR.'/libs/elasticsearch/src/Elasticsearch'); |
190
|
|
|
$psr4Loader->addPrefix('GuzzleHttp\\Ring\\', PMF_INCLUDE_DIR.'/libs/guzzlehttp/ringphp/src'); |
191
|
|
|
$psr4Loader->addPrefix('Monolog', PMF_INCLUDE_DIR.'/libs/monolog/src/Monolog'); |
192
|
|
|
$psr4Loader->addPrefix('Psr', PMF_INCLUDE_DIR.'/libs/psr/log/Psr'); |
193
|
|
|
$psr4Loader->addPrefix('React\\Promise\\', PMF_INCLUDE_DIR.'/libs/react/promise/src'); |
194
|
|
|
$psr4Loader->register(); |
195
|
|
|
|
196
|
|
|
$esClient = ClientBuilder::create() |
197
|
|
|
->setHosts($PMF_ES['hosts']) |
198
|
|
|
->build(); |
199
|
|
|
|
200
|
|
|
$faqConfig->setElasticsearch($esClient); |
201
|
|
|
$faqConfig->setElasticsearchConfig($PMF_ES); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// |
205
|
|
|
// Build attachments path |
206
|
|
|
// |
207
|
|
|
$confAttachmentsPath = trim($faqConfig->get('records.attachmentsPath')); |
208
|
|
|
if ('/' == $confAttachmentsPath[0] || preg_match('%^[a-z]:(\\\\|/)%i', $confAttachmentsPath)) { |
209
|
|
|
// If we're here, some windows or unix style absolute path was detected. |
210
|
|
|
define('PMF_ATTACHMENTS_DIR', $confAttachmentsPath); |
211
|
|
|
} else { |
212
|
|
|
// otherwise build the absolute path |
213
|
|
|
$tmp = dirname(__DIR__).DIRECTORY_SEPARATOR.$confAttachmentsPath; |
214
|
|
|
|
215
|
|
|
// Check that nobody is traversing |
216
|
|
|
if (0 === strpos((string) $tmp, dirname(__DIR__))) { |
217
|
|
|
define('PMF_ATTACHMENTS_DIR', $tmp); |
218
|
|
|
} else { |
219
|
|
|
define('PMF_ATTACHMENTS_DIR', false); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// |
224
|
|
|
// Fix if phpMyFAQ is running behind a proxy server |
225
|
|
|
// |
226
|
|
|
if (!isset($_SERVER['HTTP_HOST'])) { |
227
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_SERVER'])) { |
228
|
|
|
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_SERVER']; |
229
|
|
|
} else { |
230
|
|
|
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST']; |
231
|
|
|
}; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// |
235
|
|
|
// Fix undefined server variables in Windows IIS & CGI mode |
236
|
|
|
// |
237
|
|
|
if (!isset($_SERVER['SCRIPT_NAME'])) { |
238
|
|
|
if (isset($_SERVER['SCRIPT_FILENAME'])) { |
239
|
|
|
$_SERVER['SCRIPT_NAME'] = $_SERVER['SCRIPT_FILENAME']; |
240
|
|
|
} elseif (isset($_SERVER['PATH_TRANSLATED'])) { |
241
|
|
|
$_SERVER['SCRIPT_NAME'] = $_SERVER['PATH_TRANSLATED']; |
242
|
|
|
} elseif (isset($_SERVER['PATH_INFO'])) { |
243
|
|
|
$_SERVER['SCRIPT_NAME'] = $_SERVER['PATH_INFO']; |
244
|
|
|
} elseif (isset($_SERVER['SCRIPT_URL'])) { |
245
|
|
|
$_SERVER['SCRIPT_NAME'] = $_SERVER['SCRIPT_URL']; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// |
250
|
|
|
// phpMyFAQ exception log |
251
|
|
|
// |
252
|
|
|
$pmfExeptions = []; |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* phpMyFAQ custom error handler function, also to prevent the disclosure of |
256
|
|
|
* potential sensitive data. |
257
|
|
|
* |
258
|
|
|
* @param int $level The level of the error raised. |
259
|
|
|
* @param string $message The error message. |
260
|
|
|
* @param string $filename The filename that the error was raised in. |
261
|
|
|
* @param int $line The line number the error was raised at. |
262
|
|
|
* @param mixed $context It optionally contains an array of every variable |
263
|
|
|
* that existed in the scope the error was triggered in. |
264
|
|
|
* |
265
|
|
|
* @return boolean|null |
266
|
|
|
*/ |
267
|
|
|
function pmf_error_handler($level, $message, $filename, $line, $context) |
268
|
|
|
{ |
269
|
|
|
// Sanity check |
270
|
|
|
// Note: when DEBUG mode is true we want to track any error! |
271
|
|
|
if ( |
272
|
|
|
// 1. the @ operator sets the PHP's error_reporting() value to 0 |
273
|
|
|
(!DEBUG && (0 == error_reporting())) |
274
|
|
|
// 2. Honor the value of PHP's error_reporting() function |
275
|
|
|
|| (!DEBUG && (0 == ($level & error_reporting()))) |
276
|
|
|
) { |
277
|
|
|
// Do nothing |
278
|
|
|
return true; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
// Cleanup potential sensitive data |
282
|
|
|
$filename = (DEBUG ? $filename : basename($filename)); |
283
|
|
|
|
284
|
|
|
$errorTypes = array( |
285
|
|
|
E_ERROR => 'error', |
286
|
|
|
E_WARNING => 'warning', |
287
|
|
|
E_PARSE => 'parse error', |
288
|
|
|
E_NOTICE => 'notice', |
289
|
|
|
E_CORE_ERROR => 'code error', |
290
|
|
|
E_CORE_WARNING => 'core warning', |
291
|
|
|
E_COMPILE_ERROR => 'compile error', |
292
|
|
|
E_COMPILE_WARNING => 'compile warning', |
293
|
|
|
E_USER_ERROR => 'user error', |
294
|
|
|
E_USER_WARNING => 'user warning', |
295
|
|
|
E_USER_NOTICE => 'user notice', |
296
|
|
|
E_STRICT => 'strict warning', |
297
|
|
|
E_RECOVERABLE_ERROR => 'recoverable error', |
298
|
|
|
E_DEPRECATED => 'deprecated warning', |
299
|
|
|
E_USER_DEPRECATED => 'user deprecated warning', |
300
|
|
|
); |
301
|
|
|
$errorType = 'unknown error'; |
302
|
|
|
if (isset($errorTypes[$level])) { |
303
|
|
|
$errorType = $errorTypes[$level]; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
// Custom error message |
307
|
|
|
$errorMessage = sprintf( |
308
|
|
|
'<br><strong>phpMyFAQ %s</strong> [%s]: %s in <strong>%s</strong> on line <strong>%d</strong><br>', |
309
|
|
|
$errorType, |
310
|
|
|
$level, |
311
|
|
|
$message, |
312
|
|
|
$filename, |
313
|
|
|
$line |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
if (ini_get('display_errors')) { |
317
|
|
|
print $errorMessage; |
318
|
|
|
} |
319
|
|
|
if (ini_get('log_errors')) { |
320
|
|
|
error_log(sprintf('phpMyFAQ %s: %s in %s on line %d', |
321
|
|
|
$errorType, |
322
|
|
|
$message, |
323
|
|
|
$filename, |
324
|
|
|
$line) |
325
|
|
|
); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
switch ($level) { |
329
|
|
|
// Blocking errors |
330
|
|
|
case E_ERROR: |
331
|
|
|
case E_PARSE: |
332
|
|
|
case E_CORE_ERROR: |
333
|
|
|
case E_COMPILE_ERROR: |
334
|
|
|
case E_USER_ERROR: |
335
|
|
|
// Prevent processing any more PHP scripts |
336
|
|
|
exit(); |
337
|
|
|
break; |
338
|
|
|
// Not blocking errors |
339
|
|
|
default: |
340
|
|
|
break; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return true; |
344
|
|
|
} |
345
|
|
|
|