<commit_before><?php
include_once('common.inc');

global $new_login, $login_name;
$login_name = '';
$new_login = FALSE;

function process_query_string() {
    global $new_login;
    if (isset($_GET['new'])) {
        $new_login = $_GET['new'];
    }
}

function process_form_data() {
    if (isset($_POST['login-button'])) {
        process_login();
    } else if (isset($_POST['new-login-button'])) {
        process_new_login();
    } else if (isset($_POST['forgot-button'])) {
        process_forgot();
    }
}

function process_login() {
    global $login_name, $cookie;
    
    if (!$_POST['name_field'] || !$_POST['password-field']) {
        set_user_message("Missing login name or password", "failure");
        return;
    }

    if (!($connection = connect_to_database())) {
        set_user_message("Failed accessing database", "failure");
        return;
    }

    $login_name = mysqli_real_escape_string($connection, $_POST['name_field']);
    $password = mysqli_real_escape_string($connection, $_POST['<PASSWORD>']);

    $query = "SELECT `user_id`, `login_name`
                FROM `user_table` 
                WHERE `login_name` = '$login_name' AND 
                    `password` = MD5( CONCAT( `<PASSWORD>`, <PASSWORD>' ) ) AND 
                    `account_closed_date` IS NULL";
    $results = mysqli_query($connection, $query);
    if (!$results) {
        set_user_message(mysqli_error($connection), "failure");
        return;
    }
    $result = mysqli_fetch_array($results);
    if (! $result) {
        set_user_message("Login name not found, or password doesn't match.", "warning");
        return;
    }

    $cookie = set_session_id($result['user_id'], $connection);
    header("location: todo.php");
}

function process_new_login() {
    global $login_name, $cookie;
    
    if (!($connection = connect_to_database())) {
        set_user_message("Failed accessing database", "failure");
        return;
    }
    
    $login_name = mysqli_real_escape_string($connection, $_POST['name-field']);
    $password = mysqli_real_escape_string($connection, $_POST['password-field']);

    if (!$_POST['name-field'] || !$_POST['password-field']) {
        set_user_message("You must provide a login name and password", "warning");
        return;
    }
    
    if (!$_POST['repeat-password-field'] || $_POST['repeat-password-field'] != $_POST['password-field']) {
        set_user_message("The passwords do not match.", "warning");
        return;
    }
    
    $user_query = "INSERT INTO `user_table` (
            `login_name` , `password_salt` 
        ) VALUES (
            '$login_name' , MD5( CONCAT( '$login_name' , NOW() ) )
        )";
    $user_results = mysqli_query($connection, $user_query);
    if (!$user_results) {
        set_user_message(mysqli_error($connection), "failure");
        return;
    }
    $user_id = mysqli_insert_id($connection);
    
    $password_query = "UPDATE `user_table`
        SET `password` = <PASSWORD>( CONCAT( `password_salt`, <PASSWORD>' ) )
        WHERE `user_id` = '$user_id'";
    $password_results = mysqli_query($connection, $password_query);
    if (! $password_results) {
        set_user_message(mysqli_error($connection), "failure");
        return;
    }

    $cookie = set_session_id($user_id, $connection);
    header("location: todo.php");
}

function process_forgot() {
    
}

function show_main_login_form() {
    global $login_name, $new_login;

    echo "
                <div id='login-main'>
                    <form id='login_form' name='login_form' method='POST'>
                        <label for='name_field'>Sign-on name:</label>
                        <input type='text' name='name_field' value='$login_name'></input>
                        <label for='password-field'>Password:</label>
                        <input type='password' name='password-field'></input>";
    if ($new_login) {
        echo "
                        <label for='repeat-password_field'>Repeat password:</label>
                        <input type='password' name='repeat-password-field'></input>
                        <br/>
                        <input type='submit' name='new-login-button' value='Create login'></input>";
    } else {
        echo "
                        <br/>
                        <input type='submit' name='login-button' value='Login'></input>";
    }
    echo "<commit_msg>Fixed inconsistency in name of 'name-field'<commit_after><?php
include_once('common.inc');

global $new_login, $login_name;
$login_name = '';
$new_login = FALSE;

function process_query_string() {
    global $new_login;
    if (isset($_GET['new'])) {
        $new_login = $_GET['new'];
    }
}

function process_form_data() {
    if (isset($_POST['login-button'])) {
        process_login();
    } else if (isset($_POST['new-login-button'])) {
        process_new_login();
    } else if (isset($_POST['forgot-button'])) {
        process_forgot();
    }
}

function process_login() {
    global $login_name, $cookie;
    
    if (!$_POST['name-field'] || !$_POST['password-field']) {
        set_user_message("Missing login name or password", "failure");
        return;
    }

    if (!($connection = connect_to_database())) {
        set_user_message("Failed accessing database", "failure");
        return;
    }

    $login_name = mysqli_real_escape_string($connection, $_POST['name-field']);
    $password = mysqli_real_escape_string($connection, $_POST['password-field']);

    $query = "SELECT `user_id`, `login_name`
                FROM `user_table` 
                WHERE `login_name` = '$login_name' AND 
                    `password` = <PASSWORD>( CONCAT( `<PASSWORD>`, <PASSWORD>' ) ) AND 
                    `account_closed_date` IS NULL";
    $results = mysqli_query($connection, $query);
    if (!$results) {
        set_user_message(mysqli_error($connection), "failure");
        return;
    }
    $result = mysqli_fetch_array($results);
    if (! $result) {
        set_user_message("Login name not found, or password doesn't match.", "warning");
        return;
    }

    $cookie = set_session_id($result['user_id'], $connection);
    header("location: todo.php");
}

function process_new_login() {
    global $login_name, $cookie;
    
    if (!($connection = connect_to_database())) {
        set_user_message("Failed accessing database", "failure");
        return;
    }
    
    $login_name = mysqli_real_escape_string($connection, $_POST['name-field']);
    $password = mysqli_real_escape_string($connection, $_POST['password-field']);

    if (!$_POST['name-field'] || !$_POST['password-field']) {
        set_user_message("You must provide a login name and password", "warning");
        return;
    }
    
    if (!$_POST['repeat-password-field'] || $_POST['repeat-password-field'] != $_POST['password-field']) {
        set_user_message("The passwords do not match.", "warning");
        return;
    }
    
    $user_query = "INSERT INTO `user_table` (
            `login_name` , `password_salt` 
        ) VALUES (
            '$login_name' , MD5( CONCAT( '$login_name' , NOW() ) )
        )";
    $user_results = mysqli_query($connection, $user_query);
    if (!$user_results) {
        set_user_message(mysqli_error($connection), "failure");
        return;
    }
    $user_id = mysqli_insert_id($connection);
    
    $password_query = "UPDATE `user_table`
        SET `password` = MD5( CONCAT( `password_salt`, <PASSWORD>' ) )
        WHERE `user_id` = '$user_id'";
    $password_results = mysqli_query($connection, $password_query);
    if (! $password_results) {
        set_user_message(mysqli_error($connection), "failure");
        return;
    }

    $cookie = set_session_id($user_id, $connection);
    header("location: todo.php");
}

function process_forgot() {
    
}

function show_main_login_form() {
    global $login_name, $new_login;

    echo "
                <div id='login-main'>
                    <form id='login_form' name='login_form' method='POST'>
                        <label for='name-field'>Sign-on name:</label>
                        <input type='text' name='name-field' value='$login_name'></input>
                        <label for='password-field'>Password:</label>
                        <input type='password' name='password-field'></input>";
    if ($new_login) {
        echo "
                        <label for='repeat-password_field'>Repeat password:</label>
                        <input type='password' name='repeat-password-field'></input>
                        <br/>
                        <input type='submit' name='new-login-button' value='Create login'></input>";
    } else {
        echo "
                        <br/>
                        <input type='submit' name='login-button' value='Login'></input>";
    }
    echo "
{'fraction_non_alphanumeric': 0.15576748410535876, 'fraction_numerical': 0.00045413260672116256, 'mean_word_length': 2.6102459016393444, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 52, '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': '1481023', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2589, 'n_words': 491}