diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/controller/ContactFormController.php | 4 | ||||
| -rw-r--r-- | src/controller/UserController.php | 8 | ||||
| -rw-r--r-- | src/service/FormValidation.php | 93 |
3 files changed, 61 insertions, 44 deletions
diff --git a/src/controller/ContactFormController.php b/src/controller/ContactFormController.php index e078af2..82e9b17 100644 --- a/src/controller/ContactFormController.php +++ b/src/controller/ContactFormController.php | |||
| @@ -38,7 +38,7 @@ class ContactFormController | |||
| 38 | return new JsonResponse(['success' => true]); | 38 | return new JsonResponse(['success' => true]); |
| 39 | } | 39 | } |
| 40 | else{ | 40 | else{ |
| 41 | return new JsonResponse(['success' => false, 'error' => $form->getErrors()[0]]); // la 1ère erreur sera affichée | 41 | return new JsonResponse(['success' => false, 'error' => $form->getError()]); |
| 42 | } | 42 | } |
| 43 | } | 43 | } |
| 44 | 44 | ||
| @@ -60,7 +60,7 @@ class ContactFormController | |||
| 60 | } | 60 | } |
| 61 | } | 61 | } |
| 62 | else{ | 62 | else{ |
| 63 | $error = $form->getErrors()[0]; // la 1ère erreur sera affichée | 63 | $error = $form->getError(); |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | if(empty($error)){ | 66 | if(empty($error)){ |
diff --git a/src/controller/UserController.php b/src/controller/UserController.php index 750b01b..b68421f 100644 --- a/src/controller/UserController.php +++ b/src/controller/UserController.php | |||
| @@ -36,7 +36,7 @@ class UserController | |||
| 36 | $entityManager->flush(); | 36 | $entityManager->flush(); |
| 37 | } | 37 | } |
| 38 | else{ | 38 | else{ |
| 39 | $error = $form->getErrors()[0]; // la 1ère erreur sera affichée | 39 | $error = $form->getError(); |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | if(!empty($error)){ | 42 | if(!empty($error)){ |
| @@ -96,7 +96,7 @@ class UserController | |||
| 96 | $entityManager->flush(); | 96 | $entityManager->flush(); |
| 97 | } | 97 | } |
| 98 | else{ | 98 | else{ |
| 99 | $error = $form->getErrors()[0]; // la 1ère erreur sera affichée | 99 | $error = $form->getError(); |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | if(!empty($error)){ | 102 | if(!empty($error)){ |
| @@ -154,7 +154,7 @@ class UserController | |||
| 154 | } | 154 | } |
| 155 | } | 155 | } |
| 156 | else{ | 156 | else{ |
| 157 | $error = $form->getErrors()[0]; // la 1ère erreur sera affichée | 157 | $error = $form->getError(); |
| 158 | } | 158 | } |
| 159 | 159 | ||
| 160 | if(!empty($error)){ | 160 | if(!empty($error)){ |
| @@ -193,7 +193,7 @@ class UserController | |||
| 193 | } | 193 | } |
| 194 | } | 194 | } |
| 195 | else{ | 195 | else{ |
| 196 | $error = $form->getErrors()[0]; // la 1ère erreur sera affichée | 196 | $error = $form->getError(); |
| 197 | } | 197 | } |
| 198 | 198 | ||
| 199 | if(!empty($error)){ | 199 | if(!empty($error)){ |
diff --git a/src/service/FormValidation.php b/src/service/FormValidation.php index 9646a6b..9d0b838 100644 --- a/src/service/FormValidation.php +++ b/src/service/FormValidation.php | |||
| @@ -19,7 +19,7 @@ enum ValidationStrategy { | |||
| 19 | public function method(): string | 19 | public function method(): string |
| 20 | { | 20 | { |
| 21 | return match($this){ | 21 | return match($this){ |
| 22 | self::EmailSend => 'emailStrategy', | 22 | self::EmailSend => 'emailSendStrategy', |
| 23 | self::EmailParam => 'emailParamStrategy', | 23 | self::EmailParam => 'emailParamStrategy', |
| 24 | self::CreateUser => 'createUserStrategy', | 24 | self::CreateUser => 'createUserStrategy', |
| 25 | self::Connection => 'connectionStrategy', | 25 | self::Connection => 'connectionStrategy', |
| @@ -33,7 +33,7 @@ class FormValidation | |||
| 33 | { | 33 | { |
| 34 | private array $data; // tableau associatif (probablement $_POST) | 34 | private array $data; // tableau associatif (probablement $_POST) |
| 35 | private Closure $callStrategy; | 35 | private Closure $callStrategy; |
| 36 | private array $errors; | 36 | private ?string $error; |
| 37 | 37 | ||
| 38 | public function __construct(array $data, ValidationStrategy $strategy){ | 38 | public function __construct(array $data, ValidationStrategy $strategy){ |
| 39 | $this->data = $data; | 39 | $this->data = $data; |
| @@ -43,14 +43,13 @@ class FormValidation | |||
| 43 | 43 | ||
| 44 | public function validate(): bool | 44 | public function validate(): bool |
| 45 | { | 45 | { |
| 46 | $this->errors = []; | 46 | $this->error = ($this->callStrategy)(); // appel de la callback |
| 47 | ($this->callStrategy)(); // appel de la callback | ||
| 48 | return empty($this->errors); | 47 | return empty($this->errors); |
| 49 | } | 48 | } |
| 50 | 49 | ||
| 51 | public function getErrors(): array | 50 | public function getError(): string |
| 52 | { | 51 | { |
| 53 | return $this->errors; | 52 | return $this->error; |
| 54 | } | 53 | } |
| 55 | 54 | ||
| 56 | public function getField(string $field): string | 55 | public function getField(string $field): string |
| @@ -59,7 +58,7 @@ class FormValidation | |||
| 59 | } | 58 | } |
| 60 | 59 | ||
| 61 | // méthodes de validation | 60 | // méthodes de validation |
| 62 | private function captchaValidate(bool $clean_session = true): void | 61 | private function captchaValidate(bool $clean_session = true): ?string |
| 63 | { | 62 | { |
| 64 | $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; | 63 | $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; |
| 65 | $captcha_try = isset($this->data['captcha']) ? Captcha::controlInput($this->data['captcha']) : 0; | 64 | $captcha_try = isset($this->data['captcha']) ? Captcha::controlInput($this->data['captcha']) : 0; |
| @@ -68,14 +67,15 @@ class FormValidation | |||
| 68 | } | 67 | } |
| 69 | 68 | ||
| 70 | if($captcha_try == 0){ | 69 | if($captcha_try == 0){ |
| 71 | $this->errors[] = 'error_non_valid_captcha'; | 70 | return 'error_non_valid_captcha'; |
| 72 | } | 71 | } |
| 73 | elseif($captcha_solution == 0){ // ne peut pas arriver, si? | 72 | elseif($captcha_solution == 0){ // ne peut pas arriver, si? |
| 74 | $this->errors[] = 'captcha_server_error'; | 73 | return 'captcha_server_error'; |
| 75 | } | 74 | } |
| 76 | elseif($captcha_try !== $captcha_solution){ | 75 | elseif($captcha_try !== $captcha_solution){ |
| 77 | $this->errors[] = 'bad_solution_captcha'; | 76 | return 'bad_solution_captcha'; |
| 78 | } | 77 | } |
| 78 | return null; | ||
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | // erreurs à la création des mots de passe | 81 | // erreurs à la création des mots de passe |
| @@ -87,111 +87,128 @@ class FormValidation | |||
| 87 | 87 | ||
| 88 | 88 | ||
| 89 | // stratégies | 89 | // stratégies |
| 90 | private function emailStrategy(): void | 90 | private function emailSendStrategy(): ?string |
| 91 | { | 91 | { |
| 92 | $this->captchaValidate(false); | 92 | if($error = $this->captchaValidate(false)){ |
| 93 | return $error; | ||
| 94 | } | ||
| 93 | 95 | ||
| 94 | if(empty($this->data['name']) || empty($this->data['email']) || empty($this->data['message']) | 96 | if(empty($this->data['name']) || empty($this->data['email']) || empty($this->data['message']) |
| 95 | || !isset($this->data['hidden']) || !empty($this->data['hidden'])){ | 97 | || !isset($this->data['hidden']) || !empty($this->data['hidden'])){ |
| 96 | $this->errors[] = 'missing_fields'; | 98 | return 'missing_fields'; |
| 97 | } | 99 | } |
| 98 | 100 | ||
| 99 | elseif(!filter_var(trim($this->data['email']), FILTER_VALIDATE_EMAIL)){ | 101 | elseif(!filter_var(trim($this->data['email']), FILTER_VALIDATE_EMAIL)){ |
| 100 | $this->errors[] = 'bad_email_address'; | 102 | return 'bad_email_address'; |
| 101 | } | 103 | } |
| 102 | 104 | ||
| 103 | $this->data['name'] = htmlspecialchars(trim($this->data['name'])); | 105 | $this->data['name'] = htmlspecialchars(trim($this->data['name'])); |
| 104 | $this->data['email'] = htmlspecialchars(trim($this->data['email'])); | 106 | $this->data['email'] = htmlspecialchars(trim($this->data['email'])); |
| 105 | $this->data['message'] = htmlspecialchars($this->data['message']); | 107 | $this->data['message'] = htmlspecialchars($this->data['message']); |
| 108 | |||
| 109 | return null; | ||
| 106 | } | 110 | } |
| 107 | private function emailParamStrategy(): void | 111 | private function emailParamStrategy(): ?string |
| 108 | { | 112 | { |
| 109 | if(empty($this->data['id']) || empty($this->data['what_param']) || empty($this->data['value']) | 113 | if(empty($this->data['id']) || empty($this->data['what_param']) || empty($this->data['value']) |
| 110 | || !isset($this->data['hidden']) || !empty($this->data['hidden'])){ | 114 | || !isset($this->data['hidden']) || !empty($this->data['hidden'])){ |
| 111 | $this->errors[] = 'missing_fields'; | 115 | return 'missing_fields'; |
| 112 | } | 116 | } |
| 113 | 117 | ||
| 114 | if(!in_array($this->data['what_param'], ['smtp_host', 'smtp_secure', 'smtp_username', 'smtp_password', 'email_dest'])){ | 118 | if(!in_array($this->data['what_param'], ['smtp_host', 'smtp_secure', 'smtp_username', 'smtp_password', 'email_dest'])){ |
| 115 | $this->errors[] = 'unknown_parameter'; | 119 | return 'unknown_parameter'; |
| 116 | } | 120 | } |
| 117 | elseif($this->data['what_param'] === 'smtp_username' || $this->data['what_param'] === 'email_dest'){ | 121 | elseif($this->data['what_param'] === 'smtp_username' || $this->data['what_param'] === 'email_dest'){ |
| 118 | if(!filter_var($this->data['value'], FILTER_VALIDATE_EMAIL)){ | 122 | if(!filter_var($this->data['value'], FILTER_VALIDATE_EMAIL)){ |
| 119 | $this->errors[] = 'invalide_email_address'; | 123 | return 'invalide_email_address'; |
| 120 | } | 124 | } |
| 121 | } | 125 | } |
| 126 | return null; | ||
| 122 | } | 127 | } |
| 123 | private function createUserStrategy(): void | 128 | private function createUserStrategy(): ?string |
| 124 | { | 129 | { |
| 125 | $this->captchaValidate(); | 130 | if($error = $this->captchaValidate()){ |
| 131 | return $error; | ||
| 132 | } | ||
| 126 | 133 | ||
| 127 | // test mauvais paramètres | 134 | // test mauvais paramètres |
| 128 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['password_confirmation']) | 135 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['password_confirmation']) |
| 129 | || !isset($this->data['create_user_hidden']) || !empty($this->data['create_user_hidden'])) | 136 | || !isset($this->data['create_user_hidden']) || !empty($this->data['create_user_hidden'])) |
| 130 | { | 137 | { |
| 131 | $this->errors[] = 'bad_login_or_password'; | 138 | return 'bad_login_or_password'; |
| 132 | } | 139 | } |
| 133 | 140 | ||
| 134 | if($this->data['password'] !== $this->data['password_confirmation']){ | 141 | if($this->data['password'] !== $this->data['password_confirmation']){ |
| 135 | $this->errors[] = 'different_passwords'; | 142 | return 'different_passwords'; |
| 136 | } | 143 | } |
| 137 | 144 | ||
| 138 | if($this->data['login'] !== self::removeSpacesTabsCRLF(htmlspecialchars($this->data['login'])) | 145 | if($this->data['login'] !== self::removeSpacesTabsCRLF(htmlspecialchars($this->data['login'])) |
| 139 | || $this->data['password'] !== self::removeSpacesTabsCRLF(htmlspecialchars($this->data['password']))){ | 146 | || $this->data['password'] !== self::removeSpacesTabsCRLF(htmlspecialchars($this->data['password']))){ |
| 140 | $this->errors[] = 'forbidden_characters'; | 147 | return 'forbidden_characters'; |
| 141 | } | 148 | } |
| 149 | return null; | ||
| 142 | } | 150 | } |
| 143 | private function connectionStrategy(): void | 151 | private function connectionStrategy(): ?string |
| 144 | { | 152 | { |
| 145 | $this->captchaValidate(); | 153 | if($error = $this->captchaValidate()){ |
| 154 | return $error; | ||
| 155 | } | ||
| 146 | 156 | ||
| 147 | if(empty($this->data['login']) || empty($this->data['password']) | 157 | if(empty($this->data['login']) || empty($this->data['password']) |
| 148 | || !isset($this->data['connection_hidden']) || !empty($this->data['connection_hidden'])) | 158 | || !isset($this->data['connection_hidden']) || !empty($this->data['connection_hidden'])) |
| 149 | { | 159 | { |
| 150 | $this->errors[] = 'bad_login_or_password'; | 160 | return 'bad_login_or_password'; |
| 151 | } | 161 | } |
| 162 | return null; | ||
| 152 | } | 163 | } |
| 153 | private function usernameUpdateStrategy(): void | 164 | private function usernameUpdateStrategy(): ?string |
| 154 | { | 165 | { |
| 155 | $this->captchaValidate(); | 166 | if($error = $this->captchaValidate()){ |
| 167 | return $error; | ||
| 168 | } | ||
| 156 | 169 | ||
| 157 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['new_login']) | 170 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['new_login']) |
| 158 | || !isset($this->data['modify_username_hidden']) || !empty($this->data['modify_username_hidden'])) | 171 | || !isset($this->data['modify_username_hidden']) || !empty($this->data['modify_username_hidden'])) |
| 159 | { | 172 | { |
| 160 | $this->errors[] = 'bad_login_or_password'; | 173 | return 'bad_login_or_password'; |
| 161 | } | 174 | } |
| 162 | 175 | ||
| 163 | $new_login = self::removeSpacesTabsCRLF(htmlspecialchars($this->data['new_login'])); | 176 | $new_login = self::removeSpacesTabsCRLF(htmlspecialchars($this->data['new_login'])); |
| 164 | if($new_login !== $this->data['new_login']){ | 177 | if($new_login !== $this->data['new_login']){ |
| 165 | $this->errors[] = 'forbidden_characters'; | 178 | return 'forbidden_characters'; |
| 166 | } | 179 | } |
| 167 | 180 | ||
| 168 | if($this->data['login'] !== $_SESSION['user']['username']){ | 181 | if($this->data['login'] !== $_SESSION['user']['username']){ |
| 169 | $this->errors[] = 'bad_login_or_password'; | 182 | return 'bad_login_or_password'; |
| 170 | } | 183 | } |
| 171 | if($this->data['login'] === $new_login){ | 184 | if($this->data['login'] === $new_login){ |
| 172 | $this->errors[] = 'same_username_as_before'; | 185 | return 'same_username_as_before'; |
| 173 | } | 186 | } |
| 187 | return null; | ||
| 174 | } | 188 | } |
| 175 | private function passwordUpdateStrategy(): void | 189 | private function passwordUpdateStrategy(): ?string |
| 176 | { | 190 | { |
| 177 | $this->captchaValidate(); | 191 | if($error = $this->captchaValidate()){ |
| 192 | return $error; | ||
| 193 | } | ||
| 178 | 194 | ||
| 179 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['new_password']) | 195 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['new_password']) |
| 180 | || !isset($this->data['modify_password_hidden']) || !empty($this->data['modify_password_hidden'])) | 196 | || !isset($this->data['modify_password_hidden']) || !empty($this->data['modify_password_hidden'])) |
| 181 | { | 197 | { |
| 182 | $this->errors[] = 'bad_login_or_password'; | 198 | return 'bad_login_or_password'; |
| 183 | } | 199 | } |
| 184 | 200 | ||
| 185 | $new_password = self::removeSpacesTabsCRLF(htmlspecialchars($this->data['new_password'])); | 201 | $new_password = self::removeSpacesTabsCRLF(htmlspecialchars($this->data['new_password'])); |
| 186 | if($new_password !== $this->data['new_password']){ | 202 | if($new_password !== $this->data['new_password']){ |
| 187 | $this->errors[] = 'forbidden_characters'; | 203 | return 'forbidden_characters'; |
| 188 | } | 204 | } |
| 189 | 205 | ||
| 190 | if($this->data['login'] !== $_SESSION['user']['username']){ | 206 | if($this->data['login'] !== $_SESSION['user']['username']){ |
| 191 | $this->errors[] = 'bad_login_or_password'; | 207 | return 'bad_login_or_password'; |
| 192 | } | 208 | } |
| 193 | if($this->data['password'] === $new_password){ | 209 | if($this->data['password'] === $new_password){ |
| 194 | $this->errors[] = 'same_password_as_before'; | 210 | return 'same_password_as_before'; |
| 195 | } | 211 | } |
| 212 | return null; | ||
| 196 | } | 213 | } |
| 197 | } \ No newline at end of file | 214 | } \ No newline at end of file |
