game/onyx/session.class.php

146 lines
3.1 KiB
PHP
Raw Normal View History

2008-11-17 11:00:00 +00:00
<?php
class session {
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
var $xid;
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
var $level = 0;
2008-11-04 11:00:00 +00:00
var $cookie_name;
var $cookie;
var $values = array();
2008-11-17 11:00:00 +00:00
function clean()
{
global $config;
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
if($config['session']=='1')
{
$db = new bdd();
$db->connexion();
2008-11-04 11:00:00 +00:00
2008-11-18 11:00:00 +00:00
#$db->control($config['db_prefix']."sessions");
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$time = time()-$config['session_time'];
$table = $config['db_prefix']."sessions";
2008-11-03 11:00:00 +00:00
$db->query("DELETE FROM $table WHERE time < $time AND active = True;");
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$db->deconnexion();
}
}
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
function new_cookie()
{
global $config;
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
if($config['session']=='1')
{
$db = new bdd();
$db->connexion();
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$time = time();
$xid = random();
2008-11-04 11:00:00 +00:00
$sess_cookie = random(256);
2008-11-17 11:00:00 +00:00
$ip = encode_ip();
$table = $config['db_prefix']."sessions";
2008-11-03 11:00:00 +00:00
$db->query("INSERT INTO $table(xid,session,time,ip,var_session,level,hash,active) VALUES('$xid','$sess_cookie','$time','$ip','','1','',True) ");
2008-11-17 11:00:00 +00:00
$db->hash($table,"xid = '$xid'");
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$this->xid = $xid;
2008-11-04 11:00:00 +00:00
$this->cookie = $sess_cookie;
cookie((empty($this->cookie_name)?$config['session_name']:$this->cookie_name),$sess_cookie);
2008-11-17 11:00:00 +00:00
$db->deconnexion();
}
}
function load()
{
$this->clean();
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
global $config;
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
if($config['session']=='1')
{
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
if($cookie = gpc($config['session_name'],'cookie'))
{
$db = new bdd();
$db->connexion();
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$db->escape($cookie);
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$table = $config['db_prefix']."sessions";
2008-11-04 11:00:00 +00:00
$db->check($table,"session = '$cookie'");
2008-11-03 11:00:00 +00:00
$query = $db->unique_query("SELECT * FROM $table WHERE session='$cookie' AND active = True;");
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
if($db->num_rows == 1 && $query['ip'] == encode_ip())
{
2008-11-04 11:00:00 +00:00
$this->xid = $query['xid'];
$this->cookie = $query['session'];
2008-11-17 11:00:00 +00:00
$this->level = $query['level'];
$this->values = unserialize($query['var_session']);
2008-11-04 11:00:00 +00:00
cookie((empty($this->cookie_name)?$config['session_name']:$this->cookie_name),$this->cookie);
2008-11-17 11:00:00 +00:00
}
else $this->new_cookie();
$db->deconnexion();
}
else $this->new_cookie();
}
}
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
function put()
{
global $config;
2008-11-04 11:00:00 +00:00
if($config['session']=='1' && !empty($this->xid))
2008-11-17 11:00:00 +00:00
{
$db = new bdd();
$db->connexion();
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$time = time();
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$var_session = serialize($this->values);
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$db->escape($var_session);
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$xid = $this->xid;
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$table = $config['db_prefix']."sessions";
$db->query("UPDATE $table SET time='$time', var_session='$var_session' WHERE xid='$xid'");
$db->hash($table,"xid = '$xid'");
2008-11-04 11:00:00 +00:00
2008-11-17 11:00:00 +00:00
$db->deconnexion();
}
}
2008-11-04 11:00:00 +00:00
function close()
{
global $config;
if($config['session']=='1' && !empty($this->xid))
{
$db = new bdd();
$db->connexion();
$xid = $this->xid;
$table = $config['db_prefix']."sessions";
2008-11-03 11:00:00 +00:00
$db->query("DELETE FROM $table WHERE xid = '$xid' AND active = True;");
2008-11-04 11:00:00 +00:00
$db->deconnexion();
cookie((empty($this->cookie_name)?$config['session_name']:$this->cookie_name),$this->cookie,time()-3600);
$this->xid = NULL;
$this->values = array();
$this->level = 0;
}
}
2008-11-17 11:00:00 +00:00
}
?>