PageRenderTime 392ms CodeModel.GetById 241ms app.highlight 112ms RepoModel.GetById 19ms app.codeStats 1ms

/doweneed/forums/upgrade.php

https://bitbucket.org/natis/masscap-main
PHP | 1935 lines | 1742 code | 108 blank | 85 comment | 30 complexity | 224bd46c37e1eaaa50d6d22b261e8953 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/***************************************************************************
   3*                                  upgrade.php
   4*                              -------------------
   5*     begin                : Wed Sep 05 2001
   6*     copyright            : (C) 2001 The phpBB Group
   7*     email                : support@phpbb.com
   8*
   9*     $Id: upgrade.php,v 1.27.2.5 2002/07/29 05:04:03 dougk_ff7 Exp $
  10*
  11****************************************************************************/
  12
  13/***************************************************************************
  14 *
  15 *   This program is free software; you can redistribute it and/or modify
  16 *   it under the terms of the GNU General Public License as published by
  17 *   the Free Software Foundation; either version 2 of the License, or
  18 *   (at your option) any later version.
  19 *
  20 ***************************************************************************/
  21
  22define('IN_PHPBB', true);
  23
  24if ( !defined('INSTALLING') )
  25{
  26	error_reporting  (E_ERROR | E_WARNING | E_PARSE); // This will NOT report uninitialized variables
  27	set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
  28
  29	//
  30	// If we are being called from the install script then we don't need these
  31	// as they are already included.
  32	//
  33	include('extension.inc');
  34	include('config.'.$phpEx);
  35	include('includes/constants.'.$phpEx);
  36	include('includes/functions.'.$phpEx);
  37
  38	if( defined("PHPBB_INSTALLED") )
  39	{
  40		$header_location = ( @preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE')) ) ? 'Refresh: 0; URL=' : 'Location: ';
  41		header($header_location . " index.$phpEx");
  42		exit;
  43	}
  44}
  45
  46//
  47// Force the DB type to be MySQL
  48//
  49$dbms = 'mysql';
  50
  51include('includes/db.'.$phpEx);
  52include('includes/bbcode.'.$phpEx);
  53include('includes/functions_search.'.$phpEx);
  54
  55set_time_limit(0); // Unlimited execution time
  56
  57$months = array(
  58	'Jan' => 1,
  59	'Feb' => 2,
  60	'Mar' => 3,
  61	'Apr' => 4,
  62	'May' => 5,
  63	'Jun' => 6,
  64	'Jul' => 7,
  65	'Aug' => 8,
  66	'Sep' => 9,
  67	'Sept' => 9,
  68	'Oct' => 10,
  69	'Nov' => 11,
  70	'Dec' => 12
  71);
  72
  73// ---------------
  74// Begin functions
  75//
  76function common_header()
  77{
  78?>
  79<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  80<html>
  81<head>
  82<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  83<meta http-equiv="Content-Style-Type" content="text/css">
  84<style type="text/css">
  85<!--
  86/* Specifiy background images for selected styles
  87   This can't be done within the external style sheet as NS4 sees image paths relative to
  88   the page which called the style sheet (i.e. this page in the root phpBB directory)
  89   whereas all other browsers see image paths relative to the style sheet. Stupid NS again!
  90*/
  91TH			{ background-image: url(templates/subSilver/images/cellpic3.gif) }
  92TD.cat		{ background-image: url(templates/subSilver/images/cellpic1.gif) }
  93TD.rowpic	{ background-image: url(templates/subSilver/images/cellpic2.jpg); background-repeat: repeat-y }
  94td.icqback	{ background-image: url(templates/subSilver/images/icon_icq_add.gif); background-repeat: no-repeat }
  95TD.catHead,TD.catSides,TD.catLeft,TD.catRight,TD.catBottom { background-image: url(templates/subSilver/images/cellpic1.gif) }
  96
  97font,th,td,p,body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11pt }
  98a:link,a:active,a:visited { font-family: Verdana, Arial, Helvetica, sans-serif; color : #006699; font-size:11pt }
  99a:hover		{ font-family: Verdana, Arial, Helvetica, sans-serif;  text-decoration: underline; color : #DD6900; font-size:11pt }
 100hr	{ height: 0px; border: solid #D1D7DC 0px; border-top-width: 1px;}
 101
 102.maintitle,h1,h2	{font-weight: bold; font-size: 22px; font-family: "Trebuchet MS",Verdana, Arial, Helvetica, sans-serif; text-decoration: none; line-height : 120%; color : #000000;}
 103
 104.ok {color:green}
 105
 106/* Import the fancy styles for IE only (NS4.x doesn't use the @import function) */
 107@import url("templates/subSilver/formIE.css"); 
 108-->
 109</style>
 110</head>
 111<body bgcolor="#FFFFFF" text="#000000" link="#006699" vlink="#5584AA">
 112
 113<table width="100%" border="0" cellspacing="0" cellpadding="10" align="center"> 
 114	<tr>
 115		<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
 116			<tr>
 117				<td><img src="templates/subSilver/images/logo_phpBB.gif" border="0" alt="Forum Home" vspace="1" /></td>
 118				<td align="center" width="100%" valign="middle"><span class="maintitle">Upgrading to phpBB 2.0</span></td>
 119			</tr>
 120		</table></td>
 121	</tr>
 122</table>
 123
 124<br clear="all" />
 125
 126<?
 127	return;
 128}
 129
 130function common_footer()
 131{
 132?>
 133
 134<br clear="all" />
 135
 136</body>
 137</html>
 138<?
 139	return;
 140}
 141
 142function query($sql, $errormsg)
 143{
 144	global $db;
 145
 146	if ( !($result = $db->sql_query($sql)) )
 147	{
 148		print "<br><font color=\"red\">\n";
 149		print "$errormsg<br>";
 150
 151		$sql_error = $db->sql_error();
 152		print $sql_error['code'] .": ". $sql_error['message']. "<br>\n";
 153
 154		print "<pre>$sql</pre>";
 155		print "</font>\n";
 156
 157		return FALSE;
 158	}
 159	else
 160	{
 161		return $result;
 162	}
 163}
 164
 165function smiley_replace($text = '')
 166{
 167	global $db;
 168
 169	static $search, $replace;
 170
 171	// Did we get the smiley info in a previous call?
 172	if ( !is_array($search) )
 173	{
 174		$sql = "SELECT code, smile_url
 175			FROM smiles";
 176		$result = query($sql, "Unable to get list of smilies from the DB");
 177
 178		$smilies = $db->sql_fetchrowset($result);
 179		@usort($smilies, 'smiley_sort');
 180
 181		$search = array();
 182		$replace = array();
 183		for($i = 0; $i < count($smilies); $i++)
 184		{
 185			$search[] = '/<IMG SRC=".*?\/' . phpbb_preg_quote($smilies[$i]['smile_url'], '/') .'">/i';
 186			$replace[] = $smilies[$i]['code'];
 187		}
 188	}
 189
 190	return ( $text != '' ) ? preg_replace($search, $replace, $text) : '';
 191	
 192}
 193
 194function get_schema()
 195{
 196	global $table_prefix;
 197
 198	$schemafile = file('db/schemas/mysql_schema.sql');
 199	$tabledata = 0;
 200
 201	for($i=0; $i < count($schemafile); $i++)
 202	{
 203		$line = $schemafile[$i];
 204
 205		if ( preg_match('/^CREATE TABLE (\w+)/i', $line, $matches) )
 206		{
 207			// Start of a new table definition, set some variables and go to the next line.
 208			$tabledata = 1;
 209			// Replace the 'phpbb_' prefix by the user defined prefix.
 210			$table = str_replace('phpbb_', $table_prefix, $matches[1]);
 211			$table_def[$table] = "CREATE TABLE $table (\n";
 212			continue;
 213		}
 214
 215		if ( preg_match('/^\);/', $line) )
 216		{
 217			// End of the table definition
 218			// After this we will skip everything until the next 'CREATE' line
 219			$tabledata = 0;
 220			$table_def[$table] .= ')'; // We don't need the closing semicolon
 221		}
 222
 223		if ( $tabledata == 1 )
 224		{
 225			// We are inside a table definition, parse this line.
 226			// Add the current line to the complete table definition:
 227			$table_def[$table] .= $line;
 228			if ( preg_match('/^\s*(\w+)\s+(\w+)\(([\d,]+)\)(.*)$/', $line, $matches) )
 229			{
 230				// This is a column definition
 231				$field = $matches[1];
 232				$type = $matches[2];
 233				$size = $matches[3];
 234
 235				preg_match('/DEFAULT (NULL|\'.*?\')[,\s](.*)$/i', $matches[4], $match);
 236				$default = $match[1];
 237
 238				$notnull = ( preg_match('/NOT NULL/i', $matches[4]) ) ? 1 : 0;
 239				$auto_increment = ( preg_match('/auto_increment/i', $matches[4]) ) ? 1 : 0;
 240
 241				$field_def[$table][$field] = array(
 242					'type' => $type,
 243					'size' => $size,
 244					'default' => $default,
 245					'notnull' => $notnull,
 246					'auto_increment' => $auto_increment
 247				);
 248			}
 249			
 250			if ( preg_match('/\s*PRIMARY\s+KEY\s*\((.*)\).*/', $line, $matches) )
 251			{
 252				// Primary key
 253				$key_def[$table]['PRIMARY'] = $matches[1];
 254			}
 255			else if ( preg_match('/\s*KEY\s+(\w+)\s*\((.*)\)/', $line, $matches) )
 256			{
 257				// Normal key
 258				$key_def[$table][$matches[1]] = $matches[2];
 259			}
 260			else if ( preg_match('/^\s*(\w+)\s*(.*?),?\s*$/', $line, $matches) )
 261			{
 262				// Column definition
 263				$create_def[$table][$matches[1]] = $matches[2];
 264			}
 265			else
 266			{
 267				// It's a bird! It's a plane! It's something we didn't expect ;(
 268			}
 269		}
 270	}
 271
 272	$schema['field_def'] = $field_def;
 273	$schema['table_def'] = $table_def;
 274	$schema['create_def'] = $create_def;
 275	$schema['key_def'] = $key_def;
 276
 277	return $schema;
 278}
 279
 280function get_inserts()
 281{
 282	global $table_prefix;
 283
 284	$insertfile = file('db/schemas/mysql_basic.sql');
 285
 286	for($i = 0; $i < count($insertfile); $i++)
 287	{
 288		if ( preg_match('/(INSERT INTO (\w+)\s.*);/i', str_replace('phpbb_', $table_prefix, $insertfile[$i]), $matches) )
 289		{
 290			$returnvalue[$matches[2]][] = $matches[1];
 291		}
 292	}
 293
 294	return $returnvalue;
 295}
 296
 297function lock_tables($state, $tables= '')
 298{
 299	if ( $state == 1 )
 300	{
 301		if ( is_array($tables) )
 302		{
 303			$tables = join(' WRITE, ', $tables);
 304		}
 305
 306		query("LOCK TABLES $tables WRITE", "Couldn't do: $sql");
 307	}
 308	else
 309	{
 310		query("UNLOCK TABLES", "Couldn't unlock all tables");
 311	}
 312}
 313
 314function output_table_content($content)
 315{
 316	echo $content . "\n";
 317
 318	return;
 319}
 320
 321//
 322// Nathan's bbcode2 conversion routines
 323//
 324function bbdecode($message)
 325{
 326	// Undo [code]
 327	$code_start_html = '<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font size=-1>Code:</font><HR></TD></TR><TR><TD><FONT SIZE=-1><PRE>';
 328	$code_end_html = '</PRE></FONT></TD></TR><TR><TD><HR></TD></TR></TABLE><!-- BBCode End -->';
 329	$message = str_replace($code_start_html, '[code]', $message);
 330	$message = str_replace($code_end_html, '[/code]', $message);
 331
 332	// Undo [quote]
 333	$quote_start_html = '<!-- BBCode Quote Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font size=-1>Quote:</font><HR></TD></TR><TR><TD><FONT SIZE=-1><BLOCKQUOTE>';
 334	$quote_end_html = '</BLOCKQUOTE></FONT></TD></TR><TR><TD><HR></TD></TR></TABLE><!-- BBCode Quote End -->';
 335	$message = str_replace($quote_start_html, '[quote]', $message);
 336	$message = str_replace($quote_end_html, '[/quote]', $message);
 337
 338	// Undo [b] and [i]
 339	$message = preg_replace("#<!-- BBCode Start --><B>(.*?)</B><!-- BBCode End -->#s", "[b]\\1[/b]", $message);
 340	$message = preg_replace("#<!-- BBCode Start --><I>(.*?)</I><!-- BBCode End -->#s", "[i]\\1[/i]", $message);
 341
 342	// Undo [url] (long form)
 343	$message = preg_replace("#<!-- BBCode u2 Start --><A HREF=\"([a-z]+?://)(.*?)\" TARGET=\"_blank\">(.*?)</A><!-- BBCode u2 End -->#s", "[url=\\1\\2]\\3[/url]", $message);
 344
 345	// Undo [url] (short form)
 346	$message = preg_replace("#<!-- BBCode u1 Start --><A HREF=\"([a-z]+?://)(.*?)\" TARGET=\"_blank\">(.*?)</A><!-- BBCode u1 End -->#s", "[url]\\3[/url]", $message);
 347
 348	// Undo [email]
 349	$message = preg_replace("#<!-- BBCode Start --><A HREF=\"mailto:(.*?)\">(.*?)</A><!-- BBCode End -->#s", "[email]\\1[/email]", $message);
 350
 351	// Undo [img]
 352	$message = preg_replace("#<!-- BBCode Start --><IMG SRC=\"(.*?)\" BORDER=\"0\"><!-- BBCode End -->#s", "[img]\\1[/img]", $message);
 353
 354	// Undo lists (unordered/ordered)
 355
 356	// <li> tags:
 357	$message = str_replace('<!-- BBCode --><LI>', '[*]', $message);
 358
 359	// [list] tags:
 360	$message = str_replace('<!-- BBCode ulist Start --><UL>', '[list]', $message);
 361
 362	// [list=x] tags:
 363	$message = preg_replace('#<!-- BBCode olist Start --><OL TYPE=([A1])>#si', "[list=\\1]", $message);
 364
 365	// [/list] tags:
 366	$message = str_replace('</UL><!-- BBCode ulist End -->', '[/list]', $message);
 367	$message = str_replace('</OL><!-- BBCode olist End -->', '[/list]', $message);
 368
 369	return $message;
 370}
 371
 372//
 373// Alternative for in_array() which is only available in PHP4
 374//
 375function inarray($needle, $haystack)
 376{ 
 377	for( $i = 0 ; $i < sizeof($haystack) ; $i++ )
 378	{ 
 379		if ( $haystack[$i] == $needle )
 380		{ 
 381			return true; 
 382		} 
 383	} 
 384
 385	return false; 
 386}
 387
 388function end_step($next)
 389{
 390	global $debug;
 391
 392	print "<hr /><a href=\"$PHP_SELF?next=$next\">Next step: <b>$next</b></a><br /><br />\n";
 393}
 394//
 395// End functions
 396// -------------
 397
 398
 399//
 400// Start at the beginning if the user hasn't specified a specific starting point.
 401//
 402$next = ( isset($HTTP_GET_VARS['next']) ) ? $HTTP_GET_VARS['next'] : 'start';
 403
 404// If debug is set we'll do all steps in one go.
 405$debug = 1;
 406
 407// Parse the MySQL schema file into some arrays.
 408$schema = get_schema();
 409
 410$table_def = $schema['table_def'];
 411$field_def = $schema['field_def'];
 412$key_def = $schema['key_def'];
 413$create_def = $schema['create_def'];
 414
 415//
 416// Get mysql_basic data
 417//
 418$inserts = get_inserts();
 419
 420//
 421// Get smiley data
 422//
 423smiley_replace();
 424
 425common_header();
 426
 427if ( !empty($next) )
 428{
 429	switch($next)
 430	{
 431		case 'start':
 432			end_step('initial_drops');
 433
 434		case 'initial_drops':
 435			print " * Dropping sessions and themes tables :: ";
 436			flush();
 437
 438			query("DROP TABLE sessions", "Couldn't drop table 'sessions'");
 439			query("DROP TABLE themes", "Couldn't drop table 'themes'");   
 440
 441			print "<span class=\"ok\"><b>OK</b></span><br />\n";
 442
 443			end_step('mod_old_tables');
 444
 445		case 'mod_old_tables':
 446			$modtables = array(
 447				"banlist" => "banlist",
 448				"catagories" => "categories",
 449				"config" => "old_config",
 450				"forums" => "forums",
 451				"disallow" => "disallow",
 452				"posts" => "posts",
 453				"posts_text" => "posts_text",
 454				"priv_msgs" => "privmsgs",
 455				"ranks" => "ranks",
 456				"smiles" => "smilies",
 457				"topics" => "topics",
 458				"users" => "users",
 459				"words" => "words"
 460			);
 461
 462			while( list($old, $new) = each($modtables) )
 463			{
 464				$result = query("SHOW INDEX FROM $old", "Couldn't get list of indices for table $old");
 465
 466				while( $row = $db->sql_fetchrow($result) )
 467				{
 468					$index = $row['Key_name'];
 469					if ( $index != 'PRIMARY' )
 470					{
 471						query("ALTER TABLE $old DROP INDEX $index", "Couldn't DROP INDEX $old.$index");
 472					}
 473				}
 474
 475				// Rename table
 476				$new = $table_prefix . $new;
 477
 478				print " * Renaming '$old' to '$new' :: ";
 479				flush();
 480				query("ALTER TABLE $old RENAME $new", "Failed to rename $old to $new");
 481				print "<span class=\"ok\"><b>OK</b></span><br />\n";
 482				
 483			}
 484			end_step('create_tables');
 485			
 486		case 'create_tables':
 487			// Create array with tables in 'old' database
 488			$result = query('SHOW TABLES', "Couldn't get list of current tables");
 489
 490			while( $table = $db->sql_fetchrow($result) )
 491			{
 492				$currenttables[] = $table[0];
 493			}
 494			
 495			// Check what tables we need to CREATE
 496			while( list($table, $definition) = each($table_def) )
 497			{
 498				if ( !inarray($table, $currenttables) )
 499				{
 500					print " * Creating $table :: ";
 501
 502					query($definition, "Couldn't create table $table");
 503
 504					print "<span class=\"ok\"><b>OK</b></span><br />\n";
 505				}
 506			}
 507			
 508			end_step('create_config');
 509			
 510		case 'create_config':
 511			print " * Inserting new values into new layout config table :: ";
 512
 513			@reset($inserts);
 514			while( list($table, $inserts_table) = each($inserts) )
 515			{
 516				if ( $table == CONFIG_TABLE )
 517				{
 518					$per_pct = ceil( count($inserts_table) / 40 );
 519					$inc = 0;
 520
 521					while( list($nr, $insert) = each($inserts_table) )
 522					{
 523						query($insert, "Couldn't insert value into config table");
 524
 525						$inc++;
 526						if ( $inc == $per_pct )
 527						{
 528							print ".";
 529							flush();
 530							$inc = 0;
 531						}
 532					}
 533				}
 534			}
 535
 536			print " <span class=\"ok\"><b>OK</b></span><br />\n";
 537
 538			end_step('convert_config');
 539			
 540		case 'convert_config':
 541			print " * Converting configuration table :: ";
 542
 543			$sql = "SELECT * 
 544				FROM $table_prefix" . "old_config";
 545			$result = query($sql, "Couldn't get info from old config table");
 546
 547			$oldconfig = $db->sql_fetchrow($result);
 548
 549			//
 550			// We don't need several original config types and two others
 551			// have changed name ... so take account of this.
 552			//
 553			$ignore_configs = array("selected", "admin_passwd", "override_themes", "allow_sig");
 554			$rename_configs = array(
 555				"email_from" => "board_email",
 556				"email_sig" => "board_email_sig"
 557			);
 558
 559			while( list($name, $value) = each($oldconfig) )
 560			{
 561				if ( is_int($name) )
 562				{
 563					continue;
 564				}
 565
 566				if ( !inarray($name, $ignore_configs) )
 567				{
 568					$name = ( !empty($rename_configs[$name]) ) ? $rename_configs[$name] : $name;
 569
 570					$sql = "REPLACE INTO " . CONFIG_TABLE . " (config_name, config_value) 
 571						VALUES ('$name', '" . stripslashes($value) . "')";
 572					query($sql, "Couldn't update config table with values from old config table");
 573				}
 574			}
 575			
 576			$sql = "UPDATE " . CONFIG_TABLE . " 
 577				SET config_value = 'dutch' 
 578				WHERE config_name = 'default_lang' && config_value = 'nederlands'";
 579			query($sql, "Couldn't rename 'nederlands' to 'dutch' in config table");
 580			
 581			print "<span class=\"ok\"><b>OK</b></span><br />\n";
 582			end_step('convert_ips');
 583
 584		case 'convert_ips':
 585			$names = array( 
 586				POSTS_TABLE => array(
 587					'id' => 'post_id',
 588					'field' => 'poster_ip'
 589				), 
 590				PRIVMSGS_TABLE => array( 
 591					'id' => 'msg_id', 
 592					'field' => 'poster_ip'
 593				), 
 594				BANLIST_TABLE => array( 
 595					'id' => 'ban_id', 
 596					'field' => 'ban_ip'
 597				)
 598			);
 599
 600			lock_tables(1, array(POSTS_TABLE, PRIVMSGS_TABLE, BANLIST_TABLE));
 601
 602			$batchsize = 2000;
 603			while( list($table, $data_array) = each($names) )
 604			{
 605				$sql = "SELECT MAX(" . $data_array['id'] . ") AS max_id 
 606					FROM $table";
 607				$result = query($sql, "Couldn't obtain ip data from $table (" . $fields . ")");
 608
 609				$row = $db->sql_fetchrow($result);
 610
 611				$maxid = $row['max_id'];
 612
 613				for($i = 0; $i <= $maxid; $i += $batchsize)
 614				{
 615					$batchstart = $i;
 616					$batchend = $i + $batchsize;
 617
 618					$field_id = $data_array['id'];
 619					$field = $data_array['field'];
 620
 621					print " * Converting IP format '" . $field . "' / '$table' ( $batchstart to $batchend ) :: ";
 622					flush();
 623
 624					$sql = "SELECT $field_id, $field 
 625						FROM $table 
 626						WHERE $field_id 
 627							BETWEEN $batchstart 
 628								AND $batchend";
 629					$result = query($sql, "Couldn't obtain ip data from $table (" . $fields . ")");
 630
 631					$per_pct = ceil( $db->sql_numrows($result) / 40 );
 632					$inc = 0;
 633
 634					while( $row = $db->sql_fetchrow($result) )
 635					{
 636						$sql = "UPDATE $table 
 637							SET $field = '" . encode_ip($row[$field]) . "' 
 638							WHERE $field_id = " . $row[$field_id];
 639						query($sql, "Couldn't convert IP format of $field in $table with $field_id of " . $rowset[$field_id]);
 640
 641						$inc++;
 642						if ( $inc == $per_pct )
 643						{
 644							print ".";
 645							flush();
 646							$inc = 0;
 647						}
 648					}
 649
 650					print " <span class=\"ok\"><b>OK</b></span><br />\n";
 651				}
 652			}
 653
 654			lock_tables(0);
 655			end_step('convert_dates');
 656
 657		case 'convert_dates':
 658			$names = array(
 659				POSTS_TABLE => array('post_time'),
 660				TOPICS_TABLE => array('topic_time'), 
 661				PRIVMSGS_TABLE => array('msg_time')
 662			);
 663
 664			lock_tables(1, array(POSTS_TABLE, TOPICS_TABLE, PRIVMSGS_TABLE));
 665
 666			while( list($table, $fields) = each($names) )
 667			{
 668				print " * Converting date format of $fields[$i] in $table :: ";
 669				flush();
 670
 671				for($i = 0; $i < count($fields); $i++)
 672				{
 673					$sql = "UPDATE $table 
 674						SET " . $fields[$i] . " = UNIX_TIMESTAMP(" . $fields[$i] . ")";
 675					query($sql, "Couldn't convert date format of $table(" . $fields[$i] . ")");
 676				}
 677
 678				print "<span class=\"ok\"><b>OK</b></span><br />\n";
 679			}
 680
 681			lock_tables(0);
 682			end_step('fix_addslashes');
 683
 684		case 'fix_addslashes':
 685			$slashfields[TOPICS_TABLE] = array('topic_title');
 686			$slashfields[FORUMS_TABLE] = array('forum_desc', 'forum_name');
 687			$slashfields[CATEGORIES_TABLE] = array('cat_title');
 688			$slashfields[WORDS_TABLE] = array('word', 'replacement');
 689			$slashfields[RANKS_TABLE] = array('rank_title');
 690			$slashfields[DISALLOW_TABLE] = array('disallow_username');
 691
 692			//convert smilies?
 693			$slashes = array(
 694				"\\'" => "'",
 695				"\\\"" => "\"",
 696				"\\\\" => "\\");
 697			$slashes = array(
 698				"\\'" => "'",
 699				"\\\"" => "\"",
 700				"\\\\" => "\\");
 701
 702			lock_tables(1, array(TOPICS_TABLE, FORUMS_TABLE, CATEGORIES_TABLE, WORDS_TABLE, RANKS_TABLE, DISALLOW_TABLE, SMILIES_TABLE));
 703
 704			while( list($table, $fields) = each($slashfields) )
 705			{
 706				print " * Removing slashes from $table table :: ";
 707				flush();
 708
 709				while( list($nr, $field) = each($fields) )
 710				{
 711					@reset($slashes);
 712					while( list($search, $replace) = each($slashes) )
 713					{
 714						$sql = "UPDATE $table 
 715							SET $field = REPLACE($field, '" . addslashes($search) . "', '" . addslashes($replace) . "')";
 716						query($sql, "Couldn't remove extraneous slashes from the old data.");
 717					}
 718				}
 719
 720				print "<span class=\"ok\"><b>OK</b></span><br />\n";
 721			}
 722
 723			lock_tables(0);
 724			end_step('remove_topics');
 725
 726		case 'remove_topics':
 727			print " * Removing posts with no corresponding topics :: ";
 728			flush();
 729
 730			$sql = "SELECT p.post_id 
 731				FROM " . POSTS_TABLE . " p 
 732				LEFT JOIN " . TOPICS_TABLE . " t ON p.topic_id = t.topic_id  
 733				WHERE t.topic_id IS NULL";
 734			$result = query($sql, "Couldn't obtain list of deleted topics");
 735			
 736			$post_total = $db->sql_numrows($result);
 737
 738			if ( $post_total )
 739			{
 740				$post_id_ary = array();
 741				while( $row = $db->sql_fetchrow($result) )
 742				{
 743					$post_id_ary[] = $row['post_id'];
 744				}
 745
 746				$sql = "DELETE FROM " . POSTS_TABLE . "  
 747					WHERE post_id IN (" . implode(", ", $post_id_ary) . ")";
 748				query($sql, "Couldn't update posts to remove deleted user poster_id values");
 749
 750				$sql = "DELETE FROM " . POSTS_TEXT_TABLE . "
 751					WHERE post_id IN (" . implode(", ", $post_id_ary) . ")";
 752				query($sql, "Couldn't update posts to remove deleted user poster_id values");
 753			}
 754
 755			echo "<span class=\"ok\"><b>OK</b></span> ( Removed $post_total posts )<br />\n";
 756			end_step('convert_users');
 757
 758		case 'convert_users':
 759			//
 760			// Completely remove old soft-deleted users
 761			//
 762			$sql = "DELETE FROM " . USERS_TABLE . " 
 763				WHERE user_level = -1";
 764			query($sql, "Couldn't delete old soft-deleted users");
 765
 766			$sql = "SELECT COUNT(*) AS total, MAX(user_id) AS maxid 
 767				FROM " . USERS_TABLE;
 768			$result = query($sql, "Couldn't get max post_id.");
 769
 770			$maxid = $db->sql_fetchrow($result);
 771
 772			$totalposts = $maxid['total'];
 773			$maxid = $maxid['maxid'];
 774
 775			$sql = "ALTER TABLE " . USERS_TABLE . " 
 776				ADD user_sig_bbcode_uid CHAR(10),
 777				MODIFY user_sig text";
 778			query($sql, "Couldn't add user_sig_bbcode_uid field to users table");
 779
 780			$super_mods = array();
 781			$first_admin = -2;
 782
 783			$batchsize = 1000;
 784			for($i = -1; $i <= $maxid; $i += $batchsize)
 785			{
 786				$batchstart = $i;
 787				$batchend = $i + $batchsize;
 788				
 789				print " * Converting Users ( $batchstart to $batchend ) :: ";
 790				flush();
 791
 792				$sql = "SELECT * 
 793					FROM " . USERS_TABLE . " 
 794					WHERE user_id 
 795						BETWEEN $batchstart 
 796							AND $batchend";
 797				$result = query($sql, "Couldn't get ". USERS_TABLE .".user_id $batchstart to $batchend");
 798
 799				// Array with user fields that we want to check for invalid data (to few characters)
 800				$checklength = array(
 801					'user_occ',
 802					'user_website',
 803					'user_email',
 804					'user_from',
 805					'user_intrest',
 806					'user_aim',
 807					'user_yim',
 808					'user_msnm');
 809
 810				lock_tables(1, array(USERS_TABLE, GROUPS_TABLE, USER_GROUP_TABLE, POSTS_TABLE));
 811
 812				$per_pct = ceil( $db->sql_numrows($result) / 40 );
 813				$inc = 0;
 814
 815				$group_id = 1;
 816				while( $row = $db->sql_fetchrow($result) )
 817				{
 818					$sql = "INSERT INTO " . GROUPS_TABLE . " (group_id, group_name, group_description, group_single_user) 
 819						VALUES ($group_id, '" . addslashes($row['username']) . "', 'Personal User', 1)";
 820					query($sql, "Wasn't able to insert user ".$row['user_id']." into table ".GROUPS_TABLE);
 821
 822					$sql = "INSERT INTO " . USER_GROUP_TABLE . " (group_id, user_id, user_pending)	
 823						VALUES ($group_id, " . $row['user_id'] . ", 0)";
 824					query($sql, "Wasn't able to insert user ".$row['user_id']." into table ".USER_GROUP_TABLE);
 825
 826					if ( is_int($row['user_regdate']) )
 827					{
 828						// We already converted this post to the new style BBcode, skip this post.
 829						continue;
 830					}
 831
 832					$group_id++;
 833
 834					//
 835					// Nathan's bbcode2 conversion
 836					//
 837
 838					// undo 1.2.x encoding..
 839					$row['user_sig'] = bbdecode(stripslashes($row['user_sig']));
 840					$row['user_sig'] = undo_make_clickable($row['user_sig']);
 841					$row['user_sig'] = str_replace("<BR>", "\n", $row['user_sig']);
 842
 843					// make a uid
 844					$uid = make_bbcode_uid();
 845
 846					// do 2.x first-pass encoding..
 847					$row['user_sig'] = bbencode_first_pass($row['user_sig'], $uid);
 848					$row['user_sig'] = addslashes($row['user_sig']);
 849
 850					// Check for invalid info like '-' and '?' for a lot of fields
 851					@reset($checklength);
 852					while($field = each($checklength))
 853					{
 854						$row[$field[1]] = strlen($row[$field[1]]) < 3 ? '' : $row[$field[1]];
 855					}
 856
 857					preg_match('/(.*?) (\d{1,2}), (\d{4})/', $row['user_regdate'], $parts);
 858					$row['user_regdate'] = gmmktime(0, 0, 0, $months[$parts[1]], $parts[2], $parts[3]);
 859
 860					$website = $row['user_website'];
 861					if ( substr(strtolower($website), 0, 7) != "http://" )
 862					{
 863						$website = "http://" . $website;
 864					}
 865					if( strtolower($website) == 'http://' )
 866					{
 867						$website = '';
 868					}
 869					$row['user_website'] = addslashes($website);
 870					
 871					$row['user_icq'] = (ereg("^[0-9]+$", $row['user_icq'])) ? $row['user_icq'] : '';
 872					reset($checklength);
 873
 874					while($field = each($checklength))
 875					{
 876						if ( strlen($row[$field[1]]) < 3 )
 877						{
 878							$row[$field[1]] = '';
 879						}
 880						$row[$field[1]] = addslashes($row[$field[1]]);
 881					}
 882					
 883					//
 884					// Is user a super moderator?
 885					//
 886					if( $row['user_level'] == 3 )
 887					{
 888						$super_mods[] = $row['user_id'];
 889					}
 890
 891					$row['user_level'] = ( $row['user_level'] == 4 ) ? ADMIN : USER;
 892
 893					//
 894					// Used to define a 'practical' group moderator user_id
 895					// for super mods a little latter.
 896					//
 897					if( $first_admin == -2 && $row['user_level'] == ADMIN )
 898					{
 899						$first_admin = $row['user_id'];
 900					}
 901
 902					//
 903					// Dutch language files have been renamed from 'nederlands' to 'dutch'
 904					//
 905					if( $row['user_lang'] == 'nederlands' )
 906					{
 907						$row['user_lang'] = 'dutch';
 908					}
 909
 910					$sql = "UPDATE " . USERS_TABLE . " 
 911						SET 
 912							user_sig = '" . $row['user_sig'] . "',
 913							user_sig_bbcode_uid = '$uid', 
 914							user_regdate = '" . $row['user_regdate'] . "',
 915							user_website = '" . $row['user_website'] . "',
 916							user_occ = '" . $row['user_occ'] . "',
 917							user_email = '" . $row['user_email'] . "',
 918							user_from = '" . $row['user_from'] . "',
 919							user_intrest = '" . $row['user_intrest'] . "', 
 920							user_aim = '" . $row['user_aim'] . "',
 921							user_yim = '" . $row['user_yim'] . "',
 922							user_msnm = '" . $row['user_msnm'] . "',
 923							user_level = '" . $row['user_level'] . "', 
 924							user_desmile = NOT(user_desmile), 
 925							user_bbcode = 1, 
 926							user_theme = 1 
 927						WHERE user_id = " . $row['user_id'];
 928					query($sql, "Couldn't update ".USERS_TABLE." table with new BBcode and regdate for user_id ".$row['user_id']);
 929
 930					$inc++;
 931					if ( $inc == $per_pct )
 932					{
 933						print ".";
 934						flush();
 935						$inc = 0;
 936					}
 937				}
 938
 939				print " <span class=\"ok\"><b>OK</b></span><br />\n";
 940
 941				lock_tables(0);
 942			}
 943
 944			//
 945			// Handle super-mods, create hidden group for them
 946			//
 947			// Iterate trough access table
 948			if( count($super_mods) && $first_admin != -2 )
 949			{
 950				print "\n<br />\n * Creating new group for super moderators :: ";
 951				flush();
 952
 953				$sql = "INSERT INTO " . GROUPS_TABLE . " (group_type, group_name, group_description, group_moderator, group_single_user)
 954					VALUES (" . GROUP_HIDDEN . ", 'Super Moderators', 'Converted super moderators', $first_admin, 0)";
 955				$result = query($sql, "Couldn't create group for ".$row['forum_name']);
 956
 957				$group_id = $db->sql_nextid();
 958
 959				if ( $group_id <= 0 )
 960				{
 961					print "<font color=\"red\">Group creation failed. Aborting creation of groups...<br></font>\n";
 962					continue 2;
 963				}
 964
 965				print "<span class=\"ok\"><b>OK</b></span><br />\n";
 966
 967				print " * Updating auth_access for super moderator group :: ";
 968				flush();
 969
 970				$sql = "SELECT forum_id 
 971					FROM " . FORUMS_TABLE;
 972				$result = query($sql, "Couldn't obtain forum_id list");
 973
 974				while( $row = $db->sql_fetchrow($result) )
 975				{
 976					$sql = "INSERT INTO " . AUTH_ACCESS_TABLE . " (group_id, forum_id, auth_mod)
 977						VALUES ($group_id, " . $row['forum_id'] . ", 1)";
 978					$result_insert = query($sql, "Unable to set group auth access for super mods.");
 979				}
 980
 981				for($i = 0; $i < count($super_mods); $i++)
 982				{
 983					$sql = "INSERT INTO " . USER_GROUP_TABLE . " (group_id, user_id, user_pending)
 984						VALUES ($group_id, " . $super_mods[$i] . ", 0)";
 985					query($sql, "Unable to add user_id $user_id to group_id $group_id (super mods)<br>\n");
 986				}
 987			
 988				print "<span class=\"ok\"><b>OK</b></span><br />\n";
 989			}
 990
 991			end_step('convert_posts');
 992
 993		case 'convert_posts':
 994			print " * Adding enable_sig field to " . POSTS_TABLE . " :: ";
 995			flush();
 996			$sql = "ALTER TABLE " . POSTS_TABLE . " 
 997				ADD enable_sig tinyint(1) DEFAULT '1' NOT NULL";
 998			$result = query($sql, "Couldn't add enable_sig field to " . POSTS_TABLE . ".");
 999			print "<span class=\"ok\"><b>OK</b></span><br />\n";
1000			
1001			print " * Adding enable_bbcode field to " . POSTS_TEXT_TABLE . " :: ";
1002			flush();
1003			$sql = "ALTER TABLE " . POSTS_TEXT_TABLE . "  
1004				ADD enable_bbcode tinyint(1) DEFAULT '1' NOT NULL";
1005			$result = query($sql, "Couldn't add enable_bbcode field to " . POSTS_TABLE . ".");
1006			print "<span class=\"ok\"><b>OK</b></span><br />\n";
1007
1008			print " * Adding bbcode_uid field to " . POSTS_TEXT_TABLE . " :: ";
1009			flush();
1010			$sql = "ALTER TABLE " . POSTS_TEXT_TABLE . "  
1011				ADD bbcode_uid char(10) NOT NULL";
1012			$result = query($sql, "Couldn't add bbcode_uid field to " . POSTS_TABLE . ".");
1013			print "<span class=\"ok\"><b>OK</b></span><br />\n";
1014			
1015			print " * Adding post_edit_time field to " . POSTS_TABLE . " :: ";
1016			flush();
1017			$sql = "ALTER TABLE " . POSTS_TABLE . "  
1018				ADD post_edit_time int(11)";
1019			$result = query($sql, "Couldn't add post_edit_time field to " . POSTS_TABLE . ".");
1020			print "<span class=\"ok\"><b>OK</b></span><br />\n";
1021
1022			print " * Adding post_edit_count field to " . POSTS_TABLE . " :: ";
1023			flush();
1024			$sql = "ALTER TABLE " . POSTS_TABLE . "  
1025				ADD post_edit_count smallint(5) UNSIGNED DEFAULT '0' NOT NULL";
1026			$result = query($sql, "Couldn't add post_edit_count field to " . POSTS_TABLE . ".");
1027			print "<span class=\"ok\"><b>OK</b></span><br />\n<br />\n";
1028
1029			$sql = "SELECT COUNT(*) as total, MAX(post_id) as maxid 
1030				FROM " . POSTS_TEXT_TABLE;
1031			$result = query($sql, "Couldn't get max post_id.");
1032
1033			$maxid = $db->sql_fetchrow($result);
1034
1035			$totalposts = $maxid['total'];
1036			$maxid = $maxid['maxid'];
1037
1038			$batchsize = 2000;
1039			for($i = 0; $i <= $maxid; $i += $batchsize)
1040			{
1041				$batchstart = $i + 1;
1042				$batchend = $i + $batchsize;
1043				
1044				print " * Converting BBcode ( $batchstart to $batchend ) :: ";
1045				flush();
1046
1047				$sql = "SELECT * 
1048					FROM " . POSTS_TEXT_TABLE . "
1049					WHERE post_id 
1050						BETWEEN $batchstart 
1051							AND $batchend";
1052				$result = query($sql, "Couldn't get ". POSTS_TEXT_TABLE .".post_id $batchstart to $batchend");
1053
1054				lock_tables(1, array(POSTS_TEXT_TABLE, POSTS_TABLE));
1055
1056				$per_pct = ceil( $db->sql_numrows($result) / 40 );
1057				$inc = 0;
1058
1059				while( $row = $db->sql_fetchrow($result) )
1060				{
1061					if ( $row['bbcode_uid'] != '' )
1062					{
1063						// We already converted this post to the new style BBcode, skip this post.
1064						continue;
1065					}
1066
1067					//
1068					// Nathan's bbcode2 conversion
1069					//
1070					// undo 1.2.x encoding..
1071					$row['post_text'] = bbdecode(stripslashes($row['post_text']));
1072					$row['post_text'] = undo_make_clickable($row['post_text']);
1073					$row['post_text'] = str_replace('<BR>', "\n", $row['post_text']);
1074
1075					// make a uid
1076					$uid = make_bbcode_uid();
1077
1078					// do 2.x first-pass encoding..
1079					$row['post_text'] = smiley_replace($row['post_text']);
1080					$row['post_text'] = bbencode_first_pass($row['post_text'], $uid);
1081					$row['post_text'] = addslashes($row['post_text']);
1082
1083					$edited_sql = "";
1084					if ( preg_match('/^(.*?)([\n]+<font size=\-1>\[ This message was .*?)$/s', $row['post_text'], $matches) )
1085					{
1086						$row['post_text'] = $matches[1];
1087						$edit_info = $matches[2];
1088
1089						$edit_times = count(explode(' message ', $edit_info)) - 1; // Taken from example for substr_count in annotated PHP manual
1090
1091						if ( preg_match('/^.* by: (.*?) on (....)-(..)-(..) (..):(..) \]<\/font>/s', $edit_info, $matches) )
1092						{
1093							$edited_user = $matches[1];
1094							$edited_time = gmmktime($matches[5], $matches[6], 0, $matches[3], $matches[4], $matches[2]);
1095
1096							//
1097							// This isn't strictly correct since 2.0 won't include and edit
1098							// statement if the edit wasn't by the user who posted ...
1099							//
1100							$edited_sql = ", post_edit_time = $edited_time, post_edit_count = $edit_times";
1101						}
1102					}
1103	
1104					if ( preg_match("/^(.*?)\n-----------------\n.*$/is", $row['post_text'], $matches) )
1105					{
1106						$row['post_text'] = $matches[1];
1107						$enable_sig = 1;
1108					}
1109					else
1110					{
1111						$checksig = preg_replace('/\[addsig\]$/', '', $row['post_text']);
1112						$enable_sig = ( strlen($checksig) == strlen($row['post_text']) ) ? 0 : 1;
1113					}
1114
1115					$sql = "UPDATE " . POSTS_TEXT_TABLE . " 
1116						SET post_text = '$checksig', bbcode_uid = '$uid'
1117						WHERE post_id = " . $row['post_id'];
1118					query($sql, "Couldn't update " . POSTS_TEXT_TABLE . " table with new BBcode for post_id :: " . $row['post_id']);
1119
1120					$sql = "UPDATE " . POSTS_TABLE . " 
1121						SET enable_sig = $enable_sig" . $edited_sql . " 
1122						WHERE post_id = " . $row['post_id'];
1123					query($sql, "Couldn't update " . POSTS_TABLE . " table with signature status for post with post_id :: " . $row['post_id']);
1124
1125					$inc++;
1126					if ( $inc == $per_pct )
1127					{
1128						print '.';
1129						flush();
1130						$inc = 0;
1131					}
1132				}
1133
1134				print " <span class=\"ok\"><b>OK</b></span><br />\n";
1135
1136				lock_tables(0);
1137			}
1138
1139			print "<br />\n * Updating poster_id for deleted users :: ";
1140			flush();
1141
1142			$sql = "SELECT DISTINCT p.post_id 
1143				FROM " . POSTS_TABLE . " p 
1144				LEFT JOIN " . USERS_TABLE . " u ON p.poster_id = u.user_id 
1145				WHERE u.user_id IS NULL";
1146			$result = query($sql, "Couldn't obtain list of deleted users");
1147			
1148			$users_removed = $db->sql_numrows($result);
1149
1150			if ( $users_removed )
1151			{
1152				$post_id_ary = array();
1153				while( $row = $db->sql_fetchrow($result) )
1154				{
1155					$post_id_ary[] = $row['post_id'];
1156				}
1157
1158				$sql = "UPDATE " . POSTS_TABLE . " 
1159					SET poster_id = " . ANONYMOUS . ", enable_sig = 0 
1160					WHERE post_id IN (" . implode(", ", $post_id_ary) . ")";
1161				query($sql, "Couldn't update posts to remove deleted user poster_id values");
1162			}
1163
1164			print "<span class=\"ok\"><b>OK</b></span> ( Removed $users_removed non-existent user references )<br />\n";
1165
1166			end_step('convert_privmsgs');
1167
1168		case 'convert_privmsgs':
1169			$sql = "SELECT COUNT(*) as total, max(msg_id) as maxid 
1170				FROM " . PRIVMSGS_TABLE;
1171			$result = query($sql, "Couldn't get max privmsgs_id.");
1172
1173			$maxid = $db->sql_fetchrow($result);
1174
1175			$totalposts = $maxid['total'];
1176			$maxid = $maxid['maxid'];
1177
1178			$sql = "ALTER TABLE " . PRIVMSGS_TABLE . " 
1179				ADD privmsgs_subject VARCHAR(255),
1180				ADD privmsgs_attach_sig TINYINT(1) DEFAULT 1";
1181			query($sql, "Couldn't add privmsgs_subject field to " . PRIVMSGS_TABLE . " table");
1182
1183			$batchsize = 2000;
1184			for($i = 0; $i <= $maxid; $i += $batchsize)
1185			{
1186				$batchstart = $i + 1;
1187				$batchend = $i + $batchsize;
1188				
1189				print " * Converting Private Message ( $batchstart to $batchend ) :: ";
1190				flush();
1191
1192				$sql = "SELECT * 
1193					FROM " . PRIVMSGS_TABLE . "
1194					WHERE msg_id 
1195						BETWEEN $batchstart 
1196							AND $batchend";
1197				$result = query($sql, "Couldn't get " . POSTS_TEXT_TABLE . " post_id $batchstart to $batchend");
1198
1199				lock_tables(1, array(PRIVMSGS_TABLE, PRIVMSGS_TEXT_TABLE));
1200
1201				$per_pct = ceil( $db->sql_numrows($result) / 40 );
1202				$inc = 0;
1203
1204				while( $row = $db->sql_fetchrow($result) )
1205				{
1206					if ( $row['msg_text'] == NULL )
1207					{
1208						// We already converted this post to the new style BBcode, skip this post.
1209						continue;
1210					}
1211					//
1212					// Nathan's bbcode2 conversion
1213					//
1214					// undo 1.2.x encoding..
1215					$row['msg_text'] = bbdecode(stripslashes($row['msg_text']));
1216					$row['msg_text'] = undo_make_clickable($row['msg_text']);
1217					$row['msg_text'] = str_replace("<BR>", "\n", $row['msg_text']);
1218
1219					// make a uid
1220					$uid = make_bbcode_uid();
1221
1222					// do 2.x first-pass encoding..
1223					$row['msg_text'] = smiley_replace($row['msg_text']);
1224					$row['msg_text'] = bbencode_first_pass($row['msg_text'], $uid);
1225					
1226					$checksig = preg_replace('/\[addsig\]$/', '', $row['msg_text']);
1227					$enable_sig = (strlen($checksig) == strlen($row['msg_text'])) ? 0 : 1;
1228
1229					if ( preg_match("/^(.*?)\n-----------------\n.*$/is", $checksig, $matches) )
1230					{
1231						$checksig = $matches[1];
1232						$enable_sig = 1;
1233					}
1234
1235					$row['msg_text'] = $checksig;
1236					
1237					$row['msg_status'] = ($row['msg_status'] == 1) ? PRIVMSGS_READ_MAIL : PRIVMSGS_NEW_MAIL;
1238
1239					// Subject contains first 60 characters of msg, remove any BBCode tags
1240					$subject = addslashes(strip_tags(substr($row['msg_text'], 0, 60)));
1241					$subject = preg_replace("/\[.*?\:(([a-z0-9]:)?)$uid.*?\]/si", "", $subject);
1242					
1243					$row['msg_text'] = addslashes($row['msg_text']);
1244
1245					$sql = "INSERT INTO " . PRIVMSGS_TEXT_TABLE . " (privmsgs_text_id, privmsgs_bbcode_uid, privmsgs_text)
1246						VALUES ('" . $row['msg_id'] . "', '$uid', '" . $row['msg_text'] . "')";
1247					query($sql, "Couldn't insert PrivMsg text into " . PRIVMSGS_TEXT_TABLE . " table msg_id " . $row['msg_id']);
1248
1249					$sql = "UPDATE " . PRIVMSGS_TABLE . " 
1250						SET msg_text = NULL, msg_status = " . $row['msg_status'] . ", privmsgs_subject = '$subject', privmsgs_attach_sig = $enable_sig
1251						WHERE msg_id = " . $row['msg_id'];
1252					query($sql, "Couldn't update " . PRIVMSGS_TABLE . " table for msg_id " . $row['post_id']);
1253
1254					$inc++;
1255					if ( $inc == $per_pct )
1256					{
1257						print '.';
1258						flush();
1259						$inc = 0;
1260					}
1261				}
1262
1263				print " <span class=\"ok\"><b>OK</b></span><br />\n";
1264			}
1265
1266			lock_tables(0);
1267			end_step('convert_moderators');
1268
1269		case 'convert_moderators';
1270			$sql = "SELECT * 
1271				FROM forum_mods";
1272			$result = query($sql, "Couldn't get list with all forum moderators");
1273
1274			while( $row = $db->sql_fetchrow($result) )
1275			{
1276				// Check if this moderator and this forum still exist
1277				$sql = "SELECT user_id  
1278					FROM " . USERS_TABLE . ", " . FORUMS_TABLE . " 
1279					WHERE user_id = " . $row['user_id'] . " 
1280						AND forum_id = " . $row['forum_id'];
1281				$check_data = query($sql, "Couldn't check if user " . $row['user_id'] . " and forum " . $row['forum_id'] . " exist");
1282
1283				if ( !($rowtest = $db->sql_fetchrow($check_data)) )
1284				{
1285					// Either the moderator or the forum have been deleted, this line in forum_mods was redundant, skip it.
1286					continue;
1287				}
1288
1289				$sql = "SELECT g.group_id 
1290					FROM " . GROUPS_TABLE . " g, " . USER_GROUP_TABLE . " ug 
1291					WHERE g.group_id = ug.group_id 
1292						AND ug.user_id = " . $row['user_id'] . "
1293						AND g.group_single_user = 1";
1294				$insert_group = query($sql, "Couldn't get group number for user " . $row['user_id'] . ".");
1295
1296				$group_id = $db->sql_fetchrow($insert_group);
1297				$group_id = $group_id['group_id'];
1298
1299				print " * Adding moderator for forum " . $row['forum_id'] . " :: ";
1300				flush();
1301
1302				$sql = "INSERT INTO " . AUTH_ACCESS_TABLE . " (group_id, forum_id, auth_mod) VALUES ($group_id, ".$row['forum_id'].", 1)";
1303				query($sql, "Couldn't set moderator (user_id = " . $row['user_id'] . ") for forum " . $row['forum_id'] . ".");
1304
1305				print "<span class=\"ok\"><b>OK</b></span><br />\n";
1306			}
1307
1308			print " * Setting correct user_level for moderators ::";
1309			flush();
1310
1311			$sql = "SELECT DISTINCT u.user_id 
1312				FROM " . USERS_TABLE . " u, " . USER_GROUP_TABLE . " ug, " . AUTH_ACCESS_TABLE . " aa 
1313				WHERE aa.auth_mod = 1 
1314					AND ug.group_id = aa.group_id 
1315					AND u.user_id = ug.user_id 
1316					AND u.user_level <> " . ADMIN;
1317			$result = query($sql, "Couldn't obtain list of moderators");
1318
1319			if ( $row = $db->sql_fetchrow($result) )
1320			{
1321				$ug_sql = '';
1322
1323				do
1324				{
1325					$ug_sql .= ( ( $ug_sql != '' ) ? ', ' : '' ) . $row['user_id'];
1326				}
1327				while ( $row = $db->sql_fetchrow($result) );
1328
1329				$sql = "UPDATE " . USERS_TABLE . " 
1330					SET user_level = " . MOD . " 
1331					WHERE user_id IN ($ug_sql)";
1332				query($sql, "Couldn't set moderator status for users");
1333			}
1334
1335			print "<span class=\"ok\"><b>OK</b></span><br />\n";
1336			
1337			end_step('convert_privforums');
1338
1339		case 'convert_privforums':
1340			$sql = "SELECT fa.*, f.forum_name 
1341					FROM forum_access fa 
1342					LEFT JOIN " . FORUMS_TABLE . " f ON fa.forum_id = f.forum_id  
1343					ORDER BY fa.forum_id, fa.user_id";
1344			$forum_access = query($sql, "Couldn't get list with special forum access (forum_access)");
1345
1346			$forum_id = -1;
1347			while( $row = $db->sql_fetchrow($forum_access) )
1348			{
1349				// Iterate trough access table
1350				if ( $row['forum_id'] != $forum_id )
1351				{
1352					// This is a new forum, create new group.
1353					$forum_id = $row['forum_id'];
1354
1355					print " * Creating new group for forum $forum_id :: ";
1356					flush();
1357
1358					$sql = "INSERT INTO " . GROUPS_TABLE . " (group_type, group_name, group_description, group_moderator, group_single_user)
1359						VALUES (" . GROUP_HIDDEN . ", '" . addslashes($row['forum_name']) . " Group', 'Converted Private Forum Group', " . $row['user_id'] . ", 0)";
1360					$result = query($sql, "Couldn't create group for ".$row['forum_name']);
1361
1362					$group_id = $db->sql_nextid();
1363
1364					if ( $group_id <= 0 )
1365					{
1366						print "<font color=\"red\">Group creation failed. Aborting creation of groups...<br></font>\n";
1367						continue 2;
1368					}
1369
1370					print "<span class=\"ok\"><b>OK</b></span><br />\n";
1371
1372					print " * Creating auth_access group for forum $forum_id :: ";
1373					flush();
1374
1375					$sql = "INSERT INTO " . AUTH_ACCESS_TABLE . " (group_id, forum_id, auth_view, auth_read, auth_post, auth_reply, auth_edit, auth_delete, auth_sticky, auth_announce, auth_vote, auth_pollcreate)
1376						VALUES ($group_id, $forum_id, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1)";
1377					$result = query($sql, "Unable to set group auth access.");
1378
1379					if ( $db->sql_affectedrows($result) <= 0 )
1380					{
1381						print "<font color=\"red\">Group creation failed. Aborting creation of groups...</font><br>\n";
1382						continue 2;
1383					}
1384
1385					print "<span class=\"ok\"><b>OK</b></span><br />\n";
1386				}
1387
1388				// Add user to the group
1389				$user_id = $row['user_id'];
1390
1391				$sql = "INSERT INTO " . USER_GROUP_TABLE . " (group_id, user_id, user_pending)
1392					VALUES ($group_id, $user_id, 0)";
1393				query($sql, "Unable to add user_id $user_id to group_id $group_id <br>\n");
1394			}
1395
1396			end_step('update_schema');
1397
1398		case 'update_schema':
1399			$rename = array(
1400				$table_prefix . "users" => array(
1401					"user_interests" => "user_intrest",
1402					"user_allowsmile" => "user_desmile",
1403					"user_allowhtml" => "user_html",
1404					"user_allowbbcode" => "user_bbcode", 
1405					"user_style" => "user_theme" 
1406				),
1407				$table_prefix . "privmsgs" => array(
1408					 "privmsgs_id" => "msg_id",
1409					 "privmsgs_from_userid" => "from_userid",
1410					 "privmsgs_to_userid" => "to_userid",
1411					 "privmsgs_date" => "msg_time",
1412					 "privmsgs_ip" => "poster_ip",
1413					 "privmsgs_type" => "msg_status" 
1414				),
1415				$table_prefix . "smilies" => array(
1416					"smilies_id" => "id"
1417				)
1418			);
1419
1420			$schema = get_schema();
1421
1422			$table_def = $schema['table_def'];
1423			$field_def = $schema['field_def'];
1424
1425			// Loop tables in schema
1426			while (list($table, $table_def) = @each($field_def))
1427			{
1428				// Loop fields in table
1429				print " * Updating table '$table' :: ";
1430				flush();
1431				
1432				$sql = "SHOW FIELDS 
1433					FROM $table";
1434				$result = query($sql, "Can't get definition of current $table table");
1435
1436				while( $row = $db->sql_fetchrow($result) )
1437				{
1438					$current_fields[] = $row['Field'];
1439				}
1440				
1441				$alter_sql = "ALTER TABLE $table ";
1442				while (list($field, $definition) = each($table_def))
1443				{
1444					if ( $field == '' )
1445					{
1446						// Skip empty fields if any (shouldn't be needed)
1447						continue;
1448					}
1449
1450					$type = $definition['type'];
1451					$size = $definition['size'];
1452
1453					$default = isset($definition['default']) ? "DEFAULT " . $definition['default'] : '';
1454
1455					$notnull = $definition['notnull'] == 1 ? 'NOT NULL' : '';
1456
1457					$auto_increment = $definition['auto_increment'] == 1 ? 'auto_increment' : '';
1458
1459					$oldfield = isset($rename[$table][$field]) ? $rename[$table][$field] : $field;
1460
1461					if ( !inarray($field, $current_fields) && $oldfield == $field )
1462					{
1463						// If the current is not a key of $current_def and it is not a field that is 
1464						// to be renamed then the field doesn't currently exist.
1465						$changes[] = " ADD $field " . $create_def[$table][$field];
1466					}
1467					else
1468					{
1469						$changes[] = " CHANGE $oldfield $field " . $create_def[$table][$field];
1470					}
1471				}
1472				
1473				$alter_sql .= join(',', $changes);
1474				unset($changes);
1475				unset($current_fields);
1476				
1477				$sql = "SHOW INDEX 
1478					FROM $table";
1479				$result = query($sql, "Couldn't get list of indices for table $table");
1480
1481				unset($indices);
1482
1483				while( $row = $db->sql_fetchrow($result) )
1484				{
1485					$indices[] = $row['Key_name'];
1486				}
1487				
1488				while ( list($key_name, $key_field) = each($key_def[$table]) )
1489				{
1490					if ( !inarray($key_name, $indices) )
1491					{
1492						$alter_sql .= ($key_name == 'PRIMARY') ? ", ADD PRIMARY KEY ($key_field)" : ", ADD INDEX $key_name ($key_field)";
1493					}
1494				}
1495				query($alter_sql, "Couldn't alter table $table");
1496
1497				print "<span class=\"ok\"><b>OK</b></span><br />\n";
1498				flush();
1499			}
1500
1501			end_step('convert_forums');
1502
1503		case 'convert_forums':
1504			$sql = "SELECT * 
1505				FROM " . FORUMS_TABLE;
1506			$result = query($sql, "Couldn't get list with all forums");
1507
1508			while( $row = $db->sql_fetchrow($result) )
1509			{
1510				print " * Converting forum '" . $row['forum_name'] . "' :: ";
1511				flush();
1512
1513				// forum_access: (only concerns posting)
1514				//		1 = Registered users only
1515				//		2 = Anonymous Posting
1516				//		3 = Moderators/Administrators only
1517				switch( $row['forum_access'] )
1518				{
1519					case 1:
1520						// Public forum, no anonymous posting
1521						$auth_view			= AUTH_ALL;
1522						$auth_read			= AUTH_ALL;
1523						$auth_post			= AUTH_REG;
1524						$auth_reply			= AUTH_REG;
1525						$auth_edit			= AUTH_REG;
1526						$auth_delete		= AUTH_REG;
1527						$auth_vote			= AUTH_REG;
1528						$auth_pollcreate	= AUTH_REG;
1529						$auth_sticky		= AUTH_MOD;
1530						$auth_announce		= AUTH_MOD;
1531						break;
1532					case 2:
1533						$auth_post			= AUTH_ALL;
1534						$auth_reply			= AUTH_ALL;
1535						$auth_edit			= AUTH_REG;
1536						$auth_delete		= AUTH_REG;
1537						$auth_vote			= AUTH_ALL;
1538						$auth_pollcreate	= AUTH_ALL;
1539						$auth_sticky		= AUTH_MOD;
1540						$auth_announce		= AUTH_MOD;
1541						break;
1542					default:
1543						$auth_post			= AUTH_MOD;
1544						$auth_reply			= AUTH_MOD;
1545						$auth_edit			= AUTH_MOD;
1546						$auth_delete		= AUTH_MOD;
1547						$auth_vote			= AUTH_MOD;
1548						$auth_pollcreate	= AUTH_MOD;
1549						$auth_sticky		= AUTH_MOD;
1550						$auth_announce		= AUTH_MOD;
1551						break;
1552				}
1553				
1554				// Old auth structure:
1555				// forum_type: (only concerns viewing)
1556				//		0 = Public
1557				//		1 = Private
1558				switch( $row['forum_type'] )
1559				{
1560					case 0:
1561						$auth_view			= AUTH_ALL;
1562						$auth_read			= AUTH_ALL;
1563						break;
1564					default:
1565						//
1566						// Make it really private ... 
1567						//
1568						$auth_view			= AUTH_ACL;
1569						$auth_read			= AUTH_ACL;
1570						$auth_post			= AUTH_ACL;
1571						$auth_reply			= AUTH_ACL;
1572						$auth_edit			= AUTH_ACL;
1573						$auth_delete		= AUTH_ACL;
1574						$auth_vote			= AUTH_ACL;
1575						$auth_pollcreate	= AUTH_ACL;
1576						$auth_sticky		= AUTH_ACL;
1577						$auth_announce		= AUTH_MOD;
1578						break;
1579				}
1580
1581				$sql = "UPDATE " . FORUMS_TABLE . " SET
1582					auth_view = $auth_view,
1583					auth_read = $auth_read,
1584					auth_post = $auth_post,
1585					auth_reply = $auth_reply,
1586					auth_edit = $auth_edit,
1587					auth_delete = $auth_delete,
1588					auth_vote = $auth_vote,
1589					auth_pollcreate = $auth_pollcreate,
1590					auth_sticky = $auth_sticky,
1591					auth_announce = $auth_announce
1592					WHERE forum_id = ". $row['forum_id'];
1593				query($sql, "Was unable to update forum permissions!");
1594
1595				print "<span class=\"ok\"><b>OK</b></span><br />\n";
1596			}
1597
1598			end_step('insert_themes');
1599
1600		case 'insert_themes':
1601			print " * Inserting new values into themes table :: ";
1602
1603			@reset($inserts);
1604			while( list($table, $inserts_table) = each($inserts) )
1605			{
1606				if ( $table == THEMES_TABLE )
1607				{
1608					$per_pct = ceil( count($inserts_table) / 40 );
1609					$inc = 0;
1610
1611					while( list($nr, $insert) = each($inserts_table) )
1612					{
1613						query($insert, "Couldn't insert value into " . THEMES_TABLE);
1614
1615						$inc++;
1616						if ( $inc == $per_pct )
1617						{
1618							print ".";
1619							flush();
1620							$inc = 0;
1621						}
1622					}
1623				}
1624			}
1625
1626			print " <span class=\"ok\"><b>OK</b></span><br />\n";
1627			end_step('update_topics');
1628
1629		case 'update_topics':
1630			$sql = "SELECT MAX(topic_id) AS max_topic 
1631				FROM " . TOPICS_TABLE;
1632			$result = query($sql, "Couldn't get max topic id");
1633
1634			$row = $db->sql_fetchrow($result);
1635
1636			$maxid = $row['max_topic'];
1637
1638			lock_tables(1, array(TOPICS_TABLE, POSTS_TABLE));
1639
1640			$batchsize = 1000;
1641			for($i = 0; $i <= $maxid; $i += $batchsize)
1642			{
1643				$batchstart = $i + 1;
1644				$batchend = $i + $batchsize;
1645				
1646				print " * Setting topic first post_id ( $batchstart to $batchend ) :: ";
1647				flush();
1648
1649				$sql = "SELECT MIN(post_id) AS first_post_id, topic_id
1650					FROM " . POSTS_TABLE . "
1651					WHERE topic_id 
1652						BETWEEN $batchstart 
1653							AND $batchend 
1654					GROUP BY topic_id 
1655					ORDER BY topic_id ASC";
1656				$result = query($sql, "Couldn't get post id data");
1657
1658				$per_pct = ceil( $db->sql_numrows($result) / 40 );
1659				$inc = 0;
1660
1661				if ( $row = $db->sql_fetchrow($result) )
1662				{
1663					do
1664					{
1665						$sql = "UPDATE " . TOPICS_TABLE . " 
1666							SET topic_first_post_id = " . $row['first_post_id'] . " 
1667							WHERE topic_id = " . $row['topic_id'];
1668						query($sql, "Couldn't update topic first post id in topic :: $topic_id");
1669
1670						$inc++;
1671						if ( $inc == $per_pct )
1672						{
1673							print ".";
1674							flush();
1675							$inc = 0;
1676						}
1677					}
1678					while ( $row = $db->sql_fetchrow($result) );
1679				}
1680
1681				print " <span class=\"ok\"><b>OK</b></span><br />\n";
1682			}
1683
1684			lock_tables(0);
1685			end_step('final_configuration');
1686
1687		case 'final_configuration':
1688			//
1689			// Update forum last post information
1690			//
1691			$sql = "SELECT forum_id, forum_name 
1692				FROM " . FORUMS_TABLE;
1693			$f_result = query($sql, "Couldn't obtain forum_ids");
1694
1695			while( $forum_row = $db->sql_fetchrow($f_result) )
1696			{
1697				print " * U…

Large files files are truncated, but you can click here to view the full file