Exemplo n.º 1
0
 /**
  * Process an inbound address to obtain the data stored within it.
  *
  * @param string $address The fully formed e-mail address to process.
  */
 protected function process($address)
 {
     global $DB;
     if (!self::is_correct_format($address)) {
         // This address does not contain a subaddress to parse.
         return;
     }
     // Ensure that the instance record is empty.
     $this->record = null;
     $record = new \stdClass();
     $record->address = $address;
     list($localpart) = explode('@', $address, 2);
     list($record->mailbox, $encodeddata) = explode('+', $localpart, 2);
     $data = base64_decode($encodeddata, true);
     if (!$data) {
         // This address has no valid data.
         return;
     }
     $content = @unpack('N2handlerid/N2userid/N2datavalue/H*datakey', $data);
     if (!$content) {
         // This address has no data.
         return;
     }
     if (PHP_INT_SIZE === 8) {
         // 64-bit machine.
         $content['handlerid'] = $content['handlerid1'] << 32 | $content['handlerid2'];
         $content['userid'] = $content['userid1'] << 32 | $content['userid2'];
         $content['datavalue'] = $content['datavalue1'] << 32 | $content['datavalue2'];
     } else {
         if ($content['handlerid1'] > 0 || $content['userid1'] > 0 || $content['datavalue1'] > 0) {
             // Any 64-bit integer which is greater than the 32-bit integer size will have a non-zero value in the first
             // half of the integer.
             throw new \moodle_exception('Mixed environment.' . ' Key generated with a 64-bit machine but received into a 32-bit machine.');
         }
         $content['handlerid'] = $content['handlerid2'];
         $content['userid'] = $content['userid2'];
         $content['datavalue'] = $content['datavalue2'];
     }
     // Clear the 32-bit to 64-bit variables away.
     unset($content['handlerid1']);
     unset($content['handlerid2']);
     unset($content['userid1']);
     unset($content['userid2']);
     unset($content['datavalue1']);
     unset($content['datavalue2']);
     $record = (object) array_merge((array) $record, $content);
     // Fetch the user record.
     $record->user = $DB->get_record('user', array('id' => $record->userid));
     // Fetch and set the handler.
     if ($handler = manager::get_handler_from_id($record->handlerid)) {
         $this->handler = $handler;
         // Retrieve the record for the data key.
         $record->data = $DB->get_record('messageinbound_datakeys', array('handler' => $handler->id, 'datavalue' => $record->datavalue));
     }
     $this->record = $record;
 }