[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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


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