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); |
|
91 |
$last_updated = date('Y-m-d H:i:s'); |
7fe908
|
92 |
$this->db->query("UPDATE sys_session SET last_updated = '$last_updated' WHERE session_id = '$session_id'"); |
532ae5
|
93 |
return true; |
L |
94 |
} |
|
95 |
|
7fe908
|
96 |
|
MC |
97 |
if (@$this->session_array['session_id'] == '') { |
532ae5
|
98 |
$session_id = $this->db->quote($session_id); |
7fe908
|
99 |
$date_created = date('Y-m-d H:i:s'); |
MC |
100 |
$last_updated = date('Y-m-d H:i:s'); |
|
101 |
$session_data = $this->db->quote($session_data); |
9ae865
|
102 |
$sql = "REPLACE INTO sys_session (session_id,date_created,last_updated,session_data,permanent) VALUES ('$session_id','$date_created','$last_updated','$session_data','" . ($this->permanent ? 'y' : 'n') . "')"; |
532ae5
|
103 |
$this->db->query($sql); |
L |
104 |
|
7fe908
|
105 |
} else { |
MC |
106 |
$session_id = $this->db->quote($session_id); |
532ae5
|
107 |
$last_updated = date('Y-m-d H:i:s'); |
7fe908
|
108 |
$session_data = $this->db->quote($session_data); |
de0256
|
109 |
$sql = "UPDATE sys_session SET last_updated = '$last_updated', session_data = '$session_data'" . ($this->permanent ? ", `permanent` = 'y'" : "") . " WHERE session_id = '$session_id'"; |
532ae5
|
110 |
$this->db->query($sql); |
L |
111 |
|
7fe908
|
112 |
} |
MC |
113 |
|
|
114 |
return true; |
|
115 |
} |
|
116 |
|
532ae5
|
117 |
function destroy ($session_id) { |
L |
118 |
|
|
119 |
$session_id = $this->db->quote($session_id); |
|
120 |
$sql = "DELETE FROM sys_session WHERE session_id = '$session_id'"; |
|
121 |
$this->db->query($sql); |
7fe908
|
122 |
|
MC |
123 |
return true; |
|
124 |
} |
|
125 |
|
532ae5
|
126 |
function gc ($max_lifetime) { |
L |
127 |
|
c951bb
|
128 |
/*if($this->timeout > 0) { |
MC |
129 |
$this->db->query("DELETE FROM sys_session WHERE last_updated < DATE_SUB(NOW(), INTERVAL " . intval($this->timeout) . " MINUTE)"); |
|
130 |
} else {*/ |
|
131 |
$real_now = date('Y-m-d H:i:s'); |
|
132 |
$dt1 = strtotime("$real_now -$max_lifetime seconds"); |
|
133 |
$dt2 = date('Y-m-d H:i:s', $dt1); |
7fe908
|
134 |
|
de0256
|
135 |
$sql = "DELETE FROM sys_session WHERE last_updated < '$dt2' AND `permanent` != 'y'"; |
MC |
136 |
$this->db->query($sql); |
|
137 |
|
|
138 |
/* delete very old even if they are permanent */ |
|
139 |
$dt1 = strtotime("$real_now -365 days"); |
|
140 |
$dt2 = date('Y-m-d H:i:s', $dt1); |
|
141 |
|
c951bb
|
142 |
$sql = "DELETE FROM sys_session WHERE last_updated < '$dt2'"; |
MC |
143 |
$this->db->query($sql); |
|
144 |
//} |
7fe908
|
145 |
|
MC |
146 |
return true; |
|
147 |
|
|
148 |
} |
532ae5
|
149 |
|
L |
150 |
function __destruct () { |
7fe908
|
151 |
@session_write_close(); |
532ae5
|
152 |
|
7fe908
|
153 |
} |
532ae5
|
154 |
|
L |
155 |
} |
|
156 |
|
7fe908
|
157 |
?> |