[ Index ]

PHP Cross Reference of MyBB 1.8.40

title

Body

[close]

/install/resources/ -> upgrade30.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  /**
  12   * Upgrade Script: 1.6.14, 1.6.15, 1.6.16, 1.6.17 or 1.6.18
  13   */
  14  
  15  $upgrade_detail = array(
  16      "revert_all_templates" => 0,
  17      "revert_all_themes" => 0,
  18      "revert_all_settings" => 0
  19  );
  20  
  21  function upgrade30_dbchanges()
  22  {
  23      global $cache, $output, $mybb, $db;
  24  
  25      $output->print_header("Updating Database");
  26  
  27      echo "<p>Performing necessary upgrade queries...</p>";
  28      flush();
  29  
  30      $db->update_query('settings', array('value' => -1), 'name IN (\'postmergefignore\', \'postmergeuignore\') AND value=\'\'');
  31      $db->update_query('settings', array('optionscode' => 'forumselect'), 'name IN (\'postmergefignore\', \'portal_announcementsfid\') AND optionscode=\'text\'');
  32      $db->update_query('settings', array('optionscode' => 'groupselect'), 'name=\'postmergeuignore\' AND optionscode=\'text\'');
  33  
  34      if($db->type == "mysql" || $db->type == "mysqli")
  35      {
  36          if($db->index_exists('posts', 'tiddate'))
  37          {
  38              $db->drop_index('posts', 'tiddate');
  39          }
  40  
  41          $db->write_query("ALTER TABLE ".TABLE_PREFIX."posts ADD INDEX (`tid`, `dateline`)");
  42      }
  43  
  44      if($db->field_exists('modposts', 'usergroups'))
  45      {
  46          $db->drop_column("usergroups", "modposts");
  47      }
  48  
  49      if($db->field_exists('modthreads', 'usergroups'))
  50      {
  51          $db->drop_column("usergroups", "modthreads");
  52      }
  53  
  54      if($db->field_exists('mod_edit_posts', 'usergroups'))
  55      {
  56          $db->drop_column("usergroups", "mod_edit_posts");
  57      }
  58  
  59      if($db->field_exists('modattachments', 'usergroups'))
  60      {
  61          $db->drop_column("usergroups", "modattachments");
  62      }
  63  
  64      if($db->field_exists('regex', 'profilefields'))
  65      {
  66          $db->drop_column("profilefields", "regex");
  67      }
  68  
  69      if($db->field_exists('allowhtml', 'profilefields'))
  70      {
  71          $db->drop_column("profilefields", "allowhtml");
  72      }
  73  
  74      if($db->field_exists('allowmycode', 'profilefields'))
  75      {
  76          $db->drop_column("profilefields", "allowmycode");
  77      }
  78  
  79      if($db->field_exists('allowsmilies', 'profilefields'))
  80      {
  81          $db->drop_column("profilefields", "allowsmilies");
  82      }
  83  
  84      if($db->field_exists('allowimgcode', 'profilefields'))
  85      {
  86          $db->drop_column("profilefields", "allowimgcode");
  87      }
  88  
  89      if($db->field_exists('allowvideocode', 'profilefields'))
  90      {
  91          $db->drop_column("profilefields", "allowvideocode");
  92      }
  93  
  94      if($db->field_exists('viewableby', 'profilefields'))
  95      {
  96          $db->drop_column("profilefields", "viewableby");
  97      }
  98  
  99      if($db->field_exists('editable`', 'profilefields'))
 100      {
 101          $db->drop_column("profilefields", "editable");
 102      }
 103  
 104      if($db->field_exists('editableby', 'profilefields'))
 105      {
 106          $db->drop_column("profilefields", "editableby");
 107      }
 108  
 109      if($db->field_exists('oldgroup', 'awaitingactivation'))
 110      {
 111          $db->drop_column("awaitingactivation", "oldgroup");
 112      }
 113  
 114      if($db->field_exists('status', 'forums'))
 115      {
 116          $db->drop_column("forums", "status");
 117      }
 118  
 119      if($db->field_exists('posthash', 'posts'))
 120      {
 121          $db->drop_column("posts", "posthash");
 122      }
 123  
 124      if($db->field_exists('isdefault', 'templategroups'))
 125      {
 126          $db->drop_column("templategroups", "isdefault");
 127      }
 128  
 129      if($db->table_exists('reportedposts'))
 130      {
 131          if($db->field_exists('type', 'reportedposts'))
 132          {
 133              $db->drop_column("reportedposts", "type");
 134          }
 135  
 136          if($db->field_exists('reports', 'reportedposts'))
 137          {
 138              $db->drop_column("reportedposts", "reports");
 139          }
 140  
 141          if($db->field_exists('reporters', 'reportedposts'))
 142          {
 143              $db->drop_column("reportedposts", "reporters");
 144          }
 145  
 146          if($db->field_exists('lastreport', 'reportedposts'))
 147          {
 148              $db->drop_column("reportedposts", "lastreport");
 149          }
 150      }
 151  
 152      if($db->field_exists('warnings', 'promotions'))
 153      {
 154          $db->drop_column("promotions", "warnings");
 155      }
 156  
 157      if($db->field_exists('warningstype', 'promotions'))
 158      {
 159          $db->drop_column("promotions", "warningstype");
 160      }
 161  
 162      if($db->field_exists('useragent', 'adminsessions'))
 163      {
 164          $db->drop_column("adminsessions", "useragent");
 165      }
 166  
 167      if($db->field_exists('deletedthreads', 'forums'))
 168      {
 169          $db->drop_column("forums", "deletedthreads");
 170      }
 171  
 172      if($db->field_exists('deletedposts', 'forums'))
 173      {
 174          $db->drop_column("forums", "deletedposts");
 175      }
 176  
 177      if($db->field_exists('threads', 'promotions'))
 178      {
 179          $db->drop_column('promotions', 'threads');
 180      }
 181  
 182      if($db->field_exists('threadtype', 'promotions'))
 183      {
 184          $db->drop_column('promotions', 'threadtype');
 185      }
 186  
 187      if($db->field_exists('online', 'promotions'))
 188      {
 189          $db->drop_column('promotions', 'online');
 190      }
 191  
 192      if($db->field_exists('onlinetype', 'promotions'))
 193      {
 194          $db->drop_column('promotions', 'onlinetype');
 195      }
 196  
 197      if($db->field_exists('modposts', 'forums'))
 198      {
 199          $db->drop_column("forums", "modposts");
 200      }
 201  
 202      if($db->field_exists('modthreads', 'forums'))
 203      {
 204          $db->drop_column("forums", "modthreads");
 205      }
 206  
 207      if($db->field_exists('mod_edit_posts', 'forums'))
 208      {
 209          $db->drop_column("forums", "mod_edit_posts");
 210      }
 211  
 212      if($db->field_exists('modattachments', 'forums'))
 213      {
 214          $db->drop_column("forums", "modattachments");
 215      }
 216  
 217      // Avoid complex convert coding...
 218      if($db->field_exists('hidden', 'profilefields'))
 219      {
 220          $db->update_query('profilefields', array('hidden' => 2), 'hidden=1');
 221          $db->update_query('profilefields', array('hidden' => 1), 'hidden=0');
 222          $db->update_query('profilefields', array('hidden' => 0), 'hidden=2');
 223  
 224          switch($db->type)
 225          {
 226              case "pgsql":
 227                  $db->rename_column("profilefields", "hidden", "profile", "smallint", "set", "'0'");
 228                  break;
 229              default:
 230                  $db->rename_column("profilefields", "hidden", "profile", "tinyint(1) NOT NULL default '0'");
 231                  break;
 232          }
 233      }
 234  
 235      switch($db->type)
 236      {
 237          case "pgsql":
 238              $db->add_column("usergroups", "modposts", "smallint NOT NULL default '0' AFTER canratethreads");
 239              $db->add_column("usergroups", "modthreads", "smallint NOT NULL default '0' AFTER modposts");
 240              $db->add_column("usergroups", "mod_edit_posts", "smallint NOT NULL default '0' AFTER modthreads");
 241              $db->add_column("usergroups", "modattachments", "smallint NOT NULL default '0' AFTER mod_edit_posts");
 242              $db->add_column("profilefields", "regex", "text NOT NULL default ''");
 243              $db->add_column("profilefields", "allowhtml", "smallint NOT NULL default '0'");
 244              $db->add_column("profilefields", "allowmycode", "smallint NOT NULL default '0'");
 245              $db->add_column("profilefields", "allowsmilies", "smallint NOT NULL default '0'");
 246              $db->add_column("profilefields", "allowimgcode", "smallint NOT NULL default '0'");
 247              $db->add_column("profilefields", "allowvideocode", "smallint NOT NULL default '0'");
 248              $db->add_column("profilefields", "viewableby", "text NOT NULL default ''");
 249              $db->add_column("profilefields", "editableby", "text NOT NULL default ''");
 250              $db->add_column("templategroups", "isdefault", "smallint NOT NULL default '0'");
 251              if($db->table_exists('reportedposts'))
 252              {
 253                  $db->add_column("reportedposts", "type", "varchar(50) NOT NULL default ''");
 254                  $db->add_column("reportedposts", "reports", "int NOT NULL default '0'");
 255                  $db->add_column("reportedposts", "reporters", "text NOT NULL default ''");
 256                  $db->add_column("reportedposts", "lastreport", "bigint NOT NULL default '0'");
 257              }
 258              $db->add_column("promotions", "threads", "int NOT NULL default '0' AFTER posttype");
 259              $db->add_column("promotions", "threadtype", "varchar(2) NOT NULL default '' AFTER threads");
 260              $db->add_column("promotions", "warnings", "int NOT NULL default '0' AFTER referralstype");
 261              $db->add_column("promotions", "warningstype", "varchar(2) NOT NULL default '' AFTER warnings");
 262              $db->add_column("promotions", "online", "int NOT NULL default '0' AFTER warningstype");
 263              $db->add_column("promotions", "onlinetype", "varchar(20) NOT NULL default '' AFTER online");
 264              $db->add_column("adminsessions", "useragent", "varchar(100) NOT NULL default ''");
 265              $db->add_column("forums", "deletedthreads", "int NOT NULL default '0' AFTER unapprovedposts");
 266              $db->add_column("forums", "deletedposts", "int NOT NULL default '0' AFTER deletedthreads");
 267              break;
 268          case "sqlite":
 269              $db->add_column("usergroups", "modposts", "tinyint(1) NOT NULL default '0' AFTER canratethreads");
 270              $db->add_column("usergroups", "modthreads", "tinyint(1) NOT NULL default '0' AFTER modposts");
 271              $db->add_column("usergroups", "mod_edit_posts", "tinyint(1) NOT NULL default '0' AFTER modthreads");
 272              $db->add_column("usergroups", "modattachments", "tinyint(1) NOT NULL default '0' AFTER mod_edit_posts");
 273              $db->add_column("profilefields", "regex", "text NOT NULL default ''");
 274              $db->add_column("profilefields", "allowhtml", "tinyint(1) NOT NULL default '0'");
 275              $db->add_column("profilefields", "allowmycode", "tinyint(1) NOT NULL default '0'");
 276              $db->add_column("profilefields", "allowsmilies", "tinyint(1) NOT NULL default '0'");
 277              $db->add_column("profilefields", "allowimgcode", "tinyint(1) NOT NULL default '0'");
 278              $db->add_column("profilefields", "allowvideocode", "tinyint(1) NOT NULL default '0'");
 279              $db->add_column("profilefields", "viewableby", "text NOT NULL default ''");
 280              $db->add_column("profilefields", "editableby", "text NOT NULL default ''");
 281              $db->add_column("templategroups", "isdefault", "tinyint(1) NOT NULL default '0'");
 282              if($db->table_exists('reportedposts'))
 283              {
 284                  $db->add_column("reportedposts", "type", "varchar(50) NOT NULL default ''");
 285                  $db->add_column("reportedposts", "reports", "int NOT NULL default '0'");
 286                  $db->add_column("reportedposts", "reporters", "text NOT NULL default ''");
 287                  $db->add_column("reportedposts", "lastreport", "bigint NOT NULL default '0'");
 288              }
 289              $db->add_column("promotions", "warnings", "int NOT NULL default '0' AFTER referralstype");
 290              $db->add_column("promotions", "warningstype", "varchar(2) NOT NULL default '' AFTER warnings");
 291              $db->add_column("adminsessions", "useragent", "varchar(100) NOT NULL default ''");
 292              $db->add_column("forums", "deletedthreads", "int NOT NULL default '0' AFTER unapprovedposts");
 293              $db->add_column("forums", "deletedposts", "int NOT NULL default '0' AFTER deletedthreads");
 294              break;
 295          default:
 296              $db->add_column("usergroups", "modposts", "tinyint(1) NOT NULL default '0' AFTER canratethreads");
 297              $db->add_column("usergroups", "modthreads", "tinyint(1) NOT NULL default '0' AFTER modposts");
 298              $db->add_column("usergroups", "mod_edit_posts", "tinyint(1) NOT NULL default '0' AFTER modthreads");
 299              $db->add_column("usergroups", "modattachments", "tinyint(1) NOT NULL default '0' AFTER mod_edit_posts");
 300              $db->add_column("profilefields", "regex", "text NOT NULL");
 301              $db->add_column("profilefields", "allowhtml", "tinyint(1) NOT NULL default '0'");
 302              $db->add_column("profilefields", "allowmycode", "tinyint(1) NOT NULL default '0'");
 303              $db->add_column("profilefields", "allowsmilies", "tinyint(1) NOT NULL default '0'");
 304              $db->add_column("profilefields", "allowimgcode", "tinyint(1) NOT NULL default '0'");
 305              $db->add_column("profilefields", "allowvideocode", "tinyint(1) NOT NULL default '0'");
 306              $db->add_column("profilefields", "viewableby", "text NOT NULL");
 307              $db->add_column("profilefields", "editableby", "text NOT NULL");
 308              $db->add_column("templategroups", "isdefault", "tinyint(1) NOT NULL default '0'");
 309              if($db->table_exists('reportedposts'))
 310              {
 311                  $db->add_column("reportedposts", "type", "varchar(50) NOT NULL default ''");
 312                  $db->add_column("reportedposts", "reports", "int unsigned NOT NULL default '0'");
 313                  $db->add_column("reportedposts", "reporters", "text NOT NULL");
 314                  $db->add_column("reportedposts", "lastreport", "bigint(30) NOT NULL default '0'");
 315              }
 316              $db->add_column("promotions", "threads", "int NOT NULL default '0' AFTER posttype");
 317              $db->add_column("promotions", "threadtype", "char(2) NOT NULL default '' AFTER threads");
 318              $db->add_column("promotions", "warnings", "int NOT NULL default '0' AFTER referralstype");
 319              $db->add_column("promotions", "warningstype", "char(2) NOT NULL default '' AFTER warnings");
 320              $db->add_column("promotions", "online", "int NOT NULL default '0' AFTER warningstype");
 321              $db->add_column("promotions", "onlinetype", "varchar(20) NOT NULL default '' AFTER online");
 322              $db->add_column("adminsessions", "useragent", "varchar(100) NOT NULL default ''");
 323              $db->add_column("forums", "deletedthreads", "int(10) NOT NULL default '0' AFTER unapprovedposts");
 324              $db->add_column("forums", "deletedposts", "int(10) NOT NULL default '0' AFTER deletedthreads");
 325              break;
 326      }
 327  
 328      $db->update_query('profilefields', array('viewableby' => '-1', 'editableby' => '-1'));
 329  
 330      global $footer_extra;
 331      $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
 332  
 333      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
 334      $output->print_footer("30_dbchanges2");
 335  }
 336  
 337  function upgrade30_dbchanges2()
 338  {
 339      global $cache, $output, $mybb, $db;
 340  
 341      $output->print_header("Updating Database");
 342  
 343      echo "<p>Performing necessary upgrade queries...</p>";
 344      flush();
 345  
 346      if($db->field_exists('ipaddress', 'privatemessages'))
 347      {
 348          $db->drop_column('privatemessages', 'ipaddress');
 349      }
 350  
 351      if($db->field_exists('canonlyreplyownthreads', 'forumpermissions'))
 352      {
 353          $db->drop_column("forumpermissions", "canonlyreplyownthreads");
 354      }
 355  
 356      if($db->field_exists('modposts', 'forumpermissions'))
 357      {
 358          $db->drop_column("forumpermissions", "modposts");
 359      }
 360  
 361      if($db->field_exists('modthreads', 'forumpermissions'))
 362      {
 363          $db->drop_column("forumpermissions", "modthreads");
 364      }
 365  
 366      if($db->field_exists('mod_edit_posts', 'forumpermissions'))
 367      {
 368          $db->drop_column("forumpermissions", "mod_edit_posts");
 369      }
 370  
 371      if($db->field_exists('modattachments', 'forumpermissions'))
 372      {
 373          $db->drop_column("forumpermissions", "modattachments");
 374      }
 375  
 376      if($db->field_exists('canbereported', 'usergroups'))
 377      {
 378          $db->drop_column('usergroups', 'canbereported');
 379      }
 380  
 381      if($db->field_exists('edittimelimit', 'usergroups'))
 382      {
 383          $db->drop_column("usergroups", "edittimelimit");
 384      }
 385  
 386      if($db->field_exists('maxposts', 'usergroups'))
 387      {
 388          $db->drop_column("usergroups", "maxposts");
 389      }
 390  
 391      if($db->field_exists('showmemberlist', 'usergroups'))
 392      {
 393          $db->drop_column("usergroups", "showmemberlist");
 394      }
 395  
 396      if($db->field_exists('canviewboardclosed', 'usergroups'))
 397      {
 398          $db->drop_column("usergroups", "canviewboardclosed");
 399      }
 400  
 401      if($db->field_exists('deletedposts', 'threads'))
 402      {
 403          $db->drop_column("threads", "deletedposts");
 404      }
 405  
 406      if($db->field_exists('used', 'captcha'))
 407      {
 408          $db->drop_column("captcha", "used");
 409      }
 410  
 411      if($db->field_exists('editreason', 'posts'))
 412      {
 413          $db->drop_column("posts", "editreason");
 414      }
 415  
 416      if($db->field_exists('usethreadcounts', 'forums'))
 417      {
 418          $db->drop_column("forums", "usethreadcounts");
 419      }
 420  
 421      if($db->field_exists('requireprefix', 'forums'))
 422      {
 423          $db->drop_column("forums", "requireprefix");
 424      }
 425  
 426      if($db->field_exists('threadnum', 'users'))
 427      {
 428          $db->drop_column("users", "threadnum");
 429      }
 430  
 431      if($db->field_exists('canchangewebsite', 'usergroups'))
 432      {
 433          $db->drop_column("usergroups", "canchangewebsite");
 434      }
 435  
 436      switch($db->type)
 437      {
 438          case "pgsql":
 439              $db->add_column("forumpermissions", "canonlyreplyownthreads", "smallint NOT NULL default '0' AFTER canpostreplys");
 440              $db->add_column("forumpermissions", "modposts", "smallint NOT NULL default '0' AFTER caneditattachments");
 441              $db->add_column("forumpermissions", "modthreads", "smallint NOT NULL default '0' AFTER modposts");
 442              $db->add_column("forumpermissions", "mod_edit_posts", "smallint NOT NULL default '0' AFTER modthreads");
 443              $db->add_column("forumpermissions", "modattachments", "smallint NOT NULL default '0' AFTER mod_edit_posts");
 444              $db->add_column("usergroups", "canbereported", "smallint NOT NULL default '0' AFTER canchangename");
 445              $db->add_column("usergroups", "canchangewebsite", "smallint NOT NULL default '1' AFTER canbereported");
 446              $db->add_column("usergroups", "edittimelimit", "int NOT NULL default '0'");
 447              $db->add_column("usergroups", "maxposts", "int NOT NULL default '0'");
 448              $db->add_column("usergroups", "showmemberlist", "smallint NOT NULL default '1'");
 449              $db->add_column("usergroups", "canviewboardclosed", "smallint NOT NULL default '0' AFTER candlattachments");
 450              $db->add_column("threads", "deletedposts", "int NOT NULL default '0' AFTER unapprovedposts");
 451              $db->add_column("captcha", "used", "smallint NOT NULL default '0'");
 452              $db->add_column("posts", "editreason", "varchar(150) NOT NULL default '' AFTER edittime");
 453              $db->add_column("forums", "usethreadcounts", "smallint NOT NULL default '0' AFTER usepostcounts");
 454              $db->add_column("forums", "requireprefix", "smallint NOT NULL default '0' AFTER usethreadcounts");
 455              $db->add_column("users", "threadnum", "int NOT NULL default '0' AFTER postnum");
 456              break;
 457          default:
 458              $db->add_column("forumpermissions", "canonlyreplyownthreads", "tinyint(1) NOT NULL default '0' AFTER canpostreplys");
 459              $db->add_column("forumpermissions", "modposts", "tinyint(1) NOT NULL default '0' AFTER caneditattachments");
 460              $db->add_column("forumpermissions", "modthreads", "tinyint(1) NOT NULL default '0' AFTER modposts");
 461              $db->add_column("forumpermissions", "mod_edit_posts", "tinyint(1) NOT NULL default '0' AFTER modthreads");
 462              $db->add_column("forumpermissions", "modattachments", "tinyint(1) NOT NULL default '0' AFTER mod_edit_posts");
 463              $db->add_column("usergroups", "canbereported", "tinyint(1) NOT NULL default '0' AFTER canchangename");
 464              $db->add_column("usergroups", "canchangewebsite", "tinyint(1) NOT NULL default '1' AFTER canbereported");
 465              $db->add_column("usergroups", "edittimelimit", "int(4) NOT NULL default '0'");
 466              $db->add_column("usergroups", "maxposts", "int(4) NOT NULL default '0'");
 467              $db->add_column("usergroups", "showmemberlist", "tinyint(1) NOT NULL default '1'");
 468              $db->add_column("usergroups", "canviewboardclosed", "tinyint(1) NOT NULL default '0' AFTER candlattachments");
 469              $db->add_column("threads", "deletedposts", "int(10) NOT NULL default '0' AFTER unapprovedposts");
 470              $db->add_column("captcha", "used", "tinyint(1) NOT NULL default '0'");
 471              $db->add_column("posts", "editreason", "varchar(150) NOT NULL default '' AFTER edittime");
 472              $db->add_column("forums", "usethreadcounts", "tinyint(1) NOT NULL default '0' AFTER usepostcounts");
 473              $db->add_column("forums", "requireprefix", "tinyint(1) NOT NULL default '0' AFTER usethreadcounts");
 474              $db->add_column("users", "threadnum", "int(10) NOT NULL default '0' AFTER postnum");
 475              break;
 476      }
 477  
 478      $db->update_query('forums', array('usethreadcounts' => 1), 'usepostcounts = 1');
 479  
 480      global $footer_extra;
 481      $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
 482  
 483      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
 484      $output->print_footer("30_dbchanges3");
 485  }
 486  
 487  function upgrade30_dbchanges3()
 488  {
 489      global $cache, $output, $mybb, $db;
 490  
 491      $output->print_header("Updating Database");
 492  
 493      echo "<p>Performing necessary upgrade queries...</p>";
 494      flush();
 495  
 496      if($db->field_exists('cansoftdeleteposts', 'moderators'))
 497      {
 498          $db->drop_column('moderators', 'cansoftdeleteposts');
 499      }
 500  
 501      if($db->field_exists('canrestoreposts', 'moderators'))
 502      {
 503          $db->drop_column("moderators", "canrestoreposts");
 504      }
 505  
 506      if($db->field_exists('cansoftdeletethreads', 'moderators'))
 507      {
 508          $db->drop_column('moderators', 'cansoftdeletethreads');
 509      }
 510  
 511      if($db->field_exists('canrestorethreads', 'moderators'))
 512      {
 513          $db->drop_column("moderators", "canrestorethreads");
 514      }
 515  
 516      if($db->field_exists('candeletethreads', 'moderators'))
 517      {
 518          $db->drop_column("moderators", "candeletethreads");
 519      }
 520  
 521      if($db->field_exists('canviewunapprove', 'moderators'))
 522      {
 523          $db->drop_column("moderators", "canviewunapprove");
 524      }
 525  
 526      if($db->field_exists('canviewdeleted', 'moderators'))
 527      {
 528          $db->drop_column("moderators", "canviewdeleted");
 529      }
 530  
 531      if($db->field_exists('canstickunstickthreads', 'moderators'))
 532      {
 533          $db->drop_column("moderators", "canstickunstickthreads");
 534      }
 535  
 536      if($db->field_exists('canapproveunapprovethreads', 'moderators'))
 537      {
 538          $db->drop_column("moderators", "canapproveunapprovethreads");
 539      }
 540  
 541      if($db->field_exists('canapproveunapproveposts', 'moderators'))
 542      {
 543          $db->drop_column("moderators", "canapproveunapproveposts");
 544      }
 545  
 546      if($db->field_exists('canapproveunapproveattachs', 'moderators'))
 547      {
 548          $db->drop_column("moderators", "canapproveunapproveattachs");
 549      }
 550  
 551      if($db->field_exists('canmanagepolls', 'moderators'))
 552      {
 553          $db->drop_column("moderators", "canmanagepolls");
 554      }
 555  
 556      if($db->field_exists('canpostclosedthreads', 'moderators'))
 557      {
 558          $db->drop_column("moderators", "canpostclosedthreads");
 559      }
 560  
 561      if($db->field_exists('canmanageannouncements', 'moderators'))
 562      {
 563          $db->drop_column("moderators", "canmanageannouncements");
 564      }
 565  
 566      if($db->field_exists('canmanagereportedposts', 'moderators'))
 567      {
 568          $db->drop_column("moderators", "canmanagereportedposts");
 569      }
 570  
 571      if($db->field_exists('canviewmodlog', 'moderators'))
 572      {
 573          $db->drop_column("moderators", "canviewmodlog");
 574      }
 575  
 576      switch($db->type)
 577      {
 578          case "pgsql":
 579              $db->add_column("moderators", "cansoftdeleteposts", "smallint NOT NULL default '0' AFTER caneditposts");
 580              $db->add_column("moderators", "canrestoreposts", "smallint NOT NULL default '0' AFTER cansoftdeleteposts");
 581              $db->add_column("moderators", "cansoftdeletethreads", "smallint NOT NULL default '0' AFTER candeleteposts");
 582              $db->add_column("moderators", "canrestorethreads", "smallint NOT NULL default '0' AFTER cansoftdeletethreads");
 583              $db->add_column("moderators", "candeletethreads", "smallint NOT NULL default '0' AFTER canrestorethreads");
 584              $db->add_column("moderators", "canviewunapprove", "smallint NOT NULL default '0' AFTER canviewips");
 585              $db->add_column("moderators", "canviewdeleted", "smallint NOT NULL default '0' AFTER canviewunapprove");
 586              $db->add_column("moderators", "canstickunstickthreads", "smallint NOT NULL default '0' AFTER canopenclosethreads");
 587              $db->add_column("moderators", "canapproveunapprovethreads", "smallint NOT NULL default '0' AFTER canstickunstickthreads");
 588              $db->add_column("moderators", "canapproveunapproveposts", "smallint NOT NULL default '0' AFTER canapproveunapprovethreads");
 589              $db->add_column("moderators", "canapproveunapproveattachs", "smallint NOT NULL default '0' AFTER canapproveunapproveposts");
 590              $db->add_column("moderators", "canmanagepolls", "smallint NOT NULL default '0' AFTER canmanagethreads");
 591              $db->add_column("moderators", "canpostclosedthreads", "smallint NOT NULL default '0' AFTER canmanagepolls");
 592              $db->add_column("moderators", "canmanageannouncements", "smallint NOT NULL default '0' AFTER canusecustomtools");
 593              $db->add_column("moderators", "canmanagereportedposts", "smallint NOT NULL default '0' AFTER canmanageannouncements");
 594              $db->add_column("moderators", "canviewmodlog", "smallint NOT NULL default '0' AFTER canmanagereportedposts");
 595              break;
 596          default:
 597              $db->add_column("moderators", "cansoftdeleteposts", "tinyint(1) NOT NULL default '0' AFTER caneditposts");
 598              $db->add_column("moderators", "canrestoreposts", "tinyint(1) NOT NULL default '0' AFTER cansoftdeleteposts");
 599              $db->add_column("moderators", "cansoftdeletethreads", "tinyint(1) NOT NULL default '0' AFTER candeleteposts");
 600              $db->add_column("moderators", "canrestorethreads", "tinyint(1) NOT NULL default '0' AFTER cansoftdeletethreads");
 601              $db->add_column("moderators", "candeletethreads", "tinyint(1) NOT NULL default '0' AFTER canrestorethreads");
 602              $db->add_column("moderators", "canviewunapprove", "tinyint(1) NOT NULL default '0' AFTER canviewips");
 603              $db->add_column("moderators", "canviewdeleted", "tinyint(1) NOT NULL default '0' AFTER canviewunapprove");
 604              $db->add_column("moderators", "canstickunstickthreads", "tinyint(1) NOT NULL default '0' AFTER canopenclosethreads");
 605              $db->add_column("moderators", "canapproveunapprovethreads", "tinyint(1) NOT NULL default '0' AFTER canstickunstickthreads");
 606              $db->add_column("moderators", "canapproveunapproveposts", "tinyint(1) NOT NULL default '0' AFTER canapproveunapprovethreads");
 607              $db->add_column("moderators", "canapproveunapproveattachs", "tinyint(1) NOT NULL default '0' AFTER canapproveunapproveposts");
 608              $db->add_column("moderators", "canmanagepolls", "tinyint(1) NOT NULL default '0' AFTER canmanagethreads");
 609              $db->add_column("moderators", "canpostclosedthreads", "tinyint(1) NOT NULL default '0' AFTER canmanagepolls");
 610              $db->add_column("moderators", "canmanageannouncements", "tinyint(1) NOT NULL default '0' AFTER canusecustomtools");
 611              $db->add_column("moderators", "canmanagereportedposts", "tinyint(1) NOT NULL default '0' AFTER canmanageannouncements");
 612              $db->add_column("moderators", "canviewmodlog", "tinyint(1) NOT NULL default '0' AFTER canmanagereportedposts");
 613              break;
 614      }
 615  
 616      global $footer_extra;
 617      $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
 618  
 619      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
 620      $output->print_footer("30_dbchanges4");
 621  }
 622  
 623  function upgrade30_dbchanges4()
 624  {
 625      global $cache, $output, $mybb, $db;
 626  
 627      $output->print_header("Updating Database");
 628  
 629      echo "<p>Performing necessary upgrade queries...</p>";
 630      flush();
 631  
 632      if($db->field_exists('emailfloodtime', 'usergroups'))
 633      {
 634          $db->drop_column("usergroups", "emailfloodtime");
 635      }
 636  
 637      if($db->field_exists('canmanageannounce', 'usergroups'))
 638      {
 639          $db->drop_column("usergroups", "canmanageannounce");
 640      }
 641  
 642      if($db->field_exists('canmanagemodqueue', 'usergroups'))
 643      {
 644          $db->drop_column("usergroups", "canmanagemodqueue");
 645      }
 646  
 647      if($db->field_exists('canmanagereportedcontent', 'usergroups'))
 648      {
 649          $db->drop_column("usergroups", "canmanagereportedcontent");
 650      }
 651  
 652      if($db->field_exists('canviewmodlogs', 'usergroups'))
 653      {
 654          $db->drop_column("usergroups", "canviewmodlogs");
 655      }
 656  
 657      if($db->field_exists('caneditprofiles', 'usergroups'))
 658      {
 659          $db->drop_column("usergroups", "caneditprofiles");
 660      }
 661  
 662      if($db->field_exists('canbanusers', 'usergroups'))
 663      {
 664          $db->drop_column("usergroups", "canbanusers");
 665      }
 666  
 667      if($db->field_exists('canviewwarnlogs', 'usergroups'))
 668      {
 669          $db->drop_column("usergroups", "canviewwarnlogs");
 670      }
 671  
 672      if($db->field_exists('canuseipsearch', 'usergroups'))
 673      {
 674          $db->drop_column("usergroups", "canuseipsearch");
 675      }
 676  
 677      if($db->field_exists('type', 'maillogs'))
 678      {
 679          $db->drop_column("maillogs", "type");
 680      }
 681  
 682      if($db->field_exists('groups', 'modtools'))
 683      {
 684          $db->drop_column("modtools", "groups");
 685      }
 686  
 687      switch($db->type)
 688      {
 689          case "pgsql":
 690              $db->add_column("usergroups", "emailfloodtime", "int NOT NULL default '5' AFTER maxemails");
 691              $db->add_column("usergroups", "canmanageannounce", "smallint NOT NULL default '0' AFTER showmemberlist");
 692              $db->add_column("usergroups", "canmanagemodqueue", "smallint NOT NULL default '0' AFTER canmanageannounce");
 693              $db->add_column("usergroups", "canmanagereportedcontent", "smallint NOT NULL default '0' AFTER canmanagemodqueue");
 694              $db->add_column("usergroups", "canviewmodlogs", "smallint NOT NULL default '0' AFTER canmanagereportedcontent");
 695              $db->add_column("usergroups", "caneditprofiles", "smallint NOT NULL default '0' AFTER canviewmodlogs");
 696              $db->add_column("usergroups", "canbanusers", "smallint NOT NULL default '0' AFTER caneditprofiles");
 697              $db->add_column("usergroups", "canviewwarnlogs", "smallint NOT NULL default '0' AFTER canbanusers");
 698              $db->add_column("usergroups", "canuseipsearch", "smallint NOT NULL default '0' AFTER canviewwarnlogs");
 699              $db->add_column("maillogs", "type", "smallint NOT NULL default '0'");
 700              break;
 701          default:
 702              $db->add_column("usergroups", "emailfloodtime", "int(3) NOT NULL default '5' AFTER maxemails");
 703              $db->add_column("usergroups", "canmanageannounce", "tinyint(1) NOT NULL default '0' AFTER showmemberlist");
 704              $db->add_column("usergroups", "canmanagemodqueue", "tinyint(1) NOT NULL default '0' AFTER canmanageannounce");
 705              $db->add_column("usergroups", "canmanagereportedcontent", "tinyint(1) NOT NULL default '0' AFTER canmanagemodqueue");
 706              $db->add_column("usergroups", "canviewmodlogs", "tinyint(1) NOT NULL default '0' AFTER canmanagereportedcontent");
 707              $db->add_column("usergroups", "caneditprofiles", "tinyint(1) NOT NULL default '0' AFTER canviewmodlogs");
 708              $db->add_column("usergroups", "canbanusers", "tinyint(1) NOT NULL default '0' AFTER caneditprofiles");
 709              $db->add_column("usergroups", "canviewwarnlogs", "tinyint(1) NOT NULL default '0' AFTER canbanusers");
 710              $db->add_column("usergroups", "canuseipsearch", "tinyint(1) NOT NULL default '0' AFTER canviewwarnlogs");
 711              $db->add_column("maillogs", "type", "tinyint(1) NOT NULL default '0'");
 712              break;
 713      }
 714  
 715      switch($db->type)
 716      {
 717          case "sqlite":
 718              $db->add_column("modtools", "groups", "text NOT NULL default ''");
 719              break;
 720          default:
 721              $db->add_column("modtools", "groups", "text NOT NULL");
 722              break;
 723      }
 724  
 725      $update_array = array(
 726          "canmanageannounce" => 1,
 727          "canmanagemodqueue" => 1,
 728          "canmanagereportedcontent" => 1,
 729          "canviewmodlogs" => 1,
 730          "caneditprofiles" => 1,
 731          "canbanusers" => 1,
 732          "canviewwarnlogs" => 1,
 733          "canuseipsearch" => 1
 734      );
 735      $db->update_query("usergroups", $update_array, "canmodcp= '1'");
 736  
 737      $update_array = array(
 738          "type" => 1
 739      );
 740      $db->update_query("maillogs", $update_array, "tid= '0'");
 741  
 742      $update_array = array(
 743          "type" => 2
 744      );
 745      $db->update_query("maillogs", $update_array, "tid > '0'");
 746  
 747      global $footer_extra;
 748      $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
 749  
 750      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
 751      $output->print_footer("30_dbchanges5");
 752  }
 753  
 754  function upgrade30_dbchanges5()
 755  {
 756      global $cache, $output, $mybb, $db;
 757  
 758      $output->print_header("Updating Database");
 759  
 760      echo "<p>Performing necessary upgrade queries...</p>";
 761      flush();
 762  
 763      if($db->table_exists("questions"))
 764      {
 765          $db->drop_table("questions");
 766      }
 767  
 768      if($db->table_exists("questionsessions"))
 769      {
 770          $db->drop_table("questionsessions");
 771      }
 772  
 773      if($db->table_exists("spamlog"))
 774      {
 775          $db->drop_table("spamlog");
 776      }
 777  
 778      $collation = $db->build_create_table_collation();
 779  
 780      switch($db->type)
 781      {
 782          case "sqlite":
 783              $db->write_query("CREATE TABLE ".TABLE_PREFIX."questions (
 784                  qid INTEGER PRIMARY KEY,
 785                  question varchar(200) NOT NULL default '',
 786                  answer varchar(150) NOT NULL default '',
 787                  shown int unsigned NOT NULL default 0,
 788                  correct int unsigned NOT NULL default 0,
 789                  incorrect int unsigned NOT NULL default 0,
 790                  active tinyint(1) NOT NULL default '0'
 791              );");
 792              $db->write_query("CREATE TABLE ".TABLE_PREFIX."questionsessions (
 793                  sid varchar(32) NOT NULL default '',
 794                  qid int unsigned NOT NULL default '0',
 795                  dateline int unsigned NOT NULL default '0'
 796              );");
 797              $db->write_query("CREATE TABLE ".TABLE_PREFIX."spamlog (
 798                  sid INTEGER PRIMARY KEY,
 799                  username varchar(120) NOT NULL DEFAULT '',
 800                  email varchar(220) NOT NULL DEFAULT '',
 801                  ipaddress blob(16) NOT NULL default '',
 802                  dateline int unsigned NOT NULL default '0',
 803                  data TEXT NOT NULL
 804              );");
 805              break;
 806          case "pgsql":
 807              $db->write_query("CREATE TABLE ".TABLE_PREFIX."questions (
 808                  qid serial,
 809                  question varchar(200) NOT NULL default '',
 810                  answer varchar(150) NOT NULL default '',
 811                  shown int NOT NULL default 0,
 812                  correct int NOT NULL default 0,
 813                  incorrect int NOT NULL default 0,
 814                  active smallint NOT NULL default '0',
 815                  PRIMARY KEY (qid)
 816              );");
 817              $db->write_query("CREATE TABLE ".TABLE_PREFIX."questionsessions (
 818                  sid varchar(32) NOT NULL default '',
 819                  qid int NOT NULL default '0',
 820                  dateline int NOT NULL default '0',
 821                  UNIQUE (sid)
 822              );");
 823              $db->write_query("CREATE TABLE ".TABLE_PREFIX."spamlog (
 824                  sid serial,
 825                  username varchar(120) NOT NULL DEFAULT '',
 826                  email varchar(220) NOT NULL DEFAULT '',
 827                  ipaddress bytea NOT NULL default '',
 828                  dateline numeric(30,0) NOT NULL default '0',
 829                  data text NOT NULL default '',
 830                  PRIMARY KEY (sid)
 831              );");
 832              break;
 833          default:
 834              $db->write_query("CREATE TABLE ".TABLE_PREFIX."questions (
 835                  qid int unsigned NOT NULL auto_increment,
 836                  question varchar(200) NOT NULL default '',
 837                  answer varchar(150) NOT NULL default '',
 838                  shown int unsigned NOT NULL default 0,
 839                  correct int unsigned NOT NULL default 0,
 840                  incorrect int unsigned NOT NULL default 0,
 841                  active tinyint(1) NOT NULL default '0',
 842                  PRIMARY KEY (qid)
 843              ) ENGINE=MyISAM{$collation}");
 844              $db->write_query("CREATE TABLE ".TABLE_PREFIX."questionsessions (
 845                  sid varchar(32) NOT NULL default '',
 846                  qid int unsigned NOT NULL default '0',
 847                  dateline int unsigned NOT NULL default '0',
 848                  PRIMARY KEY (sid)
 849              ) ENGINE=MyISAM{$collation}");
 850              $db->write_query("CREATE TABLE ".TABLE_PREFIX."spamlog (
 851                  sid int unsigned NOT NULL auto_increment,
 852                  username varchar(120) NOT NULL DEFAULT '',
 853                  email varchar(220) NOT NULL DEFAULT '',
 854                  ipaddress varbinary(16) NOT NULL default '',
 855                  dateline int unsigned NOT NULL default '0',
 856                  data text NOT NULL,
 857                  PRIMARY KEY (sid)
 858              ) ENGINE=MyISAM{$collation}");
 859      }
 860  
 861      global $footer_extra;
 862      $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
 863  
 864      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
 865      $output->print_footer("30_dbchanges6");
 866  }
 867  
 868  function upgrade30_dbchanges6()
 869  {
 870      global $cache, $output, $mybb, $db;
 871  
 872      $output->print_header("Updating Database");
 873  
 874      echo "<p>Performing necessary upgrade queries...</p>";
 875      flush();
 876  
 877      if($db->table_exists("buddyrequests"))
 878      {
 879          $db->drop_table("buddyrequests");
 880      }
 881  
 882      $collation = $db->build_create_table_collation();
 883  
 884      switch($db->type)
 885      {
 886          case "pgsql":
 887              $db->write_query("CREATE TABLE ".TABLE_PREFIX."buddyrequests (
 888                   id serial,
 889                   uid int NOT NULL,
 890                   touid int NOT NULL,
 891                   date int NOT NULL,
 892                   PRIMARY KEY (id)
 893              );");
 894              break;
 895          case "sqlite":
 896              $db->write_query("CREATE TABLE ".TABLE_PREFIX."buddyrequests (
 897                   id INTEGER PRIMARY KEY,
 898                   uid bigint unsigned NOT NULL,
 899                   touid bigint unsigned NOT NULL,
 900                   date int unsigned NOT NULL
 901              );");
 902              break;
 903          default:
 904              $db->write_query("CREATE TABLE ".TABLE_PREFIX."buddyrequests (
 905                   id int(10) UNSIGNED NOT NULL auto_increment,
 906                   uid bigint(30) UNSIGNED NOT NULL,
 907                   touid bigint(30) UNSIGNED NOT NULL,
 908                   date int(11) UNSIGNED NOT NULL,
 909                   KEY (uid),
 910                   KEY (touid),
 911                   PRIMARY KEY (id)
 912              ) ENGINE=MyISAM{$collation};");
 913              break;
 914      }
 915  
 916      if($db->field_exists('msn', 'users'))
 917      {
 918          $db->drop_column("users", "msn");
 919      }
 920  
 921      if($db->field_exists('postbit', 'profilefields'))
 922      {
 923          $db->drop_column("profilefields", "postbit");
 924      }
 925  
 926      if($db->field_exists('skype', 'users'))
 927      {
 928          $db->drop_column("users", "skype");
 929      }
 930  
 931      if($db->field_exists('google', 'users'))
 932      {
 933          $db->drop_column("users", "google");
 934      }
 935  
 936      if($db->field_exists('cplanguage', 'adminoptions'))
 937      {
 938          $db->drop_column("adminoptions", "cplanguage");
 939      }
 940  
 941      if($db->field_exists('showimages', 'users'))
 942      {
 943          $db->drop_column("users", "showimages");
 944      }
 945  
 946      if($db->field_exists('showvideos', 'users'))
 947      {
 948          $db->drop_column("users", "showvideos");
 949      }
 950  
 951      if($db->field_exists('caninvitemembers', 'groupleaders'))
 952      {
 953          $db->drop_column("groupleaders", "caninvitemembers");
 954      }
 955  
 956      if($db->field_exists('invite', 'joinrequests'))
 957      {
 958          $db->drop_column("joinrequests", "invite");
 959      }
 960  
 961      if($db->field_exists('registration', 'profilefields'))
 962      {
 963          $db->drop_column("profilefields", "registration");
 964      }
 965  
 966      if($db->field_exists('validated', 'awaitingactivation'))
 967      {
 968          $db->drop_column("awaitingactivation", "validated");
 969      }
 970  
 971      if($db->field_exists('sourceeditor', 'users'))
 972      {
 973          $db->drop_column("users", "sourceeditor");
 974      }
 975  
 976      if($db->field_exists('buddyrequestspm', 'users'))
 977      {
 978          $db->drop_column("users", "buddyrequestspm");
 979      }
 980  
 981      if($db->field_exists('buddyrequestsauto', 'users'))
 982      {
 983          $db->drop_column("users", "buddyrequestsauto");
 984      }
 985  
 986      if($db->field_exists('ipaddress', 'privatemessages'))
 987      {
 988          $db->drop_column("privatemessages", "ipaddress");
 989      }
 990  
 991      if($db->field_exists('maxoptions', 'polls'))
 992      {
 993          $db->drop_column("polls", "maxoptions");
 994      }
 995  
 996      switch($db->type)
 997      {
 998          case "pgsql":
 999              $db->add_column("profilefields", "postbit", "smallint NOT NULL default '0' AFTER profile");
1000              $db->add_column("users", "skype", "varchar(75) NOT NULL default '' AFTER yahoo");
1001              $db->add_column("users", "google", "varchar(75) NOT NULL default '' AFTER skype");
1002              $db->add_column("adminoptions", "cplanguage", "varchar(50) NOT NULL default '' AFTER cpstyle");
1003              $db->add_column("users", "showimages", "smallint NOT NULL default '1' AFTER threadmode");
1004              $db->add_column("users", "showvideos", "smallint NOT NULL default '1' AFTER showimages");
1005              $db->add_column("users", "buddyrequestspm", "smallint NOT NULL default '1' AFTER pmnotify");
1006              $db->add_column("users", "buddyrequestsauto", "smallint NOT NULL default '0' AFTER buddyrequestspm");
1007              $db->add_column("groupleaders", "caninvitemembers", "smallint NOT NULL default '0'");
1008              $db->add_column("joinrequests", "invite", "smallint NOT NULL default '0'");
1009              $db->add_column("profilefields", "registration", "smallint NOT NULL default '0' AFTER required");
1010              $db->add_column("awaitingactivation", "validated", "smallint NOT NULL default '0' AFTER type");
1011              $db->add_column("users", "sourceeditor", "smallint NOT NULL default '0'");
1012              break;
1013          default:
1014              $db->add_column("profilefields", "postbit", "tinyint(1) NOT NULL default '0' AFTER profile");
1015              $db->add_column("users", "skype", "varchar(75) NOT NULL default '' AFTER yahoo");
1016              $db->add_column("users", "google", "varchar(75) NOT NULL default '' AFTER skype");
1017              $db->add_column("adminoptions", "cplanguage", "varchar(50) NOT NULL default '' AFTER cpstyle");
1018              $db->add_column("users", "showimages", "tinyint(1) NOT NULL default '1' AFTER threadmode");
1019              $db->add_column("users", "showvideos", "tinyint(1) NOT NULL default '1' AFTER showimages");
1020              $db->add_column("users", "buddyrequestspm", "tinyint(1) NOT NULL default '1' AFTER pmnotify");
1021              $db->add_column("users", "buddyrequestsauto", "tinyint(1) NOT NULL default '0' AFTER buddyrequestspm");
1022              $db->add_column("groupleaders", "caninvitemembers", "tinyint(1) NOT NULL default '0'");
1023              $db->add_column("joinrequests", "invite", "tinyint(1) NOT NULL default '0'");
1024              $db->add_column("profilefields", "registration", "tinyint(1) NOT NULL default '0' AFTER required");
1025              $db->add_column("awaitingactivation", "validated", "tinyint(1) NOT NULL default '0' AFTER type");
1026              $db->add_column("users", "sourceeditor", "tinyint(1) NOT NULL default '0'");
1027              break;
1028      }
1029  
1030      switch($db->type)
1031      {
1032          case "pgsql":
1033              $db->add_column("privatemessages", "ipaddress", "bytea NOT NULL default ''");
1034              $db->add_column("polls", "maxoptions", "smallint NOT NULL default '0'");
1035              break;
1036          case "sqlite":
1037              $db->add_column("privatemessages", "ipaddress", "blob(16) NOT NULL default ''");
1038              $db->add_column("polls", "maxoptions", "smallint NOT NULL default '0'");
1039              break;
1040          default:
1041              $db->add_column("privatemessages", "ipaddress", "varbinary(16) NOT NULL default ''");
1042              $db->add_column("polls", "maxoptions", "smallint unsigned NOT NULL default '0'");
1043              break;
1044      }
1045  
1046      $groups = range(1, 39);
1047  
1048      $sql = implode(',', $groups);
1049      $db->update_query("templategroups", array('isdefault' => 1), "gid IN ({$sql})");
1050  
1051      if($db->table_exists('reportedposts'))
1052      {
1053          $db->update_query("reportedposts", array('type' => 'post'));
1054      }
1055  
1056      $db->insert_query("questions", array('question' => 'What does 2 + 2 equal?', 'answer' => '4\nFour', 'active' => '1'));
1057  
1058      $query = $db->simple_select("attachtypes", "COUNT(*) as numexists", "extension='psd'");
1059      if($db->fetch_field($query, "numexists") == 0)
1060      {
1061          $db->insert_query("attachtypes", array('name' => "Adobe Photoshop File", 'mimetype' => 'application/x-photoshop', 'extension' => "psd", 'maxsize' => '1024', 'icon' => 'images/attachtypes/psd.png'));
1062      }
1063      // SQLite... As we modify tables below we need to close all cursors before...
1064      if($db->type == "sqlite")
1065      {
1066          $query->closeCursor();
1067      }
1068  
1069      $query = $db->simple_select("templategroups", "COUNT(*) as numexists", "prefix='video'");
1070      if($db->fetch_field($query, "numexists") == 0)
1071      {
1072          $db->insert_query("templategroups", array('prefix' => 'video', 'title' => '<lang:group_video>', 'isdefault' => '1'));
1073      }
1074      // SQLite... As we modify tables below we need to close all cursors before...
1075      if($db->type == "sqlite")
1076      {
1077          $query->closeCursor();
1078      }
1079  
1080      $query = $db->simple_select("templategroups", "COUNT(*) as numexists", "prefix='php'");
1081      if($db->fetch_field($query, "numexists") != 0)
1082      {
1083          $db->update_query("templategroups", array('prefix' => 'announcement', 'title' => '<lang:group_announcement>'), "prefix='php'");
1084      }
1085      // SQLite... As we modify tables below we need to close all cursors before...
1086      if($db->type == "sqlite")
1087      {
1088          $query->closeCursor();
1089      }
1090  
1091      $query = $db->simple_select("templategroups", "COUNT(*) as numexists", "prefix='redirect'");
1092      if($db->fetch_field($query, "numexists") != 0)
1093      {
1094          $db->update_query("templategroups", array('prefix' => 'posticons', 'title' => '<lang:group_posticons>'), "prefix='redirect'");
1095      }
1096      // SQLite... As we modify tables below we need to close all cursors before...
1097      if($db->type == "sqlite")
1098      {
1099          $query->closeCursor();
1100      }
1101  
1102      // Sync usergroups with canbereported; no moderators or banned groups
1103      echo "<p>Updating usergroup permissions...</p>";
1104      $groups = array();
1105      $usergroups = $cache->read('usergroups');
1106  
1107      foreach($usergroups as $group)
1108      {
1109          if($group['canmodcp'] || $group['isbannedgroup'])
1110          {
1111              continue;
1112          }
1113  
1114          $groups[] = "'{$group['gid']}'";
1115      }
1116  
1117      if(!empty($groups))
1118      {
1119          $usergroups = implode(',', $groups);
1120          $db->update_query('usergroups', array('canbereported' => 1), "gid IN ({$usergroups})");
1121      }
1122  
1123      $db->update_query('usergroups', array('canviewboardclosed' => 1), 'cancp = 1');
1124  
1125      if($db->table_exists('reportedposts'))
1126      {
1127          if($db->field_exists("pid", "reportedposts") && !$db->field_exists("id", "reportedposts"))
1128          {
1129              switch($db->type)
1130              {
1131                  case "pgsql":
1132                      $db->rename_column("reportedposts", "pid", "id", "int", true, "'0'");
1133                      break;
1134                  default:
1135                      $db->rename_column("reportedposts", "pid", "id", "int unsigned NOT NULL default '0'");
1136              }
1137          }
1138  
1139          if($db->field_exists("tid", "reportedposts") && !$db->field_exists("id2", "reportedposts"))
1140          {
1141              switch($db->type)
1142              {
1143                  case "pgsql":
1144                      $db->rename_column("reportedposts", "tid", "id2", "int", true, "'0'");
1145                      break;
1146                  default:
1147                      $db->rename_column("reportedposts", "tid", "id2", "int unsigned NOT NULL default '0'");
1148              }
1149          }
1150  
1151          if($db->field_exists("fid", "reportedposts") && !$db->field_exists("id3", "reportedposts"))
1152          {
1153              switch($db->type)
1154              {
1155                  case "pgsql":
1156                      $db->rename_column("reportedposts", "fid", "id3", "int", true, "'0'");
1157                      break;
1158                  default:
1159                      $db->rename_column("reportedposts", "fid", "id3", "int unsigned NOT NULL default '0'");
1160              }
1161          }
1162      }
1163  
1164      if($db->table_exists('reportedposts'))
1165      {
1166          if($db->table_exists("reportedcontent"))
1167          {
1168              $db->drop_table("reportedcontent");
1169          }
1170  
1171          $db->rename_table("reportedposts", "reportedcontent");
1172  
1173          $cache->delete('reportedposts');
1174      }
1175  
1176      $db->update_query("settings", array('optionscode' => 'select\r\n0=No CAPTCHA\r\n1=MyBB Default CAPTCHA\r\n2=reCAPTCHA\r\n3=Are You a Human'), "name='captchaimage'");
1177      $db->update_query("settings", array('optionscode' => 'select\r\ninstant=Instant Activation\r\nverify=Send Email Verification\r\nrandompass=Send Random Password\r\nadmin=Administrator Activation\r\nboth=Email Verification & Administrator Activation'), "name='regtype'");
1178      $db->update_query("settings", array('optionscode' => $db->escape_string('php
1179  <select name=\"upsetting[{$setting[\'name\']}]\">
1180  <option value=\"-12\" ".($setting[\'value\'] == -12?"selected=\"selected\"":"").">GMT -12:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -12).")</option>
1181  <option value=\"-11\" ".($setting[\'value\'] == -11?"selected=\"selected\"":"").">GMT -11:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -11).")</option>
1182  <option value=\"-10\" ".($setting[\'value\'] == -10?"selected=\"selected\"":"").">GMT -10:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -10).")</option>
1183  <option value=\"-9.5\" ".($setting[\'value\'] == -9.5?"selected=\"selected\"":"").">GMT -9:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -9.5).")</option>
1184  <option value=\"-9\" ".($setting[\'value\'] == -9?"selected=\"selected\"":"").">GMT -9:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -9).")</option>
1185  <option value=\"-8\" ".($setting[\'value\'] == -8?"selected=\"selected\"":"").">GMT -8:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -8).")</option>
1186  <option value=\"-7\" ".($setting[\'value\'] == -7?"selected=\"selected\"":"").">GMT -7:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -7).")</option>
1187  <option value=\"-6\" ".($setting[\'value\'] == -6?"selected=\"selected\"":"").">GMT -6:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -6).")</option>
1188  <option value=\"-5\" ".($setting[\'value\'] == -5?"selected=\"selected\"":"").">GMT -5:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -5).")</option>
1189  <option value=\"-4.5\" ".($setting[\'value\'] == -4.5?"selected=\"selected\"":"").">GMT -4:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -4.5).")</option>
1190  <option value=\"-4\" ".($setting[\'value\'] == -4?"selected=\"selected\"":"").">GMT -4:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -4).")</option>
1191  <option value=\"-3.5\" ".($setting[\'value\'] == -3.5?"selected=\"selected\"":"").">GMT -3:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -3.5).")</option>
1192  <option value=\"-3\" ".($setting[\'value\'] == -3?"selected=\"selected\"":"").">GMT -3:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -3).")</option>
1193  <option value=\"-2\" ".($setting[\'value\'] == -2?"selected=\"selected\"":"").">GMT -2:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -2).")</option>
1194  <option value=\"-1\" ".($setting[\'value\'] == -1?"selected=\"selected\"":"").">GMT -1:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, -1).")</option>
1195  <option value=\"0\" ".($setting[\'value\'] == 0?"selected=\"selected\"":"").">GMT (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 0).")</option>
1196  <option value=\"+1\" ".($setting[\'value\'] == 1?"selected=\"selected\"":"").">GMT +1:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 1).")</option>
1197  <option value=\"+2\" ".($setting[\'value\'] == 2?"selected=\"selected\"":"").">GMT +2:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 2).")</option>
1198  <option value=\"+3\" ".($setting[\'value\'] == 3?"selected=\"selected\"":"").">GMT +3:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 3).")</option>
1199  <option value=\"+3.5\" ".($setting[\'value\'] == 3.5?"selected=\"selected\"":"").">GMT +3:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 3.5).")</option>
1200  <option value=\"+4\" ".($setting[\'value\'] == 4?"selected=\"selected\"":"").">GMT +4:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 4).")</option>
1201  <option value=\"+4.5\" ".($setting[\'value\'] == 4.5?"selected=\"selected\"":"").">GMT +4:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 4.5).")</option>
1202  <option value=\"+5\" ".($setting[\'value\'] == 5?"selected=\"selected\"":"").">GMT +5:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 5).")</option>
1203  <option value=\"+5.5\" ".($setting[\'value\'] == 5.5?"selected=\"selected\"":"").">GMT +5:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 5.5).")</option>
1204  <option value=\"+5.75\" ".($setting[\'value\'] == 5.75?"selected=\"selected\"":"").">GMT +5:45 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 5.75).")</option>
1205  <option value=\"+6\" ".($setting[\'value\'] == 6?"selected=\"selected\"":"").">GMT +6:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 6).")</option>
1206  <option value=\"+6.5\" ".($setting[\'value\'] == 6.5?"selected=\"selected\"":"").">GMT +6:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 6.5).")</option>
1207  <option value=\"+7\" ".($setting[\'value\'] == 7?"selected=\"selected\"":"").">GMT +7:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 7).")</option>
1208  <option value=\"+8\" ".($setting[\'value\'] == 8?"selected=\"selected\"":"").">GMT +8:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 8).")</option>
1209  <option value=\"+9\" ".($setting[\'value\'] == 9?"selected=\"selected\"":"").">GMT +9:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 9).")</option>
1210  <option value=\"+9.5\" ".($setting[\'value\'] == 9.5?"selected=\"selected\"":"").">GMT +9:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 9.5).")</option>
1211  <option value=\"+10\" ".($setting[\'value\'] == 10?"selected=\"selected\"":"").">GMT +10:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 10).")</option>
1212  <option value=\"+10.5\" ".($setting[\'value\'] == 10.5?"selected=\"selected\"":"").">GMT +10:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 10.5).")</option>
1213  <option value=\"+11\" ".($setting[\'value\'] == 11?"selected=\"selected\"":"").">GMT +11:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 11).")</option>
1214  <option value=\"+11.5\" ".($setting[\'value\'] == 11.5?"selected=\"selected\"":"").">GMT +11:30 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 11.5).")</option>
1215  <option value=\"+12\" ".($setting[\'value\'] == 12?"selected=\"selected\"":"").">GMT +12:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 12).")</option>
1216  <option value=\"+12.75\" ".($setting[\'value\'] == 12.75?"selected=\"selected\"":"").">GMT +12:45 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 12.75).")</option>
1217  <option value=\"+13\" ".($setting[\'value\'] == 13?"selected=\"selected\"":"").">GMT +13:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 13).")</option>
1218  <option value=\"+14\" ".($setting[\'value\'] == 14?"selected=\"selected\"":"").">GMT +14:00 Hours (".my_date($mybb->settings[\'timeformat\'], TIME_NOW, 14).")</option>
1219  </select>')), "name='timezoneoffset'");
1220  
1221      // Update tasks
1222      $added_tasks = sync_tasks();
1223  
1224      // For the version check task, set a random date and hour (so all MyBB installs don't query mybb.com all at the same time)
1225      $update_array = array(
1226          'hour' => rand(0, 23),
1227          'weekday' => rand(0, 6)
1228      );
1229  
1230      $db->update_query("tasks", $update_array, "file = 'versioncheck'");
1231  
1232      echo "<p>Added {$added_tasks} new tasks.</p>";
1233  
1234      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
1235      $output->print_footer("30_threadcount");
1236  }
1237  
1238  function upgrade30_threadcount()
1239  {
1240      global $db, $output;
1241  
1242      $output->print_header("Counting user thread count");
1243  
1244      if(!$_POST['theadspage'])
1245      {
1246          $threads = 500;
1247      }
1248      else
1249      {
1250          $threads = (int)$_POST['theadspage'];
1251      }
1252  
1253      if($_POST['threadstart'])
1254      {
1255          $startat = (int)$_POST['threadstart'];
1256          $upper = $startat+$threads;
1257          $lower = $startat;
1258      }
1259      else
1260      {
1261          $startat = 0;
1262          $upper = $threads;
1263          $lower = 0;
1264      }
1265  
1266      $query = $db->simple_select("users", "COUNT(uid) AS usercount");
1267      $cnt = $db->fetch_array($query);
1268  
1269      if($upper > $cnt['usercount'])
1270      {
1271          $upper = $cnt['usercount'];
1272      }
1273  
1274      echo "<p>Counting thread count of user #{$lower} to #{$upper} ({$cnt['usercount']} Total)</p>";
1275      flush();
1276  
1277      $threadnum = false;
1278  
1279      $query = $db->simple_select("users", "threadnum, uid", "", array('limit_start' => $lower, 'limit' => $threads));
1280      while($thread = $db->fetch_array($query))
1281      {
1282          $query2 = $db->simple_select("threads", "COUNT(tid) AS thread_count", "uid='{$thread['uid']}' AND visible = 1");
1283          $num_threads = $db->fetch_field($query2, "thread_count");
1284  
1285          $db->update_query("users", array('threadnum' => $num_threads), "uid = '{$thread['uid']}'");
1286  
1287          $threadnum = true;
1288      }
1289  
1290      $remaining = $upper-$cnt['usercount'];
1291      if($remaining && $threadnum)
1292      {
1293          $nextact = "30_threadcount";
1294          $startat = $startat+$threads;
1295          $contents = "<p><input type=\"hidden\" name=\"theadspage\" value=\"$threads\" /><input type=\"hidden\" name=\"threadstart\" value=\"$startat\" />Done. Click Next to move on to the next set of thread counts.</p>";
1296      }
1297      else
1298      {
1299          $nextact = "30_dbchanges_optimize1";
1300          $contents = "<p>Done</p><p>All users have had their thread count counted. Click next to continue.</p>";
1301      }
1302      $output->print_contents($contents);
1303  
1304      global $footer_extra;
1305      $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
1306  
1307      $output->print_footer($nextact);
1308  }
1309  
1310  function upgrade30_dbchanges_optimize1()
1311  {
1312      global $output, $mybb, $db;
1313  
1314      $output->print_header("Optimizing Database");
1315  
1316      echo "<p>Performing necessary optimization queries...</p>";
1317      flush();
1318  
1319      switch($db->type)
1320      {
1321          case "pgsql":
1322              $db->modify_column("adminoptions", "loginattempts", "smallint", "set", "'0'");
1323              $db->modify_column("adminviews", "perpage", "smallint", "set", "'0'");
1324              $db->modify_column("calendars", "disporder", "smallint", "set", "'0'");
1325              $db->modify_column("calendars", "eventlimit", "smallint", "set", "'0'");
1326              $db->modify_column("events", "timezone", "varchar(5)", "set", "''");
1327              $db->modify_column("forums", "lastposttid", "int", "set", "'0'");
1328              $db->modify_column("mailerrors", "smtpcode", "smallint", "set", "'0'");
1329              $db->modify_column("maillogs", "touid", "int", "set", "'0'");
1330              $db->write_query("ALTER TABLE ".TABLE_PREFIX."polls ALTER COLUMN numvotes DROP DEFAULT"); // We need to drop the default first as PostgreSQL can't cast default values
1331              $db->modify_column("polls", "numvotes", "int USING (trim(numvotes)::int)", "set", "'0'");
1332              $db->modify_column("profilefields", "postnum", "smallint", "set", "'0'");
1333              $db->modify_column("reputation", "reputation", "smallint", "set", "'0'");
1334              $db->modify_column("spiders", "theme", "smallint", "set", "'0'");
1335              $db->modify_column("spiders", "usergroup", "smallint", "set", "'0'");
1336              $db->modify_column("templates", "sid", "smallint", "set", "'0'");
1337              $db->modify_column("themestylesheets", "tid", "smallint", "set", "'0'");
1338              $db->modify_column("usergroups", "canusesigxposts", "smallint", "set", "'0'");
1339              $db->modify_column("users", "timezone", "varchar(5)", "set", "''");
1340              $db->modify_column("users", "reputation", "int", "set", "'0'");
1341              $db->modify_column("warninglevels", "percentage", "smallint", "set", "'0'");
1342              $db->modify_column("warningtypes", "points", "smallint", "set", "'0'");
1343              $db->modify_column("warnings", "points", "smallint", "set", "'0'");
1344              break;
1345          case "sqlite":
1346              $db->modify_column("adminoptions", "loginattempts", "smallint NOT NULL default '0'");
1347              $db->modify_column("adminviews", "perpage", "smallint NOT NULL default '0'");
1348              $db->modify_column("calendars", "disporder", "smallint NOT NULL default '0'");
1349              $db->modify_column("calendars", "eventlimit", "smallint NOT NULL default '0'");
1350              $db->modify_column("events", "timezone", "varchar(5) NOT NULL default ''");
1351              $db->modify_column("forums", "lastposttid", "int NOT NULL default '0'");
1352              $db->modify_column("mailerrors", "smtpcode", "smallint NOT NULL default '0'");
1353              $db->modify_column("maillogs", "touid", "int NOT NULL default '0'");
1354              $db->modify_column("polls", "numvotes", "int NOT NULL default '0'");
1355              $db->modify_column("profilefields", "postnum", "smallint NOT NULL default '0'");
1356              $db->modify_column("reputation", "reputation", "smallint NOT NULL default '0'");
1357              $db->modify_column("spiders", "theme", "smallint NOT NULL default '0'");
1358              $db->modify_column("spiders", "usergroup", "smallint NOT NULL default '0'");
1359              $db->modify_column("templates", "sid", "smallint NOT NULL default '0'");
1360              $db->modify_column("themestylesheets", "tid", "smallint NOT NULL default '0'");
1361              $db->modify_column("usergroups", "canusesigxposts", "smallint NOT NULL default '0'");
1362              $db->modify_column("users", "timezone", "varchar(5) NOT NULL default ''");
1363              $db->modify_column("users", "reputation", "int NOT NULL default '0'");
1364              $db->modify_column("warninglevels", "percentage", "smallint NOT NULL default '0'");
1365              $db->modify_column("warningtypes", "points", "smallint NOT NULL default '0'");
1366              $db->modify_column("warnings", "points", "smallint NOT NULL default '0'");
1367              break;
1368          default:
1369              $db->modify_column("adminoptions", "loginattempts", "smallint unsigned NOT NULL default '0'");
1370              $db->modify_column("adminviews", "perpage", "smallint(4) NOT NULL default '0'");
1371              $db->modify_column("calendars", "disporder", "smallint unsigned NOT NULL default '0'");
1372              $db->modify_column("calendars", "eventlimit", "smallint(3) NOT NULL default '0'");
1373              $db->modify_column("events", "timezone", "varchar(5) NOT NULL default ''");
1374              $db->modify_column("forums", "lastposttid", "int unsigned NOT NULL default '0'");
1375              $db->modify_column("mailerrors", "smtpcode", "smallint(5) unsigned NOT NULL default '0'");
1376              $db->modify_column("maillogs", "touid", "int unsigned NOT NULL default '0'");
1377              $db->modify_column("polls", "numvotes", "int unsigned NOT NULL default '0'");
1378              $db->modify_column("profilefields", "postnum", "smallint unsigned NOT NULL default '0'");
1379              $db->modify_column("reputation", "reputation", "smallint NOT NULL default '0'");
1380              $db->modify_column("spiders", "theme", "smallint unsigned NOT NULL default '0'");
1381              $db->modify_column("spiders", "usergroup", "smallint unsigned NOT NULL default '0'");
1382              $db->modify_column("templates", "sid", "smallint NOT NULL default '0'");
1383              $db->modify_column("themestylesheets", "tid", "smallint unsigned NOT NULL default '0'");
1384              $db->modify_column("usergroups", "canusesigxposts", "smallint unsigned NOT NULL default '0'");
1385              $db->modify_column("users", "timezone", "varchar(5) NOT NULL default ''");
1386              $db->modify_column("users", "reputation", "int NOT NULL default '0'");
1387              $db->modify_column("warninglevels", "percentage", "smallint(3) NOT NULL default '0'");
1388              $db->modify_column("warningtypes", "points", "smallint unsigned NOT NULL default '0'");
1389              $db->modify_column("warnings", "points", "smallint unsigned NOT NULL default '0'");
1390              break;
1391      }
1392  
1393      if($db->type != "pgsql")
1394      {
1395          // PgSQL doesn't support longtext
1396          if($db->type == "sqlite")
1397          {
1398              // And SQLite doesn't like text columns without a default value...
1399              $db->modify_column("themestylesheets", "stylesheet", "longtext NOT NULL default ''");
1400          }
1401          else
1402          {
1403              // ...while MySQL hates text columns with a default value
1404              $db->modify_column("themestylesheets", "stylesheet", "longtext NOT NULL");
1405          }
1406      }
1407  
1408      global $footer_extra;
1409      $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
1410  
1411      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
1412      $output->print_footer("30_dbchanges_optimize2");
1413  }
1414  
1415  function upgrade30_dbchanges_optimize2()
1416  {
1417      global $output, $mybb, $db;
1418  
1419      $output->print_header("Optimizing Database");
1420  
1421      echo "<p>Performing necessary optimization queries...</p>";
1422      echo "<p>Adding indexes to tables...</p>";
1423      flush();
1424  
1425      if($db->index_exists('sessions', 'location1'))
1426      {
1427          $db->drop_index('sessions', 'location1');
1428      }
1429  
1430      if($db->index_exists('sessions', 'location2'))
1431      {
1432          $db->drop_index('sessions', 'location2');
1433      }
1434  
1435      if($db->type == "mysql" || $db->type == "mysqli")
1436      {
1437          $update_data = array(
1438              'adminlog' => 'uid',
1439              'banfilters' => 'type',
1440              'events' => 'cid',
1441              'forumsubscriptions' => 'uid',
1442              'moderatorlog' => array('uid', 'fid'),
1443              'polls' => 'tid',
1444              'reportedcontent' => 'reportstatus',
1445              'settings' => 'gid',
1446              'themestylesheets' => 'tid',
1447              'warnings' => 'uid',
1448              'forumpermissions' => array('fid' => array('fid', 'gid')),
1449              'sessions' => array('location' => array('location1', 'location2')),
1450              'templates' => array('sid' => array('sid', 'title'))
1451          );
1452  
1453          foreach($update_data as $table => $index)
1454          {
1455              if(!is_array($index))
1456              {
1457                  $index = array($index);
1458              }
1459  
1460              foreach($index as $_index => $keys)
1461              {
1462                  if(!is_array($keys))
1463                  {
1464                      if(!$db->index_exists($table, $keys))
1465                      {
1466                          $db->write_query("ALTER TABLE ".TABLE_PREFIX."{$table} ADD INDEX (`{$keys}`)");
1467                      }
1468                  }
1469                  else
1470                  {
1471                      if(!$db->index_exists($table, $_index))
1472                      {
1473                          $db->write_query("ALTER TABLE ".TABLE_PREFIX."{$table} ADD INDEX `{$_index}`(`".implode('`, `', $keys)."`)");
1474                      }
1475                  }
1476              }
1477          }
1478      }
1479  
1480      echo "<p>Dropping old indexes from tables...</p>";
1481  
1482      if($db->index_exists('attachments', 'posthash'))
1483      {
1484          $db->drop_index('attachments', 'posthash');
1485      }
1486  
1487      if($db->index_exists('reportedcontent', 'dateline'))
1488      {
1489          $db->drop_index('reportedcontent', 'dateline');
1490      }
1491  
1492      if($db->index_exists('reputation', 'pid'))
1493      {
1494          $db->drop_index('reputation', 'pid');
1495      }
1496  
1497      if($db->index_exists('reputation', 'dateline'))
1498      {
1499          $db->drop_index('reputation', 'dateline');
1500      }
1501  
1502      if($db->index_exists('users', 'birthday'))
1503      {
1504          $db->drop_index('users', 'birthday');
1505      }
1506  
1507      global $footer_extra;
1508      $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
1509  
1510      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
1511      $output->print_footer("30_dbchanges_optimize3");
1512  }
1513  
1514  function upgrade30_dbchanges_optimize3()
1515  {
1516      global $cache, $output, $mybb, $db;
1517  
1518      $output->print_header("Optimizing Database");
1519  
1520      echo "<p>Performing necessary optimization queries...</p>";
1521      flush();
1522  
1523      $to_tinyint = array(
1524          "adminoptions" => array("codepress"),
1525          "adminviews" => array("visibility"),
1526          "announcements" => array("allowhtml", "allowmycode", "allowsmilies"),
1527          "attachments" => array("visible"),
1528          "banfilters" => array("type"),
1529          "calendars" => array("startofweek", "showbirthdays", "moderation", "allowhtml", "allowmycode", "allowimgcode", "allowvideocode", "allowsmilies"),
1530          "calendarpermissions" => array("canviewcalendar", "canaddevents", "canbypasseventmod", "canmoderateevents"),
1531          "events" => array("visible", "private", "ignoretimezone", "usingtime"),
1532          "forumpermissions" => array("canview", "canviewthreads", "canonlyviewownthreads", "candlattachments", "canpostthreads", "canpostreplys", "canpostattachments", "canratethreads", "caneditposts", "candeleteposts", "candeletethreads", "caneditattachments", "canpostpolls", "canvotepolls", "cansearch"),
1533          "forums" => array("active", "open", "allowhtml", "allowmycode", "allowsmilies", "allowimgcode", "allowvideocode", "allowpicons", "allowtratings", "usepostcounts", "showinjump", "overridestyle", "rulestype"),
1534          "groupleaders" => array("canmanagemembers", "canmanagerequests"),
1535          "helpdocs" => array("usetranslation", "enabled"),
1536          "helpsections" => array("usetranslation", "enabled"),
1537          "moderators" => array("isgroup", "caneditposts", "candeleteposts", "canviewips", "canopenclosethreads", "canmanagethreads", "canmovetononmodforum", "canusecustomtools"),
1538          "mycode" => array("active"),
1539          "polls" => array("closed", "multiple", "public"),
1540          "posts" => array("includesig", "smilieoff", "visible"),
1541          "privatemessages" => array("status", "includesig", "smilieoff", "receipt"),
1542          "profilefields" => array("required"),
1543          "reportedcontent" => array("reportstatus"),
1544          "sessions" => array("anonymous", "nopermission"),
1545          "settinggroups" => array("isdefault"),
1546          "settings" => array("isdefault"),
1547          "smilies" => array("showclickable"),
1548          "tasks" => array("enabled", "logging"),
1549          "themes" => array("def"),
1550          "threads" => array("sticky", "visible"),
1551          "threadsubscriptions" => array("notification"),
1552          "usergroups" => array("isbannedgroup", "canview", "canviewthreads", "canviewprofiles", "candlattachments", "canviewboardclosed", "canpostthreads", "canpostreplys", "canpostattachments", "canratethreads", "caneditposts", "candeleteposts", "candeletethreads", "caneditattachments", "canpostpolls", "canvotepolls", "canundovotes", "canusepms", "cansendpms", "cantrackpms", "candenypmreceipts", "cansendemail", "cansendemailoverride", "canviewmemberlist", "canviewcalendar", "canaddevents", "canbypasseventmod", "canmoderateevents", "canviewonline", "canviewwolinvis", "canviewonlineips", "cancp", "issupermod", "cansearch", "canusercp", "canuploadavatars", "canratemembers", "canchangename", "canbereported", "showforumteam", "usereputationsystem", "cangivereputations", "candisplaygroup", "cancustomtitle", "canwarnusers", "canreceivewarnings", "canmodcp", "showinbirthdaylist", "canoverridepm", "canusesig", "signofollow"),
1553          "users" => array("allownotices", "hideemail", "subscriptionmethod", "invisible", "receivepms", "receivefrombuddy", "pmnotice", "pmnotify", "showsigs", "showavatars", "showquickreply", "showredirect", "showcodebuttons", "coppauser", "classicpostbit"),
1554          "warnings" => array("expired")
1555      );
1556  
1557      foreach($to_tinyint as $table => $columns)
1558      {
1559          echo "<p>{$table}: Converting column type to tinyint</p>";
1560          $change_column = array();
1561          foreach($columns as $column)
1562          {
1563              if($db->type == "pgsql")
1564              {
1565                  $db->modify_column($table, $column, "smallint", "set", "'0'");
1566              }
1567              else if($db->type == "sqlite")
1568              {
1569                  $change_column[] = "CHANGE {$column} {$column} tinyint(1) NOT NULL default '0'";
1570              }
1571              else
1572              {
1573                  $change_column[] = "MODIFY {$column} tinyint(1) NOT NULL default '0'";
1574              }
1575          }
1576          if($db->type != "pgsql")
1577          {
1578              $db->write_query("ALTER TABLE ".TABLE_PREFIX."{$table} ".implode(", ", $change_column));
1579          }
1580      }
1581  
1582      global $footer_extra;
1583      $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
1584  
1585      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
1586      $output->print_footer("30_dbchanges_optimize4");
1587  }
1588  
1589  function upgrade30_dbchanges_optimize4()
1590  {
1591      global $cache, $output, $mybb, $db;
1592  
1593      $output->print_header("Optimizing Database");
1594  
1595      echo "<p>Performing necessary optimization queries...</p>";
1596      flush();
1597  
1598      $to_int = array(
1599          "adminlog" => array("dateline"),
1600          "adminsessions" => array("dateline", "lastactive"),
1601          "announcements" => array("startdate", "enddate"),
1602          "attachments" => array("dateuploaded"),
1603          "awaitingactivation" => array("dateline"),
1604          "banfilters" => array("lastuse", "dateline"),
1605          "banned" => array("dateline", "lifted"),
1606          "captcha" => array("dateline"),
1607          "delayedmoderation" => array("delaydateline", "dateline"),
1608          "forumsread" => array("dateline"),
1609          "joinrequests" => array("dateline"),
1610          "massemails" => array("dateline", "senddate"),
1611          "mailerrors" => array("dateline"),
1612          "maillogs" => array("dateline"),
1613          "moderatorlog" => array("dateline"),
1614          "polls" => array("dateline", "timeout"),
1615          "pollvotes" => array("dateline"),
1616          "posts" => array("dateline", "edittime"),
1617          "privatemessages" => array("dateline", "deletetime", "statustime", "readtime"),
1618          "promotionlogs" => array("dateline"),
1619          "reportedcontent" => array("dateline", "lastreport"),
1620          "reputation" => array("dateline"),
1621          "searchlog" => array("dateline"),
1622          "sessions" => array("time"),
1623          "spiders" => array("lastvisit"),
1624          "stats" => array("dateline"),
1625          "tasks" => array("nextrun", "lastrun", "locked"),
1626          "tasklog" => array("dateline"),
1627          "templates" => array("dateline"),
1628          "themestylesheets" => array("lastmodified"),
1629          "threads" => array("dateline", "lastpost"),
1630          "threadsread" => array("dateline"),
1631          "threadsubscriptions" => array("dateline"),
1632          "usergroups" => array("reputationpower", "maxreputationsday", "maxreputationsperuser", "maxreputationsperthread", "attachquota"),
1633          "users" => array("regdate", "lastactive", "lastvisit", "lastpost", "timeonline", "moderationtime", "suspensiontime", "suspendsigtime"),
1634          "warningtypes" => array("expirationtime"),
1635          "warnings" => array("dateline", "expires", "daterevoked")
1636      );
1637  
1638      foreach($to_int as $table => $columns)
1639      {
1640          echo "<p>{$table}: Converting column type to int</p>";
1641          $change_column = array();
1642          foreach($columns as $column)
1643          {
1644              if($db->type == "pgsql")
1645              {
1646                  $db->modify_column($table, $column, "int", "set", "'0'");
1647              }
1648              else if($db->type == "sqlite")
1649              {
1650                  $change_column[] = "CHANGE {$column} {$column} int unsigned NOT NULL default '0'";
1651              }
1652              else
1653              {
1654                  $change_column[] = "MODIFY {$column} int unsigned NOT NULL default '0'";
1655              }
1656          }
1657          if($db->type != "pgsql")
1658          {
1659              $db->write_query("ALTER TABLE ".TABLE_PREFIX."{$table} ".implode(", ", $change_column));
1660          }
1661      }
1662  
1663      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
1664      $output->print_footer("30_dbchanges_smilies");
1665  }
1666  
1667  function upgrade30_dbchanges_smilies()
1668  {
1669      global $cache, $output, $db;
1670  
1671      $output->print_header("Updating Smilies");
1672  
1673      echo "<p>Performing necessary upgrade queries...</p>";
1674      flush();
1675  
1676      if($db->type == 'pgsql')
1677      {
1678          $db->modify_column("smilies", "find", "text", "set");
1679      }
1680      else
1681      {
1682          $db->modify_column("smilies", "find", "text NOT NULL");
1683      }
1684  
1685      $query = $db->simple_select('smilies', 'sid, image, find', '', array('order_by' => 'image, sid'));
1686      $last_image = null;
1687      $last_sid = 0;
1688      $skip = array();
1689      while($smilie = $db->fetch_array($query))
1690      {
1691          if(in_array($smilie['sid'], $skip))
1692          {
1693              continue;
1694          }
1695  
1696          if($smilie['image'] == $last_image && $smilie['image'] != null)
1697          {
1698              $dupe_query = $db->simple_select('smilies', 'sid, find', 'image = "'.$db->escape_string($smilie['image']).'"');
1699              $dupes = '';
1700              $find = array();
1701              $skip = array();
1702              while($dupe = $db->fetch_array($dupe_query))
1703              {
1704                  if($dupe['sid'] != $last_sid)
1705                  {
1706                      $dupes .= (int)$dupe['sid'].',';
1707                      $find[] = trim($dupe['find']);
1708                      $skip[] = (int)$dupe['sid'];
1709                  }
1710                  else
1711                  {
1712                      $find[] = $dupe['find'];
1713                  }
1714              }
1715              $dupes = rtrim($dupes, ',');
1716              $db->delete_query('smilies', 'sid IN('.$dupes.')');
1717              $db->update_query('smilies', array('find' => implode("\n", $find)), 'sid = "'.(int)$last_sid.'"');
1718              $db->free_result($dupe_query);
1719          }
1720          else
1721          {
1722              $last_sid = $smilie['sid'];
1723              $last_image = $smilie['image'];
1724          }
1725      }
1726  
1727      $cache->update_smilies();
1728  
1729      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
1730      $output->print_footer("30_dbchanges_ip");
1731  }
1732  
1733  function upgrade30_dbchanges_ip()
1734  {
1735      global $mybb, $db, $output;
1736  
1737      $output->print_header("IP Conversion");
1738  
1739      $ipstart = $iptable = '';
1740  
1741      switch($mybb->input['iptask'])
1742      {
1743          case 8:
1744              echo "<p>Adding database indices (3/3)...</p>";
1745              flush();
1746  
1747              if(!$db->index_exists('users', 'lastip'))
1748              {
1749                  // This may take a while
1750                  if($db->type == "mysql" || $db->type == "mysqli")
1751                  {
1752                      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD INDEX lastip (lastip)");
1753                  }
1754                  elseif($db->type == "pgsql")
1755                  {
1756                      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD INDEX (`lastip`)");
1757                  }
1758              }
1759              $next_task = 9;
1760              break;
1761          case 7:
1762              echo "<p>Adding database indices (2/3)...</p>";
1763              flush();
1764  
1765              if(!$db->index_exists('users', 'regip'))
1766              {
1767                  // This may take a while
1768                  if($db->type == "mysql" || $db->type == "mysqli")
1769                  {
1770                      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD INDEX regip (regip)");
1771                  }
1772                  elseif($db->type == "pgsql")
1773                  {
1774                      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD INDEX (`regip`)");
1775                  }
1776              }
1777              $next_task = 8;
1778              break;
1779          case 6:
1780              echo "<p>Adding database indices (1/3)...</p>";
1781              flush();
1782  
1783              if(!$db->index_exists('posts', 'ipaddress'))
1784              {
1785                  // This may take a while
1786                  if($db->type == "mysql" || $db->type == "mysqli")
1787                  {
1788                      $db->write_query("ALTER TABLE ".TABLE_PREFIX."posts ADD INDEX ipaddress (ipaddress)");
1789                  }
1790                  elseif($db->type == "pgsql")
1791                  {
1792                      $db->write_query("ALTER TABLE ".TABLE_PREFIX."posts ADD INDEX (`ipaddress`)");
1793                  }
1794              }
1795              $next_task = 7;
1796              break;
1797          case 5:
1798              if(!$_POST['ipspage'])
1799              {
1800                  $ipp = 5000;
1801              }
1802              else
1803              {
1804                  $ipp = (int)$_POST['ipspage'];
1805              }
1806  
1807              if($_POST['ipstart'])
1808              {
1809                  $startat = (int)$_POST['ipstart'];
1810                  $upper = $startat+$ipp-1;
1811                  $lower = $startat;
1812              }
1813              else
1814              {
1815                  $startat = 0;
1816                  $upper = $ipp;
1817                  $lower = 0;
1818              }
1819  
1820              $next_task = 5;
1821              switch($mybb->input['iptable'])
1822              {
1823                  case 7:
1824                      echo "<p>Converting user IPs...</p>";
1825                      flush();
1826                      $query = $db->simple_select("users", "COUNT(uid) AS ipcount");
1827                      if($db->type == "mysql" || $db->type == "mysqli")
1828                      {
1829                          $next_task = 6;
1830                      }
1831                      else
1832                      {
1833                          $next_task = 9;
1834                      }
1835                      break;
1836                  case 6:
1837                      echo "<p>Converting thread rating IPs...</p>";
1838                      flush();
1839                      $query = $db->simple_select("threadratings", "COUNT(rid) AS ipcount");
1840                      break;
1841                  case 5:
1842                      echo "<p>Converting session IPs...</p>";
1843                      flush();
1844                      $query = $db->simple_select("sessions", "COUNT(sid) AS ipcount");
1845                      break;
1846                  case 4:
1847                      echo "<p>Converting post IPs...</p>";
1848                      flush();
1849                      $query = $db->simple_select("posts", "COUNT(pid) AS ipcount");
1850                      break;
1851                  case 3:
1852                      echo "<p>Converting moderator log IPs...</p>";
1853                      flush();
1854                      $query = $db->simple_select("moderatorlog", "COUNT(DISTINCT ipaddress) AS ipcount");
1855                      break;
1856                  case 2:
1857                      echo "<p>Converting mail log IPs...</p>";
1858                      flush();
1859                      $query = $db->simple_select("maillogs", "COUNT(mid) AS ipcount");
1860                      break;
1861                  default:
1862                      echo "<p>Converting admin log IPs...</p>";
1863                      flush();
1864                      $query = $db->simple_select("adminlog", "COUNT(DISTINCT ipaddress) AS ipcount");
1865                      break;
1866              }
1867              $cnt = $db->fetch_array($query);
1868  
1869              if($upper > $cnt['ipcount'])
1870              {
1871                  $upper = $cnt['ipcount'];
1872              }
1873  
1874              echo "<p>Converting ip {$lower} to {$upper} ({$cnt['ipcount']} Total)</p>";
1875              flush();
1876  
1877              $ipaddress = false;
1878  
1879              switch($mybb->input['iptable'])
1880              {
1881                  case 7:
1882                      $query = $db->simple_select("users", "uid, regip, lastip", "", array('limit_start' => $lower, 'limit' => $ipp));
1883                      break;
1884                  case 6:
1885                      $query = $db->simple_select("threadratings", "rid, ipaddress", "", array('limit_start' => $lower, 'limit' => $ipp));
1886                      break;
1887                  case 5:
1888                      $query = $db->simple_select("sessions", "sid, ip", "", array('limit_start' => $lower, 'limit' => $ipp));
1889                      break;
1890                  case 4:
1891                      $query = $db->simple_select("posts", "pid, ipaddress", "", array('limit_start' => $lower, 'limit' => $ipp));
1892                      break;
1893                  case 3:
1894                      $query = $db->simple_select("moderatorlog", "DISTINCT(ipaddress)", "", array('limit_start' => $lower, 'limit' => $ipp));
1895                      break;
1896                  case 2:
1897                      $query = $db->simple_select("maillogs", "mid, ipaddress", "", array('limit_start' => $lower, 'limit' => $ipp));
1898                      break;
1899                  default:
1900                      $query = $db->simple_select("adminlog", "DISTINCT(ipaddress)", "", array('limit_start' => $lower, 'limit' => $ipp));
1901                      $mybb->input['iptable'] = 1;
1902                      break;
1903              }
1904              while($data = $db->fetch_array($query))
1905              {
1906                  // Skip invalid IPs
1907                  switch($mybb->input['iptable'])
1908                  {
1909                      case 7:
1910                          $ip1 = my_inet_pton($db->unescape_binary($data['regip']));
1911                          $ip2 = my_inet_pton($db->unescape_binary($data['lastip']));
1912                          if($ip1 === false && $ip2 === false)
1913                          {
1914                              continue 2;
1915                          }
1916                          break;
1917                      case 5:
1918                          $ip = my_inet_pton($db->unescape_binary($data['ip']));
1919                          if($ip === false)
1920                          {
1921                              continue 2;
1922                          }
1923                          break;
1924                      case 6:
1925                      case 4:
1926                      case 3:
1927                      case 2:
1928                      default:
1929                          $ip = my_inet_pton($db->unescape_binary($data['ipaddress']));
1930                          if($ip === false)
1931                          {
1932                              continue 2;
1933                          }
1934                          break;
1935                  }
1936  
1937                  switch($mybb->input['iptable'])
1938                  {
1939                      case 7:
1940                          $db->update_query("users", array('regip' => $db->escape_binary($ip1), 'lastip' => $db->escape_binary($ip2)), "uid = '".(int)$data['uid']."'");
1941                          break;
1942                      case 6:
1943                          $db->update_query("threadratings", array('ipaddress' => $db->escape_binary($ip)), "rid = '".(int)$data['rid']."'");
1944                          break;
1945                      case 5:
1946                          $db->update_query("sessions", array('ip' => $db->escape_binary($ip)), "sid = '".(int)$data['sid']."'");
1947                          break;
1948                      case 4:
1949                          $db->update_query("posts", array('ipaddress' => $db->escape_binary($ip)), "pid = '".(int)$data['pid']."'");
1950                          break;
1951                      case 3:
1952                          $db->update_query("moderatorlog", array('ipaddress' => $db->escape_binary($ip)), "ipaddress = '".$db->escape_string($data['ipaddress'])."'");
1953                          break;
1954                      case 2:
1955                          $db->update_query("maillogs", array('ipaddress' => $db->escape_binary($ip)), "mid = '".(int)$data['mid']."'");
1956                          break;
1957                      default:
1958                          $db->update_query("adminlog", array('ipaddress' => $db->escape_binary($ip)), "ipaddress = '".$db->escape_string($data['ipaddress'])."'");
1959                          break;
1960                  }
1961                  $ipaddress = true;
1962              }
1963  
1964              $remaining = $upper-$cnt['ipcount'];
1965              if($remaining && $ipaddress)
1966              {
1967                  $startat = $startat+$ipp;
1968                  $ipstart = "<input type=\"hidden\" name=\"ipstart\" value=\"$startat\" />";
1969                  $iptable = $mybb->input['iptable'];
1970              }
1971              else
1972              {
1973                  $iptable = $mybb->input['iptable']+1;
1974              }
1975              if($iptable <= 10)
1976              {
1977                  $iptable = "<input type=\"hidden\" name=\"iptable\" value=\"$iptable\" />";
1978              }
1979              break;
1980          case 4:
1981              $next_task = 4;
1982              switch($mybb->input['iptable'])
1983              {
1984                  case 10:
1985                      echo "<p>Updating user table (4/4)...</p>";
1986                      flush();
1987  
1988                      $table = 'users';
1989                      $column = 'lastip';
1990                      $next_task = 5;
1991                      break;
1992                  case 9:
1993                      echo "<p>Updating user table (3/4)...</p>";
1994                      flush();
1995  
1996                      $table = 'users';
1997                      $column = 'regip';
1998                      break;
1999                  case 8:
2000                      echo "<p>Updating threadreating table...</p>";
2001                      flush();
2002  
2003                      $table = 'threadratings';
2004                      $column = 'ipaddress';
2005                      break;
2006                  case 7:
2007                      echo "<p>Updating session table...</p>";
2008                      flush();
2009  
2010                      $table = 'sessions';
2011                      $column = 'ip';
2012                      break;
2013                  case 6:
2014                      echo "<p>Updating searchlog table...</p>";
2015                      flush();
2016  
2017                      $table = 'searchlog';
2018                      $column = 'ipaddress';
2019                      // Skip conversion
2020                      $db->delete_query('searchlog');
2021                      break;
2022                  case 5:
2023                      echo "<p>Updating post table (2/2)...</p>";
2024                      flush();
2025  
2026                      $table = 'posts';
2027                      $column = 'ipaddress';
2028                      break;
2029                  case 4:
2030                      echo "<p>Updating moderatorlog table...</p>";
2031                      flush();
2032  
2033                      $table = 'moderatorlog';
2034                      $column = 'ipaddress';
2035                      break;
2036                  case 3:
2037                      echo "<p>Updating maillog table...</p>";
2038                      flush();
2039  
2040                      $table = 'maillogs';
2041                      $column = 'ipaddress';
2042                      break;
2043                  case 2:
2044                      echo "<p>Updating adminsession table...</p>";
2045                      flush();
2046  
2047                      $table = 'adminsessions';
2048                      $column = 'ip';
2049                      // Skip conversion
2050                      $db->delete_query('adminsessions');
2051                      break;
2052                  default:
2053                      echo "<p>Updating adminlog table...</p>";
2054                      flush();
2055  
2056                      $mybb->input['iptable'] = 1;
2057                      $table = 'adminlog';
2058                      $column = 'ipaddress';
2059                      break;
2060              }
2061              // Truncate invalid IPs
2062              $db->write_query("UPDATE ".TABLE_PREFIX."{$table} SET {$column} = SUBSTR({$column}, 16) WHERE LENGTH({$column})>16");
2063              switch($db->type)
2064              {
2065                  case "pgsql":
2066                      // Drop default value before converting the column
2067                      $db->modify_column($table, $column, false, false);
2068                      $db->modify_column($table, $column, "bytea USING {$column}::bytea", 'set', "''");
2069                      break;
2070                  case "sqlite":
2071                      $db->modify_column($table, $column, "blob(16) NOT NULL default ''");
2072                      break;
2073                  default:
2074                      $db->modify_column($table, $column, "varbinary(16) NOT NULL default ''");
2075                      break;
2076              }
2077              if($mybb->input['iptable'] < 10)
2078              {
2079                  $iptable = "<input type=\"hidden\" name=\"iptable\" value=\"".($mybb->input['iptable']+1)."\" />";
2080              }
2081              break;
2082          case 3:
2083              echo "<p>Updating user table (2/4)...</p>";
2084              flush();
2085  
2086              if($db->field_exists('longlastip', 'users'))
2087              {
2088                  // This may take a while
2089                  $db->drop_column("users", "longlastip");
2090              }
2091              $next_task = 4;
2092              break;
2093          case 2:
2094              echo "<p>Updating user table (1/4)...</p>";
2095              flush();
2096  
2097              if($db->field_exists('longregip', 'users'))
2098              {
2099                  // This may take a while
2100                  $db->drop_column("users", "longregip");
2101              }
2102              $next_task = 3;
2103              break;
2104          default:
2105              echo "<p>Updating post table (1/2)...</p>";
2106              flush();
2107  
2108              if($db->field_exists('longipaddress', 'posts'))
2109              {
2110                  // This may take a while
2111                  $db->drop_column("posts", "longipaddress");
2112              }
2113              $next_task = 2;
2114              break;
2115      }
2116  
2117      if($next_task == 9)
2118      {
2119          $contents = "<p>Click next to continue with the upgrade process.</p>";
2120          $nextact = "30_updatetheme";
2121      }
2122      else
2123      {
2124          $contents = "<p><input type=\"hidden\" name=\"iptask\" value=\"{$next_task}\" />{$iptable}{$ipstart}Done. Click Next to continue the IP conversion.</p>";
2125  
2126          global $footer_extra;
2127          $footer_extra = "<script type=\"text/javascript\">$(function() { var button = $('.submit_button'); if(button) { button.val('Automatically Redirecting...'); button.prop('disabled', true); button.css('color', '#aaa'); button.css('border-color', '#aaa'); document.forms[0].submit(); } });</script>";
2128          $nextact = "30_dbchanges_ip";
2129      }
2130  
2131      $output->print_contents($contents);
2132  
2133      $output->print_footer($nextact);
2134  }
2135  
2136  function upgrade30_updatetheme()
2137  {
2138      global $db, $mybb, $output, $config;
2139  
2140      if(file_exists(MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions_themes.php"))
2141      {
2142          require_once MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions_themes.php";
2143      }
2144      else if(file_exists(MYBB_ROOT."admin/inc/functions_themes.php"))
2145      {
2146          require_once  MYBB_ROOT."admin/inc/functions_themes.php";
2147      }
2148      else
2149      {
2150          $output->print_error("Please make sure your admin directory is uploaded correctly.");
2151      }
2152  
2153      $output->print_header("Updating Themes");
2154  
2155      // New default user star
2156      $contents = "<p>Updating the Default user star image... ";
2157      $db->update_query("usergroups", array('starimage' => 'images/star.png'), "starimage='images/star.gif'");
2158      $contents .= "done.</p>";
2159  
2160      $contents .= "<p>Adding new stylesheets... ";
2161      
2162      $query = $db->simple_select("themes", "*", "tid='1'");
2163  
2164      $theme = $db->fetch_array($query);
2165      $properties = my_unserialize($theme['properties']);
2166      $stylesheets = my_unserialize($theme['stylesheets']);
2167  
2168      $old = array("global.css", "usercp.css", "modcp.css", "star_ratings.css");
2169      $colors = @file_get_contents(INSTALL_ROOT.'resources/mybb_theme.xml');
2170      $parser = create_xml_parser($colors);
2171      $tree = $parser->get_tree();
2172  
2173      if(is_array($tree) && is_array($tree['theme']))
2174      {
2175          if(is_array($tree['theme']['stylesheets']))
2176          {
2177              foreach($tree['theme']['stylesheets']['stylesheet'] as $stylesheet)
2178              {
2179                  $new_stylesheet = array(
2180                      "name" => $db->escape_string($stylesheet['attributes']['name']),
2181                      "tid" => 1,
2182                      "attachedto" => $db->escape_string($stylesheet['attributes']['attachedto']),
2183                      "stylesheet" => $db->escape_string($stylesheet['value']),
2184                      "lastmodified" => TIME_NOW,
2185                      "cachefile" => $db->escape_string($stylesheet['attributes']['name'])
2186                  );
2187  
2188                  if(in_array($new_stylesheet['name'], $old))
2189                  {
2190                      // We can update the disporder here
2191                      $properties['disporder'][$stylesheet['attributes']['name']] = $stylesheet['attributes']['disporder'];
2192                  }
2193                  else
2194                  {
2195                      // Insert new stylesheet
2196                      $sid = $db->insert_query("themestylesheets", $new_stylesheet);
2197                      $css_url = "css.php?stylesheet={$sid}";
2198  
2199                      $cached = cache_stylesheet(1, $stylesheet['attributes']['name'], $stylesheet['value']);
2200  
2201                      if($cached)
2202                      {
2203                          $css_url = $cached;
2204                      }
2205  
2206                      // Add to display and stylesheet list
2207                      $properties['disporder'][$stylesheet['attributes']['name']] = $stylesheet['attributes']['disporder'];
2208                      $attachedto = $stylesheet['attributes']['attachedto'];
2209                      if(!$attachedto)
2210                      {
2211                          $attachedto = "global";
2212                      }
2213  
2214                      // private.php?compose,folders|usercp.php,global|global
2215                      $attachedto = explode("|", $attachedto);
2216                      foreach($attachedto as $attached_file)
2217                      {
2218                          $attached_actions = explode(",", $attached_file);
2219                          $attached_file = array_shift($attached_actions);
2220                          if(count($attached_actions) == 0)
2221                          {
2222                              $attached_actions = array("global");
2223                          }
2224  
2225                          foreach($attached_actions as $action)
2226                          {
2227                              $stylesheets[$attached_file][$action][] = $css_url;
2228                          }
2229                      }
2230                  }
2231              }
2232          }
2233      }
2234  
2235      $update_array = array(
2236          "properties" => $db->escape_string(my_serialize($properties)),
2237          "stylesheets" => $db->escape_string(my_serialize($stylesheets))
2238      );
2239  
2240      $db->update_query("themes", $update_array, "tid = '1'");
2241  
2242      $contents .= "done.</p>";
2243  
2244      $contents .= "<p>Adding a disporder to all stylesheets... ";
2245  
2246      $query = $db->simple_select("themes", "tid,properties,stylesheets");
2247      while($theme = $db->fetch_array($query))
2248      {
2249          $properties = my_unserialize($theme['properties']);
2250          $stylesheets = my_unserialize($theme['stylesheets']);
2251  
2252          // Disporder already set?
2253          if(isset($properties['disporder']) && !empty($properties['disporder']))
2254          {
2255              continue;
2256          }
2257  
2258          $disporder = 1;
2259  
2260          // First go through all own stylesheets
2261          $query2 = $db->simple_select("themestylesheets", "name", "tid='{$theme['tid']}'");
2262          while($name = $db->fetch_field($query2, "name"))
2263          {
2264              $properties['disporder'][$name] = $disporder;
2265              $disporder++;
2266          }
2267  
2268          // Next go through the inherited stylesheets
2269          if(!empty($stylesheets))
2270          {
2271              foreach($stylesheets as $a)
2272              {
2273                  foreach($a as $file => $stylesheet)
2274                  {
2275                      // Don't ask me... Throws an error otherwise
2276                      if(empty($stylesheet))
2277                      {
2278                          continue;
2279                      }
2280                      foreach($stylesheet as $s)
2281                      {
2282                          $name = pathinfo($s, PATHINFO_BASENAME);
2283                          if(empty($properties['disporder']) || !in_array($name, array_keys($properties['disporder'])))
2284                          {
2285                              $properties['disporder'][$name] = $disporder;
2286                              $disporder++;
2287                          }
2288                      }
2289                  }
2290              }
2291          }
2292  
2293          $db->update_query("themes", array("properties" => $db->escape_string(my_serialize($properties))), "tid='{$theme['tid']}'");
2294      }
2295  
2296      $contents .= "done.</p>";
2297  
2298      $contents .= "<p>Adding the Default colors... ";
2299  
2300      $query = $db->simple_select("themes", "*", "tid = '2'");
2301  
2302      // Someone deleted the default theme... :o
2303      if($db->num_rows($query) != 0)
2304      {
2305          $theme = $db->fetch_array($query);
2306          $properties = my_unserialize($theme['properties']);
2307          $stylesheets = my_unserialize($theme['stylesheets']);
2308          
2309          $properties['editortheme'] = "mybb.css"; // New editor, so reset the theme for it
2310          $properties['tablespace'] = 5;
2311          $properties['borderwidth'] = 0;
2312          // Reset the logo if it's still the default one
2313          if($properties['logo'] == "images/logo.gif")
2314          {
2315              $properties['logo'] = "images/logo.png";
2316          }
2317      
2318          $colors = @file_get_contents(INSTALL_ROOT.'resources/mybb_theme_colors.xml');
2319          $parser = create_xml_parser($colors);
2320          $tree = $parser->get_tree();
2321      
2322          if(is_array($tree) && is_array($tree['colors']))
2323          {
2324              if(is_array($tree['colors']['scheme']))
2325              {
2326                  foreach($tree['colors']['scheme'] as $tag => $value)
2327                  {
2328                      $exp = explode("=", $value['value']);
2329      
2330                      $properties['colors'][$exp[0]] = $exp[1];
2331                  }
2332              }
2333      
2334              if(is_array($tree['colors']['stylesheets']))
2335              {
2336                  $count = count($properties['disporder']) + 1;
2337                  foreach($tree['colors']['stylesheets']['stylesheet'] as $stylesheet)
2338                  {
2339                      $new_stylesheet = array(
2340                          "name" => $db->escape_string($stylesheet['attributes']['name']),
2341                          "tid" => 2,
2342                          "attachedto" => $db->escape_string($stylesheet['attributes']['attachedto']),
2343                          "stylesheet" => $db->escape_string($stylesheet['value']),
2344                          "lastmodified" => TIME_NOW,
2345                          "cachefile" => $db->escape_string($stylesheet['attributes']['name'])
2346                      );
2347      
2348                      $sid = $db->insert_query("themestylesheets", $new_stylesheet);
2349                      $css_url = "css.php?stylesheet={$sid}";
2350      
2351                      $cached = cache_stylesheet($tid, $stylesheet['attributes']['name'], $stylesheet['value']);
2352      
2353                      if($cached)
2354                      {
2355                          $css_url = $cached;
2356                      }
2357      
2358                      // Add to display and stylesheet list
2359                      $properties['disporder'][$stylesheet['attributes']['name']] = $count;
2360                      $stylesheets[$stylesheet['attributes']['attachedto']]['global'][] = $css_url;
2361      
2362                      ++$count;
2363                  }
2364              }
2365      
2366              $update_array = array(
2367                  "properties" => $db->escape_string(my_serialize($properties)),
2368                  "stylesheets" => $db->escape_string(my_serialize($stylesheets))
2369              );
2370      
2371              $db->update_query("themes", $update_array, "tid = '2'");
2372          }
2373      }
2374  
2375      $contents .= "done.</p>";
2376  
2377      $contents .= '<p>Re-caching and minifying existing stylesheets...</p>';
2378  
2379      $num_re_cached = recache_existing_styles();
2380  
2381      $contents .= "Done. {$num_re_cached} stylesheets re-cached.";
2382  
2383      echo $contents;
2384  
2385      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
2386  
2387      if(!isset($config['secret_pin']) && is_writable(MYBB_ROOT."inc/config.php"))
2388      {
2389          $output->print_footer("30_acppin");
2390      }
2391      else
2392      {
2393          $output->print_footer("30_done");
2394      }
2395  }
2396  
2397  function upgrade30_acppin()
2398  {
2399      global $config, $output;
2400  
2401      $output->print_header("Add an ACP PIN");
2402  
2403      echo "<p>We added a new security function in 1.8: The possibility to set a security PIN which you need to enter the ACP.<br />\n";
2404      echo "If you don't want to set a PIN you can simply skip this step (leave the field below empty). You can still set the PIN later (see the docs to see how).</p>\n";
2405      echo '<div class="border_wrapper">
2406              <div class="title">ACP PIN Configuration</div>
2407              <table class="general" cellspacing="0">
2408                  <tbody>
2409                  <tr>
2410                      <th colspan="2" class="first last">ACP Security PIN</th>
2411                  </tr>
2412                  <tr class="first">
2413                      <td class="first"><label for="bbname">PIN:</label></td>
2414                      <td class="last alt_col"><input type="password" class="text_input" name="pin" id="pin" value="'.$config['secret_pin'].'" /></td>
2415                  </tr>
2416                  </tbody>
2417              </table>
2418          </div>';
2419  
2420      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
2421  
2422      $output->print_footer("30_acppin_submit");
2423  }
2424  
2425  function upgrade30_acppin_submit()
2426  {
2427      global $db, $mybb, $output, $config;
2428  
2429      $output->print_header("Writing the config file");
2430  
2431      $content = "<p>We're now writing your PIN (if you've entered one) to the config.php file... ";
2432  
2433      if(!is_writable(MYBB_ROOT."inc/config.php"))
2434      {
2435          $content .= "Failed (config.php not writable)";
2436      }
2437      else if(isset($config['secret_pin']))
2438      {
2439          $content .= "Skipped (PIN already set)";
2440      }
2441      else
2442      {
2443          $pin = addslashes($mybb->get_input('pin'));
2444  
2445          $file = @fopen(MYBB_ROOT."inc/config.php", "r+");
2446  
2447          $contents = '';
2448          while(!@feof($file))
2449          {
2450              $contents .= @fread($file, 8436);
2451          }
2452  
2453          $contents_temp = str_replace(array("\r", "\t", "\n", " ", "\0", "\x0B"), '', $contents);
2454  
2455          // Set the pointer before the closing php tag to remove it
2456          $pos = strrpos($contents, "?>");
2457          if(my_substr($contents_temp, -2) == "?>")
2458          {
2459              @fseek($file, $pos, SEEK_SET);
2460          }
2461  
2462          @fwrite($file, "
2463  /**
2464   * Admin CP Secret PIN
2465   *  If you wish to request a PIN
2466   *  when someone tries to login
2467   *  on your Admin CP, enter it below.
2468   */
2469  
2470  \$config['secret_pin'] = '{$pin}';");
2471  
2472          @fclose($file);
2473  
2474          $content .= "Done";        
2475      }
2476  
2477      echo $content."</p>";
2478  
2479      $output->print_contents("<p>Click next to continue with the upgrade process.</p>");
2480  
2481      $output->print_footer("30_done");
2482  }
2483  
2484  /**
2485   * Re-cache the existing stylesheets so that they get minified.
2486   *
2487   * @return int The number of re-cached stylesheets.
2488   */
2489  function recache_existing_styles()
2490  {
2491      global $db;
2492  
2493      $query = $db->simple_select('themestylesheets', '*');
2494  
2495      $num_updated = 0;
2496  
2497      while($stylesheet = $db->fetch_array($query))
2498      {
2499          if (cache_stylesheet((int) $stylesheet['tid'], $stylesheet['name'], $stylesheet['stylesheet']))
2500          {
2501              ++$num_updated;
2502          }
2503      }
2504  
2505      return $num_updated;
2506  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref