Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB_Functions {
- private $conn;
- // constructor
- function __construct() {
- require_once 'db_connect.php';
- // Establish connection
- $db = new Db_Connect();
- $this->conn = $db->connect();
- }
- private function validateToken($userid, $tokenReceived) {
- $stmt = $this->conn->prepare("SELECT id, token FROM users WHERE id = ? LIMIT 1");
- $stmt->bind_param("s", $userid);
- $stmt->execute();
- $stmt->bind_result($id, $token);
- $stmt->store_result();
- if($stmt->num_rows == 1) {
- if($stmt->fetch()) //fetching the contents of the row
- {
- if($token == $tokenReceived) {
- // session is valid
- }
- }
- else {
- // fetch failed
- }
- }
- else {
- // user not found
- }
- }
- public function registerUser($name, $email, $password) {
- $hash = $this->hashSSHA($password);
- $e_password = $hash["encrypted"]; // encrypt the password
- $salt = $hash["salt"]; // salt
- $uuid = uniqid('', true);
- $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
- //var_dump($stmt);
- $stmt->bind_param("sssss", $uuid, $name, $email, $e_password, $salt);
- $result = $stmt->execute();
- $stmt->close();
- // check for successful store
- if ($result) {
- $stmt = $this->conn->prepare("SELECT id, name FROM users WHERE email = ?");
- $stmt->bind_param("s", $email);
- $stmt->execute();
- $stmt->store_result();
- /* Bind the result to variables */
- $stmt->bind_result($id, $name);
- $user = $stmt->fetch();
- $stmt->close();
- return $user;
- } else {
- return false;
- }
- }
- public function getUserByEmailAndPassword($email, $password) {
- $email = "admin@admin.com";
- //$password = "admin";
- $stmt = $this->conn->prepare("SELECT id, name, email, salt, password, token FROM users WHERE email = ? LIMIT 1");
- $stmt->bind_param("s", $email);
- $stmt->execute();
- $stmt->bind_result($id, $name, $email, $salt, $password, $token);
- $stmt->store_result();
- if($stmt->num_rows == 1) //To check if the row exists
- {
- if($stmt->fetch()) //fetching the contents of the row
- {
- // verifying user password
- $hash = $this->checkhashSSHA($salt, $password);
- // check for password equality
- if ($password == $hash) {
- // user authentication details are correct
- $user = array(
- "status" => "OK",
- "id" => $id,
- "username" => $name,
- "token" => $token,
- "email" => $email);
- }
- else {
- $user = array("status" => "Passwords do not match.");
- }
- }
- }
- else {
- //$user = array("status" => "User does not exist.");
- $user = array("status" => "User does not exist."+ $email + " " + $password);
- }
- $stmt->close();
- return $user;
- }
- public function getUserNameByID($id) {
- $stmt = $this->conn->prepare("SELECT name FROM users WHERE id = ? LIMIT 1");
- $stmt->bind_param("s", $id);
- $stmt->execute();
- $stmt->bind_result($name);
- $stmt->store_result();
- if($stmt->num_rows == 1) //To check if the row exists
- {
- if($stmt->fetch()) //fetching the contents of the row
- {
- $user = $name;
- }
- else {
- $user = "User not found.";
- }
- }
- else {
- $user = "User does not exist.";
- }
- $stmt->close();
- return $user;
- }
- public function hashSSHA($password) {
- $salt = sha1(rand());
- $salt = substr($salt, 0, 10);
- $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
- $hash = array("salt" => $salt, "encrypted" => $encrypted);
- return $hash;
- }
- public function checkhashSSHA($salt, $password) {
- $hash = base64_encode(sha1($password . $salt, true) . $salt);
- return $hash;
- }
- public function createMatch($user1, $user2, $user1side) {
- if($user1side == 1)
- {
- $tempuser = $user2;
- $user2 = $user1;
- $user1 = $tempuser;
- }
- $stmt = $this->conn->prepare("INSERT INTO matches (id , player1id, player2id, status) VALUES ( NULL , ?, ?, 'set_word');");
- $stmt->bind_param("ss", $user1, $user2);
- $stmt->execute();
- }
- public function getUserMatchesById($uid) {
- $stmt = $this->conn->prepare("SELECT id, player1id, ratingid, player2id, wordid,status FROM matches WHERE player1id = ? OR player2id = ? ");
- $stmt->bind_param("ss", $uid, $uid);
- $stmt->execute();
- $stmt->bind_result($id, $player1id, $ratingid, $player2id, $wordid, $status);
- $stmt->store_result();
- if($stmt->num_rows > 0) //To check if the row exists
- {
- $matches = array("status" => "OK",
- "num_items" => $stmt->num_rows);
- while($stmt->fetch()) //fetching the contents of the row
- {
- if($uid == $player1id) {
- $player1name = "You";
- $player2name = $this->getUserNameByID($player2id);
- }
- else {
- $player2name = "You";
- $player1name = $this->getUserNameByID($player1id);
- }
- $match = array(
- "id" => $id,
- "ratingid" => $ratingid,
- "player1name" => $player1name,
- "player2name" => $player2name,
- "word" => $wordid,
- "game_status" => $status);
- array_push($matches, $match);
- }
- return $matches;
- }
- else {
- return array("Status" => "No matches found");
- }
- }
- public function getMatchById($id, $simple) {
- $stmt = $this->conn->prepare("SELECT id, player1id, ratingid, picture1id, picture2id, picture3id, wordid, status FROM matches WHERE id = ? LIMIT 1");
- $stmt->bind_param("s", $id);
- $stmt->execute();
- $stmt->bind_result($id, $player1id, $ratingid, $picture1id, $picture2id, $picture3id, $wordid, $game_status);
- $stmt->store_result();
- if($stmt->num_rows == 1) //To check if the row exists
- {
- if($stmt->fetch()) //fetching the contents of the row
- {
- if($simple) {
- $picturelist = array(
- $picture1id,
- $picture2id,
- $picture3id,
- );
- $match = array(
- "status" => "OK",
- "id" => $id,
- "ratingid" => $ratingid,
- "player1id" => $player1id,
- "pictures" => $picturelist,
- "word" => $wordid,
- "game_status" => $game_status);
- }
- else {
- $picturelist = array(
- $this->getPictureById($picture1id),
- $this->getPictureById($picture2id),
- $this->getPictureById($picture3id),
- );
- $match = array(
- "status" => "OK",
- "id" => $id,
- "ratingid" => $ratingid,
- "player1id" => $player1id,
- "pictures" => $picturelist,
- "word" => $this->getWordById($wordid),
- "game_status" => $game_status);
- }
- }
- else {
- $match = array("status" => "Match not found.");
- }
- }
- else {
- $match = array("status" => "Match not found.");
- }
- $stmt->close();
- return $match;
- }
- public function getPictureById($id) {
- $stmt = $this->conn->prepare("SELECT id, filename, datecreated, uid, flagged FROM pictures WHERE id = ? LIMIT 1");
- $stmt->bind_param("s", $id);
- $stmt->execute();
- $stmt->bind_result($id, $fileurl, $datecreated, $uid, $flagged);
- $stmt->store_result();
- if($stmt->num_rows == 1) //To check if the row exists
- {
- if($stmt->fetch()) //fetching the contents of the row
- {
- $picture = array(
- "status" => "OK",
- "id" => $id,
- "fileurl" => $fileurl,
- "datecreated" => $datecreated,
- "uid" => $uid,
- "flagged" => $flagged);
- }
- else {
- $picture = array("status" => "Picture not found.");
- }
- }
- else {
- $picture = array("status" => "Picture not found.");
- }
- $stmt->close();
- return $picture;
- }
- public function getWordById($id) {
- $stmt = $this->conn->prepare("SELECT id, word FROM words WHERE id = ? LIMIT 1");
- $stmt->bind_param("s", $id);
- $stmt->execute();
- $stmt->bind_result($id, $word);
- $stmt->store_result();
- if($stmt->num_rows == 1) //To check if the row exists
- {
- if($stmt->fetch()) //fetching the contents of the row
- {
- $wordarray = array(
- "status" => "OK",
- "id" => $id,
- "word" => $word);
- }
- else {
- $wordarray = array("status" => "Picture not found.");
- }
- }
- else {
- $wordarray = array("status" => "Picture not found.");
- }
- $stmt->close();
- return $wordarray;
- }
- public function startNewMatch($uid, $side) {
- $stmt = $this->conn->prepare("SELECT id, userid FROM queue WHERE side != ? AND userid !=? LIMIT 1");
- $stmt->bind_param("ss", $side, $uid);
- $stmt->execute();
- $stmt->bind_result($id, $userid);
- $stmt->store_result();
- if(($stmt->num_rows == 1)) //To check if anyone is waiting for a match
- {
- if($stmt->fetch()) //fetching the contents of the row
- {
- $uarray = array(
- "status" => "OK",
- "id" => $id,
- "userid" => $userid,
- "side" => $side);
- }
- else {
- $uarray = array("status" => "Queue item not found.");
- }
- $stmt = $this->conn->prepare("DELETE FROM queue WHERE id = ?");
- $stmt->bind_param("s", $id);
- $stmt->execute();
- $this->createMatch($uid, $userid, $side);
- // create the match
- }
- else
- {
- // Create a record in the queue
- $stmt = $this->conn->prepare("INSERT INTO queue (id , userid, side) VALUES ( NULL , ?, ?);");
- $stmt->bind_param("ss", $uid, $side);
- $stmt->execute();
- $uarray = array(
- "status" => "Waiting");
- }
- return $uarray;
- }
- public function dump($data) {
- $stmt = $this->conn->prepare("INSERT INTO pictures (id , filename) VALUES ( NULL , ?)");
- $stmt->bind_param("s", $data[1]);
- $stmt->execute();
- $picid = $stmt->insert_id;
- $matchid = $data[0];
- $status = "game_started";
- $stmt = $this->conn->prepare("UPDATE matches SET picture1id = ?, status = ? WHERE id = ?");
- $stmt->bind_param("isi", $picid, $status, $matchid);
- $stmt->execute();
- return array("status" => "OK" );
- }
- public function saveMatch($arrayMatch) {
- $am = json_decode($arrayMatch);
- $id = $am->{'id'};
- $wordid = $am->{'word'};
- $status = $am->{'game_status'};
- $stmt = $this->conn->prepare("UPDATE matches SET wordid = ?, status = ? WHERE id = ?");
- $stmt->bind_param("sss", $wordid, $status, $id);
- $stmt->execute();
- if($stmt->error != "")
- {
- return array("status" => "Failed", "error" => $stmt->error);
- }
- else
- {
- return array("status" => "OK" );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement