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