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 { |
b1a6a5
|
32 |
|
532ae5
|
33 |
private $session_array = array(); |
L |
34 |
private $db; |
b1a6a5
|
35 |
|
532ae5
|
36 |
function __construct() { |
L |
37 |
$this->db = new db; |
|
38 |
} |
b1a6a5
|
39 |
|
532ae5
|
40 |
function open ($save_path, $session_name) { |
L |
41 |
return true; |
|
42 |
} |
b1a6a5
|
43 |
|
532ae5
|
44 |
function close () { |
L |
45 |
|
|
46 |
if (!empty($this->session_array)) { |
b1a6a5
|
47 |
$result = $this->gc(ini_get('session.gc_maxlifetime')); |
MC |
48 |
return $result; |
|
49 |
} |
|
50 |
return false; |
|
51 |
} |
|
52 |
|
532ae5
|
53 |
function read ($session_id) { |
b1a6a5
|
54 |
|
532ae5
|
55 |
$rec = $this->db->queryOneRecord("SELECT * FROM sys_session WHERE session_id = '".$this->db->quote($session_id)."'"); |
L |
56 |
|
b1a6a5
|
57 |
if (is_array($rec)) { |
532ae5
|
58 |
$this->session_array = $rec; |
L |
59 |
return $this->session_array['session_data']; |
|
60 |
} else { |
|
61 |
return ''; |
|
62 |
} |
|
63 |
} |
b1a6a5
|
64 |
|
532ae5
|
65 |
function write ($session_id, $session_data) { |
b1a6a5
|
66 |
|
532ae5
|
67 |
if (!empty($this->session_array) && $this->session_array['session_id'] != $session_id) { |
b1a6a5
|
68 |
$this->session_array = array(); |
MC |
69 |
} |
|
70 |
|
532ae5
|
71 |
// Dont write session_data to DB if session data has not been changed after reading it. |
L |
72 |
if(isset($this->session_array['session_data']) && $this->session_array['session_data'] != '' && $this->session_array['session_data'] == $session_data) { |
|
73 |
$session_id = $this->db->quote($session_id); |
|
74 |
$last_updated = date('Y-m-d H:i:s'); |
b1a6a5
|
75 |
$this->db->query("UPDATE sys_session SET last_updated = '$last_updated' WHERE session_id = '$session_id'"); |
532ae5
|
76 |
return true; |
L |
77 |
} |
|
78 |
|
b1a6a5
|
79 |
|
MC |
80 |
if (@$this->session_array['session_id'] == '') { |
532ae5
|
81 |
$session_id = $this->db->quote($session_id); |
b1a6a5
|
82 |
$date_created = date('Y-m-d H:i:s'); |
MC |
83 |
$last_updated = date('Y-m-d H:i:s'); |
|
84 |
$session_data = $this->db->quote($session_data); |
532ae5
|
85 |
$sql = "INSERT INTO sys_session (session_id,date_created,last_updated,session_data) VALUES ('$session_id','$date_created','$last_updated','$session_data')"; |
L |
86 |
$this->db->query($sql); |
|
87 |
|
b1a6a5
|
88 |
} else { |
MC |
89 |
$session_id = $this->db->quote($session_id); |
532ae5
|
90 |
$last_updated = date('Y-m-d H:i:s'); |
b1a6a5
|
91 |
$session_data = $this->db->quote($session_data); |
MC |
92 |
$sql = "UPDATE sys_session SET last_updated = '$last_updated', session_data = '$session_data' WHERE session_id = '$session_id'"; |
532ae5
|
93 |
$this->db->query($sql); |
L |
94 |
|
b1a6a5
|
95 |
} |
MC |
96 |
|
|
97 |
return true; |
|
98 |
} |
|
99 |
|
532ae5
|
100 |
function destroy ($session_id) { |
L |
101 |
|
|
102 |
$session_id = $this->db->quote($session_id); |
|
103 |
$sql = "DELETE FROM sys_session WHERE session_id = '$session_id'"; |
|
104 |
$this->db->query($sql); |
b1a6a5
|
105 |
|
MC |
106 |
return true; |
|
107 |
} |
|
108 |
|
532ae5
|
109 |
function gc ($max_lifetime) { |
L |
110 |
|
|
111 |
$real_now = date('Y-m-d H:i:s'); |
b1a6a5
|
112 |
$dt1 = strtotime("$real_now -$max_lifetime seconds"); |
MC |
113 |
$dt2 = date('Y-m-d H:i:s', $dt1); |
|
114 |
|
532ae5
|
115 |
$sql = "DELETE FROM sys_session WHERE last_updated < '$dt2'"; |
L |
116 |
$this->db->query($sql); |
b1a6a5
|
117 |
|
MC |
118 |
return true; |
|
119 |
|
|
120 |
} |
532ae5
|
121 |
|
L |
122 |
function __destruct () { |
b1a6a5
|
123 |
@session_write_close(); |
532ae5
|
124 |
|
b1a6a5
|
125 |
} |
532ae5
|
126 |
|
L |
127 |
} |
|
128 |
|
b1a6a5
|
129 |
?> |