-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_handler.php
executable file
·173 lines (149 loc) · 4.61 KB
/
session_handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
function get_sessionconn()
{
include (dirname(__FILE__) . "/cfgsession.php");
$sessionconn = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$db");
return $sessionconn;
}
function sopen($s, $n)
{
if (get_sessionconn())
{
return true;
}
else
{
return false;
}
}
function sread($id)
{
$sessionconn = get_sessionconn();
if (!$sessionconn)
{
return false;
}
$sql = "SELECT 1,`value` FROM `sessions` WHERE `sessionid` = '$id'";
$result = $sessionconn->query($sql);
if (mysqli_error($sessionconn) || !$result || !mysqli_num_rows($result))
{
$save_path = ini_get("session.save_path");
if (file_exists($save_path . "/sess_$id"))
{
//check if file should have expired already
$session_duration = ini_get('session.gc_maxlifetime'); //how long a session is good for, in seconds
$use_file_for_session = true;
if (filemtime($save_path . "/sess_$id") < time() - $session_duration)
{
$use_file_for_session = false;
}
//allow fail-over without logging out
if (file_exists("/tmp/session_use"))
{
$use = file_get_contents("/tmp/session_use");
}
else
{
$use = "";
}
try
{
if (mysqli_error($sessionconn))
$message .= ' error, ';
if (!$result)
$message .= ' no result, ';
if ($result)
$message .= ' result is set ';
if (!mysqli_num_rows($result))
$message .= ' no rows, ';
$to_write = " unable to fetch result ";
if ($result)
{
$data = $result->fetch_assoc();
$to_write = "print_r: " . print_r($data, true);
}
}
catch (Exception $e)
{
}
$message .= $to_write;
if ($use_file_for_session)
{
file_put_contents("/tmp/session_use", $use . "\n" . time() . " using file for " . $id . " error: '" . mysqli_error($sessionconn) . "' sql: '" . $sql . "' message: " . $message);
}
else
{
file_put_contents("/tmp/session_use", $use . "\n" . time() . " *NOT* using file for " . $id . " error: '" . mysqli_error($sessionconn) . "' sql: '" . $sql . "' message: " . $message);
}
return file_get_contents($save_path . "/sess_$id");
}
return false;
}
$data = $result->fetch_assoc();
return stripslashes($data['value']);
}
function swrite($id, $data)
{
$sessionconn = get_sessionconn();
if (!$sessionconn)
return false;
$sql = "REPLACE INTO `sessions` (`sessionid`,`atime`,`value`) VALUES ('$id',NOW(),'" . addslashes($data) . "')";
$result = $sessionconn->query($sql);
$save_path = ini_get("session.save_path");
file_put_contents($save_path . "/sess_$id", $data);
return true;
}
function sclose()
{
return true;
}
function sdestroy($id)
{ // do not modify function parameters
$sessionconn = get_sessionconn();
if (!$sessionconn)
{
return false;
}
$sql = "DELETE FROM `sessions` WHERE `sessionid` = '$id' LIMIT 1";
$result = $sessionconn->query($sql);
$save_path = ini_get("session.save_path");
$filename = "$save_path/sess_" . $id;
@unlink($filename);
file_put_contents("/tmp/session_destroy", $use . "\n" . time() . " destroying session " . $id);
if (mysqli_error($sessionconn) || !$result)
{
return false;
}
return true;
}
function sgc($expire)
{
$sessionconn = get_sessionconn();
if (!$sessionconn)
return false;
$session_duration = ini_get('session.gc_maxlifetime'); //how long a session is good for, in seconds
$sql = "DELETE FROM `sessions` WHERE `atime` < DATE_ADD(NOW(), INTERVAL -$session_duration SECOND)"; //delete from the table where the access time is less than (NOW() minus session_duration seconds)
file_put_contents("/tmp/sql", $sql);
$result = $sessionconn->query($sql);
//if (mysqli_error($sessionconn) || !$result) return false;
$save_path = ini_get("session.save_path");
$output = time() . " \n";
foreach (glob("$save_path/sess_*") as $filename)
{
//$output .= $filename.": ".filemtime($filename)." + $session_duration < ".time();
if (@filemtime($filename) < time() - $session_duration)
{
@unlink($filename);
//$output .= " DELETE \n";
}
else
{
//$output .= " keep \n";
}
}
file_put_contents("/tmp/session_gc", $output);
return $result;
}
//session_set_save_handler ("open", "close", "read", "write", "destroy", "gc");
include_once('/var/www/script-repository/session_handler_include.php');
?>