<?php
error_reporting(E_ALL);//turn off notices
include 'connection.php';//connect to the db
//print_r($_SERVER);
/*echo $_SERVER['HTTP_REFERER'].'<br>';
echo "You sent data from ".$_SERVER['HTTP_ORIGIN'].$_SERVER['SCRIPT_NAME'];*/
//this ensures that the form data not only equals the submit button's value on this form, but also that the last page the user was on is this page...double protection!
if ($_POST['submit'] == 'Register'){
    if ($_POST['captcha'] != $_SESSION['captcha']){
       $msg = 'Please re-enter captcha';
        //tests to see if the user has entered the same captcha number as the session 
        echo $msg;
    }
    else {
        //var_dump($_POST);
        $fullname = $_POST['fullname'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $birthday = $_POST['birthday'];//needs to be yyyy-mm-dd
        //get the form data
        //NEED TO CHECK DATABASE NOW!!!!
        $checksql = $dbh->prepare("select count(id) as numusers from users where email = ?");
        $checksql->bindValue(1,$email,PDO::PARAM_STR);
        //count gets the number of ids
        //numusers is a way that I can access the number that I understand
        $checksql->execute();
        //USE THIS IF YOU THINK YOUR QUERY IS SCREWED UP!!!IF IT DOESN"T SHOW AN ERROR PASTE IN PHPMYADMIN
        //print_r($checksql->errorInfo());
        $numusers = $checksql->fetch();
        if ($numusers['numusers'] > 0){
            echo 'It appears your email is already in our database. Please click <a href="reset.php">here</a> to reset your password';
        }
        else
        {
        
  $inssql = $dbh->prepare("INSERT INTO users (fullname, email, phone,birthday, signupdate) VALUES (?,?,?,?,now())");
//using parametric binding to protect the query...make sure it's a string       
  $inssql->bindValue(1,$fullname,PDO::PARAM_STR);     
  $inssql->bindValue(2,$email,PDO::PARAM_STR);     
  $inssql->bindValue(3,$phone,PDO::PARAM_STR);   
  $inssql->bindValue(4,$birthday,PDO::PARAM_STR);  
  $inssql->execute();    
              //tells us all the errors from the database call!!!
            $id = $dbh->lastInsertId();
            $password = makepass();
            
            //both id and password came from within. id from the lastinsertid and password from the password generation function 
          $credsql =  $dbh->prepare("insert into credentials (userid,password) values ('$id','$password')");
            $credsql->execute();
            
           $link = base64_encode($email);
            
            $content = 'Thank you...your password is '.$password.'. Please click the following link to activate: <a href="http://mm214.com/webd166members/activate.php?link='.$link.'">ACTIVATE</a>';
        //use the below to find sequel errors        //print_r($inssql->errorInfo());
   //send link to user with password and activate link         
            echo "email is $email ";
         $bool =  mail($email,"Your Membership at mm214.com",$content,"Content-type: text/html");
            
            if ($bool){
            echo "Please check your email!";
            }          
        
        }
    }
    
    
    
} 
//test captcha

?>
<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="manifest" href="site.webmanifest">
        <link rel="apple-touch-icon" href="icon.png">
        <!-- Place favicon.ico in the root directory -->

        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/main.css">
    </head>
    <body>
        <!--[if lte IE 9]>
            <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="https://browsehappy.com/">upgrade your browser</a> to improve your experience and security.</p>
        <![endif]-->

        <!-- Add your site or application content here -->
        <script src="js/vendor/modernizr-3.5.0.min.js"></script>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
        <script>window.jQuery || document.write('<script src="js/vendor/jquery-3.2.1.min.js"><\/script>')</script>
        <script src="js/plugins.js"></script>
        <script src="js/main.js"></script>
        
        
        <?php
        echo $msg;
        ?>
        
        
        
        
        
        
        <form action ="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="fullname" placeholder="Full Name Here" required><br><input type="email" name="email" placeholder="Email Here" required><br>
            <input type="tel" name="phone" placeholder="Phone Here" required><br>
            <input type="date" name="birthday" placeholder="mm/dd/yyyy"><br>
        <?php
            //include captcha from other script
          include 'captcha.php';  
        ?>    
            <br>
            <input type="text" name="captcha" placeholder="Enter the above"><br>
        <input type="submit" name="submit" value="Register">
        </form>
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

        <!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
        <script>
            window.ga=function(){ga.q.push(arguments)};ga.q=[];ga.l=+new Date;
            ga('create','UA-XXXXX-Y','auto');ga('send','pageview')
        </script>
        <script src="https://www.google-analytics.com/analytics.js" async defer></script>
    </body>
</html>
