[ Index ]

PHP Cross Reference of moodle-2.8

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1