PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/login_func.php

https://gitlab.com/ColoradoSchoolOfMines/MinesPlaza-Defunct-
PHP | 172 lines | 158 code | 8 blank | 6 comment | 41 complexity | 94d6ae190e37cf694f7390b773c5e40b MD5 | raw file
  1. <?php
  2. require ('../includes/common_func.php');
  3. require_once('../includes/variables.php');
  4. //Create a session
  5. function createSession($userid){
  6. global $mysqli;
  7. $time = time() + 3600;
  8. setcookie('SessionUser', $userid, $time);
  9. setcookie('SessionID', random(32), $time);
  10. $sid = $_COOKIE['SessionID'];
  11. $result = $mysqli->query('DELETE FROM user_session WHERE user_id='.$userid);
  12. $query = 'INSERT INTO user_session (user_id, session_id, expire_time)
  13. VALUES ( '.$userid.', "'.$sid.'", '.$time.')';
  14. if ($stmt = $mysqli->prepare($query)){
  15. $stmt->execute();
  16. defineUser($userid);
  17. }
  18. else{
  19. logout($userid);
  20. }
  21. }
  22. //Check if a user is logged in, 0 means check cookies,
  23. //-1 means check cookies but don't logout if not logged in
  24. function loggedIn($userids = 0){
  25. global $mysqli;
  26. if($userids <= 0) $userid = $_COOKIE['SessionUser'];
  27. if($userid == 0 && $userids != -1) logout($userid);
  28. else if($userid == 0) return false;
  29. $query = "SELECT * FROM user_session WHERE user_id= ?";
  30. if($stmt = $mysqli->prepare($query)){
  31. $stmt->bind_param('i', $userid);
  32. $stmt->execute();
  33. $result = $stmt->get_result();
  34. $row = $result->fetch_assoc();
  35. if(strcmp($_COOKIE['SessionID'], $row['session_id'])){
  36. if(time() < $row['expire_time']){
  37. $time = time() + 3600;
  38. $query = "UPDATE user_session SET expire_time = ? WHERE user_id = ?";
  39. if($stmt = $mysqli->prepare($query)){
  40. $stmt->bind_param('ii', $time, $userid);
  41. $stmt->execute();
  42. }
  43. else if($userids != -1) logout($userid);
  44. else return false;
  45. }
  46. else if($userids != -1) logout($userid);
  47. else return false;
  48. }
  49. else if($userids != -1) logout($userid);
  50. else return false;
  51. }
  52. else if($userids != -1) logout($userid);
  53. else return false;
  54. return true;
  55. }
  56. //Logout given user or logout user based on cookies
  57. function logout($userid = 0){
  58. global $mysqli;
  59. if($userid == 0) $userid = $_COOKIE['SessionUser'];
  60. $result = $mysqli->query('DELETE FROM user_session WHERE user_id='.$userid);
  61. setcookie('SessionUser', 0, time() + 3600 * 3600);
  62. setcookie('SessionId', "", time() + 3600 * 3600);
  63. defineUser(-1);
  64. header('Location: login.php?msg=You have been logged out');
  65. exit();
  66. }
  67. //Validates password for registering
  68. function validatePassword($pass, $pass_re){
  69. if($pass != $pass_re) return "Passwords don't match||";
  70. if(strlen($pass) < 6) return "Password must be atleast 6 characters||";
  71. return '';
  72. }
  73. function validateEmail($email, $new=true){
  74. global $mysqli;
  75. if(strlen($email) > 32) return "Email is too long||";
  76. if(!preg_match('/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $email)) return "Must be a valid email||";
  77. if(($stmt = $mysqli->prepare('SELECT * FROM users WHERE email = ?')) && $new){
  78. $stmt->bind_param('s', $email);
  79. $stmt->execute();
  80. $result = $stmt->get_result();
  81. $row = $result->fetch_assoc();
  82. if (count($row)) return "Email already exists||";
  83. }
  84. else return "Cannot connect to DB||";
  85. return '';
  86. }
  87. function validateUsername($user, $new=true){
  88. global $mysqli;
  89. if(strlen($user) < 3) return "Username must be at least three characters||";
  90. if(strlen($user) > 30) return "Username is too long||";
  91. if(!preg_match('/^[\pL0-9]+$/', $user)) return "Username must be alpha numeric||";
  92. if(($stmt = $mysqli->prepare('SELECT * FROM users WHERE username = ?')) && $new){
  93. $stmt->bind_param('s', $user);
  94. $stmt->execute();
  95. $result = $stmt->get_result();
  96. $row = $result->fetch_assoc();
  97. if (count($row)) return "Username already exists||";
  98. }
  99. else return "Cannot connect to DB||";
  100. return '';
  101. }
  102. function validatePhone($phone){
  103. if(strlen($phone) != 10) return "Phone Number is not right length||";
  104. else if(!preg_match('/^[0-9]+/', $phone)) return "Phone Numbers should only contain 0-9||";
  105. else return '';
  106. }
  107. function validateName($name){
  108. if(strlen($name) <= 0) return "Name is too short||";
  109. else if(strlen($name) > 63) return "Name is too long||";
  110. else if(!preg_match('/^[\pL0-9]+$/', $name)) return "Name must be alpha numeric||";
  111. else return '';
  112. }
  113. function validateCaptcha($cap){
  114. $ch = curl_init('https://www.google.com/recaptcha/api/siteverify?secret=6LfEZgITAAAAAB0LQG4S46ghPpLi5dThqB5ZOX5Y&response='.$cap.'&remoteip='.$_SERVER['REMOTE_ADDR']);
  115. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  116. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  117. if(!$data = curl_exec($ch)){
  118. $response = json_decode($data,true);
  119. if (!$response['success']) return 'Recaptcha error';
  120. }
  121. curl_close($ch);
  122. return '';
  123. }
  124. //Pass userid for given uid, 0 for destroy, -1 for cookies
  125. function defineUser($uid = -1){
  126. global $mysqli;
  127. if($uid || $uid == -1){
  128. if($uid == -1){
  129. if(!isset($_COOKIE['SessionUser'])) return;
  130. $uid = $_COOKIE['SessionUser'];
  131. }
  132. $stmt = $mysqli->prepare('SELECT * FROM user_detail, users WHERE users.id = user_detail.user_id AND users.id = ?');
  133. $stmt->bind_param('i', $uid);
  134. $stmt->execute();
  135. $result = $stmt->get_result();
  136. $row = $result->fetch_assoc();
  137. $GLOBALS['USERID'] = $uid;
  138. $GLOBALS['USERNAME'] = $row['username'];
  139. $GLOBALS['EMAIL'] = $row['email'];
  140. $GLOBALS['FLAGGED'] = $row['flagged'];
  141. $GLOBALS['FIRSTNAME'] = $row['first_name'];
  142. $GLOBALS['LASTNAME'] = $row['last_name'];
  143. $GLOBALS['PHONE'] = $row['phone'];
  144. $GLOBALS['DISABLED'] = $row['disabled'];
  145. $GLOBALS['MOD'] = $row['moderator'];
  146. $stmt->close();
  147. }
  148. else{
  149. $GLOBALS['USERID'] = 0;
  150. $GLOBALS['USERNAME'] = "";
  151. $GLOBALS['EMAIL'] = "";
  152. $GLOBALS['FIRSTNAME'] = "";
  153. $GLOBALS['LASTNAME'] = "";
  154. $GLOBALS['PHONE'] = 0;
  155. $GLOBALS['DISABLED'] = false;
  156. $GLOBALS['MOD'] = false;
  157. }
  158. }
  159. ?>