$config['dbtype'], "database" => $config['database'], "table_prefix" => $config['table_prefix'], "hostname" => $config['hostname'], "username" => $config['username'], "password" => $config['password'], "encoding" => $config['db_encoding'], ); } $mybb->config = &$config; // Include the files necessary for installation require_once MYBB_ROOT."inc/class_timers.php"; require_once MYBB_ROOT.'inc/class_language.php'; $lang = new MyLanguage(); $lang->set_path(INSTALL_ROOT.'resources/'); $lang->load('language'); // If we're upgrading from an SQLite installation, make sure we still work. if($config['database']['type'] == 'sqlite3' || $config['database']['type'] == 'sqlite2') { $config['database']['type'] = 'sqlite'; } // Load DB interface require_once MYBB_ROOT."inc/db_base.php"; require_once MYBB_ROOT . 'inc/AbstractPdoDbDriver.php'; require_once MYBB_ROOT."inc/db_{$config['database']['type']}.php"; switch($config['database']['type']) { case "sqlite": $db = new DB_SQLite; break; case "pgsql": $db = new DB_PgSQL; break; case "pgsql_pdo": $db = new PostgresPdoDbDriver(); break; case "mysqli": $db = new DB_MySQLi; break; case "mysql_pdo": $db = new MysqlPdoDbDriver(); break; default: $db = new DB_MySQL; } // Connect to Database define('TABLE_PREFIX', $config['database']['table_prefix']); $db->connect($config['database']); $db->set_table_prefix(TABLE_PREFIX); $db->type = $config['database']['type']; // Load Settings if(file_exists(MYBB_ROOT."inc/settings.php")) { require_once MYBB_ROOT."inc/settings.php"; } if(!file_exists(MYBB_ROOT."inc/settings.php") || !$settings) { if(function_exists('rebuild_settings')) { rebuild_settings(); } else { $options = array( "order_by" => "title", "order_dir" => "ASC" ); $query = $db->simple_select("settings", "value, name", "", $options); $settings = array(); while($setting = $db->fetch_array($query)) { $setting['value'] = str_replace("\"", "\\\"", $setting['value']); $settings[$setting['name']] = $setting['value']; } } } $settings['wolcutoff'] = $settings['wolcutoffmins']*60; $settings['bbname_orig'] = $settings['bbname']; $settings['bbname'] = strip_tags($settings['bbname']); // Fix for people who for some specify a trailing slash on the board URL if(substr($settings['bburl'], -1) == "/") { $settings['bburl'] = my_substr($settings['bburl'], 0, -1); } $mybb->settings = &$settings; $mybb->parse_cookies(); require_once MYBB_ROOT."inc/class_datacache.php"; $cache = new datacache; // Load cache $cache->cache(); $mybb->cache = &$cache; require_once MYBB_ROOT."inc/class_session.php"; $session = new session; $session->init(); $mybb->session = &$session; // Include the necessary contants for installation $grouppermignore = array("gid", "type", "title", "description", "namestyle", "usertitle", "stars", "starimage", "image"); $groupzerogreater = array("pmquota", "maxpmrecipients", "maxreputationsday", "attachquota", "maxemails", "maxwarningsday", "maxposts", "edittimelimit", "canusesigxposts", "maxreputationsperuser", "maxreputationsperthread", "emailfloodtime"); $displaygroupfields = array("title", "description", "namestyle", "usertitle", "stars", "starimage", "image"); $fpermfields = array('canview', 'canviewthreads', 'candlattachments', 'canpostthreads', 'canpostreplys', 'canpostattachments', 'canratethreads', 'caneditposts', 'candeleteposts', 'candeletethreads', 'caneditattachments', 'canpostpolls', 'canvotepolls', 'cansearch', 'modposts', 'modthreads', 'modattachments', 'mod_edit_posts'); // Include the installation resources require_once INSTALL_ROOT."resources/output.php"; $output = new installerOutput; $output->script = "upgrade.php"; $output->title = "MyBB Upgrade Wizard"; if(file_exists("lock")) { $output->print_error($lang->locked); } else { $mybb->input['action'] = $mybb->get_input('action'); if($mybb->input['action'] == "logout" && $mybb->user['uid']) { // Check session ID if we have one if($mybb->get_input('logoutkey') !== $mybb->user['logoutkey']) { $output->print_error("Your user ID could not be verified to log you out. This may have been because a malicious Javascript was attempting to log you out automatically. If you intended to log out, please click the Log Out button at the top menu."); } my_unsetcookie("mybbuser"); if($mybb->user['uid']) { $time = TIME_NOW; $lastvisit = array( "lastactive" => $time-900, "lastvisit" => $time, ); $db->update_query("users", $lastvisit, "uid='".$mybb->user['uid']."'"); } header("Location: upgrade.php"); } else if($mybb->input['action'] == "do_login" && $mybb->request_method == "post") { require_once MYBB_ROOT."inc/functions_user.php"; if(!username_exists($mybb->get_input('username'))) { $output->print_error("The username you have entered appears to be invalid."); } $options = array( 'fields' => array('username', 'password', 'salt', 'loginkey') ); $user = get_user_by_username($mybb->get_input('username'), $options); if(!$user) { $output->print_error("The username you have entered appears to be invalid."); } else { $user = validate_password_from_uid($user['uid'], $mybb->get_input('password'), $user); if(!$user) { $output->print_error("The password you entered is incorrect. If you have forgotten your password, click here. Otherwise, go back and try again."); } } my_setcookie("mybbuser", $user['uid']."_".$user['loginkey'], null, true, "lax"); header("Location: ./upgrade.php"); } $output->steps = array($lang->upgrade); if($mybb->user['uid'] == 0) { $output->print_header($lang->please_login, "errormsg", 0, 1); $output->print_contents('
'.$lang->login_desc.'
'); $output->print_footer(""); exit; } else if($mybb->usergroup['cancp'] != 1 && $mybb->usergroup['cancp'] != 'yes') { $output->print_error($lang->sprintf($lang->no_permision, $mybb->user['logoutkey'])); } if(!$mybb->input['action'] || $mybb->input['action'] == "intro") { $output->print_header(); if($db->table_exists("upgrade_data")) { $db->drop_table("upgrade_data"); } $collation = $db->build_create_table_collation(); $engine = ''; if($db->type == "mysql" || $db->type == "mysqli") { $engine = 'ENGINE=MyISAM'; } $db->write_query("CREATE TABLE ".TABLE_PREFIX."upgrade_data ( title varchar(30) NOT NULL, contents text NOT NULL, UNIQUE (title) ) {$engine}{$collation};"); $dh = opendir(INSTALL_ROOT."resources"); $upgradescripts = array(); while(($file = readdir($dh)) !== false) { if(preg_match("#upgrade([0-9]+).php$#i", $file, $match)) { $upgradescripts[$match[1]] = $file; $key_order[] = $match[1]; } } closedir($dh); natsort($key_order); $key_order = array_reverse($key_order); // Figure out which version we last updated from (as of 1.6) $version_history = $cache->read("version_history"); // If array is empty then we must be upgrading to 1.6 since that's when this feature was added if(empty($version_history)) { $next_update_version = 17; // 16+1 } else { $next_update_version = (int)(end($version_history)+1); } $vers = ''; foreach($key_order as $k => $key) { $file = $upgradescripts[$key]; $upgradescript = file_get_contents(INSTALL_ROOT."resources/$file"); preg_match("#Upgrade Script:(.*)#i", $upgradescript, $verinfo); preg_match("#upgrade([0-9]+).php$#i", $file, $keynum); if(trim($verinfo[1])) { if($keynum[1] == $next_update_version) { $vers .= "\n"; } else { $vers .= "\n"; } } } unset($upgradescripts); unset($upgradescript); if(end($version_history) == reset($key_order) && empty($mybb->input['force'])) { $output->print_contents($lang->upgrade_not_needed); $output->print_footer("finished"); } else { $output->print_contents($lang->sprintf($lang->upgrade_welcome, $mybb->version)."".$lang->upgrade_send_stats); $output->print_footer("doupgrade"); } } elseif($mybb->input['action'] == "doupgrade") { add_upgrade_store("allow_anonymous_info", $mybb->get_input('allow_anonymous_info', MyBB::INPUT_INT)); require_once INSTALL_ROOT."resources/upgrade".$mybb->get_input('from', MyBB::INPUT_INT).".php"; if($db->table_exists("datacache") && !empty($upgrade_detail['requires_deactivated_plugins']) && $mybb->get_input('donewarning') != "true") { $plugins = $cache->read('plugins', true); if(!empty($plugins['active'])) { $output->print_header(); $lang->plugin_warning = "get_input('from', MyBB::INPUT_INT)."\" />\n\n
There are still ".count($plugins['active'])." plugin(s) active. Active plugins can sometimes cause problems during an upgrade procedure or may break your forum afterward. It is strongly reccommended that you deactivate your plugins before continuing.
".$lang->upgrade_continue."
"); $output->print_footer("finished"); } /** * Called as latest function. Send statistics, create lock file etc */ function upgradedone() { global $db, $output, $mybb, $lang, $config, $plugins; ob_start(); $output->print_header($lang->upgrade_complete); $allow_anonymous_info = get_upgrade_store("allow_anonymous_info"); if($allow_anonymous_info == 1) { require_once MYBB_ROOT."inc/functions_serverstats.php"; $build_server_stats = build_server_stats(0, '', $mybb->version_code, $mybb->config['database']['encoding']); if($build_server_stats['info_sent_success'] == false) { echo $build_server_stats['info_image']; } } ob_end_flush(); // Attempt to run an update check require_once MYBB_ROOT.'inc/functions_task.php'; $query = $db->simple_select('tasks', 'tid', "file='versioncheck'"); $update_check = $db->fetch_array($query); if($update_check) { // Load plugin system for update check require_once MYBB_ROOT."inc/class_plugins.php"; $plugins = new pluginSystem; run_task($update_check['tid']); } if(is_writable("./")) { $lock = @fopen("./lock", "w"); $written = @fwrite($lock, "1"); @fclose($lock); if($written) { $lock_note = $lang->sprintf($lang->upgrade_locked, $config['admin_dir']); } } if(empty($written)) { $lock_note = "".$lang->upgrade_removedir."
"; } // Rebuild inc/settings.php at the end of the upgrade if(function_exists('rebuild_settings')) { rebuild_settings(); } else { $options = array( "order_by" => "title", "order_dir" => "ASC" ); $query = $db->simple_select("settings", "value, name", "", $options); while($setting = $db->fetch_array($query)) { $setting['value'] = str_replace("\"", "\\\"", $setting['value']); $settings[$setting['name']] = $setting['value']; } } $output->print_contents($lang->sprintf($lang->upgrade_congrats, $mybb->version, $lock_note)); $output->print_footer(); } /** * Show the finish page */ function whatsnext() { global $output, $db, $system_upgrade_detail, $lang; if($system_upgrade_detail['revert_all_templates'] > 0) { $output->print_header($lang->upgrade_template_reversion); $output->print_contents($lang->upgrade_template_reversion_success); $output->print_footer("templates"); } else { upgradethemes(); } } /** * Determine the next function we need to call * * @param int $from * @param string $func * * @return string */ function next_function($from, $func="dbchanges") { global $oldvers, $system_upgrade_detail, $currentscript, $cache; load_module("upgrade".$from.".php"); if(function_exists("upgrade".$from."_".$func)) { $function = "upgrade".$from."_".$func; } else { // We're done with our last upgrade script, so add it to the upgrade scripts we've already completed. $version_history = $cache->read("version_history"); $version_history[$from] = $from; $cache->update("version_history", $version_history); $from = $from+1; if(file_exists(INSTALL_ROOT."resources/upgrade".$from.".php")) { $function = next_function($from); } } if(empty($function)) { $function = "whatsnext"; } return $function; } /** * @param string $module */ function load_module($module) { global $system_upgrade_detail, $currentscript, $upgrade_detail; require_once INSTALL_ROOT."resources/".$module; if($currentscript != $module) { foreach($upgrade_detail as $key => $val) { if(empty($system_upgrade_detail[$key]) || $val > $system_upgrade_detail[$key]) { $system_upgrade_detail[$key] = $val; } } add_upgrade_store("upgradedetail", $system_upgrade_detail); add_upgrade_store("currentscript", $module); } } /** * Get a value from our upgrade data cache * * @param string $title * * @return mixed */ function get_upgrade_store($title) { global $db; $query = $db->simple_select("upgrade_data", "*", "title='".$db->escape_string($title)."'"); $data = $db->fetch_array($query); if(!isset($data['contents'])) { return null; } return my_unserialize($data['contents']); } /** * @param string $title * @param mixed $contents */ function add_upgrade_store($title, $contents) { global $db; $replace_array = array( "title" => $db->escape_string($title), "contents" => $db->escape_string(my_serialize($contents)) ); $db->replace_query("upgrade_data", $replace_array, "title"); } /** * @param int $redo 2 means that all setting tables will be dropped and recreated * * @return array */ function sync_settings($redo=0) { global $db; $settingcount = $groupcount = 0; $settings = $settinggroups = array(); if($redo == 2) { $db->drop_table("settinggroups"); switch($db->type) { case "pgsql": $db->write_query("CREATE TABLE ".TABLE_PREFIX."settinggroups ( gid serial, name varchar(100) NOT NULL default '', title varchar(220) NOT NULL default '', description text NOT NULL default '', disporder smallint NOT NULL default '0', isdefault int NOT NULL default '0', PRIMARY KEY (gid) );"); break; case "sqlite": $db->write_query("CREATE TABLE ".TABLE_PREFIX."settinggroups ( gid INTEGER PRIMARY KEY, name varchar(100) NOT NULL default '', title varchar(220) NOT NULL default '', description TEXT NOT NULL, disporder smallint NOT NULL default '0', isdefault int(1) NOT NULL default '0' );"); break; case "mysql": default: $db->write_query("CREATE TABLE ".TABLE_PREFIX."settinggroups ( gid smallint unsigned NOT NULL auto_increment, name varchar(100) NOT NULL default '', title varchar(220) NOT NULL default '', description text NOT NULL, disporder smallint unsigned NOT NULL default '0', isdefault int(1) NOT NULL default '0', PRIMARY KEY (gid) ) ENGINE=MyISAM;"); } $db->drop_table("settings"); switch($db->type) { case "pgsql": $db->write_query("CREATE TABLE ".TABLE_PREFIX."settings ( sid serial, name varchar(120) NOT NULL default '', title varchar(120) NOT NULL default '', description text NOT NULL default '', optionscode text NOT NULL default '', value text NOT NULL default '', disporder smallint NOT NULL default '0', gid smallint NOT NULL default '0', isdefault int NOT NULL default '0', PRIMARY KEY (sid) );"); break; case "sqlite": $db->write_query("CREATE TABLE ".TABLE_PREFIX."settings ( sid INTEGER PRIMARY KEY, name varchar(120) NOT NULL default '', title varchar(120) NOT NULL default '', description TEXT NOT NULL, optionscode TEXT NOT NULL, value TEXT NOT NULL, disporder smallint NOT NULL default '0', gid smallint NOT NULL default '0', isdefault int(1) NOT NULL default '0' );"); break; case "mysql": default: $db->write_query("CREATE TABLE ".TABLE_PREFIX."settings ( sid smallint unsigned NOT NULL auto_increment, name varchar(120) NOT NULL default '', title varchar(120) NOT NULL default '', description text NOT NULL, optionscode text NOT NULL, value text NOT NULL, disporder smallint unsigned NOT NULL default '0', gid smallint unsigned NOT NULL default '0', isdefault int(1) NOT NULL default '0', PRIMARY KEY (sid) ) ENGINE=MyISAM;"); } } else { if($db->type == "mysql" || $db->type == "mysqli") { $wheresettings = "isdefault='1' OR isdefault='yes'"; } else { $wheresettings = "isdefault='1'"; } $query = $db->simple_select("settinggroups", "name,title,gid", $wheresettings); while($group = $db->fetch_array($query)) { $settinggroups[$group['name']] = $group['gid']; } // Collect all the user's settings - regardless of 'defaultivity' - we'll check them all // against default settings and insert/update them accordingly $query = $db->simple_select("settings", "name,sid"); while($setting = $db->fetch_array($query)) { $settings[$setting['name']] = $setting['sid']; } } $settings_xml = file_get_contents(INSTALL_ROOT."resources/settings.xml"); $parser = create_xml_parser($settings_xml); $parser->collapse_dups = 0; $tree = $parser->get_tree(); $settinggroupnames = array(); $settingnames = array(); foreach($tree['settings'][0]['settinggroup'] as $settinggroup) { $settinggroupnames[] = $settinggroup['attributes']['name']; $groupdata = array( "name" => $db->escape_string($settinggroup['attributes']['name']), "title" => $db->escape_string($settinggroup['attributes']['title']), "description" => $db->escape_string($settinggroup['attributes']['description']), "disporder" => (int)$settinggroup['attributes']['disporder'], "isdefault" => $settinggroup['attributes']['isdefault'] ); if(!$settinggroups[$settinggroup['attributes']['name']] || $redo == 2) { $gid = $db->insert_query("settinggroups", $groupdata); ++$groupcount; } else { $gid = $settinggroups[$settinggroup['attributes']['name']]; $db->update_query("settinggroups", $groupdata, "gid='{$gid}'"); } if(!$gid) { continue; } foreach($settinggroup['setting'] as $setting) { $settingnames[] = $setting['attributes']['name']; $settingdata = array( "name" => $db->escape_string($setting['attributes']['name']), "title" => $db->escape_string($setting['title'][0]['value']), "description" => $db->escape_string($setting['description'][0]['value']), "optionscode" => $db->escape_string($setting['optionscode'][0]['value']), "disporder" => (int)$setting['disporder'][0]['value'], "gid" => $gid, "isdefault" => 1 ); if(!$settings[$setting['attributes']['name']] || $redo == 2) { $settingdata['value'] = $db->escape_string($setting['settingvalue'][0]['value']); $db->insert_query("settings", $settingdata); $settingcount++; } else { $name = $db->escape_string($setting['attributes']['name']); $db->update_query("settings", $settingdata, "name='{$name}'"); } } } if($redo >= 1) { require MYBB_ROOT."inc/settings.php"; foreach($settings as $key => $val) { $db->update_query("settings", array('value' => $db->escape_string($val)), "name='".$db->escape_string($key)."'"); } } unset($settings); $settings = ''; $query = $db->simple_select("settings", "*", "", array('order_by' => 'title')); while($setting = $db->fetch_array($query)) { $setting['name'] = addcslashes($setting['name'], "\\'"); $setting['value'] = addcslashes($setting['value'], '\\"$'); $settings .= "\$settings['{$setting['name']}'] = \"".$setting['value']."\";\n"; } $settings = "drop_table("tasks"); switch($db->type) { case "pgsql": $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks ( tid serial, title varchar(120) NOT NULL default '', description text NOT NULL default '', file varchar(30) NOT NULL default '', minute varchar(200) NOT NULL default '', hour varchar(200) NOT NULL default '', day varchar(100) NOT NULL default '', month varchar(30) NOT NULL default '', weekday varchar(15) NOT NULL default '', nextrun bigint NOT NULL default '0', lastrun bigint NOT NULL default '0', enabled int NOT NULL default '1', logging int NOT NULL default '0', locked bigint NOT NULL default '0', PRIMARY KEY(tid) );"); break; case "sqlite": $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks ( tid INTEGER PRIMARY KEY, title varchar(120) NOT NULL default '', description TEXT NOT NULL, file varchar(30) NOT NULL default '', minute varchar(200) NOT NULL default '', hour varchar(200) NOT NULL default '', day varchar(100) NOT NULL default '', month varchar(30) NOT NULL default '', weekday varchar(15) NOT NULL default '', nextrun bigint(30) NOT NULL default '0', lastrun bigint(30) NOT NULL default '0', enabled int(1) NOT NULL default '1', logging int(1) NOT NULL default '0', locked bigint(30) NOT NULL default '0' );"); break; case "mysql": default: $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks ( tid int unsigned NOT NULL auto_increment, title varchar(120) NOT NULL default '', description text NOT NULL, file varchar(30) NOT NULL default '', minute varchar(200) NOT NULL default '', hour varchar(200) NOT NULL default '', day varchar(100) NOT NULL default '', month varchar(30) NOT NULL default '', weekday varchar(15) NOT NULL default '', nextrun bigint(30) NOT NULL default '0', lastrun bigint(30) NOT NULL default '0', enabled int(1) NOT NULL default '1', logging int(1) NOT NULL default '0', locked bigint(30) NOT NULL default '0', PRIMARY KEY (tid) ) ENGINE=MyISAM;"); } } else { $query = $db->simple_select("tasks", "file,tid"); while($task = $db->fetch_array($query)) { $tasks[$task['file']] = $task['tid']; } } require_once MYBB_ROOT."inc/functions_task.php"; $task_file = file_get_contents(INSTALL_ROOT.'resources/tasks.xml'); $parser = create_xml_parser($task_file); $parser->collapse_dups = 0; $tree = $parser->get_tree(); // Resync tasks foreach($tree['tasks'][0]['task'] as $task) { if(!$tasks[$task['file'][0]['value']] || $redo == 2) { $new_task = array( 'title' => $db->escape_string($task['title'][0]['value']), 'description' => $db->escape_string($task['description'][0]['value']), 'file' => $db->escape_string($task['file'][0]['value']), 'minute' => $db->escape_string($task['minute'][0]['value']), 'hour' => $db->escape_string($task['hour'][0]['value']), 'day' => $db->escape_string($task['day'][0]['value']), 'weekday' => $db->escape_string($task['weekday'][0]['value']), 'month' => $db->escape_string($task['month'][0]['value']), 'enabled' => $db->escape_string($task['enabled'][0]['value']), 'logging' => $db->escape_string($task['logging'][0]['value']) ); $new_task['nextrun'] = fetch_next_run($new_task); $db->insert_query("tasks", $new_task); $taskcount++; } else { $update_task = array( 'title' => $db->escape_string($task['title'][0]['value']), 'description' => $db->escape_string($task['description'][0]['value']), 'file' => $db->escape_string($task['file'][0]['value']), ); $db->update_query("tasks", $update_task, "file='".$db->escape_string($task['file'][0]['value'])."'"); } } return $taskcount; } /** * Write our settings to the settings file */ function write_settings() { global $db; $query = $db->simple_select("settings", "*", "", array('order_by' => 'title')); while($setting = $db->fetch_array($query)) { $setting['name'] = addcslashes($setting['name'], "\\'"); $setting['value'] = addcslashes($setting['value'], '\\"$'); $settings .= "\$settings['{$setting['name']}'] = \"{$setting['value']}\";\n"; } if(!empty($settings)) { $settings = "