[ Index ]

PHP Cross Reference of MyBB 1.8.37

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(!$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      return my_unserialize($data['contents']);
 796  }
 797  
 798  /**
 799   * @param string $title
 800   * @param mixed $contents
 801   */
 802  function add_upgrade_store($title, $contents)
 803  {
 804      global $db;
 805  
 806      $replace_array = array(
 807          "title" => $db->escape_string($title),
 808          "contents" => $db->escape_string(my_serialize($contents))
 809      );
 810      $db->replace_query("upgrade_data", $replace_array, "title");
 811  }
 812  
 813  /**
 814   * @param int $redo 2 means that all setting tables will be dropped and recreated
 815   *
 816   * @return array
 817   */
 818  function sync_settings($redo=0)
 819  {
 820      global $db;
 821  
 822      $settingcount = $groupcount = 0;
 823      $settings = $settinggroups = array();
 824      if($redo == 2)
 825      {
 826          $db->drop_table("settinggroups");
 827          switch($db->type)
 828          {
 829              case "pgsql":
 830                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settinggroups (
 831                    gid serial,
 832                    name varchar(100) NOT NULL default '',
 833                    title varchar(220) NOT NULL default '',
 834                    description text NOT NULL default '',
 835                    disporder smallint NOT NULL default '0',
 836                    isdefault int NOT NULL default '0',
 837                    PRIMARY KEY (gid)
 838                  );");
 839                  break;
 840              case "sqlite":
 841                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settinggroups (
 842                    gid INTEGER PRIMARY KEY,
 843                    name varchar(100) NOT NULL default '',
 844                    title varchar(220) NOT NULL default '',
 845                    description TEXT NOT NULL,
 846                    disporder smallint NOT NULL default '0',
 847                    isdefault int(1) NOT NULL default '0'
 848                  );");
 849                  break;
 850              case "mysql":
 851              default:
 852                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settinggroups (
 853                    gid smallint unsigned NOT NULL auto_increment,
 854                    name varchar(100) NOT NULL default '',
 855                    title varchar(220) NOT NULL default '',
 856                    description text NOT NULL,
 857                    disporder smallint unsigned NOT NULL default '0',
 858                    isdefault int(1) NOT NULL default '0',
 859                    PRIMARY KEY  (gid)
 860                  ) ENGINE=MyISAM;");
 861          }
 862  
 863          $db->drop_table("settings");
 864  
 865          switch($db->type)
 866          {
 867              case "pgsql":
 868                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settings (
 869                    sid serial,
 870                    name varchar(120) NOT NULL default '',
 871                    title varchar(120) NOT NULL default '',
 872                    description text NOT NULL default '',
 873                    optionscode text NOT NULL default '',
 874                    value text NOT NULL default '',
 875                    disporder smallint NOT NULL default '0',
 876                    gid smallint NOT NULL default '0',
 877                    isdefault int NOT NULL default '0',
 878                    PRIMARY KEY (sid)
 879                  );");
 880                  break;
 881              case "sqlite":
 882                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settings (
 883                    sid INTEGER PRIMARY KEY,
 884                    name varchar(120) NOT NULL default '',
 885                    title varchar(120) NOT NULL default '',
 886                    description TEXT NOT NULL,
 887                    optionscode TEXT NOT NULL,
 888                    value TEXT NOT NULL,
 889                    disporder smallint NOT NULL default '0',
 890                    gid smallint NOT NULL default '0',
 891                    isdefault int(1) NOT NULL default '0'
 892                  );");
 893                  break;
 894              case "mysql":
 895              default:
 896                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."settings (
 897                    sid smallint unsigned NOT NULL auto_increment,
 898                    name varchar(120) NOT NULL default '',
 899                    title varchar(120) NOT NULL default '',
 900                    description text NOT NULL,
 901                    optionscode text NOT NULL,
 902                    value text NOT NULL,
 903                    disporder smallint unsigned NOT NULL default '0',
 904                    gid smallint unsigned NOT NULL default '0',
 905                    isdefault int(1) NOT NULL default '0',
 906                    PRIMARY KEY (sid)
 907                  ) ENGINE=MyISAM;");
 908          }
 909      }
 910      else
 911      {
 912          if($db->type == "mysql" || $db->type == "mysqli")
 913          {
 914              $wheresettings = "isdefault='1' OR isdefault='yes'";
 915          }
 916          else
 917          {
 918              $wheresettings = "isdefault='1'";
 919          }
 920  
 921          $query = $db->simple_select("settinggroups", "name,title,gid", $wheresettings);
 922          while($group = $db->fetch_array($query))
 923          {
 924              $settinggroups[$group['name']] = $group['gid'];
 925          }
 926  
 927          // Collect all the user's settings - regardless of 'defaultivity' - we'll check them all
 928          // against default settings and insert/update them accordingly
 929          $query = $db->simple_select("settings", "name,sid");
 930          while($setting = $db->fetch_array($query))
 931          {
 932              $settings[$setting['name']] = $setting['sid'];
 933          }
 934      }
 935      $settings_xml = file_get_contents(INSTALL_ROOT."resources/settings.xml");
 936      $parser = create_xml_parser($settings_xml);
 937      $parser->collapse_dups = 0;
 938      $tree = $parser->get_tree();
 939      $settinggroupnames = array();
 940      $settingnames = array();
 941  
 942      foreach($tree['settings'][0]['settinggroup'] as $settinggroup)
 943      {
 944          $settinggroupnames[] = $settinggroup['attributes']['name'];
 945  
 946          $groupdata = array(
 947              "name" => $db->escape_string($settinggroup['attributes']['name']),
 948              "title" => $db->escape_string($settinggroup['attributes']['title']),
 949              "description" => $db->escape_string($settinggroup['attributes']['description']),
 950              "disporder" => (int)$settinggroup['attributes']['disporder'],
 951              "isdefault" => $settinggroup['attributes']['isdefault']
 952          );
 953          if(!$settinggroups[$settinggroup['attributes']['name']] || $redo == 2)
 954          {
 955              $gid = $db->insert_query("settinggroups", $groupdata);
 956              ++$groupcount;
 957          }
 958          else
 959          {
 960              $gid = $settinggroups[$settinggroup['attributes']['name']];
 961              $db->update_query("settinggroups", $groupdata, "gid='{$gid}'");
 962          }
 963  
 964          if(!$gid)
 965          {
 966              continue;
 967          }
 968  
 969          foreach($settinggroup['setting'] as $setting)
 970          {
 971              $settingnames[] = $setting['attributes']['name'];
 972  
 973              $settingdata = array(
 974                  "name" => $db->escape_string($setting['attributes']['name']),
 975                  "title" => $db->escape_string($setting['title'][0]['value']),
 976                  "description" => $db->escape_string($setting['description'][0]['value']),
 977                  "optionscode" => $db->escape_string($setting['optionscode'][0]['value']),
 978                  "disporder" => (int)$setting['disporder'][0]['value'],
 979                  "gid" => $gid,
 980                  "isdefault" => 1
 981              );
 982              if(!$settings[$setting['attributes']['name']] || $redo == 2)
 983              {
 984                  $settingdata['value'] = $db->escape_string($setting['settingvalue'][0]['value']);
 985                  $db->insert_query("settings", $settingdata);
 986                  $settingcount++;
 987              }
 988              else
 989              {
 990                  $name = $db->escape_string($setting['attributes']['name']);
 991                  $db->update_query("settings", $settingdata, "name='{$name}'");
 992              }
 993          }
 994      }
 995  
 996      if($redo >= 1)
 997      {
 998          require  MYBB_ROOT."inc/settings.php";
 999          foreach($settings as $key => $val)
1000          {
1001              $db->update_query("settings", array('value' => $db->escape_string($val)), "name='".$db->escape_string($key)."'");
1002          }
1003      }
1004      unset($settings);
1005      $settings = '';
1006      $query = $db->simple_select("settings", "*", "", array('order_by' => 'title'));
1007      while($setting = $db->fetch_array($query))
1008      {
1009          $setting['name'] = addcslashes($setting['name'], "\\'");
1010          $setting['value'] = addcslashes($setting['value'], '\\"$');
1011          $settings .= "\$settings['{$setting['name']}'] = \"".$setting['value']."\";\n";
1012      }
1013      $settings = "<?php\n/*********************************\ \n  DO NOT EDIT THIS FILE, PLEASE USE\n  THE SETTINGS EDITOR\n\*********************************/\n\n$settings\n";
1014      $file = fopen(MYBB_ROOT."inc/settings.php", "w");
1015      fwrite($file, $settings);
1016      fclose($file);
1017      return array($groupcount, $settingcount);
1018  }
1019  
1020  /**
1021   * @param int $redo 2 means that the tasks table will be dropped and recreated
1022   *
1023   * @return int
1024   */
1025  function sync_tasks($redo=0)
1026  {
1027      global $db;
1028  
1029      $taskcount = 0;
1030      $tasks = array();
1031      if($redo == 2)
1032      {
1033          $db->drop_table("tasks");
1034          switch($db->type)
1035          {
1036              case "pgsql":
1037                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks (
1038                      tid serial,
1039                      title varchar(120) NOT NULL default '',
1040                      description text NOT NULL default '',
1041                      file varchar(30) NOT NULL default '',
1042                      minute varchar(200) NOT NULL default '',
1043                      hour varchar(200) NOT NULL default '',
1044                      day varchar(100) NOT NULL default '',
1045                      month varchar(30) NOT NULL default '',
1046                      weekday varchar(15) NOT NULL default '',
1047                      nextrun bigint NOT NULL default '0',
1048                      lastrun bigint NOT NULL default '0',
1049                      enabled int NOT NULL default '1',
1050                      logging int NOT NULL default '0',
1051                      locked bigint NOT NULL default '0',
1052                      PRIMARY KEY(tid)
1053                  );");
1054                  break;
1055              case "sqlite":
1056                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks (
1057                      tid INTEGER PRIMARY KEY,
1058                      title varchar(120) NOT NULL default '',
1059                      description TEXT NOT NULL,
1060                      file varchar(30) NOT NULL default '',
1061                      minute varchar(200) NOT NULL default '',
1062                      hour varchar(200) NOT NULL default '',
1063                      day varchar(100) NOT NULL default '',
1064                      month varchar(30) NOT NULL default '',
1065                      weekday varchar(15) NOT NULL default '',
1066                      nextrun bigint(30) NOT NULL default '0',
1067                      lastrun bigint(30) NOT NULL default '0',
1068                      enabled int(1) NOT NULL default '1',
1069                      logging int(1) NOT NULL default '0',
1070                      locked bigint(30) NOT NULL default '0'
1071                  );");
1072                  break;
1073              case "mysql":
1074              default:
1075                  $db->write_query("CREATE TABLE ".TABLE_PREFIX."tasks (
1076                      tid int unsigned NOT NULL auto_increment,
1077                      title varchar(120) NOT NULL default '',
1078                      description text NOT NULL,
1079                      file varchar(30) NOT NULL default '',
1080                      minute varchar(200) NOT NULL default '',
1081                      hour varchar(200) NOT NULL default '',
1082                      day varchar(100) NOT NULL default '',
1083                      month varchar(30) NOT NULL default '',
1084                      weekday varchar(15) NOT NULL default '',
1085                      nextrun bigint(30) NOT NULL default '0',
1086                      lastrun bigint(30) NOT NULL default '0',
1087                      enabled int(1) NOT NULL default '1',
1088                      logging int(1) NOT NULL default '0',
1089                      locked bigint(30) NOT NULL default '0',
1090                      PRIMARY KEY (tid)
1091                  ) ENGINE=MyISAM;");
1092          }
1093      }
1094      else
1095      {
1096          $query = $db->simple_select("tasks", "file,tid");
1097          while($task = $db->fetch_array($query))
1098          {
1099              $tasks[$task['file']] = $task['tid'];
1100          }
1101      }
1102  
1103      require_once  MYBB_ROOT."inc/functions_task.php";
1104      $task_file = file_get_contents(INSTALL_ROOT.'resources/tasks.xml');
1105      $parser = create_xml_parser($task_file);
1106      $parser->collapse_dups = 0;
1107      $tree = $parser->get_tree();
1108  
1109      // Resync tasks
1110      foreach($tree['tasks'][0]['task'] as $task)
1111      {
1112          if(!$tasks[$task['file'][0]['value']] || $redo == 2)
1113          {
1114              $new_task = array(
1115                  'title' => $db->escape_string($task['title'][0]['value']),
1116                  'description' => $db->escape_string($task['description'][0]['value']),
1117                  'file' => $db->escape_string($task['file'][0]['value']),
1118                  'minute' => $db->escape_string($task['minute'][0]['value']),
1119                  'hour' => $db->escape_string($task['hour'][0]['value']),
1120                  'day' => $db->escape_string($task['day'][0]['value']),
1121                  'weekday' => $db->escape_string($task['weekday'][0]['value']),
1122                  'month' => $db->escape_string($task['month'][0]['value']),
1123                  'enabled' => $db->escape_string($task['enabled'][0]['value']),
1124                  'logging' => $db->escape_string($task['logging'][0]['value'])
1125              );
1126  
1127              $new_task['nextrun'] = fetch_next_run($new_task);
1128  
1129              $db->insert_query("tasks", $new_task);
1130              $taskcount++;
1131          }
1132          else
1133          {
1134              $update_task = array(
1135                  'title' => $db->escape_string($task['title'][0]['value']),
1136                  'description' => $db->escape_string($task['description'][0]['value']),
1137                  'file' => $db->escape_string($task['file'][0]['value']),
1138              );
1139  
1140              $db->update_query("tasks", $update_task, "file='".$db->escape_string($task['file'][0]['value'])."'");
1141          }
1142      }
1143  
1144      return $taskcount;
1145  }
1146  
1147  /**
1148   * Write our settings to the settings file
1149   */
1150  function write_settings()
1151  {
1152      global $db;
1153      $query = $db->simple_select("settings", "*", "", array('order_by' => 'title'));
1154      while($setting = $db->fetch_array($query))
1155      {
1156          $setting['name'] = addcslashes($setting['name'], "\\'");
1157          $setting['value'] = addcslashes($setting['value'], '\\"$');
1158          $settings .= "\$settings['{$setting['name']}'] = \"{$setting['value']}\";\n";
1159      }
1160      if(!empty($settings))
1161      {
1162          $settings = "<?php\n/*********************************\ \n  DO NOT EDIT THIS FILE, PLEASE USE\n  THE SETTINGS EDITOR\n\*********************************/\n\n{$settings}\n";
1163          $file = fopen(MYBB_ROOT."inc/settings.php", "w");
1164          fwrite($file, $settings);
1165          fclose($file);
1166      }
1167  }


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