original_string: public static function get_autologin_key($privatetoken) {
          global $CFG, $DB, $USER;
  
          $params = self::validate_parameters(self::get_autologin_key_parameters(), array('privatetoken' => $privatetoken));
          $privatetoken = $params['privatetoken'];
  
          $context = context_system::instance();
  
          // We must toletare these two exceptions: forcepasswordchangenotice and usernotfullysetup.
          try {
              self::validate_context($context);
          } catch (moodle_exception $e) {
              if ($e->errorcode != 'usernotfullysetup' && $e->errorcode != 'forcepasswordchangenotice') {
                  // In case we receive a different exception, throw it.
                  throw $e;
              }
          }
  
          // Only requests from the Moodle mobile or desktop app. This enhances security to avoid any type of XSS attack.
          // This code goes intentionally here and not inside the check_autologin_prerequisites() function because it
          // is used by other PHP scripts that can be opened in any browser.
          if (!\core_useragent::is_moodle_app()) {
              throw new moodle_exception('apprequired', 'tool_mobile');
          }
          api::check_autologin_prerequisites($USER->id);
  
          if (isset($_GET['privatetoken']) or empty($privatetoken)) {
              throw new moodle_exception('invalidprivatetoken', 'tool_mobile');
          }
  
          // Check the request counter, we must limit the number of times the privatetoken is sent.
          // Between each request 6 minutes are required.
          $last = get_user_preferences('tool_mobile_autologin_request_last', 0, $USER);
          // Check if we must reset the count.
          $timenow = time();
          if ($timenow - $last < 6 * MINSECS) {
              throw new moodle_exception('autologinkeygenerationlockout', 'tool_mobile');
          }
          set_user_preference('tool_mobile_autologin_request_last', $timenow, $USER);
  
          // We are expecting a privatetoken linked to the current token being used.
          // This WS is only valid when using mobile services via REST (this is intended).
          $currenttoken = required_param('wstoken', PARAM_ALPHANUM);
          $conditions = array(
              'userid' => $USER->id,
              'token' => $currenttoken,
              'privatetoken' => $privatetoken,
          );
          if (!$token = $DB->get_record('external_tokens', $conditions)) {
              throw new moodle_exception('invalidprivatetoken', 'tool_mobile');
          }
  
          $result = array();
          $result['key'] = api::get_autologin_key();
          $autologinurl = new moodle_url("/$CFG->admin/tool/mobile/autologin.php");
          $result['autologinurl'] = $autologinurl->out(false);
          $result['warnings'] = array();
          return $result;
      }
code: public static function get_autologin_key($privatetoken) {
          global $CFG, $DB, $USER;
  
          $params = self::validate_parameters(self::get_autologin_key_parameters(), array('privatetoken' => $privatetoken));
          $privatetoken = $params['privatetoken'];
  
          $context = context_system::instance();
  
          // We must toletare these two exceptions: forcepasswordchangenotice and usernotfullysetup.
          try {
              self::validate_context($context);
          } catch (moodle_exception $e) {
              if ($e->errorcode != 'usernotfullysetup' && $e->errorcode != 'forcepasswordchangenotice') {
                  // In case we receive a different exception, throw it.
                  throw $e;
              }
          }
  
          // Only requests from the Moodle mobile or desktop app. This enhances security to avoid any type of XSS attack.
          // This code goes intentionally here and not inside the check_autologin_prerequisites() function because it
          // is used by other PHP scripts that can be opened in any browser.
          if (!\core_useragent::is_moodle_app()) {
              throw new moodle_exception('apprequired', 'tool_mobile');
          }
          api::check_autologin_prerequisites($USER->id);
  
          if (isset($_GET['privatetoken']) or empty($privatetoken)) {
              throw new moodle_exception('invalidprivatetoken', 'tool_mobile');
          }
  
          // Check the request counter, we must limit the number of times the privatetoken is sent.
          // Between each request 6 minutes are required.
          $last = get_user_preferences('tool_mobile_autologin_request_last', 0, $USER);
          // Check if we must reset the count.
          $timenow = time();
          if ($timenow - $last < 6 * MINSECS) {
              throw new moodle_exception('autologinkeygenerationlockout', 'tool_mobile');
          }
          set_user_preference('tool_mobile_autologin_request_last', $timenow, $USER);
  
          // We are expecting a privatetoken linked to the current token being used.
          // This WS is only valid when using mobile services via REST (this is intended).
          $currenttoken = required_param('wstoken', PARAM_ALPHANUM);
          $conditions = array(
              'userid' => $USER->id,
              'token' => $currenttoken,
              'privatetoken' => $privatetoken,
          );
          if (!$token = $DB->get_record('external_tokens', $conditions)) {
              throw new moodle_exception('invalidprivatetoken', 'tool_mobile');
          }
  
          $result = array();
          $result['key'] = api::get_autologin_key();
          $autologinurl = new moodle_url("/$CFG->admin/tool/mobile/autologin.php");
          $result['autologinurl'] = $autologinurl->out(false);
          $result['warnings'] = array();
          return $result;
      }
code_tokens: public static function get_autologin_key ( $ privatetoken ) { global $ CFG , $ DB , $ USER ; $ params = self :: validate_parameters ( self :: get_autologin_key_parameters ( ) , array ( 'privatetoken' => $ privatetoken ) ) ; $ privatetoken = $ params [ 'privatetoken' ] ; $ context = context_system :: instance ( ) ; // We must toletare these two exceptions: forcepasswordchangenotice and usernotfullysetup. try { self :: validate_context ( $ context ) ; } catch ( moodle_exception $ e ) { if ( $ e -> errorcode != 'usernotfullysetup' && $ e -> errorcode != 'forcepasswordchangenotice' ) { // In case we receive a different exception, throw it. throw $ e ; } } // Only requests from the Moodle mobile or desktop app. This enhances security to avoid any type of XSS attack. // This code goes intentionally here and not inside the check_autologin_prerequisites() function because it // is used by other PHP scripts that can be opened in any browser. if ( ! \ core_useragent :: is_moodle_app ( ) ) { throw new moodle_exception ( 'apprequired' , 'tool_mobile' ) ; } api :: check_autologin_prerequisites ( $ USER -> id ) ; if ( isset ( $ _GET [ 'privatetoken' ] ) or empty ( $ privatetoken ) ) { throw new moodle_exception ( 'invalidprivatetoken' , 'tool_mobile' ) ; } // Check the request counter, we must limit the number of times the privatetoken is sent. // Between each request 6 minutes are required. $ last = get_user_preferences ( 'tool_mobile_autologin_request_last' , 0 , $ USER ) ; // Check if we must reset the count. $ timenow = time ( ) ; if ( $ timenow - $ last < 6 * MINSECS ) { throw new moodle_exception ( 'autologinkeygenerationlockout' , 'tool_mobile' ) ; } set_user_preference ( 'tool_mobile_autologin_request_last' , $ timenow , $ USER ) ; // We are expecting a privatetoken linked to the current token being used. // This WS is only valid when using mobile services via REST (this is intended). $ currenttoken = required_param ( 'wstoken' , PARAM_ALPHANUM ) ; $ conditions = array ( 'userid' => $ USER -> id , 'token' => $ currenttoken , 'privatetoken' => $ privatetoken , ) ; if ( ! $ token = $ DB -> get_record ( 'external_tokens' , $ conditions ) ) { throw new moodle_exception ( 'invalidprivatetoken' , 'tool_mobile' ) ; } $ result = array ( ) ; $ result [ 'key' ] = api :: get_autologin_key ( ) ; $ autologinurl = new moodle_url ( "/$CFG->admin/tool/mobile/autologin.php" ) ; $ result [ 'autologinurl' ] = $ autologinurl -> out ( false ) ; $ result [ 'warnings' ] = array ( ) ; return $ result ; }
docstring: Creates an auto-login key for the current user. Is created only in https sites and is restricted by time and ip address.
  
  Please note that it only works if the request comes from the Moodle mobile or desktop app.
  
  @param string $privatetoken the user private token for validating the request
  @return array with the settings and warnings
  @since  Moodle 3.2
docstring_tokens: Creates an auto - login key for the current user . Is created only in https sites and is restricted by time and ip address .