| Conditions | 20 |
| Paths | 800 |
| Total Lines | 176 |
| Code Lines | 99 |
| Lines | 38 |
| Ratio | 21.59 % |
| 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 insert_user_account() |
||
| 26 | { |
||
| 27 | $app = \Slim\Slim::getInstance(); |
||
| 28 | $env = $app->environment(); |
||
| 29 | $final_global_template_vars = $app->config('final_global_template_vars'); |
||
| 30 | require_once $_SERVER["PATH_TO_VENDOR"] . "wixel/gump/gump.class.php"; |
||
| 31 | require_once $final_global_template_vars["absolute_path_to_this_module"] . "/models/user_account.class.php"; |
||
| 32 | require_once $final_global_template_vars["absolute_path_to_this_module"] . "/models/register_account.class.php"; |
||
| 33 | require_once $final_global_template_vars["default_module_list"]["authenticate"]["absolute_path_to_this_module"] . "/models/authenticate.class.php"; |
||
| 34 | require_once $_SERVER["PATH_TO_VENDOR"] . "phpmailer/phpmailer/PHPMailerAutoload.php"; |
||
| 35 | $db_conn = new \PHPSkeleton\models\db($final_global_template_vars["db_connection"]); |
||
| 36 | $db_resource = $db_conn->get_resource(); |
||
| 37 | $useraccount = new \PHPSkeleton\UserAccount($db_resource, $final_global_template_vars["session_key"]); |
||
| 38 | $register_account = new \PHPSkeleton\RegisterAccount($db_resource, $final_global_template_vars["session_key"]); |
||
| 39 | $authenticate = new \PHPSkeleton\Authenticate($db_resource, $final_global_template_vars["session_key"]); |
||
| 40 | $gump = new GUMP(); |
||
| 41 | $mail = new PHPMailer(); |
||
| 42 | $errors = false; |
||
| 43 | |||
| 44 | $posted_data = $app->request()->post() ? $app->request()->post() : false; |
||
| 45 | |||
| 46 | $account_email_exists = $register_account->account_email_exists($posted_data["user_account_email"]); |
||
| 47 | if ($account_email_exists) |
||
|
|
|||
| 48 | { |
||
| 49 | $app->flash('message', 'It looks like you already have an account. Email address is already in use.'); |
||
| 50 | $app->redirect($final_global_template_vars["path_to_this_module"]."/register/"); |
||
| 51 | } |
||
| 52 | |||
| 53 | // GUMP validation rules |
||
| 54 | $rules = array( |
||
| 55 | "user_account_email" => "required|valid_email" |
||
| 56 | ,"user_account_password" => "required|max_len,100|min_len,6" |
||
| 57 | ,"first_name" => "required|alpha_numeric" |
||
| 58 | ,"last_name" => "required|alpha_numeric" |
||
| 59 | ); |
||
| 60 | |||
| 61 | // Validation using GUMP |
||
| 62 | View Code Duplication | if($posted_data) |
|
| 63 | { |
||
| 64 | $validated = array(); |
||
| 65 | $errors = array(); |
||
| 66 | $validated = $gump->validate($posted_data, $rules); |
||
| 67 | if($validated !== true) |
||
| 68 | { |
||
| 69 | $errors = \phpskeleton\models\utility::gump_parse_errors($validated); |
||
| 70 | } |
||
| 71 | if($errors) |
||
| 72 | { |
||
| 73 | $env = $app->environment(); |
||
| 74 | $env["default_validation_errors"] = $errors; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | $default_validation_errors = isset($env["default_validation_errors"]) ? $env["default_validation_errors"] : false; |
||
| 79 | |||
| 80 | // If there are no errors, process posted data and email to user |
||
| 81 | if (!$default_validation_errors && $posted_data) |
||
| 82 | { |
||
| 83 | $emailed_hash = md5(rand(0, 1000)); |
||
| 84 | |||
| 85 | // INSERT this user into the user_account table |
||
| 86 | $statement = $db_resource->prepare("INSERT INTO user_account |
||
| 87 | (user_account_email, user_account_password, first_name, last_name, acceptable_use_policy, created_date, active, emailed_hash) |
||
| 88 | VALUES ( :user_account_email, :user_account_password, :first_name, :last_name, 1, NOW(), 0, :emailed_hash )"); |
||
| 89 | $statement->bindValue(":user_account_email", $posted_data['user_account_email'], PDO::PARAM_STR); |
||
| 90 | $statement->bindValue(":user_account_password", $authenticate->generate_hashed_password($posted_data['user_account_password']), PDO::PARAM_STR); |
||
| 91 | $statement->bindValue(":first_name", $posted_data['first_name'], PDO::PARAM_STR); |
||
| 92 | $statement->bindValue(":last_name", $posted_data['last_name'], PDO::PARAM_STR); |
||
| 93 | $statement->bindValue(":emailed_hash", $emailed_hash, PDO::PARAM_STR); |
||
| 94 | $statement->execute(); |
||
| 95 | $error = $db_resource->errorInfo(); |
||
| 96 | if ($error[0] != "00000") |
||
| 97 | { |
||
| 98 | die('The INSERT INTO user_account failed.'); |
||
| 99 | } |
||
| 100 | $last_inserted_user_account_id = $db_resource->lastInsertId(); |
||
| 101 | |||
| 102 | // INSERT this user into the user_account_groups table with "Author" privileges |
||
| 103 | $statement = $db_resource->prepare("INSERT INTO user_account_groups |
||
| 104 | (role_id, user_account_id, group_id) |
||
| 105 | VALUES ( 2, :user_account_id, 1 )"); |
||
| 106 | $statement->bindValue(":user_account_id", $last_inserted_user_account_id, PDO::PARAM_INT); |
||
| 107 | $statement->execute(); |
||
| 108 | $error = $db_resource->errorInfo(); |
||
| 109 | if ($error[0] != "00000") |
||
| 110 | { |
||
| 111 | die('The INSERT INTO user_account_groups failed.'); |
||
| 112 | } |
||
| 113 | |||
| 114 | // Send emails |
||
| 115 | |||
| 116 | // Email setup for user |
||
| 117 | $to = $posted_data['user_account_email']; // Send email to our user |
||
| 118 | $subject = 'Signup | Verification'; // Give the email a subject |
||
| 119 | $message = '<h2>Hello '.$posted_data['first_name'].'!</h2> |
||
| 120 | <p>Your account has been created, you can login with the following credentials after you have |
||
| 121 | activated your account by accessing the url below.</p> |
||
| 122 | <hr> |
||
| 123 | <p>Username: '.$posted_data['user_account_email'].'</p> |
||
| 124 | <p>Password: (The password you submitted during the registration process.)</p> |
||
| 125 | <hr> |
||
| 126 | <p>Please click this link to activate your account:<br /> |
||
| 127 | <a href="http://'.$_SERVER["SERVER_NAME"].'/user_account/verify/?user_account_email='.$posted_data['user_account_email'].'&emailed_hash='.$emailed_hash.'">http://'.$_SERVER["SERVER_NAME"].'/user_account/verify/?user_account_email='.$posted_data['user_account_email'].'&emailed_hash='.$emailed_hash.'</a></p>'; // Our message above including the link |
||
| 128 | |||
| 129 | // Email setup for Universal Administrators |
||
| 130 | |||
| 131 | // First, get all of the "Universal Administrator" email addresses |
||
| 132 | $admin_emails = array(); |
||
| 133 | $universal_administrator_emails = $useraccount->get_universal_administrator_emails(); |
||
| 134 | |||
| 135 | // Create a comma-delimited list of email addresses |
||
| 136 | if(is_array($universal_administrator_emails) && !empty($universal_administrator_emails)) { |
||
| 137 | foreach ($universal_administrator_emails as $email) |
||
| 138 | { |
||
| 139 | array_push($admin_emails, $email["user_account_email"]); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $subject_admins = 'New User Registration'; // Give the email a subject |
||
| 144 | $message_admins = '<h2>New User</h2> |
||
| 145 | <p>A new user has registered.</p> |
||
| 146 | <h3>Details</h3> |
||
| 147 | <p>Name: '.$posted_data['first_name'].' '.$posted_data['last_name'].'</p> |
||
| 148 | <p>Email: '.$posted_data['user_account_email'].'</p> |
||
| 149 | <hr> |
||
| 150 | <p><a href="http://'.$_SERVER["SERVER_NAME"].'/authenticate/">Login to administer</a></p>'; // Our message above including the link |
||
| 151 | |||
| 152 | // For the ability to send emails from an AWS EC2 instance |
||
| 153 | // If you need this functionality, you can configure the settings accordingly in /default_global_settings.php |
||
| 154 | View Code Duplication | if ($final_global_template_vars["hosting_vendor"] && ($final_global_template_vars["hosting_vendor"] == "aws_ec2")) |
|
| 155 | { |
||
| 156 | $email = array(); |
||
| 157 | require_once $final_global_template_vars["path_to_smtp_settings"]; |
||
| 158 | |||
| 159 | // SMTP Settings |
||
| 160 | $mail->IsSMTP(); |
||
| 161 | $mail->SMTPAuth = $email['settings']['smtpauth']; |
||
| 162 | $mail->SMTPSecure = $email['settings']['smtpsecure']; |
||
| 163 | $mail->Host = $email['settings']['host']; |
||
| 164 | $mail->Username = $email['settings']['username']; |
||
| 165 | $mail->Password = $email['settings']['password']; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Send email to user |
||
| 169 | $mail->SetFrom($final_global_template_vars["send_emails_from"], $final_global_template_vars["site_name"].' Accounts'); // From (verified email address) |
||
| 170 | $mail->Subject = $subject; // Subject |
||
| 171 | $mail->MsgHTML($message); |
||
| 172 | $mail->AddAddress($to); // Recipient |
||
| 173 | $mail->Send(); |
||
| 174 | $mail->ClearAllRecipients(); |
||
| 175 | |||
| 176 | // Send email to Universal Administrators |
||
| 177 | // Subject |
||
| 178 | $mail->Subject = $subject_admins; |
||
| 179 | $mail->MsgHTML($message_admins); |
||
| 180 | // Universal Admin recipients |
||
| 181 | if(is_array($universal_administrator_emails) && !empty($universal_administrator_emails)) { |
||
| 182 | foreach ($universal_administrator_emails as $email) { |
||
| 183 | $mail->AddAddress($email["user_account_email"]); |
||
| 184 | } |
||
| 185 | $mail->Send(); |
||
| 186 | $mail->ClearAllRecipients(); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | View Code Duplication | if (!$errors) |
|
| 191 | { |
||
| 192 | $app->flash('message', 'Account creation was successful. You will receive an email shortly with further instructions.'); |
||
| 193 | $app->redirect($final_global_template_vars["path_to_this_module"]."/register/"); |
||
| 194 | } |
||
| 195 | else |
||
| 196 | { |
||
| 197 | $env = $app->environment(); |
||
| 198 | $env["default_validation_errors"] = $errors; |
||
| 199 | } |
||
| 200 | } |
||
| 201 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: