<?php
    if (isset($_GET['source'])) {
        highlight_file($_SERVER['SCRIPT_FILENAME']);
        exit;
    }
    session_start();
?>

<html>
    <head>
        <title>User Registration Form</title>
        
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="theme-color" content="#B5C7DD" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <link rel="stylesheet" href="css/reset.css">
        <link rel="stylesheet" href="css/mandy.css">
        <link rel="stylesheet" href="css/form.css">

<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
    </head>
    
    <body>
        <?php
        $user = $email = $pass1 = $pass2 = "";
        $userError = $emailError = $pass1Error = $pass2Error = "";
        $uvalid = $evalid = $pvalid = false;
        
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            
            if (empty($_POST["username"])) {
                $userError = "<span class=\"error\">
                Username is required
                </span>";
            }
            else {
                $_SESSION["user"] = $user = test_input($_POST["username"]);
                if (!preg_match("/^[a-zA-Z0-9 ]*$/", $user)) { # performs regular expression match
                    $userError = "<span class=\"error\">
                    Only letters, numbers, and white space allowed
                    </span>";
                }
                else {
                    $uvalid = true;
                }
            }
            if (empty($_POST["email"])) {
                $emailError = "<span class=\"error\">
                Email is required
                </span>";
            }
            else {
                $_SESSION["email"] = $email = test_input($_POST["email"]);
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { # validates whether value is a valid email address
                    $emailError = "<span class=\"error\">
                    Invalid email format
                    </span>";
                }
                else {
                    $evalid = true;
                }
            }
            if (empty($_POST["pwd1"])) {
                $pass1Error = "<span class=\"error\">
                Password is required
                </span>";
            }
            else {
                $_SESSION["pass1"] = $pass1 = test_input($_POST["pwd1"]);
            }
            if (empty($_POST["pwd2"])) {
                $pass2Error = "<span class=\"error\">
                Password is required
                </span>";
            }
            else {
                $_SESSION["pass2"] = $pass2 = test_input($_POST["pwd2"]);
                if ($pass2 != $pass1) {
                    $pass2Error = "<span class=\"error\">
                    Passwords do not match
                    </span>";
                }
                else {
                    $pvalid = true;
                }
            }
        }

        if($uvalid == true && $evalid == true && $pvalid == true)  {
            header("Location: form_data.php");
        }
            
        function test_input($data) {
            $data = trim($data); # remove any whitespace from beginning and end of string
            $data = stripcslashes($data); # strip string of backslashes
            $data = htmlspecialchars($data); # convert code to HTML escaped code
            return $data;
        }        
        ?>
        
        <h3 class="nudge">User Registration Form</h3>
        <form class="flex-form" method="POST">
            
            <div id="u">
            <label>Username:</label>
            <input name="username" type="text" value="<?php echo $user;?>">
            <?php echo $userError;?> 
            </div> 
            
            <div id="e">
            <label>Email Address:</label>
            <input name="email" type="text" value="<?php echo $email;?>">
            <?php echo $emailError;?>
            </div>
            
            <div id="p">
            <label>Password:</label>
            <input name="pwd1" type="password" value="<?php echo $pass1;?>">
            <?php echo $pass1Error;?> 
            </div>
            
            <div id="r">
            <label>Re-enter Password:</label>
            <input name="pwd2" type="password" value="<?php echo $pass2;?>">
            <?php echo $pass2Error;?>
            <p><span class="error">Required field</span></p>
            </div>
                
            
            <p><input class="button" name="reset" type="reset" value="Reset">
            <input class="button" name="submit" type="submit" value="Submit"></p>
            <br>
        </form>
        
    <p class="hide">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
</p>
  
    <div class="stretch"></div>
        
    </body>
</html>