Exemple #1
0
 /**
  * Generate an e-mail address for the Inbound Message handler, storing a private
  * key for the data object if one was not specified.
  *
  * @param int $userid The ID of the user to generated an address for.
  * @param string $userkey The unique key for this user. If not specified this will be retrieved using
  * get_user_key(). This key must have been created using get_user_key(). This parameter is provided as a performance
  * optimisation for when generating multiple addresses for the same user.
  * @return string|null The generated address, or null if an address could not be generated.
  */
 public function generate($userid, $userkey = null)
 {
     global $CFG;
     // Ensure that Inbound Message is enabled and that there is enough information to proceed.
     if (!manager::is_enabled()) {
         return null;
     }
     if ($userkey == null) {
         $userkey = get_user_key('messageinbound_handler', $userid);
     }
     // Ensure that the minimum requirements are in place.
     if (!isset($this->handler) || !$this->handler) {
         throw new \coding_exception('Inbound Message handler not specified.');
     }
     // Ensure that the requested handler is actually enabled.
     if (!$this->handler->enabled) {
         return null;
     }
     if (!isset($this->datavalue)) {
         throw new \coding_exception('Inbound Message data item has not been specified.');
     }
     $data = array(self::pack_int($this->handler->id), self::pack_int($userid), self::pack_int($this->datavalue), pack('H*', substr(md5($this->fetch_data_key() . $userkey), 0, self::HASHSIZE)));
     $subaddress = base64_encode(implode($data));
     return $CFG->messageinbound_mailbox . '+' . $subaddress . '@' . $CFG->messageinbound_domain;
 }