Advertisement
Guest User

Untitled

a guest
May 12th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.03 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Created by PhpStorm.
  5.  * User: Thomas kjos
  6.  * Date: 08.04.2016
  7.  * Time: 13:33
  8.  */
  9. class member
  10. {
  11.  
  12.     private $member_Id;
  13.     private $member_name;
  14.     private $email;
  15.     private $email_checked;
  16.     private $logged_in;
  17.     private $hash_salted;
  18.     private $permissions;
  19.     private $post_count;
  20.     private $comment_count;
  21.     private $randomvalue;
  22.  
  23.     public function __construct()
  24.     {
  25.     }
  26.  
  27.  
  28.     public static function loadAllUsers()
  29.     {
  30.         $serverQuery = db()->prepare("SELECT * FROM member");
  31.  
  32.         if ($serverQuery->execute()) {
  33.             $users = array();
  34.             while ($user = $serverQuery->fetchObject('member')) {
  35.                 $users[] = $user;
  36.             }
  37.             return $users;
  38.         } else {
  39.             return false;
  40.         }
  41.     }
  42.  
  43.     public function loginUser($username, $password)
  44.     {
  45.         $serverQuery = db()->prepare("SELECT * FROM member WHERE member_name=:username");
  46.         $serverQuery->bindParam(':username', $username, PDO::PARAM_STR);
  47.         $serverQuery->execute();
  48.  
  49.         $data = $serverQuery->fetch(PDO::FETCH_ASSOC);
  50.         $this->bindUserData($data);
  51.  
  52.         $passwordCheck = password_verify($password, $this->hash_salted);
  53.  
  54.         if ($passwordCheck && $this->email_checked == 1) {
  55.             $this->logged_in = true;
  56.             echo "Success";
  57.             return true;
  58.         } else if ($passwordCheck && $this->email_checked == 0) {
  59.             echo "Brukeren er ikke validert";
  60.             return false;
  61.         } else {
  62.             echo "Brukernavn eller passord er feil!";
  63.             return false;
  64.         }
  65.     }
  66.  
  67.     public function bindUserData($data)
  68.     {
  69.         $this->member_Id = $data['member_Id'];
  70.         $this->member_name = $data['member_name'];
  71.         $this->hash_salted = $data['hash_salt'];
  72.         $this->email = $data['email'];
  73.         $this->permissions = $data['permissions'];
  74.         $this->email_checked = $data['email_checked'];
  75.         $this->post_count = $data['post_count'];
  76.         $this->comment_count =$data['comment_count'];
  77.     }
  78.  
  79.     public function registerUser($username, $password, $email)
  80.     {
  81.         $this->member_name = $username;
  82.         $this->hash_salted = password_hash($password, PASSWORD_DEFAULT);
  83.         $this->email = $email;
  84.         $this->email_checked = 0;
  85.         $this->post_count = 0;
  86.         $this->permissions = 0;
  87.         $this->randomvalue = random_int(1000000000, 2147483647);
  88.  
  89.         $datetoday = date('Y-m-d H:i:s');
  90.         $serverQuery = db()->prepare("INSERT INTO member (member_name, hash_salt, email, created, permissions, randomvalue, email_checked, post_count)
  91.                                                        VALUES (:memberName, :hashSalt, :email,:created,:permissions,:randomvalue,:email_checked,:post_count)");
  92.         $serverQuery->bindParam(':memberName', $this->member_name, PDO::PARAM_STR);
  93.         $serverQuery->bindParam(':hashSalt', $this->hash_salted, PDO::PARAM_STR);
  94.         $serverQuery->bindParam(':email', $this->email, PDO::PARAM_STR);
  95.         $serverQuery->bindParam(':created', $datetoday, PDO::PARAM_STR);
  96.         $serverQuery->bindParam(':permissions', $this->permissions, PDO::PARAM_INT);
  97.         $serverQuery->bindParam(':randomvalue', $this->randomvalue, PDO::PARAM_INT);
  98.         $serverQuery->bindParam(':email_checked', $this->email_checked, PDO::PARAM_BOOL);
  99.         $serverQuery->bindParam(':post_count', $this->post_count, PDO::PARAM_INT);
  100.         $serverQuery->execute();
  101.     }
  102.  
  103.     public function sendValidationEmail($username, $email)
  104.     {
  105.         $subject = "New account registration";
  106.         $message = "Welcome " . $username . ", you need to verify your account, to do so click this link: http://" . $_SERVER['HTTP_HOST'] . "/prosjekt/registerForm.php?value=" . $this->randomvalue;
  107.         mail($email, $subject, $message);
  108.     }
  109.  
  110.     public function validateUser($randomValue)
  111.     {
  112.         $validValue = 1;
  113.         $serverQuery = db()->prepare("UPDATE member SET email_checked = :validValue WHERE randomvalue =:randomvalue ");
  114.         $serverQuery->bindParam(':randomvalue', $randomValue, PDO::PARAM_INT);
  115.         $serverQuery->bindParam(':validValue', $validValue, PDO::PARAM_INT);
  116.         $serverQuery->execute();
  117.         if ($serverQuery->rowCount() == 1) {
  118.             return true;
  119.         } else {
  120.             return false;
  121.         }
  122.     }
  123.  
  124.     public function changeUserName($name)
  125.     {
  126.         $this->member_name = $name;
  127.         $serverQuery = db()->prepare("UPDATE member SET member_name=:newName WHERE member_Id=:id");
  128.         $serverQuery->bindParam('newName', $this->member_name, PDO::PARAM_STR);
  129.         $serverQuery->bindParam(':id', $this->member_Id, PDO::PARAM_INT);
  130.         $serverQuery->execute();
  131.         if ($serverQuery) {
  132.             return true;
  133.         } else {
  134.             return false;
  135.         }
  136.     }
  137.  
  138.     public function sendUserRequestToChangeMail()
  139.     {
  140.         $this->randomvalue = random_int(1000000000, 2147483647);
  141.         $serverQuery = db()->prepare("UPDATE member SET randomvalue=:randomValue Where member_Id=:id");
  142.         $serverQuery->bindParam(':randomValue', $this->randomvalue, PDO::PARAM_STR);
  143.         $serverQuery->bindParam(':id', $this->member_Id, PDO::PARAM_INT);
  144.         $serverQuery->execute();
  145.         $subject = "Email change request";
  146.  
  147.         $message = $this->member_name . " Someone has requested an email change for this account, if it was you and you intend to change the email you can use this link to do so: http://" . $_SERVER['HTTP_HOST'] . "/prosjekt/Profile.php?value=" . $this->randomvalue;
  148.  
  149.         mail($this->email, $subject, $message);
  150.     }
  151.  
  152.     public function sendUserRequestToChangePassword()
  153.     {
  154.         $this->randomvalue = random_int(1000000000, 2147483647);
  155.         $serverQuery = db()->prepare("UPDATE member SET randomvalue=:randomValue Where member_Id=:id");
  156.         $serverQuery->bindParam(':randomValue', $this->randomvalue, PDO::PARAM_STR);
  157.         $serverQuery->bindParam(':id', $this->member_Id, PDO::PARAM_INT);
  158.         $serverQuery->execute();
  159.         $subject = "Password change request";
  160.  
  161.         $message = $this->member_name . " Someone has requested an password change for this account, if it was you and you intend to change the password you can use this link to do so: http://" . $_SERVER['HTTP_HOST'] . "/prosjekt/Profile.php?pwdcreq=" . $this->randomvalue;
  162.  
  163.         mail($this->email, $subject, $message);
  164.     }
  165.  
  166.     public function changeUserPassword($password, $randomvalue)
  167.     {
  168.  
  169.         $this->hash_salted = password_hash($password, PASSWORD_DEFAULT);
  170.         $serverQuery = db()->prepare("UPDATE member SET hash_salt=:hash_salted WHERE randomvalue=:randomvalue");
  171.         $serverQuery->bindParam(':hash_salted', $this->hash_salted, PDO::PARAM_STR);
  172.         $serverQuery->bindParam(':randomvalue', $randomvalue, PDO::PARAM_INT);
  173.  
  174.         if ($serverQuery->execute()) {
  175.             return true;
  176.         } else {
  177.             return false;
  178.         }
  179.     }
  180.  
  181.     public function changeUserEmail($newMail, $randomvalue)
  182.     {
  183.         $this->email = $newMail;
  184.         $serverQuery = db()->prepare("UPDATE member SET email=:newMail WHERE member_Id=:id AND randomvalue=:randomvalue");
  185.         $serverQuery->bindParam('newMail', $this->email, PDO::PARAM_STR);
  186.         $serverQuery->bindParam(':id', $this->member_Id, PDO::PARAM_INT);
  187.         $serverQuery->bindParam(':randomvalue', $randomvalue, PDO::PARAM_INT);
  188.  
  189.         if ($serverQuery->execute()) {
  190.             return true;
  191.         } else {
  192.             return false;
  193.         }
  194.     }
  195.  
  196.     public function forgotPassword($username)
  197.     {
  198.         $this->randomvalue = random_int(1000000000, 2147483647);
  199.         $serverQuery = db()->prepare("UPDATE member SET randomvalue=:randomvalue WHERE member_name =:username");
  200.         $serverQuery->bindParam(':randomvalue', $this->randomvalue, PDO::PARAM_INT);
  201.         $serverQuery->bindParam(':username', $username, PDO::PARAM_STR);
  202.         if ($serverQuery->execute()) {
  203.             $this->sendForgotPasswordMail();
  204.             return true;
  205.         } else {
  206.             return false;
  207.         }
  208.     }
  209.  
  210.     private function sendForgotPasswordMail()
  211.     {
  212.         $serverQuery = db()->prepare("SELECT email FROM member WHere randomvalue = :randomValue");
  213.         $serverQuery->bindParam(':randomValue', $this->randomvalue, PDO::PARAM_INT);
  214.         if ($serverQuery->execute()) {
  215.             $data = $serverQuery->fetch(PDO::FETCH_ASSOC);
  216.             $to = $data['email'];
  217.             $subject = "Forgot Password";
  218.             $message = "It seems you have forgot your password. To change it you can click this link: http://" . $_SERVER['HTTP_HOST'] . "/prosjekt/RegisterForm.php?forgotPassword=" . $this->randomvalue;
  219.             mail($to, $subject, $message);
  220.         }
  221.  
  222.     }
  223.  
  224.     public function authenticateUser()
  225.     {
  226.         if ($this->logged_in && $this->email_checked) {
  227.             return true;
  228.         } else {
  229.             return false;
  230.         }
  231.     }  // do i need this?
  232.  
  233.     public function getUserId()
  234.     {
  235.         return $this->member_Id;
  236.     }
  237.  
  238.     public function getRandomValue()
  239.     {
  240.         return $this->randomvalue;
  241.     }
  242.  
  243.     public function setUserId($member_Id)
  244.     {
  245.         $this->member_Id = $member_Id;
  246.     }
  247.  
  248.     public function getUserName()
  249.     {
  250.         return $this->member_name;
  251.     }
  252.  
  253.     public function setUserName($member_name)
  254.     {
  255.         $this->member_name = $member_name;
  256.     }
  257.  
  258.     public function getUserEmail()
  259.     {
  260.         return $this->email;
  261.     }
  262.  
  263.  
  264.     public function incrementCommentCount()
  265.     {
  266.         $this->comment_count++;//should probably just do this in the database but this is good enough for now
  267.     }
  268.  
  269.     public function setUserEmail($member_email)
  270.     {
  271.         $this->email = $member_email;
  272.     }
  273.  
  274.     public function getValidUser()
  275.     {
  276.         return $this->email_checked;
  277.     }
  278.  
  279.     public function setValidUser($valid_user)
  280.     {
  281.         $this->email_checked = $valid_user;
  282.     }
  283.  
  284.  
  285.     public function getLoggedIn()
  286.     {
  287.         return $this->logged_in;
  288.     }
  289.  
  290.     public function setLoggedIn($logged_in)
  291.     {
  292.         $this->logged_in = $logged_in;
  293.     }
  294.  
  295.     public function getHashSalted()
  296.     {
  297.         return $this->hash_salted;
  298.     }
  299.  
  300.     public function getPermissions()
  301.     {
  302.         return $this->permissions;
  303.     }
  304.  
  305.     public function setPermissions($permissions)
  306.     {
  307.         $this->permissions = $permissions;
  308.     }
  309.  
  310.     public function getPostCount()
  311.     {
  312.         return $this->post_count;
  313.     }
  314.  
  315.     public function getCommentCount()
  316.     {
  317.         return $this->comment_count;
  318.     }
  319.  
  320.     public function checkIfUserAlreadyExists($clean_username)
  321.     {
  322.         $serverQuery = db()->prepare("SELECT * FROM member WHERE member_name =:username");
  323.         $serverQuery->bindParam(':username', $clean_username, PDO::PARAM_STR);
  324.         $serverQuery->execute();
  325.         return $serverQuery->rowCount();
  326.     }
  327.  
  328.     public function checkIfEmailAlreadyExists($clean_email)
  329.     {
  330.         $serverQuery = db()->prepare("SELECT * FROM member WHERE email =:email");
  331.         $serverQuery->bindParam(':email', $clean_email, PDO::PARAM_STR);
  332.         $serverQuery->execute();
  333.         return $serverQuery->rowCount();
  334.     }
  335.  
  336.     public function deleteSelectedUser($id)
  337.     {
  338.         $serverQuery = db()->prepare("DELETE FROM member WHERE member_Id=:id");
  339.         $serverQuery->bindParam(':id', $id, PDO::PARAM_INT);
  340.         if ($serverQuery->execute()) {
  341.             //do something, give feedback that it was successfull
  342.         } else {
  343.             //tell the user that it failed
  344.         }
  345.     }
  346.  
  347.     public function giveUserAdminPriv($id)
  348.     {
  349.         $giveAdminPermissions = 1;
  350.         $serverQuery = db()->prepare("UPDATE member SET permissions = :permissions WHERE member_Id=:id ");
  351.         $serverQuery->bindParam(':permissions', $giveAdminPermissions, PDO::PARAM_INT);
  352.         $serverQuery->bindParam(':id', $id, PDO::PARAM_INT);
  353.         $serverQuery->execute();
  354.     }
  355.     public function removeUserAdminPriv($id)
  356.     {
  357.         $giveAdminPermissions = 0;
  358.         $serverQuery = db()->prepare("UPDATE member SET permissions = :permissions WHERE member_Id=:id ");
  359.         $serverQuery->bindParam(':permissions', $giveAdminPermissions, PDO::PARAM_INT);
  360.         $serverQuery->bindParam(':id', $id, PDO::PARAM_INT);
  361.         $serverQuery->execute();
  362.     }
  363.  
  364.     public function updateUserPostCount($id)
  365.     {
  366.         $serverQuery = db()->prepare("UPDATE member SET post_count = post_count+1 WHERE member_Id=:id");
  367.         $serverQuery->bindParam(':id', $id, PDO::PARAM_INT);
  368.         $serverQuery->execute();
  369.         $this->post_count = $this->post_count++;
  370.     }
  371.  
  372.     public function updateUserCommentCount($id)
  373.     {
  374.         $serverQuery = db()->prepare("UPDATE member SET comment_count = comment_count+1 WHERE member_Id=:id");
  375.         $serverQuery->bindParam(':id', $id, PDO::PARAM_INT);
  376.         $serverQuery->execute();
  377.         $this->comment_count = $this->comment_count++;
  378.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement