Conditions | 14 |
Paths | 576 |
Total Lines | 114 |
Code Lines | 71 |
Lines | 32 |
Ratio | 28.07 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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) { |
||
|
|||
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) { |
||
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 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
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: