install/database_update.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 16:41:10 +0400
changeset 0 ceef6d64c21d
permissions -rw-r--r--
Added phpBB-3.0.10
     1 <?php
     2 /**
     3 *
     4 * @package install
     5 * @version $Id$
     6 * @copyright (c) 2006 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 define('UPDATES_TO_VERSION', '3.0.10');
    12 
    13 // Enter any version to update from to test updates. The version within the db will not be updated.
    14 define('DEBUG_FROM_VERSION', false);
    15 
    16 // Which oldest version does this updater support?
    17 define('OLDEST_FROM_VERSION', '3.0.0');
    18 
    19 // Return if we "just include it" to find out for which version the database update is responsible for
    20 if (defined('IN_PHPBB') && defined('IN_INSTALL'))
    21 {
    22 	$updates_to_version = UPDATES_TO_VERSION;
    23 	$debug_from_version = DEBUG_FROM_VERSION;
    24 	$oldest_from_version = OLDEST_FROM_VERSION;
    25 
    26 	return;
    27 }
    28 
    29 /**
    30 */
    31 define('IN_PHPBB', true);
    32 define('IN_INSTALL', true);
    33 
    34 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
    35 $phpEx = substr(strrchr(__FILE__, '.'), 1);
    36 
    37 if (!function_exists('phpbb_require_updated'))
    38 {
    39 	function phpbb_require_updated($path, $optional = false)
    40 	{
    41 		global $phpbb_root_path;
    42 
    43 		$new_path = $phpbb_root_path . 'install/update/new/' . $path;
    44 		$old_path = $phpbb_root_path . $path;
    45 
    46 		if (file_exists($new_path))
    47 		{
    48 			require($new_path);
    49 		}
    50 		else if (!$optional || file_exists($old_path))
    51 		{
    52 			require($old_path);
    53 		}
    54 	}
    55 }
    56 
    57 phpbb_require_updated('includes/startup.' . $phpEx);
    58 
    59 $updates_to_version = UPDATES_TO_VERSION;
    60 $debug_from_version = DEBUG_FROM_VERSION;
    61 $oldest_from_version = OLDEST_FROM_VERSION;
    62 
    63 error_reporting(E_ALL);
    64 
    65 @set_time_limit(0);
    66 
    67 // Include essential scripts
    68 include($phpbb_root_path . 'config.' . $phpEx);
    69 
    70 if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
    71 {
    72 	die("Please read: <a href='../docs/INSTALL.html'>INSTALL.html</a> before attempting to update.");
    73 }
    74 
    75 // Load Extensions
    76 if (!empty($load_extensions) && function_exists('dl'))
    77 {
    78 	$load_extensions = explode(',', $load_extensions);
    79 
    80 	foreach ($load_extensions as $extension)
    81 	{
    82 		@dl(trim($extension));
    83 	}
    84 }
    85 
    86 // Include files
    87 require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
    88 require($phpbb_root_path . 'includes/cache.' . $phpEx);
    89 require($phpbb_root_path . 'includes/template.' . $phpEx);
    90 require($phpbb_root_path . 'includes/session.' . $phpEx);
    91 require($phpbb_root_path . 'includes/auth.' . $phpEx);
    92 
    93 require($phpbb_root_path . 'includes/functions.' . $phpEx);
    94 
    95 phpbb_require_updated('includes/functions_content.' . $phpEx, true);
    96 
    97 require($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
    98 require($phpbb_root_path . 'includes/constants.' . $phpEx);
    99 require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
   100 require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
   101 
   102 phpbb_require_updated('includes/db/db_tools.' . $phpEx);
   103 
   104 // new table constants are separately defined here in case the updater is run
   105 // before the files are updated
   106 if (!defined('LOGIN_ATTEMPT_TABLE'))
   107 {
   108 	define('LOGIN_ATTEMPT_TABLE', $table_prefix . 'login_attempts');
   109 }
   110 
   111 $user = new user();
   112 $cache = new cache();
   113 $db = new $sql_db();
   114 
   115 // Add own hook handler, if present. :o
   116 if (file_exists($phpbb_root_path . 'includes/hooks/index.' . $phpEx))
   117 {
   118 	require($phpbb_root_path . 'includes/hooks/index.' . $phpEx);
   119 	$phpbb_hook = new phpbb_hook(array('exit_handler', 'phpbb_user_session_handler', 'append_sid', array('template', 'display')));
   120 
   121 	foreach ($cache->obtain_hooks() as $hook)
   122 	{
   123 		@include($phpbb_root_path . 'includes/hooks/' . $hook . '.' . $phpEx);
   124 	}
   125 }
   126 else
   127 {
   128 	$phpbb_hook = false;
   129 }
   130 
   131 // Connect to DB
   132 $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false);
   133 
   134 // We do not need this any longer, unset for safety purposes
   135 unset($dbpasswd);
   136 
   137 $user->ip = (!empty($_SERVER['REMOTE_ADDR'])) ? htmlspecialchars($_SERVER['REMOTE_ADDR']) : '';
   138 $user->ip = (stripos($user->ip, '::ffff:') === 0) ? substr($user->ip, 7) : $user->ip;
   139 
   140 $sql = "SELECT config_value
   141 	FROM " . CONFIG_TABLE . "
   142 	WHERE config_name = 'default_lang'";
   143 $result = $db->sql_query($sql);
   144 $row = $db->sql_fetchrow($result);
   145 $db->sql_freeresult($result);
   146 
   147 $language = basename(request_var('language', ''));
   148 
   149 if (!$language)
   150 {
   151 	$language = $row['config_value'];
   152 }
   153 
   154 if (!file_exists($phpbb_root_path . 'language/' . $language))
   155 {
   156 	die('No language found!');
   157 }
   158 
   159 // And finally, load the relevant language files
   160 include($phpbb_root_path . 'language/' . $language . '/common.' . $phpEx);
   161 include($phpbb_root_path . 'language/' . $language . '/acp/common.' . $phpEx);
   162 include($phpbb_root_path . 'language/' . $language . '/install.' . $phpEx);
   163 
   164 // Set PHP error handler to ours
   165 //set_error_handler('msg_handler');
   166 
   167 // Define some variables for the database update
   168 $inline_update = (request_var('type', 0)) ? true : false;
   169 
   170 // To let set_config() calls succeed, we need to make the config array available globally
   171 $config = array();
   172 
   173 $sql = 'SELECT *
   174 	FROM ' . CONFIG_TABLE;
   175 $result = $db->sql_query($sql);
   176 
   177 while ($row = $db->sql_fetchrow($result))
   178 {
   179 	$config[$row['config_name']] = $row['config_value'];
   180 }
   181 $db->sql_freeresult($result);
   182 
   183 // phpbb_db_tools will be taken from new files (under install/update/new)
   184 // if possible, falling back to the board's copy.
   185 $db_tools = new phpbb_db_tools($db, true);
   186 
   187 $database_update_info = database_update_info();
   188 
   189 $error_ary = array();
   190 $errored = false;
   191 
   192 header('Content-type: text/html; charset=UTF-8');
   193 
   194 ?>
   195 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   196 <html xmlns="http://www.w3.org/1999/xhtml" dir="<?php echo $lang['DIRECTION']; ?>" lang="<?php echo $lang['USER_LANG']; ?>" xml:lang="<?php echo $lang['USER_LANG']; ?>">
   197 <head>
   198 
   199 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
   200 <meta http-equiv="content-language" content="<?php echo $lang['USER_LANG']; ?>" />
   201 <meta http-equiv="content-style-type" content="text/css" />
   202 <meta http-equiv="imagetoolbar" content="no" />
   203 
   204 <title><?php echo $lang['UPDATING_TO_LATEST_STABLE']; ?></title>
   205 
   206 <link href="../adm/style/admin.css" rel="stylesheet" type="text/css" media="screen" />
   207 
   208 </head>
   209 
   210 <body>
   211 <div id="wrap">
   212 	<div id="page-header">&nbsp;</div>
   213 
   214 	<div id="page-body">
   215 		<div id="acp">
   216 		<div class="panel">
   217 			<span class="corners-top"><span></span></span>
   218 				<div id="content">
   219 					<div id="main" class="install-body">
   220 
   221 	<h1><?php echo $lang['UPDATING_TO_LATEST_STABLE']; ?></h1>
   222 
   223 	<br />
   224 
   225 	<p><?php echo $lang['DATABASE_TYPE']; ?> :: <strong><?php echo $db->sql_layer; ?></strong><br />
   226 <?php
   227 
   228 if ($debug_from_version !== false)
   229 {
   230 	$config['version'] = $debug_from_version;
   231 }
   232 
   233 echo $lang['PREVIOUS_VERSION'] . ' :: <strong>' . $config['version'] . '</strong><br />';
   234 echo $lang['UPDATED_VERSION'] . ' :: <strong>' . $updates_to_version . '</strong></p>';
   235 
   236 $current_version = str_replace('rc', 'RC', strtolower($config['version']));
   237 $latest_version = str_replace('rc', 'RC', strtolower($updates_to_version));
   238 $orig_version = $config['version'];
   239 
   240 // Fill DB version
   241 if (empty($config['dbms_version']))
   242 {
   243 	set_config('dbms_version', $db->sql_server_info(true));
   244 }
   245 
   246 // Firebird update from Firebird 2.0 to 2.1+ required?
   247 if ($db->sql_layer == 'firebird')
   248 {
   249 	// We do not trust any PHP5 function enabled, we will simply test for a function new in 2.1
   250 	$db->sql_return_on_error(true);
   251 
   252 	$sql = 'SELECT 1 FROM RDB$DATABASE
   253 		WHERE BIN_AND(10, 1) = 0';
   254 	$result = $db->sql_query($sql);
   255 
   256 	if (!$result || $db->sql_error_triggered)
   257 	{
   258 		echo '<br /><br />';
   259 		echo '<h1>' . $lang['ERROR'] . '</h1><br />';
   260 
   261 		echo '<p>' . $lang['FIREBIRD_DBMS_UPDATE_REQUIRED'] . '</p>';
   262 
   263 		_print_footer();
   264 
   265 		exit_handler();
   266 		exit;
   267 	}
   268 
   269 	$db->sql_freeresult($result);
   270 	$db->sql_return_on_error(false);
   271 }
   272 
   273 // MySQL update from MySQL 3.x/4.x to > 4.1.x required?
   274 if ($db->sql_layer == 'mysql' || $db->sql_layer == 'mysql4' || $db->sql_layer == 'mysqli')
   275 {
   276 	// Verify by fetching column... if the column type matches the new type we update dbms_version...
   277 	$sql = "SHOW COLUMNS FROM " . CONFIG_TABLE;
   278 	$result = $db->sql_query($sql);
   279 
   280 	$column_type = '';
   281 	while ($row = $db->sql_fetchrow($result))
   282 	{
   283 		$field = strtolower($row['Field']);
   284 
   285 		if ($field == 'config_value')
   286 		{
   287 			$column_type = strtolower($row['Type']);
   288 			break;
   289 		}
   290 	}
   291 	$db->sql_freeresult($result);
   292 
   293 	// If column type is blob, but mysql version says we are on > 4.1.3, then the schema needs an update
   294 	if (strpos($column_type, 'blob') !== false && version_compare($db->sql_server_info(true), '4.1.3', '>='))
   295 	{
   296 		echo '<br /><br />';
   297 		echo '<h1>' . $lang['ERROR'] . '</h1><br />';
   298 
   299 		echo '<p>' . sprintf($lang['MYSQL_SCHEMA_UPDATE_REQUIRED'], $config['dbms_version'], $db->sql_server_info(true)) . '</p>';
   300 
   301 		_print_footer();
   302 
   303 		exit_handler();
   304 		exit;
   305 	}
   306 }
   307 
   308 // Now check if the user wants to update from a version we no longer support updates from
   309 if (version_compare($current_version, $oldest_from_version, '<'))
   310 {
   311 	echo '<br /><br /><h1>' . $lang['ERROR'] . '</h1><br />';
   312 	echo '<p>' . sprintf($lang['DB_UPDATE_NOT_SUPPORTED'], $oldest_from_version, $current_version) . '</p>';
   313 
   314 	_print_footer();
   315 	exit_handler();
   316 	exit;
   317 }
   318 
   319 // If the latest version and the current version are 'unequal', we will update the version_update_from, else we do not update anything.
   320 if ($inline_update)
   321 {
   322 	if ($current_version !== $latest_version)
   323 	{
   324 		set_config('version_update_from', $orig_version);
   325 	}
   326 }
   327 else
   328 {
   329 	// If not called from the update script, we will actually remove the traces
   330 	$db->sql_query('DELETE FROM ' . CONFIG_TABLE . " WHERE config_name = 'version_update_from'");
   331 }
   332 
   333 // Schema updates
   334 ?>
   335 	<br /><br />
   336 
   337 	<h1><?php echo $lang['UPDATE_DATABASE_SCHEMA']; ?></h1>
   338 
   339 	<br />
   340 	<p><?php echo $lang['PROGRESS']; ?> :: <strong>
   341 
   342 <?php
   343 
   344 flush();
   345 
   346 // We go through the schema changes from the lowest to the highest version
   347 // We try to also include versions 'in-between'...
   348 $no_updates = true;
   349 $versions = array_keys($database_update_info);
   350 for ($i = 0; $i < sizeof($versions); $i++)
   351 {
   352 	$version = $versions[$i];
   353 	$schema_changes = $database_update_info[$version];
   354 
   355 	$next_version = (isset($versions[$i + 1])) ? $versions[$i + 1] : $updates_to_version;
   356 
   357 	// If the installed version to be updated to is < than the current version, and if the current version is >= as the version to be updated to next, we will skip the process
   358 	if (version_compare($version, $current_version, '<') && version_compare($current_version, $next_version, '>='))
   359 	{
   360 		continue;
   361 	}
   362 
   363 	if (!sizeof($schema_changes))
   364 	{
   365 		continue;
   366 	}
   367 
   368 	$no_updates = false;
   369 
   370 	// We run one index after the other... to be consistent with schema changes...
   371 	foreach ($schema_changes as $key => $changes)
   372 	{
   373 		$statements = $db_tools->perform_schema_changes(array($key => $changes));
   374 
   375 		foreach ($statements as $sql)
   376 		{
   377 			_sql($sql, $errored, $error_ary);
   378 		}
   379 	}
   380 }
   381 
   382 _write_result($no_updates, $errored, $error_ary);
   383 
   384 // Data updates
   385 $error_ary = array();
   386 $errored = $no_updates = false;
   387 
   388 ?>
   389 
   390 <br /><br />
   391 <h1><?php echo $lang['UPDATING_DATA']; ?></h1>
   392 <br />
   393 <p><?php echo $lang['PROGRESS']; ?> :: <strong>
   394 
   395 <?php
   396 
   397 flush();
   398 
   399 $no_updates = true;
   400 $versions = array_keys($database_update_info);
   401 
   402 // some code magic
   403 for ($i = 0; $i < sizeof($versions); $i++)
   404 {
   405 	$version = $versions[$i];
   406 	$next_version = (isset($versions[$i + 1])) ? $versions[$i + 1] : $updates_to_version;
   407 
   408 	// If the installed version to be updated to is < than the current version, and if the current version is >= as the version to be updated to next, we will skip the process
   409 	if (version_compare($version, $current_version, '<') && version_compare($current_version, $next_version, '>='))
   410 	{
   411 		continue;
   412 	}
   413 
   414 	change_database_data($no_updates, $version);
   415 }
   416 
   417 _write_result($no_updates, $errored, $error_ary);
   418 
   419 $error_ary = array();
   420 $errored = $no_updates = false;
   421 
   422 ?>
   423 
   424 <br /><br />
   425 <h1><?php echo $lang['UPDATE_VERSION_OPTIMIZE']; ?></h1>
   426 <br />
   427 <p><?php echo $lang['PROGRESS']; ?> :: <strong>
   428 
   429 <?php
   430 
   431 flush();
   432 
   433 if ($debug_from_version === false)
   434 {
   435 	// update the version
   436 	$sql = "UPDATE " . CONFIG_TABLE . "
   437 		SET config_value = '$updates_to_version'
   438 		WHERE config_name = 'version'";
   439 	_sql($sql, $errored, $error_ary);
   440 }
   441 
   442 // Reset permissions
   443 $sql = 'UPDATE ' . USERS_TABLE . "
   444 	SET user_permissions = '',
   445 		user_perm_from = 0";
   446 _sql($sql, $errored, $error_ary);
   447 
   448 // Update the dbms version if everything is ok...
   449 set_config('dbms_version', $db->sql_server_info(true));
   450 
   451 /* Optimize/vacuum analyze the tables where appropriate
   452 // this should be done for each version in future along with
   453 // the version number update
   454 switch ($db->sql_layer)
   455 {
   456 	case 'mysql':
   457 	case 'mysqli':
   458 	case 'mysql4':
   459 		$sql = 'OPTIMIZE TABLE ' . $table_prefix . 'auth_access, ' . $table_prefix . 'banlist, ' . $table_prefix . 'categories, ' . $table_prefix . 'config, ' . $table_prefix . 'disallow, ' . $table_prefix . 'forum_prune, ' . $table_prefix . 'forums, ' . $table_prefix . 'groups, ' . $table_prefix . 'posts, ' . $table_prefix . 'posts_text, ' . $table_prefix . 'privmsgs, ' . $table_prefix . 'privmsgs_text, ' . $table_prefix . 'ranks, ' . $table_prefix . 'search_results, ' . $table_prefix . 'search_wordlist, ' . $table_prefix . 'search_wordmatch, ' . $table_prefix . 'sessions_keys' . $table_prefix . 'smilies, ' . $table_prefix . 'themes, ' . $table_prefix . 'themes_name, ' . $table_prefix . 'topics, ' . $table_prefix . 'topics_watch, ' . $table_prefix . 'user_group, ' . $table_prefix . 'users, ' . $table_prefix . 'vote_desc, ' . $table_prefix . 'vote_results, ' . $table_prefix . 'vote_voters, ' . $table_prefix . 'words';
   460 		_sql($sql, $errored, $error_ary);
   461 	break;
   462 
   463 	case 'postgresql':
   464 		_sql("VACUUM ANALYZE", $errored, $error_ary);
   465 	break;
   466 }
   467 */
   468 
   469 _write_result($no_updates, $errored, $error_ary);
   470 
   471 ?>
   472 
   473 <br />
   474 <h1><?php echo $lang['UPDATE_COMPLETED']; ?></h1>
   475 
   476 <br />
   477 
   478 <?php
   479 
   480 if (!$inline_update)
   481 {
   482 ?>
   483 
   484 	<p style="color:red"><?php echo $lang['UPDATE_FILES_NOTICE']; ?></p>
   485 
   486 	<p><?php echo $lang['COMPLETE_LOGIN_TO_BOARD']; ?></p>
   487 
   488 <?php
   489 }
   490 else
   491 {
   492 ?>
   493 
   494 	<p><?php echo ((isset($lang['INLINE_UPDATE_SUCCESSFUL'])) ? $lang['INLINE_UPDATE_SUCCESSFUL'] : 'The database update was successful. Now you need to continue the update process.'); ?></p>
   495 
   496 	<p><a href="<?php echo append_sid("{$phpbb_root_path}install/index.{$phpEx}", "mode=update&amp;sub=file_check&amp;language=$language"); ?>" class="button1"><?php echo (isset($lang['CONTINUE_UPDATE_NOW'])) ? $lang['CONTINUE_UPDATE_NOW'] : 'Continue the update process now'; ?></a></p>
   497 
   498 <?php
   499 }
   500 
   501 // Add database update to log
   502 add_log('admin', 'LOG_UPDATE_DATABASE', $orig_version, $updates_to_version);
   503 
   504 // Now we purge the session table as well as all cache files
   505 $cache->purge();
   506 
   507 _print_footer();
   508 
   509 garbage_collection();
   510 
   511 if (function_exists('exit_handler'))
   512 {
   513 	exit_handler();
   514 }
   515 
   516 /**
   517 * Print out footer
   518 */
   519 function _print_footer()
   520 {
   521 	echo <<<EOF
   522 					</div>
   523 				</div>
   524 			<span class="corners-bottom"><span></span></span>
   525 		</div>
   526 		</div>
   527 	</div>
   528 
   529 	<div id="page-footer">
   530 		Powered by <a href="http://www.phpbb.com/">phpBB</a>&reg; Forum Software &copy; phpBB Group
   531 	</div>
   532 </div>
   533 
   534 </body>
   535 </html>
   536 EOF;
   537 }
   538 
   539 /**
   540 * Function for triggering an sql statement
   541 */
   542 function _sql($sql, &$errored, &$error_ary, $echo_dot = true)
   543 {
   544 	global $db;
   545 
   546 	if (defined('DEBUG_EXTRA'))
   547 	{
   548 		echo "<br />\n{$sql}\n<br />";
   549 	}
   550 
   551 	$db->sql_return_on_error(true);
   552 
   553 	if ($sql === 'begin')
   554 	{
   555 		$result = $db->sql_transaction('begin');
   556 	}
   557 	else if ($sql === 'commit')
   558 	{
   559 		$result = $db->sql_transaction('commit');
   560 	}
   561 	else
   562 	{
   563 		$result = $db->sql_query($sql);
   564 		if ($db->sql_error_triggered)
   565 		{
   566 			$errored = true;
   567 			$error_ary['sql'][] = $db->sql_error_sql;
   568 			$error_ary['error_code'][] = $db->sql_error_returned;
   569 		}
   570 	}
   571 
   572 	$db->sql_return_on_error(false);
   573 
   574 	if ($echo_dot)
   575 	{
   576 		echo ". \n";
   577 		flush();
   578 	}
   579 
   580 	return $result;
   581 }
   582 
   583 function _write_result($no_updates, $errored, $error_ary)
   584 {
   585 	global $lang;
   586 
   587 	if ($no_updates)
   588 	{
   589 		echo ' ' . $lang['NO_UPDATES_REQUIRED'] . '</strong></p>';
   590 	}
   591 	else
   592 	{
   593 		echo ' <span class="success">' . $lang['DONE'] . '</span></strong><br />' . $lang['RESULT'] . ' :: ';
   594 
   595 		if ($errored)
   596 		{
   597 			echo ' <strong>' . $lang['SOME_QUERIES_FAILED'] . '</strong> <ul>';
   598 
   599 			for ($i = 0; $i < sizeof($error_ary['sql']); $i++)
   600 			{
   601 				echo '<li>' . $lang['ERROR'] . ' :: <strong>' . htmlspecialchars($error_ary['error_code'][$i]['message']) . '</strong><br />';
   602 				echo $lang['SQL'] . ' :: <strong>' . htmlspecialchars($error_ary['sql'][$i]) . '</strong><br /><br /></li>';
   603 			}
   604 
   605 			echo '</ul> <br /><br />' . $lang['SQL_FAILURE_EXPLAIN'] . '</p>';
   606 		}
   607 		else
   608 		{
   609 			echo '<strong>' . $lang['NO_ERRORS'] . '</strong></p>';
   610 		}
   611 	}
   612 }
   613 
   614 function _add_modules($modules_to_install)
   615 {
   616 	global $phpbb_root_path, $phpEx, $db;
   617 
   618 	include_once($phpbb_root_path . 'includes/acp/acp_modules.' . $phpEx);
   619 
   620 	$_module = new acp_modules();
   621 
   622 	foreach ($modules_to_install as $module_mode => $module_data)
   623 	{
   624 		$_module->module_class = $module_data['class'];
   625 
   626 		// Determine parent id first
   627 		$sql = 'SELECT module_id
   628 			FROM ' . MODULES_TABLE . "
   629 			WHERE module_class = '" . $db->sql_escape($module_data['class']) . "'
   630 				AND module_langname = '" . $db->sql_escape($module_data['cat']) . "'
   631 				AND module_mode = ''
   632 				AND module_basename = ''";
   633 		$result = $db->sql_query($sql);
   634 
   635 		// There may be more than one categories with the same name
   636 		$categories = array();
   637 		while ($row = $db->sql_fetchrow($result))
   638 		{
   639 			$categories[] = (int) $row['module_id'];
   640 		}
   641 		$db->sql_freeresult($result);
   642 
   643 		if (!sizeof($categories))
   644 		{
   645 			continue;
   646 		}
   647 
   648 		// Add the module to all categories found
   649 		foreach ($categories as $parent_id)
   650 		{
   651 			// Check if the module already exists
   652 			$sql = 'SELECT *
   653 				FROM ' . MODULES_TABLE . "
   654 				WHERE module_basename = '" . $db->sql_escape($module_data['base']) . "'
   655 					AND module_class = '" . $db->sql_escape($module_data['class']) . "'
   656 					AND module_langname = '" . $db->sql_escape($module_data['title']) . "'
   657 					AND module_mode = '" . $db->sql_escape($module_mode) . "'
   658 					AND module_auth = '" . $db->sql_escape($module_data['auth']) . "'
   659 					AND parent_id = {$parent_id}";
   660 			$result = $db->sql_query($sql);
   661 			$row = $db->sql_fetchrow($result);
   662 			$db->sql_freeresult($result);
   663 
   664 			// If it exists, we simply continue with the next category
   665 			if ($row)
   666 			{
   667 				continue;
   668 			}
   669 
   670 			// Build the module sql row
   671 			$module_row = array(
   672 				'module_basename'	=> $module_data['base'],
   673 				'module_enabled'	=> (isset($module_data['enabled'])) ? (int) $module_data['enabled'] : 1,
   674 				'module_display'	=> (isset($module_data['display'])) ? (int) $module_data['display'] : 1,
   675 				'parent_id'			=> $parent_id,
   676 				'module_class'		=> $module_data['class'],
   677 				'module_langname'	=> $module_data['title'],
   678 				'module_mode'		=> $module_mode,
   679 				'module_auth'		=> $module_data['auth'],
   680 			);
   681 
   682 			$_module->update_module_data($module_row, true);
   683 
   684 			// Ok, do we need to re-order the module, move it up or down?
   685 			if (!isset($module_data['after']))
   686 			{
   687 				continue;
   688 			}
   689 
   690 			$after_mode = $module_data['after'][0];
   691 			$after_langname = $module_data['after'][1];
   692 
   693 			// First of all, get the module id for the module this one has to be placed after
   694 			$sql = 'SELECT left_id
   695 				FROM ' . MODULES_TABLE . "
   696 				WHERE module_class = '" . $db->sql_escape($module_data['class']) . "'
   697 					AND module_basename = '" . $db->sql_escape($module_data['base']) . "'
   698 					AND module_langname = '" . $db->sql_escape($after_langname) . "'
   699 					AND module_mode = '" . $db->sql_escape($after_mode) . "'
   700 					AND parent_id = '{$parent_id}'";
   701 			$result = $db->sql_query($sql);
   702 			$first_left_id = (int) $db->sql_fetchfield('left_id');
   703 			$db->sql_freeresult($result);
   704 
   705 			if (!$first_left_id)
   706 			{
   707 				continue;
   708 			}
   709 
   710 			// Ok, count the number of modules between $after_mode and the added module
   711 			$sql = 'SELECT COUNT(module_id) as num_modules
   712 				FROM ' . MODULES_TABLE . "
   713 				WHERE module_class = '" . $db->sql_escape($module_data['class']) . "'
   714 					AND parent_id = {$parent_id}
   715 					AND left_id BETWEEN {$first_left_id} AND {$module_row['left_id']}";
   716 			$result = $db->sql_query($sql);
   717 			$steps = (int) $db->sql_fetchfield('num_modules');
   718 			$db->sql_freeresult($result);
   719 
   720 			// We need to substract 2
   721 			$steps -= 2;
   722 
   723 			if ($steps <= 0)
   724 			{
   725 				continue;
   726 			}
   727 
   728 			// Ok, move module up $num_modules times. ;)
   729 			$_module->move_module_by($module_row, 'move_up', $steps);
   730 		}
   731 	}
   732 
   733 	$_module->remove_cache_file();
   734 }
   735 
   736 /****************************************************************************
   737 * ADD YOUR DATABASE SCHEMA CHANGES HERE										*
   738 *****************************************************************************/
   739 function database_update_info()
   740 {
   741 	return array(
   742 		// Changes from 3.0.0 to the next version
   743 		'3.0.0'			=> array(
   744 			// Add the following columns
   745 			'add_columns'		=> array(
   746 				FORUMS_TABLE			=> array(
   747 					'display_subforum_list'		=> array('BOOL', 1),
   748 				),
   749 				SESSIONS_TABLE			=> array(
   750 					'session_forum_id'		=> array('UINT', 0),
   751 				),
   752 			),
   753 			'drop_keys'		=> array(
   754 				GROUPS_TABLE			=> array('group_legend'),
   755 			),
   756 			'add_index'		=> array(
   757 				SESSIONS_TABLE			=> array(
   758 					'session_forum_id'		=> array('session_forum_id'),
   759 				),
   760 				GROUPS_TABLE			=> array(
   761 					'group_legend_name'		=> array('group_legend', 'group_name'),
   762 				),
   763 			),
   764 		),
   765 		// No changes from 3.0.1-RC1 to 3.0.1
   766 		'3.0.1-RC1'		=> array(),
   767 		// No changes from 3.0.1 to 3.0.2-RC1
   768 		'3.0.1'			=> array(),
   769 		// Changes from 3.0.2-RC1 to 3.0.2-RC2
   770 		'3.0.2-RC1'		=> array(
   771 			'change_columns'	=> array(
   772 				DRAFTS_TABLE			=> array(
   773 					'draft_subject'		=> array('STEXT_UNI', ''),
   774 				),
   775 				FORUMS_TABLE	=> array(
   776 					'forum_last_post_subject' => array('STEXT_UNI', ''),
   777 				),
   778 				POSTS_TABLE		=> array(
   779 					'post_subject'			=> array('STEXT_UNI', '', 'true_sort'),
   780 				),
   781 				PRIVMSGS_TABLE	=> array(
   782 					'message_subject'		=> array('STEXT_UNI', ''),
   783 				),
   784 				TOPICS_TABLE	=> array(
   785 					'topic_title'				=> array('STEXT_UNI', '', 'true_sort'),
   786 					'topic_last_post_subject'	=> array('STEXT_UNI', ''),
   787 				),
   788 			),
   789 			'drop_keys'		=> array(
   790 				SESSIONS_TABLE			=> array('session_forum_id'),
   791 			),
   792 			'add_index'		=> array(
   793 				SESSIONS_TABLE			=> array(
   794 					'session_fid'		=> array('session_forum_id'),
   795 				),
   796 			),
   797 		),
   798 		// No changes from 3.0.2-RC2 to 3.0.2
   799 		'3.0.2-RC2'		=> array(),
   800 
   801 		// Changes from 3.0.2 to 3.0.3-RC1
   802 		'3.0.2'			=> array(
   803 			// Add the following columns
   804 			'add_columns'		=> array(
   805 				STYLES_TEMPLATE_TABLE			=> array(
   806 					'template_inherits_id'		=> array('UINT:4', 0),
   807 					'template_inherit_path'		=> array('VCHAR', ''),
   808 				),
   809 				GROUPS_TABLE					=> array(
   810 					'group_max_recipients'		=> array('UINT', 0),
   811 				),
   812 			),
   813 		),
   814 
   815 		// No changes from 3.0.3-RC1 to 3.0.3
   816 		'3.0.3-RC1'		=> array(),
   817 
   818 		// Changes from 3.0.3 to 3.0.4-RC1
   819 		'3.0.3'			=> array(
   820 			'add_columns'		=> array(
   821 				PROFILE_FIELDS_TABLE			=> array(
   822 					'field_show_profile'		=> array('BOOL', 0),
   823 				),
   824 			),
   825 			'change_columns'	=> array(
   826 				STYLES_TABLE				=> array(
   827 					'style_id'				=> array('UINT', NULL, 'auto_increment'),
   828 					'template_id'			=> array('UINT', 0),
   829 					'theme_id'				=> array('UINT', 0),
   830 					'imageset_id'			=> array('UINT', 0),
   831 				),
   832 				STYLES_IMAGESET_TABLE		=> array(
   833 					'imageset_id'				=> array('UINT', NULL, 'auto_increment'),
   834 				),
   835 				STYLES_IMAGESET_DATA_TABLE	=> array(
   836 					'image_id'				=> array('UINT', NULL, 'auto_increment'),
   837 					'imageset_id'			=> array('UINT', 0),
   838 				),
   839 				STYLES_THEME_TABLE			=> array(
   840 					'theme_id'				=> array('UINT', NULL, 'auto_increment'),
   841 				),
   842 				STYLES_TEMPLATE_TABLE		=> array(
   843 					'template_id'			=> array('UINT', NULL, 'auto_increment'),
   844 				),
   845 				STYLES_TEMPLATE_DATA_TABLE	=> array(
   846 					'template_id'			=> array('UINT', 0),
   847 				),
   848 				FORUMS_TABLE				=> array(
   849 					'forum_style'			=> array('UINT', 0),
   850 				),
   851 				USERS_TABLE					=> array(
   852 					'user_style'			=> array('UINT', 0),
   853 				),
   854 			),
   855 		),
   856 
   857 		// Changes from 3.0.4-RC1 to 3.0.4
   858 		'3.0.4-RC1'		=> array(),
   859 
   860 		// Changes from 3.0.4 to 3.0.5-RC1
   861 		'3.0.4'			=> array(
   862 			'change_columns'	=> array(
   863 				FORUMS_TABLE				=> array(
   864 					'forum_style'			=> array('UINT', 0),
   865 				),
   866 			),
   867 		),
   868 
   869 		// No changes from 3.0.5-RC1 to 3.0.5
   870 		'3.0.5-RC1'		=> array(),
   871 
   872 		// Changes from 3.0.5 to 3.0.6-RC1
   873 		'3.0.5'		=> array(
   874 			'add_columns'		=> array(
   875 				CONFIRM_TABLE			=> array(
   876 					'attempts'		=> array('UINT', 0),
   877 				),
   878 				USERS_TABLE			=> array(
   879 					'user_new'			=> array('BOOL', 1),
   880 					'user_reminded'		=> array('TINT:4', 0),
   881 					'user_reminded_time'=> array('TIMESTAMP', 0),
   882 				),
   883 				GROUPS_TABLE			=> array(
   884 					'group_skip_auth'		=> array('BOOL', 0, 'after' => 'group_founder_manage'),
   885 				),
   886 				PRIVMSGS_TABLE		=> array(
   887 					'message_reported'	=> array('BOOL', 0),
   888 				),
   889 				REPORTS_TABLE		=> array(
   890 					'pm_id'				=> array('UINT', 0),
   891 				),
   892 				PROFILE_FIELDS_TABLE			=> array(
   893 					'field_show_on_vt'		=> array('BOOL', 0),
   894 				),
   895 				FORUMS_TABLE		=> array(
   896 					'forum_options'			=> array('UINT:20', 0),
   897 				),
   898 			),
   899 			'change_columns'		=> array(
   900 				USERS_TABLE				=> array(
   901 					'user_options'		=> array('UINT:11', 230271),
   902 				),
   903 			),
   904 			'add_index'		=> array(
   905 				REPORTS_TABLE		=> array(
   906 					'post_id'		=> array('post_id'),
   907 					'pm_id'			=> array('pm_id'),
   908 				),
   909 				POSTS_TABLE			=> array(
   910 					'post_username'		=> array('post_username:255'),
   911 				),
   912 			),
   913 		),
   914 
   915 		// No changes from 3.0.6-RC1 to 3.0.6-RC2
   916 		'3.0.6-RC1'		=> array(),
   917 		// No changes from 3.0.6-RC2 to 3.0.6-RC3
   918 		'3.0.6-RC2'		=> array(),
   919 		// No changes from 3.0.6-RC3 to 3.0.6-RC4
   920 		'3.0.6-RC3'		=> array(),
   921 		// No changes from 3.0.6-RC4 to 3.0.6
   922 		'3.0.6-RC4'		=> array(),
   923 
   924 		// Changes from 3.0.6 to 3.0.7-RC1
   925 		'3.0.6'		=> array(
   926 			'drop_keys'		=> array(
   927 				LOG_TABLE			=> array('log_time'),
   928 			),
   929 			'add_index'		=> array(
   930 				TOPICS_TRACK_TABLE	=> array(
   931 					'topic_id'		=> array('topic_id'),
   932 				),
   933 			),
   934 		),
   935 
   936 		// No changes from 3.0.7-RC1 to 3.0.7-RC2
   937 		'3.0.7-RC1'		=> array(),
   938 		// No changes from 3.0.7-RC2 to 3.0.7
   939 		'3.0.7-RC2'		=> array(),
   940 		// No changes from 3.0.7 to 3.0.7-PL1
   941 		'3.0.7'		=> array(),
   942 		// No changes from 3.0.7-PL1 to 3.0.8-RC1
   943 		'3.0.7-PL1'		=> array(),
   944 		// No changes from 3.0.8-RC1 to 3.0.8
   945 		'3.0.8-RC1'		=> array(),
   946 		// Changes from 3.0.8 to 3.0.9-RC1
   947 		'3.0.8'			=> array(
   948 			'add_tables'		=> array(
   949 				LOGIN_ATTEMPT_TABLE	=> array(
   950 					'COLUMNS'			=> array(
   951 						// this column was removed from the database updater
   952 						// after 3.0.9-RC3 was released. It might still exist
   953 						// in 3.0.9-RCX installations and has to be dropped in
   954 						// 3.0.11 after the db_tools class is capable of properly
   955 						// removing a primary key.
   956 						// 'attempt_id'			=> array('UINT', NULL, 'auto_increment'),
   957 						'attempt_ip'			=> array('VCHAR:40', ''),
   958 						'attempt_browser'		=> array('VCHAR:150', ''),
   959 						'attempt_forwarded_for'	=> array('VCHAR:255', ''),
   960 						'attempt_time'			=> array('TIMESTAMP', 0),
   961 						'user_id'				=> array('UINT', 0),
   962 						'username'				=> array('VCHAR_UNI:255', 0),
   963 						'username_clean'		=> array('VCHAR_CI', 0),
   964 					),
   965 					//'PRIMARY_KEY'		=> 'attempt_id',
   966 					'KEYS'				=> array(
   967 						'att_ip'			=> array('INDEX', array('attempt_ip', 'attempt_time')),
   968 						'att_for'	=> array('INDEX', array('attempt_forwarded_for', 'attempt_time')),
   969 						'att_time'			=> array('INDEX', array('attempt_time')),
   970 						'user_id'				=> array('INDEX', 'user_id'),
   971 					),
   972 				),
   973 			),
   974 			'change_columns'	=> array(
   975 				BBCODES_TABLE	=> array(
   976 					'bbcode_id'	=> array('USINT', 0),
   977 				),
   978 			),
   979 		),
   980 		// No changes from 3.0.9-RC1 to 3.0.9-RC2
   981 		'3.0.9-RC1'		=> array(),
   982 		// No changes from 3.0.9-RC2 to 3.0.9-RC3
   983 		'3.0.9-RC2'		=> array(),
   984 		// No changes from 3.0.9-RC3 to 3.0.9-RC4
   985 		'3.0.9-RC3'     => array(),
   986 		// No changes from 3.0.9-RC4 to 3.0.9
   987 		'3.0.9-RC4'     => array(),
   988 		// No changes from 3.0.9 to 3.0.10-RC1
   989 		'3.0.9'			=> array(),
   990 		// No changes from 3.0.10-RC1 to 3.0.10-RC2
   991 		'3.0.10-RC1'	=> array(),
   992 		// No changes from 3.0.10-RC2 to 3.0.10-RC3
   993 		'3.0.10-RC2'	=> array(),
   994 		// No changes from 3.0.10-RC3 to 3.0.10
   995 		'3.0.10-RC3'	=> array(),
   996 
   997 		/** @todo DROP LOGIN_ATTEMPT_TABLE.attempt_id in 3.0.11-RC1 */
   998 	);
   999 }
  1000 
  1001 /****************************************************************************
  1002 * ADD YOUR DATABASE DATA CHANGES HERE										*
  1003 * REMEMBER: You NEED to enter a schema array above and a data array here,	*
  1004 * even if both or one of them are empty.									*
  1005 *****************************************************************************/
  1006 function change_database_data(&$no_updates, $version)
  1007 {
  1008 	global $db, $errored, $error_ary, $config, $phpbb_root_path, $phpEx;
  1009 
  1010 	switch ($version)
  1011 	{
  1012 		case '3.0.0':
  1013 
  1014 			$sql = 'UPDATE ' . TOPICS_TABLE . "
  1015 				SET topic_last_view_time = topic_last_post_time
  1016 				WHERE topic_last_view_time = 0";
  1017 			_sql($sql, $errored, $error_ary);
  1018 
  1019 			// Update smiley sizes
  1020 			$smileys = array('icon_e_surprised.gif', 'icon_eek.gif', 'icon_cool.gif', 'icon_lol.gif', 'icon_mad.gif', 'icon_razz.gif', 'icon_redface.gif', 'icon_cry.gif', 'icon_evil.gif', 'icon_twisted.gif', 'icon_rolleyes.gif', 'icon_exclaim.gif', 'icon_question.gif', 'icon_idea.gif', 'icon_arrow.gif', 'icon_neutral.gif', 'icon_mrgreen.gif', 'icon_e_ugeek.gif');
  1021 
  1022 			foreach ($smileys as $smiley)
  1023 			{
  1024 				if (file_exists($phpbb_root_path . 'images/smilies/' . $smiley))
  1025 				{
  1026 					list($width, $height) = getimagesize($phpbb_root_path . 'images/smilies/' . $smiley);
  1027 
  1028 					$sql = 'UPDATE ' . SMILIES_TABLE . '
  1029 						SET smiley_width = ' . $width . ', smiley_height = ' . $height . "
  1030 						WHERE smiley_url = '" . $db->sql_escape($smiley) . "'";
  1031 
  1032 					_sql($sql, $errored, $error_ary);
  1033 				}
  1034 			}
  1035 
  1036 			$no_updates = false;
  1037 		break;
  1038 
  1039 		// No changes from 3.0.1-RC1 to 3.0.1
  1040 		case '3.0.1-RC1':
  1041 		break;
  1042 
  1043 		// changes from 3.0.1 to 3.0.2-RC1
  1044 		case '3.0.1':
  1045 
  1046 			set_config('referer_validation', '1');
  1047 			set_config('check_attachment_content', '1');
  1048 			set_config('mime_triggers', 'body|head|html|img|plaintext|a href|pre|script|table|title');
  1049 
  1050 			$no_updates = false;
  1051 		break;
  1052 
  1053 		// No changes from 3.0.2-RC1 to 3.0.2-RC2
  1054 		case '3.0.2-RC1':
  1055 		break;
  1056 
  1057 		// No changes from 3.0.2-RC2 to 3.0.2
  1058 		case '3.0.2-RC2':
  1059 		break;
  1060 
  1061 		// Changes from 3.0.2 to 3.0.3-RC1
  1062 		case '3.0.2':
  1063 			set_config('enable_queue_trigger', '0');
  1064 			set_config('queue_trigger_posts', '3');
  1065 
  1066 			set_config('pm_max_recipients', '0');
  1067 
  1068 			// Set maximum number of recipients for the registered users, bots, guests group
  1069 			$sql = 'UPDATE ' . GROUPS_TABLE . ' SET group_max_recipients = 5
  1070 				WHERE ' . $db->sql_in_set('group_name', array('GUESTS', 'REGISTERED', 'REGISTERED_COPPA', 'BOTS'));
  1071 			_sql($sql, $errored, $error_ary);
  1072 
  1073 			// Not prefilling yet
  1074 			set_config('dbms_version', '');
  1075 
  1076 			// Add new permission u_masspm_group and duplicate settings from u_masspm
  1077 			include_once($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
  1078 			$auth_admin = new auth_admin();
  1079 
  1080 			// Only add the new permission if it does not already exist
  1081 			if (empty($auth_admin->acl_options['id']['u_masspm_group']))
  1082 			{
  1083 				$auth_admin->acl_add_option(array('global' => array('u_masspm_group')));
  1084 
  1085 				// Now the tricky part, filling the permission
  1086 				$old_id = $auth_admin->acl_options['id']['u_masspm'];
  1087 				$new_id = $auth_admin->acl_options['id']['u_masspm_group'];
  1088 
  1089 				$tables = array(ACL_GROUPS_TABLE, ACL_ROLES_DATA_TABLE, ACL_USERS_TABLE);
  1090 
  1091 				foreach ($tables as $table)
  1092 				{
  1093 					$sql = 'SELECT *
  1094 						FROM ' . $table . '
  1095 						WHERE auth_option_id = ' . $old_id;
  1096 					$result = _sql($sql, $errored, $error_ary);
  1097 
  1098 					$sql_ary = array();
  1099 					while ($row = $db->sql_fetchrow($result))
  1100 					{
  1101 						$row['auth_option_id'] = $new_id;
  1102 						$sql_ary[] = $row;
  1103 					}
  1104 					$db->sql_freeresult($result);
  1105 
  1106 					if (sizeof($sql_ary))
  1107 					{
  1108 						$db->sql_multi_insert($table, $sql_ary);
  1109 					}
  1110 				}
  1111 
  1112 				// Remove any old permission entries
  1113 				$auth_admin->acl_clear_prefetch();
  1114 			}
  1115 
  1116 			/**
  1117 			* Do not resync post counts here. An admin may do this later from the ACP
  1118 			$start = 0;
  1119 			$step = ($config['num_posts']) ? (max((int) ($config['num_posts'] / 5), 20000)) : 20000;
  1120 
  1121 			$sql = 'UPDATE ' . USERS_TABLE . ' SET user_posts = 0';
  1122 			_sql($sql, $errored, $error_ary);
  1123 
  1124 			do
  1125 			{
  1126 				$sql = 'SELECT COUNT(post_id) AS num_posts, poster_id
  1127 					FROM ' . POSTS_TABLE . '
  1128 					WHERE post_id BETWEEN ' . ($start + 1) . ' AND ' . ($start + $step) . '
  1129 						AND post_postcount = 1 AND post_approved = 1
  1130 					GROUP BY poster_id';
  1131 				$result = _sql($sql, $errored, $error_ary);
  1132 
  1133 				if ($row = $db->sql_fetchrow($result))
  1134 				{
  1135 					do
  1136 					{
  1137 						$sql = 'UPDATE ' . USERS_TABLE . " SET user_posts = user_posts + {$row['num_posts']} WHERE user_id = {$row['poster_id']}";
  1138 						_sql($sql, $errored, $error_ary);
  1139 					}
  1140 					while ($row = $db->sql_fetchrow($result));
  1141 
  1142 					$start += $step;
  1143 				}
  1144 				else
  1145 				{
  1146 					$start = 0;
  1147 				}
  1148 				$db->sql_freeresult($result);
  1149 			}
  1150 			while ($start);
  1151 			*/
  1152 
  1153 			$sql = 'UPDATE ' . MODULES_TABLE . '
  1154 				SET module_auth = \'acl_a_email && cfg_email_enable\'
  1155 				WHERE module_class = \'acp\'
  1156 					AND module_basename = \'email\'';
  1157 			_sql($sql, $errored, $error_ary);
  1158 
  1159 			$no_updates = false;
  1160 		break;
  1161 
  1162 		// Changes from 3.0.3-RC1 to 3.0.3
  1163 		case '3.0.3-RC1':
  1164 			if ($db->sql_layer == 'oracle')
  1165 			{
  1166 				// log_operation is CLOB - but we can change this later
  1167 				$sql = 'UPDATE ' . LOG_TABLE . "
  1168 					SET log_operation = 'LOG_DELETE_TOPIC'
  1169 					WHERE log_operation LIKE 'LOG_TOPIC_DELETED'";
  1170 				_sql($sql, $errored, $error_ary);
  1171 			}
  1172 			else
  1173 			{
  1174 				$sql = 'UPDATE ' . LOG_TABLE . "
  1175 					SET log_operation = 'LOG_DELETE_TOPIC'
  1176 					WHERE log_operation = 'LOG_TOPIC_DELETED'";
  1177 				_sql($sql, $errored, $error_ary);
  1178 			}
  1179 
  1180 			$no_updates = false;
  1181 		break;
  1182 
  1183 		// Changes from 3.0.3 to 3.0.4-RC1
  1184 		case '3.0.3':
  1185 			// Update the Custom Profile Fields based on previous settings to the new format
  1186 			$sql = 'SELECT field_id, field_required, field_show_on_reg, field_hide
  1187 					FROM ' . PROFILE_FIELDS_TABLE;
  1188 			$result = _sql($sql, $errored, $error_ary);
  1189 
  1190 			while ($row = $db->sql_fetchrow($result))
  1191 			{
  1192 				$sql_ary = array(
  1193 					'field_required'	=> 0,
  1194 					'field_show_on_reg'	=> 0,
  1195 					'field_hide'		=> 0,
  1196 					'field_show_profile'=> 0,
  1197 				);
  1198 
  1199 				if ($row['field_required'])
  1200 				{
  1201 					$sql_ary['field_required'] = $sql_ary['field_show_on_reg'] = $sql_ary['field_show_profile'] = 1;
  1202 				}
  1203 				else if ($row['field_show_on_reg'])
  1204 				{
  1205 					$sql_ary['field_show_on_reg'] = $sql_ary['field_show_profile'] = 1;
  1206 				}
  1207 				else if ($row['field_hide'])
  1208 				{
  1209 					// Only administrators and moderators can see this CPF, if the view is enabled, they can see it, otherwise just admins in the acp_users module
  1210 					$sql_ary['field_hide'] = 1;
  1211 				}
  1212 				else
  1213 				{
  1214 					// equivelant to "none", which is the "Display in user control panel" option
  1215 					$sql_ary['field_show_profile'] = 1;
  1216 				}
  1217 
  1218 				_sql('UPDATE ' . PROFILE_FIELDS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE field_id = ' . $row['field_id'], $errored, $error_ary);
  1219 			}
  1220 			$no_updates = false;
  1221 
  1222 		break;
  1223 
  1224 		// Changes from 3.0.4-RC1 to 3.0.4
  1225 		case '3.0.4-RC1':
  1226 		break;
  1227 
  1228 		// Changes from 3.0.4 to 3.0.5-RC1
  1229 		case '3.0.4':
  1230 
  1231 			// Captcha config variables
  1232 			set_config('captcha_gd_wave', 0);
  1233 			set_config('captcha_gd_3d_noise', 1);
  1234 			set_config('captcha_gd_fonts', 1);
  1235 			set_config('confirm_refresh', 1);
  1236 
  1237 			// Maximum number of keywords
  1238 			set_config('max_num_search_keywords', 10);
  1239 
  1240 			// Remove static config var and put it back as dynamic variable
  1241 			$sql = 'UPDATE ' . CONFIG_TABLE . "
  1242 				SET is_dynamic = 1
  1243 				WHERE config_name = 'search_indexing_state'";
  1244 			_sql($sql, $errored, $error_ary);
  1245 
  1246 			// Hash old MD5 passwords
  1247 			$sql = 'SELECT user_id, user_password
  1248 					FROM ' . USERS_TABLE . '
  1249 					WHERE user_pass_convert = 1';
  1250 			$result = _sql($sql, $errored, $error_ary);
  1251 
  1252 			while ($row = $db->sql_fetchrow($result))
  1253 			{
  1254 				if (strlen($row['user_password']) == 32)
  1255 				{
  1256 					$sql_ary = array(
  1257 						'user_password'	=> phpbb_hash($row['user_password']),
  1258 					);
  1259 
  1260 					_sql('UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE user_id = ' . $row['user_id'], $errored, $error_ary);
  1261 				}
  1262 			}
  1263 			$db->sql_freeresult($result);
  1264 
  1265 			// Adjust bot entry
  1266 			$sql = 'UPDATE ' . BOTS_TABLE . "
  1267 				SET bot_agent = 'ichiro/'
  1268 				WHERE bot_agent = 'ichiro/2'";
  1269 			_sql($sql, $errored, $error_ary);
  1270 
  1271 
  1272 			// Before we are able to add a unique key to auth_option, we need to remove duplicate entries
  1273 
  1274 			// We get duplicate entries first
  1275 			$sql = 'SELECT auth_option
  1276 				FROM ' . ACL_OPTIONS_TABLE . '
  1277 				GROUP BY auth_option
  1278 				HAVING COUNT(*) >= 2';
  1279 			$result = $db->sql_query($sql);
  1280 
  1281 			$auth_options = array();
  1282 			while ($row = $db->sql_fetchrow($result))
  1283 			{
  1284 				$auth_options[] = $row['auth_option'];
  1285 			}
  1286 			$db->sql_freeresult($result);
  1287 
  1288 			// Remove specific auth options
  1289 			if (!empty($auth_options))
  1290 			{
  1291 				foreach ($auth_options as $option)
  1292 				{
  1293 					// Select auth_option_ids... the largest id will be preserved
  1294 					$sql = 'SELECT auth_option_id
  1295 						FROM ' . ACL_OPTIONS_TABLE . "
  1296 						WHERE auth_option = '" . $db->sql_escape($option) . "'
  1297 						ORDER BY auth_option_id DESC";
  1298 					// sql_query_limit not possible here, due to bug in postgresql layer
  1299 					$result = $db->sql_query($sql);
  1300 
  1301 					// Skip first row, this is our original auth option we want to preserve
  1302 					$row = $db->sql_fetchrow($result);
  1303 
  1304 					while ($row = $db->sql_fetchrow($result))
  1305 					{
  1306 						// Ok, remove this auth option...
  1307 						_sql('DELETE FROM ' . ACL_OPTIONS_TABLE . ' WHERE auth_option_id = ' . $row['auth_option_id'], $errored, $error_ary);
  1308 						_sql('DELETE FROM ' . ACL_ROLES_DATA_TABLE . ' WHERE auth_option_id = ' . $row['auth_option_id'], $errored, $error_ary);
  1309 						_sql('DELETE FROM ' . ACL_GROUPS_TABLE . ' WHERE auth_option_id = ' . $row['auth_option_id'], $errored, $error_ary);
  1310 						_sql('DELETE FROM ' . ACL_USERS_TABLE . ' WHERE auth_option_id = ' . $row['auth_option_id'], $errored, $error_ary);
  1311 					}
  1312 					$db->sql_freeresult($result);
  1313 				}
  1314 			}
  1315 
  1316 			// Now make auth_option UNIQUE, by dropping the old index and adding a UNIQUE one.
  1317 			$changes = array(
  1318 				'drop_keys'			=> array(
  1319 					ACL_OPTIONS_TABLE		=> array('auth_option'),
  1320 				),
  1321 			);
  1322 
  1323 			global $db_tools;
  1324 
  1325 			$statements = $db_tools->perform_schema_changes($changes);
  1326 
  1327 			foreach ($statements as $sql)
  1328 			{
  1329 				_sql($sql, $errored, $error_ary);
  1330 			}
  1331 
  1332 			$changes = array(
  1333 				'add_unique_index'	=> array(
  1334 					ACL_OPTIONS_TABLE		=> array(
  1335 						'auth_option'		=> array('auth_option'),
  1336 					),
  1337 				),
  1338 			);
  1339 
  1340 			$statements = $db_tools->perform_schema_changes($changes);
  1341 
  1342 			foreach ($statements as $sql)
  1343 			{
  1344 				_sql($sql, $errored, $error_ary);
  1345 			}
  1346 
  1347 			$no_updates = false;
  1348 
  1349 		break;
  1350 
  1351 		// No changes from 3.0.5-RC1 to 3.0.5
  1352 		case '3.0.5-RC1':
  1353 		break;
  1354 
  1355 		// Changes from 3.0.5 to 3.0.6-RC1
  1356 		case '3.0.5':
  1357 			// Let's see if the GD Captcha can be enabled... we simply look for what *is* enabled...
  1358 			if (!empty($config['captcha_gd']) && !isset($config['captcha_plugin']))
  1359 			{
  1360 				set_config('captcha_plugin', 'phpbb_captcha_gd');
  1361 			}
  1362 			else if (!isset($config['captcha_plugin']))
  1363 			{
  1364 				set_config('captcha_plugin', 'phpbb_captcha_nogd');
  1365 			}
  1366 
  1367 			// Entries for the Feed Feature
  1368 			set_config('feed_enable', '0');
  1369 			set_config('feed_limit', '10');
  1370 
  1371 			set_config('feed_overall_forums', '1');
  1372 			set_config('feed_overall_forums_limit', '15');
  1373 
  1374 			set_config('feed_overall_topics', '0');
  1375 			set_config('feed_overall_topics_limit', '15');
  1376 
  1377 			set_config('feed_forum', '1');
  1378 			set_config('feed_topic', '1');
  1379 			set_config('feed_item_statistics', '1');
  1380 
  1381 			// Entries for smiley pagination
  1382 			set_config('smilies_per_page', '50');
  1383 
  1384 			// Entry for reporting PMs
  1385 			set_config('allow_pm_report', '1');
  1386 
  1387 			// Install modules
  1388 			$modules_to_install = array(
  1389 				'feed'					=> array(
  1390 					'base'		=> 'board',
  1391 					'class'		=> 'acp',
  1392 					'title'		=> 'ACP_FEED_SETTINGS',
  1393 					'auth'		=> 'acl_a_board',
  1394 					'cat'		=> 'ACP_BOARD_CONFIGURATION',
  1395 					'after'		=> array('signature', 'ACP_SIGNATURE_SETTINGS')
  1396 				),
  1397 				'warnings'				=> array(
  1398 					'base'		=> 'users',
  1399 					'class'		=> 'acp',
  1400 					'title'		=> 'ACP_USER_WARNINGS',
  1401 					'auth'		=> 'acl_a_user',
  1402 					'display'	=> 0,
  1403 					'cat'		=> 'ACP_CAT_USERS',
  1404 					'after'		=> array('feedback', 'ACP_USER_FEEDBACK')
  1405 				),
  1406 				'send_statistics'		=> array(
  1407 					'base'		=> 'send_statistics',
  1408 					'class'		=> 'acp',
  1409 					'title'		=> 'ACP_SEND_STATISTICS',
  1410 					'auth'		=> 'acl_a_server',
  1411 					'cat'		=> 'ACP_SERVER_CONFIGURATION'
  1412 				),
  1413 				'setting_forum_copy'	=> array(
  1414 					'base'		=> 'permissions',
  1415 					'class'		=> 'acp',
  1416 					'title'		=> 'ACP_FORUM_PERMISSIONS_COPY',
  1417 					'auth'		=> 'acl_a_fauth && acl_a_authusers && acl_a_authgroups && acl_a_mauth',
  1418 					'cat'		=> 'ACP_FORUM_BASED_PERMISSIONS',
  1419 					'after'		=> array('setting_forum_local', 'ACP_FORUM_PERMISSIONS')
  1420 				),
  1421 				'pm_reports'			=> array(
  1422 					'base'		=> 'pm_reports',
  1423 					'class'		=> 'mcp',
  1424 					'title'		=> 'MCP_PM_REPORTS_OPEN',
  1425 					'auth'		=> 'aclf_m_report',
  1426 					'cat'		=> 'MCP_REPORTS'
  1427 				),
  1428 				'pm_reports_closed'		=> array(
  1429 					'base'		=> 'pm_reports',
  1430 					'class'		=> 'mcp',
  1431 					'title'		=> 'MCP_PM_REPORTS_CLOSED',
  1432 					'auth'		=> 'aclf_m_report',
  1433 					'cat'		=> 'MCP_REPORTS'
  1434 				),
  1435 				'pm_report_details'		=> array(
  1436 					'base'		=> 'pm_reports',
  1437 					'class'		=> 'mcp',
  1438 					'title'		=> 'MCP_PM_REPORT_DETAILS',
  1439 					'auth'		=> 'aclf_m_report',
  1440 					'cat'		=> 'MCP_REPORTS'
  1441 				),
  1442 			);
  1443 
  1444 			_add_modules($modules_to_install);
  1445 
  1446 			// Add newly_registered group... but check if it already exists (we always supported running the updater on any schema)
  1447 			$sql = 'SELECT group_id
  1448 				FROM ' . GROUPS_TABLE . "
  1449 				WHERE group_name = 'NEWLY_REGISTERED'";
  1450 			$result = $db->sql_query($sql);
  1451 			$group_id = (int) $db->sql_fetchfield('group_id');
  1452 			$db->sql_freeresult($result);
  1453 
  1454 			if (!$group_id)
  1455 			{
  1456 				$sql = 'INSERT INTO ' .  GROUPS_TABLE . " (group_name, group_type, group_founder_manage, group_colour, group_legend, group_avatar, group_desc, group_desc_uid, group_max_recipients) VALUES ('NEWLY_REGISTERED', 3, 0, '', 0, '', '', '', 5)";
  1457 				_sql($sql, $errored, $error_ary);
  1458 
  1459 				$group_id = $db->sql_nextid();
  1460 			}
  1461 
  1462 			// Insert new user role... at the end of the chain
  1463 			$sql = 'SELECT role_id
  1464 				FROM ' . ACL_ROLES_TABLE . "
  1465 				WHERE role_name = 'ROLE_USER_NEW_MEMBER'
  1466 					AND role_type = 'u_'";
  1467 			$result = $db->sql_query($sql);
  1468 			$u_role = (int) $db->sql_fetchfield('role_id');
  1469 			$db->sql_freeresult($result);
  1470 
  1471 			if (!$u_role)
  1472 			{
  1473 				$sql = 'SELECT MAX(role_order) as max_order_id
  1474 					FROM ' . ACL_ROLES_TABLE . "
  1475 					WHERE role_type = 'u_'";
  1476 				$result = $db->sql_query($sql);
  1477 				$next_order_id = (int) $db->sql_fetchfield('max_order_id');
  1478 				$db->sql_freeresult($result);
  1479 
  1480 				$next_order_id++;
  1481 
  1482 				$sql = 'INSERT INTO ' . ACL_ROLES_TABLE . " (role_name, role_description, role_type, role_order) VALUES ('ROLE_USER_NEW_MEMBER', 'ROLE_DESCRIPTION_USER_NEW_MEMBER', 'u_', $next_order_id)";
  1483 				_sql($sql, $errored, $error_ary);
  1484 				$u_role = $db->sql_nextid();
  1485 
  1486 				if (!$errored)
  1487 				{
  1488 					// Now add the correct data to the roles...
  1489 					// The standard role says that new users are not able to send a PM, Mass PM, are not able to PM groups
  1490 					$sql = 'INSERT INTO ' . ACL_ROLES_DATA_TABLE . " (role_id, auth_option_id, auth_setting) SELECT $u_role, auth_option_id, 0 FROM " . ACL_OPTIONS_TABLE . " WHERE auth_option LIKE 'u_%' AND auth_option IN ('u_sendpm', 'u_masspm', 'u_masspm_group')";
  1491 					_sql($sql, $errored, $error_ary);
  1492 
  1493 					// Add user role to group
  1494 					$sql = 'INSERT INTO ' . ACL_GROUPS_TABLE . " (group_id, forum_id, auth_option_id, auth_role_id, auth_setting) VALUES ($group_id, 0, 0, $u_role, 0)";
  1495 					_sql($sql, $errored, $error_ary);
  1496 				}
  1497 			}
  1498 
  1499 			// Insert new forum role
  1500 			$sql = 'SELECT role_id
  1501 				FROM ' . ACL_ROLES_TABLE . "
  1502 				WHERE role_name = 'ROLE_FORUM_NEW_MEMBER'
  1503 					AND role_type = 'f_'";
  1504 			$result = $db->sql_query($sql);
  1505 			$f_role = (int) $db->sql_fetchfield('role_id');
  1506 			$db->sql_freeresult($result);
  1507 
  1508 			if (!$f_role)
  1509 			{
  1510 				$sql = 'SELECT MAX(role_order) as max_order_id
  1511 					FROM ' . ACL_ROLES_TABLE . "
  1512 					WHERE role_type = 'f_'";
  1513 				$result = $db->sql_query($sql);
  1514 				$next_order_id = (int) $db->sql_fetchfield('max_order_id');
  1515 				$db->sql_freeresult($result);
  1516 
  1517 				$next_order_id++;
  1518 
  1519 				$sql = 'INSERT INTO ' . ACL_ROLES_TABLE . " (role_name, role_description, role_type, role_order) VALUES  ('ROLE_FORUM_NEW_MEMBER', 'ROLE_DESCRIPTION_FORUM_NEW_MEMBER', 'f_', $next_order_id)";
  1520 				_sql($sql, $errored, $error_ary);
  1521 				$f_role = $db->sql_nextid();
  1522 
  1523 				if (!$errored)
  1524 				{
  1525 					$sql = 'INSERT INTO ' . ACL_ROLES_DATA_TABLE . " (role_id, auth_option_id, auth_setting) SELECT $f_role, auth_option_id, 0 FROM " . ACL_OPTIONS_TABLE . " WHERE auth_option LIKE 'f_%' AND auth_option IN ('f_noapprove')";
  1526 					_sql($sql, $errored, $error_ary);
  1527 				}
  1528 			}
  1529 
  1530 			// Set every members user_new column to 0 (old users) only if there is no one yet (this makes sure we do not execute this more than once)
  1531 			$sql = 'SELECT 1
  1532 				FROM ' . USERS_TABLE . '
  1533 				WHERE user_new = 0';
  1534 			$result = $db->sql_query_limit($sql, 1);
  1535 			$row = $db->sql_fetchrow($result);
  1536 			$db->sql_freeresult($result);
  1537 
  1538 			if (!$row)
  1539 			{
  1540 				$sql = 'UPDATE ' . USERS_TABLE . ' SET user_new = 0';
  1541 				_sql($sql, $errored, $error_ary);
  1542 			}
  1543 
  1544 			// Newly registered users limit
  1545 			if (!isset($config['new_member_post_limit']))
  1546 			{
  1547 				set_config('new_member_post_limit', (!empty($config['enable_queue_trigger'])) ? $config['queue_trigger_posts'] : 0);
  1548 			}
  1549 
  1550 			if (!isset($config['new_member_group_default']))
  1551 			{
  1552 				set_config('new_member_group_default', 0);
  1553 			}
  1554 
  1555 			// To mimick the old "feature" we will assign the forum role to every forum, regardless of the setting (this makes sure there are no "this does not work!!!! YUO!!!" posts...
  1556 			// Check if the role is already assigned...
  1557 			$sql = 'SELECT forum_id
  1558 				FROM ' . ACL_GROUPS_TABLE . '
  1559 				WHERE group_id = ' . $group_id . '
  1560 					AND auth_role_id = ' . $f_role;
  1561 			$result = $db->sql_query($sql);
  1562 			$is_options = (int) $db->sql_fetchfield('forum_id');
  1563 			$db->sql_freeresult($result);
  1564 
  1565 			// Not assigned at all... :/
  1566 			if (!$is_options)
  1567 			{
  1568 				// Get postable forums
  1569 				$sql = 'SELECT forum_id
  1570 					FROM ' . FORUMS_TABLE . '
  1571 					WHERE forum_type != ' . FORUM_LINK;
  1572 				$result = $db->sql_query($sql);
  1573 
  1574 				while ($row = $db->sql_fetchrow($result))
  1575 				{
  1576 					_sql('INSERT INTO ' . ACL_GROUPS_TABLE . ' (group_id, forum_id, auth_option_id, auth_role_id, auth_setting) VALUES (' . $group_id . ', ' . (int) $row['forum_id'] . ', 0, ' . $f_role . ', 0)', $errored, $error_ary);
  1577 				}
  1578 				$db->sql_freeresult($result);
  1579 			}
  1580 
  1581 			// Clear permissions...
  1582 			include_once($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
  1583 			$auth_admin = new auth_admin();
  1584 			$auth_admin->acl_clear_prefetch();
  1585 
  1586 			if (!isset($config['allow_avatar']))
  1587 			{
  1588 				if ($config['allow_avatar_upload'] || $config['allow_avatar_local'] || $config['allow_avatar_remote'])
  1589 				{
  1590 					set_config('allow_avatar', '1');
  1591 				}
  1592 				else
  1593 				{
  1594 					set_config('allow_avatar', '0');
  1595 				}
  1596 			}
  1597 
  1598 			if (!isset($config['allow_avatar_remote_upload']))
  1599 			{
  1600 				if ($config['allow_avatar_remote'] && $config['allow_avatar_upload'])
  1601 				{
  1602 					set_config('allow_avatar_remote_upload', '1');
  1603 				}
  1604 				else
  1605 				{
  1606 					set_config('allow_avatar_remote_upload', '0');
  1607 				}
  1608 			}
  1609 
  1610 			// Minimum number of characters
  1611 			if (!isset($config['min_post_chars']))
  1612 			{
  1613 				set_config('min_post_chars', '1');
  1614 			}
  1615 
  1616 			if (!isset($config['allow_quick_reply']))
  1617 			{
  1618 				set_config('allow_quick_reply', '1');
  1619 			}
  1620 
  1621 			// Set every members user_options column to enable
  1622 			// bbcode, smilies and URLs for signatures by default
  1623 			$sql = 'SELECT user_options
  1624 				FROM ' . USERS_TABLE . '
  1625 				WHERE user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')';
  1626 			$result = $db->sql_query_limit($sql, 1);
  1627 			$user_option = (int) $db->sql_fetchfield('user_options');
  1628 			$db->sql_freeresult($result);
  1629 
  1630 			// Check if we already updated the database by checking bit 15 which we used to store the sig_bbcode option
  1631 			if (!($user_option & 1 << 15))
  1632 			{
  1633 				// 229376 is the added value to enable all three signature options
  1634 				$sql = 'UPDATE ' . USERS_TABLE . ' SET user_options = user_options + 229376';
  1635 				_sql($sql, $errored, $error_ary);
  1636 			}
  1637 
  1638 			if (!isset($config['delete_time']))
  1639 			{
  1640 				set_config('delete_time', $config['edit_time']);
  1641 			}
  1642 
  1643 			$no_updates = false;
  1644 		break;
  1645 
  1646 		// No changes from 3.0.6-RC1 to 3.0.6-RC2
  1647 		case '3.0.6-RC1':
  1648 		break;
  1649 
  1650 		// Changes from 3.0.6-RC2 to 3.0.6-RC3
  1651 		case '3.0.6-RC2':
  1652 
  1653 			// Update the Custom Profile Fields based on previous settings to the new format
  1654 			$sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . '
  1655 				SET field_show_on_vt = 1
  1656 				WHERE field_hide = 0
  1657 					AND (field_required = 1 OR field_show_on_reg = 1 OR field_show_profile = 1)';
  1658 			_sql($sql, $errored, $error_ary);
  1659 			$no_updates = false;
  1660 
  1661 		break;
  1662 
  1663 		// No changes from 3.0.6-RC3 to 3.0.6-RC4
  1664 		case '3.0.6-RC3':
  1665 		break;
  1666 
  1667 		// No changes from 3.0.6-RC4 to 3.0.6
  1668 		case '3.0.6-RC4':
  1669 		break;
  1670 
  1671 		// Changes from 3.0.6 to 3.0.7-RC1
  1672 		case '3.0.6':
  1673 
  1674 			// ATOM Feeds
  1675 			set_config('feed_overall', '1');
  1676 			set_config('feed_http_auth', '0');
  1677 			set_config('feed_limit_post', (string) (isset($config['feed_limit']) ? (int) $config['feed_limit'] : 15));
  1678 			set_config('feed_limit_topic', (string) (isset($config['feed_overall_topics_limit']) ? (int) $config['feed_overall_topics_limit'] : 10));
  1679 			set_config('feed_topics_new', (!empty($config['feed_overall_topics']) ? '1' : '0'));
  1680 			set_config('feed_topics_active', (!empty($config['feed_overall_topics']) ? '1' : '0'));
  1681 
  1682 			// Delete all text-templates from the template_data
  1683 			$sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . '
  1684 				WHERE template_filename ' . $db->sql_like_expression($db->any_char . '.txt');
  1685 			_sql($sql, $errored, $error_ary);
  1686 
  1687 			$no_updates = false;
  1688 		break;
  1689 
  1690 		// Changes from 3.0.7-RC1 to 3.0.7-RC2
  1691 		case '3.0.7-RC1':
  1692 
  1693 			$sql = 'SELECT user_id, user_email, user_email_hash
  1694 				FROM ' . USERS_TABLE . '
  1695 				WHERE user_type <> ' . USER_IGNORE . "
  1696 					AND user_email <> ''";
  1697 			$result = $db->sql_query($sql);
  1698 
  1699 			$i = 0;
  1700 			while ($row = $db->sql_fetchrow($result))
  1701 			{
  1702 				// Snapshot of the phpbb_email_hash() function
  1703 				// We cannot call it directly because the auto updater updates the DB first. :/
  1704 				$user_email_hash = sprintf('%u', crc32(strtolower($row['user_email']))) . strlen($row['user_email']);
  1705 
  1706 				if ($user_email_hash != $row['user_email_hash'])
  1707 				{
  1708 					$sql_ary = array(
  1709 						'user_email_hash'	=> $user_email_hash,
  1710 					);
  1711 
  1712 					$sql = 'UPDATE ' . USERS_TABLE . '
  1713 						SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1714 						WHERE user_id = ' . (int) $row['user_id'];
  1715 					_sql($sql, $errored, $error_ary, ($i % 100 == 0));
  1716 
  1717 					++$i;
  1718 				}
  1719 			}
  1720 			$db->sql_freeresult($result);
  1721 
  1722 			$no_updates = false;
  1723 
  1724 		break;
  1725 
  1726 		// No changes from 3.0.7-RC2 to 3.0.7
  1727 		case '3.0.7-RC2':
  1728 		break;
  1729 
  1730 		// No changes from 3.0.7 to 3.0.7-PL1
  1731 		case '3.0.7':
  1732 		break;
  1733 
  1734 		// Changes from 3.0.7-PL1 to 3.0.8-RC1
  1735 		case '3.0.7-PL1':
  1736 			// Update file extension group names to use language strings.
  1737 			$sql = 'SELECT lang_dir
  1738 				FROM ' . LANG_TABLE;
  1739 			$result = $db->sql_query($sql);
  1740 
  1741 			$extension_groups_updated = array();
  1742 			while ($lang_dir = $db->sql_fetchfield('lang_dir'))
  1743 			{
  1744 				$lang_dir = basename($lang_dir);
  1745 
  1746 				// The language strings we need are either in language/.../acp/attachments.php
  1747 				// in the update package if we're updating to 3.0.8-RC1 or later,
  1748 				// or they are in language/.../install.php when we're updating from 3.0.7-PL1 or earlier.
  1749 				// On an already updated board, they can also already be in language/.../acp/attachments.php
  1750 				// in the board root.
  1751 				$lang_files = array(
  1752 					"{$phpbb_root_path}install/update/new/language/$lang_dir/acp/attachments.$phpEx",
  1753 					"{$phpbb_root_path}language/$lang_dir/install.$phpEx",
  1754 					"{$phpbb_root_path}language/$lang_dir/acp/attachments.$phpEx",
  1755 				);
  1756 
  1757 				foreach ($lang_files as $lang_file)
  1758 				{
  1759 					if (!file_exists($lang_file))
  1760 					{
  1761 						continue;
  1762 					}
  1763 
  1764 					$lang = array();
  1765 					include($lang_file);
  1766 
  1767 					foreach($lang as $lang_key => $lang_val)
  1768 					{
  1769 						if (isset($extension_groups_updated[$lang_key]) || strpos($lang_key, 'EXT_GROUP_') !== 0)
  1770 						{
  1771 							continue;
  1772 						}
  1773 
  1774 						$sql_ary = array(
  1775 							'group_name'	=> substr($lang_key, 10), // Strip off 'EXT_GROUP_'
  1776 						);
  1777 
  1778 						$sql = 'UPDATE ' . EXTENSION_GROUPS_TABLE . '
  1779 							SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
  1780 							WHERE group_name = '" . $db->sql_escape($lang_val) . "'";
  1781 						_sql($sql, $errored, $error_ary);
  1782 
  1783 						$extension_groups_updated[$lang_key] = true;
  1784 					}
  1785 				}
  1786 			}
  1787 			$db->sql_freeresult($result);
  1788 
  1789 			// Install modules
  1790 			$modules_to_install = array(
  1791 				'post'					=> array(
  1792 					'base'		=> 'board',
  1793 					'class'		=> 'acp',
  1794 					'title'		=> 'ACP_POST_SETTINGS',
  1795 					'auth'		=> 'acl_a_board',
  1796 					'cat'		=> 'ACP_MESSAGES',
  1797 					'after'		=> array('message', 'ACP_MESSAGE_SETTINGS')
  1798 				),
  1799 			);
  1800 
  1801 			_add_modules($modules_to_install);
  1802 
  1803 			// update
  1804 			$sql = 'UPDATE ' . MODULES_TABLE . '
  1805 				SET module_auth = \'cfg_allow_avatar && (cfg_allow_avatar_local || cfg_allow_avatar_remote || cfg_allow_avatar_upload || cfg_allow_avatar_remote_upload)\'
  1806 				WHERE module_class = \'ucp\'
  1807 					AND module_basename = \'profile\'
  1808 					AND module_mode = \'avatar\'';
  1809 			_sql($sql, $errored, $error_ary);
  1810 
  1811 			// add Bing Bot
  1812 			$bot_name = 'Bing [Bot]';
  1813 			$bot_name_clean = utf8_clean_string($bot_name);
  1814 
  1815 			$sql = 'SELECT user_id
  1816 				FROM ' . USERS_TABLE . "
  1817 				WHERE username_clean = '" . $db->sql_escape($bot_name_clean) . "'";
  1818 			$result = $db->sql_query($sql);
  1819 			$bing_already_added = (bool) $db->sql_fetchfield('user_id');
  1820 			$db->sql_freeresult($result);
  1821 
  1822 			if (!$bing_already_added)
  1823 			{
  1824 				$bot_agent = 'bingbot/';
  1825 				$bot_ip = '';
  1826 				$sql = 'SELECT group_id, group_colour
  1827 					FROM ' . GROUPS_TABLE . "
  1828 					WHERE group_name = 'BOTS'";
  1829 				$result = $db->sql_query($sql);
  1830 				$group_row = $db->sql_fetchrow($result);
  1831 				$db->sql_freeresult($result);
  1832 
  1833 				if (!$group_row)
  1834 				{
  1835 					// default fallback, should never get here
  1836 					$group_row['group_id'] = 6;
  1837 					$group_row['group_colour'] = '9E8DA7';
  1838 				}
  1839 
  1840 				if (!function_exists('user_add'))
  1841 				{
  1842 					include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  1843 				}
  1844 
  1845 				$user_row = array(
  1846 					'user_type'				=> USER_IGNORE,
  1847 					'group_id'				=> $group_row['group_id'],
  1848 					'username'				=> $bot_name,
  1849 					'user_regdate'			=> time(),
  1850 					'user_password'			=> '',
  1851 					'user_colour'			=> $group_row['group_colour'],
  1852 					'user_email'			=> '',
  1853 					'user_lang'				=> $config['default_lang'],
  1854 					'user_style'			=> $config['default_style'],
  1855 					'user_timezone'			=> 0,
  1856 					'user_dateformat'		=> $config['default_dateformat'],
  1857 					'user_allow_massemail'	=> 0,
  1858 				);
  1859 
  1860 				$user_id = user_add($user_row);
  1861 
  1862 				$sql = 'INSERT INTO ' . BOTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  1863 					'bot_active'	=> 1,
  1864 					'bot_name'		=> (string) $bot_name,
  1865 					'user_id'		=> (int) $user_id,
  1866 					'bot_agent'		=> (string) $bot_agent,
  1867 					'bot_ip'		=> (string) $bot_ip,
  1868 				));
  1869 
  1870 				_sql($sql, $errored, $error_ary);
  1871 			}
  1872 			// end Bing Bot addition
  1873 
  1874 			// Delete shadow topics pointing to not existing topics
  1875 			$batch_size = 500;
  1876 
  1877 			// Set of affected forums we have to resync
  1878 			$sync_forum_ids = array();
  1879 
  1880 			do
  1881 			{
  1882 				$sql_array = array(
  1883 					'SELECT'	=> 't1.topic_id, t1.forum_id',
  1884 					'FROM'		=> array(
  1885 						TOPICS_TABLE	=> 't1',
  1886 					),
  1887 					'LEFT_JOIN'	=> array(
  1888 						array(
  1889 							'FROM'	=> array(TOPICS_TABLE	=> 't2'),
  1890 							'ON'	=> 't1.topic_moved_id = t2.topic_id',
  1891 						),
  1892 					),
  1893 					'WHERE'		=> 't1.topic_moved_id <> 0
  1894 								AND t2.topic_id IS NULL',
  1895 				);
  1896 				$sql = $db->sql_build_query('SELECT', $sql_array);
  1897 				$result = $db->sql_query_limit($sql, $batch_size);
  1898 
  1899 				$topic_ids = array();
  1900 				while ($row = $db->sql_fetchrow($result))
  1901 				{
  1902 					$topic_ids[] = (int) $row['topic_id'];
  1903 
  1904 					$sync_forum_ids[(int) $row['forum_id']] = (int) $row['forum_id'];
  1905 				}
  1906 				$db->sql_freeresult($result);
  1907 
  1908 				if (!empty($topic_ids))
  1909 				{
  1910 					$sql = 'DELETE FROM ' . TOPICS_TABLE . '
  1911 						WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1912 					$db->sql_query($sql);
  1913 				}
  1914 			}
  1915 			while (sizeof($topic_ids) == $batch_size);
  1916 
  1917 			// Sync the forums we have deleted shadow topics from.
  1918 			sync('forum', 'forum_id', $sync_forum_ids, true, true);
  1919 
  1920 			// Unread posts search load switch
  1921 			set_config('load_unreads_search', '1');
  1922 
  1923 			// Reduce queue interval to 60 seconds, email package size to 20
  1924 			if ($config['queue_interval'] == 600)
  1925 			{
  1926 				set_config('queue_interval', '60');
  1927 			}
  1928 
  1929 			if ($config['email_package_size'] == 50)
  1930 			{
  1931 				set_config('email_package_size', '20');
  1932 			}
  1933 
  1934 			$no_updates = false;
  1935 		break;
  1936 
  1937 		// No changes from 3.0.8-RC1 to 3.0.8
  1938 		case '3.0.8-RC1':
  1939 		break;
  1940 
  1941 		// Changes from 3.0.8 to 3.0.9-RC1
  1942 		case '3.0.8':
  1943 			set_config('ip_login_limit_max', '50');
  1944 			set_config('ip_login_limit_time', '21600');
  1945 			set_config('ip_login_limit_use_forwarded', '0');
  1946 
  1947 			// Update file extension group names to use language strings, again.
  1948 			$sql = 'SELECT group_id, group_name
  1949 				FROM ' . EXTENSION_GROUPS_TABLE . '
  1950 				WHERE group_name ' . $db->sql_like_expression('EXT_GROUP_' . $db->any_char);
  1951 			$result = $db->sql_query($sql);
  1952 
  1953 			while ($row = $db->sql_fetchrow($result))
  1954 			{
  1955 				$sql_ary = array(
  1956 					'group_name'	=> substr($row['group_name'], 10), // Strip off 'EXT_GROUP_'
  1957 				);
  1958 
  1959 				$sql = 'UPDATE ' . EXTENSION_GROUPS_TABLE . '
  1960 					SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1961 					WHERE group_id = ' . $row['group_id'];
  1962 				_sql($sql, $errored, $error_ary);
  1963 			}
  1964 			$db->sql_freeresult($result);
  1965 
  1966 			global $db_tools, $table_prefix;
  1967 
  1968 			// Recover from potentially broken Q&A CAPTCHA table on firebird
  1969 			// Q&A CAPTCHA was uninstallable, so it's safe to remove these
  1970 			// without data loss
  1971 			if ($db_tools->sql_layer == 'firebird')
  1972 			{
  1973 				$tables = array(
  1974 					$table_prefix . 'captcha_questions',
  1975 					$table_prefix . 'captcha_answers',
  1976 					$table_prefix . 'qa_confirm',
  1977 				);
  1978 				foreach ($tables as $table)
  1979 				{
  1980 					if ($db_tools->sql_table_exists($table))
  1981 					{
  1982 						$db_tools->sql_table_drop($table);
  1983 					}
  1984 				}
  1985 			}
  1986 
  1987 			$no_updates = false;
  1988 		break;
  1989 
  1990 		// No changes from 3.0.9-RC1 to 3.0.9-RC2
  1991 		case '3.0.9-RC1':
  1992 		break;
  1993 
  1994 		// No changes from 3.0.9-RC2 to 3.0.9-RC3
  1995 		case '3.0.9-RC2':
  1996 		break;
  1997 
  1998 		// No changes from 3.0.9-RC3 to 3.0.9-RC4
  1999 		case '3.0.9-RC3':
  2000 		break;
  2001 
  2002 		// No changes from 3.0.9-RC4 to 3.0.9
  2003 		case '3.0.9-RC4':
  2004 		break;
  2005 
  2006 		// Changes from 3.0.9 to 3.0.10-RC1
  2007 		case '3.0.9':
  2008 			if (!isset($config['email_max_chunk_size']))
  2009 			{
  2010 				set_config('email_max_chunk_size', '50');
  2011 			}
  2012 
  2013 			$no_updates = false;
  2014 		break;
  2015 
  2016 		// No changes from 3.0.10-RC1 to 3.0.10-RC2
  2017 		case '3.0.10-RC1':
  2018 		break;
  2019 
  2020 		// No changes from 3.0.10-RC2 to 3.0.10-RC3
  2021 		case '3.0.10-RC2':
  2022 		break;
  2023 
  2024 		// No changes from 3.0.10-RC3 to 3.0.10
  2025 		case '3.0.10-RC3':
  2026 		break;
  2027 	}
  2028 }
  2029 
  2030 ?>