/models/auth.php

https://bitbucket.org/bfontecc/platonicaether · PHP · 204 lines · 86 code · 11 blank · 107 comment · 6 complexity · 10bcf6ecceff5258069cbd4866309f69 MD5 · raw file

  1. <?php
  2. /**
  3. * This code was written for csci e-75 with Professor Malan and TA Chris Gerber
  4. * It is used here as-is because I really can't think of any other way to implement this, besides
  5. * using similar, publicly available code such as 'idiorm'.
  6. *
  7. * db_model.php
  8. * @author Bret Fontecchio
  9. *
  10. * The mysql database containing user login credentials will be maintained and accessed here.
  11. *
  12. * salting and hashing is done during query preparation
  13. *
  14. * written in 2012, accessed from bitbucket
  15. * https://bitbucket.org/bfontecc/project1/src/2b818b8a6133/models/db_model.php?at=master
  16. *
  17. */
  18. //database variables - based on Chris Gerber's source
  19. define('DB_HOST', 'localhost');
  20. define('DB_USER', 'plato');
  21. define('DB_PASSWORD', 'FCzAJes668ARcZ4X');
  22. define('DB_DATABASE', 'platonicaether');
  23. define('SALT', '$$$');
  24. /**
  25. * get_handle()
  26. * connect to mysql server, select database, and return handle
  27. *
  28. * @return PDO Object $dbh
  29. */
  30. function get_handle() {
  31. $dbh = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_DATABASE, DB_USER, DB_PASSWORD);
  32. return $dbh;
  33. }
  34. /**
  35. * salt_and_hash()
  36. * adds salt constant defined above to a string
  37. * uses SHA1 to hash the salted string
  38. *
  39. * @param string $pass
  40. * @return string $password_hash
  41. */
  42. function salt_and_hash($pass) {
  43. $pass .= SALT;
  44. return hash("SHA1", $pass);
  45. }
  46. /**
  47. * get_check_query_combo()
  48. * Prepares a PDO SQL statement for checking whether an email and password combo
  49. * is in the users table and selects user_id for that row.
  50. * Takes password, NOT password hash.
  51. *
  52. * @param PDO Object $dbh
  53. * @param string $email
  54. * @param string $pass
  55. *
  56. * @return PDOStatement Object $check_query
  57. */
  58. function get_check_query_combo($dbh, $email, $pass) {
  59. $email = strtolower($email);
  60. $password_hash = salt_and_hash($pass);
  61. //prepare and bind query statement
  62. $check_query = $dbh->prepare("
  63. SELECT user_id FROM users
  64. WHERE LOWER(email) = :email_bind
  65. AND password_hash = :hash_bind
  66. ");
  67. $check_query->bindValue(':email_bind', $email, PDO::PARAM_STR);
  68. $check_query->bindValue(':hash_bind', $password_hash, PDO::PARAM_STR);
  69. return $check_query;
  70. }
  71. /**
  72. * get_check_query_email()
  73. * Prepares a PDO SQL statement for checking whether an email
  74. * is in the users table and selects user_id for that row.
  75. *
  76. * @param PDO Object $dbh
  77. * @param string $email
  78. *
  79. * @return PDOStatement Object $check_query
  80. */
  81. function get_check_query_email($dbh, $email) {
  82. $email = strtolower($email);
  83. //prepare and bind query statement
  84. $check_query = $dbh->prepare("
  85. SELECT user_id FROM users
  86. WHERE LOWER(email) = :email_bind
  87. ");
  88. $check_query->bindValue(':email_bind', $email, PDO::PARAM_STR);
  89. return $check_query;
  90. }
  91. /**
  92. * get_insert_user_query()
  93. * Prepares a PDO SQL statement for inserting a user email and pass into users
  94. * Takes password, NOT password hash.
  95. *
  96. * @param PDO Object $dbh
  97. * @param string $email
  98. * @param string $pass
  99. *
  100. * @return PDOStatement Object $check_query
  101. */
  102. function get_insert_user_query($dbh, $email, $pass) {
  103. $email = strtolower($email);
  104. $password_hash = salt_and_hash($pass);
  105. //prepare and bind query statement
  106. $insert_query = $dbh->prepare("
  107. INSERT INTO users (email, password_hash, cash)
  108. VALUES (:email_bind, :hash_bind, 10000);
  109. ");
  110. $insert_query->bindValue(':email_bind', $email, PDO::PARAM_STR);
  111. $insert_query->bindValue(':hash_bind', $password_hash, PDO::PARAM_STR);
  112. return $insert_query;
  113. }
  114. /**
  115. * check_user_id()
  116. * Determines whether there is one and only one user_id in an array returned by
  117. * PDOStatement::fetchAll
  118. * If so, returns it. If not, returns false.
  119. *
  120. * @param array $query_results
  121. * @return [false | numeric user_id]
  122. */
  123. function check_user_id($query_results) {
  124. if (
  125. !isset($query_results[0])
  126. || !isset($query_results[0]['user_id'])
  127. || isset($query_results[1]['user_id'])
  128. ) {
  129. return false;
  130. } else {
  131. return $query_results[0]['user_id'];
  132. }
  133. }
  134. /**
  135. * register_user()
  136. * Create new row in table users.
  137. *
  138. * returns false on failure
  139. *
  140. * @param string $email
  141. * @param string $pass
  142. *
  143. * @return array $status
  144. * array keys:
  145. * 'register_error' => string
  146. * 'register_status' => bool
  147. */
  148. function register_user($email, $pass) {
  149. $dbh = get_handle();
  150. //prepare PDO statements
  151. $check_query = get_check_query_email($dbh, $email);
  152. $insert_query = get_insert_user_query($dbh, $email, $pass);
  153. $dbh->beginTransaction();
  154. $check_query->execute();
  155. $check_result = $check_query->fetchAll();
  156. if (check_user_id($check_result)):
  157. $dbh->rollback();
  158. return array('register_status' => false, 'register_error' => 'That email address is taken.');
  159. else:
  160. if($insert_query->execute()):
  161. $dbh->commit();
  162. return array('register_status' => true, 'register_error' => 'Success. No error.');
  163. else:
  164. $dbh->rollback();
  165. return array('register_status' => false, 'register_error' => $insert_query->errorCode());
  166. endif;
  167. endif;
  168. }
  169. /**
  170. * user_login()
  171. * Check user credentials against database.
  172. * Return numeric user_id from users table
  173. *
  174. * Returns false on any failure to log in.
  175. *
  176. * @param string $email
  177. * @param string $pass
  178. *
  179. * @return int $user_id
  180. */
  181. function user_login($email, $pass) {
  182. //get database handle PDO Object
  183. $dbh = get_handle();
  184. //prepare and bind query statement
  185. $check_query = get_check_query_combo($dbh, $email, $pass);
  186. //execute statement and fetch results
  187. $check_query->execute();
  188. $query_results = $check_query->fetchAll();
  189. //check and return user_id
  190. $user_id = check_user_id($query_results);
  191. return $user_id;
  192. }
  193. ?>