auth.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. class auth {
  3. var $username = '';
  4. var $password = '';
  5. function auth () {
  6. if (!session_id ()){
  7. session_start();
  8. }
  9. if ($this->check_auth ()) {
  10. $_SESSION['logged_in'] = true;
  11. }
  12. else {
  13. $_SESSION['logged_in'] = false;
  14. }
  15. }
  16. function check_auth () {
  17. if (session_id ()
  18. && isset ($_SESSION['challengekey'])
  19. && strlen ($_SESSION['challengekey']) === 32
  20. && isset ($_SESSION['username'])
  21. && $_SESSION['username'] != ''
  22. && isset ($_SESSION['logged_in'])
  23. && $_SESSION['logged_in']) {
  24. return true;
  25. }
  26. else if ($this->check_cookie ()) {
  27. return true;
  28. }
  29. return false;
  30. }
  31. function assign_data () {
  32. if ( isset($_POST['username'])
  33. && isset($_POST['password'])
  34. && $_POST['username'] != ''
  35. && $_POST['password'] != '') {
  36. $this->username = $_POST['username'];
  37. $this->password = $_POST['password'];
  38. return true;
  39. }
  40. return false;
  41. }
  42. function login () {
  43. $_SESSION['logged_in'] = false;
  44. if ($this->assign_data ()) {
  45. global $mysql;
  46. $query = sprintf("SELECT COUNT(*) FROM user WHERE md5(username)=md5('%s') AND password=md5('%s')",
  47. $mysql->escape ($this->username),
  48. $mysql->escape ($this->password));
  49. if ($mysql->query ($query) && reset(mysqli_fetch_assoc ($mysql->result)) === "1") {
  50. if (isset ($_POST['remember'])) {
  51. global $cookie;
  52. $cookie['data'] = serialize (array ($this->username, md5 ($cookie['seed'] . md5 ($this->password))));
  53. @setcookie ($cookie['name'],
  54. $cookie['data'],
  55. $cookie['expire'],
  56. $cookie['path'],
  57. $cookie['domain']);
  58. }
  59. $this->set_login_data ($this->username);
  60. }
  61. else {
  62. $this->logout ();
  63. }
  64. }
  65. unset ($_POST['password']);
  66. unset ($this->password);
  67. }
  68. function logout () {
  69. global $cookie;
  70. unset ($_SESSION['challengekey']);
  71. unset ($_SESSION['username']);
  72. @setcookie ($cookie['name'], "", time() - 1, $cookie['path'], $cookie['domain']);
  73. $_SESSION['logged_in'] = false;
  74. }
  75. function set_login_data ($username) {
  76. $_SESSION['challengekey'] = md5 ($username . microtime ());
  77. $_SESSION['username'] = $username;
  78. $_SESSION['logged_in'] = true;
  79. }
  80. function check_cookie () {
  81. global $cookie, $mysql;
  82. if ( isset ($cookie['name'])
  83. && $cookie['name'] != ''
  84. && isset ($_COOKIE[$cookie['name']])) {
  85. list ($cookie['username'], $cookie['password_hash']) = @unserialize ($_COOKIE[$cookie['name']]);
  86. $query = sprintf("SELECT COUNT(*) FROM user WHERE username='%s' AND MD5(CONCAT('%s', password))='%s'",
  87. $mysql->escape ($cookie['username']),
  88. $mysql->escape ($cookie['seed']),
  89. $mysql->escape ($cookie['password_hash']));
  90. if ($mysql->query ($query) && mysqli_fetch_assoc ($mysql->result) === "1") {
  91. $this->set_login_data ($cookie['username']);
  92. return true;
  93. }
  94. else {
  95. $this->logout ();
  96. return false;
  97. }
  98. }
  99. return false;
  100. }
  101. function display_login_form () {
  102. ?>
  103. <form name="loginform" method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
  104. <center>
  105. <table border="0" style="text-align:left;">
  106. <tr>
  107. <td>Username:</td>
  108. <td><input name="username" type="text" value="" tabindex="1"></td>
  109. </tr>
  110. <tr>
  111. <td>Password:</td>
  112. <td><input name="password" type="password" value="" tabindex="2"></td>
  113. </tr>
  114. <tr>
  115. <td>Remember login:</td>
  116. <td><input type="checkbox" name="remember" tabindex="3"></td>
  117. </tr>
  118. <tr>
  119. <td></td>
  120. <td><input type="submit" value="Login" tabindex="4"></td>
  121. </tr>
  122. </table>
  123. <?php
  124. if (strtolower (basename ($_SERVER['SCRIPT_NAME'])) == "index.php") {
  125. echo '<br><div><a href="./shared.php">Users Sharing Bookmarks</a></div>';
  126. }
  127. ?>
  128. </center>
  129. </form>
  130. <script type="text/javascript">
  131. document.loginform.username.focus();
  132. </script>
  133. <?php
  134. }
  135. }
  136. ?>