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