File: session.php

package info (click to toggle)
opendb 0.62p9-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,544 kB
  • ctags: 3,560
  • sloc: php: 26,575; sql: 1,982; sh: 250; makefile: 54
file content (156 lines) | stat: -rw-r--r-- 3,884 bytes parent folder | download
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
<?php
/* 	OpenDb - Open Lending Database Project
	Copyright (C) 2001,2002 by Jason Pell

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

include_once("./functions/function.php");
include_once("./functions/utils.php");

/*
 * function: mysql_session_open()
 * 
 * Does nothing.
 */
function mysql_session_open($session_path, $session_name)
{
	return TRUE;
}

/*
 * function: mysql_session_close()
 */
function mysql_session_close()
{
	return TRUE;
}

/*
 * function: mysql_session_read()
 * 
 * Reads the session data from the database
 */
function mysql_session_read($SID)
{
	global $CONFIG_VARS;
	
	$query = "SELECT value FROM ".$CONFIG_VARS['session_handler.session_table'].
			" WHERE SID = '$SID' AND ".
			" expiration > ". time();

	$result = run_opendb_query($query, FALSE, 'session_handler');
	if($result && @mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result);
		mysql_free_result($result);

		return $found['value'];
	}
	else
		return "";
}

/*
 * function: mysql_session_write()
 * 
 * This function writes the session data to the database. If that SID 
 * already exists, then the existing data will be updated.
 */
function mysql_session_write($SID, $value)
{
	global $CONFIG_VARS;

	$expiration = time() + get_cfg_var("session.gc_maxlifetime");
	
	$query = "INSERT INTO ".$CONFIG_VARS['session_handler.session_table'].
			" VALUES('$SID', '$expiration', '$value')";

	$result = run_opendb_query($query, FALSE, 'session_handler');
	if($result && @mysql_affected_rows($result))
		return TRUE;
	else //if (! $result)
	{
		$query = "UPDATE ".$CONFIG_VARS['session_handler.session_table'].
		" SET expiration = '$expiration',".
			" value = '$value' WHERE ".
			" SID = '$SID' AND expiration >". time();

		$result = run_opendb_query($query, FALSE, 'session_handler');
		if($result && @mysql_affected_rows($result))
			return TRUE;
		else
			return FALSE;
	}
	
	//else
	return FALSE;
}

/*
 * function: mysql_session_destroy()
 * 
 * Deletes all session information having input SID (only one row)
 */
function mysql_session_destroy($SID)
{
	global $CONFIG_VARS;
	
	$query = "DELETE FROM ".$CONFIG_VARS['session_handler.session_table'].
      " WHERE SID = '$SID'";

	$result = run_opendb_query($query, FALSE, 'session_handler');
	if($result && @mysql_affected_rows($result))
		return TRUE;
	else
		return FALSE;
		
}

/*
 * function: mysql_session_gc()
 * 
 * Deletes all sessions that have expired.
*/
function mysql_session_gc($maxlifetime)
{
	global $CONFIG_VARS;
	
	$query = "DELETE FROM ".$CONFIG_VARS['session_handler.session_table'].
			" WHERE sess_expiration < ".time() - $maxlifetime;
			
	$result = run_opendb_query($query, FALSE, 'session_handler');
	if($result && @mysql_affected_rows($result))
		return TRUE;
	else
		return FALSE;

}

// Attempt to change the ini value if required, but complain if not possible.
if(strtolower(ini_get("session.save_handler")) == "user" || ini_set("session.save_handler", "user"))
{
	session_set_save_handler("mysql_session_open", 
							"mysql_session_close",
							"mysql_session_read",
							"mysql_session_write",
							"mysql_session_destroy",
							"mysql_session_gc");
}
else
{
	opendb_log("session.php:: Cannot set session.save_handler to 'user'");
}
?>