Blame view

htdocs/documents/include/DB_Functions.php 4.1 KB
c10d2abc   rcavalie   ajout fichiers se...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  <?php
  
  class DB_Functions {
  
      private $conn;
  
      // constructor
      function __construct() {
          require_once 'DB_Connect.php';
          // connecting to database
          $db = new Db_Connect();
          $this->conn = $db->connect();
      }
  
      // destructor
      function __destruct() {
          
      }
  
      /**
       * Storing new user
       * returns user details
       */
      public function storeUser($name, $username, $password){
          $uuid = uniqid('', true);
          $hash = $this->hashSSHA($password);
          $encrypted_password = $hash["encrypted"]; // encrypted password
          $salt = $hash["salt"]; // salt
  		
          $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, username, encrypted_password, salt) VALUES(?, ?, ?, ?, ?)");
          $stmt->bind_param("sssss", $uuid, $name, $username, $encrypted_password, $salt);
          $result = $stmt->execute();
          $stmt->close();
  
          // check for successful store
          if ($result) {
              $stmt = $this->conn->prepare("SELECT * FROM users WHERE username = ?");
              $stmt->bind_param("s", $username);
              $stmt->execute();
              $user = $stmt->get_result()->fetch_assoc();
              $stmt->close();
  
              return $user;
          } else {
              return false;
          }
      }
  
      /**
       * Get user by username and password
       */
      public function getUserByLoginAndPassword($username, $password) {
  
          $stmt = $this->conn->prepare("SELECT * FROM users WHERE username = ?");
  
          $stmt->bind_param("s", $username);
  
          if ($stmt->execute()) {
              $user = $stmt->get_result()->fetch_assoc();
              $stmt->close();
  
              // verifying user password
              $salt = $user['salt'];
              $encrypted_password = $user['encrypted_password'];
              $hash = $this->checkhashSSHA($salt, $password);
              // check for password equality
              if ($encrypted_password == $hash) {
                  // user authentication details are correct
                  return $user;
              }
          } else {
              return NULL;
          }
      }
  	
  	/**
       * Get all users
       */
      function getUsers() {
          $stmt = $this->conn->prepare("SELECT username FROM users");
  		$stmt->execute();
  		$users = $stmt->get_result()->fetch_all();
          $stmt->close();
          return $users;
      }
  
      /**
       * Check user is existed or not
       */
      public function isUserExisted($username) {
          $stmt = $this->conn->prepare("SELECT username from users WHERE username = ?");
  
          $stmt->bind_param("s", $username);
  
          $stmt->execute();
  
          $stmt->store_result();
  
          if ($stmt->num_rows > 0) {
              // user existed 
              $stmt->close();
              return true;
          } else {
              // user not existed
              $stmt->close();
              return false;
          }
      }
  	
  	/**
       * Delete a user if user existed
       */
  	public function deleteUser($username) {
          $stmt = $this->conn->prepare("SELECT username from users WHERE username = ?");
  
          $stmt->bind_param("s", $username);
  
          $stmt->execute();
  
          $stmt->store_result();
  
          if ($stmt->num_rows > 0) {
              // user existed
  			$stmt = $this->conn->prepare("DELETE from users WHERE username = ?");
  			$stmt->bind_param("s", $username);
  			$stmt->execute();
              $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;
      }
  
  }
  
  ?>