-
Notifications
You must be signed in to change notification settings - Fork 788
/
Registration.php
105 lines (94 loc) · 3.15 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
105
<?php
/**
* class Registration
* handles the user registration
*
* @author Panique <[email protected]>
* @version 1.0
*/
class Registration extends Auth
{
/**
* Registration Status
* @var boolean
*/
private $is_registration_ok = false;
/**
* The constructor execute the registration on set the registration status
*/
public function __construct()
{
parent::__construct();
$action = filter_input(
INPUT_GET,
'action',
FILTER_SANITIZE_STRING,
FILTER_REQUIRE_SCALAR|FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH
);
if ('register' == $action) {
$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 - Input Filtering and Validation
$arguments = array(
'user_name' => array('filter' => FILTER_CALLBACK, 'options' => 'Auth::isValidUserName'),
'user_email' => array('filter' => FILTER_CALLBACK, 'options' => 'Auth::isValidEmail'),
'user_password_new' => array('filter' => FILTER_CALLBACK, 'options' => 'Auth::isValidPassword'),
'user_password_repeat' => array('filter' => FILTER_CALLBACK, 'options' => 'Auth::isValidPassword'),
);
$params = filter_input_array(INPUT_POST, $arguments);
$this->errors = array_map(array($this, 'isDataValid'), $params);
foreach ($this->errors as $key => $value) {
if ($value == self::DATA_OK) {
unset($this->errors[$key]);
}
}
if (! isset($this->errors['user_password_new'], $this->errors['user_password_repeat']) &&
($params['user_password_new'] != $params['user_password_repeat'])
) {
$this->errors['user_password_repeat'] = self::DATA_MISMATCH;
}
if (! isset($this->errors['user_name'], $this->errors['user_email']) &&
$this->isUserExists($params['user_name'], $params['user_email'])
) {
$this->errors['user_name'] = self::USER_EXISTS;
}
if (count($this->errors)) {
return false;
}
//2 - write new user data into database
$params['user_password_hash'] = 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;
}
}