Advertisement
Guest User

Functions.php

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