blob: e40222fce59292f1f60bfdb7de78cb82d454aac1 [file] [log] [blame]
<?php
include_once 'ldap.php';
include_once 'redis.php';
include_once 'config.php';
include_once 'utils.php';
$TEMPLATE = template_path();
function validate_username(string $username)
{
global $TEMPLATE;
include 'config.php';
include $TEMPLATE . 'strings.php';
$error = "";
if (ldap_user_count($username)) {
$error = $error . $USERNAME_VALIDATION_ERROR->registered;
}
if (preg_match("/\s/", $username)) {
$error = $error . $USERNAME_VALIDATION_ERROR->no_whitespaces;
}
if (strlen($username) > $VAL_USER->max_username) {
echo $VAL_USER->max_username;
echo $USERNAME_VALIDATION_ERROR->smaller_than;
$error = $error . format($USERNAME_VALIDATION_ERROR->smaller_than, ["num" => $VAL_USER->max_username + 1]);
echo $error;
}
if (strlen($username) < $VAL_USER->min_username) {
$error = $error . format($USERNAME_VALIDATION_ERROR->bigger_than, ["num" => $VAL_USER->min_username - 1]);
}
if (preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $username)) {
$error = $error . $USERNAME_VALIDATION_ERROR->no_special_chars;
}
if (preg_match('/^\d/', $username)) {
$error = $error . $USERNAME_VALIDATION_ERROR->no_number_begining;
}
include "blacklists/usernames.php";
if (in_array($username, $USERNAME_BLACKLIST)) {
$error = $error . $USERNAME_VALIDATION_ERROR->blacklisted;
}
return $error;
}
function validate_name(string $name, object $ERRORS)
{
global $TEMPLATE;
include "config.php";
include $TEMPLATE . 'strings.php';
$error = "";
if (preg_match("/\s/", $name)) {
$error = $error . $ERRORS->no_whitespaces;
}
if (strlen($name) > $VAL_USER->max_first_name) {
$error = $error . format($ERRORS->smaller_than, ["num" => $VAL_USER->max_first_name + 1]);
}
if (strlen($name) < $VAL_USER->min_first_name) {
$error = $error . format($ERRORS->bigger_than, ["num" => $VAL_USER->min_first_name - 1]);
}
if (preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\0-9]/', $name)) {
$error = $error . $ERRORS->no_special_chars;
}
return $error;
}
function validate_email(string $email)
{
global $TEMPLATE;
include "config.php";
include $TEMPLATE . 'strings.php';
$error = "";
if (ldap_mail_count($email)) {
$error = $error . format($EMAIL_VALIDATION_ERROR->registered, ["link" => $BASE_URL . "?type=recover"]);
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = $error . $EMAIL_VALIDATION_ERROR->invalid;
} elseif (in_array(explode("@", $email)[1], $MAIL_HOST_BLACKLIST)) {
$error = $error . $EMAIL_VALIDATION_ERROR->blacklisted;
}
$pending = redis_get("pending");
if ($pending) {
$maillist = $pending->mails;
if (in_array($email, $maillist)) {
$error = $error . $EMAIL_VALIDATION_ERROR->pending;
}
}
return $error;
}
function validate_password(string $password)
{
global $TEMPLATE;
include "config.php";
include $TEMPLATE . 'strings.php';
$error = "";
if ($_POST["password"] != $_POST["password_confirm"]) {;
$error = $error . $PASSWORD_VALIDATION_ERROR->no_match;
}
if (strlen($password) < $VAL_USER->min_password) {
$error = $error . format($PASSWORD_VALIDATION_ERROR->bigger_than, ["num" => $VAL_USER->min_password]);
}
if (strlen($password) > $VAL_USER->max_password) {
$error = $error . format($PASSWORD_VALIDATION_ERROR->smaller_than, ["num" => $VAL_USER->max_password]);
}
include "blacklists/password.php";
if (in_array($password, $PASSWORD_BLACKLIST)) {
$error = $error . $PASSWORD_VALIDATION_ERROR->blacklisted;
}
foreach (array("username", "name", "last_name", "email") as &$field) {
if (!isset($_POST[$field]))
continue;
$value = strtoupper($_POST[$field]);
$PASSWORD = strtoupper($password);
if (strpos($value, $PASSWORD) !== false || strpos($PASSWORD, $value) !== false) {
$error = $error . $PASSWORD_VALIDATION_ERROR->shared_inclusion;
break;
}
}
return $error;
}