1 | <?php |
||
8 | class RegistrationModel |
||
|
|||
9 | { |
||
10 | /** |
||
11 | * Handles the entire registration process for DEFAULT users (not for people who register with |
||
12 | * 3rd party services, like facebook) and creates a new user in the database if everything is fine |
||
13 | * |
||
14 | * @return boolean Gives back the success status of the registration |
||
15 | */ |
||
16 | public static function registerNewUser() |
||
17 | { |
||
18 | // clean the input |
||
19 | $user_name = strip_tags(Request::post('user_name')); |
||
20 | $user_email = strip_tags(Request::post('user_email')); |
||
21 | $user_email_repeat = strip_tags(Request::post('user_email_repeat')); |
||
22 | $user_password_new = Request::post('user_password_new'); |
||
23 | $user_password_repeat = Request::post('user_password_repeat'); |
||
24 | |||
25 | // stop registration flow if registrationInputValidation() returns false (= anything breaks the input check rules) |
||
26 | $validation_result = self::registrationInputValidation(Request::post('captcha'), $user_name, $user_password_new, $user_password_repeat, $user_email, $user_email_repeat); |
||
27 | if (!$validation_result) { |
||
28 | return false; |
||
29 | } |
||
30 | |||
31 | // crypt the password with the PHP 5.5's password_hash() function, results in a 60 character hash string. |
||
32 | // @see php.net/manual/en/function.password-hash.php for more, especially for potential options |
||
33 | $user_password_hash = password_hash($user_password_new, PASSWORD_DEFAULT); |
||
34 | |||
35 | // make return a bool variable, so both errors can come up at once if needed |
||
36 | $return = true; |
||
37 | |||
38 | // check if username already exists |
||
39 | if (UserModel::doesUsernameAlreadyExist($user_name)) { |
||
40 | Session::add('feedback_negative', Text::get('FEEDBACK_USERNAME_ALREADY_TAKEN')); |
||
41 | $return = false; |
||
42 | } |
||
43 | |||
44 | // check if email already exists |
||
45 | if (UserModel::doesEmailAlreadyExist($user_email)) { |
||
46 | Session::add('feedback_negative', Text::get('FEEDBACK_USER_EMAIL_ALREADY_TAKEN')); |
||
47 | $return = false; |
||
48 | } |
||
49 | |||
50 | // if Username or Email were false, return false |
||
51 | if (!$return) return false; |
||
52 | |||
53 | // generate random hash for email verification (40 char string) |
||
54 | $user_activation_hash = sha1(uniqid(mt_rand(), true)); |
||
55 | |||
56 | // write user data to database |
||
57 | if (!self::writeNewUserToDatabase($user_name, $user_password_hash, $user_email, time(), $user_activation_hash)) { |
||
58 | Session::add('feedback_negative', Text::get('FEEDBACK_ACCOUNT_CREATION_FAILED')); |
||
59 | return false; // no reason not to return false here |
||
60 | } |
||
61 | |||
62 | // get user_id of the user that has been created, to keep things clean we DON'T use lastInsertId() here |
||
63 | $user_id = UserModel::getUserIdByUsername($user_name); |
||
64 | |||
65 | if (!$user_id) { |
||
66 | Session::add('feedback_negative', Text::get('FEEDBACK_UNKNOWN_ERROR')); |
||
67 | return false; |
||
68 | } |
||
69 | |||
70 | // send verification email |
||
71 | if (self::sendVerificationEmail($user_id, $user_email, $user_activation_hash)) { |
||
72 | Session::add('feedback_positive', Text::get('FEEDBACK_ACCOUNT_SUCCESSFULLY_CREATED')); |
||
73 | return true; |
||
74 | } |
||
75 | |||
76 | // if verification email sending failed: instantly delete the user |
||
77 | self::rollbackRegistrationByUserId($user_id); |
||
78 | Session::add('feedback_negative', Text::get('FEEDBACK_VERIFICATION_MAIL_SENDING_FAILED')); |
||
79 | return false; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Validates the registration input |
||
84 | * |
||
85 | * @param $captcha |
||
86 | * @param $user_name |
||
87 | * @param $user_password_new |
||
88 | * @param $user_password_repeat |
||
89 | * @param $user_email |
||
90 | * @param $user_email_repeat |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | public static function registrationInputValidation($captcha, $user_name, $user_password_new, $user_password_repeat, $user_email, $user_email_repeat) |
||
95 | { |
||
96 | $return = true; |
||
97 | |||
98 | // perform all necessary checks |
||
99 | if (!CaptchaModel::checkCaptcha($captcha)) { |
||
100 | Session::add('feedback_negative', Text::get('FEEDBACK_CAPTCHA_WRONG')); |
||
101 | $return = false; |
||
102 | } |
||
103 | |||
104 | // if username, email and password are all correctly validated, but make sure they all run on first sumbit |
||
105 | if (self::validateUserName($user_name) AND self::validateUserEmail($user_email, $user_email_repeat) AND self::validateUserPassword($user_password_new, $user_password_repeat) AND $return) { |
||
106 | return true; |
||
107 | } |
||
108 | |||
109 | // otherwise, return false |
||
110 | return false; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Validates the username |
||
115 | * |
||
116 | * @param $user_name |
||
117 | * @return bool |
||
118 | */ |
||
119 | public static function validateUserName($user_name) |
||
134 | |||
135 | /** |
||
136 | * Validates the email |
||
137 | * |
||
138 | * @param $user_email |
||
139 | * @param $user_email_repeat |
||
140 | * @return bool |
||
141 | */ |
||
142 | public static function validateUserEmail($user_email, $user_email_repeat) |
||
164 | |||
165 | /** |
||
166 | * Validates the password |
||
167 | * |
||
168 | * @param $user_password_new |
||
169 | * @param $user_password_repeat |
||
170 | * @return bool |
||
171 | */ |
||
172 | public static function validateUserPassword($user_password_new, $user_password_repeat) |
||
191 | |||
192 | /** |
||
193 | * Writes the new user's data to the database |
||
194 | * |
||
195 | * @param $user_name |
||
196 | * @param $user_password_hash |
||
197 | * @param $user_email |
||
198 | * @param $user_creation_timestamp |
||
199 | * @param $user_activation_hash |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public static function writeNewUserToDatabase($user_name, $user_password_hash, $user_email, $user_creation_timestamp, $user_activation_hash) |
||
224 | |||
225 | /** |
||
226 | * Deletes the user from users table. Currently used to rollback a registration when verification mail sending |
||
227 | * was not successful. |
||
228 | * |
||
229 | * @param $user_id |
||
230 | */ |
||
231 | public static function rollbackRegistrationByUserId($user_id) |
||
238 | |||
239 | /** |
||
240 | * Sends the verification email (to confirm the account). |
||
241 | * The construction of the mail $body looks weird at first, but it's really just a simple string. |
||
242 | * |
||
243 | * @param int $user_id user's id |
||
244 | * @param string $user_email user's email |
||
245 | * @param string $user_activation_hash user's mail verification hash string |
||
246 | * |
||
247 | * @return boolean gives back true if mail has been sent, gives back false if no mail could been sent |
||
248 | */ |
||
249 | public static function sendVerificationEmail($user_id, $user_email, $user_activation_hash) |
||
267 | |||
268 | /** |
||
269 | * checks the email/verification code combination and set the user's activation status to true in the database |
||
270 | * |
||
271 | * @param int $user_id user id |
||
272 | * @param string $user_activation_verification_code verification token |
||
273 | * |
||
274 | * @return bool success status |
||
275 | */ |
||
276 | public static function verifyNewUser($user_id, $user_activation_verification_code) |
||
293 | } |
||
294 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.