Advertisement
Guest User

login.php

a guest
Nov 8th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.01 KB | None | 0 0
  1. <?php
  2.  
  3. class DB_Functions {
  4.  
  5.     private $conn;
  6.  
  7.  
  8.     // constructor
  9.     function __construct() {
  10.        
  11.         require_once 'db_connect.php';
  12.         // Establish connection
  13.         $db = new Db_Connect();
  14.         $this->conn = $db->connect();
  15.     }
  16.    
  17.     private function validateToken($userid, $tokenReceived) {
  18.         $stmt = $this->conn->prepare("SELECT id, token FROM users WHERE id = ? LIMIT 1");
  19.         $stmt->bind_param("s", $userid);
  20.         $stmt->execute();
  21.        
  22.         $stmt->bind_result($id, $token);
  23.         $stmt->store_result();
  24.         if($stmt->num_rows == 1) {
  25.             if($stmt->fetch()) //fetching the contents of the row
  26.                 {
  27.                     if($token == $tokenReceived) {
  28.                         // session is valid
  29.                     }
  30.                 }
  31.             else {
  32.                 // fetch failed
  33.             }
  34.         }
  35.         else {
  36.             // user not found
  37.         }
  38.        
  39.        
  40.     }
  41.    
  42.     public function registerUser($name, $email, $password) {
  43.        
  44.         $hash = $this->hashSSHA($password);
  45.         $e_password = $hash["encrypted"]; // encrypt the password
  46.         $salt = $hash["salt"]; // salt
  47.         $uuid = uniqid('', true);
  48.        
  49.         $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
  50.         //var_dump($stmt);
  51.     $stmt->bind_param("sssss", $uuid, $name, $email, $e_password, $salt);
  52.         $result = $stmt->execute();
  53.         $stmt->close();
  54.  
  55.         // check for successful store
  56.         if ($result) {
  57.             $stmt = $this->conn->prepare("SELECT id, name FROM users WHERE email = ?");
  58.             $stmt->bind_param("s", $email);
  59.             $stmt->execute();
  60.            
  61.             $stmt->store_result();
  62.  
  63.             /* Bind the result to variables */
  64.             $stmt->bind_result($id, $name);
  65.  
  66.             $user = $stmt->fetch();
  67.             $stmt->close();
  68.  
  69.             return $user;
  70.         } else {
  71.             return false;
  72.         }
  73.     }
  74.    
  75.     public function getUserByEmailAndPassword($email, $password) {
  76. $email = "admin@admin.com";
  77. //$password = "admin";
  78.        
  79.         $stmt = $this->conn->prepare("SELECT id, name, email, salt, password, token FROM users WHERE email = ? LIMIT 1");
  80.         $stmt->bind_param("s", $email);
  81.         $stmt->execute();
  82.         $stmt->bind_result($id, $name, $email, $salt, $password, $token);
  83.         $stmt->store_result();
  84.         if($stmt->num_rows == 1)  //To check if the row exists
  85.         {
  86.             if($stmt->fetch()) //fetching the contents of the row
  87.             {
  88.                 // verifying user password
  89.                 $hash = $this->checkhashSSHA($salt, $password);
  90.                 // check for password equality
  91.                 if ($password == $hash) {
  92.                 // user authentication details are correct
  93.                 $user = array(
  94.                     "status" => "OK",
  95.                     "id" => $id,
  96.                     "username" => $name,
  97.                     "token" => $token,
  98.                     "email" => $email);
  99.                 }
  100.                 else {
  101.                     $user = array("status" => "Passwords do not match.");
  102.                 }
  103.             }
  104.         }
  105.         else {
  106.             //$user = array("status" => "User does not exist.");
  107. $user = array("status" => "User does not exist."+ $email + " " + $password);
  108.         }
  109.         $stmt->close();
  110.         return $user;
  111.     }
  112.    
  113.     public function getUserNameByID($id) {
  114.        
  115.         $stmt = $this->conn->prepare("SELECT name FROM users WHERE id = ? LIMIT 1");
  116.         $stmt->bind_param("s", $id);
  117.         $stmt->execute();
  118.         $stmt->bind_result($name);
  119.         $stmt->store_result();
  120.         if($stmt->num_rows == 1)  //To check if the row exists
  121.             {
  122.                 if($stmt->fetch()) //fetching the contents of the row
  123.                 {
  124.                    
  125.                     $user = $name;
  126.                 }
  127.                 else {
  128.                     $user = "User not found.";
  129.                 }
  130.             }
  131.         else {
  132.             $user = "User does not exist.";
  133.         }
  134.         $stmt->close();
  135.         return $user;
  136.     }
  137.    
  138.     public function hashSSHA($password) {
  139.  
  140.         $salt = sha1(rand());
  141.         $salt = substr($salt, 0, 10);
  142.         $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
  143.         $hash = array("salt" => $salt, "encrypted" => $encrypted);
  144.         return $hash;
  145.     }
  146.    
  147.     public function checkhashSSHA($salt, $password) {
  148.        
  149.  
  150.         $hash = base64_encode(sha1($password . $salt, true) . $salt);
  151.  
  152.         return $hash;
  153.     }
  154.    
  155.     public function createMatch($user1, $user2, $user1side) {
  156.         if($user1side == 1)
  157.         {
  158.             $tempuser = $user2;
  159.             $user2 = $user1;
  160.             $user1 = $tempuser;
  161.         }
  162.         $stmt = $this->conn->prepare("INSERT INTO matches (id , player1id, player2id, status) VALUES ( NULL ,  ?, ?, 'set_word');");
  163.         $stmt->bind_param("ss", $user1, $user2);
  164.         $stmt->execute();
  165.     }
  166.    
  167.     public function getUserMatchesById($uid) {
  168.         $stmt = $this->conn->prepare("SELECT id, player1id, ratingid, player2id, wordid,status FROM matches WHERE player1id = ? OR player2id = ? ");
  169.         $stmt->bind_param("ss", $uid, $uid);
  170.         $stmt->execute();
  171.         $stmt->bind_result($id, $player1id, $ratingid, $player2id, $wordid, $status);
  172.         $stmt->store_result();
  173.        
  174.         if($stmt->num_rows > 0)  //To check if the row exists
  175.             {
  176.                 $matches = array("status" => "OK",
  177.                                  "num_items" => $stmt->num_rows);
  178.                 while($stmt->fetch()) //fetching the contents of the row
  179.                 {
  180.                     if($uid == $player1id) {
  181.                         $player1name = "You";
  182.                         $player2name = $this->getUserNameByID($player2id);
  183.                     }
  184.                     else {
  185.                         $player2name = "You";
  186.                         $player1name = $this->getUserNameByID($player1id);
  187.                     }
  188.                     $match = array(
  189.                        "id" => $id,
  190.                        "ratingid" => $ratingid,
  191.                        "player1name" => $player1name,
  192.                        "player2name" => $player2name,
  193.                        "word" => $wordid,
  194.                        "game_status" => $status);
  195.                        
  196.                     array_push($matches, $match);
  197.                 }
  198.                 return $matches;
  199.             }
  200.         else {
  201.             return array("Status" => "No matches found");
  202.         }
  203.     }
  204.    
  205.     public function getMatchById($id, $simple) {
  206.         $stmt = $this->conn->prepare("SELECT id, player1id, ratingid, picture1id, picture2id, picture3id, wordid, status FROM matches WHERE id = ? LIMIT 1");
  207.         $stmt->bind_param("s", $id);
  208.         $stmt->execute();
  209.         $stmt->bind_result($id, $player1id, $ratingid, $picture1id, $picture2id, $picture3id, $wordid, $game_status);
  210.         $stmt->store_result();
  211.        
  212.         if($stmt->num_rows == 1)  //To check if the row exists
  213.             {
  214.                 if($stmt->fetch()) //fetching the contents of the row
  215.                 {
  216.                     if($simple) {
  217.                         $picturelist = array(
  218.                             $picture1id,
  219.                             $picture2id,
  220.                             $picture3id,
  221.                         );
  222.                        
  223.                        
  224.                         $match = array(
  225.                        "status" => "OK",
  226.                        "id" => $id,
  227.                        "ratingid" => $ratingid,
  228.                        "player1id" => $player1id,
  229.                        "pictures" => $picturelist,
  230.                        "word" => $wordid,
  231.                        "game_status" => $game_status);
  232.                     }
  233.                     else {
  234.                         $picturelist = array(
  235.                             $this->getPictureById($picture1id),
  236.                             $this->getPictureById($picture2id),
  237.                             $this->getPictureById($picture3id),
  238.                         );
  239.                        
  240.                        
  241.                         $match = array(
  242.                            "status" => "OK",
  243.                            "id" => $id,
  244.                            "ratingid" => $ratingid,
  245.                            "player1id" => $player1id,
  246.                            "pictures" => $picturelist,
  247.                            "word" => $this->getWordById($wordid),
  248.                            "game_status" => $game_status);
  249.                     }
  250.                 }
  251.                 else {
  252.                     $match = array("status" => "Match not found.");
  253.                 }
  254.             }
  255.         else {
  256.             $match = array("status" => "Match not found.");
  257.         }
  258.         $stmt->close();
  259.         return $match;
  260.     }
  261.    
  262.     public function getPictureById($id) {
  263.         $stmt = $this->conn->prepare("SELECT id, filename, datecreated, uid, flagged FROM pictures WHERE id = ? LIMIT 1");
  264.         $stmt->bind_param("s", $id);
  265.         $stmt->execute();
  266.         $stmt->bind_result($id, $fileurl, $datecreated, $uid, $flagged);
  267.         $stmt->store_result();
  268.        
  269.         if($stmt->num_rows == 1)  //To check if the row exists
  270.             {
  271.                 if($stmt->fetch()) //fetching the contents of the row
  272.                 {
  273.                     $picture = array(
  274.                        "status" => "OK",
  275.                        "id" => $id,
  276.                        "fileurl" => $fileurl,
  277.                        "datecreated" => $datecreated,
  278.                        "uid" => $uid,
  279.                        "flagged" => $flagged);
  280.                 }
  281.                 else {
  282.                     $picture = array("status" => "Picture not found.");
  283.                 }
  284.             }
  285.         else {
  286.             $picture = array("status" => "Picture not found.");
  287.         }
  288.         $stmt->close();
  289.         return $picture;
  290.     }
  291.    
  292.     public function getWordById($id) {
  293.         $stmt = $this->conn->prepare("SELECT id, word FROM words WHERE id = ? LIMIT 1");
  294.         $stmt->bind_param("s", $id);
  295.         $stmt->execute();
  296.         $stmt->bind_result($id, $word);
  297.         $stmt->store_result();
  298.        
  299.         if($stmt->num_rows == 1)  //To check if the row exists
  300.             {
  301.                 if($stmt->fetch()) //fetching the contents of the row
  302.                 {
  303.                     $wordarray = array(
  304.                        "status" => "OK",
  305.                        "id" => $id,
  306.                        "word" => $word);
  307.                 }
  308.                 else {
  309.                     $wordarray = array("status" => "Picture not found.");
  310.                 }
  311.             }
  312.         else {
  313.             $wordarray = array("status" => "Picture not found.");
  314.         }
  315.         $stmt->close();
  316.         return $wordarray;
  317.     }
  318.    
  319.     public function startNewMatch($uid, $side) {
  320.         $stmt = $this->conn->prepare("SELECT id, userid FROM queue WHERE side != ? AND userid !=? LIMIT 1");
  321.         $stmt->bind_param("ss", $side, $uid);
  322.         $stmt->execute();
  323.         $stmt->bind_result($id, $userid);
  324.         $stmt->store_result();
  325.        
  326.         if(($stmt->num_rows == 1))  //To check if anyone is waiting for a match
  327.             {
  328.                 if($stmt->fetch()) //fetching the contents of the row
  329.                 {
  330.                     $uarray = array(
  331.                        "status" => "OK",
  332.                        "id" => $id,
  333.                        "userid" => $userid,
  334.                        "side" => $side);
  335.                 }
  336.                 else {
  337.                     $uarray = array("status" => "Queue item not found.");
  338.                 }
  339.                 $stmt = $this->conn->prepare("DELETE FROM queue WHERE id = ?");
  340.                 $stmt->bind_param("s", $id);
  341.                 $stmt->execute();
  342.                
  343.                 $this->createMatch($uid, $userid, $side);
  344.                 // create the match
  345.                
  346.             }
  347.         else
  348.             {
  349.                 // Create a record in the queue
  350.                 $stmt = $this->conn->prepare("INSERT INTO queue (id , userid, side) VALUES ( NULL ,  ?, ?);");
  351.                 $stmt->bind_param("ss", $uid, $side);
  352.                 $stmt->execute();
  353.                 $uarray = array(
  354.                        "status" => "Waiting");
  355.             }
  356.            
  357.         return $uarray;
  358.     }
  359.    
  360.     public function dump($data) {
  361.         $stmt = $this->conn->prepare("INSERT INTO pictures (id , filename) VALUES ( NULL ,  ?)");
  362.         $stmt->bind_param("s", $data[1]);
  363.         $stmt->execute();
  364.        
  365.         $picid = $stmt->insert_id;
  366.         $matchid = $data[0];
  367.         $status = "game_started";
  368.        
  369.         $stmt = $this->conn->prepare("UPDATE matches SET picture1id = ?, status = ? WHERE id = ?");
  370.         $stmt->bind_param("isi", $picid, $status, $matchid);
  371.         $stmt->execute();
  372.        
  373.         return array("status" => "OK" );
  374.     }
  375.    
  376.     public function saveMatch($arrayMatch) {
  377.        
  378.         $am = json_decode($arrayMatch);
  379.         $id  = $am->{'id'};
  380.         $wordid  = $am->{'word'};
  381.         $status  = $am->{'game_status'};       
  382.        
  383.         $stmt = $this->conn->prepare("UPDATE matches SET wordid = ?, status = ? WHERE id = ?");
  384.         $stmt->bind_param("sss", $wordid, $status, $id);
  385.         $stmt->execute();
  386.        
  387.         if($stmt->error != "")
  388.         {
  389.             return array("status" => "Failed", "error" => $stmt->error);
  390.         }
  391.         else
  392.         {
  393.             return array("status" => "OK" );
  394.         }
  395.     }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement