matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 1 | <?php |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 2 | include_once 'ldap.php'; |
| 3 | include_once 'redis.php'; |
| 4 | include_once 'config.php'; |
| 5 | include_once 'utils.php'; |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame^] | 6 | include_once 'db_backend.php'; |
| 7 | |
| 8 | use \DB as DB; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 9 | |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 10 | $TEMPLATE = template_path(); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 11 | |
| 12 | function validate_username(string $username) |
| 13 | { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 14 | global $TEMPLATE; |
| 15 | include 'config.php'; |
| 16 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 17 | $error = ""; |
| 18 | if (ldap_user_count($username)) { |
| 19 | $error = $error . $USERNAME_VALIDATION_ERROR->registered; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 20 | } |
| 21 | if (preg_match("/\s/", $username)) { |
| 22 | $error = $error . $USERNAME_VALIDATION_ERROR->no_whitespaces; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 23 | } |
| 24 | if (strlen($username) > $VAL_USER->max_username) { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 25 | echo $VAL_USER->max_username; |
| 26 | echo $USERNAME_VALIDATION_ERROR->smaller_than; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 27 | $error = $error . format($USERNAME_VALIDATION_ERROR->smaller_than, ["num" => $VAL_USER->max_username + 1]); |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 28 | echo $error; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 29 | } |
| 30 | if (strlen($username) < $VAL_USER->min_username) { |
| 31 | $error = $error . format($USERNAME_VALIDATION_ERROR->bigger_than, ["num" => $VAL_USER->min_username - 1]); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 32 | } |
| 33 | if (preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $username)) { |
| 34 | $error = $error . $USERNAME_VALIDATION_ERROR->no_special_chars; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 35 | } |
| 36 | if (preg_match('/^\d/', $username)) { |
| 37 | $error = $error . $USERNAME_VALIDATION_ERROR->no_number_begining; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 38 | } |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 39 | include "blacklists/usernames.php"; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 40 | if (in_array($username, $USERNAME_BLACKLIST)) { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 41 | $error = $error . $USERNAME_VALIDATION_ERROR->blacklisted; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 42 | } |
| 43 | return $error; |
| 44 | } |
| 45 | |
| 46 | function validate_name(string $name, object $ERRORS) |
| 47 | { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 48 | global $TEMPLATE; |
| 49 | include "config.php"; |
| 50 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 51 | $error = ""; |
| 52 | if (preg_match("/\s/", $name)) { |
| 53 | $error = $error . $ERRORS->no_whitespaces; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 54 | } |
| 55 | if (strlen($name) > $VAL_USER->max_first_name) { |
| 56 | $error = $error . format($ERRORS->smaller_than, ["num" => $VAL_USER->max_first_name + 1]); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 57 | } |
| 58 | if (strlen($name) < $VAL_USER->min_first_name) { |
| 59 | $error = $error . format($ERRORS->bigger_than, ["num" => $VAL_USER->min_first_name - 1]); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 60 | } |
| 61 | if (preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\0-9]/', $name)) { |
| 62 | $error = $error . $ERRORS->no_special_chars; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 63 | } |
| 64 | return $error; |
| 65 | } |
| 66 | |
| 67 | function validate_email(string $email) |
| 68 | { |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame^] | 69 | global $CONFERENCE_REGISTRATION, $TEMPLATE, $log, $MAIL_HOST_BLACKLIST, $EMAIL_VALIDATION_ERROR, $BASE_URL; |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 70 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 71 | $error = ""; |
| 72 | |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame^] | 73 | if (($CONFERENCE_REGISTRATION && (new DB(null))->mail_count($email)) || (!$CONFERENCE_REGISTRATION && ldap_mail_count($email))) { |
| 74 | $log->info("Email already registered"); |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 75 | $error = $error . format($EMAIL_VALIDATION_ERROR->registered, ["link" => $BASE_URL . "?type=recover"]); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 76 | } |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame^] | 77 | if (in_array(explode("@", $email)[1], $MAIL_HOST_BLACKLIST)) { |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 78 | $error = $error . $EMAIL_VALIDATION_ERROR->blacklisted; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 79 | } |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame^] | 80 | $log->debug("Checking if email is pending"); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 81 | $pending = redis_get("pending"); |
| 82 | if ($pending) { |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame^] | 83 | $log->debug("Email might be pending"); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 84 | $maillist = $pending->mails; |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame^] | 85 | if (is_array($maillist) && in_array($email, $maillist)) { |
| 86 | if ($CONFERENCE_REGISTRATION) { |
| 87 | unset($maillist[array_search($user->email, $maillist)]); |
| 88 | redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY); |
| 89 | $log->debug("Email was pending, but let participant change details."); |
| 90 | echo '<div class="alert alert-warning" role="alert">A confirmation request has already been sent to this email, but no problem.</div>'; |
| 91 | } else { |
| 92 | $error = $error . $EMAIL_VALIDATION_ERROR->pending; |
| 93 | $log->debug("Email is pending"); |
| 94 | } |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 95 | } |
| 96 | } |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame^] | 97 | $log->debug("email validated, result: $error"); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 98 | return $error; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | function validate_password(string $password) |
| 103 | { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 104 | global $TEMPLATE; |
| 105 | include "config.php"; |
| 106 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 107 | $error = ""; |
| 108 | if ($_POST["password"] != $_POST["password_confirm"]) {; |
| 109 | $error = $error . $PASSWORD_VALIDATION_ERROR->no_match; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 110 | } |
| 111 | if (strlen($password) < $VAL_USER->min_password) { |
| 112 | $error = $error . format($PASSWORD_VALIDATION_ERROR->bigger_than, ["num" => $VAL_USER->min_password]); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 113 | } |
| 114 | if (strlen($password) > $VAL_USER->max_password) { |
| 115 | $error = $error . format($PASSWORD_VALIDATION_ERROR->smaller_than, ["num" => $VAL_USER->max_password]); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 116 | } |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame] | 117 | include "blacklists/password.php"; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 118 | if (in_array($password, $PASSWORD_BLACKLIST)) { |
| 119 | $error = $error . $PASSWORD_VALIDATION_ERROR->blacklisted; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 120 | } |
| 121 | foreach (array("username", "name", "last_name", "email") as &$field) { |
| 122 | if (!isset($_POST[$field])) |
| 123 | continue; |
| 124 | $value = strtoupper($_POST[$field]); |
| 125 | $PASSWORD = strtoupper($password); |
| 126 | if (strpos($value, $PASSWORD) !== false || strpos($PASSWORD, $value) !== false) { |
| 127 | $error = $error . $PASSWORD_VALIDATION_ERROR->shared_inclusion; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | } |
| 131 | return $error; |
| 132 | } |