Issues (1065)

Sources/Subs-Admin.php (2 issues)

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 2025 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1.5
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
 * Describes properties of all known Settings.php variables and other content.
291
 * Helper for updateSettingsFile(); also called by saveSettings().
292
 *
293
 * @return array Descriptions of all known Settings.php content
294
 */
295
function get_settings_defs()
296
{
297
	/*
298
	 * A big, fat array to define properties of all the Settings.php variables
299
	 * and other content like code blocks.
300
	 *
301
	 * - String keys are used to identify actual variables.
302
	 *
303
	 * - Integer keys are used for content not connected to any particular
304
	 *   variable, such as code blocks or the license block.
305
	 *
306
	 * - The content of the 'text' element is simply printed out, if it is used
307
	 *   at all. Use it for comments or to insert code blocks, etc.
308
	 *
309
	 * - The 'default' element, not surprisingly, gives a default value for
310
	 *   the variable.
311
	 *
312
	 * - The 'type' element defines the expected variable type or types. If
313
	 *   more than one type is allowed, this should be an array listing them.
314
	 *   Types should match the possible types returned by gettype().
315
	 *
316
	 * - If 'raw_default' is true, the default should be printed directly,
317
	 *   rather than being handled as a string. Use it if the default contains
318
	 *   code, e.g. 'dirname(__FILE__)'
319
	 *
320
	 * - If 'required' is true and a value for the variable is undefined,
321
	 *   the update will be aborted. (The only exception is during the SMF
322
	 *   installation process.)
323
	 *
324
	 * - If 'auto_delete' is 1 or true and the variable is empty, the variable
325
	 *   will be deleted from Settings.php. If 'auto_delete' is 0/false/null,
326
	 *   the variable will never be deleted. If 'auto_delete' is 2, behaviour
327
	 *   depends on $rebuild: if $rebuild is true, 'auto_delete' == 2 behaves
328
	 *   like 'auto_delete' == 1; if $rebuild is false, 'auto_delete' == 2
329
	 *   behaves like 'auto_delete' == 0.
330
	 *
331
	 * - The 'is_password' element indicates that a value is a password. This
332
	 *   is used primarily to tell SMF how to interpret input when the value
333
	 *   is being set to a new value.
334
	 *
335
	 * - The optional 'search_pattern' element defines a custom regular
336
	 *   expression to search for the existing entry in the file. This is
337
	 *   primarily useful for code blocks rather than variables.
338
	 *
339
	 * - The optional 'replace_pattern' element defines a custom regular
340
	 *   expression to decide where the replacement entry should be inserted.
341
	 *   Note: 'replace_pattern' should be avoided unless ABSOLUTELY necessary.
342
	 */
343
	$settings_defs = array(
344
		array(
345
			'text' => implode("\n", array(
346
				'',
347
				'/**',
348
				' * The settings file contains all of the basic settings that need to be present when a database/cache is not available.',
349
				' *',
350
				' * Simple Machines Forum (SMF)',
351
				' *',
352
				' * @package SMF',
353
				' * @author Simple Machines https://www.simplemachines.org',
354
				' * @copyright ' . SMF_SOFTWARE_YEAR . ' Simple Machines and individual contributors',
355
				' * @license https://www.simplemachines.org/about/smf/license.php BSD',
356
				' *',
357
				' * @version ' . SMF_VERSION,
358
				' */',
359
				'',
360
			)),
361
			'search_pattern' => '~/\*\*.*?@package\h+SMF\b.*?\*/\n{0,2}~s',
362
		),
363
		'maintenance' => array(
364
			'text' => implode("\n", array(
365
				'',
366
				'########## Maintenance ##########',
367
				'/**',
368
				' * The maintenance "mode"',
369
				' * Set to 1 to enable Maintenance Mode, 2 to make the forum untouchable. (you\'ll have to make it 0 again manually!)',
370
				' * 0 is default and disables maintenance mode.',
371
				' *',
372
				' * @var int 0, 1, 2',
373
				' * @global int $maintenance',
374
				' */',
375
			)),
376
			'default' => 0,
377
			'type' => 'integer',
378
		),
379
		'mtitle' => array(
380
			'text' => implode("\n", array(
381
				'/**',
382
				' * Title for the Maintenance Mode message.',
383
				' *',
384
				' * @var string',
385
				' * @global int $mtitle',
386
				' */',
387
			)),
388
			'default' => 'Maintenance Mode',
389
			'type' => 'string',
390
		),
391
		'mmessage' => array(
392
			'text' => implode("\n", array(
393
				'/**',
394
				' * Description of why the forum is in maintenance mode.',
395
				' *',
396
				' * @var string',
397
				' * @global string $mmessage',
398
				' */',
399
			)),
400
			'default' => 'Okay faithful users...we\'re attempting to restore an older backup of the database...news will be posted once we\'re back!',
401
			'type' => 'string',
402
		),
403
		'mbname' => array(
404
			'text' => implode("\n", array(
405
				'',
406
				'########## Forum Info ##########',
407
				'/**',
408
				' * The name of your forum.',
409
				' *',
410
				' * @var string',
411
				' */',
412
			)),
413
			'default' => 'My Community',
414
			'type' => 'string',
415
		),
416
		'language' => array(
417
			'text' => implode("\n", array(
418
				'/**',
419
				' * The default language file set for the forum.',
420
				' *',
421
				' * @var string',
422
				' */',
423
			)),
424
			'default' => 'english',
425
			'type' => 'string',
426
		),
427
		'boardurl' => array(
428
			'text' => implode("\n", array(
429
				'/**',
430
				' * URL to your forum\'s folder. (without the trailing /!)',
431
				' *',
432
				' * @var string',
433
				' */',
434
			)),
435
			'default' => 'http://127.0.0.1/smf',
436
			'type' => 'string',
437
		),
438
		'webmaster_email' => array(
439
			'text' => implode("\n", array(
440
				'/**',
441
				' * Email address to send emails from. (like [email protected].)',
442
				' *',
443
				' * @var string',
444
				' */',
445
			)),
446
			'default' => '[email protected]',
447
			'type' => 'string',
448
		),
449
		'cookiename' => array(
450
			'text' => implode("\n", array(
451
				'/**',
452
				' * Name of the cookie to set for authentication.',
453
				' *',
454
				' * @var string',
455
				' */',
456
			)),
457
			'default' => 'SMFCookie11',
458
			'type' => 'string',
459
		),
460
		'auth_secret' => array(
461
			'text' => implode("\n", array(
462
				'/**',
463
				' * Secret key used to create and verify cookies, tokens, etc.',
464
				' * Do not change this unless absolutely necessary, and NEVER share it.',
465
				' *',
466
				' * Note: Changing this will immediately log out all members of your forum',
467
				' * and break the token-based links in all previous email notifications,',
468
				' * among other possible effects.',
469
				' *',
470
				' * @var string',
471
				' */',
472
			)),
473
			'default' => null,
474
			'auto_delete' => 1,
475
			'type' => 'string',
476
		),
477
		'db_type' => array(
478
			'text' => implode("\n", array(
479
				'',
480
				'########## Database Info ##########',
481
				'/**',
482
				' * The database type',
483
				' * Default options: mysql, postgresql',
484
				' *',
485
				' * @var string',
486
				' */',
487
			)),
488
			'default' => 'mysql',
489
			'type' => 'string',
490
		),
491
		'db_port' => array(
492
			'text' => implode("\n", array(
493
				'/**',
494
				' * The database port',
495
				' * 0 to use default port for the database type',
496
				' *',
497
				' * @var int',
498
				' */',
499
			)),
500
			'default' => 0,
501
			'type' => 'integer',
502
		),
503
		'db_server' => array(
504
			'text' => implode("\n", array(
505
				'/**',
506
				' * The server to connect to (or a Unix socket)',
507
				' *',
508
				' * @var string',
509
				' */',
510
			)),
511
			'default' => 'localhost',
512
			'required' => true,
513
			'type' => 'string',
514
		),
515
		'db_name' => array(
516
			'text' => implode("\n", array(
517
				'/**',
518
				' * The database name',
519
				' *',
520
				' * @var string',
521
				' */',
522
			)),
523
			'default' => 'smf',
524
			'required' => true,
525
			'type' => 'string',
526
		),
527
		'db_user' => array(
528
			'text' => implode("\n", array(
529
				'/**',
530
				' * Database username',
531
				' *',
532
				' * @var string',
533
				' */',
534
			)),
535
			'default' => 'root',
536
			'required' => true,
537
			'type' => 'string',
538
		),
539
		'db_passwd' => array(
540
			'text' => implode("\n", array(
541
				'/**',
542
				' * Database password',
543
				' *',
544
				' * @var string',
545
				' */',
546
			)),
547
			'default' => '',
548
			'required' => true,
549
			'type' => 'string',
550
			'is_password' => true,
551
		),
552
		'ssi_db_user' => array(
553
			'text' => implode("\n", array(
554
				'/**',
555
				' * Database user for when connecting with SSI',
556
				' *',
557
				' * @var string',
558
				' */',
559
			)),
560
			'default' => '',
561
			'type' => 'string',
562
		),
563
		'ssi_db_passwd' => array(
564
			'text' => implode("\n", array(
565
				'/**',
566
				' * Database password for when connecting with SSI',
567
				' *',
568
				' * @var string',
569
				' */',
570
			)),
571
			'default' => '',
572
			'type' => 'string',
573
			'is_password' => true,
574
		),
575
		'db_prefix' => array(
576
			'text' => implode("\n", array(
577
				'/**',
578
				' * A prefix to put in front of your table names.',
579
				' * This helps to prevent conflicts',
580
				' *',
581
				' * @var string',
582
				' */',
583
			)),
584
			'default' => 'smf_',
585
			'required' => true,
586
			'type' => 'string',
587
		),
588
		'db_persist' => array(
589
			'text' => implode("\n", array(
590
				'/**',
591
				' * Use a persistent database connection',
592
				' *',
593
				' * @var bool',
594
				' */',
595
			)),
596
			'default' => false,
597
			'type' => 'boolean',
598
		),
599
		'db_error_send' => array(
600
			'text' => implode("\n", array(
601
				'/**',
602
				' * Send emails on database connection error',
603
				' *',
604
				' * @var bool',
605
				' */',
606
			)),
607
			'default' => false,
608
			'type' => 'boolean',
609
		),
610
		'db_mb4' => array(
611
			'text' => implode("\n", array(
612
				'/**',
613
				' * Override the default behavior of the database layer for mb4 handling',
614
				' * null keep the default behavior untouched',
615
				' *',
616
				' * @var null|bool',
617
				' */',
618
			)),
619
			'default' => null,
620
			'type' => array('NULL', 'boolean'),
621
		),
622
		'cache_accelerator' => array(
623
			'text' => implode("\n", array(
624
				'',
625
				'########## Cache Info ##########',
626
				'/**',
627
				' * Select a cache system. You want to leave this up to the cache area of the admin panel for',
628
				' * proper detection of memcached, output_cache, or smf file system',
629
				' * (you can add more with a mod).',
630
				' *',
631
				' * @var string',
632
				' */',
633
			)),
634
			'default' => '',
635
			'type' => 'string',
636
		),
637
		'cache_enable' => array(
638
			'text' => implode("\n", array(
639
				'/**',
640
				' * The level at which you would like to cache. Between 0 (off) through 3 (cache a lot).',
641
				' *',
642
				' * @var int',
643
				' */',
644
			)),
645
			'default' => 0,
646
			'type' => 'integer',
647
		),
648
		'cache_memcached' => array(
649
			'text' => implode("\n", array(
650
				'/**',
651
				' * This is only used for memcache / memcached. Should be a string of \'server:port,server:port\'',
652
				' *',
653
				' * @var array',
654
				' */',
655
			)),
656
			'default' => '',
657
			'type' => 'string',
658
		),
659
		'cachedir' => array(
660
			'text' => implode("\n", array(
661
				'/**',
662
				' * This is only for the \'smf\' file cache system. It is the path to the cache directory.',
663
				' * It is also recommended that you place this in /tmp/ if you are going to use this.',
664
				' *',
665
				' * @var string',
666
				' */',
667
			)),
668
			'default' => 'dirname(__FILE__) . \'/cache\'',
669
			'raw_default' => true,
670
			'type' => 'string',
671
		),
672
		'cachedir_sqlite' => array(
673
			'text' => implode("\n", array(
674
				'/**',
675
				' * This is only for SQLite3 cache system. It is the path to the directory where the SQLite3',
676
				' * database file will be saved.',
677
				' *',
678
				' * @var string',
679
				' */',
680
			)),
681
			'default' => '',
682
			'auto_delete' => 2,
683
			'type' => 'string',
684
		),
685
		'image_proxy_enabled' => array(
686
			'text' => implode("\n", array(
687
				'',
688
				'########## Image Proxy ##########',
689
				'# This is done entirely in Settings.php to avoid loading the DB while serving the images',
690
				'/**',
691
				' * Whether the proxy is enabled or not',
692
				' *',
693
				' * @var bool',
694
				' */',
695
			)),
696
			'default' => true,
697
			'type' => 'boolean',
698
		),
699
		'image_proxy_secret' => array(
700
			'text' => implode("\n", array(
701
				'/**',
702
				' * Secret key to be used by the proxy',
703
				' *',
704
				' * @var string',
705
				' */',
706
			)),
707
			'default' => 'smfisawesome',
708
			'type' => 'string',
709
		),
710
		'image_proxy_maxsize' => array(
711
			'text' => implode("\n", array(
712
				'/**',
713
				' * Maximum file size (in KB) for individual files',
714
				' *',
715
				' * @var int',
716
				' */',
717
			)),
718
			'default' => 5192,
719
			'type' => 'integer',
720
		),
721
		'boarddir' => array(
722
			'text' => implode("\n", array(
723
				'',
724
				'########## Directories/Files ##########',
725
				'# Note: These directories do not have to be changed unless you move things.',
726
				'/**',
727
				' * The absolute path to the forum\'s folder. (not just \'.\'!)',
728
				' *',
729
				' * @var string',
730
				' */',
731
			)),
732
			'default' => 'dirname(__FILE__)',
733
			'raw_default' => true,
734
			'type' => 'string',
735
		),
736
		'sourcedir' => array(
737
			'text' => implode("\n", array(
738
				'/**',
739
				' * Path to the Sources directory.',
740
				' *',
741
				' * @var string',
742
				' */',
743
			)),
744
			'default' => 'dirname(__FILE__) . \'/Sources\'',
745
			'raw_default' => true,
746
			'type' => 'string',
747
		),
748
		'packagesdir' => array(
749
			'text' => implode("\n", array(
750
				'/**',
751
				' * Path to the Packages directory.',
752
				' *',
753
				' * @var string',
754
				' */',
755
			)),
756
			'default' => 'dirname(__FILE__) . \'/Packages\'',
757
			'raw_default' => true,
758
			'type' => 'string',
759
		),
760
		'tasksdir' => array(
761
			'text' => implode("\n", array(
762
				'/**',
763
				' * Path to the tasks directory.',
764
				' *',
765
				' * @var string',
766
				' */',
767
			)),
768
			'default' => '$sourcedir . \'/tasks\'',
769
			'raw_default' => true,
770
			'type' => 'string',
771
		),
772
		array(
773
			'text' => implode("\n", array(
774
				'',
775
				'# Make sure the paths are correct... at least try to fix them.',
776
				'if (!is_dir(realpath($boarddir)) && file_exists(dirname(__FILE__) . \'/agreement.txt\'))',
777
				'	$boarddir = dirname(__FILE__);',
778
				'if (!is_dir(realpath($sourcedir)) && is_dir($boarddir . \'/Sources\'))',
779
				'	$sourcedir = $boarddir . \'/Sources\';',
780
				'if (!is_dir(realpath($tasksdir)) && is_dir($sourcedir . \'/tasks\'))',
781
				'	$tasksdir = $sourcedir . \'/tasks\';',
782
				'if (!is_dir(realpath($packagesdir)) && is_dir($boarddir . \'/Packages\'))',
783
				'	$packagesdir = $boarddir . \'/Packages\';',
784
				'if (!is_dir(realpath($cachedir)) && is_dir($boarddir . \'/cache\'))',
785
				'	$cachedir = $boarddir . \'/cache\';',
786
			)),
787
			'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',
788
		),
789
		'db_character_set' => array(
790
			'text' => implode("\n", array(
791
				'',
792
				'######### Legacy Settings #########',
793
				'# UTF-8 is now the only character set supported in 2.1.',
794
			)),
795
			'default' => 'utf8',
796
			'type' => 'string',
797
		),
798
		'db_show_debug' => array(
799
			'text' => implode("\n", array(
800
				'',
801
				'######### Developer Settings #########',
802
				'# Show debug info.',
803
			)),
804
			'default' => false,
805
			'auto_delete' => 2,
806
			'type' => 'boolean',
807
		),
808
		array(
809
			'text' => implode("\n", array(
810
				'',
811
				'########## Error-Catching ##########',
812
				'# Note: You shouldn\'t touch these settings.',
813
				'if (file_exists((isset($cachedir) ? $cachedir : dirname(__FILE__)) . \'/db_last_error.php\'))',
814
				'	include((isset($cachedir) ? $cachedir : dirname(__FILE__)) . \'/db_last_error.php\');',
815
				'',
816
				'if (!isset($db_last_error))',
817
				'{',
818
				'	// File does not exist so lets try to create it',
819
				'	file_put_contents((isset($cachedir) ? $cachedir : dirname(__FILE__)) . \'/db_last_error.php\', \'<\' . \'?\' . "php\n" . \'$db_last_error = 0;\' . "\n" . \'?\' . \'>\');',
820
				'	$db_last_error = 0;',
821
				'}',
822
			)),
823
			// Designed to match both 2.0 and 2.1 versions of this code.
824
			'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',
825
		),
826
		// Temporary variable used during the upgrade process.
827
		'upgradeData' => array(
828
			'default' => '',
829
			'auto_delete' => 1,
830
			'type' => 'string',
831
		),
832
		// Temporary variable used while installing or uninstalling packages.
833
		'package_installing' => [
834
			'default' => false,
835
			'auto_delete' => 1,
836
			'type' => 'boolean',
837
		],
838
		// This should be removed if found.
839
		'db_last_error' => array(
840
			'default' => 0,
841
			'auto_delete' => 1,
842
			'type' => 'integer',
843
		),
844
	);
845
846
	// Allow mods the option to define comments, defaults, etc., for their settings.
847
	// Check if function exists, in case we are calling from installer or upgrader.
848
	if (function_exists('call_integration_hook'))
849
		call_integration_hook('integrate_update_settings_file', array(&$settings_defs));
850
851
	return $settings_defs;
852
}
853
854
/**
855
 * Update the Settings.php file.
856
 *
857
 * The most important function in this file for mod makers happens to be the
858
 * updateSettingsFile() function, but it shouldn't be used often anyway.
859
 *
860
 * - Updates the Settings.php file with the changes supplied in config_vars.
861
 *
862
 * - Expects config_vars to be an associative array, with the keys as the
863
 *   variable names in Settings.php, and the values the variable values.
864
 *
865
 * - Correctly formats the values using smf_var_export().
866
 *
867
 * - Restores standard formatting of the file, if $rebuild is true.
868
 *
869
 * - Checks for changes to db_last_error and passes those off to a separate
870
 *   handler.
871
 *
872
 * - Creates a backup file and will use it should the writing of the
873
 *   new settings file fail.
874
 *
875
 * - Tries to intelligently trim quotes and remove slashes from string values.
876
 *   This is done for backwards compatibility purposes (old versions of this
877
 *   function expected strings to have been manually escaped and quoted). This
878
 *   behaviour can be controlled by the $keep_quotes parameter.
879
 *
880
 * MOD AUTHORS: If you are adding a setting to Settings.php, you should use the
881
 * integrate_update_settings_file hook to define it in get_settings_defs().
882
 *
883
 * @param array $config_vars An array of one or more variables to update.
884
 * @param bool|null $keep_quotes Whether to strip slashes & trim quotes from string values. Defaults to auto-detection.
885
 * @param bool $rebuild If true, attempts to rebuild with standard format. Default false.
886
 * @return bool True on success, false on failure.
887
 */
888
function updateSettingsFile($config_vars, $keep_quotes = null, $rebuild = false)
889
{
890
	// In this function we intentionally don't declare any global variables.
891
	// This allows us to work with everything cleanly.
892
893
	static $mtime;
894
895
	// Should we try to unescape the strings?
896
	if (empty($keep_quotes))
897
	{
898
		foreach ($config_vars as $var => $val)
899
		{
900
			if (is_string($val) && ($keep_quotes === false || strpos($val, '\'') === 0 && strrpos($val, '\'') === strlen($val) - 1))
901
				$config_vars[$var] = trim(stripcslashes($val), '\'');
902
		}
903
	}
904
905
	// Updating the db_last_error, then don't mess around with Settings.php
906
	if (isset($config_vars['db_last_error']))
907
	{
908
		updateDbLastError($config_vars['db_last_error']);
909
910
		if (count($config_vars) === 1 && empty($rebuild))
911
			return true;
912
913
		// Make sure we delete this from Settings.php, if present.
914
		$config_vars['db_last_error'] = 0;
915
	}
916
917
	// Rebuilding should not be undertaken lightly, so we're picky about the parameter.
918
	if (!is_bool($rebuild))
919
		$rebuild = false;
920
921
	$mtime = isset($mtime) ? (int) $mtime : (defined('TIME_START') ? TIME_START : $_SERVER['REQUEST_TIME']);
922
923
	/*****************
924
	 * PART 1: Setup *
925
	 *****************/
926
927
	// Typically Settings.php is in $boarddir, but maybe this is a custom setup...
928
	foreach (get_included_files() as $settingsFile)
929
		if (basename($settingsFile) === 'Settings.php')
930
			break;
931
932
	// Fallback in case Settings.php isn't loaded (e.g. while installing)
933
	if (basename($settingsFile) !== 'Settings.php')
934
		$settingsFile = (!empty($GLOBALS['boarddir']) && @realpath($GLOBALS['boarddir']) ? $GLOBALS['boarddir'] : (!empty($_SERVER['SCRIPT_FILENAME']) ? dirname($_SERVER['SCRIPT_FILENAME']) : dirname(__DIR__))) . '/Settings.php';
935
936
	// File not found? Attempt an emergency on-the-fly fix!
937
	if (!file_exists($settingsFile))
938
		@touch($settingsFile);
939
940
	// When was Settings.php last changed?
941
	$last_settings_change = filemtime($settingsFile);
942
943
	// Get the current values of everything in Settings.php.
944
	$settings_vars = get_current_settings($mtime, $settingsFile);
945
946
	// If Settings.php is empty for some reason, see if we can use the backup.
947
	if (empty($settings_vars) && file_exists(dirname($settingsFile) . '/Settings_bak.php'))
948
		$settings_vars = get_current_settings($mtime, dirname($settingsFile) . '/Settings_bak.php');
949
950
	// False means there was a problem with the file and we can't safely continue.
951
	if ($settings_vars === false)
952
		return false;
953
954
	// It works best to set everything afresh.
955
	$new_settings_vars = array_merge($settings_vars, $config_vars);
956
957
	// Are we using UTF-8?
958
	$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));
959
960
	// Get our definitions for all known Settings.php variables and other content.
961
	$settings_defs = get_settings_defs();
962
963
	// If Settings.php is empty or invalid, try to recover using whatever is in $GLOBALS.
964
	if ($settings_vars === array())
965
	{
966
		foreach ($settings_defs as $var => $setting_def)
967
			if (isset($GLOBALS[$var]))
968
				$settings_vars[$var] = $GLOBALS[$var];
969
970
		$new_settings_vars = array_merge($settings_vars, $config_vars);
971
	}
972
973
	// During install/upgrade, don't set anything until we're ready for it.
974
	if (defined('SMF_INSTALLING') && empty($rebuild))
975
	{
976
		foreach ($settings_defs as $var => $setting_def)
977
			if (!in_array($var, array_keys($new_settings_vars)) && !is_int($var))
978
				unset($settings_defs[$var]);
979
	}
980
981
	/*******************************
982
	 * PART 2: Build substitutions *
983
	 *******************************/
984
985
	$type_regex = array(
986
		'string' =>
987
			'(?:' .
988
				// match the opening quotation mark...
989
				'(["\'])' .
990
				// then any number of other characters or escaped quotation marks...
991
				'(?:.(?!\\1)|\\\(?=\\1))*.?' .
992
				// then the closing quotation mark.
993
				'\\1' .
994
				// Maybe there's a second string concatenated to this one.
995
				'(?:\s*\.\s*)*' .
996
			')+',
997
		// Some numeric values might have been stored as strings.
998
		'integer' =>  '["\']?[+-]?\d+["\']?',
999
		'double' =>  '["\']?[+-]?\d+\.\d+([Ee][+-]\d+)?["\']?',
1000
		// Some boolean values might have been stored as integers.
1001
		'boolean' =>  '(?i:TRUE|FALSE|(["\']?)[01]\b\\1)',
1002
		'NULL' =>  '(?i:NULL)',
1003
		// These use a PCRE subroutine to match nested arrays.
1004
		'array' =>  'array\s*(\((?'.'>[^()]|(?1))*\))',
1005
		'object' =>  '\w+::__set_state\(array\s*(\((?'.'>[^()]|(?1))*\))\)',
1006
	);
1007
1008
	/*
1009
	 * The substitutions take place in one of two ways:
1010
	 *
1011
	 *  1: The search_pattern regex finds a string in Settings.php, which is
1012
	 *     temporarily replaced by a placeholder. Once all the placeholders
1013
	 *     have been inserted, each is replaced by the final replacement string
1014
	 *     that we want to use. This is the standard method.
1015
	 *
1016
	 *  2: The search_pattern regex finds a string in Settings.php, which is
1017
	 *     then deleted by replacing it with an empty placeholder. Then after
1018
	 *     all the real placeholders have been dealt with, the replace_pattern
1019
	 *     regex finds where to insert the final replacement string that we
1020
	 *     want to use. This method is for special cases.
1021
	 */
1022
	$prefix = mt_rand() . '-';
1023
	$neg_index = -1;
1024
	$substitutions = array(
1025
		$neg_index-- => array(
1026
			'search_pattern' => '~^\s*<\?(php\b)?\n?~',
1027
			'placeholder' => '',
1028
			'replace_pattern' => '~^~',
1029
			'replacement' => '<' . "?php\n",
1030
		),
1031
		$neg_index-- => array(
1032
			'search_pattern' => '~\S\K\s*(\?' . '>)?\s*$~',
1033
			'placeholder' => "\n" . md5($prefix . '?' . '>'),
1034
			'replacement' => "\n\n?" . '>',
1035
		),
1036
		// Remove the code that redirects to the installer.
1037
		$neg_index-- => array(
1038
			'search_pattern' => '~^if\s*\(file_exists\(dirname\(__FILE__\)\s*\.\s*\'/install\.php\'\)\)\s*(?:({(?'.'>[^{}]|(?1))*})\h*|header(\((?' . '>[^()]|(?2))*\));\n)~m',
1039
			'placeholder' => '',
1040
		),
1041
	);
1042
1043
	if (defined('SMF_INSTALLING'))
1044
		$substitutions[$neg_index--] = array(
1045
			'search_pattern' => '~/\*.*?SMF\s+1\.\d.*?\*/~s',
1046
			'placeholder' => '',
1047
		);
1048
1049
	foreach ($settings_defs as $var => $setting_def)
1050
	{
1051
		$placeholder = md5($prefix . $var);
1052
		$replacement = '';
1053
1054
		if (!empty($setting_def['text']))
1055
		{
1056
			// Special handling for the license block: always at the beginning.
1057
			if (strpos($setting_def['text'], "* @package SMF\n") !== false)
1058
			{
1059
				$substitutions[$var]['search_pattern'] = $setting_def['search_pattern'];
1060
				$substitutions[$var]['placeholder'] = '';
1061
				$substitutions[-1]['replacement'] .= $setting_def['text'] . "\n";
1062
			}
1063
			// Special handling for the Error-Catching block: always at the end.
1064
			elseif (strpos($setting_def['text'], 'Error-Catching') !== false)
1065
			{
1066
				$errcatch_var = $var;
1067
				$substitutions[$var]['search_pattern'] = $setting_def['search_pattern'];
1068
				$substitutions[$var]['placeholder'] = '';
1069
				$substitutions[-2]['replacement'] = "\n" . $setting_def['text'] . $substitutions[-2]['replacement'];
1070
			}
1071
			// The text is the whole thing (code blocks, etc.)
1072
			elseif (is_int($var))
1073
			{
1074
				// Remember the path correcting code for later.
1075
				if (strpos($setting_def['text'], '# Make sure the paths are correct') !== false)
1076
					$pathcode_var = $var;
1077
1078
				if (!empty($setting_def['search_pattern']))
1079
					$substitutions[$var]['search_pattern'] = $setting_def['search_pattern'];
1080
				else
1081
					$substitutions[$var]['search_pattern'] = '~' . preg_quote($setting_def['text'], '~') . '~';
1082
1083
				$substitutions[$var]['placeholder'] = $placeholder;
1084
1085
				$replacement .= $setting_def['text'] . "\n";
1086
			}
1087
			// We only include comments when rebuilding.
1088
			elseif (!empty($rebuild))
1089
				$replacement .= $setting_def['text'] . "\n";
1090
		}
1091
1092
		if (is_string($var))
1093
		{
1094
			// Ensure the value is good.
1095
			if (in_array($var, array_keys($new_settings_vars)))
1096
			{
1097
				// Objects without a __set_state method need a fallback.
1098
				if (is_object($new_settings_vars[$var]) && !method_exists($new_settings_vars[$var], '__set_state'))
1099
				{
1100
					if (method_exists($new_settings_vars[$var], '__toString'))
1101
						$new_settings_vars[$var] = (string) $new_settings_vars[$var];
1102
					else
1103
						$new_settings_vars[$var] = (array) $new_settings_vars[$var];
1104
				}
1105
1106
				// Normalize the type if necessary.
1107
				if (isset($setting_def['type']))
1108
				{
1109
					$expected_types = (array) $setting_def['type'];
1110
					$var_type = gettype($new_settings_vars[$var]);
1111
1112
					// Variable is not of an expected type.
1113
					if (!in_array($var_type, $expected_types))
1114
					{
1115
						// Passed in an unexpected array.
1116
						if ($var_type == 'array')
1117
						{
1118
							$temp = reset($new_settings_vars[$var]);
1119
1120
							// Use the first element if there's only one and it is a scalar.
1121
							if (count($new_settings_vars[$var]) === 1 && is_scalar($temp))
1122
								$new_settings_vars[$var] = $temp;
1123
1124
							// Or keep the old value, if that is good.
1125
							elseif (isset($settings_vars[$var]) && in_array(gettype($settings_vars[$var]), $expected_types))
1126
								$new_settings_vars[$var] = $settings_vars[$var];
1127
1128
							// Fall back to the default
1129
							else
1130
								$new_settings_vars[$var] = $setting_def['default'];
1131
						}
1132
1133
						// Cast it to whatever type was expected.
1134
						// Note: the order of the types in this loop matters.
1135
						foreach (array('boolean', 'integer', 'double', 'string', 'array') as $to_type)
1136
						{
1137
							if (in_array($to_type, $expected_types))
1138
							{
1139
								settype($new_settings_vars[$var], $to_type);
1140
								break;
1141
							}
1142
						}
1143
					}
1144
				}
1145
			}
1146
			// Abort if a required one is undefined (unless we're installing).
1147
			elseif (!empty($setting_def['required']) && !defined('SMF_INSTALLING'))
1148
				return false;
1149
1150
			// Create the search pattern.
1151
			if (!empty($setting_def['search_pattern']))
1152
				$substitutions[$var]['search_pattern'] = $setting_def['search_pattern'];
1153
			else
1154
			{
1155
				$var_pattern = array();
1156
1157
				if (isset($setting_def['type']))
1158
				{
1159
					foreach ((array) $setting_def['type'] as $type)
1160
						$var_pattern[] = $type_regex[$type];
1161
				}
1162
1163
				if (in_array($var, array_keys($config_vars)))
1164
				{
1165
					$var_pattern[] = @$type_regex[gettype($config_vars[$var])];
1166
1167
					if (is_string($config_vars[$var]) && strpos($config_vars[$var], dirname($settingsFile)) === 0)
1168
						$var_pattern[] = '(?:__DIR__|dirname\(__FILE__\)) . \'' . (preg_quote(str_replace(dirname($settingsFile), '', $config_vars[$var]), '~')) . '\'';
1169
				}
1170
1171
				if (in_array($var, array_keys($settings_vars)))
1172
				{
1173
					$var_pattern[] = @$type_regex[gettype($settings_vars[$var])];
1174
1175
					if (is_string($settings_vars[$var]) && strpos($settings_vars[$var], dirname($settingsFile)) === 0)
1176
						$var_pattern[] = '(?:__DIR__|dirname\(__FILE__\)) . \'' . (preg_quote(str_replace(dirname($settingsFile), '', $settings_vars[$var]), '~')) . '\'';
1177
				}
1178
1179
				if (!empty($setting_def['raw_default']) && $setting_def['default'] !== '')
1180
				{
1181
					$var_pattern[] = preg_replace('/\s+/', '\s+', preg_quote($setting_def['default'], '~'));
1182
1183
					if (strpos($setting_def['default'], 'dirname(__FILE__)') !== false)
1184
						$var_pattern[] = preg_replace('/\s+/', '\s+', preg_quote(str_replace('dirname(__FILE__)', '__DIR__', $setting_def['default']), '~'));
1185
1186
					if (strpos($setting_def['default'], '__DIR__') !== false)
1187
						$var_pattern[] = preg_replace('/\s+/', '\s+', preg_quote(str_replace('__DIR__', 'dirname(__FILE__)', $setting_def['default']), '~'));
1188
				}
1189
1190
				$var_pattern = array_unique($var_pattern);
1191
1192
				$var_pattern = count($var_pattern) > 1 ? '(?:' . (implode('|', $var_pattern)) . ')' : $var_pattern[0];
1193
1194
				$substitutions[$var]['search_pattern'] = '~(?<=^|\s)\h*\$' . preg_quote($var, '~') . '\s*=\s*' . $var_pattern . ';~' . (!empty($utf8) ? 'u' : '');
1195
			}
1196
1197
			// Next create the placeholder or replace_pattern.
1198
			if (!empty($setting_def['replace_pattern']))
1199
				$substitutions[$var]['replace_pattern'] = $setting_def['replace_pattern'];
1200
			else
1201
				$substitutions[$var]['placeholder'] = $placeholder;
1202
1203
			// Now create the replacement.
1204
			// A setting to delete.
1205
			if (!empty($setting_def['auto_delete']) && empty($new_settings_vars[$var]))
1206
			{
1207
				if ($setting_def['auto_delete'] === 2 && empty($rebuild) && in_array($var, array_keys($new_settings_vars)))
1208
				{
1209
					$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)) . ";";
1210
				}
1211
				else
1212
				{
1213
					$replacement = '';
1214
					$substitutions[$var]['placeholder'] = '';
1215
1216
					// This is just for cosmetic purposes. Removes the blank line.
1217
					$substitutions[$var]['search_pattern'] = str_replace('(?<=^|\s)', '\n?', $substitutions[$var]['search_pattern']);
1218
				}
1219
			}
1220
			// Add this setting's value.
1221
			elseif (in_array($var, array_keys($new_settings_vars)))
1222
			{
1223
				$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)) . ";";
1224
			}
1225
			// Fall back to the default value.
1226
			elseif (isset($setting_def['default']))
1227
			{
1228
				$replacement .= '$' . $var . ' = ' . (!empty($setting_def['raw_default']) ? sprintf($setting_def['default']) : smf_var_export($setting_def['default'], true)) . ';';
1229
			}
1230
			// This shouldn't happen, but we've got nothing.
1231
			else
1232
				$replacement .= '$' . $var . ' = null;';
1233
		}
1234
1235
		$substitutions[$var]['replacement'] = $replacement;
1236
1237
		// We're done with this one.
1238
		unset($new_settings_vars[$var]);
1239
	}
1240
1241
	// Any leftovers to deal with?
1242
	foreach ($new_settings_vars as $var => $val)
1243
	{
1244
		$var_pattern = array();
1245
1246
		if (in_array($var, array_keys($config_vars)))
1247
			$var_pattern[] = $type_regex[gettype($config_vars[$var])];
1248
1249
		if (in_array($var, array_keys($settings_vars)))
1250
			$var_pattern[] = $type_regex[gettype($settings_vars[$var])];
1251
1252
		$var_pattern = array_unique($var_pattern);
1253
1254
		$var_pattern = count($var_pattern) > 1 ? '(?:' . (implode('|', $var_pattern)) . ')' : $var_pattern[0];
1255
1256
		$placeholder = md5($prefix . $var);
1257
1258
		$substitutions[$var]['search_pattern'] = '~(?<=^|\s)\h*\$' . preg_quote($var, '~') . '\s*=\s*' . $var_pattern . ';~' . (!empty($utf8) ? 'u' : '');
1259
		$substitutions[$var]['placeholder'] = $placeholder;
1260
		$substitutions[$var]['replacement'] = '$' . $var . ' = ' . smf_var_export($val, true) . ";";
1261
	}
1262
1263
	// During an upgrade, some of the path variables may not have been declared yet.
1264
	if (defined('SMF_INSTALLING') && empty($rebuild))
1265
	{
1266
		preg_match_all('~^\h*\$(\w+)\s*=\s*~m', $substitutions[$pathcode_var]['replacement'], $matches);
1267
		$missing_pathvars = array_diff($matches[1], array_keys($substitutions));
1268
1269
		if (!empty($missing_pathvars))
1270
		{
1271
			foreach ($missing_pathvars as $var)
1272
			{
1273
				$substitutions[$pathcode_var]['replacement'] = preg_replace('~\nif[^\n]+\$' . $var . '[^\n]+\n\h*\$' . $var . ' = [^\n]+~', '', $substitutions[$pathcode_var]['replacement']);
1274
			}
1275
		}
1276
	}
1277
1278
	// It's important to do the numbered ones before the named ones, or messes happen.
1279
	uksort(
1280
		$substitutions,
1281
		function($a, $b) {
1282
			if (is_int($a) && is_int($b))
1283
				return $a > $b ? 1 : ($a < $b ? -1 : 0);
1284
			elseif (is_int($a))
1285
				return -1;
1286
			elseif (is_int($b))
1287
				return 1;
1288
			else
1289
				return strcasecmp($b, $a);
1290
		}
1291
	);
1292
1293
	/******************************
1294
	 * PART 3: Content processing *
1295
	 ******************************/
1296
1297
	/* 3.a: Get the content of Settings.php and make sure it is good. */
1298
1299
	// Retrieve the contents of Settings.php and normalize the line endings.
1300
	$settingsText = trim(strtr(file_get_contents($settingsFile), array("\r\n" => "\n", "\r" => "\n")));
1301
1302
	// If Settings.php is empty or corrupt for some reason, see if we can recover.
1303
	if ($settingsText == '' || substr($settingsText, 0, 5) !== '<' . '?php')
1304
	{
1305
		// Try restoring from the backup.
1306
		if (file_exists(dirname($settingsFile) . '/Settings_bak.php'))
1307
			$settingsText = strtr(file_get_contents(dirname($settingsFile) . '/Settings_bak.php'), array("\r\n" => "\n", "\r" => "\n"));
1308
1309
		// Backup is bad too? Our only option is to create one from scratch.
1310
		if ($settingsText == '' || substr($settingsText, 0, 5) !== '<' . '?php' || substr($settingsText, -2) !== '?' . '>')
1311
		{
1312
			$settingsText = '<' . "?php\n";
1313
			foreach ($settings_defs as $var => $setting_def)
1314
			{
1315
				if (is_string($var) && !empty($setting_def['text']) && strpos($substitutions[$var]['replacement'], $setting_def['text']) === false)
1316
					$substitutions[$var]['replacement'] = $setting_def['text'] . "\n" . $substitutions[$var]['replacement'];
1317
1318
				$settingsText .= $substitutions[$var]['replacement'] . "\n";
1319
			}
1320
			$settingsText .= "\n\n?" . '>';
1321
			$rebuild = true;
1322
		}
1323
	}
1324
1325
	// Settings.php is unlikely to contain any heredocs, but just in case...
1326
	if (preg_match_all('/<<<([\'"]?)(\w+)\1\R(.*?)\R\h*\2;$/ms', $settingsText, $matches))
1327
	{
1328
		foreach ($matches[0] as $mkey => $heredoc)
1329
		{
1330
			if (!empty($matches[1][$mkey]) && $matches[1][$mkey] === '\'')
1331
				$heredoc_replacements[$heredoc] = var_export($matches[3][$mkey], true) . ';';
1332
			else
1333
				$heredoc_replacements[$heredoc] = '"' . strtr(substr(var_export($matches[3][$mkey], true), 1, -1), array("\\'" => "'", '"' => '\"')) . '";';
1334
		}
1335
1336
		$settingsText = strtr($settingsText, $heredoc_replacements);
1337
	}
1338
1339
	/* 3.b: Loop through all our substitutions to insert placeholders, etc. */
1340
1341
	$last_var = null;
1342
	$bare_settingsText = $settingsText;
1343
	$force_before_pathcode = array();
1344
	foreach ($substitutions as $var => $substitution)
1345
	{
1346
		$placeholders[$var] = $substitution['placeholder'];
1347
1348
		if (!empty($substitution['placeholder']))
1349
		{
1350
			$simple_replacements[$substitution['placeholder']] = $substitution['replacement'];
1351
		}
1352
		elseif (!empty($substitution['replace_pattern']))
1353
		{
1354
			$replace_patterns[$var] = $substitution['replace_pattern'];
1355
			$replace_strings[$var] = $substitution['replacement'];
1356
		}
1357
1358
		if (strpos($substitutions[$pathcode_var]['replacement'], '$' . $var . ' = ') !== false)
1359
			$force_before_pathcode[] = $var;
1360
1361
		// Look before you leap.
1362
		preg_match_all($substitution['search_pattern'], $bare_settingsText, $matches);
1363
1364
		if ((is_string($var) || $var === $pathcode_var) && count($matches[0]) !== 1 && $substitution['replacement'] !== '')
1365
		{
1366
			// More than one instance of the variable = not good.
1367
			if (count($matches[0]) > 1)
1368
			{
1369
				if (is_string($var))
1370
				{
1371
					// Maybe we can try something more interesting?
1372
					$sp = substr($substitution['search_pattern'], 1);
1373
1374
					if (strpos($sp, '(?<=^|\s)') === 0)
1375
						$sp = substr($sp, 9);
1376
1377
					if (strpos($sp, '^') === 0 || strpos($sp, '(?<') === 0)
1378
						return false;
1379
1380
					// See if we can exclude `if` blocks, etc., to narrow down the matches.
1381
					// @todo Multiple layers of nested brackets might confuse this.
1382
					$sp = '~(?:^|//[^\n]+c\n|\*/|[;}]|' . implode('|', array_filter($placeholders)) . ')\s*' . (strpos($sp, '\K') === false ? '\K' : '') . $sp;
1383
1384
					preg_match_all($sp, $settingsText, $matches);
1385
				}
1386
				else
1387
					$sp = $substitution['search_pattern'];
1388
1389
				// Found at least some that are simple assignment statements.
1390
				if (count($matches[0]) > 0)
1391
				{
1392
					// Remove any duplicates.
1393
					if (count($matches[0]) > 1)
1394
						$settingsText = preg_replace($sp, '', $settingsText, count($matches[0]) - 1);
1395
1396
					// Insert placeholder for the last one.
1397
					$settingsText = preg_replace($sp, $substitution['placeholder'], $settingsText, 1);
1398
				}
1399
1400
				// All instances are inside more complex code structures.
1401
				else
1402
				{
1403
					// Only safe option at this point is to skip it.
1404
					unset($substitutions[$var], $new_settings_vars[$var], $settings_defs[$var], $simple_replacements[$substitution['placeholder']], $replace_patterns[$var], $replace_strings[$var]);
1405
1406
					continue;
1407
				}
1408
			}
1409
			// No matches found.
1410
			elseif (count($matches[0]) === 0)
1411
			{
1412
				$found = false;
1413
				$in_c = in_array($var, array_keys($config_vars));
1414
				$in_s = in_array($var, array_keys($settings_vars));
1415
1416
				// Is it in there at all?
1417
				if (!preg_match('~(^|\s)\$' . preg_quote($var, '~') . '\s*=\s*~', $bare_settingsText))
1418
				{
1419
					// It's defined by Settings.php, but not by code in the file.
1420
					// Probably done via an include or something. Skip it.
1421
					if ($in_s)
1422
						unset($substitutions[$var], $settings_defs[$var]);
1423
1424
					// Admin is explicitly trying to set this one, so we'll handle
1425
					// it as if it were a new custom setting being added.
1426
					elseif ($in_c)
1427
						$new_settings_vars[$var] = $config_vars[$var];
1428
1429
					continue;
1430
				}
1431
1432
				// It's in there somewhere, so check if the value changed type.
1433
				foreach (array('scalar', 'object', 'array') as $type)
1434
				{
1435
					// Try all the other scalar types first.
1436
					if ($type == 'scalar')
1437
						$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' => '')))) . ')';
1438
1439
					// Maybe it's an object? (Probably not, but we should check.)
1440
					elseif ($type == 'object')
1441
					{
1442
						if (strpos($settingsText, '__set_state') === false)
1443
							continue;
1444
1445
						$sp = $type_regex['object'];
1446
					}
1447
1448
					// Maybe it's an array?
1449
					else
1450
						$sp = $type_regex['array'];
1451
1452
					if (preg_match('~(^|\s)\$' . preg_quote($var, '~') . '\s*=\s*' . $sp . '~', $bare_settingsText, $derp))
1453
					{
1454
						$settingsText = preg_replace('~(^|\s)\$' . preg_quote($var, '~') . '\s*=\s*' . $sp . '~', $substitution['placeholder'], $settingsText);
1455
						$found = true;
1456
						break;
1457
					}
1458
				}
1459
1460
				// Something weird is going on. Better just leave it alone.
1461
				if (!$found)
1462
				{
1463
					// $var? What $var? Never heard of it.
1464
					unset($substitutions[$var], $new_settings_vars[$var], $settings_defs[$var], $simple_replacements[$substitution['placeholder']], $replace_patterns[$var], $replace_strings[$var]);
1465
					continue;
1466
				}
1467
			}
1468
		}
1469
		// Good to go, so insert our placeholder.
1470
		else
1471
			$settingsText = preg_replace($substitution['search_pattern'], $substitution['placeholder'], $settingsText);
1472
1473
		// Once the code blocks are done, we want to compare to a version without comments.
1474
		if (is_int($last_var) && is_string($var))
1475
			$bare_settingsText = strip_php_comments($settingsText);
1476
1477
		$last_var = $var;
1478
	}
1479
1480
	// Rebuilding requires more work.
1481
	if (!empty($rebuild))
1482
	{
1483
		// Strip out the leading and trailing placeholders to prevent duplication.
1484
		$settingsText = str_replace(array($substitutions[-1]['placeholder'], $substitutions[-2]['placeholder']), '', $settingsText);
1485
1486
		// Strip out all our standard comments.
1487
		foreach ($settings_defs as $var => $setting_def)
1488
		{
1489
			if (isset($setting_def['text']))
1490
				$settingsText = strtr($settingsText, array($setting_def['text'] . "\n" => '', $setting_def['text'] => '',));
1491
		}
1492
1493
		// We need to refresh $bare_settingsText at this point.
1494
		$bare_settingsText = strip_php_comments($settingsText);
1495
1496
		// Fix up whitespace to make comparison easier.
1497
		foreach ($placeholders as $placeholder)
1498
		{
1499
			$bare_settingsText = str_replace(array($placeholder . "\n\n", $placeholder), $placeholder . "\n", $bare_settingsText);
1500
		}
1501
		$bare_settingsText = preg_replace('/\h+$/m', '', rtrim($bare_settingsText));
1502
1503
		/*
1504
		 * Divide the existing content into sections.
1505
		 * The idea here is to make sure we don't mess with the relative position
1506
		 * of any code blocks in the file, since that could break things. Within
1507
		 * each section, however, we'll reorganize the content to match the
1508
		 * default layout as closely as we can.
1509
		 */
1510
		$sections = array(array());
1511
		$section_num = 0;
1512
		$trimmed_placeholders = array_filter(array_map('trim', $placeholders));
1513
		$newsection_placeholders = array();
1514
		$all_custom_content = '';
1515
		foreach ($substitutions as $var => $substitution)
1516
		{
1517
			if (is_int($var) && ($var === -2 || $var > 0) && isset($trimmed_placeholders[$var]) && strpos($bare_settingsText, $trimmed_placeholders[$var]) !== false)
1518
				$newsection_placeholders[$var] = $trimmed_placeholders[$var];
1519
		}
1520
		foreach (preg_split('~(?<=' . implode('|', $trimmed_placeholders) . ')|(?=' . implode('|', $trimmed_placeholders) . ')~', $bare_settingsText) as $part)
1521
		{
1522
			$part = trim($part);
1523
1524
			if (empty($part))
1525
				continue;
1526
1527
			// Build a list of placeholders for this section.
1528
			if (in_array($part, $trimmed_placeholders) && !in_array($part, $newsection_placeholders))
1529
			{
1530
				$sections[$section_num][] = $part;
1531
			}
1532
			// Custom content and newsection_placeholders get their own sections.
1533
			else
1534
			{
1535
				if (!empty($sections[$section_num]))
1536
					++$section_num;
1537
1538
				$sections[$section_num][] = $part;
1539
1540
				++$section_num;
1541
1542
				if (!in_array($part, $trimmed_placeholders))
1543
					$all_custom_content .= "\n" . $part;
1544
			}
1545
		}
1546
1547
		// And now, rebuild the content!
1548
		$new_settingsText = '';
1549
		$done_defs = array();
1550
		$sectionkeys = array_keys($sections);
1551
		foreach ($sections as $sectionkey => $section)
1552
		{
1553
			// Custom content needs to be preserved.
1554
			if (count($section) === 1 && !in_array($section[0], $trimmed_placeholders))
1555
			{
1556
				$prev_section_end = $sectionkey < 1 ? 0 : strpos($settingsText, end($sections[$sectionkey - 1])) + strlen(end($sections[$sectionkey - 1]));
1557
				$next_section_start = $sectionkey == end($sectionkeys) ? strlen($settingsText) : strpos($settingsText, $sections[$sectionkey + 1][0]);
1558
1559
				$new_settingsText .= "\n" . substr($settingsText, $prev_section_end, $next_section_start - $prev_section_end) . "\n";
1560
			}
1561
			// Put the placeholders in this section into canonical order.
1562
			else
1563
			{
1564
				$section_parts = array_flip($section);
1565
				$pathcode_reached = false;
1566
				foreach ($settings_defs as $var => $setting_def)
1567
				{
1568
					if ($var === $pathcode_var)
1569
						$pathcode_reached = true;
1570
1571
					// Already did this setting, so move on to the next.
1572
					if (in_array($var, $done_defs))
1573
						continue;
1574
1575
					// Stop when we hit a setting definition that will start a later section.
1576
					if (isset($newsection_placeholders[$var]) && count($section) !== 1)
1577
						break;
1578
1579
					// Stop when everything in this section is done, unless it's the last.
1580
					// This helps maintain the relative position of any custom content.
1581
					if (empty($section_parts) && $sectionkey < (count($sections) - 1))
1582
						break;
1583
1584
					$p = trim($substitutions[$var]['placeholder']);
1585
1586
					// Can't do anything with an empty placeholder.
1587
					if ($p === '')
1588
						continue;
1589
1590
					// Does this need to be inserted before the path correction code?
1591
					if (strpos($new_settingsText, trim($substitutions[$pathcode_var]['placeholder'])) !== false && in_array($var, $force_before_pathcode))
1592
					{
1593
						$new_settingsText = strtr($new_settingsText, array($substitutions[$pathcode_var]['placeholder'] => $p . "\n" . $substitutions[$pathcode_var]['placeholder']));
1594
1595
						$bare_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1596
						$done_defs[] = $var;
1597
						unset($section_parts[trim($substitutions[$var]['placeholder'])]);
1598
					}
1599
1600
					// If it's in this section, add it to the new text now.
1601
					elseif (in_array($p, $section))
1602
					{
1603
						$new_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1604
						$done_defs[] = $var;
1605
						unset($section_parts[trim($substitutions[$var]['placeholder'])]);
1606
					}
1607
1608
					// Perhaps it is safe to reposition it anyway.
1609
					elseif (is_string($var) && strpos($new_settingsText, $p) === false && strpos($all_custom_content, '$' . $var) === false)
1610
					{
1611
						$new_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1612
						$done_defs[] = $var;
1613
						unset($section_parts[trim($substitutions[$var]['placeholder'])]);
1614
					}
1615
1616
					// If this setting is missing entirely, fix it.
1617
					elseif (strpos($bare_settingsText, $p) === false)
1618
					{
1619
						// Special case if the path code is missing. Put it near the end,
1620
						// and also anything else that is missing that normally follows it.
1621
						if (!isset($newsection_placeholders[$pathcode_var]) && $pathcode_reached === true && $sectionkey < (count($sections) - 1))
1622
							break;
1623
1624
						$new_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1625
						$bare_settingsText .= "\n" . $substitutions[$var]['placeholder'];
1626
						$done_defs[] = $var;
1627
						unset($section_parts[trim($substitutions[$var]['placeholder'])]);
1628
					}
1629
				}
1630
			}
1631
		}
1632
		$settingsText = $new_settingsText;
1633
1634
		// Restore the leading and trailing placeholders as necessary.
1635
		foreach (array(-1, -2) as $var)
1636
		{
1637
			if (!empty($substitutions[$var]['placeholder']) && strpos($settingsText, $substitutions[$var]['placeholder']) === false);
1638
			{
1639
				$settingsText = ($var == -1 ? $substitutions[$var]['placeholder'] : '') . $settingsText . ($var == -2 ? $substitutions[$var]['placeholder'] : '');
1640
			}
1641
		}
1642
	}
1643
	// Even if not rebuilding, there are a few variables that may need to be moved around.
1644
	else
1645
	{
1646
		$pathcode_pos = strpos($settingsText, $substitutions[$pathcode_var]['placeholder']);
1647
1648
		if ($pathcode_pos !== false)
1649
		{
1650
			foreach ($force_before_pathcode as $var)
1651
			{
1652
				if (!empty($substitutions[$var]['placeholder']) && strpos($settingsText, $substitutions[$var]['placeholder']) > $pathcode_pos)
1653
				{
1654
					$settingsText = strtr($settingsText, array(
1655
						$substitutions[$var]['placeholder'] => '',
1656
						$substitutions[$pathcode_var]['placeholder'] => $substitutions[$var]['placeholder'] . "\n" . $substitutions[$pathcode_var]['placeholder'],
1657
					));
1658
				}
1659
			}
1660
		}
1661
	}
1662
1663
	/* 3.c: Replace the placeholders with the final values */
1664
1665
	// Where possible, perform simple substitutions.
1666
	$settingsText = strtr($settingsText, $simple_replacements);
1667
1668
	// Deal with any complicated ones.
1669
	if (!empty($replace_patterns))
1670
		$settingsText = preg_replace($replace_patterns, $replace_strings, $settingsText);
1671
1672
	// Make absolutely sure that the path correction code is included.
1673
	if (strpos($settingsText, $substitutions[$pathcode_var]['replacement']) === false)
1674
		$settingsText = preg_replace('~(?=\n#+ Error.Catching #+)~', "\n" . $substitutions[$pathcode_var]['replacement'] . "\n", $settingsText);
1675
1676
	// If we did not rebuild, do just enough to make sure the thing is viable.
1677
	if (empty($rebuild))
1678
	{
1679
		// We need to refresh $bare_settingsText again, and remove the code blocks from it.
1680
		$bare_settingsText = $settingsText;
1681
		foreach ($substitutions as $var => $substitution)
1682
		{
1683
			if (!is_int($var))
1684
				break;
1685
1686
			if (isset($substitution['replacement']))
1687
				$bare_settingsText = str_replace($substitution['replacement'], '', $bare_settingsText);
1688
		}
1689
		$bare_settingsText = strip_php_comments($bare_settingsText);
1690
1691
		// Now insert any defined settings that are missing.
1692
		$pathcode_reached = false;
1693
		foreach ($settings_defs as $var => $setting_def)
1694
		{
1695
			if ($var === $pathcode_var)
1696
				$pathcode_reached = true;
1697
1698
			if (is_int($var))
1699
				continue;
1700
1701
			// Do nothing if it is already in there.
1702
			if (preg_match($substitutions[$var]['search_pattern'], $bare_settingsText))
1703
				continue;
1704
1705
			// Insert it either before or after the path correction code, whichever is appropriate.
1706
			if (!$pathcode_reached || in_array($var, $force_before_pathcode))
1707
			{
1708
				$settingsText = preg_replace($substitutions[$pathcode_var]['search_pattern'], $substitutions[$var]['replacement'] . "\n\n$0", $settingsText);
1709
			}
1710
			else
1711
			{
1712
				$settingsText = preg_replace($substitutions[$pathcode_var]['search_pattern'], "$0\n\n" . $substitutions[$var]['replacement'], $settingsText);
1713
			}
1714
		}
1715
	}
1716
1717
	// If we have any brand new settings to add, do so.
1718
	foreach ($new_settings_vars as $var => $val)
1719
	{
1720
		if (isset($substitutions[$var]) && !preg_match($substitutions[$var]['search_pattern'], $settingsText))
1721
		{
1722
			if (!isset($settings_defs[$var]) && strpos($settingsText, '# Custom Settings #') === false)
1723
				$settingsText = preg_replace('~(?=\n#+ Error.Catching #+)~', "\n\n######### Custom Settings #########\n", $settingsText);
1724
1725
			$settingsText = preg_replace('~(?=\n#+ Error.Catching #+)~', $substitutions[$var]['replacement'] . "\n", $settingsText);
1726
		}
1727
	}
1728
1729
	// This is just cosmetic. Get rid of extra lines of whitespace.
1730
	$settingsText = preg_replace('~\n\s*\n~', "\n\n", $settingsText);
1731
1732
	/**************************************
1733
	 * PART 4: Check syntax before saving *
1734
	 **************************************/
1735
1736
	$temp_sfile = tempnam(sm_temp_dir(), md5($prefix . 'Settings.php'));
1737
	file_put_contents($temp_sfile, $settingsText);
1738
1739
	$result = get_current_settings(filemtime($temp_sfile), $temp_sfile);
1740
1741
	unlink($temp_sfile);
1742
1743
	// If the syntax is borked, try rebuilding to see if that fixes it.
1744
	if ($result === false)
1745
		return empty($rebuild) ? updateSettingsFile($config_vars, $keep_quotes, true) : false;
1746
1747
	/******************************************
1748
	 * PART 5: Write updated settings to file *
1749
	 ******************************************/
1750
1751
	$success = safe_file_write($settingsFile, $settingsText, dirname($settingsFile) . '/Settings_bak.php', $last_settings_change);
1752
1753
	// Remember this in case updateSettingsFile is called twice.
1754
	$mtime = filemtime($settingsFile);
1755
1756
	return $success;
1757
}
1758
1759
/**
1760
 * Retrieves a copy of the current values of all settings defined in Settings.php.
1761
 *
1762
 * Importantly, it does this without affecting our actual global variables at all,
1763
 * and it performs safety checks before acting. The result is an array of the
1764
 * values as recorded in the settings file.
1765
 *
1766
 * @param int $mtime Timestamp of last known good configuration. Defaults to time SMF started.
1767
 * @param string $settingsFile The settings file. Defaults to SMF's standard Settings.php.
1768
 * @return array An array of name/value pairs for all the settings in the file.
1769
 */
1770
function get_current_settings($mtime = null, $settingsFile = null)
1771
{
1772
	$mtime = is_null($mtime) ? (defined('TIME_START') ? TIME_START : $_SERVER['REQUEST_TIME']) : (int) $mtime;
1773
1774
	if (!is_file($settingsFile))
1775
	{
1776
		foreach (get_included_files() as $settingsFile)
1777
			if (basename($settingsFile) === 'Settings.php')
1778
				break;
1779
1780
		if (basename($settingsFile) !== 'Settings.php')
1781
			return false;
1782
	}
1783
1784
	// If the file has been changed since the last known good configuration, bail out.
1785
	clearstatcache();
1786
	if (filemtime($settingsFile) > $mtime)
1787
		return false;
1788
1789
	// Strip out opening and closing PHP tags.
1790
	$settingsText = trim(file_get_contents($settingsFile));
1791
	if (substr($settingsText, 0, 5) == '<' . '?php')
1792
		$settingsText = substr($settingsText, 5);
1793
	if (substr($settingsText, -2) == '?' . '>')
1794
		$settingsText = substr($settingsText, 0, -2);
1795
1796
	// Since we're using eval, we need to manually replace these with strings.
1797
	$settingsText = strtr($settingsText, array(
1798
		'__FILE__' => var_export($settingsFile, true),
1799
		'__DIR__' => var_export(dirname($settingsFile), true),
1800
	));
1801
1802
	// Prevents warnings about constants that are already defined.
1803
	$settingsText = preg_replace_callback(
1804
		'~\bdefine\s*\(\s*(["\'])(\w+)\1~',
1805
		function ($matches)
1806
		{
1807
			return 'define(\'' . md5(mt_rand()) . '\'';
1808
		},
1809
		$settingsText
1810
	);
1811
1812
	// Handle eval errors gracefully in both PHP 5 and PHP 7
1813
	try
1814
	{
1815
		if($settingsText !== '' && @eval($settingsText) === false)
1816
			throw new ErrorException('eval error');
1817
1818
		unset($mtime, $settingsFile, $settingsText);
1819
		$defined_vars = get_defined_vars();
1820
	}
1821
	catch (Throwable $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1822
	catch (ErrorException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1823
	if (isset($e))
1824
		return false;
1825
1826
	return $defined_vars;
1827
}
1828
1829
/**
1830
 * Writes data to a file, optionally making a backup, while avoiding race conditions.
1831
 *
1832
 * @param string $file The filepath of the file where the data should be written.
1833
 * @param string $data The data to be written to $file.
1834
 * @param string $backup_file The filepath where the backup should be saved. Default null.
1835
 * @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.
1836
 * @param bool $append If true, the data will be appended instead of overwriting the existing content of the file. Default false.
1837
 * @return bool Whether the write operation succeeded or not.
1838
 */
1839
function safe_file_write($file, $data, $backup_file = null, $mtime = null, $append = false)
1840
{
1841
	// Sanity checks.
1842
	if (!file_exists($file) && !is_dir(dirname($file)))
1843
		return false;
1844
1845
	if (!is_int($mtime))
1846
		$mtime = $_SERVER['REQUEST_TIME'];
1847
1848
	$temp_dir = sm_temp_dir();
1849
1850
	// Our temp files.
1851
	$temp_sfile = tempnam($temp_dir, pathinfo($file, PATHINFO_FILENAME) . '.');
1852
1853
	if (!empty($backup_file))
1854
		$temp_bfile = tempnam($temp_dir, pathinfo($backup_file, PATHINFO_FILENAME) . '.');
1855
1856
	// We need write permissions.
1857
	$failed = false;
1858
	foreach (array($file, $backup_file) as $sf)
1859
	{
1860
		if (empty($sf))
1861
			continue;
1862
1863
		if (!file_exists($sf))
1864
			touch($sf);
1865
		elseif (!is_file($sf))
1866
			$failed = true;
1867
1868
		if (!$failed)
1869
			$failed = !smf_chmod($sf);
1870
	}
1871
1872
	// Now let's see if writing to a temp file succeeds.
1873
	if (!$failed && file_put_contents($temp_sfile, $data, LOCK_EX) !== strlen($data))
1874
		$failed = true;
1875
1876
	// Tests passed, so it's time to do the job.
1877
	if (!$failed)
1878
	{
1879
		// Back up the backup, just in case.
1880
		if (!empty($backup_file) && file_exists($backup_file))
1881
			$temp_bfile_saved = @copy($backup_file, $temp_bfile);
1882
1883
		// Make sure no one changed the file while we weren't looking.
1884
		clearstatcache();
1885
		if (filemtime($file) <= $mtime)
1886
		{
1887
			// Attempt to open the file.
1888
			$sfhandle = @fopen($file, 'c');
1889
1890
			// Let's do this thing!
1891
			if ($sfhandle !== false)
1892
			{
1893
				// Immediately get a lock.
1894
				flock($sfhandle, LOCK_EX);
1895
1896
				// Make sure the backup works before we do anything more.
1897
				$temp_sfile_saved = @copy($file, $temp_sfile);
1898
1899
				// Now write our data to the file.
1900
				if ($temp_sfile_saved)
1901
				{
1902
					if (empty($append))
1903
					{
1904
						ftruncate($sfhandle, 0);
1905
						rewind($sfhandle);
1906
					}
1907
1908
					$failed = fwrite($sfhandle, $data) !== strlen($data);
1909
				}
1910
				else
1911
					$failed = true;
1912
1913
				// If writing failed, put everything back the way it was.
1914
				if ($failed)
1915
				{
1916
					if (!empty($temp_sfile_saved))
1917
						@rename($temp_sfile, $file);
1918
1919
					if (!empty($temp_bfile_saved))
1920
						@rename($temp_bfile, $backup_file);
1921
				}
1922
				// It worked, so make our temp backup the new permanent backup.
1923
				elseif (!empty($backup_file))
1924
					@rename($temp_sfile, $backup_file);
1925
1926
				// And we're done.
1927
				flock($sfhandle, LOCK_UN);
1928
				fclose($sfhandle);
1929
			}
1930
		}
1931
	}
1932
1933
	clearstatcache();
1934
1935
	// We're done with these.
1936
	if (is_file($temp_sfile))
1937
		@unlink($temp_sfile);
1938
1939
	if (!empty($temp_bfile) && is_file($temp_bfile))
1940
		@unlink($temp_bfile);
1941
1942
	if ($failed)
1943
		return false;
1944
1945
	// Even though on normal installations the filemtime should invalidate any cached version
1946
	// it seems that there are times it might not. So let's MAKE it dump the cache.
1947
	if (function_exists('opcache_invalidate'))
1948
		opcache_invalidate($file, true);
1949
1950
	return true;
1951
}
1952
1953
/**
1954
 * A wrapper around var_export whose output matches SMF coding conventions.
1955
 *
1956
 * @todo Add special handling for objects?
1957
 *
1958
 * @param mixed $var The variable to export
1959
 * @return mixed A PHP-parseable representation of the variable's value
1960
 */
1961
function smf_var_export($var)
1962
{
1963
	/*
1964
	 * Old versions of updateSettingsFile couldn't handle multi-line values.
1965
	 * Even though technically we can now, we'll keep arrays on one line for
1966
	 * the sake of backwards compatibility.
1967
	 */
1968
	if (is_array($var))
1969
	{
1970
		$return = array();
1971
1972
		foreach ($var as $key => $value)
1973
			$return[] = var_export($key, true) . ' => ' . smf_var_export($value);
1974
1975
		return 'array(' . implode(', ', $return) . ')';
1976
	}
1977
1978
	// For the same reason, replace literal returns and newlines with "\r" and "\n"
1979
	elseif (is_string($var) && (strpos($var, "\n") !== false || strpos($var, "\r") !== false))
1980
	{
1981
		return strtr(
1982
			preg_replace_callback(
1983
				'/[\r\n]+/',
1984
				function($m)
1985
				{
1986
					return '\' . "' . strtr($m[0], array("\r" => '\r', "\n" => '\n')) . '" . \'';
1987
				},
1988
				var_export($var, true)
1989
			),
1990
			array("'' . " => '', " . ''" => '')
1991
		);
1992
	}
1993
1994
	// We typically use lowercase true/false/null.
1995
	elseif (in_array(gettype($var), array('boolean', 'NULL')))
1996
		return strtolower(var_export($var, true));
1997
1998
	// Nothing special.
1999
	else
2000
		return var_export($var, true);
2001
};
2002
2003
/**
2004
 * Deletes all PHP comments from a string.
2005
 *
2006
 * @param string $code_str A string containing PHP code.
2007
 * @return string A string of PHP code with no comments in it.
2008
 */
2009
function strip_php_comments($code_str)
2010
{
2011
	// This is the faster, better way.
2012
	if (is_callable('token_get_all'))
2013
	{
2014
		$tokens = token_get_all($code_str);
2015
2016
		$parts = array();
2017
		foreach ($tokens as $token)
2018
		{
2019
			if (is_string($token))
2020
				$parts[] = $token;
2021
			else
2022
			{
2023
				list($id, $text) = $token;
2024
2025
				switch ($id) {
2026
					case T_COMMENT:
2027
					case T_DOC_COMMENT:
2028
						end($parts);
2029
						$prev_part = key($parts);
2030
2031
						// For the sake of tider output, trim any horizontal
2032
						// whitespace that immediately preceded the comment.
2033
						$parts[$prev_part] = rtrim($parts[$prev_part], "\t ");
2034
2035
						// For 'C' style comments, also trim one preceding
2036
						// line break, if present.
2037
						if (strpos($text, '/*') === 0)
2038
						{
2039
							if (substr($parts[$prev_part], -2) === "\r\n")
2040
								$parts[$prev_part] = substr($parts[$prev_part], 0, -2);
2041
							elseif (in_array(substr($parts[$prev_part], -1), array("\r", "\n")))
2042
								$parts[$prev_part] = substr($parts[$prev_part], 0, -1);
2043
						}
2044
2045
						break;
2046
2047
					default:
2048
						$parts[] = $text;
2049
						break;
2050
				}
2051
			}
2052
		}
2053
2054
		$code_str = implode('', $parts);
2055
2056
		return $code_str;
2057
	}
2058
2059
	// If the tokenizer extension has been disabled, do the job manually.
2060
2061
	// Leave any heredocs alone.
2062
	if (preg_match_all('/<<<([\'"]?)(\w+)\1?\R(.*?)\R\h*\2;$/ms', $code_str, $matches))
2063
	{
2064
		$heredoc_replacements = array();
2065
2066
		foreach ($matches[0] as $mkey => $heredoc)
2067
			$heredoc_replacements[$heredoc] = var_export(md5($matches[3][$mkey]), true) . ';';
2068
2069
		$code_str = strtr($code_str, $heredoc_replacements);
2070
	}
2071
2072
	// Split before everything that could possibly delimit a comment or a string.
2073
	$parts = preg_split('~(?=#+|/(?=/|\*)|\*/|\R|(?<!\\\)[\'"])~m', $code_str);
2074
2075
	$in_string = 0;
2076
	$in_comment = 0;
2077
	foreach ($parts as $partkey => $part)
2078
	{
2079
		$one_char = substr($part, 0, 1);
2080
		$two_char = substr($part, 0, 2);
2081
		$to_remove = 0;
2082
2083
		/*
2084
		 * Meaning of $in_string values:
2085
		 *	0: not in a string
2086
		 *	1: in a single quote string
2087
		 *	2: in a double quote string
2088
		 */
2089
		if ($one_char == "'")
2090
		{
2091
			if (!empty($in_comment))
2092
				$in_string = 0;
2093
			elseif (in_array($in_string, array(0, 1)))
2094
				$in_string = ($in_string ^ 1);
2095
		}
2096
		elseif ($one_char == '"')
2097
		{
2098
			if (!empty($in_comment))
2099
				$in_string = 0;
2100
			elseif (in_array($in_string, array(0, 2)))
2101
				$in_string = ($in_string ^ 2);
2102
		}
2103
2104
		/*
2105
		 * Meaning of $in_comment values:
2106
		 * 	0: not in a comment
2107
		 *	1: in a single line comment
2108
		 *	2: in a multi-line comment
2109
		 */
2110
		elseif ($one_char == '#' || $two_char == '//')
2111
		{
2112
			$in_comment = !empty($in_string) ? 0 : (empty($in_comment) ? 1 : $in_comment);
2113
2114
			if ($in_comment == 1)
2115
			{
2116
				$parts[$partkey - 1] = rtrim($parts[$partkey - 1], "\t ");
2117
2118
				if (substr($parts[$partkey - 1], -2) === "\r\n")
2119
					$parts[$partkey - 1] = substr($parts[$partkey - 1], 0, -2);
2120
				elseif (in_array(substr($parts[$partkey - 1], -1), array("\r", "\n")))
2121
					$parts[$partkey - 1] = substr($parts[$partkey - 1], 0, -1);
2122
			}
2123
		}
2124
		elseif ($two_char === "\r\n" || $one_char === "\r" || $one_char === "\n")
2125
		{
2126
			if ($in_comment == 1)
2127
				$in_comment = 0;
2128
		}
2129
		elseif ($two_char == '/*')
2130
		{
2131
			$in_comment = !empty($in_string) ? 0 : (empty($in_comment) ? 2 : $in_comment);
2132
2133
			if ($in_comment == 2)
2134
			{
2135
				$parts[$partkey - 1] = rtrim($parts[$partkey - 1], "\t ");
2136
2137
				if (substr($parts[$partkey - 1], -2) === "\r\n")
2138
					$parts[$partkey - 1] = substr($parts[$partkey - 1], 0, -2);
2139
				elseif (in_array(substr($parts[$partkey - 1], -1), array("\r", "\n")))
2140
					$parts[$partkey - 1] = substr($parts[$partkey - 1], 0, -1);
2141
			}
2142
		}
2143
		elseif ($two_char == '*/')
2144
		{
2145
			if ($in_comment == 2)
2146
			{
2147
				$in_comment = 0;
2148
2149
				// Delete the comment closing.
2150
				$to_remove = 2;
2151
			}
2152
		}
2153
2154
		if (empty($in_comment))
2155
			$parts[$partkey] = strlen($part) > $to_remove ? substr($part, $to_remove) : '';
2156
		else
2157
			$parts[$partkey] = '';
2158
	}
2159
2160
	$code_str = implode('', $parts);
2161
2162
	if (!empty($heredoc_replacements))
2163
		$code_str = strtr($code_str, array_flip($heredoc_replacements));
2164
2165
	return $code_str;
2166
}
2167
2168
/**
2169
 * Saves the time of the last db error for the error log
2170
 * - Done separately from updateSettingsFile to avoid race conditions
2171
 *   which can occur during a db error
2172
 * - If it fails Settings.php will assume 0
2173
 *
2174
 * @param int $time The timestamp of the last DB error
2175
 * @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.
2176
 * @return bool True If we could succesfully put the file or not.
2177
 */
2178
function updateDbLastError($time, $update = true)
2179
{
2180
	global $boarddir, $cachedir, $db_last_error;
2181
2182
	// Write out the db_last_error file with the error timestamp
2183
	if (!empty($cachedir) && is_writable($cachedir))
2184
		$errorfile = $cachedir . '/db_last_error.php';
2185
2186
	elseif (file_exists(dirname(__DIR__) . '/cache'))
2187
		$errorfile = dirname(__DIR__) . '/cache/db_last_error.php';
2188
2189
	else
2190
		$errorfile = dirname(__DIR__) . '/db_last_error.php';
2191
2192
	$result = file_put_contents($errorfile, '<' . '?' . "php\n" . '$db_last_error = ' . $time . ';' . "\n" . '?' . '>', LOCK_EX);
2193
2194
	@touch($boarddir . '/' . 'Settings.php');
2195
2196
	// Unless requested, we should update $db_last_error as well.
2197
	if ($update)
2198
		$db_last_error = $time;
2199
2200
	// We  do a loose match here rather than strict (!==) as 0 is also false.
2201
	return $result != false;
2202
}
2203
2204
/**
2205
 * Saves the admin's current preferences to the database.
2206
 */
2207
function updateAdminPreferences()
2208
{
2209
	global $options, $context, $smcFunc, $settings, $user_info;
2210
2211
	// This must exist!
2212
	if (!isset($context['admin_preferences']))
2213
		return false;
2214
2215
	// This is what we'll be saving.
2216
	$options['admin_preferences'] = $smcFunc['json_encode']($context['admin_preferences']);
2217
2218
	// Just check we haven't ended up with something theme exclusive somehow.
2219
	$smcFunc['db_query']('', '
2220
		DELETE FROM {db_prefix}themes
2221
		WHERE id_theme != {int:default_theme}
2222
			AND variable = {string:admin_preferences}',
2223
		array(
2224
			'default_theme' => 1,
2225
			'admin_preferences' => 'admin_preferences',
2226
		)
2227
	);
2228
2229
	// Update the themes table.
2230
	$smcFunc['db_insert']('replace',
2231
		'{db_prefix}themes',
2232
		array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
2233
		array($user_info['id'], 1, 'admin_preferences', $options['admin_preferences']),
2234
		array('id_member', 'id_theme', 'variable')
2235
	);
2236
2237
	// Make sure we invalidate any cache.
2238
	cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 0);
2239
}
2240
2241
/**
2242
 * Send all the administrators a lovely email.
2243
 * - loads all users who are admins or have the admin forum permission.
2244
 * - uses the email template and replacements passed in the parameters.
2245
 * - sends them an email.
2246
 *
2247
 * @param string $template Which email template to use
2248
 * @param array $replacements An array of items to replace the variables in the template
2249
 * @param array $additional_recipients An array of arrays of info for additional recipients. Should have 'id', 'email' and 'name' for each.
2250
 */
2251
function emailAdmins($template, $replacements = array(), $additional_recipients = array())
2252
{
2253
	global $smcFunc, $sourcedir, $language, $modSettings;
2254
2255
	// We certainly want this.
2256
	require_once($sourcedir . '/Subs-Post.php');
2257
2258
	// Load all members which are effectively admins.
2259
	require_once($sourcedir . '/Subs-Members.php');
2260
	$members = membersAllowedTo('admin_forum');
2261
2262
	// Load their alert preferences
2263
	require_once($sourcedir . '/Subs-Notify.php');
2264
	$prefs = getNotifyPrefs($members, 'announcements', true);
2265
2266
	$request = $smcFunc['db_query']('', '
2267
		SELECT id_member, member_name, real_name, lngfile, email_address
2268
		FROM {db_prefix}members
2269
		WHERE id_member IN({array_int:members})',
2270
		array(
2271
			'members' => $members,
2272
		)
2273
	);
2274
	$emails_sent = array();
2275
	while ($row = $smcFunc['db_fetch_assoc']($request))
2276
	{
2277
		if (empty($prefs[$row['id_member']]['announcements']))
2278
			continue;
2279
2280
		// Stick their particulars in the replacement data.
2281
		$replacements['IDMEMBER'] = $row['id_member'];
2282
		$replacements['REALNAME'] = $row['member_name'];
2283
		$replacements['USERNAME'] = $row['real_name'];
2284
2285
		// Load the data from the template.
2286
		$emaildata = loadEmailTemplate($template, $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
2287
2288
		// Then send the actual email.
2289
		sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, $template, $emaildata['is_html'], 1);
2290
2291
		// Track who we emailed so we don't do it twice.
2292
		$emails_sent[] = $row['email_address'];
2293
	}
2294
	$smcFunc['db_free_result']($request);
2295
2296
	// Any additional users we must email this to?
2297
	if (!empty($additional_recipients))
2298
		foreach ($additional_recipients as $recipient)
2299
		{
2300
			if (in_array($recipient['email'], $emails_sent))
2301
				continue;
2302
2303
			$replacements['IDMEMBER'] = $recipient['id'];
2304
			$replacements['REALNAME'] = $recipient['name'];
2305
			$replacements['USERNAME'] = $recipient['name'];
2306
2307
			// Load the template again.
2308
			$emaildata = loadEmailTemplate($template, $replacements, empty($recipient['lang']) || empty($modSettings['userLanguage']) ? $language : $recipient['lang']);
2309
2310
			// Send off the email.
2311
			sendmail($recipient['email'], $emaildata['subject'], $emaildata['body'], null, $template, $emaildata['is_html'], 1);
2312
		}
2313
}
2314
2315
/**
2316
 * Locates the most appropriate temp directory.
2317
 *
2318
 * Systems using `open_basedir` restrictions may receive errors with
2319
 * `sys_get_temp_dir()` due to misconfigurations on servers. Other
2320
 * cases sys_temp_dir may not be set to a safe value. Additionally
2321
 * `sys_get_temp_dir` may use a readonly directory. This attempts to
2322
 * find a working temp directory that is accessible under the
2323
 * restrictions and is writable to the web service account.
2324
 *
2325
 * Directories checked against `open_basedir`:
2326
 *
2327
 * - `sys_get_temp_dir()`
2328
 * - `upload_tmp_dir`
2329
 * - `session.save_path`
2330
 * - `cachedir`
2331
 *
2332
 * @return string
2333
*/
2334
function sm_temp_dir()
2335
{
2336
	global $cachedir;
2337
2338
	static $temp_dir = null;
2339
2340
	// Already did this.
2341
	if (!empty($temp_dir))
2342
		return $temp_dir;
2343
2344
	// Temp Directory options order.
2345
	$temp_dir_options = array(
2346
		0 => 'sys_get_temp_dir',
2347
		1 => 'upload_tmp_dir',
2348
		2 => 'session.save_path',
2349
		3 => 'cachedir'
2350
	);
2351
2352
	// Determine if we should detect a restriction and what restrictions that may be.
2353
	$open_base_dir = ini_get('open_basedir');
2354
	$restriction = !empty($open_base_dir) ? explode(':', $open_base_dir) : false;
2355
2356
	// Prevent any errors as we search.
2357
	$old_error_reporting = error_reporting(0);
2358
2359
	// Search for a working temp directory.
2360
	foreach ($temp_dir_options as $id_temp => $temp_option)
2361
	{
2362
		switch ($temp_option) {
2363
			case 'cachedir':
2364
				$possible_temp = rtrim($cachedir, '/');
2365
				break;
2366
2367
			case 'session.save_path':
2368
				$possible_temp = rtrim(ini_get('session.save_path'), '/');
2369
				break;
2370
2371
			case 'upload_tmp_dir':
2372
				$possible_temp = rtrim(ini_get('upload_tmp_dir'), '/');
2373
				break;
2374
2375
			default:
2376
				$possible_temp = sys_get_temp_dir();
2377
				break;
2378
		}
2379
2380
		// Check if we have a restriction preventing this from working.
2381
		if ($restriction)
2382
		{
2383
			foreach ($restriction as $dir)
2384
			{
2385
				if (strpos($possible_temp, $dir) !== false && is_writable($possible_temp))
2386
				{
2387
					$temp_dir = $possible_temp;
2388
					break;
2389
				}
2390
			}
2391
		}
2392
		// No restrictions, but need to check for writable status.
2393
		elseif (is_writable($possible_temp))
2394
		{
2395
			$temp_dir = $possible_temp;
2396
			break;
2397
		}
2398
	}
2399
2400
	// Fall back to sys_get_temp_dir even though it won't work, so we have something.
2401
	if (empty($temp_dir))
2402
		$temp_dir = sys_get_temp_dir();
2403
2404
	// Fix the path.
2405
	$temp_dir = substr($temp_dir, -1) === '/' ? $temp_dir : $temp_dir . '/';
2406
2407
	// Put things back.
2408
	error_reporting($old_error_reporting);
2409
2410
	return $temp_dir;
2411
}
2412
2413
?>