Commit | Line | Data |
---|---|---|
ce07ae0c MW |
1 | <?php |
2 | namespace wcf\system\attachment; | |
c5a8bd8c | 3 | use wcf\data\conversation\message\ConversationMessage; |
a0c1a541 | 4 | use wcf\data\conversation\message\ConversationMessageList; |
ce07ae0c MW |
5 | use wcf\data\conversation\Conversation; |
6 | use wcf\system\WCF; | |
f7b9b1f3 | 7 | use wcf\util\ArrayUtil; |
ce07ae0c MW |
8 | |
9 | /** | |
10 | * Attachment object type implementation for conversations. | |
11 | * | |
12 | * @author Marcel Werk | |
a2df5242 | 13 | * @copyright 2001-2016 WoltLab GmbH |
ce07ae0c MW |
14 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
15 | * @package com.woltlab.wcf.conversation | |
16 | * @subpackage system.attachment | |
61f754e0 | 17 | * @category Community Framework |
ce07ae0c MW |
18 | */ |
19 | class ConversationMessageAttachmentObjectType extends AbstractAttachmentObjectType { | |
f7b9b1f3 | 20 | /** |
2270f7e9 | 21 | * @see \wcf\system\attachment\IAttachmentObjectType::getMaxSize() |
f7b9b1f3 MW |
22 | */ |
23 | public function getMaxSize() { | |
24 | return WCF::getSession()->getPermission('user.conversation.maxAttachmentSize'); | |
25 | } | |
26 | ||
27 | /** | |
2270f7e9 | 28 | * @see \wcf\system\attachment\IAttachmentObjectType::getAllowedExtensions() |
f7b9b1f3 MW |
29 | */ |
30 | public function getAllowedExtensions() { | |
31 | return ArrayUtil::trim(explode("\n", WCF::getSession()->getPermission('user.conversation.allowedAttachmentExtensions'))); | |
32 | } | |
33 | ||
34 | /** | |
2270f7e9 | 35 | * @see \wcf\system\attachment\IAttachmentObjectType::getMaxCount() |
f7b9b1f3 MW |
36 | */ |
37 | public function getMaxCount() { | |
38 | return WCF::getSession()->getPermission('user.conversation.maxAttachmentCount'); | |
39 | } | |
40 | ||
ce07ae0c | 41 | /** |
2270f7e9 | 42 | * @see \wcf\system\attachment\IAttachmentObjectType::canDownload() |
ce07ae0c MW |
43 | */ |
44 | public function canDownload($objectID) { | |
45 | if ($objectID) { | |
46 | $message = new ConversationMessage($objectID); | |
47 | $conversation = Conversation::getUserConversation($message->conversationID, WCF::getUser()->userID); | |
48 | if ($conversation->canRead()) return true; | |
49 | } | |
50 | ||
51 | return false; | |
52 | } | |
53 | ||
54 | /** | |
2270f7e9 | 55 | * @see \wcf\system\attachment\IAttachmentObjectType::canUpload() |
ce07ae0c MW |
56 | */ |
57 | public function canUpload($objectID, $parentObjectID = 0) { | |
4c21e83f MW |
58 | if (!WCF::getSession()->getPermission('user.conversation.canUploadAttachment')) { |
59 | return false; | |
60 | } | |
61 | ||
ce07ae0c MW |
62 | if ($objectID) { |
63 | $message = new ConversationMessage($objectID); | |
64 | if ($message->userID == WCF::getUser()->userID) return true; | |
4c21e83f | 65 | return false; |
ce07ae0c MW |
66 | } |
67 | ||
4c21e83f | 68 | return true; |
ce07ae0c MW |
69 | } |
70 | ||
71 | /** | |
2270f7e9 | 72 | * @see \wcf\system\attachment\IAttachmentObjectType::canDelete() |
ce07ae0c MW |
73 | */ |
74 | public function canDelete($objectID) { | |
75 | if ($objectID) { | |
76 | $message = new ConversationMessage($objectID); | |
77 | if ($message->userID == WCF::getUser()->userID) return true; | |
78 | } | |
79 | ||
80 | return false; | |
81 | } | |
a0c1a541 MW |
82 | |
83 | /** | |
84 | * @see \wcf\system\attachment\IAttachmentObjectType::cacheObjects() | |
85 | */ | |
86 | public function cacheObjects(array $objectIDs) { | |
87 | $messageList = new ConversationMessageList(); | |
88 | $messageList->setObjectIDs($objectIDs); | |
89 | $messageList->readObjects(); | |
eaf1c8eb | 90 | $conversationIDs = []; |
a0c1a541 MW |
91 | foreach ($messageList as $message) { |
92 | $conversationIDs[] = $message->conversationID; | |
93 | } | |
94 | if (!empty($conversationIDs)) { | |
95 | $conversations = Conversation::getUserConversations($conversationIDs, WCF::getUser()->userID); | |
96 | foreach ($messageList as $message) { | |
97 | if (isset($conversations[$message->conversationID])) $message->setConversation($conversations[$message->conversationID]); | |
98 | } | |
99 | } | |
773237a9 MW |
100 | |
101 | foreach ($messageList->getObjects() as $objectID => $object) { | |
102 | $this->cachedObjects[$objectID] = $object; | |
103 | } | |
a0c1a541 MW |
104 | } |
105 | ||
106 | /** | |
107 | * @see \wcf\system\attachment\IAttachmentObjectType::setPermissions() | |
108 | */ | |
109 | public function setPermissions(array $attachments) { | |
eaf1c8eb | 110 | $messageIDs = []; |
a0c1a541 MW |
111 | foreach ($attachments as $attachment) { |
112 | // set default permissions | |
eaf1c8eb | 113 | $attachment->setPermissions([ |
a0c1a541 MW |
114 | 'canDownload' => false, |
115 | 'canViewPreview' => false | |
eaf1c8eb | 116 | ]); |
249da2b9 | 117 | |
a0c1a541 MW |
118 | if ($this->getObject($attachment->objectID) === null) { |
119 | $messageIDs[] = $attachment->objectID; | |
120 | } | |
121 | } | |
249da2b9 | 122 | |
a0c1a541 MW |
123 | if (!empty($messageIDs)) { |
124 | $this->cacheObjects($messageIDs); | |
125 | } | |
249da2b9 | 126 | |
a0c1a541 MW |
127 | foreach ($attachments as $attachment) { |
128 | if (($message = $this->getObject($attachment->objectID)) !== null) { | |
129 | if (!$message->getConversation()->canRead()) continue; | |
130 | ||
eaf1c8eb | 131 | $attachment->setPermissions([ |
a0c1a541 MW |
132 | 'canDownload' => true, |
133 | 'canViewPreview' => true | |
eaf1c8eb | 134 | ]); |
a0c1a541 MW |
135 | } |
136 | else if ($attachment->tmpHash != '' && $attachment->userID == WCF::getUser()->userID) { | |
eaf1c8eb | 137 | $attachment->setPermissions([ |
a0c1a541 MW |
138 | 'canDownload' => true, |
139 | 'canViewPreview' => true | |
eaf1c8eb | 140 | ]); |
a0c1a541 MW |
141 | } |
142 | } | |
143 | } | |
ce07ae0c | 144 | } |