Merge branch '2.0'
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / data / conversation / message / ConversationMessageAction.class.php
1 <?php
2 namespace wcf\data\conversation\message;
3 use wcf\data\conversation\Conversation;
4 use wcf\data\conversation\ConversationAction;
5 use wcf\data\conversation\ConversationEditor;
6 use wcf\data\smiley\SmileyCache;
7 use wcf\data\AbstractDatabaseObjectAction;
8 use wcf\data\DatabaseObject;
9 use wcf\data\IAttachmentMessageQuickReplyAction;
10 use wcf\data\IMessageInlineEditorAction;
11 use wcf\data\IMessageQuoteAction;
12 use wcf\system\attachment\AttachmentHandler;
13 use wcf\system\bbcode\BBCodeHandler;
14 use wcf\system\bbcode\BBCodeParser;
15 use wcf\system\bbcode\PreParser;
16 use wcf\system\exception\PermissionDeniedException;
17 use wcf\system\exception\UserInputException;
18 use wcf\system\message\censorship\Censorship;
19 use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
20 use wcf\system\message\quote\MessageQuoteManager;
21 use wcf\system\message\QuickReplyManager;
22 use wcf\system\moderation\queue\ModerationQueueManager;
23 use wcf\system\request\LinkHandler;
24 use wcf\system\search\SearchIndexManager;
25 use wcf\system\user\notification\object\ConversationMessageUserNotificationObject;
26 use wcf\system\user\notification\UserNotificationHandler;
27 use wcf\system\user\storage\UserStorageHandler;
28 use wcf\system\WCF;
29 use wcf\util\MessageUtil;
30 use wcf\util\StringUtil;
31
32 /**
33 * Executes conversation message-related actions.
34 *
35 * @author Marcel Werk
36 * @copyright 2001-2014 WoltLab GmbH
37 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
38 * @package com.woltlab.wcf.conversation
39 * @subpackage data.conversation.message
40 * @category Community Framework
41 */
42 class ConversationMessageAction extends AbstractDatabaseObjectAction implements IAttachmentMessageQuickReplyAction, IMessageInlineEditorAction, IMessageQuoteAction {
43 /**
44 * @see \wcf\data\AbstractDatabaseObjectAction::$className
45 */
46 protected $className = 'wcf\data\conversation\message\ConversationMessageEditor';
47
48 /**
49 * conversation object
50 * @var \wcf\data\conversation\Conversation
51 */
52 public $conversation = null;
53
54 /**
55 * conversation message object
56 * @var \wcf\data\conversation\message\ConversationMessage
57 */
58 public $message = null;
59
60 /**
61 * @see \wcf\data\AbstractDatabaseObjectAction::create()
62 */
63 public function create() {
64 // count attachments
65 if (isset($this->parameters['attachmentHandler']) && $this->parameters['attachmentHandler'] !== null) {
66 $this->parameters['data']['attachments'] = count($this->parameters['attachmentHandler']);
67 }
68
69 if (LOG_IP_ADDRESS) {
70 // add ip address
71 if (!isset($this->parameters['data']['ipAddress'])) {
72 $this->parameters['data']['ipAddress'] = WCF::getSession()->ipAddress;
73 }
74 }
75 else {
76 // do not track ip address
77 if (isset($this->parameters['data']['ipAddress'])) {
78 unset($this->parameters['data']['ipAddress']);
79 }
80 }
81
82 // create message
83 $message = parent::create();
84 $messageEditor = new ConversationMessageEditor($message);
85
86 // get conversation
87 $conversation = (isset($this->parameters['converation']) ? $this->parameters['converation'] : new Conversation($message->conversationID));
88 $conversationEditor = new ConversationEditor($conversation);
89
90 if (empty($this->parameters['isFirstPost'])) {
91 // update last message
92 $conversationEditor->addMessage($message);
93
94 // fire notification event
95 if (!$conversation->isDraft) {
96 $notificationRecipients = array_diff($conversation->getParticipantIDs(true), array($message->userID)); // don't notify message author
97 if (!empty($notificationRecipients)) {
98 UserNotificationHandler::getInstance()->fireEvent('conversationMessage', 'com.woltlab.wcf.conversation.message.notification', new ConversationMessageUserNotificationObject($message), $notificationRecipients);
99 }
100 }
101
102 $userConversation = Conversation::getUserConversation($conversation->conversationID, $message->userID);
103 if ($userConversation !== null && $userConversation->isInvisible) {
104 // make invisible participant visible
105 $sql = "UPDATE wcf".WCF_N."_conversation_to_user
106 SET isInvisible = 0
107 WHERE participantID = ?
108 AND conversationID = ?";
109 $statement = WCF::getDB()->prepareStatement($sql);
110 $statement->execute(array($message->userID, $conversation->conversationID));
111
112 $conversationEditor->updateParticipantSummary();
113 $conversationEditor->updateParticipantCount();
114 }
115
116 // reset visibility if it was hidden but not left
117 $sql = "UPDATE wcf".WCF_N."_conversation_to_user
118 SET hideConversation = ?
119 WHERE conversationID = ?
120 AND hideConversation = ?";
121 $statement = WCF::getDB()->prepareStatement($sql);
122 $statement->execute(array(
123 Conversation::STATE_DEFAULT,
124 $conversation->conversationID,
125 Conversation::STATE_HIDDEN
126 ));
127 }
128
129 // reset storage
130 UserStorageHandler::getInstance()->reset($conversation->getParticipantIDs(), 'unreadConversationCount');
131
132 // update search index
133 SearchIndexManager::getInstance()->add('com.woltlab.wcf.conversation.message', $message->messageID, $message->message, (!empty($this->parameters['isFirstPost']) ? $conversation->subject : ''), $message->time, $message->userID, $message->username);
134
135 // save embedded objects
136 if (MessageEmbeddedObjectManager::getInstance()->registerObjects('com.woltlab.wcf.conversation.message', $message->messageID, $message->message)) {
137 $messageEditor->update(array(
138 'hasEmbeddedObjects' => 1
139 ));
140 }
141
142 // update attachments
143 if (isset($this->parameters['attachmentHandler']) && $this->parameters['attachmentHandler'] !== null) {
144 $this->parameters['attachmentHandler']->updateObjectID($message->messageID);
145 }
146
147 // clear quotes
148 if (isset($this->parameters['removeQuoteIDs']) && !empty($this->parameters['removeQuoteIDs'])) {
149 MessageQuoteManager::getInstance()->markQuotesForRemoval($this->parameters['removeQuoteIDs']);
150 }
151 MessageQuoteManager::getInstance()->removeMarkedQuotes();
152
153 // return new message
154 return $message;
155 }
156
157 /**
158 * @see \wcf\data\AbstractDatabaseObjectAction::update()
159 */
160 public function update() {
161 // count attachments
162 if (isset($this->parameters['attachmentHandler']) && $this->parameters['attachmentHandler'] !== null) {
163 $this->parameters['data']['attachments'] = count($this->parameters['attachmentHandler']);
164 }
165
166 parent::update();
167
168 // update search index / embedded objects
169 if (isset($this->parameters['data']) && isset($this->parameters['data']['message'])) {
170 foreach ($this->objects as $message) {
171 $conversation = $message->getConversation();
172 SearchIndexManager::getInstance()->update('com.woltlab.wcf.conversation.message', $message->messageID, $this->parameters['data']['message'], ($conversation->firstMessageID == $message->messageID ? $conversation->subject : ''), $message->time, $message->userID, $message->username);
173
174 if ($message->hasEmbeddedObjects != MessageEmbeddedObjectManager::getInstance()->registerObjects('com.woltlab.wcf.conversation.message', $message->messageID, $this->parameters['data']['message'])) {
175 $message->update(array(
176 'hasEmbeddedObjects' => ($message->hasEmbeddedObjects ? 0 : 1)
177 ));
178 }
179 }
180 }
181 }
182
183 /**
184 * @see \wcf\data\AbstractDatabaseObjectAction::delete()
185 */
186 public function delete() {
187 $count = parent::delete();
188
189 $attachmentMessageIDs = $conversationIDs = array();
190 foreach ($this->objects as $message) {
191 if (!in_array($message->conversationID, $conversationIDs)) {
192 $conversationIDs[] = $message->conversationID;
193 }
194
195 if ($message->attachments) {
196 $attachmentMessageIDs[] = $message->messageID;
197 }
198 }
199
200 // rebuild conversations
201 if (!empty($conversationIDs)) {
202 $conversationAction = new ConversationAction($conversationIDs, 'rebuild');
203 $conversationAction->executeAction();
204 }
205
206 if (!empty($this->objectIDs)) {
207 // delete notifications
208 UserNotificationHandler::getInstance()->deleteNotifications('conversationMessage', 'com.woltlab.wcf.conversation.message.notification', array(), $this->objectIDs);
209
210 // update search index
211 SearchIndexManager::getInstance()->delete('com.woltlab.wcf.conversation.message', $this->objectIDs);
212
213 // update embedded objects
214 MessageEmbeddedObjectManager::getInstance()->removeObjects('com.woltlab.wcf.conversation.message', $this->objectIDs);
215
216 // remove moderation queues
217 ModerationQueueManager::getInstance()->removeQueues('com.woltlab.wcf.conversation.message', $this->objectIDs);
218 }
219
220 // remove attachments
221 if (!empty($attachmentMessageIDs)) {
222 AttachmentHandler::removeAttachments('com.woltlab.wcf.conversation.message', $attachmentMessageIDs);
223 }
224
225 return $count;
226 }
227
228 /**
229 * @see \wcf\data\IMessageQuickReply::validateQuickReply()
230 */
231 public function validateQuickReply() {
232 QuickReplyManager::getInstance()->setAllowedBBCodes(explode(',', WCF::getSession()->getPermission('user.message.allowedBBCodes')));
233 QuickReplyManager::getInstance()->validateParameters($this, $this->parameters, 'wcf\data\conversation\Conversation');
234 }
235
236 /**
237 * @see \wcf\data\IMessageQuickReply::quickReply()
238 */
239 public function quickReply() {
240 return QuickReplyManager::getInstance()->createMessage(
241 $this,
242 $this->parameters,
243 'wcf\data\conversation\ConversationAction',
244 CONVERSATION_LIST_DEFAULT_SORT_ORDER,
245 'conversationMessageList'
246 );
247 }
248
249 /**
250 * @see \wcf\data\IExtendedMessageQuickReplyAction::validateJumpToExtended()
251 */
252 public function validateJumpToExtended() {
253 $this->readInteger('containerID');
254 $this->readString('message', true);
255
256 $this->conversation = new Conversation($this->parameters['containerID']);
257 if (!$this->conversation->conversationID) {
258 throw new UserInputException('containerID');
259 }
260 else if ($this->conversation->isClosed || !Conversation::isParticipant(array($this->conversation->conversationID))) {
261 throw new PermissionDeniedException();
262 }
263
264 // editing existing message
265 if (isset($this->parameters['messageID'])) {
266 $this->message = new ConversationMessage(intval($this->parameters['messageID']));
267 if (!$this->message->messageID || ($this->message->conversationID != $this->conversation->conversationID)) {
268 throw new UserInputException('messageID');
269 }
270
271 if (!$this->message->canEdit()) {
272 throw new PermissionDeniedException();
273 }
274 }
275 }
276
277 /**
278 * @see \wcf\data\IExtendedMessageQuickReplyAction::jumpToExtended()
279 */
280 public function jumpToExtended() {
281 // quick reply
282 if ($this->message === null) {
283 QuickReplyManager::getInstance()->setMessage('conversation', $this->conversation->conversationID, $this->parameters['message']);
284 $url = LinkHandler::getInstance()->getLink('ConversationMessageAdd', array('id' => $this->conversation->conversationID));
285 }
286 else {
287 // editing message
288 QuickReplyManager::getInstance()->setMessage('conversationMessage', $this->message->messageID, $this->parameters['message']);
289 $url = LinkHandler::getInstance()->getLink('ConversationMessageEdit', array('id' => $this->message->messageID));
290 }
291
292 // redirect
293 return array(
294 'url' => $url
295 );
296 }
297
298 /**
299 * @see \wcf\data\IMessageInlineEditorAction::validateBeginEdit()
300 */
301 public function validateBeginEdit() {
302 $this->readInteger('containerID');
303 $this->readInteger('objectID');
304
305 $this->conversation = new Conversation($this->parameters['containerID']);
306 if (!$this->conversation->conversationID) {
307 throw new UserInputException('containerID');
308 }
309
310 if ($this->conversation->isClosed || !Conversation::isParticipant(array($this->conversation->conversationID))) {
311 throw new PermissionDeniedException();
312 }
313
314 $this->message = new ConversationMessage($this->parameters['objectID']);
315 if (!$this->message->messageID) {
316 throw new UserInputException('objectID');
317 }
318
319 if (!$this->message->canEdit()) {
320 throw new PermissionDeniedException();
321 }
322 }
323
324 /**
325 * @see \wcf\data\IMessageInlineEditorAction::beginEdit()
326 */
327 public function beginEdit() {
328 BBCodeHandler::getInstance()->setAllowedBBCodes(explode(',', WCF::getSession()->getPermission('user.message.allowedBBCodes')));
329
330 WCF::getTPL()->assign(array(
331 'defaultSmilies' => SmileyCache::getInstance()->getCategorySmilies(),
332 'message' => $this->message,
333 'permissionCanUseSmilies' => 'user.message.canUseSmilies',
334 'wysiwygSelector' => 'messageEditor'.$this->message->messageID
335 ));
336
337 if (MODULE_ATTACHMENT) {
338 $tmpHash = StringUtil::getRandomID();
339 $attachmentHandler = new AttachmentHandler('com.woltlab.wcf.conversation.message', $this->message->messageID, $tmpHash);
340 $attachmentList = $attachmentHandler->getAttachmentList();
341
342 WCF::getTPL()->assign(array(
343 'attachmentHandler' => $attachmentHandler,
344 'attachmentList' => $attachmentList->getObjects(),
345 'attachmentObjectID' => $this->message->messageID,
346 'attachmentObjectType' => 'com.woltlab.wcf.conversation.message',
347 'attachmentParentObjectID' => 0,
348 'tmpHash' => $tmpHash
349 ));
350 }
351
352 return array(
353 'actionName' => 'beginEdit',
354 'template' => WCF::getTPL()->fetch('conversationMessageInlineEditor')
355 );
356 }
357
358 /**
359 * @see \wcf\data\IMessageInlineEditorAction::validateSave()
360 */
361 public function validateSave() {
362 $this->readString('message', true, 'data');
363
364 if (empty($this->parameters['data']['message'])) {
365 throw new UserInputException('message', WCF::getLanguage()->get('wcf.global.form.error.empty'));
366 }
367
368 $this->validateBeginEdit();
369 $this->validateMessage($this->conversation, $this->parameters['data']['message']);
370 }
371
372 /**
373 * @see \wcf\data\IMessageInlineEditorAction::save()
374 */
375 public function save() {
376 $data = array(
377 'message' => PreParser::getInstance()->parse(MessageUtil::stripCrap($this->parameters['data']['message']), explode(',', WCF::getSession()->getPermission('user.message.allowedBBCodes')))
378 );
379 if (!$this->message->getConversation()->isDraft) {
380 $data['lastEditTime'] = TIME_NOW;
381 $data['editCount'] = $this->message->editCount + 1;
382 }
383 // execute update action
384 $action = new ConversationMessageAction(array($this->message), 'update', array('data' => $data));
385 $action->executeAction();
386
387 // load new message
388 $this->message = new ConversationMessage($this->message->messageID);
389 $this->message->getAttachments();
390
391 if (MODULE_ATTACHMENT) {
392 $attachmentList = $this->message->getAttachments();
393 if ($attachmentList !== null) {
394 // set permisions
395 $attachmentList->setPermissions(array(
396 'canDownload' => true,
397 'canViewPreview' => true
398 ));
399 }
400 }
401
402 // load embedded objects
403 MessageEmbeddedObjectManager::getInstance()->loadObjects('com.woltlab.wcf.conversation.message', array($this->message->messageID));
404
405 $data = array(
406 'actionName' => 'save',
407 'message' => $this->message->getFormattedMessage()
408 );
409
410 if (MODULE_ATTACHMENT) {
411 WCF::getTPL()->assign(array(
412 'attachmentList' => $attachmentList,
413 'objectID' => $this->message->messageID
414 ));
415 $data['attachmentList'] = WCF::getTPL()->fetch('attachments');
416 }
417
418 return $data;
419 }
420
421 /**
422 * @see \wcf\data\IMessageQuickReply::validateContainer()
423 */
424 public function validateContainer(DatabaseObject $conversation) {
425 if (!$conversation->conversationID) {
426 throw new UserInputException('objectID');
427 }
428 else if ($conversation->isClosed || !Conversation::isParticipant(array($conversation->conversationID))) {
429 throw new PermissionDeniedException();
430 }
431 }
432
433 /**
434 * @see \wcf\data\IMessageQuickReplyAction::validateMessage()
435 */
436 public function validateMessage(DatabaseObject $container, $message) {
437 $message = MessageUtil::stripCrap($message);
438
439 if (mb_strlen($message) > WCF::getSession()->getPermission('user.conversation.maxLength')) {
440 throw new UserInputException('message', WCF::getLanguage()->getDynamicVariable('wcf.message.error.tooLong', array('maxTextLength' => WCF::getSession()->getPermission('user.conversation.maxLength'))));
441 }
442
443 // search for disallowed bbcodes
444 $disallowedBBCodes = BBCodeParser::getInstance()->validateBBCodes($message, explode(',', WCF::getSession()->getPermission('user.message.allowedBBCodes')));
445 if (!empty($disallowedBBCodes)) {
446 throw new UserInputException('text', WCF::getLanguage()->getDynamicVariable('wcf.message.error.disallowedBBCodes', array('disallowedBBCodes' => $disallowedBBCodes)));
447 }
448
449 // search for censored words
450 if (ENABLE_CENSORSHIP) {
451 $result = Censorship::getInstance()->test($message);
452 if ($result) {
453 throw new UserInputException('message', WCF::getLanguage()->getDynamicVariable('wcf.message.error.censoredWordsFound', array('censoredWords' => $result)));
454 }
455 }
456 }
457
458 /**
459 * @see \wcf\data\IMessageQuickReplyAction::getMessageList()
460 */
461 public function getMessageList(DatabaseObject $conversation, $lastMessageTime) {
462 $messageList = new ViewableConversationMessageList();
463 $messageList->setConversation($conversation);
464 $messageList->getConditionBuilder()->add("conversation_message.conversationID = ?", array($conversation->conversationID));
465 $messageList->getConditionBuilder()->add("conversation_message.time > ?", array($lastMessageTime));
466 $messageList->sqlOrderBy = "conversation_message.time ".CONVERSATION_LIST_DEFAULT_SORT_ORDER;
467 $messageList->readObjects();
468
469 return $messageList;
470 }
471
472 /**
473 * @see \wcf\data\IMessageQuickReply::getPageNo()
474 */
475 public function getPageNo(DatabaseObject $conversation) {
476 $sql = "SELECT COUNT(*) AS count
477 FROM wcf".WCF_N."_conversation_message
478 WHERE conversationID = ?";
479 $statement = WCF::getDB()->prepareStatement($sql);
480 $statement->execute(array($conversation->conversationID));
481 $count = $statement->fetchArray();
482
483 return array(intval(ceil($count['count'] / CONVERSATION_MESSAGES_PER_PAGE)), $count['count']);
484 }
485
486 /**
487 * @see \wcf\data\IMessageQuickReply::getRedirectUrl()
488 */
489 public function getRedirectUrl(DatabaseObject $conversation, DatabaseObject $message) {
490 return LinkHandler::getInstance()->getLink('Conversation', array(
491 'object' => $conversation,
492 'messageID' => $message->messageID
493 )).'#message'.$message->messageID;
494 }
495
496 /**
497 * @see \wcf\data\IMessageQuoteAction::validateSaveFullQuote()
498 */
499 public function validateSaveFullQuote() {
500 $this->message = $this->getSingleObject();
501
502 if (!Conversation::isParticipant(array($this->message->conversationID))) {
503 throw new PermissionDeniedException();
504 }
505 }
506
507 /**
508 * @see \wcf\data\IMessageQuoteAction::saveFullQuote()
509 */
510 public function saveFullQuote() {
511 if (!MessageQuoteManager::getInstance()->addQuote('com.woltlab.wcf.conversation.message', $this->message->conversationID, $this->message->messageID, $this->message->getExcerpt(), $this->message->getMessage())) {
512 $quoteID = MessageQuoteManager::getInstance()->getQuoteID('com.woltlab.wcf.conversation.message', $this->message->conversationID, $this->message->messageID, $this->message->getExcerpt(), $this->message->getMessage());
513 MessageQuoteManager::getInstance()->removeQuote($quoteID);
514 }
515
516 return array(
517 'count' => MessageQuoteManager::getInstance()->countQuotes(),
518 'fullQuoteMessageIDs' => MessageQuoteManager::getInstance()->getFullQuoteObjectIDs(array('com.woltlab.wcf.conversation.message'))
519 );
520 }
521
522 /**
523 * @see \wcf\data\IMessageQuoteAction::validateSaveQuote()
524 */
525 public function validateSaveQuote() {
526 $this->readString('message');
527 $this->message = $this->getSingleObject();
528
529 if (!Conversation::isParticipant(array($this->message->conversationID))) {
530 throw new PermissionDeniedException();
531 }
532 }
533
534 /**
535 * @see \wcf\data\IMessageQuoteAction::saveQuote()
536 */
537 public function saveQuote() {
538 MessageQuoteManager::getInstance()->addQuote('com.woltlab.wcf.conversation.message', $this->message->conversationID, $this->message->messageID, $this->parameters['message']);
539
540 return array(
541 'count' => MessageQuoteManager::getInstance()->countQuotes(),
542 'fullQuoteMessageIDs' => MessageQuoteManager::getInstance()->getFullQuoteObjectIDs(array('com.woltlab.wcf.conversation.message'))
543 );
544 }
545
546 /**
547 * @see \wcf\data\IMessageQuoteAction::validateGetRenderedQuotes()
548 */
549 public function validateGetRenderedQuotes() {
550 $this->readInteger('parentObjectID');
551
552 $this->conversation = new Conversation($this->parameters['parentObjectID']);
553 if (!$this->conversation->conversationID) {
554 throw new UserInputException('parentObjectID');
555 }
556 }
557
558 /**
559 * @see \wcf\data\IMessageQuoteAction::getRenderedQuotes()
560 */
561 public function getRenderedQuotes() {
562 $quotes = MessageQuoteManager::getInstance()->getQuotesByParentObjectID('com.woltlab.wcf.conversation.message', $this->conversation->conversationID);
563
564 return array(
565 'template' => implode("\n\n", $quotes)
566 );
567 }
568
569 /**
570 * @see \wcf\data\IAttachmentMessageQuickReplyAction::getAttachmentHandler()
571 */
572 public function getAttachmentHandler(DatabaseObject $conversation) {
573 return new AttachmentHandler('com.woltlab.wcf.conversation.message', 0, $this->parameters['tmpHash']);
574 }
575 }