reset_password.php ➔ reset_password()   C
last analyzed

Complexity

Conditions 9
Paths 24

Size

Total Lines 77
Code Lines 41

Duplication

Lines 20
Ratio 25.97 %

Importance

Changes 0
Metric Value
cc 9
eloc 41
nc 24
nop 0
dl 20
loc 77
rs 5.7699
c 0
b 0
f 0

How to fix   Long Method   

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
 * Reset 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 reset_password()
26
{
27
    $app = \Slim\Slim::getInstance();
28
    $final_global_template_vars = $app->config('final_global_template_vars');
29
    require_once $final_global_template_vars["absolute_path_to_this_module"] . "/models/register_account.class.php";
30
    require_once $_SERVER["PATH_TO_VENDOR"] . "phpmailer/phpmailer/PHPMailerAutoload.php";
31
    $db_conn = new \PHPSkeleton\models\db($final_global_template_vars["db_connection"]);
32
    $db_resource = $db_conn->get_resource();
33
    $register_account = new \PHPSkeleton\RegisterAccount($db_resource, $final_global_template_vars["session_key"]);
34
    $mail = new PHPMailer();
35
    $posted_data = $app->request()->post() ? $app->request()->post() : false;
36
    $account_email_exists = false;
37
38
    // Is the email address in the database?
39 View Code Duplication
    if ($posted_data) {
40
        $account_email_exists = $register_account->account_email_exists($posted_data["user_account_email"]);
41
        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...
42
            $app->flash('message', 'The entered email address was not found in our database.');
43
            $app->redirect($final_global_template_vars["path_to_this_module"]."/password/");
44
        }
45
    }
46
47
    // If there are no errors, process posted data and email to user
48
    if ($account_email_exists && $posted_data) {
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...
49
50
        $emailed_hash = md5(rand(0, 1000));
51
        // Attempt to update the emailed_hash and set account to inactive (returns boolean)
52
        $updated = $register_account->update_emailed_hash($account_email_exists['user_account_id'], $emailed_hash);
53
54
        if ($updated) {
55
            
56
            // Prepare the email...
57
            // The email subject.
58
            $subject = 'Reset Password';
59
            // The message, including the link.
60
            $message = '<h2>Reset Your Password</h2>
61
            <hr>
62
            <p>Please click this link to reset your password:<br />
63
            <a href="http://'.$_SERVER["SERVER_NAME"].'/user_account/reset/?user_account_email='.$account_email_exists['user_account_email'].'&emailed_hash='.$emailed_hash.'">http://'.$_SERVER["SERVER_NAME"].'/user_account/reset/?user_account_email='.$account_email_exists['user_account_email'].'&emailed_hash='.$emailed_hash.'</a></p>';
64
65
            // For the ability to send emails from an AWS EC2 instance...
66
            // If you need this functionality, you can configure the settings accordingly in /default_global_settings.php
67 View Code Duplication
            if ($final_global_template_vars["hosting_vendor"] && ($final_global_template_vars["hosting_vendor"] == "aws_ec2")) {
68
69
                $email = array();
70
                require_once $final_global_template_vars["path_to_smtp_settings"];
71
                // SMTP Settings
72
                $mail->IsSMTP();
73
                $mail->SMTPAuth   = $email['settings']['smtpauth'];
74
                $mail->SMTPSecure = $email['settings']['smtpsecure'];
75
                $mail->Host       = $email['settings']['host'];
76
                $mail->Username   = $email['settings']['username'];
77
                $mail->Password   = $email['settings']['password'];
78
79
            }
80
81
            // From (verified email address).
82
            $mail->SetFrom($final_global_template_vars["send_emails_from"], $final_global_template_vars["site_name"].' Accounts');
83
            // Subject
84
            $mail->Subject = $subject;
85
            // Message
86
            $mail->MsgHTML($message);
87
            // Recipient
88
            $mail->AddAddress($posted_data['user_account_email']);
89
            // Send the email.
90
            $mail->Send();
91
92
            $app->flash('message', 'Thank you. Further instructions are being sent to your email address.');
93
        }
94
        else
95
        {
96
            $app->flash('message', 'Processing failed.');
97
        }
98
99
        $app->redirect($final_global_template_vars["path_to_this_module"]."/password/");
100
    }
101
}
102