some changes/fixes
[phpbb.git] / phpBB / install / index.php
blobfed5b6754571b9dab2738e940b62646130899d5f
1 <?php
2 /**
4 * @package install
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
13 define('IN_PHPBB', true);
14 define('IN_INSTALL', true);
16 $phpbb_root_path = './../';
17 $phpEx = substr(strrchr(__FILE__, '.'), 1);
19 // Report all errors, except notices
20 error_reporting(E_ALL ^ E_NOTICE);
22 // @todo Review this test and see if we can find out what it is which prevents PHP 4.2.x from even displaying the page with requirements on it
23 if (version_compare(phpversion(), '4.3.0') < 0)
25 die('You are running an unsupported PHP version. Please upgrade to PHP 4.3.3 or higher before trying to install phpBB 3.0');
29 * Remove variables created by register_globals from the global scope
30 * Thanks to Matt Kavanagh
32 function deregister_globals()
34 $not_unset = array(
35 'GLOBALS' => true,
36 '_GET' => true,
37 '_POST' => true,
38 '_COOKIE' => true,
39 '_REQUEST' => true,
40 '_SERVER' => true,
41 '_SESSION' => true,
42 '_ENV' => true,
43 '_FILES' => true,
44 'phpEx' => true,
45 'phpbb_root_path' => true
48 // Not only will array_merge and array_keys give a warning if
49 // a parameter is not an array, array_merge will actually fail.
50 // So we check if _SESSION has been initialised.
51 if (!isset($_SESSION) || !is_array($_SESSION))
53 $_SESSION = array();
56 // Merge all into one extremely huge array; unset
57 // this later
58 $input = array_merge(
59 array_keys($_GET),
60 array_keys($_POST),
61 array_keys($_COOKIE),
62 array_keys($_SERVER),
63 array_keys($_SESSION),
64 array_keys($_ENV),
65 array_keys($_FILES)
68 foreach ($input as $varname)
70 if (isset($not_unset[$varname]))
72 // Hacking attempt. No point in continuing.
73 exit;
76 unset($GLOBALS[$varname]);
79 unset($input);
82 // If we are on PHP >= 6.0.0 we do not need some code
83 if (version_compare(phpversion(), '6.0.0-dev', '>='))
85 /**
86 * @ignore
88 define('STRIP', false);
90 else
92 set_magic_quotes_runtime(0);
94 // Be paranoid with passed vars
95 if (@ini_get('register_globals') == '1' || strtolower(@ini_get('register_globals')) == 'on')
97 deregister_globals();
100 define('STRIP', (get_magic_quotes_gpc()) ? true : false);
103 @set_time_limit(120);
105 // Include essential scripts
106 require($phpbb_root_path . 'includes/functions.' . $phpEx);
107 include($phpbb_root_path . 'includes/auth.' . $phpEx);
108 include($phpbb_root_path . 'includes/session.' . $phpEx);
109 include($phpbb_root_path . 'includes/template.' . $phpEx);
110 include($phpbb_root_path . 'includes/acm/acm_file.' . $phpEx);
111 include($phpbb_root_path . 'includes/cache.' . $phpEx);
112 include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
114 // Try and load an appropriate language if required
115 $language = request_var('language', '');
117 if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) && !$language)
119 $accept_lang_ary = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
120 foreach ($accept_lang_ary as $accept_lang)
122 // Set correct format ... guess full xx_YY form
123 $accept_lang = substr($accept_lang, 0, 2) . '_' . strtoupper(substr($accept_lang, 3, 2));
125 if (file_exists($phpbb_root_path . 'language/' . $accept_lang))
127 $language = $accept_lang;
128 break;
130 else
132 // No match on xx_YY so try xx
133 $accept_lang = substr($accept_lang, 0, 2);
134 if (file_exists($phpbb_root_path . 'language/' . $accept_lang))
136 $language = $accept_lang;
137 break;
143 // No appropriate language found ... so let's use the first one in the language
144 // dir, this may or may not be English
145 if (!$language)
147 $dir = @opendir($phpbb_root_path . 'language');
148 while (($file = readdir($dir)) !== false)
150 $path = $phpbb_root_path . 'language/' . $file;
152 if (!is_file($path) && !is_link($path) && file_exists($path . '/iso.txt'))
154 $language = $file;
155 break;
160 // And finally, load the relevant language files
161 include($phpbb_root_path . 'language/' . $language . '/common.' . $phpEx);
162 include($phpbb_root_path . 'language/' . $language . '/acp/common.' . $phpEx);
163 include($phpbb_root_path . 'language/' . $language . '/acp/board.' . $phpEx);
164 include($phpbb_root_path . 'language/' . $language . '/install.' . $phpEx);
165 include($phpbb_root_path . 'language/' . $language . '/posting.' . $phpEx);
167 $mode = request_var('mode', 'overview');
168 $sub = request_var('sub', '');
170 // Set PHP error handler to ours
171 set_error_handler('msg_handler');
173 $user = new user();
174 $auth = new auth();
175 $cache = new cache();
176 $template = new template();
178 $template->set_custom_template('../adm/style', 'admin');
179 $template->assign_var('T_TEMPLATE_PATH', '../adm/style');
181 $install = new module();
183 $install->create('install', "index.$phpEx", $mode, $sub);
184 $install->load();
186 // Generate the page
187 $install->page_header();
188 $install->generate_navigation();
190 $template->set_filenames(array(
191 'body' => $install->get_tpl_name())
194 $install->page_footer();
197 * @package install
199 class module
201 var $id = 0;
202 var $type = 'install';
203 var $module_ary = array();
204 var $filename;
205 var $module_url = '';
206 var $tpl_name = '';
207 var $mode;
208 var $sub;
211 * Private methods, should not be overwritten
213 function create($module_type, $module_url, $selected_mod = false, $selected_submod = false)
215 global $db, $config, $phpEx, $phpbb_root_path;
217 $module = array();
219 // Grab module information using Bart's "neat-o-module" system (tm)
220 $dir = @opendir('.');
222 $setmodules = 1;
223 while (($file = readdir($dir)) !== false)
225 if (preg_match('#^install_(.*?)\.' . $phpEx . '$#', $file))
227 include($file);
230 @closedir($dir);
232 unset($setmodules);
234 if (!sizeof($module))
236 $this->error('No installation modules found', __LINE__, __FILE__);
239 foreach ($module as $row)
241 // Check any module pre-reqs
242 if ($row['module_reqs'] != '')
246 $this->module_ary[$row['module_order']]['name'] = $row['module_title'];
247 $this->module_ary[$row['module_order']]['filename'] = $row['module_filename'];
248 $this->module_ary[$row['module_order']]['subs'] = $row['module_subs'];
249 $this->module_ary[$row['module_order']]['stages'] = $row['module_stages'];
251 if (strtolower($selected_mod) == strtolower($row['module_title']))
253 $this->id = (int) $row['module_order'];
254 $this->filename = (string) $row['module_filename'];
255 $this->module_url = (string) $module_url;
256 $this->mode = (string) $selected_mod;
257 // Check that the sub-mode specified is valid or set a default if not
258 if (is_array($row['module_subs']))
260 $this->sub = strtolower((in_array(strtoupper($selected_submod), $row['module_subs'])) ? $selected_submod : $row['module_subs'][0]);
262 else if (is_array($row['module_stages']))
264 $this->sub = strtolower((in_array(strtoupper($selected_submod), $row['module_stages'])) ? $selected_submod : $row['module_stages'][0]);
266 else
268 $this->sub = '';
271 } // END foreach
272 } // END create
275 * Load and run the relevant module if applicable
277 function load($mode = false, $run = true)
279 global $phpbb_root_path, $phpEx;
281 if ($run)
283 if (!empty($mode))
285 $this->mode = $mode;
288 $module = $this->filename;
289 $this->module = new $module($this);
291 if (method_exists($this->module, 'main'))
293 $this->module->main($this->mode, $this->sub);
299 * Output the standard page header
301 function page_header()
303 if (defined('HEADER_INC'))
305 return;
308 define('HEADER_INC', true);
309 global $template, $lang, $stage, $phpbb_root_path;
311 $template->assign_vars(array(
312 'L_CHANGE' => $lang['CHANGE'],
313 'L_INSTALL_PANEL' => $lang['INSTALL_PANEL'],
314 'L_SELECT_LANG' => $lang['SELECT_LANG'],
315 'PAGE_TITLE' => $this->get_page_title(),
316 'T_IMAGE_PATH' => $phpbb_root_path . 'adm/images/',
318 'S_CONTENT_DIRECTION' => $lang['DIRECTION'],
319 'S_CONTENT_ENCODING' => $lang['ENCODING'],
320 'S_CONTENT_DIR_LEFT' => $lang['LEFT'],
321 'S_CONTENT_DIR_RIGHT' => $lang['RIGHT'],
325 if (!empty($lang['ENCODING']))
327 header('Content-type: text/html; charset: ' . $lang['ENCODING']);
329 header('Cache-Control: private, no-cache="set-cookie"');
330 header('Expires: 0');
331 header('Pragma: no-cache');
333 return;
337 * Output the standard page footer
339 function page_footer()
341 global $db, $template;
343 $template->display('body');
345 // Close our DB connection.
346 if (isset($db))
348 $db->sql_close();
351 exit;
355 * Returns desired template name
357 function get_tpl_name()
359 return $this->module->tpl_name . '.html';
363 * Returns the desired page title
365 function get_page_title()
367 global $lang;
369 if (!isset($this->module->page_title))
371 return '';
374 return (isset($lang[$this->module->page_title])) ? $lang[$this->module->page_title] : $this->module->page_title;
378 * Generate an HTTP/1.1 header to redirect the user to another page
379 * This is used during the installation when we do not have a database available to call the normal redirect function
380 * @param string $page The page to redirect to relative to the installer root path
382 function redirect($page)
384 $server_name = (!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME');
385 $server_port = (!empty($_SERVER['SERVER_PORT'])) ? (int) $_SERVER['SERVER_PORT'] : (int) getenv('SERVER_PORT');
386 $secure = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 1 : 0;
388 $script_name = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : getenv('PHP_SELF');
389 if (!$script_name)
391 $script_name = (!empty($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : getenv('REQUEST_URI');
394 // Replace backslashes and doubled slashes (could happen on some proxy setups)
395 $script_name = str_replace(array('\\', '//'), '/', $script_name);
396 $script_path = trim(dirname($script_name));
398 $url = (($secure) ? 'https://' : 'http://') . $server_name;
400 if ($server_port && (($secure && $server_port <> 443) || (!$secure && $server_port <> 80)))
402 $url .= ':' . $server_port;
405 $url .= $script_path . '/' . $page;
406 header('Location: ' . $url);
407 exit;
411 * Generate the navigation tabs
413 function generate_navigation()
415 global $lang, $template, $phpEx, $language;
417 if (is_array($this->module_ary))
419 @ksort($this->module_ary);
420 foreach ($this->module_ary as $cat_ary)
422 $cat = $cat_ary['name'];
423 $l_cat = (!empty($lang['CAT_' . $cat])) ? $lang['CAT_' . $cat] : preg_replace('#_#', ' ', $cat);
424 $cat = strtolower($cat);
425 $url = $this->module_url . "?mode=$cat&amp;language=$language";
427 if ($this->mode == $cat)
429 $template->assign_block_vars('t_block1', array(
430 'L_TITLE' => $l_cat,
431 'S_SELECTED' => true,
432 'U_TITLE' => $url,
435 if (is_array($this->module_ary[$this->id]['subs']))
437 $subs = $this->module_ary[$this->id]['subs'];
438 foreach ($subs as $option)
440 $l_option = (!empty($lang['SUB_' . $option])) ? $lang['SUB_' . $option] : preg_replace('#_#', ' ', $option);
441 $option = strtolower($option);
442 $url = $this->module_url . '?mode=' . $this->mode . "&amp;sub=$option&amp;language=$language";
444 $template->assign_block_vars('l_block1', array(
445 'L_TITLE' => $l_option,
446 'S_SELECTED' => ($this->sub == $option),
447 'U_TITLE' => $url,
452 if (is_array($this->module_ary[$this->id]['stages']))
454 $subs = $this->module_ary[$this->id]['stages'];
455 $matched = false;
456 foreach ($subs as $option)
458 $l_option = (!empty($lang['STAGE_' . $option])) ? $lang['STAGE_' . $option] : preg_replace('#_#', ' ', $option);
459 $option = strtolower($option);
460 $matched = ($this->sub == $option) ? true : $matched;
462 $template->assign_block_vars('l_block2', array(
463 'L_TITLE' => $l_option,
464 'S_SELECTED' => ($this->sub == $option),
465 'S_COMPLETE' => !$matched,
470 else
472 $template->assign_block_vars('t_block1', array(
473 'L_TITLE' => $l_cat,
474 'S_SELECTED' => false,
475 'U_TITLE' => $url,
483 * Output an error message
484 * If skip is true, return and continue execution, else exit
486 function error($error, $line, $file, $skip = false)
488 global $lang, $db, $template;
490 if ($skip)
492 $template->assign_block_vars('checks', array(
493 'S_LEGEND' => true,
494 'LEGEND' => $lang['INST_ERR'],
497 $template->assign_block_vars('checks', array(
498 'TITLE' => basename($file) . ' [ ' . $line . ' ]',
499 'RESULT' => '<b style="color:red">' . $error . '</b>',
502 return;
505 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
506 echo '<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">';
507 echo '<head>';
508 echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
509 echo '<title>' . $lang['INST_ERR_FATAL'] . '</title>';
510 echo '<link href="../adm/style/admin.css" rel="stylesheet" type="text/css" media="screen" />';
511 echo '</head>';
512 echo '<body id="errorpage">';
513 echo '<div id="wrap">';
514 echo ' <div id="page-header">';
515 echo ' </div>';
516 echo ' <div id="page-body">';
517 echo ' <div class="panel">';
518 echo ' <span class="corners-top"><span></span></span>';
519 echo ' <div id="content">';
520 echo ' <h1>' . $lang['INST_ERR_FATAL'] . '</h1>';
521 echo ' <p>' . $lang['INST_ERR_FATAL'] . "</p>\n";
522 echo ' <p>' . basename($file) . ' [ ' . $line . " ]</p>\n";
523 echo ' <p><b>' . $error . "</b></p>\n";
524 echo ' </div>';
525 echo ' <span class="corners-bottom"><span></span></span>';
526 echo ' </div>';
527 echo ' </div>';
528 echo ' <div id="page-footer">';
529 echo ' Powered by phpBB &copy; ' . date('Y') . ' <a href="http://www.phpbb.com/">phpBB Group</a>';
530 echo ' </div>';
531 echo '</div>';
532 echo '</body>';
533 echo '</html>';
535 if (isset($db))
537 $db->sql_close();
540 exit;
544 * Output an error message for a database related problem
545 * If skip is true, return and continue execution, else exit
547 function db_error($error, $sql, $line, $file, $skip = false)
549 global $lang, $db, $template;
551 if ($skip)
553 $template->assign_block_vars('checks', array(
554 'S_LEGEND' => true,
555 'LEGEND' => $lang['INST_ERR_FATAL'],
558 $template->assign_block_vars('checks', array(
559 'TITLE' => basename($file) . ' [ ' . $line . ' ]',
560 'RESULT' => '<b style="color:red">' . $error . '</b><br />&#187; SQL:' . $sql,
563 return;
566 $template->set_filenames(array(
567 'body' => 'install_error.html')
569 $this->page_header();
570 $this->generate_navigation();
572 $template->assign_vars(array(
573 'MESSAGE_TITLE' => $lang['INST_ERR_FATAL_DB'],
574 'MESSAGE_TEXT' => '<p>' . basename($file) . ' [ ' . $line . ' ]</p><p>SQL : ' . $sql . '</p><p><b>' . $error . '</b></p>',
577 // Rollback if in transaction
578 if ($db->transaction)
580 $db->sql_transaction('rollback');
583 $this->page_footer();
587 * Generate the relevant HTML for an input field and the assosciated label and explanatory text
589 function input_field($name, $type, $value='', $options='')
591 global $lang;
592 $tpl_type = explode(':', $type);
593 $tpl = '';
595 switch ($tpl_type[0])
597 case 'text':
598 case 'password':
599 $size = (int) $tpl_type[1];
600 $maxlength = (int) $tpl_type[2];
602 $tpl = '<input id="' . $name . '" type="' . $tpl_type[0] . '"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="' . $name . '" value="' . $value . '" />';
603 break;
605 case 'textarea':
606 $rows = (int) $tpl_type[1];
607 $cols = (int) $tpl_type[2];
609 $tpl = '<textarea id="' . $name . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' . $value . '</textarea>';
610 break;
612 case 'radio':
613 $key_yes = ($value) ? ' checked="checked"' : '';
614 $key_no = (!$value) ? ' checked="checked"' : '';
616 $tpl_type_cond = explode('_', $tpl_type[1]);
617 $type_no = ($tpl_type_cond[0] == 'disabled' || $tpl_type_cond[0] == 'enabled') ? false : true;
619 $tpl_no = '<input type="radio" name="' . $name . '" value="0"' . $key_no . ' class="radio" />&nbsp;' . (($type_no) ? $lang['NO'] : $lang['DISABLED']);
620 $tpl_yes = '<input type="radio" name="' . $name . '" value="1"' . $key_yes . ' class="radio" />&nbsp;' . (($type_no) ? $lang['YES'] : $lang['ENABLED']);
622 $tpl = ($tpl_type_cond[0] == 'yes' || $tpl_type_cond[0] == 'enabled') ? $tpl_yes . '&nbsp;&nbsp;' . $tpl_no : $tpl_no . '&nbsp;&nbsp;' . $tpl_yes;
623 break;
625 case 'select':
626 eval('$s_options = ' . str_replace('{VALUE}', $value, $options) . ';');
627 $tpl = '<select id="' . $name . '" name="' . $name . '">' . $s_options . '</select>';
628 break;
630 case 'custom':
631 eval('$tpl = ' . str_replace('{VALUE}', $value, $options) . ';');
632 break;
634 default:
635 break;
638 return $tpl;
642 * Generate the drop down of available language packs
644 function inst_language_select($default = '')
646 global $phpbb_root_path, $phpEx;
648 $dir = @opendir($phpbb_root_path . 'language');
650 while ($file = readdir($dir))
652 $path = $phpbb_root_path . 'language/' . $file;
654 if (is_file($path) || is_link($path) || $file == '.' || $file == '..' || $file == 'CVS')
656 continue;
659 if (file_exists($path . '/iso.txt'))
661 list($displayname) = @file($path . '/iso.txt');
662 $lang[$displayname] = $file;
665 @closedir($dir);
667 @asort($lang);
668 @reset($lang);
670 $user_select = '';
671 foreach ($lang as $displayname => $filename)
673 $selected = (strtolower($default) == strtolower($filename)) ? ' selected="selected"' : '';
674 $user_select .= '<option value="' . $filename . '"' . $selected . '>' . ucwords($displayname) . '</option>';
677 return $user_select;