-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_server_side.php
202 lines (173 loc) · 6.6 KB
/
validate_server_side.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
require_once 'database/connection.php';
function validateData($conn, $data)
{
$nameErr = $emailErr = $passwordErr = $confirm_passwordErr = $contactErr = $role_asErr = "";
$name = $email = $password = $confirm_password = $contact = $role_as = "";
extract($data);
if (empty($name)) {
$nameErr = "Name must be provided";
} else {
$name = test_input($name);
if (!preg_match("/[a-zA-Z\s]{3,25}/", $name)) {
$nameErr = "Invalid name";
}
}
if (empty($email)) {
$emailErr = "Email must be provided";
} else {
$email = test_input($email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email";
}
}
if (empty($password)) {
$passwordErr = "Password must be provided";
} else {
$password = test_input($password);
if (!preg_match("/(?!.*\s).{4,10}/", $password)) {
$passwordErr = "Invalid password";
}
}
if (empty($confirm_password)) {
$confirm_passwordErr = "Password must be provided";
} else {
$confirm_password = test_input($confirm_password);
if (!preg_match("/(?!.*\s).{4,10}/", $confirm_password)) {
$confirm_passwordErr = "Invalid password";
}
}
if (empty($contact)) {
$contactErr = "Contact must be provided";
} else {
$contact = test_input($contact);
if (!preg_match("/01[3-9]{1}[0-9]{8}/", $contact)) {
$contactErr = "Invalid contact";
}
}
if (empty($role_as)) {
$role_asErr = "Role must be provided";
}
function escapeString($conn, $str)
{
return mysqli_real_escape_string($conn, $str);
}
$name = $nameErr ? escapeString($conn, $nameErr) : escapeString($conn, $name);
$email = $emailErr ? escapeString($conn, $emailErr) : escapeString($conn, $email);
$contact = $contactErr ? escapeString($conn, $contactErr) : escapeString($conn, $contact);
$password = $passwordErr ? escapeString($conn, $passwordErr) : escapeString($conn, $password);
$confirm_password = $confirm_passwordErr ? escapeString($conn, $confirm_passwordErr) : escapeString($conn, $confirm_password);
$role_as = $role_asErr ? escapeString($conn, $role_asErr) : escapeString($conn, $role_as);
$arr = ["name" => $name, "email" => $email, "contact" => $contact, "password" => $password, "confirm_password" => $confirm_password, "role_as" => $role_as];
return $arr;
}
function validateLoginData($conn, $loginData)
{
$emailErr = $passwordErr = "";
$email = $password = "";
extract($loginData);
// print_r($loginData);
if (empty($email)) {
$emailErr = "Email must be provided";
} else {
$email = test_input($email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email";
}
}
if (empty($password)) {
$passwordErr = "Password must be provided";
} else {
$password = test_input($password);
if (!preg_match("/(?!.*\s).{4,10}/", $password)) {
$passwordErr = "Invalid password";
}
}
function escapeLoginString($conn, $str)
{
return mysqli_real_escape_string($conn, $str);
}
$email = $emailErr ? escapeLoginString($conn, $emailErr) : escapeLoginString($conn, $email);
$password = $passwordErr ? escapeLoginString($conn, $passwordErr) : escapeLoginString($conn, $password);
$loginArr = ["email" => $email, "password" => $password];
return $loginArr;
}
function validateAdminLoginData($conn, $adminLoginData)
{
$nameErr = $emailErr = "";
$name = $email = "";
extract($adminLoginData);
// print_r($loginData);
if (empty($name)) {
$nameErr = "Name must be provided";
} else {
$name = test_input($name);
if (!preg_match("/[a-zA-Z\s]{3,25}/", $name)) {
$nameErr = "Invalid name";
}
}
if (empty($email)) {
$emailErr = "Email must be provided";
} else {
$email = test_input($email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email";
}
}
function escapeAdminLoginString($conn, $str)
{
return mysqli_real_escape_string($conn, $str);
}
$name = $nameErr ? escapeAdminLoginString($conn, $nameErr) : escapeAdminLoginString($conn, $name);
$email = $emailErr ? escapeAdminLoginString($conn, $emailErr) : escapeAdminLoginString($conn, $email);
$loginArr = ["name" => $name, "email" => $email];
return $loginArr;
}
function validateAdminLoginPassData($conn, $adminLoginPassData)
{
$previousPasswordErr = $newPasswordErr = $retypeNewPasswordErr = "";
$previousPassword = $newPassword = $retypeNewPassword = "";
extract($adminLoginPassData);
// print_r($loginData);
if (empty($previous_password)) {
$previousPasswordErr = "Current Password must be provided";
} else {
$previousPassword = test_input($previous_password);
if (!preg_match("/(?!.*\s).{4,10}/", $previousPassword)) {
$previousPasswordErr = "Invalid password";
}
}
if (empty($new_password)) {
$newPasswordErr = "New Password must be provided";
} else {
$newPassword = test_input($new_password);
if (!preg_match("/(?!.*\s).{4,10}/", $newPassword)) {
$newPasswordErr = "Invalid password";
}
}
if (empty($retype_new_password)) {
$retypeNewPasswordErr = "Retype New Password must be provided";
} else {
$retypeNewPassword = test_input($retype_new_password);
if (!preg_match("/(?!.*\s).{4,10}/", $retypeNewPassword)) {
$retypeNewPasswordErr = "Invalid password";
}
}
function escapeAdminLoginPassString($conn, $str)
{
return mysqli_real_escape_string($conn, $str);
}
$previousPassword = $previousPasswordErr ? escapeAdminLoginPassString($conn, $previousPasswordErr) : escapeAdminLoginPassString($conn, $previousPassword);
$newPassword = $newPasswordErr ? escapeAdminLoginPassString($conn, $newPasswordErr) : escapeAdminLoginPassString($conn, $newPassword);
$retypeNewPassword = $retypeNewPasswordErr ? escapeAdminLoginPassString($conn, $retypeNewPasswordErr) : escapeAdminLoginPassString($conn, $retypeNewPassword);
$loginArr = ["previous_password" => $previousPassword, "new_password" => $newPassword, "retype_new_password" => $retypeNewPassword];
return $loginArr;
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = strip_tags($data);
$data = htmlspecialchars($data);
return $data;
}