Advertisement
Guest User

Untitled

a guest
May 14th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.88 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: ares7
  5.  * Date: 14.05.2016
  6.  * Time: 11:30
  7.  */
  8.  
  9. if(isset($_POST)) {
  10.     session_start();
  11.     if(isset($_POST["displayName"])) {
  12.         // this is probably register
  13.         if(isset($_POST["username"])) {
  14.             if(isset($_POST["password"])) {
  15.                 if(isset($_POST["email"])) {
  16.                     if(isset($_POST["gameKey"])) {
  17.                         // This is register for sure tho
  18.                         // establish database connection
  19.                         define("allowed_to_view_database_info", true);
  20.                         include "../database.php";
  21.                         $con = mysqli_connect(SQLInfo::$host, SQLInfo::$user, SQLInfo::$pass, SQLInfo::$base)
  22.                             or die(mysqli_error($con));
  23.  
  24.                         // save our post data
  25.                         $username = $_POST["username"];
  26.                         $displayName = $_POST["displayName"];
  27.                         $password = $_POST["password"];
  28.                         $password = md5($password); // We double md5 the password
  29.                         $email = $_POST["email"];
  30.                         $gameKey = $_POST["gameKey"];
  31.  
  32.                         // SQL Injection fix
  33.                         $username = mysqli_escape_string($con, $username);
  34.                         $displayName = secureSQLinjection($con, $displayName);
  35.                         $password = mysqli_escape_string($con, $password);
  36.                         $email = mysqli_escape_string($con, $email);
  37.                         $gameKey = mysqli_escape_string($con, $gameKey);
  38.  
  39.                         // check if there is already that username in database
  40.                         $usernameCheckQuery = "SELECT * FROM bft_accounts WHERE username='$username'";
  41.                         $usernameCheckResult = mysqli_query($con, $usernameCheckQuery)
  42.                             or die(mysqli_error($con));
  43.  
  44.                         if(mysqli_num_rows($usernameCheckResult) > 0) {
  45.                             // The username is already taken
  46.                             mysqli_close($con);
  47.                             die("Username is already registered! (Error #10)");
  48.                         }
  49.                         // The username is good,
  50.                         // check email now
  51.                         $emailCheckQuery = "SELECT * FROM bft_accounts WHERE email='$email'";
  52.                         $emailCheckResult = mysqli_query($con, $emailCheckQuery)
  53.                             or die(mysqli_error($con));
  54.  
  55.                         if(mysqli_num_rows($emailCheckResult) > 0) {
  56.                             // The email is already taken
  57.                             mysqli_close($con);
  58.                             die("Email is already registered! (Error #11)");
  59.                         }
  60.                         // the email is good,
  61.                         // check game key now
  62.                         $keyCheckQuery = "SELECT * FROM bft_accounts WHERE game_key='$gameKey'";
  63.                         $keyCheckResult = mysqli_query($con, $keyCheckQuery)
  64.                             or die(mysqli_error($con));
  65.  
  66.                         if(mysqli_num_rows($keyCheckResult) > 0) {
  67.                             // The game key is already registered
  68.                             mysqli_close($con);
  69.                             die("The registration key is already registered! (Error #12)");
  70.                         }
  71.                         // the game key is not taken, but is valid?
  72.                         $gk = false;
  73.                         if($gameKey != "FALSE") {
  74.                             // the game key was entered in
  75.                             if (count($gameKey) != 15) {
  76.                                 // not valid key uh
  77.                                 mysqli_close($con);
  78.                                 die("The game key is not valid! (Error #13)");
  79.                             }
  80.  
  81.                             // the key could be valid, check now
  82.                             $realKeyCheckQuery = "SELECT * FROM bft_gamekeys WHERE `key`='$gameKey'";
  83.                             $realKeyCheckResult = mysqli_query($con, $realKeyCheckQuery)
  84.                             or die(mysqli_error($con));
  85.  
  86.                             if (mysqli_num_rows($realKeyCheckResult) == 0) {
  87.                                 // the key doesn't exists
  88.                                 mysqli_close($con);
  89.                                 die("The game key is not valid! (Error #14)");
  90.                             }
  91.                             // so the key exists everything looks okay, create new account now!
  92.                             $gk = true;
  93.                         }
  94.                         // else the game key input was empty
  95.                         // generate some important keys
  96.                         // generate public key with size of 15
  97.                         $publicKey = generateKey(15);
  98.                         // generate private key with size of 10
  99.                         $privateKey = generateKey(10);
  100.                         // generate password salt with size of 10
  101.                         $passwordSalt = generateKey(10);
  102.                         // generate activation key with size of 30
  103.                         $activationKey = generateKey(30);
  104.  
  105.                         // salt the password
  106.                         $password = md5($passwordSalt . $password);
  107.  
  108.                         // lowercase the username
  109.                         $username = strtolower($username);
  110.  
  111.                         // when do we register
  112.                         $registerDate = time();
  113.  
  114.                         if(!$gk) $gameKey = "";
  115.  
  116.                         // insert the new account into the database
  117.                         $newAccountQuery = "INSERT INTO bft_accounts (username, display_name, password, email,
  118.                        register_date, public_key, private_key, password_salt, game_key, activated, activationKey)
  119.                        VALUES ('$username', '$displayName', '$password', '$email', '$registerDate', '$publicKey',
  120.                        '$privateKey', '$passwordSalt', '$gameKey', '0', '$activationKey')";
  121.                         $newAccountResult = mysqli_query($con, $newAccountQuery)
  122.                             or die(mysqli_error($con));
  123.  
  124.                         // looks alright, lets send activation email
  125.                         $htmlContent = file_get_contents("emailTemplates/activateEmailTemplate.html");
  126.                         $translations = array(
  127.                             "{DISPLAY_NAME}" => $displayName,
  128.                             "{EMAIL}" => $email,
  129.                             "{ACTIVATION_KEY}" => $activationKey,
  130.                             "{USERNAME}" => $username
  131.                         );
  132.  
  133.                         // translate it now
  134.                         $htmlContent = strtr($htmlContent, $translations);
  135.  
  136.                         $subject = "Battle for Treasure account registration";
  137.                         $headers = "MIME-Version: 1.0" . "\r\n";
  138.                         $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
  139.                         $headers .= "From: <no-reply@battlefortreasure.com>" . "\r\n";
  140.  
  141.                         $mail = mail($email, $subject, $htmlContent, $headers);
  142.  
  143.                         if(!$mail) die(error_get_last());
  144.                         // Email should have been sent
  145.                         // close sql connection
  146.                         mysqli_close($con);
  147.  
  148.                         die("success");
  149.  
  150.                     } else die("Error #9");
  151.                 } else die("Error #8");
  152.             } else die("Error #7");
  153.         } else die("Error #6");
  154.     } else {
  155.         // Probably login
  156.         if(isset($_POST["username"])) {
  157.             if(isset($_POST["password"])) {
  158.                 if(isset($_POST["rememberme"])) {
  159.                     // This is login for sure tho
  160.                     // establish database connection
  161.                     define("allowed_to_view_database_info", true);
  162.                     include "../database.php";
  163.  
  164.                     // get formated variables
  165.                     $username = $_POST["username"];
  166.                     $password = md5($_POST["password"]); // We double md5 the pw
  167.                     $rememberme = $_POST["rememberme"];
  168.  
  169.                     // prepare the connection
  170.                     $con = mysqli_connect(SQLInfo::$host, SQLInfo::$user, SQLInfo::$pass, SQLInfo::$base)
  171.                         or die(mysqli_error($con));
  172.  
  173.                     // error 20 is already occupied
  174.                     // --> login is disabled
  175.                     // cannot validate email
  176.  
  177.                     // check if the user is in the database
  178.                     $username = mysqli_escape_string($con, $username);
  179.                     $username = strtolower($username);
  180.                     $authCheckQuery = "SELECT * FROM bft_accounts WHERE username='$username'";
  181.                     $authCheckResult = mysqli_query($con, $authCheckQuery)
  182.                         or die(mysqli_error($con));
  183.  
  184.                     if(mysqli_num_rows($authCheckResult) == 0) {
  185.                         // Account doesn't exists x(
  186.                         mysqli_close($con);
  187.                         die("Username is not registered! (Error #15)");
  188.                     }
  189.                     // it may work here :P
  190.                     // get the password salt
  191.                     $passwordSalt = mysqli_result($authCheckResult, 0, "password_salt");
  192.                     $realPassword = mysqli_result($authCheckResult, 0, "password");
  193.                     // we can easily check all the stuff now
  194.                     // hash the password
  195.                     $password = md5($passwordSalt . $password);
  196.                     if($password != $realPassword) {
  197.                         // there is password mismatch! - Wrong password
  198.                         mysqli_close($con);
  199.                         die("Wrong password! (Error #16)");
  200.                     }
  201.                     // else the password was correct
  202.                     // check if the account is activated
  203.                     if(mysqli_result($authCheckResult, 0, "activated") == 0) {
  204.                         // The account is not activated yet!
  205.                         mysqli_close($con);
  206.                         die("Account is not activated yet! (Error #20)");
  207.                     }
  208.                     // the account is activated and everything is alright
  209.                     // login
  210.                     // generate session key
  211.                     $sessionkey = generateKey(50);
  212.                     if(!isset($_SESSION)) {
  213.                         session_start();
  214.                     }
  215.                    
  216.                     $_SESSION[""];
  217.  
  218.                 } else die("Error #5");
  219.             } else die("Error #4");
  220.         } else die("Error #3");
  221.     }
  222. } else die("Error #1");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement