A new, object-oriented, better vesion of μForum
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.1 KiB

  1. <?php
  2. require_once('./inc/model.class.php');
  3. class SessionModel extends Model
  4. {
  5. private $userdata = array();
  6. public function __construct(&$db)
  7. {
  8. $this->db = $db;
  9. if (isset($_COOKIE[COOKIE_NAME]))
  10. {
  11. $result = $this->select(SESSIONS_TABLE, '*', 'session_id=\''.$_COOKIE[COOKIE_NAME].'\' AND expiry_time>=NOW()');
  12. if (count($result) > 0)
  13. {
  14. //update session expiry time in database and in cookie
  15. if ($result[0]['IP'] != $_SERVER['REMOTE_ADDR'])
  16. {
  17. $this->deleteSession();
  18. }
  19. else
  20. {
  21. $this->updateSession();
  22. $uid = $result[0]['user_id'];
  23. $result = $this->select(USERS_TABLE, 'user_id, nick, rank', 'user_id=\''.$uid.'\'');
  24. $this->userdata = $result[0];
  25. }
  26. }
  27. else
  28. {
  29. $this->deleteSession();
  30. }
  31. }
  32. }
  33. public function updateSession()
  34. {
  35. /*$this->db->query('UPDATE '.SESSIONS_TABLE.' SET expiry_time=(NOW() + INTERVAL 120 MINUTE) WHERE session_id=\''.$_COOKIE[COOKIE_NAME].'\'');
  36. setcookie(COOKIE_NAME, $_COOKIE[COOKIE_NAME], $_SERVER['REQUEST_TIME']+7200, COOKIE_PATH, COOKIE_DOMAIN, false, true); */
  37. $newid = $this->generateSessionID();
  38. $this->db->query('UPDATE '.SESSIONS_TABLE.' SET expiry_time=(NOW() + INTERVAL 120 MINUTE), session_id=\''.$newid.'\' WHERE session_id=\''.$_COOKIE[COOKIE_NAME].'\'');
  39. setcookie(COOKIE_NAME, $newid, $_SERVER['REQUEST_TIME']+7200, COOKIE_PATH, COOKIE_DOMAIN, false, true);
  40. $_COOKIE[COOKIE_NAME] = $newid;
  41. }
  42. public function deleteSession()
  43. {
  44. setcookie(COOKIE_NAME, '', $_SERVER['REQUEST_TIME']-3600, COOKIE_PATH, COOKIE_DOMAIN, false, true);
  45. $this->db->query('DELETE FROM '.SESSIONS_TABLE.' WHERE session_id=\''.$_COOKIE[COOKIE_NAME].'\'');
  46. }
  47. private function generateSessionID()
  48. {
  49. $out = $_SERVER['HTTP_USER_AGENT'].$_SERVER['REQUEST_TIME_FLOAT'].$_SERVER['REMOTE_ADDR'].rand(1, 50000);
  50. return md5($out);
  51. }
  52. public function registerNewSession($user_id)
  53. {
  54. $newID = $this->generateSessionID();
  55. $query = 'UPDATE '.USERS_INFO_TABLE.'
  56. SET lastvisit=NOW()
  57. WHERE user_id=\''.$user_id.'\'';
  58. $this->db->query($query);
  59. $query =
  60. 'INSERT INTO
  61. '.SESSIONS_TABLE.'
  62. (session_id, user_id, IP, expiry_time)
  63. VALUES
  64. (\''.$newID.'\', \''.$user_id.'\', \''.$_SERVER['REMOTE_ADDR'].'\', (NOW() + INTERVAL 120 MINUTE) );';
  65. $this->db->query($query);
  66. setcookie(COOKIE_NAME, $newID, $_SERVER['REQUEST_TIME']+7200, COOKIE_PATH, COOKIE_DOMAIN, false, true);
  67. }
  68. public function tryGetUser($nick, $passwd)
  69. {
  70. $result = $this->select(USERS_TABLE, 'user_id, nick', 'nick=\''.$nick.'\' AND password=\''.$passwd.'\'', '', 1);
  71. if (count($result) > 0 )
  72. return $result[0];
  73. else
  74. return array();
  75. }
  76. public function isLogged()
  77. {
  78. if (count($this->userdata) > 0)
  79. return true;
  80. else
  81. return false;
  82. }
  83. public function getNick()
  84. {
  85. if ($this->isLogged())
  86. return $this->userdata['nick'];
  87. else
  88. return null;
  89. }
  90. public function getRank()
  91. {
  92. if ($this->isLogged())
  93. return $this->userdata['rank'];
  94. else
  95. return null;
  96. }
  97. public function getID()
  98. {
  99. if ($this->isLogged())
  100. return $this->userdata['user_id'];
  101. else
  102. return null;
  103. }
  104. }
  105. ?>