PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Auth_staff.php

https://bitbucket.org/krishna2793/aces
PHP | 208 lines | 153 code | 33 blank | 22 comment | 19 complexity | dfbaedc3f81bf81ce614f30aabf4dab8 MD5 | raw file
  1. <?php
  2. class Auth_staff
  3. {
  4. private $siteKey;
  5. public function connect()
  6. {
  7. $con = mysql_connect('localhost','root','mysql');
  8. if (!$con)
  9. {
  10. die('Could not connect: ' . mysql_error());
  11. }
  12. // make userdata the current db
  13. $db_selected = mysql_select_db('aces', $con);
  14. if (!$db_selected) {
  15. die ('Can\'t use userdata : ' . mysql_error());
  16. }
  17. return $con;
  18. }
  19. public function disconnect($con)
  20. {
  21. mysql_close($con);
  22. }
  23. public function __construct()
  24. {
  25. $this->siteKey = 'adsvdsugygabhbbdh2767';
  26. }
  27. public function randomString($length = 50)
  28. {
  29. $characters = '0123456789abcdefghijklmnopqrstaffvwxyz';
  30. $string = '';
  31. for ($p = 0; $p < $length; $p++)
  32. {
  33. $string .= $characters[mt_rand(0, strlen($characters)-1)];
  34. }
  35. return $string;
  36. }
  37. protected function hashData($data)
  38. {
  39. return hash_hmac('sha512', $data, $this->siteKey);
  40. }
  41. public function createUser($reg_id, $password, $address,$designation,$dob,$dept,$name,$con,$gender)
  42. {
  43. //Generate users salt
  44. $user_salt = $this->randomString();
  45. //Salt and Hash the password
  46. $password = $user_salt . $password;
  47. $password = $this->hashData($password);
  48. //Create verification code
  49. $code = $this->randomString();
  50. //Commit values to database here.
  51. $db=$this->connect();
  52. $query = "INSERT into staff(staff_id,pass,user_salt,address,department,designation,name,dob,contact,gender) values('$reg_id','$password','$user_salt','$address','$dept','$designation','$name','$dob','$con','$gender')";
  53. $created = mysql_query($query) or die("cant insert".mysql_error());
  54. $this->disconnect($db);
  55. if($created != false)
  56. {
  57. return true;
  58. }
  59. return false;
  60. }
  61. public function login($reg_id, $password)
  62. {
  63. //Select users row from database base on $reg_id
  64. $db=$this->connect();
  65. $result = mysql_query("SELECT * FROM staff WHERE staff_id = '$reg_id'");
  66. if (!$result)
  67. {
  68. echo 'Could not run query: ' . mysql_error();
  69. exit;
  70. }
  71. $selection = mysql_fetch_array($result);
  72. //Salt and hash password for checking
  73. $password = $selection['user_salt'] . $password;
  74. $password = $this->hashData($password);
  75. //Check reg_id and password hash match database row
  76. $match=((strcmp($selection['pass'],$password))==0);
  77. if($match == true)
  78. {
  79. //reg_id/Password combination exists, set sessions
  80. //First, generate a random string.
  81. $random = $this->randomString();
  82. //Build the token
  83. $token = $_SERVER['HTTP_USER_AGENT'] . $random;
  84. $token = $this->hashData($token);
  85. //Setup sessions vars
  86. session_start();
  87. $_SESSION['token'] = $token;
  88. $_SESSION['user_id'] = $selection['id'];
  89. $_SESSION['role'] = 'staff';
  90. $_SESSION['name'] = $selection['name'];
  91. //Update old logged_in_member records for user
  92. $kp = mysql_query("select * from logged_in_staff where user_id ='$selection[id]'");
  93. if(mysql_num_rows($kp)==1)
  94. {
  95. $sid=session_id();
  96. $update = mysql_query("UPDATE logged_in_staff SET session_id='$sid',token='$token' WHERE user_id = '$_SESSION[user_id]'") ;
  97. if ($update)
  98. {
  99. $this->disconnect($db);
  100. return true;
  101. }
  102. }
  103. else
  104. {
  105. $sid=session_id();
  106. //Insert new logged_in_member record for user
  107. $inserted = mysql_query("INSERT into logged_in_staff(user_id,session_id,token) values('$_SESSION[user_id]','$sid','$token') ");
  108. if ($inserted)
  109. {
  110. $this->disconnect($db);
  111. return true;
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. public function checkSession()
  118. {
  119. //Select the row
  120. $db=$this->connect();
  121. $result = mysql_query("SELECT * FROM logged_in_staff WHERE user_id = '$_SESSION[user_id]'");
  122. if (!$result) {
  123. echo 'Could not run query: ' . mysql_error();
  124. exit;
  125. }
  126. $selection = mysql_fetch_array($result);
  127. if($selection) {
  128. //Check ID and Token
  129. if(session_id() == $selection['session_id'] && $_SESSION['token'] == $selection['token']) {
  130. //Id and token match, refresh the session for the next request
  131. $this->refreshSession();
  132. $this->disconnect($db);
  133. return true;
  134. }
  135. }
  136. $this->disconnect($db);
  137. return false;
  138. }
  139. private function refreshSession()
  140. {
  141. //Regenerate id
  142. session_regenerate_id();
  143. //Regenerate token
  144. $random = $this->randomString();
  145. //Build the token
  146. $token = $_SERVER['HTTP_USER_AGENT'] . $random;
  147. $token = $this->hashData($token);
  148. //Store in session
  149. $_SESSION['token'] = $token;
  150. $sid=session_id();
  151. //update logged_in_member table
  152. $update = mysql_query("UPDATE logged_in_staff SET session_id='$sid',token='$token' WHERE user_id = '$_SESSION[user_id]'") or die('failed');
  153. if ($update)
  154. {
  155. return true;
  156. }
  157. }
  158. public function logout()
  159. {
  160. $db=$this->connect();
  161. $delete = mysql_query("DELETE from logged_in_staff WHERE user_id = '$_SESSION[user_id]'");
  162. $this->disconnect($db);
  163. if ($delete)
  164. {
  165. return true;
  166. }
  167. session_unset();
  168. session_destroy();
  169. }
  170. public function __toString()
  171. {
  172. return $this->siteKey;
  173. }
  174. }
  175. ?>