Marius Burkard
2016-07-01 49441bdd0f3ff75d5092d5b832b97ea722a66363
commit | author | age
532ae5 1 <?php
L 2
3 /*
4 Copyright (c) 2010, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class session {
7fe908 32
532ae5 33     private $session_array = array();
L 34     private $db;
c951bb 35     private $timeout = 0;
de0256 36     private $permanent = false;
7fe908 37
c951bb 38     function __construct($session_timeout = 0) {
532ae5 39         $this->db = new db;
c951bb 40         $this->timeout = $session_timeout;
MC 41     }
42     
43     function set_timeout($session_timeout = 0) {
44         $old_timeout = $this->timeout;
45         $this->timeout = $session_timeout;
46         return $old_timeout;
de0256 47     }
MC 48     
49     function set_permanent($value = false) {
50         $this->permanent = $value;
532ae5 51     }
7fe908 52
532ae5 53     function open ($save_path, $session_name) {
L 54         return true;
55     }
7fe908 56
532ae5 57     function close () {
L 58
59         if (!empty($this->session_array)) {
7fe908 60             $result = $this->gc(ini_get('session.gc_maxlifetime'));
MC 61             return $result;
62         }
63         return false;
64     }
65
532ae5 66     function read ($session_id) {
c951bb 67         
MC 68         if($this->timeout > 0) {
cc7a82 69             $rec = $this->db->queryOneRecord("SELECT * FROM sys_session WHERE session_id = ? AND (`permanent` = 'y' OR last_updated >= DATE_SUB(NOW(), INTERVAL ? MINUTE))", $session_id, $this->timeout);
c951bb 70         } else {
cc7a82 71             $rec = $this->db->queryOneRecord("SELECT * FROM sys_session WHERE session_id = ?", $session_id);
c951bb 72         }
532ae5 73
7fe908 74         if (is_array($rec)) {
532ae5 75             $this->session_array = $rec;
L 76             return $this->session_array['session_data'];
77         } else {
78             return '';
79         }
80     }
7fe908 81
532ae5 82     function write ($session_id, $session_data) {
7fe908 83
532ae5 84         if (!empty($this->session_array) && $this->session_array['session_id'] != $session_id) {
7fe908 85             $this->session_array = array();
MC 86         }
87
532ae5 88         // Dont write session_data to DB if session data has not been changed after reading it.
L 89         if(isset($this->session_array['session_data']) && $this->session_array['session_data'] != '' && $this->session_array['session_data'] == $session_data) {
cc7a82 90             $this->db->query("UPDATE sys_session SET last_updated = NOW() WHERE session_id = ?", $session_id);
532ae5 91             return true;
L 92         }
93
7fe908 94
MC 95         if (@$this->session_array['session_id'] == '') {
ebcb3b 96             $sql = "REPLACE INTO sys_session (session_id,date_created,last_updated,session_data,permanent) VALUES (?,NOW(),NOW(),?,?)";
TB 97             $this->db->query($sql, $session_id, $session_data, ($this->permanent ? 'y' : 'n'));
532ae5 98
7fe908 99         } else {
cc7a82 100             $sql = "UPDATE sys_session SET last_updated = NOW(), session_data = ?" . ($this->permanent ? ", `permanent` = 'y'" : "") . " WHERE session_id = ?";
MC 101             $this->db->query($sql, $session_data, $session_id);
532ae5 102
7fe908 103         }
MC 104
105         return true;
106     }
107
532ae5 108     function destroy ($session_id) {
L 109
cc7a82 110         $sql = "DELETE FROM sys_session WHERE session_id = ?";
MC 111         $this->db->query($sql, $session_id);
7fe908 112
MC 113         return true;
114     }
115
532ae5 116     function gc ($max_lifetime) {
L 117
cc7a82 118         $sql = "DELETE FROM sys_session WHERE last_updated < DATE_SUB(NOW(), INTERVAL ? SECOND) AND `permanent` != 'y'";
MC 119         $this->db->query($sql, intval($max_lifetime));
de0256 120             
cc7a82 121         /* delete very old even if they are permanent */
MC 122         $sql = "DELETE FROM sys_session WHERE last_updated < DATE_SUB(NOW(), INTERVAL 1 YEAR)";
123         $this->db->query($sql);
7fe908 124
MC 125         return true;
126
127     }
532ae5 128
L 129     function __destruct () {
7fe908 130         @session_write_close();
532ae5 131
7fe908 132     }
532ae5 133
L 134 }
135
7fe908 136 ?>