<?php

/**
 * ECSHOP SESSION 公用类库
 * ============================================================================
 * 版权所有 (C) 2005-2007 北京亿商互动科技发展有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * @author:     Paul Gao <paulgao@yeah.net>
 * @version:    v2.1
 * ---------------------------------------------
 * $Author: weberliu $
 * $Date: 2007-06-26 10:35:03 +0800 (星期二, 26 六月 2007) $
 * $Id: cls_session.php 9436 2007-06-26 02:35:03Z weberliu $
*/

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

class cls_session
{
    var $db            = NULL;
    var $session_table = '';

    var $max_life_time = 1440; // SESSION 过期时间

    var $session_name  = '';
    var $session_id    = '';

    var $ip            = '';

    var $session_cookie_path   = '/';
    var $session_cookie_domain = '';
    var $session_cookie_secure = false;

    function __construct(&$db, $session_table, $session_name = 'ECS_ID', $session_id = '')
    {
        $this->cls_session($db, $session_table, $session_name, $session_id);
    }

    function cls_session(&$db, $session_table, $session_name = 'ECS_ID', $session_id = '')
    {
        global $_SESSION;

        if (empty($_SESSION) == true)
        {
            $_SESSION = array();
        }

        if (!empty($GLOBALS['cookie_path']))
        {
            $this->session_cookie_path = $GLOBALS['cookie_path'];
        }
        else
        {
            $this->session_cookie_path = '/';
        }

        if (!empty($GLOBALS['cookie_domain']))
        {
            $this->session_cookie_domain = $GLOBALS['cookie_domain'];
        }
        else
        {
            $this->session_cookie_domain = '';
        }

        if (!empty($GLOBALS['cookie_secure']))
        {
            $this->session_cookie_secure = $GLOBALS['cookie_secure'];
        }
        else
        {
            $this->session_cookie_secure = false;
        }

        $this->session_name  = $session_name;
        $this->session_table = $session_table;

        $this->db = &$db;

        $this->ip = real_ip();

        if ($session_id == '' && !empty($_COOKIE[$this->session_name]))
        {
            $this->session_id = $_COOKIE[$this->session_name];
        }
        else
        {
            $this->session_id = $session_id;
        }

        if ($this->session_id)
        {
            $tmp_session_id = substr($this->session_id, 0, 32);
            if ($this->gen_session_key($tmp_session_id) == substr($this->session_id, 32))
            {
                $this->session_id = $tmp_session_id;
            }
            else
            {
                $this->session_id = '';
            }
        }

        if ($this->session_id)
        {
            $this->load_session();
        }
        else
        {
            $this->session_id = $this->gen_session_id();

            setcookie($this->session_name, $this->session_id . $this->gen_session_key($this->session_id), 0, $this->session_cookie_path, $this->session_cookie_domain, $this->session_cookie_secure);
        }

        register_shutdown_function(array(&$this, 'close_session'));
    }

    function gen_session_id()
    {
        $id = md5(uniqid(rand(), true));

        $this->insert_session($id);

        return $id;
    }

    function gen_session_key($session_id)
    {
        $ip = substr($this->ip, 0, strrpos($this->ip, '.'));

        return sprintf('%08x', crc32(!empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] . ROOT_PATH . $ip . $session_id : ROOT_PATH . $ip . $session_id));
    }

    function insert_session($id)
    {
        return $this->db->query('INSERT INTO ' . $this->session_table . " (sesskey, expiry, ip, data) VALUES ('" . $id . "', '". time() ."', '". $this->ip ."', 'a:0:{}')");
    }

    function load_session()
    {
        global $_SESSION;

        $session = $this->db->getRow('SELECT data, expiry FROM ' . $this->session_table . " WHERE sesskey = '" . $this->session_id . "'");
        if (empty($session))
        {
            $this->insert_session($this->session_id);
            $_SESSION = array();
        }
        else
        {
            if (!empty($session['data']) && time() - $session['expiry'] <= $this->max_life_time)
            {
                $_SESSION = unserialize($session['data']);
            }
            else
            {
                $_SESSION = array();
            }
        }
    }

    function update_session()
    {
        global $_SESSION;

        $adminid = !empty($_SESSION['admin_id']) ? intval($_SESSION['admin_id']) : 0;
        $userid  = !empty($_SESSION['user_id'])  ? intval($_SESSION['user_id'])  : 0;

        return $this->db->query('UPDATE ' . $this->session_table . " SET expiry = '" . time() . "', ip = '" . $this->ip . "', userid = '" . $userid . "', adminid = '" . $adminid . "', data = '" . addslashes(serialize($_SESSION)) . "' WHERE sesskey = '" . $this->session_id . "' LIMIT 1");
    }

    function close_session()
    {
        $this->update_session();

        return $this->db->query('DELETE FROM ' . $this->session_table . ' WHERE expiry < ' . (time() - $this->max_life_time));
    }

    function delete_spec_admin_session($adminid)
    {
        if (!empty($_SESSION['admin_id']) && $adminid)
        {
            return $this->db->query('DELETE FROM ' . $this->session_table . " WHERE adminid = '$adminid'");
        }
        else
        {
            return false;
        }
    }

    function destroy_session()
    {
        global $_SESSION;

        $_SESSION = array();

        setcookie($this->session_name, $this->session_id, 1, $this->session_cookie_path, $this->session_cookie_domain, $this->session_cookie_secure);

        /* 自定义执行部分 */
        if (!empty($GLOBALS['ecs']))
        {
            $this->db->query('DELETE FROM ' . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '$this->session_id'");
        }
        /* 自定义执行部分 */

        return $this->db->query('DELETE FROM ' . $this->session_table . " WHERE sesskey = '". $this->session_id ."' LIMIT 1");
    }

    function get_session_id()
    {
        return $this->session_id;
    }

    function get_users_count()
    {
        return $this->db->getOne('SELECT count(*) FROM ' . $this->session_table);
    }
}

?>