<?php

include ('includes/header.html');

# Execute the registration form once the submit button is pressed.
if ($_POST['submit']) {

    # Initialize an array which will be used to store any missing field errors.
    $errors = array();

    # Validate that a username has been entered.
    if (empty($_POST['username'])) {
        $errors[] = 'Please enter a username.';
    } else {
        if (eregi('^[a-z0-9.-_]{4,20}$', $_POST['username'])) {
            $username = mysqli_real_escape_string($dbc, trim($_POST['username']));
        } else {
            $errors[] = 'Your username must contain only the characters listed below.';
        }
    }

    # Validate that two matching passwords have been entered.
    if (empty($_POST['password'])) {
        $errors[] = 'Please enter a password.';
    } else {
        if (eregi('^[a-z0-9]{4,20}$', $_POST['password'])) {
            $password = $_POST['password'];
        } else {
            $errors[] = 'Your password must contain only the characters listed below.';
        }
        if (empty($_POST['password2'])) {
            $errors[] = 'Please confirm your password.';
        } else {
            if (eregi('^[a-z0-9]{4,20}$', $_POST['password'])) {
                $password2 = $_POST['password2'];
            } else {
                $errors[] = 'Your password must contain only the characters listed below.';
            }

            if ($password == $password2) {
                $password3 = mysqli_real_escape_string($dbc, trim($_POST['password']));
            } else {
                $errors[] = 'Your passwords don\'t match.';
            }
        }
    }

    # Validate that an email address has been entered.
    if (empty($_POST['email'])) {
        $errors[] = 'Please enter an email address.';
    } else {
    	if (eregi('^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$', $_POST['email'])) {
    		$email = mysqli_real_escape_string($dbc, trim($_POST['email']));
    	} else {
    		$errors[] = 'Your email must contain only the characters listed below.';
    	}
        
    }

    # Execute this block of code if there are no validation errors.
    if (empty($errors)) {

        # Checks if the username supplied already exists.
        $usercheck = "SELECT username FROM users WHERE username='$username'";
        $usercheck2 = mysqli_query($dbc, $usercheck);

        # Checks if the password supplied already exists.
        $emailcheck = "SELECT email FROM users WHERE email='$email'";
        $emailcheck2 = mysqli_query($dbc, $emailcheck);

        # If the username already exists, do not register the user and inform them of this.
        if (mysqli_num_rows($usercheck2) == 1) {
            echo "Sorry the username is already taken.";
        } else {
            # If the email address already exists, do not register the user and inform them of this.
            if (mysqli_num_rows($emailcheck2) == 1) {
                echo "Sorry this email address has already been registered.";
            } else {
                # Insert all the details into the database.
                $ip = $_SERVER['REMOTE_ADDR'];
                $q = "INSERT INTO users (username, password, email, date, ip) VALUES ('$username', SHA1('$password3'), '$email', NOW(), '$ip' )";
                # If the row was successfully added, take the user to registered.php.
                if ($r = mysqli_query($dbc, $q)) {
                    header("Location:registered.php");
                } else {
                    # If the row was not added successfully, echo an error.
                    echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
                }
            }
        }

        # If there are validation errors, echo each one out from the errors array.
    } else {
        echo "Please correct the following errors:<br />";
        foreach ($errors as $msg) {
            echo '- ' . $msg . '<br />';
        }
    }
}

# This is an HTML form for the user to fill in to register.


?>
<style type="text/css">
@import url(main.css);
</style>
<script type="text/javascript">
window.onload=function(){document.forms['register'].elements['username'].focus();}
</script>

<div id="title">
<h2>Register</h2>
</div>

<form action="register.php" method="post" name="register">

<div id="box">
<p>Username:</p> <p><input type="text" name="username" size="27" maxlength="30" /></p>
<p class="small-reg">A-Z, 0-9, dots, dashes, underscores.</p>
<p>Password:</p> <p><input type="password" name="password" size="27" maxlength="30" /></p>
<p class="small-reg">A-Z, 0-9.</p>
<p>Confirm Password:</p> <p><input type="password" name="password2" size="27" maxlength="30" /></p>
<p class="small-reg">A-Z, 0-9.</p>
<p>Email:</p> <p><input type="text" name="email" size="27" maxlength="30" /></p>
<p class="small-reg">Valid email.</p>
<p><input type="submit" name="submit" value="Register" /></p>
</div>

</form>
<?php

include ('includes/footer.html');

?>