Marius Cramer
2014-08-07 a1be59c6db4f286640d5a3eb0229e43bed641785
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) {
de0256 69             $rec = $this->db->queryOneRecord("SELECT * FROM sys_session WHERE session_id = '".$this->db->quote($session_id)."' AND (`permanent` = 'y' OR last_updated >= DATE_SUB(NOW(), INTERVAL " . intval($this->timeout) . " MINUTE))");
c951bb 70         } else {
MC 71             $rec = $this->db->queryOneRecord("SELECT * FROM sys_session WHERE session_id = '".$this->db->quote($session_id)."'");
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) {
90             $session_id   = $this->db->quote($session_id);
a1be59 91             $this->db->query("UPDATE sys_session SET last_updated = NOW() WHERE session_id = '$session_id'");
532ae5 92             return true;
L 93         }
94
7fe908 95
MC 96         if (@$this->session_array['session_id'] == '') {
532ae5 97             $session_id   = $this->db->quote($session_id);
7fe908 98             $session_data = $this->db->quote($session_data);
a1be59 99             $sql = "REPLACE INTO sys_session (session_id,date_created,last_updated,session_data,permanent) VALUES ('$session_id',NOW(),NOW(),'$session_data','" . ($this->permanent ? 'y' : 'n') . "')";
532ae5 100             $this->db->query($sql);
L 101
7fe908 102         } else {
MC 103             $session_id   = $this->db->quote($session_id);
104             $session_data = $this->db->quote($session_data);
a1be59 105             $sql = "UPDATE sys_session SET last_updated = NOW(), session_data = '$session_data'" . ($this->permanent ? ", `permanent` = 'y'" : "") . " WHERE session_id = '$session_id'";
532ae5 106             $this->db->query($sql);
L 107
7fe908 108         }
MC 109
110         return true;
111     }
112
532ae5 113     function destroy ($session_id) {
L 114
115         $session_id   = $this->db->quote($session_id);
116         $sql = "DELETE FROM sys_session WHERE session_id = '$session_id'";
117         $this->db->query($sql);
7fe908 118
MC 119         return true;
120     }
121
532ae5 122     function gc ($max_lifetime) {
L 123
c951bb 124         /*if($this->timeout > 0) {
MC 125             $this->db->query("DELETE FROM sys_session WHERE last_updated < DATE_SUB(NOW(), INTERVAL " . intval($this->timeout) . " MINUTE)");
126         } else {*/
a1be59 127             $sql = "DELETE FROM sys_session WHERE last_updated < DATE_SUB(NOW(), INTERVAL " . intval($max_lifetime) . " SECOND) AND `permanent` != 'y'";
de0256 128             $this->db->query($sql);
MC 129             
130             /* delete very old even if they are permanent */
a1be59 131             $sql = "DELETE FROM sys_session WHERE last_updated < DATE_SUB(NOW(), INTERVAL 1 YEAR)";
c951bb 132             $this->db->query($sql);
MC 133         //}
7fe908 134
MC 135         return true;
136
137     }
532ae5 138
L 139     function __destruct () {
7fe908 140         @session_write_close();
532ae5 141
7fe908 142     }
532ae5 143
L 144 }
145
7fe908 146 ?>