[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/install/ -> upgrade.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  define('MYBB_ROOT', dirname(dirname(__FILE__))."/");
  12  define("INSTALL_ROOT", dirname(__FILE__)."/");
  13  define("TIME_NOW", time());
  14  define('IN_MYBB', 1);
  15  define("IN_UPGRADE", 1);
  16  
  17  if(function_exists('date_default_timezone_set') && !ini_get('date.timezone'))
  18  {
  19      date_default_timezone_set('GMT');
  20  }
  21  
  22  require_once  MYBB_ROOT.'inc/class_error.php';
  23  $error_handler = new errorHandler();
  24  
  25  require_once  MYBB_ROOT."inc/functions.php";
  26  
  27  require_once  MYBB_ROOT."inc/class_core.php";
  28  $mybb = new MyBB;
  29  
  30  require_once MYBB_ROOT."inc/config.php";
  31  
  32  $orig_config = $config;
  33  
  34  if(!is_array($config['database']))
  35  {
  36      $config['database'] = array(
  37          "type" => $config['dbtype'],
  38          "database" => $config['database'],
  39          "table_prefix" => $config['table_prefix'],
  40          "hostname" => $config['hostname'],
  41          "username" => $config['username'],
  42          "password" => $config['password'],
  43          "encoding" => $config['db_encoding'],
  44      );
  45  }
  46  $mybb->config = &$config;
  47  
  48  // Include the files necessary for installation
  49  require_once  MYBB_ROOT."inc/class_timers.php";
  50  require_once  MYBB_ROOT.'inc/class_language.php';
  51  
  52  $lang = new MyLanguage();
  53  $lang->set_path(INSTALL_ROOT.'resources/');
  54  $lang->load('language');
  55  
  56  // If we're upgrading from an SQLite installation, make sure we still work.
  57  if($config['database']['type'] == 'sqlite3' || $config['database']['type'] == 'sqlite2')
  58  {
  59      $config['database']['type'] = 'sqlite';
  60  }
  61  
  62  // Load DB interface
  63  require_once  MYBB_ROOT."inc/db_base.php";
  64  require_once  MYBB_ROOT . 'inc/AbstractPdoDbDriver.php';
  65  
  66  require_once MYBB_ROOT."inc/db_{$config['database']['type']}.php";
  67  switch($config['database']['type'])
  68  {
  69      case "sqlite":
  70          $db = new DB_SQLite;
  71          break;
  72      case "pgsql":
  73          $db = new DB_PgSQL;
  74          break;
  75      case "pgsql_pdo":
  76          $db = new PostgresPdoDbDriver();
  77          break;
  78      case "mysqli":
  79          $db = new DB_MySQLi;
  80          break;
  81      case "mysql_pdo":
  82          $db = new MysqlPdoDbDriver();
  83          break;
  84      default:
  85          $db = new DB_MySQL;
  86  }
  87  
  88  // Connect to Database
  89  define('TABLE_PREFIX', $config['database']['table_prefix']);
  90  $db->connect($config['database']);
  91  $db->set_table_prefix(TABLE_PREFIX);
  92  $db->type = $config['database']['type'];
  93  
  94  // Load Settings
  95  if(file_exists(MYBB_ROOT."inc/settings.php"))
  96  {
  97      require_once  MYBB_ROOT."inc/settings.php";
  98  }
  99  
 100  if(!file_exists(MYBB_ROOT."inc/settings.php") || !$settings)
 101  {
 102      if(function_exists('rebuild_settings'))
 103      {
 104          rebuild_settings();
 105      }
 106      else
 107      {
 108          $options = array(
 109              "order_by" => "title",
 110              "order_dir" => "ASC"
 111          );
 112  
 113          $query = $db->simple_select("settings", "value, name", "", $options);
 114  
 115          $settings = array();
 116          while($setting = $db->fetch_array($query))
 117          {
 118              $setting['value'] = str_replace("\"", "\\\"", $setting['value']);
 119              $settings[$setting['name']] = $setting['value'];
 120          }
 121      }
 122  }
 123  
 124  $settings['wolcutoff'] = $settings['wolcutoffmins']*60;
 125  $settings['bbname_orig'] = $settings['bbname'];
 126  $settings['bbname'] = strip_tags($settings['bbname']);
 127  
 128  // Fix for people who for some specify a trailing slash on the board URL
 129  if(substr($settings['bburl'], -1) == "/")
 130  {
 131      $settings['bburl'] = my_substr($settings['bburl'], 0, -1);
 132  }
 133  
 134  $mybb->settings = &$settings;
 135  $mybb->parse_cookies();
 136  
 137  require_once  MYBB_ROOT."inc/class_datacache.php";
 138  $cache = new datacache;
 139  
 140  // Load cache
 141  $cache->cache();
 142  
 143  $mybb->cache = &$cache;
 144  
 145  require_once  MYBB_ROOT."inc/class_session.php";
 146  $session = new session;
 147  $session->init();
 148  $mybb->session = &$session;
 149  
 150  // Include the necessary contants for installation
 151  $grouppermignore = array("gid", "type", "title", "description", "namestyle", "usertitle", "stars", "starimage", "image");
 152  $groupzerogreater = array("pmquota", "maxpmrecipients", "maxreputationsday", "attachquota", "maxemails", "maxwarningsday", "maxposts", "edittimelimit", "canusesigxposts", "maxreputationsperuser", "maxreputationsperthread", "emailfloodtime");
 153  $displaygroupfields = array("title", "description", "namestyle", "usertitle", "stars", "starimage", "image");
 154  $fpermfields = array('canview', 'canviewthreads', 'candlattachments', 'canpostthreads', 'canpostreplys', 'canpostattachments', 'canratethreads', 'caneditposts', 'candeleteposts', 'candeletethreads', 'caneditattachments', 'canpostpolls', 'canvotepolls', 'cansearch', 'modposts', 'modthreads', 'modattachments', 'mod_edit_posts');
 155  
 156  // Include the installation resources
 157  require_once  INSTALL_ROOT."resources/output.php";
 158  $output = new installerOutput;
 159  $output->script = "upgrade.php";
 160  $output->title = "MyBB Upgrade Wizard";
 161  
 162  if(file_exists("lock"))
 163  {
 164      $output->print_error($lang->locked);
 165  }
 166  else
 167  {
 168      $mybb->input['action'] = $mybb->get_input('action');
 169      if($mybb->input['action'] == "logout" && $mybb->user['uid'])
 170      {
 171          // Check session ID if we have one
 172          if($mybb->get_input('logoutkey') !== $mybb->user['logoutkey'])
 173          {
 174              $output->print_error("Your user ID could not be verified to log you out.  This may have been because a malicious Javascript was attempting to log you out automatically.  If you intended to log out, please click the Log Out button at the top menu.");
 175          }
 176  
 177          my_unsetcookie("mybbuser");
 178  
 179          if($mybb->user['uid'])
 180          {
 181              $time = TIME_NOW;
 182              $lastvisit = array(
 183                  "lastactive" => $time-900,
 184                  "lastvisit" => $time,
 185              );
 186              $db->update_query("users", $lastvisit, "uid='".$mybb->user['uid']."'");
 187          }
 188          header("Location: upgrade.php");
 189      }
 190      else if($mybb->input['action'] == "do_login" && $mybb->request_method == "post")
 191      {
 192          require_once  MYBB_ROOT."inc/functions_user.php";
 193  
 194          if(!username_exists($mybb->get_input('username')))
 195          {
 196              $output->print_error("The username you have entered appears to be invalid.");
 197          }
 198          $options = array(
 199              'fields' => array('username', 'password', 'salt', 'loginkey')
 200          );
 201          $user = get_user_by_username($mybb->get_input('username'), $options);
 202  
 203          if(!$user)
 204          {
 205              $output->print_error("The username you have entered appears to be invalid.");
 206          }
 207          else
 208          {
 209              $user = validate_password_from_uid($user['uid'], $mybb->get_input('password'), $user);
 210              if(!$user)
 211              {
 212                  $output->print_error("The password you entered is incorrect. If you have forgotten your password, click <a href=\"../member.php?action=lostpw\">here</a>. Otherwise, go back and try again.");
 213              }
 214          }
 215  
 216          my_setcookie("mybbuser", $user['uid']."_".$user['loginkey'], null, true, "lax");
 217  
 218          header("Location: ./upgrade.php");
 219      }
 220  
 221      $output->steps = array($lang->upgrade);
 222  
 223      if($mybb->user['uid'] == 0)
 224      {
 225          $output->print_header($lang->please_login, "errormsg", 0, 1);
 226  
 227          $output->print_contents('<p>'.$lang->login_desc.'</p>
 228  <form action="upgrade.php" method="post">
 229      <div class="border_wrapper">
 230          <table class="general" cellspacing="0">
 231          <thead>
 232              <tr>
 233                  <th colspan="2" class="first last">'.$lang->login.'</th>
 234              </tr>
 235          </thead>
 236          <tbody>
 237              <tr class="first">
 238                  <td class="first">'.$lang->login_username.':</td>
 239                  <td class="last alt_col"><input type="text" class="textbox" name="username" size="25" maxlength="'.$mybb->settings['maxnamelength'].'" style="width: 200px;" /></td>
 240              </tr>
 241              <tr class="alt_row last">
 242                  <td class="first">'.$lang->login_password.':<br /><small>'.$lang->login_password_desc.'</small></td>
 243                  <td class="last alt_col"><input type="password" class="textbox" name="password" size="25" style="width: 200px;" /></td>
 244              </tr>
 245          </tbody>
 246          </table>
 247      </div>
 248      <div id="next_button">
 249          <input type="submit" class="submit_button" name="submit" value="'.$lang->login.'" />
 250          <input type="hidden" name="action" value="do_login" />
 251      </div>
 252  </form>');
 253          $output->print_footer("");
 254  
 255          exit;
 256      }
 257      else if($mybb->usergroup['cancp'] != 1 && $mybb->usergroup['cancp'] != 'yes')
 258      {
 259          $output->print_error($lang->sprintf($lang->no_permision, $mybb->user['logoutkey']));
 260      }
 261  
 262      if(!$mybb->input['action'] || $mybb->input['action'] == "intro")
 263      {
 264          $output->print_header();
 265  
 266          if($db->table_exists("upgrade_data"))
 267          {
 268              $db->drop_table("upgrade_data");
 269          }
 270  
 271          $collation = $db->build_create_table_collation();
 272          
 273          $engine = '';
 274          if($db->type == "mysql" || $db->type == "mysqli")
 275          {
 276              $engine = 'ENGINE=MyISAM';
 277          }
 278          
 279          $db->write_query("CREATE TABLE ".TABLE_PREFIX."upgrade_data (
 280              title varchar(30) NOT NULL,
 281              contents text NOT NULL,
 282              UNIQUE (title)
 283          ) {$engine}{$collation};");
 284  
 285          $dh = opendir(INSTALL_ROOT."resources");
 286  
 287          $upgradescripts = array();
 288          while(($file = readdir($dh)) !== false)
 289          {
 290              if(preg_match("#upgrade([0-9]+).php$#i", $file, $match))
 291              {
 292                  $upgradescripts[$match[1]] = $file;
 293                  $key_order[] = $match[1];
 294              }
 295          }
 296          closedir($dh);
 297          natsort($key_order);
 298          $key_order = array_reverse($key_order);
 299  
 300          // Figure out which version we last updated from (as of 1.6)
 301          $version_history = $cache->read("version_history");
 302  
 303          // If array is empty then we must be upgrading to 1.6 since that's when this feature was added
 304          if(empty($version_history))
 305          {
 306              $next_update_version = 17; // 16+1
 307          }
 308          else
 309          {
 310              $next_update_version = (int)(end($version_history)+1);
 311          }
 312  
 313          $vers = '';
 314          foreach($key_order as $k => $key)
 315          {
 316              $file = $upgradescripts[$key];
 317              $upgradescript = file_get_contents(INSTALL_ROOT."resources/$file");
 318              preg_match("#Upgrade Script:(.*)#i", $upgradescript, $verinfo);
 319              preg_match("#upgrade([0-9]+).php$#i", $file, $keynum);
 320              if(trim($verinfo[1]))
 321              {
 322                  if($keynum[1] == $next_update_version)
 323                  {
 324                      $vers .= "<option value=\"$keynum[1]\" selected=\"selected\">$verinfo[1]</option>\n";
 325                  }
 326                  else
 327                  {
 328                      $vers .= "<option value=\"$keynum[1]\">$verinfo[1]</option>\n";
 329                  }
 330              }
 331          }
 332          unset($upgradescripts);
 333          unset($upgradescript);
 334  
 335          if(end($version_history) == reset($key_order) && empty($mybb->input['force']))
 336          {
 337              $output->print_contents($lang->upgrade_not_needed);
 338              $output->print_footer("finished");
 339          }
 340          else
 341          {
 342              $output->print_contents($lang->sprintf($lang->upgrade_welcome, $mybb->version)."<p><select name=\"from\">$vers</select>".$lang->upgrade_send_stats);
 343              $output->print_footer("doupgrade");
 344          }
 345      }
 346      elseif($mybb->input['action'] == "doupgrade")
 347      {
 348          add_upgrade_store("allow_anonymous_info", $mybb->get_input('allow_anonymous_info', MyBB::INPUT_INT));
 349          require_once INSTALL_ROOT."resources/upgrade".$mybb->get_input('from', MyBB::INPUT_INT).".php";
 350          if($db->table_exists("datacache") && !empty($upgrade_detail['requires_deactivated_plugins']) && $mybb->get_input('donewarning') != "true")
 351          {
 352              $plugins = $cache->read('plugins', true);
 353              if(!empty($plugins['active']))
 354              {
 355                  $output->print_header();
 356                  $lang->plugin_warning = "<input type=\"hidden\" name=\"from\" value=\"".$mybb->get_input('from', MyBB::INPUT_INT)."\" />\n<input type=\"hidden\" name=\"donewarning\" value=\"true\" />\n<div class=\"error\"><strong><span style=\"color: red\">Warning:</span></strong> <p>There are still ".count($plugins['active'])." plugin(s) active. Active plugins can sometimes cause problems during an upgrade procedure or may break your forum afterward. It is <strong>strongly</strong> reccommended that you deactivate your plugins before continuing.</p></div> <br />";
 357                  $output->print_contents($lang->sprintf($lang->plugin_warning, $mybb->version));
 358                  $output->print_footer("doupgrade");
 359              }
 360              else
 361              {
 362                  add_upgrade_store("startscript", $mybb->get_input('from', MyBB::INPUT_INT));
 363                  $runfunction = next_function($mybb->get_input('from', MyBB::INPUT_INT));
 364              }
 365          }
 366          else
 367          {
 368              add_upgrade_store("startscript", $mybb->get_input('from', MyBB::INPUT_INT));
 369              $runfunction = next_function($mybb->get_input('from', MyBB::INPUT_INT));
 370          }
 371      }
 372      $currentscript = get_upgrade_store("currentscript");
 373      $system_upgrade_detail = get_upgrade_store("upgradedetail");
 374  
 375      if($mybb->input['action'] == "templates")
 376      {
 377          $runfunction = "upgradethemes";
 378      }
 379      elseif($mybb->input['action'] == "rebuildsettings")
 380      {
 381          $runfunction = "buildsettings";
 382      }
 383      elseif($mybb->input['action'] == "buildcaches")
 384      {
 385          $runfunction = "buildcaches";
 386      }
 387      elseif($mybb->input['action'] == "finished")
 388      {
 389          $runfunction = "upgradedone";
 390      }
 391      else // Busy running modules, come back later
 392      {
 393          $bits = explode("_", $mybb->input['action'], 2);
 394          if(!empty($bits[1])) // We're still running a module
 395          {
 396              $from = $bits[0];
 397              $runfunction = next_function($bits[0], $bits[1]);
 398  
 399          }
 400      }
 401  
 402      // Fetch current script we're in
 403      if(function_exists($runfunction))
 404      {
 405          $runfunction();
 406      }
 407  }
 408  
 409  /**
 410   * Do the upgrade changes
 411   */
 412  function upgradethemes()
 413  {
 414      global $output, $db, $system_upgrade_detail, $lang, $mybb;
 415  
 416      $output->print_header($lang->upgrade_templates_reverted);
 417  
 418      $charset = $db->build_create_table_collation();
 419  
 420      if($system_upgrade_detail['revert_all_templates'] > 0)
 421      {
 422          $db->drop_table("templates");
 423          $db->write_query("CREATE TABLE ".TABLE_PREFIX."templates (
 424            tid int unsigned NOT NULL auto_increment,
 425            title varchar(120) NOT NULL default '',
 426            template text NOT NULL,
 427            sid int(10) NOT NULL default '0',
 428            version varchar(20) NOT NULL default '0',
 429            status varchar(10) NOT NULL default '',
 430            dateline int(10) NOT NULL default '0',
 431            PRIMARY KEY  (tid)
 432          ) ENGINE=MyISAM{$charset};");
 433      }
 434  
 435      if($system_upgrade_detail['revert_all_themes'] > 0)
 436      {
 437          $db->drop_table("themes");
 438          $db->write_query("CREATE TABLE ".TABLE_PREFIX."themes (
 439           tid smallint unsigned NOT NULL auto_increment,
 440           name varchar(100) NOT NULL default '',
 441           pid smallint unsigned NOT NULL default '0',
 442           def smallint(1) NOT NULL default '0',
 443           properties text NOT NULL,
 444           stylesheets text NOT NULL,
 445           allowedgroups text NOT NULL,
 446           PRIMARY KEY (tid)
 447          ) ENGINE=MyISAM{$charset};");
 448  
 449          $db->drop_table("themestylesheets");
 450          $db->write_query("CREATE TABLE ".TABLE_PREFIX."themestylesheets(
 451              sid int unsigned NOT NULL auto_increment,
 452              name varchar(30) NOT NULL default '',
 453              tid int unsigned NOT NULL default '0',
 454              attachedto text NOT NULL,
 455              stylesheet text NOT NULL,
 456              cachefile varchar(100) NOT NULL default '',
 457              lastmodified bigint(30) NOT NULL default '0',
 458              PRIMARY KEY(sid)
 459          ) ENGINE=MyISAM{$charset};");
 460  
 461          $contents = @file_get_contents(INSTALL_ROOT.'resources/mybb_theme.xml');
 462          if(file_exists(MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions_themes.php"))
 463          {
 464              require_once MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions_themes.php";
 465          }
 466          else if(file_exists(MYBB_ROOT."admin/inc/functions_themes.php"))
 467          {
 468              require_once  MYBB_ROOT."admin/inc/functions_themes.php";
 469          }
 470          else
 471          {
 472              $output->print_error("Please make sure your admin directory is uploaded correctly.");
 473          }
 474          import_theme_xml($contents, array("templateset" => -2, "no_templates" => 1, "version_compat" => 1));
 475          $tid = build_new_theme("Default", null, 1);
 476  
 477          $db->update_query("themes", array("def" => 1), "tid='{$tid}'");
 478          $db->update_query("users", array('style' => $tid));
 479          $db->update_query("forums", array('style' => 0));
 480  
 481          $db->drop_table("templatesets");
 482          $db->write_query("CREATE TABLE ".TABLE_PREFIX."templatesets (
 483            sid smallint unsigned NOT NULL auto_increment,
 484            title varchar(120) NOT NULL default '',
 485            PRIMARY KEY  (sid)
 486          ) ENGINE=MyISAM{$charset};");
 487  
 488          $db->insert_query("templatesets", array('title' => 'Default Templates'));
 489      }
 490      else
 491      {
 492          // Re-import master
 493          $contents = @file_get_contents(INSTALL_ROOT.'resources/mybb_theme.xml');
 494          if(file_exists(MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions_themes.php"))
 495          {
 496              require_once MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions.php";
 497              require_once MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions_themes.php";
 498          }
 499          elseif(file_exists(MYBB_ROOT."admin/inc/functions_themes.php"))
 500          {
 501              require_once  MYBB_ROOT."admin/inc/functions.php";
 502              require_once  MYBB_ROOT."admin/inc/functions_themes.php";
 503          }
 504          else
 505          {
 506              $output->print_error($lang->no_theme_functions_file);
 507          }
 508  
 509          // Import master theme
 510          import_theme_xml($contents, array("tid" => 1, "no_templates" => 1, "version_compat" => 1));
 511      }
 512  
 513      $sid = -2;
 514  
 515      // Now deal with the master templates
 516      $contents = @file_get_contents(INSTALL_ROOT.'resources/mybb_theme.xml');
 517      $parser = create_xml_parser($contents);
 518      $tree = $parser->get_tree();
 519  
 520      $theme = $tree['theme'];
 521  
 522      if(is_array($theme['templates']))
 523      {
 524          $templates = $theme['templates']['template'];
 525          foreach($templates as $template)
 526          {
 527              $templatename = $db->escape_string($template['attributes']['name']);
 528              $templateversion = (int)$template['attributes']['version'];
 529              $templatevalue = $db->escape_string($template['value']);
 530              $time = TIME_NOW;
 531              $query = $db->simple_select("templates", "tid", "sid='-2' AND title='".$db->escape_string($templatename)."'");
 532              $oldtemp = $db->fetch_array($query);
 533              if($oldtemp)
 534              {
 535                  $update_array = array(
 536                      'template' => $templatevalue,
 537                      'version' => $templateversion,
 538                      'dateline' => $time
 539                  );
 540                  $db->update_query("templates", $update_array, "title='".$db->escape_string($templatename)."' AND sid='-2'");
 541              }
 542              else
 543              {
 544                  $insert_array = array(
 545                      'title' => $templatename,
 546                      'template' => $templatevalue,
 547                      'sid' => $sid,
 548                      'version' => $templateversion,
 549                      'dateline' => $time
 550                  );
 551  
 552                  $db->insert_query("templates", $insert_array);
 553                  ++$newcount;
 554              }
 555          }
 556      }
 557  
 558      $output->print_contents($lang->upgrade_templates_reverted_success);
 559      $output->print_footer("rebuildsettings");
 560  }
 561  
 562  /**
 563   * Update the settings
 564   */
 565  function buildsettings()
 566  {
 567      global $db, $output, $system_upgrade_detail, $lang;
 568  
 569      if(!is_writable(MYBB_ROOT."inc/settings.php"))
 570      {
 571          $output->print_header("Rebuilding Settings");
 572          echo "<p><div class=\"error\"><span style=\"color: red; font-weight: bold;\">Error: Unable to open inc/settings.php</span><h3>Before the upgrade process can continue, you need to changes the permissions of inc/settings.php so it is writable.</h3></div></p>";
 573          $output->print_footer("rebuildsettings");
 574          exit;
 575      }
 576      $synccount = sync_settings($system_upgrade_detail['revert_all_settings']);
 577  
 578      $output->print_header($lang->upgrade_settings_sync);
 579      $output->print_contents($lang->sprintf($lang->upgrade_settings_sync_success, $synccount[1], $synccount[0]));
 580      $output->print_footer("buildcaches");
 581  }
 582  
 583  /**
 584   * Rebuild caches
 585   */
 586  function buildcaches()
 587  {
 588      global $db, $output, $cache, $lang, $mybb;
 589  
 590      $output->print_header($lang->upgrade_datacache_building);
 591  
 592      $contents = $lang->upgrade_building_datacache;
 593  
 594      $cache->update_version();
 595      $cache->update_attachtypes();
 596      $cache->update_smilies();
 597      $cache->update_badwords();
 598      $cache->update_usergroups();
 599      $cache->update_forumpermissions();
 600      $cache->update_stats();
 601      $cache->update_statistics();
 602      $cache->update_moderators();
 603      $cache->update_forums();
 604      $cache->update_usertitles();
 605      $cache->update_reportedcontent();
 606      $cache->update_awaitingactivation();
 607      $cache->update_mycode();
 608      $cache->update_profilefields();
 609      $cache->update_posticons();
 610      $cache->update_update_check();
 611      $cache->update_tasks();
 612      $cache->update_spiders();
 613      $cache->update_bannedips();
 614      $cache->update_birthdays();
 615      $cache->update_most_replied_threads();
 616      $cache->update_most_viewed_threads();
 617      $cache->update_groupleaders();
 618      $cache->update_threadprefixes();
 619      $cache->update_forumsdisplay();
 620      $cache->update_reportreasons(true);
 621  
 622      $contents .= $lang->done."</p>";
 623  
 624      $output->print_contents("$contents<p>".$lang->upgrade_continue."</p>");
 625      $output->print_footer("finished");
 626  }
 627  
 628  /**
 629   * Called as latest function. Send statistics, create lock file etc
 630   */
 631  function upgradedone()
 632  {
 633      global $db, $output, $mybb, $lang, $config, $plugins;
 634  
 635      ob_start();
 636      $output->print_header($lang->upgrade_complete);
 637  
 638      $allow_anonymous_info = get_upgrade_store("allow_anonymous_info");
 639      if($allow_anonymous_info == 1)
 640      {
 641          require_once  MYBB_ROOT."inc/functions_serverstats.php";
 642          $build_server_stats = build_server_stats(0, '', $mybb->version_code, $mybb->config['database']['encoding']);
 643  
 644          if($build_server_stats['info_sent_success'] == false)
 645          {
 646              echo $build_server_stats['info_image'];
 647          }
 648      }
 649      ob_end_flush();
 650  
 651      // Attempt to run an update check
 652      require_once  MYBB_ROOT.'inc/functions_task.php';
 653      $query = $db->simple_select('tasks', 'tid', "file='versioncheck'");
 654      $update_check = $db->fetch_array($query);
 655      if($update_check)
 656      {
 657          // Load plugin system for update check
 658          require_once  MYBB_ROOT."inc/class_plugins.php";
 659          $plugins = new pluginSystem;
 660  
 661          run_task($update_check['tid']);
 662      }
 663  
 664      if(is_writable("./"))
 665      {
 666          $lock = @fopen("./lock", "w");
 667          $written = @fwrite($lock, "1");
 668          @fclose($lock);
 669          if($written)
 670          {
 671              $lock_note = $lang->sprintf($lang->upgrade_locked, $config['admin_dir']);
 672          }
 673      }
 674      if(empty($written))
 675      {
 676          $lock_note = "<p><b><span style=\"color: red;\">".$lang->upgrade_removedir."</span></b></p>";
 677      }
 678  
 679      // Rebuild inc/settings.php at the end of the upgrade
 680      if(function_exists('rebuild_settings'))
 681      {
 682          rebuild_settings();
 683      }
 684      else
 685      {
 686          $options = array(
 687              "order_by" => "title",
 688              "order_dir" => "ASC"
 689          );
 690  
 691          $query = $db->simple_select("settings", "value, name", "", $options);
 692          while($setting = $db->fetch_array($query))
 693          {
 694              $setting['value'] = str_replace("\"", "\\\"", $setting['value']);
 695              $settings[$setting['name']] = $setting['value'];
 696          }
 697      }
 698  
 699      $output->print_contents($lang->sprintf($lang->upgrade_congrats, $mybb->version, $lock_note));
 700      $output->print_footer();
 701  }
 702  
 703  /**
 704   * Show the finish page
 705   */
 706  function whatsnext()
 707  {
 708      global $output, $db, $system_upgrade_detail, $lang;
 709  
 710      if($system_upgrade_detail['revert_all_templates'] > 0)
 711      {
 712          $output->print_header($lang->upgrade_template_reversion);
 713          $output->print_contents($lang->upgrade_template_reversion_success);
 714          $output->print_footer("templates");
 715      }
 716      else
 717      {
 718          upgradethemes();
 719      }
 720  }
 721  
 722  /**
 723   * Determine the next function we need to call
 724   *
 725   * @param int $from
 726   * @param string $func
 727   *
 728   * @return string
 729   */
 730  function next_function($from, $func="dbchanges")
 731  {
 732      global $oldvers, $system_upgrade_detail, $currentscript, $cache;
 733  
 734      load_module("upgrade".$from.".php");
 735      if(function_exists("upgrade".$from."_".$func))
 736      {
 737          $function = "upgrade".$from."_".$func;
 738      }
 739      else
 740      {
 741           // We're done with our last upgrade script, so add it to the upgrade scripts we've already completed.
 742          $version_history = $cache->read("version_history");
 743          $version_history[$from] = $from;
 744          $cache->update("version_history", $version_history);
 745  
 746          $from = $from+1;
 747          if(file_exists(INSTALL_ROOT."resources/upgrade".$from.".php"))
 748          {
 749              $function = next_function($from);
 750          }
 751      }
 752  
 753      if(empty($function))
 754      {
 755          $function = "whatsnext";
 756      }
 757      return $function;
 758  }
 759  
 760  /**
 761   * @param string $module
 762   */
 763  function load_module($module)
 764  {
 765      global $system_upgrade_detail, $currentscript, $upgrade_detail;
 766  
 767      require_once INSTALL_ROOT."resources/".$module;
 768      if($currentscript != $module)
 769      {
 770          foreach($upgrade_detail as $key => $val)
 771          {
 772              if(empty($system_upgrade_detail[$key]) || $val > $system_upgrade_detail[$key])
 773              {
 774                  $system_upgrade_detail[$key] = $val;
 775              }
 776          }
 777          add_upgrade_store("upgradedetail", $system_upgrade_detail);
 778          add_upgrade_store("currentscript", $module);
 779      }
 780  }
 781  
 782  /**
 783   * Get a value from our upgrade data cache
 784   *
 785   * @param string $title
 786   *
 787   * @return mixed
 788   */
 789  function get_upgrade_store($title)
 790  {
 791      global $db;
 792  
 793      $query = $db->simple_select("upgrade_data", "*", "title='".$db->escape_string($title)."'");
 794      $data = $db->fetch_array($query);
 795  
 796      if(!isset($data['contents']))
 797      {
 798          return null;
 799      }
 800  
 801      return my_unserialize($data['contents']);
 802  }
 803  
 804  /**
 805   * @param string $title
 806   * @param mixed $contents
 807   */
 808  function add_upgrade_store($title, $contents)
 809  {
 810      global $db;
 811  
 812      $replace_array = array(
 813          "title" => $db->escape_string($title),
 814          "contents" => $db->escape_string(my_serialize($contents))
 815      );
 816      $db->replace_query("upgrade_data", $replace_array, "title");
 817  }
 818  
 819  /**
 820   * @param int $redo 2 means that all setting tables will be dropped and recreated
 821   *
 822   * @return array
 823   */
 824  function sync_settings($redo=0)
 825  {
 826      global $db;
 827  
 828      $settingcount = $groupcount = 0;
 829      $settings = $settinggroups = array();
 830      if($redo == 2)
 831      {
 832          $db->drop_table("settinggroups");
 833          switch($db->type)
 834          {
 835              case "pgsql":
 836                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settinggroups (
 837                    gid serial,
 838                    name varchar(100) NOT NULL default '',
 839                    title varchar(220) NOT NULL default '',
 840                    description text NOT NULL default '',
 841                    disporder smallint NOT NULL default '0',
 842                    isdefault int NOT NULL default '0',
 843                    PRIMARY KEY (gid)
 844                  );");
 845                  break;
 846              case "sqlite":
 847                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settinggroups (
 848                    gid INTEGER PRIMARY KEY,
 849                    name varchar(100) NOT NULL default '',
 850                    title varchar(220) NOT NULL default '',
 851                    description TEXT NOT NULL,
 852                    disporder smallint NOT NULL default '0',
 853                    isdefault int(1) NOT NULL default '0'
 854                  );");
 855                  break;
 856              case "mysql":
 857              default:
 858                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settinggroups (
 859                    gid smallint unsigned NOT NULL auto_increment,
 860                    name varchar(100) NOT NULL default '',
 861                    title varchar(220) NOT NULL default '',
 862                    description text NOT NULL,
 863                    disporder smallint unsigned NOT NULL default '0',
 864                    isdefault int(1) NOT NULL default '0',
 865                    PRIMARY KEY  (gid)
 866                  ) ENGINE=MyISAM;");
 867          }
 868  
 869          $db->drop_table("settings");
 870  
 871          switch($db->type)
 872          {
 873              case "pgsql":
 874                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settings (
 875                    sid serial,
 876                    name varchar(120) NOT NULL default '',
 877                    title varchar(120) NOT NULL default '',
 878                    description text NOT NULL default '',
 879                    optionscode text NOT NULL default '',
 880                    value text NOT NULL default '',
 881                    disporder smallint NOT NULL default '0',
 882                    gid smallint NOT NULL default '0',
 883                    isdefault int NOT NULL default '0',
 884                    PRIMARY KEY (sid)
 885                  );");
 886                  break;
 887              case "sqlite":
 888                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settings (
 889                    sid INTEGER PRIMARY KEY,
 890                    name varchar(120) NOT NULL default '',
 891                    title varchar(120) NOT NULL default '',
 892                    description TEXT NOT NULL,
 893                    optionscode TEXT NOT NULL,
 894                    value TEXT NOT NULL,
 895                    disporder smallint NOT NULL default '0',
 896                    gid smallint NOT NULL default '0',
 897                    isdefault int(1) NOT NULL default '0'
 898                  );");
 899                  break;
 900              case "mysql":
 901              default:
 902                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settings (
 903                    sid smallint unsigned NOT NULL auto_increment,
 904                    name varchar(120) NOT NULL default '',
 905                    title varchar(120) NOT NULL default '',
 906                    description text NOT NULL,
 907                    optionscode text NOT NULL,
 908                    value text NOT NULL,
 909                    disporder smallint unsigned NOT NULL default '0',
 910                    gid smallint unsigned NOT NULL default '0',
 911                    isdefault int(1) NOT NULL default '0',
 912                    PRIMARY KEY (sid)
 913                  ) ENGINE=MyISAM;");
 914          }
 915      }
 916      else
 917      {
 918          if($db->type == "mysql" || $db->type == "mysqli")
 919          {
 920              $wheresettings = "isdefault='1' OR isdefault='yes'";
 921          }
 922          else
 923          {
 924              $wheresettings = "isdefault='1'";
 925          }
 926  
 927          $query = $db->simple_select("settinggroups", "name,title,gid", $wheresettings);
 928          while($group = $db->fetch_array($query))
 929          {
 930              $settinggroups[$group['name']] = $group['gid'];
 931          }
 932  
 933          // Collect all the user's settings - regardless of 'defaultivity' - we'll check them all
 934          // against default settings and insert/update them accordingly
 935          $query = $db->simple_select("settings", "name,sid");
 936          while($setting = $db->fetch_array($query))
 937          {
 938              $settings[$setting['name']] = $setting['sid'];
 939          }
 940      }
 941      $settings_xml = file_get_contents(INSTALL_ROOT."resources/settings.xml");
 942      $parser = create_xml_parser($settings_xml);
 943      $parser->collapse_dups = 0;
 944      $tree = $parser->get_tree();
 945      $settinggroupnames = array();
 946      $settingnames = array();
 947  
 948      foreach($tree['settings'][0]['settinggroup'] as $settinggroup)
 949      {
 950          $settinggroupnames[] = $settinggroup['attributes']['name'];
 951  
 952          $groupdata = array(
 953              "name" => $db->escape_string($settinggroup['attributes']['name']),
 954              "title" => $db->escape_string($settinggroup['attributes']['title']),
 955              "description" => $db->escape_string($settinggroup['attributes']['description']),
 956              "disporder" => (int)$settinggroup['attributes']['disporder'],
 957              "isdefault" => $settinggroup['attributes']['isdefault']
 958          );
 959          if(!$settinggroups[$settinggroup['attributes']['name']] || $redo == 2)
 960          {
 961              $gid = $db->insert_query("settinggroups", $groupdata);
 962              ++$groupcount;
 963          }
 964          else
 965          {
 966              $gid = $settinggroups[$settinggroup['attributes']['name']];
 967              $db->update_query("settinggroups", $groupdata, "gid='{$gid}'");
 968          }
 969  
 970          if(!$gid)
 971          {
 972              continue;
 973          }
 974  
 975          foreach($settinggroup['setting'] as $setting)
 976          {
 977              $settingnames[] = $setting['attributes']['name'];
 978  
 979              $settingdata = array(
 980                  "name" => $db->escape_string($setting['attributes']['name']),
 981                  "title" => $db->escape_string($setting['title'][0]['value']),
 982                  "description" => $db->escape_string($setting['description'][0]['value']),
 983                  "optionscode" => $db->escape_string($setting['optionscode'][0]['value']),
 984                  "disporder" => (int)$setting['disporder'][0]['value'],
 985                  "gid" => $gid,
 986                  "isdefault" => 1
 987              );
 988              if(!$settings[$setting['attributes']['name']] || $redo == 2)
 989              {
 990                  $settingdata['value'] = $db->escape_string($setting['settingvalue'][0]['value']);
 991                  $db->insert_query("settings", $settingdata);
 992                  $settingcount++;
 993              }
 994              else
 995              {
 996                  $name = $db->escape_string($setting['attributes']['name']);
 997                  $db->update_query("settings", $settingdata, "name='{$name}'");
 998              }
 999          }
1000      }
1001  
1002      if($redo >= 1)
1003      {
1004          require  MYBB_ROOT."inc/settings.php";
1005          foreach($settings as $key => $val)
1006          {
1007              $db->update_query("settings", array('value' => $db->escape_string($val)), "name='".$db->escape_string($key)."'");
1008          }
1009      }
1010      unset($settings);
1011      $settings = '';
1012      $query = $db->simple_select("settings", "*", "", array('order_by' => 'title'));
1013      while($setting = $db->fetch_array($query))
1014      {
1015          $setting['name'] = addcslashes($setting['name'], "\\'");
1016          $setting['value'] = addcslashes($setting['value'], '\\"$');
1017          $settings .= "\$settings['{$setting['name']}'] = \"".$setting['value']."\";\n";
1018      }
1019      $settings = "<?php\n/*********************************\ \n  DO NOT EDIT THIS FILE, PLEASE USE\n  THE SETTINGS EDITOR\n\*********************************/\n\n$settings\n";
1020      $file = fopen(MYBB_ROOT."inc/settings.php", "w");
1021      fwrite($file, $settings);
1022      fclose($file);
1023      return array($groupcount, $settingcount);
1024  }
1025  
1026  /**
1027   * @param int $redo 2 means that the tasks table will be dropped and recreated
1028   *
1029   * @return int
1030   */
1031  function sync_tasks($redo=0)
1032  {
1033      global $db;
1034  
1035      $taskcount = 0;
1036      $tasks = array();
1037      if($redo == 2)
1038      {
1039          $db->drop_table("tasks");
1040          switch($db->type)
1041          {
1042              case "pgsql":
1043                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks (
1044                      tid serial,
1045                      title varchar(120) NOT NULL default '',
1046                      description text NOT NULL default '',
1047                      file varchar(30) NOT NULL default '',
1048                      minute varchar(200) NOT NULL default '',
1049                      hour varchar(200) NOT NULL default '',
1050                      day varchar(100) NOT NULL default '',
1051                      month varchar(30) NOT NULL default '',
1052                      weekday varchar(15) NOT NULL default '',
1053                      nextrun bigint NOT NULL default '0',
1054                      lastrun bigint NOT NULL default '0',
1055                      enabled int NOT NULL default '1',
1056                      logging int NOT NULL default '0',
1057                      locked bigint NOT NULL default '0',
1058                      PRIMARY KEY(tid)
1059                  );");
1060                  break;
1061              case "sqlite":
1062                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks (
1063                      tid INTEGER PRIMARY KEY,
1064                      title varchar(120) NOT NULL default '',
1065                      description TEXT NOT NULL,
1066                      file varchar(30) NOT NULL default '',
1067                      minute varchar(200) NOT NULL default '',
1068                      hour varchar(200) NOT NULL default '',
1069                      day varchar(100) NOT NULL default '',
1070                      month varchar(30) NOT NULL default '',
1071                      weekday varchar(15) NOT NULL default '',
1072                      nextrun bigint(30) NOT NULL default '0',
1073                      lastrun bigint(30) NOT NULL default '0',
1074                      enabled int(1) NOT NULL default '1',
1075                      logging int(1) NOT NULL default '0',
1076                      locked bigint(30) NOT NULL default '0'
1077                  );");
1078                  break;
1079              case "mysql":
1080              default:
1081                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks (
1082                      tid int unsigned NOT NULL auto_increment,
1083                      title varchar(120) NOT NULL default '',
1084                      description text NOT NULL,
1085                      file varchar(30) NOT NULL default '',
1086                      minute varchar(200) NOT NULL default '',
1087                      hour varchar(200) NOT NULL default '',
1088                      day varchar(100) NOT NULL default '',
1089                      month varchar(30) NOT NULL default '',
1090                      weekday varchar(15) NOT NULL default '',
1091                      nextrun bigint(30) NOT NULL default '0',
1092                      lastrun bigint(30) NOT NULL default '0',
1093                      enabled int(1) NOT NULL default '1',
1094                      logging int(1) NOT NULL default '0',
1095                      locked bigint(30) NOT NULL default '0',
1096                      PRIMARY KEY (tid)
1097                  ) ENGINE=MyISAM;");
1098          }
1099      }
1100      else
1101      {
1102          $query = $db->simple_select("tasks", "file,tid");
1103          while($task = $db->fetch_array($query))
1104          {
1105              $tasks[$task['file']] = $task['tid'];
1106          }
1107      }
1108  
1109      require_once  MYBB_ROOT."inc/functions_task.php";
1110      $task_file = file_get_contents(INSTALL_ROOT.'resources/tasks.xml');
1111      $parser = create_xml_parser($task_file);
1112      $parser->collapse_dups = 0;
1113      $tree = $parser->get_tree();
1114  
1115      // Resync tasks
1116      foreach($tree['tasks'][0]['task'] as $task)
1117      {
1118          if(!$tasks[$task['file'][0]['value']] || $redo == 2)
1119          {
1120              $new_task = array(
1121                  'title' => $db->escape_string($task['title'][0]['value']),
1122                  'description' => $db->escape_string($task['description'][0]['value']),
1123                  'file' => $db->escape_string($task['file'][0]['value']),
1124                  'minute' => $db->escape_string($task['minute'][0]['value']),
1125                  'hour' => $db->escape_string($task['hour'][0]['value']),
1126                  'day' => $db->escape_string($task['day'][0]['value']),
1127                  'weekday' => $db->escape_string($task['weekday'][0]['value']),
1128                  'month' => $db->escape_string($task['month'][0]['value']),
1129                  'enabled' => $db->escape_string($task['enabled'][0]['value']),
1130                  'logging' => $db->escape_string($task['logging'][0]['value'])
1131              );
1132  
1133              $new_task['nextrun'] = fetch_next_run($new_task);
1134  
1135              $db->insert_query("tasks", $new_task);
1136              $taskcount++;
1137          }
1138          else
1139          {
1140              $update_task = array(
1141                  'title' => $db->escape_string($task['title'][0]['value']),
1142                  'description' => $db->escape_string($task['description'][0]['value']),
1143                  'file' => $db->escape_string($task['file'][0]['value']),
1144              );
1145  
1146              $db->update_query("tasks", $update_task, "file='".$db->escape_string($task['file'][0]['value'])."'");
1147          }
1148      }
1149  
1150      return $taskcount;
1151  }
1152  
1153  /**
1154   * Write our settings to the settings file
1155   */
1156  function write_settings()
1157  {
1158      global $db;
1159      $query = $db->simple_select("settings", "*", "", array('order_by' => 'title'));
1160      while($setting = $db->fetch_array($query))
1161      {
1162          $setting['name'] = addcslashes($setting['name'], "\\'");
1163          $setting['value'] = addcslashes($setting['value'], '\\"$');
1164          $settings .= "\$settings['{$setting['name']}'] = \"{$setting['value']}\";\n";
1165      }
1166      if(!empty($settings))
1167      {
1168          $settings = "<?php\n/*********************************\ \n  DO NOT EDIT THIS FILE, PLEASE USE\n  THE SETTINGS EDITOR\n\*********************************/\n\n{$settings}\n";
1169          $file = fopen(MYBB_ROOT."inc/settings.php", "w");
1170          fwrite($file, $settings);
1171          fclose($file);
1172      }
1173  }


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