Passed
Push — release-2.1 ( 0e5bfc...908430 )
by Mathias
07:53 queued 12s
created

sm_temp_dir_option()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 16
rs 9.9666
1
<?php
2
3
/**
4
 * This file contains functions that are specifically done by administrators.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2021 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC4
14
 */
15
16
use SMF\Cache\CacheApiInterface;
17
18
if (!defined('SMF'))
19
	die('No direct access...');
20
21
/**
22
 * Get a list of versions that are currently installed on the server.
23
 *
24
 * @param array $checkFor An array of what to check versions for - can contain one or more of 'gd', 'imagemagick', 'db_server', 'phpa', 'memcache', 'php' or 'server'
25
 * @return array An array of versions (keys are same as what was in $checkFor, values are the versions)
26
 */
27
function getServerVersions($checkFor)
28
{
29
	global $txt, $db_connection, $sourcedir, $smcFunc, $modSettings;
30
31
	loadLanguage('Admin');
32
	loadLanguage('ManageSettings');
33
34
	$versions = array();
35
36
	// Is GD available?  If it is, we should show version information for it too.
37
	if (in_array('gd', $checkFor) && function_exists('gd_info'))
38
	{
39
		$temp = gd_info();
40
		$versions['gd'] = array('title' => $txt['support_versions_gd'], 'version' => $temp['GD Version']);
41
	}
42
43
	// Why not have a look at ImageMagick? If it's installed, we should show version information for it too.
44
	if (in_array('imagemagick', $checkFor) && (class_exists('Imagick') || function_exists('MagickGetVersionString')))
45
	{
46
		if (class_exists('Imagick'))
47
		{
48
			$temp = New Imagick;
49
			$temp2 = $temp->getVersion();
50
			$im_version = $temp2['versionString'];
51
			$extension_version = 'Imagick ' . phpversion('Imagick');
52
		}
53
		else
54
		{
55
			$im_version = MagickGetVersionString();
56
			$extension_version = 'MagickWand ' . phpversion('MagickWand');
57
		}
58
59
		// We already know it's ImageMagick and the website isn't needed...
60
		$im_version = str_replace(array('ImageMagick ', ' https://www.imagemagick.org'), '', $im_version);
61
		$versions['imagemagick'] = array('title' => $txt['support_versions_imagemagick'], 'version' => $im_version . ' (' . $extension_version . ')');
62
	}
63
64
	// Now lets check for the Database.
65
	if (in_array('db_server', $checkFor))
66
	{
67
		db_extend();
68
		if (!isset($db_connection) || $db_connection === false)
69
		{
70
			loadLanguage('Errors');
71
			trigger_error($txt['get_server_versions_no_database'], E_USER_NOTICE);
72
		}
73
		else
74
		{
75
			$versions['db_engine'] = array(
76
				'title' => sprintf($txt['support_versions_db_engine'], $smcFunc['db_title']),
77
				'version' => $smcFunc['db_get_vendor'](),
78
			);
79
			$versions['db_server'] = array(
80
				'title' => sprintf($txt['support_versions_db'], $smcFunc['db_title']),
81
				'version' => $smcFunc['db_get_version'](),
82
			);
83
		}
84
	}
85
86
	// Check to see if we have any accelerators installed.
87
	require_once($sourcedir . '/ManageServer.php');
88
	$detected = loadCacheAPIs();
89
90
	/* @var CacheApiInterface $cache_api */
91
	foreach ($detected as $class_name => $cache_api)
92
	{
93
		$class_name_txt_key = strtolower($cache_api->getImplementationClassKeyName());
94
95
		if (in_array($class_name_txt_key, $checkFor))
96
			$versions[$class_name_txt_key] = array(
97
				'title' => isset($txt[$class_name_txt_key . '_cache']) ?
98
					$txt[$class_name_txt_key . '_cache'] : $class_name,
99
				'version' => $cache_api->getVersion(),
100
			);
101
	}
102
103
	if (in_array('php', $checkFor))
104
		$versions['php'] = array(
105
			'title' => 'PHP',
106
			'version' => PHP_VERSION,
107
			'more' => '?action=admin;area=serversettings;sa=phpinfo',
108
		);
109
110
	if (in_array('server', $checkFor))
111
		$versions['server'] = array(
112
			'title' => $txt['support_versions_server'],
113
			'version' => $_SERVER['SERVER_SOFTWARE'],
114
		);
115
116
	return $versions;
117
}
118
119
/**
120
 * Search through source, theme and language files to determine their version.
121
 * Get detailed version information about the physical SMF files on the server.
122
 *
123
 * - the input parameter allows to set whether to include SSI.php and whether
124
 *   the results should be sorted.
125
 * - returns an array containing information on source files, templates and
126
 *   language files found in the default theme directory (grouped by language).
127
 *
128
 * @param array &$versionOptions An array of options. Can contain one or more of 'include_ssi', 'include_subscriptions', 'include_tasks' and 'sort_results'
129
 * @return array An array of file version info.
130
 */
131
function getFileVersions(&$versionOptions)
132
{
133
	global $boarddir, $sourcedir, $settings, $tasksdir;
134
135
	// Default place to find the languages would be the default theme dir.
136
	$lang_dir = $settings['default_theme_dir'] . '/languages';
137
138
	$version_info = array(
139
		'file_versions' => array(),
140
		'default_template_versions' => array(),
141
		'template_versions' => array(),
142
		'default_language_versions' => array(),
143
		'tasks_versions' => array(),
144
	);
145
146
	// Find the version in SSI.php's file header.
147
	if (!empty($versionOptions['include_ssi']) && file_exists($boarddir . '/SSI.php'))
148
	{
149
		$fp = fopen($boarddir . '/SSI.php', 'rb');
150
		$header = fread($fp, 4096);
151
		fclose($fp);
152
153
		// The comment looks rougly like... that.
154
		if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
155
			$version_info['file_versions']['SSI.php'] = $match[1];
156
		// Not found!  This is bad.
157
		else
158
			$version_info['file_versions']['SSI.php'] = '??';
159
	}
160
161
	// Do the paid subscriptions handler?
162
	if (!empty($versionOptions['include_subscriptions']) && file_exists($boarddir . '/subscriptions.php'))
163
	{
164
		$fp = fopen($boarddir . '/subscriptions.php', 'rb');
165
		$header = fread($fp, 4096);
166
		fclose($fp);
167
168
		// Found it?
169
		if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
170
			$version_info['file_versions']['subscriptions.php'] = $match[1];
171
		// If we haven't how do we all get paid?
172
		else
173
			$version_info['file_versions']['subscriptions.php'] = '??';
174
	}
175
176
	// Load all the files in the Sources directory, except this file and the redirect.
177
	$sources_dir = dir($sourcedir);
178
	while ($entry = $sources_dir->read())
179
	{
180
		if (substr($entry, -4) === '.php' && !is_dir($sourcedir . '/' . $entry) && $entry !== 'index.php')
181
		{
182
			// Read the first 4k from the file.... enough for the header.
183
			$fp = fopen($sourcedir . '/' . $entry, 'rb');
184
			$header = fread($fp, 4096);
185
			fclose($fp);
186
187
			// Look for the version comment in the file header.
188
			if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
189
				$version_info['file_versions'][$entry] = $match[1];
190
			// It wasn't found, but the file was... show a '??'.
191
			else
192
				$version_info['file_versions'][$entry] = '??';
193
		}
194
	}
195
	$sources_dir->close();
196
197
	// Load all the files in the tasks directory.
198
	if (!empty($versionOptions['include_tasks']))
199
	{
200
		$tasks_dir = dir($tasksdir);
201
		while ($entry = $tasks_dir->read())
202
		{
203
			if (substr($entry, -4) === '.php' && !is_dir($tasksdir . '/' . $entry) && $entry !== 'index.php')
204
			{
205
				// Read the first 4k from the file.... enough for the header.
206
				$fp = fopen($tasksdir . '/' . $entry, 'rb');
207
				$header = fread($fp, 4096);
208
				fclose($fp);
209
210
				// Look for the version comment in the file header.
211
				if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
212
					$version_info['tasks_versions'][$entry] = $match[1];
213
				// It wasn't found, but the file was... show a '??'.
214
				else
215
					$version_info['tasks_versions'][$entry] = '??';
216
			}
217
		}
218
		$tasks_dir->close();
219
	}
220
221
	// Load all the files in the default template directory - and the current theme if applicable.
222
	$directories = array('default_template_versions' => $settings['default_theme_dir']);
223
	if ($settings['theme_id'] != 1)
224
		$directories += array('template_versions' => $settings['theme_dir']);
225
226
	foreach ($directories as $type => $dirname)
227
	{
228
		$this_dir = dir($dirname);
229
		while ($entry = $this_dir->read())
230
		{
231
			if (substr($entry, -12) == 'template.php' && !is_dir($dirname . '/' . $entry))
232
			{
233
				// Read the first 768 bytes from the file.... enough for the header.
234
				$fp = fopen($dirname . '/' . $entry, 'rb');
235
				$header = fread($fp, 768);
236
				fclose($fp);
237
238
				// Look for the version comment in the file header.
239
				if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
240
					$version_info[$type][$entry] = $match[1];
241
				// It wasn't found, but the file was... show a '??'.
242
				else
243
					$version_info[$type][$entry] = '??';
244
			}
245
		}
246
		$this_dir->close();
247
	}
248
249
	// Load up all the files in the default language directory and sort by language.
250
	$this_dir = dir($lang_dir);
251
	while ($entry = $this_dir->read())
252
	{
253
		if (substr($entry, -4) == '.php' && $entry != 'index.php' && !is_dir($lang_dir . '/' . $entry))
254
		{
255
			// Read the first 768 bytes from the file.... enough for the header.
256
			$fp = fopen($lang_dir . '/' . $entry, 'rb');
257
			$header = fread($fp, 768);
258
			fclose($fp);
259
260
			// Split the file name off into useful bits.
261
			list ($name, $language) = explode('.', $entry);
262
263
			// Look for the version comment in the file header.
264
			if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '(?:[\s]{2}|\*/)~i', $header, $match) == 1)
265
				$version_info['default_language_versions'][$language][$name] = $match[1];
266
			// It wasn't found, but the file was... show a '??'.
267
			else
268
				$version_info['default_language_versions'][$language][$name] = '??';
269
		}
270
	}
271
	$this_dir->close();
272
273
	// Sort the file versions by filename.
274
	if (!empty($versionOptions['sort_results']))
275
	{
276
		ksort($version_info['file_versions']);
277
		ksort($version_info['default_template_versions']);
278
		ksort($version_info['template_versions']);
279
		ksort($version_info['default_language_versions']);
280
		ksort($version_info['tasks_versions']);
281
282
		// For languages sort each language too.
283
		foreach ($version_info['default_language_versions'] as $language => $dummy)
284
			ksort($version_info['default_language_versions'][$language]);
285
	}
286
	return $version_info;
287
}
288
289
/**
290
 * Update the Settings.php file.
291
 *
292
 * The most important function in this file for mod makers happens to be the
293
 * updateSettingsFile() function, but it shouldn't be used often anyway.
294
 *
295
 * - Updates the Settings.php file with the changes supplied in config_vars.
296
 *
297
 * - Expects config_vars to be an associative array, with the keys as the
298
 *   variable names in Settings.php, and the values the variable values.
299
 *
300
 * - Correctly formats the values using smf_var_export().
301
 *
302
 * - Restores standard formatting of the file, if $rebuild is true.
303
 *
304
 * - Checks for changes to db_last_error and passes those off to a separate
305
 *   handler.
306
 *
307
 * - Creates a backup file and will use it should the writing of the
308
 *   new settings file fail.
309
 *
310
 * - Tries to intelligently trim quotes and remove slashes from string values.
311
 *   This is done for backwards compatibility purposes (old versions of this
312
 *   function expected strings to have been manually escaped and quoted). This
313
 *   behaviour can be controlled by the $keep_quotes parameter.
314
 *
315
 * @param array $config_vars An array of one or more variables to update.
316
 * @param bool|null $keep_quotes Whether to strip slashes & trim quotes from string values. Defaults to auto-detection.
317
 * @param bool $rebuild If true, attempts to rebuild with standard format. Default false.
318
 * @return bool True on success, false on failure.
319
 */
320
function updateSettingsFile($config_vars, $keep_quotes = null, $rebuild = false)
321
{
322
	// In this function we intentionally don't declare any global variables.
323
	// This allows us to work with everything cleanly.
324
325
	static $mtime;
326
327
	// Should we try to unescape the strings?
328
	if (empty($keep_quotes))
329
	{
330
		foreach ($config_vars as $var => $val)
331
		{
332
			if (is_string($val) && ($keep_quotes === false || strpos($val, '\'') === 0 && strrpos($val, '\'') === strlen($val) - 1))
333
				$config_vars[$var] = trim(stripcslashes($val), '\'');
334
		}
335
	}
336
337
	// Updating the db_last_error, then don't mess around with Settings.php
338
	if (isset($config_vars['db_last_error']))
339
	{
340
		updateDbLastError($config_vars['db_last_error']);
341
342
		if (count($config_vars) === 1 && empty($rebuild))
343
			return true;
344
345
		// Make sure we delete this from Settings.php, if present.
346
		$config_vars['db_last_error'] = 0;
347
	}
348
349
	// Rebuilding should not be undertaken lightly, so we're picky about the parameter.
350
	if (!is_bool($rebuild))
0 ignored issues
show
introduced by
The condition is_bool($rebuild) is always true.
Loading history...
351
		$rebuild = false;
352
353
	$mtime = isset($mtime) ? (int) $mtime : (defined('TIME_START') ? TIME_START : $_SERVER['REQUEST_TIME']);
354
355
	/*****************
356
	 * PART 1: Setup *
357
	 *****************/
358
359
	// Typically Settings.php is in $boarddir, but maybe this is a custom setup...
360
	foreach (get_included_files() as $settingsFile)
361
		if (basename($settingsFile) === 'Settings.php')
362
			break;
363
364
	// Fallback in case Settings.php isn't loaded (e.g. while installing)
365
	if (basename($settingsFile) !== 'Settings.php')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $settingsFile seems to be defined by a foreach iteration on line 360. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
366
		$settingsFile = (!empty($GLOBALS['boarddir']) && @realpath($GLOBALS['boarddir']) ? $GLOBALS['boarddir'] : (!empty($_SERVER['SCRIPT_FILENAME']) ? dirname($_SERVER['SCRIPT_FILENAME']) : dirname(__DIR__))) . '/Settings.php';
367
368
	// File not found? Attempt an emergency on-the-fly fix!
369
	if (!file_exists($settingsFile))
370
		@touch($settingsFile);
371
372
	// When was Settings.php last changed?
373
	$last_settings_change = filemtime($settingsFile);
374
375
	// Get the current values of everything in Settings.php.
376
	$settings_vars = get_current_settings($mtime, $settingsFile);
377
378
	// If Settings.php is empty for some reason, see if we can use the backup.
379
	if (empty($settings_vars) && file_exists(dirname($settingsFile) . '/Settings_bak.php'))
380
		$settings_vars = get_current_settings($mtime, dirname($settingsFile) . '/Settings_bak.php');
381
382
	// False means there was a problem with the file and we can't safely continue.
383
	if ($settings_vars === false)
384
		return false;
385
386
	// It works best to set everything afresh.
387
	$new_settings_vars = array_merge($settings_vars, $config_vars);
388
389
	// Are we using UTF-8?
390
	$utf8 = isset($GLOBALS['context']['utf8']) ? $GLOBALS['context']['utf8'] : (isset($GLOBALS['utf8']) ? $GLOBALS['utf8'] : (isset($settings_vars['db_character_set']) ? $settings_vars['db_character_set'] === 'utf8' : false));
391
392
	/*
393
	 * A big, fat array to define properties of all the Settings.php variables.
394
	 *
395
	 * - String keys are used to identify actual variables.
396
	 *
397
	 * - Integer keys are used for content not connected to any particular
398
	 *   variable, such as code blocks or the license block.
399
	 *
400
	 * - The content of the 'text' element is simply printed out, if it is used
401
	 *   at all. Use it for comments or to insert code blocks, etc.
402
	 *
403
	 * - The 'default' element, not surprisingly, gives a default value for
404
	 *   the variable.
405
	 *
406
	 * - The 'type' element defines the expected variable type or types. If
407
	 *   more than one type is allowed, this should be an array listing them.
408
	 *   Types should match the possible types returned by gettype().
409
	 *
410
	 * - If 'raw_default' is true, the default should be printed directly,
411
	 *   rather than being handled as a string. Use it if the default contains
412
	 *   code, e.g. 'dirname(__FILE__)'
413
	 *
414
	 * - If 'required' is true and a value for the variable is undefined,
415
	 *   the update will be aborted. (The only exception is during the SMF
416
	 *   installation process.)
417
	 *
418
	 * - If 'auto_delete' is 1 or true and the variable is empty, the variable
419
	 *   will be deleted from Settings.php. If 'auto_delete' is 0/false/null,
420
	 *   the variable will never be deleted. If 'auto_delete' is 2, behaviour
421
	 *   depends on $rebuild: if $rebuild is true, 'auto_delete' == 2 behaves
422
	 *   like 'auto_delete' == 1; if $rebuild is false, 'auto_delete' == 2
423
	 *   behaves like 'auto_delete' == 0.
424
	 *
425
	 * - The optional 'search_pattern' element defines a custom regular
426
	 *   expression to search for the existing entry in the file. This is
427
	 *   primarily useful for code blocks rather than variables.
428
	 *
429
	 * - The optional 'replace_pattern' element defines a custom regular
430
	 *   expression to decide where the replacement entry should be inserted.
431
	 *   Note: 'replace_pattern' should be avoided unless ABSOLUTELY necessary.
432
	 */
433
	$settings_defs = array(
434
		array(
435
			'text' => implode("\n", array(
436
				'',
437
				'/**',
438
				' * The settings file contains all of the basic settings that need to be present when a database/cache is not available.',
439
				' *',
440
				' * Simple Machines Forum (SMF)',
441
				' *',
442
				' * @package SMF',
443
				' * @author Simple Machines https://www.simplemachines.org',
444
				' * @copyright ' . SMF_SOFTWARE_YEAR . ' Simple Machines and individual contributors',
445
				' * @license https://www.simplemachines.org/about/smf/license.php BSD',
446
				' *',
447
				' * @version ' . SMF_VERSION,
448
				' */',
449
				'',
450
			)),
451
			'search_pattern' => '~/\*\*.*?@package\h+SMF\b.*?\*/\n{0,2}~s',
452
		),
453
		'maintenance' => array(
454
			'text' => implode("\n", array(
455
				'',
456
				'########## Maintenance ##########',
457
				'/**',
458
				' * The maintenance "mode"',
459
				' * Set to 1 to enable Maintenance Mode, 2 to make the forum untouchable. (you\'ll have to make it 0 again manually!)',
460
				' * 0 is default and disables maintenance mode.',
461
				' *',
462
				' * @var int 0, 1, 2',
463
				' * @global int $maintenance',
464
				' */',
465
			)),
466
			'default' => 0,
467
			'type' => 'integer',
468
		),
469
		'mtitle' => array(
470
			'text' => implode("\n", array(
471
				'/**',
472
				' * Title for the Maintenance Mode message.',
473
				' *',
474
				' * @var string',
475
				' * @global int $mtitle',
476
				' */',
477
			)),
478
			'default' => 'Maintenance Mode',
479
			'type' => 'string',
480
		),
481
		'mmessage' => array(
482
			'text' => implode("\n", array(
483
				'/**',
484
				' * Description of why the forum is in maintenance mode.',
485
				' *',
486
				' * @var string',
487
				' * @global string $mmessage',
488
				' */',
489
			)),
490
			'default' => 'Okay faithful users...we\'re attempting to restore an older backup of the database...news will be posted once we\'re back!',
491
			'type' => 'string',
492
		),
493
		'mbname' => array(
494
			'text' => implode("\n", array(
495
				'',
496
				'########## Forum Info ##########',
497
				'/**',
498
				' * The name of your forum.',
499
				' *',
500
				' * @var string',
501
				' */',
502
			)),
503
			'default' => 'My Community',
504
			'type' => 'string',
505
		),
506
		'language' => array(
507
			'text' => implode("\n", array(
508
				'/**',
509
				' * The default language file set for the forum.',
510
				' *',
511
				' * @var string',
512
				' */',
513
			)),
514
			'default' => 'english',
515
			'type' => 'string',
516
		),
517
		'boardurl' => array(
518
			'text' => implode("\n", array(
519
				'/**',
520
				' * URL to your forum\'s folder. (without the trailing /!)',
521
				' *',
522
				' * @var string',
523
				' */',
524
			)),
525
			'default' => 'http://127.0.0.1/smf',
526
			'type' => 'string',
527
		),
528
		'webmaster_email' => array(
529
			'text' => implode("\n", array(
530
				'/**',
531
				' * Email address to send emails from. (like [email protected].)',
532
				' *',
533
				' * @var string',
534
				' */',
535
			)),
536
			'default' => '[email protected]',
537
			'type' => 'string',
538
		),
539
		'cookiename' => array(
540
			'text' => implode("\n", array(
541
				'/**',
542
				' * Name of the cookie to set for authentication.',
543
				' *',
544
				' * @var string',
545
				' */',
546
			)),
547
			'default' => 'SMFCookie11',
548
			'type' => 'string',
549
		),
550
		'auth_secret' => array(
551
			'text' => implode("\n", array(
552
				'/**',
553
				' * Secret key used to create and verify cookies, tokens, etc.',
554
				' * Do not change this unless absolutely necessary, and NEVER share it.',
555
				' *',
556
				' * Note: Changing this will immediately log out all members of your forum',
557
				' * and break the token-based links in all previous email notifications,',
558
				' * among other possible effects.',
559
				' *',
560
				' * @var string',
561
				' */',
562
			)),
563
			'default' => null,
564
			'auto_delete' => 1,
565
			'type' => 'string',
566
		),
567
		'db_type' => array(
568
			'text' => implode("\n", array(
569
				'',
570
				'########## Database Info ##########',
571
				'/**',
572
				' * The database type',
573
				' * Default options: mysql, postgresql',
574
				' *',
575
				' * @var string',
576
				' */',
577
			)),
578
			'default' => 'mysql',
579
			'type' => 'string',
580
		),
581
		'db_port' => array(
582
			'text' => implode("\n", array(
583
				'/**',
584
				' * The database port',
585
				' * 0 to use default port for the database type',
586
				' *',
587
				' * @var int',
588
				' */',
589
			)),
590
			'default' => 0,
591
			'type' => 'integer',
592
		),
593
		'db_server' => array(
594
			'text' => implode("\n", array(
595
				'/**',
596
				' * The server to connect to (or a Unix socket)',
597
				' *',
598
				' * @var string',
599
				' */',
600
			)),
601
			'default' => 'localhost',
602
			'required' => true,
603
			'type' => 'string',
604
		),
605
		'db_name' => array(
606
			'text' => implode("\n", array(
607
				'/**',
608
				' * The database name',
609
				' *',
610
				' * @var string',
611
				' */',
612
			)),
613
			'default' => 'smf',
614
			'required' => true,
615
			'type' => 'string',
616
		),
617
		'db_user' => array(
618
			'text' => implode("\n", array(
619
				'/**',
620
				' * Database username',
621
				' *',
622
				' * @var string',
623
				' */',
624
			)),
625
			'default' => 'root',
626
			'required' => true,
627
			'type' => 'string',
628
		),
629
		'db_passwd' => array(
630
			'text' => implode("\n", array(
631
				'/**',
632
				' * Database password',
633
				' *',
634
				' * @var string',
635
				' */',
636
			)),
637
			'default' => '',
638
			'required' => true,
639
			'type' => 'string',
640
		),
641
		'ssi_db_user' => array(
642
			'text' => implode("\n", array(
643
				'/**',
644
				' * Database user for when connecting with SSI',
645
				' *',
646
				' * @var string',
647
				' */',
648
			)),
649
			'default' => '',
650
			'type' => 'string',
651
		),
652
		'ssi_db_passwd' => array(
653
			'text' => implode("\n", array(
654
				'/**',
655
				' * Database password for when connecting with SSI',
656
				' *',
657
				' * @var string',
658
				' */',
659
			)),
660
			'default' => '',
661
			'type' => 'string',
662
		),
663
		'db_prefix' => array(
664
			'text' => implode("\n", array(
665
				'/**',
666
				' * A prefix to put in front of your table names.',
667
				' * This helps to prevent conflicts',
668
				' *',
669
				' * @var string',
670
				' */',
671
			)),
672
			'default' => 'smf_',
673
			'required' => true,
674
			'type' => 'string',
675
		),
676
		'db_persist' => array(
677
			'text' => implode("\n", array(
678
				'/**',
679
				' * Use a persistent database connection',
680
				' *',
681
				' * @var bool',
682
				' */',
683
			)),
684
			'default' => false,
685
			'type' => 'boolean',
686
		),
687
		'db_error_send' => array(
688
			'text' => implode("\n", array(
689
				'/**',
690
				' * Send emails on database connection error',
691
				' *',
692
				' * @var bool',
693
				' */',
694
			)),
695
			'default' => false,
696
			'type' => 'boolean',
697
		),
698
		'db_mb4' => array(
699
			'text' => implode("\n", array(
700
				'/**',
701
				' * Override the default behavior of the database layer for mb4 handling',
702
				' * null keep the default behavior untouched',
703
				' *',
704
				' * @var null|bool',
705
				' */',
706
			)),
707
			'default' => null,
708
			'type' => array('NULL', 'boolean'),
709
		),
710
		'cache_accelerator' => array(
711
			'text' => implode("\n", array(
712
				'',
713
				'########## Cache Info ##########',
714
				'/**',
715
				' * Select a cache system. You want to leave this up to the cache area of the admin panel for',
716
				' * proper detection of memcached, output_cache, or smf file system',
717
				' * (you can add more with a mod).',
718
				' *',
719
				' * @var string',
720
				' */',
721
			)),
722
			'default' => '',
723
			'type' => 'string',
724
		),
725
		'cache_enable' => array(
726
			'text' => implode("\n", array(
727
				'/**',
728
				' * The level at which you would like to cache. Between 0 (off) through 3 (cache a lot).',
729
				' *',
730
				' * @var int',
731
				' */',
732
			)),
733
			'default' => 0,
734
			'type' => 'integer',
735
		),
736
		'cache_memcached' => array(
737
			'text' => implode("\n", array(
738
				'/**',
739
				' * This is only used for memcache / memcached. Should be a string of \'server:port,server:port\'',
740
				' *',
741
				' * @var array',
742
				' */',
743
			)),
744
			'default' => '',
745
			'type' => 'string',
746
		),
747
		'cachedir' => array(
748
			'text' => implode("\n", array(
749
				'/**',
750
				' * This is only for the \'smf\' file cache system. It is the path to the cache directory.',
751
				' * It is also recommended that you place this in /tmp/ if you are going to use this.',
752
				' *',
753
				' * @var string',
754
				' */',
755
			)),
756
			'default' => 'dirname(__FILE__) . \'/cache\'',
757
			'raw_default' => true,
758
			'type' => 'string',
759
		),
760
		'image_proxy_enabled' => array(
761
			'text' => implode("\n", array(
762
				'',
763
				'########## Image Proxy ##########',
764
				'# This is done entirely in Settings.php to avoid loading the DB while serving the images',
765
				'/**',
766
				' * Whether the proxy is enabled or not',
767
				' *',
768
				' * @var bool',
769
				' */',
770
			)),
771
			'default' => true,
772
			'type' => 'boolean',
773
		),
774
		'image_proxy_secret' => array(
775
			'text' => implode("\n", array(
776
				'/**',
777
				' * Secret key to be used by the proxy',
778
				' *',
779
				' * @var string',
780
				' */',
781
			)),
782
			'default' => 'smfisawesome',
783
			'type' => 'string',
784
		),
785
		'image_proxy_maxsize' => array(
786
			'text' => implode("\n", array(
787
				'/**',
788
				' * Maximum file size (in KB) for individual files',
789
				' *',
790
				' * @var int',
791
				' */',
792
			)),
793
			'default' => 5192,
794
			'type' => 'integer',
795
		),
796
		'boarddir' => array(
797
			'text' => implode("\n", array(
798
				'',
799
				'########## Directories/Files ##########',
800
				'# Note: These directories do not have to be changed unless you move things.',
801
				'/**',
802
				' * The absolute path to the forum\'s folder. (not just \'.\'!)',
803
				' *',
804
				' * @var string',
805
				' */',
806
			)),
807
			'default' => 'dirname(__FILE__)',
808
			'raw_default' => true,
809
			'type' => 'string',
810
		),
811
		'sourcedir' => array(
812
			'text' => implode("\n", array(
813
				'/**',
814
				' * Path to the Sources directory.',
815
				' *',
816
				' * @var string',
817
				' */',
818
			)),
819
			'default' => 'dirname(__FILE__) . \'/Sources\'',
820
			'raw_default' => true,
821
			'type' => 'string',
822
		),
823
		'packagesdir' => array(
824
			'text' => implode("\n", array(
825
				'/**',
826
				' * Path to the Packages directory.',
827
				' *',
828
				' * @var string',
829
				' */',
830
			)),
831
			'default' => 'dirname(__FILE__) . \'/Packages\'',
832
			'raw_default' => true,
833
			'type' => 'string',
834
		),
835
		'tasksdir' => array(
836
			'text' => implode("\n", array(
837
				'/**',
838
				' * Path to the tasks directory.',
839
				' *',
840
				' * @var string',
841
				' */',
842
			)),
843
			'default' => '$sourcedir . \'/tasks\'',
844
			'raw_default' => true,
845
			'type' => 'string',
846
		),
847
		array(
848
			'text' => implode("\n", array(
849
				'',
850
				'# Make sure the paths are correct... at least try to fix them.',
851
				'if (!is_dir(realpath($boarddir)) && file_exists(dirname(__FILE__) . \'/agreement.txt\'))',
852
				'	$boarddir = dirname(__FILE__);',
853
				'if (!is_dir(realpath($sourcedir)) && is_dir($boarddir . \'/Sources\'))',
854
				'	$sourcedir = $boarddir . \'/Sources\';',
855
				'if (!is_dir(realpath($tasksdir)) && is_dir($sourcedir . \'/tasks\'))',
856
				'	$tasksdir = $sourcedir . \'/tasks\';',
857
				'if (!is_dir(realpath($packagesdir)) && is_dir($boarddir . \'/Packages\'))',
858
				'	$packagesdir = $boarddir . \'/Packages\';',
859
				'if (!is_dir(realpath($cachedir)) && is_dir($boarddir . \'/cache\'))',
860
				'	$cachedir = $boarddir . \'/cache\';',
861
			)),
862
			'search_pattern' => '~\n?(#[^\n]+)?(?:\n\h*if\s*\((?:\!file_exists\(\$(?'.'>boarddir|sourcedir|tasksdir|packagesdir|cachedir)\)|\!is_dir\(realpath\(\$(?'.'>boarddir|sourcedir|tasksdir|packagesdir|cachedir)\)\))[^;]+\n\h*\$(?'.'>boarddir|sourcedir|tasksdir|packagesdir|cachedir)[^\n]+;)+~sm',
863
		),
864
		'db_character_set' => array(
865
			'text' => implode("\n", array(
866
				'',
867
				'######### Legacy Settings #########',
868
				'# UTF-8 is now the only character set supported in 2.1.',
869
			)),
870
			'default' => 'utf8',
871
			'type' => 'string',
872
		),
873
		'db_show_debug' => array(
874
			'text' => implode("\n", array(
875
				'',
876
				'######### Developer Settings #########',
877
				'# Show debug info.',
878
			)),
879
			'default' => false,
880
			'auto_delete' => 2,
881
			'type' => 'boolean',
882
		),
883
		array(
884
			'text' => implode("\n", array(
885
				'',
886
				'########## Error-Catching ##########',
887
				'# Note: You shouldn\'t touch these settings.',
888
				'if (file_exists((isset($cachedir) ? $cachedir : dirname(__FILE__)) . \'/db_last_error.php\'))',
889
				'	include((isset($cachedir) ? $cachedir : dirname(__FILE__)) . \'/db_last_error.php\');',
890
				'',
891
				'if (!isset($db_last_error))',
892
				'{',
893
				'	// File does not exist so lets try to create it',
894
				'	file_put_contents((isset($cachedir) ? $cachedir : dirname(__FILE__)) . \'/db_last_error.php\', \'<\' . \'?\' . "php\n" . \'$db_last_error = 0;\' . "\n" . \'?\' . \'>\');',
895
				'	$db_last_error = 0;',
896
				'}',
897
			)),
898
			// Designed to match both 2.0 and 2.1 versions of this code.
899
			'search_pattern' => '~\n?#+ Error.Catching #+\n[^\n]*?settings\.\n(?:\$db_last_error = \d{1,11};|if \(file_exists.*?\$db_last_error = 0;(?' . '>\s*}))(?=\n|\?' . '>|$)~s',
900
		),
901
		// Temporary variable used during the upgrade process.
902
		'upgradeData' => array(
903
			'default' => '',
904
			'auto_delete' => 1,
905
			'type' => 'string',
906
		),
907
		// This should be removed if found.
908
		'db_last_error' => array(
909
			'default' => 0,
910
			'auto_delete' => 1,
911
			'type' => 'integer',
912
		),
913
	);
914
915
	// Allow mods the option to define comments, defaults, etc., for their settings.
916
	// Check if function exists, in case we are calling from installer or upgrader.
917
	if (function_exists('call_integration_hook'))
918
		call_integration_hook('integrate_update_settings_file', array(&$settings_defs));
919
920
	// If Settings.php is empty or invalid, try to recover using whatever is in $GLOBALS.
921
	if ($settings_vars === array())
922
	{
923
		foreach ($settings_defs as $var => $setting_def)
924
			if (isset($GLOBALS[$var]))
925
				$settings_vars[$var] = $GLOBALS[$var];
926
927
		$new_settings_vars = array_merge($settings_vars, $config_vars);
928
	}
929
930
	// During install/upgrade, don't set anything until we're ready for it.
931
	if (defined('SMF_INSTALLING') && empty($rebuild))
932
	{
933
		foreach ($settings_defs as $var => $setting_def)
934
			if (!in_array($var, array_keys($new_settings_vars)) && !is_int($var))
935
				unset($settings_defs[$var]);
936
	}
937
938
	/*******************************
939
	 * PART 2: Build substitutions *
940
	 *******************************/
941
942
	$type_regex = array(
943
		'string' =>
944
			'(?:' .
945
				// match the opening quotation mark...
946
				'(["\'])' .
947
				// then any number of other characters or escaped quotation marks...
948
				'(?:.(?!\\1)|\\\(?=\\1))*.?' .
949
				// then the closing quotation mark.
950
				'\\1' .
951
				// Maybe there's a second string concatenated to this one.
952
				'(?:\s*\.\s*)*' .
953
			')+',
954
		// Some numeric values might have been stored as strings.
955
		'integer' =>  '["\']?[+-]?\d+["\']?',
956
		'double' =>  '["\']?[+-]?\d+\.\d+([Ee][+-]\d+)?["\']?',
957
		// Some boolean values might have been stored as integers.
958
		'boolean' =>  '(?i:TRUE|FALSE|(["\']?)[01]\b\\1)',
959
		'NULL' =>  '(?i:NULL)',
960
		// These use a PCRE subroutine to match nested arrays.
961
		'array' =>  'array\s*(\((?'.'>[^()]|(?1))*\))',
962
		'object' =>  '\w+::__set_state\(array\s*(\((?'.'>[^()]|(?1))*\))\)',
963
	);
964
965
	/*
966
	 * The substitutions take place in one of two ways:
967
	 *
968
	 *  1: The search_pattern regex finds a string in Settings.php, which is
969
	 *     temporarily replaced by a placeholder. Once all the placeholders
970
	 *     have been inserted, each is replaced by the final replacement string
971
	 *     that we want to use. This is the standard method.
972
	 *
973
	 *  2: The search_pattern regex finds a string in Settings.php, which is
974
	 *     then deleted by replacing it with an empty placeholder. Then after
975
	 *     all the real placeholders have been dealt with, the replace_pattern
976
	 *     regex finds where to insert the final replacement string that we
977
	 *     want to use. This method is for special cases.
978
	 */
979
	$prefix = mt_rand() . '-';
980
	$neg_index = -1;
981
	$substitutions = array(
982
		$neg_index-- => array(
983
			'search_pattern' => '~^\s*<\?(php\b)?\n?~',
984
			'placeholder' => '',
985
			'replace_pattern' => '~^~',
986
			'replacement' => '<' . "?php\n",
987
		),
988
		$neg_index-- => array(
989
			'search_pattern' => '~\S\K\s*(\?' . '>)?\s*$~',
990
			'placeholder' => "\n" . md5($prefix . '?' . '>'),
991
			'replacement' => "\n\n?" . '>',
992
		),
993
		// Remove the code that redirects to the installer.
994
		$neg_index-- => array(
995
			'search_pattern' => '~^if\s*\(file_exists\(dirname\(__FILE__\)\s*\.\s*\'/install\.php\'\)\)\s*(?:({(?'.'>[^{}]|(?1))*})\h*|header(\((?' . '>[^()]|(?2))*\));\n)~m',
996
			'placeholder' => '',
997
		),
998
	);
999
1000
	if (defined('SMF_INSTALLING'))
1001
		$substitutions[$neg_index--] = array(
1002
			'search_pattern' => '~/\*.*?SMF\s+1\.\d.*?\*/~s',
1003
			'placeholder' => '',
1004
		);
1005
1006
	foreach ($settings_defs as $var => $setting_def)
1007
	{
1008
		$placeholder = md5($prefix . $var);
1009
		$replacement = '';
1010
1011
		if (!empty($setting_def['text']))
1012
		{
1013
			// Special handling for the license block: always at the beginning.
1014
			if (strpos($setting_def['text'], "* @package SMF\n") !== false)
1015
			{
1016
				$substitutions[$var]['search_pattern'] = $setting_def['search_pattern'];
1017
				$substitutions[$var]['placeholder'] = '';
1018
				$substitutions[-1]['replacement'] .= $setting_def['text'] . "\n";
1019
			}
1020
			// Special handling for the Error-Catching block: always at the end.
1021
			elseif (strpos($setting_def['text'], 'Error-Catching') !== false)
1022
			{
1023
				$errcatch_var = $var;
0 ignored issues
show
Unused Code introduced by
The assignment to $errcatch_var is dead and can be removed.
Loading history...
1024
				$substitutions[$var]['search_pattern'] = $setting_def['search_pattern'];
1025
				$substitutions[$var]['placeholder'] = '';
1026
				$substitutions[-2]['replacement'] = "\n" . $setting_def['text'] . $substitutions[-2]['replacement'];
1027
			}
1028
			// The text is the whole thing (code blocks, etc.)
1029
			elseif (is_int($var))
1030
			{
1031
				// Remember the path correcting code for later.
1032
				if (strpos($setting_def['text'], '# Make sure the paths are correct') !== false)
1033
					$pathcode_var = $var;
1034
1035
				if (!empty($setting_def['search_pattern']))
1036
					$substitutions[$var]['search_pattern'] = $setting_def['search_pattern'];
1037
				else
1038
					$substitutions[$var]['search_pattern'] = '~' . preg_quote($setting_def['text'], '~') . '~';
1039
1040
				$substitutions[$var]['placeholder'] = $placeholder;
1041
1042
				$replacement .= $setting_def['text'] . "\n";
1043
			}
1044
			// We only include comments when rebuilding.
1045
			elseif (!empty($rebuild))
1046
				$replacement .= $setting_def['text'] . "\n";
1047
		}
1048
1049
		if (is_string($var))
1050
		{
1051
			// Ensure the value is good.
1052
			if (in_array($var, array_keys($new_settings_vars)))
1053
			{
1054
				// Objects without a __set_state method need a fallback.
1055
				if (is_object($new_settings_vars[$var]) && !method_exists($new_settings_vars[$var], '__set_state'))
1056
				{
1057
					if (method_exists($new_settings_vars[$var], '__toString'))
1058
						$new_settings_vars[$var] = (string) $new_settings_vars[$var];
1059
					else
1060
						$new_settings_vars[$var] = (array) $new_settings_vars[$var];
1061
				}
1062
1063
				// Normalize the type if necessary.
1064
				if (isset($setting_def['type']))
1065
				{
1066
					$expected_types = (array) $setting_def['type'];
1067
					$var_type = gettype($new_settings_vars[$var]);
1068
1069
					// Variable is not of an expected type.
1070
					if (!in_array($var_type, $expected_types))
1071
					{
1072
						// Passed in an unexpected array.
1073
						if ($var_type == 'array')
1074
						{
1075
							$temp = reset($new_settings_vars[$var]);
1076
1077
							// Use the first element if there's only one and it is a scalar.
1078
							if (count($new_settings_vars[$var]) === 1 && is_scalar($temp))
1079
								$new_settings_vars[$var] = $temp;
1080
1081
							// Or keep the old value, if that is good.
1082
							elseif (isset($settings_vars[$var]) && in_array(gettype($settings_vars[$var]), $expected_types))
1083
								$new_settings_vars[$var] = $settings_vars[$var];
1084
1085
							// Fall back to the default
1086
							else
1087
								$new_settings_vars[$var] = $setting_def['default'];
1088
						}
1089
1090
						// Cast it to whatever type was expected.
1091
						// Note: the order of the types in this loop matters.
1092
						foreach (array('boolean', 'integer', 'double', 'string', 'array') as $to_type)
1093
						{
1094
							if (in_array($to_type, $expected_types))
1095
							{
1096
								settype($new_settings_vars[$var], $to_type);
1097
								break;
1098
							}
1099
						}
1100
					}
1101
				}
1102
			}
1103
			// Abort if a required one is undefined (unless we're installing).
1104
			elseif (!empty($setting_def['required']) && !defined('SMF_INSTALLING'))
1105
				return false;
1106
1107
			// Create the search pattern.
1108
			if (!empty($setting_def['search_pattern']))
1109
				$substitutions[$var]['search_pattern'] = $setting_def['search_pattern'];
1110
			else
1111
			{
1112
				$var_pattern = array();
1113
1114
				if (isset($setting_def['type']))
1115
				{
1116
					foreach ((array) $setting_def['type'] as $type)
1117
						$var_pattern[] = $type_regex[$type];
1118
				}
1119
1120
				if (in_array($var, array_keys($config_vars)))
1121
				{
1122
					$var_pattern[] = @$type_regex[gettype($config_vars[$var])];
1123
1124
					if (is_string($config_vars[$var]) && strpos($config_vars[$var], dirname($settingsFile)) === 0)
1125
						$var_pattern[] = '(?:__DIR__|dirname\(__FILE__\)) . \'' . (preg_quote(str_replace(dirname($settingsFile), '', $config_vars[$var]), '~')) . '\'';
1126
				}
1127
1128
				if (in_array($var, array_keys($settings_vars)))
1129
				{
1130
					$var_pattern[] = @$type_regex[gettype($settings_vars[$var])];
1131
1132
					if (is_string($settings_vars[$var]) && strpos($settings_vars[$var], dirname($settingsFile)) === 0)
1133
						$var_pattern[] = '(?:__DIR__|dirname\(__FILE__\)) . \'' . (preg_quote(str_replace(dirname($settingsFile), '', $settings_vars[$var]), '~')) . '\'';
1134
				}
1135
1136
				if (!empty($setting_def['raw_default']) && $setting_def['default'] !== '')
1137
				{
1138
					$var_pattern[] = preg_replace('/\s+/', '\s+', preg_quote($setting_def['default'], '~'));
1139
1140
					if (strpos($setting_def['default'], 'dirname(__FILE__)') !== false)
1141
						$var_pattern[] = preg_replace('/\s+/', '\s+', preg_quote(str_replace('dirname(__FILE__)', '__DIR__', $setting_def['default']), '~'));
1142
1143
					if (strpos($setting_def['default'], '__DIR__') !== false)
1144
						$var_pattern[] = preg_replace('/\s+/', '\s+', preg_quote(str_replace('__DIR__', 'dirname(__FILE__)', $setting_def['default']), '~'));
1145
				}
1146
1147
				$var_pattern = array_unique($var_pattern);
1148
1149
				$var_pattern = count($var_pattern) > 1 ? '(?:' . (implode('|', $var_pattern)) . ')' : $var_pattern[0];
1150
1151
				$substitutions[$var]['search_pattern'] = '~(?<=^|\s)\h*\$' . preg_quote($var, '~') . '\s*=\s*' . $var_pattern . ';~' . (!empty($utf8) ? 'u' : '');
1152
			}
1153
1154
			// Next create the placeholder or replace_pattern.
1155
			if (!empty($setting_def['replace_pattern']))
1156
				$substitutions[$var]['replace_pattern'] = $setting_def['replace_pattern'];
1157
			else
1158
				$substitutions[$var]['placeholder'] = $placeholder;
1159
1160
			// Now create the replacement.
1161
			// A setting to delete.
1162
			if (!empty($setting_def['auto_delete']) && empty($new_settings_vars[$var]))
1163
			{
1164
				if ($setting_def['auto_delete'] === 2 && empty($rebuild) && in_array($var, array_keys($new_settings_vars)))
1165
				{
1166
					$replacement .= '$' . $var . ' = ' . ($new_settings_vars[$var] === $setting_def['default'] && !empty($setting_def['raw_default']) ? sprintf($new_settings_vars[$var]) : smf_var_export($new_settings_vars[$var], true)) . ";";
0 ignored issues
show
Unused Code introduced by
The call to smf_var_export() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1166
					$replacement .= '$' . $var . ' = ' . ($new_settings_vars[$var] === $setting_def['default'] && !empty($setting_def['raw_default']) ? sprintf($new_settings_vars[$var]) : /** @scrutinizer ignore-call */ smf_var_export($new_settings_vars[$var], true)) . ";";

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1167
				}
1168
				else
1169
				{
1170
					$replacement = '';
1171
1172
					// This is just for cosmetic purposes. Removes the blank line.
1173
					$substitutions[$var]['search_pattern'] = str_replace('(?<=^|\s)', '\n?', $substitutions[$var]['search_pattern']);
1174
				}
1175
			}
1176
			// Add this setting's value.
1177
			elseif (in_array($var, array_keys($new_settings_vars)))
1178
			{
1179
				$replacement .= '$' . $var . ' = ' . ($new_settings_vars[$var] === $setting_def['default'] && !empty($setting_def['raw_default']) ? sprintf($new_settings_vars[$var]) : smf_var_export($new_settings_vars[$var], true)) . ";";
1180
			}
1181
			// Fall back to the default value.
1182
			elseif (isset($setting_def['default']))
1183
			{
1184
				$replacement .= '$' . $var . ' = ' . (!empty($setting_def['raw_default']) ? sprintf($setting_def['default']) : smf_var_export($setting_def['default'], true)) . ';';
1185
			}
1186
			// This shouldn't happen, but we've got nothing.
1187
			else
1188
				$replacement .= '$' . $var . ' = null;';
1189
		}
1190
1191
		$substitutions[$var]['replacement'] = $replacement;
1192
1193
		// We're done with this one.
1194
		unset($new_settings_vars[$var]);
1195
	}
1196
1197
	// Any leftovers to deal with?
1198
	foreach ($new_settings_vars as $var => $val)
1199
	{
1200
		$var_pattern = array();
1201
1202
		if (in_array($var, array_keys($config_vars)))
1203
			$var_pattern[] = $type_regex[gettype($config_vars[$var])];
1204
1205
		if (in_array($var, array_keys($settings_vars)))
1206
			$var_pattern[] = $type_regex[gettype($settings_vars[$var])];
1207
1208
		$var_pattern = array_unique($var_pattern);
1209
1210
		$var_pattern = count($var_pattern) > 1 ? '(?:' . (implode('|', $var_pattern)) . ')' : $var_pattern[0];
1211
1212
		$placeholder = md5($prefix . $var);
1213
1214
		$substitutions[$var]['search_pattern'] = '~(?<=^|\s)\h*\$' . preg_quote($var, '~') . '\s*=\s*' . $var_pattern . ';~' . (!empty($utf8) ? 'u' : '');
1215
		$substitutions[$var]['placeholder'] = $placeholder;
1216
		$substitutions[$var]['replacement'] = '$' . $var . ' = ' . smf_var_export($val, true) . ";";
1217
	}
1218
1219
	// During an upgrade, some of the path variables may not have been declared yet.
1220
	if (defined('SMF_INSTALLING') && empty($rebuild))
1221
	{
1222
		preg_match_all('~^\h*\$(\w+)\s*=\s*~m', $substitutions[$pathcode_var]['replacement'], $matches);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pathcode_var does not seem to be defined for all execution paths leading up to this point.
Loading history...
1223
		$missing_pathvars = array_diff($matches[1], array_keys($substitutions));
1224
1225
		if (!empty($missing_pathvars))
1226
		{
1227
			foreach ($missing_pathvars as $var)
1228
			{
1229
				$substitutions[$pathcode_var]['replacement'] = preg_replace('~\nif[^\n]+\$' . $var . '[^\n]+\n\h*\$' . $var . ' = [^\n]+~', '', $substitutions[$pathcode_var]['replacement']);
1230
			}
1231
		}
1232
	}
1233
1234
	// It's important to do the numbered ones before the named ones, or messes happen.
1235
	uksort(
1236
		$substitutions,
1237
		function($a, $b) {
1238
			if (is_int($a) && is_int($b))
1239
				return $a > $b ? 1 : ($a < $b ? -1 : 0);
1240
			elseif (is_int($a))
1241
				return -1;
1242
			elseif (is_int($b))
1243
				return 1;
1244
			else
1245
				return strcasecmp($b, $a);
1246
		}
1247
	);
1248
1249
	/******************************
1250
	 * PART 3: Content processing *
1251
	 ******************************/
1252
1253
	/* 3.a: Get the content of Settings.php and make sure it is good. */
1254
1255
	// Retrieve the contents of Settings.php and normalize the line endings.
1256
	$settingsText = trim(strtr(file_get_contents($settingsFile), array("\r\n" => "\n", "\r" => "\n")));
1257
1258
	// If Settings.php is empty or corrupt for some reason, see if we can recover.
1259
	if ($settingsText == '' || substr($settingsText, 0, 5) !== '<' . '?php')
1260
	{
1261
		// Try restoring from the backup.
1262
		if (file_exists(dirname($settingsFile) . '/Settings_bak.php'))
1263
			$settingsText = strtr(file_get_contents(dirname($settingsFile) . '/Settings_bak.php'), array("\r\n" => "\n", "\r" => "\n"));
1264
1265
		// Backup is bad too? Our only option is to create one from scratch.
1266
		if ($settingsText == '' || substr($settingsText, 0, 5) !== '<' . '?php' || substr($settingsText, -2) !== '?' . '>')
1267
		{
1268
			$settingsText = '<' . "?php\n";
1269
			foreach ($settings_defs as $var => $setting_def)
1270
			{
1271
				if (!empty($setting_def['text']) && strpos($substitutions[$var]['replacement'], $setting_def['text']) === false)
1272
					$substitutions[$var]['replacement'] = $setting_def['text'] . "\n" . $substitutions[$var]['replacement'];
1273
1274
				$settingsText .= $substitutions[$var]['replacement'] . "\n";
1275
			}
1276
			$settingsText .= "\n\n?" . '>';
1277
			$rebuild = true;
1278
		}
1279
	}
1280
1281
	// Settings.php is unlikely to contain any heredocs, but just in case...
1282
	if (preg_match_all('/<<<([\'"]?)(\w+)\1\R(.*?)\R\h*\2;$/ms', $settingsText, $matches))
1283
	{
1284
		foreach ($matches[0] as $mkey => $heredoc)
1285
		{
1286
			if (!empty($matches[1][$mkey]) && $matches[1][$mkey] === '\'')
1287
				$heredoc_replacements[$heredoc] = var_export($matches[3][$mkey], true) . ';';
1288
			else
1289
				$heredoc_replacements[$heredoc] = '"' . strtr(substr(var_export($matches[3][$mkey], true), 1, -1), array("\\'" => "'", '"' => '\"')) . '";';
1290
		}
1291
1292
		$settingsText = strtr($settingsText, $heredoc_replacements);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $heredoc_replacements seems to be defined by a foreach iteration on line 1284. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
1293
	}
1294
1295
	/* 3.b: Loop through all our substitutions to insert placeholders, etc. */
1296
1297
	$last_var = null;
1298
	$bare_settingsText = $settingsText;
1299
	$force_before_pathcode = array();
1300
	foreach ($substitutions as $var => $substitution)
1301
	{
1302
		$placeholders[$var] = $substitution['placeholder'];
1303
1304
		if (!empty($substitution['placeholder']))
1305
		{
1306
			$simple_replacements[$substitution['placeholder']] = $substitution['replacement'];
1307
		}
1308
		elseif (!empty($substitution['replace_pattern']))
1309
		{
1310
			$replace_patterns[$var] = $substitution['replace_pattern'];
1311
			$replace_strings[$var] = $substitution['replacement'];
1312
		}
1313
1314
		if (strpos($substitutions[$pathcode_var]['replacement'], '$' . $var . ' = ') !== false)
1315
			$force_before_pathcode[] = $var;
1316
1317
		// Look before you leap.
1318
		preg_match_all($substitution['search_pattern'], $bare_settingsText, $matches);
1319
1320
		if ((is_string($var) || $var === $pathcode_var) && count($matches[0]) !== 1 && $substitution['replacement'] !== '')
1321
		{
1322
			// More than one instance of the variable = not good.
1323
			if (count($matches[0]) > 1)
1324
			{
1325
				if (is_string($var))
1326
				{
1327
					// Maybe we can try something more interesting?
1328
					$sp = substr($substitution['search_pattern'], 1);
1329
1330
					if (strpos($sp, '(?<=^|\s)') === 0)
1331
						$sp = substr($sp, 9);
1332
1333
					if (strpos($sp, '^') === 0 || strpos($sp, '(?<') === 0)
1334
						return false;
1335
1336
					// See if we can exclude `if` blocks, etc., to narrow down the matches.
1337
					// @todo Multiple layers of nested brackets might confuse this.
1338
					$sp = '~(?:^|//[^\n]+c\n|\*/|[;}]|' . implode('|', array_filter($placeholders)) . ')\s*' . (strpos($sp, '\K') === false ? '\K' : '') . $sp;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $placeholders seems to be defined later in this foreach loop on line 1302. Are you sure it is defined here?
Loading history...
1339
1340
					preg_match_all($sp, $settingsText, $matches);
1341
				}
1342
				else
1343
					$sp = $substitution['search_pattern'];
1344
1345
				// Found at least some that are simple assignment statements.
1346
				if (count($matches[0]) > 0)
1347
				{
1348
					// Remove any duplicates.
1349
					if (count($matches[0]) > 1)
1350
						$settingsText = preg_replace($sp, '', $settingsText, count($matches[0]) - 1);
1351
1352
					// Insert placeholder for the last one.
1353
					$settingsText = preg_replace($sp, $substitution['placeholder'], $settingsText, 1);
1354
				}
1355
1356
				// All instances are inside more complex code structures.
1357
				else
1358
				{
1359
					// Only safe option at this point is to skip it.
1360
					unset($substitutions[$var], $new_settings_vars[$var], $settings_defs[$var], $simple_replacements[$substitution['placeholder']], $replace_patterns[$var], $replace_strings[$var]);
1361
1362
					continue;
1363
				}
1364
			}
1365
			// No matches found.
1366
			elseif (count($matches[0]) === 0)
1367
			{
1368
				$found = false;
1369
				$in_c = in_array($var, array_keys($config_vars));
1370
				$in_s = in_array($var, array_keys($settings_vars));
1371
1372
				// Is it in there at all?
1373
				if (!preg_match('~(^|\s)\$' . preg_quote($var, '~') . '\s*=\s*~', $bare_settingsText))
1374
				{
1375
					// It's defined by Settings.php, but not by code in the file.
1376
					// Probably done via an include or something. Skip it.
1377
					if ($in_s)
1378
						unset($substitutions[$var], $settings_defs[$var]);
1379
1380
					// Admin is explicitly trying to set this one, so we'll handle
1381
					// it as if it were a new custom setting being added.
1382
					elseif ($in_c)
1383
						$new_settings_vars[$var] = $config_vars[$var];
1384
1385
					continue;
1386
				}
1387
1388
				// It's in there somewhere, so check if the value changed type.
1389
				foreach (array('scalar', 'object', 'array') as $type)
1390
				{
1391
					// Try all the other scalar types first.
1392
					if ($type == 'scalar')
1393
						$sp = '(?:' . (implode('|', array_diff_key($type_regex, array($in_c ? gettype($config_vars[$var]) : ($in_s ? gettype($settings_vars[$var]) : PHP_INT_MAX) => '', 'array' => '', 'object' => '')))) . ')';
1394
1395
					// Maybe it's an object? (Probably not, but we should check.)
1396
					elseif ($type == 'object')
1397
					{
1398
						if (strpos($settingsText, '__set_state') === false)
1399
							continue;
1400
1401
						$sp = $type_regex['object'];
1402
					}
1403
1404
					// Maybe it's an array?
1405
					else
1406
						$sp = $type_regex['array'];
1407
1408
					if (preg_match('~(^|\s)\$' . preg_quote($var, '~') . '\s*=\s*' . $sp . '~', $bare_settingsText, $derp))
1409
					{
1410
						$settingsText = preg_replace('~(^|\s)\$' . preg_quote($var, '~') . '\s*=\s*' . $sp . '~', $substitution['placeholder'], $settingsText);
1411
						$found = true;
1412
						break;
1413
					}
1414
				}
1415
1416
				// Something weird is going on. Better just leave it alone.
1417
				if (!$found)
1418
				{
1419
					// $var? What $var? Never heard of it.
1420
					unset($substitutions[$var], $new_settings_vars[$var], $settings_defs[$var], $simple_replacements[$substitution['placeholder']], $replace_patterns[$var], $replace_strings[$var]);
1421
					continue;
1422
				}
1423
			}
1424
		}
1425
		// Good to go, so insert our placeholder.
1426
		else
1427
			$settingsText = preg_replace($substitution['search_pattern'], $substitution['placeholder'], $settingsText);
1428
1429
		// Once the code blocks are done, we want to compare to a version without comments.
1430
		if (is_int($last_var) && is_string($var))
1431
			$bare_settingsText = strip_php_comments($settingsText);
1432
1433
		$last_var = $var;
1434
	}
1435
1436
	// Rebuilding requires more work.
1437
	if (!empty($rebuild))
1438
	{
1439
		// Strip out the leading and trailing placeholders to prevent duplication.
1440
		$settingsText = str_replace(array($substitutions[-1]['placeholder'], $substitutions[-2]['placeholder']), '', $settingsText);
1441
1442
		// Strip out all our standard comments.
1443
		foreach ($settings_defs as $var => $setting_def)
1444
		{
1445
			if (isset($setting_def['text']))
1446
				$settingsText = strtr($settingsText, array($setting_def['text'] . "\n" => '', $setting_def['text'] => '',));
1447
		}
1448
1449
		// We need to refresh $bare_settingsText at this point.
1450
		$bare_settingsText = strip_php_comments($settingsText);
1451
1452
		// Fix up whitespace to make comparison easier.
1453
		foreach ($placeholders as $placeholder)
1454
		{
1455
			$bare_settingsText = str_replace(array($placeholder . "\n\n", $placeholder), $placeholder . "\n", $bare_settingsText);
1456
		}
1457
		$bare_settingsText = preg_replace('/\h+$/m', '', rtrim($bare_settingsText));
1458
1459
		/*
1460
		 * Divide the existing content into sections.
1461
		 * The idea here is to make sure we don't mess with the relative position
1462
		 * of any code blocks in the file, since that could break things. Within
1463
		 * each section, however, we'll reorganize the content to match the
1464
		 * default layout as closely as we can.
1465
		 */
1466
		$sections = array(array());
1467
		$section_num = 0;
1468
		$trimmed_placeholders = array_filter(array_map('trim', $placeholders));
1469
		$newsection_placeholders = array();
1470
		$all_custom_content = '';
1471
		foreach ($substitutions as $var => $substitution)
1472
		{
1473
			if (is_int($var) && ($var === -2 || $var > 0) && isset($trimmed_placeholders[$var]) && strpos($bare_settingsText, $trimmed_placeholders[$var]) !== false)
1474
				$newsection_placeholders[$var] = $trimmed_placeholders[$var];
1475
		}
1476
		foreach (preg_split('~(?<=' . implode('|', $trimmed_placeholders) . ')|(?=' . implode('|', $trimmed_placeholders) . ')~', $bare_settingsText) as $part)
1477
		{
1478
			$part = trim($part);
1479
1480
			if (empty($part))
1481
				continue;
1482
1483
			// Build a list of placeholders for this section.
1484
			if (in_array($part, $trimmed_placeholders) && !in_array($part, $newsection_placeholders))
1485
			{
1486
				$sections[$section_num][] = $part;
1487
			}
1488
			// Custom content and newsection_placeholders get their own sections.
1489
			else
1490
			{
1491
				if (!empty($sections[$section_num]))
1492
					++$section_num;
1493
1494
				$sections[$section_num][] = $part;
1495
1496
				++$section_num;
1497
1498
				if (!in_array($part, $trimmed_placeholders))
1499
					$all_custom_content .= "\n" . $part;
1500
			}
1501
		}
1502
1503
		// And now, rebuild the content!
1504
		$new_settingsText = '';
1505
		$done_defs = array();
1506
		$sectionkeys = array_keys($sections);
1507
		foreach ($sections as $sectionkey => $section)
1508
		{
1509
			// Custom content needs to be preserved.
1510
			if (count($section) === 1 && !in_array($section[0], $trimmed_placeholders))
1511
			{
1512
				$prev_section_end = $sectionkey < 1 ? 0 : strpos($settingsText, end($sections[$sectionkey - 1])) + strlen(end($sections[$sectionkey - 1]));
1513
				$next_section_start = $sectionkey == end($sectionkeys) ? strlen($settingsText) : strpos($settingsText, $sections[$sectionkey + 1][0]);
1514
1515
				$new_settingsText .= "\n" . substr($settingsText, $prev_section_end, $next_section_start - $prev_section_end) . "\n";
1516
			}
1517
			// Put the placeholders in this section into canonical order.
1518
			else
1519
			{
1520
				$section_parts = array_flip($section);
1521
				$pathcode_reached = false;
1522
				foreach ($settings_defs as $var => $setting_def)
1523
				{
1524
					if ($var === $pathcode_var)
1525
						$pathcode_reached = true;
1526
1527
					// Already did this setting, so move on to the next.
1528
					if (in_array($var, $done_defs))
1529
						continue;
1530
1531
					// Stop when we hit a setting definition that will start a later section.
1532
					if (isset($newsection_placeholders[$var]) && count($section) !== 1)
1533
						break;
1534
1535
					// Stop when everything in this section is done, unless it's the last.
1536
					// This helps maintain the relative position of any custom content.
1537
					if (empty($section_parts) && $sectionkey < (count($sections) - 1))
1538
						break;
1539
1540
					$p = trim($substitutions[$var]['placeholder']);
1541
1542
					// Can't do anything with an empty placeholder.
1543
					if ($p === '')
1544
						continue;
1545
1546
					// Does this need to be inserted before the path correction code?
1547
					if (strpos($new_settingsText, trim($substitutions[$pathcode_var]['placeholder'])) !== false && in_array($var, $force_before_pathcode))
1548
					{
1549
						$new_settingsText = strtr($new_settingsText, array($substitutions[$pathcode_var]['placeholder'] => $p . "\n" . $substitutions[$pathcode_var]['placeholder']));
1550
1551
						$bare_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1552
						$done_defs[] = $var;
1553
						unset($section_parts[trim($substitutions[$var]['placeholder'])]);
1554
					}
1555
1556
					// If it's in this section, add it to the new text now.
1557
					elseif (in_array($p, $section))
1558
					{
1559
						$new_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1560
						$done_defs[] = $var;
1561
						unset($section_parts[trim($substitutions[$var]['placeholder'])]);
1562
					}
1563
1564
					// Perhaps it is safe to reposition it anyway.
1565
					elseif (is_string($var) && strpos($new_settingsText, $p) === false && strpos($all_custom_content, '$' . $var) === false)
1566
					{
1567
						$new_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1568
						$done_defs[] = $var;
1569
						unset($section_parts[trim($substitutions[$var]['placeholder'])]);
1570
					}
1571
1572
					// If this setting is missing entirely, fix it.
1573
					elseif (strpos($bare_settingsText, $p) === false)
1574
					{
1575
						// Special case if the path code is missing. Put it near the end,
1576
						// and also anything else that is missing that normally follows it.
1577
						if (!isset($newsection_placeholders[$pathcode_var]) && $pathcode_reached === true && $sectionkey < (count($sections) - 1))
1578
							break;
1579
1580
						$new_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1581
						$bare_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1582
						$done_defs[] = $var;
1583
						unset($section_parts[trim($substitutions[$var]['placeholder'])]);
1584
					}
1585
				}
1586
			}
1587
		}
1588
		$settingsText = $new_settingsText;
1589
1590
		// Restore the leading and trailing placeholders as necessary.
1591
		foreach (array(-1, -2) as $var)
1592
		{
1593
			if (!empty($substitutions[$var]['placeholder']) && strpos($settingsText, $substitutions[$var]['placeholder']) === false);
1594
			{
1595
				$settingsText = ($var == -1 ? $substitutions[$var]['placeholder'] : '') . $settingsText . ($var == -2 ? $substitutions[$var]['placeholder'] : '');
1596
			}
1597
		}
1598
	}
1599
	// Even if not rebuilding, there are a few variables that may need to be moved around.
1600
	else
1601
	{
1602
		$pathcode_pos = strpos($settingsText, $substitutions[$pathcode_var]['placeholder']);
1603
1604
		if ($pathcode_pos !== false)
1605
		{
1606
			foreach ($force_before_pathcode as $var)
1607
			{
1608
				if (!empty($substitutions[$var]['placeholder']) && strpos($settingsText, $substitutions[$var]['placeholder']) > $pathcode_pos)
1609
				{
1610
					$settingsText = strtr($settingsText, array(
1611
						$substitutions[$var]['placeholder'] => '',
1612
						$substitutions[$pathcode_var]['placeholder'] => $substitutions[$var]['placeholder'] . "\n" . $substitutions[$pathcode_var]['placeholder'],
1613
					));
1614
				}
1615
			}
1616
		}
1617
	}
1618
1619
	/* 3.c: Replace the placeholders with the final values */
1620
1621
	// Where possible, perform simple substitutions.
1622
	$settingsText = strtr($settingsText, $simple_replacements);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $simple_replacements does not seem to be defined for all execution paths leading up to this point.
Loading history...
1623
1624
	// Deal with any complicated ones.
1625
	if (!empty($replace_patterns))
1626
		$settingsText = preg_replace($replace_patterns, $replace_strings, $settingsText);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $replace_strings does not seem to be defined for all execution paths leading up to this point.
Loading history...
1627
1628
	// Make absolutely sure that the path correction code is included.
1629
	if (strpos($settingsText, $substitutions[$pathcode_var]['replacement']) === false)
1630
		$settingsText = preg_replace('~(?=\n#+ Error.Catching #+)~', "\n" . $substitutions[$pathcode_var]['replacement'] . "\n", $settingsText);
1631
1632
	// If we did not rebuild, do just enough to make sure the thing is viable.
1633
	if (empty($rebuild))
1634
	{
1635
		// We need to refresh $bare_settingsText again, and remove the code blocks from it.
1636
		$bare_settingsText = $settingsText;
1637
		foreach ($substitutions as $var => $substitution)
1638
		{
1639
			if (!is_int($var))
1640
				break;
1641
1642
			if (isset($substitution['replacement']))
1643
				$bare_settingsText = str_replace($substitution['replacement'], '', $bare_settingsText);
1644
		}
1645
		$bare_settingsText = strip_php_comments($bare_settingsText);
1646
1647
		// Now insert any defined settings that are missing.
1648
		$pathcode_reached = false;
1649
		foreach ($settings_defs as $var => $setting_def)
1650
		{
1651
			if ($var === $pathcode_var)
1652
				$pathcode_reached = true;
1653
1654
			if (is_int($var))
1655
				continue;
1656
1657
			// Do nothing if it is already in there.
1658
			if (preg_match($substitutions[$var]['search_pattern'], $bare_settingsText))
1659
				continue;
1660
1661
			// Insert it either before or after the path correction code, whichever is appropriate.
1662
			if (!$pathcode_reached || in_array($var, $force_before_pathcode))
1663
			{
1664
				$settingsText = preg_replace($substitutions[$pathcode_var]['search_pattern'], $substitutions[$var]['replacement'] . "\n$0", $settingsText);
1665
			}
1666
			else
1667
			{
1668
				$settingsText = preg_replace($substitutions[$pathcode_var]['search_pattern'], "$0\n" . $substitutions[$var]['replacement'], $settingsText);
1669
			}
1670
		}
1671
	}
1672
1673
	// If we have any brand new settings to add, do so.
1674
	foreach ($new_settings_vars as $var => $val)
1675
	{
1676
		if (isset($substitutions[$var]) && !preg_match($substitutions[$var]['search_pattern'], $settingsText))
1677
		{
1678
			if (!isset($settings_defs[$var]) && strpos($settingsText, '# Custom Settings #') === false)
1679
				$settingsText = preg_replace('~(?=\n#+ Error.Catching #+)~', "\n\n######### Custom Settings #########\n", $settingsText);
1680
1681
			$settingsText = preg_replace('~(?=\n#+ Error.Catching #+)~', $substitutions[$var]['replacement'] . "\n", $settingsText);
1682
		}
1683
	}
1684
1685
	// This is just cosmetic. Get rid of extra lines of whitespace.
1686
	$settingsText = preg_replace('~\n\s*\n~', "\n\n", $settingsText);
1687
1688
	/**************************************
1689
	 * PART 4: Check syntax before saving *
1690
	 **************************************/
1691
1692
	$temp_sfile = tempnam(sm_temp_dir(), md5($prefix . 'Settings.php'));
1693
	file_put_contents($temp_sfile, $settingsText);
1694
1695
	$result = get_current_settings(filemtime($temp_sfile), $temp_sfile);
1696
1697
	unlink($temp_sfile);
1698
1699
	// If the syntax is borked, try rebuilding to see if that fixes it.
1700
	if ($result === false)
0 ignored issues
show
introduced by
The condition $result === false is always false.
Loading history...
1701
		return empty($rebuild) ? updateSettingsFile($config_vars, $keep_quotes, true) : false;
1702
1703
	/******************************************
1704
	 * PART 5: Write updated settings to file *
1705
	 ******************************************/
1706
1707
	$success = safe_file_write($settingsFile, $settingsText, dirname($settingsFile) . '/Settings_bak.php', $last_settings_change);
1708
1709
	// Remember this in case updateSettingsFile is called twice.
1710
	$mtime = filemtime($settingsFile);
0 ignored issues
show
Unused Code introduced by
The assignment to $mtime is dead and can be removed.
Loading history...
1711
1712
	return $success;
1713
}
1714
1715
/**
1716
 * Retrieves a copy of the current values of all settings defined in Settings.php.
1717
 *
1718
 * Importantly, it does this without affecting our actual global variables at all,
1719
 * and it performs safety checks before acting. The result is an array of the
1720
 * values as recorded in the settings file.
1721
 *
1722
 * @param int $mtime Timestamp of last known good configuration. Defaults to time SMF started.
1723
 * @param string $settingsFile The settings file. Defaults to SMF's standard Settings.php.
1724
 * @return array An array of name/value pairs for all the settings in the file.
1725
 */
1726
function get_current_settings($mtime = null, $settingsFile = null)
1727
{
1728
	$mtime = is_null($mtime) ? (defined('TIME_START') ? TIME_START : $_SERVER['REQUEST_TIME']) : (int) $mtime;
1729
1730
	if (!is_file($settingsFile))
0 ignored issues
show
Bug introduced by
It seems like $settingsFile can also be of type null; however, parameter $filename of is_file() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1730
	if (!is_file(/** @scrutinizer ignore-type */ $settingsFile))
Loading history...
1731
	{
1732
		foreach (get_included_files() as $settingsFile)
1733
			if (basename($settingsFile) === 'Settings.php')
1734
				break;
1735
1736
		if (basename($settingsFile) !== 'Settings.php')
0 ignored issues
show
Bug introduced by
It seems like $settingsFile can also be of type null; however, parameter $path of basename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1736
		if (basename(/** @scrutinizer ignore-type */ $settingsFile) !== 'Settings.php')
Loading history...
1737
			return false;
1738
	}
1739
1740
	// If the file has been changed since the last known good configuration, bail out.
1741
	clearstatcache();
1742
	if (filemtime($settingsFile) > $mtime)
0 ignored issues
show
Bug introduced by
It seems like $settingsFile can also be of type null; however, parameter $filename of filemtime() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1742
	if (filemtime(/** @scrutinizer ignore-type */ $settingsFile) > $mtime)
Loading history...
1743
		return false;
1744
1745
	// Strip out opening and closing PHP tags.
1746
	$settingsText = trim(file_get_contents($settingsFile));
0 ignored issues
show
Bug introduced by
It seems like $settingsFile can also be of type null; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1746
	$settingsText = trim(file_get_contents(/** @scrutinizer ignore-type */ $settingsFile));
Loading history...
1747
	if (substr($settingsText, 0, 5) == '<' . '?php')
1748
		$settingsText = substr($settingsText, 5);
1749
	if (substr($settingsText, -2) == '?' . '>')
1750
		$settingsText = substr($settingsText, 0, -2);
1751
1752
	// Since we're using eval, we need to manually replace these with strings.
1753
	$settingsText = strtr($settingsText, array(
1754
		'__FILE__' => var_export($settingsFile, true),
1755
		'__DIR__' => var_export(dirname($settingsFile), true),
0 ignored issues
show
Bug introduced by
It seems like $settingsFile can also be of type null; however, parameter $path of dirname() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1755
		'__DIR__' => var_export(dirname(/** @scrutinizer ignore-type */ $settingsFile), true),
Loading history...
1756
	));
1757
1758
	// Prevents warnings about constants that are already defined.
1759
	$settingsText = preg_replace_callback(
1760
		'~\bdefine\s*\(\s*(["\'])(\w+)\1~',
1761
		function ($matches)
1762
		{
1763
			return 'define(\'' . md5(mt_rand()) . '\'';
1764
		},
1765
		$settingsText
1766
	);
1767
1768
	// Handle eval errors gracefully in both PHP 5 and PHP 7
1769
	try
1770
	{
1771
		if($settingsText !== '' && @eval($settingsText) === false)
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
1772
			throw new ErrorException('eval error');
1773
1774
		unset($mtime, $settingsFile, $settingsText);
1775
		$defined_vars = get_defined_vars();
1776
	}
1777
	catch (Throwable $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1778
	catch (ErrorException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1779
	if (isset($e))
1780
		return false;
1781
1782
	return $defined_vars;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defined_vars does not seem to be defined for all execution paths leading up to this point.
Loading history...
1783
}
1784
1785
/**
1786
 * Writes data to a file, optionally making a backup, while avoiding race conditions.
1787
 *
1788
 * @param string $file The filepath of the file where the data should be written.
1789
 * @param string $data The data to be written to $file.
1790
 * @param string $backup_file The filepath where the backup should be saved. Default null.
1791
 * @param int $mtime If modification time of $file is more recent than this Unix timestamp, the write operation will abort. Defaults to time that the script started execution.
1792
 * @param bool $append If true, the data will be appended instead of overwriting the existing content of the file. Default false.
1793
 * @return bool Whether the write operation succeeded or not.
1794
 */
1795
function safe_file_write($file, $data, $backup_file = null, $mtime = null, $append = false)
1796
{
1797
	// Sanity checks.
1798
	if (!file_exists($file) && !is_dir(dirname($file)))
1799
		return false;
1800
1801
	if (!is_int($mtime))
1802
		$mtime = $_SERVER['REQUEST_TIME'];
1803
1804
	$temp_dir = sm_temp_dir();
1805
1806
	// Our temp files.
1807
	$temp_sfile = tempnam($temp_dir, pathinfo($file, PATHINFO_FILENAME) . '.');
0 ignored issues
show
Bug introduced by
Are you sure pathinfo($file, PATHINFO_FILENAME) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1807
	$temp_sfile = tempnam($temp_dir, /** @scrutinizer ignore-type */ pathinfo($file, PATHINFO_FILENAME) . '.');
Loading history...
1808
1809
	if (!empty($backup_file))
1810
		$temp_bfile = tempnam($temp_dir, pathinfo($backup_file, PATHINFO_FILENAME) . '.');
0 ignored issues
show
Bug introduced by
Are you sure pathinfo($backup_file, PATHINFO_FILENAME) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1810
		$temp_bfile = tempnam($temp_dir, /** @scrutinizer ignore-type */ pathinfo($backup_file, PATHINFO_FILENAME) . '.');
Loading history...
1811
1812
	// We need write permissions.
1813
	$failed = false;
1814
	foreach (array($file, $backup_file) as $sf)
1815
	{
1816
		if (empty($sf))
1817
			continue;
1818
1819
		if (!file_exists($sf))
1820
			touch($sf);
1821
		elseif (!is_file($sf))
1822
			$failed = true;
1823
1824
		if (!$failed)
1825
			$failed = !smf_chmod($sf);
1826
	}
1827
1828
	// Now let's see if writing to a temp file succeeds.
1829
	if (!$failed && file_put_contents($temp_sfile, $data, LOCK_EX) !== strlen($data))
1830
		$failed = true;
1831
1832
	// Tests passed, so it's time to do the job.
1833
	if (!$failed)
1834
	{
1835
		// Back up the backup, just in case.
1836
		if (file_exists($backup_file))
0 ignored issues
show
Bug introduced by
It seems like $backup_file can also be of type null; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1836
		if (file_exists(/** @scrutinizer ignore-type */ $backup_file))
Loading history...
1837
			$temp_bfile_saved = @copy($backup_file, $temp_bfile);
0 ignored issues
show
Bug introduced by
It seems like $backup_file can also be of type null; however, parameter $from of copy() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1837
			$temp_bfile_saved = @copy(/** @scrutinizer ignore-type */ $backup_file, $temp_bfile);
Loading history...
Comprehensibility Best Practice introduced by
The variable $temp_bfile does not seem to be defined for all execution paths leading up to this point.
Loading history...
1838
1839
		// Make sure no one changed the file while we weren't looking.
1840
		clearstatcache();
1841
		if (filemtime($file) <= $mtime)
1842
		{
1843
			// Attempt to open the file.
1844
			$sfhandle = @fopen($file, 'c');
1845
1846
			// Let's do this thing!
1847
			if ($sfhandle !== false)
1848
			{
1849
				// Immediately get a lock.
1850
				flock($sfhandle, LOCK_EX);
1851
1852
				// Make sure the backup works before we do anything more.
1853
				$temp_sfile_saved = @copy($file, $temp_sfile);
1854
1855
				// Now write our data to the file.
1856
				if ($temp_sfile_saved)
1857
				{
1858
					if (empty($append))
1859
					{
1860
						ftruncate($sfhandle, 0);
1861
						rewind($sfhandle);
1862
					}
1863
1864
					$failed = fwrite($sfhandle, $data) !== strlen($data);
1865
				}
1866
				else
1867
					$failed = true;
1868
1869
				// If writing failed, put everything back the way it was.
1870
				if ($failed)
1871
				{
1872
					if (!empty($temp_sfile_saved))
1873
						@rename($temp_sfile, $file);
1874
1875
					if (!empty($temp_bfile_saved))
1876
						@rename($temp_bfile, $backup_file);
0 ignored issues
show
Bug introduced by
It seems like $backup_file can also be of type null; however, parameter $to of rename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1876
						@rename($temp_bfile, /** @scrutinizer ignore-type */ $backup_file);
Loading history...
1877
				}
1878
				// It worked, so make our temp backup the new permanent backup.
1879
				elseif (!empty($backup_file))
1880
					@rename($temp_sfile, $backup_file);
1881
1882
				// And we're done.
1883
				flock($sfhandle, LOCK_UN);
1884
				fclose($sfhandle);
1885
			}
1886
		}
1887
	}
1888
1889
	// We're done with these.
1890
	@unlink($temp_sfile);
1891
	@unlink($temp_bfile);
1892
1893
	if ($failed)
1894
		return false;
1895
1896
	// Even though on normal installations the filemtime should invalidate any cached version
1897
	// it seems that there are times it might not. So let's MAKE it dump the cache.
1898
	if (function_exists('opcache_invalidate'))
1899
		opcache_invalidate($file, true);
1900
1901
	return true;
1902
}
1903
1904
/**
1905
 * A wrapper around var_export whose output matches SMF coding conventions.
1906
 *
1907
 * @todo Add special handling for objects?
1908
 *
1909
 * @param mixed $var The variable to export
1910
 * @return mixed A PHP-parseable representation of the variable's value
1911
 */
1912
function smf_var_export($var)
1913
{
1914
	/*
1915
	 * Old versions of updateSettingsFile couldn't handle multi-line values.
1916
	 * Even though technically we can now, we'll keep arrays on one line for
1917
	 * the sake of backwards compatibility.
1918
	 */
1919
	if (is_array($var))
1920
	{
1921
		$return = array();
1922
1923
		foreach ($var as $key => $value)
1924
			$return[] = var_export($key, true) . ' => ' . smf_var_export($value);
1925
1926
		return 'array(' . implode(', ', $return) . ')';
1927
	}
1928
1929
	// For the same reason, replace literal returns and newlines with "\r" and "\n"
1930
	elseif (is_string($var) && (strpos($var, "\n") !== false || strpos($var, "\r") !== false))
1931
	{
1932
		return strtr(
1933
			preg_replace_callback(
1934
				'/[\r\n]+/',
1935
				function($m)
1936
				{
1937
					return '\' . "' . strtr($m[0], array("\r" => '\r', "\n" => '\n')) . '" . \'';
1938
				},
1939
				var_export($var, true)
1940
			),
1941
			array("'' . " => '', " . ''" => '')
1942
		);
1943
	}
1944
1945
	// We typically use lowercase true/false/null.
1946
	elseif (in_array(gettype($var), array('boolean', 'NULL')))
1947
		return strtolower(var_export($var, true));
1948
1949
	// Nothing special.
1950
	else
1951
		return var_export($var, true);
1952
};
1953
1954
/**
1955
 * Deletes all PHP comments from a string.
1956
 *
1957
 * @param string $code_str A string containing PHP code.
1958
 * @return string A string of PHP code with no comments in it.
1959
 */
1960
function strip_php_comments($code_str)
1961
{
1962
	// This is the faster, better way.
1963
	if (is_callable('token_get_all'))
1964
	{
1965
		$tokens = token_get_all($code_str);
1966
1967
		$parts = array();
1968
		foreach ($tokens as $token)
1969
		{
1970
			if (is_string($token))
1971
				$parts[] = $token;
1972
			else
1973
			{
1974
				list($id, $text) = $token;
1975
1976
				switch ($id) {
1977
					case T_COMMENT:
1978
					case T_DOC_COMMENT:
1979
						end($parts);
1980
						$prev_part = key($parts);
1981
1982
						// For the sake of tider output, trim any horizontal
1983
						// whitespace that immediately preceded the comment.
1984
						$parts[$prev_part] = rtrim($parts[$prev_part], "\t ");
1985
1986
						// For 'C' style comments, also trim one preceding
1987
						// line break, if present.
1988
						if (strpos($text, '/*') === 0)
1989
						{
1990
							if (substr($parts[$prev_part], -2) === "\r\n")
1991
								$parts[$prev_part] = substr($parts[$prev_part], 0, -2);
1992
							elseif (in_array(substr($parts[$prev_part], -1), array("\r", "\n")))
1993
								$parts[$prev_part] = substr($parts[$prev_part], 0, -1);
1994
						}
1995
1996
						break;
1997
1998
					default:
1999
						$parts[] = $text;
2000
						break;
2001
				}
2002
			}
2003
		}
2004
2005
		$code_str = implode('', $parts);
2006
2007
		return $code_str;
2008
	}
2009
2010
	// If the tokenizer extension has been disabled, do the job manually.
2011
2012
	// Leave any heredocs alone.
2013
	if (preg_match_all('/<<<([\'"]?)(\w+)\1?\R(.*?)\R\h*\2;$/ms', $code_str, $matches))
2014
	{
2015
		$heredoc_replacements = array();
2016
2017
		foreach ($matches[0] as $mkey => $heredoc)
2018
			$heredoc_replacements[$heredoc] = var_export(md5($matches[3][$mkey]), true) . ';';
2019
2020
		$code_str = strtr($code_str, $heredoc_replacements);
2021
	}
2022
2023
	// Split before everything that could possibly delimit a comment or a string.
2024
	$parts = preg_split('~(?=#+|/(?=/|\*)|\*/|\R|(?<!\\\)[\'"])~m', $code_str);
2025
2026
	$in_string = 0;
2027
	$in_comment = 0;
2028
	foreach ($parts as $partkey => $part)
2029
	{
2030
		$one_char = substr($part, 0, 1);
2031
		$two_char = substr($part, 0, 2);
2032
		$to_remove = 0;
2033
2034
		/*
2035
		 * Meaning of $in_string values:
2036
		 *	0: not in a string
2037
		 *	1: in a single quote string
2038
		 *	2: in a double quote string
2039
		 */
2040
		if ($one_char == "'")
2041
		{
2042
			if (!empty($in_comment))
2043
				$in_string = 0;
2044
			elseif (in_array($in_string, array(0, 1)))
2045
				$in_string = ($in_string ^ 1);
2046
		}
2047
		elseif ($one_char == '"')
2048
		{
2049
			if (!empty($in_comment))
2050
				$in_string = 0;
2051
			elseif (in_array($in_string, array(0, 2)))
2052
				$in_string = ($in_string ^ 2);
2053
		}
2054
2055
		/*
2056
		 * Meaning of $in_comment values:
2057
		 * 	0: not in a comment
2058
		 *	1: in a single line comment
2059
		 *	2: in a multi-line comment
2060
		 */
2061
		elseif ($one_char == '#' || $two_char == '//')
2062
		{
2063
			$in_comment = !empty($in_string) ? 0 : (empty($in_comment) ? 1 : $in_comment);
2064
2065
			if ($in_comment == 1)
2066
			{
2067
				$parts[$partkey - 1] = rtrim($parts[$partkey - 1], "\t ");
2068
2069
				if (substr($parts[$partkey - 1], -2) === "\r\n")
2070
					$parts[$partkey - 1] = substr($parts[$partkey - 1], 0, -2);
2071
				elseif (in_array(substr($parts[$partkey - 1], -1), array("\r", "\n")))
2072
					$parts[$partkey - 1] = substr($parts[$partkey - 1], 0, -1);
2073
			}
2074
		}
2075
		elseif ($two_char === "\r\n" || $one_char === "\r" || $one_char === "\n")
2076
		{
2077
			if ($in_comment == 1)
2078
				$in_comment = 0;
2079
		}
2080
		elseif ($two_char == '/*')
2081
		{
2082
			$in_comment = !empty($in_string) ? 0 : (empty($in_comment) ? 2 : $in_comment);
2083
2084
			if ($in_comment == 2)
2085
			{
2086
				$parts[$partkey - 1] = rtrim($parts[$partkey - 1], "\t ");
2087
2088
				if (substr($parts[$partkey - 1], -2) === "\r\n")
2089
					$parts[$partkey - 1] = substr($parts[$partkey - 1], 0, -2);
2090
				elseif (in_array(substr($parts[$partkey - 1], -1), array("\r", "\n")))
2091
					$parts[$partkey - 1] = substr($parts[$partkey - 1], 0, -1);
2092
			}
2093
		}
2094
		elseif ($two_char == '*/')
2095
		{
2096
			if ($in_comment == 2)
2097
			{
2098
				$in_comment = 0;
2099
2100
				// Delete the comment closing.
2101
				$to_remove = 2;
2102
			}
2103
		}
2104
2105
		if (empty($in_comment))
2106
			$parts[$partkey] = strlen($part) > $to_remove ? substr($part, $to_remove) : '';
2107
		else
2108
			$parts[$partkey] = '';
2109
	}
2110
2111
	$code_str = implode('', $parts);
2112
2113
	if (!empty($heredoc_replacements))
2114
		$code_str = strtr($code_str, array_flip($heredoc_replacements));
2115
2116
	return $code_str;
2117
}
2118
2119
/**
2120
 * Saves the time of the last db error for the error log
2121
 * - Done separately from updateSettingsFile to avoid race conditions
2122
 *   which can occur during a db error
2123
 * - If it fails Settings.php will assume 0
2124
 *
2125
 * @param int $time The timestamp of the last DB error
2126
 * @param bool True If we should update the current db_last_error context as well.  This may be useful in cases where the current context needs to know a error was logged since the last check.
2127
 * @return bool True If we could succesfully put the file or not.
2128
 */
2129
function updateDbLastError($time, $update = true)
2130
{
2131
	global $boarddir, $cachedir, $db_last_error;
2132
2133
	// Write out the db_last_error file with the error timestamp
2134
	if (!empty($cachedir) && is_writable($cachedir))
2135
		$errorfile = $cachedir . '/db_last_error.php';
2136
2137
	elseif (file_exists(dirname(__DIR__) . '/cache'))
2138
		$errorfile = dirname(__DIR__) . '/cache/db_last_error.php';
2139
2140
	else
2141
		$errorfile = dirname(__DIR__) . '/db_last_error.php';
2142
2143
	$result = file_put_contents($errorfile, '<' . '?' . "php\n" . '$db_last_error = ' . $time . ';' . "\n" . '?' . '>', LOCK_EX);
2144
2145
	@touch($boarddir . '/' . 'Settings.php');
2146
2147
	// Unless requested, we should update $db_last_error as well.
2148
	if ($update)
2149
		$db_last_error = $time;
2150
2151
	// We  do a loose match here rather than strict (!==) as 0 is also false.
2152
	return $result != false;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $result of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
2153
}
2154
2155
/**
2156
 * Saves the admin's current preferences to the database.
2157
 */
2158
function updateAdminPreferences()
2159
{
2160
	global $options, $context, $smcFunc, $settings, $user_info;
2161
2162
	// This must exist!
2163
	if (!isset($context['admin_preferences']))
2164
		return false;
2165
2166
	// This is what we'll be saving.
2167
	$options['admin_preferences'] = $smcFunc['json_encode']($context['admin_preferences']);
2168
2169
	// Just check we haven't ended up with something theme exclusive somehow.
2170
	$smcFunc['db_query']('', '
2171
		DELETE FROM {db_prefix}themes
2172
		WHERE id_theme != {int:default_theme}
2173
			AND variable = {string:admin_preferences}',
2174
		array(
2175
			'default_theme' => 1,
2176
			'admin_preferences' => 'admin_preferences',
2177
		)
2178
	);
2179
2180
	// Update the themes table.
2181
	$smcFunc['db_insert']('replace',
2182
		'{db_prefix}themes',
2183
		array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
2184
		array($user_info['id'], 1, 'admin_preferences', $options['admin_preferences']),
2185
		array('id_member', 'id_theme', 'variable')
2186
	);
2187
2188
	// Make sure we invalidate any cache.
2189
	cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 0);
2190
}
2191
2192
/**
2193
 * Send all the administrators a lovely email.
2194
 * - loads all users who are admins or have the admin forum permission.
2195
 * - uses the email template and replacements passed in the parameters.
2196
 * - sends them an email.
2197
 *
2198
 * @param string $template Which email template to use
2199
 * @param array $replacements An array of items to replace the variables in the template
2200
 * @param array $additional_recipients An array of arrays of info for additional recipients. Should have 'id', 'email' and 'name' for each.
2201
 */
2202
function emailAdmins($template, $replacements = array(), $additional_recipients = array())
2203
{
2204
	global $smcFunc, $sourcedir, $language, $modSettings;
2205
2206
	// We certainly want this.
2207
	require_once($sourcedir . '/Subs-Post.php');
2208
2209
	// Load all members which are effectively admins.
2210
	require_once($sourcedir . '/Subs-Members.php');
2211
	$members = membersAllowedTo('admin_forum');
2212
2213
	// Load their alert preferences
2214
	require_once($sourcedir . '/Subs-Notify.php');
2215
	$prefs = getNotifyPrefs($members, 'announcements', true);
2216
2217
	$request = $smcFunc['db_query']('', '
2218
		SELECT id_member, member_name, real_name, lngfile, email_address
2219
		FROM {db_prefix}members
2220
		WHERE id_member IN({array_int:members})',
2221
		array(
2222
			'members' => $members,
2223
		)
2224
	);
2225
	$emails_sent = array();
2226
	while ($row = $smcFunc['db_fetch_assoc']($request))
2227
	{
2228
		if (empty($prefs[$row['id_member']]['announcements']))
2229
			continue;
2230
2231
		// Stick their particulars in the replacement data.
2232
		$replacements['IDMEMBER'] = $row['id_member'];
2233
		$replacements['REALNAME'] = $row['member_name'];
2234
		$replacements['USERNAME'] = $row['real_name'];
2235
2236
		// Load the data from the template.
2237
		$emaildata = loadEmailTemplate($template, $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
2238
2239
		// Then send the actual email.
2240
		sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, $template, $emaildata['is_html'], 1);
2241
2242
		// Track who we emailed so we don't do it twice.
2243
		$emails_sent[] = $row['email_address'];
2244
	}
2245
	$smcFunc['db_free_result']($request);
2246
2247
	// Any additional users we must email this to?
2248
	if (!empty($additional_recipients))
2249
		foreach ($additional_recipients as $recipient)
2250
		{
2251
			if (in_array($recipient['email'], $emails_sent))
2252
				continue;
2253
2254
			$replacements['IDMEMBER'] = $recipient['id'];
2255
			$replacements['REALNAME'] = $recipient['name'];
2256
			$replacements['USERNAME'] = $recipient['name'];
2257
2258
			// Load the template again.
2259
			$emaildata = loadEmailTemplate($template, $replacements, empty($recipient['lang']) || empty($modSettings['userLanguage']) ? $language : $recipient['lang']);
2260
2261
			// Send off the email.
2262
			sendmail($recipient['email'], $emaildata['subject'], $emaildata['body'], null, $template, $emaildata['is_html'], 1);
2263
		}
2264
}
2265
2266
/**
2267
 * Locates the most appropriate temp directory.
2268
 *
2269
 * Systems using `open_basedir` restrictions may receive errors with
2270
 * `sys_get_temp_dir()` due to misconfigurations on servers. Other
2271
 * cases sys_temp_dir may not be set to a safe value. Additionally
2272
 * `sys_get_temp_dir` may use a readonly directory. This attempts to
2273
 * find a working temp directory that is accessible under the
2274
 * restrictions and is writable to the web service account.
2275
 *
2276
 * Directories checked against `open_basedir`:
2277
 *
2278
 * - `sys_get_temp_dir()`
2279
 * - `upload_tmp_dir`
2280
 * - `session.save_path`
2281
 * - `cachedir`
2282
 *
2283
 * @return string
2284
*/
2285
function sm_temp_dir()
2286
{
2287
	global $cachedir;
2288
2289
	static $temp_dir = null;
2290
2291
	// Already did this.
2292
	if (!empty($temp_dir))
2293
		return $temp_dir;
2294
2295
	// Temp Directory options order.
2296
	$temp_dir_options = array(
2297
		0 => 'sys_get_temp_dir',
2298
		1 => 'upload_tmp_dir',
2299
		2 => 'session.save_path',
2300
		3 => 'cachedir'
2301
	);
2302
2303
	// Determine if we should detect a restriction and what restrictions that may be.
2304
	$open_base_dir = ini_get('open_basedir');
2305
	$restriction = !empty($open_base_dir) ? explode(':', $open_base_dir) : false;
2306
2307
	// Prevent any errors as we search.
2308
	$old_error_reporting = error_reporting(0);
2309
2310
	// Search for a working temp directory.
2311
	foreach ($temp_dir_options as $id_temp => $temp_option)
2312
	{
2313
		switch ($temp_option) {
2314
			case 'cachedir':
2315
				$possible_temp = rtrim($cachedir, '/');
2316
				break;
2317
2318
			case 'session.save_path':
2319
				$possible_temp = rtrim(ini_get('session.save_path'), '/');
2320
				break;
2321
2322
			case 'upload_tmp_dir':
2323
				$possible_temp = rtrim(ini_get('upload_tmp_dir'), '/');
2324
				break;
2325
2326
			default:
2327
				$possible_temp = sys_get_temp_dir();
2328
				break;
2329
		}
2330
2331
		// Check if we have a restriction preventing this from working.
2332
		if ($restriction)
2333
		{
2334
			foreach ($restriction as $dir)
2335
			{
2336
				if (strpos($possible_temp, $dir) !== false && is_writable($possible_temp))
2337
				{
2338
					$temp_dir = $possible_temp;
2339
					break;
2340
				}
2341
			}
2342
		}
2343
		// No restrictions, but need to check for writable status.
2344
		elseif (is_writable($possible_temp))
2345
		{
2346
			$temp_dir = $possible_temp;
2347
			break;
2348
		}
2349
	}
2350
2351
	// Fall back to sys_get_temp_dir even though it won't work, so we have something.
2352
	if (empty($temp_dir))
2353
		$temp_dir = sys_get_temp_dir();
2354
2355
	// Fix the path.
2356
	$temp_dir = substr($temp_dir, -1) === '/' ? $temp_dir : $temp_dir . '/';
2357
2358
	// Put things back.
2359
	error_reporting($old_error_reporting);
2360
2361
	return $temp_dir;
2362
}
2363
2364
?>