PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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