Guest User

Untitled

a guest
May 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. $db_host = "localhost";
  5. $db_username = "root";
  6. $db_name = "android_api";
  7.  
  8. @mysql_connect("$db_host","$db_username") or die("Could not connect to MySQL");
  9. @mysql_select_db("$db_name") or die("Could not find database");
  10.  
  11. ?>
  12.  
  13. <?php
  14. class DB_Connect {
  15. private $conn;
  16.  
  17. // Connecting to database
  18. public function connect() {
  19. require_once 'include/Config.php';
  20.  
  21. // Connecting to mysql database
  22. //$this->conn = new mysqli($db_host,$db_username,$db_name);
  23. $this->conn = (@mysql_connect("$db_host","$db_username") or die("Could not connect to MySQL"))&&(@mysql_select_db("$db_name") or die("Could not find database"));
  24.  
  25.  
  26. // return database handler
  27. return $this->conn;
  28.  
  29.  
  30. }
  31. }
  32.  
  33. ?>
  34.  
  35. <?php
  36.  
  37. class DB_Functions {
  38.  
  39. private $conn;
  40.  
  41. // constructor
  42. function __construct() {
  43. require_once 'include/DB_Connect.php';
  44. // connecting to database
  45. $db = new Db_Connect();
  46. $this->conn = $db->connect();
  47. }
  48.  
  49. // destructor
  50. function __destruct() {
  51.  
  52. }
  53.  
  54. /**
  55. * Storing new user
  56. * returns user details
  57. */
  58. public function storeUser($name, $email, $password) {
  59. $uuid = uniqid('', true);
  60. $hash = $this->hashSSHA($password);
  61. $encrypted_password = $hash["encrypted"]; // encrypted password
  62. $salt = $hash["salt"]; // salt
  63.  
  64. $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
  65. $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
  66. $result = $stmt->execute();
  67. $stmt->close();
  68.  
  69. // check for successful store
  70. if ($result) {
  71. $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  72. $stmt->bind_param("s", $email);
  73. $stmt->execute();
  74. $user = $stmt->get_result()->fetch_assoc();
  75. $stmt->close();
  76.  
  77. return $user;
  78. } else {
  79. return false;
  80. }
  81. }
  82.  
  83. /**
  84. * Get user by email and password
  85. */
  86. public function getUserByEmailAndPassword($email, $password) {
  87.  
  88. $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  89.  
  90. $stmt->bind_param("s", $email);
  91.  
  92. if ($stmt->execute()) {
  93. $user = $stmt->get_result()->fetch_assoc();
  94. $stmt->close();
  95.  
  96. // verifying user password
  97. $salt = $user['salt'];
  98. $encrypted_password = $user['encrypted_password'];
  99. $hash = $this->checkhashSSHA($salt, $password);
  100. // check for password equality
  101. if ($encrypted_password == $hash) {
  102. // user authentication details are correct
  103. return $user;
  104. }
  105. } else {
  106. return NULL;
  107. }
  108. }
  109.  
  110. /**
  111. * Check user is existed or not
  112. */
  113. public function isUserExisted($email) {
  114. $stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
  115.  
  116. $stmt->bind_param("s", $email);
  117.  
  118. $stmt->execute();
  119.  
  120. $stmt->store_result();
  121.  
  122. if ($stmt->num_rows > 0) {
  123. // user existed
  124. $stmt->close();
  125. return true;
  126. } else {
  127. // user not existed
  128. $stmt->close();
  129. return false;
  130. }
  131. }
  132.  
  133. /**
  134. * Encrypting password
  135. * @param password
  136. * returns salt and encrypted password
  137. */
  138. public function hashSSHA($password) {
  139.  
  140. $salt = sha1(rand());
  141. $salt = substr($salt, 0, 10);
  142. $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
  143. $hash = array("salt" => $salt, "encrypted" => $encrypted);
  144. return $hash;
  145. }
  146.  
  147. /**
  148. * Decrypting password
  149. * @param salt, password
  150. * returns hash string
  151. */
  152. public function checkhashSSHA($salt, $password) {
  153.  
  154. $hash = base64_encode(sha1($password . $salt, true) . $salt);
  155.  
  156. return $hash;
  157. }
  158.  
  159. }
  160.  
  161. ?>
  162.  
  163. <?php
  164.  
  165. require_once 'include/DB_Functions.php';
  166. $db = new DB_Functions();
  167.  
  168. // json response array
  169. $response = array("error" => FALSE);
  170.  
  171. if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
  172.  
  173. // receiving the post params
  174. $name = $_POST['name'];
  175. $email = $_POST['email'];
  176. $password = $_POST['password'];
  177.  
  178. // check if user is already existed with the same email
  179. if ($db->isUserExisted($email)) {
  180. // user already existed
  181. $response["error"] = TRUE;
  182. $response["error_msg"] = "User already existed with " . $email;
  183. echo json_encode($response);
  184. } else {
  185. // create a new user
  186. $user = $db->storeUser($name, $email, $password);
  187. if ($user) {
  188. // user stored successfully
  189. $response["error"] = FALSE;
  190. $response["uid"] = $user["unique_id"];
  191. $response["user"]["name"] = $user["name"];
  192. $response["user"]["email"] = $user["email"];
  193. $response["user"]["created_at"] = $user["created_at"];
  194. $response["user"]["updated_at"] = $user["updated_at"];
  195. echo json_encode($response);
  196. } else {
  197. // user failed to store
  198. $response["error"] = TRUE;
  199. $response["error_msg"] = "Unknown error occurred in registration!";
  200. echo json_encode($response);
  201. }
  202. }
  203. } else {
  204. $response["error"] = TRUE;
  205. $response["error_msg"] = "Required parameters (name, email or password) is missing!";
  206. echo json_encode($response);
  207. }
  208. ?>
Add Comment
Please, Sign In to add comment