blob: e40222fce59292f1f60bfdb7de78cb82d454aac1 [file] [log] [blame]
matheusfillipe47cf90b2021-05-13 03:36:21 -03001<?php
matheusfillipec0ce7fa2021-05-13 05:15:37 -03002include_once 'ldap.php';
3include_once 'redis.php';
4include_once 'config.php';
5include_once 'utils.php';
matheusfillipe47cf90b2021-05-13 03:36:21 -03006
matheusfillipec0ce7fa2021-05-13 05:15:37 -03007$TEMPLATE = template_path();
matheusfillipe47cf90b2021-05-13 03:36:21 -03008
9function validate_username(string $username)
10{
matheusfillipec0ce7fa2021-05-13 05:15:37 -030011 global $TEMPLATE;
12 include 'config.php';
13 include $TEMPLATE . 'strings.php';
matheusfillipe47cf90b2021-05-13 03:36:21 -030014 $error = "";
15 if (ldap_user_count($username)) {
16 $error = $error . $USERNAME_VALIDATION_ERROR->registered;
matheusfillipe47cf90b2021-05-13 03:36:21 -030017 }
18 if (preg_match("/\s/", $username)) {
19 $error = $error . $USERNAME_VALIDATION_ERROR->no_whitespaces;
matheusfillipe47cf90b2021-05-13 03:36:21 -030020 }
21 if (strlen($username) > $VAL_USER->max_username) {
matheusfillipec0ce7fa2021-05-13 05:15:37 -030022 echo $VAL_USER->max_username;
23 echo $USERNAME_VALIDATION_ERROR->smaller_than;
matheusfillipe47cf90b2021-05-13 03:36:21 -030024 $error = $error . format($USERNAME_VALIDATION_ERROR->smaller_than, ["num" => $VAL_USER->max_username + 1]);
matheusfillipec0ce7fa2021-05-13 05:15:37 -030025 echo $error;
matheusfillipe47cf90b2021-05-13 03:36:21 -030026 }
27 if (strlen($username) < $VAL_USER->min_username) {
28 $error = $error . format($USERNAME_VALIDATION_ERROR->bigger_than, ["num" => $VAL_USER->min_username - 1]);
matheusfillipe47cf90b2021-05-13 03:36:21 -030029 }
30 if (preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $username)) {
31 $error = $error . $USERNAME_VALIDATION_ERROR->no_special_chars;
matheusfillipe47cf90b2021-05-13 03:36:21 -030032 }
33 if (preg_match('/^\d/', $username)) {
34 $error = $error . $USERNAME_VALIDATION_ERROR->no_number_begining;
matheusfillipe47cf90b2021-05-13 03:36:21 -030035 }
matheusfillipec0ce7fa2021-05-13 05:15:37 -030036 include "blacklists/usernames.php";
matheusfillipe47cf90b2021-05-13 03:36:21 -030037 if (in_array($username, $USERNAME_BLACKLIST)) {
matheusfillipec0ce7fa2021-05-13 05:15:37 -030038 $error = $error . $USERNAME_VALIDATION_ERROR->blacklisted;
matheusfillipe47cf90b2021-05-13 03:36:21 -030039 }
40 return $error;
41}
42
43function validate_name(string $name, object $ERRORS)
44{
matheusfillipec0ce7fa2021-05-13 05:15:37 -030045 global $TEMPLATE;
46 include "config.php";
47 include $TEMPLATE . 'strings.php';
matheusfillipe47cf90b2021-05-13 03:36:21 -030048 $error = "";
49 if (preg_match("/\s/", $name)) {
50 $error = $error . $ERRORS->no_whitespaces;
matheusfillipe47cf90b2021-05-13 03:36:21 -030051 }
52 if (strlen($name) > $VAL_USER->max_first_name) {
53 $error = $error . format($ERRORS->smaller_than, ["num" => $VAL_USER->max_first_name + 1]);
matheusfillipe47cf90b2021-05-13 03:36:21 -030054 }
55 if (strlen($name) < $VAL_USER->min_first_name) {
56 $error = $error . format($ERRORS->bigger_than, ["num" => $VAL_USER->min_first_name - 1]);
matheusfillipe47cf90b2021-05-13 03:36:21 -030057 }
58 if (preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\0-9]/', $name)) {
59 $error = $error . $ERRORS->no_special_chars;
matheusfillipe47cf90b2021-05-13 03:36:21 -030060 }
61 return $error;
62}
63
64function validate_email(string $email)
65{
matheusfillipec0ce7fa2021-05-13 05:15:37 -030066 global $TEMPLATE;
67 include "config.php";
68 include $TEMPLATE . 'strings.php';
matheusfillipe47cf90b2021-05-13 03:36:21 -030069 $error = "";
70
71 if (ldap_mail_count($email)) {
matheusfillipec0ce7fa2021-05-13 05:15:37 -030072 $error = $error . format($EMAIL_VALIDATION_ERROR->registered, ["link" => $BASE_URL . "?type=recover"]);
matheusfillipe47cf90b2021-05-13 03:36:21 -030073 }
74 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
75 $error = $error . $EMAIL_VALIDATION_ERROR->invalid;
matheusfillipe47cf90b2021-05-13 03:36:21 -030076 } elseif (in_array(explode("@", $email)[1], $MAIL_HOST_BLACKLIST)) {
77 $error = $error . $EMAIL_VALIDATION_ERROR->blacklisted;
matheusfillipe47cf90b2021-05-13 03:36:21 -030078 }
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;
matheusfillipe47cf90b2021-05-13 03:36:21 -030084 }
85 }
86 return $error;
87}
88
89
90function validate_password(string $password)
91{
matheusfillipec0ce7fa2021-05-13 05:15:37 -030092 global $TEMPLATE;
93 include "config.php";
94 include $TEMPLATE . 'strings.php';
matheusfillipe47cf90b2021-05-13 03:36:21 -030095 $error = "";
96 if ($_POST["password"] != $_POST["password_confirm"]) {;
97 $error = $error . $PASSWORD_VALIDATION_ERROR->no_match;
matheusfillipe47cf90b2021-05-13 03:36:21 -030098 }
99 if (strlen($password) < $VAL_USER->min_password) {
100 $error = $error . format($PASSWORD_VALIDATION_ERROR->bigger_than, ["num" => $VAL_USER->min_password]);
matheusfillipe47cf90b2021-05-13 03:36:21 -0300101 }
102 if (strlen($password) > $VAL_USER->max_password) {
103 $error = $error . format($PASSWORD_VALIDATION_ERROR->smaller_than, ["num" => $VAL_USER->max_password]);
matheusfillipe47cf90b2021-05-13 03:36:21 -0300104 }
matheusfillipec0ce7fa2021-05-13 05:15:37 -0300105 include "blacklists/password.php";
matheusfillipe47cf90b2021-05-13 03:36:21 -0300106 if (in_array($password, $PASSWORD_BLACKLIST)) {
107 $error = $error . $PASSWORD_VALIDATION_ERROR->blacklisted;
matheusfillipe47cf90b2021-05-13 03:36:21 -0300108 }
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;
matheusfillipe47cf90b2021-05-13 03:36:21 -0300116 break;
117 }
118 }
119 return $error;
120}