From 3f4f2da9e0fe17366df855458e06b9914d554568 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Mon, 18 Mar 2019 15:23:59 +0800 Subject: [PATCH] MDL-61738 messageinbound: Fix quota checks & filesize for email uploads Private files uploaded by email will now honour the file quota limit, because the filesize is set correctly and checked against users' remaining personal quota limit. Previously, attachment size was always set to zero, and quota was checked against the draft area (this is not valid for email uploads, because each file is moved out of the draft area as it is processed, so multiple files totalling greater than the remaining quota would still pass the check). --- admin/tool/messageinbound/classes/manager.php | 2 +- lib/classes/message/inbound/private_files_handler.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/admin/tool/messageinbound/classes/manager.php b/admin/tool/messageinbound/classes/manager.php index 81e8c1b69e9..0e25a023202 100644 --- a/admin/tool/messageinbound/classes/manager.php +++ b/admin/tool/messageinbound/classes/manager.php @@ -672,7 +672,7 @@ class manager { $attachment->charset = $partdata->getCharset(); $attachment->description = $partdata->getDescription(); $attachment->contentid = $partdata->getContentId(); - $attachment->filesize = $messagedata->getBodyPartSize($part); + $attachment->filesize = $partdata->getBytes(); if (!empty($CFG->antiviruses)) { mtrace("--> Attempting virus scan of '{$attachment->filename}'"); diff --git a/lib/classes/message/inbound/private_files_handler.php b/lib/classes/message/inbound/private_files_handler.php index a0eba4071a6..64d9d2541b5 100644 --- a/lib/classes/message/inbound/private_files_handler.php +++ b/lib/classes/message/inbound/private_files_handler.php @@ -26,6 +26,8 @@ namespace core\message\inbound; defined('MOODLE_INTERNAL') || die(); +require_once($CFG->libdir . '/filelib.php'); + /** * A Handler to store attachments sent in e-mails as private files. * @@ -98,15 +100,17 @@ class private_files_handler extends handler { $uploadedfiles = array(); $failedfiles = array(); + $usedspace = file_get_user_used_space(); $fs = get_file_storage(); foreach ($data->attachments as $attachmenttype => $attachments) { foreach ($attachments as $attachment) { mtrace("--- Processing attachment '{$attachment->filename}'"); - if (file_is_draft_area_limit_reached($itemid, $maxbytes, $attachment->filesize)) { + if ($maxbytes != USER_CAN_IGNORE_FILE_SIZE_LIMITS && + ($attachment->filesize + $usedspace) > $maxbytes) { // The user quota will be exceeded if this file is included. $skippedfiles[] = $attachment; - mtrace("---- Skipping attacment. User will be over quota."); + mtrace("---- Skipping attachment. User will be over quota."); continue; } @@ -132,8 +136,9 @@ class private_files_handler extends handler { // File created successfully. mtrace("---- File uploaded successfully as {$record->filename}."); $uploadedfiles[] = $attachment; + $usedspace += $attachment->filesize; } else { - mtrace("---- Skipping attacment. Unknown failure during creation."); + mtrace("---- Skipping attachment. Unknown failure during creation."); $failedfiles[] = $attachment; } }