Advertisement
Guest User

DataManagerDao

a guest
Jan 10th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.62 KB | None | 0 0
  1. class DataManagerDao
  2. {
  3.     var $dbhost = null;
  4.     var $dbuser = null;
  5.     var $dbpassword = null;
  6.     var $dbname = null;
  7.     var $connection = null;
  8.     var $result = null;
  9.  
  10.     function __construct(){
  11.  
  12.         $this->dbhost = Connection::__getDBHost();
  13.         $this->dbuser = Connection::__getDBUser();
  14.         $this->dbpassword = Connection::__getDBPassword();
  15.         $this->dbname = Connection::__getDBName();
  16.  
  17.     }
  18.  
  19.     public function openDBConnection(){
  20.  
  21.         try{
  22.  
  23.             $this->connection = new mysqli($this->dbhost, $this->dbuser, $this->dbpassword, $this->dbname);
  24.  
  25.         }catch (Exception $ex){
  26.  
  27.             die("Non รจ possibile connettersi al database " . $ex->getMessage());
  28.         }
  29.  
  30.     }
  31.  
  32.     // funzione per la connessione al database mysql
  33.     public function db_connection(){
  34.  
  35.         $this->connection = new mysqli($this->dbhost, $this->dbuser, $this->dbpassword, $this->dbname);
  36.  
  37.         if (mysqli_connect_error()) {
  38.             return false;
  39.         }
  40.  
  41.         mysqli_set_charset($this->connection, 'utf8');
  42.  
  43.         return true;
  44.  
  45.     }
  46.  
  47.     function isDBConnected(){
  48.  
  49.         if ($this->connection != null)
  50.             return true;
  51.  
  52.         return false;
  53.     }
  54.  
  55.     // restituisce la connessione
  56.     public function getConnection(){
  57.  
  58.         return $this->connection;
  59.  
  60.     }
  61.  
  62.     public function closeDBConnection(){
  63.  
  64.         if (!empty($this->connection)) {
  65.  
  66.             $this->connection->close();
  67.         }
  68.  
  69.     }
  70.  
  71.     public function getUserDetails($email){
  72.  
  73.         $returnValue = array();
  74.         $sql = "SELECT * FROM account_user WHERE Email = '" . $email . "'";
  75.         $result = $this->connection->query($sql);
  76.  
  77.         if ($result != null && (mysqli_num_rows($result) >= 1)){
  78.  
  79.             $row = $result->fetch_array(MYSQLI_ASSOC);
  80.  
  81.             if (!empty($row)){
  82.  
  83.                 $returnValue = $row;
  84.  
  85.             }
  86.         }
  87.  
  88.         return $returnValue;
  89.  
  90.     }
  91.  
  92.     public function getUserDetailsWithPassword($email, $password){
  93.  
  94.         $returnValue = array();
  95.         $sql = "SELECT User_ID, Email  FROM account_user WHERE Email = '" . $email . "' AND Password = '" . $password . "'";
  96.         $result = $this->connection->query($sql);
  97.  
  98.         if ($result != null && (mysqli_num_rows($result) >= 1)){
  99.  
  100.             $row = $result->fetch_array(MYSQLI_ASSOC);
  101.  
  102.             if (!empty($row)){
  103.  
  104.                 $returnValue = $row;
  105.  
  106.             }
  107.         }
  108.  
  109.         return $returnValue;
  110.  
  111.     }
  112.  
  113.     public function registerUser($username, $email, $password){
  114.  
  115.         $sql = "INSERT INTO account_user SET Username=?, Email=?, Password=?";
  116.         $stmt = $this->connection->prepare($sql);
  117.  
  118.         if (!$stmt){
  119.  
  120.             throw new Exception($stmt->error);
  121.  
  122.         }
  123.  
  124.         $stmt->bind_param("sss", $username, $email, $password);
  125.  
  126.         $returnValue = $stmt->execute();
  127.  
  128.         return $returnValue;
  129.  
  130.     }
  131.  
  132.     public function storeEmailToken($user_id, $emailToken){
  133.  
  134.         $sql = "INSERT INTO email_tokens SET User_ID=?, email_token=?";
  135.         $stmt = $this->connection->prepare($sql);
  136.  
  137.         if (!$stmt){
  138.  
  139.             throw new Exception($stmt->error);
  140.  
  141.         }
  142.  
  143.         $stmt->bind_param("is", $user_id, $emailToken);
  144.  
  145.         $returnValue = $stmt->execute();
  146.  
  147.         return $returnValue;
  148.  
  149.     }
  150.  
  151.     public function getUserWithToken($emailToken){
  152.  
  153.         $returnValue = array();
  154.         $sql = "SELECT User_ID, email_token FROM email_tokens WHERE email_token='" . $emailToken ."'";
  155.  
  156.         $result = $this->connection->query($sql);
  157.  
  158.         if ($result != null && (mysqli_num_rows($result) >= 1)){
  159.  
  160.             $row = $result->fetch_array(MYSQLI_ASSOC);
  161.  
  162.             if (!empty($row)){
  163.  
  164.                 $returnValue = $row['User_ID'];
  165.  
  166.             }
  167.         }
  168.  
  169.         return $returnValue;
  170.  
  171.     }
  172.  
  173.     public function setEmailConfirmedStatus($status, $user_id){
  174.  
  175.         $sql = "UPDATE account_user SET isEmailConfirmed=? WHERE User_ID=?";
  176.         $stmt = $this->connection->prepare($sql);
  177.  
  178.         if (!$stmt)
  179.             throw  new Exception($stmt->error);
  180.  
  181.         $stmt->bind_param("ii", $status, $user_id);
  182.         $returnValue = $stmt->execute();
  183.  
  184.         return $returnValue;
  185.     }
  186.  
  187.     public function deleteUsedToken($emailToken){
  188.  
  189.         $sql = "DELETE FROM email_tokens WHERE email_token=?";
  190.         $stmt = $this->connection->prepare($sql);
  191.  
  192.         if (!$stmt)
  193.             throw  new Exception($stmt->error);
  194.  
  195.         $stmt->bind_param("s", $emailToken);
  196.         $returnValue = $stmt->execute();
  197.  
  198.         return $returnValue;
  199.  
  200.     }
  201.  
  202.  
  203.  
  204.  
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement