[ Index ]

PHP Cross Reference of MyBB 1.8.40

title

Body

[close]

/install/resources/ -> upgrade12.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.2.12, 1.2.13 or 1.2.14
  13   */
  14  
  15  $upgrade_detail = array(
  16      "revert_all_templates" => 0,
  17      "revert_all_themes" => 0,
  18      "revert_all_settings" => 0,
  19      "requires_deactivated_plugins" => 1,
  20  );
  21  
  22  // We need to globalize $db here because when this script is called
  23  // during load_module $db is not globalized in the function
  24  global $db;
  25  
  26  // FIRST STEP IS FOR INTEGER CONVERSION PROJECT
  27  
  28  function upgrade12_dbchanges()
  29  {
  30      global $db, $output, $mybb;
  31  
  32      $output->print_header("Integer Conversion Project");
  33  
  34      $perpage = 10000;
  35  
  36      $to_int = array(
  37          "announcements" => array("aid", "allowhtml", "allowmycode", "allowsmilies"),
  38          "forumpermissions" => array("pid", "canview","canviewthreads","candlattachments","canpostthreads","canpostreplys","canpostattachments","canratethreads","caneditposts","candeleteposts","candeletethreads","caneditattachments","canpostpolls","canvotepolls","cansearch"),
  39          "forums" => array("fid", "active","open","allowhtml","allowmycode","allowsmilies","allowimgcode","allowpicons","allowtratings","usepostcounts","showinjump","modposts","modthreads","modattachments","overridestyle"),
  40          "groupleaders" => array("lid", "canmanagemembers","canmanagerequests"),
  41          "helpdocs" => array("hid", "usetranslation","enabled"),
  42          "helpsections" => array("sid", "usetranslation","enabled"),
  43          "moderators" => array("mid", "caneditposts","candeleteposts","canviewips","canopenclosethreads","canmanagethreads","canmovetononmodforum"),
  44          "mycode" => array("cid", "active"),
  45          "polls" => array("pid", "closed","multiple","public"),
  46          "posts" => array("pid", "includesig","smilieoff"),
  47          "privatemessages" => array("pmid", "includesig","smilieoff"),
  48          "profilefields" => array("fid", "required","editable","hidden"),
  49          "smilies" => array("sid", "showclickable"),
  50          "usergroups" => array("gid","isbannedgroup","canview","canviewthreads","canviewprofiles","candlattachments","canpostthreads","canpostreplys","canpostattachments","canratethreads","caneditposts","candeleteposts","candeletethreads","caneditattachments","canpostpolls","canvotepolls","canusepms","cansendpms","cantrackpms","candenypmreceipts","cansendemail","canviewmemberlist","canviewcalendar","canviewonline","canviewwolinvis","canviewonlineips","cancp","issupermod","cansearch","canusercp","canuploadavatars","canratemembers","canchangename","showforumteam","usereputationsystem","cangivereputations","candisplaygroup","cancustomtitle"),
  51          "users" => array("uid","allownotices","hideemail","invisible","receivepms","pmpopup","pmnotify","remember","showsigs","showavatars","showquickreply","showredirect","away"),
  52          "threads" => array("tid", "closed")
  53      );
  54  
  55      if(!$db->field_exists('pmpopup', "users"))
  56      {
  57          $pmpopup_key = array_search('pmpopup', $to_int['users']);
  58          if($pmpopup_key)
  59          {
  60              unset($to_int['users'][$pmpopup_key]);
  61          }
  62      }
  63  
  64      // Continuing?
  65      if($mybb->input['last_table'])
  66      {
  67          $current_table = $mybb->input['last_table'];
  68      }
  69      else
  70      {
  71          $current_table = array_keys($to_int);
  72          $current_table = $current_table[0];
  73          echo "<p>MyBB 1.4 represents a huge leap forward for the MyBB project.</p><p>Due to this, lots of information in the database needs to be converted to a new format.</p>";
  74      }
  75  
  76      echo "<p>MyBB is now currently converting a section of data to the new format, this may take a while.</p>";
  77  
  78      $remaining = $perpage;
  79  
  80      $final_table = array_keys($to_int);
  81      $final_table = $final_table[count($final_table)-1];
  82  
  83      $next_act = "12_dbchanges";
  84  
  85      $start = $mybb->get_input('start', MyBB::INPUT_INT);
  86      $count = $mybb->input['count'];
  87  
  88      foreach($to_int as $table => $columns)
  89      {
  90          if($table == $current_table)
  91          {
  92              $form_fields['last_table'] = $current_table;
  93              if($remaining <= 0)
  94              {
  95                  break;
  96              }
  97              $columns_sql = implode(",", $columns);
  98              $primary_key = $columns[0];
  99              if(!$mybb->input['count'])
 100              {
 101                  $query = $db->simple_select($table, "COUNT({$primary_key}) AS count");
 102                  $count = $form_fields['count'] = $db->fetch_field($query, "count");
 103              }
 104              if($start <= $count)
 105              {
 106                  $end = $start+$perpage;
 107                  if($end > $count) $end = $count;
 108                  echo "<p>{$table}: Converting {$start} to {$end} of {$count}</p>";
 109                  flush();
 110                  $form_fields['start'] = $perpage+$start;
 111  
 112                  $query = $db->simple_select($table, $columns_sql, "", array('order_by' => $primary_key, 'limit_start' => $start, 'limit' => $remaining));
 113                  while($row = $db->fetch_array($query))
 114                  {
 115                      $updated_row = array();
 116                      foreach($columns as $column)
 117                      {
 118                          if($column == $primary_key || is_int($row[$column])) continue;
 119                          if($row[$column] == "yes" || $row[$column] == "on")
 120                          {
 121                              $updated_row[$column] = 1;
 122                          }
 123                          else if($row[$column] == "off" || $row[$column] == "no" || $row[$column] == 'new' || $row[$column] == "")
 124                          {
 125                              $updated_row[$column] = 0;
 126                          }
 127                      }
 128                      if(count($updated_row) > 0)
 129                      {
 130                          $db->update_query($table, $updated_row, "{$primary_key}={$row[$primary_key]}");
 131                      }
 132                      --$remaining;
 133                  }
 134              }
 135              else
 136              {
 137                  echo "<p>{$table}: Converting column type</p>";
 138                  flush();
 139                  $change_column = array();
 140                  foreach($columns as $column)
 141                  {
 142                      // Closed does not get converted to an int
 143                      if($column == $primary_key || ($table == "threads" && $column == "closed"))
 144                      {
 145                          continue;
 146                      }
 147                      $change_column[] = "MODIFY {$column} int(1) NOT NULL default '0'";
 148                  }
 149                  $db->write_query("ALTER TABLE ".TABLE_PREFIX."{$table} ".implode(", ", $change_column));
 150  
 151                  if($table == $final_table)
 152                  {
 153                      // Finished, after all this!
 154                      $next_act = "12_dbchanges1";
 155                  }
 156                  else
 157                  {
 158                      $tbl_keys = array_keys($to_int);
 159                      $current = array_search($current_table, $tbl_keys);
 160                      $form_fields['last_table'] = $current_table = $tbl_keys[$current+1];
 161                      $form_fields['start'] = $start = 0;
 162                      $form_fields['count'] = $count = $mybb->input['count'] = 0;
 163                  }
 164              }
 165          }
 166      }
 167  
 168      // Still converting
 169      if($next_act == "12_dbchanges")
 170      {
 171          echo "<p>Done</p>";
 172          echo "<p>Click next to continue with the integer conversion process.</p>";
 173          foreach($form_fields as $key => $val)
 174          {
 175              echo "<input type=\"hidden\" name=\"{$key}\" value=\"{$val}\" />";
 176          }
 177          global $footer_extra;
 178          $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>";
 179          $output->print_footer($next_act);
 180      }
 181      else
 182      {
 183          // Convert settings table
 184          $query = $db->write_query("UPDATE ".TABLE_PREFIX."settings SET value=1 WHERE value='yes' OR value='on'");
 185          $query = $db->write_query("UPDATE ".TABLE_PREFIX."settings SET value=0 WHERE value='no' OR value='off'");
 186          echo "<p>Done</p>";
 187          echo "<p><strong>The integrer conversion process is now complete.</strong></p>";
 188          echo "<p>Click next to continue with the upgrade process.</p>";
 189          global $footer_extra;
 190          $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>";
 191  
 192          $output->print_footer($next_act);
 193      }
 194  }
 195  
 196  function upgrade12_dbchanges1()
 197  {
 198      global $db, $output, $mybb;
 199  
 200      $output->print_header("Performing Queries");
 201  
 202      echo "<p>Performing necessary upgrade queries..</p>";
 203      echo "<p>Adding index to private messages table ... ";
 204      flush();
 205  
 206      $db->write_query("ALTER TABLE ".TABLE_PREFIX."privatemessages ADD INDEX ( `uid` )");
 207  
 208      echo "done.</p>";
 209  
 210      $contents = "<p>Click next to continue with the upgrade process.</p>";
 211      $output->print_contents($contents);
 212  
 213      global $footer_extra;
 214      $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>";
 215  
 216      $output->print_footer("12_dbchanges_post1");
 217  }
 218  
 219  function upgrade12_dbchanges_post1()
 220  {
 221      global $db, $output, $mybb;
 222  
 223      $output->print_header("Performing Queries");
 224  
 225      echo "<p>Performing necessary upgrade queries..</p>";
 226      echo "<p>Adding index to posts table ... ";
 227      flush();
 228  
 229      // This will take a LONG time on huge post databases, so we only run it isolted from most of the other queries
 230      $db->write_query("ALTER TABLE ".TABLE_PREFIX."posts ADD INDEX ( `visible` )");
 231  
 232      echo "done.</p>";
 233      flush();
 234  
 235      $contents = "<p>Click next to continue with the upgrade process.</p>";
 236      $output->print_contents($contents);
 237  
 238      global $footer_extra;
 239      $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>";
 240  
 241      $output->print_footer("12_dbchanges_post2");
 242  }
 243  
 244  function upgrade12_dbchanges_post2()
 245  {
 246      global $db, $output, $mybb;
 247  
 248      $output->print_header("Performing Queries");
 249  
 250      echo "<p>Performing necessary upgrade queries..</p>";
 251  
 252      if($db->field_exists('longipaddress', "posts"))
 253      {
 254          echo "<p>Dropping longipaddress column in posts table ... ";
 255          flush();
 256  
 257          $db->write_query("ALTER TABLE ".TABLE_PREFIX."posts DROP longipaddress;");
 258  
 259          echo "done.</p>";
 260          flush();
 261      }
 262  
 263      echo "<p>Adding longipaddress column to posts table ... ";
 264      flush();
 265  
 266      $db->write_query("ALTER TABLE ".TABLE_PREFIX."posts ADD longipaddress int(11) NOT NULL default '0' AFTER ipaddress");
 267  
 268      echo "done.</p>";
 269      flush();
 270  
 271      $contents = "<p>Click next to continue with the upgrade process.</p>";
 272      $output->print_contents($contents);
 273  
 274      global $footer_extra;
 275      $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>";
 276  
 277      $output->print_footer("12_dbchanges_user");
 278  }
 279  
 280  function upgrade12_dbchanges_user()
 281  {
 282      global $db, $output, $mybb;
 283  
 284      $output->print_header("Performing Queries");
 285  
 286      echo "<p>Performing necessary upgrade queries..</p>";
 287      echo "<p>Adding index to users table ... ";
 288      flush();
 289  
 290      // This will take a LONG time on huge user databases, so we only run it isolted from most of the other queries
 291      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD INDEX ( `lastvisit` )");
 292      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD INDEX ( `regdate` )");
 293  
 294      echo "done.</p>";
 295      flush();
 296  
 297      $contents = "<p>Click next to continue with the upgrade process.</p>";
 298      $output->print_contents($contents);
 299  
 300      global $footer_extra;
 301      $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>";
 302  
 303      $output->print_footer("12_dbchanges2");
 304  }
 305  
 306  function upgrade12_dbchanges2()
 307  {
 308      global $db, $output, $mybb;
 309  
 310      $output->print_header("Performing Queries");
 311  
 312      echo "<p>Performing necessary upgrade queries..</p>";
 313      flush();
 314  
 315      if($db->field_exists('recipients', "privatemessages"))
 316      {
 317          $db->write_query("ALTER TABLE ".TABLE_PREFIX."privatemessages DROP recipients;");
 318      }
 319      $db->write_query("ALTER TABLE ".TABLE_PREFIX."privatemessages ADD recipients text NOT NULL AFTER fromid");
 320  
 321      if($db->field_exists('deletetime', "privatemessages"))
 322      {
 323          $db->write_query("ALTER TABLE ".TABLE_PREFIX."privatemessages DROP deletetime;");
 324      }
 325      $db->write_query("ALTER TABLE ".TABLE_PREFIX."privatemessages ADD deletetime bigint(30) NOT NULL default '0' AFTER dateline");
 326  
 327      if($db->field_exists('maxpmrecipients', "usergroups"))
 328      {
 329          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups DROP maxpmrecipients;");
 330      }
 331      $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups ADD maxpmrecipients int(4) NOT NULL default '5' AFTER pmquota");
 332  
 333      if($db->field_exists('canwarnusers', "usergroups"))
 334      {
 335          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups DROP canwarnusers;");
 336      }
 337      $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups ADD canwarnusers int(1) NOT NULL default '0' AFTER cancustomtitle");
 338  
 339      if($db->field_exists('lastip', "users"))
 340      {
 341          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP lastip;");
 342      }
 343      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD lastip varchar(50) NOT NULL default '' AFTER regip");
 344  
 345  
 346  
 347      if($db->field_exists('coppauser', "users"))
 348      {
 349          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP coppauser;");
 350      }
 351      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD coppauser int(1) NOT NULL default '0'");
 352  
 353      if($db->field_exists('classicpostbit', 'users'))
 354      {
 355          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP classicpostbit;");
 356      }
 357      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD classicpostbit int(1) NOT NULL  default '0'");
 358  
 359      if($db->field_exists('canreceivewarnings', "usergroups"))
 360      {
 361          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups DROP canreceivewarnings;");
 362      }
 363      $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups ADD canreceivewarnings int(1) NOT NULL default '0' AFTER canwarnusers");
 364  
 365      if($db->field_exists('maxwarningsday', "usergroups"))
 366      {
 367          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups DROP maxwarningsday;");
 368      }
 369      $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups ADD maxwarningsday int(3) NOT NULL default '3' AFTER canreceivewarnings");
 370  
 371      $db->update_query("usergroups", array('canreceivewarnings' => 1), "cancp != 1");
 372      $db->update_query("usergroups", array('maxwarningsday' => 3, 'canwarnusers' => 1), "cancp=1 OR issupermod=1 OR gid=6"); // Admins, Super Mods and Mods
 373  
 374      if($db->field_exists('canmodcp', "usergroups"))
 375      {
 376          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups DROP canmodcp;");
 377      }
 378      $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups ADD canmodcp int(1) NOT NULL default '0' AFTER maxwarningsday");
 379      $db->update_query("usergroups", array('canmodcp' => 1), "cancp=1 OR issupermod=1 OR gid='6'"); // Admins, Super Mods and Mods
 380  
 381      if($db->field_exists('newpms', "users"))
 382      {
 383          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP newpms;");
 384      }
 385  
 386      if($db->field_exists('keywords', "searchlog"))
 387      {
 388          $db->write_query("ALTER TABLE ".TABLE_PREFIX."searchlog DROP keywords;");
 389      }
 390      $db->write_query("ALTER TABLE ".TABLE_PREFIX."searchlog ADD keywords text NOT NULL AFTER querycache");
 391  
 392      if($db->field_exists('canaddpublicevents', "usergroups") && !$db->field_exists('canaddevents', "usergroups"))
 393      {
 394          $db->update_query("usergroups", array('canaddpublicevents' => 0), "canaddpublicevents='no'");
 395          $db->update_query("usergroups", array('canaddpublicevents' => 1), "canaddpublicevents='yes'");
 396          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups CHANGE canaddpublicevents canaddevents int(1) NOT NULL default '0';");
 397      }
 398  
 399      if($db->field_exists('canaddprivateevents', "usergroups"))
 400      {
 401          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups DROP canaddprivateevents;");
 402      }
 403  
 404      if($db->field_exists('canbypasseventmod', "usergroups"))
 405      {
 406          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups DROP canbypasseventmod;");
 407      }
 408      $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups ADD canbypasseventmod int(1) NOT NULL default '0' AFTER canaddevents;");
 409  
 410      if($db->field_exists('canmoderateevents', "usergroups"))
 411      {
 412          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups DROP canmoderateevents;");
 413      }
 414      $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups ADD canmoderateevents int(1) NOT NULL default '0' AFTER canbypasseventmod;");
 415      $db->update_query("usergroups", array('canbypasseventmod' => 1, 'canmoderateevents' => 1), "cancp=1 OR issupermod=1");
 416      $db->update_query("usergroups", array('canbypasseventmod' => 0, 'canmoderateevents' => 0), "cancp=0 AND issupermod=0");
 417      $db->update_query("usergroups", array('canaddevents' => 0), "gid='1'");
 418  
 419      $db->drop_table("maillogs");
 420      $db->drop_table("mailerrors");
 421      $db->drop_table("promotions");
 422      $db->drop_table("promotionlogs");
 423      $db->drop_table("massemails");
 424  
 425      $collation = $db->build_create_table_collation();
 426  
 427      $db->write_query("CREATE TABLE ".TABLE_PREFIX."massemails (
 428          mid int unsigned NOT NULL auto_increment,
 429          uid int unsigned NOT NULL default '0',
 430          subject varchar(200) NOT NULL default '',
 431          message text NOT NULL,
 432          htmlmessage text NOT NULL,
 433          type tinyint(1) NOT NULL default '0',
 434          format tinyint(1) NOT NULL default '0',
 435          dateline bigint(30) NOT NULL default '0',
 436          senddate bigint(30) NOT NULL default '0',
 437          status tinyint(1) NOT NULL default '0',
 438          sentcount int unsigned NOT NULL default '0',
 439          totalcount int unsigned NOT NULL default '0',
 440          conditions text NOT NULL,
 441          perpage smallint(4) NOT NULL default '50',
 442          PRIMARY KEY(mid)
 443      ) ENGINE=MyISAM{$collation};");
 444  
 445      $db->write_query("CREATE TABLE ".TABLE_PREFIX."maillogs (
 446          mid int unsigned NOT NULL auto_increment,
 447          subject varchar(200) not null default '',
 448          message TEXT NOT NULL,
 449          dateline bigint(30) NOT NULL default '0',
 450          fromuid int unsigned NOT NULL default '0',
 451          fromemail varchar(200) not null default '',
 452          touid bigint(30) NOT NULL default '0',
 453          toemail varchar(200) NOT NULL default '',
 454          tid int unsigned NOT NULL default '0',
 455          ipaddress varchar(20) NOT NULL default '',
 456          PRIMARY KEY(mid)
 457      ) ENGINE=MyISAM{$collation};");
 458  
 459      $db->write_query("CREATE TABLE ".TABLE_PREFIX."mailerrors(
 460          eid int unsigned NOT NULL auto_increment,
 461          subject varchar(200) NOT NULL default '',
 462          message TEXT NOT NULL,
 463          toaddress varchar(150) NOT NULL default '',
 464          fromaddress varchar(150) NOT NULL default '',
 465          dateline bigint(30) NOT NULL default '0',
 466          error text NOT NULL,
 467          smtperror varchar(200) NOT NULL default '',
 468          smtpcode int(5) NOT NULL default '0',
 469          PRIMARY KEY(eid)
 470       ) ENGINE=MyISAM{$collation};");
 471  
 472      $db->write_query("CREATE TABLE ".TABLE_PREFIX."promotions (
 473          pid int unsigned NOT NULL auto_increment,
 474          title varchar(120) NOT NULL default '',
 475          description text NOT NULL,
 476          enabled tinyint(1) NOT NULL default '1',
 477          logging tinyint(1) NOT NULL default '0',
 478          posts int NOT NULL default '0',
 479          posttype char(2) NOT NULL default '',
 480          registered int NOT NULL default '0',
 481          registeredtype varchar(20) NOT NULL default '',
 482          reputations int NOT NULL default '0',
 483          reputationtype char(2) NOT NULL default '',
 484          requirements varchar(200) NOT NULL default '',
 485          originalusergroup varchar(120) NOT NULL default '0',
 486          newusergroup smallint unsigned NOT NULL default '0',
 487          usergrouptype varchar(120) NOT NULL default '0',
 488          PRIMARY KEY (pid)
 489      ) ENGINE=MyISAM{$collation};");
 490  
 491      $db->write_query("CREATE TABLE ".TABLE_PREFIX."promotionlogs(
 492          plid int unsigned NOT NULL auto_increment,
 493          pid int unsigned NOT NULL default '0',
 494          uid int unsigned NOT NULL default '0',
 495          oldusergroup varchar(200) NOT NULL default '0',
 496          newusergroup smallint unsigned NOT NULL default '0',
 497          dateline bigint(30) NOT NULL default '0',
 498          type varchar(9) NOT NULL default 'primary',
 499          PRIMARY KEY(plid)
 500       ) ENGINE=MyISAM{$collation};");
 501  
 502      if($db->field_exists('maxemails', "usergroups"))
 503      {
 504          $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups DROP maxemails;");
 505      }
 506      $db->write_query("ALTER TABLE ".TABLE_PREFIX."usergroups ADD maxemails int(3) NOT NULL default '5' AFTER cansendemail");
 507  
 508      if($db->field_exists('parseorder', "mycode"))
 509      {
 510          $db->write_query("ALTER TABLE ".TABLE_PREFIX."mycode DROP parseorder;");
 511      }
 512      $db->write_query("ALTER TABLE ".TABLE_PREFIX."mycode ADD parseorder smallint unsigned NOT NULL default '0' AFTER active");
 513  
 514      if($db->field_exists('mod_edit_posts', "forums"))
 515      {
 516          $db->write_query("ALTER TABLE ".TABLE_PREFIX."forums DROP mod_edit_posts;");
 517      }
 518      $db->write_query("ALTER TABLE ".TABLE_PREFIX."forums ADD mod_edit_posts int(1) NOT NULL default '0' AFTER modthreads");
 519  
 520      if($db->field_exists('pmpopup', "users") && !$db->field_exists('pmnotice', "users"))
 521      {
 522          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users CHANGE pmpopup pmnotice int(1) NOT NULL default '0'");
 523      }
 524  
 525      $db->drop_table("tasks");
 526      $db->drop_table("tasklog");
 527  
 528      $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks (
 529          tid int unsigned NOT NULL auto_increment,
 530          title varchar(120) NOT NULL default '',
 531          description text NOT NULL,
 532          file varchar(30) NOT NULL default '',
 533          minute varchar(200) NOT NULL default '',
 534          hour varchar(200) NOT NULL default '',
 535          day varchar(100) NOT NULL default '',
 536          month varchar(30) NOT NULL default '',
 537          weekday varchar(15) NOT NULL default '',
 538          nextrun bigint(30) NOT NULL default '0',
 539          lastrun bigint(30) NOT NULL default '0',
 540          enabled int(1) NOT NULL default '1',
 541          logging int(1) NOT NULL default '0',
 542          locked bigint(30) NOT NULL default '0',
 543          PRIMARY KEY(tid)
 544      ) ENGINE=MyISAM{$collation};");
 545  
 546  
 547      $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasklog (
 548          lid int unsigned NOT NULL auto_increment,
 549          tid int unsigned NOT NULL default '0',
 550          dateline bigint(30) NOT NULL default '0',
 551          data text NOT NULL,
 552          PRIMARY KEY(lid)
 553      ) ENGINE=MyISAM{$collation};");
 554  
 555  
 556      include_once  MYBB_ROOT."inc/functions_task.php";
 557      $tasks = file_get_contents(INSTALL_ROOT.'resources/tasks.xml');
 558      $parser = create_xml_parser($tasks);
 559      $parser->collapse_dups = 0;
 560      $tree = $parser->get_tree();
 561  
 562      // Insert scheduled tasks
 563      foreach($tree['tasks'][0]['task'] as $task)
 564      {
 565          $new_task = array(
 566              'title' => $db->escape_string($task['title'][0]['value']),
 567              'description' => $db->escape_string($task['description'][0]['value']),
 568              'file' => $db->escape_string($task['file'][0]['value']),
 569              'minute' => $db->escape_string($task['minute'][0]['value']),
 570              'hour' => $db->escape_string($task['hour'][0]['value']),
 571              'day' => $db->escape_string($task['day'][0]['value']),
 572              'weekday' => $db->escape_string($task['weekday'][0]['value']),
 573              'month' => $db->escape_string($task['month'][0]['value']),
 574              'enabled' => $db->escape_string($task['enabled'][0]['value']),
 575              'logging' => $db->escape_string($task['logging'][0]['value'])
 576          );
 577  
 578          $new_task['nextrun'] = fetch_next_run($new_task);
 579  
 580          $db->insert_query("tasks", $new_task);
 581          $taskcount++;
 582      }
 583  
 584      if($db->table_exists("favorites") && !$db->table_exists("threadsubscriptions"))
 585      {
 586          $db->write_query("RENAME TABLE ".TABLE_PREFIX."favorites TO ".TABLE_PREFIX."threadsubscriptions");
 587      }
 588  
 589      if($db->field_exists('fid', "threadsubscriptions"))
 590      {
 591          $db->write_query("ALTER TABLE ".TABLE_PREFIX."threadsubscriptions CHANGE fid sid int unsigned NOT NULL auto_increment");
 592      }
 593  
 594      if($db->field_exists('type', "threadsubscriptions"))
 595      {
 596          $db->update_query("threadsubscriptions", array('type' => 0), "type='f'");
 597          $db->update_query("threadsubscriptions", array('type' => 1), "type='s'");
 598          $db->write_query("ALTER TABLE ".TABLE_PREFIX."threadsubscriptions CHANGE type notification int(1) NOT NULL default '0'");
 599      }
 600  
 601      if($db->field_exists('dateline', "threadsubscriptions"))
 602      {
 603          $db->write_query("ALTER TABLE ".TABLE_PREFIX."threadsubscriptions DROP dateline;");
 604      }
 605      $db->write_query("ALTER TABLE ".TABLE_PREFIX."threadsubscriptions ADD dateline bigint(30) NOT NULL default '0'");
 606  
 607      if($db->field_exists('subscriptionkey', "threadsubscriptions"))
 608      {
 609          $db->write_query("ALTER TABLE ".TABLE_PREFIX."threadsubscriptions DROP subscriptionkey;");
 610      }
 611      $db->write_query("ALTER TABLE ".TABLE_PREFIX."threadsubscriptions ADD subscriptionkey varchar(32) NOT NULL default ''");
 612  
 613      if($db->field_exists('emailnotify', "users"))
 614      {
 615          $db->update_query("users", array('emailnotify' => 0), "emailnotify='no' OR emailnotify='0'");
 616          $db->update_query("users", array('emailnotify' => 2), "emailnotify='yes' OR emailnotify='1'");
 617          $db->update_query("users", array('emailnotify' => 0), "emailnotify != 1 AND emailnotify != 2");
 618          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users CHANGE emailnotify subscriptionmethod int(1) NOT NULL default '0'");
 619      }
 620  
 621      $db->drop_table("warninglevels");
 622      $db->drop_table("warningtypes");
 623      $db->drop_table("warnings");
 624  
 625      $db->write_query("CREATE TABLE ".TABLE_PREFIX."warninglevels (
 626          lid int unsigned NOT NULL auto_increment,
 627          percentage int(3) NOT NULL default '0',
 628          action text NOT NULL,
 629          PRIMARY KEY(lid)
 630      ) ENGINE=MyISAM{$collation};");
 631  
 632      $db->write_query("CREATE TABLE ".TABLE_PREFIX."warningtypes (
 633          tid int unsigned NOT NULL auto_increment,
 634          title varchar(120) NOT NULL default '',
 635          points int unsigned NOT NULL default '0',
 636          expirationtime bigint(30) NOT NULL default '0',
 637          PRIMARY KEY(tid)
 638      ) ENGINE=MyISAM{$collation};");
 639  
 640      $db->write_query("CREATE TABLE ".TABLE_PREFIX."warnings (
 641          wid int unsigned NOT NULL auto_increment,
 642          uid int unsigned NOT NULL default '0',
 643          tid int unsigned NOT NULL default '0',
 644          pid int unsigned NOT NULL default '0',
 645          title varchar(120) NOT NULL default '',
 646          points int unsigned NOT NULL default '0',
 647          dateline bigint(30) NOT NULL default '0',
 648          issuedby int unsigned NOT NULL default '0',
 649          expires bigint(30) NOT NULL default '0',
 650          expired int(1) NOT NULL default '0',
 651          daterevoked bigint(30) NOT NULL default '0',
 652          revokedby int unsigned NOT NULL default '0',
 653          revokereason text NOT NULL,
 654          notes text NOT NULL,
 655          PRIMARY KEY(wid)
 656      ) ENGINE=MyISAM{$collation};");
 657  
 658      if($db->field_exists('warningpoints', "users"))
 659      {
 660          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP warningpoints;");
 661      }
 662      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD warningpoints int(3) NOT NULL default '0' AFTER unreadpms");
 663  
 664      if($db->field_exists('moderateposts', "users"))
 665      {
 666          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP moderateposts;");
 667      }
 668      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD moderateposts int(1) NOT NULL default '0' AFTER warningpoints");
 669  
 670      if($db->field_exists('moderationtime', "users"))
 671      {
 672          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP moderationtime;");
 673      }
 674      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD moderationtime bigint(30) NOT NULL default '0' AFTER moderateposts");
 675  
 676      if($db->field_exists('suspendposting', "users"))
 677      {
 678          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP suspendposting;");
 679      }
 680      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD suspendposting int(1) NOT NULL default '0' AFTER moderationtime");
 681  
 682      if($db->field_exists('suspensiontime', "users"))
 683      {
 684          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP suspensiontime;");
 685      }
 686      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD suspensiontime bigint(30) NOT NULL default '0' AFTER suspendposting");
 687  
 688      $db->write_query("ALTER TABLE ".TABLE_PREFIX."banned CHANGE oldadditionalgroups oldadditionalgroups TEXT NOT NULL");
 689  
 690      if($db->field_exists('birthdayprivacy', "users"))
 691      {
 692          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP birthdayprivacy;");
 693      }
 694      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD birthdayprivacy varchar(4) NOT NULL default 'all' AFTER birthday");
 695  
 696      if($db->field_exists('birthdayprivacy', "users"))
 697      {
 698          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP birthdayprivacy;");
 699      }
 700      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD birthdayprivacy varchar(4) NOT NULL default 'all' AFTER birthday");
 701  
 702      if($db->field_exists('longregip', "users"))
 703      {
 704          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP longregip;");
 705      }
 706      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD longregip int(11) NOT NULL default '0' AFTER lastip");
 707  
 708      if($db->field_exists('longlastip', "users"))
 709      {
 710          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP longlastip;");
 711      }
 712      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD longlastip int(11) NOT NULL default '0' AFTER lastip");
 713  
 714      // Unused column
 715      if($db->field_exists('titles', "searchlog"))
 716      {
 717          $db->write_query("ALTER TABLE ".TABLE_PREFIX."searchlog DROP titles;");
 718      }
 719  
 720      $db->drop_table("adminlog");
 721      $db->write_query("CREATE TABLE ".TABLE_PREFIX."adminlog (
 722        uid int unsigned NOT NULL default '0',
 723        ipaddress varchar(50) NOT NULL default '',
 724        dateline bigint(30) NOT NULL default '0',
 725        module varchar(50) NOT NULL default '',
 726        action varchar(50) NOT NULL default '',
 727        data text NOT NULL,
 728        KEY module (module, action)
 729      ) ENGINE=MyISAM{$collation};");
 730  
 731      if($db->field_exists('data', "adminsessions"))
 732      {
 733          $db->write_query("ALTER TABLE ".TABLE_PREFIX."adminsessions DROP data;");
 734      }
 735  
 736      $db->write_query("ALTER TABLE ".TABLE_PREFIX."adminsessions ADD data TEXT NOT NULL AFTER lastactive;");
 737  
 738      if($db->field_exists('isdefault', "settings"))
 739      {
 740          $db->write_query("ALTER TABLE ".TABLE_PREFIX."settings DROP isdefault;");
 741      }
 742      $db->write_query("ALTER TABLE ".TABLE_PREFIX."settings ADD isdefault int(1) NOT NULL default '0' AFTER gid;");
 743  
 744      $setting_group_cache = array();
 745      $query = $db->simple_select("settinggroups", "gid, isdefault");
 746      while($settinggroup = $db->fetch_array($query))
 747      {
 748          $setting_group_cache[$settinggroup['gid']] = $settinggroup['isdefault'];
 749      }
 750  
 751      $query = $db->simple_select("settings", "gid, sid");
 752      while($setting = $db->fetch_array($query))
 753      {
 754          if($setting_group_cache[$setting['gid']] == 1)
 755          {
 756              $db->update_query("settings", array('isdefault' => 1), "sid = '{$setting['sid']}'", 1);
 757          }
 758      }
 759  
 760      $db->update_query("settings", array('value' => 'classic'), "name='postlayout' AND value != 'horizontal'");
 761  
 762      $db->update_query("settings", array('optionscode' => $db->escape_string('php
 763  <select name=\"upsetting[{$setting[\'name\']}]\"><option value=\"standard\">".(isset($lang->setting_searchtype_standard)?$lang->setting_searchtype_standard:"Standard")."</option>".($db->supports_fulltext("threads") && $db->supports_fulltext_boolean("posts")?"<option value=\"fulltext\"".($setting[\'value\']=="fulltext"?" selected=\"selected\"":"").">".(isset($lang->setting_searchtype_fulltext)?$lang->setting_searchtype_fulltext:"Full Text")."</option>":"")."</select>')), "name='searchtype'", 1);
 764  
 765      $contents = "Done</p>";
 766      $contents .= "<p>Click next to continue with the upgrade process.</p>";
 767      $output->print_contents($contents);
 768  
 769      global $footer_extra;
 770      $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>";
 771  
 772      $output->print_footer("12_dbchanges3");
 773  }
 774  
 775  function upgrade12_dbchanges3()
 776  {
 777      global $db, $output, $mybb;
 778  
 779      $output->print_header("Converting Ban Filters");
 780  
 781      echo "<p>Converting existing banned IP addresses, email addresses and usernames..</p>";
 782      flush();
 783  
 784      $db->drop_table("banfilters");
 785  
 786      $collation = $db->build_create_table_collation();
 787  
 788      $db->write_query("CREATE TABLE ".TABLE_PREFIX."banfilters (
 789        fid int unsigned NOT NULL auto_increment,
 790        filter varchar(200) NOT NULL default '',
 791        type int(1) NOT NULL default '0',
 792        lastuse bigint(30) NOT NULL default '0',
 793        dateline bigint(30) NOT NULL default '0',
 794        PRIMARY KEY (fid)
 795      ) ENGINE=MyISAM{$collation};");
 796  
 797      // Now we convert all of the old bans in to the new system!
 798      $ban_types = array('bannedips', 'bannedemails', 'bannedusernames');
 799      foreach($ban_types as $type)
 800      {
 801          // Some people put spaces or breaks (\r\n) instead, so we should take that into account.
 802          $mybb->settings[$type] = str_replace(array("\n", "\r\n", "\r"), ",", $mybb->settings[$type]);
 803  
 804          // Usernames can have spaces so don't replace those with commas.
 805          if($type != 'bannedusernames')
 806          {
 807              $mybb->settings[$type] = str_replace(" ", ",", $mybb->settings[$type]);
 808          }
 809  
 810          $bans = explode(",", $mybb->settings[$type]);
 811          $bans = array_unique($bans);
 812          $bans = array_map("trim", $bans);
 813          foreach($bans as $ban)
 814          {
 815              if(!$ban)
 816              {
 817                  continue;
 818              }
 819  
 820              if($type == "bannedips")
 821              {
 822                  $ban_type = 1;
 823              }
 824              else if($type == "bannedusernames")
 825              {
 826                  $ban_type = 2;
 827              }
 828              else if($type == "bannedemails")
 829              {
 830                  $ban_type = 3;
 831              }
 832              $new_ban = array(
 833                  "filter" => $db->escape_string($ban),
 834                  "type" => $ban_type,
 835                  "dateline" => TIME_NOW
 836              );
 837              $db->insert_query("banfilters", $new_ban);
 838          }
 839      }
 840  
 841      $contents = "Done</p>";
 842      $contents .= "<p>Click next to continue with the upgrade process.</p>";
 843      $output->print_contents($contents);
 844  
 845      global $footer_extra;
 846      $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>";
 847  
 848      $output->print_footer("12_dbchanges4");
 849  }
 850  
 851  function upgrade12_dbchanges4()
 852  {
 853      global $db, $output, $mybb;
 854  
 855      $output->print_header("Performing Queries");
 856  
 857      echo "<p>Performing necessary upgrade queries..</p>";
 858      flush();
 859  
 860      $db->drop_table("spiders");
 861      $db->drop_table("stats");
 862  
 863      $collation = $db->build_create_table_collation();
 864  
 865      $db->write_query("CREATE TABLE ".TABLE_PREFIX."spiders (
 866          sid int unsigned NOT NULL auto_increment,
 867          name varchar(100) NOT NULL default '',
 868          theme int unsigned NOT NULL default '0',
 869          language varchar(20) NOT NULL default '',
 870          usergroup int unsigned NOT NULL default '0',
 871          useragent varchar(200) NOT NULL default '',
 872          lastvisit bigint(30) NOT NULL default '0',
 873          PRIMARY KEY(sid)
 874      ) ENGINE=MyISAM{$collation};");
 875  
 876      $db->write_query("CREATE TABLE ".TABLE_PREFIX."stats (
 877          dateline bigint(30) NOT NULL default '0',
 878          numusers int unsigned NOT NULL default '0',
 879          numthreads int unsigned NOT NULL default '0',
 880          numposts int unsigned NOT NULL default '0',
 881          PRIMARY KEY(dateline)
 882      ) ENGINE=MyISAM{$collation};");
 883  
 884      $db->insert_query("spiders", array('name' => 'GoogleBot', 'useragent' => 'google'));
 885      $db->insert_query("spiders", array('name' => 'Lycos', 'useragent' => 'lycos'));
 886      $db->insert_query("spiders", array('name' => 'Ask Jeeves', 'useragent' => 'ask jeeves'));
 887      $db->insert_query("spiders", array('name' => 'Hot Bot', 'useragent' => 'slurp@inktomi'));
 888      $db->insert_query("spiders", array('name' => 'What You Seek', 'useragent' => 'whatuseek'));
 889      $db->insert_query("spiders", array('name' => 'Archive.org', 'useragent' => 'is_archiver'));
 890      $db->insert_query("spiders", array('name' => 'Altavista', 'useragent' => 'scooter'));
 891      $db->insert_query("spiders", array('name' => 'Alexa', 'useragent' => 'ia_archiver'));
 892      $db->insert_query("spiders", array('name' => 'MSN Search', 'useragent' => 'msnbot'));
 893      $db->insert_query("spiders", array('name' => 'Yahoo!', 'useragent' => 'yahoo! slurp'));
 894  
 895      // DST correction changes
 896      $db->update_query("users", array('dst' => 1), "dst=1");
 897      $db->update_query("users", array('dst' => 0), "dst=0");
 898      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users CHANGE dst dst INT(1) NOT NULL default '0'");
 899      if($db->field_exists('dstcorrection', "users"))
 900      {
 901          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP dstcorrection;");
 902      }
 903      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD dstcorrection INT(1) NOT NULL default '0' AFTER dst");
 904  
 905      $db->update_query("users", array('dstcorrection' => 2));
 906  
 907      $db->update_query("adminoptions", array('cpstyle' => ''));
 908  
 909      if($db->field_exists('permsset', "adminoptions") && !$db->field_exists('permissions', "adminoptions"))
 910      {
 911          $db->write_query("ALTER TABLE ".TABLE_PREFIX."adminoptions CHANGE permsset permissions TEXT NOT NULL ");
 912      }
 913  
 914      $adminoptions = file_get_contents(INSTALL_ROOT.'resources/adminoptions.xml');
 915      $parser = create_xml_parser($adminoptions);
 916      $parser->collapse_dups = 0;
 917      $tree = $parser->get_tree();
 918  
 919      // Fetch default permissions list
 920      $default_permissions = array();
 921      foreach($tree['adminoptions'][0]['user'] as $users)
 922      {
 923          $uid = $users['attributes']['uid'];
 924          if($uid == -4)
 925          {
 926              foreach($users['permissions'][0]['module'] as $module)
 927              {
 928                  foreach($module['permission'] as $permission)
 929                  {
 930                      $default_permissions[$module['attributes']['name']][$permission['attributes']['name']] = $permission['value'];
 931                  }
 932              }
 933              break;
 934          }
 935      }
 936  
 937      $convert_permissions = array(
 938          "caneditsettings" => array(
 939                  "module" => "config",
 940                  "permission" => "settings"
 941              ),
 942          "caneditann" => array(
 943                  "module" => "forum",
 944                  "permission" => "announcements",
 945              ),
 946          "caneditforums" => array(
 947                  "module" => "forum",
 948                  "permission" => "management",
 949              ),
 950          "canmodposts" => array(
 951                  "module" => "forum",
 952                  "permission" => "moderation_queue",
 953              ),
 954          "caneditsmilies" => array(
 955                  "module" => "config",
 956                  "permission" => "smilies",
 957              ),
 958          "caneditpicons" => array(
 959                  "module" => "config",
 960                  "permission" => "post_icons",
 961              ),
 962          "caneditthemes" => array(
 963                  "module" => "style",
 964                  "permission" => "themes",
 965              ),
 966          "canedittemps" => array(
 967                  "module" => "style",
 968                  "permission" => "templates",
 969              ),
 970          "caneditusers" => array(
 971                  "module" => "user",
 972                  "permission" => "view",
 973              ),
 974          "caneditpfields" => array(
 975                  "module" => "config",
 976                  "permission" => "profile_fields",
 977              ),
 978          "caneditmodactions" => array(
 979                  "module" => "config",
 980                  "permission" => "mod_tools",
 981              ),
 982          "caneditugroups" => array(
 983                  "module" => "user",
 984                  "permission" => "groups",
 985              ),
 986          "caneditaperms" => array(
 987                  "module" => "user",
 988                  "permission" => "admin_permissions",
 989              ),
 990          "caneditutitles" => array(
 991                  "module" => "user",
 992                  "permission" => "titles",
 993              ),
 994          "caneditattach" => array(
 995                  "module" => "forum",
 996                  "permission" => "attachments",
 997              ),
 998          "canedithelp" => array(
 999                  "module" => "config",
1000                  "permission" => "help_documents",
1001              ),
1002          "caneditlangs" => array(
1003                  "module" => "config",
1004                  "permission" => "languages",
1005              ),
1006          "canrunmaint" => array(
1007                  "module" => "tools",
1008                  "permission" => "recount_rebuild",
1009              ),
1010          "canrundbtools" => array(
1011                  "module" => "tools",
1012                  "permission" => "backupdb",
1013              ),
1014      );
1015  
1016      $new_permissions = $default_permissions;
1017  
1018      $query = $db->simple_select("adminoptions");
1019      while($adminoption = $db->fetch_array($query))
1020      {
1021          foreach($adminoption as $field => $value)
1022          {
1023              if(strtolower(substr($field, 0, 3)) != "can")
1024              {
1025                  continue;
1026              }
1027  
1028              if(array_key_exists($field, $convert_permissions))
1029              {
1030                  // Note: old adminoptions table is still yes/no - do not change me
1031                  if($value == "yes")
1032                  {
1033                      $value = 1;
1034                  }
1035                  else
1036                  {
1037                      $value = $default_permissions[$convert_permissions[$field]['module']][$convert_permissions[$field]['permission']];
1038                  }
1039                  $new_permissions[$convert_permissions[$field]['module']][$convert_permissions[$field]['permission']] = $value;
1040              }
1041          }
1042  
1043          $db->update_query("adminoptions", array('permissions' => my_serialize($new_permissions)), "uid = '{$adminoption['uid']}'");
1044  
1045          $new_permissions = $default_permissions;
1046      }
1047  
1048      foreach($convert_permissions as $field => $value)
1049      {
1050          if($db->field_exists($field, "adminoptions"))
1051          {
1052              $db->write_query("ALTER TABLE ".TABLE_PREFIX."adminoptions DROP {$field}");
1053          }
1054      }
1055  
1056      // Set default views
1057      if($db->field_exists('defaultviews', "adminoptions"))
1058      {
1059          $db->write_query("ALTER TABLE ".TABLE_PREFIX."adminoptions DROP defaultviews");
1060      }
1061      $db->write_query("ALTER TABLE ".TABLE_PREFIX."adminoptions ADD defaultviews TEXT NOT NULL");
1062      $db->update_query("adminoptions", array('defaultviews' => my_serialize(array('user' => 1))));
1063  
1064      $query = $db->simple_select("forums", "SUM(threads) AS numthreads, SUM(posts) AS numposts, SUM(unapprovedthreads) AS numunapprovedthreads, SUM(unapprovedposts) AS numunapprovedposts");
1065      $stats = $db->fetch_array($query);
1066  
1067      $query = $db->simple_select("users", "COUNT(uid) AS users");
1068      $stats['numusers'] = $db->fetch_field($query, 'users');
1069  
1070      update_stats($stats, true);
1071  
1072      $contents = "Done</p>";
1073      $contents .= "<p>Click next to continue with the upgrade process.</p>";
1074      $output->print_contents($contents);
1075  
1076      global $footer_extra;
1077      $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>";
1078  
1079      $output->print_footer("12_dbchanges5");
1080  }
1081  
1082  function upgrade12_dbchanges5()
1083  {
1084      global $db, $output, $mybb;
1085  
1086      $output->print_header("Performing Queries");
1087  
1088      echo "<p>Performing necessary upgrade queries..</p>";
1089      flush();
1090  
1091      $db->drop_table("templategroups");
1092      $db->write_query("CREATE TABLE ".TABLE_PREFIX."templategroups (
1093          gid int unsigned NOT NULL auto_increment,
1094          prefix varchar(50) NOT NULL default '',
1095          title varchar(100) NOT NULL default '',
1096          PRIMARY KEY (gid)
1097          ) ENGINE=MyISAM{$collation};");
1098  
1099      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('1','calendar','<lang:group_calendar>');");
1100      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('2','editpost','<lang:group_editpost>');");
1101      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('3','forumbit','<lang:group_forumbit>');");
1102      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('4','forumjump','<lang:group_forumjump>');");
1103      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('5','forumdisplay','<lang:group_forumdisplay>');");
1104      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('6','index','<lang:group_index>');");
1105      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('7','error','<lang:group_error>');");
1106      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('8','memberlist','<lang:group_memberlist>');");
1107      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('9','multipage','<lang:group_multipage>');");
1108      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('10','private','<lang:group_private>');");
1109      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('11','portal','<lang:group_portal>');");
1110      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('12','postbit','<lang:group_postbit>');");
1111      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('13','redirect','<lang:group_redirect>');");
1112      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('14','showthread','<lang:group_showthread>');");
1113      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('15','usercp','<lang:group_usercp>');");
1114      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('16','online','<lang:group_online>');");
1115      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('17','moderation','<lang:group_moderation>');");
1116      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('18','nav','<lang:group_nav>');");
1117      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('19','search','<lang:group_search>');");
1118      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('20','showteam','<lang:group_showteam>');");
1119      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('21','reputation','<lang:group_reputation>');");
1120      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('22','newthread','<lang:group_newthread>');");
1121      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('23','newreply','<lang:group_newreply>');");
1122      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('24','member','<lang:group_member>');");
1123      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('25','warnings','<lang:group_warning>');");
1124      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('26','global','<lang:group_global>');");
1125      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('27','header','<lang:group_header>');");
1126      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('28','managegroup','<lang:group_managegroup>');");
1127      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('29','misc','<lang:group_misc>');");
1128      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('30','modcp','<lang:group_modcp>');");
1129      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('31','php','<lang:group_php>');");
1130      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('32','polls','<lang:group_polls>');");
1131      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('33','post','<lang:group_post>');");
1132      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('34','printthread','<lang:group_printthread>');");
1133      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('35','report','<lang:group_report>');");
1134      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('36','smilieinsert','<lang:group_smilieinsert>');");
1135      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('37','stats','<lang:group_stats>');");
1136      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('38','xmlhttp','<lang:group_xmlhttp>');");
1137      $db->write_query("INSERT INTO ".TABLE_PREFIX."templategroups (gid,prefix,title) VALUES ('39','footer','<lang:group_footer>');");
1138  
1139      $query = $db->query("SHOW INDEX FROM ".TABLE_PREFIX."users");
1140      while($ukey = $db->fetch_array($query))
1141      {
1142          if($ukey['Key_name'] == "username")
1143          {
1144              $index = $ukey;
1145              break;
1146          }
1147      }
1148      if($index)
1149      {
1150          $db->write_query("ALTER TABLE ".TABLE_PREFIX."users DROP KEY username");
1151      }
1152      $db->write_query("ALTER TABLE ".TABLE_PREFIX."users ADD UNIQUE KEY username (username)");
1153  
1154      if($db->field_exists('statustime', "privatemessages"))
1155      {
1156          $db->write_query("ALTER TABLE ".TABLE_PREFIX."privatemessages DROP statustime;");
1157      }
1158      $db->write_query("ALTER TABLE ".TABLE_PREFIX."privatemessages ADD statustime bigint(30) NOT NULL default '0' AFTER status");
1159  
1160      $collation = $db->build_create_table_collation();
1161  
1162      $db->drop_table("calendars");
1163      $db->drop_table("calendarpermissions");
1164      $db->drop_table("forumsread");
1165      $db->drop_table("adminviews");
1166      $db->drop_table("threadviews");
1167  
1168      $db->write_query("CREATE TABLE ".TABLE_PREFIX."threadviews (
1169          tid int unsigned NOT NULL default '0'
1170      ) ENGINE=MyISAM{$collation};");
1171  
1172      $db->write_query("CREATE TABLE ".TABLE_PREFIX."calendars (
1173        cid int unsigned NOT NULL auto_increment,
1174        name varchar(100) NOT NULL default '',
1175        disporder int unsigned NOT NULL default '0',
1176        startofweek int(1) NOT NULL default '0',
1177        showbirthdays int(1) NOT NULL default '0',
1178        eventlimit int(3) NOT NULL default '0',
1179        moderation int(1) NOT NULL default '0',
1180        allowhtml int(1) NOT NULL default '0',
1181        allowmycode int(1) NOT NULL default '0',
1182        allowimgcode int(1) NOT NULL default '0',
1183        allowsmilies int(1) NOT NULL default '0',
1184        PRIMARY KEY(cid)
1185      ) ENGINE=MyISAM{$collation};");
1186  
1187      $calendar_array = array(
1188          'name' => 'Default Calendar',
1189          'disporder' => 1,
1190          'startofweek' => 0,
1191          'showbirthdays' => 1,
1192          'eventlimit' => 4,
1193          'moderation' => 0,
1194          'allowhtml' => 0,
1195          'allowmycode' => 1,
1196          'allowimgcode' => 1,
1197          'allowsmilies' => 1
1198      );
1199      $db->insert_query("calendars", $calendar_array);
1200  
1201      $db->write_query("CREATE TABLE ".TABLE_PREFIX."calendarpermissions (
1202        cid int unsigned NOT NULL default '0',
1203        gid int unsigned NOT NULL default '0',
1204        canviewcalendar int(1) NOT NULL default '0',
1205        canaddevents int(1) NOT NULL default '0',
1206        canbypasseventmod int(1) NOT NULL default '0',
1207        canmoderateevents int(1) NOT NULL default '0'
1208      ) ENGINE=MyISAM{$collation};");
1209  
1210      $db->write_query("CREATE TABLE ".TABLE_PREFIX."forumsread (
1211        fid int unsigned NOT NULL default '0',
1212        uid int unsigned NOT NULL default '0',
1213        dateline int(10) NOT NULL default '0',
1214        KEY dateline (dateline),
1215        UNIQUE KEY fid (fid,uid)
1216      ) ENGINE=MyISAM{$collation};");
1217  
1218      if($db->field_exists('dateuploaded', "attachments"))
1219      {
1220          $db->write_query("ALTER TABLE ".TABLE_PREFIX."attachments DROP dateuploaded;");
1221      }
1222      $db->write_query("ALTER TABLE ".TABLE_PREFIX."attachments ADD dateuploaded bigint(30) NOT NULL default '0' AFTER downloads");
1223  
1224      $db->write_query("CREATE TABLE ".TABLE_PREFIX."adminviews (
1225          vid int unsigned NOT NULL auto_increment,
1226          uid int unsigned NOT NULL default '0',
1227          title varchar(100) NOT NULL default '',
1228          type varchar(6) NOT NULL default '',
1229          visibility int(1) NOT NULL default '0',
1230          `fields` text NOT NULL,
1231          conditions text NOT NULL,
1232          sortby varchar(20) NOT NULL default '',
1233          sortorder varchar(4) NOT NULL default '',
1234          perpage int(4) NOT NULL default '0',
1235          view_type varchar(6) NOT NULL default '',
1236          PRIMARY KEY(vid)
1237      ) ENGINE=MyISAM{$collation};");
1238  
1239      $views = file_get_contents(INSTALL_ROOT.'resources/adminviews.xml');
1240      $parser = create_xml_parser($views);
1241      $parser->collapse_dups = 0;
1242      $tree = $parser->get_tree();
1243  
1244      // Insert admin views
1245      foreach($tree['adminviews'][0]['view'] as $view)
1246      {
1247          $fields = array();
1248          foreach($view['fields'][0]['field'] as $field)
1249          {
1250              $fields[] = $field['attributes']['name'];
1251          }
1252          $conditions = array();
1253          if($view['conditions'][0]['condition'])
1254          {
1255              foreach($view['conditions'][0]['condition'] as $condition)
1256              {
1257                  if(!$condition['value']) continue;
1258                  if($condition['attributes']['is_serialized'] == 1)
1259                  {
1260                      $condition['value'] = my_unserialize($condition['value']);
1261                  }
1262                  $conditions[$condition['attributes']['name']] = $condition['value'];
1263              }
1264          }
1265          $new_view = array(
1266              "uid" => 0,
1267              "type" => $db->escape_string($view['attributes']['type']),
1268              "visibility" => (int)$view['attributes']['visibility'],
1269              "title" => $db->escape_string($view['title'][0]['value']),
1270              "fields" => $db->escape_string(my_serialize($fields)),
1271              "conditions" => $db->escape_string(my_serialize($conditions)),
1272              "sortby" => $db->escape_string($view['sortby'][0]['value']),
1273              "sortorder" => $db->escape_string($view['sortorder'][0]['value']),
1274              "perpage" => (int)$view['perpage'][0]['value'],
1275              "view_type" => $db->escape_string($view['view_type'][0]['value'])
1276          );
1277          $db->insert_query("adminviews", $new_view);
1278          $view_count++;
1279      }
1280  
1281      $db->simple_select("users", "uid", "avatar != '' AND avatardimensions = ''");
1282      while($user = $db->fetch_array($query))
1283      {
1284          $db->update_query("users", array('avatardimensions' => $avatardimensions), "uid='{$user['uid']}'", 1);
1285      }
1286  
1287      $contents = "Done</p>";
1288      $contents .= "<p>Click next to continue with the upgrade process.</p>";
1289      $output->print_contents($contents);
1290  
1291      global $footer_extra;
1292      $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>";
1293  
1294      $output->print_footer("12_redoconfig");
1295  }
1296  
1297  function upgrade12_redoconfig()
1298  {
1299      global $db, $output, $orig_config, $mybb;
1300  
1301      $config = $orig_config;
1302  
1303      $output->print_header("Rewriting config.php");
1304  
1305      if(!is_array($config['database']))
1306      {
1307          // Backup our old Config file
1308          @copy(MYBB_ROOT."inc/config.php", MYBB_ROOT."inc/config.backup.php");
1309  
1310          $fh = @fopen(MYBB_ROOT."inc/config.php", "w");
1311          if(!$fh)
1312          {
1313              echo "<p><span style=\"color: red; font-weight: bold;\">Unable to open inc/config.php</span><br />Before the upgrade process can continue, you need to changes the permissions of inc/config.php so it is writable.</p>";
1314              $output->print_footer("12_redoconfig");
1315              exit;
1316          }
1317  
1318          if(!$config['memcache_host'])
1319          {
1320              $config['memcache_host'] = "localhost";
1321          }
1322  
1323          if(!$config['memcache_port'])
1324          {
1325              $config['memcache_port'] = 11211;
1326          }
1327  
1328          $comment = "";
1329  
1330          if(!$db->db_encoding || !$config['db_encoding'])
1331          {
1332              $comment = " // ";
1333          }
1334  
1335          if(!$config['db_encoding'])
1336          {
1337              $config['db_encoding'] = "utf8";
1338          }
1339  
1340          $configdata = "<?php
1341  /**
1342   * Database configuration
1343   *
1344   * Please see the MyBB Docs for advanced
1345   * database configuration for larger installations
1346   * https://docs.mybb.com/
1347   */
1348  
1349  \$config['database']['type'] = '{$config['dbtype']}';
1350  \$config['database']['database'] = '{$config['database']}';
1351  \$config['database']['table_prefix'] = '{$config['table_prefix']}';
1352  
1353  \$config['database']['hostname'] = '{$config['hostname']}';
1354  \$config['database']['username'] = '{$config['username']}';
1355  \$config['database']['password'] = '{$config['password']}';
1356  
1357  /**
1358   * Admin CP directory
1359   *  For security reasons, it is recommended you
1360   *  rename your Admin CP directory. You then need
1361   *  to adjust the value below to point to the
1362   *  new directory.
1363   */
1364  
1365  \$config['admin_dir'] = '{$config['admin_dir']}';
1366  
1367  /**
1368   * Hide all Admin CP links
1369   *  If you wish to hide all Admin CP links
1370   *  on the front end of the board after
1371   *  renaming your Admin CP directory, set this
1372   *  to 1.
1373   */
1374  
1375  \$config['hide_admin_links'] = {$config['hide_admin_links']};
1376  
1377  /**
1378   * Data-cache configuration
1379   *  The data cache is a temporary cache
1380   *  of the most commonly accessed data in MyBB.
1381   *  By default, the database is used to store this data.
1382   *
1383   *  If you wish to use the file system (cache/ directory)
1384   *  you can change the value below to 'files', 'memcache' or 'eaccelerator' from 'db'.
1385   */
1386  
1387  \$config['cache_store'] = '{$config['cache_store']}';
1388  
1389  /**
1390   * Memcache configuration
1391   *  If you are using memcache as your data-cache,
1392   *  you need to configure the hostname and port
1393   *  of your memcache server below.
1394   *
1395   * If not using memcache, ignore this section.
1396   */
1397  
1398  \$config['memcache_host'] = '{$config['memcache_host']}';
1399  \$config['memcache_port'] = {$config['memcache_port']};
1400  
1401  /**
1402   * Super Administrators
1403   *  A comma separated list of user IDs who cannot
1404   *  be edited, deleted or banned in the Admin CP.
1405   *  The administrator permissions for these users
1406   *  cannot be altered either.
1407   */
1408  
1409  \$config['super_admins'] = '{$config['super_admins']}';
1410  
1411  /**
1412   * Database Encoding
1413   *  If you wish to set an encoding for MyBB uncomment
1414   *  the line below (if it isn't already) and change
1415   *  the current value to the mysql charset:
1416   *  http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html
1417   */
1418  
1419  {$comment}\$config['database']['encoding'] = '{$config['db_encoding']}';
1420  
1421  /**
1422   * Automatic Log Pruning
1423   *  The MyBB task system can automatically prune
1424   *  various log files created by MyBB.
1425   *  To enable this functionality for the logs below, set the
1426   *  the number of days before each log should be pruned.
1427   *  If you set the value to 0, the logs will not be pruned.
1428   */
1429  
1430  \$config['log_pruning'] = array(
1431      'admin_logs' => 365, // Administrator logs
1432      'mod_logs' => 0, // Moderator logs
1433      'task_logs' => 30, // Scheduled task logs
1434      'mail_logs' => 180, // Mail error logs
1435      'user_mail_logs' => 180, // User mail logs
1436      'promotion_logs' => 180 // Promotion logs
1437  );
1438  
1439  ?".">";
1440          fwrite($fh, $configdata);
1441          fclose($fh);
1442      }
1443      echo "<p>The configuration file has successfully been rewritten.</p>";
1444      echo "<p>Click next to continue with the upgrade process.</p>";
1445  
1446      global $footer_extra;
1447      $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>";
1448  
1449      $output->print_footer("12_dbchanges6");
1450  }
1451  
1452  function upgrade12_dbchanges6()
1453  {
1454      global $db, $output;
1455  
1456      $output->print_header("Post IP Conversion");
1457  
1458      if(!$_POST['ipspage'])
1459      {
1460          $ipp = 5000;
1461      }
1462      else
1463      {
1464          $ipp = (int)$_POST['ipspage'];
1465      }
1466  
1467      if($_POST['ipstart'])
1468      {
1469          $startat = (int)$_POST['ipstart'];
1470          $upper = $startat+$ipp;
1471          $lower = $startat;
1472      }
1473      else
1474      {
1475          $startat = 0;
1476          $upper = $ipp;
1477          $lower = 1;
1478      }
1479  
1480      $query = $db->simple_select("posts", "COUNT(pid) AS ipcount");
1481      $cnt = $db->fetch_array($query);
1482  
1483      if($upper > $cnt['ipcount'])
1484      {
1485          $upper = $cnt['ipcount'];
1486      }
1487  
1488      echo "<p>Converting ip {$lower} to {$upper} ({$cnt['ipcount']} Total)</p>";
1489      flush();
1490  
1491      $ipaddress = false;
1492  
1493      $query = $db->simple_select("posts", "ipaddress, longipaddress, pid", "", array('limit_start' => $lower, 'limit' => $ipp));
1494      while($post = $db->fetch_array($query))
1495      {
1496          // Have we already converted this ip?
1497          if(!$post['longipaddress'])
1498          {
1499              $db->update_query("posts", array('longipaddress' => my_ip2long($post['ipaddress'])), "pid = '{$post['pid']}'");
1500          }
1501          $ipaddress = true;
1502      }
1503  
1504      $remaining = $upper-$cnt['ipcount'];
1505      if($remaining && $ipaddress)
1506      {
1507          $nextact = "12_dbchanges6";
1508          $startat = $startat+$ipp;
1509          $contents = "<p><input type=\"hidden\" name=\"ipspage\" value=\"$ipp\" /><input type=\"hidden\" name=\"ipstart\" value=\"$startat\" />Done. Click Next to move on to the next set of post ips.</p>";
1510      }
1511      else
1512      {
1513          $nextact = "12_dbchanges7";
1514          $contents = "<p>Done</p><p>All post ips have been converted to the new ip format. Click next to continue.</p>";
1515      }
1516      $output->print_contents($contents);
1517  
1518      global $footer_extra;
1519      $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>";
1520  
1521      $output->print_footer($nextact);
1522  }
1523  
1524  function upgrade12_dbchanges7()
1525  {
1526      global $db, $output;
1527  
1528      $output->print_header("User IP Conversion");
1529  
1530      if(!$_POST['ipspage'])
1531      {
1532          $ipp = 5000;
1533      }
1534      else
1535      {
1536          $ipp = (int)$_POST['ipspage'];
1537      }
1538  
1539      if($_POST['ipstart'])
1540      {
1541          $startat = (int)$_POST['ipstart'];
1542          $upper = $startat+$ipp;
1543          $lower = $startat;
1544      }
1545      else
1546      {
1547          $startat = 0;
1548          $upper = $ipp;
1549          $lower = 1;
1550      }
1551  
1552      $query = $db->simple_select("users", "COUNT(uid) AS ipcount");
1553      $cnt = $db->fetch_array($query);
1554  
1555      if($upper > $cnt['ipcount'])
1556      {
1557          $upper = $cnt['ipcount'];
1558      }
1559  
1560      $contents .= "<p>Converting ip {$lower} to {$upper} ({$cnt['ipcount']} Total)</p>";
1561  
1562      $ipaddress = false;
1563      $update_array = array();
1564  
1565      $query = $db->simple_select("users", "regip, lastip, longlastip, longregip, uid", "", array('limit_start' => $lower, 'limit' => $ipp));
1566      while($user = $db->fetch_array($query))
1567      {
1568          // Have we already converted this ip?
1569          if(!$user['longregip'])
1570          {
1571              $update_array['longregip'] = (int)my_ip2long($user['regip']);
1572          }
1573  
1574          if(!$user['longlastip'])
1575          {
1576              $update_array['longlastip'] = (int)my_ip2long($user['lastip']);
1577          }
1578  
1579          if(!empty($update_array))
1580          {
1581              $db->update_query("users", $update_array, "uid = '{$user['uid']}'");
1582          }
1583  
1584          $update_array = array();
1585          $ipaddress = true;
1586      }
1587  
1588      $remaining = $upper-$cnt['ipcount'];
1589      if($remaining && $ipaddress)
1590      {
1591          $nextact = "12_dbchanges7";
1592          $startat = $startat+$ipp;
1593          $contents .= "<p><input type=\"hidden\" name=\"ipspage\" value=\"$ipp\" /><input type=\"hidden\" name=\"ipstart\" value=\"$startat\" />Done. Click Next to move on to the next set of user ips.</p>";
1594      }
1595      else
1596      {
1597          $nextact = "12_dbchanges8";
1598          $contents .= "<p>Done</p><p>All user ips have been converted to the new ip format. Click next to continue.</p>";
1599      }
1600      $output->print_contents($contents);
1601  
1602      global $footer_extra;
1603      $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>";
1604  
1605      $output->print_footer($nextact);
1606  }
1607  
1608  function upgrade12_dbchanges8()
1609  {
1610      global $db, $output;
1611  
1612      $output->print_header("Event Conversion");
1613  
1614      if(!$_POST['eventspage'])
1615      {
1616          $epp = 50;
1617      }
1618      else
1619      {
1620          $epp = (int)$_POST['eventspage'];
1621      }
1622  
1623      if($_POST['eventstart'])
1624      {
1625          $startat = (int)$_POST['eventstart'];
1626          $upper = $startat+$epp;
1627          $lower = $startat;
1628      }
1629      else
1630      {
1631          $startat = 0;
1632          $upper = $epp;
1633          $lower = 1;
1634      }
1635  
1636      $query = $db->simple_select("events", "COUNT(eid) AS eventcount");
1637      $cnt = $db->fetch_array($query);
1638  
1639      if($upper > $cnt['eventcount'])
1640      {
1641          $upper = $cnt['eventcount'];
1642      }
1643  
1644      $contents .= "<p>Converting events {$lower} to {$upper} ({$cnt['eventcount']} Total)</p>";
1645  
1646      // Just started - add fields
1647      if(!$db->field_exists("donecon", "events"))
1648      {
1649          // Add temporary column
1650          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD donecon smallint(1) NOT NULL;");
1651  
1652          if($db->field_exists('cid', "events"))
1653          {
1654              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP cid;");
1655          }
1656  
1657          if($db->field_exists('visible', "events"))
1658          {
1659              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP visible;");
1660          }
1661  
1662          if($db->field_exists('dateline', "events"))
1663          {
1664              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP dateline;");
1665          }
1666  
1667          if($db->field_exists('starttime', "events"))
1668          {
1669              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP starttime;");
1670          }
1671  
1672          if($db->field_exists('endtime', "events"))
1673          {
1674              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP endtime;");
1675          }
1676  
1677          if($db->field_exists('timezone', "events"))
1678          {
1679              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP timezone;");
1680          }
1681  
1682          if($db->field_exists('ignoretimezone', "events"))
1683          {
1684              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP ignoretimezone;");
1685          }
1686  
1687          if($db->field_exists('usingtime', "events"))
1688          {
1689              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP usingtime;");
1690          }
1691  
1692          if($db->field_exists('repeats', "events"))
1693          {
1694              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP repeats;");
1695          }
1696  
1697          // Got structural changes?
1698          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD cid int unsigned NOT NULL default '0' AFTER eid");
1699  
1700          if($db->field_exists('author', "events") && !$db->field_exists('uid', "events"))
1701          {
1702              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events CHANGE author uid int unsigned NOT NULL default '0'");
1703          }
1704  
1705          if($db->field_exists('subject', "events") && !$db->field_exists('name', "events"))
1706          {
1707              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events CHANGE subject name varchar(120) NOT NULL default ''");
1708          }
1709          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD visible int(1) NOT NULL default '0' AFTER description");
1710          $db->update_query("events", array('private' => 1), "private='yes' OR private='1'");
1711          $db->update_query("events", array('private' => 0), "private='no' OR private='0'");
1712          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events CHANGE private private int(1) NOT NULL default '0'");
1713          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD dateline int(10) unsigned NOT NULL default '0' AFTER private");
1714          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD starttime int(10) unsigned NOT NULL default '0' AFTER dateline");
1715          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD endtime int(10) unsigned NOT NULL default '0' AFTER starttime");
1716          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD timezone int(3) NOT NULL default '0' AFTER endtime");
1717          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD ignoretimezone int(1) NOT NULL default '0' AFTER timezone");
1718          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD usingtime int(1) NOT NULL default '0' AFTER ignoretimezone");
1719          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events ADD repeats text NOT NULL AFTER usingtime");
1720      }
1721  
1722      if($db->field_exists('date', "events"))
1723      {
1724          $query = $db->simple_select("events", "*", "donecon!=1", array("order_by" => "eid", "limit" => $epp));
1725          while($event = $db->fetch_array($query))
1726          {
1727              $e_date = explode("-", $event['date']);
1728              if(!$e_date[2]) $e_date[2] = 2005;
1729              $starttime = gmmktime(0, 0, 0, $e_date[1], $e_date[0], $e_date[2]);
1730              $updated_event = array(
1731                  "cid" => 1,
1732                  "visible" => 1,
1733                  "donecon" => 1,
1734                  "starttime" => $starttime,
1735                  "dateline" => $starttime
1736              );
1737              $db->update_query("events", $updated_event, "eid='{$event['eid']}'", 1);
1738          }
1739  
1740          $date = true;
1741      }
1742      else
1743      {
1744          $date = false;
1745      }
1746  
1747      $query = $db->simple_select("events", "COUNT(eid) AS remaining", "donecon!=1");
1748      $remaining = $db->fetch_field($query, "remaining");
1749      if($remaining && $date)
1750      {
1751          $nextact = "12_dbchanges8";
1752          $startat = $startat+$epp;
1753          $contents .= "<p><input type=\"hidden\" name=\"eventspage\" value=\"$epp\" /><input type=\"hidden\" name=\"eventstart\" value=\"$startat\" />Done. Click Next to move on to the next set of events.</p>";
1754      }
1755      else
1756      {
1757          $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP donecon");
1758          if($db->field_exists('date', "events"))
1759          {
1760              $db->write_query("ALTER TABLE ".TABLE_PREFIX."events DROP date");
1761          }
1762          $nextact = "12_redothemes";
1763          $contents .= "<p>Done</p><p>All events have been converted to the new calendar system. Click next to continue.</p>";
1764      }
1765      $output->print_contents($contents);
1766  
1767      global $footer_extra;
1768      $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>";
1769  
1770      $output->print_footer($nextact);
1771  }
1772  
1773  function upgrade12_redothemes()
1774  {
1775      global $db, $output, $config, $mybb;
1776  
1777      $output->print_header("Converting themes");
1778  
1779      if(!@is_dir(MYBB_ROOT.'cache/'))
1780      {
1781          @mkdir(MYBB_ROOT.'cache/', 077);
1782  
1783          // Add in empty index.html!
1784          $fp = @fopen(MYBB_ROOT."cache/index.html", "w");
1785          @fwrite($fp, "");
1786          @fclose($fp);
1787      }
1788      $cachewritable = @fopen(MYBB_ROOT.'cache/test.write', 'w');
1789      if(!$cachewritable)
1790      {
1791          $not_writable = true;
1792      }
1793      else
1794      {
1795          @fclose($cachewritable);
1796            @my_chmod(MYBB_ROOT.'cache', '0777');
1797            @my_chmod(MYBB_ROOT.'cache/test.write', '0777');
1798          @unlink(MYBB_ROOT.'cache/test.write');
1799      }
1800  
1801      if($not_writable)
1802      {
1803          echo "<p><span style=\"color: red; font-weight: bold;\">Unable to write to the cache/ directory.</span><br />Before the upgrade process can continue you need to make sure this directory exists and is writable (chmod 777)</p>";
1804          $output->print_footer("12_redothemes");
1805          exit;
1806      }
1807  
1808      $not_writable = false;
1809      if(!@is_dir(MYBB_ROOT.'cache/themes/'))
1810      {
1811          @mkdir(MYBB_ROOT.'cache/themes/', 077);
1812  
1813          // Add in empty index.html!
1814          $fp = @fopen(MYBB_ROOT."cache/themes/index.html", "w");
1815          @fwrite($fp, "");
1816          @fclose($fp);
1817      }
1818      $themewritable = @fopen(MYBB_ROOT.'cache/themes/test.write', 'w');
1819      if(!$themewritable)
1820      {
1821          $not_writable = true;
1822      }
1823      else
1824      {
1825          @fclose($themewritable);
1826            @my_chmod(MYBB_ROOT.'cache/themes', '0777');
1827            @my_chmod(MYBB_ROOT.'cache/themes/test.write', '0777');
1828          @unlink(MYBB_ROOT.'cache/themes/test.write');
1829      }
1830  
1831      if($not_writable)
1832      {
1833          echo "<p><span style=\"color: red; font-weight: bold;\">Unable to write to the cache/themes/ directory.</span><br />Before the upgrade process can continue you need to make sure this directory exists and is writable (chmod 777)</p>";
1834          $output->print_footer("12_redothemes");
1835          exit;
1836      }
1837  
1838      if($db->field_exists('themebits', "themes") && !$db->field_exists('properties', "themes"))
1839      {
1840          $db->write_query("ALTER TABLE ".TABLE_PREFIX."themes CHANGE themebits properties text NOT NULL");
1841      }
1842  
1843      if($db->field_exists('cssbits', "themes"))
1844      {
1845          $db->write_query("ALTER TABLE ".TABLE_PREFIX."themes DROP cssbits");
1846      }
1847  
1848      if($db->field_exists('csscached', "themes"))
1849      {
1850          $db->write_query("ALTER TABLE ".TABLE_PREFIX."themes DROP csscached");
1851      }
1852  
1853      if($db->field_exists('stylesheets', "themes"))
1854      {
1855          $db->write_query("ALTER TABLE ".TABLE_PREFIX."themes DROP stylesheets");
1856      }
1857      $db->write_query("ALTER TABLE ".TABLE_PREFIX."themes ADD stylesheets text NOT NULL AFTER properties");
1858  
1859      if($db->table_exists("themestylesheets"))
1860      {
1861          $db->drop_table("themestylesheets");
1862      }
1863  
1864      $collation = $db->build_create_table_collation();
1865  
1866      $db->write_query("CREATE TABLE ".TABLE_PREFIX."themestylesheets(
1867          sid int unsigned NOT NULL auto_increment,
1868          name varchar(30) NOT NULL default '',
1869          tid int unsigned NOT NULL default '0',
1870          attachedto text NOT NULL,
1871          stylesheet text NOT NULL,
1872          cachefile varchar(100) NOT NULL default '',
1873          lastmodified bigint(30) NOT NULL default '0',
1874          PRIMARY KEY(sid)
1875      ) ENGINE=MyISAM{$collation};");
1876  
1877      // Define our default stylesheets - MyBB 1.4 contains additional stylesheets that our converted themes will also need
1878      $contents = @file_get_contents(INSTALL_ROOT.'resources/mybb_theme.xml');
1879      if(file_exists(MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions_themes.php"))
1880      {
1881          require_once MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions_themes.php";
1882      }
1883      else if(file_exists(MYBB_ROOT."admin/inc/functions_themes.php"))
1884      {
1885          require_once  MYBB_ROOT."admin/inc/functions_themes.php";
1886      }
1887      else
1888      {
1889          $output->print_error("Please make sure your admin directory is uploaded correctly.");
1890      }
1891  
1892      // Import master theme
1893      if(import_theme_xml($contents, array("tid" => 1, "no_templates" => 1, "version_compat" => 1)) === -1)
1894      {
1895          $output->print_error("Please make sure your install/resources/mybb_theme.xml file is uploaded correctly.");
1896      }
1897  
1898      // Fetch out default stylesheets from master
1899      $query = $db->simple_select("themes", "*", "tid=1");
1900      $master_theme = $db->fetch_array($query);
1901  
1902      $master_stylesheets = my_unserialize($master_theme['stylesheets']);
1903  
1904      if(is_array($master_stylesheets))
1905      {
1906          // Note: 1.4 only ships with one global|global stylesheet
1907          foreach($master_stylesheets as $location => $sheets)
1908          {
1909              foreach($sheets as $action => $sheets)
1910              {
1911                  foreach($sheets as $stylesheet)
1912                  {
1913                      if($location == "global" && $action == "global")
1914                      {
1915                          continue; // Skip global
1916                      }
1917  
1918                      $default_stylesheets[$location][$action][] = $stylesheet;
1919                      $default_stylesheets['inherited']["{$location}_{$action}"][$stylesheet] = 1; // This stylesheet is inherited from the master
1920                  }
1921              }
1922          }
1923      }
1924  
1925      $query = $db->simple_select("themes");
1926      while($theme = $db->fetch_array($query))
1927      {
1928          if(!$theme['css'])
1929          {
1930              continue;
1931          }
1932  
1933          $theme['css'] .= "\n\n".$theme['extracss'];
1934  
1935          $theme['css'] = upgrade_css_120_to_140($theme['css']);
1936  
1937  
1938          // Create stylesheets
1939          $cache_file = cache_stylesheet($theme['tid'], "global.css", $theme['css']);
1940  
1941          $new_stylesheet = array(
1942              "tid" => $theme['tid'],
1943              "name" => "global.css",
1944              "attachedto" => "",
1945              "stylesheet" => $db->escape_string($theme['css']),
1946              "cachefile" => "global.css",
1947              "lastmodified" => TIME_NOW
1948          );
1949          $sid = $db->insert_query("themestylesheets", $new_stylesheet);
1950          $css_url = "css.php?stylesheet={$sid}";
1951          if($cache_file)
1952          {
1953              $css_url = $cache_file;
1954          }
1955  
1956          // Now we go and update the stylesheets column for this theme
1957          $stylesheets = $default_stylesheets;
1958  
1959          // Add in our local for this theme
1960          $stylesheets['global']['global'][] = $css_url;
1961  
1962          // Update the theme
1963          $db->update_query("themes", array("stylesheets" => $db->escape_string(my_serialize($stylesheets))), "tid='{$theme['tid']}'");
1964      }
1965  
1966      if($db->field_exists('css', "themes"))
1967      {
1968          $db->write_query("ALTER TABLE ".TABLE_PREFIX."themes DROP css");
1969      }
1970  
1971      if($db->field_exists('extracss', "themes"))
1972      {
1973          $db->write_query("ALTER TABLE ".TABLE_PREFIX."themes DROP extracss");
1974      }
1975  
1976      // We need to replace this for our themes css to show up
1977      // <link rel="stylesheet" type="text/css" href="{$theme['css_url']}" /> must be present in the old template (it usually is)
1978      $query = $db->simple_select("templates", "tid,template", "title='headerinclude'");
1979      while($template = $db->fetch_array($query))
1980      {
1981          $template['template'] = str_replace('<link rel="stylesheet" type="text/css" href="{$theme[\'css_url\']}" />', '{$stylesheets}', $template['template']);
1982  
1983          $db->update_query("templates", array('template' => $db->escape_string($template['template'])), "tid='{$template['tid']}'");
1984      }
1985  
1986      echo "<p>Your themes have successfully been converted to the new theme system.</p>";
1987      echo "<p>Click next to continue with the upgrade process.</p>";
1988  
1989      global $footer_extra;
1990      //$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>";
1991  
1992      $output->print_footer("12_done");
1993  }


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