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.

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  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Session manager class.
  19   *
  20   * @package    core
  21   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\session;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use html_writer;
  30  
  31  /**
  32   * Session manager, this is the public Moodle API for sessions.
  33   *
  34   * Following PHP functions MUST NOT be used directly:
  35   * - session_start() - not necessary, lib/setup.php starts session automatically,
  36   *   use define('NO_MOODLE_COOKIE', true) if session not necessary.
  37   * - session_write_close() - use \core\session\manager::write_close() instead.
  38   * - session_destroy() - use require_logout() instead.
  39   *
  40   * @package    core
  41   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class manager {
  45      /** @var int A hard cutoff of maximum stored history */
  46      const MAXIMUM_STORED_SESSION_HISTORY = 50;
  47  
  48      /** @var int The recent session locks array is reset if there is a time gap more than this value in seconds */
  49      const SESSION_RESET_GAP_THRESHOLD = 1;
  50  
  51      /** @var handler $handler active session handler instance */
  52      protected static $handler;
  53  
  54      /** @var bool $sessionactive Is the session active? */
  55      protected static $sessionactive = null;
  56  
  57      /** @var string $logintokenkey Key used to get and store request protection for login form. */
  58      protected static $logintokenkey = 'core_auth_login';
  59  
  60      /** @var array Stores the the SESSION before a request is performed, used to check incorrect read-only modes */
  61      private static $priorsession = [];
  62  
  63      /**
  64       * @var bool Used to trigger the SESSION mutation warning without actually preventing SESSION mutation.
  65       * This variable is used to "copy" what the $requireslock parameter  does in start_session().
  66       * Once requireslock is set in start_session it's later accessible via $handler->requires_write_lock,
  67       * When using $CFG->enable_read_only_sessions_debug mode, this variable serves the same purpose without
  68       * actually setting the handler as requiring a write lock.
  69       */
  70      private static $requireslockdebug;
  71  
  72      /**
  73       * If the current session is not writeable, abort it, and re-open it
  74       * requesting (and blocking) until a write lock is acquired.
  75       * If current session was already opened with an intentional write lock,
  76       * this call will not do anything.
  77       * NOTE: Even when using a session handler that does not support non-locking sessions,
  78       * if the original session was not opened with the explicit intention of being locked,
  79       * this will still restart your session so that code behaviour matches as closely
  80       * as practical across environments.
  81       *
  82       * @param bool $readonlysession Used by debugging logic to determine if whatever
  83       *                              triggered the restart (e.g., a webservice) declared
  84       *                              itself as read only.
  85       */
  86      public static function restart_with_write_lock(bool $readonlysession) {
  87          global $CFG;
  88  
  89          self::$requireslockdebug = !$readonlysession;
  90  
  91          if (self::$sessionactive && !self::$handler->requires_write_lock()) {
  92              @self::$handler->abort();
  93              self::$sessionactive = false;
  94              self::start_session(true);
  95          }
  96      }
  97  
  98      /**
  99       * Start user session.
 100       *
 101       * Note: This is intended to be called only from lib/setup.php!
 102       */
 103      public static function start() {
 104          global $CFG, $DB, $PERF;
 105  
 106          if (isset(self::$sessionactive)) {
 107              debugging('Session was already started!', DEBUG_DEVELOPER);
 108              return;
 109          }
 110  
 111          // Grab the time before session lock starts.
 112          $PERF->sessionlock['start'] = microtime(true);
 113          self::load_handler();
 114  
 115          // Init the session handler only if everything initialised properly in lib/setup.php file
 116          // and the session is actually required.
 117          if (empty($DB) or empty($CFG->version) or !defined('NO_MOODLE_COOKIES') or NO_MOODLE_COOKIES or CLI_SCRIPT) {
 118              self::$sessionactive = false;
 119              self::init_empty_session();
 120              return;
 121          }
 122  
 123          if (defined('READ_ONLY_SESSION') && !empty($CFG->enable_read_only_sessions)) {
 124              $requireslock = !READ_ONLY_SESSION;
 125          } else {
 126              $requireslock = true; // For backwards compatibility, we default to assuming that a lock is needed.
 127          }
 128  
 129          // By default make the two variables the same. This means that when they are
 130          // checked below in start_session and write_close there is no possibility for
 131          // the debug version to "accidentally" execute the debug mode.
 132          self::$requireslockdebug = $requireslock;
 133          if (defined('READ_ONLY_SESSION') && !empty($CFG->enable_read_only_sessions_debug)) {
 134              // Only change the debug variable if READ_ONLY_SESSION is declared,
 135              // as would happen with the real requireslock variable.
 136              self::$requireslockdebug = !READ_ONLY_SESSION;
 137          }
 138  
 139          self::start_session($requireslock);
 140      }
 141  
 142      /**
 143       * Handles starting a session.
 144       *
 145       * @param bool $requireslock If this is false then no write lock will be acquired,
 146       *                           and the session will be read-only.
 147       */
 148      private static function start_session(bool $requireslock) {
 149          global $PERF, $CFG;
 150  
 151          try {
 152              self::$handler->init();
 153              self::$handler->set_requires_write_lock($requireslock);
 154              self::prepare_cookies();
 155              $isnewsession = empty($_COOKIE[session_name()]);
 156  
 157              if (!self::$handler->start()) {
 158                  // Could not successfully start/recover session.
 159                  throw new \core\session\exception(get_string('servererror'));
 160              }
 161  
 162              // Grab the time when session lock starts.
 163              $PERF->sessionlock['gained'] = microtime(true);
 164              $PERF->sessionlock['wait'] = $PERF->sessionlock['gained'] - $PERF->sessionlock['start'];
 165              self::initialise_user_session($isnewsession);
 166              self::$sessionactive = true; // Set here, so the session can be cleared if the security check fails.
 167              self::check_security();
 168  
 169              if (!$requireslock || !self::$requireslockdebug) {
 170                  self::$priorsession = (array) $_SESSION['SESSION'];
 171              }
 172  
 173              if (!empty($CFG->enable_read_only_sessions) && isset($_SESSION['SESSION']->cachestore_session)) {
 174                  $caches = join(', ', array_keys($_SESSION['SESSION']->cachestore_session));
 175                  $caches = str_replace('default_session-', '', $caches);
 176                  throw new \moodle_exception("The session caches can not be in the session store when "
 177                      . "enable_read_only_sessions is enabled. Please map all session mode caches to be outside of the "
 178                      . "default session store before enabling this features. Found these definitions in the session: $caches");
 179              }
 180  
 181              // Link global $USER and $SESSION,
 182              // this is tricky because PHP does not allow references to references
 183              // and global keyword uses internally once reference to the $GLOBALS array.
 184              // The solution is to use the $GLOBALS['USER'] and $GLOBALS['$SESSION']
 185              // as the main storage of data and put references to $_SESSION.
 186              $GLOBALS['USER'] = $_SESSION['USER'];
 187              $_SESSION['USER'] =& $GLOBALS['USER'];
 188              $GLOBALS['SESSION'] = $_SESSION['SESSION'];
 189              $_SESSION['SESSION'] =& $GLOBALS['SESSION'];
 190  
 191          } catch (\Exception $ex) {
 192              self::init_empty_session();
 193              self::$sessionactive = false;
 194              throw $ex;
 195          }
 196      }
 197  
 198      /**
 199       * Returns current page performance info.
 200       *
 201       * @return array perf info
 202       */
 203      public static function get_performance_info() {
 204          global $CFG, $PERF;
 205  
 206          if (!session_id()) {
 207              return array();
 208          }
 209  
 210          self::load_handler();
 211          $size = display_size(strlen(session_encode()));
 212          $handler = get_class(self::$handler);
 213  
 214          $info = array();
 215          $info['size'] = $size;
 216          $info['html'] = html_writer::div("Session ($handler): $size", "sessionsize");
 217          $info['txt'] = "Session ($handler): $size ";
 218  
 219          if (!empty($CFG->debugsessionlock)) {
 220              $sessionlock = self::get_session_lock_info();
 221              if (!empty($sessionlock['held'])) {
 222                  // The page displays the footer and the session has been closed.
 223                  $sessionlocktext = "Session lock held: ".number_format($sessionlock['held'], 3)." secs";
 224              } else {
 225                  // The session hasn't yet been closed and so we assume now with microtime.
 226                  $sessionlockheld = microtime(true) - $PERF->sessionlock['gained'];
 227                  $sessionlocktext = "Session lock open: ".number_format($sessionlockheld, 3)." secs";
 228              }
 229              $info['txt'] .= $sessionlocktext;
 230              $info['html'] .= html_writer::div($sessionlocktext, "sessionlockstart");
 231              $sessionlockwaittext = "Session lock wait: ".number_format($sessionlock['wait'], 3)." secs";
 232              $info['txt'] .= $sessionlockwaittext;
 233              $info['html'] .= html_writer::div($sessionlockwaittext, "sessionlockwait");
 234          }
 235  
 236          return $info;
 237      }
 238  
 239      /**
 240       * Get fully qualified name of session handler class.
 241       *
 242       * @return string The name of the handler class
 243       */
 244      public static function get_handler_class() {
 245          global $CFG, $DB;
 246  
 247          if (PHPUNIT_TEST) {
 248              return '\core\session\file';
 249          } else if (!empty($CFG->session_handler_class)) {
 250              return $CFG->session_handler_class;
 251          } else if (!empty($CFG->dbsessions) and $DB->session_lock_supported()) {
 252              return '\core\session\database';
 253          }
 254  
 255          return '\core\session\file';
 256      }
 257  
 258      /**
 259       * Create handler instance.
 260       */
 261      protected static function load_handler() {
 262          if (self::$handler) {
 263              return;
 264          }
 265  
 266          // Find out which handler to use.
 267          $class = self::get_handler_class();
 268          self::$handler = new $class();
 269      }
 270  
 271      /**
 272       * Empty current session, fill it with not-logged-in user info.
 273       *
 274       * This is intended for installation scripts, unit tests and other
 275       * special areas. Do NOT use for logout and session termination
 276       * in normal requests!
 277       *
 278       * @param mixed $newsid only used after initialising a user session, is this a new user session?
 279       */
 280      public static function init_empty_session(?bool $newsid = null) {
 281          global $CFG;
 282  
 283          if (isset($GLOBALS['SESSION']->notifications)) {
 284              // Backup notifications. These should be preserved across session changes until the user fetches and clears them.
 285              $notifications = $GLOBALS['SESSION']->notifications;
 286          }
 287          $GLOBALS['SESSION'] = new \stdClass();
 288          if (isset($newsid)) {
 289              $GLOBALS['SESSION']->isnewsessioncookie = $newsid;
 290          }
 291  
 292          $GLOBALS['USER'] = new \stdClass();
 293          $GLOBALS['USER']->id = 0;
 294  
 295          if (!empty($notifications)) {
 296              // Restore notifications.
 297              $GLOBALS['SESSION']->notifications = $notifications;
 298          }
 299          if (isset($CFG->mnet_localhost_id)) {
 300              $GLOBALS['USER']->mnethostid = $CFG->mnet_localhost_id;
 301          } else {
 302              // Not installed yet, the future host id will be most probably 1.
 303              $GLOBALS['USER']->mnethostid = 1;
 304          }
 305  
 306          // Link global $USER and $SESSION.
 307          $_SESSION = array();
 308          $_SESSION['USER'] =& $GLOBALS['USER'];
 309          $_SESSION['SESSION'] =& $GLOBALS['SESSION'];
 310      }
 311  
 312      /**
 313       * Make sure all cookie and session related stuff is configured properly before session start.
 314       */
 315      protected static function prepare_cookies() {
 316          global $CFG;
 317  
 318          $cookiesecure = is_moodle_cookie_secure();
 319  
 320          if (!isset($CFG->cookiehttponly)) {
 321              $CFG->cookiehttponly = 0;
 322          }
 323  
 324          // Set sessioncookie variable if it isn't already.
 325          if (!isset($CFG->sessioncookie)) {
 326              $CFG->sessioncookie = '';
 327          }
 328          $sessionname = 'MoodleSession'.$CFG->sessioncookie;
 329  
 330          // Make sure cookie domain makes sense for this wwwroot.
 331          if (!isset($CFG->sessioncookiedomain)) {
 332              $CFG->sessioncookiedomain = '';
 333          } else if ($CFG->sessioncookiedomain !== '') {
 334              $host = parse_url($CFG->wwwroot, PHP_URL_HOST);
 335              if ($CFG->sessioncookiedomain !== $host) {
 336                  if (substr($CFG->sessioncookiedomain, 0, 1) === '.') {
 337                      if (!preg_match('|^.*'.preg_quote($CFG->sessioncookiedomain, '|').'$|', $host)) {
 338                          // Invalid domain - it must be end part of host.
 339                          $CFG->sessioncookiedomain = '';
 340                      }
 341                  } else {
 342                      if (!preg_match('|^.*\.'.preg_quote($CFG->sessioncookiedomain, '|').'$|', $host)) {
 343                          // Invalid domain - it must be end part of host.
 344                          $CFG->sessioncookiedomain = '';
 345                      }
 346                  }
 347              }
 348          }
 349  
 350          // Make sure the cookiepath is valid for this wwwroot or autodetect if not specified.
 351          if (!isset($CFG->sessioncookiepath)) {
 352              $CFG->sessioncookiepath = '';
 353          }
 354          if ($CFG->sessioncookiepath !== '/') {
 355              $path = parse_url($CFG->wwwroot, PHP_URL_PATH).'/';
 356              if ($CFG->sessioncookiepath === '') {
 357                  $CFG->sessioncookiepath = $path;
 358              } else {
 359                  if (strpos($path, $CFG->sessioncookiepath) !== 0 or substr($CFG->sessioncookiepath, -1) !== '/') {
 360                      $CFG->sessioncookiepath = $path;
 361                  }
 362              }
 363          }
 364  
 365          // Discard session ID from POST, GET and globals to tighten security,
 366          // this is session fixation prevention.
 367          unset($GLOBALS[$sessionname]);
 368          unset($_GET[$sessionname]);
 369          unset($_POST[$sessionname]);
 370          unset($_REQUEST[$sessionname]);
 371  
 372          // Compatibility hack for non-browser access to our web interface.
 373          if (!empty($_COOKIE[$sessionname]) && $_COOKIE[$sessionname] == "deleted") {
 374              unset($_COOKIE[$sessionname]);
 375          }
 376  
 377          // Set configuration.
 378          session_name($sessionname);
 379  
 380          $sessionoptions = [
 381              'lifetime' => 0,
 382              'path' => $CFG->sessioncookiepath,
 383              'domain' => $CFG->sessioncookiedomain,
 384              'secure' => $cookiesecure,
 385              'httponly' => $CFG->cookiehttponly,
 386          ];
 387  
 388          if (self::should_use_samesite_none()) {
 389              // If $samesite is empty, we don't want there to be any SameSite attribute.
 390              $sessionoptions['samesite'] = 'None';
 391          }
 392  
 393          session_set_cookie_params($sessionoptions);
 394  
 395          ini_set('session.use_trans_sid', '0');
 396          ini_set('session.use_only_cookies', '1');
 397          ini_set('session.use_strict_mode', '0');      // We have custom protection in session init.
 398          ini_set('session.serialize_handler', 'php');  // We can move to 'php_serialize' after we require PHP 5.5.4 form Moodle.
 399  
 400          // Moodle does normal session timeouts, this is for leftovers only.
 401          ini_set('session.gc_probability', 1);
 402          ini_set('session.gc_divisor', 1000);
 403          ini_set('session.gc_maxlifetime', 60*60*24*4);
 404      }
 405  
 406      /**
 407       * Initialise $_SESSION, handles google access
 408       * and sets up not-logged-in user properly.
 409       *
 410       * WARNING: $USER and $SESSION are set up later, do not use them yet!
 411       *
 412       * @param bool $newsid is this a new session in first http request?
 413       */
 414      protected static function initialise_user_session($newsid) {
 415          global $CFG, $DB;
 416  
 417          $sid = session_id();
 418          if (!$sid) {
 419              // No session, very weird.
 420              error_log('Missing session ID, session not started!');
 421              self::init_empty_session($newsid);
 422              return;
 423          }
 424  
 425          if (!$record = $DB->get_record('sessions', array('sid'=>$sid), 'id, sid, state, userid, lastip, timecreated, timemodified')) {
 426              if (!$newsid) {
 427                  if (!empty($_SESSION['USER']->id)) {
 428                      // This should not happen, just log it, we MUST not produce any output here!
 429                      error_log("Cannot find session record $sid for user ".$_SESSION['USER']->id.", creating new session.");
 430                  }
 431                  // Prevent session fixation attacks.
 432                  session_regenerate_id(true);
 433              }
 434              $_SESSION = array();
 435          }
 436          unset($sid);
 437  
 438          if (isset($_SESSION['USER']->id)) {
 439              if (!empty($_SESSION['USER']->realuser)) {
 440                  $userid = $_SESSION['USER']->realuser;
 441              } else {
 442                  $userid = $_SESSION['USER']->id;
 443              }
 444  
 445              // Verify timeout first.
 446              $maxlifetime = $CFG->sessiontimeout;
 447              $timeout = false;
 448              if (isguestuser($userid) or empty($userid)) {
 449                  // Ignore guest and not-logged in timeouts, there is very little risk here.
 450                  $timeout = false;
 451  
 452              } else if ($record->timemodified < time() - $maxlifetime) {
 453                  $timeout = true;
 454                  $authsequence = get_enabled_auth_plugins(); // Auths, in sequence.
 455                  foreach ($authsequence as $authname) {
 456                      $authplugin = get_auth_plugin($authname);
 457                      if ($authplugin->ignore_timeout_hook($_SESSION['USER'], $record->sid, $record->timecreated, $record->timemodified)) {
 458                          $timeout = false;
 459                          break;
 460                      }
 461                  }
 462              }
 463  
 464              if ($timeout) {
 465                  if (defined('NO_SESSION_UPDATE') && NO_SESSION_UPDATE) {
 466                      return;
 467                  }
 468                  session_regenerate_id(true);
 469                  $_SESSION = array();
 470                  $DB->delete_records('sessions', array('id'=>$record->id));
 471  
 472              } else {
 473                  // Update session tracking record.
 474  
 475                  $update = new \stdClass();
 476                  $updated = false;
 477  
 478                  if ($record->userid != $userid) {
 479                      $update->userid = $record->userid = $userid;
 480                      $updated = true;
 481                  }
 482  
 483                  $ip = getremoteaddr();
 484                  if ($record->lastip != $ip) {
 485                      $update->lastip = $record->lastip = $ip;
 486                      $updated = true;
 487                  }
 488  
 489                  $updatefreq = empty($CFG->session_update_timemodified_frequency) ? 20 : $CFG->session_update_timemodified_frequency;
 490  
 491                  if ($record->timemodified == $record->timecreated) {
 492                      // Always do first update of existing record.
 493                      $update->timemodified = $record->timemodified = time();
 494                      $updated = true;
 495  
 496                  } else if ($record->timemodified < time() - $updatefreq) {
 497                      // Update the session modified flag only once every 20 seconds.
 498                      $update->timemodified = $record->timemodified = time();
 499                      $updated = true;
 500                  }
 501  
 502                  if ($updated && (!defined('NO_SESSION_UPDATE') || !NO_SESSION_UPDATE)) {
 503                      $update->id = $record->id;
 504                      $DB->update_record('sessions', $update);
 505                  }
 506  
 507                  return;
 508              }
 509          } else {
 510              if ($record) {
 511                  // This happens when people switch session handlers...
 512                  session_regenerate_id(true);
 513                  $_SESSION = array();
 514                  $DB->delete_records('sessions', array('id'=>$record->id));
 515              }
 516          }
 517          unset($record);
 518  
 519          $timedout = false;
 520          if (!isset($_SESSION['SESSION'])) {
 521              $_SESSION['SESSION'] = new \stdClass();
 522              if (!$newsid) {
 523                  $timedout = true;
 524              }
 525          }
 526  
 527          $user = null;
 528  
 529          if (!empty($CFG->opentowebcrawlers)) {
 530              if (\core_useragent::is_web_crawler()) {
 531                  $user = guest_user();
 532              }
 533              $referer = get_local_referer(false);
 534              if (!empty($CFG->guestloginbutton) and !$user and !empty($referer)) {
 535                  // Automatically log in users coming from search engine results.
 536                  if (strpos($referer, 'google') !== false ) {
 537                      $user = guest_user();
 538                  } else if (strpos($referer, 'altavista') !== false ) {
 539                      $user = guest_user();
 540                  }
 541              }
 542          }
 543  
 544          // Setup $USER and insert the session tracking record.
 545          if ($user) {
 546              self::set_user($user);
 547              self::add_session_record($user->id);
 548          } else {
 549              self::init_empty_session($newsid);
 550              self::add_session_record(0);
 551          }
 552  
 553          if ($timedout) {
 554              $_SESSION['SESSION']->has_timed_out = true;
 555          }
 556      }
 557  
 558      /**
 559       * Insert new empty session record.
 560       * @param int $userid
 561       * @return \stdClass the new record
 562       */
 563      protected static function add_session_record($userid) {
 564          global $DB;
 565          $record = new \stdClass();
 566          $record->state       = 0;
 567          $record->sid         = session_id();
 568          $record->sessdata    = null;
 569          $record->userid      = $userid;
 570          $record->timecreated = $record->timemodified = time();
 571          $record->firstip     = $record->lastip = getremoteaddr();
 572  
 573          $record->id = $DB->insert_record('sessions', $record);
 574  
 575          return $record;
 576      }
 577  
 578      /**
 579       * Do various session security checks.
 580       *
 581       * WARNING: $USER and $SESSION are set up later, do not use them yet!
 582       * @throws \core\session\exception
 583       */
 584      protected static function check_security() {
 585          global $CFG;
 586  
 587          if (!empty($_SESSION['USER']->id) and !empty($CFG->tracksessionip)) {
 588              // Make sure current IP matches the one for this session.
 589              $remoteaddr = getremoteaddr();
 590  
 591              if (empty($_SESSION['USER']->sessionip)) {
 592                  $_SESSION['USER']->sessionip = $remoteaddr;
 593              }
 594  
 595              if ($_SESSION['USER']->sessionip != $remoteaddr) {
 596                  // This is a security feature - terminate the session in case of any doubt.
 597                  self::terminate_current();
 598                  throw new exception('sessionipnomatch2', 'error');
 599              }
 600          }
 601      }
 602  
 603      /**
 604       * Login user, to be called from complete_user_login() only.
 605       * @param \stdClass $user
 606       */
 607      public static function login_user(\stdClass $user) {
 608          global $DB;
 609  
 610          // Regenerate session id and delete old session,
 611          // this helps prevent session fixation attacks from the same domain.
 612  
 613          $sid = session_id();
 614          session_regenerate_id(true);
 615          $DB->delete_records('sessions', array('sid'=>$sid));
 616          self::add_session_record($user->id);
 617  
 618          // Let enrol plugins deal with new enrolments if necessary.
 619          enrol_check_plugins($user);
 620  
 621          // Setup $USER object.
 622          self::set_user($user);
 623      }
 624  
 625      /**
 626       * Returns a valid setting for the SameSite cookie attribute.
 627       *
 628       * @return string The desired setting for the SameSite attribute on the cookie. Empty string indicates the SameSite attribute
 629       * should not be set at all.
 630       */
 631      private static function should_use_samesite_none(): bool {
 632          // We only want None or no attribute at this point. When we have cookie handling compatible with Lax,
 633          // we can look at checking a setting.
 634  
 635          // Browser support for none is not consistent yet. There are known issues with Safari, and IE11.
 636          // Things are stablising, however as they're not stable yet we will deal specifically with the version of chrome
 637          // that introduces a default of lax, setting it to none for the current version of chrome (2 releases before the change).
 638          // We also check you are using secure cookies and HTTPS because if you are not running over HTTPS
 639          // then setting SameSite=None will cause your session cookie to be rejected.
 640          if (\core_useragent::is_chrome() && \core_useragent::check_chrome_version('78') && is_moodle_cookie_secure()) {
 641              return true;
 642          }
 643          return false;
 644      }
 645  
 646      /**
 647       * Terminate current user session.
 648       * @return void
 649       */
 650      public static function terminate_current() {
 651          global $DB;
 652  
 653          if (!self::$sessionactive) {
 654              self::init_empty_session();
 655              self::$sessionactive = false;
 656              return;
 657          }
 658  
 659          try {
 660              $DB->delete_records('external_tokens', array('sid'=>session_id(), 'tokentype'=>EXTERNAL_TOKEN_EMBEDDED));
 661          } catch (\Exception $ignored) {
 662              // Probably install/upgrade - ignore this problem.
 663          }
 664  
 665          // Initialize variable to pass-by-reference to headers_sent(&$file, &$line).
 666          $file = null;
 667          $line = null;
 668          if (headers_sent($file, $line)) {
 669              error_log('Cannot terminate session properly - headers were already sent in file: '.$file.' on line '.$line);
 670          }
 671  
 672          // Write new empty session and make sure the old one is deleted.
 673          $sid = session_id();
 674          session_regenerate_id(true);
 675          $DB->delete_records('sessions', array('sid'=>$sid));
 676          self::init_empty_session();
 677          self::add_session_record($_SESSION['USER']->id); // Do not use $USER here because it may not be set up yet.
 678          self::write_close();
 679      }
 680  
 681      /**
 682       * No more changes in session expected.
 683       * Unblocks the sessions, other scripts may start executing in parallel.
 684       */
 685      public static function write_close() {
 686          global $PERF, $ME, $CFG;
 687  
 688          if (self::$sessionactive) {
 689              // Grab the time when session lock is released.
 690              $PERF->sessionlock['released'] = microtime(true);
 691              if (!empty($PERF->sessionlock['gained'])) {
 692                  $PERF->sessionlock['held'] = $PERF->sessionlock['released'] - $PERF->sessionlock['gained'];
 693              }
 694              $PERF->sessionlock['url'] = me();
 695              self::update_recent_session_locks($PERF->sessionlock);
 696              self::sessionlock_debugging();
 697  
 698              $requireslock = self::$handler->requires_write_lock();
 699              if (!$requireslock || !self::$requireslockdebug) {
 700                  // Compare the array of the earlier session data with the array now, if
 701                  // there is a difference then a lock is required.
 702                  $arraydiff = self::array_session_diff(
 703                      self::$priorsession,
 704                      (array) $_SESSION['SESSION']
 705                  );
 706  
 707                  if ($arraydiff) {
 708                      $error = "Script $ME defined READ_ONLY_SESSION but the following SESSION attributes were changed:";
 709                      foreach ($arraydiff as $key => $value) {
 710                          $error .= ' $SESSION->' . $key;
 711                      }
 712                      // This will emit an error if debugging is on, even if $CFG->enable_read_only_sessions
 713                      // is not true as we need to surface this class of errors.
 714                      error_log($error); // phpcs:ignore
 715                  }
 716              }
 717          }
 718  
 719          // More control over whether session data
 720          // is persisted or not.
 721          if (self::$sessionactive && session_id()) {
 722              // Write session and release lock only if
 723              // indication session start was clean.
 724              self::$handler->write_close();
 725          } else {
 726              // Otherwise, if possible lock exists want
 727              // to clear it, but do not write session.
 728              // If the $handler has not been set then
 729              // there is no session to abort.
 730              if (isset(self::$handler)) {
 731                  @self::$handler->abort();
 732              }
 733          }
 734  
 735          self::$sessionactive = false;
 736      }
 737  
 738      /**
 739       * Does the PHP session with given id exist?
 740       *
 741       * The session must exist both in session table and actual
 742       * session backend and the session must not be timed out.
 743       *
 744       * Timeout evaluation is simplified, the auth hooks are not executed.
 745       *
 746       * @param string $sid
 747       * @return bool
 748       */
 749      public static function session_exists($sid) {
 750          global $DB, $CFG;
 751  
 752          if (empty($CFG->version)) {
 753              // Not installed yet, do not try to access database.
 754              return false;
 755          }
 756  
 757          // Note: add sessions->state checking here if it gets implemented.
 758          if (!$record = $DB->get_record('sessions', array('sid' => $sid), 'id, userid, timemodified')) {
 759              return false;
 760          }
 761  
 762          if (empty($record->userid) or isguestuser($record->userid)) {
 763              // Ignore guest and not-logged-in timeouts, there is very little risk here.
 764          } else if ($record->timemodified < time() - $CFG->sessiontimeout) {
 765              return false;
 766          }
 767  
 768          // There is no need the existence of handler storage in public API.
 769          self::load_handler();
 770          return self::$handler->session_exists($sid);
 771      }
 772  
 773      /**
 774       * Return the number of seconds remaining in the current session.
 775       * @param string $sid
 776       */
 777      public static function time_remaining($sid) {
 778          global $DB, $CFG;
 779  
 780          if (empty($CFG->version)) {
 781              // Not installed yet, do not try to access database.
 782              return ['userid' => 0, 'timeremaining' => $CFG->sessiontimeout];
 783          }
 784  
 785          // Note: add sessions->state checking here if it gets implemented.
 786          if (!$record = $DB->get_record('sessions', array('sid' => $sid), 'id, userid, timemodified')) {
 787              return ['userid' => 0, 'timeremaining' => $CFG->sessiontimeout];
 788          }
 789  
 790          if (empty($record->userid) or isguestuser($record->userid)) {
 791              // Ignore guest and not-logged-in timeouts, there is very little risk here.
 792              return ['userid' => 0, 'timeremaining' => $CFG->sessiontimeout];
 793          } else {
 794              return ['userid' => $record->userid, 'timeremaining' => $CFG->sessiontimeout - (time() - $record->timemodified)];
 795          }
 796      }
 797  
 798      /**
 799       * Fake last access for given session, this prevents session timeout.
 800       * @param string $sid
 801       */
 802      public static function touch_session($sid) {
 803          global $DB;
 804  
 805          // Timeouts depend on core sessions table only, no need to update anything in external stores.
 806  
 807          $sql = "UPDATE {sessions} SET timemodified = :now WHERE sid = :sid";
 808          $DB->execute($sql, array('now'=>time(), 'sid'=>$sid));
 809      }
 810  
 811      /**
 812       * Terminate all sessions unconditionally.
 813       */
 814      public static function kill_all_sessions() {
 815          global $DB;
 816  
 817          self::terminate_current();
 818  
 819          self::load_handler();
 820          self::$handler->kill_all_sessions();
 821  
 822          try {
 823              $DB->delete_records('sessions');
 824          } catch (\dml_exception $ignored) {
 825              // Do not show any warnings - might be during upgrade/installation.
 826          }
 827      }
 828  
 829      /**
 830       * Terminate give session unconditionally.
 831       * @param string $sid
 832       */
 833      public static function kill_session($sid) {
 834          global $DB;
 835  
 836          self::load_handler();
 837  
 838          if ($sid === session_id()) {
 839              self::write_close();
 840          }
 841  
 842          self::$handler->kill_session($sid);
 843  
 844          $DB->delete_records('sessions', array('sid'=>$sid));
 845      }
 846  
 847      /**
 848       * Terminate all sessions of given user unconditionally.
 849       * @param int $userid
 850       * @param string $keepsid keep this sid if present
 851       */
 852      public static function kill_user_sessions($userid, $keepsid = null) {
 853          global $DB;
 854  
 855          $sessions = $DB->get_records('sessions', array('userid'=>$userid), 'id DESC', 'id, sid');
 856          foreach ($sessions as $session) {
 857              if ($keepsid and $keepsid === $session->sid) {
 858                  continue;
 859              }
 860              self::kill_session($session->sid);
 861          }
 862      }
 863  
 864      /**
 865       * Terminate other sessions of current user depending
 866       * on $CFG->limitconcurrentlogins restriction.
 867       *
 868       * This is expected to be called right after complete_user_login().
 869       *
 870       * NOTE:
 871       *  * Do not use from SSO auth plugins, this would not work.
 872       *  * Do not use from web services because they do not have sessions.
 873       *
 874       * @param int $userid
 875       * @param string $sid session id to be always keep, usually the current one
 876       * @return void
 877       */
 878      public static function apply_concurrent_login_limit($userid, $sid = null) {
 879          global $CFG, $DB;
 880  
 881          // NOTE: the $sid parameter is here mainly to allow testing,
 882          //       in most cases it should be current session id.
 883  
 884          if (isguestuser($userid) or empty($userid)) {
 885              // This applies to real users only!
 886              return;
 887          }
 888  
 889          if (empty($CFG->limitconcurrentlogins) or $CFG->limitconcurrentlogins < 0) {
 890              return;
 891          }
 892  
 893          $count = $DB->count_records('sessions', array('userid' => $userid));
 894  
 895          if ($count <= $CFG->limitconcurrentlogins) {
 896              return;
 897          }
 898  
 899          $i = 0;
 900          $select = "userid = :userid";
 901          $params = array('userid' => $userid);
 902          if ($sid) {
 903              if ($DB->record_exists('sessions', array('sid' => $sid, 'userid' => $userid))) {
 904                  $select .= " AND sid <> :sid";
 905                  $params['sid'] = $sid;
 906                  $i = 1;
 907              }
 908          }
 909  
 910          $sessions = $DB->get_records_select('sessions', $select, $params, 'timecreated DESC', 'id, sid');
 911          foreach ($sessions as $session) {
 912              $i++;
 913              if ($i <= $CFG->limitconcurrentlogins) {
 914                  continue;
 915              }
 916              self::kill_session($session->sid);
 917          }
 918      }
 919  
 920      /**
 921       * Set current user.
 922       *
 923       * @param \stdClass $user record
 924       */
 925      public static function set_user(\stdClass $user) {
 926          global $ADMIN;
 927          $GLOBALS['USER'] = $user;
 928          unset($GLOBALS['USER']->description); // Conserve memory.
 929          unset($GLOBALS['USER']->password);    // Improve security.
 930          if (isset($GLOBALS['USER']->lang)) {
 931              // Make sure it is a valid lang pack name.
 932              $GLOBALS['USER']->lang = clean_param($GLOBALS['USER']->lang, PARAM_LANG);
 933          }
 934  
 935          // Relink session with global $USER just in case it got unlinked somehow.
 936          $_SESSION['USER'] =& $GLOBALS['USER'];
 937  
 938          // Nullify the $ADMIN tree global. If we're changing users, then this is now stale and must be generated again if needed.
 939          $ADMIN = null;
 940  
 941          // Init session key.
 942          sesskey();
 943  
 944          // Make sure the user is correct in web server access logs.
 945          set_access_log_user();
 946      }
 947  
 948      /**
 949       * Periodic timed-out session cleanup.
 950       */
 951      public static function gc() {
 952          global $CFG, $DB;
 953  
 954          // This may take a long time...
 955          \core_php_time_limit::raise();
 956  
 957          $maxlifetime = $CFG->sessiontimeout;
 958  
 959          try {
 960              // Kill all sessions of deleted and suspended users without any hesitation.
 961              $rs = $DB->get_recordset_select('sessions', "userid IN (SELECT id FROM {user} WHERE deleted <> 0 OR suspended <> 0)", array(), 'id DESC', 'id, sid');
 962              foreach ($rs as $session) {
 963                  self::kill_session($session->sid);
 964              }
 965              $rs->close();
 966  
 967              // Kill sessions of users with disabled plugins.
 968              $authsequence = get_enabled_auth_plugins();
 969              $authsequence = array_flip($authsequence);
 970              unset($authsequence['nologin']); // No login means user cannot login.
 971              $authsequence = array_flip($authsequence);
 972  
 973              list($notplugins, $params) = $DB->get_in_or_equal($authsequence, SQL_PARAMS_QM, '', false);
 974              $rs = $DB->get_recordset_select('sessions', "userid IN (SELECT id FROM {user} WHERE auth $notplugins)", $params, 'id DESC', 'id, sid');
 975              foreach ($rs as $session) {
 976                  self::kill_session($session->sid);
 977              }
 978              $rs->close();
 979  
 980              // Now get a list of time-out candidates - real users only.
 981              $sql = "SELECT u.*, s.sid, s.timecreated AS s_timecreated, s.timemodified AS s_timemodified
 982                        FROM {user} u
 983                        JOIN {sessions} s ON s.userid = u.id
 984                       WHERE s.timemodified < :purgebefore AND u.id <> :guestid";
 985              $params = array('purgebefore' => (time() - $maxlifetime), 'guestid'=>$CFG->siteguest);
 986  
 987              $authplugins = array();
 988              foreach ($authsequence as $authname) {
 989                  $authplugins[$authname] = get_auth_plugin($authname);
 990              }
 991              $rs = $DB->get_recordset_sql($sql, $params);
 992              foreach ($rs as $user) {
 993                  foreach ($authplugins as $authplugin) {
 994                      /** @var \auth_plugin_base $authplugin*/
 995                      if ($authplugin->ignore_timeout_hook($user, $user->sid, $user->s_timecreated, $user->s_timemodified)) {
 996                          continue 2;
 997                      }
 998                  }
 999                  self::kill_session($user->sid);
1000              }
1001              $rs->close();
1002  
1003              // Delete expired sessions for guest user account, give them larger timeout, there is no security risk here.
1004              $params = array('purgebefore' => (time() - ($maxlifetime * 5)), 'guestid'=>$CFG->siteguest);
1005              $rs = $DB->get_recordset_select('sessions', 'userid = :guestid AND timemodified < :purgebefore', $params, 'id DESC', 'id, sid');
1006              foreach ($rs as $session) {
1007                  self::kill_session($session->sid);
1008              }
1009              $rs->close();
1010  
1011              // Delete expired sessions for userid = 0 (not logged in), better kill them asap to release memory.
1012              $params = array('purgebefore' => (time() - $maxlifetime));
1013              $rs = $DB->get_recordset_select('sessions', 'userid = 0 AND timemodified < :purgebefore', $params, 'id DESC', 'id, sid');
1014              foreach ($rs as $session) {
1015                  self::kill_session($session->sid);
1016              }
1017              $rs->close();
1018  
1019              // Cleanup letfovers from the first browser access because it may set multiple cookies and then use only one.
1020              $params = array('purgebefore' => (time() - 60*3));
1021              $rs = $DB->get_recordset_select('sessions', 'userid = 0 AND timemodified = timecreated AND timemodified < :purgebefore', $params, 'id ASC', 'id, sid');
1022              foreach ($rs as $session) {
1023                  self::kill_session($session->sid);
1024              }
1025              $rs->close();
1026  
1027          } catch (\Exception $ex) {
1028              debugging('Error gc-ing sessions: '.$ex->getMessage(), DEBUG_NORMAL, $ex->getTrace());
1029          }
1030      }
1031  
1032      /**
1033       * Is current $USER logged-in-as somebody else?
1034       * @return bool
1035       */
1036      public static function is_loggedinas() {
1037          return !empty($GLOBALS['USER']->realuser);
1038      }
1039  
1040      /**
1041       * Returns the $USER object ignoring current login-as session
1042       * @return \stdClass user object
1043       */
1044      public static function get_realuser() {
1045          if (self::is_loggedinas()) {
1046              return $_SESSION['REALUSER'];
1047          } else {
1048              return $GLOBALS['USER'];
1049          }
1050      }
1051  
1052      /**
1053       * Login as another user - no security checks here.
1054       * @param int $userid
1055       * @param \context $context
1056       * @param bool $generateevent Set to false to prevent the loginas event to be generated
1057       * @return void
1058       */
1059      public static function loginas($userid, \context $context, $generateevent = true) {
1060          global $USER;
1061  
1062          if (self::is_loggedinas()) {
1063              return;
1064          }
1065  
1066          // Switch to fresh new $_SESSION.
1067          $_SESSION = array();
1068          $_SESSION['REALSESSION'] = clone($GLOBALS['SESSION']);
1069          $GLOBALS['SESSION'] = new \stdClass();
1070          $_SESSION['SESSION'] =& $GLOBALS['SESSION'];
1071  
1072          // Create the new $USER object with all details and reload needed capabilities.
1073          $_SESSION['REALUSER'] = clone($GLOBALS['USER']);
1074          $user = get_complete_user_data('id', $userid);
1075          $user->realuser       = $_SESSION['REALUSER']->id;
1076          $user->loginascontext = $context;
1077  
1078          // Let enrol plugins deal with new enrolments if necessary.
1079          enrol_check_plugins($user);
1080  
1081          if ($generateevent) {
1082              // Create event before $USER is updated.
1083              $event = \core\event\user_loggedinas::create(
1084                  array(
1085                      'objectid' => $USER->id,
1086                      'context' => $context,
1087                      'relateduserid' => $userid,
1088                      'other' => array(
1089                          'originalusername' => fullname($USER, true),
1090                          'loggedinasusername' => fullname($user, true)
1091                      )
1092                  )
1093              );
1094          }
1095  
1096          // Set up global $USER.
1097          \core\session\manager::set_user($user);
1098  
1099          if ($generateevent) {
1100              $event->trigger();
1101          }
1102  
1103          // Queue migrating the messaging data, if we need to.
1104          if (!get_user_preferences('core_message_migrate_data', false, $userid)) {
1105              // Check if there are any legacy messages to migrate.
1106              if (\core_message\helper::legacy_messages_exist($userid)) {
1107                  \core_message\task\migrate_message_data::queue_task($userid);
1108              } else {
1109                  set_user_preference('core_message_migrate_data', true, $userid);
1110              }
1111          }
1112      }
1113  
1114      /**
1115       * Add a JS session keepalive to the page.
1116       *
1117       * A JS session keepalive script will be called to update the session modification time every $frequency seconds.
1118       *
1119       * Upon failure, the specified error message will be shown to the user.
1120       *
1121       * @param string $identifier The string identifier for the message to show on failure.
1122       * @param string $component The string component for the message to show on failure.
1123       * @param int $frequency The update frequency in seconds.
1124       * @param int $timeout The timeout of each request in seconds.
1125       * @throws coding_exception IF the frequency is longer than the session lifetime.
1126       */
1127      public static function keepalive($identifier = 'sessionerroruser', $component = 'error', $frequency = null, $timeout = 0) {
1128          global $CFG, $PAGE;
1129  
1130          if ($frequency) {
1131              if ($frequency > $CFG->sessiontimeout) {
1132                  // Sanity check the frequency.
1133                  throw new \coding_exception('Keepalive frequency is longer than the session lifespan.');
1134              }
1135          } else {
1136              // A frequency of sessiontimeout / 10 matches the timeouts in core/network amd module.
1137              $frequency = $CFG->sessiontimeout / 10;
1138          }
1139  
1140          $PAGE->requires->js_call_amd('core/network', 'keepalive', array(
1141                  $frequency,
1142                  $timeout,
1143                  $identifier,
1144                  $component
1145              ));
1146      }
1147  
1148      /**
1149       * Generate a new login token and store it in the session.
1150       *
1151       * @return array The current login state.
1152       */
1153      private static function create_login_token() {
1154          global $SESSION;
1155  
1156          $state = [
1157              'token' => random_string(32),
1158              'created' => time() // Server time - not user time.
1159          ];
1160  
1161          if (!isset($SESSION->logintoken)) {
1162              $SESSION->logintoken = [];
1163          }
1164  
1165          // Overwrite any previous values.
1166          $SESSION->logintoken[self::$logintokenkey] = $state;
1167  
1168          return $state;
1169      }
1170  
1171      /**
1172       * Get the current login token or generate a new one.
1173       *
1174       * All login forms generated from Moodle must include a login token
1175       * named "logintoken" with the value being the result of this function.
1176       * Logins will be rejected if they do not include this token as well as
1177       * the username and password fields.
1178       *
1179       * @return string The current login token.
1180       */
1181      public static function get_login_token() {
1182          global $CFG, $SESSION;
1183  
1184          $state = false;
1185  
1186          if (!isset($SESSION->logintoken)) {
1187              $SESSION->logintoken = [];
1188          }
1189  
1190          if (array_key_exists(self::$logintokenkey, $SESSION->logintoken)) {
1191              $state = $SESSION->logintoken[self::$logintokenkey];
1192          }
1193          if (empty($state)) {
1194              $state = self::create_login_token();
1195          }
1196  
1197          // Check token lifespan.
1198          if ($state['created'] < (time() - $CFG->sessiontimeout)) {
1199              $state = self::create_login_token();
1200          }
1201  
1202          // Return the current session login token.
1203          if (array_key_exists('token', $state)) {
1204              return $state['token'];
1205          } else {
1206              return false;
1207          }
1208      }
1209  
1210      /**
1211       * Check the submitted value against the stored login token.
1212       *
1213       * @param mixed $token The value submitted in the login form that we are validating.
1214       *                     If false is passed for the token, this function will always return true.
1215       * @return boolean If the submitted token is valid.
1216       */
1217      public static function validate_login_token($token = false) {
1218          global $CFG;
1219  
1220          if (!empty($CFG->alternateloginurl) || !empty($CFG->disablelogintoken)) {
1221              // An external login page cannot generate the login token we need to protect CSRF on
1222              // login requests.
1223              // Other custom login workflows may skip this check by setting disablelogintoken in config.
1224              return true;
1225          }
1226          if ($token === false) {
1227              // authenticate_user_login is a core function was extended to validate tokens.
1228              // For existing uses other than the login form it does not
1229              // validate that a token was generated.
1230              // Some uses that do not validate the token are login/token.php,
1231              // or an auth plugin like auth/ldap/auth.php.
1232              return true;
1233          }
1234  
1235          $currenttoken = self::get_login_token();
1236  
1237          // We need to clean the login token so the old one is not valid again.
1238          self::create_login_token();
1239  
1240          if ($currenttoken !== $token) {
1241              // Fail the login.
1242              return false;
1243          }
1244          return true;
1245      }
1246  
1247      /**
1248       * Get the recent session locks array.
1249       *
1250       * @return array Recent session locks array.
1251       */
1252      public static function get_recent_session_locks() {
1253          global $SESSION;
1254  
1255          if (!isset($SESSION->recentsessionlocks)) {
1256              // This will hold the pages that blocks other page.
1257              $SESSION->recentsessionlocks = array();
1258          }
1259  
1260          return $SESSION->recentsessionlocks;
1261      }
1262  
1263      /**
1264       * Updates the recent session locks.
1265       *
1266       * This function will store session lock info of all the pages visited.
1267       *
1268       * @param array $sessionlock Session lock array.
1269       */
1270      public static function update_recent_session_locks($sessionlock) {
1271          global $CFG, $SESSION;
1272  
1273          if (empty($CFG->debugsessionlock)) {
1274              return;
1275          }
1276  
1277          $readonlysession = defined('READ_ONLY_SESSION') && READ_ONLY_SESSION;
1278          $readonlydebugging = !empty($CFG->enable_read_only_sessions) || !empty($CFG->enable_read_only_sessions_debug);
1279          if ($readonlysession && $readonlydebugging) {
1280              return;
1281          }
1282  
1283          $SESSION->recentsessionlocks = self::get_recent_session_locks();
1284          array_push($SESSION->recentsessionlocks, $sessionlock);
1285  
1286          self::cleanup_recent_session_locks();
1287      }
1288  
1289      /**
1290       * Reset recent session locks array if there is a time gap more than SESSION_RESET_GAP_THRESHOLD.
1291       */
1292      public static function cleanup_recent_session_locks() {
1293          global $SESSION;
1294  
1295          $locks = self::get_recent_session_locks();
1296  
1297          if (count($locks) > self::MAXIMUM_STORED_SESSION_HISTORY) {
1298              // Keep the last MAXIMUM_STORED_SESSION_HISTORY locks and ignore the rest.
1299              $locks = array_slice($locks, -1 * self::MAXIMUM_STORED_SESSION_HISTORY);
1300          }
1301  
1302          if (count($locks) > 2) {
1303              for ($i = count($locks) - 1; $i > 0; $i--) {
1304                  // Calculate the gap between session locks.
1305                  $gap = $locks[$i]['released'] - $locks[$i - 1]['start'];
1306                  if ($gap >= self::SESSION_RESET_GAP_THRESHOLD) {
1307                      // Remove previous locks if the gap is 1 second or more.
1308                      $SESSION->recentsessionlocks = array_slice($locks, $i);
1309                      break;
1310                  }
1311              }
1312          }
1313      }
1314  
1315      /**
1316       * Get the page that blocks other pages at a specific timestamp.
1317       *
1318       * Look for a page whose lock was gained before that timestamp, and released after that timestamp.
1319       *
1320       * @param  float $time Time before session lock starts.
1321       * @return array|null
1322       */
1323      public static function get_locked_page_at($time) {
1324          $recentsessionlocks = self::get_recent_session_locks();
1325          foreach ($recentsessionlocks as $recentsessionlock) {
1326              if ($time >= $recentsessionlock['gained'] &&
1327                  $time <= $recentsessionlock['released']) {
1328                  return $recentsessionlock;
1329              }
1330          }
1331      }
1332  
1333      /**
1334       * Display the page which blocks other pages.
1335       *
1336       * @return string
1337       */
1338      public static function display_blocking_page() {
1339          global $PERF;
1340  
1341          $page = self::get_locked_page_at($PERF->sessionlock['start']);
1342          $output = "Script ".me()." was blocked for ";
1343          $output .= number_format($PERF->sessionlock['wait'], 3);
1344          if ($page != null) {
1345              $output .= " second(s) by script: ";
1346              $output .= $page['url'];
1347          } else {
1348              $output .= " second(s) by an unknown script.";
1349          }
1350  
1351          return $output;
1352      }
1353  
1354      /**
1355       * Get session lock info of the current page.
1356       *
1357       * @return array
1358       */
1359      public static function get_session_lock_info() {
1360          global $PERF;
1361  
1362          if (!isset($PERF->sessionlock)) {
1363              return null;
1364          }
1365          return $PERF->sessionlock;
1366      }
1367  
1368      /**
1369       * Display debugging info about slow and blocked script.
1370       */
1371      public static function sessionlock_debugging() {
1372          global $CFG, $PERF;
1373  
1374          if (!empty($CFG->debugsessionlock)) {
1375              if (isset($PERF->sessionlock['held']) && $PERF->sessionlock['held'] > $CFG->debugsessionlock) {
1376                  debugging("Script ".me()." locked the session for ".number_format($PERF->sessionlock['held'], 3)
1377                  ." seconds, it should close the session using \core\session\manager::write_close().", DEBUG_NORMAL);
1378              }
1379  
1380              if (isset($PERF->sessionlock['wait']) && $PERF->sessionlock['wait'] > $CFG->debugsessionlock) {
1381                  $output = self::display_blocking_page();
1382                  debugging($output, DEBUG_DEVELOPER);
1383              }
1384          }
1385      }
1386  
1387      /**
1388       * Compares two arrays outputs the difference.
1389       *
1390       * Note this does not use array_diff_assoc due to
1391       * the use of stdClasses in Moodle sessions.
1392       *
1393       * @param array $array1
1394       * @param array $array2
1395       * @return array
1396       */
1397      private static function array_session_diff(array $array1, array $array2) : array {
1398          $difference = [];
1399          foreach ($array1 as $key => $value) {
1400              if (!isset($array2[$key])) {
1401                  $difference[$key] = $value;
1402              } else if ($array2[$key] !== $value) {
1403                  $difference[$key] = $value;
1404              }
1405          }
1406  
1407          return $difference;
1408      }
1409  }