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