<?php

/**manasera
 */

class DB_Functions {

    private $conn;

    // constructor
    function __construct() {
        require_once ('../credentials.php');
        // connecting to database
        $this->conn = mysqli_connect(HOST, USERNAME, PASSWORD, DBNAME) or die("Connection failed: " . mysqli_connect_error());
        
    }

    // destructor
    function __destruct() {
        
    }

    /**
     * Storing new user
     * returns user details
     */
    public function storeUser($firstname,$lastname,$level,$sex,$department,$faculty,$user_id,$month,$day,$password,$who) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt
        $stmt = $this->conn->prepare("INSERT INTO ".$who."(firstname,lastname,level,sex,department,faculty,user_id,month,day,salt,password) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
        $stmt->bind_param("sssssssssss", $firstname,$lastname,$level,$sex,$department,$faculty,$user_id,$month,$day,$salt,$encrypted_password);
        $result = $stmt->execute();
        $stmt->close();

        // check for successful store
        if ($result) {
            $stmt = $this->conn->prepare("SELECT * FROM ".$who." WHERE user_id = ?");
            $stmt->bind_param("s", $user_id);
            $stmt->execute();
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();

            return $user;
        } else {
            return false;
        }
    }

	/**
     * Storing new parent user
     * returns user details
     */
    public function storeUser_parent($firstname,$lastname,$phone,$sex,$user_id,$month,$day,$password,$who) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt
        $stmt = $this->conn->prepare("INSERT INTO ".$who."(firstname,lastname,phone,sex,user_id,month,day,salt,password) VALUES(?,?,?,?,?,?,?,?,?)");
        $stmt->bind_param("sssssssss", $firstname,$lastname,$phone,$sex,$user_id,$month,$day,$salt,$encrypted_password);
        $result = $stmt->execute();
        $stmt->close();

        // check for successful store
        if ($result) {
            $stmt = $this->conn->prepare("SELECT * FROM ".$who." WHERE user_id = ?");
            $stmt->bind_param("s", $user_id);
            $stmt->execute();
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();

            return $user;
        } else {
            return false;
        }
    }

    /**
     * Get user by email and password
     */
    public function getUserByEmailAndPassword($user_id, $password,$who) {

        $stmt = $this->conn->prepare("SELECT * FROM ".$who." WHERE user_id = ?");

        $stmt->bind_param("s", $user_id);

        if ($stmt->execute()) {
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();

            // verifying user password
            $salt = $user['salt'];
            $encrypted_password = $user['password'];
            $hash = $this->checkhashSSHA($salt, $password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                return $user;
            }
        } else {
            return NULL;
        }
    }

    /**
     * Check user is existed or not
     */
    public function isUserExisted($user_id,$who) {
        $stmt = $this->conn->prepare("SELECT user_id FROM ".$who." WHERE user_id = ?");

        $stmt->bind_param("s", $user_id);

		
        $stmt->execute();

        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            // user existed 
            $stmt->close();
            return true;
        } else {
            // user not existed
            $stmt->close();
            return false;
        }
    }

    /**
     * Encrypting password
     * @param password
     * returns salt and encrypted password
     */
    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;
    }

    /**
     * Decrypting password
     * @param salt, password
     * returns hash string
     */
    public function checkhashSSHA($salt, $password) {

        $hash = base64_encode(sha1($password . $salt, true) . $salt);

        return $hash;
    }

}

?>
