-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.php
More file actions
220 lines (171 loc) · 7.02 KB
/
validation.php
File metadata and controls
220 lines (171 loc) · 7.02 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
require_once('validation/ValidationMessage.php');
require_once('validation/ValidationResult.php');
require_once('validation/ValidationType.php');
/*
* VALIDATE REGISTRATION - Hier werden alle Validierungen durchgefuehrt
*/
function validateRegistration(string $username, string $email, string $password, string $passwordbest): ValidationResult
{
$validationResult = checkEmptyFields($username, $email, $password, $passwordbest);
if ($validationResult->getValidationType() === ValidationType::ERROR) {
return $validationResult;
}
$validationResult = validateUsername($username);
if ($validationResult->getValidationType() === ValidationType::ERROR) {
return $validationResult;
}
$validationResult = validateEmail($email);
if ($validationResult->getValidationType() === ValidationType::ERROR) {
return $validationResult;
}
$validationResult = validatePassword($password, $passwordbest);
if ($validationResult->getValidationType() === ValidationType::ERROR) {
return $validationResult;
}
return new ValidationResult(ValidationType::SUCCESS, ValidationMessage::EMPTY);
}
/**
* Prueft ob alle uebergebenen Felder gefuellt sind (nicht ob diese korrekt gefuellt sind)
*
* @param string $username Der Username
* @param string $email Die Email-Adresse
* @param string $password Das Passwort
* @param string $passwordbest Das Passwort bestaetigt
* @return ValidationResult
*/
function checkEmptyFields(string $username, string $email, string $password, string $passwordbest): ValidationResult
{
Logger::trace("Entry -> USERNAME=" . $username . ' EMAIL=' . $email . ' PASSWORD=' . $password . ' PASSWORDBEST=' . $passwordbest);
if (empty($username)) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::USERNAME_EMPTY);
}
if (empty($email)) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::EMAIL_EMPTY);
}
if (empty($password)) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::PASSWORD_EMPTY);
}
if (empty($passwordbest)) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::PASSWORDBEST_EMPTY);
}
Logger::trace("Validierung erfolgreich");
return new ValidationResult(ValidationType::SUCCESS, ValidationMessage::EMPTY);
}
/**
* Verschiedenste Methoden um einen Usernamen zu verifizieren
*
* @param string $username Der zu pruefende Username
* @return ValidationResult ValidationType::SUCCESS=okay, ValidationType::ERROR=nokay
*/
function validateUsername(string $username): ValidationResult
{
Logger::trace("Entry -> USERNAME=" . $username);
if (containsForbiddenChars($username)) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::USERNAMECONTAINSFORBIDDENCHARS);
}
if (strlen($username) < USERNAME_MIN_CHARS) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::USERNAMENOTENOUGHCHARS);
}
if (existsUsername($username)) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::USERNAMEALREADYEXISTS);
}
Logger::trace("Validierung erfolgreich");
return new ValidationResult(ValidationType::SUCCESS, ValidationMessage::EMPTY);
}
/**
* Validierungsmethode fuer die Email-Adresse. Hier wird geprueft, ob die Email ein @-Zeichen enthaelt und die Endung .de oder .com
* enthaelt. Des Weiteren wird geprueft, ob die Emailadresse bereits in der Datenbank vorhanden ist
*
* @param string $email Die zu pruefende Email
* @return ValidationResult ValidationResult::SUCCESS=okay, ValidationResult::ERROR=nokay
*/
function validateEmail(string $email): ValidationResult
{
Logger::trace("Entry -> EMAIL=" . $email);
if (!str_contains($email, "@")) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::EMAILNOTOKAY);
}
if (!str_ends_with(strtolower(trim($email)), ".com") && !str_ends_with(strtolower(trim($email)), ".de")) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::EMAILDOMAINNOTOKAY);
}
if (existsEmail($email)) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::EMAILALREADYEXISTS);
}
Logger::trace("Validierung erfolgreich");
return new ValidationResult(ValidationType::SUCCESS, ValidationMessage::EMPTY);
}
/**
* Validierungsmethode fuer die Passworteingaben
*
* @param string $password Das zu pruefende Passwort
* @param string $passwordbest Das zu pruefende Passwort aus Passwort bestaetigen
* @return ValidationResult ValidationResult::SUCCESS=okay, ValidationResult::ERROR=nokay
*/
function validatePassword(string $password, string $passwordbest): ValidationResult
{
Logger::trace("Entry -> PASSWORD=" . $password . ' PASSWORDBEST=' . $passwordbest);
if (strlen($password) < PASSWORD_MIN_CHARS) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::PASSWORDTOOSHORT);
}
if ($password !== $passwordbest) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::PASSWORDSDOESNTMATCH);
}
if (str_contains($password, ' ') || str_contains($passwordbest, ' ')) {
return new ValidationResult(ValidationType::ERROR, ValidationMessage::PASSWORDWITHEMPTYCHARS);
}
Logger::trace("Validierung erfolgreich");
return new ValidationResult(ValidationType::SUCCESS, ValidationMessage::EMPTY);
}
/**
* Prueft, ob der uebergebene Nutzername bereits in der Datenbank vorhanden ist. Dabei wird die Gross- und Kleinschreibung
* des Usernamens ignoriert!
*
* @param string $username Der zu pruefende Username
* @return bool true=Username existiert, false=Username nicht gefunden
*/
function existsUsername(string $username): bool
{
Logger::trace('Enter -> USERNAME=' . $username);
$lowerUsername = strtolower($username);
$db = new PDO('sqlite:db/bookmarkservice.db');
$sql = "SELECT ID, NAME, EMAIL, PASS, VERIFIED, CREATED, LASTLOGIN FROM USER WHERE LOWER(NAME) = :username";
$stmt = $db -> prepare($sql);
$stmt -> bindParam(PARAM_USERNAME, $lowerUsername);
Logger::sql($sql);
$stmt -> execute();
$res = $stmt -> fetchAll(PDO::FETCH_ASSOC);
if (!empty($res)) {
Logger::trace('Exit -> true');
return true;
}
$db = null;
Logger::trace('Exit -> false');
return false;
}
/**
* Prueft, ob die uebergebene Emailadresse bereits in der Datenbank vorhanden ist. Dabei wird die Gross- und Kleinschreibung
* dee Email ignoriert!
*
* @param string $email Die zu pruefende Email
* @return bool true=Email existiert, false=Email nicht gefunden
*/
function existsEmail(string $email): bool
{
Logger::trace('Enter -> EMAIL=' . $email);
$lowerEmail = strtolower($email);
$db = new PDO('sqlite:db/bookmarkservice.db');
$sql = "SELECT ID, NAME, EMAIL, PASS, VERIFIED, CREATED, LASTLOGIN FROM USER WHERE LOWER(EMAIL) = :email";
$stmt = $db -> prepare($sql);
$stmt -> bindParam(PARAM_EMAIL, $lowerEmail);
Logger::trace('Folgender SQL wird ausgefuehrt: ' . $sql);
$stmt -> execute();
$res = $stmt -> fetchAll(PDO::FETCH_ASSOC);
if (!empty($res)) {
Logger::trace('Exit -> true');
return true;
}
$db = null;
Logger::trace('Exit -> false');
return false;
}