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.

132 lines
3.1 KiB

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