Issues (195)

includes/Pages/Request/PageConfirmEmail.php (2 issues)

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\Request;
10
11
use Exception;
12
use Waca\DataObjects\Request;
13
use Waca\Exceptions\ApplicationLogicException;
14
use Waca\Exceptions\OptimisticLockFailedException;
15
use Waca\Helpers\Logger;
16
use Waca\RequestStatus;
17
use Waca\Tasks\PublicInterfacePageBase;
18
use Waca\WebRequest;
19
20
class PageConfirmEmail extends PublicInterfacePageBase
21
{
22
    /**
23
     * Main function for this page, when no specific actions are called.
24
     * @throws ApplicationLogicException
25
     * @throws Exception
26
     */
27
    protected function main()
28
    {
29
        $id = WebRequest::getInt('id');
30
        $si = WebRequest::getString('si');
31
32
        if ($id === null || $si === null) {
33
            throw new ApplicationLogicException('Link incomplete - please double check the link you received.');
34
        }
35
36
        /** @var Request|false $request */
37
        $request = Request::getById($id, $this->getDatabase());
38
39
        if ($request === false) {
0 ignored issues
show
The condition $request === false is always false.
Loading history...
40
            throw new ApplicationLogicException('Request not found');
41
        }
42
43
        if ($request->getEmailConfirm() === 'Confirmed') {
44
            // request has already been confirmed. Bomb out silently.
45
            $this->redirect('requestSubmitted');
46
47
            return;
48
        }
49
50
        if ($request->getEmailConfirm() === $si) {
51
            $request->setEmailConfirm('Confirmed');
52
        }
53
        else {
54
            throw new ApplicationLogicException('The confirmation value does not appear to match the expected value');
55
        }
56
57
        try {
58
            $request->save();
59
        }
60
        catch (OptimisticLockFailedException $ex) {
61
            // Okay. Someone's edited this in the time between us loading this page and doing the checks, and us getting
62
            // to saving the page. We *do not* want to show an optimistic lock failure, the most likely problem is they
63
            // double-loaded this page (see #255). Let's confirm this, and bomb out with a success message if it's the
64
            // case.
65
66
            $request = Request::getById($id, $this->getDatabase());
67
            if ($request->getEmailConfirm() === 'Confirmed') {
0 ignored issues
show
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

67
            if ($request->/** @scrutinizer ignore-call */ getEmailConfirm() === 'Confirmed') {
Loading history...
68
                // we've already done the sanity checks above
69
70
                $this->redirect('requestSubmitted');
71
72
                // skip the log and notification
73
                return;
74
            }
75
76
            // something really weird happened. Another race condition?
77
            throw $ex;
78
        }
79
80
        Logger::emailConfirmed($this->getDatabase(), $request);
81
82
        if ($request->getStatus() != RequestStatus::CLOSED) {
83
            $this->getNotificationHelper()->requestReceived($request);
84
        }
85
86
        $this->redirect('requestSubmitted');
87
    }
88
}