Commit | Line | Data |
---|---|---|
91223df6 AN |
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 process replies to forum posts. | |
19 | * | |
20 | * @package mod_forum | |
21 | * @subpackage core_message | |
22 | * @copyright 2014 Andrew Nicols | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | namespace mod_forum\message\inbound; | |
27 | ||
28 | defined('MOODLE_INTERNAL') || die(); | |
29 | ||
30 | require_once($CFG->dirroot . '/mod/forum/lib.php'); | |
31 | require_once($CFG->dirroot . '/repository/lib.php'); | |
346ba4f4 | 32 | require_once($CFG->libdir . '/completionlib.php'); |
91223df6 AN |
33 | |
34 | /** | |
35 | * A Handler to process replies to forum posts. | |
36 | * | |
37 | * @copyright 2014 Andrew Nicols | |
38 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
39 | */ | |
91223df6 AN |
40 | class reply_handler extends \core\message\inbound\handler { |
41 | ||
42 | /** | |
43 | * Return a description for the current handler. | |
44 | * | |
45 | * @return string | |
46 | */ | |
47 | public function get_description() { | |
48 | return get_string('reply_handler', 'mod_forum'); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Return a short name for the current handler. | |
53 | * This appears in the admin pages as a human-readable name. | |
54 | * | |
55 | * @return string | |
56 | */ | |
57 | public function get_name() { | |
58 | return get_string('reply_handler_name', 'mod_forum'); | |
59 | } | |
60 | ||
61 | /** | |
62 | * Process a message received and validated by the Inbound Message processor. | |
63 | * | |
e90a0396 SH |
64 | * @throws \core\message\inbound\processing_failed_exception |
65 | * @param \stdClass $messagedata The Inbound Message record | |
66 | * @param \stdClass $messagedata The message data packet | |
91223df6 AN |
67 | * @return bool Whether the message was successfully processed. |
68 | */ | |
69 | public function process_message(\stdClass $record, \stdClass $messagedata) { | |
e90a0396 | 70 | global $DB, $USER; |
91223df6 AN |
71 | |
72 | // Load the post being replied to. | |
73 | $post = $DB->get_record('forum_posts', array('id' => $record->datavalue)); | |
74 | if (!$post) { | |
75 | mtrace("--> Unable to find a post matching with id {$record->datavalue}"); | |
76 | return false; | |
77 | } | |
78 | ||
79 | // Load the discussion that this post is in. | |
80 | $discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion)); | |
81 | if (!$post) { | |
82 | mtrace("--> Unable to find the discussion for post {$record->datavalue}"); | |
83 | return false; | |
84 | } | |
85 | ||
86 | // Load the other required data. | |
87 | $forum = $DB->get_record('forum', array('id' => $discussion->forum)); | |
88 | $course = $DB->get_record('course', array('id' => $forum->course)); | |
91223df6 AN |
89 | $cm = get_fast_modinfo($course->id)->instances['forum'][$forum->id]; |
90 | $modcontext = \context_module::instance($cm->id); | |
91 | $usercontext = \context_user::instance($USER->id); | |
92 | ||
93 | // Make sure user can post in this discussion. | |
94 | $canpost = true; | |
95 | if (!forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext)) { | |
96 | $canpost = false; | |
97 | } | |
98 | ||
99 | if (isset($cm->groupmode) && empty($course->groupmodeforce)) { | |
100 | $groupmode = $cm->groupmode; | |
101 | } else { | |
102 | $groupmode = $course->groupmode; | |
103 | } | |
104 | if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) { | |
105 | if ($discussion->groupid == -1) { | |
106 | $canpost = false; | |
107 | } else { | |
108 | if (!groups_is_member($discussion->groupid)) { | |
109 | $canpost = false; | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | if (!$canpost) { | |
115 | $data = new \stdClass(); | |
116 | $data->forum = $forum; | |
e90a0396 | 117 | throw new \core\message\inbound\processing_failed_exception('messageinboundnopostforum', 'mod_forum', $data); |
91223df6 AN |
118 | } |
119 | ||
120 | // And check the availability. | |
5d78635e | 121 | if (!\core_availability\info_module::is_user_visible($cm)) { |
91223df6 AN |
122 | $data = new \stdClass(); |
123 | $data->forum = $forum; | |
e90a0396 | 124 | throw new \core\message\inbound\processing_failed_exception('messageinboundforumhidden', 'mod_forum', $data); |
91223df6 AN |
125 | } |
126 | ||
127 | // Before we add this we must check that the user will not exceed the blocking threshold. | |
128 | // This should result in an appropriate reply. | |
129 | $thresholdwarning = forum_check_throttling($forum, $cm); | |
130 | if (!empty($thresholdwarning) && !$thresholdwarning->canpost) { | |
131 | $data = new \stdClass(); | |
132 | $data->forum = $forum; | |
133 | $data->message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional); | |
e90a0396 | 134 | throw new \core\message\inbound\processing_failed_exception('messageinboundthresholdhit', 'mod_forum', $data); |
91223df6 AN |
135 | } |
136 | ||
338ee3d6 AN |
137 | $subject = clean_param($messagedata->envelope->subject, PARAM_TEXT); |
138 | $restring = get_string('re', 'forum'); | |
139 | if (strpos($subject, $discussion->name)) { | |
140 | // The discussion name is mentioned in the e-mail subject. This is probably just the standard reply. Use the | |
141 | // standard reply subject instead. | |
142 | $newsubject = $restring . ' ' . $discussion->name; | |
143 | mtrace("--> Note: Post subject matched discussion name. Optimising from {$subject} to {$newsubject}"); | |
144 | $subject = $newsubject; | |
145 | } else if (strpos($subject, $post->subject)) { | |
146 | // The replied-to post's subject is mentioned in the e-mail subject. | |
147 | // Use the previous post's subject instead of the e-mail subject. | |
148 | $newsubject = $post->subject; | |
149 | if (!strpos($restring, $post->subject)) { | |
150 | // The previous post did not contain a re string, add it. | |
72590a51 | 151 | $newsubject = $restring . ' ' . $newsubject; |
338ee3d6 AN |
152 | } |
153 | mtrace("--> Note: Post subject matched original post subject. Optimising from {$subject} to {$newsubject}"); | |
154 | $subject = $newsubject; | |
155 | } | |
156 | ||
91223df6 AN |
157 | $addpost = new \stdClass(); |
158 | $addpost->course = $course->id; | |
159 | $addpost->forum = $forum->id; | |
160 | $addpost->discussion = $discussion->id; | |
161 | $addpost->modified = $messagedata->timestamp; | |
338ee3d6 | 162 | $addpost->subject = $subject; |
91223df6 AN |
163 | $addpost->parent = $post->id; |
164 | $addpost->itemid = file_get_unused_draft_itemid(); | |
165 | ||
aef3c18b AA |
166 | list ($message, $format) = self::remove_quoted_text($messagedata); |
167 | $addpost->message = $message; | |
168 | $addpost->messageformat = $format; | |
91223df6 AN |
169 | |
170 | // We don't trust text coming from e-mail. | |
171 | $addpost->messagetrust = false; | |
172 | ||
e95df45b MG |
173 | // Find all attachments. If format is plain text, treat inline attachments as regular ones. |
174 | $attachments = !empty($messagedata->attachments['attachment']) ? $messagedata->attachments['attachment'] : []; | |
175 | $inlineattachments = []; | |
176 | if (!empty($messagedata->attachments['inline'])) { | |
177 | if ($addpost->messageformat == FORMAT_HTML) { | |
178 | $inlineattachments = $messagedata->attachments['inline']; | |
179 | } else { | |
180 | $attachments = array_merge($attachments, $messagedata->attachments['inline']); | |
181 | } | |
182 | } | |
183 | ||
91223df6 | 184 | // Add attachments to the post. |
e95df45b MG |
185 | if ($attachments) { |
186 | $attachmentcount = count($attachments); | |
581e75bf | 187 | if (!forum_can_create_attachment($forum, $modcontext)) { |
91223df6 AN |
188 | // Attachments are not allowed. |
189 | mtrace("--> User does not have permission to attach files in this forum. Rejecting e-mail."); | |
190 | ||
191 | $data = new \stdClass(); | |
192 | $data->forum = $forum; | |
193 | $data->attachmentcount = $attachmentcount; | |
e90a0396 | 194 | throw new \core\message\inbound\processing_failed_exception('messageinboundattachmentdisallowed', 'mod_forum', $data); |
91223df6 AN |
195 | } |
196 | ||
197 | if ($forum->maxattachments < $attachmentcount) { | |
198 | // Too many attachments. | |
199 | mtrace("--> User attached {$attachmentcount} files when only {$forum->maxattachments} where allowed. " | |
200 | . " Rejecting e-mail."); | |
201 | ||
202 | $data = new \stdClass(); | |
203 | $data->forum = $forum; | |
204 | $data->attachmentcount = $attachmentcount; | |
e90a0396 | 205 | throw new \core\message\inbound\processing_failed_exception('messageinboundfilecountexceeded', 'mod_forum', $data); |
91223df6 AN |
206 | } |
207 | ||
208 | $filesize = 0; | |
209 | $addpost->attachments = file_get_unused_draft_itemid(); | |
e95df45b | 210 | foreach ($attachments as $attachment) { |
91223df6 AN |
211 | mtrace("--> Processing {$attachment->filename} as an attachment."); |
212 | $this->process_attachment('*', $usercontext, $addpost->attachments, $attachment); | |
213 | $filesize += $attachment->filesize; | |
214 | } | |
215 | ||
216 | if ($forum->maxbytes < $filesize) { | |
217 | // Too many attachments. | |
218 | mtrace("--> User attached {$filesize} bytes of files when only {$forum->maxbytes} where allowed. " | |
219 | . "Rejecting e-mail."); | |
220 | $data = new \stdClass(); | |
221 | $data->forum = $forum; | |
222 | $data->maxbytes = display_size($forum->maxbytes); | |
223 | $data->filesize = display_size($filesize); | |
e90a0396 | 224 | throw new \core\message\inbound\processing_failed_exception('messageinboundfilesizeexceeded', 'mod_forum', $data); |
91223df6 AN |
225 | } |
226 | } | |
227 | ||
228 | // Process any files in the message itself. | |
e95df45b MG |
229 | if ($inlineattachments) { |
230 | foreach ($inlineattachments as $attachment) { | |
91223df6 AN |
231 | mtrace("--> Processing {$attachment->filename} as an inline attachment."); |
232 | $this->process_attachment('*', $usercontext, $addpost->itemid, $attachment); | |
233 | ||
234 | // Convert the contentid link in the message. | |
235 | $draftfile = \moodle_url::make_draftfile_url($addpost->itemid, '/', $attachment->filename); | |
236 | $addpost->message = preg_replace('/cid:' . $attachment->contentid . '/', $draftfile, $addpost->message); | |
237 | } | |
238 | } | |
239 | ||
240 | // Insert the message content now. | |
241 | $addpost->id = forum_add_new_post($addpost, true); | |
242 | ||
243 | // Log the new post creation. | |
244 | $params = array( | |
245 | 'context' => $modcontext, | |
246 | 'objectid' => $addpost->id, | |
247 | 'other' => array( | |
248 | 'discussionid' => $discussion->id, | |
249 | 'forumid' => $forum->id, | |
250 | 'forumtype' => $forum->type, | |
251 | ) | |
252 | ); | |
253 | $event = \mod_forum\event\post_created::create($params); | |
254 | $event->add_record_snapshot('forum_posts', $addpost); | |
255 | $event->add_record_snapshot('forum_discussions', $discussion); | |
256 | $event->trigger(); | |
257 | ||
346ba4f4 CW |
258 | // Update completion state. |
259 | $completion = new \completion_info($course); | |
260 | if ($completion->is_enabled($cm) && ($forum->completionreplies || $forum->completionposts)) { | |
261 | $completion->update_state($cm, COMPLETION_COMPLETE); | |
262 | ||
263 | mtrace("--> Updating completion status for user {$USER->id} in forum {$forum->id} for post {$addpost->id}."); | |
264 | } | |
265 | ||
91223df6 AN |
266 | mtrace("--> Created a post {$addpost->id} in {$discussion->id}."); |
267 | return $addpost; | |
268 | } | |
269 | ||
270 | /** | |
271 | * Process attachments included in a message. | |
272 | * | |
e90a0396 SH |
273 | * @param string[] $acceptedtypes String The mimetypes of the acceptable attachment types. |
274 | * @param \context_user $context context_user The context of the user creating this attachment. | |
275 | * @param int $itemid int The itemid to store this attachment under. | |
276 | * @param \stdClass $attachment stdClass The Attachment data to store. | |
277 | * @return \stored_file | |
91223df6 AN |
278 | */ |
279 | protected function process_attachment($acceptedtypes, \context_user $context, $itemid, \stdClass $attachment) { | |
e90a0396 | 280 | global $USER, $CFG; |
91223df6 AN |
281 | |
282 | // Create the file record. | |
283 | $record = new \stdClass(); | |
284 | $record->filearea = 'draft'; | |
285 | $record->component = 'user'; | |
286 | ||
287 | $record->itemid = $itemid; | |
288 | $record->license = $CFG->sitedefaultlicense; | |
289 | $record->author = fullname($USER); | |
290 | $record->contextid = $context->id; | |
291 | $record->userid = $USER->id; | |
292 | ||
293 | // All files sent by e-mail should have a flat structure. | |
294 | $record->filepath = '/'; | |
295 | ||
296 | $record->filename = $attachment->filename; | |
297 | ||
298 | mtrace("--> Attaching {$record->filename} to " . | |
299 | "/{$record->contextid}/{$record->component}/{$record->filearea}/" . | |
300 | "{$record->itemid}{$record->filepath}{$record->filename}"); | |
301 | ||
302 | $fs = get_file_storage(); | |
303 | return $fs->create_file_from_string($record, $attachment->content); | |
304 | } | |
305 | ||
91223df6 AN |
306 | /** |
307 | * Return the content of any success notification to be sent. | |
308 | * Both an HTML and Plain Text variant must be provided. | |
309 | * | |
310 | * @param \stdClass $messagedata The message data. | |
311 | * @param \stdClass $handlerresult The record for the newly created post. | |
312 | * @return \stdClass with keys `html` and `plain`. | |
313 | */ | |
314 | public function get_success_message(\stdClass $messagedata, $handlerresult) { | |
315 | $a = new \stdClass(); | |
316 | $a->subject = $handlerresult->subject; | |
317 | $discussionurl = new \moodle_url('/mod/forum/discuss.php', array('d' => $handlerresult->discussion)); | |
f0b7925f | 318 | $discussionurl->set_anchor('p' . $handlerresult->id); |
91223df6 AN |
319 | $a->discussionurl = $discussionurl->out(); |
320 | ||
321 | $message = new \stdClass(); | |
322 | $message->plain = get_string('postbymailsuccess', 'mod_forum', $a); | |
323 | $message->html = get_string('postbymailsuccess_html', 'mod_forum', $a); | |
324 | return $message; | |
325 | } | |
91223df6 | 326 | } |