Passed
Pull Request — release-2.1 (#5772)
by Fran
04:08
created

fixModSecurity()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 25
c 0
b 0
f 0
nop 0
dl 0
loc 47
rs 7.6666
nc 8

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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