|
|
- <?php
- /**
- * @package uForum2
- * @file inc/models/SessionModel.php
- * @copyright 2007-2015 (c) PioDer
- * @link http://www.pioder.pl/
- * @license see LICENSE.txt
- **/
-
- class SessionModel extends Model
- {
- private $userdata = array();
-
- public function __construct(&$db)
- {
- $this->db = $db;
-
- if (isset($_COOKIE[COOKIE_NAME]))
- {
- $result = $this->select(SESSIONS_TABLE, '*', 'session_id=\''.$_COOKIE[COOKIE_NAME].'\' AND expiry_time>=NOW()');
- if (count($result) > 0)
- {
- //update session expiry time in database and in cookie
- if ($result[0]['IP'] != $_SERVER['REMOTE_ADDR'])
- {
- $this->deleteSession();
- }
- else
- {
- $this->updateSession();
- $uid = $result[0]['user_id'];
-
- $result = $this->select(USERS_TABLE, 'user_id, nick, rank', 'user_id=\''.$uid.'\'');
- $this->userdata = $result[0];
- }
- }
- else
- {
- $this->deleteSession();
- }
- }
- }
-
- public function updateSession()
- {
- $newID = $this->generateSessionID();
- $this->db->query('UPDATE '.SESSIONS_TABLE.' SET expiry_time=(NOW() + INTERVAL 120 MINUTE), session_id=\''.$newID.'\' WHERE session_id=\''.$_COOKIE[COOKIE_NAME].'\'');
- $this->registerSessionCookie($newID, $_SERVER['REQUEST_TIME']+7200);
- }
-
- public function deleteSession()
- {
- $this->db->query('DELETE FROM '.SESSIONS_TABLE.' WHERE session_id=\''.$_COOKIE[COOKIE_NAME].'\'');
- $this->registerSessionCookie('', $_SERVER['REQUEST_TIME']-3600);
- }
-
- private function generateSessionID()
- {
- $out = $_SERVER['HTTP_USER_AGENT'].$_SERVER['REQUEST_TIME_FLOAT'].$_SERVER['REMOTE_ADDR'].rand(1, 50000);
- return md5($out);
- }
-
- public function registerNewSession($user_id)
- {
- $newID = $this->generateSessionID();
- $query = 'UPDATE '.USERS_INFO_TABLE.'
- SET lastvisit=NOW()
- WHERE user_id=\''.$user_id.'\'';
-
- $this->db->query($query);
-
- $query =
- 'INSERT INTO
- '.SESSIONS_TABLE.'
- (session_id, user_id, IP, expiry_time)
- VALUES
- (\''.$newID.'\', \''.$user_id.'\', \''.$_SERVER['REMOTE_ADDR'].'\', (NOW() + INTERVAL 120 MINUTE) );';
-
- $this->db->query($query);
-
- $this->registerSessionCookie($newID, $_SERVER['REQUEST_TIME']+7200);
- }
-
- public function tryGetUser($nick, $passwd)
- {
- $result = $this->select(USERS_TABLE, 'user_id, nick', 'nick=\''.$nick.'\' AND password=\''.$passwd.'\'', '', 1);
-
- if (count($result) > 0 )
- return $result[0];
- else
- return array();
- }
-
- public function isLogged()
- {
- if (count($this->userdata) > 0)
- return true;
- else
- return false;
- }
-
- public function getNick()
- {
- if ($this->isLogged())
- return $this->userdata['nick'];
- else
- return null;
- }
-
- public function getRank()
- {
- if ($this->isLogged())
- return $this->userdata['rank'];
- else
- return null;
- }
-
- public function getID()
- {
- if ($this->isLogged())
- return $this->userdata['user_id'];
- else
- return null;
- }
-
- private function registerSessionCookie($session_id, $expiry_time)
- {
- $domain = (FORUM_DOMAIN == 'localhost') ? '' : FORUM_DOMAIN;
- setcookie(COOKIE_NAME, $session_id, $expiry_time, FORUM_PATH, $domain, false, true);
- $_COOKIE[COOKIE_NAME] = $session_id;
- }
- }
- ?>
|