AVE.CMS v3.28
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.

155 lines
3.6 KiB

8 years ago
<?php
/**
6 years ago
* AVE.cms
8 years ago
*
6 years ago
* @package AVE.cms
* @version 3.x
* @filesource
* @copyright © 2007-2014 AVE.cms, http://www.ave-cms.ru
8 years ago
*
6 years ago
* @license GPL v.2
8 years ago
*/
6 years ago
class AVE_Session_DB
{
8 years ago
6 years ago
public $sess_lifetime;
/**
* Хост
*
* @var string
*/
protected $db_host;
/**
* Имя пользователя
*
* @var string
*/
protected $db_user;
/**
* Пароль
*
* @var string
*/
protected $db_pass;
/**
* Имя текущей БД.
*
* @var string
*/
protected $db_dbase;
/**
* Префикс БД.
*
* @var string
*/
protected $db_prefix;
private $mysql_connect = null;
private $mysql_db = null;
/* Create a connection to a database */
function __construct()
{
// Подключаем конфигурационный файл с параметрами подключения
require (BASE_DIR . '/config/db.config.php');
8 years ago
6 years ago
$this->db_host = $config['dbhost'];
$this->db_user = $config['dbuser'];
$this->db_pass = $config['dbpass'];
$this->db_dbase = $config['dbname'];
$this->db_prefix = $config['dbpref'];
8 years ago
6 years ago
$this->sess_lifetime = (defined('SESSION_LIFETIME') && is_numeric(SESSION_LIFETIME))
? SESSION_LIFETIME
: (get_cfg_var("session.gc_maxlifetime") < 1440
? 1440
: get_cfg_var("session.gc_maxlifetime"));
8 years ago
6 years ago
if (! $this->mysql_connect = mysqli_connect ($this->db_host, $this->db_user, $this->db_pass))
$this->error();
8 years ago
6 years ago
if (! $this->mysql_db = mysqli_select_db ($this->mysql_connect, $this->db_dbase))
$this->error();
8 years ago
6 years ago
$this->mysql_connect->set_charset('utf8');
8 years ago
6 years ago
return true;
}
8 years ago
6 years ago
/* Open session */
function _open($path, $name)
{
return true;
}
8 years ago
6 years ago
/* Close session */
function _close()
{
@mysqli_query($this->mysql_connect, "DELETE FROM " . PREFIX . "_sessions WHERE expiry < '" . time() . "'");
8 years ago
6 years ago
if ($this->mysql_connect !== null)
@mysqli_close($this->mysql_connect);
8 years ago
6 years ago
return true;
8 years ago
}
6 years ago
/* Read session */
function _read($ses_id)
8 years ago
{
6 years ago
$qid = @mysqli_query($this->mysql_connect, "SELECT value, Ip FROM " . PREFIX . "_sessions WHERE sesskey = '" . $ses_id . "' AND expiry > '" . time() . "'");
if ((list($value, $ip) = @mysqli_fetch_row($qid)) && $ip == $_SERVER['REMOTE_ADDR'])
return $value;
return '';
8 years ago
}
6 years ago
/* Write new data */
function _write($ses_id, $data)
{
if (! $qid = @mysqli_query($this->mysql_connect, "INSERT INTO ".PREFIX."_sessions VALUES ('".$ses_id."', ".(time()+$this->sess_lifetime).", '".addslashes($data)."', '".$_SERVER['REMOTE_ADDR']."', FROM_UNIXTIME(expiry, '%d.%m.%Y, %H:%i:%s'))"))
$qid = @mysqli_query($this->mysql_connect, "UPDATE ".PREFIX."_sessions SET expiry = ".(time()+$this->sess_lifetime).", expire_datum = FROM_UNIXTIME(expiry,'%d.%m.%Y, %H:%i:%s'), value = '".addslashes($data)."', Ip = '".$_SERVER['REMOTE_ADDR']."' WHERE sesskey = '".$ses_id."' AND expiry > '".time()."'");
8 years ago
6 years ago
return $qid;
}
8 years ago
6 years ago
/* Destroy session */
function _destroy($ses_id)
{
return @mysqli_query($this->mysql_connect, "DELETE FROM ".PREFIX."_sessions WHERE sesskey = '".$ses_id."'");
8 years ago
}
6 years ago
/* Garbage collection, deletes old sessions */
function _gc($maxlifetime)
8 years ago
{
6 years ago
$session_res = @mysqli_query($this->mysql_connect, "DELETE FROM ".PREFIX."_sessions WHERE expire < (UNIX_TIMESTAMP(NOW()) - " . (int)$maxlifetime . ")");
if (! $session_res)
return false;
else
return true;
8 years ago
}
6 years ago
function error() {
ob_start();
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600');
header('X-Powered-By:');
display_notice("Error connect to MySQL.");
die;
}
8 years ago
}
?>