[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/ -> sessionlib.php (source)

   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   * @package    core
  19   * @subpackage session
  20   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  21   * @copyright  2008, 2009 Petr Skoda  {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  
  28  /**
  29   * Makes sure that $USER->sesskey exists, if $USER itself exists. It sets a new sesskey
  30   * if one does not already exist, but does not overwrite existing sesskeys. Returns the
  31   * sesskey string if $USER exists, or boolean false if not.
  32   *
  33   * @uses $USER
  34   * @return string
  35   */
  36  function sesskey() {
  37      // note: do not use $USER because it may not be initialised yet
  38      if (empty($_SESSION['USER']->sesskey)) {
  39          if (!isset($_SESSION['USER'])) {
  40              // This should never happen,
  41              // do not mess with session and globals here,
  42              // let any checks fail instead!
  43              return false;
  44          }
  45          $_SESSION['USER']->sesskey = random_string(10);
  46      }
  47  
  48      return $_SESSION['USER']->sesskey;
  49  }
  50  
  51  
  52  /**
  53   * Check the sesskey and return true of false for whether it is valid.
  54   * (You might like to imagine this function is called sesskey_is_valid().)
  55   *
  56   * Every script that lets the user perform a significant action (that is,
  57   * changes data in the database) should check the sesskey before doing the action.
  58   * Depending on your code flow, you may want to use the {@link require_sesskey()}
  59   * helper function.
  60   *
  61   * @param string $sesskey The sesskey value to check (optional). Normally leave this blank
  62   *      and this function will do required_param('sesskey', ...).
  63   * @return bool whether the sesskey sent in the request matches the one stored in the session.
  64   */
  65  function confirm_sesskey($sesskey=NULL) {
  66      global $USER;
  67  
  68      if (!empty($USER->ignoresesskey)) {
  69          return true;
  70      }
  71  
  72      if (empty($sesskey)) {
  73          $sesskey = required_param('sesskey', PARAM_RAW);  // Check script parameters
  74      }
  75  
  76      return (sesskey() === $sesskey);
  77  }
  78  
  79  /**
  80   * Check the session key using {@link confirm_sesskey()},
  81   * and cause a fatal error if it does not match.
  82   */
  83  function require_sesskey() {
  84      if (!confirm_sesskey()) {
  85          print_error('invalidsesskey');
  86      }
  87  }
  88  
  89  /**
  90   * Sets a moodle cookie with a weakly encrypted username
  91   *
  92   * @param string $username to encrypt and place in a cookie, '' means delete current cookie
  93   * @return void
  94   */
  95  function set_moodle_cookie($username) {
  96      global $CFG;
  97  
  98      if (NO_MOODLE_COOKIES) {
  99          return;
 100      }
 101  
 102      if (empty($CFG->rememberusername)) {
 103          // erase current and do not store permanent cookies
 104          $username = '';
 105      }
 106  
 107      if ($username === 'guest') {
 108          // keep previous cookie in case of guest account login
 109          return;
 110      }
 111  
 112      $cookiename = 'MOODLEID1_'.$CFG->sessioncookie;
 113  
 114      // delete old cookie
 115      setcookie($cookiename, '', time() - HOURSECS, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure, $CFG->cookiehttponly);
 116  
 117      if ($username !== '') {
 118          // set username cookie for 60 days
 119          setcookie($cookiename, rc4encrypt($username), time()+(DAYSECS*60), $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure, $CFG->cookiehttponly);
 120      }
 121  }
 122  
 123  /**
 124   * Gets a moodle cookie with a weakly encrypted username
 125   *
 126   * @return string username
 127   */
 128  function get_moodle_cookie() {
 129      global $CFG;
 130  
 131      if (NO_MOODLE_COOKIES) {
 132          return '';
 133      }
 134  
 135      if (empty($CFG->rememberusername)) {
 136          return '';
 137      }
 138  
 139      $cookiename = 'MOODLEID1_'.$CFG->sessioncookie;
 140  
 141      if (empty($_COOKIE[$cookiename])) {
 142          return '';
 143      } else {
 144          $username = rc4decrypt($_COOKIE[$cookiename]);
 145          if ($username === 'guest' or $username === 'nobody') {
 146              // backwards compatibility - we do not set these cookies any more
 147              $username = '';
 148          }
 149          return $username;
 150      }
 151  }
 152  
 153  /**
 154   * Sets up current user and course environment (lang, etc.) in cron.
 155   * Do not use outside of cron script!
 156   *
 157   * @param stdClass $user full user object, null means default cron user (admin),
 158   *                 value 'reset' means reset internal static caches.
 159   * @param stdClass $course full course record, null means $SITE
 160   * @return void
 161   */
 162  function cron_setup_user($user = NULL, $course = NULL) {
 163      global $CFG, $SITE, $PAGE;
 164  
 165      if (!CLI_SCRIPT) {
 166          throw new coding_exception('Function cron_setup_user() cannot be used in normal requests!');
 167      }
 168  
 169      static $cronuser    = NULL;
 170      static $cronsession = NULL;
 171  
 172      if ($user === 'reset') {
 173          $cronuser = null;
 174          $cronsession = null;
 175          \core\session\manager::init_empty_session();
 176          return;
 177      }
 178  
 179      if (empty($cronuser)) {
 180          /// ignore admins timezone, language and locale - use site default instead!
 181          $cronuser = get_admin();
 182          $cronuser->timezone = $CFG->timezone;
 183          $cronuser->lang     = '';
 184          $cronuser->theme    = '';
 185          unset($cronuser->description);
 186  
 187          $cronsession = new stdClass();
 188      }
 189  
 190      if (!$user) {
 191          // Cached default cron user (==modified admin for now).
 192          \core\session\manager::init_empty_session();
 193          \core\session\manager::set_user($cronuser);
 194          $GLOBALS['SESSION'] = $cronsession;
 195  
 196      } else {
 197          // Emulate real user session - needed for caps in cron.
 198          if ($GLOBALS['USER']->id != $user->id) {
 199              \core\session\manager::init_empty_session();
 200              \core\session\manager::set_user($user);
 201          }
 202      }
 203  
 204      // TODO MDL-19774 relying on global $PAGE in cron is a bad idea.
 205      // Temporary hack so that cron does not give fatal errors.
 206      $PAGE = new moodle_page();
 207      if ($course) {
 208          $PAGE->set_course($course);
 209      } else {
 210          $PAGE->set_course($SITE);
 211      }
 212  
 213      // TODO: it should be possible to improve perf by caching some limited number of users here ;-)
 214  
 215  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1