| 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\object\type\ObjectTypeCache; |
| 8 | use wcf\data\user\ignore\UserIgnore; |
| 9 | use wcf\data\user\UserProfile; |
| 10 | use wcf\system\cache\runtime\UserProfileRuntimeCache; |
| 11 | use wcf\system\cache\runtime\UserRuntimeCache; |
| 12 | use wcf\system\cache\runtime\ViewableCommentResponseRuntimeCache; |
| 13 | use wcf\system\cache\runtime\ViewableCommentRuntimeCache; |
| 14 | use wcf\system\like\IViewableLikeProvider; |
| 15 | use wcf\system\WCF; |
| 16 | |
| 17 | /** |
| 18 | * User profile comment manager implementation. |
| 19 | * |
| 20 | * @author Alexander Ebert |
| 21 | * @copyright 2001-2019 WoltLab GmbH |
| 22 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 23 | */ |
| 24 | class UserProfileCommentManager extends AbstractCommentManager implements |
| 25 | IViewableLikeProvider, |
| 26 | ICommentPermissionManager |
| 27 | { |
| 28 | /** |
| 29 | * @inheritDoc |
| 30 | */ |
| 31 | protected $permissionAdd = 'user.profileComment.canAddComment'; |
| 32 | |
| 33 | /** |
| 34 | * @inheritDoc |
| 35 | */ |
| 36 | protected $permissionAddWithoutModeration = 'user.profileComment.canAddCommentWithoutModeration'; |
| 37 | |
| 38 | /** |
| 39 | * @inheritDoc |
| 40 | */ |
| 41 | protected $permissionCanModerate = 'mod.profileComment.canModerateComment'; |
| 42 | |
| 43 | /** |
| 44 | * @inheritDoc |
| 45 | */ |
| 46 | protected $permissionDelete = 'user.profileComment.canDeleteComment'; |
| 47 | |
| 48 | /** |
| 49 | * @inheritDoc |
| 50 | */ |
| 51 | protected $permissionEdit = 'user.profileComment.canEditComment'; |
| 52 | |
| 53 | /** |
| 54 | * @inheritDoc |
| 55 | */ |
| 56 | protected $permissionModDelete = 'mod.profileComment.canDeleteComment'; |
| 57 | |
| 58 | /** |
| 59 | * @inheritDoc |
| 60 | */ |
| 61 | protected $permissionModEdit = 'mod.profileComment.canEditComment'; |
| 62 | |
| 63 | /** |
| 64 | * @inheritDoc |
| 65 | */ |
| 66 | public function isAccessible($objectID, $validateWritePermission = false) |
| 67 | { |
| 68 | // check object id |
| 69 | $userProfile = UserProfileRuntimeCache::getInstance()->getObject($objectID); |
| 70 | if ($userProfile === null) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | // check visibility |
| 75 | if ($userProfile->isProtected()) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // check target user settings |
| 80 | if ($validateWritePermission) { |
| 81 | if ( |
| 82 | !$userProfile->isAccessible('canWriteProfileComments') |
| 83 | && $userProfile->userID != WCF::getUser()->userID |
| 84 | ) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if ($userProfile->isIgnoredUser(WCF::getUser()->userID, UserIgnore::TYPE_BLOCK_DIRECT_CONTACT)) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | #[\Override] |
| 97 | public function canModerateObject(int $objectTypeID, int $objectID, UserProfile $user): bool |
| 98 | { |
| 99 | $userProfile = UserProfileRuntimeCache::getInstance()->getObject($objectID); |
| 100 | if ($userProfile === null) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /** @see UserProfile::isProtected() */ |
| 105 | if ( |
| 106 | !( |
| 107 | $user->getPermission('admin.general.canViewPrivateUserOptions') |
| 108 | || $userProfile->isAccessible('canViewProfile', $user->userID) |
| 109 | || $userProfile->userID === $user->userID |
| 110 | ) |
| 111 | ) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | return (bool)$user->getPermission($this->permissionCanModerate); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @inheritDoc |
| 120 | */ |
| 121 | public function getLink($objectTypeID, $objectID) |
| 122 | { |
| 123 | $user = UserRuntimeCache::getInstance()->getObject($objectID); |
| 124 | if ($user) { |
| 125 | return $user->getLink(); |
| 126 | } |
| 127 | |
| 128 | return ''; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @inheritDoc |
| 133 | */ |
| 134 | public function getCommentLink(Comment $comment) |
| 135 | { |
| 136 | return $this->getLink($comment->objectTypeID, $comment->objectID) . '#wall/comment' . $comment->commentID; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @inheritDoc |
| 141 | */ |
| 142 | public function getResponseLink(CommentResponse $response) |
| 143 | { |
| 144 | return $this->getLink($response->getComment()->objectTypeID, $response->getComment()->objectID) |
| 145 | . '#wall/comment' . $response->commentID . '/response' . $response->responseID; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @inheritDoc |
| 150 | */ |
| 151 | public function getTitle($objectTypeID, $objectID, $isResponse = false) |
| 152 | { |
| 153 | if ($isResponse) { |
| 154 | return WCF::getLanguage()->get('wcf.user.profile.content.wall.commentResponse'); |
| 155 | } |
| 156 | |
| 157 | return WCF::getLanguage()->getDynamicVariable('wcf.user.profile.content.wall.comment'); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @inheritDoc |
| 162 | */ |
| 163 | public function updateCounter($objectID, $value) |
| 164 | { |
| 165 | // does nothing |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @inheritDoc |
| 170 | */ |
| 171 | public function canDeleteComment(Comment $comment) |
| 172 | { |
| 173 | if ( |
| 174 | $comment->objectID == WCF::getUser()->userID |
| 175 | && WCF::getSession()->getPermission('user.profileComment.canDeleteCommentInOwnProfile') |
| 176 | ) { |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | return parent::canDeleteComment($comment); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @inheritDoc |
| 185 | */ |
| 186 | public function canDeleteResponse(CommentResponse $response) |
| 187 | { |
| 188 | if ( |
| 189 | $response->getComment()->objectID == WCF::getUser()->userID |
| 190 | && WCF::getSession()->getPermission('user.profileComment.canDeleteCommentInOwnProfile') |
| 191 | ) { |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | return parent::canDeleteResponse($response); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @inheritDoc |
| 200 | */ |
| 201 | public function prepare(array $likes) |
| 202 | { |
| 203 | if (!WCF::getSession()->getPermission('user.profile.canViewUserProfile')) { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | $commentLikeObjectType = ObjectTypeCache::getInstance() |
| 208 | ->getObjectTypeByName('com.woltlab.wcf.like.likeableObject', 'com.woltlab.wcf.comment'); |
| 209 | |
| 210 | $commentIDs = $responseIDs = []; |
| 211 | foreach ($likes as $like) { |
| 212 | if ($like->objectTypeID == $commentLikeObjectType->objectTypeID) { |
| 213 | $commentIDs[] = $like->objectID; |
| 214 | } else { |
| 215 | $responseIDs[] = $like->objectID; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // fetch response |
| 220 | $userIDs = $responses = []; |
| 221 | if (!empty($responseIDs)) { |
| 222 | $responses = ViewableCommentResponseRuntimeCache::getInstance()->getObjects($responseIDs); |
| 223 | |
| 224 | foreach ($responses as $response) { |
| 225 | $commentIDs[] = $response->commentID; |
| 226 | if ($response->userID) { |
| 227 | $userIDs[] = $response->userID; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // fetch comments |
| 233 | $comments = ViewableCommentRuntimeCache::getInstance()->getObjects($commentIDs); |
| 234 | |
| 235 | // fetch users |
| 236 | $users = []; |
| 237 | foreach ($comments as $comment) { |
| 238 | $userIDs[] = $comment->objectID; |
| 239 | if ($comment->userID) { |
| 240 | $userIDs[] = $comment->userID; |
| 241 | } |
| 242 | } |
| 243 | if (!empty($userIDs)) { |
| 244 | $users = UserProfileRuntimeCache::getInstance()->getObjects(\array_unique($userIDs)); |
| 245 | } |
| 246 | |
| 247 | // set message |
| 248 | foreach ($likes as $like) { |
| 249 | if ($like->objectTypeID == $commentLikeObjectType->objectTypeID) { |
| 250 | // comment like |
| 251 | if (isset($comments[$like->objectID])) { |
| 252 | $comment = $comments[$like->objectID]; |
| 253 | |
| 254 | if (isset($users[$comment->objectID]) && !$users[$comment->objectID]->isProtected()) { |
| 255 | $like->setIsAccessible(); |
| 256 | |
| 257 | // short output |
| 258 | $text = WCF::getLanguage()->getDynamicVariable( |
| 259 | 'wcf.like.title.com.woltlab.wcf.user.profileComment', |
| 260 | [ |
| 261 | 'commentAuthor' => $comment->userID ? $users[$comment->userID] : null, |
| 262 | 'comment' => $comment, |
| 263 | 'user' => $users[$comment->objectID], |
| 264 | 'reaction' => $like, |
| 265 | // @deprecated 5.3 Use `$reaction` instead |
| 266 | 'like' => $like, |
| 267 | ] |
| 268 | ); |
| 269 | $like->setTitle($text); |
| 270 | |
| 271 | // output |
| 272 | $like->setDescription($comment->getExcerpt()); |
| 273 | } |
| 274 | } |
| 275 | } else { |
| 276 | // response like |
| 277 | if (isset($responses[$like->objectID])) { |
| 278 | $response = $responses[$like->objectID]; |
| 279 | $comment = $comments[$response->commentID]; |
| 280 | |
| 281 | if (isset($users[$comment->objectID]) && !$users[$comment->objectID]->isProtected()) { |
| 282 | $like->setIsAccessible(); |
| 283 | |
| 284 | // short output |
| 285 | $text = WCF::getLanguage()->getDynamicVariable( |
| 286 | 'wcf.like.title.com.woltlab.wcf.user.profileComment.response', |
| 287 | [ |
| 288 | 'responseAuthor' => $response->userID ? $users[$response->userID] : null, |
| 289 | 'commentAuthor' => $comment->userID ? $users[$comment->userID] : null, |
| 290 | 'user' => $users[$comment->objectID], |
| 291 | 'reaction' => $like, |
| 292 | // @deprecated 5.3 Use `$reaction` instead |
| 293 | 'like' => $like, |
| 294 | 'response' => $response, |
| 295 | ] |
| 296 | ); |
| 297 | $like->setTitle($text); |
| 298 | |
| 299 | // output |
| 300 | $like->setDescription($response->getExcerpt()); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |