|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
|
4
|
|
|
* * |
|
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
|
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
|
7
|
|
|
******************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Waca\Pages; |
|
10
|
|
|
|
|
11
|
|
|
use Exception; |
|
12
|
|
|
use Waca\DataObjects\Comment; |
|
13
|
|
|
use Waca\DataObjects\Request; |
|
14
|
|
|
use Waca\DataObjects\User; |
|
15
|
|
|
use Waca\Exceptions\AccessDeniedException; |
|
16
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
|
17
|
|
|
use Waca\Helpers\Logger; |
|
18
|
|
|
use Waca\SessionAlert; |
|
19
|
|
|
use Waca\Tasks\InternalPageBase; |
|
20
|
|
|
use Waca\WebRequest; |
|
21
|
|
|
|
|
22
|
|
|
class PageEditComment extends InternalPageBase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Main function for this page, when no specific actions are called. |
|
26
|
|
|
* @throws ApplicationLogicException |
|
27
|
|
|
* @throws Exception |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function main() |
|
30
|
|
|
{ |
|
31
|
|
|
$commentId = WebRequest::getInt('id'); |
|
32
|
|
|
if ($commentId === null) { |
|
33
|
|
|
throw new ApplicationLogicException('Comment ID not specified'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$database = $this->getDatabase(); |
|
37
|
|
|
|
|
38
|
|
|
/** @var Comment|false $comment */ |
|
39
|
|
|
$comment = Comment::getById($commentId, $database); |
|
40
|
|
|
if ($comment === false) { |
|
|
|
|
|
|
41
|
|
|
throw new ApplicationLogicException('Comment not found'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$currentUser = User::getCurrent($database); |
|
45
|
|
|
if ($comment->getUser() !== $currentUser->getId() && !$this->barrierTest('editOthers', $currentUser)) { |
|
46
|
|
|
throw new AccessDeniedException($this->getSecurityManager()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($comment->getVisibility() === 'admin' |
|
50
|
|
|
&& !$this->barrierTest('seeRestrictedComments', $currentUser, 'RequestData') |
|
51
|
|
|
&& $comment->getUser() !== $currentUser->getId()) { |
|
52
|
|
|
throw new AccessDeniedException($this->getSecurityManager()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($comment->getVisibility() === 'checkuser' |
|
56
|
|
|
&& !$this->barrierTest('seeCheckuserComments', $currentUser, 'RequestData') |
|
57
|
|
|
&& $comment->getUser() !== $currentUser->getId()) { |
|
58
|
|
|
throw new AccessDeniedException($this->getSecurityManager()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @var Request|false $request */ |
|
62
|
|
|
$request = Request::getById($comment->getRequest(), $database); |
|
63
|
|
|
|
|
64
|
|
|
if ($request === false) { |
|
|
|
|
|
|
65
|
|
|
throw new ApplicationLogicException('Request was not found.'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (WebRequest::wasPosted()) { |
|
69
|
|
|
$this->validateCSRFToken(); |
|
70
|
|
|
$newComment = WebRequest::postString('newcomment'); |
|
71
|
|
|
$visibility = WebRequest::postString('visibility'); |
|
72
|
|
|
|
|
73
|
|
|
if ($newComment === $comment->getComment() && ($comment->getVisibility() === 'requester' || $comment->getVisibility() === $visibility)) { |
|
74
|
|
|
// Only save and log if the comment changed |
|
75
|
|
|
$this->redirect('viewRequest', null, array('id' => $comment->getRequest())); |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($comment->getVisibility() !== 'requester') { |
|
80
|
|
|
if ($visibility !== 'user' && $visibility !== 'admin' && $visibility !== 'checkuser') { |
|
81
|
|
|
throw new ApplicationLogicException('Comment visibility is not valid'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$comment->setVisibility($visibility); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// optimistically lock from the load of the edit comment form |
|
88
|
|
|
$updateVersion = WebRequest::postInt('updateversion'); |
|
89
|
|
|
$comment->setUpdateVersion($updateVersion); |
|
90
|
|
|
|
|
91
|
|
|
$comment->setComment($newComment); |
|
92
|
|
|
|
|
93
|
|
|
$comment->save(); |
|
94
|
|
|
|
|
95
|
|
|
Logger::editComment($database, $comment, $request); |
|
96
|
|
|
$this->getNotificationHelper()->commentEdited($comment, $request); |
|
97
|
|
|
SessionAlert::success("Comment has been saved successfully"); |
|
98
|
|
|
|
|
99
|
|
|
$this->redirect('viewRequest', null, array('id' => $comment->getRequest())); |
|
100
|
|
|
} |
|
101
|
|
|
else { |
|
102
|
|
|
$this->assignCSRFToken(); |
|
103
|
|
|
$this->assign('comment', $comment); |
|
104
|
|
|
$this->assign('request', $request); |
|
105
|
|
|
$this->assign('user', User::getById($comment->getUser(), $database)); |
|
106
|
|
|
$this->setTemplate('edit-comment.tpl'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|