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