PageRenderTime 80ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/database_utils.php

https://gitlab.com/j0nbiz/autocomplete
PHP | 304 lines | 231 code | 68 blank | 5 comment | 3 complexity | ba2b08b6fe15beea798ad10197806552 MD5 | raw file
  1. <?php
  2. function getPDO(){
  3. $db_uname = 'CS1437203';
  4. $db_pword = 'rfershib';
  5. $db_src = 'mysql:host=korra.dawsoncollege.qc.ca;dbname=CS1437203';
  6. return new PDO($db_src, $db_uname, $db_pword);
  7. }
  8. function setup_database() {
  9. try {
  10. $pdo = getPDO();
  11. $sql_create_table_cities = 'CREATE TABLE cities (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, weight INT, city VARCHAR(255))';
  12. $sql_create_table_users = "CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, uname VARCHAR(255), pword VARCHAR(255), attempts INT DEFAULT 0)";
  13. $sql_create_table_histories = 'CREATE TABLE histories (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, uname VARCHAR(255), term VARCHAR(255), date TIMESTAMP DEFAULT CURRENT_TIMESTAMP)';
  14. $pdo->exec($sql_create_table_cities);
  15. $pdo->exec($sql_create_table_users);
  16. $pdo->exec($sql_create_table_histories);
  17. } catch (PDOException $e) {
  18. echo $e->getMessage();
  19. } finally {
  20. unset($pdo);
  21. }
  22. }
  23. function populate_tables(){
  24. try {
  25. $pdo = getPDO();
  26. $stmt = $pdo->prepare('INSERT INTO cities(weight, city) VALUES(?, ?)');
  27. echo 'Reading from file and populating, please wait...';
  28. $cities = fopen("res/cities.txt", "r");
  29. while(!feof($cities)) {
  30. $city = explode(';', fgets($cities));
  31. $stmt->bindValue(1, trim(preg_replace('/\s+/', ' ', $city[0])));
  32. $stmt->bindValue(2, trim(preg_replace('/\s+/', ' ', $city[1])));
  33. $stmt->execute();
  34. }
  35. fclose($cities);
  36. echo 'Populated!';
  37. } catch (PDOException $e) {
  38. echo $e->getMessage();
  39. } finally {
  40. unset($pdo);
  41. }
  42. }
  43. function get_cities_matching($pattern){
  44. try {
  45. // Get matching terms if any
  46. $cities = get_terms_matching($pattern);
  47. $pdo = getPDO();
  48. $stmt = $pdo->prepare('SELECT city FROM cities WHERE city LIKE ? ORDER BY weight LIMIT ?');
  49. $stmt->bindValue(1, $pattern."%");
  50. $stmt->bindValue(2, 5 - count($cities), PDO::PARAM_INT); // Limit ammount of returned cities if terms are found
  51. $stmt->execute();
  52. while($city = $stmt->fetch()[0]){
  53. $cities[] = $city;
  54. }
  55. return $cities;
  56. } catch (PDOException $e) {
  57. echo $e->getMessage();
  58. } finally {
  59. unset($pdo);
  60. }
  61. }
  62. function get_terms_matching($pattern){
  63. try {
  64. $pdo = getPDO();
  65. $stmt = $pdo->prepare('SELECT term FROM histories WHERE uname = ? AND term LIKE ? ORDER BY date DESC');
  66. $stmt->bindValue(1, $_SESSION['uname']); // Use session var since user should only be able to get here if logged in
  67. $stmt->bindValue(2, $pattern."%");
  68. $stmt->execute();
  69. $terms = [];
  70. while($term = $stmt->fetch()[0]){
  71. $terms[] = $term;
  72. }
  73. return $terms;
  74. } catch (PDOException $e) {
  75. echo $e->getMessage();
  76. } finally {
  77. unset($pdo);
  78. }
  79. }
  80. function create_user($uname, $pword){
  81. try {
  82. $pdo = getPDO();
  83. $stmt = $pdo->prepare('INSERT INTO users(uname, pword) VALUES(?, ?)');
  84. $stmt->bindValue(1, strip_tags(trim(substr($uname, 0,255))));
  85. $stmt->bindValue(2, password_hash($pword, PASSWORD_DEFAULT));
  86. $stmt->execute();
  87. } catch (PDOException $e) {
  88. echo $e->getMessage();
  89. } finally {
  90. unset($pdo);
  91. }
  92. }
  93. function is_unique($uname){
  94. try {
  95. $pdo = getPDO();
  96. $stmt = $pdo->prepare('SELECT uname FROM users WHERE uname = ?');
  97. $stmt->bindValue(1, trim(substr($uname, 0,255)));
  98. $stmt->execute();
  99. return empty($stmt->fetch()); // Return true if nothing returned
  100. } catch (PDOException $e) {
  101. echo $e->getMessage();
  102. } finally {
  103. unset($pdo);
  104. }
  105. }
  106. function increment_attempts($uname){
  107. try {
  108. $pdo = getPDO();
  109. $stmt = $pdo->prepare('SELECT attempts FROM users WHERE uname = ?');
  110. $stmt->bindValue(1, $uname);
  111. $stmt->execute();
  112. $attempt = (int)$stmt->fetch()[0] + 1;
  113. $stmt = $pdo->prepare('UPDATE users SET attempts = ? WHERE uname = ?');
  114. $stmt->bindValue(1, $attempt);
  115. $stmt->bindValue(2, $uname);
  116. $stmt->execute();
  117. } catch (PDOException $e) {
  118. echo $e->getMessage();
  119. } finally {
  120. unset($pdo);
  121. }
  122. }
  123. function verify($uname, $pword){
  124. try {
  125. $pdo = getPDO();
  126. $stmt = $pdo->prepare('SELECT uname, pword FROM users WHERE uname = ?');
  127. $stmt->bindValue(1, $uname);
  128. $stmt->execute();
  129. if(!password_verify($pword, $stmt->fetch()[1])){
  130. increment_attempts($uname);
  131. return false; // Increment the attempts of specific user
  132. }
  133. // Need to reset login attempts to 0 if successful
  134. $stmt = $pdo->prepare('UPDATE users SET attempts = ? WHERE uname = ?');
  135. $stmt->bindValue(1, 0);
  136. $stmt->bindValue(2, $uname);
  137. $stmt->execute();
  138. return true;
  139. } catch (PDOException $e) {
  140. echo $e->getMessage();
  141. } finally {
  142. unset($pdo);
  143. }
  144. }
  145. function is_locked_out($uname){
  146. try {
  147. $pdo = getPDO();
  148. $stmt = $pdo->prepare('SELECT attempts FROM users WHERE uname = ?');
  149. $stmt->bindValue(1, $uname);
  150. $stmt->execute();
  151. return ($stmt->fetch()[0] >= 5);
  152. } catch (PDOException $e) {
  153. echo $e->getMessage();
  154. } finally {
  155. unset($pdo);
  156. }
  157. }
  158. function get_history(){
  159. try {
  160. $pdo = getPDO();
  161. $stmt = $pdo->prepare('SELECT term, date FROM histories WHERE uname = ? ORDER BY date DESC');
  162. $stmt->bindValue(1, $_SESSION['uname']); // Use session var since user should only be able to get here if logged in
  163. $stmt->execute();
  164. $terms = [];
  165. while($term = $stmt->fetch()){
  166. $terms[] = array($term[0], $term[1]);
  167. }
  168. // Cap array if not completely full
  169. for($i = count($terms); $i < 5; $i++){
  170. $terms[] = array("", "");
  171. }
  172. return $terms;
  173. } catch (PDOException $e) {
  174. echo $e->getMessage();
  175. } finally {
  176. unset($pdo);
  177. }
  178. }
  179. function update_history($term){
  180. try {
  181. // Remove oldest first
  182. remove_oldest_term();
  183. $pdo = getPDO();
  184. $stmt = $pdo->prepare('INSERT INTO histories(uname, term) VALUES(?, ?)');
  185. $stmt->bindValue(1, $_SESSION['uname']);
  186. $stmt->bindValue(2, strip_tags($term));
  187. $stmt->execute();
  188. } catch (PDOException $e) {
  189. echo $e->getMessage();
  190. } finally {
  191. unset($pdo);
  192. }
  193. }
  194. function is_in_history($term){
  195. try {
  196. $db_terms = get_history();
  197. // Check in db for match
  198. foreach ($db_terms as $db_term) {
  199. if($db_term[0] === $term){
  200. return true;
  201. }
  202. }
  203. return false;
  204. } catch (PDOException $e) {
  205. echo $e->getMessage();
  206. } finally {
  207. unset($pdo);
  208. }
  209. }
  210. function update_term_date($term){
  211. try {
  212. $pdo = getPDO();
  213. $stmt = $pdo->prepare('UPDATE histories SET date = ? WHERE uname = ? AND term = ?');
  214. $stmt->bindValue(1, date('Y-m-d G:i:s')); // Set new date
  215. $stmt->bindValue(2, $_SESSION['uname']);
  216. $stmt->bindValue(3, $term);
  217. $stmt->execute();
  218. } catch (PDOException $e) {
  219. echo $e->getMessage();
  220. } finally {
  221. unset($pdo);
  222. }
  223. }
  224. function remove_oldest_term(){
  225. try {
  226. $pdo = getPDO();
  227. $stmt = $pdo->prepare('DELETE FROM histories WHERE uname = ? AND date = ?');
  228. $stmt->bindValue(1, $_SESSION['uname']); // Use session var since user should only be able to get here if logged in
  229. $stmt->bindValue(2, get_history()[4][1]); // Get oldest entry date
  230. $stmt->execute();
  231. } catch (PDOException $e) {
  232. echo $e->getMessage();
  233. } finally {
  234. unset($pdo);
  235. }
  236. }