Merge branch '5.2' into 5.3
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / comment / manager / AbstractCommentManager.class.php
CommitLineData
285b1d92
MW
1<?php
2namespace wcf\system\comment\manager;
3use wcf\data\comment\response\CommentResponse;
4use wcf\data\comment\Comment;
9098dfea 5use wcf\data\DatabaseObjectDecorator;
fa7ebf51 6use wcf\system\bbcode\BBCodeHandler;
285b1d92
MW
7use wcf\system\SingletonFactory;
8use wcf\system\WCF;
9
10/**
11 * Default implementation for comment managers.
12 *
13 * @author Alexander Ebert
7b7b9764 14 * @copyright 2001-2019 WoltLab GmbH
285b1d92 15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 16 * @package WoltLabSuite\Core\System\Comment\Manager
285b1d92
MW
17 */
18abstract class AbstractCommentManager extends SingletonFactory implements ICommentManager {
19 /**
20 * display comments per page
21 * @var integer
22 */
9d007b40 23 public $commentsPerPage = 30;
285b1d92
MW
24
25 /**
26 * permission name for comment/response creation
27 * @var string
28 */
29 protected $permissionAdd = '';
30
d8684224
AE
31 /**
32 * permission name for comment/response creation without approval
33 * @var string
34 */
35 protected $permissionAddWithoutModeration = '';
36
285b1d92
MW
37 /**
38 * permission name for comment/response moderation
39 * @var string
40 */
41 protected $permissionCanModerate = '';
42
43 /**
44 * permission name for deletion of own comments/responses
45 * @var string
46 */
47 protected $permissionDelete = '';
48
49 /**
50 * permission name for editing of own comments/responses
51 * @var string
52 */
53 protected $permissionEdit = '';
54
55 /**
56 * permission name for deletion of comments/responses (moderator)
57 * @var string
58 */
59 protected $permissionModDelete = '';
60
61 /**
62 * permission name for editing of comments/responses (moderator)
63 * @var string
64 */
65 protected $permissionModEdit = '';
66
fa7ebf51
AE
67 /**
68 * permission name for the list of disallowed bbcodes
69 * @var string
70 */
71 protected $permissionDisallowedBBCodes = 'user.comment.disallowedBBCodes';
72
285b1d92 73 /**
0fcfe5f6 74 * @inheritDoc
285b1d92
MW
75 */
76 public function canAdd($objectID) {
43535295 77 if (VISITOR_USE_TINY_BUILD && !WCF::getUser()->userID) {
eec1d83c
AE
78 return false;
79 }
80
285b1d92
MW
81 if (!$this->isAccessible($objectID, true)) {
82 return false;
83 }
84
85 return (WCF::getSession()->getPermission($this->permissionAdd) ? true : false);
86 }
87
d8684224
AE
88 /**
89 * @inheritDoc
90 */
91 public function canAddWithoutApproval($objectID) {
43535295 92 if (VISITOR_USE_TINY_BUILD && !WCF::getUser()->userID) {
eec1d83c
AE
93 return false;
94 }
95
d8684224 96 if (empty($this->permissionAddWithoutModeration)) {
87202a16
AE
97 if (ENABLE_DEBUG_MODE) {
98 throw new \RuntimeException("Missing permission name to create comments without approval.");
99 }
100
101 // backwards-compatibility in production mode
102 return true;
d8684224
AE
103 }
104
105 return (WCF::getSession()->getPermission($this->permissionAddWithoutModeration) ? true : false);
106 }
107
fa7ebf51
AE
108 /**
109 * @inheritDoc
110 */
111 public function setDisallowedBBCodes() {
112 BBCodeHandler::getInstance()->setDisallowedBBCodes(explode(',', WCF::getSession()->getPermission($this->permissionDisallowedBBCodes)));
113 }
114
285b1d92 115 /**
0fcfe5f6 116 * @inheritDoc
285b1d92
MW
117 */
118 public function canEditComment(Comment $comment) {
63b9817b 119 return $this->canEdit($comment->userID == WCF::getUser()->userID);
285b1d92
MW
120 }
121
122 /**
0fcfe5f6 123 * @inheritDoc
285b1d92
MW
124 */
125 public function canEditResponse(CommentResponse $response) {
63b9817b 126 return $this->canEdit($response->userID == WCF::getUser()->userID);
285b1d92
MW
127 }
128
129 /**
0fcfe5f6 130 * @inheritDoc
285b1d92
MW
131 */
132 public function canDeleteComment(Comment $comment) {
63b9817b 133 return $this->canDelete($comment->userID == WCF::getUser()->userID);
285b1d92
MW
134 }
135
136 /**
0fcfe5f6 137 * @inheritDoc
285b1d92
MW
138 */
139 public function canDeleteResponse(CommentResponse $response) {
63b9817b 140 return $this->canDelete($response->userID == WCF::getUser()->userID);
285b1d92
MW
141 }
142
143 /**
0fcfe5f6 144 * @inheritDoc
285b1d92
MW
145 */
146 public function canModerate($objectTypeID, $objectID) {
147 return (WCF::getSession()->getPermission($this->permissionCanModerate) ? true : false);
148 }
149
150 /**
151 * Returns true if the current user may edit a comment/response.
152 *
153 * @param boolean $isOwner
154 * @return boolean
155 */
156 protected function canEdit($isOwner) {
157 // disallow guests
158 if (!WCF::getUser()->userID) {
159 return false;
160 }
161
162 // check moderator permission
163 if (WCF::getSession()->getPermission($this->permissionModEdit)) {
164 return true;
165 }
166
167 // check user permission and ownership
168 if ($isOwner && WCF::getSession()->getPermission($this->permissionEdit)) {
169 return true;
170 }
171
172 return false;
173 }
174
175 /**
176 * Returns true if the current user may delete a comment/response.
177 *
178 * @param boolean $isOwner
179 * @return boolean
180 */
181 protected function canDelete($isOwner) {
182 // disallow guests
183 if (!WCF::getUser()->userID) {
184 return false;
185 }
186
187 // check moderator permission
188 if (WCF::getSession()->getPermission($this->permissionModDelete)) {
189 return true;
190 }
191
192 // check user permission and ownership
193 if ($isOwner && WCF::getSession()->getPermission($this->permissionDelete)) {
194 return true;
195 }
196
197 return false;
198 }
199
200 /**
0fcfe5f6 201 * @inheritDoc
285b1d92
MW
202 */
203 public function getCommentsPerPage() {
204 return $this->commentsPerPage;
205 }
166d2b91
MW
206
207 /**
0fcfe5f6 208 * @inheritDoc
166d2b91
MW
209 */
210 public function supportsLike() {
211 return true;
212 }
213
214 /**
0fcfe5f6 215 * @inheritDoc
166d2b91
MW
216 */
217 public function supportsReport() {
218 return true;
219 }
d02542af
MS
220
221 /**
222 * @inheritDoc
223 */
224 public function getCommentLink(Comment $comment) {
225 return $this->getLink($comment->objectTypeID, $comment->objectID) . '#comment' . $comment->commentID;
226 }
227
228 /**
229 * @inheritDoc
230 */
231 public function getResponseLink(CommentResponse $response) {
232 return $this->getLink($response->getComment()->objectTypeID, $response->getComment()->objectID)
233 . '#comment' . $response->commentID . '/response' . $response->responseID;
234 }
5726d6d8
TD
235
236 /**
237 * @inheritDoc
238 */
239 public function isContentAuthor($commentOrResponse) {
240 return false;
241 }
9098dfea
TD
242
243 /**
244 * Returns the object ID for the given Comment or CommentResponse.
245 *
246 * @return integer
247 */
248 protected final function getObjectID($commentOrResponse) {
249 if ($commentOrResponse instanceof CommentResponse || ($commentOrResponse instanceof DatabaseObjectDecorator && $commentOrResponse->getDecoratedObject() instanceof CommentResponse)) {
250 return $commentOrResponse->getComment()->objectID;
251 }
252 else {
253 return $commentOrResponse->objectID;
254 }
255 }
285b1d92 256}