lowheartrate

register.php 11242016_0359

Nov 24th, 2016
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.43 KB | None | 0 0
  1. <?php
  2. // include common.inc.php
  3. include 'core/common.inc.php';
  4. // show header / add title
  5. showHeader('Registration');
  6. // protect page from users already logged in
  7. protect();
  8. // turn off error_reporting
  9. error_reporting(0);
  10. ?>
  11.  
  12. <center>
  13.   <form enctype="multipart/form-data" action="" method="post" class="register_module">
  14.  
  15.     <h2 style="text-align:left;font-family:'Roboto',sans-serif;">Account Credentials<span style="font-size:12px;font-weight:400;"> (required)</span></h2>
  16.  
  17.     <div class="account_credentials">
  18.       <input class="register_module protect_from_spaces" type="text" name="username" placeholder="choose a username*" maxlength="30" required><br />
  19.       <input class="register_module protect_from_spaces" type="email" name="email" placeholder="email address*" required><br />
  20.  
  21.       <div class="row_inline">
  22.         <input class="register_module input protect_from_spaces" type="password" name="password" placeholder="password*" maxlength="18" required>
  23.         <input class="register_module input protect_from_spaces" type="password" name="cpassword" placeholder="confirm password*" maxlength="18" required><br />
  24.       </div>
  25.     </div>
  26.       <br />
  27.  
  28.       <!-- here add user details section (name, avatar, birthday, etc...) -->
  29.       <h2 style="text-align:left;font-family:'Roboto',sans-serif;">Account Details<span style="font-size:12px;font-weight:400;"> (optional)</span></h2>
  30.  
  31.       <br />
  32.       <div class="account_details">
  33.         <p style="text-align:left;font-family:'Roboto',sans-serif;font-weight:400;">Name:<span style="font-size:12px;font-weight:400;"> (required)</span></p>
  34.         <input class="register_module_short protect_from_spaces" style="margin-right:1%;" type="text" name="first_name" placeholder="first name*" required />
  35.         <input class="register_module_short protect_from_spaces" type="text" name="last_name" placeholder="last name" />
  36.  
  37.         <p style="text-align:left;font-family:'Roboto',sans-serif;font-weight:400;">Birthday:</p>
  38.         <input style="margin-top:-10px;" class="register_module protect_from_spaces" type="date" name="birthday" />
  39.  
  40.         <p style="text-align:left;font-family:'Roboto',sans-serif;font-weight:400;">Social Link(s):</p>
  41.         <input class="register_module protect_from_spaces" type="text" style="margin-top:-10px;" type="url" name="steam_profile" placeholder="steam profile (http://www.steamcommunity.com/id/lowheartrate/)" />
  42.         <input class="register_module protect_from_spaces" type="text" name="twitter_profile" placeholder="twitter profile (https://twitter.com/lowheartrate)" />
  43.         <input class="register_module protect_from_spaces" type="text" name="facebook_profile" placeholder="facebook profile (https://www.facebook.com/officiallowheartrate)" />
  44.         <input class="register_module protect_from_spaces" type="text" name="instagram_profile" placeholder="instagram profile (https://instagram.com/lowheartrate)" />
  45.  
  46.         <!-- image upload for avatar... -->
  47.         <div class="image_uploader">
  48.           <h2 style="text-align:left;font-family:'Roboto',sans-serif;">✌ Avatar Uploader</h2><br />
  49.  
  50.           <p style="margin-top:-20px;font-size:12px;text-align:left;">Select image to upload:</p>
  51.           <div style="text-align:left;font-size:12px;margin-top:-5px;margin-bottom:25px;">
  52.             <input type="file" name="avatar" class="" />
  53.           </div>
  54.         </div>
  55.       </div>
  56.  
  57.       <!-- check errors in registration -->
  58.       <?php checkRegisterErrors(); ?>
  59.  
  60.       <br />
  61.       <p class="pull-left" style="max-width:50%;">by signing up, you agree to our <a href="#">terms</a> and that you have read our <a href="#">privacy policy</a> and <a href="#">content policy</a>.</p>
  62.       <div class="g-recaptcha pull-right" data-sitekey="6Levrg4TAAAAAN-pL6Xl2tndj3ZDn5nJ3PRUhMV-"></div><br />
  63.  
  64.       <br />
  65.  
  66.       <button type="submit" name="submit_registration" class="btn-register" style="margin-top:10px;">sign up</button>
  67.   </form>
  68. </center>
  69.  
  70. <?php
  71. // Generate Random String function taken from
  72. // http://stackoverflow.com/questions/4356289/php-random-string-generator
  73. function generateRandomString($length) {
  74.     $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  75.     $charactersLength = strlen($characters);
  76.     $randomString = '';
  77.     for($i = 0; $i < $length; $i++) {
  78.         $randomString .= $characters[rand(0, $charactersLength - 1)];
  79.     }
  80.     return $randomString;
  81. }
  82.  
  83. // checks registration errors...
  84. function checkRegisterErrors() {
  85.   $username = $_POST['username'];
  86.  
  87.   // check to see if the form was submitted
  88.   if($_SERVER['REQUEST_METHOD'] == 'POST') {
  89.       require 'findConflict.php';
  90.       $username = $_POST['username'];
  91.       $password = $_POST['password'];
  92.       $cpassword = $_POST['cpassword'];
  93.       $email = $_POST['email'];
  94.       $hash = password_hash($password, PASSWORD_DEFAULT);
  95.       $query = dbConnect()->prepare("SELECT email, username FROM users WHERE username = :username OR email = :email");
  96.       $query->bindParam(':username', $username);
  97.       $query->bindParam(':email', $email);
  98.       $query->execute();
  99.       $conflictingItems = [];
  100.       while ( $result = $query->fetch( PDO::FETCH_ASSOC ) ) {
  101.           $conflictingItems[] = $result;
  102.       }
  103.       // for checking checkGoogleCaptcha() function!!
  104.       require_once 'core/recaptchalib.php';
  105.       if(isset($_POST['g-recaptcha-response']))
  106.       $captcha = $_POST['g-recaptcha-response'];
  107.       $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6Levrg4TAAAAAFmjcgKW8kDakmXTiBhmiCnUMchD&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
  108.  
  109.       if ( count ($conflictingItems) == 1 ) {
  110.         switch ( getConflict($conflictingItems, $username, $email) ) {
  111.             case 1:
  112.             // username conflict
  113.             echo "<p class='error'>username taken</p>";
  114.             break;
  115.             case 2:
  116.             // Email conflict
  117.             echo "<p class='error'>email already in use</p>";
  118.             break;
  119.             case 3:
  120.             // email & username conflict
  121.             echo '<p class="error">username & email in use</p>';
  122.             break;
  123.         }
  124.       } elseif ( count($conflictingItems) == 2 ) {
  125.         echo 'username & email already in use';
  126.  
  127.       // check if passwords match!
  128.       } elseif ($password != $cpassword) {
  129.         echo '<p class="error">passwords do not match</p>';
  130.  
  131.       // make sure password is between 8 - 16 characters
  132.       } elseif (strlen($password) < 8 OR strlen($password) > 16) {
  133.         echo '<p class="error">password must be between 8 & 16 characters</p>';
  134.  
  135.       // checks to make sure google recaptcha was posted!
  136.       } elseif (!$captcha) {
  137.         echo '<p class="error">please complete the captcha</p>';
  138.       } elseif($response['success'] == false) {
  139.         echo '<p class="error">you are a robot</p>';
  140.  
  141.  
  142.       } else {
  143.         // require config.php to get maximum file size --> $maximum_avatar_file_size = '125000';
  144.         //                    to get avatars directory --> $avatars_directory = 'includes/uploads/avatars';
  145.         require 'core/config.php';
  146.  
  147.         // need for file upload query & query to insert fields into database..
  148.         $activation_id = uniqid(true);
  149.  
  150.         // file upload stuff... ONLY NEEDED FOR TESTING PURPOSES!
  151.         // var_dump($_FILES);
  152.  
  153.         // move uploaded file to directory (includes/uploads/avatar)
  154.         $upload_directory = $avatars_directory; // the directory you want to store the avatars in...
  155.  
  156.         // sets $image & gets image type, name, tmp_name
  157.         $image = $_FILES['avatar']; // create and append new varaible to $_FILES['avatar'] so that we don't have to manually type in the full variable for it.
  158.  
  159.         // get image temporary name...
  160.         $image_tmp_name = $image['tmp_name']; // get the tmp file
  161.  
  162.         // get image size...
  163.         $image = getimagesize($image['tmp_name']); // get the image size of tmp file
  164.  
  165.         $image_mime = $image['mime']; // create and append the mime array to $image_mime
  166.  
  167.         // List the mime types you want to allow in your upload system...
  168.         $valid_image_mime = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');
  169.  
  170.         // generate random string with generateRandomString() function...
  171.         $random = generateRandomString(16);
  172.  
  173.         $ext = '.jpg'; // file extension we want it to end up being once its uploaded.
  174.  
  175.         $file_name = $random . $ext; // Append the generated string along with the desired extension to the new file name.
  176.  
  177.         // Check to see if the mime type from the tmp file is in our array above...
  178.         if(in_array($image_mime, $valid_image_mime)) {
  179.  
  180.           // So we are good to go. The mime type is listed in our array
  181.  
  182.           // Check to see moving the tmp file to our desired directory works.
  183.           if(move_uploaded_file($image_tmp_name, $upload_directory . '/' . $file_name )) {
  184.  
  185.             // since the file has been uploaded, we can now update our old avatar record
  186.             // and set it to the current one.
  187.  
  188.             $sql = "UPDATE users SET avatar = :avatar WHERE username = :username";
  189.  
  190.             $prepare = dbConnect()->prepare($sql);
  191.             $parameters = array(':avatar' => $file_name, ':username' => $username);
  192.             $prepare->execute($parameters);
  193.  
  194.             // check to see if the query executed
  195.             if ($prepare->rowCount()) {
  196.  
  197.               // print('Good');
  198.               header('Location: ' . $_SERVER['REQUEST_URI']);
  199.  
  200.             } else {
  201.  
  202.               // print('Bad Upload');
  203.               header('Location: ' . $_SERVER['REQUEST_URI']);
  204.  
  205.             }
  206.  
  207.           } else {
  208.  
  209.             // print('Bad');
  210.             header('Location: ' . $_SERVER['REQUEST_URI']);
  211.  
  212.           }
  213.  
  214.         }
  215.  
  216.  
  217.         // query to insert fields into database...
  218.         //$activation_id = uniqid(true);
  219.         $query = dbConnect()->prepare("INSERT INTO users (username, password, email, activated, activation_id) VALUES (:username, :password, :email, :activated, :activation_id)");
  220.         $query->bindParam(':username', $username);
  221.         $query->bindParam(':email', $email);
  222.         $query->bindParam(':password', $hash);
  223.         $query->bindValue(':activated', "0");
  224.         $query->bindValue(':activation_id', $activation_id);
  225.         $query->execute();
  226.  
  227.         // set variables for optional fields...
  228.         $firstName = $_POST['first_name'];
  229.         $lastName = $_POST['last_name'];
  230.         $birthday = $_POST['birthday'];
  231.         $steam_profile = $_POST['steam_profile'];
  232.         $twitter_profile = $_POST['twitter_profile'];
  233.         $facebook_profile = $_POST['facebook_profile'];
  234.         $instagram_profile = $_POST['instagram_profile'];
  235.  
  236.         // update user_details database
  237.         $query2 = dbConnect()->prepare("INSERT INTO user_details (first_name, last_name, birthday, steam_profile, twitter_profile, facebook_profile, instagram_profile) VALUES (:firstName, :lastName, :birthday, :steam, :twitter, :facebook, :instagram)");
  238.         $query2->bindParam(':firstName', $firstName);
  239.         $query2->bindParam(':lastName', $lastName);
  240.         $query2->bindParam(':birthday', $birthday);
  241.         $query2->bindParam(':steam', $steam_profile);
  242.         $query2->bindParam(':twitter', $twitter_profile);
  243.         $query2->bindParam(':facebook', $facebook_profile);
  244.         $query2->bindParam(':instagram', $instagram_profile);
  245.         $query2->execute();
  246.  
  247.         // query to add image name to database to echo later on...
  248.         /*
  249.         $add_image_to_database = dbConnect()->prepare("INSERT INTO users (avatar) VALUES (:avatar)");
  250.         $add_image_to_database->bindParam(':avatar', $file_name);
  251.         $add_image_to_database->execute();
  252.         */
  253.  
  254.         // send email verification
  255.         require 'core/config.php';
  256.         $subject = $url . 'Registration Confirmation';
  257.         $link = 'http://' . $url. "/activate.php?activation_id=" . $activation_id;
  258.         $message = 'Thanks for registering with us! Please verify your account at' . $link . ' so you can login.';
  259.         $headers = 'From: ' .$contact_email;
  260.         mail($email/* <-- Who its being sent to */, $subject, $message, $headers);
  261.         echo '<p class="info">registration successful, please verify email before logging in.</p>';
  262.  
  263.         header('refresh:5;url=index.php');
  264.  
  265.         echo '<p class="info">if you are not redirected, try to <a href="index.php">refresh</a> your page.</p>';
  266.       }
  267.   }
  268. }
  269. ?>
Advertisement
Add Comment
Please, Sign In to add comment