blob: 743cd138897662bf08f0ab8d6708a7d724812c2d (
plain)
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
|
<?php
// src/FormValidation.php
class FormValidation
{
private array $data; // tableau associatif (probablement $_POST)
private string $validation_strategy; // à remplacer plus tard par un objet (pattern stratégie) d'interface ValidationStrategy
private array $errors;
private bool $validated = false;
public function __construct(array $data, string $validation_strategy){
$this->data = $data;
$this->validation_strategy = $validation_strategy;
}
public function validate(): bool
{
$this->errors = [];
// pattern stratégie en une seule classe
switch($this->validation_strategy){
case 'email':
$this->emailStrategy();
break;
case 'create_user':
$this->createUserStrategy();
break;
case 'connection':
$this->connectionStrategy();
break;
case 'username_update':
$this->usernameUpdateStrategy();
break;
case 'password_update':
$this->passwordUpdateStrategy();
break;
default:
http_response_code(500); // c'est un peu comme jeter une exception
echo json_encode(['success' => false, 'error' => 'server_error']);
die;
}
$this->validated = true;
return empty($this->errors);
}
public function getErrors(): array
{
return $this->errors;
}
public function getField(string $field): string
{
return $this->validated ? $this->data[$field] : '';
}
// méthodes de validation
private function captchaValidate(bool $clean_session = true): void
{
$captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0;
$captcha_try = isset($this->data['captcha']) ? Captcha::controlInput($this->data['captcha']) : 0;
if($clean_session){
unset($_SESSION['captcha']);
}
if($captcha_try == 0){
$error = 'error_non_valid_captcha';
}
elseif($captcha_solution == 0){ // ne peut pas arriver, si?
$error = 'captcha_server_error';
}
elseif($captcha_try !== $captcha_solution){
$this->errors[] = 'bad_solution_captcha';
}
}
// erreurs à la création des mots de passe
static private function removeSpacesTabsCRLF(string $chaine): string
{
$cibles = [' ', "\t", "\n", "\r"]; // doubles quotes !!
return(str_replace($cibles, '', $chaine));
}
// stratégies
private function emailStrategy(): void
{
$this->captchaValidate(false);
if(!isset($this->data['name']) || empty($this->data['name'])
|| !isset($this->data['email']) || empty($this->data['email'])
|| !isset($this->data['message']) || empty($this->data['message'])
|| !isset($this->data['hidden']) || !empty($this->data['hidden'])){
$this->errors[] = 'missing_fields';
}
if(!filter_var(trim($this->data['email']), FILTER_VALIDATE_EMAIL)){
$this->errors[] = 'bad_email_address';
}
$this->data['name'] = htmlspecialchars(trim($this->data['name']));
$this->data['email'] = htmlspecialchars(trim($this->data['email']));
$this->data['message'] = htmlspecialchars($this->data['message']);
}
private function createUserStrategy(): void
{
$this->captchaValidate();
// test mauvais paramètres
if(!isset($this->data['login']) || empty($this->data['login'])
|| !isset($this->data['password']) || empty($this->data['password'])
|| !isset($this->data['password_confirmation']) || empty($this->data['password_confirmation'])
|| !isset($this->data['create_user_hidden']) || !empty($this->data['create_user_hidden']))
{
$this->errors[] = 'bad_login_or_password';
}
if($this->data['password'] !== $this->data['password_confirmation']){
$this->errors[] = 'different_passwords';
}
if($this->data['login'] !== self::removeSpacesTabsCRLF(htmlspecialchars($this->data['login']))
|| $this->data['password'] !== self::removeSpacesTabsCRLF(htmlspecialchars($this->data['password']))){
$this->errors[] = 'forbidden_characters';
}
}
private function connectionStrategy(): void
{
$this->captchaValidate();
if(!isset($this->data['login']) || empty($this->data['login'])
|| !isset($this->data['password']) || empty($this->data['password'])
|| !isset($this->data['connection_hidden']) || !empty($this->data['connection_hidden']))
{
$this->errors[] = 'bad_login_or_password';
}
}
private function usernameUpdateStrategy(): void
{
$this->captchaValidate();
if(!isset($this->data['login']) || empty($this->data['login'])
|| !isset($this->data['password']) || empty($this->data['password'])
|| !isset($this->data['new_login']) || empty($this->data['new_login'])
|| !isset($this->data['modify_username_hidden']) || !empty($this->data['modify_username_hidden']))
{
$this->errors[] = 'bad_login_or_password';
}
$new_login = self::removeSpacesTabsCRLF(htmlspecialchars($this->data['new_login']));
if($new_login !== $this->data['new_login']){
$this->errors[] = 'forbidden_characters';
}
if($this->data['login'] !== $_SESSION['user']){
$this->errors[] = 'bad_login_or_password';
}
if($this->data['login'] === $new_login){
$this->errors[] = 'same_username_as_before';
}
}
private function passwordUpdateStrategy(): void
{
$this->captchaValidate();
if(!isset($this->data['login']) || empty($this->data['login'])
|| !isset($this->data['password']) || empty($this->data['password'])
|| !isset($this->data['new_password']) || empty($this->data['new_password'])
|| !isset($this->data['modify_password_hidden']) || !empty($this->data['modify_password_hidden']))
{
$this->errors[] = 'bad_login_or_password';
}
$new_password = self::removeSpacesTabsCRLF(htmlspecialchars($this->data['new_password']));
if($new_password !== $this->data['new_password']){
$this->errors[] = 'forbidden_characters';
}
if($this->data['login'] !== $_SESSION['user']){
$this->errors[] = 'bad_login_or_password';
}
if($this->data['password'] === $new_password){
$this->errors[] = 'same_password_as_before';
}
}
}
|