Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
/admin/ -> index.php (source)

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Main administration script.
  20   *
  21   * @package    core
  22   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // Check that config.php exists, if not then call the install script
  27  if (!file_exists('../config.php')) {
  28      header('Location: ../install.php');
  29      die();
  30  }
  31  
  32  // Check that PHP is of a sufficient version as soon as possible.
  33  require_once (__DIR__.'/../lib/phpminimumversionlib.php');
  34  moodle_require_minimum_php_version();
  35  
  36  // make sure iconv is available and actually works
  37  if (!function_exists('iconv')) {
  38      // this should not happen, this must be very borked install
  39      echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
  40      die();
  41  }
  42  
  43  // Make sure php5-json is available.
  44  if (!function_exists('json_encode') || !function_exists('json_decode')) {
  45      // This also shouldn't happen.
  46      echo 'Moodle requires the json PHP extension. Please install or enable the json extension.';
  47      die();
  48  }
  49  
  50  // Make sure xml extension is available.
  51  if (!extension_loaded('xml')) {
  52      echo 'Moodle requires the xml PHP extension. Please install or enable the xml extension.';
  53      die();
  54  }
  55  
  56  // Make sure mbstring extension is available.
  57  if (!extension_loaded('mbstring')) {
  58      echo 'Moodle requires the mbstring PHP extension. Please install or enable the mbstring extension.';
  59      die();
  60  }
  61  
  62  define('NO_OUTPUT_BUFFERING', true);
  63  
  64  if (isset($_POST['upgradekey'])) {
  65      // Before you start reporting issues about the collision attacks against
  66      // SHA-1, you should understand that we are not actually attempting to do
  67      // any cryptography here. This is hashed purely so that the key is not
  68      // that apparent in the address bar itself. Anyone who catches the HTTP
  69      // traffic can immediately use it as a valid admin key.
  70      header('Location: index.php?cache=0&upgradekeyhash='.sha1($_POST['upgradekey']));
  71      die();
  72  }
  73  
  74  if ((isset($_GET['cache']) and $_GET['cache'] === '0')
  75          or (isset($_POST['cache']) and $_POST['cache'] === '0')
  76          or (!isset($_POST['cache']) and !isset($_GET['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey']))) {
  77      // Prevent caching at all cost when visiting this page directly,
  78      // we redirect to self once we known no upgrades are necessary.
  79      // Note: $_GET and $_POST are used here intentionally because our param cleaning is not loaded yet.
  80      // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
  81      define('CACHE_DISABLE_ALL', true);
  82  
  83      // Force OPcache reset if used, we do not want any stale caches
  84      // when detecting if upgrade necessary or when running upgrade.
  85      if (function_exists('opcache_reset')) {
  86          opcache_reset();
  87      }
  88      $cache = 0;
  89  
  90  } else {
  91      $cache = 1;
  92  }
  93  
  94  require('../config.php');
  95  
  96  // Invalidate the cache of version.php in any circumstances to help core_component
  97  // detecting if the version has changed and component cache should be reset.
  98  if (function_exists('opcache_invalidate')) {
  99      opcache_invalidate($CFG->dirroot . '/version.php', true);
 100  }
 101  // Make sure the component cache gets rebuilt if necessary, any method that
 102  // indirectly calls the protected init() method is good here.
 103  core_component::get_core_subsystems();
 104  
 105  if (is_major_upgrade_required() && isloggedin()) {
 106      // A major upgrade is required.
 107      // Terminate the session and redirect back here before anything DB-related happens.
 108      redirect_if_major_upgrade_required();
 109  }
 110  
 111  require_once($CFG->libdir.'/adminlib.php');    // various admin-only functions
 112  require_once($CFG->libdir.'/upgradelib.php');  // general upgrade/install related functions
 113  
 114  $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); // Core upgrade confirmed?
 115  $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); // Core release info and server checks confirmed?
 116  $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL); // Plugins check page confirmed?
 117  $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL); // Show all plugins on the plugins check page?
 118  $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); // GPL license confirmed for installation?
 119  $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
 120  $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW); // Plugin installation requested at moodle.org/plugins.
 121  $upgradekeyhash = optional_param('upgradekeyhash', null, PARAM_ALPHANUM); // Hash of provided upgrade key.
 122  $installdep = optional_param('installdep', null, PARAM_COMPONENT); // Install given missing dependency (required plugin).
 123  $installdepx = optional_param('installdepx', false, PARAM_BOOL); // Install all missing dependencies.
 124  $confirminstalldep = optional_param('confirminstalldep', false, PARAM_BOOL); // Installing dependencies confirmed.
 125  $abortinstall = optional_param('abortinstall', null, PARAM_COMPONENT); // Cancel installation of the given new plugin.
 126  $abortinstallx = optional_param('abortinstallx', null, PARAM_BOOL); // Cancel installation of all new plugins.
 127  $confirmabortinstall = optional_param('confirmabortinstall', false, PARAM_BOOL); // Installation cancel confirmed.
 128  $abortupgrade = optional_param('abortupgrade', null, PARAM_COMPONENT); // Cancel upgrade of the given existing plugin.
 129  $abortupgradex = optional_param('abortupgradex', null, PARAM_BOOL); // Cancel upgrade of all upgradable plugins.
 130  $confirmabortupgrade = optional_param('confirmabortupgrade', false, PARAM_BOOL); // Upgrade cancel confirmed.
 131  $installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
 132  $installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
 133  $installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
 134  $confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
 135  
 136  if (!empty($CFG->disableupdateautodeploy)) {
 137      // Invalidate all requests to install plugins via the admin UI.
 138      $newaddonreq = null;
 139      $installdep = null;
 140      $installdepx = false;
 141      $abortupgrade = null;
 142      $abortupgradex = null;
 143      $installupdate = null;
 144      $installupdateversion = null;
 145      $installupdatex = false;
 146  }
 147  
 148  // Set up PAGE.
 149  $url = new moodle_url('/admin/index.php');
 150  $url->param('cache', $cache);
 151  if (isset($upgradekeyhash)) {
 152      $url->param('upgradekeyhash', $upgradekeyhash);
 153  }
 154  $PAGE->set_url($url);
 155  unset($url);
 156  
 157  // Are we returning from an add-on installation request at moodle.org/plugins?
 158  if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
 159      $target = new moodle_url('/admin/tool/installaddon/index.php', array(
 160          'installaddonrequest' => $newaddonreq,
 161          'confirm' => 0));
 162      if (!isloggedin() or isguestuser()) {
 163          // Login and go the the add-on tool page.
 164          $SESSION->wantsurl = $target->out();
 165          redirect(get_login_url());
 166      }
 167      redirect($target);
 168  }
 169  
 170  $PAGE->set_pagelayout('admin'); // Set a default pagelayout
 171  
 172  $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
 173  
 174  // Check some PHP server settings
 175  
 176  if (ini_get_bool('session.auto_start')) {
 177      print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
 178  }
 179  
 180  if (!ini_get_bool('file_uploads')) {
 181      print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
 182  }
 183  
 184  if (is_float_problem()) {
 185      print_error('phpfloatproblem', 'admin', '', $documentationlink);
 186  }
 187  
 188  // Set some necessary variables during set-up to avoid PHP warnings later on this page
 189  if (!isset($CFG->release)) {
 190      $CFG->release = '';
 191  }
 192  if (!isset($CFG->version)) {
 193      $CFG->version = '';
 194  }
 195  if (!isset($CFG->branch)) {
 196      $CFG->branch = '';
 197  }
 198  
 199  $version = null;
 200  $release = null;
 201  $branch = null;
 202  require("$CFG->dirroot/version.php");       // defines $version, $release, $branch and $maturity
 203  $CFG->target_release = $release;            // used during installation and upgrades
 204  
 205  if (!$version or !$release) {
 206      print_error('withoutversion', 'debug'); // without version, stop
 207  }
 208  
 209  if (!core_tables_exist()) {
 210      $PAGE->set_pagelayout('maintenance');
 211      $PAGE->set_popup_notification_allowed(false);
 212  
 213      // fake some settings
 214      $CFG->docroot = 'http://docs.moodle.org';
 215  
 216      $strinstallation = get_string('installation', 'install');
 217  
 218      // remove current session content completely
 219      \core\session\manager::terminate_current();
 220  
 221      if (empty($agreelicense)) {
 222          $strlicense = get_string('license');
 223  
 224          $PAGE->navbar->add($strlicense);
 225          $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
 226          $PAGE->set_heading($strinstallation);
 227          $PAGE->set_cacheable(false);
 228  
 229          $output = $PAGE->get_renderer('core', 'admin');
 230          echo $output->install_licence_page();
 231          die();
 232      }
 233      if (empty($confirmrelease)) {
 234          require_once($CFG->libdir.'/environmentlib.php');
 235          list($envstatus, $environmentresults) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
 236          $strcurrentrelease = get_string('currentrelease');
 237  
 238          $PAGE->navbar->add($strcurrentrelease);
 239          $PAGE->set_title($strinstallation);
 240          $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
 241          $PAGE->set_cacheable(false);
 242  
 243          $output = $PAGE->get_renderer('core', 'admin');
 244          echo $output->install_environment_page($maturity, $envstatus, $environmentresults, $release);
 245          die();
 246      }
 247  
 248      // check plugin dependencies
 249      $failed = array();
 250      if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed, $CFG->branch)) {
 251          $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
 252          $PAGE->set_title($strinstallation);
 253          $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
 254  
 255          $output = $PAGE->get_renderer('core', 'admin');
 256          $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
 257          echo $output->unsatisfied_dependencies_page($version, $failed, $url);
 258          die();
 259      }
 260      unset($failed);
 261  
 262      //TODO: add a page with list of non-standard plugins here
 263  
 264      $strdatabasesetup = get_string('databasesetup');
 265      upgrade_init_javascript();
 266  
 267      $PAGE->navbar->add($strdatabasesetup);
 268      $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
 269      $PAGE->set_heading($strinstallation);
 270      $PAGE->set_cacheable(false);
 271  
 272      $output = $PAGE->get_renderer('core', 'admin');
 273      echo $output->header();
 274  
 275      if (!$DB->setup_is_unicodedb()) {
 276          if (!$DB->change_db_encoding()) {
 277              // If could not convert successfully, throw error, and prevent installation
 278              print_error('unicoderequired', 'admin');
 279          }
 280      }
 281  
 282      install_core($version, true);
 283  }
 284  
 285  
 286  // Check version of Moodle code on disk compared with database
 287  // and upgrade if possible.
 288  
 289  if (!$cache) {
 290      // Do not try to do anything fancy in non-cached mode,
 291      // this prevents themes from fetching data from non-existent tables.
 292      $PAGE->set_pagelayout('maintenance');
 293      $PAGE->set_popup_notification_allowed(false);
 294  }
 295  
 296  $stradministration = get_string('administration');
 297  $PAGE->set_context(context_system::instance());
 298  
 299  if (empty($CFG->version)) {
 300      print_error('missingconfigversion', 'debug');
 301  }
 302  
 303  // Detect config cache inconsistency, this happens when you switch branches on dev servers.
 304  if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
 305      purge_all_caches();
 306      redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
 307  }
 308  
 309  if (!$cache and $version > $CFG->version) {  // upgrade
 310  
 311      $PAGE->set_url(new moodle_url($PAGE->url, array(
 312          'confirmupgrade' => $confirmupgrade,
 313          'confirmrelease' => $confirmrelease,
 314          'confirmplugincheck' => $confirmplugins,
 315      )));
 316  
 317      check_upgrade_key($upgradekeyhash);
 318  
 319      // Warning about upgrading a test site.
 320      $testsite = false;
 321      if (defined('BEHAT_SITE_RUNNING')) {
 322          $testsite = 'behat';
 323      }
 324  
 325      if (isset($CFG->themerev)) {
 326          // Store the themerev to restore after purging caches.
 327          $themerev = $CFG->themerev;
 328      }
 329  
 330      // We purge all of MUC's caches here.
 331      // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
 332      // This ensures a real config object is loaded and the stores will be purged.
 333      // This is the only way we can purge custom caches such as memcache or APC.
 334      // Note: all other calls to caches will still used the disabled API.
 335      cache_helper::purge_all(true);
 336      // We then purge the regular caches.
 337      purge_all_caches();
 338  
 339      if (isset($themerev)) {
 340          // Restore the themerev
 341          set_config('themerev', $themerev);
 342      }
 343  
 344      $output = $PAGE->get_renderer('core', 'admin');
 345  
 346      if (upgrade_stale_php_files_present()) {
 347          $PAGE->set_title($stradministration);
 348          $PAGE->set_cacheable(false);
 349  
 350          echo $output->upgrade_stale_php_files_page();
 351          die();
 352      }
 353  
 354      if (empty($confirmupgrade)) {
 355          $a = new stdClass();
 356          $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
 357          $a->newversion = "$release (".sprintf('%.2f', $version).")";
 358          $strdatabasechecking = get_string('databasechecking', '', $a);
 359  
 360          $PAGE->set_title($stradministration);
 361          $PAGE->set_heading($strdatabasechecking);
 362          $PAGE->set_cacheable(false);
 363  
 364          echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
 365          die();
 366  
 367      } else if (empty($confirmrelease)) {
 368          require_once($CFG->libdir.'/environmentlib.php');
 369          list($envstatus, $environmentresults) = check_moodle_environment($release, ENV_SELECT_RELEASE);
 370          $strcurrentrelease = get_string('currentrelease');
 371  
 372          $PAGE->navbar->add($strcurrentrelease);
 373          $PAGE->set_title($strcurrentrelease);
 374          $PAGE->set_heading($strcurrentrelease);
 375          $PAGE->set_cacheable(false);
 376  
 377          echo $output->upgrade_environment_page($release, $envstatus, $environmentresults);
 378          die();
 379  
 380      } else if (empty($confirmplugins)) {
 381          $strplugincheck = get_string('plugincheck');
 382  
 383          $PAGE->navbar->add($strplugincheck);
 384          $PAGE->set_title($strplugincheck);
 385          $PAGE->set_heading($strplugincheck);
 386          $PAGE->set_cacheable(false);
 387  
 388          $pluginman = core_plugin_manager::instance();
 389  
 390          // Check for available updates.
 391          if ($fetchupdates) {
 392              // No sesskey support guaranteed here, because sessions might not work yet.
 393              $updateschecker = \core\update\checker::instance();
 394              if ($updateschecker->enabled()) {
 395                  $updateschecker->fetch();
 396              }
 397              redirect($PAGE->url);
 398          }
 399  
 400          // Cancel all plugin installations.
 401          if ($abortinstallx) {
 402              // No sesskey support guaranteed here, because sessions might not work yet.
 403              $abortables = $pluginman->list_cancellable_installations();
 404              if ($abortables) {
 405                  if ($confirmabortinstall) {
 406                      foreach ($abortables as $plugin) {
 407                          $pluginman->cancel_plugin_installation($plugin->component);
 408                      }
 409                      redirect($PAGE->url);
 410                  } else {
 411                      $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1));
 412                      echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
 413                      die();
 414                  }
 415              }
 416              redirect($PAGE->url);
 417          }
 418  
 419          // Cancel single plugin installation.
 420          if ($abortinstall) {
 421              // No sesskey support guaranteed here, because sessions might not work yet.
 422              if ($confirmabortinstall) {
 423                  $pluginman->cancel_plugin_installation($abortinstall);
 424                  redirect($PAGE->url);
 425              } else {
 426                  $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
 427                  $abortable = $pluginman->get_plugin_info($abortinstall);
 428                  if ($pluginman->can_cancel_plugin_installation($abortable)) {
 429                      echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
 430                      die();
 431                  }
 432                  redirect($PAGE->url);
 433              }
 434          }
 435  
 436          // Cancel all plugins upgrades (that is, restore archived versions).
 437          if ($abortupgradex) {
 438              // No sesskey support guaranteed here, because sessions might not work yet.
 439              $restorable = $pluginman->list_restorable_archives();
 440              if ($restorable) {
 441                  upgrade_install_plugins($restorable, $confirmabortupgrade,
 442                      get_string('cancelupgradehead', 'core_plugin'),
 443                      new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
 444                  );
 445              }
 446              redirect($PAGE->url);
 447          }
 448  
 449          // Cancel single plugin upgrade (that is, install the archived version).
 450          if ($abortupgrade) {
 451              // No sesskey support guaranteed here, because sessions might not work yet.
 452              $restorable = $pluginman->list_restorable_archives();
 453              if (isset($restorable[$abortupgrade])) {
 454                  $restorable = array($restorable[$abortupgrade]);
 455                  upgrade_install_plugins($restorable, $confirmabortupgrade,
 456                      get_string('cancelupgradehead', 'core_plugin'),
 457                      new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
 458                  );
 459              }
 460              redirect($PAGE->url);
 461          }
 462  
 463          // Install all available missing dependencies.
 464          if ($installdepx) {
 465              // No sesskey support guaranteed here, because sessions might not work yet.
 466              $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
 467              upgrade_install_plugins($installable, $confirminstalldep,
 468                  get_string('dependencyinstallhead', 'core_plugin'),
 469                  new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
 470              );
 471          }
 472  
 473          // Install single available missing dependency.
 474          if ($installdep) {
 475              // No sesskey support guaranteed here, because sessions might not work yet.
 476              $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
 477              if (!empty($installable[$installdep])) {
 478                  $installable = array($installable[$installdep]);
 479                  upgrade_install_plugins($installable, $confirminstalldep,
 480                      get_string('dependencyinstallhead', 'core_plugin'),
 481                      new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
 482                  );
 483              }
 484          }
 485  
 486          // Install all available updates.
 487          if ($installupdatex) {
 488              // No sesskey support guaranteed here, because sessions might not work yet.
 489              $installable = $pluginman->filter_installable($pluginman->available_updates());
 490              upgrade_install_plugins($installable, $confirminstallupdate,
 491                  get_string('updateavailableinstallallhead', 'core_admin'),
 492                  new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
 493              );
 494          }
 495  
 496          // Install single available update.
 497          if ($installupdate and $installupdateversion) {
 498              // No sesskey support guaranteed here, because sessions might not work yet.
 499              if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
 500                  $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
 501                  upgrade_install_plugins($installable, $confirminstallupdate,
 502                      get_string('updateavailableinstallallhead', 'core_admin'),
 503                      new moodle_url($PAGE->url, array('installupdate' => $installupdate,
 504                          'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
 505                      )
 506                  );
 507              }
 508          }
 509  
 510          echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
 511                  $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
 512          die();
 513  
 514      } else {
 515          // Always verify plugin dependencies!
 516          $failed = array();
 517          if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed, $CFG->branch)) {
 518              echo $output->unsatisfied_dependencies_page($version, $failed, new moodle_url($PAGE->url,
 519                  array('confirmplugincheck' => 0)));
 520              die();
 521          }
 522          unset($failed);
 523  
 524          // Launch main upgrade.
 525          upgrade_core($version, true);
 526      }
 527  } else if ($version < $CFG->version) {
 528      // better stop here, we can not continue with plugin upgrades or anything else
 529      throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
 530  }
 531  
 532  // Updated human-readable release version if necessary
 533  if (!$cache and $release <> $CFG->release) {  // Update the release version
 534      set_config('release', $release);
 535  }
 536  
 537  if (!$cache and $branch <> $CFG->branch) {  // Update the branch
 538      set_config('branch', $branch);
 539  }
 540  
 541  if (!$cache and moodle_needs_upgrading()) {
 542  
 543      $PAGE->set_url(new moodle_url($PAGE->url, array(
 544          'confirmrelease' => $confirmrelease,
 545          'confirmplugincheck' => $confirmplugins,
 546      )));
 547  
 548      check_upgrade_key($upgradekeyhash);
 549  
 550      if (!$PAGE->headerprinted) {
 551          // means core upgrade or installation was not already done
 552  
 553          $pluginman = core_plugin_manager::instance();
 554          $output = $PAGE->get_renderer('core', 'admin');
 555  
 556          if (empty($confirmrelease)) {
 557              require_once($CFG->libdir . '/environmentlib.php');
 558  
 559              list($envstatus, $environmentresults) = check_moodle_environment($release, ENV_SELECT_RELEASE);
 560              $strcurrentrelease = get_string('currentrelease');
 561  
 562              $PAGE->navbar->add($strcurrentrelease);
 563              $PAGE->set_title($strcurrentrelease);
 564              $PAGE->set_heading($strcurrentrelease);
 565              $PAGE->set_cacheable(false);
 566  
 567              echo $output->upgrade_environment_page($release, $envstatus, $environmentresults);
 568              die();
 569  
 570          } else if (!$confirmplugins) {
 571              $strplugincheck = get_string('plugincheck');
 572  
 573              $PAGE->navbar->add($strplugincheck);
 574              $PAGE->set_title($strplugincheck);
 575              $PAGE->set_heading($strplugincheck);
 576              $PAGE->set_cacheable(false);
 577  
 578              // Check for available updates.
 579              if ($fetchupdates) {
 580                  require_sesskey();
 581                  $updateschecker = \core\update\checker::instance();
 582                  if ($updateschecker->enabled()) {
 583                      $updateschecker->fetch();
 584                  }
 585                  redirect($PAGE->url);
 586              }
 587  
 588              // Cancel all plugin installations.
 589              if ($abortinstallx) {
 590                  require_sesskey();
 591                  $abortables = $pluginman->list_cancellable_installations();
 592                  if ($abortables) {
 593                      if ($confirmabortinstall) {
 594                          foreach ($abortables as $plugin) {
 595                              $pluginman->cancel_plugin_installation($plugin->component);
 596                          }
 597                          redirect($PAGE->url);
 598                      } else {
 599                          $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx,
 600                              'confirmabortinstall' => 1));
 601                          echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
 602                          die();
 603                      }
 604                  }
 605                  redirect($PAGE->url);
 606              }
 607  
 608              // Cancel single plugin installation.
 609              if ($abortinstall) {
 610                  require_sesskey();
 611                  if ($confirmabortinstall) {
 612                      $pluginman->cancel_plugin_installation($abortinstall);
 613                      redirect($PAGE->url);
 614                  } else {
 615                      $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
 616                      $abortable = $pluginman->get_plugin_info($abortinstall);
 617                      if ($pluginman->can_cancel_plugin_installation($abortable)) {
 618                          echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
 619                          die();
 620                      }
 621                      redirect($PAGE->url);
 622                  }
 623              }
 624  
 625              // Cancel all plugins upgrades (that is, restore archived versions).
 626              if ($abortupgradex) {
 627                  require_sesskey();
 628                  $restorable = $pluginman->list_restorable_archives();
 629                  if ($restorable) {
 630                      upgrade_install_plugins($restorable, $confirmabortupgrade,
 631                          get_string('cancelupgradehead', 'core_plugin'),
 632                          new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
 633                      );
 634                  }
 635                  redirect($PAGE->url);
 636              }
 637  
 638              // Cancel single plugin upgrade (that is, install the archived version).
 639              if ($abortupgrade) {
 640                  require_sesskey();
 641                  $restorable = $pluginman->list_restorable_archives();
 642                  if (isset($restorable[$abortupgrade])) {
 643                      $restorable = array($restorable[$abortupgrade]);
 644                      upgrade_install_plugins($restorable, $confirmabortupgrade,
 645                          get_string('cancelupgradehead', 'core_plugin'),
 646                          new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
 647                      );
 648                  }
 649                  redirect($PAGE->url);
 650              }
 651  
 652              // Install all available missing dependencies.
 653              if ($installdepx) {
 654                  require_sesskey();
 655                  $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
 656                  upgrade_install_plugins($installable, $confirminstalldep,
 657                      get_string('dependencyinstallhead', 'core_plugin'),
 658                      new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
 659                  );
 660              }
 661  
 662              // Install single available missing dependency.
 663              if ($installdep) {
 664                  require_sesskey();
 665                  $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
 666                  if (!empty($installable[$installdep])) {
 667                      $installable = array($installable[$installdep]);
 668                      upgrade_install_plugins($installable, $confirminstalldep,
 669                          get_string('dependencyinstallhead', 'core_plugin'),
 670                          new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
 671                      );
 672                  }
 673              }
 674  
 675              // Install all available updates.
 676              if ($installupdatex) {
 677                  require_sesskey();
 678                  $installable = $pluginman->filter_installable($pluginman->available_updates());
 679                  upgrade_install_plugins($installable, $confirminstallupdate,
 680                      get_string('updateavailableinstallallhead', 'core_admin'),
 681                      new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
 682                  );
 683              }
 684  
 685              // Install single available update.
 686              if ($installupdate and $installupdateversion) {
 687                  require_sesskey();
 688                  if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
 689                      $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
 690                      upgrade_install_plugins($installable, $confirminstallupdate,
 691                          get_string('updateavailableinstallallhead', 'core_admin'),
 692                          new moodle_url($PAGE->url, array('installupdate' => $installupdate,
 693                              'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
 694                          )
 695                      );
 696                  }
 697              }
 698  
 699              // Show plugins info.
 700              echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
 701                      $version, $showallplugins,
 702                      new moodle_url($PAGE->url),
 703                      new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
 704              die();
 705          }
 706  
 707          // Make sure plugin dependencies are always checked.
 708          $failed = array();
 709          if (!$pluginman->all_plugins_ok($version, $failed, $CFG->branch)) {
 710              $output = $PAGE->get_renderer('core', 'admin');
 711              echo $output->unsatisfied_dependencies_page($version, $failed, new moodle_url($PAGE->url,
 712                  array('confirmplugincheck' => 0)));
 713              die();
 714          }
 715          unset($failed);
 716      }
 717  
 718      // install/upgrade all plugins and other parts
 719      upgrade_noncore(true);
 720  }
 721  
 722  // If this is the first install, indicate that this site is fully configured
 723  // except the admin password
 724  if (during_initial_install()) {
 725      set_config('rolesactive', 1); // after this, during_initial_install will return false.
 726      set_config('adminsetuppending', 1);
 727      set_config('registrationpending', 1); // Remind to register site after all other setup is finished.
 728  
 729      // Apply default preset, if it is defined in $CFG and has a valid value.
 730      if (!empty($CFG->setsitepresetduringinstall)) {
 731          \core_adminpresets\helper::change_default_preset($CFG->setsitepresetduringinstall);
 732      }
 733  
 734      // we need this redirect to setup proper session
 735      upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
 736  }
 737  
 738  // make sure admin user is created - this is the last step because we need
 739  // session to be working properly in order to edit admin account
 740   if (!empty($CFG->adminsetuppending)) {
 741      $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
 742      if (!$sessionstarted) {
 743          redirect("index.php?sessionstarted=1&lang=$CFG->lang");
 744      } else {
 745          $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
 746          if (!$sessionverify) {
 747              $SESSION->sessionverify = 1;
 748              redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
 749          } else {
 750              if (empty($SESSION->sessionverify)) {
 751                  print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
 752              }
 753              unset($SESSION->sessionverify);
 754          }
 755      }
 756  
 757      // Cleanup SESSION to make sure other code does not complain in the future.
 758      unset($SESSION->has_timed_out);
 759      unset($SESSION->wantsurl);
 760  
 761      // at this stage there can be only one admin unless more were added by install - users may change username, so do not rely on that
 762      $adminids = explode(',', $CFG->siteadmins);
 763      $adminuser = get_complete_user_data('id', reset($adminids));
 764  
 765      if ($adminuser->password === 'adminsetuppending') {
 766          // prevent installation hijacking
 767          if ($adminuser->lastip !== getremoteaddr()) {
 768              print_error('installhijacked', 'admin');
 769          }
 770          // login user and let him set password and admin details
 771          $adminuser->newadminuser = 1;
 772          complete_user_login($adminuser);
 773          redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
 774  
 775      } else {
 776          unset_config('adminsetuppending');
 777      }
 778  
 779  } else {
 780      // just make sure upgrade logging is properly terminated
 781      upgrade_finished('upgradesettings.php');
 782  }
 783  
 784  if (has_capability('moodle/site:config', context_system::instance())) {
 785      if ($fetchupdates) {
 786          require_sesskey();
 787          $updateschecker = \core\update\checker::instance();
 788          if ($updateschecker->enabled()) {
 789              $updateschecker->fetch();
 790          }
 791          redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
 792      }
 793  }
 794  
 795  // Now we can be sure everything was upgraded and caches work fine,
 796  // redirect if necessary to make sure caching is enabled.
 797  if (!$cache) {
 798      redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
 799  }
 800  
 801  // Check for valid admin user - no guest autologin
 802  require_login(0, false);
 803  if (isguestuser()) {
 804      // Login as real user!
 805      $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
 806      redirect(get_login_url());
 807  }
 808  $context = context_system::instance();
 809  
 810  if (!has_capability('moodle/site:config', $context)) {
 811      // Do not throw exception display an empty page with administration menu if visible for current user.
 812      $PAGE->set_title($SITE->fullname);
 813      $PAGE->set_heading($SITE->fullname);
 814      echo $OUTPUT->header();
 815      echo $OUTPUT->footer();
 816      exit;
 817  }
 818  
 819  // check that site is properly customized
 820  $site = get_site();
 821  if (empty($site->shortname)) {
 822      // probably new installation - lets return to frontpage after this step
 823      // remove settings that we want uninitialised
 824      unset_config('registerauth');
 825      unset_config('timezone'); // Force admin to select timezone!
 826      redirect('upgradesettings.php?return=site');
 827  }
 828  
 829  // setup critical warnings before printing admin tree block
 830  $insecuredataroot = is_dataroot_insecure(true);
 831  $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
 832  
 833  $adminroot = admin_get_root();
 834  $PAGE->set_primary_active_tab('siteadminnode');
 835  
 836  // Check if there are any new admin settings which have still yet to be set
 837  if (any_new_admin_settings($adminroot)) {
 838      redirect('upgradesettings.php');
 839  }
 840  
 841  // Return to original page that started the plugin uninstallation if necessary.
 842  if (isset($SESSION->pluginuninstallreturn)) {
 843      $return = $SESSION->pluginuninstallreturn;
 844      unset($SESSION->pluginuninstallreturn);
 845      if ($return) {
 846          redirect($return);
 847      }
 848  }
 849  
 850  // If site registration needs updating, redirect.
 851  \core\hub\registration::registration_reminder('/admin/index.php');
 852  
 853  // Everything should now be set up, and the user is an admin
 854  
 855  // Print default admin page with notifications.
 856  $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
 857  
 858  $lastcron = get_config('tool_task', 'lastcronstart');
 859  $cronoverdue = ($lastcron < time() - 3600 * 24);
 860  $lastcroninterval = get_config('tool_task', 'lastcroninterval');
 861  
 862  $expectedfrequency = $CFG->expectedcronfrequency ?? MINSECS;
 863  $croninfrequent = !$cronoverdue && ($lastcroninterval > ($expectedfrequency + MINSECS) || $lastcron < time() - $expectedfrequency);
 864  $dbproblems = $DB->diagnose();
 865  $maintenancemode = !empty($CFG->maintenance_enabled);
 866  
 867  // Available updates for Moodle core.
 868  $updateschecker = \core\update\checker::instance();
 869  $availableupdates = array();
 870  $availableupdatesfetch = null;
 871  
 872  if ($updateschecker->enabled()) {
 873      // Only compute the update information when it is going to be displayed to the user.
 874      $availableupdates['core'] = $updateschecker->get_update_info('core',
 875          array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
 876  
 877      // Available updates for contributed plugins
 878      $pluginman = core_plugin_manager::instance();
 879      foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
 880          foreach ($plugintypeinstances as $pluginname => $plugininfo) {
 881              $pluginavailableupdates = $plugininfo->available_updates();
 882              if (!empty($pluginavailableupdates)) {
 883                  foreach ($pluginavailableupdates as $pluginavailableupdate) {
 884                      if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
 885                          $availableupdates[$plugintype.'_'.$pluginname] = array();
 886                      }
 887                      $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
 888                  }
 889              }
 890          }
 891      }
 892  
 893      // The timestamp of the most recent check for available updates
 894      $availableupdatesfetch = $updateschecker->get_last_timefetched();
 895  }
 896  
 897  $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
 898  //check if the site is registered on Moodle.org
 899  $registered = \core\hub\registration::is_registered();
 900  // Check if there are any cache warnings.
 901  $cachewarnings = cache_helper::warnings();
 902  // Check if there are events 1 API handlers.
 903  $eventshandlers = $DB->get_records_sql('SELECT DISTINCT component FROM {events_handlers}');
 904  $themedesignermode = !empty($CFG->themedesignermode);
 905  $mobileconfigured = !empty($CFG->enablemobilewebservice);
 906  $invalidforgottenpasswordurl = !empty($CFG->forgottenpasswordurl) && empty(clean_param($CFG->forgottenpasswordurl, PARAM_URL));
 907  
 908  // Check if a directory with development libraries exists.
 909  if (empty($CFG->disabledevlibdirscheck) && (is_dir($CFG->dirroot.'/vendor') || is_dir($CFG->dirroot.'/node_modules'))) {
 910      $devlibdir = true;
 911  } else {
 912      $devlibdir = false;
 913  }
 914  // Check if the site is being foced onto ssl.
 915  $overridetossl = !empty($CFG->overridetossl);
 916  
 917  // Check if moodle campaign content setting is enabled or not.
 918  $showcampaigncontent = !isset($CFG->showcampaigncontent) || $CFG->showcampaigncontent;
 919  
 920  // Encourage admins to enable the user feedback feature if it is not enabled already.
 921  $showfeedbackencouragement = empty($CFG->enableuserfeedback);
 922  
 923  // Check if the service and support content setting is enabled or not.
 924  $servicesandsupportcontent = !isset($CFG->showservicesandsupportcontent) || $CFG->showservicesandsupportcontent;
 925  
 926  // Check whether the XML-RPC protocol is enabled or not.
 927  require_once($CFG->libdir . '/environmentlib.php');
 928  $result = new environment_results('custom_checks');
 929  $result = check_xmlrpc_usage($result);
 930  $xmlrpcwarning = !is_null($result) ? get_string($result->getFeedbackStr(), 'admin') : '';
 931  
 932  admin_externalpage_setup('adminnotifications');
 933  
 934  $output = $PAGE->get_renderer('core', 'admin');
 935  
 936  echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
 937                                         $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
 938                                         $registered, $cachewarnings, $eventshandlers, $themedesignermode, $devlibdir,
 939                                         $mobileconfigured, $overridetossl, $invalidforgottenpasswordurl, $croninfrequent,
 940                                         $showcampaigncontent, $showfeedbackencouragement, $servicesandsupportcontent,
 941                                         $xmlrpcwarning);