Add namespace.
This commit is contained in:
72
Session/Session.model.php
Normal file
72
Session/Session.model.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php namespace Majestic;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Session
|
||||
* @since 2010-02-28
|
||||
*/
|
||||
|
||||
class SessionModel extends SqlModel
|
||||
{
|
||||
|
||||
protected $life_time;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->life_time = get_cfg_var('session.gc_maxlifetime');
|
||||
}
|
||||
|
||||
/* Session handler methods */
|
||||
|
||||
public function open($save_path, $sess_name)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function read($id)
|
||||
{
|
||||
$sql = 'SELECT `data` FROM :table WHERE `id`=? AND `expires` > UNIX_TIMESTAMP()';
|
||||
return (string) $this->fetchField($sql, $id, 'data');
|
||||
}
|
||||
|
||||
public function write($id, $data)
|
||||
{
|
||||
preg_match('/user\|.+s:2:"id";s:(\d+):"(\d+)"/', $data, $match);
|
||||
$user_id = empty($match) ? 0 : (int) $match[2];
|
||||
|
||||
$ip = Env::Server('HTTP_X_FORWARDED_FOR', Env::Server('REMOTE_ADDR'));
|
||||
|
||||
$update = array(
|
||||
'expires' => new DbExpr('UNIX_TIMESTAMP() + ' . (int) $this->life_time),
|
||||
'ip' => new DbExpr('INET_ATON(' . $this->quote($ip) . ')'),
|
||||
'user_id' => $user_id,
|
||||
'data' => $data
|
||||
);
|
||||
return (bool) $this->insert(array('id' => $id) + $update, $update);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
return (bool) $this->db->delete($this->table(), array('`id`=?' => (string) $id));
|
||||
}
|
||||
|
||||
public function gc($max_life_time)
|
||||
{
|
||||
return (bool) $this->db->delete($this->table(), '`expires` < UNIX_TIMESTAMP()');
|
||||
}
|
||||
|
||||
/* End of Session handler methods */
|
||||
|
||||
|
||||
public function destroyByUserId($user_id)
|
||||
{
|
||||
return $this->db->delete($this->table(), array('`user_id`=?' => $user_id));
|
||||
}
|
||||
}
|
242
Session/Session.php
Normal file
242
Session/Session.php
Normal file
@ -0,0 +1,242 @@
|
||||
<?php namespace Majestic\Session;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage session
|
||||
* @since 2010-03-14
|
||||
*/
|
||||
|
||||
class Session
|
||||
{
|
||||
/**
|
||||
* Default number of seconds the session will be remembered
|
||||
* for when asked to be remembered
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private static $remember_time = 1209600; // 2 weeks
|
||||
|
||||
private static $started = false;
|
||||
|
||||
/**
|
||||
* Avoid new
|
||||
* @codeCoverageIgnoreStart
|
||||
*/
|
||||
private function __construct(){}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnoreEnd
|
||||
*/
|
||||
|
||||
/**
|
||||
* Avoid cloning
|
||||
* @codeCoverageIgnoreStart
|
||||
*/
|
||||
private function __clone(){}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnoreEnd
|
||||
*/
|
||||
|
||||
/**
|
||||
* Retrieve a member of the $_SESSION
|
||||
*
|
||||
* If no $key is passed, returns the entire $_SESSION array.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default Default value to use if key not found
|
||||
* @return mixed Returns null if key does not exist
|
||||
*/
|
||||
public static function get($key = null, $default = null)
|
||||
{
|
||||
if (!self::$started) {
|
||||
if (self::isExists()) {
|
||||
self::start();
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
if (null === $key) {
|
||||
return $_SESSION;
|
||||
}
|
||||
return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $_SESSION values
|
||||
*
|
||||
* @param string|array $spec
|
||||
* @param null|mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public static function set($spec, $value = null)
|
||||
{
|
||||
if (!self::$started) {
|
||||
self::start();
|
||||
}
|
||||
if (is_array($spec)) {
|
||||
foreach ($spec as $key => $value) {
|
||||
self::set($key, $value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$_SESSION[(string) $spec] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Session value
|
||||
*
|
||||
* @param string $key
|
||||
*/
|
||||
public static function del($key)
|
||||
{
|
||||
if (!self::$started) {
|
||||
if (self::isExists()) {
|
||||
self::start();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (isset($_SESSION[$key])) {
|
||||
unset($_SESSION[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* remember() - Write a persistent cookie that expires after a number
|
||||
* of seconds in the future. If no number of seconds is specified,
|
||||
* then this defaults to self::$rememberMeSeconds.
|
||||
*
|
||||
* @param $seconds integer - OPTIONAL specifies TTL for cookie in seconds from present time
|
||||
* @return void
|
||||
*/
|
||||
public static function remember($seconds = null)
|
||||
{
|
||||
$seconds = (int) $seconds;
|
||||
$seconds = ($seconds > 0) ? $seconds : self::$remember_time;
|
||||
self::rememberUntil($seconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* forget() - Write a volatile session cookie, removing any persistent
|
||||
* cookie that may have existed. The session would end upon,
|
||||
* for example, termination of a web browser program.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function forget()
|
||||
{
|
||||
self::rememberUntil();
|
||||
}
|
||||
|
||||
/**
|
||||
* rememberUntil() - This method does the work of changing the state of the session
|
||||
* cookie and making sure that it gets resent to the browser via regenerateId()
|
||||
*
|
||||
* @param int $seconds
|
||||
* @return void
|
||||
*/
|
||||
public static function rememberUntil($seconds = 0)
|
||||
{
|
||||
$params = session_get_cookie_params();
|
||||
session_set_cookie_params(
|
||||
$seconds,
|
||||
$params['path'],
|
||||
$params['domain'],
|
||||
$params['secure']
|
||||
);
|
||||
self::regenerateId();
|
||||
}
|
||||
|
||||
/**
|
||||
* regenerateId() - Regenerate the session id. Best practice is to call this after
|
||||
* session is started. If called prior to session starting, session id will be regenerated
|
||||
* at start time.
|
||||
*
|
||||
* @throws Zend_Session_Exception
|
||||
* @return void
|
||||
*/
|
||||
public static function regenerateId()
|
||||
{
|
||||
if (self::$started) {
|
||||
session_regenerate_id(true);
|
||||
}
|
||||
}
|
||||
|
||||
public static function isExists()
|
||||
{
|
||||
if (isset($_COOKIE[session_name()])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function start()
|
||||
{
|
||||
if (self::$started) {
|
||||
return;
|
||||
}
|
||||
self::$started = true;
|
||||
session_start();
|
||||
}
|
||||
|
||||
/**
|
||||
* getId() - get the current session id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getId()
|
||||
{
|
||||
return session_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* destroy() - This is used to destroy session data, and optionally,
|
||||
* the session cookie itself
|
||||
*
|
||||
* @param bool $remove_cookie - OPTIONAL remove session id cookie,
|
||||
* defaults to true (remove cookie)
|
||||
* @return void
|
||||
*/
|
||||
public static function destroy($remove_cookie = true)
|
||||
{
|
||||
session_destroy();
|
||||
if ($remove_cookie) {
|
||||
self::expireSessionCookie();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* expireSessionCookie() - Sends an expired session id cookie, causing the client to delete the session cookie
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function expireSessionCookie()
|
||||
{
|
||||
if (isset($_COOKIE[session_name()])) {
|
||||
$params = session_get_cookie_params();
|
||||
setcookie(
|
||||
session_name(),
|
||||
false,
|
||||
0,
|
||||
$params['path'],
|
||||
$params['domain'],
|
||||
$params['secure']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static function setSessionHandler($handler)
|
||||
{
|
||||
session_set_save_handler(
|
||||
array($handler, 'open'),
|
||||
array($handler, 'close'),
|
||||
array($handler, 'read'),
|
||||
array($handler, 'write'),
|
||||
array($handler, 'destroy'),
|
||||
array($handler, 'gc')
|
||||
);
|
||||
}
|
||||
}
|
16
Session/session.sql
Normal file
16
Session/session.sql
Normal file
@ -0,0 +1,16 @@
|
||||
--
|
||||
-- $Id$
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `session`;
|
||||
|
||||
CREATE TABLE `session` (
|
||||
`id` CHAR( 32 ) NOT NULL ,
|
||||
`expires` INT UNSIGNED NOT NULL ,
|
||||
`ip` INT UNSIGNED NOT NULL ,
|
||||
`user_id` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`data` TEXT NOT NULL ,
|
||||
PRIMARY KEY ( `id` ) ,
|
||||
INDEX `expires` ( `expires` ) ,
|
||||
INDEX `user_id` ( `user_id` )
|
||||
) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8;
|
Reference in New Issue
Block a user