<commit_before><?php

define('INSIDE', true);
define('UEC_INLOGIN', true);

$_DontShowMenus = true;

$_EnginePath = './';
include($_EnginePath.'common.php');
include_once($_EnginePath . 'modules/session/_includes.php');

use UniEngine\Engine\Includes\Helpers\Users;
use UniEngine\Engine\Modules\Session;

includeLang('login');

$sessionCookieKey = getSessionCookieKey();

if($_POST)
{
    if ($_POST['uniSelect'] != LOGINPAGE_UNIVERSUMCODE) {
        message($_Lang['Login_BadUniversum'], $_Lang['Err_Title']);
    }

    if (time() < SERVER_MAINOPEN_TSTAMP) {
        $serverStartMessage = sprintf(
            $_Lang['Login_UniversumNotStarted'],
            prettyDate('d m Y', SERVER_MAINOPEN_TSTAMP, 1),
            date('H:i:s', SERVER_MAINOPEN_TSTAMP)
        );

        message($serverStartMessage, $_Lang['Page_Title']);
    }

    $Username = trim($_POST['username']);
    if (preg_match(REGEXP_USERNAME_ABSOLUTE, $Username)) {
        $Search['mode'] = 1;
        $Search['where'] = "`username` = '{$Username}'";
        $Search['password'] = <PASSWORD>($_POST['password']);
        $Search['IPHash'] = md5(Users\Session\getCurrentIP());

        $rateLimitVerificationResult = Session\Utils\RateLimiter\verifyLoginRateLimit([
            'ipHash' => $Search['IPHash'],
        ]);

        if ($rateLimitVerificationResult['isIpRateLimited']) {
            $Search['error'] = 5;
            $Search['where'] = '';
        }
    } else {
        $Search['error'] = 1;
    }
} else if (!empty($_COOKIE[$sessionCookieKey])) {
    $loginAttemptResult = Session\Input\CookieLogin\handleCookieLogin([]);

    if ($loginAttemptResult['isSuccess']) {
        Session\Utils\Redirects\redirectToOverview();

        die();
    }

    $Search['mode'] = 2;

    switch ($loginAttemptResult['error']['code']) {
        case 'NO_COOKIE':
            $Search['error'] = 2;
            break;
        case 'INVALID_USER_ID':
            $Search['error'] = 2;
            break;
        case 'USER_NOT_FOUND':
            $Search['error'] = 3;
            break;
        case 'INVALID_PASSWORD':
            $Search['error'] = 4;
            break;
    }
}

if(!empty($Search['where']))
{
    $Query_User_Fields = "`id`, `username`, `password`, `isAI`";
    $Query_User_GetData = "SELECT {$Query_User_Fields} FROM {{table}} WHERE {$Search['where']} LIMIT 1;";
    $UserData = doquery($Query_User_GetData, 'users', true);
    if($UserData['id'] > 0)
    {
        include_once($_EnginePath.'/includes/functions/IPandUA_Logger.php');

        $PasswordOK = <PASSWORD>;
        if($Search['mode'] == 1 AND $UserData['password'] == $Search['password'])
        {
            $PasswordOK = true;
        }
        if($PasswordOK === true)
        {
            // User is ready to Login
            if($Search['mode'] == 1)
            {
                if($_POST['rememberme'] == 'on')
                {
                    $Cookie_Expire = time() + TIME_YEAR;
                    $Cookie_Remember = 1;
                }
                else
                {
                    $Cookie_Expire = 0;
                    $Cookie_Remember = 0;
                }

                $Cookie_Set = Session\Utils\Cookie\packSessionCookie([
                    'userId' => $UserData['id'],
                    'username' => $UserData['username'],
                    'obscuredPasswordHash' => Session\Utils\Cookie\createCookiePasswordHash([
                        'passwordHash' => $UserData['password'],
                    ]),
                    'isRememberMeActive' => $Cookie_Remember,
                ]);

                setcookie($sessionCookieKey, $Cookie_Set, $Cookie_Expire, '/', '', false, true);
            }

            IPandUA_Logger($UserData);
            header("Location: ./overview.php");
            die();
        }
        else
        {
            $Search['error'] = 4;
        }
    }
    else
    {
        $Search['error'] = 3;
    }
}
if(!empty($Search['error']))
{
    if (
        $Search['mode'] == 1 &&
        !empty($Search['IPHash'])
    ) {
        Session\Utils\RateLimiter\updateLoginRateLimiterEntry([
            'ipHash' => $Search['IPHash'],
        ]);
    }

    if($UserData['id'] > 0)
    {
        IPandUA_Logger($UserData, true);
    }
    if($Search['mode'] == 2)
    {
        setcookie($sessionCookieKey, false, 0, '/', '');
    }
    if($Search['error'] == 1)
    {
        message($_Lang['Login_BadSignsUser'], $_Lang['Err_Title']);<commit_msg>GH-150 Remove redundant cookie clearing<commit_after><?php

define('INSIDE', true);
define('UEC_INLOGIN', true);

$_DontShowMenus = true;

$_EnginePath = './';
include($_EnginePath.'common.php');
include_once($_EnginePath . 'modules/session/_includes.php');

use UniEngine\Engine\Includes\Helpers\Users;
use UniEngine\Engine\Modules\Session;

includeLang('login');

$sessionCookieKey = getSessionCookieKey();

if($_POST)
{
    if ($_POST['uniSelect'] != LOGINPAGE_UNIVERSUMCODE) {
        message($_Lang['Login_BadUniversum'], $_Lang['Err_Title']);
    }

    if (time() < SERVER_MAINOPEN_TSTAMP) {
        $serverStartMessage = sprintf(
            $_Lang['Login_UniversumNotStarted'],
            prettyDate('d m Y', SERVER_MAINOPEN_TSTAMP, 1),
            date('H:i:s', SERVER_MAINOPEN_TSTAMP)
        );

        message($serverStartMessage, $_Lang['Page_Title']);
    }

    $Username = trim($_POST['username']);
    if (preg_match(REGEXP_USERNAME_ABSOLUTE, $Username)) {
        $Search['mode'] = 1;
        $Search['where'] = "`username` = '{$Username}'";
        $Search['password'] = <PASSWORD>($_POST['password']);
        $Search['IPHash'] = md5(Users\Session\getCurrentIP());

        $rateLimitVerificationResult = Session\Utils\RateLimiter\verifyLoginRateLimit([
            'ipHash' => $Search['IPHash'],
        ]);

        if ($rateLimitVerificationResult['isIpRateLimited']) {
            $Search['error'] = 5;
            $Search['where'] = '';
        }
    } else {
        $Search['error'] = 1;
    }
} else if (!empty($_COOKIE[$sessionCookieKey])) {
    $loginAttemptResult = Session\Input\CookieLogin\handleCookieLogin([]);

    if ($loginAttemptResult['isSuccess']) {
        Session\Utils\Redirects\redirectToOverview();

        die();
    }

    $Search['mode'] = 2;

    switch ($loginAttemptResult['error']['code']) {
        case 'NO_COOKIE':
            $Search['error'] = 2;
            break;
        case 'INVALID_USER_ID':
            $Search['error'] = 2;
            break;
        case 'USER_NOT_FOUND':
            $Search['error'] = 3;
            break;
        case 'INVALID_PASSWORD':
            $Search['error'] = 4;
            break;
    }
}

if(!empty($Search['where']))
{
    $Query_User_Fields = "`id`, `username`, `password`, `isAI`";
    $Query_User_GetData = "SELECT {$Query_User_Fields} FROM {{table}} WHERE {$Search['where']} LIMIT 1;";
    $UserData = doquery($Query_User_GetData, 'users', true);
    if($UserData['id'] > 0)
    {
        include_once($_EnginePath.'/includes/functions/IPandUA_Logger.php');

        $PasswordOK = false;
        if($Search['mode'] == 1 AND $UserData['password'] == $Search['password'])
        {
            $PasswordOK = true;
        }
        if($PasswordOK === true)
        {
            // User is ready to Login
            if($Search['mode'] == 1)
            {
                if($_POST['rememberme'] == 'on')
                {
                    $Cookie_Expire = time() + TIME_YEAR;
                    $Cookie_Remember = 1;
                }
                else
                {
                    $Cookie_Expire = 0;
                    $Cookie_Remember = 0;
                }

                $Cookie_Set = Session\Utils\Cookie\packSessionCookie([
                    'userId' => $UserData['id'],
                    'username' => $UserData['username'],
                    'obscuredPasswordHash' => Session\Utils\Cookie\createCookiePasswordHash([
                        'passwordHash' => $UserData['password'],
                    ]),
                    'isRememberMeActive' => $Cookie_Remember,
                ]);

                setcookie($sessionCookieKey, $Cookie_Set, $Cookie_Expire, '/', '', false, true);
            }

            IPandUA_Logger($UserData);
            header("Location: ./overview.php");
            die();
        }
        else
        {
            $Search['error'] = 4;
        }
    }
    else
    {
        $Search['error'] = 3;
    }
}
if(!empty($Search['error']))
{
    if (
        $Search['mode'] == 1 &&
        !empty($Search['IPHash'])
    ) {
        Session\Utils\RateLimiter\updateLoginRateLimiterEntry([
            'ipHash' => $Search['IPHash'],
        ]);
    }

    if($UserData['id'] > 0)
    {
        IPandUA_Logger($UserData, true);
    }
    if($Search['error'] == 1)
    {
        message($_Lang['Login_BadSignsUser'], $_Lang['Err_Title']);
{'fraction_non_alphanumeric': 0.1652232594758745, 'fraction_numerical': 0.005511191092115623, 'mean_word_length': 2.084287200832466, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '587285', 'n_tokens_mistral': 2736, 'n_tokens_neox': 2602, 'n_words': 394}