Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.20 KB | None | 0 0
  1. <?php
  2.  
  3. class User{
  4.     static private $conn;
  5.  
  6.     private $name;
  7.     private $id;
  8.     private $email;
  9.     private $info;
  10.     private $password;
  11.  
  12.     // This function sets connection for this class to use
  13.     // This function needs to be run on startup
  14.     public static function SetConnection($newConnection){
  15.         User::$conn = $newConnection;
  16.     }
  17.  
  18.     //this function returns:
  19.     //  null id user with given id is not in db
  20.     //  User loaded from db if id is ok
  21.     public static function GetUser($id){
  22.         $sqlStatement = "Select * from Users where id = '$id'";
  23.         $result = User::$conn->query($sqlStatement);
  24.         if ($result->num_rows == 1) {
  25.             $userData = $result->fetch_assoc();
  26.             return new User($userData['id'], $userData['name'], $userData['info'], $userData['email'], $userData['password']);
  27.         }
  28.         //there is user with this name in db
  29.         return -1;
  30.     }
  31.  
  32.     //this function returns:
  33.     //   null if user exist in database
  34.     //   new User object if new entry was added to table
  35.     public static function CreateUser($userMail, $password){
  36.         $sqlStatement = "Select * from Users where email = '$userMail'";
  37.         $result = User::$conn->query($sqlStatement);
  38.         if ($result->num_rows == 0) {
  39.             //inserting user to db
  40.             $hashed_password = md5($password);
  41.             $sqlStatement = "INSERT INTO Users(name, email, password, info) values ('', '$userMail', '$hashed_password', '')";
  42.             if (User::$conn->query($sqlStatement) === TRUE) {
  43.                 //entery was added to DB so we can return new object
  44.                 return new User(User::$conn->insert_id, 'jakies', $userMail, 'glupoty', $hashed_password);
  45.             }
  46.         }
  47.         //there is user with this name in db
  48.         return null;
  49.     }
  50.  
  51.     //this function returns:
  52.     //   null if user does not exist in database or password does not match
  53.     //   new User object if User was authenticated
  54.     public static function AuthenticateUser($userMail, $password){
  55.         $sqlStatement = "Select * from Users where email = '$userMail'";
  56.         $result = User::$conn->query($sqlStatement);
  57.         if ($result->num_rows == 1) {
  58.             $userData = $result->fetch_assoc();
  59.             $user = new User($userData['id'], $userData['name'], $userData['email'], $userData['info'], $userData['password']);
  60.  
  61.             if($user->authenticate($password)){
  62.                 //User is authenticated - we can return him
  63.                 return $user;
  64.             }
  65.         }
  66.         //there is no user with this name in db or User was not authenticated
  67.         return null;
  68.     }
  69.  
  70.     //this function return:
  71.     //   true if user was deleted
  72.     //   false if not
  73.     public static function DeleteUser(User $toDelete, $password){
  74.         if($toDelete->authenticate($password)){
  75.             $userMail = $toDelete->getEmail();
  76.             $sql = "DELETE FROM Users WHERE email = '$userMail'";
  77.             if (User::$conn->query($sql) === TRUE) {
  78.                 return true;
  79.             }
  80.         }
  81.         return false;
  82.     }
  83.  
  84.     public static function GetAllUserNames(){
  85.         $ret = array();
  86.         $sqlStatement = "Select id, name, email from Users";
  87.         $result = User::$conn->query($sqlStatement);
  88.         if ($result->num_rows > 0) {
  89.             while($row = $result->fetch_assoc()){
  90.                 $ret[] = $row;
  91.             }
  92.         }
  93.         return $ret;
  94.     }
  95.  
  96.     public static function GetUserInfo($id){
  97.         $sqlStatement = "Select id, name, email, info from Users where id=$id";
  98.         $result = User::$conn->query($sqlStatement);
  99.         if ($result->num_rows > 0) {
  100.             return $result->fetch_assoc();
  101.         }
  102.         return null;
  103.     }
  104.  
  105.     public function __construct($newId, $newName, $newMail, $newInfo, $password){
  106.         $this->id = $newId;
  107.         $this->name = $newName;
  108.         $this->email = $newMail;
  109.         $this->info = $newInfo;
  110.         $this->password = $password;
  111.     }
  112.     // @codeCoverageIgnoreStart
  113.     public function getId(){
  114.         return $this->id;
  115.     }
  116.  
  117.     public function getName(){
  118.         return $this->name;
  119.     }
  120.  
  121.     public function setName($newName){
  122.         $this->name = $newName;
  123.     }
  124.  
  125.     public function getEmail(){
  126.         return $this->email;
  127.     }
  128.  
  129.     public function setEmail($newEmail){
  130.         $this->email = $newEmail;
  131.     }
  132.  
  133.     public function getInfo(){
  134.         return $this->info;
  135.     }
  136.  
  137.     public function setInfo($newInfo){
  138.         $this->info = $newInfo;
  139.     }
  140.  
  141.     public function setPassword($newPassword){
  142.         $this->password = md5($newPassword);
  143.     }
  144.     // @codeCoverageIgnoreEnd
  145.  
  146.     //this function is responsible for saving any changes done to User to database
  147.     public function saveToDB(){
  148.         $sql = "UPDATE Users SET name='{$this->name}', email='{$this->email}', info='{$this->info}', password='{$this->password}' WHERE id={$this->id}";
  149.         return User::$conn->query($sql);
  150.     }
  151.  
  152.     public function authenticate($password){
  153.         if(md5($password) == $this->password){
  154.             //User is verified
  155.             return true;
  156.         }
  157.         return false;
  158.     }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement