[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/install/ -> update_to_latest.php (source)

   1  <?php
   2  /***************************************************************************
   3   *                             update_to_xxx.php
   4   *                            -------------------
   5   *   begin                : Wednesday, May 16, 2002
   6   *   copyright            : (C) 2001 The phpBB Group
   7   *   email                : support@phpbb.com
   8   *
   9   *   $Id: update_to_latest.php 8367 2008-02-02 15:05:23Z acydburn $
  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  
  22  function _sql($sql, &$errored, &$error_ary, $echo_dot = true)
  23  {
  24      global $db;
  25  
  26      if (!($result = $db->sql_query($sql)))
  27      {
  28          $errored = true;
  29          $error_ary['sql'][] = (is_array($sql)) ? $sql[$i] : $sql;
  30          $error_ary['error_code'][] = $db->sql_error();
  31      }
  32  
  33      if ($echo_dot)
  34      {
  35          echo ". \n";
  36          flush();
  37      }
  38  
  39      return $result;
  40  }
  41  
  42  @set_time_limit(120);
  43  
  44  define('IN_PHPBB', 1);
  45  $phpbb_root_path = './../';
  46  include ($phpbb_root_path . 'extension.inc');
  47  include($phpbb_root_path . 'config.'.$phpEx);
  48  if(!isset($dbms))
  49  {
  50      die("Please read: <a href='../docs/INSTALL.html'>INSTALL.html</a> before attempting to update.");
  51  }
  52  include($phpbb_root_path . 'includes/constants.'.$phpEx);
  53  include($phpbb_root_path . 'includes/functions.'.$phpEx);
  54  include($phpbb_root_path . 'includes/functions_admin.'.$phpEx);
  55  include($phpbb_root_path . 'includes/functions_search.'.$phpEx);
  56  include($phpbb_root_path . 'includes/db.'.$phpEx);
  57  
  58  
  59  //
  60  //
  61  //
  62  $updates_to_version = '.0.23';
  63  //
  64  //
  65  //
  66  
  67  ?>
  68  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  69  <html>
  70  <head>
  71  <meta http-equiv="Content-Type" content="text/html;">
  72  <meta http-equiv="Content-Style-Type" content="text/css">
  73  <style type="text/css">
  74  <!--
  75  
  76  font,th,td,p,body { font-family: "Courier New", courier; font-size: 11pt }
  77  
  78  a:link,a:active,a:visited { color : #006699; }
  79  a:hover        { text-decoration: underline; color : #DD6900;}
  80  
  81  hr    { height: 0px; border: solid #D1D7DC 0px; border-top-width: 1px;}
  82  
  83  .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;}
  84  
  85  .ok {color:green}
  86  
  87  /* Import the fancy styles for IE only (NS4.x doesn't use the @import function) */
  88  @import url("../templates/subSilver/formIE.css");
  89  -->
  90  </style>
  91  </head>
  92  <body bgcolor="#FFFFFF" text="#000000" link="#006699" vlink="#5584AA">
  93  
  94  <table width="100%" border="0" cellspacing="0" cellpadding="10" align="center">
  95      <tr>
  96          <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  97              <tr>
  98                  <td><img src="../templates/subSilver/images/logo_phpBB.gif" border="0" alt="Forum Home" vspace="1" /></td>
  99                  <td align="center" width="100%" valign="middle"><span class="maintitle">Updating to latest stable release</span></td>
 100              </tr>
 101          </table></td>
 102      </tr>
 103  </table>
 104  
 105  <br clear="all" />
 106  
 107  <h2>Information</h2>
 108  
 109  <?php
 110  
 111  echo '<p>Database type &nbsp; &nbsp;:: <b>' . SQL_LAYER . '</b><br />';
 112  
 113  $sql = "SELECT config_value
 114      FROM " . CONFIG_TABLE . "
 115      WHERE config_name = 'version'";
 116  if (!($result = $db->sql_query($sql)))
 117  {
 118      die("Couldn't obtain version info");
 119  }
 120  
 121  $row = $db->sql_fetchrow($result);
 122  
 123  $sql = array();
 124  
 125  switch ($row['config_value'])
 126  {
 127      case '':
 128          echo 'Previous version :: <b>&lt; RC-3</b></p><br />';
 129          break;
 130      case 'RC-3':
 131          echo 'Previous version :: <b>RC-3</b></p><br />';
 132          break;
 133      case 'RC-4':
 134          echo 'Previous version :: <b>RC-4</b></p><br />';
 135          break;
 136      default:
 137          echo 'Previous version :: <b>2' . $row['config_value'] . '</b><br />';
 138          break;
 139  }
 140  
 141  echo 'Updated version &nbsp;:: <b>2' . $updates_to_version . '</b></p>' ."\n";
 142  
 143  //
 144  // Schema updates
 145  //
 146  switch ($row['config_value'])
 147  {
 148      case '':
 149          switch (SQL_LAYER)
 150          {
 151              case 'mysql':
 152              case 'mysql4':
 153                  $sql[] = "ALTER TABLE " . USERS_TABLE . " DROP
 154                      COLUMN user_autologin_key";
 155  
 156                  $sql[] = "ALTER TABLE " . RANKS_TABLE . " DROP
 157                      COLUMN rank_max";
 158  
 159                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 160                      ADD COLUMN user_session_time int(11) DEFAULT '0' NOT NULL,
 161                      ADD COLUMN user_session_page smallint(5) DEFAULT '0' NOT NULL,
 162                      ADD INDEX (user_session_time)";
 163                  $sql[] = "ALTER TABLE " . SEARCH_TABLE . "
 164                      MODIFY search_id int(11) NOT NULL";
 165  
 166                  $sql[] = "ALTER TABLE " . TOPICS_TABLE . "
 167                      MODIFY topic_moved_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
 168                      ADD COLUMN topic_first_post_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
 169                      ADD INDEX (topic_first_post_id)";
 170  
 171                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 172                      ADD COLUMN tr_class1_name varchar(50) NULL,
 173                      ADD COLUMN tr_class2_name varchar(50) NULL,
 174                      ADD COLUMN tr_class3_name varchar(50) NULL,
 175                      ADD COLUMN th_class1_name varchar(50) NULL,
 176                      ADD COLUMN th_class2_name varchar(50) NULL,
 177                      ADD COLUMN th_class3_name varchar(50) NULL,
 178                      ADD COLUMN td_class1_name varchar(50) NULL,
 179                      ADD COLUMN td_class2_name varchar(50) NULL,
 180                      ADD COLUMN td_class3_name varchar(50) NULL,
 181                      ADD COLUMN span_class1_name varchar(50) NULL,
 182                      ADD COLUMN span_class2_name varchar(50) NULL,
 183                      ADD COLUMN span_class3_name varchar(50) NULL";
 184                  break;
 185              case 'postgresql':
 186                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 187                      ADD COLUMN user_session_time int4";
 188                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 189                      ADD COLUMN user_session_page int2";
 190                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 191                      ALTER COLUMN user_session_time SET DEFAULT '0'";
 192                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 193                      ALTER COLUMN user_session_page SET DEFAULT '0'";
 194                  $sql[] = "CREATE INDEX user_session_time_" . $table_prefix . "users_index
 195                      ON " . USERS_TABLE . " (user_session_time)";
 196  
 197                  $sql[] = "ALTER TABLE " . TOPICS_TABLE . "
 198                      ADD COLUMN topic_first_post_id int4";
 199                  $sql[] = "CREATE INDEX topic_first_post_id_" . $table_prefix . "topics_index
 200                      ON " . TOPICS_TABLE . " (topic_first_post_id)";
 201  
 202                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 203                      ADD COLUMN tr_class1_name varchar(50) NULL";
 204                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 205                      ADD COLUMN tr_class2_name varchar(50) NULL";
 206                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 207                      ADD COLUMN tr_class3_name varchar(50) NULL";
 208                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 209                      ADD COLUMN th_class1_name varchar(50) NULL";
 210                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 211                      ADD COLUMN th_class2_name varchar(50) NULL";
 212                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 213                      ADD COLUMN th_class3_name varchar(50) NULL";
 214                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 215                      ADD COLUMN td_class1_name varchar(50) NULL";
 216                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 217                      ADD COLUMN td_class2_name varchar(50) NULL";
 218                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 219                      ADD COLUMN td_class3_name varchar(50) NULL";
 220                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 221                      ADD COLUMN span_class1_name varchar(50) NULL";
 222                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 223                      ADD COLUMN span_class2_name varchar(50) NULL";
 224                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . "
 225                      ADD COLUMN span_class3_name varchar(50) NULL";
 226                  break;
 227  
 228              case 'mssql-odbc':
 229              case 'mssql':
 230                  $sql[] = "ALTER TABLE " . USERS_TABLE . " DROP
 231                      COLUMN user_autologin_key";
 232  
 233                  $sql[] = "ALTER TABLE " . USERS_TABLE . " ADD
 234                      user_session_time int NOT NULL,
 235                      user_session_page smallint NOT NULL,
 236                      CONSTRAINT [DF_" . $table_prefix . "users_user_session_time] DEFAULT (0) FOR [user_session_time],
 237                      CONSTRAINT [DF_" . $table_prefix . "users_user_session_page] DEFAULT (0) FOR [user_session_page]";
 238                  $sql[] = "CREATE INDEX [IX_" . $table_prefix . "users]
 239                      ON [" . USERS_TABLE . "]([user_session_time]) ON [PRIMARY]";
 240  
 241                  /* ---------------------------------------------------------------------
 242                      DROP FORUM TABLE -- if this may cause you problems you can safely
 243                      comment it out, remember to manually remove the IDENTITY setting on
 244                      the forum_id column
 245                     --------------------------------------------------------------------- */
 246                  $sql [] = "ALTER TABLE " . FORUMS_TABLE . " DROP
 247                      CONSTRAINT [DF_" . $table_prefix . "forums_forum_posts],
 248                      CONSTRAINT [DF_" . $table_prefix . "forums_forum_topics],
 249                      CONSTRAINT [DF_" . $table_prefix . "forums_forum_last_post_id],
 250                      CONSTRAINT [DF_" . $table_prefix . "forums_prune_enable],
 251                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_view],
 252                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_read],
 253                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_post],
 254                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_reply],
 255                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_edit],
 256                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_delete],
 257                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_sticky],
 258                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_announce],
 259                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_vote],
 260                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_pollcreate],
 261                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_attachments]";
 262                  $sql[] = "CREATE TABLE Tmp_" . FORUMS_TABLE . "
 263                      (forum_id int NOT NULL, cat_id int NOT NULL, forum_name varchar(100) NOT NULL, forum_desc varchar(255) NULL, forum_status smallint NOT NULL, forum_order int NOT NULL, forum_posts int NOT NULL, forum_topics smallint NOT NULL, forum_last_post_id int NOT NULL, prune_next int NULL, prune_enable smallint NOT NULL, auth_view smallint NOT NULL, auth_read smallint NOT NULL, auth_post smallint NOT NULL, auth_reply smallint NOT NULL, auth_edit smallint NOT NULL, auth_delete smallint NOT NULL,    auth_sticky smallint NOT NULL, auth_announce smallint NOT NULL, auth_vote smallint NOT NULL, auth_pollcreate smallint NOT NULL, auth_attachments smallint NOT NULL) ON [PRIMARY]";
 264                  $sql[] = "ALTER TABLE [Tmp_" . FORUMS_TABLE . "] WITH NOCHECK ADD
 265                      CONSTRAINT [DF_" . $table_prefix . "forums_forum_posts] DEFAULT (0) FOR [forum_posts],
 266                      CONSTRAINT [DF_" . $table_prefix . "forums_forum_topics] DEFAULT (0) FOR [forum_topics],
 267                      CONSTRAINT [DF_" . $table_prefix . "forums_forum_last_post_id] DEFAULT (0) FOR [forum_last_post_id],
 268                      CONSTRAINT [DF_" . $table_prefix . "forums_prune_enable] DEFAULT (0) FOR [prune_enable],
 269                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_view] DEFAULT (0) FOR [auth_view],
 270                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_read] DEFAULT (0) FOR [auth_read],
 271                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_post] DEFAULT (0) FOR [auth_post],
 272                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_reply] DEFAULT (0) FOR [auth_reply],
 273                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_edit] DEFAULT (0) FOR [auth_edit],
 274                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_delete] DEFAULT (0) FOR [auth_delete],
 275                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_sticky] DEFAULT (0) FOR [auth_sticky],
 276                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_announce] DEFAULT (0) FOR [auth_announce],
 277                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_vote] DEFAULT (0) FOR [auth_vote],
 278                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_pollcreate] DEFAULT (0) FOR [auth_pollcreate],
 279                      CONSTRAINT [DF_" . $table_prefix . "forums_auth_attachments] DEFAULT (0) FOR [auth_attachments]";
 280                  $sql[] = "INSERT INTO Tmp_" . FORUMS_TABLE . " (forum_id, cat_id, forum_name, forum_desc, forum_status, forum_order, forum_posts, forum_topics, forum_last_post_id, prune_next, prune_enable, auth_view, auth_read, auth_post, auth_reply, auth_edit, auth_delete, auth_sticky, auth_announce, auth_vote, auth_pollcreate, auth_attachments)
 281                          SELECT forum_id, cat_id, forum_name, forum_desc, forum_status, forum_order, forum_posts, forum_topics, forum_last_post_id, prune_next, prune_enable, auth_view, auth_read, auth_post, auth_reply, auth_edit, auth_delete, auth_sticky, auth_announce, auth_vote, auth_pollcreate, auth_attachments FROM " . FORUMS_TABLE . " TABLOCKX";
 282                  $sql[] = "DROP TABLE " . FORUMS_TABLE;
 283                  $sql[] = "EXECUTE sp_rename N'Tmp_" . FORUMS_TABLE . "', N'" . FORUMS_TABLE . "', 'OBJECT'";
 284                  $sql[] = "ALTER TABLE " . FORUMS_TABLE . " ADD
 285                      CONSTRAINT [PK_" . $table_prefix . "forums] PRIMARY KEY CLUSTERED (forum_id) ON [PRIMARY]";
 286                  $sql[] = "CREATE NONCLUSTERED INDEX [IX_" . $table_prefix . "forums]
 287                      ON " . FORUMS_TABLE . " (cat_id, forum_order, forum_last_post_id) ON [PRIMARY]";
 288                  /* --------------------------------------------------------------
 289                      END OF DROP FORUM -- don't remove anything after this point!
 290                     -------------------------------------------------------------- */
 291  
 292                  $sql[] = "DROP INDEX " . RANKS_TABLE . ".IX_" . $table_prefix . "ranks";
 293                  $sql[] = "ALTER TABLE " . RANKS_TABLE . " DROP
 294                      COLUMN rank_max";
 295                  $sql[] = "CREATE  INDEX [IX_" . $table_prefix . "ranks]
 296                      ON [" . RANKS_TABLE . "]([rank_min], [rank_special]) ON [PRIMARY]";
 297  
 298                  $sql[] = "DROP INDEX " . TOPICS_TABLE . ".IX_" . $table_prefix . "topics";
 299                  $sql[] = "ALTER TABLE " . TOPICS_TABLE . " ADD
 300                      topic_first_post_id int NULL,
 301                      CONSTRAINT [DF_" . $table_prefix . "topics_topic_first_post_id] FOR [topic_first_post_id]";
 302                  $sql[] = "CREATE  INDEX [IX_" . $table_prefix . "topics]
 303                      ON [" . TOPICS_TABLE . "]([forum_id], [topic_type], [topic_first_post_id], [topic_last_post_id]) ON [PRIMARY]";
 304  
 305                  $sql[] = "ALTER TABLE " . SEARCH_WORD_TABLE . " DROP
 306                      CONSTRAINT [PK_" . $table_prefix . "search_wordlist]";
 307                  $sql[] = "CREATE UNIQUE INDEX [IX_" . $table_prefix . "search_wordlist]
 308                      ON [" . SEARCH_WORD_TABLE . "]([word_text]) WITH IGNORE_DUP_KEY ON [PRIMARY]";
 309                  $sql[] = "CREATE  INDEX [IX_" . $table_prefix . "search_wordlist_1]
 310                      ON [" . SEARCH_WORD_TABLE . "]([word_common]) ON [PRIMARY]";
 311  
 312                  $sql[] = "CREATE INDEX [IX_" . $table_prefix . "search_wordmatch_1]
 313                      ON [" . SEARCH_MATCH_TABLE . "]([word_id]) ON [PRIMARY]";
 314  
 315                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . " ADD
 316                      tr_class1_name varchar(50) NULL,
 317                      tr_class2_name varchar(50) NULL,
 318                      tr_class3_name varchar(50) NULL,
 319                      th_class1_name varchar(50) NULL,
 320                      th_class2_name varchar(50) NULL,
 321                      th_class3_name varchar(50) NULL,
 322                      td_class1_name varchar(50) NULL,
 323                      td_class2_name varchar(50) NULL,
 324                      td_class3_name varchar(50) NULL,
 325                      span_class1_name varchar(50) NULL,
 326                      span_class2_name varchar(50) NULL,
 327                      span_class3_name varchar(50) NULL";
 328                  break;
 329  
 330              case 'msaccess':
 331                  $sql[] = "ALTER TABLE " . USERS_TABLE . " DROP
 332                      COLUMN user_autologin_key";
 333  
 334                  $sql[] = "ALTER TABLE " . USERS_TABLE . " ADD
 335                      user_session_time int NOT NULL,
 336                      user_session_page smallint NOT NULL";
 337                  $sql[] = "CREATE INDEX user_session_time
 338                      ON " . USERS_TABLE . " (user_session_time)";
 339  
 340                  $sql[] = "ALTER TABLE " . TOPICS_TABLE . " ADD
 341                      topic_first_post_id int NULL";
 342                  $sql[] = "CREATE INDEX topic_first_post_id
 343                      ON " . TOPICS_TABLE . " (topic_first_post_id)";
 344  
 345                  $sql[] = "ALTER TABLE " . THEMES_NAME_TABLE . " ADD
 346                      tr_class1_name varchar(50) NULL,
 347                      tr_class2_name varchar(50) NULL,
 348                      tr_class3_name varchar(50) NULL,
 349                      th_class1_name varchar(50) NULL,
 350                      th_class2_name varchar(50) NULL,
 351                      th_class3_name varchar(50) NULL,
 352                      td_class1_name varchar(50) NULL,
 353                      td_class2_name varchar(50) NULL,
 354                      td_class3_name varchar(50) NULL,
 355                      span_class1_name varchar(50) NULL,
 356                      span_class2_name varchar(50) NULL,
 357                      span_class3_name varchar(50) NULL";
 358                  break;
 359  
 360              default:
 361                  die("No DB LAYER found!");
 362                  break;
 363          }
 364  
 365      case 'RC-3':
 366      case 'RC-4':
 367      case '.0.0':
 368          switch (SQL_LAYER)
 369          {
 370              case 'mysql':
 371              case 'mysql4':
 372                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 373                      MODIFY COLUMN user_id  mediumint(8) NOT NULL,
 374                      MODIFY COLUMN user_timezone decimal(5,2) DEFAULT '0' NOT NULL";
 375                  break;
 376              case 'postgresql':
 377                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 378                      RENAME COLUMN user_timezone TO user_timezone_old";
 379                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 380                      ADD COLUMN user_timezone decimal(5)";
 381                  break;
 382              case 'mssql':
 383              case 'mssql-odbc':
 384                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 385                      ALTER COLUMN [user_timezone] [decimal] (5,2) NOT NULL";
 386                  break;
 387          }
 388  
 389      case '.0.1':
 390          switch (SQL_LAYER)
 391          {
 392              case 'mysql':
 393              case 'mysql4':
 394                  $sql[] = "ALTER TABLE " . GROUPS_TABLE . "
 395                      MODIFY COLUMN group_id mediumint(8) NOT NULL auto_increment";
 396                  break;
 397              case 'mssql':
 398              case 'mssql-odbc':
 399                  /* ---------------------------------------------------------------------
 400                      DROP GROUP TABLE -- if this may cause you problems you can safely
 401                      comment it out, remember to manually add the IDENTITY setting on
 402                      the group_id column
 403                     --------------------------------------------------------------------- */
 404                  $sql[] = "CREATE TABLE Tmp_" . GROUPS_TABLE . "
 405                      (group_id int IDENTITY (1, 1) NOT NULL, group_type smallint NULL, group_name varchar(50) NOT NULL, group_description varchar(255) NOT NULL, group_moderator int NULL, group_single_user smallint NOT NULL) ON [PRIMARY]";
 406                  $sql[] = "SET IDENTITY_INSERT " . GROUPS_TABLE . " ON";
 407                  $sql[] = "INSERT INTO Tmp_" . GROUPS_TABLE . " (group_id, group_type, group_name, group_description, group_moderator, group_single_user)
 408                      SELECT group_id, group_type, group_name, group_description, group_moderator, group_single_user FROM " . GROUPS_TABLE . " TABLOCKX";
 409                  $sql[] = "SET IDENTITY_INSERT " . GROUPS_TABLE . " OFF";
 410                  $sql[] = "DROP TABLE " . GROUPS_TABLE;
 411                  $sql[] = "EXECUTE sp_rename N'Tmp_" . GROUPS_TABLE . "', N'" . GROUPS_TABLE . "', 'OBJECT'";
 412                  $sql[] = "ALTER TABLE " . GROUPS_TABLE . " ADD
 413                      CONSTRAINT [PK_" . $table_prefix . "groups] PRIMARY KEY CLUSTERED (group_id) ON [PRIMARY]";
 414                  $sql[] = "CREATE INDEX [IX_" . $table_prefix . "groups]
 415                      ON " . GROUPS_TABLE . " (group_single_user) ON [PRIMARY]";
 416                  /* --------------------------------------------------------------
 417                      END OF DROP GROUP -- don't remove anything after this point!
 418                     -------------------------------------------------------------- */
 419                  break;
 420              
 421          }
 422  
 423      case '.0.2':
 424  
 425      case '.0.3':
 426  
 427          switch (SQL_LAYER)
 428          {
 429              case 'mysql':
 430              case 'mysql4':
 431                  // Add indexes to post_id in search match table (+ word_id for MS Access)
 432                  $sql[] = "ALTER TABLE " . SEARCH_MATCH_TABLE . " 
 433                      ADD INDEX post_id (post_id)";
 434  
 435                  // Modify user_timezone to decimal(5,2) for mysql ... mysql4/mssql/pgsql/msaccess
 436                  // should be completely unaffected
 437                  // Change default user_notify to 0 
 438                  $sql[] = "ALTER TABLE " . USERS_TABLE . " 
 439                      MODIFY COLUMN user_timezone decimal(5,2) DEFAULT '0' NOT NULL, 
 440                      MODIFY COLUMN user_notify tinyint(1) DEFAULT '0' NOT NULL";
 441  
 442                  // Adjust field type for prune_days, prune_freq ... was too small
 443                  $sql[] = "ALTER TABLE " . PRUNE_TABLE . " 
 444                      MODIFY COLUMN prune_days smallint(5) UNSIGNED NOT NULL, 
 445                      MODIFY COLUMN prune_freq smallint(5) UNSIGNED NOT NULL";
 446                  break;
 447  
 448              case 'msaccess':
 449                  // Add indexes to post_id in search match table (+ word_id for MS Access)
 450                  $sql[] = "CREATE INDEX " . SEARCH_MATCH_TABLE . " 
 451                      ON " . SEARCH_MATCH_TABLE . " ([post_id])";
 452                  $sql[] = "CREATE INDEX " . SEARCH_MATCH_TABLE . "_1 
 453                      ON " . SEARCH_MATCH_TABLE . " ([word_id])";
 454                  break;
 455  
 456              case 'postgresql':
 457                  // Add indexes to post_id in search match table (+ word_id for MS Access)
 458                  $sql[] = "CREATE INDEX post_id_" . SEARCH_MATCH_TABLE . " 
 459                      ON " . SEARCH_MATCH_TABLE . " (post_id)";
 460  
 461                  // Regenerate groups table with incremented group_id for pgsql 
 462                  // ... missing in 2.0.3 ...
 463                  $sql[] = "CREATE SEQUENCE " . GROUPS_TABLE . "_id_seq start 3 increment 1 maxvalue 2147483647 minvalue 1 cache 1";
 464                  $sql[] = "CREATE TABLE tmp_" . GROUPS_TABLE . " 
 465                      AS SELECT group_id, group_name, group_type, group_description, group_moderator, group_single_user 
 466                          FROM " . GROUPS_TABLE;
 467                  $sql[] = "DROP TABLE " . GROUPS_TABLE;
 468                  $sql[] = "CREATE TABLE {$table_prefix}groups (group_id int DEFAULT nextval('" . GROUPS_TABLE . "_id_seq'::text) NOT NULL, group_name varchar(40) NOT NULL, group_type int2 DEFAULT '1' NOT NULL, group_description varchar(255) NOT NULL, group_moderator int4 DEFAULT '0' NOT NULL, group_single_user int2 DEFAULT '0' NOT NULL, CONSTRAINT {$table_prefix}groups_pkey PRIMARY KEY (group_id))";
 469                  $sql[] = "INSERT INTO " . GROUPS_TABLE . " (group_id, group_name, group_type, group_description, group_moderator, group_single_user) 
 470                      SELECT group_id, group_name, group_type, group_description, group_moderator, group_single_user 
 471                          FROM tmp_" . GROUPS_TABLE;
 472                  $sql[] = "DROP TABLE tmp_" . GROUPS_TABLE;
 473                  break;
 474          }
 475  
 476      case '.0.4':
 477  
 478          switch (SQL_LAYER)
 479          {
 480              case 'mssql':
 481              case 'mssql-odbc':
 482                  // Add missing defaults to MSSQL post table schema, failed in previous updates
 483                  $sql[] = "ALTER TABLE [" . POSTS_TABLE . "] WITH NOCHECK ADD
 484                      CONSTRAINT [DF_" . POSTS_TABLE . "_enable_bbcode] DEFAULT (1) FOR [enable_bbcode],
 485                      CONSTRAINT [DF_" . POSTS_TABLE . "_enable_html] DEFAULT (0) FOR [enable_html],
 486                      CONSTRAINT [DF_" . POSTS_TABLE . "_enable_smilies] DEFAULT (1) FOR [enable_smilies],
 487                      CONSTRAINT [DF_" . POSTS_TABLE . "_enable_sig] DEFAULT (1) FOR [enable_sig],
 488                      CONSTRAINT [DF_" . POSTS_TABLE . "_post_edit_count] DEFAULT (0) FOR [post_edit_count]";
 489                  break;
 490          }
 491  
 492          // Add tables for visual confirmation ... saves me the trouble of writing a seperate
 493          // script :D
 494          switch (SQL_LAYER)
 495          {
 496              case 'mysql':
 497              case 'mysql4':
 498                  $sql[] = 'CREATE TABLE ' . $table_prefix . 'confirm (confirm_id char(32) DEFAULT \'\' NOT NULL, session_id char(32) DEFAULT \'\' NOT NULL, code char(6) DEFAULT \'\' NOT NULL, PRIMARY KEY (session_id, confirm_id))';
 499                  break;
 500  
 501              case 'mssql':
 502              case 'mssql-odbc':
 503                  $sql[] = 'CREATE TABLE [' . $table_prefix . 'confirm] ([confirm_id] [char] (32) NOT NULL , [session_id] [char] (32) NOT NULL , [code] [char] (6) NOT NULL ) ON [PRIMARY]';
 504                  $sql[] = 'ALTER TABLE [' . $table_prefix . 'confirm] WITH NOCHECK ADD CONSTRAINT [PK_' . $table_prefix . 'confirm] PRIMARY KEY  CLUSTERED ( [session_id],[confirm_id])  ON [PRIMARY]';
 505                  $sql[] = 'ALTER TABLE [' . $table_prefix . 'confirm] WITH NOCHECK ADD CONSTRAINT [DF_' . $table_prefix . 'confirm_confirm_id] DEFAULT (\'\') FOR [confirm_id], CONSTRAINT [DF_' . $table_prefix . 'confirm_session_id] DEFAULT (\'\') FOR [session_id], CONSTRAINT [DF_' . $table_prefix . 'confirm_code] DEFAULT (\'\') FOR [code]';
 506                  break;
 507  
 508              case 'msaccess':
 509                  $sql[] = 'CREATE TABLE ' . $table_prefix . 'confirm (confirm_id char(32) NOT NULL, session_id char(32) NOT NULL, code char(6) NOT NULL)';
 510                  $sql[] = 'ALTER TABLE ' . $table_prefix . 'confirm ADD (PRIMARY KEY (session_id, confirm_id))';
 511                  break;
 512  
 513              case 'postgresql':
 514                  $sql[] = 'CREATE TABLE ' . $table_prefix . 'confirm (confirm_id char(32) DEFAULT \'\' NOT NULL,  session_id char(32) DEFAULT \'\' NOT NULL, code char(6) DEFAULT \'\' NOT NULL, CONSTRAINT {$table_prefix}confirm_pkey PRIMARY KEY (session_id, confirm_id))';
 515                  break;
 516          }
 517  
 518      case '.0.5':
 519      case '.0.6':
 520      case '.0.7':
 521      case '.0.8':
 522      case '.0.9':
 523      case '.0.10':
 524      case '.0.11':
 525      case '.0.12':
 526      case '.0.13':
 527      case '.0.14':
 528  
 529          switch (SQL_LAYER)
 530          {
 531              case 'mysql':
 532              case 'mysql4':
 533                  $sql[] = "ALTER TABLE " . SESSIONS_TABLE . "
 534                      ADD COLUMN session_admin tinyint(2) DEFAULT '0' NOT NULL";
 535                  break;
 536  
 537              case 'postgresql':
 538                  $sql[] = "ALTER TABLE " . SESSIONS_TABLE . "
 539                      ADD COLUMN session_admin int2";
 540                  $sql[] = "ALTER TABLE " . SESSIONS_TABLE . "
 541                      ALTER COLUMN session_admin SET DEFAULT '0'";
 542                  break;
 543  
 544              case 'mssql-odbc':
 545              case 'mssql':
 546                  $sql[] = "ALTER TABLE " . SESSIONS_TABLE . " ADD
 547                      session_admin smallint NOT NULL,
 548                      CONSTRAINT [DF_" . $table_prefix . "sessions_session_admin] DEFAULT (0) FOR [session_admin]";
 549                  break;
 550  
 551              case 'msaccess':
 552                  $sql[] = "ALTER TABLE " . SESSIONS_TABLE . " ADD
 553                      session_admin smallint NOT NULL";
 554                  break;
 555          }
 556  
 557      case '.0.15':
 558      case '.0.16':
 559      case '.0.17':
 560          // Add tables for session keys
 561          switch (SQL_LAYER)
 562          {
 563              case 'mysql':
 564              case 'mysql4':
 565                  $sql[] = 'CREATE TABLE ' . $table_prefix . 'sessions_keys (key_id varchar(32) DEFAULT \'0\' NOT NULL, user_id mediumint(8) DEFAULT \'0\' NOT NULL, last_ip varchar(8) DEFAULT \'0\' NOT NULL, last_login int(11) DEFAULT \'0\' NOT NULL, PRIMARY KEY (key_id, user_id), KEY last_login (last_login))';
 566                  break;
 567  
 568              case 'mssql':
 569              case 'mssql-odbc':
 570                  $sql[] = 'CREATE TABLE [' . $table_prefix . 'sessions_keys] ([key_id] [char] (32) NOT NULL , [user_id] [int] NOT NULL , [last_ip] [char] (8) NOT NULL , [last_login] [int] NOT NULL) ON [PRIMARY]';
 571  
 572                  $sql[] = 'CREATE INDEX [IX_' . $table_prefix . 'sessions_keys] ON [' . $table_prefix . 'sessions_keys]([key_id], [user_id]) ON [PRIMARY]';
 573                  $sql[] = 'CREATE  INDEX [IX_' . $table_prefix . 'sessions_keys_1] ON [' . $table_prefix . 'sessions_keys]([last_login]) ON [PRIMARY]';
 574                  break;
 575  
 576              case 'msaccess':
 577                  $sql[] = 'CREATE TABLE ' . $table_prefix . 'sessions_keys (key_id char(32) NOT NULL, user_id int NOT NULL, last_ip char(8) NOT NULL, last_login int NOT NULL)';
 578                  $sql[] = 'ALTER TABLE ' . $table_prefix . 'sessions_keys ADD PRIMARY KEY (key_id, user_id)';
 579                  break;
 580  
 581              case 'postgresql':
 582                  $sql[] = 'CREATE TABLE ' . $table_prefix . 'sessions_keys (key_id char(32) DEFAULT \'0\' NOT NULL, user_id int4 DEFAULT \'0\' NOT NULL, last_ip char(8) DEFAULT \'0\' NOT NULL, last_login int4 DEFAULT \'0\' NOT NULL, CONSTRAINT ' . $table_prefix . 'sessions_keys_pkey PRIMARY KEY (key_id, user_id))';
 583                  $sql[] = 'CREATE INDEX last_login_' . $table_prefix . 'sessions_keys_index ON ' . $table_prefix . 'sessions_keys (last_login)';
 584                  break;
 585          }
 586  
 587      case '.0.18':
 588  
 589          // Add login columns to user table
 590          switch (SQL_LAYER)
 591          {
 592              case 'mysql':
 593              case 'mysql4':
 594                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 595                      ADD COLUMN user_login_tries smallint(5) UNSIGNED DEFAULT '0' NOT NULL";
 596                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 597                      ADD COLUMN user_last_login_try int(11) DEFAULT '0' NOT NULL";
 598                  break;
 599  
 600              case 'postgresql':
 601                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 602                      ADD COLUMN user_login_tries int2";
 603                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 604                      ALTER COLUMN user_login_tries SET DEFAULT '0'";
 605                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 606                      ADD COLUMN user_last_login_try int4";
 607                  $sql[] = "ALTER TABLE " . USERS_TABLE . "
 608                      ALTER COLUMN user_last_login_try SET DEFAULT '0'";
 609                  break;
 610  
 611              case 'mssql-odbc':
 612              case 'mssql':
 613                  $sql[] = "ALTER TABLE " . USERS_TABLE . " ADD
 614                      user_login_tries smallint NOT NULL,
 615                      CONSTRAINT [DF_" . $table_prefix . "users_user_login_tries] DEFAULT (0) FOR [user_login_tries]";
 616                  $sql[] = "ALTER TABLE " . USERS_TABLE . " ADD
 617                      user_last_login_try int NOT NULL,
 618                      CONSTRAINT [DF_" . $table_prefix . "users_user_last_login_try] DEFAULT (0) FOR [user_last_login_try]";
 619                  break;
 620  
 621              case 'msaccess':
 622                  $sql[] = "ALTER TABLE " . USERS_TABLE . " ADD
 623                      user_login_tries smallint NOT NULL";
 624                  $sql[] = "ALTER TABLE " . USERS_TABLE . " ADD
 625                      user_last_login_try int NOT NULL";
 626                  break;
 627          }
 628  
 629      case '.0.19':
 630  
 631          // Add search time to the search table
 632          switch (SQL_LAYER)
 633          {
 634              case 'mysql':
 635              case 'mysql4':
 636                  $sql[] = "ALTER TABLE " . SEARCH_TABLE . "
 637                      ADD COLUMN search_time int(11) DEFAULT '0' NOT NULL";
 638                  break;
 639  
 640              case 'postgresql':
 641                  $sql[] = "ALTER TABLE " . SEARCH_TABLE . "
 642                      ADD COLUMN search_time int4";
 643                  $sql[] = "ALTER TABLE " . SEARCH_TABLE . "
 644                      ALTER COLUMN search_time SET DEFAULT '0'";
 645                  break;
 646  
 647              case 'mssql-odbc':
 648              case 'mssql':
 649                  $sql[] = "ALTER TABLE " . SEARCH_TABLE . " ADD
 650                      search_time int NOT NULL,
 651                      CONSTRAINT [DF_" . $table_prefix . "search_results_search_time] DEFAULT (0) FOR [search_time]";
 652                  break;
 653  
 654              case 'msaccess':
 655                  $sql[] = "ALTER TABLE " . SEARCH_TABLE . " ADD
 656                      search_time int NOT NULL";
 657                  break;
 658          }
 659  
 660          break;
 661  
 662      case '.0.21':
 663  
 664          // MySQL only change
 665          switch (SQL_LAYER)
 666          {
 667              case 'mysql':
 668              case 'mysql4':
 669                  $sql[] = 'ALTER TABLE ' . SEARCH_TABLE . '
 670                      MODIFY COLUMN search_array MEDIUMTEXT NOT NULL';
 671          }
 672  
 673          break;
 674  }
 675  
 676  echo "<h2>Updating database schema</h2>\n";
 677  echo "<p>Progress :: <b>";
 678  flush();
 679  
 680  $error_ary = array();
 681  $errored = false;
 682  if (count($sql))
 683  {
 684      for ($i = 0; $i < count($sql); $i++)
 685      {
 686          _sql($sql[$i], $errored, $error_ary);
 687      }
 688  
 689      echo "</b> <b class=\"ok\">Done</b><br />Result &nbsp; :: \n";
 690  
 691      if ($errored)
 692      {
 693          echo " <b>Some queries failed, the statements and errors are listing below</b>\n<ul>";
 694  
 695          for ($i = 0; $i < count($error_ary['sql']); $i++)
 696          {
 697              echo "<li>Error :: <b>" . $error_ary['error_code'][$i]['message'] . "</b><br />";
 698              echo "SQL &nbsp; :: <b>" . $error_ary['sql'][$i] . "</b><br /><br /></li>";
 699          }
 700  
 701          echo "</ul>\n<p>This is probably nothing to worry about, update will continue. Should this fail to complete you may need to seek help at our development board. See <a href=\"docs\README.html\">README</a> for details on how to obtain advice.</p>\n";
 702      }
 703      else
 704      {
 705          echo "<b>No errors</b>\n";
 706      }
 707  }
 708  else
 709  {
 710      echo " No updates required</b></p>\n";
 711  }
 712  
 713  //
 714  // Data updates
 715  //
 716  unset($sql);
 717  $error_ary = array();
 718  $errored = false;
 719  
 720  echo "<h2>Updating data</h2>\n";
 721  echo "<p>Progress :: <b>";
 722  flush();
 723  
 724  switch ($row['config_value'])
 725  {
 726      case '':
 727          $sql = "SELECT themes_id
 728              FROM " . THEMES_TABLE . "
 729              WHERE template_name = 'subSilver'";
 730          $result = _sql($sql, $errored, $error_ary);
 731  
 732          if ($row = $db->sql_fetchrow($result))
 733          {
 734              $theme_id = $row['themes_id'];
 735  
 736              $sql = "UPDATE " . THEMES_TABLE . "
 737                  SET head_stylesheet = 'subSilver.css', body_background = '', body_bgcolor = 'E5E5E5', body_text = '000000', body_link = '006699', body_vlink = '5493B4', body_alink = '', body_hlink = 'DD6900', tr_color1 = 'EFEFEF', tr_color2 = 'DEE3E7', tr_color3 = 'D1D7DC', tr_class1 = '', tr_class2 = '', tr_class3 = '', th_color1 = '98AAB1', th_color2 = '006699', th_color3 = 'FFFFFF', th_class1 = 'cellpic1.gif', th_class2 = 'cellpic3.gif', th_class3 = 'cellpic2.jpg', td_color1 = 'FAFAFA', td_color2 = 'FFFFFF', td_color3 = '', td_class1 = 'row1', td_class2 = 'row2', td_class3 = '', fontface1 = 'Verdana, Arial, Helvetica, sans-serif', fontface2 = 'Trebuchet MS', fontface3 = 'Courier, ''Courier New'', sans-serif', fontsize1 = 10, fontsize2 = 11, fontsize3 = 12, fontcolor1 = '444444', fontcolor2 = '006600', fontcolor3 = 'FFA34F', span_class1 = '', span_class2 = '', span_class3 = ''
 738                  WHERE themes_id = $theme_id";
 739              _sql($sql, $errored, $error_ary);
 740  
 741              $sql = "DELETE FROM " . THEMES_NAME_TABLE . "
 742                  WHERE themes_id = $theme_id";
 743              _sql($sql, $errored, $error_ary);
 744  
 745              $sql = "INSERT INTO " . THEMES_NAME_TABLE . " (themes_id, tr_color1_name, tr_color2_name, tr_color3_name, tr_class1_name, tr_class2_name, tr_class3_name, th_color1_name, th_color2_name, th_color3_name, th_class1_name, th_class2_name, th_class3_name, td_color1_name, td_color2_name, td_color3_name, td_class1_name, td_class2_name, td_class3_name, fontface1_name, fontface2_name, fontface3_name, fontsize1_name, fontsize2_name, fontsize3_name, fontcolor1_name, fontcolor2_name, fontcolor3_name, span_class1_name, span_class2_name, span_class3_name)
 746                  VALUES ($theme_id, 'The lightest row colour', 'The medium row color', 'The darkest row colour', '', '', '', 'Border round the whole page', 'Outer table border', 'Inner table border', 'Silver gradient picture', 'Blue gradient picture', 'Fade-out gradient on index', 'Background for quote boxes', 'All white areas', '', 'Background for topic posts', '2nd background for topic posts', '', 'Main fonts', 'Additional topic title font', 'Form fonts', 'Smallest font size', 'Medium font size', 'Normal font size (post body etc)', 'Quote & copyright text', 'Code text colour', 'Main table header text colour', '', '', '')";
 747              _sql($sql, $errored, $error_ary);
 748          }
 749          $db->sql_freeresult($result);
 750  
 751          $sql = "SELECT MIN(post_id) AS first_post_id, topic_id
 752              FROM " . POSTS_TABLE . "
 753              GROUP BY topic_id
 754              ORDER BY topic_id ASC";
 755          $result = _sql($sql, $errored, $error_ary);
 756  
 757          if ($row = $db->sql_fetchrow($result))
 758          {
 759              do
 760              {
 761                  $sql = "UPDATE " . TOPICS_TABLE . "
 762                      SET topic_first_post_id = " . $row['first_post_id'] . "
 763                      WHERE topic_id = " . $row['topic_id'];
 764                  _sql($sql, $errored, $error_ary);
 765              }
 766              while ($row = $db->sql_fetchrow($result));
 767          }
 768          $db->sql_freeresult($result);
 769  
 770          $sql = "SELECT DISTINCT u.user_id
 771              FROM " . USERS_TABLE . " u, " . USER_GROUP_TABLE . " ug, " . AUTH_ACCESS_TABLE . " aa
 772              WHERE aa.auth_mod = 1
 773                  AND ug.group_id = aa.group_id
 774                  AND u.user_id = ug.user_id
 775                  AND u.user_level <> " . ADMIN;
 776          $result = _sql($sql, $errored, $error_ary);
 777  
 778          $mod_user = array();
 779          while ($row = $db->sql_fetchrow($result))
 780          {
 781              $mod_user[] = $row['user_id'];
 782          }
 783          $db->sql_freeresult($result);
 784  
 785          if (count($mod_user))
 786          {
 787              $sql = "UPDATE " . USERS_TABLE . "
 788                  SET user_level = " . MOD . "
 789                  WHERE user_id IN (" . implode(', ', $mod_user) . ")";
 790              _sql($sql, $errored, $error_ary);
 791          }
 792  
 793          $sql = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value)
 794              VALUES ('server_name', 'www.myserver.tld')";
 795          _sql($sql, $errored, $error_ary);
 796  
 797          $sql = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value)
 798              VALUES ('script_path', '/phpBB2/')";
 799          _sql($sql, $errored, $error_ary);
 800  
 801          $sql = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value)
 802              VALUES ('server_port', '80')";
 803          _sql($sql, $errored, $error_ary);
 804  
 805          $sql = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value)
 806              VALUES ('record_online_users', '1')";
 807          _sql($sql, $errored, $error_ary);
 808  
 809          $sql = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value)
 810              VALUES ('record_online_date', '" . time() . "')";
 811          _sql($sql, $errored, $error_ary);
 812  
 813      case 'RC-3':
 814      case 'RC-4':
 815      case '.0.0':
 816      case '.0.1':
 817          if (SQL_LAYER == 'postgresql')
 818          {
 819              $sql = "SELECT user_id, user_timezone_old
 820                  FROM " . USERS_TABLE;
 821              $result = _sql($sql, $errored, $error_ary);
 822  
 823              while ($row = $db->sql_fetchrow($result))
 824              {
 825                  $sql = "UPDATE " . USERS_TABLE . "
 826                      SET user_timezone = " . $row['user_timezone_old'] . "
 827                      WHERE user_id = " . $row['user_id'];
 828                  _sql($sql, $errored, $error_ary);
 829              }
 830              $db->sql_freeresult($result);
 831          }
 832  
 833          $sql = "SELECT topic_id, topic_moved_id
 834              FROM " . TOPICS_TABLE . "
 835              WHERE topic_moved_id <> 0
 836                  AND topic_status = " . TOPIC_MOVED;
 837          $result = _sql($sql, $errored, $error_ary);
 838  
 839          $topic_ary = array();
 840          while ($row = $db->sql_fetchrow($result))
 841          {
 842              $topic_ary[$row['topic_id']] = $row['topic_moved_id'];
 843          }
 844          $db->sql_freeresult($result);
 845  
 846          while (list($topic_id, $topic_moved_id) = each($topic_ary))
 847          {
 848              $sql = "SELECT MAX(post_id) AS last_post, MIN(post_id) AS first_post, COUNT(post_id) AS total_posts
 849                  FROM " . POSTS_TABLE . "
 850                  WHERE topic_id = $topic_moved_id";
 851              $result = _sql($sql, $errored, $error_ary);
 852  
 853              $sql = ($row = $db->sql_fetchrow($result)) ? "UPDATE " . TOPICS_TABLE . "    SET topic_replies = " . ($row['total_posts'] - 1) . ", topic_first_post_id = " . $row['first_post'] . ", topic_last_post_id = " . $row['last_post'] . " WHERE topic_id = $topic_id" : "DELETE FROM " . TOPICS_TABLE . " WHERE topic_id = " . $row['topic_id'];
 854              _sql($sql, $errored, $error_ary);
 855          }
 856  
 857          unset($sql);
 858  
 859          sync('all forums');
 860  
 861      case '.0.2':
 862  
 863      case '.0.3':
 864  
 865          // Topics will resync automatically
 866  
 867          // Remove stop words from search match and search words
 868          $dirname = 'language';
 869          $dir = opendir($phpbb_root_path . $dirname);
 870  
 871          while ($file = readdir($dir))
 872          {
 873              if (preg_match("#^lang_#i", $file) && !is_file($phpbb_root_path . $dirname . "/" . $file) && !is_link($phpbb_root_path . $dirname . "/" . $file) && file_exists($phpbb_root_path . $dirname . "/" . $file . '/search_stopwords.txt'))
 874              {
 875  
 876                  $stopword_list = trim(preg_replace('#([\w\.\-_\+\'-\\\]+?)[ \n\r]*?(,|$)#', '\'\1\'\2', str_replace("'", "\'", implode(', ', file($phpbb_root_path . $dirname . "/" . $file . '/search_stopwords.txt')))));
 877  
 878                  $sql = "SELECT word_id 
 879                      FROM " . SEARCH_WORD_TABLE . " 
 880                      WHERE word_text IN ($stopword_list)";
 881                  $result = _sql($sql, $errored, $error_ary);
 882  
 883                  $word_id_sql = '';
 884                  if ($row = $db->sql_fetchrow($result))
 885                  {
 886                      do
 887                      {
 888                          $word_id_sql .= (($word_id_sql != '') ? ', ' : '') . $row['word_id'];
 889                      }
 890                      while ($row = $db->sql_fetchrow($result));
 891  
 892                      $sql = "DELETE FROM " . SEARCH_WORD_TABLE . " 
 893                          WHERE word_id IN ($word_id_sql)";
 894                      _sql($sql, $errored, $error_ary);
 895  
 896                      $sql = "DELETE FROM " . SEARCH_MATCH_TABLE . " 
 897                          WHERE word_id IN ($word_id_sql)";
 898                      _sql($sql, $errored, $error_ary);
 899                  }
 900                  $db->sql_freeresult($result);
 901              }
 902          }
 903          closedir($dir);
 904  
 905          // Mark common words ...
 906          remove_common('global', 4/10);
 907  
 908          // remove superfluous polls ... grab polls with topics then delete polls
 909          // not in that list
 910          $sql = "SELECT v.vote_id 
 911              FROM " . TOPICS_TABLE . " t, " . VOTE_DESC_TABLE . " v
 912              WHERE v.topic_id = t.topic_id";
 913          $result = _sql($sql, $errored, $error_ary);
 914  
 915          $vote_id_sql = '';
 916          if ($row = $db->sql_fetchrow($result))
 917          {
 918              do
 919              {
 920                  $vote_id_sql .= (($vote_id_sql != '') ? ', ' : '') . $row['vote_id'];
 921              }
 922              while ($row = $db->sql_fetchrow($result));
 923  
 924              $sql = "DELETE FROM " . VOTE_DESC_TABLE . " 
 925                  WHERE vote_id NOT IN ($vote_id_sql)";
 926              _sql($sql, $errored, $error_ary);
 927  
 928              $sql = "DELETE FROM " . VOTE_RESULTS_TABLE . " 
 929                  WHERE vote_id NOT IN ($vote_id_sql)";
 930              _sql($sql, $errored, $error_ary);
 931  
 932              $sql = "DELETE FROM " . VOTE_USERS_TABLE . " 
 933                  WHERE vote_id NOT IN ($vote_id_sql)";
 934              _sql($sql, $errored, $error_ary);
 935          }
 936          $db->sql_freeresult($result);
 937  
 938          // update pm counters
 939          $sql = "SELECT privmsgs_to_userid, COUNT(privmsgs_id) AS unread_count 
 940              FROM " . PRIVMSGS_TABLE . " 
 941              WHERE privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " 
 942              GROUP BY privmsgs_to_userid";
 943          $result = _sql($sql, $errored, $error_ary);
 944  
 945          if ($row = $db->sql_fetchrow($result))
 946          {
 947              $update_users = array();
 948              do
 949              {
 950                  $update_users[$row['unread_count']][] = $row['privmsgs_to_userid'];
 951              }
 952              while ($row = $db->sql_fetchrow($result));
 953  
 954              while (list($num, $user_ary) = each($update_users))
 955              {
 956                  $user_ids = implode(', ', $user_ary);
 957  
 958                  $sql = "UPDATE " . USERS_TABLE . " 
 959                      SET user_unread_privmsg = $num 
 960                      WHERE user_id IN ($user_ids)";
 961                  _sql($sql, $errored, $error_ary);
 962              }
 963              unset($update_list);
 964          }
 965          $db->sql_freeresult($result);
 966  
 967          $sql = "SELECT privmsgs_to_userid, COUNT(privmsgs_id) AS new_count 
 968              FROM " . PRIVMSGS_TABLE . " 
 969              WHERE privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
 970              GROUP BY privmsgs_to_userid";
 971          $result = _sql($sql, $errored, $error_ary);
 972  
 973          if ($row = $db->sql_fetchrow($result))
 974          {
 975              $update_users = array();
 976              do
 977              {
 978                  $update_users[$row['new_count']][] = $row['privmsgs_to_userid'];
 979              }
 980              while ($row = $db->sql_fetchrow($result));
 981  
 982              while (list($num, $user_ary) = each($update_users))
 983              {
 984                  $user_ids = implode(', ', $user_ary);
 985  
 986                  $sql = "UPDATE " . USERS_TABLE . " 
 987                      SET user_new_privmsg = $num 
 988                      WHERE user_id IN ($user_ids)";
 989                  _sql($sql, $errored, $error_ary);
 990              }
 991              unset($update_list);
 992          }
 993          $db->sql_freeresult($result);
 994  
 995          // Remove superfluous watched topics
 996          $sql = "SELECT t.topic_id 
 997              FROM " . TOPICS_TABLE . " t, " . TOPICS_WATCH_TABLE . " w
 998              WHERE w.topic_id = t.topic_id";
 999          $result = _sql($sql, $errored, $error_ary);
1000  
1001          $topic_id_sql = '';
1002          if ($row = $db->sql_fetchrow($result))
1003          {
1004              do
1005              {
1006                  $topic_id_sql .= (($topic_id_sql != '') ? ', ' : '') . $row['topic_id'];
1007              }
1008              while ($row = $db->sql_fetchrow($result));
1009  
1010              $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . " 
1011                  WHERE topic_id NOT IN ($topic_id_sql)";
1012              _sql($sql, $errored, $error_ary);
1013          }
1014          $db->sql_freeresult($result);
1015  
1016          // Reset any email addresses which are non-compliant ... something
1017          // not done in the upgrade script and thus which may affect some 
1018          // mysql users
1019          switch (SQL_LAYER)
1020          {
1021              case 'mysql':
1022                  $sql = "UPDATE " . USERS_TABLE . " 
1023                      SET user_email = '' 
1024                      WHERE user_email NOT REGEXP '^[a-zA-Z0-9_\+\.\-]+@.*[a-zA-Z0-9_\-]+\.[a-zA-Z]{2,}$'";
1025                  _sql($sql, $errored, $error_ary);
1026          }
1027  
1028      case '.0.4':
1029  
1030          // Add the confirmation code switch ... save time and trouble elsewhere
1031          $sql = 'INSERT INTO ' . CONFIG_TABLE . " (config_name, config_value)
1032              VALUES ('enable_confirm', '0')";
1033          _sql($sql, $errored, $error_ary);
1034  
1035          $sql = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value)
1036              VALUES ('sendmail_fix', '0')";
1037          _sql($sql, $errored, $error_ary);
1038  
1039      case '.0.5':
1040          
1041          $sql = "SELECT user_id, username 
1042              FROM " . USERS_TABLE;
1043          $result = _sql($sql, $errored, $error_ary);
1044  
1045          while ($row = $db->sql_fetchrow($result))
1046          {
1047              if (!preg_match('#(&gt;)|(&lt;)|(&quot)|(&amp;)#', $row['username']))
1048              {
1049                  if ($row['username'] != htmlspecialchars($row['username']))
1050                  {
1051                      $sql = "UPDATE " . USERS_TABLE . "
1052                          SET username = '" . str_replace("'", "''", htmlspecialchars($row['username'])) . "'
1053                          WHERE user_id = " . $row['user_id'];
1054                      _sql($sql, $errored, $error_ary);
1055                  }
1056              }
1057          }
1058          $db->sql_freeresult($result);
1059          
1060      case '.0.6':
1061      case '.0.7':
1062      case '.0.8':
1063      case '.0.9':
1064      case '.0.10':
1065      case '.0.11':
1066      case '.0.12':
1067      case '.0.13':
1068      case '.0.14':
1069  
1070          $sql = 'UPDATE ' . USERS_TABLE . ' SET user_allowhtml = 1 WHERE user_id = ' . ANONYMOUS;
1071          _sql($sql, $errored, $error_ary);
1072  
1073      case '.0.15':
1074      case '.0.16':
1075      case '.0.17':
1076  
1077          $sql = 'UPDATE ' . USERS_TABLE . ' SET user_active = 0 WHERE user_id = ' . ANONYMOUS;
1078          _sql($sql, $errored, $error_ary);
1079  
1080          $sql = 'INSERT INTO ' . CONFIG_TABLE . " (config_name, config_value)
1081              VALUES ('allow_autologin', '1')";
1082          _sql($sql, $errored, $error_ary);
1083  
1084          $sql = 'INSERT INTO ' . CONFIG_TABLE . " (config_name, config_value)
1085              VALUES ('max_autologin_time', '0')";
1086          _sql($sql, $errored, $error_ary);
1087          
1088      case '.0.18':
1089  
1090          $sql = 'INSERT INTO ' . CONFIG_TABLE . " (config_name, config_value)
1091              VALUES ('max_login_attempts', '5')";
1092          _sql($sql, $errored, $error_ary);
1093  
1094          $sql = 'INSERT INTO ' . CONFIG_TABLE . " (config_name, config_value)
1095              VALUES ('login_reset_time', '30')";
1096          _sql($sql, $errored, $error_ary);
1097  
1098      case '.0.19':
1099  
1100          $sql = 'INSERT INTO ' . CONFIG_TABLE . " (config_name, config_value)
1101              VALUES ('search_flood_interval', '15')";
1102          _sql($sql, $errored, $error_ary);
1103  
1104          $sql = 'INSERT INTO ' . CONFIG_TABLE . " (config_name, config_value)
1105              VALUES ('rand_seed', '0')";
1106          _sql($sql, $errored, $error_ary);
1107  
1108      case '.0.20':
1109  
1110          $sql = 'INSERT INTO ' . CONFIG_TABLE . " (config_name, config_value)
1111              VALUES ('search_min_chars', '3')";
1112          _sql($sql, $errored, $error_ary);
1113  
1114          // We reset those having autologin enabled and forcing the re-assignment of a session id
1115          // since there have been changes to the way these are handled from previous versions
1116          $sql = 'DELETE FROM ' . SESSIONS_TABLE;
1117          _sql($sql, $errored, $error_ary);
1118  
1119          $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE;
1120          _sql($sql, $errored, $error_ary);
1121  
1122          break;
1123  
1124      default:
1125          echo " No updates were required</b></p>\n";
1126          break;
1127  }
1128  
1129  echo "<h2>Updating version and optimizing tables</h2>\n";
1130  echo "<p>Progress :: <b>";
1131  flush();
1132  
1133  // update the version
1134  $sql = "UPDATE " . CONFIG_TABLE . "
1135      SET config_value = '$updates_to_version'
1136      WHERE config_name = 'version'";
1137  _sql($sql, $errored, $error_ary);
1138  
1139  // Optimize/vacuum analyze the tables where appropriate 
1140  // this should be done for each version in future along with 
1141  // the version number update
1142  switch (SQL_LAYER)
1143  {
1144      case 'mysql':
1145      case 'mysql4':
1146          $sql = 'OPTIMIZE TABLE ' . $table_prefix . 'auth_access, ' . $table_prefix . 'banlist, ' . $table_prefix . 'categories, ' . $table_prefix . 'config, ' . $table_prefix . 'disallow, ' . $table_prefix . 'forum_prune, ' . $table_prefix . 'forums, ' . $table_prefix . 'groups, ' . $table_prefix . 'posts, ' . $table_prefix . 'posts_text, ' . $table_prefix . 'privmsgs, ' . $table_prefix . 'privmsgs_text, ' . $table_prefix . 'ranks, ' . $table_prefix . 'search_results, ' . $table_prefix . 'search_wordlist, ' . $table_prefix . 'search_wordmatch, ' . $table_prefix . 'sessions_keys, ' . $table_prefix . 'smilies, ' . $table_prefix . 'themes, ' . $table_prefix . 'themes_name, ' . $table_prefix . 'topics, ' . $table_prefix . 'topics_watch, ' . $table_prefix . 'user_group, ' . $table_prefix . 'users, ' . $table_prefix . 'vote_desc, ' . $table_prefix . 'vote_results, ' . $table_prefix . 'vote_voters, ' . $table_prefix . 'words';
1147          _sql($sql, $errored, $error_ary);
1148          break;
1149  
1150      case 'postgresql':
1151          _sql("VACUUM ANALYZE", $errored, $error_ary);
1152          break;
1153  }
1154  
1155  echo "</b> <b class=\"ok\">Done</b><br />Result &nbsp; :: \n";
1156  
1157  if ($errored)
1158  {
1159      echo " <b>Some queries failed, the statements and errors are listing below</b>\n<ul>";
1160  
1161      for ($i = 0; $i < count($error_ary['sql']); $i++)
1162      {
1163          echo "<li>Error :: <b>" . $error_ary['error_code'][$i]['message'] . "</b><br />";
1164          echo "SQL &nbsp; :: <b>" . $error_ary['sql'][$i] . "</b><br /><br /></li>";
1165      }
1166  
1167      echo "</ul>\n<p>This is probably nothing to worry about, update will continue. Should this fail to complete you may need to seek help at our development board. See <a href=\"docs\README.html\">README</a> for details on how to obtain advice.</p>\n";
1168  }
1169  else
1170  {
1171      echo "<b>No errors</b>\n";
1172  }
1173  
1174  echo "<h2>Update completed</h2>\n";
1175  echo "\n" . '<p style="color:red">Please make sure you have updated your board files too, this file is only updating your database.</p>';
1176  echo "\n<p>You should now visit the General Configuration settings page in the <a href=\"../admin/\">Administration Panel</a> and check the General Configuration of the board. If you updated from versions prior to RC-3 you <b>must</b> update some entries. If you do not do this emails sent from the board will contain incorrect information. Don't forget to delete this file!</p>\n";
1177  
1178  ?>
1179  
1180  <br clear="all" />
1181  
1182  </body>
1183  </html>


Generated: Mon Jan 14 19:21:40 2013 Cross-referenced by PHPXref 0.7.1