6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 define('IN_PHPBB', true);
15 define('IN_INSTALL', true);
18 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
19 $phpEx = substr(strrchr(__FILE__, '.'), 1);
21 // @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
22 if (version_compare(PHP_VERSION, '4.3.3') < 0)
24 die('You are running an unsupported PHP version. Please upgrade to PHP 4.3.3 or higher before trying to install phpBB 3.0');
27 function phpbb_require_updated($path, $optional = false)
29 global $phpbb_root_path;
31 $new_path = $phpbb_root_path . 'install/update/new/' . $path;
32 $old_path = $phpbb_root_path . $path;
34 if (file_exists($new_path))
38 else if (!$optional || file_exists($old_path))
44 phpbb_require_updated('includes/startup.' . $phpEx);
46 // Try to override some limits - maybe it helps some...
48 $mem_limit = @ini_get('memory_limit');
49 if (!empty($mem_limit))
51 $unit = strtolower(substr($mem_limit, -1, 1));
52 $mem_limit = (int) $mem_limit;
56 $mem_limit = floor($mem_limit / 1024);
58 else if ($unit == 'g')
62 else if (is_numeric($unit))
64 $mem_limit = floor((int) ($mem_limit . $unit) / 1048576);
66 $mem_limit = max(128, $mem_limit) . 'M';
72 @ini_set('memory_limit', $mem_limit);
74 // Include essential scripts
75 require($phpbb_root_path . 'includes/functions.' . $phpEx);
77 phpbb_require_updated('includes/functions_content.' . $phpEx, true);
79 include($phpbb_root_path . 'includes/auth.' . $phpEx);
80 include($phpbb_root_path . 'includes/session.' . $phpEx);
81 include($phpbb_root_path . 'includes/template.' . $phpEx);
82 include($phpbb_root_path . 'includes/acm/acm_file.' . $phpEx);
83 include($phpbb_root_path . 'includes/cache.' . $phpEx);
84 include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
85 include($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
86 require($phpbb_root_path . 'includes/functions_install.' . $phpEx);
88 // Try and load an appropriate language if required
89 $language = basename(request_var('language', ''));
91 if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) && !$language)
93 $accept_lang_ary = explode(',', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
94 foreach ($accept_lang_ary as $accept_lang)
96 // Set correct format ... guess full xx_yy form
97 $accept_lang = substr($accept_lang, 0, 2) . '_' . substr($accept_lang, 3, 2);
99 if (file_exists($phpbb_root_path . 'language/' . $accept_lang) && is_dir($phpbb_root_path . 'language/' . $accept_lang))
101 $language = $accept_lang;
106 // No match on xx_yy so try xx
107 $accept_lang = substr($accept_lang, 0, 2);
108 if (file_exists($phpbb_root_path . 'language/' . $accept_lang) && is_dir($phpbb_root_path . 'language/' . $accept_lang))
110 $language = $accept_lang;
117 // No appropriate language found ... so let's use the first one in the language
118 // dir, this may or may not be English
121 $dir = @opendir($phpbb_root_path . 'language');
125 die('Unable to access the language directory');
129 while (($file = readdir($dir)) !== false)
131 $path = $phpbb_root_path . 'language/' . $file;
133 if (!is_file($path) && !is_link($path) && file_exists($path . '/iso.txt'))
142 if (!file_exists($phpbb_root_path . 'language/' . $language) || !is_dir($phpbb_root_path . 'language/' . $language))
144 die('No language found!');
147 // And finally, load the relevant language files
148 include($phpbb_root_path . 'language/' . $language . '/common.' . $phpEx);
149 include($phpbb_root_path . 'language/' . $language . '/acp/common.' . $phpEx);
150 include($phpbb_root_path . 'language/' . $language . '/acp/board.' . $phpEx);
151 include($phpbb_root_path . 'language/' . $language . '/install.' . $phpEx);
152 include($phpbb_root_path . 'language/' . $language . '/posting.' . $phpEx);
154 // usually we would need every single constant here - and it would be consistent. For 3.0.x, use a dirty hack... :(
156 // Define needed constants
157 define('CHMOD_ALL', 7);
158 define('CHMOD_READ', 4);
159 define('CHMOD_WRITE', 2);
160 define('CHMOD_EXECUTE', 1);
162 $mode = request_var('mode', 'overview');
163 $sub = request_var('sub', '');
165 // Set PHP error handler to ours
166 set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler');
170 $cache = new cache();
171 $template = new template();
173 // Add own hook handler, if present. :o
174 if (file_exists($phpbb_root_path . 'includes/hooks/index.' . $phpEx))
176 require($phpbb_root_path . 'includes/hooks/index.' . $phpEx);
177 $phpbb_hook = new phpbb_hook(array('exit_handler', 'phpbb_user_session_handler', 'append_sid', array('template', 'display')));
179 foreach ($cache->obtain_hooks() as $hook)
181 @include($phpbb_root_path . 'includes/hooks/' . $hook . '.' . $phpEx);
189 // Set some standard variables we want to force
191 'load_tplcompile' => '1'
194 $template->set_custom_template('../adm/style', 'admin');
195 $template->assign_var('T_TEMPLATE_PATH', '../adm/style');
197 // the acp template is never stored in the database
198 $user->theme['template_storedb'] = false;
200 $install = new module();
202 $install->create('install', "index.$phpEx", $mode, $sub);
206 $install->page_header();
207 $install->generate_navigation();
209 $template->set_filenames(array(
210 'body' => $install->get_tpl_name())
213 $install->page_footer();
221 var $type = 'install';
222 var $module_ary = array();
224 var $module_url = '';
230 * Private methods, should not be overwritten
232 function create($module_type, $module_url, $selected_mod = false, $selected_submod = false)
234 global $db, $config, $phpEx, $phpbb_root_path;
238 // Grab module information using Bart's "neat-o-module" system (tm)
239 $dir = @opendir('.');
243 $this->error('Unable to access the installation directory', __LINE__, __FILE__);
247 while (($file = readdir($dir)) !== false)
249 if (preg_match('#^install_(.*?)\.' . $phpEx . '$#', $file))
258 if (!sizeof($module))
260 $this->error('No installation modules found', __LINE__, __FILE__);
263 // Order to use and count further if modules get assigned to the same position or not having an order
264 $max_module_order = 1000;
266 foreach ($module as $row)
268 // Module order not specified or module already assigned at this position?
269 if (!isset($row['module_order']) || isset($this->module_ary[$row['module_order']]))
271 $row['module_order'] = $max_module_order;
275 $this->module_ary[$row['module_order']]['name'] = $row['module_title'];
276 $this->module_ary[$row['module_order']]['filename'] = $row['module_filename'];
277 $this->module_ary[$row['module_order']]['subs'] = $row['module_subs'];
278 $this->module_ary[$row['module_order']]['stages'] = $row['module_stages'];
280 if (strtolower($selected_mod) == strtolower($row['module_title']))
282 $this->id = (int) $row['module_order'];
283 $this->filename = (string) $row['module_filename'];
284 $this->module_url = (string) $module_url;
285 $this->mode = (string) $selected_mod;
286 // Check that the sub-mode specified is valid or set a default if not
287 if (is_array($row['module_subs']))
289 $this->sub = strtolower((in_array(strtoupper($selected_submod), $row['module_subs'])) ? $selected_submod : $row['module_subs'][0]);
291 else if (is_array($row['module_stages']))
293 $this->sub = strtolower((in_array(strtoupper($selected_submod), $row['module_stages'])) ? $selected_submod : $row['module_stages'][0]);
304 * Load and run the relevant module if applicable
306 function load($mode = false, $run = true)
308 global $phpbb_root_path, $phpEx;
317 $module = $this->filename;
318 if (!class_exists($module))
320 $this->error('Module "' . htmlspecialchars($module) . '" not accessible.', __LINE__, __FILE__);
322 $this->module = new $module($this);
324 if (method_exists($this->module, 'main'))
326 $this->module->main($this->mode, $this->sub);
332 * Output the standard page header
334 function page_header()
336 if (defined('HEADER_INC'))
341 define('HEADER_INC', true);
342 global $template, $lang, $stage, $phpbb_root_path;
344 $template->assign_vars(array(
345 'L_CHANGE' => $lang['CHANGE'],
346 'L_INSTALL_PANEL' => $lang['INSTALL_PANEL'],
347 'L_SELECT_LANG' => $lang['SELECT_LANG'],
348 'L_SKIP' => $lang['SKIP'],
349 'PAGE_TITLE' => $this->get_page_title(),
350 'T_IMAGE_PATH' => $phpbb_root_path . 'adm/images/',
352 'S_CONTENT_DIRECTION' => $lang['DIRECTION'],
353 'S_CONTENT_FLOW_BEGIN' => ($lang['DIRECTION'] == 'ltr') ? 'left' : 'right',
354 'S_CONTENT_FLOW_END' => ($lang['DIRECTION'] == 'ltr') ? 'right' : 'left',
355 'S_CONTENT_ENCODING' => 'UTF-8',
357 'S_USER_LANG' => $lang['USER_LANG'],
361 header('Content-type: text/html; charset=UTF-8');
362 header('Cache-Control: private, no-cache="set-cookie"');
363 header('Expires: 0');
364 header('Pragma: no-cache');
370 * Output the standard page footer
372 function page_footer()
374 global $db, $template;
376 $template->display('body');
378 // Close our DB connection.
379 if (!empty($db) && is_object($db))
384 if (function_exists('exit_handler'))
391 * Returns desired template name
393 function get_tpl_name()
395 return $this->module->tpl_name . '.html';
399 * Returns the desired page title
401 function get_page_title()
405 if (!isset($this->module->page_title))
410 return (isset($lang[$this->module->page_title])) ? $lang[$this->module->page_title] : $this->module->page_title;
414 * Generate an HTTP/1.1 header to redirect the user to another page
415 * This is used during the installation when we do not have a database available to call the normal redirect function
416 * @param string $page The page to redirect to relative to the installer root path
418 function redirect($page)
420 // HTTP_HOST is having the correct browser url in most cases...
421 $server_name = (!empty($_SERVER['HTTP_HOST'])) ? strtolower($_SERVER['HTTP_HOST']) : ((!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME'));
422 $server_port = (!empty($_SERVER['SERVER_PORT'])) ? (int) $_SERVER['SERVER_PORT'] : (int) getenv('SERVER_PORT');
423 $secure = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 1 : 0;
425 $script_name = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : getenv('PHP_SELF');
428 $script_name = (!empty($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : getenv('REQUEST_URI');
431 // Replace backslashes and doubled slashes (could happen on some proxy setups)
432 $script_name = str_replace(array('\\', '//'), '/', $script_name);
433 $script_path = trim(dirname($script_name));
435 $url = (($secure) ? 'https://' : 'http://') . $server_name;
437 if ($server_port && (($secure && $server_port <> 443) || (!$secure && $server_port <> 80)))
439 // HTTP HOST can carry a port number...
440 if (strpos($server_name, ':') === false)
442 $url .= ':' . $server_port;
446 $url .= $script_path . '/' . $page;
447 header('Location: ' . $url);
452 * Generate the navigation tabs
454 function generate_navigation()
456 global $lang, $template, $phpEx, $language;
458 if (is_array($this->module_ary))
460 @ksort($this->module_ary);
461 foreach ($this->module_ary as $cat_ary)
463 $cat = $cat_ary['name'];
464 $l_cat = (!empty($lang['CAT_' . $cat])) ? $lang['CAT_' . $cat] : preg_replace('#_#', ' ', $cat);
465 $cat = strtolower($cat);
466 $url = $this->module_url . "?mode=$cat&language=$language";
468 if ($this->mode == $cat)
470 $template->assign_block_vars('t_block1', array(
472 'S_SELECTED' => true,
476 if (is_array($this->module_ary[$this->id]['subs']))
478 $subs = $this->module_ary[$this->id]['subs'];
479 foreach ($subs as $option)
481 $l_option = (!empty($lang['SUB_' . $option])) ? $lang['SUB_' . $option] : preg_replace('#_#', ' ', $option);
482 $option = strtolower($option);
483 $url = $this->module_url . '?mode=' . $this->mode . "&sub=$option&language=$language";
485 $template->assign_block_vars('l_block1', array(
486 'L_TITLE' => $l_option,
487 'S_SELECTED' => ($this->sub == $option),
493 if (is_array($this->module_ary[$this->id]['stages']))
495 $subs = $this->module_ary[$this->id]['stages'];
497 foreach ($subs as $option)
499 $l_option = (!empty($lang['STAGE_' . $option])) ? $lang['STAGE_' . $option] : preg_replace('#_#', ' ', $option);
500 $option = strtolower($option);
501 $matched = ($this->sub == $option) ? true : $matched;
503 $template->assign_block_vars('l_block2', array(
504 'L_TITLE' => $l_option,
505 'S_SELECTED' => ($this->sub == $option),
506 'S_COMPLETE' => !$matched,
513 $template->assign_block_vars('t_block1', array(
515 'S_SELECTED' => false,
524 * Output an error message
525 * If skip is true, return and continue execution, else exit
527 function error($error, $line, $file, $skip = false)
529 global $lang, $db, $template;
533 $template->assign_block_vars('checks', array(
535 'LEGEND' => $lang['INST_ERR'],
538 $template->assign_block_vars('checks', array(
539 'TITLE' => basename($file) . ' [ ' . $line . ' ]',
540 'RESULT' => '<b style="color:red">' . $error . '</b>',
546 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
547 echo '<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">';
549 echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
550 echo '<title>' . $lang['INST_ERR_FATAL'] . '</title>';
551 echo '<link href="../adm/style/admin.css" rel="stylesheet" type="text/css" media="screen" />';
553 echo '<body id="errorpage">';
554 echo '<div id="wrap">';
555 echo ' <div id="page-header">';
557 echo ' <div id="page-body">';
558 echo ' <div id="acp">';
559 echo ' <div class="panel">';
560 echo ' <span class="corners-top"><span></span></span>';
561 echo ' <div id="content">';
562 echo ' <h1>' . $lang['INST_ERR_FATAL'] . '</h1>';
563 echo ' <p>' . $lang['INST_ERR_FATAL'] . "</p>\n";
564 echo ' <p>' . basename($file) . ' [ ' . $line . " ]</p>\n";
565 echo ' <p><b>' . $error . "</b></p>\n";
567 echo ' <span class="corners-bottom"><span></span></span>';
571 echo ' <div id="page-footer">';
572 echo ' Powered by <a href="http://www.phpbb.com/">phpBB</a>® Forum Software © phpBB Group';
578 if (!empty($db) && is_object($db))
587 * Output an error message for a database related problem
588 * If skip is true, return and continue execution, else exit
590 function db_error($error, $sql, $line, $file, $skip = false)
592 global $lang, $db, $template;
596 $template->assign_block_vars('checks', array(
598 'LEGEND' => $lang['INST_ERR_FATAL'],
601 $template->assign_block_vars('checks', array(
602 'TITLE' => basename($file) . ' [ ' . $line . ' ]',
603 'RESULT' => '<b style="color:red">' . $error . '</b><br />» SQL:' . $sql,
609 $template->set_filenames(array(
610 'body' => 'install_error.html')
612 $this->page_header();
613 $this->generate_navigation();
615 $template->assign_vars(array(
616 'MESSAGE_TITLE' => $lang['INST_ERR_FATAL_DB'],
617 'MESSAGE_TEXT' => '<p>' . basename($file) . ' [ ' . $line . ' ]</p><p>SQL : ' . $sql . '</p><p><b>' . $error . '</b></p>',
620 // Rollback if in transaction
621 if ($db->transaction)
623 $db->sql_transaction('rollback');
626 $this->page_footer();
630 * Generate the relevant HTML for an input field and the associated label and explanatory text
632 function input_field($name, $type, $value='', $options='')
635 $tpl_type = explode(':', $type);
638 switch ($tpl_type[0])
642 $size = (int) $tpl_type[1];
643 $maxlength = (int) $tpl_type[2];
645 $tpl = '<input id="' . $name . '" type="' . $tpl_type[0] . '"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="' . $name . '" value="' . $value . '" />';
649 $rows = (int) $tpl_type[1];
650 $cols = (int) $tpl_type[2];
652 $tpl = '<textarea id="' . $name . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' . $value . '</textarea>';
656 $key_yes = ($value) ? ' checked="checked" id="' . $name . '"' : '';
657 $key_no = (!$value) ? ' checked="checked" id="' . $name . '"' : '';
659 $tpl_type_cond = explode('_', $tpl_type[1]);
660 $type_no = ($tpl_type_cond[0] == 'disabled' || $tpl_type_cond[0] == 'enabled') ? false : true;
662 $tpl_no = '<label><input type="radio" name="' . $name . '" value="0"' . $key_no . ' class="radio" /> ' . (($type_no) ? $lang['NO'] : $lang['DISABLED']) . '</label>';
663 $tpl_yes = '<label><input type="radio" name="' . $name . '" value="1"' . $key_yes . ' class="radio" /> ' . (($type_no) ? $lang['YES'] : $lang['ENABLED']) . '</label>';
665 $tpl = ($tpl_type_cond[0] == 'yes' || $tpl_type_cond[0] == 'enabled') ? $tpl_yes . ' ' . $tpl_no : $tpl_no . ' ' . $tpl_yes;
669 eval('$s_options = ' . str_replace('{VALUE}', $value, $options) . ';');
670 $tpl = '<select id="' . $name . '" name="' . $name . '">' . $s_options . '</select>';
674 eval('$tpl = ' . str_replace('{VALUE}', $value, $options) . ';');
685 * Generate the drop down of available language packs
687 function inst_language_select($default = '')
689 global $phpbb_root_path, $phpEx;
691 $dir = @opendir($phpbb_root_path . 'language');
695 $this->error('Unable to access the language directory', __LINE__, __FILE__);
698 while ($file = readdir($dir))
700 $path = $phpbb_root_path . 'language/' . $file;
702 if ($file == '.' || $file == '..' || is_link($path) || is_file($path) || $file == 'CVS')
707 if (file_exists($path . '/iso.txt'))
709 list($displayname, $localname) = @file($path . '/iso.txt');
710 $lang[$localname] = $file;
719 foreach ($lang as $displayname => $filename)
721 $selected = (strtolower($default) == strtolower($filename)) ? ' selected="selected"' : '';
722 $user_select .= '<option value="' . $filename . '"' . $selected . '>' . ucwords($displayname) . '</option>';