session_saas.php 4.07 KB
Newer Older
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
1 2 3 4 5
<?php
// This class store/retrieve the sessions in/from the database. 
// You will need this if you have more than one webserver
// to enable the persistence of sessions accross different front servers.

6
class session_saas {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
7 8

    private $life_time      = 0;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
9
    private $debug = false;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
10 11

    public function __construct() {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
12 13 14
        if($this->debug) {
            trigger_error('session_saas::__construct called', E_USER_NOTICE);
        }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
        session_set_save_handler(
            array(&$this, 'open'),
            array(&$this, 'close'),
            array(&$this, 'read'),
            array(&$this, 'write'),
            array(&$this, 'destroy'),
            array(&$this, 'gc')
        );
        // session_name("whatever");
        $this->life_time = ini_get('session.gc_maxlifetime');
        // -- Define a lifetime on session cookie
        if ( ini_get('session.use_only_cookies') == 1 && intval($this->life_time)>0 ) {
            ini_set('session.cookie_secure',   FALSE);
            ini_set('session.cookie_httponly', TRUE);
            session_set_cookie_params($this->life_time);
        }
        session_start();
32
        return true;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
33 34 35
    }

    public function __destruct() {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
36 37 38
        if($this->debug) {
            trigger_error('session_saas::__destruct called', E_USER_NOTICE);
        }
39
        return true;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
40 41 42
    }

    public function open() {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
43 44 45
        if($this->debug) {
            trigger_error('session_saas::open called', E_USER_NOTICE);
        }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
46 47 48 49 50 51 52 53
        // -- Maintain session cookie updated for each requests
        if ( ini_get('session.use_only_cookies')==1 && intval($this->life_time)>0 ) {
            setcookie(session_name(),session_id(),(time()+$this->life_time), '/');
        }
        return true;
    }

    public function close() {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
54 55 56
        if($this->debug) {
            trigger_error('session_saas::close called', E_USER_NOTICE);
        }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
57 58 59 60 61
        $this->gc($this->life_time);
        return true;
    }

    public function read($id) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
62 63 64
        if($this->debug) {
            trigger_error('session_saas::read called', E_USER_NOTICE);
        }
65 66 67 68 69
        if($session = json_decode(file_get_contents($GLOBALS["saas_sessions_url"].
            "?a=read&i=".$id))) {
            if(isset($session->data)) {
                return base64_decode($session->data);
            }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
70 71
        }
        return '';
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
72 73 74
    }

    public function write($id, $data) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
75 76 77
        if($this->debug) {
            trigger_error('session_saas::write called', E_USER_NOTICE);
        }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

        $postdata = http_build_query(
            array(
                'a' => 'write',
                'i' => $id,
                'data' => base64_encode($data)
            )
        );

        $opts = array('http' =>
            array(
                'method'  => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => $postdata
            )
        );
        
        $context  = stream_context_create($opts);
96 97 98 99
        if(file_get_contents($GLOBALS["saas_sessions_url"], false, $context)) {
            return false;
        }
        return true;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
100 101 102
    }

    public function destroy($id, $key_only = FALSE) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
103 104 105
        if($this->debug) {
            trigger_error('session_saas::destroy called', E_USER_NOTICE);
        }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        $postdata = http_build_query(
            array(
                'a' => 'destroy',
                'i' => $id
            )
        );

        $opts = array('http' =>
            array(
                'method'  => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => $postdata
            )
        );
        
        $context  = stream_context_create($opts);
122 123 124 125
        if(!file_get_contents($GLOBALS["saas_sessions_url"], false, $context)) {
            return false;
        }
        return true;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
126 127 128
    }

    public function gc($max) {
129
        /*
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
130 131 132
        if($this->debug) {
            trigger_error('session_saas::gc called', E_USER_NOTICE);
        }
133 134 135 136 137
        if(!file_get_contents($GLOBALS["saas_sessions_url"]."?a=gc")) {
            return false;
        }
        */
        return true;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
138 139
    }
}