diff options
Diffstat (limited to 'src/service/FormValidation.php')
| -rw-r--r-- | src/service/FormValidation.php | 214 |
1 files changed, 0 insertions, 214 deletions
diff --git a/src/service/FormValidation.php b/src/service/FormValidation.php deleted file mode 100644 index 9d0b838..0000000 --- a/src/service/FormValidation.php +++ /dev/null | |||
| @@ -1,214 +0,0 @@ | |||
| 1 | <?php | ||
| 2 | // src/service/FormValidation.php | ||
| 3 | // | ||
| 4 | // pattern strategy simplifié, permet évolution future vers le vrai pattern | ||
| 5 | |||
| 6 | declare(strict_types=1); | ||
| 7 | |||
| 8 | // classe spéciale permettant de bénéficier du typage de classe au lieu de passer une chaîne à FormValidation | ||
| 9 | enum ValidationStrategy { | ||
| 10 | // bloc formulaire de contact | ||
| 11 | case EmailSend; | ||
| 12 | case EmailParam; | ||
| 13 | // formulaires pages spéciales | ||
| 14 | case CreateUser; | ||
| 15 | case Connection; | ||
| 16 | case UsernameUpdate; | ||
| 17 | case PasswordUpdate; | ||
| 18 | |||
| 19 | public function method(): string | ||
| 20 | { | ||
| 21 | return match($this){ | ||
| 22 | self::EmailSend => 'emailSendStrategy', | ||
| 23 | self::EmailParam => 'emailParamStrategy', | ||
| 24 | self::CreateUser => 'createUserStrategy', | ||
| 25 | self::Connection => 'connectionStrategy', | ||
| 26 | self::UsernameUpdate => 'usernameUpdateStrategy', | ||
| 27 | self::PasswordUpdate => 'passwordUpdateStrategy' | ||
| 28 | }; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | class FormValidation | ||
| 33 | { | ||
| 34 | private array $data; // tableau associatif (probablement $_POST) | ||
| 35 | private Closure $callStrategy; | ||
| 36 | private ?string $error; | ||
| 37 | |||
| 38 | public function __construct(array $data, ValidationStrategy $strategy){ | ||
| 39 | $this->data = $data; | ||
| 40 | $method = $strategy->method(); | ||
| 41 | $this->callStrategy = fn() => $this->$method(); | ||
| 42 | } | ||
| 43 | |||
| 44 | public function validate(): bool | ||
| 45 | { | ||
| 46 | $this->error = ($this->callStrategy)(); // appel de la callback | ||
| 47 | return empty($this->errors); | ||
| 48 | } | ||
| 49 | |||
| 50 | public function getError(): string | ||
| 51 | { | ||
| 52 | return $this->error; | ||
| 53 | } | ||
| 54 | |||
| 55 | public function getField(string $field): string | ||
| 56 | { | ||
| 57 | return $this->data[$field] ?? ''; | ||
| 58 | } | ||
| 59 | |||
| 60 | // méthodes de validation | ||
| 61 | private function captchaValidate(bool $clean_session = true): ?string | ||
| 62 | { | ||
| 63 | $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; | ||
| 64 | $captcha_try = isset($this->data['captcha']) ? Captcha::controlInput($this->data['captcha']) : 0; | ||
| 65 | if($clean_session){ | ||
| 66 | unset($_SESSION['captcha']); | ||
| 67 | } | ||
| 68 | |||
| 69 | if($captcha_try == 0){ | ||
| 70 | return 'error_non_valid_captcha'; | ||
| 71 | } | ||
| 72 | elseif($captcha_solution == 0){ // ne peut pas arriver, si? | ||
| 73 | return 'captcha_server_error'; | ||
| 74 | } | ||
| 75 | elseif($captcha_try !== $captcha_solution){ | ||
| 76 | return 'bad_solution_captcha'; | ||
| 77 | } | ||
| 78 | return null; | ||
| 79 | } | ||
| 80 | |||
| 81 | // erreurs à la création des mots de passe | ||
| 82 | static private function removeSpacesTabsCRLF(string $chaine): string | ||
| 83 | { | ||
| 84 | $cibles = [' ', "\t", "\n", "\r"]; // doubles quotes !! | ||
| 85 | return(str_replace($cibles, '', $chaine)); | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | // stratégies | ||
| 90 | private function emailSendStrategy(): ?string | ||
| 91 | { | ||
| 92 | if($error = $this->captchaValidate(false)){ | ||
| 93 | return $error; | ||
| 94 | } | ||
| 95 | |||
| 96 | if(empty($this->data['name']) || empty($this->data['email']) || empty($this->data['message']) | ||
| 97 | || !isset($this->data['hidden']) || !empty($this->data['hidden'])){ | ||
| 98 | return 'missing_fields'; | ||
| 99 | } | ||
| 100 | |||
| 101 | elseif(!filter_var(trim($this->data['email']), FILTER_VALIDATE_EMAIL)){ | ||
| 102 | return 'bad_email_address'; | ||
| 103 | } | ||
| 104 | |||
| 105 | $this->data['name'] = htmlspecialchars(trim($this->data['name'])); | ||
| 106 | $this->data['email'] = htmlspecialchars(trim($this->data['email'])); | ||
| 107 | $this->data['message'] = htmlspecialchars($this->data['message']); | ||
| 108 | |||
| 109 | return null; | ||
| 110 | } | ||
| 111 | private function emailParamStrategy(): ?string | ||
| 112 | { | ||
| 113 | if(empty($this->data['id']) || empty($this->data['what_param']) || empty($this->data['value']) | ||
| 114 | || !isset($this->data['hidden']) || !empty($this->data['hidden'])){ | ||
| 115 | return 'missing_fields'; | ||
| 116 | } | ||
| 117 | |||
| 118 | if(!in_array($this->data['what_param'], ['smtp_host', 'smtp_secure', 'smtp_username', 'smtp_password', 'email_dest'])){ | ||
| 119 | return 'unknown_parameter'; | ||
| 120 | } | ||
| 121 | elseif($this->data['what_param'] === 'smtp_username' || $this->data['what_param'] === 'email_dest'){ | ||
| 122 | if(!filter_var($this->data['value'], FILTER_VALIDATE_EMAIL)){ | ||
| 123 | return 'invalide_email_address'; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | return null; | ||
| 127 | } | ||
| 128 | private function createUserStrategy(): ?string | ||
| 129 | { | ||
| 130 | if($error = $this->captchaValidate()){ | ||
| 131 | return $error; | ||
| 132 | } | ||
| 133 | |||
| 134 | // test mauvais paramètres | ||
| 135 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['password_confirmation']) | ||
| 136 | || !isset($this->data['create_user_hidden']) || !empty($this->data['create_user_hidden'])) | ||
| 137 | { | ||
| 138 | return 'bad_login_or_password'; | ||
| 139 | } | ||
| 140 | |||
| 141 | if($this->data['password'] !== $this->data['password_confirmation']){ | ||
| 142 | return 'different_passwords'; | ||
| 143 | } | ||
| 144 | |||
| 145 | if($this->data['login'] !== self::removeSpacesTabsCRLF(htmlspecialchars($this->data['login'])) | ||
| 146 | || $this->data['password'] !== self::removeSpacesTabsCRLF(htmlspecialchars($this->data['password']))){ | ||
| 147 | return 'forbidden_characters'; | ||
| 148 | } | ||
| 149 | return null; | ||
| 150 | } | ||
| 151 | private function connectionStrategy(): ?string | ||
| 152 | { | ||
| 153 | if($error = $this->captchaValidate()){ | ||
| 154 | return $error; | ||
| 155 | } | ||
| 156 | |||
| 157 | if(empty($this->data['login']) || empty($this->data['password']) | ||
| 158 | || !isset($this->data['connection_hidden']) || !empty($this->data['connection_hidden'])) | ||
| 159 | { | ||
| 160 | return 'bad_login_or_password'; | ||
| 161 | } | ||
| 162 | return null; | ||
| 163 | } | ||
| 164 | private function usernameUpdateStrategy(): ?string | ||
| 165 | { | ||
| 166 | if($error = $this->captchaValidate()){ | ||
| 167 | return $error; | ||
| 168 | } | ||
| 169 | |||
| 170 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['new_login']) | ||
| 171 | || !isset($this->data['modify_username_hidden']) || !empty($this->data['modify_username_hidden'])) | ||
| 172 | { | ||
| 173 | return 'bad_login_or_password'; | ||
| 174 | } | ||
| 175 | |||
| 176 | $new_login = self::removeSpacesTabsCRLF(htmlspecialchars($this->data['new_login'])); | ||
| 177 | if($new_login !== $this->data['new_login']){ | ||
| 178 | return 'forbidden_characters'; | ||
| 179 | } | ||
| 180 | |||
| 181 | if($this->data['login'] !== $_SESSION['user']['username']){ | ||
| 182 | return 'bad_login_or_password'; | ||
| 183 | } | ||
| 184 | if($this->data['login'] === $new_login){ | ||
| 185 | return 'same_username_as_before'; | ||
| 186 | } | ||
| 187 | return null; | ||
| 188 | } | ||
| 189 | private function passwordUpdateStrategy(): ?string | ||
| 190 | { | ||
| 191 | if($error = $this->captchaValidate()){ | ||
| 192 | return $error; | ||
| 193 | } | ||
| 194 | |||
| 195 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['new_password']) | ||
| 196 | || !isset($this->data['modify_password_hidden']) || !empty($this->data['modify_password_hidden'])) | ||
| 197 | { | ||
| 198 | return 'bad_login_or_password'; | ||
| 199 | } | ||
| 200 | |||
| 201 | $new_password = self::removeSpacesTabsCRLF(htmlspecialchars($this->data['new_password'])); | ||
| 202 | if($new_password !== $this->data['new_password']){ | ||
| 203 | return 'forbidden_characters'; | ||
| 204 | } | ||
| 205 | |||
| 206 | if($this->data['login'] !== $_SESSION['user']['username']){ | ||
| 207 | return 'bad_login_or_password'; | ||
| 208 | } | ||
| 209 | if($this->data['password'] === $new_password){ | ||
| 210 | return 'same_password_as_before'; | ||
| 211 | } | ||
| 212 | return null; | ||
| 213 | } | ||
| 214 | } \ No newline at end of file | ||
