<?php

class session_nodb

{

    public $expire = 3600;



    public function __construct(&$locator)

    {

        $this->database = $this -> connect();

        $this->request  =& $locator->get('request');



        session_set_save_handler(array(&$this, 'open'),

                                 array(&$this, 'close'),

                                 array(&$this, 'read'),

                                 array(&$this, 'write'),

                                 array(&$this, 'destroy'),

                                 array(&$this, 'clean'));



        register_shutdown_function('session_write_close');

        if (!$this->request->has('access', 'cookie')) {

            setcookie('access', 'yes', time() + 60 * 60 * 24 * 30, '/', NULL, false);

        }



        if ($this->request->has('access', 'cookie')) {

            session_set_cookie_params(0, '/');



            session_start();

        }

    }



    public function connect()

    {

        require_once(DIR_LIBRARY . 'database/database.php');

        $database = new Database($this);

        $database->connect('localhost', 'beststoreitems_admin', 'FDS#2015', 'beststoreitems_database');



        return $database;

    }



    public function set($key, $value)

    {

        $_SESSION[$key] = $value;

    }



    public function get($key)

    {

        return (isset($_SESSION[$key]) ? $_SESSION[$key] : NULL);

    }



    public function has($key)

    {

        return isset($_SESSION[$key]);

    }



    public function delete($key)

    {

        if (isset($_SESSION[$key])) {

            unset($_SESSION[$key]);

        }

    }



    public function open()

    {

        return TRUE;

    }



    public function close()

    {

        return TRUE;

    }



    public function read($session_id)

    {

        $result = $this->database->getRow($this->database->parse("select value from session where session_id = '?' and expire > '?'", $session_id, time()));



//      print_r ($result);

//      echo "<script>alert('read session $session_id');</script>";

        return (isset($result['value']) ? $result['value'] : NULL);

    }



    public function write($session_id, $data)

    {

        date_default_timezone_set('Asia/Hong_Kong');



//      print_r ($data);

//      echo "<script>alert('write session $session_id');</script>";



        if (!$this->database->getRow($this->database->parse("select * from session where session_id = '?'", $session_id))) {



            $sql = "insert into session set session_id = '?', expire = '?', `value` = '?', ip = '?', time = '".date('Y-m-d H:i:s',time())."', url = '?'";

              $this->database->query($this->database->parse($sql, $session_id, time() + $this->expire, $data, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']));

        } else {

              $sql = "update session set expire = '?', `value` = '?', ip = '?', time = '".date('Y-m-d H:i:s',time())."', url = '?' where session_id = '?'";

              $this->database->query($this->database->parse($sql, time() + $this->expire, $data, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI'], $session_id));

        }



        return $this->database->countAffected();

    }



    public function destroy($session_id)

    {

        $this->database->query($this->database->parse("delete from session where session_id = '?'", $session_id));



        return $this->database->countAffected();

    }



    public function clean($maxlifetime)

    {

        $this->database->query($this->database->parse("delete from session where expire < '?'", time()));



        return $this->database->countAffected();

    }

}

