| 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 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->checkCommentAccess($comment, $currentUser); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         /** @var Request|false $request */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $request = Request::getById($comment->getRequest(), $database); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         if ($request === false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             throw new ApplicationLogicException('Request was not found.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $canUnflag = $this->barrierTest('unflag', $currentUser, PageFlagComment::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         if (WebRequest::wasPosted()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $this->validateCSRFToken(); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $newComment = WebRequest::postString('newcomment'); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $visibility = WebRequest::postString('visibility'); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             $doUnflag = WebRequest::postBoolean('unflag'); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             if ($newComment === null || $newComment === "") { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 throw new ApplicationLogicException("Comment cannot be empty!"); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             $commentDataUnchanged = $newComment === $comment->getComment() | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                 && ($comment->getVisibility() === 'requester' || $comment->getVisibility() === $visibility); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             $flagStatusUnchanged = (!$canUnflag || $comment->getFlagged() && !$doUnflag); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             if ($commentDataUnchanged && $flagStatusUnchanged) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                 // No change was made; redirect back. | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                 $this->redirectBack($comment->getRequest()); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                 return; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             // optimistically lock from the load of the edit comment form | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             $updateVersion = WebRequest::postInt('updateversion'); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             // comment data has changed, update the object | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             if (!$commentDataUnchanged) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 $this->updateCommentData($comment, $visibility, $newComment); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             if ($doUnflag && $canUnflag) { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                 $comment->setFlagged(false); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             $comment->setUpdateVersion($updateVersion); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             $comment->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             if (!$commentDataUnchanged) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 Logger::editComment($database, $comment, $request); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 $this->getNotificationHelper()->commentEdited($comment, $request); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             if ($doUnflag && $canUnflag) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                 Logger::unflaggedComment($database, $comment, $request->getDomain()); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             SessionAlert::success("Comment has been saved successfully"); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             $this->redirectBack($comment->getRequest()); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         else { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             $this->assignCSRFToken(); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             $this->assign('comment', $comment); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             $this->assign('request', $request); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             $this->assign('user', User::getById($comment->getUser(), $database)); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             $this->assign('canUnflag', $canUnflag); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             $this->setTemplate('edit-comment.tpl'); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * @param $comment | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * @param $currentUser | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @throws AccessDeniedException | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     protected function checkCommentAccess($comment, $currentUser): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if ($comment->getUser() !== $currentUser->getId() && !$this->barrierTest('editOthers', $currentUser)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if ($comment->getVisibility() === 'admin' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             && !$this->barrierTest('seeRestrictedComments', $currentUser, 'RequestData') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             && $comment->getUser() !== $currentUser->getId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if ($comment->getVisibility() === 'checkuser' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             && !$this->barrierTest('seeCheckuserComments', $currentUser, 'RequestData') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             && $comment->getUser() !== $currentUser->getId()) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |             throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * @param             $comment | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * @param string|null $visibility | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      * @param             $newComment | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * @throws ApplicationLogicException | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |     protected function updateCommentData(Comment $comment, ?string $visibility, string $newComment): void | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         if ($comment->getVisibility() !== 'requester') { | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |             if ($visibility !== 'user' && $visibility !== 'admin' && $visibility !== 'checkuser') { | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |                 throw new ApplicationLogicException('Comment visibility is not valid'); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |             $comment->setVisibility($visibility); | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |         $comment->setComment($newComment); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |         $comment->touchEdited(); | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     protected function redirectBack(int $requestId): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $source = WebRequest::getString("from"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         if ($source == "flagged") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $this->redirect('flaggedComments'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         } else { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  |             $this->redirect('viewRequest', null, array('id' => $requestId)); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 171 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |  |