Issues (1014)

other/install.php (1 issue)

1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines https://www.simplemachines.org
8
 * @copyright 2022 Simple Machines and individual contributors
9
 * @license https://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1.2
12
 */
13
14
define('SMF_VERSION', '2.1.2');
15
define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION);
16
define('SMF_SOFTWARE_YEAR', '2022');
17
define('DB_SCRIPT_VERSION', '2-1');
18
define('SMF_INSTALLING', 1);
19
20
define('JQUERY_VERSION', '3.6.0');
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'] = '7.0.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
if (version_compare(PHP_VERSION, '8.0.0', '>='))
39
	require_once('Sources/Subs-Compat.php');
40
41
// Database info.
42
$databases = array(
43
	'mysql' => array(
44
		'name' => 'MySQL',
45
		'version' => '5.6.0',
46
		'version_check' => function() {
47
			global $db_connection;
48
			if (!function_exists('mysqli_fetch_row'))
49
				return false;
50
			return mysqli_fetch_row(mysqli_query($db_connection, 'SELECT VERSION();'))[0];
51
		},
52
		'supported' => function_exists('mysqli_connect'),
53
		'default_user' => 'mysql.default_user',
54
		'default_password' => 'mysql.default_password',
55
		'default_host' => 'mysql.default_host',
56
		'default_port' => 'mysql.default_port',
57
		'utf8_support' => function()
58
		{
59
			return true;
60
		},
61
		'utf8_version' => '5.0.22',
62
		'utf8_version_check' => function() {
63
			global $db_connection;
64
			return mysqli_get_server_info($db_connection);
65
		},
66
		'alter_support' => true,
67
		'validate_prefix' => function(&$value)
68
		{
69
			$value = preg_replace('~[^A-Za-z0-9_\$]~', '', $value);
70
			return true;
71
		},
72
	),
73
	'postgresql' => array(
74
		'name' => 'PostgreSQL',
75
		'version' => '9.6',
76
		'version_check' => function() {
77
			global $db_connection;
78
			$request = pg_query($db_connection, 'SELECT version()');
79
			list ($version) = pg_fetch_row($request);
80
			list($pgl, $version) = explode(' ', $version);
81
			return $version;
82
		},
83
		'supported' => function_exists('pg_connect'),
84
		'always_has_db' => true,
85
		'utf8_support' => function()
86
		{
87
			global $db_connection;
88
			$request = pg_query($db_connection, 'SHOW SERVER_ENCODING');
89
90
			list ($charcode) = pg_fetch_row($request);
91
92
			if ($charcode == 'UTF8')
93
				return true;
94
			else
95
				return false;
96
		},
97
		'utf8_version' => '8.0',
98
		'utf8_version_check' => function (){
99
			global $db_connection;
100
			$request = pg_query($db_connection, 'SELECT version()');
101
			list ($version) = pg_fetch_row($request);
102
			list($pgl, $version) = explode(' ', $version);
103
			return $version;
104
		},
105
		'validate_prefix' => function(&$value)
106
		{
107
			global $txt;
108
109
			$value = preg_replace('~[^A-Za-z0-9_\$]~', '', $value);
110
111
			// Is it reserved?
112
			if ($value == 'pg_')
113
				return $txt['error_db_prefix_reserved'];
114
115
			// Is the prefix numeric?
116
			if (preg_match('~^\d~', $value))
117
				return $txt['error_db_prefix_numeric'];
118
119
			return true;
120
		},
121
	),
122
);
123
124
global $txt;
125
126
// Initialize everything and load the language files.
127
initialize_inputs();
128
load_lang_file();
129
130
// This is what we are.
131
$installurl = $_SERVER['PHP_SELF'];
132
133
// All the steps in detail.
134
// Number,Name,Function,Progress Weight.
135
$incontext['steps'] = array(
136
	0 => array(1, $txt['install_step_welcome'], 'Welcome', 0),
137
	1 => array(2, $txt['install_step_writable'], 'CheckFilesWritable', 10),
138
	2 => array(3, $txt['install_step_databaseset'], 'DatabaseSettings', 15),
139
	3 => array(4, $txt['install_step_forum'], 'ForumSettings', 40),
140
	4 => array(5, $txt['install_step_databasechange'], 'DatabasePopulation', 15),
141
	5 => array(6, $txt['install_step_admin'], 'AdminAccount', 20),
142
	6 => array(7, $txt['install_step_delete'], 'DeleteInstall', 0),
143
);
144
145
// Default title...
146
$incontext['page_title'] = $txt['smf_installer'];
147
148
// What step are we on?
149
$incontext['current_step'] = isset($_GET['step']) ? (int) $_GET['step'] : 0;
150
151
// Loop through all the steps doing each one as required.
152
$incontext['overall_percent'] = 0;
153
154
foreach ($incontext['steps'] as $num => $step)
155
{
156
	if ($num >= $incontext['current_step'])
157
	{
158
		// The current weight of this step in terms of overall progress.
159
		$incontext['step_weight'] = $step[3];
160
		// Make sure we reset the skip button.
161
		$incontext['skip'] = false;
162
163
		// Call the step and if it returns false that means pause!
164
		if (function_exists($step[2]) && $step[2]() === false)
165
			break;
166
		elseif (function_exists($step[2]))
167
			$incontext['current_step']++;
168
169
		// No warnings pass on.
170
		$incontext['warning'] = '';
171
	}
172
	$incontext['overall_percent'] += $step[3];
173
}
174
175
// Actually do the template stuff.
176
installExit();
177
178
function initialize_inputs()
179
{
180
	global $databases;
181
182
	// Just so people using older versions of PHP aren't left in the cold.
183
	if (!isset($_SERVER['PHP_SELF']))
184
		$_SERVER['PHP_SELF'] = isset($GLOBALS['HTTP_SERVER_VARS']['PHP_SELF']) ? $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'] : 'install.php';
185
186
	// In pre-release versions, report all errors.
187
	if (strspn(SMF_VERSION, '1234567890.') !== strlen(SMF_VERSION))
188
		error_reporting(E_ALL);
189
	// Otherwise, report all errors except for deprecation notices.
190
	else
191
		error_reporting(E_ALL & ~E_DEPRECATED);
192
193
	// Fun.  Low PHP version...
194
	if (!isset($_GET))
195
	{
196
		$GLOBALS['_GET']['step'] = 0;
197
		return;
198
	}
199
200
	if (!isset($_GET['obgz']))
201
	{
202
		ob_start();
203
204
		if (ini_get('session.save_handler') == 'user')
205
			@ini_set('session.save_handler', 'files');
206
		if (function_exists('session_start'))
207
			@session_start();
208
	}
209
	else
210
	{
211
		ob_start('ob_gzhandler');
212
213
		if (ini_get('session.save_handler') == 'user')
214
			@ini_set('session.save_handler', 'files');
215
		session_start();
216
217
		if (!headers_sent())
218
			echo '<!DOCTYPE html>
219
<html>
220
	<head>
221
		<title>', htmlspecialchars($_GET['pass_string']), '</title>
222
	</head>
223
	<body style="background-color: #d4d4d4; margin-top: 16%; text-align: center; font-size: 16pt;">
224
		<strong>', htmlspecialchars($_GET['pass_string']), '</strong>
225
	</body>
226
</html>';
227
		exit;
228
	}
229
230
	// This is really quite simple; if ?delete is on the URL, delete the installer...
231
	if (isset($_GET['delete']))
232
	{
233
		if (isset($_SESSION['installer_temp_ftp']))
234
		{
235
			$ftp = new ftp_connection($_SESSION['installer_temp_ftp']['server'], $_SESSION['installer_temp_ftp']['port'], $_SESSION['installer_temp_ftp']['username'], $_SESSION['installer_temp_ftp']['password']);
236
			$ftp->chdir($_SESSION['installer_temp_ftp']['path']);
237
238
			$ftp->unlink('install.php');
239
240
			foreach ($databases as $key => $dummy)
241
			{
242
				$type = ($key == 'mysqli') ? 'mysql' : $key;
243
				$ftp->unlink('install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql');
244
			}
245
246
			$ftp->close();
247
248
			unset($_SESSION['installer_temp_ftp']);
249
		}
250
		else
251
		{
252
			@unlink(__FILE__);
253
254
			foreach ($databases as $key => $dummy)
255
			{
256
				$type = ($key == 'mysqli') ? 'mysql' : $key;
257
				@unlink(dirname(__FILE__) . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql');
258
			}
259
		}
260
261
		// Now just redirect to a blank.png...
262
		$secure = false;
263
264
		if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
265
			$secure = true;
266
		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')
267
			$secure = true;
268
269
		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');
270
		exit;
271
	}
272
273
	// PHP 5 might cry if we don't do this now.
274
	if (function_exists('date_default_timezone_set'))
275
	{
276
		// Get PHP's default timezone, if set
277
		$ini_tz = ini_get('date.timezone');
278
		if (!empty($ini_tz))
279
			$timezone_id = $ini_tz;
280
		else
281
			$timezone_id = '';
282
283
		// If date.timezone is unset, invalid, or just plain weird, make a best guess
284
		if (!in_array($timezone_id, timezone_identifiers_list()))
285
		{
286
			$server_offset = @mktime(0, 0, 0, 1, 1, 1970) * -1;
287
			$timezone_id = timezone_name_from_abbr('', $server_offset, 0);
288
289
			if (empty($timezone_id))
290
				$timezone_id = 'UTC';
291
		}
292
293
		date_default_timezone_set($timezone_id);
294
	}
295
	header('X-Frame-Options: SAMEORIGIN');
296
	header('X-XSS-Protection: 1');
297
	header('X-Content-Type-Options: nosniff');
298
299
	// Force an integer step, defaulting to 0.
300
	$_GET['step'] = (int) @$_GET['step'];
301
}
302
303
// Load the list of language files, and the current language file.
304
function load_lang_file()
305
{
306
	global $incontext, $user_info, $txt;
307
308
	$incontext['detected_languages'] = array();
309
310
	// Make sure the languages directory actually exists.
311
	if (file_exists(dirname(__FILE__) . '/Themes/default/languages'))
312
	{
313
		// Find all the "Install" language files in the directory.
314
		$dir = dir(dirname(__FILE__) . '/Themes/default/languages');
315
		while ($entry = $dir->read())
316
		{
317
			if (substr($entry, 0, 8) == 'Install.' && substr($entry, -4) == '.php')
318
				$incontext['detected_languages'][$entry] = ucfirst(substr($entry, 8, strlen($entry) - 12));
319
		}
320
		$dir->close();
321
	}
322
323
	// Didn't find any, show an error message!
324
	if (empty($incontext['detected_languages']))
325
	{
326
		// Let's not cache this message, eh?
327
		header('expires: Mon, 26 Jul 1997 05:00:00 GMT');
328
		header('last-modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
329
		header('cache-control: no-cache');
330
331
		echo '<!DOCTYPE html>
332
<html>
333
	<head>
334
		<title>SMF Installer: Error!</title>
335
		<style>
336
			body {
337
				font-family: sans-serif;
338
				max-width: 700px; }
339
340
			h1 {
341
				font-size: 14pt; }
342
343
			.directory {
344
				margin: 0.3em;
345
				font-family: monospace;
346
				font-weight: bold; }
347
		</style>
348
	</head>
349
	<body>
350
		<h1>A critical error has occurred.</h1>
351
352
		<p>This installer was unable to find the installer\'s language file or files. They should be found under:</p>
353
354
		<div class="directory">', dirname($_SERVER['PHP_SELF']) != '/' ? dirname($_SERVER['PHP_SELF']) : '', '/Themes/default/languages</div>
355
356
		<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>
357
		<p>If that doesn\'t help, please make sure this install.php file is in the same place as the Themes folder.</p>
358
		<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>
359
	</div></body>
360
</html>';
361
		die;
362
	}
363
364
	// Override the language file?
365
	if (isset($_GET['lang_file']))
366
		$_SESSION['installer_temp_lang'] = $_GET['lang_file'];
367
	elseif (isset($GLOBALS['HTTP_GET_VARS']['lang_file']))
368
		$_SESSION['installer_temp_lang'] = $GLOBALS['HTTP_GET_VARS']['lang_file'];
369
370
	// Make sure it exists, if it doesn't reset it.
371
	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']))
372
	{
373
		// Use the first one...
374
		list ($_SESSION['installer_temp_lang']) = array_keys($incontext['detected_languages']);
375
376
		// If we have english and some other language, use the other language.  We Americans hate english :P.
377
		if ($_SESSION['installer_temp_lang'] == 'Install.english.php' && count($incontext['detected_languages']) > 1)
378
			list (, $_SESSION['installer_temp_lang']) = array_keys($incontext['detected_languages']);
379
	}
380
381
	// And now include the actual language file itself.
382
	require_once(dirname(__FILE__) . '/Themes/default/languages/' . $_SESSION['installer_temp_lang']);
383
384
	// Which language did we load? Assume that he likes his language.
385
	preg_match('~^Install\.(.+[^-utf8])\.php$~', $_SESSION['installer_temp_lang'], $matches);
386
	$user_info['language'] = $matches[1];
387
}
388
389
// This handy function loads some settings and the like.
390
function load_database()
391
{
392
	global $db_prefix, $db_connection, $sourcedir, $smcFunc, $modSettings, $db_port;
393
	global $db_server, $db_passwd, $db_type, $db_name, $db_user, $db_persist, $db_mb4;
394
395
	if (empty($sourcedir))
396
		$sourcedir = dirname(__FILE__) . '/Sources';
397
398
	// Need this to check whether we need the database password.
399
	require(dirname(__FILE__) . '/Settings.php');
400
	if (!defined('SMF'))
401
		define('SMF', 1);
402
	if (empty($smcFunc))
403
		$smcFunc = array();
404
405
	$modSettings['disableQueryCheck'] = true;
406
407
	// Connect the database.
408
	if (!$db_connection)
409
	{
410
		require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
411
412
		$options = array('persist' => $db_persist);
413
414
		if (!empty($db_port))
415
			$options['port'] = $db_port;
416
417
		if (!empty($db_mb4))
418
			$options['db_mb4'] = $db_mb4;
419
420
		if (!$db_connection)
421
			$db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $options);
422
	}
423
}
424
425
// This is called upon exiting the installer, for template etc.
426
function installExit($fallThrough = false)
427
{
428
	global $incontext, $installurl, $txt;
429
430
	// Send character set.
431
	header('content-type: text/html; charset=' . (isset($txt['lang_character_set']) ? $txt['lang_character_set'] : 'UTF-8'));
432
433
	// We usually dump our templates out.
434
	if (!$fallThrough)
435
	{
436
		// The top install bit.
437
		template_install_above();
438
439
		// Call the template.
440
		if (isset($incontext['sub_template']))
441
		{
442
			$incontext['form_url'] = $installurl . '?step=' . $incontext['current_step'];
443
444
			call_user_func('template_' . $incontext['sub_template']);
445
		}
446
		// @todo REMOVE THIS!!
447
		else
448
		{
449
			if (function_exists('doStep' . $_GET['step']))
450
				call_user_func('doStep' . $_GET['step']);
451
		}
452
		// Show the footer.
453
		template_install_below();
454
	}
455
456
	// Bang - gone!
457
	die();
458
}
459
460
function Welcome()
461
{
462
	global $incontext, $txt, $databases, $installurl;
463
464
	$incontext['page_title'] = $txt['install_welcome'];
465
	$incontext['sub_template'] = 'welcome_message';
466
467
	// Done the submission?
468
	if (isset($_POST['contbutt']))
469
		return true;
470
471
	// See if we think they have already installed it?
472
	if (is_readable(dirname(__FILE__) . '/Settings.php'))
473
	{
474
		$probably_installed = 0;
475
		foreach (file(dirname(__FILE__) . '/Settings.php') as $line)
476
		{
477
			if (preg_match('~^\$db_passwd\s=\s\'([^\']+)\';$~', $line))
478
				$probably_installed++;
479
			if (preg_match('~^\$boardurl\s=\s\'([^\']+)\';~', $line) && !preg_match('~^\$boardurl\s=\s\'http://127\.0\.0\.1/smf\';~', $line))
480
				$probably_installed++;
481
		}
482
483
		if ($probably_installed == 2)
484
			$incontext['warning'] = $txt['error_already_installed'];
485
	}
486
487
	// Is some database support even compiled in?
488
	$incontext['supported_databases'] = array();
489
	foreach ($databases as $key => $db)
490
	{
491
		if ($db['supported'])
492
		{
493
			$type = ($key == 'mysqli') ? 'mysql' : $key;
494
			if (!file_exists(dirname(__FILE__) . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql'))
495
			{
496
				$databases[$key]['supported'] = false;
497
				$notFoundSQLFile = true;
498
				$txt['error_db_script_missing'] = sprintf($txt['error_db_script_missing'], 'install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql');
499
			}
500
			else
501
				$incontext['supported_databases'][] = $db;
502
		}
503
	}
504
505
	// Check the PHP version.
506
	if ((!function_exists('version_compare') || version_compare($GLOBALS['required_php_version'], PHP_VERSION, '>=')))
507
		$error = 'error_php_too_low';
508
	// Make sure we have a supported database
509
	elseif (empty($incontext['supported_databases']))
510
		$error = empty($notFoundSQLFile) ? 'error_db_missing' : 'error_db_script_missing';
511
	// How about session support?  Some crazy sysadmin remove it?
512
	elseif (!function_exists('session_start'))
513
		$error = 'error_session_missing';
514
	// Make sure they uploaded all the files.
515
	elseif (!file_exists(dirname(__FILE__) . '/index.php'))
516
		$error = 'error_missing_files';
517
	// Very simple check on the session.save_path for Windows.
518
	// @todo Move this down later if they don't use database-driven sessions?
519
	elseif (@ini_get('session.save_path') == '/tmp' && substr(__FILE__, 1, 2) == ':\\')
520
		$error = 'error_session_save_path';
521
522
	// Since each of the three messages would look the same, anyway...
523
	if (isset($error))
524
		$incontext['error'] = $txt[$error];
525
526
	// Mod_security blocks everything that smells funny. Let SMF handle security.
527
	if (!fixModSecurity() && !isset($_GET['overmodsecurity']))
528
		$incontext['error'] = $txt['error_mod_security'] . '<br><br><a href="' . $installurl . '?overmodsecurity=true">' . $txt['error_message_click'] . '</a> ' . $txt['error_message_bad_try_again'];
529
530
	// Confirm mbstring is loaded...
531
	if (!extension_loaded('mbstring'))
532
		$incontext['error'] = $txt['install_no_mbstring'];
533
534
	// Confirm fileinfo is loaded...
535
	if (!extension_loaded('fileinfo'))
536
		$incontext['error'] = $txt['install_no_fileinfo'];
537
538
	// Check for https stream support.
539
	$supported_streams = stream_get_wrappers();
540
	if (!in_array('https', $supported_streams))
541
		$incontext['warning'] = $txt['install_no_https'];
542
543
	return false;
544
}
545
546
function CheckFilesWritable()
547
{
548
	global $txt, $incontext;
549
550
	$incontext['page_title'] = $txt['ftp_checking_writable'];
551
	$incontext['sub_template'] = 'chmod_files';
552
553
	$writable_files = array(
554
		'attachments',
555
		'avatars',
556
		'custom_avatar',
557
		'cache',
558
		'Packages',
559
		'Smileys',
560
		'Themes',
561
		'agreement.txt',
562
		'Settings.php',
563
		'Settings_bak.php',
564
		'cache/db_last_error.php',
565
	);
566
567
	foreach ($incontext['detected_languages'] as $lang => $temp)
568
		$extra_files[] = 'Themes/default/languages/' . $lang;
569
570
	// With mod_security installed, we could attempt to fix it with .htaccess.
571
	if (function_exists('apache_get_modules') && in_array('mod_security', apache_get_modules()))
572
		$writable_files[] = file_exists(dirname(__FILE__) . '/.htaccess') ? '.htaccess' : '.';
573
574
	$failed_files = array();
575
576
	// On linux, it's easy - just use is_writable!
577
	if (substr(__FILE__, 1, 2) != ':\\')
578
	{
579
		$incontext['systemos'] = 'linux';
580
581
		foreach ($writable_files as $file)
582
		{
583
			// Some files won't exist, try to address up front
584
			if (!file_exists(dirname(__FILE__) . '/' . $file))
585
				@touch(dirname(__FILE__) . '/' . $file);
586
			// NOW do the writable check...
587
			if (!is_writable(dirname(__FILE__) . '/' . $file))
588
			{
589
				@chmod(dirname(__FILE__) . '/' . $file, 0755);
590
591
				// Well, 755 hopefully worked... if not, try 777.
592
				if (!is_writable(dirname(__FILE__) . '/' . $file) && !@chmod(dirname(__FILE__) . '/' . $file, 0777))
593
					$failed_files[] = $file;
594
			}
595
		}
596
		foreach ($extra_files as $file)
597
			@chmod(dirname(__FILE__) . (empty($file) ? '' : '/' . $file), 0777);
598
	}
599
	// Windows is trickier.  Let's try opening for r+...
600
	else
601
	{
602
		$incontext['systemos'] = 'windows';
603
604
		foreach ($writable_files as $file)
605
		{
606
			// Folders can't be opened for write... but the index.php in them can ;)
607
			if (is_dir(dirname(__FILE__) . '/' . $file))
608
				$file .= '/index.php';
609
610
			// Funny enough, chmod actually does do something on windows - it removes the read only attribute.
611
			@chmod(dirname(__FILE__) . '/' . $file, 0777);
612
			$fp = @fopen(dirname(__FILE__) . '/' . $file, 'r+');
613
614
			// Hmm, okay, try just for write in that case...
615
			if (!is_resource($fp))
616
				$fp = @fopen(dirname(__FILE__) . '/' . $file, 'w');
617
618
			if (!is_resource($fp))
619
				$failed_files[] = $file;
620
621
			@fclose($fp);
622
		}
623
		foreach ($extra_files as $file)
624
			@chmod(dirname(__FILE__) . (empty($file) ? '' : '/' . $file), 0777);
625
	}
626
627
	$failure = count($failed_files) >= 1;
628
629
	if (!isset($_SERVER))
630
		return !$failure;
631
632
	// Put the list into context.
633
	$incontext['failed_files'] = $failed_files;
634
635
	// It's not going to be possible to use FTP on windows to solve the problem...
636
	if ($failure && substr(__FILE__, 1, 2) == ':\\')
637
	{
638
		$incontext['error'] = $txt['error_windows_chmod'] . '
639
					<ul class="error_content">
640
						<li>' . implode('</li>
641
						<li>', $failed_files) . '</li>
642
					</ul>';
643
644
		return false;
645
	}
646
	// We're going to have to use... FTP!
647
	elseif ($failure)
648
	{
649
		// Load any session data we might have...
650
		if (!isset($_POST['ftp_username']) && isset($_SESSION['installer_temp_ftp']))
651
		{
652
			$_POST['ftp_server'] = $_SESSION['installer_temp_ftp']['server'];
653
			$_POST['ftp_port'] = $_SESSION['installer_temp_ftp']['port'];
654
			$_POST['ftp_username'] = $_SESSION['installer_temp_ftp']['username'];
655
			$_POST['ftp_password'] = $_SESSION['installer_temp_ftp']['password'];
656
			$_POST['ftp_path'] = $_SESSION['installer_temp_ftp']['path'];
657
		}
658
659
		$incontext['ftp_errors'] = array();
660
		require_once('Sources/Class-Package.php');
661
		if (isset($_POST['ftp_username']))
662
		{
663
			$ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
664
665
			if ($ftp->error === false)
666
			{
667
				// Try it without /home/abc just in case they messed up.
668
				if (!$ftp->chdir($_POST['ftp_path']))
669
				{
670
					$incontext['ftp_errors'][] = $ftp->last_message;
671
					$ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
672
				}
673
			}
674
		}
675
676
		if (!isset($ftp) || $ftp->error !== false)
677
		{
678
			if (!isset($ftp))
679
				$ftp = new ftp_connection(null);
680
			// Save the error so we can mess with listing...
681
			elseif ($ftp->error !== false && empty($incontext['ftp_errors']) && !empty($ftp->last_message))
682
				$incontext['ftp_errors'][] = $ftp->last_message;
683
684
			list ($username, $detect_path, $found_path) = $ftp->detect_path(dirname(__FILE__));
685
686
			if (empty($_POST['ftp_path']) && $found_path)
687
				$_POST['ftp_path'] = $detect_path;
688
689
			if (!isset($_POST['ftp_username']))
690
				$_POST['ftp_username'] = $username;
691
692
			// Set the username etc, into context.
693
			$incontext['ftp'] = array(
694
				'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : 'localhost',
695
				'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : '21',
696
				'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : '',
697
				'path' => isset($_POST['ftp_path']) ? $_POST['ftp_path'] : '/',
698
				'path_msg' => !empty($found_path) ? $txt['ftp_path_found_info'] : $txt['ftp_path_info'],
699
			);
700
701
			return false;
702
		}
703
		else
704
		{
705
			$_SESSION['installer_temp_ftp'] = array(
706
				'server' => $_POST['ftp_server'],
707
				'port' => $_POST['ftp_port'],
708
				'username' => $_POST['ftp_username'],
709
				'password' => $_POST['ftp_password'],
710
				'path' => $_POST['ftp_path']
711
			);
712
713
			$failed_files_updated = array();
714
715
			foreach ($failed_files as $file)
716
			{
717
				if (!is_writable(dirname(__FILE__) . '/' . $file))
718
					$ftp->chmod($file, 0755);
719
				if (!is_writable(dirname(__FILE__) . '/' . $file))
720
					$ftp->chmod($file, 0777);
721
				if (!is_writable(dirname(__FILE__) . '/' . $file))
722
				{
723
					$failed_files_updated[] = $file;
724
					$incontext['ftp_errors'][] = rtrim($ftp->last_message) . ' -> ' . $file . "\n";
725
				}
726
			}
727
728
			$ftp->close();
729
730
			// Are there any errors left?
731
			if (count($failed_files_updated) >= 1)
732
			{
733
				// Guess there are...
734
				$incontext['failed_files'] = $failed_files_updated;
735
736
				// Set the username etc, into context.
737
				$incontext['ftp'] = $_SESSION['installer_temp_ftp'] += array(
738
					'path_msg' => $txt['ftp_path_info'],
739
				);
740
741
				return false;
742
			}
743
		}
744
	}
745
746
	return true;
747
}
748
749
function DatabaseSettings()
750
{
751
	global $txt, $databases, $incontext, $smcFunc, $sourcedir;
752
	global $db_server, $db_name, $db_user, $db_passwd, $db_port, $db_mb4, $db_connection;
753
754
	$incontext['sub_template'] = 'database_settings';
755
	$incontext['page_title'] = $txt['db_settings'];
756
	$incontext['continue'] = 1;
757
758
	// Set up the defaults.
759
	$incontext['db']['server'] = 'localhost';
760
	$incontext['db']['user'] = '';
761
	$incontext['db']['name'] = '';
762
	$incontext['db']['pass'] = '';
763
	$incontext['db']['type'] = '';
764
	$incontext['supported_databases'] = array();
765
766
	$foundOne = false;
767
	foreach ($databases as $key => $db)
768
	{
769
		// Override with the defaults for this DB if appropriate.
770
		if ($db['supported'])
771
		{
772
			$incontext['supported_databases'][$key] = $db;
773
774
			if (!$foundOne)
775
			{
776
				if (isset($db['default_host']))
777
					$incontext['db']['server'] = ini_get($db['default_host']) or $incontext['db']['server'] = 'localhost';
778
				if (isset($db['default_user']))
779
				{
780
					$incontext['db']['user'] = ini_get($db['default_user']);
781
					$incontext['db']['name'] = ini_get($db['default_user']);
782
				}
783
				if (isset($db['default_password']))
784
					$incontext['db']['pass'] = ini_get($db['default_password']);
785
786
				// For simplicity and less confusion, leave the port blank by default
787
				$incontext['db']['port'] = '';
788
789
				$incontext['db']['type'] = $key;
790
				$foundOne = true;
791
			}
792
		}
793
	}
794
795
	// Override for repost.
796
	if (isset($_POST['db_user']))
797
	{
798
		$incontext['db']['user'] = $_POST['db_user'];
799
		$incontext['db']['name'] = $_POST['db_name'];
800
		$incontext['db']['server'] = $_POST['db_server'];
801
		$incontext['db']['prefix'] = $_POST['db_prefix'];
802
803
		if (!empty($_POST['db_port']))
804
			$incontext['db']['port'] = $_POST['db_port'];
805
	}
806
	else
807
	{
808
		$incontext['db']['prefix'] = 'smf_';
809
	}
810
811
	// Are we submitting?
812
	if (isset($_POST['db_type']))
813
	{
814
		// What type are they trying?
815
		$db_type = preg_replace('~[^A-Za-z0-9]~', '', $_POST['db_type']);
816
		$db_prefix = $_POST['db_prefix'];
817
		// Validate the prefix.
818
		$valid_prefix = $databases[$db_type]['validate_prefix']($db_prefix);
819
820
		if ($valid_prefix !== true)
821
		{
822
			$incontext['error'] = $valid_prefix;
823
			return false;
824
		}
825
826
		// Take care of these variables...
827
		$vars = array(
828
			'db_type' => $db_type,
829
			'db_name' => $_POST['db_name'],
830
			'db_user' => $_POST['db_user'],
831
			'db_passwd' => isset($_POST['db_passwd']) ? $_POST['db_passwd'] : '',
832
			'db_server' => $_POST['db_server'],
833
			'db_prefix' => $db_prefix,
834
			// The cookiename is special; we want it to be the same if it ever needs to be reinstalled with the same info.
835
			'cookiename' => 'SMFCookie' . abs(crc32($_POST['db_name'] . preg_replace('~[^A-Za-z0-9_$]~', '', $_POST['db_prefix'])) % 1000),
836
		);
837
838
		// Only set the port if we're not using the default
839
		if (!empty($_POST['db_port']))
840
		{
841
			// For MySQL, we can get the "default port" from PHP. PostgreSQL has no such option though.
842
			if (($db_type == 'mysql' || $db_type == 'mysqli') && $_POST['db_port'] != ini_get($db_type . '.default_port'))
843
				$vars['db_port'] = (int) $_POST['db_port'];
844
			elseif ($db_type == 'postgresql' && $_POST['db_port'] != 5432)
845
				$vars['db_port'] = (int) $_POST['db_port'];
846
		}
847
848
		// God I hope it saved!
849
		if (!installer_updateSettingsFile($vars))
850
		{
851
			$incontext['error'] = $txt['settings_error'];
852
			return false;
853
		}
854
855
		// Make sure it works.
856
		require(dirname(__FILE__) . '/Settings.php');
857
858
		if (empty($sourcedir))
859
			$sourcedir = dirname(__FILE__) . '/Sources';
860
861
		// Better find the database file!
862
		if (!file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php'))
863
		{
864
			$incontext['error'] = sprintf($txt['error_db_file'], 'Subs-Db-' . $db_type . '.php');
865
			return false;
866
		}
867
868
		// Now include it for database functions!
869
		if (!defined('SMF'))
870
			define('SMF', 1);
871
872
		$modSettings['disableQueryCheck'] = true;
873
		if (empty($smcFunc))
874
			$smcFunc = array();
875
876
		require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
877
878
		// Attempt a connection.
879
		$needsDB = !empty($databases[$db_type]['always_has_db']);
880
881
		$options = array('non_fatal' => true, 'dont_select_db' => !$needsDB);
882
		// Add in the port if needed
883
		if (!empty($db_port))
884
			$options['port'] = $db_port;
885
886
		if (!empty($db_mb4))
887
			$options['db_mb4'] = $db_mb4;
888
889
		$db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $options);
890
891
		// Still no connection?  Big fat error message :P.
892
		if (!$db_connection)
893
		{
894
			// Get error info...  Recast just in case we get false or 0...
895
			$error_message = $smcFunc['db_connect_error']();
896
			if (empty($error_message))
897
				$error_message = '';
898
			$error_number = $smcFunc['db_connect_errno']();
899
			if (empty($error_number))
900
				$error_number = '';
901
			$db_error = (!empty($error_number) ? $error_number . ': ' : '') . $error_message;
902
903
			$incontext['error'] = $txt['error_db_connect'] . '<div class="error_content"><strong>' . $db_error . '</strong></div>';
904
			return false;
905
		}
906
907
		// Do they meet the install requirements?
908
		// @todo Old client, new server?
909
		if (version_compare($databases[$db_type]['version'], preg_replace('~^\D*|\-.+?$~', '', $databases[$db_type]['version_check']())) > 0)
910
		{
911
			$incontext['error'] = $txt['error_db_too_low'];
912
			return false;
913
		}
914
915
		// Let's try that database on for size... assuming we haven't already lost the opportunity.
916
		if ($db_name != '' && !$needsDB)
917
		{
918
			$smcFunc['db_query']('', "
919
				CREATE DATABASE IF NOT EXISTS `$db_name`",
920
				array(
921
					'security_override' => true,
922
					'db_error_skip' => true,
923
				),
924
				$db_connection
925
			);
926
927
			// Okay, let's try the prefix if it didn't work...
928
			if (!$smcFunc['db_select_db']($db_name, $db_connection) && $db_name != '')
929
			{
930
				$smcFunc['db_query']('', "
931
					CREATE DATABASE IF NOT EXISTS `$_POST[db_prefix]$db_name`",
932
					array(
933
						'security_override' => true,
934
						'db_error_skip' => true,
935
					),
936
					$db_connection
937
				);
938
939
				if ($smcFunc['db_select_db']($_POST['db_prefix'] . $db_name, $db_connection))
940
				{
941
					$db_name = $_POST['db_prefix'] . $db_name;
942
					installer_updateSettingsFile(array('db_name' => $db_name));
943
				}
944
			}
945
946
			// Okay, now let's try to connect...
947
			if (!$smcFunc['db_select_db']($db_name, $db_connection))
948
			{
949
				$incontext['error'] = sprintf($txt['error_db_database'], $db_name);
950
				return false;
951
			}
952
		}
953
954
		return true;
955
	}
956
957
	return false;
958
}
959
960
// Let's start with basic forum type settings.
961
function ForumSettings()
962
{
963
	global $txt, $incontext, $databases, $db_type, $db_connection, $smcFunc;
964
965
	$incontext['sub_template'] = 'forum_settings';
966
	$incontext['page_title'] = $txt['install_settings'];
967
968
	// Let's see if we got the database type correct.
969
	if (isset($_POST['db_type'], $databases[$_POST['db_type']]))
970
		$db_type = $_POST['db_type'];
971
972
	// Else we'd better be able to get the connection.
973
	else
974
		load_database();
975
976
	$db_type = isset($_POST['db_type']) ? $_POST['db_type'] : $db_type;
977
978
	// What host and port are we on?
979
	$host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . (empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT']) : $_SERVER['HTTP_HOST'];
980
981
	$secure = false;
982
983
	if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
984
		$secure = true;
985
	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')
986
		$secure = true;
987
988
	// Now, to put what we've learned together... and add a path.
989
	$incontext['detected_url'] = 'http' . ($secure ? 's' : '') . '://' . $host . substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));
990
991
	// Check if the database sessions will even work.
992
	$incontext['test_dbsession'] = (ini_get('session.auto_start') != 1);
993
994
	$incontext['continue'] = 1;
995
996
	// Check Postgres setting
997
	if ( $db_type === 'postgresql')
998
	{
999
		load_database();
1000
		$result = $smcFunc['db_query']('', '
1001
			show standard_conforming_strings',
1002
			array(
1003
				'db_error_skip' => true,
1004
			)
1005
		);
1006
1007
		if ($result !== false)
1008
		{
1009
			$row = $smcFunc['db_fetch_assoc']($result);
1010
			if ($row['standard_conforming_strings'] !== 'on')
1011
				{
1012
					$incontext['continue'] = 0;
1013
					$incontext['error'] = $txt['error_pg_scs'];
1014
				}
1015
			$smcFunc['db_free_result']($result);
1016
		}
1017
	}
1018
1019
	// Setup the SSL checkbox...
1020
	$incontext['ssl_chkbx_protected'] = false;
1021
	$incontext['ssl_chkbx_checked'] = false;
1022
1023
	// If redirect in effect, force ssl ON
1024
	require_once(dirname(__FILE__) . '/Sources/Subs.php');
1025
	if (https_redirect_active($incontext['detected_url']))
1026
	{
1027
		$incontext['ssl_chkbx_protected'] = true;
1028
		$incontext['ssl_chkbx_checked'] = true;
1029
		$_POST['force_ssl'] = true;
1030
	}
1031
	// If no cert, make sure ssl stays OFF
1032
	if (!ssl_cert_found($incontext['detected_url']))
1033
	{
1034
		$incontext['ssl_chkbx_protected'] = true;
1035
		$incontext['ssl_chkbx_checked'] = false;
1036
	}
1037
1038
	// Submitting?
1039
	if (isset($_POST['boardurl']))
1040
	{
1041
		if (substr($_POST['boardurl'], -10) == '/index.php')
1042
			$_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
1043
		elseif (substr($_POST['boardurl'], -1) == '/')
1044
			$_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
1045
		if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://')
1046
			$_POST['boardurl'] = 'http://' . $_POST['boardurl'];
1047
1048
		// Make sure boardurl is aligned with ssl setting
1049
		if (empty($_POST['force_ssl']))
1050
			$_POST['boardurl'] = strtr($_POST['boardurl'], array('https://' => 'http://'));
1051
		else
1052
			$_POST['boardurl'] = strtr($_POST['boardurl'], array('http://' => 'https://'));
1053
1054
		// Make sure international domain names are normalized correctly.
1055
		if ($txt['lang_character_set'] == 'UTF-8')
1056
			$_POST['boardurl'] = normalize_iri($_POST['boardurl']);
1057
1058
		// Deal with different operating systems' directory structure...
1059
		$path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', __DIR__), '/');
1060
1061
		// Load the compatibility library if necessary.
1062
		if (!is_callable('random_bytes'))
1063
			require_once('Sources/random_compat/random.php');
1064
1065
		// Save these variables.
1066
		$vars = array(
1067
			'boardurl' => $_POST['boardurl'],
1068
			'boarddir' => $path,
1069
			'sourcedir' => $path . '/Sources',
1070
			'cachedir' => $path . '/cache',
1071
			'packagesdir' => $path . '/Packages',
1072
			'tasksdir' => $path . '/Sources/tasks',
1073
			'mbname' => strtr($_POST['mbname'], array('\"' => '"')),
1074
			'language' => substr($_SESSION['installer_temp_lang'], 8, -4),
1075
			'image_proxy_secret' => bin2hex(random_bytes(10)),
1076
			'image_proxy_enabled' => !empty($_POST['force_ssl']),
1077
			'auth_secret' => bin2hex(random_bytes(32)),
1078
		);
1079
1080
		// Must save!
1081
		if (!installer_updateSettingsFile($vars))
1082
		{
1083
			$incontext['error'] = $txt['settings_error'];
1084
			return false;
1085
		}
1086
1087
		// Make sure it works.
1088
		require(dirname(__FILE__) . '/Settings.php');
1089
1090
		// UTF-8 requires a setting to override the language charset.
1091
		if (!$databases[$db_type]['utf8_support']())
1092
		{
1093
			$incontext['error'] = sprintf($txt['error_utf8_support']);
1094
			return false;
1095
		}
1096
1097
		if (!empty($databases[$db_type]['utf8_version_check']) && version_compare($databases[$db_type]['utf8_version'], preg_replace('~\-.+?$~', '', $databases[$db_type]['utf8_version_check']()), '>'))
1098
		{
1099
			$incontext['error'] = sprintf($txt['error_utf8_version'], $databases[$db_type]['utf8_version']);
1100
			return false;
1101
		}
1102
1103
		// Set the character set here.
1104
		installer_updateSettingsFile(array('db_character_set' => 'utf8'), true);
1105
1106
		// Good, skip on.
1107
		return true;
1108
	}
1109
1110
	return false;
1111
}
1112
1113
// Step one: Do the SQL thang.
1114
function DatabasePopulation()
1115
{
1116
	global $db_character_set, $txt, $db_connection, $smcFunc, $databases, $modSettings, $db_type, $db_prefix, $incontext, $db_name, $boardurl;
1117
1118
	$incontext['sub_template'] = 'populate_database';
1119
	$incontext['page_title'] = $txt['db_populate'];
1120
	$incontext['continue'] = 1;
1121
1122
	// Already done?
1123
	if (isset($_POST['pop_done']))
1124
		return true;
1125
1126
	// Reload settings.
1127
	require(dirname(__FILE__) . '/Settings.php');
1128
	load_database();
1129
1130
	// Before running any of the queries, let's make sure another version isn't already installed.
1131
	$result = $smcFunc['db_query']('', '
1132
		SELECT variable, value
1133
		FROM {db_prefix}settings',
1134
		array(
1135
			'db_error_skip' => true,
1136
		)
1137
	);
1138
	$newSettings = array();
1139
	$modSettings = array();
1140
	if ($result !== false)
1141
	{
1142
		while ($row = $smcFunc['db_fetch_assoc']($result))
1143
			$modSettings[$row['variable']] = $row['value'];
1144
		$smcFunc['db_free_result']($result);
1145
1146
		// Do they match?  If so, this is just a refresh so charge on!
1147
		if (!isset($modSettings['smfVersion']) || $modSettings['smfVersion'] != SMF_VERSION)
1148
		{
1149
			$incontext['error'] = $txt['error_versions_do_not_match'];
1150
			return false;
1151
		}
1152
	}
1153
	$modSettings['disableQueryCheck'] = true;
1154
1155
	// If doing UTF8, select it. PostgreSQL requires passing it as a string...
1156
	$smcFunc['db_query']('', '
1157
		SET NAMES {string:utf8}',
1158
		array(
1159
			'db_error_skip' => true,
1160
			'utf8' => 'utf8',
1161
		)
1162
	);
1163
1164
	// Windows likes to leave the trailing slash, which yields to C:\path\to\SMF\/attachments...
1165
	if (substr(__DIR__, -1) == '\\')
1166
		$attachdir = __DIR__ . 'attachments';
1167
	else
1168
		$attachdir = __DIR__ . '/attachments';
1169
1170
	$replaces = array(
1171
		'{$db_prefix}' => $db_prefix,
1172
		'{$attachdir}' => json_encode(array(1 => $smcFunc['db_escape_string']($attachdir))),
1173
		'{$boarddir}' => $smcFunc['db_escape_string'](dirname(__FILE__)),
1174
		'{$boardurl}' => $boardurl,
1175
		'{$enableCompressedOutput}' => isset($_POST['compress']) ? '1' : '0',
1176
		'{$databaseSession_enable}' => isset($_POST['dbsession']) ? '1' : '0',
1177
		'{$smf_version}' => SMF_VERSION,
1178
		'{$current_time}' => time(),
1179
		'{$sched_task_offset}' => 82800 + mt_rand(0, 86399),
1180
		'{$registration_method}' => isset($_POST['reg_mode']) ? $_POST['reg_mode'] : 0,
1181
	);
1182
1183
	foreach ($txt as $key => $value)
1184
	{
1185
		if (substr($key, 0, 8) == 'default_')
1186
			$replaces['{$' . $key . '}'] = $smcFunc['db_escape_string']($value);
1187
	}
1188
	$replaces['{$default_reserved_names}'] = strtr($replaces['{$default_reserved_names}'], array('\\\\n' => '\\n'));
1189
1190
	// MySQL-specific stuff - storage engine and UTF8 handling
1191
	if (substr($db_type, 0, 5) == 'mysql')
1192
	{
1193
		// Just in case the query fails for some reason...
1194
		$engines = array();
1195
1196
		// Figure out storage engines - what do we have, etc.
1197
		$get_engines = $smcFunc['db_query']('', 'SHOW ENGINES', array());
1198
1199
		while ($row = $smcFunc['db_fetch_assoc']($get_engines))
1200
		{
1201
			if ($row['Support'] == 'YES' || $row['Support'] == 'DEFAULT')
1202
				$engines[] = $row['Engine'];
1203
		}
1204
1205
		// Done with this now
1206
		$smcFunc['db_free_result']($get_engines);
1207
1208
		// InnoDB is better, so use it if possible...
1209
		$has_innodb = in_array('InnoDB', $engines);
1210
		$replaces['{$engine}'] = $has_innodb ? 'InnoDB' : 'MyISAM';
1211
		$replaces['{$memory}'] = (!$has_innodb && in_array('MEMORY', $engines)) ? 'MEMORY' : $replaces['{$engine}'];
1212
1213
		// UTF-8 is required.
1214
		$replaces['{$engine}'] .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
1215
		$replaces['{$memory}'] .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
1216
1217
		// One last thing - if we don't have InnoDB, we can't do transactions...
1218
		if (!$has_innodb)
1219
		{
1220
			$replaces['START TRANSACTION;'] = '';
1221
			$replaces['COMMIT;'] = '';
1222
		}
1223
	}
1224
	else
1225
	{
1226
		$has_innodb = false;
1227
	}
1228
1229
	// Read in the SQL.  Turn this on and that off... internationalize... etc.
1230
	$type = ($db_type == 'mysqli' ? 'mysql' : $db_type);
1231
	$sql_lines = explode("\n", strtr(implode(' ', file(dirname(__FILE__) . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql')), $replaces));
1232
1233
	// Execute the SQL.
1234
	$current_statement = '';
1235
	$exists = array();
1236
	$incontext['failures'] = array();
1237
	$incontext['sql_results'] = array(
1238
		'tables' => 0,
1239
		'inserts' => 0,
1240
		'table_dups' => 0,
1241
		'insert_dups' => 0,
1242
	);
1243
	foreach ($sql_lines as $count => $line)
1244
	{
1245
		// No comments allowed!
1246
		if (substr(trim($line), 0, 1) != '#')
1247
			$current_statement .= "\n" . rtrim($line);
1248
1249
		// Is this the end of the query string?
1250
		if (empty($current_statement) || (preg_match('~;[\s]*$~s', $line) == 0 && $count != count($sql_lines)))
1251
			continue;
1252
1253
		// Does this table already exist?  If so, don't insert more data into it!
1254
		if (preg_match('~^\s*INSERT INTO ([^\s\n\r]+?)~', $current_statement, $match) != 0 && in_array($match[1], $exists))
1255
		{
1256
			preg_match_all('~\)[,;]~', $current_statement, $matches);
1257
			if (!empty($matches[0]))
1258
				$incontext['sql_results']['insert_dups'] += count($matches[0]);
1259
			else
1260
				$incontext['sql_results']['insert_dups']++;
1261
1262
			$current_statement = '';
1263
			continue;
1264
		}
1265
1266
		if ($smcFunc['db_query']('', $current_statement, array('security_override' => true, 'db_error_skip' => true), $db_connection) === false)
1267
		{
1268
			// Error 1050: Table already exists!
1269
			// @todo Needs to be made better!
1270
			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
Consider adding parentheses for clarity. Current Interpretation: ($db_type != 'mysql' && ...statement, $match) == 1, Probably Intended Meaning: $db_type != 'mysql' && $...tatement, $match) == 1)
Loading history...
1271
			{
1272
				$exists[] = $match[1];
1273
				$incontext['sql_results']['table_dups']++;
1274
			}
1275
			// Don't error on duplicate indexes (or duplicate operators in PostgreSQL.)
1276
			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)))
1277
			{
1278
				// MySQLi requires a connection object. It's optional with MySQL and Postgres
1279
				$incontext['failures'][$count] = $smcFunc['db_error']($db_connection);
1280
			}
1281
		}
1282
		else
1283
		{
1284
			if (preg_match('~^\s*CREATE TABLE ([^\s\n\r]+?)~', $current_statement, $match) == 1)
1285
				$incontext['sql_results']['tables']++;
1286
			elseif (preg_match('~^\s*INSERT INTO ([^\s\n\r]+?)~', $current_statement, $match) == 1)
1287
			{
1288
				preg_match_all('~\)[,;]~', $current_statement, $matches);
1289
				if (!empty($matches[0]))
1290
					$incontext['sql_results']['inserts'] += count($matches[0]);
1291
				else
1292
					$incontext['sql_results']['inserts']++;
1293
			}
1294
		}
1295
1296
		$current_statement = '';
1297
1298
		// Wait, wait, I'm still working here!
1299
		@set_time_limit(60);
1300
	}
1301
1302
	// Sort out the context for the SQL.
1303
	foreach ($incontext['sql_results'] as $key => $number)
1304
	{
1305
		if ($number == 0)
1306
			unset($incontext['sql_results'][$key]);
1307
		else
1308
			$incontext['sql_results'][$key] = sprintf($txt['db_populate_' . $key], $number);
1309
	}
1310
1311
	// Make sure UTF will be used globally.
1312
	$newSettings[] = array('global_character_set', 'UTF-8');
1313
1314
	// Are we allowing stat collection?
1315
	if (!empty($_POST['stats']) && substr($boardurl, 0, 16) != 'http://localhost' && empty($modSettings['allow_sm_stats']) && empty($modSettings['enable_sm_stats']))
1316
	{
1317
		$incontext['allow_sm_stats'] = true;
1318
1319
		// Attempt to register the site etc.
1320
		$fp = @fsockopen('www.simplemachines.org', 443, $errno, $errstr);
1321
		if (!$fp)
1322
			$fp = @fsockopen('www.simplemachines.org', 80, $errno, $errstr);
1323
		if ($fp)
1324
		{
1325
			$out = 'GET /smf/stats/register_stats.php?site=' . base64_encode($boardurl) . ' HTTP/1.1' . "\r\n";
1326
			$out .= 'Host: www.simplemachines.org' . "\r\n";
1327
			$out .= 'Connection: Close' . "\r\n\r\n";
1328
			fwrite($fp, $out);
1329
1330
			$return_data = '';
1331
			while (!feof($fp))
1332
				$return_data .= fgets($fp, 128);
1333
1334
			fclose($fp);
1335
1336
			// Get the unique site ID.
1337
			preg_match('~SITE-ID:\s(\w{10})~', $return_data, $ID);
1338
1339
			if (!empty($ID[1]))
1340
				$smcFunc['db_insert']('replace',
1341
					$db_prefix . 'settings',
1342
					array('variable' => 'string', 'value' => 'string'),
1343
					array(
1344
						array('sm_stats_key', $ID[1]),
1345
						array('enable_sm_stats', 1),
1346
					),
1347
					array('variable')
1348
				);
1349
		}
1350
	}
1351
	// Don't remove stat collection unless we unchecked the box for real, not from the loop.
1352
	elseif (empty($_POST['stats']) && empty($incontext['allow_sm_stats']))
1353
		$smcFunc['db_query']('', '
1354
			DELETE FROM {db_prefix}settings
1355
			WHERE variable = {string:enable_sm_stats}',
1356
			array(
1357
				'enable_sm_stats' => 'enable_sm_stats',
1358
				'db_error_skip' => true,
1359
			)
1360
		);
1361
1362
	// Are we enabling SSL?
1363
	if (!empty($_POST['force_ssl']))
1364
		$newSettings[] = array('force_ssl', 1);
1365
1366
	// Setting a timezone is required.
1367
	if (!isset($modSettings['default_timezone']) && function_exists('date_default_timezone_set'))
1368
	{
1369
		// Get PHP's default timezone, if set
1370
		$ini_tz = ini_get('date.timezone');
1371
		if (!empty($ini_tz))
1372
			$timezone_id = $ini_tz;
1373
		else
1374
			$timezone_id = '';
1375
1376
		// If date.timezone is unset, invalid, or just plain weird, make a best guess
1377
		if (!in_array($timezone_id, timezone_identifiers_list()))
1378
		{
1379
			$server_offset = @mktime(0, 0, 0, 1, 1, 1970) * -1;
1380
			$timezone_id = timezone_name_from_abbr('', $server_offset, 0);
1381
1382
			if (empty($timezone_id))
1383
				$timezone_id = 'UTC';
1384
		}
1385
1386
		if (date_default_timezone_set($timezone_id))
1387
			$newSettings[] = array('default_timezone', $timezone_id);
1388
	}
1389
1390
	if (!empty($newSettings))
1391
	{
1392
		$smcFunc['db_insert']('replace',
1393
			'{db_prefix}settings',
1394
			array('variable' => 'string-255', 'value' => 'string-65534'),
1395
			$newSettings,
1396
			array('variable')
1397
		);
1398
	}
1399
1400
	// Populate the smiley_files table.
1401
	// Can't just dump this data in the SQL file because we need to know the id for each smiley.
1402
	$smiley_filenames = array(
1403
		':)' => 'smiley',
1404
		';)' => 'wink',
1405
		':D' => 'cheesy',
1406
		';D' => 'grin',
1407
		'>:(' => 'angry',
1408
		':(' => 'sad',
1409
		':o' => 'shocked',
1410
		'8)' => 'cool',
1411
		'???' => 'huh',
1412
		'::)' => 'rolleyes',
1413
		':P' => 'tongue',
1414
		':-[' => 'embarrassed',
1415
		':-X' => 'lipsrsealed',
1416
		':-\\' => 'undecided',
1417
		':-*' => 'kiss',
1418
		':\'(' => 'cry',
1419
		'>:D' => 'evil',
1420
		'^-^' => 'azn',
1421
		'O0' => 'afro',
1422
		':))' => 'laugh',
1423
		'C:-)' => 'police',
1424
		'O:-)' => 'angel'
1425
	);
1426
	$smiley_set_extensions = array('fugue' => '.png', 'alienine' => '.png');
1427
1428
	$smiley_inserts = array();
1429
	$request = $smcFunc['db_query']('', '
1430
		SELECT id_smiley, code
1431
		FROM {db_prefix}smileys',
1432
		array()
1433
	);
1434
	while ($row = $smcFunc['db_fetch_assoc']($request))
1435
	{
1436
		foreach ($smiley_set_extensions as $set => $ext)
1437
			$smiley_inserts[] = array($row['id_smiley'], $set, $smiley_filenames[$row['code']] . $ext);
1438
	}
1439
	$smcFunc['db_free_result']($request);
1440
1441
	$smcFunc['db_insert']('ignore',
1442
		'{db_prefix}smiley_files',
1443
		array('id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48'),
1444
		$smiley_inserts,
1445
		array('id_smiley', 'smiley_set')
1446
	);
1447
1448
	// Let's optimize those new tables, but not on InnoDB, ok?
1449
	if (!$has_innodb)
1450
	{
1451
		db_extend();
1452
		$tables = $smcFunc['db_list_tables']($db_name, $db_prefix . '%');
1453
		foreach ($tables as $table)
1454
		{
1455
			$smcFunc['db_optimize_table']($table) != -1 or $db_messed = true;
1456
1457
			if (!empty($db_messed))
1458
			{
1459
				$incontext['failures'][-1] = $smcFunc['db_error']();
1460
				break;
1461
			}
1462
		}
1463
	}
1464
1465
	// MySQL specific stuff
1466
	if (substr($db_type, 0, 5) != 'mysql')
1467
		return false;
1468
1469
	// Find database user privileges.
1470
	$privs = array();
1471
	$get_privs = $smcFunc['db_query']('', 'SHOW PRIVILEGES', array());
1472
	while ($row = $smcFunc['db_fetch_assoc']($get_privs))
1473
	{
1474
		if ($row['Privilege'] == 'Alter')
1475
			$privs[] = $row['Privilege'];
1476
	}
1477
	$smcFunc['db_free_result']($get_privs);
1478
1479
	// Check for the ALTER privilege.
1480
	if (!empty($databases[$db_type]['alter_support']) && !in_array('Alter', $privs))
1481
	{
1482
		$incontext['error'] = $txt['error_db_alter_priv'];
1483
		return false;
1484
	}
1485
1486
	if (!empty($exists))
1487
	{
1488
		$incontext['page_title'] = $txt['user_refresh_install'];
1489
		$incontext['was_refresh'] = true;
1490
	}
1491
1492
	return false;
1493
}
1494
1495
// Ask for the administrator login information.
1496
function AdminAccount()
1497
{
1498
	global $txt, $db_type, $smcFunc, $incontext, $db_prefix, $db_passwd, $sourcedir, $db_character_set;
1499
1500
	$incontext['sub_template'] = 'admin_account';
1501
	$incontext['page_title'] = $txt['user_settings'];
1502
	$incontext['continue'] = 1;
1503
1504
	// Skipping?
1505
	if (!empty($_POST['skip']))
1506
		return true;
1507
1508
	// Need this to check whether we need the database password.
1509
	require(dirname(__FILE__) . '/Settings.php');
1510
	load_database();
1511
1512
	require_once($sourcedir . '/Subs-Auth.php');
1513
1514
	require_once($sourcedir . '/Subs.php');
1515
1516
	// Reload settings & set some global funcs
1517
	require_once($sourcedir . '/Load.php');
1518
	reloadSettings();
1519
1520
	// We need this to properly hash the password for Admin
1521
	$smcFunc['strtolower'] = function($string)
1522
	{
1523
		global $sourcedir;
1524
		if (function_exists('mb_strtolower'))
1525
			return mb_strtolower($string, 'UTF-8');
1526
		require_once($sourcedir . '/Subs-Charset.php');
1527
		return utf8_strtolower($string);
1528
	};
1529
1530
	if (!isset($_POST['username']))
1531
		$_POST['username'] = '';
1532
	if (!isset($_POST['email']))
1533
		$_POST['email'] = '';
1534
	if (!isset($_POST['server_email']))
1535
		$_POST['server_email'] = '';
1536
1537
	$incontext['username'] = htmlspecialchars($_POST['username']);
1538
	$incontext['email'] = htmlspecialchars($_POST['email']);
1539
	$incontext['server_email'] = htmlspecialchars($_POST['server_email']);
1540
1541
	$incontext['require_db_confirm'] = empty($db_type);
1542
1543
	// Only allow skipping if we think they already have an account setup.
1544
	$request = $smcFunc['db_query']('', '
1545
		SELECT id_member
1546
		FROM {db_prefix}members
1547
		WHERE id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0
1548
		LIMIT 1',
1549
		array(
1550
			'db_error_skip' => true,
1551
			'admin_group' => 1,
1552
		)
1553
	);
1554
	if ($smcFunc['db_num_rows']($request) != 0)
1555
		$incontext['skip'] = 1;
1556
	$smcFunc['db_free_result']($request);
1557
1558
	// Trying to create an account?
1559
	if (isset($_POST['password1']) && !empty($_POST['contbutt']))
1560
	{
1561
		// Wrong password?
1562
		if ($incontext['require_db_confirm'] && $_POST['password3'] != $db_passwd)
1563
		{
1564
			$incontext['error'] = $txt['error_db_connect'];
1565
			return false;
1566
		}
1567
		// Not matching passwords?
1568
		if ($_POST['password1'] != $_POST['password2'])
1569
		{
1570
			$incontext['error'] = $txt['error_user_settings_again_match'];
1571
			return false;
1572
		}
1573
		// No password?
1574
		if (strlen($_POST['password1']) < 4)
1575
		{
1576
			$incontext['error'] = $txt['error_user_settings_no_password'];
1577
			return false;
1578
		}
1579
		if (!file_exists($sourcedir . '/Subs.php'))
1580
		{
1581
			$incontext['error'] = sprintf($txt['error_sourcefile_missing'], 'Subs.php');
1582
			return false;
1583
		}
1584
1585
		// Update the webmaster's email?
1586
		if (!empty($_POST['server_email']) && (empty($webmaster_email) || $webmaster_email == '[email protected]'))
1587
			installer_updateSettingsFile(array('webmaster_email' => $_POST['server_email']));
1588
1589
		// Work out whether we're going to have dodgy characters and remove them.
1590
		$invalid_characters = preg_match('~[<>&"\'=\\\]~', $_POST['username']) != 0;
1591
		$_POST['username'] = preg_replace('~[<>&"\'=\\\]~', '', $_POST['username']);
1592
1593
		$result = $smcFunc['db_query']('', '
1594
			SELECT id_member, password_salt
1595
			FROM {db_prefix}members
1596
			WHERE member_name = {string:username} OR email_address = {string:email}
1597
			LIMIT 1',
1598
			array(
1599
				'username' => $_POST['username'],
1600
				'email' => $_POST['email'],
1601
				'db_error_skip' => true,
1602
			)
1603
		);
1604
		if ($smcFunc['db_num_rows']($result) != 0)
1605
		{
1606
			list ($incontext['member_id'], $incontext['member_salt']) = $smcFunc['db_fetch_row']($result);
1607
			$smcFunc['db_free_result']($result);
1608
1609
			$incontext['account_existed'] = $txt['error_user_settings_taken'];
1610
		}
1611
		elseif ($_POST['username'] == '' || strlen($_POST['username']) > 25)
1612
		{
1613
			// Try the previous step again.
1614
			$incontext['error'] = $_POST['username'] == '' ? $txt['error_username_left_empty'] : $txt['error_username_too_long'];
1615
			return false;
1616
		}
1617
		elseif ($invalid_characters || $_POST['username'] == '_' || $_POST['username'] == '|' || strpos($_POST['username'], '[code') !== false || strpos($_POST['username'], '[/code') !== false)
1618
		{
1619
			// Try the previous step again.
1620
			$incontext['error'] = $txt['error_invalid_characters_username'];
1621
			return false;
1622
		}
1623
		elseif (empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) || strlen($_POST['email']) > 255)
1624
		{
1625
			// One step back, this time fill out a proper admin email address.
1626
			$incontext['error'] = sprintf($txt['error_valid_admin_email_needed'], $_POST['username']);
1627
			return false;
1628
		}
1629
		elseif (empty($_POST['server_email']) || !filter_var($_POST['server_email'], FILTER_VALIDATE_EMAIL) || strlen($_POST['server_email']) > 255)
1630
		{
1631
			// One step back, this time fill out a proper admin email address.
1632
			$incontext['error'] = $txt['error_valid_server_email_needed'];
1633
			return false;
1634
		}
1635
		elseif ($_POST['username'] != '')
1636
		{
1637
			if (!is_callable('random_int'))
1638
				require_once('Sources/random_compat/random.php');
1639
1640
			$incontext['member_salt'] = bin2hex(random_bytes(16));
1641
1642
			// Format the username properly.
1643
			$_POST['username'] = preg_replace('~[\t\n\r\x0B\0\xA0]+~', ' ', $_POST['username']);
1644
			$ip = isset($_SERVER['REMOTE_ADDR']) ? substr($_SERVER['REMOTE_ADDR'], 0, 255) : '';
1645
1646
			$_POST['password1'] = hash_password($_POST['username'], $_POST['password1']);
1647
1648
			$incontext['member_id'] = $smcFunc['db_insert']('',
1649
				$db_prefix . 'members',
1650
				array(
1651
					'member_name' => 'string-25',
1652
					'real_name' => 'string-25',
1653
					'passwd' => 'string',
1654
					'email_address' => 'string',
1655
					'id_group' => 'int',
1656
					'posts' => 'int',
1657
					'date_registered' => 'int',
1658
					'password_salt' => 'string',
1659
					'lngfile' => 'string',
1660
					'personal_text' => 'string',
1661
					'avatar' => 'string',
1662
					'member_ip' => 'inet',
1663
					'member_ip2' => 'inet',
1664
					'buddy_list' => 'string',
1665
					'pm_ignore_list' => 'string',
1666
					'website_title' => 'string',
1667
					'website_url' => 'string',
1668
					'signature' => 'string',
1669
					'usertitle' => 'string',
1670
					'secret_question' => 'string',
1671
					'additional_groups' => 'string',
1672
					'ignore_boards' => 'string',
1673
				),
1674
				array(
1675
					$_POST['username'],
1676
					$_POST['username'],
1677
					$_POST['password1'],
1678
					$_POST['email'],
1679
					1,
1680
					0,
1681
					time(),
1682
					$incontext['member_salt'],
1683
					'',
1684
					'',
1685
					'',
1686
					$ip,
1687
					$ip,
1688
					'',
1689
					'',
1690
					'',
1691
					'',
1692
					'',
1693
					'',
1694
					'',
1695
					'',
1696
					'',
1697
				),
1698
				array('id_member'),
1699
				1
1700
			);
1701
		}
1702
1703
		// If we're here we're good.
1704
		return true;
1705
	}
1706
1707
	return false;
1708
}
1709
1710
// Final step, clean up and a complete message!
1711
function DeleteInstall()
1712
{
1713
	global $smcFunc, $db_character_set, $context, $txt, $incontext;
1714
	global $databases, $sourcedir, $modSettings, $user_info, $db_type, $boardurl;
1715
	global $auth_secret, $cookiename;
1716
1717
	$incontext['page_title'] = $txt['congratulations'];
1718
	$incontext['sub_template'] = 'delete_install';
1719
	$incontext['continue'] = 0;
1720
1721
	require(dirname(__FILE__) . '/Settings.php');
1722
	load_database();
1723
1724
	chdir(dirname(__FILE__));
1725
1726
	require_once($sourcedir . '/Errors.php');
1727
	require_once($sourcedir . '/Logging.php');
1728
	require_once($sourcedir . '/Subs.php');
1729
	require_once($sourcedir . '/Load.php');
1730
	require_once($sourcedir . '/Security.php');
1731
	require_once($sourcedir . '/Subs-Auth.php');
1732
1733
	// Reload settings & set some global funcs
1734
	reloadSettings();
1735
1736
	// Bring a warning over.
1737
	if (!empty($incontext['account_existed']))
1738
		$incontext['warning'] = $incontext['account_existed'];
1739
1740
	$smcFunc['db_query']('', '
1741
		SET NAMES {string:db_character_set}',
1742
		array(
1743
			'db_character_set' => $db_character_set,
1744
			'db_error_skip' => true,
1745
		)
1746
	);
1747
1748
	// As track stats is by default enabled let's add some activity.
1749
	$smcFunc['db_insert']('ignore',
1750
		'{db_prefix}log_activity',
1751
		array('date' => 'date', 'topics' => 'int', 'posts' => 'int', 'registers' => 'int'),
1752
		array(smf_strftime('%Y-%m-%d', time()), 1, 1, (!empty($incontext['member_id']) ? 1 : 0)),
1753
		array('date')
1754
	);
1755
1756
	// We're going to want our lovely $modSettings now.
1757
	$request = $smcFunc['db_query']('', '
1758
		SELECT variable, value
1759
		FROM {db_prefix}settings',
1760
		array(
1761
			'db_error_skip' => true,
1762
		)
1763
	);
1764
	// Only proceed if we can load the data.
1765
	if ($request)
1766
	{
1767
		while ($row = $smcFunc['db_fetch_row']($request))
1768
			$modSettings[$row[0]] = $row[1];
1769
		$smcFunc['db_free_result']($request);
1770
	}
1771
1772
	// Automatically log them in ;)
1773
	if (isset($incontext['member_id']) && isset($incontext['member_salt']))
1774
		setLoginCookie(3153600 * 60, $incontext['member_id'], hash_salt($_POST['password1'], $incontext['member_salt']));
1775
1776
	$result = $smcFunc['db_query']('', '
1777
		SELECT value
1778
		FROM {db_prefix}settings
1779
		WHERE variable = {string:db_sessions}',
1780
		array(
1781
			'db_sessions' => 'databaseSession_enable',
1782
			'db_error_skip' => true,
1783
		)
1784
	);
1785
	if ($smcFunc['db_num_rows']($result) != 0)
1786
		list ($db_sessions) = $smcFunc['db_fetch_row']($result);
1787
	$smcFunc['db_free_result']($result);
1788
1789
	if (empty($db_sessions))
1790
		$_SESSION['admin_time'] = time();
1791
	else
1792
	{
1793
		$_SERVER['HTTP_USER_AGENT'] = substr($_SERVER['HTTP_USER_AGENT'], 0, 211);
1794
1795
		$smcFunc['db_insert']('replace',
1796
			'{db_prefix}sessions',
1797
			array(
1798
				'session_id' => 'string', 'last_update' => 'int', 'data' => 'string',
1799
			),
1800
			array(
1801
				session_id(), time(), 'USER_AGENT|s:' . strlen($_SERVER['HTTP_USER_AGENT']) . ':"' . $_SERVER['HTTP_USER_AGENT'] . '";admin_time|i:' . time() . ';',
1802
			),
1803
			array('session_id')
1804
		);
1805
	}
1806
1807
	updateStats('member');
1808
	updateStats('message');
1809
	updateStats('topic');
1810
1811
	// This function is needed to do the updateStats('subject') call.
1812
	$smcFunc['strtolower'] = function($string)
1813
	{
1814
		global $sourcedir;
1815
		if (function_exists('mb_strtolower'))
1816
			return mb_strtolower($string, 'UTF-8');
1817
		require_once($sourcedir . '/Subs-Charset.php');
1818
		return utf8_strtolower($string);
1819
	};
1820
1821
	$request = $smcFunc['db_query']('', '
1822
		SELECT id_msg
1823
		FROM {db_prefix}messages
1824
		WHERE id_msg = 1
1825
			AND modified_time = 0
1826
		LIMIT 1',
1827
		array(
1828
			'db_error_skip' => true,
1829
		)
1830
	);
1831
	$context['utf8'] = true;
1832
	if ($smcFunc['db_num_rows']($request) > 0)
1833
		updateStats('subject', 1, htmlspecialchars($txt['default_topic_subject']));
1834
	$smcFunc['db_free_result']($request);
1835
1836
	// Now is the perfect time to fetch the SM files.
1837
	require_once($sourcedir . '/ScheduledTasks.php');
1838
	// Sanity check that they loaded earlier!
1839
	if (isset($modSettings['recycle_board']))
1840
	{
1841
		scheduled_fetchSMfiles(); // Now go get those files!
1842
1843
		// We've just installed!
1844
		$user_info['ip'] = $_SERVER['REMOTE_ADDR'];
1845
		$user_info['id'] = isset($incontext['member_id']) ? $incontext['member_id'] : 0;
1846
		logAction('install', array('version' => SMF_FULL_VERSION), 'admin');
1847
	}
1848
1849
	// Disable the legacy BBC by default for new installs
1850
	updateSettings(array(
1851
		'disabledBBC' => implode(',', $context['legacy_bbc']),
1852
	));
1853
1854
	// Some final context for the template.
1855
	$incontext['dir_still_writable'] = is_writable(dirname(__FILE__)) && substr(__FILE__, 1, 2) != ':\\';
1856
	$incontext['probably_delete_install'] = isset($_SESSION['installer_temp_ftp']) || is_writable(dirname(__FILE__)) || is_writable(__FILE__);
1857
1858
	// Update hash's cost to an appropriate setting
1859
	updateSettings(array(
1860
		'bcrypt_hash_cost' => hash_benchmark(),
1861
	));
1862
1863
	return false;
1864
}
1865
1866
function installer_updateSettingsFile($vars, $rebuild = false)
1867
{
1868
	global $sourcedir, $context, $db_character_set, $txt;
1869
1870
	$context['utf8'] = true;
1871
1872
	if (empty($sourcedir))
1873
	{
1874
		if (file_exists(dirname(__FILE__) . '/Sources') && is_dir(dirname(__FILE__) . '/Sources'))
1875
			$sourcedir = dirname(__FILE__) . '/Sources';
1876
		else
1877
			return false;
1878
	}
1879
1880
	if (!is_writeable(dirname(__FILE__) . '/Settings.php'))
1881
	{
1882
		@chmod(dirname(__FILE__) . '/Settings.php', 0777);
1883
1884
		if (!is_writeable(dirname(__FILE__) . '/Settings.php'))
1885
			return false;
1886
	}
1887
1888
	require_once($sourcedir . '/Subs.php');
1889
	require_once($sourcedir . '/Subs-Admin.php');
1890
1891
	return updateSettingsFile($vars, false, $rebuild);
1892
}
1893
1894
// Create an .htaccess file to prevent mod_security. SMF has filtering built-in.
1895
function fixModSecurity()
1896
{
1897
	$htaccess_addition = '
1898
<IfModule mod_security.c>
1899
	# Turn off mod_security filtering.  SMF is a big boy, it doesn\'t need its hands held.
1900
	SecFilterEngine Off
1901
1902
	# The below probably isn\'t needed, but better safe than sorry.
1903
	SecFilterScanPOST Off
1904
</IfModule>';
1905
1906
	if (!function_exists('apache_get_modules') || !in_array('mod_security', apache_get_modules()))
1907
		return true;
1908
	elseif (file_exists(dirname(__FILE__) . '/.htaccess') && is_writable(dirname(__FILE__) . '/.htaccess'))
1909
	{
1910
		$current_htaccess = implode('', file(dirname(__FILE__) . '/.htaccess'));
1911
1912
		// Only change something if mod_security hasn't been addressed yet.
1913
		if (strpos($current_htaccess, '<IfModule mod_security.c>') === false)
1914
		{
1915
			if ($ht_handle = fopen(dirname(__FILE__) . '/.htaccess', 'a'))
1916
			{
1917
				fwrite($ht_handle, $htaccess_addition);
1918
				fclose($ht_handle);
1919
				return true;
1920
			}
1921
			else
1922
				return false;
1923
		}
1924
		else
1925
			return true;
1926
	}
1927
	elseif (file_exists(dirname(__FILE__) . '/.htaccess'))
1928
		return strpos(implode('', file(dirname(__FILE__) . '/.htaccess')), '<IfModule mod_security.c>') !== false;
1929
	elseif (is_writable(dirname(__FILE__)))
1930
	{
1931
		if ($ht_handle = fopen(dirname(__FILE__) . '/.htaccess', 'w'))
1932
		{
1933
			fwrite($ht_handle, $htaccess_addition);
1934
			fclose($ht_handle);
1935
			return true;
1936
		}
1937
		else
1938
			return false;
1939
	}
1940
	else
1941
		return false;
1942
}
1943
1944
function template_install_above()
1945
{
1946
	global $incontext, $txt, $installurl;
1947
1948
	echo '<!DOCTYPE html>
1949
<html', $txt['lang_rtl'] == true ? ' dir="rtl"' : '', '>
1950
<head>
1951
	<meta charset="', isset($txt['lang_character_set']) ? $txt['lang_character_set'] : 'UTF-8', '">
1952
	<meta name="robots" content="noindex">
1953
	<title>', $txt['smf_installer'], '</title>
1954
	<link rel="stylesheet" href="Themes/default/css/index.css">
1955
	<link rel="stylesheet" href="Themes/default/css/install.css">
1956
	', $txt['lang_rtl'] == true ? '<link rel="stylesheet" href="Themes/default/css/rtl.css">' : '', '
1957
1958
	<script src="Themes/default/scripts/jquery-' . JQUERY_VERSION . '.min.js"></script>
1959
	<script src="Themes/default/scripts/script.js"></script>
1960
</head>
1961
<body>
1962
	<div id="footerfix">
1963
	<div id="header">
1964
		<h1 class="forumtitle">', $txt['smf_installer'], '</h1>
1965
		<img id="smflogo" src="Themes/default/images/smflogo.svg" alt="Simple Machines Forum" title="Simple Machines Forum">
1966
	</div>
1967
	<div id="wrapper">';
1968
1969
	// Have we got a language drop down - if so do it on the first step only.
1970
	if (!empty($incontext['detected_languages']) && count($incontext['detected_languages']) > 1 && $incontext['current_step'] == 0)
1971
	{
1972
		echo '
1973
		<div id="upper_section">
1974
			<div id="inner_section">
1975
				<div id="inner_wrap">
1976
					<div class="news">
1977
						<form action="', $installurl, '" method="get">
1978
							<label for="installer_language">', $txt['installer_language'], ':</label>
1979
							<select id="installer_language" name="lang_file" onchange="location.href = \'', $installurl, '?lang_file=\' + this.options[this.selectedIndex].value;">';
1980
1981
		foreach ($incontext['detected_languages'] as $lang => $name)
1982
			echo '
1983
								<option', isset($_SESSION['installer_temp_lang']) && $_SESSION['installer_temp_lang'] == $lang ? ' selected' : '', ' value="', $lang, '">', $name, '</option>';
1984
1985
		echo '
1986
							</select>
1987
							<noscript><input type="submit" value="', $txt['installer_language_set'], '" class="button"></noscript>
1988
						</form>
1989
					</div><!-- .news -->
1990
					<hr class="clear">
1991
				</div><!-- #inner_wrap -->
1992
			</div><!-- #inner_section -->
1993
		</div><!-- #upper_section -->';
1994
	}
1995
1996
	echo '
1997
		<div id="content_section">
1998
			<div id="main_content_section">
1999
				<div id="main_steps">
2000
					<h2>', $txt['upgrade_progress'], '</h2>
2001
					<ul class="steps_list">';
2002
2003
	foreach ($incontext['steps'] as $num => $step)
2004
		echo '
2005
						<li', $num == $incontext['current_step'] ? ' class="stepcurrent"' : '', '>
2006
							', $txt['upgrade_step'], ' ', $step[0], ': ', $step[1], '
2007
						</li>';
2008
2009
	echo '
2010
					</ul>
2011
				</div>
2012
				<div id="install_progress">
2013
					<div id="progress_bar" class="progress_bar progress_green">
2014
						<h3>'. $txt['upgrade_overall_progress'], '</h3>
2015
						<span id="overall_text">', $incontext['overall_percent'], '%</span>
2016
						<div id="overall_progress" class="bar" style="width: ', $incontext['overall_percent'], '%;"></div>
2017
					</div>
2018
				</div>
2019
				<div id="main_screen" class="clear">
2020
					<h2>', $incontext['page_title'], '</h2>
2021
					<div class="panel">';
2022
}
2023
2024
function template_install_below()
2025
{
2026
	global $incontext, $txt;
2027
2028
	if (!empty($incontext['continue']) || !empty($incontext['skip']))
2029
	{
2030
		echo '
2031
							<div class="floatright">';
2032
2033
		if (!empty($incontext['continue']))
2034
			echo '
2035
								<input type="submit" id="contbutt" name="contbutt" value="', $txt['upgrade_continue'], '" onclick="return submitThisOnce(this);" class="button">';
2036
		if (!empty($incontext['skip']))
2037
			echo '
2038
								<input type="submit" id="skip" name="skip" value="', $txt['upgrade_skip'], '" onclick="return submitThisOnce(this);" class="button">';
2039
		echo '
2040
							</div>';
2041
	}
2042
2043
	// Show the closing form tag and other data only if not in the last step
2044
	if (count($incontext['steps']) - 1 !== (int) $incontext['current_step'])
2045
		echo '
2046
						</form>';
2047
2048
	echo '
2049
					</div><!-- .panel -->
2050
				</div><!-- #main_screen -->
2051
			</div><!-- #main_content_section -->
2052
		</div><!-- #content_section -->
2053
	</div><!-- #wrapper -->
2054
	</div><!-- #footerfix -->
2055
	<div id="footer">
2056
		<ul>
2057
			<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>
2058
		</ul>
2059
	</div>
2060
</body>
2061
</html>';
2062
}
2063
2064
// Welcome them to the wonderful world of SMF!
2065
function template_welcome_message()
2066
{
2067
	global $incontext, $txt;
2068
2069
	echo '
2070
	<script src="https://www.simplemachines.org/smf/current-version.js?version=' . urlencode(SMF_VERSION) . '"></script>
2071
	<form action="', $incontext['form_url'], '" method="post">
2072
		<p>', sprintf($txt['install_welcome_desc'], SMF_VERSION), '</p>
2073
		<div id="version_warning" class="noticebox hidden">
2074
			<h3>', $txt['error_warning_notice'], '</h3>
2075
			', sprintf($txt['error_script_outdated'], '<em id="smfVersion" style="white-space: nowrap;">??</em>', '<em id="yourVersion" style="white-space: nowrap;">' . SMF_VERSION . '</em>'), '
2076
		</div>';
2077
2078
	// Show the warnings, or not.
2079
	if (template_warning_divs())
2080
		echo '
2081
		<h3>', $txt['install_all_lovely'], '</h3>';
2082
2083
	// Say we want the continue button!
2084
	if (empty($incontext['error']))
2085
		$incontext['continue'] = 1;
2086
2087
	// For the latest version stuff.
2088
	echo '
2089
		<script>
2090
			// Latest version?
2091
			function smfCurrentVersion()
2092
			{
2093
				var smfVer, yourVer;
2094
2095
				if (!(\'smfVersion\' in window))
2096
					return;
2097
2098
				window.smfVersion = window.smfVersion.replace(/SMF\s?/g, \'\');
2099
2100
				smfVer = document.getElementById("smfVersion");
2101
				yourVer = document.getElementById("yourVersion");
2102
2103
				setInnerHTML(smfVer, window.smfVersion);
2104
2105
				var currentVersion = getInnerHTML(yourVer);
2106
				if (currentVersion < window.smfVersion)
2107
					document.getElementById(\'version_warning\').classList.remove(\'hidden\');
2108
			}
2109
			addLoadEvent(smfCurrentVersion);
2110
		</script>';
2111
}
2112
2113
// A shortcut for any warning stuff.
2114
function template_warning_divs()
2115
{
2116
	global $txt, $incontext;
2117
2118
	// Errors are very serious..
2119
	if (!empty($incontext['error']))
2120
		echo '
2121
		<div class="errorbox">
2122
			<h3>', $txt['upgrade_critical_error'], '</h3>
2123
			', $incontext['error'], '
2124
		</div>';
2125
	// A warning message?
2126
	elseif (!empty($incontext['warning']))
2127
		echo '
2128
		<div class="errorbox">
2129
			<h3>', $txt['upgrade_warning'], '</h3>
2130
			', $incontext['warning'], '
2131
		</div>';
2132
2133
	return empty($incontext['error']) && empty($incontext['warning']);
2134
}
2135
2136
function template_chmod_files()
2137
{
2138
	global $txt, $incontext;
2139
2140
	echo '
2141
		<p>', $txt['ftp_setup_why_info'], '</p>
2142
		<ul class="error_content">
2143
			<li>', implode('</li>
2144
			<li>', $incontext['failed_files']), '</li>
2145
		</ul>';
2146
2147
	if (isset($incontext['systemos'], $incontext['detected_path']) && $incontext['systemos'] == 'linux')
2148
		echo '
2149
		<hr>
2150
		<p>', $txt['chmod_linux_info'], '</p>
2151
		<samp># chmod a+w ', implode(' ' . $incontext['detected_path'] . '/', $incontext['failed_files']), '</samp>';
2152
2153
	// This is serious!
2154
	if (!template_warning_divs())
2155
		return;
2156
2157
	echo '
2158
		<hr>
2159
		<p>', $txt['ftp_setup_info'], '</p>';
2160
2161
	if (!empty($incontext['ftp_errors']))
2162
		echo '
2163
		<div class="error_message">
2164
			', $txt['error_ftp_no_connect'], '<br><br>
2165
			<code>', implode('<br>', $incontext['ftp_errors']), '</code>
2166
		</div>';
2167
2168
	echo '
2169
		<form action="', $incontext['form_url'], '" method="post">
2170
			<dl class="settings">
2171
				<dt>
2172
					<label for="ftp_server">', $txt['ftp_server'], ':</label>
2173
				</dt>
2174
				<dd>
2175
					<div class="floatright">
2176
						<label for="ftp_port" class="textbox"><strong>', $txt['ftp_port'], ':&nbsp;</strong></label>
2177
						<input type="text" size="3" name="ftp_port" id="ftp_port" value="', $incontext['ftp']['port'], '">
2178
					</div>
2179
					<input type="text" size="30" name="ftp_server" id="ftp_server" value="', $incontext['ftp']['server'], '">
2180
					<div class="smalltext block">', $txt['ftp_server_info'], '</div>
2181
				</dd>
2182
				<dt>
2183
					<label for="ftp_username">', $txt['ftp_username'], ':</label>
2184
				</dt>
2185
				<dd>
2186
					<input type="text" size="30" name="ftp_username" id="ftp_username" value="', $incontext['ftp']['username'], '">
2187
					<div class="smalltext block">', $txt['ftp_username_info'], '</div>
2188
				</dd>
2189
				<dt>
2190
					<label for="ftp_password">', $txt['ftp_password'], ':</label>
2191
				</dt>
2192
				<dd>
2193
					<input type="password" size="30" name="ftp_password" id="ftp_password">
2194
					<div class="smalltext block">', $txt['ftp_password_info'], '</div>
2195
				</dd>
2196
				<dt>
2197
					<label for="ftp_path">', $txt['ftp_path'], ':</label>
2198
				</dt>
2199
				<dd>
2200
					<input type="text" size="30" name="ftp_path" id="ftp_path" value="', $incontext['ftp']['path'], '">
2201
					<div class="smalltext block">', $incontext['ftp']['path_msg'], '</div>
2202
				</dd>
2203
			</dl>
2204
			<div class="righttext buttons">
2205
				<input type="submit" value="', $txt['ftp_connect'], '" onclick="return submitThisOnce(this);" class="button">
2206
			</div>
2207
		</form>
2208
		<a href="', $incontext['form_url'], '">', $txt['error_message_click'], '</a> ', $txt['ftp_setup_again'];
2209
}
2210
2211
// Get the database settings prepared.
2212
function template_database_settings()
2213
{
2214
	global $incontext, $txt;
2215
2216
	echo '
2217
	<form action="', $incontext['form_url'], '" method="post">
2218
		<p>', $txt['db_settings_info'], '</p>';
2219
2220
	template_warning_divs();
2221
2222
	echo '
2223
		<dl class="settings">';
2224
2225
	// More than one database type?
2226
	if (count($incontext['supported_databases']) > 1)
2227
	{
2228
		echo '
2229
			<dt>
2230
				<label for="db_type_input">', $txt['db_settings_type'], ':</label>
2231
			</dt>
2232
			<dd>
2233
				<select name="db_type" id="db_type_input" onchange="toggleDBInput();">';
2234
2235
		foreach ($incontext['supported_databases'] as $key => $db)
2236
			echo '
2237
					<option value="', $key, '"', isset($_POST['db_type']) && $_POST['db_type'] == $key ? ' selected' : '', '>', $db['name'], '</option>';
2238
2239
		echo '
2240
				</select>
2241
				<div class="smalltext">', $txt['db_settings_type_info'], '</div>
2242
			</dd>';
2243
	}
2244
	else
2245
	{
2246
		echo '
2247
			<dd>
2248
				<input type="hidden" name="db_type" value="', $incontext['db']['type'], '">
2249
			</dd>';
2250
	}
2251
2252
	echo '
2253
			<dt>
2254
				<label for="db_server_input">', $txt['db_settings_server'], ':</label>
2255
			</dt>
2256
			<dd>
2257
				<input type="text" name="db_server" id="db_server_input" value="', $incontext['db']['server'], '" size="30">
2258
				<div class="smalltext">', $txt['db_settings_server_info'], '</div>
2259
			</dd>
2260
			<dt>
2261
				<label for="db_port_input">', $txt['db_settings_port'], ':</label>
2262
			</dt>
2263
			<dd>
2264
				<input type="text" name="db_port" id="db_port_input" value="', $incontext['db']['port'], '">
2265
				<div class="smalltext">', $txt['db_settings_port_info'], '</div>
2266
			</dd>
2267
			<dt>
2268
				<label for="db_user_input">', $txt['db_settings_username'], ':</label>
2269
			</dt>
2270
			<dd>
2271
				<input type="text" name="db_user" id="db_user_input" value="', $incontext['db']['user'], '" size="30">
2272
				<div class="smalltext">', $txt['db_settings_username_info'], '</div>
2273
			</dd>
2274
			<dt>
2275
				<label for="db_passwd_input">', $txt['db_settings_password'], ':</label>
2276
			</dt>
2277
			<dd>
2278
				<input type="password" name="db_passwd" id="db_passwd_input" value="', $incontext['db']['pass'], '" size="30">
2279
				<div class="smalltext">', $txt['db_settings_password_info'], '</div>
2280
			</dd>
2281
			<dt>
2282
				<label for="db_name_input">', $txt['db_settings_database'], ':</label>
2283
			</dt>
2284
			<dd>
2285
				<input type="text" name="db_name" id="db_name_input" value="', empty($incontext['db']['name']) ? 'smf' : $incontext['db']['name'], '" size="30">
2286
				<div class="smalltext">
2287
					', $txt['db_settings_database_info'], '
2288
					<span id="db_name_info_warning">', $txt['db_settings_database_info_note'], '</span>
2289
				</div>
2290
			</dd>
2291
			<dt>
2292
				<label for="db_prefix_input">', $txt['db_settings_prefix'], ':</label>
2293
			</dt>
2294
			<dd>
2295
				<input type="text" name="db_prefix" id="db_prefix_input" value="', $incontext['db']['prefix'], '" size="30">
2296
				<div class="smalltext">', $txt['db_settings_prefix_info'], '</div>
2297
			</dd>
2298
		</dl>';
2299
2300
	// Toggles a warning related to db names in PostgreSQL
2301
	echo '
2302
		<script>
2303
			function toggleDBInput()
2304
			{
2305
				if (document.getElementById(\'db_type_input\').value == \'postgresql\')
2306
					document.getElementById(\'db_name_info_warning\').classList.add(\'hidden\');
2307
				else
2308
					document.getElementById(\'db_name_info_warning\').classList.remove(\'hidden\');
2309
			}
2310
			toggleDBInput();
2311
		</script>';
2312
}
2313
2314
// Stick in their forum settings.
2315
function template_forum_settings()
2316
{
2317
	global $incontext, $txt;
2318
2319
	echo '
2320
	<form action="', $incontext['form_url'], '" method="post">
2321
		<h3>', $txt['install_settings_info'], '</h3>';
2322
2323
	template_warning_divs();
2324
2325
	echo '
2326
		<dl class="settings">
2327
			<dt>
2328
				<label for="mbname_input">', $txt['install_settings_name'], ':</label>
2329
			</dt>
2330
			<dd>
2331
				<input type="text" name="mbname" id="mbname_input" value="', $txt['install_settings_name_default'], '" size="65">
2332
				<div class="smalltext">', $txt['install_settings_name_info'], '</div>
2333
			</dd>
2334
			<dt>
2335
				<label for="boardurl_input">', $txt['install_settings_url'], ':</label>
2336
			</dt>
2337
			<dd>
2338
				<input type="text" name="boardurl" id="boardurl_input" value="', $incontext['detected_url'], '" size="65">
2339
				<div class="smalltext">', $txt['install_settings_url_info'], '</div>
2340
			</dd>
2341
			<dt>
2342
				<label for="reg_mode">', $txt['install_settings_reg_mode'], ':</label>
2343
			</dt>
2344
			<dd>
2345
				<select name="reg_mode" id="reg_mode">
2346
					<optgroup label="', $txt['install_settings_reg_modes'], ':">
2347
						<option value="0" selected>', $txt['install_settings_reg_immediate'], '</option>
2348
						<option value="1">', $txt['install_settings_reg_email'], '</option>
2349
						<option value="2">', $txt['install_settings_reg_admin'], '</option>
2350
						<option value="3">', $txt['install_settings_reg_disabled'], '</option>
2351
					</optgroup>
2352
				</select>
2353
				<div class="smalltext">', $txt['install_settings_reg_mode_info'], '</div>
2354
			</dd>
2355
			<dt>', $txt['install_settings_compress'], ':</dt>
2356
			<dd>
2357
				<input type="checkbox" name="compress" id="compress_check" checked>
2358
				<label for="compress_check">', $txt['install_settings_compress_title'], '</label>
2359
				<div class="smalltext">', $txt['install_settings_compress_info'], '</div>
2360
			</dd>
2361
			<dt>', $txt['install_settings_dbsession'], ':</dt>
2362
			<dd>
2363
				<input type="checkbox" name="dbsession" id="dbsession_check" checked>
2364
				<label for="dbsession_check">', $txt['install_settings_dbsession_title'], '</label>
2365
				<div class="smalltext">', $incontext['test_dbsession'] ? $txt['install_settings_dbsession_info1'] : $txt['install_settings_dbsession_info2'], '</div>
2366
			</dd>
2367
			<dt>', $txt['install_settings_stats'], ':</dt>
2368
			<dd>
2369
				<input type="checkbox" name="stats" id="stats_check" checked="checked">
2370
				<label for="stats_check">', $txt['install_settings_stats_title'], '</label>
2371
				<div class="smalltext">', $txt['install_settings_stats_info'], '</div>
2372
			</dd>
2373
			<dt>', $txt['force_ssl'], ':</dt>
2374
			<dd>
2375
				<input type="checkbox" name="force_ssl" id="force_ssl"', $incontext['ssl_chkbx_checked'] ? ' checked' : '',
2376
					$incontext['ssl_chkbx_protected'] ? ' disabled' : '', '>
2377
				<label for="force_ssl">', $txt['force_ssl_label'], '</label>
2378
				<div class="smalltext"><strong>', $txt['force_ssl_info'], '</strong></div>
2379
			</dd>
2380
		</dl>';
2381
}
2382
2383
// Show results of the database population.
2384
function template_populate_database()
2385
{
2386
	global $incontext, $txt;
2387
2388
	echo '
2389
	<form action="', $incontext['form_url'], '" method="post">
2390
		<p>', !empty($incontext['was_refresh']) ? $txt['user_refresh_install_desc'] : $txt['db_populate_info'], '</p>';
2391
2392
	if (!empty($incontext['sql_results']))
2393
	{
2394
		echo '
2395
		<ul>
2396
			<li>', implode('</li><li>', $incontext['sql_results']), '</li>
2397
		</ul>';
2398
	}
2399
2400
	if (!empty($incontext['failures']))
2401
	{
2402
		echo '
2403
		<div class="red">', $txt['error_db_queries'], '</div>
2404
		<ul>';
2405
2406
		foreach ($incontext['failures'] as $line => $fail)
2407
			echo '
2408
			<li><strong>', $txt['error_db_queries_line'], $line + 1, ':</strong> ', nl2br(htmlspecialchars($fail)), '</li>';
2409
2410
		echo '
2411
		</ul>';
2412
	}
2413
2414
	echo '
2415
		<p>', $txt['db_populate_info2'], '</p>';
2416
2417
	template_warning_divs();
2418
2419
	echo '
2420
		<input type="hidden" name="pop_done" value="1">';
2421
}
2422
2423
// Create the admin account.
2424
function template_admin_account()
2425
{
2426
	global $incontext, $txt;
2427
2428
	echo '
2429
	<form action="', $incontext['form_url'], '" method="post">
2430
		<p>', $txt['user_settings_info'], '</p>';
2431
2432
	template_warning_divs();
2433
2434
	echo '
2435
		<dl class="settings">
2436
			<dt>
2437
				<label for="username">', $txt['user_settings_username'], ':</label>
2438
			</dt>
2439
			<dd>
2440
				<input type="text" name="username" id="username" value="', $incontext['username'], '" size="40">
2441
				<div class="smalltext">', $txt['user_settings_username_info'], '</div>
2442
			</dd>
2443
			<dt>
2444
				<label for="password1">', $txt['user_settings_password'], ':</label>
2445
			</dt>
2446
			<dd>
2447
				<input type="password" name="password1" id="password1" size="40">
2448
				<div class="smalltext">', $txt['user_settings_password_info'], '</div>
2449
			</dd>
2450
			<dt>
2451
				<label for="password2">', $txt['user_settings_again'], ':</label>
2452
			</dt>
2453
			<dd>
2454
				<input type="password" name="password2" id="password2" size="40">
2455
				<div class="smalltext">', $txt['user_settings_again_info'], '</div>
2456
			</dd>
2457
			<dt>
2458
				<label for="email">', $txt['user_settings_admin_email'], ':</label>
2459
			</dt>
2460
			<dd>
2461
				<input type="email" name="email" id="email" value="', $incontext['email'], '" size="40">
2462
				<div class="smalltext">', $txt['user_settings_admin_email_info'], '</div>
2463
			</dd>
2464
			<dt>
2465
				<label for="server_email">', $txt['user_settings_server_email'], ':</label>
2466
			</dt>
2467
			<dd>
2468
				<input type="text" name="server_email" id="server_email" value="', $incontext['server_email'], '" size="40">
2469
				<div class="smalltext">', $txt['user_settings_server_email_info'], '</div>
2470
			</dd>
2471
		</dl>';
2472
2473
	if ($incontext['require_db_confirm'])
2474
		echo '
2475
		<h2>', $txt['user_settings_database'], '</h2>
2476
		<p>', $txt['user_settings_database_info'], '</p>
2477
2478
		<div class="lefttext">
2479
			<input type="password" name="password3" size="30">
2480
		</div>';
2481
}
2482
2483
// Tell them it's done, and to delete.
2484
function template_delete_install()
2485
{
2486
	global $incontext, $installurl, $txt, $boardurl;
2487
2488
	echo '
2489
		<p>', $txt['congratulations_help'], '</p>';
2490
2491
	template_warning_divs();
2492
2493
	// Install directory still writable?
2494
	if ($incontext['dir_still_writable'])
2495
		echo '
2496
		<p><em>', $txt['still_writable'], '</em></p>';
2497
2498
	// Don't show the box if it's like 99% sure it won't work :P.
2499
	if ($incontext['probably_delete_install'])
2500
		echo '
2501
		<label>
2502
			<input type="checkbox" id="delete_self" onclick="doTheDelete();">
2503
			<strong>', $txt['delete_installer'], !isset($_SESSION['installer_temp_ftp']) ? ' ' . $txt['delete_installer_maybe'] : '', '</strong>
2504
		</label>
2505
		<script>
2506
			function doTheDelete()
2507
			{
2508
				var theCheck = document.getElementById ? document.getElementById("delete_self") : document.all.delete_self;
2509
				var tempImage = new Image();
2510
2511
				tempImage.src = "', $installurl, '?delete=1&ts_" + (new Date().getTime());
2512
				tempImage.width = 0;
2513
				theCheck.disabled = true;
2514
			}
2515
		</script>';
2516
2517
	echo '
2518
		<p>', sprintf($txt['go_to_your_forum'], $boardurl . '/index.php'), '</p>
2519
		<br>
2520
		', $txt['good_luck'];
2521
}
2522
2523
?>