<?php
    require_once('include/functions/valid_username.php');

    $_cache = isset($_POST['cache']) ? trim(strval($_POST['cache'])) : '';
    $_name = isset($_POST['name'])  ? trim(strval($_POST['name'])) : '';
    $_pass1 = isset($_POST['pass1']) ? trim(strval($_POST['pass1'])) : '';
    $_pass2 = isset($_POST['pass2']) ? trim(strval($_POST['pass2'])) : '';

    if (!$_name)
    {
        make_cookie('notice', 'You did not enter a username.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }
    elseif (strlen($_name) < USERNAME_MIN)
    {
        make_cookie('notice', 'Your username is not long enough. Your username must be at least ' . USERNAME_MIN . ' characters long.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }
    elseif (strlen($_name) > USERNAME_MAX)
    {
        make_cookie('notice', 'Your username is too long. Your username cannot be more than ' . USERNAME_MAX . ' characters long.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }
    elseif (mysqli_num_rows(mysqli_query_logged("SELECT * FROM members WHERE username = " . sq($_name))))
    {
        make_cookie('notice', 'Sorry, that username is already in use.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }
    elseif (!is_valid_username($_name))
    {
        make_cookie('notice', 'Your username is not valid. Please only use alpha-numerica characters.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }
        
    if (!$_pass1)
    {
        make_cookie('notice', 'You need to enter a password.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }
    
    if (!$_pass2)
    {
        make_cookie('notice', 'You need to re-enter your password.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }

    if ($_pass1 && $_pass2 && $_pass1 != $_pass2)
    {
        make_cookie('notice', 'Your passwords do not match.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }
    elseif (strlen($_pass1) < PASSWORD_MIN)
    {
        make_cookie('notice', 'Your password is not long enough. Your password must be at least ' . PASSWORD_MIN . ' characters long.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }
    elseif (strlen($_pass1) > PASSWORD_MAX)
    {
        make_cookie('notice', 'Your password is too long. Your password cannot be more than ' . PASSWORD_MAX . ' characters long.');
        header('Location: ./?s=new_complete&c=' . $_cache);
        die;
    }

    $cache = mysqli_query_logged("SELECT email FROM members_create WHERE cache = " . sq($_cache));
    if ($cache = mysqli_fetch_assoc($cache))
    {
        $email = $cache['email'];
    }
    else
    {
        make_cookie('notice', 'Sorry, there was an error in the registration process. Please try clicking the link we emailed you again to start over.');
        header('Location: ./?s=new');
        die;
    }
    
    if (mysqli_num_rows(mysqli_query_logged("SELECT email FROM members WHERE username = '" . addslashes($email) . "'")))
    {
        make_cookie('notice', 'Sorry, that email address is already in use.');
        header('Location: ./?s=new');
        die;
    }
    
    mysqli_query_logged("INSERT INTO members SET username = " . sq($_name) . ", password = '" . md5(strtolower(rq($_pass1))) . "', email = '" . addslashes($email) . "', created_on = NOW()");
    $mysql_insert_id = mysqli_insert_id($GLOBALS['mysqli']);
    mysqli_query_logged("INSERT INTO members_extras SET user_id = '" . $mysql_insert_id . "'");
    mysqli_query_logged("INSERT INTO members_laston SET user_id = '" . $mysql_insert_id . "', laston = NOW()");
    mysqli_query_logged("DELETE FROM members_create WHERE email = '" . addslashes($email) . "'");
    
    make_cookie('login_email', $email);
    make_cookie('login_password', md5(strtolower(rq($_pass1))));

    header('Location: ./?s=userinfo');
    die;
?>