Advertisement
Guest User

Untitled

a guest
Jun 12th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. <?php
  2.  
  3. class loginService {
  4.  
  5. public static function e {
  6. $session_name = 'sec_session_id'; // Set a custom session name
  7. $secure = SECURE;
  8. // This stops JavaScript being able to access the session id.
  9. $httponly = true;
  10. // Forces sessions to only use cookies.
  11. if (ini_set('session.use_only_cookies', 1) === FALSE) {
  12. header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
  13. exit();
  14. }
  15. // Gets current cookies params.
  16. $cookieParams = session_get_cookie_params();
  17. session_set_cookie_params($cookieParams["lifetime"],
  18. $cookieParams["path"],
  19. $cookieParams["domain"],
  20. $secure,
  21. $httponly);
  22. // Sets the session name to the one set above.
  23. session_name($session_name);
  24. session_start(); // Start the PHP session
  25. session_regenerate_id(); // regenerated the session, delete the old one.
  26. }
  27.  
  28. public static function login($email, $password, $mysqli) {
  29. // Using prepared statements means that SQL injection is not possible.
  30. if ($stmt = $mysqli->prepare("SELECT id, username, password, salt
  31. FROM users
  32. WHERE email = ?
  33. LIMIT 1")) {
  34. $stmt->bind_param('s', $email); // Bind "$email" to parameter.
  35. $stmt->execute(); // Execute the prepared query.
  36. $stmt->store_result();
  37.  
  38. // get variables from result.
  39. $stmt->bind_result($user_id, $username, $db_password, $salt);
  40. $stmt->fetch();
  41.  
  42. // hash the password with the unique salt.
  43. $password = hash('sha512', $password . $salt);
  44. if ($stmt->num_rows == 1) {
  45. // If the user exists we check if the account is locked
  46. // from too many login attempts
  47.  
  48. if (loginService::checkbrute($user_id, $mysqli) == true) {
  49. // Account is locked
  50. // Send an email to user saying their account is locked
  51. return false;
  52. } else {
  53. // Check if the password in the database matches
  54. // the password the user submitted.
  55. if ($db_password == $password) {
  56. // Password is correct!
  57. // Get the user-agent string of the user.
  58. $user_browser = $_SERVER['HTTP_USER_AGENT'];
  59. // XSS protection as we might print this value
  60. $user_id = preg_replace("/[^0-9]+/", "", $user_id);
  61. $_SESSION['user_id'] = $user_id;
  62. // XSS protection as we might print this value
  63. $username = preg_replace("/[^a-zA-Z0-9_\-]+/",
  64. "",
  65. $username);
  66. $_SESSION['username'] = $username;
  67. $_SESSION['login_string'] = hash('sha512',
  68. $password . $user_browser);
  69. // Login successful.
  70. return true;
  71. } else {
  72. // Password is not correct
  73. // We record this attempt in the database
  74. $now = time();
  75. $mysqli->query("INSERT INTO login_attempts(user_id, time)
  76. VALUES ('$user_id', '$now')");
  77. return false;
  78. }
  79. }
  80. } else {
  81. // No user exists.
  82. return false;
  83. }
  84. }
  85. }
  86.  
  87. public static function checkbrute($user_id, $mysqli) {
  88. // Get timestamp of current time
  89. $now = time();
  90.  
  91. // All login attempts are counted from the past 2 hours.
  92. $valid_attempts = $now - (2 * 60 * 60);
  93.  
  94. if ($stmt = $mysqli->prepare("SELECT time
  95. FROM login_attempts <code><pre>
  96. WHERE user_id = ?
  97. AND time > '$valid_attempts'")) {
  98. $stmt->bind_param('i', $user_id);
  99.  
  100. // Execute the prepared query.
  101. $stmt->execute();
  102. $stmt->store_result();
  103.  
  104. // If there have been more than 5 failed logins
  105. if ($stmt->num_rows > 5) {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. }
  112.  
  113. public static function login_check($mysqli) {
  114. // Check if all session variables are set
  115. if (isset($_SESSION['user_id'],
  116. $_SESSION['username'],
  117. $_SESSION['login_string'])) {
  118.  
  119. $user_id = $_SESSION['user_id'];
  120. $login_string = $_SESSION['login_string'];
  121. $username = $_SESSION['username'];
  122.  
  123. // Get the user-agent string of the user.
  124. $user_browser = $_SERVER['HTTP_USER_AGENT'];
  125.  
  126. if ($stmt = $mysqli->prepare("SELECT password
  127. FROM users
  128. WHERE id = ? LIMIT 1")) {
  129. // Bind "$user_id" to parameter.
  130. $stmt->bind_param('i', $user_id);
  131. $stmt->execute(); // Execute the prepared query.
  132. $stmt->store_result();
  133.  
  134. if ($stmt->num_rows == 1) {
  135. // If the user exists get variables from result.
  136. $stmt->bind_result($password);
  137. $stmt->fetch();
  138. $login_check = hash('sha512', $password . $user_browser);
  139.  
  140. if ($login_check == $login_string) {
  141. // Logged In!!!!
  142. return true;
  143. } else {
  144. // Not logged in
  145. return false;
  146. }
  147. } else {
  148. // Not logged in
  149. return false;
  150. }
  151. } else {
  152. // Not logged in
  153. return false;
  154. }
  155. } else {
  156. // Not logged in
  157. return false;
  158. }
  159. }
  160.  
  161. public static function check_wedding_setup($mysqli) {
  162. if (isset($_SESSION['user_id'],
  163. $_SESSION['username'],
  164. $_SESSION['login_string'])) {
  165.  
  166. $user_id = $_SESSION['user_id'];
  167. if ($stmt = $mysqli->prepare("SELECT weddingId FROM users WHERE id = ? LIMIT 1")) {
  168. $stmt->bind_result($weddingId);
  169. $stmt->fetch();
  170. if ($weddingId != null) {
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. } else {
  176. return false;
  177. }
  178. }
  179.  
  180. return false;
  181. }
  182.  
  183.  
  184. public static function esc_url($url) {
  185. if ('' == $url) {
  186. return $url;
  187. }
  188.  
  189. $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
  190.  
  191. $strip = array('%0d', '%0a', '%0D', '%0A');
  192. $url = (string) $url;
  193.  
  194. $count = 1;
  195. while ($count) {
  196. $url = str_replace($strip, '', $url, $count);
  197. }
  198.  
  199. $url = str_replace(';//', '://', $url);
  200.  
  201. $url = htmlentities($url);
  202.  
  203. $url = str_replace('&amp;', '&#038;', $url);
  204. $url = str_replace("'", '&#039;', $url);
  205.  
  206. if ($url[0] !== '/') {
  207. // We're only interested in relative links from $_SERVER['PHP_SELF']
  208. return '';
  209. } else {
  210. return $url;
  211. }
  212. }
  213.  
  214. }
  215.  
  216. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement