From bd2519c596d2cb744508e54dcd0c64eb777f4a01 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Fri, 25 Jul 2014 11:44:44 +0800 Subject: [PATCH 1/3] MDL-44257 message: check if the attachment exists before remove it Credit goes to Mark Nielsen, thanks. --- message/output/email/message_output_email.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/message/output/email/message_output_email.php b/message/output/email/message_output_email.php index cfe9fd460cf..4b95db11433 100644 --- a/message/output/email/message_output_email.php +++ b/message/output/email/message_output_email.php @@ -88,7 +88,9 @@ class message_output_email extends message_output { $eventdata->fullmessagehtml, $attachment, $attachname); // Remove an attachment file if any. - @unlink($attachment); + if (!empty($attachment) && file_exists($CFG->dataroot.'/'.$attachment)) { + unlink($CFG->dataroot.'/'.$attachment); + } return $result; } From 1780c99e0661a50d09657d339c4f82e0c2960c1a Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Fri, 12 Sep 2014 16:45:49 +0800 Subject: [PATCH 2/3] MDL-44257 message: added test to make sure the stored file still exists --- lib/moodlelib.php | 11 ++++++++++- lib/tests/messagelib_test.php | 5 +++++ message/output/email/message_output_email.php | 7 +++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index ec54c3a3ca7..26425261c7d 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -5884,7 +5884,16 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', } else { require_once($CFG->libdir.'/filelib.php'); $mimetype = mimeinfo('type', $attachname); - $mail->addAttachment($CFG->dataroot .'/'. $attachment, $attachname, 'base64', $mimetype); + + $attachmentpath = $attachment; + + // If the attachment is a full path to a file in the tempdir, use it as is, + // otherwise assume it is a relative path from the dataroot (for backwards compatibility reasons). + if (strpos($attachmentpath, $CFG->tempdir) !== 0) { + $attachmentpath = $CFG->dataroot . '/' . $attachmentpath; + } + + $mail->addAttachment($attachmentpath, $attachname, 'base64', $mimetype); } } diff --git a/lib/tests/messagelib_test.php b/lib/tests/messagelib_test.php index 6f4bd2797eb..20b602fbe9a 100644 --- a/lib/tests/messagelib_test.php +++ b/lib/tests/messagelib_test.php @@ -231,6 +231,11 @@ class core_messagelib_testcase extends advanced_testcase { $email = reset($emails); $this->assertTrue(strpos($email->body, 'Content-Disposition: attachment;') !== false); $this->assertTrue(strpos($email->body, 'emailtest.txt') !== false); + + // Check if the stored file still exists after remove the temporary attachment. + $storedfileexists = $fs->file_exists($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'], + $filerecord['itemid'], $filerecord['filepath'], $filerecord['filename']); + $this->assertTrue($storedfileexists); } /** diff --git a/message/output/email/message_output_email.php b/message/output/email/message_output_email.php index 4b95db11433..722aea1db4c 100644 --- a/message/output/email/message_output_email.php +++ b/message/output/email/message_output_email.php @@ -77,8 +77,7 @@ class message_output_email extends message_output { } else { // Copy attachment file to a temporary directory and get the file path. $attachment = $eventdata->attachment->copy_content_to_temp(); - // Function email_to_user() adds $CFG->dataroot to file path, so removing it here. - $attachment = str_replace($CFG->dataroot, '', $attachment); + // Get attachment file name. $attachname = clean_filename($eventdata->attachname); } @@ -88,8 +87,8 @@ class message_output_email extends message_output { $eventdata->fullmessagehtml, $attachment, $attachname); // Remove an attachment file if any. - if (!empty($attachment) && file_exists($CFG->dataroot.'/'.$attachment)) { - unlink($CFG->dataroot.'/'.$attachment); + if (!empty($attachment) && file_exists($attachment)) { + unlink($attachment); } return $result; From 7972603cc4398fe87d7f9a86216d2d71f29c1270 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Tue, 16 Sep 2014 10:16:37 +0800 Subject: [PATCH 3/3] MDL-44257 lib: fix the attachment code to accept tempdir and dataroot. Credit goes to Damyon, thanks.