Skip to content
Snippets Groups Projects
user.class.php 5.94 KiB
Newer Older
<?php

class user
{
    private $conn;
    public $username;
    public $id;
    public $table;

    public function __call($name, $arguments)/* here this $arguments is of default enumarated array function which
    consists of the passed parameters so if you wanted to call a value where this is saved just call or use like
    exactly how you treat the array $arguments[0],[1] and soooo onnnn */
    {
        //$name = "getBio";
        $property = preg_replace("/[^0-9a-zA-Z]/", "", substr($name, 3));
        //this regex removes the get and returns the remaining with the help of substr function which actually
        //has to be provided with a string input,offset(where to start),length in our func above it removes first three letters
        $property = strtolower(preg_replace('/\B([A-Z])/', '_$1', $property));
        //this regex converts camelcase to sanke case eg HarishRagavenDhar=>harish_ragaven_dhar;
        if(substr($name, 0, 3) == "get") {
            return $this->_get_data($property);
        } elseif(substr($name, 0, 3) == "set") {
            return $this->_set_data($property, $arguments);
        } else {
            throw new Exception("No such function is available");
        }
    }

    public static function validate($user, $email, $pass, $phone)//signup page
    {
        $conn = database::getconnetion();
        $options = [
            'cost' => 9
        ];
        $pass = password_hash($pass, PASSWORD_DEFAULT, $options);
        $sql = "INSERT INTO `user_data` (`Username`, `Email`, `Password`, `phone`,`blocked`,`active`)
        VALUES ('$user', '$email', '$pass', '$phone','0','1')";
        $result = false;
        if ($conn->query($sql) === true) {
            $result = true;
        } else {
            print("Error: " . $sql . "<br>" . $conn->error) ;
            $result = false;

        }
        $conn->close();
        return $result;
    }

    public static function login($user, $pass)//login
    {
        $conn = database::getconnetion();
        // $pass=password_verify($pass);

        $sql = "SELECT*FROM user_data WHERE Username='$user'";
        $result = $conn->query($sql);
        if($result->num_rows === 1) {
            $row = $result->fetch_assoc();
            //if($row['Username']==$user and $row['Password']==$pass)
            if(password_verify($pass, $row['Password'])) {
                return $row['Username'];
            } else {
                return false;
            }
        }
    }
    //in __construct we are fetching the username using username and the Id allotted to that user from the userSession
    public function __construct($username)
    {
        $this->table = 'user_data';
        $this->conn = database::getconnetion();
        $this->username = $username;
        $sql = "SELECT*FROM user_data WHERE `Username`='$username'OR`ID`='$username'";
        // in the OR class the $uid from the userSession.class will will be there instead of username
        //(i.e) ID = $username($this->uid) eventually we coul fetch the user from user_data table
        //print($sql);
        $result = $this->conn->query($sql);
        if($result->num_rows === 1) {
            $row = $result->fetch_assoc();
            $this->id = $row['ID'];
            // return $this->id;
            // print_r($row['ID']);
        } //else {
        //     throw new Exception("invalid username");
        // }
    }

    private function _set_data($variable, $data)
    {
        if(!$this->conn) {
            $this->conn = database::getconnetion();
        }
        TODO:/* They have replaced user_personal with users if needed that change has to be done*/
        $sql = "UPDATE '$this->table' SET $variable ='$data[0]' WHERE ID ='$this->id' " ;
        if($this->conn->query($sql)) {
            return true;
        } else {
            return false;
        }
    }
    private function _get_data($variable)
    {
        if(!$this->conn);
        {
            $this->conn = database::getconnetion();
        }
        $sql = "SELECT $variable FROM '$this->table' WHERE ID='$this->id'";
        $result = $this->conn->query($sql);
        if($result->num_rows === 1) {
            $row = $result->fetch_assoc();
            return $row[$variable];
        }

    }

    public function getUsername()
    {
        return $this->username;
    }
    public function setdob($year, $month, $day)
    {
        return $this->_set_data('dob', $year, $month, $day);
    }
    public function getDob()
    {
        return $this->_get_data('dob');
    }
    // public function setBio($bio)
    // {
    //     return $this->_set_data('bio', $bio);
    // }
    // public function getBio()
    // {
    //     return $this->_get_data('bio');
    // }
    // public function setAvatar($avatar)
    // {
    //     return $this->_set_data('avatar', $avatar);
    // }
    // public function getAvatar()
    // {
    //     return $this->_get_data('avatar');
    // }
    // public function setFirstname($first)
    // {
    //     return $this->_set_data('firstname', $first);
    // }
    // public function getFirstname()
    // {
    //     return $this->_get_data('firstname');

    // }
    // public function setLastname($last)
    // {
    //     return $this->_set_data('lastname', $last);
    // }
    // public function getLastname()
    // {
    //     return $this->_get_data('lastname');

    // }

    // public function setInstagram($insta)
    // {
    //     return $this->_set_data('instagram', $insta);
    // }
    // public function getInstagram()
    // {
    //     return $this->_get_data('instagram');

    // }
    // public function setFacebook($fb)
    // {
    //     return $this->_set_data('facebook', $fb);
    // }
    // public function getFacebook()
    // {
    //     return $this->_get_data('facebook');
    // }
    // public function setTwitter($tweet)
    // {
    //     return $this->_set_data('twitter', $tweet);
    // }
    // public function getTwitter()
    // {
    //     return $this->_get_data('twitter');
    // }

}