-
Notifications
You must be signed in to change notification settings - Fork 790
/
Registration.php
104 lines (93 loc) · 3.04 KB
/
Registration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* class Registration
* handles the user registration
*
* @author Panique <[email protected]>
* @version 1.0
*/
class Registration extends Auth
{
/**
* Registration Status
*
*/
private $is_registration_ok = false;
/**
* The constructor execute the registration on set the registration status
*
*/
public function __construct()
{
parent::__construct();
$this->is_registration_ok = $this->registerNewUser();
}
/**
* return the registration status
*
* @return boolean
*/
public function isRegistrationSuccessful()
{
return $this->is_registration_ok;
}
/**
* registerNewUser
*
* handles the entire registration process.
* checks all error possibilities,
* and creates a new user in the database if
* everything is fine
*
* @return boolean
*
*/
private function registerNewUser()
{
//1 - reset the errors property
$this->errors = array();
if (filter_has_var(INPUT_POST, 'register')) {
return false;
}
//2 - Input Filtering and Validation
$arguments = array(
'user_name' => array('filter' => FILTER_CALLBACK, 'options' => array('Auth::isValidUserName')),
'user_email' => array('filter' => FILTER_CALLBACK, 'options' => array('Auth::isValidEmail')),
'user_password_new' => array('filter' => FILTER_CALLBACK, 'options' => array('Auth::isValidPassword')),
'user_password_repeat' => array('filter' => FILTER_CALLBACK, 'options' => array('Auth::isValidPassword')),
);
$params = filter_input_array(INPUT_POST, $arguments);
if (! $params) {
$params = array_fill_keys(array_keys($arguments), null);
}
foreach (array_keys($arguments) as $keys) {
$value = $params[$keys];
if (is_null($value)) {
$this->errors[$keys] = self::DATA_MISSING;
} elseif (! $value) {
$this->errors[$keys] = self::DATA_INVALID;
}
}
if (! $this->errors && ($params['user_password_new'] != $params['user_password_repeat'])) {
$this->errors['user_password_repeat'] = self::DATA_MISMATCH;
}
if (! $this->errors && $this->isUserExists($params['user_name'], $params['user_email'])) {
$this->errors['user_name'] = self::USER_EXISTS;
}
if (count($this->errors)) {
return false;
}
//3 - write new user data into database
$params['user_password'] = password_hash($params['user_password_new'], PASSWORD_DEFAULT);
unset($params['user_password_new'], $params['user_password_repeat']);
$params = array_map(array($this->conn, 'real_escape_string'), $params);
$res = $this->conn->query(
"INSERT INTO users (".implode(',', array_keys($params)).") VALUES ('."implode("','", $params)".')"
);
if (! $res) {
$this->errors['user_name'] = self::REGISTRATION_FAILED;
return false;
}
return true;
}
}