-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.php
2604 lines (2223 loc) · 86.4 KB
/
install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines http://www.simplemachines.org
* @copyright 2011 Simple Machines
* @license http://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.0.15
*/
$GLOBALS['current_smf_version'] = '2.0.15';
$GLOBALS['db_script_version'] = '2-0';
$GLOBALS['required_php_version'] = '5.4.0';
// Don't have PHP support, do you?
// ><html dir="ltr"><head><title>Error!</title></head><body>Sorry, this installer requires PHP!<div style="display: none;">
// Database info.
$databases = array(
'mysql' => array(
'name' => 'MySQL',
'version' => '4.0.18',
'version_check' => 'return min(mysqli_get_server_info($db_connection), mysqli_get_client_info());',
'supported' => function_exists('mysqli_connect'),
'default_user' => 'mysqli.default_user',
'default_password' => 'mysqli.default_password',
'default_host' => 'mysqli.default_host',
'default_port' => 'mysqli.default_port',
'utf8_support' => true,
'utf8_version' => '4.1.0',
'utf8_version_check' => 'return mysqli_get_server_info($db_connection);',
'utf8_default' => true,
'utf8_required' => false,
'alter_support' => true,
'validate_prefix' => create_function('&$value', '
$value = preg_replace(\'~[^A-Za-z0-9_\$]~\', \'\', $value);
return true;
'),
),
'postgresql' => array(
'name' => 'PostgreSQL',
'version' => '8.0',
'function_check' => 'pg_connect',
'version_check' => '$request = pg_query(\'SELECT version()\'); list ($version) = pg_fetch_row($request); list($pgl, $version) = explode(" ", $version); return $version;',
'supported' => function_exists('pg_connect'),
'always_has_db' => true,
'utf8_default' => true,
'utf8_required' => true,
'utf8_support' => true,
'utf8_version' => '8.0',
'utf8_version_check' => '$request = pg_query(\'SELECT version()\'); list ($version) = pg_fetch_row($request); list($pgl, $version) = explode(" ", $version); return $version;',
'validate_prefix' => create_function('&$value', '
$value = preg_replace(\'~[^A-Za-z0-9_\$]~\', \'\', $value);
// Is it reserved?
if ($value == \'pg_\')
return $txt[\'error_db_prefix_reserved\'];
// Is the prefix numeric?
if (preg_match(\'~^\d~\', $value))
return $txt[\'error_db_prefix_numeric\'];
return true;
'),
),
'sqlite' => array(
'name' => 'SQLite',
'version' => '1',
'function_check' => 'sqlite_open',
'version_check' => 'return 1;',
'supported' => function_exists('sqlite_open'),
'always_has_db' => true,
'utf8_default' => true,
'utf8_required' => true,
'validate_prefix' => create_function('&$value', '
global $incontext, $txt;
$value = preg_replace(\'~[^A-Za-z0-9_\$]~\', \'\', $value);
// Is it reserved?
if ($value == \'sqlite_\')
return $txt[\'error_db_prefix_reserved\'];
// Is the prefix numeric?
if (preg_match(\'~^\d~\', $value))
return $txt[\'error_db_prefix_numeric\'];
return true;
'),
),
);
// Initialize everything and load the language files.
initialize_inputs();
load_lang_file();
// This is what we are.
$installurl = $_SERVER['PHP_SELF'];
// This is where SMF is.
$smfsite = 'http://www.simplemachines.org/smf';
// All the steps in detail.
// Number,Name,Function,Progress Weight.
$incontext['steps'] = array(
0 => array(1, $txt['install_step_welcome'], 'Welcome', 0),
1 => array(2, $txt['install_step_writable'], 'CheckFilesWritable', 10),
2 => array(3, $txt['install_step_databaseset'], 'DatabaseSettings', 15),
3 => array(4, $txt['install_step_forum'], 'ForumSettings', 40),
4 => array(5, $txt['install_step_databasechange'], 'DatabasePopulation', 15),
5 => array(6, $txt['install_step_admin'], 'AdminAccount', 20),
6 => array(7, $txt['install_step_delete'], 'DeleteInstall', 0),
);
// Default title...
$incontext['page_title'] = $txt['smf_installer'];
// What step are we on?
$incontext['current_step'] = isset($_GET['step']) ? (int) $_GET['step'] : 0;
// Loop through all the steps doing each one as required.
$incontext['overall_percent'] = 0;
foreach ($incontext['steps'] as $num => $step)
{
if ($num >= $incontext['current_step'])
{
// The current weight of this step in terms of overall progress.
$incontext['step_weight'] = $step[3];
// Make sure we reset the skip button.
$incontext['skip'] = false;
// Call the step and if it returns false that means pause!
if (function_exists($step[2]) && $step[2]() === false)
break;
elseif (function_exists($step[2]))
$incontext['current_step']++;
// No warnings pass on.
$incontext['warning'] = '';
}
$incontext['overall_percent'] += $step[3];
}
// Actually do the template stuff.
installExit();
function initialize_inputs()
{
global $databases, $incontext;
// Just so people using older versions of PHP aren't left in the cold.
if (!isset($_SERVER['PHP_SELF']))
$_SERVER['PHP_SELF'] = isset($GLOBALS['HTTP_SERVER_VARS']['PHP_SELF']) ? $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'] : 'install.php';
// Turn off magic quotes runtime and enable error reporting.
if (function_exists('set_magic_quotes_runtime'))
@set_magic_quotes_runtime(0);
error_reporting(E_ALL);
// Fun. Low PHP version...
if (!isset($_GET))
{
$GLOBALS['_GET']['step'] = 0;
return;
}
if (!isset($_GET['obgz']))
{
ob_start();
if (@ini_get('session.save_handler') == 'user')
@ini_set('session.save_handler', 'files');
if (function_exists('session_start'))
@session_start();
}
else
{
ob_start('ob_gzhandler');
if (@ini_get('session.save_handler') == 'user')
@ini_set('session.save_handler', 'files');
session_start();
if (!headers_sent())
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>', htmlspecialchars($_GET['pass_string']), '</title>
</head>
<body style="background-color: #d4d4d4; margin-top: 16%; text-align: center; font-size: 16pt;">
<strong>', htmlspecialchars($_GET['pass_string']), '</strong>
</body>
</html>';
exit;
}
// Are we calling the backup css file?
if (isset($_GET['infile_css']))
{
header('Content-Type: text/css');
template_css();
exit;
}
// Anybody home?
if (!isset($_GET['xml']))
{
$incontext['remote_files_available'] = false;
$test = @fsockopen('www.simplemachines.org', 80, $errno, $errstr, 1);
if ($test)
$incontext['remote_files_available'] = true;
@fclose($test);
}
// Add slashes, as long as they aren't already being added.
if (!function_exists('get_magic_quotes_gpc') || @get_magic_quotes_gpc() == 0)
foreach ($_POST as $k => $v)
$_POST[$k] = addslashes($v);
// This is really quite simple; if ?delete is on the URL, delete the installer...
if (isset($_GET['delete']))
{
if (isset($_SESSION['installer_temp_ftp']))
{
$ftp = new ftp_connection($_SESSION['installer_temp_ftp']['server'], $_SESSION['installer_temp_ftp']['port'], $_SESSION['installer_temp_ftp']['username'], $_SESSION['installer_temp_ftp']['password']);
$ftp->chdir($_SESSION['installer_temp_ftp']['path']);
$ftp->unlink('install.php');
$ftp->unlink('webinstall.php');
foreach ($databases as $key => $dummy)
$ftp->unlink('install_' . $GLOBALS['db_script_version'] . '_' . $key . '.sql');
$ftp->close();
unset($_SESSION['installer_temp_ftp']);
}
else
{
@unlink(__FILE__);
@unlink(dirname(__FILE__) . '/webinstall.php');
foreach ($databases as $key => $dummy)
@unlink(dirname(__FILE__) . '/install_' . $GLOBALS['db_script_version'] . '_' . $key . '.sql');
}
// Now just redirect to a blank.gif...
header('Location: http://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']) . dirname($_SERVER['PHP_SELF']) . '/Themes/default/images/blank.gif');
exit;
}
// PHP 5 might cry if we don't do this now.
if (function_exists('date_default_timezone_set'))
{
$server_offset = @mktime(0, 0, 0, 1, 1, 1970);
date_default_timezone_set('Etc/GMT' . ($server_offset > 0 ? '+' : '') . ($server_offset / 3600));
}
// Force an integer step, defaulting to 0.
$_GET['step'] = (int) @$_GET['step'];
}
// Load the list of language files, and the current language file.
function load_lang_file()
{
global $txt, $incontext;
$incontext['detected_languages'] = array();
// Make sure the languages directory actually exists.
if (file_exists(dirname(__FILE__) . '/Themes/default/languages'))
{
// Find all the "Install" language files in the directory.
$dir = dir(dirname(__FILE__) . '/Themes/default/languages');
while ($entry = $dir->read())
{
if (substr($entry, 0, 8) == 'Install.' && substr($entry, -4) == '.php')
$incontext['detected_languages'][$entry] = ucfirst(substr($entry, 8, strlen($entry) - 12));
}
$dir->close();
}
// Didn't find any, show an error message!
if (empty($incontext['detected_languages']))
{
// Let's not cache this message, eh?
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache');
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SMF Installer: Error!</title>
</head>
<body style="font-family: sans-serif;"><div style="width: 600px;">
<h1 style="font-size: 14pt;">A critical error has occurred.</h1>
<p>This installer was unable to find the installer\'s language file or files. They should be found under:</p>
<div style="margin: 1ex; font-family: monospace; font-weight: bold;">', dirname($_SERVER['PHP_SELF']) != '/' ? dirname($_SERVER['PHP_SELF']) : '', '/Themes/default/languages</div>
<p>In some cases, FTP clients do not properly upload files with this many folders. Please double check to make sure you <span style="font-weight: 600;">have uploaded all the files in the distribution</span>.</p>
<p>If that doesn\'t help, please make sure this install.php file is in the same place as the Themes folder.</p>
<p>If you continue to get this error message, feel free to <a href="http://support.simplemachines.org/">look to us for support</a>.</p>
</div></body>
</html>';
die;
}
// Override the language file?
if (isset($_GET['lang_file']))
$_SESSION['installer_temp_lang'] = $_GET['lang_file'];
elseif (isset($GLOBALS['HTTP_GET_VARS']['lang_file']))
$_SESSION['installer_temp_lang'] = $GLOBALS['HTTP_GET_VARS']['lang_file'];
// Make sure it exists, if it doesn't reset it.
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']))
{
// Use the first one...
list ($_SESSION['installer_temp_lang']) = array_keys($incontext['detected_languages']);
// If we have english and some other language, use the other language. We Americans hate english :P.
if ($_SESSION['installer_temp_lang'] == 'Install.english.php' && count($incontext['detected_languages']) > 1)
list (, $_SESSION['installer_temp_lang']) = array_keys($incontext['detected_languages']);
}
// And now include the actual language file itself.
require_once(dirname(__FILE__) . '/Themes/default/languages/' . $_SESSION['installer_temp_lang']);
}
// This handy function loads some settings and the like.
function load_database()
{
global $db_prefix, $db_connection, $db_character_set, $sourcedir, $language;
global $smcFunc, $mbname, $scripturl, $boardurl, $modSettings, $db_type, $db_name, $db_user;
if (empty($sourcedir))
$sourcedir = dirname(__FILE__) . '/Sources';
// Need this to check whether we need the database password.
require(dirname(__FILE__) . '/Settings.php');
if (!defined('SMF'))
define('SMF', 1);
if (empty($smcFunc))
$smcFunc = array();
$modSettings['disableQueryCheck'] = true;
// Connect the database.
if (!$db_connection)
{
require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
if (@version_compare(PHP_VERSION, '5') == -1)
require_once($sourcedir . '/Subs-Compat.php');
if (!$db_connection)
$db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('persist' => $db_persist));
}
}
// This is called upon exiting the installer, for template etc.
function installExit($fallThrough = false)
{
global $incontext, $installurl, $txt;
// Send character set.
header('Content-Type: text/html; charset=' . (isset($txt['lang_character_set']) ? $txt['lang_character_set'] : 'ISO-8859-1'));
// We usually dump our templates out.
if (!$fallThrough)
{
// The top install bit.
template_install_above();
// Call the template.
if (isset($incontext['sub_template']))
{
$incontext['form_url'] = $installurl . '?step=' . $incontext['current_step'];
call_user_func('template_' . $incontext['sub_template']);
}
//!!! REMOVE THIS!!
else
{
if (function_exists('doStep' . $_GET['step']))
call_user_func('doStep' . $_GET['step']);
}
// Show the footer.
template_install_below();
}
// Bang - gone!
die();
}
function Welcome()
{
global $incontext, $txt, $databases, $installurl;
$incontext['page_title'] = $txt['install_welcome'];
$incontext['sub_template'] = 'welcome_message';
// Done the submission?
if (isset($_POST['contbutt']))
return true;
// Check the PHP version.
if ((!function_exists('version_compare') || version_compare($GLOBALS['required_php_version'], PHP_VERSION) > 0))
{
$incontext['warning'] = $txt['error_php_too_low'];
}
// See if we think they have already installed it?
if (is_readable(dirname(__FILE__) . '/Settings.php'))
{
$probably_installed = 0;
foreach (file(dirname(__FILE__) . '/Settings.php') as $line)
{
if (preg_match('~^\$db_passwd\s=\s\'([^\']+)\';$~', $line))
$probably_installed++;
if (preg_match('~^\$boardurl\s=\s\'([^\']+)\';~', $line) && !preg_match('~^\$boardurl\s=\s\'http://127\.0\.0\.1/smf\';~', $line))
$probably_installed++;
}
if ($probably_installed == 2)
$incontext['warning'] = $txt['error_already_installed'];
}
// Is some database support even compiled in?
$incontext['supported_databases'] = array();
foreach ($databases as $key => $db)
{
if ($db['supported'])
{
if (!file_exists(dirname(__FILE__) . '/install_' . $GLOBALS['db_script_version'] . '_' . $key . '.sql'))
{
$databases[$key]['supported'] = false;
$notFoundSQLFile = true;
$txt['error_db_script_missing'] = sprintf($txt['error_db_script_missing'], 'install_' . $GLOBALS['db_script_version'] . '_' . $key . '.sql');
}
else
{
$db_type = $key;
$incontext['supported_databases'][] = $db;
}
}
}
if (empty($incontext['supported_databases']))
$error = empty($notFoundSQLFile) ? 'error_db_missing' : 'error_db_script_missing';
// How about session support? Some crazy sysadmin remove it?
elseif (!function_exists('session_start'))
$error = 'error_session_missing';
// Make sure they uploaded all the files.
elseif (!file_exists(dirname(__FILE__) . '/index.php'))
$error = 'error_missing_files';
// Very simple check on the session.save_path for Windows.
// !!! Move this down later if they don't use database-driven sessions?
elseif (@ini_get('session.save_path') == '/tmp' && substr(__FILE__, 1, 2) == ':\\')
$error = 'error_session_save_path';
// Since each of the three messages would look the same, anyway...
if (isset($error))
$incontext['error'] = $txt[$error];
// Mod_security blocks everything that smells funny. Let SMF handle security.
if (!fixModSecurity() && !isset($_GET['overmodsecurity']))
$incontext['error'] = $txt['error_mod_security'] . '<br /><br /><a href="' . $installurl . '?overmodsecurity=true">' . $txt['error_message_click'] . '</a> ' . $txt['error_message_bad_try_again'];
return false;
}
function CheckFilesWritable()
{
global $txt, $incontext;
$incontext['page_title'] = $txt['ftp_checking_writable'];
$incontext['sub_template'] = 'chmod_files';
$writable_files = array(
'attachments',
'avatars',
'cache',
'Packages',
'Packages/installed.list',
'Smileys',
'Themes',
'agreement.txt',
'Settings.php',
'Settings_bak.php'
);
$extra_files = array(
'Themes/classic/index.template.php',
'Themes/classic/style.css'
);
foreach ($incontext['detected_languages'] as $lang => $temp)
$extra_files[] = 'Themes/default/languages/' . $lang;
// With mod_security installed, we could attempt to fix it with .htaccess.
if (function_exists('apache_get_modules') && in_array('mod_security', apache_get_modules()))
$writable_files[] = file_exists(dirname(__FILE__) . '/.htaccess') ? '.htaccess' : '.';
$failed_files = array();
// On linux, it's easy - just use is_writable!
if (substr(__FILE__, 1, 2) != ':\\')
{
foreach ($writable_files as $file)
{
if (!is_writable(dirname(__FILE__) . '/' . $file))
{
@chmod(dirname(__FILE__) . '/' . $file, 0755);
// Well, 755 hopefully worked... if not, try 777.
if (!is_writable(dirname(__FILE__) . '/' . $file) && !@chmod(dirname(__FILE__) . '/' . $file, 0777))
$failed_files[] = $file;
}
}
foreach ($extra_files as $file)
@chmod(dirname(__FILE__) . (empty($file) ? '' : '/' . $file), 0777);
}
// Windows is trickier. Let's try opening for r+...
else
{
foreach ($writable_files as $file)
{
// Folders can't be opened for write... but the index.php in them can ;)
if (is_dir(dirname(__FILE__) . '/' . $file))
$file .= '/index.php';
// Funny enough, chmod actually does do something on windows - it removes the read only attribute.
@chmod(dirname(__FILE__) . '/' . $file, 0777);
$fp = @fopen(dirname(__FILE__) . '/' . $file, 'r+');
// Hmm, okay, try just for write in that case...
if (!is_resource($fp))
$fp = @fopen(dirname(__FILE__) . '/' . $file, 'w');
if (!is_resource($fp))
$failed_files[] = $file;
@fclose($fp);
}
foreach ($extra_files as $file)
@chmod(dirname(__FILE__) . (empty($file) ? '' : '/' . $file), 0777);
}
$failure = count($failed_files) >= 1;
if (!isset($_SERVER))
return !$failure;
// Put the list into context.
$incontext['failed_files'] = $failed_files;
// It's not going to be possible to use FTP on windows to solve the problem...
if ($failure && substr(__FILE__, 1, 2) == ':\\')
{
$incontext['error'] = $txt['error_windows_chmod'] . '
<ul style="margin: 2.5ex; font-family: monospace;">
<li>' . implode('</li>
<li>', $failed_files) . '</li>
</ul>';
return false;
}
// We're going to have to use... FTP!
elseif ($failure)
{
// Load any session data we might have...
if (!isset($_POST['ftp_username']) && isset($_SESSION['installer_temp_ftp']))
{
$_POST['ftp_server'] = $_SESSION['installer_temp_ftp']['server'];
$_POST['ftp_port'] = $_SESSION['installer_temp_ftp']['port'];
$_POST['ftp_username'] = $_SESSION['installer_temp_ftp']['username'];
$_POST['ftp_password'] = $_SESSION['installer_temp_ftp']['password'];
$_POST['ftp_path'] = $_SESSION['installer_temp_ftp']['path'];
}
$incontext['ftp_errors'] = array();
if (isset($_POST['ftp_username']))
{
$ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
if ($ftp->error === false)
{
// Try it without /home/abc just in case they messed up.
if (!$ftp->chdir($_POST['ftp_path']))
{
$incontext['ftp_errors'][] = $ftp->last_message;
$ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
}
}
}
if (!isset($ftp) || $ftp->error !== false)
{
if (!isset($ftp))
$ftp = new ftp_connection(null);
// Save the error so we can mess with listing...
elseif ($ftp->error !== false && empty($incontext['ftp_errors']) && !empty($ftp->last_message))
$incontext['ftp_errors'][] = $ftp->last_message;
list ($username, $detect_path, $found_path) = $ftp->detect_path(dirname(__FILE__));
if (empty($_POST['ftp_path']) && $found_path)
$_POST['ftp_path'] = $detect_path;
if (!isset($_POST['ftp_username']))
$_POST['ftp_username'] = $username;
// Set the username etc, into context.
$incontext['ftp'] = array(
'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : 'localhost',
'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : '21',
'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : '',
'path' => isset($_POST['ftp_path']) ? $_POST['ftp_path'] : '/',
'path_msg' => !empty($found_path) ? $txt['ftp_path_found_info'] : $txt['ftp_path_info'],
);
return false;
}
else
{
$_SESSION['installer_temp_ftp'] = array(
'server' => $_POST['ftp_server'],
'port' => $_POST['ftp_port'],
'username' => $_POST['ftp_username'],
'password' => $_POST['ftp_password'],
'path' => $_POST['ftp_path']
);
$failed_files_updated = array();
foreach ($failed_files as $file)
{
if (!is_writable(dirname(__FILE__) . '/' . $file))
$ftp->chmod($file, 0755);
if (!is_writable(dirname(__FILE__) . '/' . $file))
$ftp->chmod($file, 0777);
if (!is_writable(dirname(__FILE__) . '/' . $file))
{
$failed_files_updated[] = $file;
$incontext['ftp_errors'][] = rtrim($ftp->last_message) . ' -> ' . $file . "\n";
}
}
$ftp->close();
// Are there any errors left?
if (count($failed_files_updated) >= 1)
{
// Guess there are...
$incontext['failed_files'] = $failed_files_updated;
// Set the username etc, into context.
$incontext['ftp'] = $_SESSION['installer_temp_ftp'] += array(
'path_msg' => $txt['ftp_path_info'],
);
return false;
}
}
}
return true;
}
function DatabaseSettings()
{
global $txt, $databases, $incontext, $smcFunc;
$incontext['sub_template'] = 'database_settings';
$incontext['page_title'] = $txt['db_settings'];
$incontext['continue'] = 1;
// Set up the defaults.
$incontext['db']['server'] = 'localhost';
$incontext['db']['user'] = '';
$incontext['db']['name'] = '';
$incontext['db']['pass'] = '';
$incontext['db']['type'] = '';
$incontext['supported_databases'] = array();
$foundOne = false;
foreach ($databases as $key => $db)
{
// Override with the defaults for this DB if appropriate.
if ($db['supported'])
{
$incontext['supported_databases'][$key] = $db;
if (!$foundOne)
{
if (isset($db['default_host']))
$incontext['db']['server'] = @ini_get($db['default_host']) or $incontext['db']['server'] = 'localhost';
if (isset($db['default_user']))
{
$incontext['db']['user'] = @ini_get($db['default_user']);
$incontext['db']['name'] = @ini_get($db['default_user']);
}
if (isset($db['default_password']))
$incontext['db']['pass'] = @ini_get($db['default_password']);
if (isset($db['default_port']))
$db_port = @ini_get($db['default_port']);
$incontext['db']['type'] = $key;
$foundOne = true;
}
}
}
// Override for repost.
if (isset($_POST['db_user']))
{
$incontext['db']['user'] = $_POST['db_user'];
$incontext['db']['name'] = $_POST['db_type'] == 'sqlite' && isset($_POST['db_filename']) ? $_POST['db_filename'] : $_POST['db_name'];
$incontext['db']['server'] = $_POST['db_server'];
$incontext['db']['prefix'] = $_POST['db_prefix'];
}
else
$incontext['db']['prefix'] = 'smf_';
// Should we use a non standard port?
if (!empty($_port))
$incontext['db']['server'] .= ':' . $db_port;
// Are we submitting?
if (isset($_POST['db_type']))
{
if (isset($_POST['db_filename']))
{
// You better enter enter a database name for SQLite.
if (trim($_POST['db_filename']) == '')
{
$incontext['error'] = $txt['error_db_filename'];
return false;
}
// Duplicate name in the same dir? Can't do that with SQLite. Weird things happen.
if (file_exists($_POST['db_filename'] . (substr($_POST['db_filename'], -3) != '.db' ? '.db' : '')))
{
$incontext['error'] = $txt['error_db_filename_exists'];
return false;
}
}
// What type are they trying?
$db_type = preg_replace('~[^A-Za-z0-9]~', '', $_POST['db_type']);
$db_prefix = $_POST['db_prefix'];
// Validate the prefix.
$valid_prefix = $databases[$db_type]['validate_prefix']($db_prefix);
if ($valid_prefix !== true)
{
$incontext['error'] = $valid_prefix;
return false;
}
// Take care of these variables...
$vars = array(
'db_type' => $db_type,
'db_name' => $_POST['db_type'] == 'sqlite' && isset($_POST['db_filename']) ? $_POST['db_filename'] : $_POST['db_name'],
'db_user' => $_POST['db_user'],
'db_passwd' => isset($_POST['db_passwd']) ? $_POST['db_passwd'] : '',
'db_server' => $_POST['db_server'],
'db_prefix' => $db_prefix,
// The cookiename is special; we want it to be the same if it ever needs to be reinstalled with the same info.
'cookiename' => 'SMFCookie' . abs(crc32($_POST['db_name'] . preg_replace('~[^A-Za-z0-9_$]~', '', $_POST['db_prefix'])) % 1000),
);
// God I hope it saved!
if (!updateSettingsFile($vars) && substr(__FILE__, 1, 2) == ':\\')
{
$incontext['error'] = $txt['error_windows_chmod'];
return false;
}
// Make sure it works.
require(dirname(__FILE__) . '/Settings.php');
if (empty($sourcedir))
$sourcedir = dirname(__FILE__) . '/Sources';
// Better find the database file!
if (!file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php'))
{
$incontext['error'] = sprintf($txt['error_db_file'], 'Subs-Db-' . $db_type . '.php');
return false;
}
// Now include it for database functions!
define('SMF', 1);
$modSettings['disableQueryCheck'] = true;
if (empty($smcFunc))
$smcFunc = array();
require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
// What - running PHP4? The shame!
if (@version_compare(PHP_VERSION, '5') == -1)
require_once($sourcedir . '/Subs-Compat.php');
// Attempt a connection.
$needsDB = !empty($databases[$db_type]['always_has_db']);
$db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true, 'dont_select_db' => !$needsDB));
// No dice? Let's try adding the prefix they specified, just in case they misread the instructions ;)
if ($db_connection == null)
{
$db_error = @$smcFunc['db_error']();
$db_connection = smf_db_initiate($db_server, $db_name, $_POST['db_prefix'] . $db_user, $db_passwd, $db_prefix, array('non_fatal' => true, 'dont_select_db' => !$needsDB));
if ($db_connection != null)
{
$db_user = $_POST['db_prefix'] . $db_user;
updateSettingsFile(array('db_user' => $db_user));
}
}
// Still no connection? Big fat error message :P.
if (!$db_connection)
{
$incontext['error'] = $txt['error_db_connect'] . '<div style="margin: 2.5ex; font-family: monospace;"><strong>' . $db_error . '</strong></div>';
return false;
}
// Do they meet the install requirements?
// !!! Old client, new server?
if (version_compare($databases[$db_type]['version'], preg_replace('~^\D*|\-.+?$~', '', eval($databases[$db_type]['version_check']))) > 0)
{
$incontext['error'] = $txt['error_db_too_low'];
return false;
}
// Let's try that database on for size... assuming we haven't already lost the opportunity.
if ($db_name != '' && !$needsDB)
{
$smcFunc['db_query']('', "
CREATE DATABASE IF NOT EXISTS `$db_name`",
array(
'security_override' => true,
'db_error_skip' => true,
),
$db_connection
);
// Okay, let's try the prefix if it didn't work...
if (!$smcFunc['db_select_db']($db_name, $db_connection) && $db_name != '')
{
$smcFunc['db_query']('', "
CREATE DATABASE IF NOT EXISTS `$_POST[db_prefix]$db_name`",
array(
'security_override' => true,
'db_error_skip' => true,
),
$db_connection
);
if ($smcFunc['db_select_db']($_POST['db_prefix'] . $db_name, $db_connection))
{
$db_name = $_POST['db_prefix'] . $db_name;
updateSettingsFile(array('db_name' => $db_name));
}
}
// Okay, now let's try to connect...
if (!$smcFunc['db_select_db']($db_name, $db_connection))
{
$incontext['error'] = sprintf($txt['error_db_database'], $db_name);
return false;
}
}
return true;
}
return false;
}
// Let's start with basic forum type settings.
function ForumSettings()
{
global $txt, $incontext, $databases, $smcFunc, $db_connection, $db_type;
$incontext['sub_template'] = 'forum_settings';
$incontext['page_title'] = $txt['install_settings'];
// Let's see if we got the database type correct.
if (isset($_POST['db_type'], $databases[$_POST['db_type']]))
$db_type = $_POST['db_type'];
// Else we'd better be able to get the connection.
else
load_database();
$db_type = isset($_POST['db_type']) ? $_POST['db_type'] : $db_type;
// What host and port are we on?
$host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . (empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT']) : $_SERVER['HTTP_HOST'];
// Now, to put what we've learned together... and add a path.
$incontext['detected_url'] = 'http' . (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 's' : '') . '://' . $host . substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));
// Check if the database sessions will even work.
$incontext['test_dbsession'] = @ini_get('session.auto_start') != 1 && @version_compare(PHP_VERSION, '4.2.0') != -1;
$incontext['utf8_should_work'] = strpos(strtolower(PHP_OS), 'win') === false || @version_compare(PHP_VERSION, '4.2.3') != -1;
$incontext['utf8_default'] = $databases[$db_type]['utf8_default'];
$incontext['utf8_required'] = $databases[$db_type]['utf8_required'];
$incontext['continue'] = 1;
// Submitting?
if (isset($_POST['boardurl']))
{
if (substr($_POST['boardurl'], -10) == '/index.php')
$_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
elseif (substr($_POST['boardurl'], -1) == '/')
$_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://')
$_POST['boardurl'] = 'http://' . $_POST['boardurl'];
// Save these variables.
$vars = array(
'boardurl' => $_POST['boardurl'],
'boarddir' => addslashes(dirname(__FILE__)),
'sourcedir' => addslashes(dirname(__FILE__)) . '/Sources',
'cachedir' => addslashes(dirname(__FILE__)) . '/cache',
'mbname' => strtr($_POST['mbname'], array('\"' => '"')),
'language' => substr($_SESSION['installer_temp_lang'], 8, -4),
'image_proxy_secret' => substr(sha1(mt_rand()), 0, 20),
'image_proxy_maxsize' => 5190,
'image_proxy_enabled' => 0,
);
// Must save!
if (!updateSettingsFile($vars) && substr(__FILE__, 1, 2) == ':\\')
{
$incontext['error'] = $txt['error_windows_chmod'];
return false;
}
// Make sure it works.
require(dirname(__FILE__) . '/Settings.php');
// UTF-8 requires a setting to override the language charset.
if (isset($_POST['utf8']) && !empty($databases[$db_type]['utf8_support']))
{
if (version_compare($databases[$db_type]['utf8_version'], preg_replace('~\-.+?$~', '', eval($databases[$db_type]['utf8_version_check']))) > 0)
{
$incontext['error'] = sprintf($txt['error_utf8_version'], $databases[$db_type]['utf8_version']);
return false;
}
else
// Set the character set here.
updateSettingsFile(array('db_character_set' => 'utf8'));
}
// Good, skip on.
return true;
}
return false;
}
// Step one: Do the SQL thang.
function DatabasePopulation()
{
global $db_character_set, $txt, $db_connection, $smcFunc, $databases, $modSettings, $db_type, $sourcedir, $db_prefix, $incontext, $db_name, $boardurl;
$incontext['sub_template'] = 'populate_database';
$incontext['page_title'] = $txt['db_populate'];
$incontext['continue'] = 1;
// Already done?
if (isset($_POST['pop_done']))
return true;
// Force opcache invalidate for Settings.php file.
if (function_exists('opcache_invalidate')) {
opcache_invalidate(dirname(__FILE__) . '/Settings.php', true);
}
// Reload settings.
require(dirname(__FILE__) . '/Settings.php');
load_database();
// Before running any of the queries, let's make sure another version isn't already installed.
$result = $smcFunc['db_query']('', '
SELECT variable, value
FROM {db_prefix}settings',
array(
'db_error_skip' => true,
)
);
$modSettings = array();