update_password.php ➔ update_password()   F
last analyzed

Complexity

Conditions 14
Paths 576

Size

Total Lines 114
Code Lines 71

Duplication

Lines 32
Ratio 28.07 %

Importance

Changes 0
Metric Value
cc 14
eloc 71
nc 576
nop 0
dl 32
loc 114
rs 2.5831
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * The PHP Skeleton App
4
 *
5
 * @author      Goran Halusa <[email protected]>
6
 * @copyright   2015 Goran Halusa
7
 * @link        https://github.com/ghalusa/PHP-Skeleton-App
8
 * @license     https://github.com/ghalusa/PHP-Skeleton-App/wiki/License
9
 * @version     0.1.1
10
 * @package     PHP Skeleton App
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 */
15
16
/**
17
 * Update Password
18
 *
19
 * Controller for the User Account module.
20
 *
21
 * @author      Goran Halusa <[email protected]>
22
 * @since       0.1.0
23
 */
24
25
function update_password()
26
{
27
    $app = \Slim\Slim::getInstance();
28
    $final_global_template_vars = $app->config('final_global_template_vars');
29
    require_once $_SERVER["PATH_TO_VENDOR"] . "wixel/gump/gump.class.php";
30
    require_once $final_global_template_vars["absolute_path_to_this_module"] . "/models/register_account.class.php";
31
    require_once $final_global_template_vars["default_module_list"]["authenticate"]["absolute_path_to_this_module"] . "/models/authenticate.class.php";
32
    require_once $_SERVER["PATH_TO_VENDOR"] . "phpmailer/phpmailer/PHPMailerAutoload.php";
33
    $db_conn = new \PHPSkeleton\models\db($final_global_template_vars["db_connection"]);
34
    $db_resource = $db_conn->get_resource();
35
    $register_account = new \PHPSkeleton\RegisterAccount($db_resource, $final_global_template_vars["session_key"]);
36
    $authenticate = new \PHPSkeleton\Authenticate($db_resource, $final_global_template_vars["session_key"]);
37
    $gump = new GUMP();
38
    $mail = new PHPMailer();
39
    $post = $app->request()->post() ? $app->request()->post() : false;
40
    $account_email_exists = false;
41
42
    // Is the email address in the database?
43 View Code Duplication
    if ($post) {
44
        $account_email_exists = $register_account->account_email_exists($post["user_account_email"]);
45
46
        if (!$account_email_exists) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $account_email_exists of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
47
            $app->flash('message', 'The entered email address was not found in our database.');
48
            $app->redirect($final_global_template_vars["path_to_this_module"]."/password/");
49
        }
50
    }
51
52
    $rules = array();
53
54
    if ($account_email_exists) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $account_email_exists of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
55
        $rules = array(
56
            "user_account_password" => "required|max_len,100|min_len,6"
57
            ,"password_check" => "required|max_len,100|min_len,6"
58
        );
59
    }
60
61
    $validated = $gump->validate($post, $rules);
62
63 View Code Duplication
    if ($post["user_account_password"] != $post["password_check"]) {
64
        $validated_password_check = array(
65
            "field" => "user_account_password_check"
66
            ,"value" => null
67
            ,"rule" => "validate_required"
68
        );
69
        if (is_array($validated)) {
70
            array_push($validated, $validated_password_check);
71
        } else {
72
            $validated = array($validated_password_check);
73
        }
74
    }
75
76
    $errors = array();
77
    if ($validated !== true) {
78
        $errors = \phpskeleton\models\utility::gump_parse_errors($validated);
79
    }
80
81
    if (isset($errors["user_account_password_check"])) {
82
        $errors["user_account_password_check"] = "Passwords did not match.";
83
    }
84
85
    // If there are no errors, process posted data and email to user
86
    if (empty($errors) && $post) {
87
        // Attempt to update the user_account_password and set the account to active (returns boolean)
88
        $updated = $register_account->update_password(
89
            $authenticate->generate_hashed_password($post["user_account_password"]),
90
            $account_email_exists['user_account_id'],
91
            $post["emailed_hash"]
92
        );
93
94
        if ($updated) {
95
            // Prepare the email...
96
            // The email subject.
97
            $subject = 'Your Password Has Been Reset';
98
            // The message.
99
            $message = '<h2>Your Password Has Been Reset</h2>
100
            <hr>
101
            <p>If you did not execute this change, please contact the site administrator as soon as possible.</p>';
102
103
            // For the ability to send emails from an AWS EC2 instance
104
            // If you need this functionality, you can configure the settings accordingly in /default_global_settings.php
105 View Code Duplication
            if ($final_global_template_vars["hosting_vendor"] && ($final_global_template_vars["hosting_vendor"] == "aws_ec2")) {
106
                $email = array();
107
                require_once($final_global_template_vars["path_to_smtp_settings"]);
108
                // SMTP Settings
109
                $mail = new PHPMailer();
110
                $mail->IsSMTP();
111
                $mail->SMTPAuth   = $email['settings']['smtpauth'];
112
                $mail->SMTPSecure = $email['settings']['smtpsecure'];
113
                $mail->Host       = $email['settings']['host'];
114
                $mail->Username   = $email['settings']['username'];
115
                $mail->Password   = $email['settings']['password'];
116
            }
117
118
            // From (verified email address).
119
            $mail->SetFrom($final_global_template_vars["send_emails_from"], $final_global_template_vars["site_name"].' Accounts');
120
            // Subject
121
            $mail->Subject = $subject;
122
            $mail->MsgHTML($message);
123
            // Recipient
124
            $mail->AddAddress($post['user_account_email']);
125
            // Send the email.
126
            $mail->Send();
127
128
            $app->flash('message', 'Your password has been reset.');
129
            $app->redirect($final_global_template_vars["path_to_this_module"]."/password/");
130
        } else {
131
            $app->flash('message', 'Processing failed.');
132
            $app->redirect($final_global_template_vars["path_to_this_module"]."/password/");
133
        }
134
    } else {
135
        $app->flash('message', $errors["user_account_password"]);
136
        $app->redirect($final_global_template_vars["path_to_this_module"]."/reset/?user_account_email=".$account_email_exists['user_account_email']."&emailed_hash=".$post["emailed_hash"]);
137
    }
138
}
139