| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /****************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Wikipedia Account Creation Assistance tool                                 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * ACC Development Team. Please see team.json for a list of contributors.     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  *                                                                            * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This is free and unencumbered software released into the public domain.    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Please see LICENSE.md for the full licencing statement.                    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  ******************************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Waca\Pages\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Waca\DataObjects\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Waca\DataObjects\RequestData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Waca\Exceptions\ApplicationLogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Waca\Exceptions\OptimisticLockFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Waca\Helpers\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Waca\RequestStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Waca\Tasks\PublicInterfacePageBase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Waca\WebRequest; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | class PageConfirmEmail extends PublicInterfacePageBase | 
            
                                                                        
                            
            
                                    
            
            
                | 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 |  |  |         $id = WebRequest::getInt('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $si = WebRequest::getString('si'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         if ($id === null || $si === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             throw new ApplicationLogicException('Link incomplete - please double check the link you received.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         /** @var Request|false $request */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $request = Request::getById($id, $this->getDatabase()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if ($request === false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             throw new ApplicationLogicException('Request not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if ($request->getEmailConfirm() === 'Confirmed') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             // request has already been confirmed. Bomb out silently. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $this->redirect('requestSubmitted'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if ($request->getEmailConfirm() === $si) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $request->setEmailConfirm('Confirmed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             throw new ApplicationLogicException('The confirmation value does not appear to match the expected value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $request->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         catch (OptimisticLockFailedException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             // Okay. Someone's edited this in the time between us loading this page and doing the checks, and us getting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             // to saving the page. We *do not* want to show an optimistic lock failure, the most likely problem is they | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             // double-loaded this page (see #255). Let's confirm this, and bomb out with a success message if it's the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             // case. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $request = Request::getById($id, $this->getDatabase()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             if ($request->getEmailConfirm() === 'Confirmed') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 // we've already done the sanity checks above | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $this->redirect('requestSubmitted'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 // skip the log and notification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             // something really weird happened. Another race condition? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             throw $ex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         Logger::emailConfirmed($this->getDatabase(), $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if ($request->getStatus() != RequestStatus::CLOSED) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $this->getNotificationHelper()->requestReceived($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         RequestData::saveForRequest($request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             RequestData::TYPE_CONFIRM_USERAGENT, WebRequest::userAgent()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $xffProvider = $this->getXffTrustProvider(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $trustedIp = $xffProvider->getTrustedClientIp(WebRequest::remoteAddress(), WebRequest::forwardedAddress()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if (filter_var($trustedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_IPV4, $trustedIp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         elseif (filter_var($trustedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_IPV6, $trustedIp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         foreach ($this->getSiteConfiguration()->getAcceptClientHints() as $header) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $value = WebRequest::httpHeader($header); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             if ($value === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             RequestData::saveForRequest($request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 RequestData::TYPE_CONFIRM_CLIENTHINT, $value, $header); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  |         $this->redirect('requestSubmitted'); | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  | } |