1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the alphaz Framework. |
5
|
|
|
* |
6
|
|
|
* @author Muhammad Umer Farooq (Malik) <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/alphazframework/framework |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
* @since 1.0.0 |
13
|
|
|
* |
14
|
|
|
* @license MIT |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace alphaz\Auth; |
18
|
|
|
|
19
|
|
|
use alphaz\Common\PasswordManipulation; |
20
|
|
|
use alphaz\Database\Db as DB; |
21
|
|
|
use alphaz\Hashing\Hash; |
22
|
|
|
use alphaz\Site\Site; |
23
|
|
|
use alphaz\Validation\Validation; |
24
|
|
|
|
25
|
|
|
class Signup extends Handler |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Store the error msgs. |
29
|
|
|
* |
30
|
|
|
* @since 1.0.0 |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $errors = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Signup the users. |
38
|
|
|
* |
39
|
|
|
* @param (mixed) $username username of user |
40
|
|
|
* @param (mixed) $email email of user |
41
|
|
|
* @param (mixed) $password password of users |
42
|
|
|
* @param (array) $params extra field like [name => value] array |
43
|
|
|
* |
44
|
|
|
* @since 1.0.0 |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function signup($username, $email, $password, $params) |
49
|
|
|
{ |
50
|
|
|
$rules = [ |
51
|
|
|
'username' => ['required' => true], |
52
|
|
|
'email' => ['required' => true, 'email' => true], |
53
|
|
|
'password' => ['required' => true], |
54
|
|
|
]; |
55
|
|
|
$inputs = [ |
56
|
|
|
'username' => $username, |
57
|
|
|
'email' => $email, |
58
|
|
|
'password' => $password, |
59
|
|
|
]; |
60
|
|
|
$requireValidate = new Validation($inputs, $rules); |
61
|
|
|
if ($requireValidate->fail()) { |
62
|
|
|
Error::set($requireValidate->error()->get()); |
63
|
|
|
} |
64
|
|
|
$uniqueUsername = new Validation(['field'=> 'username', 'value'=>$username], __config()->auth->db_table, 'database'); |
|
|
|
|
65
|
|
|
if ($uniqueUsername->fail()) { |
66
|
|
|
Error::set($uniqueUsername->error()->get()); |
67
|
|
|
} |
68
|
|
|
$uniqueEmail = new Validation(['field'=> 'email', 'value'=>$email], __config()->auth->db_table, 'database'); |
69
|
|
|
if ($uniqueEmail->fail()) { |
70
|
|
|
Error::set($uniqueEmail->error()->get()); |
71
|
|
|
} |
72
|
|
|
if (is_array($params)) { |
|
|
|
|
73
|
|
|
foreach (array_keys($params) as $key => $value) { |
74
|
|
|
$paramsRules = [$value => ['required' => true]]; |
75
|
|
|
} |
76
|
|
|
$paramsValidate = new Validation($params, $paramsRules); |
|
|
|
|
77
|
|
|
if ($paramsValidate->fail()) { |
78
|
|
|
Error::set($paramsValidate->error()->get()); |
79
|
|
|
} |
80
|
|
|
if (isset($params['passConfirm'])) { |
81
|
|
|
if ($password !== $params['passConfirm']) { |
82
|
|
|
Error::set(__printl('auth:error:password:confirm'), 'password'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
if (__config()->auth->sticky_password) { |
87
|
|
|
if (!(new PasswordManipulation())->isValid($password)) { |
88
|
|
|
Error::set(__printl('auth:error:password:sticky'), 'password'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
if (!(new User())->isLogin()) { |
92
|
|
|
if ($this->fail() !== true) { |
93
|
|
|
$salts = (new Site())::salts(12); |
94
|
|
|
$password_hash = Hash::make($password); |
95
|
|
|
if (__config()->auth->is_verify_email) { |
96
|
|
|
$token = (new Site())::salts(8); |
97
|
|
|
} else { |
98
|
|
|
$token = 'NULL'; |
99
|
|
|
} |
100
|
|
|
$param = [ |
101
|
|
|
'username' => $username, |
102
|
|
|
'email' => $email, |
103
|
|
|
'password' => $password_hash, |
104
|
|
|
'salts' => $salts, |
105
|
|
|
'token' => $token, |
106
|
|
|
]; |
107
|
|
|
$fields = [ |
108
|
|
|
'db_name' => __config()->auth->db_name, |
109
|
|
|
'table' => __config()->auth->db_table, |
110
|
|
|
]; |
111
|
|
|
unset($params['passConfirm']); |
112
|
|
|
$data = ['columns' => array_merge($param, $params)]; |
113
|
|
|
$values = array_merge($fields, $data); |
114
|
|
|
$db = new DB(); |
115
|
|
|
Success::set($db->db()->insert($values)); |
116
|
|
|
$db->db()->close(); |
117
|
|
|
if (__config()->auth->is_verify_email === true) { |
118
|
|
|
$subject = __printl('auth:subject:need:verify'); |
119
|
|
|
$link = site_base_url().__config()->auth->verification_link.'/'.$token; |
120
|
|
|
$html = __printl('auth:body:need:verify'); |
121
|
|
|
$html = str_replace(':email', $email, $html); |
122
|
|
|
$html = str_replace(':link', $link, $html); |
123
|
|
|
new EmailHandler($subject, $html, $email); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
|
|
Error::set(__printl('auth:error:already:login'), 'login'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.