PageConfirmEmail   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 42
c 1
b 0
f 0
dl 0
loc 93
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C main() 0 86 14
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) {
0 ignored issues
show
introduced by
The condition $request === false is always false.
Loading history...
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') {
0 ignored issues
show
Bug introduced by
The method getEmailConfirm() does not exist on Waca\DataObject. It seems like you code against a sub-type of Waca\DataObject such as Waca\DataObjects\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            if ($request->/** @scrutinizer ignore-call */ getEmailConfirm() === 'Confirmed') {
Loading history...
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
        $userAgent = WebRequest::userAgent();
89
        if ($userAgent !== null) {
90
            RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_USERAGENT, $userAgent);
91
        }
92
93
        $xffProvider = $this->getXffTrustProvider();
94
        $trustedIp = $xffProvider->getTrustedClientIp(WebRequest::remoteAddress(), WebRequest::forwardedAddress());
95
96
        if (filter_var($trustedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
97
            RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_IPV4, $trustedIp);
98
        }
99
        elseif (filter_var($trustedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
100
            RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_IPV6, $trustedIp);
101
        }
102
103
        foreach ($this->getSiteConfiguration()->getAcceptClientHints() as $header) {
104
            $value = WebRequest::httpHeader($header);
105
106
            if ($value === null) {
107
                continue;
108
            }
109
110
            RequestData::saveForRequest($request,
111
                RequestData::TYPE_CONFIRM_CLIENTHINT, $value, $header);
112
        }
113
114
        $this->redirect('requestSubmitted');
115
    }
116
}