Passed
Pull Request — release-2.1 (#6764)
by
unknown
04:59
created

download_ccSLD_list()   A

Complexity

Conditions 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines https://www.simplemachines.org
8
 * @copyright 2021 Simple Machines and individual contributors
9
 * @license https://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 RC3
12
 */
13
14
define('SMF_VERSION', '2.1 RC3');
15
define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION);
16
define('SMF_SOFTWARE_YEAR', '2021');
17
define('DB_SCRIPT_VERSION', '2-1');
18
define('SMF_INSTALLING', 1);
19
20
define('JQUERY_VERSION', '3.5.1');
21
define('POSTGRE_TITLE', 'PostgreSQL');
22
define('MYSQL_TITLE', 'MySQL');
23
define('SMF_USER_AGENT', 'Mozilla/5.0 (' . php_uname('s') . ' ' . php_uname('m') . ') AppleWebKit/605.1.15 (KHTML, like Gecko)  SMF/' . strtr(SMF_VERSION, ' ', '.'));
24
if (!defined('TIME_START'))
25
	define('TIME_START', microtime(true));
26
27
$GLOBALS['required_php_version'] = '5.6.0';
28
29
// Don't have PHP support, do you?
30
// ><html dir="ltr"><head><title>Error!</title></head><body>Sorry, this installer requires PHP!<div style="display: none;">
31
32
// Let's pull in useful classes
33
if (!defined('SMF'))
34
	define('SMF', 1);
35
36
require_once('Sources/Class-Package.php');
37
38
// Database info.
39
$databases = array(
40
	'mysql' => array(
41
		'name' => 'MySQL',
42
		'version' => '5.0.22',
43
		'version_check' => 'return min(mysqli_get_server_info($db_connection), mysqli_get_client_info());',
44
		'supported' => function_exists('mysqli_connect'),
45
		'default_user' => 'mysql.default_user',
46
		'default_password' => 'mysql.default_password',
47
		'default_host' => 'mysql.default_host',
48
		'default_port' => 'mysql.default_port',
49
		'utf8_support' => function()
50
		{
51
			return true;
52
		},
53
		'utf8_version' => '5.0.22',
54
		'utf8_version_check' => 'return mysqli_get_server_info($db_connection);',
55
		'utf8_default' => true,
56
		'utf8_required' => true,
57
		'alter_support' => true,
58
		'validate_prefix' => function(&$value)
59
		{
60
			$value = preg_replace('~[^A-Za-z0-9_\$]~', '', $value);
61
			return true;
62
		},
63
	),
64
	'postgresql' => array(
65
		'name' => 'PostgreSQL',
66
		'version' => '9.4',
67
		'function_check' => 'pg_connect',
68
		'version_check' => '$request = pg_query(\'SELECT version()\'); list ($version) = pg_fetch_row($request); list($pgl, $version) = explode(" ", $version); return $version;',
69
		'supported' => function_exists('pg_connect'),
70
		'always_has_db' => true,
71
		'utf8_default' => true,
72
		'utf8_required' => true,
73
		'utf8_support' => function()
74
		{
75
			$request = pg_query('SHOW SERVER_ENCODING');
0 ignored issues
show
Bug introduced by
The call to pg_query() has too few arguments starting with query. ( Ignorable by Annotation )

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

75
			$request = /** @scrutinizer ignore-call */ pg_query('SHOW SERVER_ENCODING');

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
Bug introduced by
'SHOW SERVER_ENCODING' of type string is incompatible with the type resource expected by parameter $connection of pg_query(). ( Ignorable by Annotation )

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

75
			$request = pg_query(/** @scrutinizer ignore-type */ 'SHOW SERVER_ENCODING');
Loading history...
76
77
			list ($charcode) = pg_fetch_row($request);
78
79
			if ($charcode == 'UTF8')
80
				return true;
81
			else
82
				return false;
83
		},
84
		'utf8_version' => '8.0',
85
		'utf8_version_check' => '$request = pg_query(\'SELECT version()\'); list ($version) = pg_fetch_row($request); list($pgl, $version) = explode(" ", $version); return $version;',
86
		'validate_prefix' => function(&$value)
87
		{
88
			global $txt;
89
90
			$value = preg_replace('~[^A-Za-z0-9_\$]~', '', $value);
91
92
			// Is it reserved?
93
			if ($value == 'pg_')
94
				return $txt['error_db_prefix_reserved'];
95
96
			// Is the prefix numeric?
97
			if (preg_match('~^\d~', $value))
98
				return $txt['error_db_prefix_numeric'];
99
100
			return true;
101
		},
102
	),
103
);
104
105
global $txt;
106
107
// Initialize everything and load the language files.
108
initialize_inputs();
109
load_lang_file();
110
111
// This is what we are.
112
$installurl = $_SERVER['PHP_SELF'];
113
114
// All the steps in detail.
115
// Number,Name,Function,Progress Weight.
116
$incontext['steps'] = array(
117
	0 => array(1, $txt['install_step_welcome'], 'Welcome', 0),
118
	1 => array(2, $txt['install_step_writable'], 'CheckFilesWritable', 10),
119
	2 => array(3, $txt['install_step_databaseset'], 'DatabaseSettings', 15),
120
	3 => array(4, $txt['install_step_forum'], 'ForumSettings', 40),
121
	4 => array(5, $txt['install_step_databasechange'], 'DatabasePopulation', 15),
122
	5 => array(6, $txt['install_step_admin'], 'AdminAccount', 20),
123
	6 => array(7, $txt['install_step_delete'], 'DeleteInstall', 0),
124
);
125
126
// Default title...
127
$incontext['page_title'] = $txt['smf_installer'];
128
129
// What step are we on?
130
$incontext['current_step'] = isset($_GET['step']) ? (int) $_GET['step'] : 0;
131
132
// Loop through all the steps doing each one as required.
133
$incontext['overall_percent'] = 0;
134
135
foreach ($incontext['steps'] as $num => $step)
136
{
137
	if ($num >= $incontext['current_step'])
138
	{
139
		// The current weight of this step in terms of overall progress.
140
		$incontext['step_weight'] = $step[3];
141
		// Make sure we reset the skip button.
142
		$incontext['skip'] = false;
143
144
		// Call the step and if it returns false that means pause!
145
		if (function_exists($step[2]) && $step[2]() === false)
146
			break;
147
		elseif (function_exists($step[2]))
148
			$incontext['current_step']++;
149
150
		// No warnings pass on.
151
		$incontext['warning'] = '';
152
	}
153
	$incontext['overall_percent'] += $step[3];
154
}
155
156
// Actually do the template stuff.
157
installExit();
158
159
function initialize_inputs()
160
{
161
	global $databases;
162
163
	// Just so people using older versions of PHP aren't left in the cold.
164
	if (!isset($_SERVER['PHP_SELF']))
165
		$_SERVER['PHP_SELF'] = isset($GLOBALS['HTTP_SERVER_VARS']['PHP_SELF']) ? $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'] : 'install.php';
166
167
	// In pre-release versions, report all errors.
168
	if (strspn(SMF_VERSION, '1234567890.') !== strlen(SMF_VERSION))
169
		error_reporting(E_ALL);
170
	// Otherwise, report all errors except for deprecation notices.
171
	else
172
		error_reporting(E_ALL & ~E_DEPRECATED);
173
174
	// Fun.  Low PHP version...
175
	if (!isset($_GET))
176
	{
177
		$GLOBALS['_GET']['step'] = 0;
178
		return;
179
	}
180
181
	if (!isset($_GET['obgz']))
182
	{
183
		ob_start();
184
185
		if (ini_get('session.save_handler') == 'user')
186
			@ini_set('session.save_handler', 'files');
187
		if (function_exists('session_start'))
188
			@session_start();
189
	}
190
	else
191
	{
192
		ob_start('ob_gzhandler');
193
194
		if (ini_get('session.save_handler') == 'user')
195
			@ini_set('session.save_handler', 'files');
196
		session_start();
197
198
		if (!headers_sent())
199
			echo '<!DOCTYPE html>
200
<html>
201
	<head>
202
		<title>', htmlspecialchars($_GET['pass_string']), '</title>
203
	</head>
204
	<body style="background-color: #d4d4d4; margin-top: 16%; text-align: center; font-size: 16pt;">
205
		<strong>', htmlspecialchars($_GET['pass_string']), '</strong>
206
	</body>
207
</html>';
208
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
209
	}
210
211
	// This is really quite simple; if ?delete is on the URL, delete the installer...
212
	if (isset($_GET['delete']))
213
	{
214
		if (isset($_SESSION['installer_temp_ftp']))
215
		{
216
			$ftp = new ftp_connection($_SESSION['installer_temp_ftp']['server'], $_SESSION['installer_temp_ftp']['port'], $_SESSION['installer_temp_ftp']['username'], $_SESSION['installer_temp_ftp']['password']);
217
			$ftp->chdir($_SESSION['installer_temp_ftp']['path']);
218
219
			$ftp->unlink('install.php');
220
221
			foreach ($databases as $key => $dummy)
222
			{
223
				$type = ($key == 'mysqli') ? 'mysql' : $key;
224
				$ftp->unlink('install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql');
225
			}
226
227
			$ftp->close();
228
229
			unset($_SESSION['installer_temp_ftp']);
230
		}
231
		else
232
		{
233
			@unlink(__FILE__);
234
235
			foreach ($databases as $key => $dummy)
236
			{
237
				$type = ($key == 'mysqli') ? 'mysql' : $key;
238
				@unlink(dirname(__FILE__) . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql');
239
			}
240
		}
241
242
		// Now just redirect to a blank.png...
243
		$secure = false;
244
245
		if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
246
			$secure = true;
247
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on')
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! empty($_SERVER['HTTP_...FORWARDED_SSL'] == 'on', Probably Intended Meaning: ! empty($_SERVER['HTTP_X...ORWARDED_SSL'] == 'on')
Loading history...
248
			$secure = true;
249
250
		header('location: http' . ($secure ? 's' : '') . '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']) . dirname($_SERVER['PHP_SELF']) . '/Themes/default/images/blank.png');
251
		exit;
252
	}
253
254
	// PHP 5 might cry if we don't do this now.
255
	if (function_exists('date_default_timezone_set'))
256
	{
257
		// Get PHP's default timezone, if set
258
		$ini_tz = ini_get('date.timezone');
259
		if (!empty($ini_tz))
260
			$timezone_id = $ini_tz;
261
		else
262
			$timezone_id = '';
263
264
		// If date.timezone is unset, invalid, or just plain weird, make a best guess
265
		if (!in_array($timezone_id, timezone_identifiers_list()))
0 ignored issues
show
Bug introduced by
timezone_identifiers_list() of type void is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

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

265
		if (!in_array($timezone_id, /** @scrutinizer ignore-type */ timezone_identifiers_list()))
Loading history...
Bug introduced by
Are you sure the usage of timezone_identifiers_list() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
266
		{
267
			$server_offset = @mktime(0, 0, 0, 1, 1, 1970);
268
			$timezone_id = timezone_name_from_abbr('', $server_offset, 0);
0 ignored issues
show
Bug introduced by
It seems like $server_offset can also be of type false; however, parameter $utcOffset of timezone_name_from_abbr() does only seem to accept integer, 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

268
			$timezone_id = timezone_name_from_abbr('', /** @scrutinizer ignore-type */ $server_offset, 0);
Loading history...
269
		}
270
271
		date_default_timezone_set($timezone_id);
272
	}
273
	header('X-Frame-Options: SAMEORIGIN');
274
	header('X-XSS-Protection: 1');
275
	header('X-Content-Type-Options: nosniff');
276
277
	// Force an integer step, defaulting to 0.
278
	$_GET['step'] = (int) @$_GET['step'];
279
}
280
281
// Load the list of language files, and the current language file.
282
function load_lang_file()
283
{
284
	global $incontext, $user_info, $txt;
285
286
	$incontext['detected_languages'] = array();
287
288
	// Make sure the languages directory actually exists.
289
	if (file_exists(dirname(__FILE__) . '/Themes/default/languages'))
290
	{
291
		// Find all the "Install" language files in the directory.
292
		$dir = dir(dirname(__FILE__) . '/Themes/default/languages');
293
		while ($entry = $dir->read())
294
		{
295
			if (substr($entry, 0, 8) == 'Install.' && substr($entry, -4) == '.php')
296
				$incontext['detected_languages'][$entry] = ucfirst(substr($entry, 8, strlen($entry) - 12));
297
		}
298
		$dir->close();
299
	}
300
301
	// Didn't find any, show an error message!
302
	if (empty($incontext['detected_languages']))
303
	{
304
		// Let's not cache this message, eh?
305
		header('expires: Mon, 26 Jul 1997 05:00:00 GMT');
306
		header('last-modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
307
		header('cache-control: no-cache');
308
309
		echo '<!DOCTYPE html>
310
<html>
311
	<head>
312
		<title>SMF Installer: Error!</title>
313
		<style>
314
			body {
315
				font-family: sans-serif;
316
				max-width: 700px; }
317
318
			h1 {
319
				font-size: 14pt; }
320
321
			.directory {
322
				margin: 0.3em;
323
				font-family: monospace;
324
				font-weight: bold; }
325
		</style>
326
	</head>
327
	<body>
328
		<h1>A critical error has occurred.</h1>
329
330
		<p>This installer was unable to find the installer\'s language file or files. They should be found under:</p>
331
332
		<div class="directory">', dirname($_SERVER['PHP_SELF']) != '/' ? dirname($_SERVER['PHP_SELF']) : '', '/Themes/default/languages</div>
333
334
		<p>In some cases, FTP clients do not properly upload files with this many folders. Please double check to make sure you <strong>have uploaded all the files in the distribution</strong>.</p>
335
		<p>If that doesn\'t help, please make sure this install.php file is in the same place as the Themes folder.</p>
336
		<p>If you continue to get this error message, feel free to <a href="https://support.simplemachines.org/">look to us for support</a>.</p>
337
	</div></body>
338
</html>';
339
		die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
340
	}
341
342
	// Override the language file?
343
	if (isset($_GET['lang_file']))
344
		$_SESSION['installer_temp_lang'] = $_GET['lang_file'];
345
	elseif (isset($GLOBALS['HTTP_GET_VARS']['lang_file']))
346
		$_SESSION['installer_temp_lang'] = $GLOBALS['HTTP_GET_VARS']['lang_file'];
347
348
	// Make sure it exists, if it doesn't reset it.
349
	if (!isset($_SESSION['installer_temp_lang']) || preg_match('~[^\\w_\\-.]~', $_SESSION['installer_temp_lang']) === 1 || !file_exists(dirname(__FILE__) . '/Themes/default/languages/' . $_SESSION['installer_temp_lang']))
350
	{
351
		// Use the first one...
352
		list ($_SESSION['installer_temp_lang']) = array_keys($incontext['detected_languages']);
353
354
		// If we have english and some other language, use the other language.  We Americans hate english :P.
355
		if ($_SESSION['installer_temp_lang'] == 'Install.english.php' && count($incontext['detected_languages']) > 1)
356
			list (, $_SESSION['installer_temp_lang']) = array_keys($incontext['detected_languages']);
357
	}
358
359
	// And now include the actual language file itself.
360
	require_once(dirname(__FILE__) . '/Themes/default/languages/' . $_SESSION['installer_temp_lang']);
361
362
	// Which language did we load? Assume that he likes his language.
363
	preg_match('~^Install\.(.+[^-utf8])\.php$~', $_SESSION['installer_temp_lang'], $matches);
364
	$user_info['language'] = $matches[1];
365
}
366
367
// This handy function loads some settings and the like.
368
function load_database()
369
{
370
	global $db_prefix, $db_connection, $sourcedir, $smcFunc, $modSettings, $db_port;
371
	global $db_server, $db_passwd, $db_type, $db_name, $db_user, $db_persist, $db_mb4;
372
373
	if (empty($sourcedir))
374
		$sourcedir = dirname(__FILE__) . '/Sources';
375
376
	// Need this to check whether we need the database password.
377
	require(dirname(__FILE__) . '/Settings.php');
378
	if (!defined('SMF'))
379
		define('SMF', 1);
380
	if (empty($smcFunc))
381
		$smcFunc = array();
382
383
	$modSettings['disableQueryCheck'] = true;
384
385
	// Connect the database.
386
	if (!$db_connection)
387
	{
388
		require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
389
390
		$options = array('persist' => $db_persist);
391
392
		if (!empty($db_port))
393
			$options['port'] = $db_port;
394
395
		if (!empty($db_mb4))
396
			$options['db_mb4'] = $db_mb4;
397
398
		if (!$db_connection)
399
			$db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $options);
400
	}
401
}
402
403
// This is called upon exiting the installer, for template etc.
404
function installExit($fallThrough = false)
405
{
406
	global $incontext, $installurl, $txt;
407
408
	// Send character set.
409
	header('content-type: text/html; charset=' . (isset($txt['lang_character_set']) ? $txt['lang_character_set'] : 'UTF-8'));
410
411
	// We usually dump our templates out.
412
	if (!$fallThrough)
413
	{
414
		// The top install bit.
415
		template_install_above();
416
417
		// Call the template.
418
		if (isset($incontext['sub_template']))
419
		{
420
			$incontext['form_url'] = $installurl . '?step=' . $incontext['current_step'];
421
422
			call_user_func('template_' . $incontext['sub_template']);
423
		}
424
		// @todo REMOVE THIS!!
425
		else
426
		{
427
			if (function_exists('doStep' . $_GET['step']))
428
				call_user_func('doStep' . $_GET['step']);
429
		}
430
		// Show the footer.
431
		template_install_below();
432
	}
433
434
	// Bang - gone!
435
	die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
436
}
437
438
function Welcome()
439
{
440
	global $incontext, $txt, $databases, $installurl;
441
442
	$incontext['page_title'] = $txt['install_welcome'];
443
	$incontext['sub_template'] = 'welcome_message';
444
445
	// Done the submission?
446
	if (isset($_POST['contbutt']))
447
		return true;
448
449
	// See if we think they have already installed it?
450
	if (is_readable(dirname(__FILE__) . '/Settings.php'))
451
	{
452
		$probably_installed = 0;
453
		foreach (file(dirname(__FILE__) . '/Settings.php') as $line)
454
		{
455
			if (preg_match('~^\$db_passwd\s=\s\'([^\']+)\';$~', $line))
456
				$probably_installed++;
457
			if (preg_match('~^\$boardurl\s=\s\'([^\']+)\';~', $line) && !preg_match('~^\$boardurl\s=\s\'http://127\.0\.0\.1/smf\';~', $line))
458
				$probably_installed++;
459
		}
460
461
		if ($probably_installed == 2)
462
			$incontext['warning'] = $txt['error_already_installed'];
463
	}
464
465
	// Is some database support even compiled in?
466
	$incontext['supported_databases'] = array();
467
	foreach ($databases as $key => $db)
468
	{
469
		if ($db['supported'])
470
		{
471
			$type = ($key == 'mysqli') ? 'mysql' : $key;
472
			if (!file_exists(dirname(__FILE__) . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql'))
473
			{
474
				$databases[$key]['supported'] = false;
475
				$notFoundSQLFile = true;
476
				$txt['error_db_script_missing'] = sprintf($txt['error_db_script_missing'], 'install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql');
477
			}
478
			else
479
				$incontext['supported_databases'][] = $db;
480
		}
481
	}
482
483
	// Check the PHP version.
484
	if ((!function_exists('version_compare') || version_compare($GLOBALS['required_php_version'], PHP_VERSION, '>=')))
485
		$error = 'error_php_too_low';
486
	// Make sure we have a supported database
487
	elseif (empty($incontext['supported_databases']))
488
		$error = empty($notFoundSQLFile) ? 'error_db_missing' : 'error_db_script_missing';
489
	// How about session support?  Some crazy sysadmin remove it?
490
	elseif (!function_exists('session_start'))
491
		$error = 'error_session_missing';
492
	// Make sure they uploaded all the files.
493
	elseif (!file_exists(dirname(__FILE__) . '/index.php'))
494
		$error = 'error_missing_files';
495
	// Very simple check on the session.save_path for Windows.
496
	// @todo Move this down later if they don't use database-driven sessions?
497
	elseif (@ini_get('session.save_path') == '/tmp' && substr(__FILE__, 1, 2) == ':\\')
498
		$error = 'error_session_save_path';
499
500
	// Since each of the three messages would look the same, anyway...
501
	if (isset($error))
502
		$incontext['error'] = $txt[$error];
503
504
	// Mod_security blocks everything that smells funny. Let SMF handle security.
505
	if (!fixModSecurity() && !isset($_GET['overmodsecurity']))
506
		$incontext['error'] = $txt['error_mod_security'] . '<br><br><a href="' . $installurl . '?overmodsecurity=true">' . $txt['error_message_click'] . '</a> ' . $txt['error_message_bad_try_again'];
507
508
	// Confirm mbstring is loaded...
509
	if (!extension_loaded('mbstring'))
510
		$incontext['error'] = $txt['install_no_mbstring'];
511
512
	// Check for https stream support.
513
	$supported_streams = stream_get_wrappers();
514
	if (!in_array('https', $supported_streams))
515
		$incontext['warning'] = $txt['install_no_https'];
516
517
	return false;
518
}
519
520
function CheckFilesWritable()
521
{
522
	global $txt, $incontext;
523
524
	$incontext['page_title'] = $txt['ftp_checking_writable'];
525
	$incontext['sub_template'] = 'chmod_files';
526
527
	$writable_files = array(
528
		'attachments',
529
		'avatars',
530
		'custom_avatar',
531
		'cache',
532
		'Packages',
533
		'Smileys',
534
		'Themes',
535
		'agreement.txt',
536
		'Settings.php',
537
		'Settings_bak.php',
538
		'cache/db_last_error.php',
539
	);
540
541
	foreach ($incontext['detected_languages'] as $lang => $temp)
542
		$extra_files[] = 'Themes/default/languages/' . $lang;
543
544
	// With mod_security installed, we could attempt to fix it with .htaccess.
545
	if (function_exists('apache_get_modules') && in_array('mod_security', apache_get_modules()))
546
		$writable_files[] = file_exists(dirname(__FILE__) . '/.htaccess') ? '.htaccess' : '.';
547
548
	$failed_files = array();
549
550
	// On linux, it's easy - just use is_writable!
551
	if (substr(__FILE__, 1, 2) != ':\\')
552
	{
553
		$incontext['systemos'] = 'linux';
554
555
		foreach ($writable_files as $file)
556
		{
557
			// Some files won't exist, try to address up front
558
			if (!file_exists(dirname(__FILE__) . '/' . $file))
559
				@touch(dirname(__FILE__) . '/' . $file);
560
			// NOW do the writable check...
561
			if (!is_writable(dirname(__FILE__) . '/' . $file))
562
			{
563
				@chmod(dirname(__FILE__) . '/' . $file, 0755);
564
565
				// Well, 755 hopefully worked... if not, try 777.
566
				if (!is_writable(dirname(__FILE__) . '/' . $file) && !@chmod(dirname(__FILE__) . '/' . $file, 0777))
567
					$failed_files[] = $file;
568
			}
569
		}
570
		foreach ($extra_files as $file)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $extra_files seems to be defined by a foreach iteration on line 541. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
571
			@chmod(dirname(__FILE__) . (empty($file) ? '' : '/' . $file), 0777);
572
	}
573
	// Windows is trickier.  Let's try opening for r+...
574
	else
575
	{
576
		$incontext['systemos'] = 'windows';
577
578
		foreach ($writable_files as $file)
579
		{
580
			// Folders can't be opened for write... but the index.php in them can ;)
581
			if (is_dir(dirname(__FILE__) . '/' . $file))
582
				$file .= '/index.php';
583
584
			// Funny enough, chmod actually does do something on windows - it removes the read only attribute.
585
			@chmod(dirname(__FILE__) . '/' . $file, 0777);
586
			$fp = @fopen(dirname(__FILE__) . '/' . $file, 'r+');
587
588
			// Hmm, okay, try just for write in that case...
589
			if (!is_resource($fp))
590
				$fp = @fopen(dirname(__FILE__) . '/' . $file, 'w');
591
592
			if (!is_resource($fp))
593
				$failed_files[] = $file;
594
595
			@fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $stream of fclose() does only seem to accept resource, 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

595
			@fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
596
		}
597
		foreach ($extra_files as $file)
598
			@chmod(dirname(__FILE__) . (empty($file) ? '' : '/' . $file), 0777);
599
	}
600
601
	$failure = count($failed_files) >= 1;
602
603
	if (!isset($_SERVER))
604
		return !$failure;
605
606
	// Put the list into context.
607
	$incontext['failed_files'] = $failed_files;
608
609
	// It's not going to be possible to use FTP on windows to solve the problem...
610
	if ($failure && substr(__FILE__, 1, 2) == ':\\')
611
	{
612
		$incontext['error'] = $txt['error_windows_chmod'] . '
613
					<ul class="error_content">
614
						<li>' . implode('</li>
615
						<li>', $failed_files) . '</li>
616
					</ul>';
617
618
		return false;
619
	}
620
	// We're going to have to use... FTP!
621
	elseif ($failure)
622
	{
623
		// Load any session data we might have...
624
		if (!isset($_POST['ftp_username']) && isset($_SESSION['installer_temp_ftp']))
625
		{
626
			$_POST['ftp_server'] = $_SESSION['installer_temp_ftp']['server'];
627
			$_POST['ftp_port'] = $_SESSION['installer_temp_ftp']['port'];
628
			$_POST['ftp_username'] = $_SESSION['installer_temp_ftp']['username'];
629
			$_POST['ftp_password'] = $_SESSION['installer_temp_ftp']['password'];
630
			$_POST['ftp_path'] = $_SESSION['installer_temp_ftp']['path'];
631
		}
632
633
		$incontext['ftp_errors'] = array();
634
		require_once('Sources/Class-Package.php');
635
		if (isset($_POST['ftp_username']))
636
		{
637
			$ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
638
639
			if ($ftp->error === false)
0 ignored issues
show
introduced by
The condition $ftp->error === false is always false.
Loading history...
640
			{
641
				// Try it without /home/abc just in case they messed up.
642
				if (!$ftp->chdir($_POST['ftp_path']))
643
				{
644
					$incontext['ftp_errors'][] = $ftp->last_message;
645
					$ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
646
				}
647
			}
648
		}
649
650
		if (!isset($ftp) || $ftp->error !== false)
651
		{
652
			if (!isset($ftp))
653
				$ftp = new ftp_connection(null);
654
			// Save the error so we can mess with listing...
655
			elseif ($ftp->error !== false && empty($incontext['ftp_errors']) && !empty($ftp->last_message))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ftp does not seem to be defined for all execution paths leading up to this point.
Loading history...
656
				$incontext['ftp_errors'][] = $ftp->last_message;
657
658
			list ($username, $detect_path, $found_path) = $ftp->detect_path(dirname(__FILE__));
659
660
			if (empty($_POST['ftp_path']) && $found_path)
661
				$_POST['ftp_path'] = $detect_path;
662
663
			if (!isset($_POST['ftp_username']))
664
				$_POST['ftp_username'] = $username;
665
666
			// Set the username etc, into context.
667
			$incontext['ftp'] = array(
668
				'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : 'localhost',
669
				'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : '21',
670
				'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : '',
671
				'path' => isset($_POST['ftp_path']) ? $_POST['ftp_path'] : '/',
672
				'path_msg' => !empty($found_path) ? $txt['ftp_path_found_info'] : $txt['ftp_path_info'],
673
			);
674
675
			return false;
676
		}
677
		else
678
		{
679
			$_SESSION['installer_temp_ftp'] = array(
680
				'server' => $_POST['ftp_server'],
681
				'port' => $_POST['ftp_port'],
682
				'username' => $_POST['ftp_username'],
683
				'password' => $_POST['ftp_password'],
684
				'path' => $_POST['ftp_path']
685
			);
686
687
			$failed_files_updated = array();
688
689
			foreach ($failed_files as $file)
690
			{
691
				if (!is_writable(dirname(__FILE__) . '/' . $file))
692
					$ftp->chmod($file, 0755);
693
				if (!is_writable(dirname(__FILE__) . '/' . $file))
694
					$ftp->chmod($file, 0777);
695
				if (!is_writable(dirname(__FILE__) . '/' . $file))
696
				{
697
					$failed_files_updated[] = $file;
698
					$incontext['ftp_errors'][] = rtrim($ftp->last_message) . ' -> ' . $file . "\n";
699
				}
700
			}
701
702
			$ftp->close();
703
704
			// Are there any errors left?
705
			if (count($failed_files_updated) >= 1)
706
			{
707
				// Guess there are...
708
				$incontext['failed_files'] = $failed_files_updated;
709
710
				// Set the username etc, into context.
711
				$incontext['ftp'] = $_SESSION['installer_temp_ftp'] += array(
712
					'path_msg' => $txt['ftp_path_info'],
713
				);
714
715
				return false;
716
			}
717
		}
718
	}
719
720
	return true;
721
}
722
723
function DatabaseSettings()
724
{
725
	global $txt, $databases, $incontext, $smcFunc, $sourcedir;
726
	global $db_server, $db_name, $db_user, $db_passwd, $db_port, $db_mb4;
727
728
	$incontext['sub_template'] = 'database_settings';
729
	$incontext['page_title'] = $txt['db_settings'];
730
	$incontext['continue'] = 1;
731
732
	// Set up the defaults.
733
	$incontext['db']['server'] = 'localhost';
734
	$incontext['db']['user'] = '';
735
	$incontext['db']['name'] = '';
736
	$incontext['db']['pass'] = '';
737
	$incontext['db']['type'] = '';
738
	$incontext['supported_databases'] = array();
739
740
	$foundOne = false;
741
	foreach ($databases as $key => $db)
742
	{
743
		// Override with the defaults for this DB if appropriate.
744
		if ($db['supported'])
745
		{
746
			$incontext['supported_databases'][$key] = $db;
747
748
			if (!$foundOne)
749
			{
750
				if (isset($db['default_host']))
751
					$incontext['db']['server'] = ini_get($db['default_host']) or $incontext['db']['server'] = 'localhost';
752
				if (isset($db['default_user']))
753
				{
754
					$incontext['db']['user'] = ini_get($db['default_user']);
755
					$incontext['db']['name'] = ini_get($db['default_user']);
756
				}
757
				if (isset($db['default_password']))
758
					$incontext['db']['pass'] = ini_get($db['default_password']);
759
760
				// For simplicity and less confusion, leave the port blank by default
761
				$incontext['db']['port'] = '';
762
763
				$incontext['db']['type'] = $key;
764
				$foundOne = true;
765
			}
766
		}
767
	}
768
769
	// Override for repost.
770
	if (isset($_POST['db_user']))
771
	{
772
		$incontext['db']['user'] = $_POST['db_user'];
773
		$incontext['db']['name'] = $_POST['db_name'];
774
		$incontext['db']['server'] = $_POST['db_server'];
775
		$incontext['db']['prefix'] = $_POST['db_prefix'];
776
777
		if (!empty($_POST['db_port']))
778
			$incontext['db']['port'] = $_POST['db_port'];
779
	}
780
	else
781
	{
782
		$incontext['db']['prefix'] = 'smf_';
783
	}
784
785
	// Are we submitting?
786
	if (isset($_POST['db_type']))
787
	{
788
		// What type are they trying?
789
		$db_type = preg_replace('~[^A-Za-z0-9]~', '', $_POST['db_type']);
790
		$db_prefix = $_POST['db_prefix'];
791
		// Validate the prefix.
792
		$valid_prefix = $databases[$db_type]['validate_prefix']($db_prefix);
793
794
		if ($valid_prefix !== true)
795
		{
796
			$incontext['error'] = $valid_prefix;
797
			return false;
798
		}
799
800
		// Take care of these variables...
801
		$vars = array(
802
			'db_type' => $db_type,
803
			'db_name' => $_POST['db_name'],
804
			'db_user' => $_POST['db_user'],
805
			'db_passwd' => isset($_POST['db_passwd']) ? $_POST['db_passwd'] : '',
806
			'db_server' => $_POST['db_server'],
807
			'db_prefix' => $db_prefix,
808
			// The cookiename is special; we want it to be the same if it ever needs to be reinstalled with the same info.
809
			'cookiename' => 'SMFCookie' . abs(crc32($_POST['db_name'] . preg_replace('~[^A-Za-z0-9_$]~', '', $_POST['db_prefix'])) % 1000),
810
		);
811
812
		// Only set the port if we're not using the default
813
		if (!empty($_POST['db_port']))
814
		{
815
			// For MySQL, we can get the "default port" from PHP. PostgreSQL has no such option though.
816
			if (($db_type == 'mysql' || $db_type == 'mysqli') && $_POST['db_port'] != ini_get($db_type . '.default_port'))
817
				$vars['db_port'] = (int) $_POST['db_port'];
818
			elseif ($db_type == 'postgresql' && $_POST['db_port'] != 5432)
819
				$vars['db_port'] = (int) $_POST['db_port'];
820
		}
821
822
		// God I hope it saved!
823
		if (!installer_updateSettingsFile($vars))
824
		{
825
			$incontext['error'] = $txt['settings_error'];
826
			return false;
827
		}
828
829
		// Make sure it works.
830
		require(dirname(__FILE__) . '/Settings.php');
831
832
		if (empty($sourcedir))
833
			$sourcedir = dirname(__FILE__) . '/Sources';
834
835
		// Better find the database file!
836
		if (!file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php'))
837
		{
838
			$incontext['error'] = sprintf($txt['error_db_file'], 'Subs-Db-' . $db_type . '.php');
839
			return false;
840
		}
841
842
		// Now include it for database functions!
843
		if (!defined('SMF'))
844
			define('SMF', 1);
845
846
		$modSettings['disableQueryCheck'] = true;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$modSettings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $modSettings = array(); before regardless.
Loading history...
847
		if (empty($smcFunc))
848
			$smcFunc = array();
849
850
		require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
851
852
		// Attempt a connection.
853
		$needsDB = !empty($databases[$db_type]['always_has_db']);
854
855
		$options = array('non_fatal' => true, 'dont_select_db' => !$needsDB);
856
		// Add in the port if needed
857
		if (!empty($db_port))
858
			$options['port'] = $db_port;
859
860
		if (!empty($db_mb4))
861
			$options['db_mb4'] = $db_mb4;
862
863
		$db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $options);
864
865
		// Still no connection?  Big fat error message :P.
866
		if (!$db_connection)
0 ignored issues
show
introduced by
$db_connection is of type null|resource, thus it always evaluated to false.
Loading history...
867
		{
868
			// Get error info...  Recast just in case we get false or 0...
869
			$error_message = $smcFunc['db_connect_error']();
870
			if (empty($error_message))
871
				$error_message = '';
872
			$error_number = $smcFunc['db_connect_errno']();
873
			if (empty($error_number))
874
				$error_number = '';
875
			$db_error = (!empty($error_number) ? $error_number . ': ' : '') . $error_message;
876
877
			$incontext['error'] = $txt['error_db_connect'] . '<div class="error_content"><strong>' . $db_error . '</strong></div>';
878
			return false;
879
		}
880
881
		// Do they meet the install requirements?
882
		// @todo Old client, new server?
883
		if (version_compare($databases[$db_type]['version'], preg_replace('~^\D*|\-.+?$~', '', eval($databases[$db_type]['version_check']))) > 0)
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
884
		{
885
			$incontext['error'] = $txt['error_db_too_low'];
886
			return false;
887
		}
888
889
		// Let's try that database on for size... assuming we haven't already lost the opportunity.
890
		if ($db_name != '' && !$needsDB)
891
		{
892
			$smcFunc['db_query']('', "
893
				CREATE DATABASE IF NOT EXISTS `$db_name`",
894
				array(
895
					'security_override' => true,
896
					'db_error_skip' => true,
897
				),
898
				$db_connection
899
			);
900
901
			// Okay, let's try the prefix if it didn't work...
902
			if (!$smcFunc['db_select_db']($db_name, $db_connection) && $db_name != '')
903
			{
904
				$smcFunc['db_query']('', "
905
					CREATE DATABASE IF NOT EXISTS `$_POST[db_prefix]$db_name`",
906
					array(
907
						'security_override' => true,
908
						'db_error_skip' => true,
909
					),
910
					$db_connection
911
				);
912
913
				if ($smcFunc['db_select_db']($_POST['db_prefix'] . $db_name, $db_connection))
914
				{
915
					$db_name = $_POST['db_prefix'] . $db_name;
916
					installer_updateSettingsFile(array('db_name' => $db_name));
917
				}
918
			}
919
920
			// Okay, now let's try to connect...
921
			if (!$smcFunc['db_select_db']($db_name, $db_connection))
922
			{
923
				$incontext['error'] = sprintf($txt['error_db_database'], $db_name);
924
				return false;
925
			}
926
		}
927
928
		return true;
929
	}
930
931
	return false;
932
}
933
934
// Let's start with basic forum type settings.
935
function ForumSettings()
936
{
937
	global $txt, $incontext, $databases, $db_type, $db_connection, $smcFunc;
938
939
	$incontext['sub_template'] = 'forum_settings';
940
	$incontext['page_title'] = $txt['install_settings'];
941
942
	// Let's see if we got the database type correct.
943
	if (isset($_POST['db_type'], $databases[$_POST['db_type']]))
944
		$db_type = $_POST['db_type'];
945
946
	// Else we'd better be able to get the connection.
947
	else
948
		load_database();
949
950
	$db_type = isset($_POST['db_type']) ? $_POST['db_type'] : $db_type;
951
952
	// What host and port are we on?
953
	$host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . (empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT']) : $_SERVER['HTTP_HOST'];
954
955
	$secure = false;
956
957
	if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
958
		$secure = true;
959
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on')
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! empty($_SERVER['HTTP_...FORWARDED_SSL'] == 'on', Probably Intended Meaning: ! empty($_SERVER['HTTP_X...ORWARDED_SSL'] == 'on')
Loading history...
960
		$secure = true;
961
962
	// Now, to put what we've learned together... and add a path.
963
	$incontext['detected_url'] = 'http' . ($secure ? 's' : '') . '://' . $host . substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));
964
965
	// Check if the database sessions will even work.
966
	$incontext['test_dbsession'] = (ini_get('session.auto_start') != 1);
967
	$incontext['utf8_default'] = $databases[$db_type]['utf8_default'];
968
	$incontext['utf8_required'] = $databases[$db_type]['utf8_required'];
969
970
	$incontext['continue'] = 1;
971
972
	// Check Postgres setting
973
	if ( $db_type === 'postgresql')
974
	{
975
		load_database();
976
		$result = $smcFunc['db_query']('', '
977
			show standard_conforming_strings',
978
			array(
979
				'db_error_skip' => true,
980
			)
981
		);
982
983
		if ($result !== false)
984
		{
985
			$row = $smcFunc['db_fetch_assoc']($result);
986
			if ($row['standard_conforming_strings'] !== 'on')
987
				{
988
					$incontext['continue'] = 0;
989
					$incontext['error'] = $txt['error_pg_scs'];
990
				}
991
			$smcFunc['db_free_result']($result);
992
		}
993
	}
994
995
	// Setup the SSL checkbox...
996
	$incontext['ssl_chkbx_protected'] = false;
997
	$incontext['ssl_chkbx_checked'] = false;
998
999
	// If redirect in effect, force ssl ON
1000
	require_once(dirname(__FILE__) . '/Sources/Subs.php');
1001
	if (https_redirect_active($incontext['detected_url']))
1002
	{
1003
		$incontext['ssl_chkbx_protected'] = true;
1004
		$incontext['ssl_chkbx_checked'] = true;
1005
		$_POST['force_ssl'] = true;
1006
	}
1007
	// If no cert, make sure ssl stays OFF
1008
	if (!ssl_cert_found($incontext['detected_url']))
1009
	{
1010
		$incontext['ssl_chkbx_protected'] = true;
1011
		$incontext['ssl_chkbx_checked'] = false;
1012
	}
1013
1014
	// Submitting?
1015
	if (isset($_POST['boardurl']))
1016
	{
1017
		if (substr($_POST['boardurl'], -10) == '/index.php')
1018
			$_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
1019
		elseif (substr($_POST['boardurl'], -1) == '/')
1020
			$_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
1021
		if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://')
1022
			$_POST['boardurl'] = 'http://' . $_POST['boardurl'];
1023
1024
		// Make sure boardurl is aligned with ssl setting
1025
		if (empty($_POST['force_ssl']))
1026
			$_POST['boardurl'] = strtr($_POST['boardurl'], array('https://' => 'http://'));
1027
		else
1028
			$_POST['boardurl'] = strtr($_POST['boardurl'], array('http://' => 'https://'));
1029
1030
		// Deal with different operating systems' directory structure...
1031
		$path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', __DIR__), '/');
1032
1033
		// Load the compatibility library if necessary.
1034
		if (!is_callable('random_bytes'))
1035
			require_once('Sources/random_compat/random.php');
1036
1037
		// Save these variables.
1038
		$vars = array(
1039
			'boardurl' => $_POST['boardurl'],
1040
			'boarddir' => $path,
1041
			'sourcedir' => $path . '/Sources',
1042
			'cachedir' => $path . '/cache',
1043
			'packagesdir' => $path . '/Packages',
1044
			'tasksdir' => $path . '/Sources/tasks',
1045
			'mbname' => strtr($_POST['mbname'], array('\"' => '"')),
1046
			'language' => substr($_SESSION['installer_temp_lang'], 8, -4),
1047
			'image_proxy_secret' => bin2hex(random_bytes(10)),
1048
			'image_proxy_enabled' => !empty($_POST['force_ssl']),
1049
			'auth_secret' => bin2hex(random_bytes(32)),
1050
		);
1051
1052
		// Must save!
1053
		if (!installer_updateSettingsFile($vars))
1054
		{
1055
			$incontext['error'] = $txt['settings_error'];
1056
			return false;
1057
		}
1058
1059
		// Make sure it works.
1060
		require(dirname(__FILE__) . '/Settings.php');
1061
1062
		// UTF-8 requires a setting to override the language charset.
1063
		if ((!empty($databases[$db_type]['utf8_support']) && !empty($databases[$db_type]['utf8_required'])) || (empty($databases[$db_type]['utf8_required']) && !empty($databases[$db_type]['utf8_support']) && isset($_POST['utf8'])))
1064
		{
1065
			if (!$databases[$db_type]['utf8_support']())
1066
			{
1067
				$incontext['error'] = sprintf($txt['error_utf8_support']);
1068
				return false;
1069
			}
1070
1071
			if (!empty($databases[$db_type]['utf8_version_check']) && version_compare($databases[$db_type]['utf8_version'], preg_replace('~\-.+?$~', '', eval($databases[$db_type]['utf8_version_check'])), '>'))
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
1072
			{
1073
				$incontext['error'] = sprintf($txt['error_utf8_version'], $databases[$db_type]['utf8_version']);
1074
				return false;
1075
			}
1076
			else
1077
				// Set the character set here.
1078
				installer_updateSettingsFile(array('db_character_set' => 'utf8'));
1079
		}
1080
1081
		// Good, skip on.
1082
		return true;
1083
	}
1084
1085
	return false;
1086
}
1087
1088
// Step one: Do the SQL thang.
1089
function DatabasePopulation()
1090
{
1091
	global $db_character_set, $txt, $db_connection, $smcFunc, $databases, $modSettings, $db_type, $db_prefix, $incontext, $db_name, $boardurl;
1092
1093
	$incontext['sub_template'] = 'populate_database';
1094
	$incontext['page_title'] = $txt['db_populate'];
1095
	$incontext['continue'] = 1;
1096
1097
	// Already done?
1098
	if (isset($_POST['pop_done']))
1099
		return true;
1100
1101
	// Reload settings.
1102
	require(dirname(__FILE__) . '/Settings.php');
1103
	load_database();
1104
1105
	// Before running any of the queries, let's make sure another version isn't already installed.
1106
	$result = $smcFunc['db_query']('', '
1107
		SELECT variable, value
1108
		FROM {db_prefix}settings',
1109
		array(
1110
			'db_error_skip' => true,
1111
		)
1112
	);
1113
	$newSettings = array();
1114
	$modSettings = array();
1115
	if ($result !== false)
1116
	{
1117
		while ($row = $smcFunc['db_fetch_assoc']($result))
1118
			$modSettings[$row['variable']] = $row['value'];
1119
		$smcFunc['db_free_result']($result);
1120
1121
		// Do they match?  If so, this is just a refresh so charge on!
1122
		if (!isset($modSettings['smfVersion']) || $modSettings['smfVersion'] != SMF_VERSION)
1123
		{
1124
			$incontext['error'] = $txt['error_versions_do_not_match'];
1125
			return false;
1126
		}
1127
	}
1128
	$modSettings['disableQueryCheck'] = true;
1129
1130
	// If doing UTF8, select it. PostgreSQL requires passing it as a string...
1131
	if (!empty($db_character_set) && $db_character_set == 'utf8' && !empty($databases[$db_type]['utf8_support']))
1132
		$smcFunc['db_query']('', '
1133
			SET NAMES {string:utf8}',
1134
			array(
1135
				'db_error_skip' => true,
1136
				'utf8' => 'utf8',
1137
			)
1138
		);
1139
1140
	// Windows likes to leave the trailing slash, which yields to C:\path\to\SMF\/attachments...
1141
	if (substr(__DIR__, -1) == '\\')
1142
		$attachdir = __DIR__ . 'attachments';
1143
	else
1144
		$attachdir = __DIR__ . '/attachments';
1145
1146
	$replaces = array(
1147
		'{$db_prefix}' => $db_prefix,
1148
		'{$attachdir}' => json_encode(array(1 => $smcFunc['db_escape_string']($attachdir))),
1149
		'{$boarddir}' => $smcFunc['db_escape_string'](dirname(__FILE__)),
1150
		'{$boardurl}' => $boardurl,
1151
		'{$enableCompressedOutput}' => isset($_POST['compress']) ? '1' : '0',
1152
		'{$databaseSession_enable}' => isset($_POST['dbsession']) ? '1' : '0',
1153
		'{$smf_version}' => SMF_VERSION,
1154
		'{$current_time}' => time(),
1155
		'{$sched_task_offset}' => 82800 + mt_rand(0, 86399),
1156
		'{$registration_method}' => isset($_POST['reg_mode']) ? $_POST['reg_mode'] : 0,
1157
	);
1158
1159
	foreach ($txt as $key => $value)
1160
	{
1161
		if (substr($key, 0, 8) == 'default_')
1162
			$replaces['{$' . $key . '}'] = $smcFunc['db_escape_string']($value);
1163
	}
1164
	$replaces['{$default_reserved_names}'] = strtr($replaces['{$default_reserved_names}'], array('\\\\n' => '\\n'));
1165
1166
	// MySQL-specific stuff - storage engine and UTF8 handling
1167
	if (substr($db_type, 0, 5) == 'mysql')
1168
	{
1169
		// Just in case the query fails for some reason...
1170
		$engines = array();
1171
1172
		// Figure out storage engines - what do we have, etc.
1173
		$get_engines = $smcFunc['db_query']('', 'SHOW ENGINES', array());
1174
1175
		while ($row = $smcFunc['db_fetch_assoc']($get_engines))
1176
		{
1177
			if ($row['Support'] == 'YES' || $row['Support'] == 'DEFAULT')
1178
				$engines[] = $row['Engine'];
1179
		}
1180
1181
		// Done with this now
1182
		$smcFunc['db_free_result']($get_engines);
1183
1184
		// InnoDB is better, so use it if possible...
1185
		$has_innodb = in_array('InnoDB', $engines);
1186
		$replaces['{$engine}'] = $has_innodb ? 'InnoDB' : 'MyISAM';
1187
		$replaces['{$memory}'] = (!$has_innodb && in_array('MEMORY', $engines)) ? 'MEMORY' : $replaces['{$engine}'];
1188
1189
		// If the UTF-8 setting was enabled, add it to the table definitions.
1190
		if (!empty($databases[$db_type]['utf8_support']) && (!empty($databases[$db_type]['utf8_required']) || isset($_POST['utf8'])))
1191
		{
1192
			$replaces['{$engine}'] .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
1193
			$replaces['{$memory}'] .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
1194
		}
1195
1196
		// One last thing - if we don't have InnoDB, we can't do transactions...
1197
		if (!$has_innodb)
1198
		{
1199
			$replaces['START TRANSACTION;'] = '';
1200
			$replaces['COMMIT;'] = '';
1201
		}
1202
	}
1203
	else
1204
	{
1205
		$has_innodb = false;
1206
	}
1207
1208
	// Read in the SQL.  Turn this on and that off... internationalize... etc.
1209
	$type = ($db_type == 'mysqli' ? 'mysql' : $db_type);
1210
	$sql_lines = explode("\n", strtr(implode(' ', file(dirname(__FILE__) . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql')), $replaces));
1211
1212
	// Execute the SQL.
1213
	$current_statement = '';
1214
	$exists = array();
1215
	$incontext['failures'] = array();
1216
	$incontext['sql_results'] = array(
1217
		'tables' => 0,
1218
		'inserts' => 0,
1219
		'table_dups' => 0,
1220
		'insert_dups' => 0,
1221
	);
1222
	foreach ($sql_lines as $count => $line)
1223
	{
1224
		// No comments allowed!
1225
		if (substr(trim($line), 0, 1) != '#')
1226
			$current_statement .= "\n" . rtrim($line);
1227
1228
		// Is this the end of the query string?
1229
		if (empty($current_statement) || (preg_match('~;[\s]*$~s', $line) == 0 && $count != count($sql_lines)))
1230
			continue;
1231
1232
		// Does this table already exist?  If so, don't insert more data into it!
1233
		if (preg_match('~^\s*INSERT INTO ([^\s\n\r]+?)~', $current_statement, $match) != 0 && in_array($match[1], $exists))
1234
		{
1235
			preg_match_all('~\)[,;]~', $current_statement, $matches);
1236
			if (!empty($matches[0]))
1237
				$incontext['sql_results']['insert_dups'] += count($matches[0]);
1238
			else
1239
				$incontext['sql_results']['insert_dups']++;
1240
1241
			$current_statement = '';
1242
			continue;
1243
		}
1244
1245
		if ($smcFunc['db_query']('', $current_statement, array('security_override' => true, 'db_error_skip' => true), $db_connection) === false)
1246
		{
1247
			// Error 1050: Table already exists!
1248
			// @todo Needs to be made better!
1249
			if ((($db_type != 'mysql' && $db_type != 'mysqli') || mysqli_errno($db_connection) == 1050) && preg_match('~^\s*CREATE TABLE ([^\s\n\r]+?)~', $current_statement, $match) == 1)
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($db_type != 'mysql' && ...statement, $match) == 1, Probably Intended Meaning: $db_type != 'mysql' && $...tatement, $match) == 1)
Loading history...
1250
			{
1251
				$exists[] = $match[1];
1252
				$incontext['sql_results']['table_dups']++;
1253
			}
1254
			// Don't error on duplicate indexes (or duplicate operators in PostgreSQL.)
1255
			elseif (!preg_match('~^\s*CREATE( UNIQUE)? INDEX ([^\n\r]+?)~', $current_statement, $match) && !($db_type == 'postgresql' && preg_match('~^\s*CREATE OPERATOR (^\n\r]+?)~', $current_statement, $match)))
1256
			{
1257
				// MySQLi requires a connection object. It's optional with MySQL and Postgres
1258
				$incontext['failures'][$count] = $smcFunc['db_error']($db_connection);
1259
			}
1260
		}
1261
		else
1262
		{
1263
			if (preg_match('~^\s*CREATE TABLE ([^\s\n\r]+?)~', $current_statement, $match) == 1)
1264
				$incontext['sql_results']['tables']++;
1265
			elseif (preg_match('~^\s*INSERT INTO ([^\s\n\r]+?)~', $current_statement, $match) == 1)
1266
			{
1267
				preg_match_all('~\)[,;]~', $current_statement, $matches);
1268
				if (!empty($matches[0]))
1269
					$incontext['sql_results']['inserts'] += count($matches[0]);
1270
				else
1271
					$incontext['sql_results']['inserts']++;
1272
			}
1273
		}
1274
1275
		$current_statement = '';
1276
1277
		// Wait, wait, I'm still working here!
1278
		set_time_limit(60);
1279
	}
1280
1281
	// Sort out the context for the SQL.
1282
	foreach ($incontext['sql_results'] as $key => $number)
1283
	{
1284
		if ($number == 0)
1285
			unset($incontext['sql_results'][$key]);
1286
		else
1287
			$incontext['sql_results'][$key] = sprintf($txt['db_populate_' . $key], $number);
1288
	}
1289
1290
	// Make sure UTF will be used globally.
1291
	if ((!empty($databases[$db_type]['utf8_support']) && !empty($databases[$db_type]['utf8_required'])) || (empty($databases[$db_type]['utf8_required']) && !empty($databases[$db_type]['utf8_support']) && isset($_POST['utf8'])))
1292
		$newSettings[] = array('global_character_set', 'UTF-8');
1293
1294
	// Auto-detect local & global cookie settings
1295
	$url_parts = parse_url($boardurl);
1296
	if ($url_parts !== false)
1297
	{
1298
		unset($globalCookies, $globalCookiesDomain, $localCookies);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $globalCookiesDomain seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $globalCookies does not exist. Did you maybe mean $globalCookiesDomain?
Loading history...
Comprehensibility Best Practice introduced by
The variable $localCookies seems to be never defined.
Loading history...
1299
1300
		// Look for subdomain, if found, set globalCookie settings
1301
		// Don't bother looking if you have an ip address for host
1302
		if (!empty($url_parts['host']) && (filter_var($url_parts['host'], FILTER_VALIDATE_IP) === false))
1303
		{
1304
			// www isn't really a subdomain in this sense, so strip it out
1305
			$url_parts['host'] = str_ireplace('www.', '', $url_parts['host']);
1306
1307
			// If host only contains one period there can't be any subdomain
1308
			if (substr_count($url_parts['host'], '.') > 1)
0 ignored issues
show
Bug introduced by
It seems like $url_parts['host'] can also be of type array; however, parameter $haystack of substr_count() 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

1308
			if (substr_count(/** @scrutinizer ignore-type */ $url_parts['host'], '.') > 1)
Loading history...
1309
			{
1310
				$allowed_tld = download_ccSLD_list();
1311
				if (!empty($allowed_tld))
1312
				{
1313
					foreach($allowed_tld as $value)
1314
					{
1315
						$pattern = '/\.' . strtr($value, array('.' => '\.', '*' => '[^.]*?')) . '$/';
1316
						if (preg_match($pattern, $url_parts['host'], $matches, PREG_OFFSET_CAPTURE) === 1)
0 ignored issues
show
Bug introduced by
It seems like $url_parts['host'] can also be of type array; however, parameter $subject of preg_match() 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

1316
						if (preg_match($pattern, /** @scrutinizer ignore-type */ $url_parts['host'], $matches, PREG_OFFSET_CAPTURE) === 1)
Loading history...
1317
						{
1318
							$stripped_domain = substr($url_parts['host'], 0, $matches[0][1]);
0 ignored issues
show
Bug introduced by
It seems like $url_parts['host'] can also be of type array; however, parameter $string of substr() 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

1318
							$stripped_domain = substr(/** @scrutinizer ignore-type */ $url_parts['host'], 0, $matches[0][1]);
Loading history...
1319
							$tld = $matches[0][0];
1320
							break;
1321
						}
1322
					}
1323
1324
					$pos1 = strrpos($stripped_domain, '.');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $stripped_domain does not seem to be defined for all execution paths leading up to this point.
Loading history...
1325
					// If we find a period this indicates you have a subdomain
1326
					if ($pos1 !== false)
1327
					{
1328
						$globalCookies = '1';
1329
						$globalCookiesDomain = substr($stripped_domain, $pos1 + 1) . $tld;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tld does not seem to be defined for all execution paths leading up to this point.
Loading history...
1330
					}
1331
				}
1332
				else
1333
				{
1334
					// Fallback if ccSLD list can't be retrieved
1335
					$pos1 = strrpos($url_parts['host'], '.');
0 ignored issues
show
Bug introduced by
It seems like $url_parts['host'] can also be of type array; however, parameter $haystack of strrpos() 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

1335
					$pos1 = strrpos(/** @scrutinizer ignore-type */ $url_parts['host'], '.');
Loading history...
1336
					if ($pos1 !== false)
1337
					{
1338
						// 2nd period from the right indicates you have a subdomain
1339
						$pos2 = strrpos(substr($url_parts['host'], 0, $pos1 - 1), '.');
1340
						if ($pos2 !== false)
1341
						{
1342
							$globalCookies = '1';
1343
							$globalCookiesDomain = substr($url_parts['host'], $pos2 + 1);
1344
						}
1345
					}
1346
				}
1347
			}
1348
		}
1349
1350
		// Look for subfolder, if found, set localCookie
1351
		// Checking for len > 1 ensures you don't have just a slash...
1352
		if (!empty($url_parts['path']) && strlen($url_parts['path']) > 1)
1353
			$localCookies = '1';
1354
1355
		if (isset($globalCookies))
1356
			$newSettings[] = array('globalCookies', $globalCookies);
1357
		if (isset($globalCookiesDomain))
1358
			$newSettings[] = array('globalCookiesDomain', $globalCookiesDomain);
1359
		if (isset($localCookies))
1360
			$newSettings[] = array('localCookies', $localCookies);
1361
	}
1362
1363
	// Are we allowing stat collection?
1364
	if (!empty($_POST['stats']) && substr($boardurl, 0, 16) != 'http://localhost' && empty($modSettings['allow_sm_stats']) && empty($modSettings['enable_sm_stats']))
1365
	{
1366
		$incontext['allow_sm_stats'] = true;
1367
1368
		// Attempt to register the site etc.
1369
		$fp = @fsockopen('www.simplemachines.org', 443, $errno, $errstr);
1370
		if (!$fp)
0 ignored issues
show
introduced by
$fp is of type false|resource, thus it always evaluated to false.
Loading history...
1371
			$fp = @fsockopen('www.simplemachines.org', 80, $errno, $errstr);
1372
		if ($fp)
0 ignored issues
show
introduced by
$fp is of type false|resource, thus it always evaluated to false.
Loading history...
1373
		{
1374
			$out = 'GET /smf/stats/register_stats.php?site=' . base64_encode($boardurl) . ' HTTP/1.1' . "\r\n";
1375
			$out .= 'Host: www.simplemachines.org' . "\r\n";
1376
			$out .= 'Connection: Close' . "\r\n\r\n";
1377
			fwrite($fp, $out);
1378
1379
			$return_data = '';
1380
			while (!feof($fp))
1381
				$return_data .= fgets($fp, 128);
1382
1383
			fclose($fp);
1384
1385
			// Get the unique site ID.
1386
			preg_match('~SITE-ID:\s(\w{10})~', $return_data, $ID);
1387
1388
			if (!empty($ID[1]))
1389
				$smcFunc['db_insert']('replace',
1390
					$db_prefix . 'settings',
1391
					array('variable' => 'string', 'value' => 'string'),
1392
					array(
1393
						array('sm_stats_key', $ID[1]),
1394
						array('enable_sm_stats', 1),
1395
					),
1396
					array('variable')
1397
				);
1398
		}
1399
	}
1400
	// Don't remove stat collection unless we unchecked the box for real, not from the loop.
1401
	elseif (empty($_POST['stats']) && empty($incontext['allow_sm_stats']))
1402
		$smcFunc['db_query']('', '
1403
			DELETE FROM {db_prefix}settings
1404
			WHERE variable = {string:enable_sm_stats}',
1405
			array(
1406
				'enable_sm_stats' => 'enable_sm_stats',
1407
				'db_error_skip' => true,
1408
			)
1409
		);
1410
1411
	// Are we enabling SSL?
1412
	if (!empty($_POST['force_ssl']))
1413
		$newSettings[] = array('force_ssl', 1);
1414
1415
	// Setting a timezone is required.
1416
	if (!isset($modSettings['default_timezone']) && function_exists('date_default_timezone_set'))
1417
	{
1418
		// Get PHP's default timezone, if set
1419
		$ini_tz = ini_get('date.timezone');
1420
		if (!empty($ini_tz))
1421
			$timezone_id = $ini_tz;
1422
		else
1423
			$timezone_id = '';
1424
1425
		// If date.timezone is unset, invalid, or just plain weird, make a best guess
1426
		if (!in_array($timezone_id, timezone_identifiers_list()))
0 ignored issues
show
Bug introduced by
Are you sure the usage of timezone_identifiers_list() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
timezone_identifiers_list() of type void is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

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

1426
		if (!in_array($timezone_id, /** @scrutinizer ignore-type */ timezone_identifiers_list()))
Loading history...
1427
		{
1428
			$server_offset = @mktime(0, 0, 0, 1, 1, 1970);
1429
			$timezone_id = timezone_name_from_abbr('', $server_offset, 0);
0 ignored issues
show
Bug introduced by
It seems like $server_offset can also be of type false; however, parameter $utcOffset of timezone_name_from_abbr() does only seem to accept integer, 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

1429
			$timezone_id = timezone_name_from_abbr('', /** @scrutinizer ignore-type */ $server_offset, 0);
Loading history...
1430
		}
1431
1432
		if (date_default_timezone_set($timezone_id))
1433
			$newSettings[] = array('default_timezone', $timezone_id);
1434
	}
1435
1436
	if (!empty($newSettings))
1437
	{
1438
		$smcFunc['db_insert']('replace',
1439
			'{db_prefix}settings',
1440
			array('variable' => 'string-255', 'value' => 'string-65534'),
1441
			$newSettings,
1442
			array('variable')
1443
		);
1444
	}
1445
1446
	// Populate the smiley_files table.
1447
	// Can't just dump this data in the SQL file because we need to know the id for each smiley.
1448
	$smiley_filenames = array(
1449
		':)' => 'smiley',
1450
		';)' => 'wink',
1451
		':D' => 'cheesy',
1452
		';D' => 'grin',
1453
		'>:(' => 'angry',
1454
		':(' => 'sad',
1455
		':o' => 'shocked',
1456
		'8)' => 'cool',
1457
		'???' => 'huh',
1458
		'::)' => 'rolleyes',
1459
		':P' => 'tongue',
1460
		':-[' => 'embarrassed',
1461
		':-X' => 'lipsrsealed',
1462
		':-\\' => 'undecided',
1463
		':-*' => 'kiss',
1464
		':\'(' => 'cry',
1465
		'>:D' => 'evil',
1466
		'^-^' => 'azn',
1467
		'O0' => 'afro',
1468
		':))' => 'laugh',
1469
		'C:-)' => 'police',
1470
		'O:-)' => 'angel'
1471
	);
1472
	$smiley_set_extensions = array('fugue' => '.png', 'alienine' => '.png');
1473
1474
	$smiley_inserts = array();
1475
	$request = $smcFunc['db_query']('', '
1476
		SELECT id_smiley, code
1477
		FROM {db_prefix}smileys',
1478
		array()
1479
	);
1480
	while ($row = $smcFunc['db_fetch_assoc']($request))
1481
	{
1482
		foreach ($smiley_set_extensions as $set => $ext)
1483
			$smiley_inserts[] = array($row['id_smiley'], $set, $smiley_filenames[$row['code']] . $ext);
1484
	}
1485
	$smcFunc['db_free_result']($request);
1486
1487
	$smcFunc['db_insert']('ignore',
1488
		'{db_prefix}smiley_files',
1489
		array('id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48'),
1490
		$smiley_inserts,
1491
		array('id_smiley', 'smiley_set')
1492
	);
1493
1494
	// Let's optimize those new tables, but not on InnoDB, ok?
1495
	if (!$has_innodb)
1496
	{
1497
		db_extend();
1498
		$tables = $smcFunc['db_list_tables']($db_name, $db_prefix . '%');
1499
		foreach ($tables as $table)
1500
		{
1501
			$smcFunc['db_optimize_table']($table) != -1 or $db_messed = true;
1502
1503
			if (!empty($db_messed))
1504
			{
1505
				$incontext['failures'][-1] = $smcFunc['db_error']();
1506
				break;
1507
			}
1508
		}
1509
	}
1510
1511
	// MySQL specific stuff
1512
	if (substr($db_type, 0, 5) != 'mysql')
1513
		return false;
1514
1515
	// Find database user privileges.
1516
	$privs = array();
1517
	$get_privs = $smcFunc['db_query']('', 'SHOW PRIVILEGES', array());
1518
	while ($row = $smcFunc['db_fetch_assoc']($get_privs))
1519
	{
1520
		if ($row['Privilege'] == 'Alter')
1521
			$privs[] = $row['Privilege'];
1522
	}
1523
	$smcFunc['db_free_result']($get_privs);
1524
1525
	// Check for the ALTER privilege.
1526
	if (!empty($databases[$db_type]['alter_support']) && !in_array('Alter', $privs))
1527
	{
1528
		$incontext['error'] = $txt['error_db_alter_priv'];
1529
		return false;
1530
	}
1531
1532
	if (!empty($exists))
1533
	{
1534
		$incontext['page_title'] = $txt['user_refresh_install'];
1535
		$incontext['was_refresh'] = true;
1536
	}
1537
1538
	return false;
1539
}
1540
1541
function download_ccSLD_list()
1542
{
1543
	// Download list of all known ccSLDs
1544
	$list = explode("\n", file_get_contents('https://publicsuffix.org/list/public_suffix_list.dat'));
1545
	$allowed_tld = array();
1546
	foreach ($list as $value)
1547
		// Remove all empty lines and comments
1548
		if (!empty($value) && substr($value, 0, 2) != '//')
1549
			$allowed_tld[] = $value;
1550
	return array_reverse($allowed_tld);
1551
}
1552
1553
// Ask for the administrator login information.
1554
function AdminAccount()
1555
{
1556
	global $txt, $db_type, $smcFunc, $incontext, $db_prefix, $db_passwd, $sourcedir, $db_character_set;
1557
1558
	$incontext['sub_template'] = 'admin_account';
1559
	$incontext['page_title'] = $txt['user_settings'];
1560
	$incontext['continue'] = 1;
1561
1562
	// Skipping?
1563
	if (!empty($_POST['skip']))
1564
		return true;
1565
1566
	// Need this to check whether we need the database password.
1567
	require(dirname(__FILE__) . '/Settings.php');
1568
	load_database();
1569
1570
	require_once($sourcedir . '/Subs-Auth.php');
1571
1572
	require_once($sourcedir . '/Subs.php');
1573
1574
	// Reload settings & set some global funcs
1575
	require_once($sourcedir . '/Load.php');
1576
	reloadSettings();
1577
1578
	// We need this to properly hash the password for Admin
1579
	$smcFunc['strtolower'] = $db_character_set != 'utf8' && $txt['lang_character_set'] != 'UTF-8' ? 'strtolower' : function($string)
1580
	{
1581
		global $sourcedir;
1582
		if (function_exists('mb_strtolower'))
1583
			return mb_strtolower($string, 'UTF-8');
1584
		require_once($sourcedir . '/Subs-Charset.php');
1585
		return utf8_strtolower($string);
1586
	};
1587
1588
	if (!isset($_POST['username']))
1589
		$_POST['username'] = '';
1590
	if (!isset($_POST['email']))
1591
		$_POST['email'] = '';
1592
	if (!isset($_POST['server_email']))
1593
		$_POST['server_email'] = '';
1594
1595
	$incontext['username'] = htmlspecialchars($_POST['username']);
1596
	$incontext['email'] = htmlspecialchars($_POST['email']);
1597
	$incontext['server_email'] = htmlspecialchars($_POST['server_email']);
1598
1599
	$incontext['require_db_confirm'] = empty($db_type);
1600
1601
	// Only allow skipping if we think they already have an account setup.
1602
	$request = $smcFunc['db_query']('', '
1603
		SELECT id_member
1604
		FROM {db_prefix}members
1605
		WHERE id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0
1606
		LIMIT 1',
1607
		array(
1608
			'db_error_skip' => true,
1609
			'admin_group' => 1,
1610
		)
1611
	);
1612
	if ($smcFunc['db_num_rows']($request) != 0)
1613
		$incontext['skip'] = 1;
1614
	$smcFunc['db_free_result']($request);
1615
1616
	// Trying to create an account?
1617
	if (isset($_POST['password1']) && !empty($_POST['contbutt']))
1618
	{
1619
		// Wrong password?
1620
		if ($incontext['require_db_confirm'] && $_POST['password3'] != $db_passwd)
1621
		{
1622
			$incontext['error'] = $txt['error_db_connect'];
1623
			return false;
1624
		}
1625
		// Not matching passwords?
1626
		if ($_POST['password1'] != $_POST['password2'])
1627
		{
1628
			$incontext['error'] = $txt['error_user_settings_again_match'];
1629
			return false;
1630
		}
1631
		// No password?
1632
		if (strlen($_POST['password1']) < 4)
1633
		{
1634
			$incontext['error'] = $txt['error_user_settings_no_password'];
1635
			return false;
1636
		}
1637
		if (!file_exists($sourcedir . '/Subs.php'))
1638
		{
1639
			$incontext['error'] = sprintf($txt['error_sourcefile_missing'], 'Subs.php');
1640
			return false;
1641
		}
1642
1643
		// Update the webmaster's email?
1644
		if (!empty($_POST['server_email']) && (empty($webmaster_email) || $webmaster_email == '[email protected]'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $webmaster_email seems to never exist and therefore empty should always be true.
Loading history...
1645
			installer_updateSettingsFile(array('webmaster_email' => $_POST['server_email']));
1646
1647
		// Work out whether we're going to have dodgy characters and remove them.
1648
		$invalid_characters = preg_match('~[<>&"\'=\\\]~', $_POST['username']) != 0;
1649
		$_POST['username'] = preg_replace('~[<>&"\'=\\\]~', '', $_POST['username']);
1650
1651
		$result = $smcFunc['db_query']('', '
1652
			SELECT id_member, password_salt
1653
			FROM {db_prefix}members
1654
			WHERE member_name = {string:username} OR email_address = {string:email}
1655
			LIMIT 1',
1656
			array(
1657
				'username' => $_POST['username'],
1658
				'email' => $_POST['email'],
1659
				'db_error_skip' => true,
1660
			)
1661
		);
1662
		if ($smcFunc['db_num_rows']($result) != 0)
1663
		{
1664
			list ($incontext['member_id'], $incontext['member_salt']) = $smcFunc['db_fetch_row']($result);
1665
			$smcFunc['db_free_result']($result);
1666
1667
			$incontext['account_existed'] = $txt['error_user_settings_taken'];
1668
		}
1669
		elseif ($_POST['username'] == '' || strlen($_POST['username']) > 25)
1670
		{
1671
			// Try the previous step again.
1672
			$incontext['error'] = $_POST['username'] == '' ? $txt['error_username_left_empty'] : $txt['error_username_too_long'];
1673
			return false;
1674
		}
1675
		elseif ($invalid_characters || $_POST['username'] == '_' || $_POST['username'] == '|' || strpos($_POST['username'], '[code') !== false || strpos($_POST['username'], '[/code') !== false)
1676
		{
1677
			// Try the previous step again.
1678
			$incontext['error'] = $txt['error_invalid_characters_username'];
1679
			return false;
1680
		}
1681
		elseif (empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) || strlen($_POST['email']) > 255)
1682
		{
1683
			// One step back, this time fill out a proper admin email address.
1684
			$incontext['error'] = sprintf($txt['error_valid_admin_email_needed'], $_POST['username']);
1685
			return false;
1686
		}
1687
		elseif (empty($_POST['server_email']) || !filter_var($_POST['server_email'], FILTER_VALIDATE_EMAIL) || strlen($_POST['server_email']) > 255)
1688
		{
1689
			// One step back, this time fill out a proper admin email address.
1690
			$incontext['error'] = $txt['error_valid_server_email_needed'];
1691
			return false;
1692
		}
1693
		elseif ($_POST['username'] != '')
1694
		{
1695
			if (!is_callable('random_int'))
1696
				require_once('Sources/random_compat/random.php');
1697
1698
			$incontext['member_salt'] = bin2hex(random_bytes(16));
1699
1700
			// Format the username properly.
1701
			$_POST['username'] = preg_replace('~[\t\n\r\x0B\0\xA0]+~', ' ', $_POST['username']);
1702
			$ip = isset($_SERVER['REMOTE_ADDR']) ? substr($_SERVER['REMOTE_ADDR'], 0, 255) : '';
1703
1704
			$_POST['password1'] = hash_password($_POST['username'], $_POST['password1']);
1705
1706
			$incontext['member_id'] = $smcFunc['db_insert']('',
1707
				$db_prefix . 'members',
1708
				array(
1709
					'member_name' => 'string-25',
1710
					'real_name' => 'string-25',
1711
					'passwd' => 'string',
1712
					'email_address' => 'string',
1713
					'id_group' => 'int',
1714
					'posts' => 'int',
1715
					'date_registered' => 'int',
1716
					'password_salt' => 'string',
1717
					'lngfile' => 'string',
1718
					'personal_text' => 'string',
1719
					'avatar' => 'string',
1720
					'member_ip' => 'inet',
1721
					'member_ip2' => 'inet',
1722
					'buddy_list' => 'string',
1723
					'pm_ignore_list' => 'string',
1724
					'website_title' => 'string',
1725
					'website_url' => 'string',
1726
					'signature' => 'string',
1727
					'usertitle' => 'string',
1728
					'secret_question' => 'string',
1729
					'additional_groups' => 'string',
1730
					'ignore_boards' => 'string',
1731
				),
1732
				array(
1733
					$_POST['username'],
1734
					$_POST['username'],
1735
					$_POST['password1'],
1736
					$_POST['email'],
1737
					1,
1738
					0,
1739
					time(),
1740
					$incontext['member_salt'],
1741
					'',
1742
					'',
1743
					'',
1744
					$ip,
1745
					$ip,
1746
					'',
1747
					'',
1748
					'',
1749
					'',
1750
					'',
1751
					'',
1752
					'',
1753
					'',
1754
					'',
1755
				),
1756
				array('id_member'),
1757
				1
1758
			);
1759
		}
1760
1761
		// If we're here we're good.
1762
		return true;
1763
	}
1764
1765
	return false;
1766
}
1767
1768
// Final step, clean up and a complete message!
1769
function DeleteInstall()
1770
{
1771
	global $smcFunc, $db_character_set, $context, $txt, $incontext;
1772
	global $databases, $sourcedir, $modSettings, $user_info, $db_type, $boardurl;
1773
	global $auth_secret, $cookiename;
1774
1775
	$incontext['page_title'] = $txt['congratulations'];
1776
	$incontext['sub_template'] = 'delete_install';
1777
	$incontext['continue'] = 0;
1778
1779
	require(dirname(__FILE__) . '/Settings.php');
1780
	load_database();
1781
1782
	chdir(dirname(__FILE__));
1783
1784
	require_once($sourcedir . '/Errors.php');
1785
	require_once($sourcedir . '/Logging.php');
1786
	require_once($sourcedir . '/Subs.php');
1787
	require_once($sourcedir . '/Load.php');
1788
	require_once($sourcedir . '/Security.php');
1789
	require_once($sourcedir . '/Subs-Auth.php');
1790
1791
	// Reload settings & set some global funcs
1792
	reloadSettings();
1793
1794
	// Bring a warning over.
1795
	if (!empty($incontext['account_existed']))
1796
		$incontext['warning'] = $incontext['account_existed'];
1797
1798
	if (!empty($db_character_set) && !empty($databases[$db_type]['utf8_support']))
1799
		$smcFunc['db_query']('', '
1800
			SET NAMES {string:db_character_set}',
1801
			array(
1802
				'db_character_set' => $db_character_set,
1803
				'db_error_skip' => true,
1804
			)
1805
		);
1806
1807
	// As track stats is by default enabled let's add some activity.
1808
	$smcFunc['db_insert']('ignore',
1809
		'{db_prefix}log_activity',
1810
		array('date' => 'date', 'topics' => 'int', 'posts' => 'int', 'registers' => 'int'),
1811
		array(strftime('%Y-%m-%d', time()), 1, 1, (!empty($incontext['member_id']) ? 1 : 0)),
1812
		array('date')
1813
	);
1814
1815
	// We're going to want our lovely $modSettings now.
1816
	$request = $smcFunc['db_query']('', '
1817
		SELECT variable, value
1818
		FROM {db_prefix}settings',
1819
		array(
1820
			'db_error_skip' => true,
1821
		)
1822
	);
1823
	// Only proceed if we can load the data.
1824
	if ($request)
1825
	{
1826
		while ($row = $smcFunc['db_fetch_row']($request))
1827
			$modSettings[$row[0]] = $row[1];
1828
		$smcFunc['db_free_result']($request);
1829
	}
1830
1831
	// Automatically log them in ;)
1832
	if (isset($incontext['member_id']) && isset($incontext['member_salt']))
1833
		setLoginCookie(3153600 * 60, $incontext['member_id'], hash_salt($_POST['password1'], $incontext['member_salt']));
1834
1835
	$result = $smcFunc['db_query']('', '
1836
		SELECT value
1837
		FROM {db_prefix}settings
1838
		WHERE variable = {string:db_sessions}',
1839
		array(
1840
			'db_sessions' => 'databaseSession_enable',
1841
			'db_error_skip' => true,
1842
		)
1843
	);
1844
	if ($smcFunc['db_num_rows']($result) != 0)
1845
		list ($db_sessions) = $smcFunc['db_fetch_row']($result);
1846
	$smcFunc['db_free_result']($result);
1847
1848
	if (empty($db_sessions))
1849
		$_SESSION['admin_time'] = time();
1850
	else
1851
	{
1852
		$_SERVER['HTTP_USER_AGENT'] = substr($_SERVER['HTTP_USER_AGENT'], 0, 211);
1853
1854
		$smcFunc['db_insert']('replace',
1855
			'{db_prefix}sessions',
1856
			array(
1857
				'session_id' => 'string', 'last_update' => 'int', 'data' => 'string',
1858
			),
1859
			array(
1860
				session_id(), time(), 'USER_AGENT|s:' . strlen($_SERVER['HTTP_USER_AGENT']) . ':"' . $_SERVER['HTTP_USER_AGENT'] . '";admin_time|i:' . time() . ';',
1861
			),
1862
			array('session_id')
1863
		);
1864
	}
1865
1866
	updateStats('member');
1867
	updateStats('message');
1868
	updateStats('topic');
1869
1870
	// This function is needed to do the updateStats('subject') call.
1871
	$smcFunc['strtolower'] = $db_character_set != 'utf8' && $txt['lang_character_set'] != 'UTF-8' ? 'strtolower' : function($string)
1872
	{
1873
		global $sourcedir;
1874
		if (function_exists('mb_strtolower'))
1875
			return mb_strtolower($string, 'UTF-8');
1876
		require_once($sourcedir . '/Subs-Charset.php');
1877
		return utf8_strtolower($string);
1878
	};
1879
1880
	$request = $smcFunc['db_query']('', '
1881
		SELECT id_msg
1882
		FROM {db_prefix}messages
1883
		WHERE id_msg = 1
1884
			AND modified_time = 0
1885
		LIMIT 1',
1886
		array(
1887
			'db_error_skip' => true,
1888
		)
1889
	);
1890
	$context['utf8'] = $db_character_set === 'utf8' || $txt['lang_character_set'] === 'UTF-8';
1891
	if ($smcFunc['db_num_rows']($request) > 0)
1892
		updateStats('subject', 1, htmlspecialchars($txt['default_topic_subject']));
1893
	$smcFunc['db_free_result']($request);
1894
1895
	// Now is the perfect time to fetch the SM files.
1896
	require_once($sourcedir . '/ScheduledTasks.php');
1897
	// Sanity check that they loaded earlier!
1898
	if (isset($modSettings['recycle_board']))
1899
	{
1900
		scheduled_fetchSMfiles(); // Now go get those files!
1901
1902
		// We've just installed!
1903
		$user_info['ip'] = $_SERVER['REMOTE_ADDR'];
1904
		$user_info['id'] = isset($incontext['member_id']) ? $incontext['member_id'] : 0;
1905
		logAction('install', array('version' => SMF_FULL_VERSION), 'admin');
1906
	}
1907
1908
	// Disable the legacy BBC by default for new installs
1909
	updateSettings(array(
1910
		'disabledBBC' => implode(',', $context['legacy_bbc']),
1911
	));
1912
1913
	// Some final context for the template.
1914
	$incontext['dir_still_writable'] = is_writable(dirname(__FILE__)) && substr(__FILE__, 1, 2) != ':\\';
1915
	$incontext['probably_delete_install'] = isset($_SESSION['installer_temp_ftp']) || is_writable(dirname(__FILE__)) || is_writable(__FILE__);
1916
1917
	// Update hash's cost to an appropriate setting
1918
	updateSettings(array(
1919
		'bcrypt_hash_cost' => hash_benchmark(),
1920
	));
1921
1922
	return false;
1923
}
1924
1925
function installer_updateSettingsFile($vars, $rebuild = false)
1926
{
1927
	global $sourcedir, $context, $db_character_set, $txt;
1928
1929
	$context['utf8'] = (isset($vars['db_character_set']) && $vars['db_character_set'] === 'utf8') || (!empty($db_character_set) && $db_character_set === 'utf8') || (!empty($txt) && $txt['lang_character_set'] === 'UTF-8');
1930
1931
	if (empty($sourcedir))
1932
	{
1933
		if (file_exists(dirname(__FILE__) . '/Sources') && is_dir(dirname(__FILE__) . '/Sources'))
1934
			$sourcedir = dirname(__FILE__) . '/Sources';
1935
		else
1936
			return false;
1937
	}
1938
1939
	if (!is_writeable(dirname(__FILE__) . '/Settings.php'))
1940
	{
1941
		@chmod(dirname(__FILE__) . '/Settings.php', 0777);
1942
1943
		if (!is_writeable(dirname(__FILE__) . '/Settings.php'))
1944
			return false;
1945
	}
1946
1947
	require_once($sourcedir . '/Subs.php');
1948
	require_once($sourcedir . '/Subs-Admin.php');
1949
1950
	return updateSettingsFile($vars, false, $rebuild);
1951
}
1952
1953
// Create an .htaccess file to prevent mod_security. SMF has filtering built-in.
1954
function fixModSecurity()
1955
{
1956
	$htaccess_addition = '
1957
<IfModule mod_security.c>
1958
	# Turn off mod_security filtering.  SMF is a big boy, it doesn\'t need its hands held.
1959
	SecFilterEngine Off
1960
1961
	# The below probably isn\'t needed, but better safe than sorry.
1962
	SecFilterScanPOST Off
1963
</IfModule>';
1964
1965
	if (!function_exists('apache_get_modules') || !in_array('mod_security', apache_get_modules()))
1966
		return true;
1967
	elseif (file_exists(dirname(__FILE__) . '/.htaccess') && is_writable(dirname(__FILE__) . '/.htaccess'))
1968
	{
1969
		$current_htaccess = implode('', file(dirname(__FILE__) . '/.htaccess'));
1970
1971
		// Only change something if mod_security hasn't been addressed yet.
1972
		if (strpos($current_htaccess, '<IfModule mod_security.c>') === false)
1973
		{
1974
			if ($ht_handle = fopen(dirname(__FILE__) . '/.htaccess', 'a'))
1975
			{
1976
				fwrite($ht_handle, $htaccess_addition);
1977
				fclose($ht_handle);
1978
				return true;
1979
			}
1980
			else
1981
				return false;
1982
		}
1983
		else
1984
			return true;
1985
	}
1986
	elseif (file_exists(dirname(__FILE__) . '/.htaccess'))
1987
		return strpos(implode('', file(dirname(__FILE__) . '/.htaccess')), '<IfModule mod_security.c>') !== false;
1988
	elseif (is_writable(dirname(__FILE__)))
1989
	{
1990
		if ($ht_handle = fopen(dirname(__FILE__) . '/.htaccess', 'w'))
1991
		{
1992
			fwrite($ht_handle, $htaccess_addition);
1993
			fclose($ht_handle);
1994
			return true;
1995
		}
1996
		else
1997
			return false;
1998
	}
1999
	else
2000
		return false;
2001
}
2002
2003
function template_install_above()
2004
{
2005
	global $incontext, $txt, $installurl;
2006
2007
	echo '<!DOCTYPE html>
2008
<html', $txt['lang_rtl'] == true ? ' dir="rtl"' : '', '>
2009
<head>
2010
	<meta charset="', isset($txt['lang_character_set']) ? $txt['lang_character_set'] : 'UTF-8', '">
2011
	<meta name="robots" content="noindex">
2012
	<title>', $txt['smf_installer'], '</title>
2013
	<link rel="stylesheet" href="Themes/default/css/index.css">
2014
	<link rel="stylesheet" href="Themes/default/css/install.css">
2015
	', $txt['lang_rtl'] == true ? '<link rel="stylesheet" href="Themes/default/css/rtl.css">' : '', '
2016
2017
	<script src="Themes/default/scripts/jquery-' . JQUERY_VERSION . '.min.js"></script>
2018
	<script src="Themes/default/scripts/script.js"></script>
2019
</head>
2020
<body>
2021
	<div id="footerfix">
2022
	<div id="header">
2023
		<h1 class="forumtitle">', $txt['smf_installer'], '</h1>
2024
		<img id="smflogo" src="Themes/default/images/smflogo.svg" alt="Simple Machines Forum" title="Simple Machines Forum">
2025
	</div>
2026
	<div id="wrapper">';
2027
2028
	// Have we got a language drop down - if so do it on the first step only.
2029
	if (!empty($incontext['detected_languages']) && count($incontext['detected_languages']) > 1 && $incontext['current_step'] == 0)
2030
	{
2031
		echo '
2032
		<div id="upper_section">
2033
			<div id="inner_section">
2034
				<div id="inner_wrap">
2035
					<div class="news">
2036
						<form action="', $installurl, '" method="get">
2037
							<label for="installer_language">', $txt['installer_language'], ':</label>
2038
							<select id="installer_language" name="lang_file" onchange="location.href = \'', $installurl, '?lang_file=\' + this.options[this.selectedIndex].value;">';
2039
2040
		foreach ($incontext['detected_languages'] as $lang => $name)
2041
			echo '
2042
								<option', isset($_SESSION['installer_temp_lang']) && $_SESSION['installer_temp_lang'] == $lang ? ' selected' : '', ' value="', $lang, '">', $name, '</option>';
2043
2044
		echo '
2045
							</select>
2046
							<noscript><input type="submit" value="', $txt['installer_language_set'], '" class="button"></noscript>
2047
						</form>
2048
					</div><!-- .news -->
2049
					<hr class="clear">
2050
				</div><!-- #inner_wrap -->
2051
			</div><!-- #inner_section -->
2052
		</div><!-- #upper_section -->';
2053
	}
2054
2055
	echo '
2056
		<div id="content_section">
2057
			<div id="main_content_section">
2058
				<div id="main_steps">
2059
					<h2>', $txt['upgrade_progress'], '</h2>
2060
					<ul class="steps_list">';
2061
2062
	foreach ($incontext['steps'] as $num => $step)
2063
		echo '
2064
						<li', $num == $incontext['current_step'] ? ' class="stepcurrent"' : '', '>
2065
							', $txt['upgrade_step'], ' ', $step[0], ': ', $step[1], '
2066
						</li>';
2067
2068
	echo '
2069
					</ul>
2070
				</div>
2071
				<div id="install_progress">
2072
					<div id="progress_bar" class="progress_bar progress_green">
2073
						<h3>'. $txt['upgrade_overall_progress'], '</h3>
2074
						<span id="overall_text">', $incontext['overall_percent'], '%</span>
2075
						<div id="overall_progress" class="bar" style="width: ', $incontext['overall_percent'], '%;"></div>
2076
					</div>
2077
				</div>
2078
				<div id="main_screen" class="clear">
2079
					<h2>', $incontext['page_title'], '</h2>
2080
					<div class="panel">';
2081
}
2082
2083
function template_install_below()
2084
{
2085
	global $incontext, $txt;
2086
2087
	if (!empty($incontext['continue']) || !empty($incontext['skip']))
2088
	{
2089
		echo '
2090
							<div class="floatright">';
2091
2092
		if (!empty($incontext['continue']))
2093
			echo '
2094
								<input type="submit" id="contbutt" name="contbutt" value="', $txt['upgrade_continue'], '" onclick="return submitThisOnce(this);" class="button">';
2095
		if (!empty($incontext['skip']))
2096
			echo '
2097
								<input type="submit" id="skip" name="skip" value="', $txt['upgrade_skip'], '" onclick="return submitThisOnce(this);" class="button">';
2098
		echo '
2099
							</div>';
2100
	}
2101
2102
	// Show the closing form tag and other data only if not in the last step
2103
	if (count($incontext['steps']) - 1 !== (int) $incontext['current_step'])
2104
		echo '
2105
						</form>';
2106
2107
	echo '
2108
					</div><!-- .panel -->
2109
				</div><!-- #main_screen -->
2110
			</div><!-- #main_content_section -->
2111
		</div><!-- #content_section -->
2112
	</div><!-- #wrapper -->
2113
	</div><!-- #footerfix -->
2114
	<div id="footer">
2115
		<ul>
2116
			<li class="copyright"><a href="https://www.simplemachines.org/" title="Simple Machines Forum" target="_blank" rel="noopener">' . SMF_FULL_VERSION . ' &copy; ' . SMF_SOFTWARE_YEAR . ', Simple Machines</a></li>
2117
		</ul>
2118
	</div>
2119
</body>
2120
</html>';
2121
}
2122
2123
// Welcome them to the wonderful world of SMF!
2124
function template_welcome_message()
2125
{
2126
	global $incontext, $txt;
2127
2128
	echo '
2129
	<script src="https://www.simplemachines.org/smf/current-version.js?version=' . urlencode(SMF_VERSION) . '"></script>
2130
	<form action="', $incontext['form_url'], '" method="post">
2131
		<p>', sprintf($txt['install_welcome_desc'], SMF_VERSION), '</p>
2132
		<div id="version_warning" class="noticebox hidden">
2133
			<h3>', $txt['error_warning_notice'], '</h3>
2134
			', sprintf($txt['error_script_outdated'], '<em id="smfVersion" style="white-space: nowrap;">??</em>', '<em id="yourVersion" style="white-space: nowrap;">' . SMF_VERSION . '</em>'), '
2135
		</div>';
2136
2137
	// Show the warnings, or not.
2138
	if (template_warning_divs())
2139
		echo '
2140
		<h3>', $txt['install_all_lovely'], '</h3>';
2141
2142
	// Say we want the continue button!
2143
	if (empty($incontext['error']))
2144
		$incontext['continue'] = 1;
2145
2146
	// For the latest version stuff.
2147
	echo '
2148
		<script>
2149
			// Latest version?
2150
			function smfCurrentVersion()
2151
			{
2152
				var smfVer, yourVer;
2153
2154
				if (!(\'smfVersion\' in window))
2155
					return;
2156
2157
				window.smfVersion = window.smfVersion.replace(/SMF\s?/g, \'\');
2158
2159
				smfVer = document.getElementById("smfVersion");
2160
				yourVer = document.getElementById("yourVersion");
2161
2162
				setInnerHTML(smfVer, window.smfVersion);
2163
2164
				var currentVersion = getInnerHTML(yourVer);
2165
				if (currentVersion < window.smfVersion)
2166
					document.getElementById(\'version_warning\').classList.remove(\'hidden\');
2167
			}
2168
			addLoadEvent(smfCurrentVersion);
2169
		</script>';
2170
}
2171
2172
// A shortcut for any warning stuff.
2173
function template_warning_divs()
2174
{
2175
	global $txt, $incontext;
2176
2177
	// Errors are very serious..
2178
	if (!empty($incontext['error']))
2179
		echo '
2180
		<div class="errorbox">
2181
			<h3>', $txt['upgrade_critical_error'], '</h3>
2182
			', $incontext['error'], '
2183
		</div>';
2184
	// A warning message?
2185
	elseif (!empty($incontext['warning']))
2186
		echo '
2187
		<div class="errorbox">
2188
			<h3>', $txt['upgrade_warning'], '</h3>
2189
			', $incontext['warning'], '
2190
		</div>';
2191
2192
	return empty($incontext['error']) && empty($incontext['warning']);
2193
}
2194
2195
function template_chmod_files()
2196
{
2197
	global $txt, $incontext;
2198
2199
	echo '
2200
		<p>', $txt['ftp_setup_why_info'], '</p>
2201
		<ul class="error_content">
2202
			<li>', implode('</li>
2203
			<li>', $incontext['failed_files']), '</li>
2204
		</ul>';
2205
2206
	if (isset($incontext['systemos'], $incontext['detected_path']) && $incontext['systemos'] == 'linux')
2207
		echo '
2208
		<hr>
2209
		<p>', $txt['chmod_linux_info'], '</p>
2210
		<samp># chmod a+w ', implode(' ' . $incontext['detected_path'] . '/', $incontext['failed_files']), '</samp>';
2211
2212
	// This is serious!
2213
	if (!template_warning_divs())
2214
		return;
2215
2216
	echo '
2217
		<hr>
2218
		<p>', $txt['ftp_setup_info'], '</p>';
2219
2220
	if (!empty($incontext['ftp_errors']))
2221
		echo '
2222
		<div class="error_message">
2223
			', $txt['error_ftp_no_connect'], '<br><br>
2224
			<code>', implode('<br>', $incontext['ftp_errors']), '</code>
2225
		</div>';
2226
2227
	echo '
2228
		<form action="', $incontext['form_url'], '" method="post">
2229
			<dl class="settings">
2230
				<dt>
2231
					<label for="ftp_server">', $txt['ftp_server'], ':</label>
2232
				</dt>
2233
				<dd>
2234
					<div class="floatright">
2235
						<label for="ftp_port" class="textbox"><strong>', $txt['ftp_port'], ':&nbsp;</strong></label>
2236
						<input type="text" size="3" name="ftp_port" id="ftp_port" value="', $incontext['ftp']['port'], '">
2237
					</div>
2238
					<input type="text" size="30" name="ftp_server" id="ftp_server" value="', $incontext['ftp']['server'], '">
2239
					<div class="smalltext block">', $txt['ftp_server_info'], '</div>
2240
				</dd>
2241
				<dt>
2242
					<label for="ftp_username">', $txt['ftp_username'], ':</label>
2243
				</dt>
2244
				<dd>
2245
					<input type="text" size="30" name="ftp_username" id="ftp_username" value="', $incontext['ftp']['username'], '">
2246
					<div class="smalltext block">', $txt['ftp_username_info'], '</div>
2247
				</dd>
2248
				<dt>
2249
					<label for="ftp_password">', $txt['ftp_password'], ':</label>
2250
				</dt>
2251
				<dd>
2252
					<input type="password" size="30" name="ftp_password" id="ftp_password">
2253
					<div class="smalltext block">', $txt['ftp_password_info'], '</div>
2254
				</dd>
2255
				<dt>
2256
					<label for="ftp_path">', $txt['ftp_path'], ':</label>
2257
				</dt>
2258
				<dd>
2259
					<input type="text" size="30" name="ftp_path" id="ftp_path" value="', $incontext['ftp']['path'], '">
2260
					<div class="smalltext block">', $incontext['ftp']['path_msg'], '</div>
2261
				</dd>
2262
			</dl>
2263
			<div class="righttext buttons">
2264
				<input type="submit" value="', $txt['ftp_connect'], '" onclick="return submitThisOnce(this);" class="button">
2265
			</div>
2266
		</form>
2267
		<a href="', $incontext['form_url'], '">', $txt['error_message_click'], '</a> ', $txt['ftp_setup_again'];
2268
}
2269
2270
// Get the database settings prepared.
2271
function template_database_settings()
2272
{
2273
	global $incontext, $txt;
2274
2275
	echo '
2276
	<form action="', $incontext['form_url'], '" method="post">
2277
		<p>', $txt['db_settings_info'], '</p>';
2278
2279
	template_warning_divs();
2280
2281
	echo '
2282
		<dl class="settings">';
2283
2284
	// More than one database type?
2285
	if (count($incontext['supported_databases']) > 1)
2286
	{
2287
		echo '
2288
			<dt>
2289
				<label for="db_type_input">', $txt['db_settings_type'], ':</label>
2290
			</dt>
2291
			<dd>
2292
				<select name="db_type" id="db_type_input" onchange="toggleDBInput();">';
2293
2294
		foreach ($incontext['supported_databases'] as $key => $db)
2295
			echo '
2296
					<option value="', $key, '"', isset($_POST['db_type']) && $_POST['db_type'] == $key ? ' selected' : '', '>', $db['name'], '</option>';
2297
2298
		echo '
2299
				</select>
2300
				<div class="smalltext">', $txt['db_settings_type_info'], '</div>
2301
			</dd>';
2302
	}
2303
	else
2304
	{
2305
		echo '
2306
			<dd>
2307
				<input type="hidden" name="db_type" value="', $incontext['db']['type'], '">
2308
			</dd>';
2309
	}
2310
2311
	echo '
2312
			<dt>
2313
				<label for="db_server_input">', $txt['db_settings_server'], ':</label>
2314
			</dt>
2315
			<dd>
2316
				<input type="text" name="db_server" id="db_server_input" value="', $incontext['db']['server'], '" size="30">
2317
				<div class="smalltext">', $txt['db_settings_server_info'], '</div>
2318
			</dd>
2319
			<dt>
2320
				<label for="db_port_input">', $txt['db_settings_port'], ':</label>
2321
			</dt>
2322
			<dd>
2323
				<input type="text" name="db_port" id="db_port_input" value="', $incontext['db']['port'], '">
2324
				<div class="smalltext">', $txt['db_settings_port_info'], '</div>
2325
			</dd>
2326
			<dt>
2327
				<label for="db_user_input">', $txt['db_settings_username'], ':</label>
2328
			</dt>
2329
			<dd>
2330
				<input type="text" name="db_user" id="db_user_input" value="', $incontext['db']['user'], '" size="30">
2331
				<div class="smalltext">', $txt['db_settings_username_info'], '</div>
2332
			</dd>
2333
			<dt>
2334
				<label for="db_passwd_input">', $txt['db_settings_password'], ':</label>
2335
			</dt>
2336
			<dd>
2337
				<input type="password" name="db_passwd" id="db_passwd_input" value="', $incontext['db']['pass'], '" size="30">
2338
				<div class="smalltext">', $txt['db_settings_password_info'], '</div>
2339
			</dd>
2340
			<dt>
2341
				<label for="db_name_input">', $txt['db_settings_database'], ':</label>
2342
			</dt>
2343
			<dd>
2344
				<input type="text" name="db_name" id="db_name_input" value="', empty($incontext['db']['name']) ? 'smf' : $incontext['db']['name'], '" size="30">
2345
				<div class="smalltext">
2346
					', $txt['db_settings_database_info'], '
2347
					<span id="db_name_info_warning">', $txt['db_settings_database_info_note'], '</span>
2348
				</div>
2349
			</dd>
2350
			<dt>
2351
				<label for="db_prefix_input">', $txt['db_settings_prefix'], ':</label>
2352
			</dt>
2353
			<dd>
2354
				<input type="text" name="db_prefix" id="db_prefix_input" value="', $incontext['db']['prefix'], '" size="30">
2355
				<div class="smalltext">', $txt['db_settings_prefix_info'], '</div>
2356
			</dd>
2357
		</dl>';
2358
2359
	// Toggles a warning related to db names in PostgreSQL
2360
	echo '
2361
		<script>
2362
			function toggleDBInput()
2363
			{
2364
				if (document.getElementById(\'db_type_input\').value == \'postgresql\')
2365
					document.getElementById(\'db_name_info_warning\').classList.add(\'hidden\');
2366
				else
2367
					document.getElementById(\'db_name_info_warning\').classList.remove(\'hidden\');
2368
			}
2369
			toggleDBInput();
2370
		</script>';
2371
}
2372
2373
// Stick in their forum settings.
2374
function template_forum_settings()
2375
{
2376
	global $incontext, $txt;
2377
2378
	echo '
2379
	<form action="', $incontext['form_url'], '" method="post">
2380
		<h3>', $txt['install_settings_info'], '</h3>';
2381
2382
	template_warning_divs();
2383
2384
	echo '
2385
		<dl class="settings">
2386
			<dt>
2387
				<label for="mbname_input">', $txt['install_settings_name'], ':</label>
2388
			</dt>
2389
			<dd>
2390
				<input type="text" name="mbname" id="mbname_input" value="', $txt['install_settings_name_default'], '" size="65">
2391
				<div class="smalltext">', $txt['install_settings_name_info'], '</div>
2392
			</dd>
2393
			<dt>
2394
				<label for="boardurl_input">', $txt['install_settings_url'], ':</label>
2395
			</dt>
2396
			<dd>
2397
				<input type="text" name="boardurl" id="boardurl_input" value="', $incontext['detected_url'], '" size="65">
2398
				<div class="smalltext">', $txt['install_settings_url_info'], '</div>
2399
			</dd>
2400
			<dt>
2401
				<label for="reg_mode">', $txt['install_settings_reg_mode'], ':</label>
2402
			</dt>
2403
			<dd>
2404
				<select name="reg_mode" id="reg_mode">
2405
					<optgroup label="', $txt['install_settings_reg_modes'], ':">
2406
						<option value="0" selected>', $txt['install_settings_reg_immediate'], '</option>
2407
						<option value="1">', $txt['install_settings_reg_email'], '</option>
2408
						<option value="2">', $txt['install_settings_reg_admin'], '</option>
2409
						<option value="3">', $txt['install_settings_reg_disabled'], '</option>
2410
					</optgroup>
2411
				</select>
2412
				<div class="smalltext">', $txt['install_settings_reg_mode_info'], '</div>
2413
			</dd>
2414
			<dt>', $txt['install_settings_compress'], ':</dt>
2415
			<dd>
2416
				<input type="checkbox" name="compress" id="compress_check" checked>
2417
				<label for="compress_check">', $txt['install_settings_compress_title'], '</label>
2418
				<div class="smalltext">', $txt['install_settings_compress_info'], '</div>
2419
			</dd>
2420
			<dt>', $txt['install_settings_dbsession'], ':</dt>
2421
			<dd>
2422
				<input type="checkbox" name="dbsession" id="dbsession_check" checked>
2423
				<label for="dbsession_check">', $txt['install_settings_dbsession_title'], '</label>
2424
				<div class="smalltext">', $incontext['test_dbsession'] ? $txt['install_settings_dbsession_info1'] : $txt['install_settings_dbsession_info2'], '</div>
2425
			</dd>
2426
			<dt>', $txt['install_settings_utf8'], ':</dt>
2427
			<dd>
2428
				<input type="checkbox" name="utf8" id="utf8_check"', $incontext['utf8_default'] ? ' checked' : '', '', $incontext['utf8_required'] ? ' disabled' : '', '>
2429
				<label for="utf8_check">', $txt['install_settings_utf8_title'], '</label>
2430
				<div class="smalltext">', $txt['install_settings_utf8_info'], '</div>
2431
			</dd>
2432
			<dt>', $txt['install_settings_stats'], ':</dt>
2433
			<dd>
2434
				<input type="checkbox" name="stats" id="stats_check" checked="checked">
2435
				<label for="stats_check">', $txt['install_settings_stats_title'], '</label>
2436
				<div class="smalltext">', $txt['install_settings_stats_info'], '</div>
2437
			</dd>
2438
			<dt>', $txt['force_ssl'], ':</dt>
2439
			<dd>
2440
				<input type="checkbox" name="force_ssl" id="force_ssl"', $incontext['ssl_chkbx_checked'] ? ' checked' : '',
2441
					$incontext['ssl_chkbx_protected'] ? ' disabled' : '', '>
2442
				<label for="force_ssl">', $txt['force_ssl_label'], '</label>
2443
				<div class="smalltext"><strong>', $txt['force_ssl_info'], '</strong></div>
2444
			</dd>
2445
		</dl>';
2446
}
2447
2448
// Show results of the database population.
2449
function template_populate_database()
2450
{
2451
	global $incontext, $txt;
2452
2453
	echo '
2454
	<form action="', $incontext['form_url'], '" method="post">
2455
		<p>', !empty($incontext['was_refresh']) ? $txt['user_refresh_install_desc'] : $txt['db_populate_info'], '</p>';
2456
2457
	if (!empty($incontext['sql_results']))
2458
	{
2459
		echo '
2460
		<ul>
2461
			<li>', implode('</li><li>', $incontext['sql_results']), '</li>
2462
		</ul>';
2463
	}
2464
2465
	if (!empty($incontext['failures']))
2466
	{
2467
		echo '
2468
		<div class="red">', $txt['error_db_queries'], '</div>
2469
		<ul>';
2470
2471
		foreach ($incontext['failures'] as $line => $fail)
2472
			echo '
2473
			<li><strong>', $txt['error_db_queries_line'], $line + 1, ':</strong> ', nl2br(htmlspecialchars($fail)), '</li>';
2474
2475
		echo '
2476
		</ul>';
2477
	}
2478
2479
	echo '
2480
		<p>', $txt['db_populate_info2'], '</p>';
2481
2482
	template_warning_divs();
2483
2484
	echo '
2485
		<input type="hidden" name="pop_done" value="1">';
2486
}
2487
2488
// Create the admin account.
2489
function template_admin_account()
2490
{
2491
	global $incontext, $txt;
2492
2493
	echo '
2494
	<form action="', $incontext['form_url'], '" method="post">
2495
		<p>', $txt['user_settings_info'], '</p>';
2496
2497
	template_warning_divs();
2498
2499
	echo '
2500
		<dl class="settings">
2501
			<dt>
2502
				<label for="username">', $txt['user_settings_username'], ':</label>
2503
			</dt>
2504
			<dd>
2505
				<input type="text" name="username" id="username" value="', $incontext['username'], '" size="40">
2506
				<div class="smalltext">', $txt['user_settings_username_info'], '</div>
2507
			</dd>
2508
			<dt>
2509
				<label for="password1">', $txt['user_settings_password'], ':</label>
2510
			</dt>
2511
			<dd>
2512
				<input type="password" name="password1" id="password1" size="40">
2513
				<div class="smalltext">', $txt['user_settings_password_info'], '</div>
2514
			</dd>
2515
			<dt>
2516
				<label for="password2">', $txt['user_settings_again'], ':</label>
2517
			</dt>
2518
			<dd>
2519
				<input type="password" name="password2" id="password2" size="40">
2520
				<div class="smalltext">', $txt['user_settings_again_info'], '</div>
2521
			</dd>
2522
			<dt>
2523
				<label for="email">', $txt['user_settings_admin_email'], ':</label>
2524
			</dt>
2525
			<dd>
2526
				<input type="email" name="email" id="email" value="', $incontext['email'], '" size="40">
2527
				<div class="smalltext">', $txt['user_settings_admin_email_info'], '</div>
2528
			</dd>
2529
			<dt>
2530
				<label for="server_email">', $txt['user_settings_server_email'], ':</label>
2531
			</dt>
2532
			<dd>
2533
				<input type="text" name="server_email" id="server_email" value="', $incontext['server_email'], '" size="40">
2534
				<div class="smalltext">', $txt['user_settings_server_email_info'], '</div>
2535
			</dd>
2536
		</dl>';
2537
2538
	if ($incontext['require_db_confirm'])
2539
		echo '
2540
		<h2>', $txt['user_settings_database'], '</h2>
2541
		<p>', $txt['user_settings_database_info'], '</p>
2542
2543
		<div class="lefttext">
2544
			<input type="password" name="password3" size="30">
2545
		</div>';
2546
}
2547
2548
// Tell them it's done, and to delete.
2549
function template_delete_install()
2550
{
2551
	global $incontext, $installurl, $txt, $boardurl;
2552
2553
	echo '
2554
		<p>', $txt['congratulations_help'], '</p>';
2555
2556
	template_warning_divs();
2557
2558
	// Install directory still writable?
2559
	if ($incontext['dir_still_writable'])
2560
		echo '
2561
		<p><em>', $txt['still_writable'], '</em></p>';
2562
2563
	// Don't show the box if it's like 99% sure it won't work :P.
2564
	if ($incontext['probably_delete_install'])
2565
		echo '
2566
		<label>
2567
			<input type="checkbox" id="delete_self" onclick="doTheDelete();">
2568
			<strong>', $txt['delete_installer'], !isset($_SESSION['installer_temp_ftp']) ? ' ' . $txt['delete_installer_maybe'] : '', '</strong>
2569
		</label>
2570
		<script>
2571
			function doTheDelete()
2572
			{
2573
				var theCheck = document.getElementById ? document.getElementById("delete_self") : document.all.delete_self;
2574
				var tempImage = new Image();
2575
2576
				tempImage.src = "', $installurl, '?delete=1&ts_" + (new Date().getTime());
2577
				tempImage.width = 0;
2578
				theCheck.disabled = true;
2579
			}
2580
		</script>';
2581
2582
	echo '
2583
		<p>', sprintf($txt['go_to_your_forum'], $boardurl . '/index.php'), '</p>
2584
		<br>
2585
		', $txt['good_luck'];
2586
}
2587
2588
?>