Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   * A Handler to store attachments sent in e-mails as private files.
  19   *
  20   * @package    core_message
  21   * @copyright  2014 Andrew Nicols
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\message\inbound;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir . '/filelib.php');
  30  
  31  /**
  32   * A Handler to store attachments sent in e-mails as private files.
  33   *
  34   * @package    core
  35   * @copyright  2014 Andrew Nicols
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class private_files_handler extends handler {
  39  
  40      /**
  41       * Email generated by this handler should not expire.
  42       *
  43       * @return bool
  44       */
  45      public function can_change_defaultexpiration() {
  46          return false;
  47      }
  48  
  49      /**
  50       * Return a description for the current handler.
  51       *
  52       * @return string
  53       */
  54      public function get_description() {
  55          return get_string('private_files_handler', 'moodle');
  56      }
  57  
  58      /**
  59       * Return a short name for the current handler.
  60       * This appears in the admin pages as a human-readable name.
  61       *
  62       * @return string
  63       */
  64      public function get_name() {
  65          return get_string('private_files_handler_name', 'moodle');
  66      }
  67  
  68      /**
  69       * Process a message received and validated by the Inbound Message processor.
  70       *
  71       * @throws \core\message\inbound\processing_failed_exception
  72       * @param \stdClass $record The Inbound Message record
  73       * @param \stdClass $data The message data packet
  74       * @return bool Whether the message was successfully processed.
  75       */
  76      public function process_message(\stdClass $record, \stdClass $data) {
  77          global $USER, $CFG;
  78  
  79          $context = \context_user::instance($USER->id);
  80  
  81          if (!has_capability('moodle/user:manageownfiles', $context)) {
  82              throw new \core\message\inbound\processing_failed_exception('emailtoprivatefilesdenied', 'moodle', $data);
  83          }
  84  
  85          // Initial setup.
  86          $component  = 'user';
  87          $filearea   = 'private';
  88          $itemid     = 0;
  89          $license    = $CFG->sitedefaultlicense;
  90          $author     = fullname($USER);
  91  
  92          // Determine the quota space for this user.
  93          $maxbytes = $CFG->userquota;
  94          if (has_capability('moodle/user:ignoreuserquota', $context)) {
  95              $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
  96          }
  97  
  98          // Keep track of files which were uploaded, and which were skipped.
  99          $skippedfiles   = array();
 100          $uploadedfiles  = array();
 101          $failedfiles    = array();
 102  
 103          $usedspace = file_get_user_used_space();
 104          $fs = get_file_storage();
 105          foreach ($data->attachments as $attachmenttype => $attachments) {
 106              foreach ($attachments as $attachment) {
 107                  mtrace("--- Processing attachment '{$attachment->filename}'");
 108  
 109                  if ($maxbytes != USER_CAN_IGNORE_FILE_SIZE_LIMITS &&
 110                          ($attachment->filesize + $usedspace) > $maxbytes) {
 111                      // The user quota will be exceeded if this file is included.
 112                      $skippedfiles[] = $attachment;
 113                      mtrace("---- Skipping attachment. User will be over quota.");
 114                      continue;
 115                  }
 116  
 117                  // Create a new record for this file.
 118                  $record = new \stdClass();
 119                  $record->filearea   = $filearea;
 120                  $record->component  = $component;
 121                  $record->filepath   = '/';
 122                  $record->itemid     = $itemid;
 123                  $record->license    = $license;
 124                  $record->author     = $author;
 125                  $record->contextid  = $context->id;
 126                  $record->userid     = $USER->id;
 127  
 128                  $record->filename = $fs->get_unused_filename($context->id, $record->component, $record->filearea,
 129                          $record->itemid, $record->filepath, $attachment->filename);
 130  
 131                  mtrace("--> Attaching {$record->filename} to " .
 132                         "/{$record->contextid}/{$record->component}/{$record->filearea}/" .
 133                         "{$record->itemid}{$record->filepath}{$record->filename}");
 134  
 135                  if ($fs->create_file_from_string($record, $attachment->content)) {
 136                      // File created successfully.
 137                      mtrace("---- File uploaded successfully as {$record->filename}.");
 138                      $uploadedfiles[] = $attachment;
 139                      $usedspace += $attachment->filesize;
 140                  } else {
 141                      mtrace("---- Skipping attachment. Unknown failure during creation.");
 142                      $failedfiles[] = $attachment;
 143                  }
 144              }
 145          }
 146  
 147          // TODO send the user a confirmation e-mail.
 148          // Note, some files may have failed because the user has been pushed over quota. This does not constitute a failure.
 149  
 150          return true;
 151      }
 152  }