diff options
Diffstat (limited to 'src/FormValidation.php')
-rw-r--r-- | src/FormValidation.php | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/FormValidation.php b/src/FormValidation.php index 743cd13..b3a3793 100644 --- a/src/FormValidation.php +++ b/src/FormValidation.php | |||
@@ -1,6 +1,8 @@ | |||
1 | <?php | 1 | <?php |
2 | // src/FormValidation.php | 2 | // src/FormValidation.php |
3 | 3 | ||
4 | declare(strict_types=1); | ||
5 | |||
4 | class FormValidation | 6 | class FormValidation |
5 | { | 7 | { |
6 | private array $data; // tableau associatif (probablement $_POST) | 8 | private array $data; // tableau associatif (probablement $_POST) |
@@ -19,9 +21,15 @@ class FormValidation | |||
19 | 21 | ||
20 | // pattern stratégie en une seule classe | 22 | // pattern stratégie en une seule classe |
21 | switch($this->validation_strategy){ | 23 | switch($this->validation_strategy){ |
22 | case 'email': | 24 | // bloc formulaire de contact |
25 | case 'email_send': | ||
23 | $this->emailStrategy(); | 26 | $this->emailStrategy(); |
24 | break; | 27 | break; |
28 | case 'email_params': // paramètrage en mode admin | ||
29 | $this->emailParamsStrategy(); | ||
30 | break; | ||
31 | |||
32 | // formulaires pages spéciales | ||
25 | case 'create_user': | 33 | case 'create_user': |
26 | $this->createUserStrategy(); | 34 | $this->createUserStrategy(); |
27 | break; | 35 | break; |
@@ -34,6 +42,7 @@ class FormValidation | |||
34 | case 'password_update': | 42 | case 'password_update': |
35 | $this->passwordUpdateStrategy(); | 43 | $this->passwordUpdateStrategy(); |
36 | break; | 44 | break; |
45 | |||
37 | default: | 46 | default: |
38 | http_response_code(500); // c'est un peu comme jeter une exception | 47 | http_response_code(500); // c'est un peu comme jeter une exception |
39 | echo json_encode(['success' => false, 'error' => 'server_error']); | 48 | echo json_encode(['success' => false, 'error' => 'server_error']); |
@@ -94,7 +103,7 @@ class FormValidation | |||
94 | $this->errors[] = 'missing_fields'; | 103 | $this->errors[] = 'missing_fields'; |
95 | } | 104 | } |
96 | 105 | ||
97 | if(!filter_var(trim($this->data['email']), FILTER_VALIDATE_EMAIL)){ | 106 | elseif(!filter_var(trim($this->data['email']), FILTER_VALIDATE_EMAIL)){ |
98 | $this->errors[] = 'bad_email_address'; | 107 | $this->errors[] = 'bad_email_address'; |
99 | } | 108 | } |
100 | 109 | ||
@@ -102,6 +111,26 @@ class FormValidation | |||
102 | $this->data['email'] = htmlspecialchars(trim($this->data['email'])); | 111 | $this->data['email'] = htmlspecialchars(trim($this->data['email'])); |
103 | $this->data['message'] = htmlspecialchars($this->data['message']); | 112 | $this->data['message'] = htmlspecialchars($this->data['message']); |
104 | } | 113 | } |
114 | private function emailParamsStrategy(): void | ||
115 | { | ||
116 | if(!isset($this->data['id'], $this->data['what_param'], $this->data['value'], $this->data['hidden']) | ||
117 | || !empty($this->data['hidden'])){ | ||
118 | $this->errors[] = 'missing_fields'; | ||
119 | } | ||
120 | |||
121 | elseif($this->data['value'] !== ''){ | ||
122 | if(!in_array($this->data['what_param'], ['smtp_host', 'smtp_secure', 'smtp_username', 'smtp_password', 'email_dest'])){ | ||
123 | $this->errors[] = 'unknown_parameter'; | ||
124 | } | ||
125 | elseif($this->data['what_param'] === 'smtp_username' || $this->data['what_param'] === 'email_dest'){ | ||
126 | if(!filter_var($this->data['value'], FILTER_VALIDATE_EMAIL)){ | ||
127 | $this->errors[] = 'invalide_email_address'; | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | |||
132 | // htmlspecialchars exécutés à l'affichage dans FormBuilder | ||
133 | } | ||
105 | private function createUserStrategy(): void | 134 | private function createUserStrategy(): void |
106 | { | 135 | { |
107 | $this->captchaValidate(); | 136 | $this->captchaValidate(); |