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'; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 6 | |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 7 | $TEMPLATE = template_path(); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 8 | |
| 9 | function validate_username(string $username) |
| 10 | { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 11 | global $TEMPLATE; |
| 12 | include 'config.php'; |
| 13 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 14 | $error = ""; |
| 15 | if (ldap_user_count($username)) { |
| 16 | $error = $error . $USERNAME_VALIDATION_ERROR->registered; |
| 17 | unset($_POST["username"]); |
| 18 | } |
| 19 | if (preg_match("/\s/", $username)) { |
| 20 | $error = $error . $USERNAME_VALIDATION_ERROR->no_whitespaces; |
| 21 | unset($_POST["username"]); |
| 22 | } |
| 23 | if (strlen($username) > $VAL_USER->max_username) { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 24 | echo $VAL_USER->max_username; |
| 25 | echo $USERNAME_VALIDATION_ERROR->smaller_than; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 26 | $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^] | 27 | echo $error; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 28 | unset($_POST["username"]); |
| 29 | } |
| 30 | if (strlen($username) < $VAL_USER->min_username) { |
| 31 | $error = $error . format($USERNAME_VALIDATION_ERROR->bigger_than, ["num" => $VAL_USER->min_username - 1]); |
| 32 | unset($_POST["username"]); |
| 33 | } |
| 34 | if (preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $username)) { |
| 35 | $error = $error . $USERNAME_VALIDATION_ERROR->no_special_chars; |
| 36 | unset($_POST["username"]); |
| 37 | } |
| 38 | if (preg_match('/^\d/', $username)) { |
| 39 | $error = $error . $USERNAME_VALIDATION_ERROR->no_number_begining; |
| 40 | unset($_POST["username"]); |
| 41 | } |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 42 | include "blacklists/usernames.php"; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 43 | if (in_array($username, $USERNAME_BLACKLIST)) { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 44 | $error = $error . $USERNAME_VALIDATION_ERROR->blacklisted; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 45 | unset($_POST["username"]); |
| 46 | } |
| 47 | return $error; |
| 48 | } |
| 49 | |
| 50 | function validate_name(string $name, object $ERRORS) |
| 51 | { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 52 | global $TEMPLATE; |
| 53 | include "config.php"; |
| 54 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 55 | $error = ""; |
| 56 | if (preg_match("/\s/", $name)) { |
| 57 | $error = $error . $ERRORS->no_whitespaces; |
| 58 | unset($_POST["name"]); |
| 59 | } |
| 60 | if (strlen($name) > $VAL_USER->max_first_name) { |
| 61 | $error = $error . format($ERRORS->smaller_than, ["num" => $VAL_USER->max_first_name + 1]); |
| 62 | unset($_POST["name"]); |
| 63 | } |
| 64 | if (strlen($name) < $VAL_USER->min_first_name) { |
| 65 | $error = $error . format($ERRORS->bigger_than, ["num" => $VAL_USER->min_first_name - 1]); |
| 66 | unset($_POST["name"]); |
| 67 | } |
| 68 | if (preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\0-9]/', $name)) { |
| 69 | $error = $error . $ERRORS->no_special_chars; |
| 70 | unset($_POST["name"]); |
| 71 | } |
| 72 | return $error; |
| 73 | } |
| 74 | |
| 75 | function validate_email(string $email) |
| 76 | { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 77 | global $TEMPLATE; |
| 78 | include "config.php"; |
| 79 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 80 | $error = ""; |
| 81 | |
| 82 | if (ldap_mail_count($email)) { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 83 | $error = $error . format($EMAIL_VALIDATION_ERROR->registered, ["link" => $BASE_URL . "?type=recover"]); |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 84 | unset($_POST["email"]); |
| 85 | } |
| 86 | if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
| 87 | $error = $error . $EMAIL_VALIDATION_ERROR->invalid; |
| 88 | unset($_POST["email"]); |
| 89 | } elseif (in_array(explode("@", $email)[1], $MAIL_HOST_BLACKLIST)) { |
| 90 | $error = $error . $EMAIL_VALIDATION_ERROR->blacklisted; |
| 91 | unset($_POST["email"]); |
| 92 | } |
| 93 | $pending = redis_get("pending"); |
| 94 | if ($pending) { |
| 95 | $maillist = $pending->mails; |
| 96 | if (in_array($email, $maillist)) { |
| 97 | $error = $error . $EMAIL_VALIDATION_ERROR->pending; |
| 98 | unset($_POST["email"]); |
| 99 | } |
| 100 | } |
| 101 | return $error; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | function validate_password(string $password) |
| 106 | { |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 107 | global $TEMPLATE; |
| 108 | include "config.php"; |
| 109 | include $TEMPLATE . 'strings.php'; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 110 | $error = ""; |
| 111 | if ($_POST["password"] != $_POST["password_confirm"]) {; |
| 112 | $error = $error . $PASSWORD_VALIDATION_ERROR->no_match; |
| 113 | unset($_POST["password_confirm"]); |
| 114 | } |
| 115 | if (strlen($password) < $VAL_USER->min_password) { |
| 116 | $error = $error . format($PASSWORD_VALIDATION_ERROR->bigger_than, ["num" => $VAL_USER->min_password]); |
| 117 | unset($_POST["password"]); |
| 118 | unset($_POST["password_confirm"]); |
| 119 | } |
| 120 | if (strlen($password) > $VAL_USER->max_password) { |
| 121 | $error = $error . format($PASSWORD_VALIDATION_ERROR->smaller_than, ["num" => $VAL_USER->max_password]); |
| 122 | unset($_POST["password"]); |
| 123 | unset($_POST["password_confirm"]); |
| 124 | } |
matheusfillipe | c0ce7fa | 2021-05-13 05:15:37 -0300 | [diff] [blame^] | 125 | include "blacklists/password.php"; |
matheusfillipe | 47cf90b | 2021-05-13 03:36:21 -0300 | [diff] [blame] | 126 | if (in_array($password, $PASSWORD_BLACKLIST)) { |
| 127 | $error = $error . $PASSWORD_VALIDATION_ERROR->blacklisted; |
| 128 | unset($_POST["password"]); |
| 129 | unset($_POST["password_confirm"]); |
| 130 | } |
| 131 | foreach (array("username", "name", "last_name", "email") as &$field) { |
| 132 | if (!isset($_POST[$field])) |
| 133 | continue; |
| 134 | $value = strtoupper($_POST[$field]); |
| 135 | $PASSWORD = strtoupper($password); |
| 136 | if (strpos($value, $PASSWORD) !== false || strpos($PASSWORD, $value) !== false) { |
| 137 | $error = $error . $PASSWORD_VALIDATION_ERROR->shared_inclusion; |
| 138 | unset($_POST["password"]); |
| 139 | unset($_POST["password_confirm"]); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | return $error; |
| 144 | } |