Completed
Branch newinternal (113bb8)
by Michael
05:12
created

PageForgotPassword::main()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 18
cp 0
rs 9.2222
cc 6
nc 3
nop 0
crap 42
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 Waca\DataObjects\User;
12
use Waca\Exceptions\ApplicationLogicException;
13
use Waca\PdoDatabase;
14
use Waca\SessionAlert;
15
use Waca\Tasks\InternalPageBase;
16
use Waca\WebRequest;
17
18
class PageForgotPassword extends InternalPageBase
19
{
20
    /**
21
     * Main function for this page, when no specific actions are called.
22
     *
23
     * This is the forgotten password reset form
24
     * @category Security-Critical
25
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
26
    protected function main()
27
    {
28
        if (WebRequest::wasPosted()) {
29
            $this->validateCSRFToken();
30
            $username = WebRequest::postString('username');
31
            $email = WebRequest::postEmail('email');
32
            $database = $this->getDatabase();
33
34
            if ($username === null || trim($username) === "" || $email === null || trim($email) === "") {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
35
                throw new ApplicationLogicException("Both username and email address must be specified!");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Both username and email address must be specified! does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
36
            }
37
38
            $user = User::getByUsername($username, $database);
39
            $this->sendResetMail($user, $email);
40
41
            SessionAlert::success('<strong>Your password reset request has been completed.</strong> Please check your e-mail.');
42
43
            $this->redirect('login');
44
        }
45
        else {
46
            $this->assignCSRFToken();
47
            $this->setTemplate('forgot-password/forgotpw.tpl');
48
        }
49
    }
0 ignored issues
show
Coding Style introduced by
Expected //end main()
Loading history...
50
51
    /**
52
     * Sends a reset email if the user is authenticated
53
     *
54
     * @param User|boolean $user  The user located from the database, or false. Doesn't really matter, since we do the
55
     *                            check anyway within this method and silently skip if we don't have a user.
56
     * @param string       $email The provided email address
57
     */
58
    private function sendResetMail($user, $email)
59
    {
60
        // If the user isn't found, or the email address is wrong, skip sending the details silently.
61
        if (!$user instanceof User) {
62
            return;
63
        }
64
65
        if (strtolower($user->getEmail()) === strtolower($email)) {
66
            $clientIp = $this->getXffTrustProvider()
67
                ->getTrustedClientIp(WebRequest::remoteAddress(), WebRequest::forwardedAddress());
68
69
            $this->assign("user", $user);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal user does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
70
            $this->assign("hash", $user->getForgottenPasswordHash());
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal hash does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
71
            $this->assign("remoteAddress", $clientIp);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal remoteAddress does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
72
73
            $emailContent = $this->fetchTemplate('forgot-password/reset-mail.tpl');
74
75
            $this->getEmailHelper()->sendMail($user->getEmail(), "", $emailContent);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
76
        }
77
    }
0 ignored issues
show
Coding Style introduced by
Expected //end sendResetMail()
Loading history...
78
79
    /**
80
     * Entry point for the reset action
81
     *
82
     * This is the reset password part of the form.
83
     * @category Security-Critical
84
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
85
    protected function reset()
86
    {
87
        $si = WebRequest::getString('si');
88
        $id = WebRequest::getString('id');
89
90
        if ($si === null || trim($si) === "" || $id === null || trim($id) === "") {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
91
            throw new ApplicationLogicException("Link not valid, please ensure it has copied correctly");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Link not valid, please e...it has copied correctly does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
92
        }
93
94
        $database = $this->getDatabase();
95
        $user = $this->getResettingUser($id, $database, $si);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $id of Waca\Pages\PageForgotPassword::getResettingUser(). ( Ignorable by Annotation )

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

95
        $user = $this->getResettingUser(/** @scrutinizer ignore-type */ $id, $database, $si);
Loading history...
96
97
        // Dual mode
98
        if (WebRequest::wasPosted()) {
99
            $this->validateCSRFToken();
100
            try {
101
                $this->doReset($user);
102
            }
103
            catch (ApplicationLogicException $ex) {
104
                SessionAlert::error($ex->getMessage());
105
                $this->redirect('forgotPassword', 'reset', array('si' => $si, 'id' => $id));
106
107
                return;
108
            }
109
        }
110
        else {
111
            $this->assignCSRFToken();
112
            $this->assign('user', $user);
113
            $this->setTemplate('forgot-password/forgotpwreset.tpl');
114
        }
115
    }
0 ignored issues
show
Coding Style introduced by
Expected //end reset()
Loading history...
116
117
    /**
118
     * Gets the user resetting their password from the database, or throwing an exception if that is not possible.
119
     *
120
     * @param integer     $id       The ID of the user to retrieve
121
     * @param PdoDatabase $database The database object to use
122
     * @param string      $si       The reset hash provided
123
     *
124
     * @return User
125
     * @throws ApplicationLogicException
126
     */
127
    private function getResettingUser($id, $database, $si)
128
    {
129
        $user = User::getById($id, $database);
130
131
        if ($user === false || $user->getForgottenPasswordHash() !== $si || $user->isCommunityUser()) {
132
            throw new ApplicationLogicException("User not found");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal User not found does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
133
        }
134
135
        return $user;
136
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getResettingUser()
Loading history...
137
138
    /**
139
     * Performs the setting of the new password
140
     *
141
     * @param User $user The user to set the password for
142
     *
143
     * @throws ApplicationLogicException
144
     */
145
    private function doReset(User $user)
146
    {
147
        $pw = WebRequest::postString('pw');
148
        $pw2 = WebRequest::postString('pw2');
149
150
        if ($pw !== $pw2) {
151
            throw new ApplicationLogicException('Passwords do not match!');
152
        }
153
154
        $user->setPassword($pw);
155
        $user->save();
156
157
        SessionAlert::success('You may now log in!');
158
        $this->redirect('login');
159
    }
0 ignored issues
show
Coding Style introduced by
Expected //end doReset()
Loading history...
160
161
    protected function isProtectedPage()
162
    {
163
        return false;
164
    }
0 ignored issues
show
Coding Style introduced by
Expected //end isProtectedPage()
Loading history...
165
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
166