Passed
Branch develop (20f4b5)
by
unknown
33:34
created

dolSessionOpen()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 64
nop 2
dl 0
loc 21
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2020 Laurent Destailleur  <[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 * or see https://www.gnu.org/
17
 */
18
19
/**
20
 *  \file		htdocs/core/lib/phpsessionindb.lib.php
21
 *  \ingroup    core
22
 *  \brief		Set function handlers for PHP session management in DB.
23
 */
24
25
// The session handler file must be included just after the call of the master.inc.php into main.inc.php
26
// The $conf is already defined from conf.php file.
27
// To use it set in your PHP.ini:  session.save_handler = user
28
29
/**
30
 * The session open handler called by PHP whenever a session is initialized.
31
 *
32
 * @param	string	$save_path      Value of session.save_path into php.ini
33
 * @param	string	$session_name	Session name (Example: 'DOLSESSID_xxxxxx')
34
 * @return	boolean					Always true
35
 */
36
function dolSessionOpen($save_path, $session_name)
37
{
38
	global $dbsession;
39
40
	global $dolibarr_main_db_type, $dolibarr_main_db_host;
41
	global $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name, $dolibarr_main_db_port;
42
43
	global $dolibarr_session_db_type, $dolibarr_session_db_host;
44
	global $dolibarr_session_db_user, $dolibarr_session_db_pass, $dolibarr_session_db_name, $dolibarr_session_db_port;
45
46
	if (empty($dolibarr_session_db_type)) {	$dolibarr_session_db_type = $dolibarr_main_db_type; }
47
	if (empty($dolibarr_session_db_host)) {	$dolibarr_session_db_host = $dolibarr_main_db_host; }
48
	if (empty($dolibarr_session_db_user)) {	$dolibarr_session_db_user = $dolibarr_main_db_user; }
49
	if (empty($dolibarr_session_db_pass)) {	$dolibarr_session_db_pass = $dolibarr_main_db_pass; }
50
	if (empty($dolibarr_session_db_name)) {	$dolibarr_session_db_name = $dolibarr_main_db_name; }
51
	if (empty($dolibarr_session_db_port)) {	$dolibarr_session_db_port = $dolibarr_main_db_port; }
52
	//var_dump('open '.$database_name.' '.$table_name);
53
54
	$dbsession = getDoliDBInstance($dolibarr_session_db_type, $dolibarr_session_db_host, $dolibarr_session_db_user, $dolibarr_session_db_pass, $dolibarr_session_db_name, $dolibarr_session_db_port);
55
56
	return true;
57
}
58
59
/**
60
 * This function is called whenever a session_start() call is made and reads the session variables.
61
 *
62
 * @param	string		$sess_id	Session ID
63
 * @return	string					Returns "" when a session is not found  or (serialized)string if session exists
64
 */
65
function dolSessionRead($sess_id)
66
{
67
	global $dbsession;
68
	global $sessionlastvalueread;
69
	global $sessionidfound;
70
71
	$sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
72
	$sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
73
74
	// Execute the query
75
	$resql = $dbsession->query($sql);
76
	$num_rows = $dbsession->num_rows($resql);
77
	if ($num_rows == 0) {
78
		// No session found - return an empty string
79
		$sessionlastvalueread = '';
80
		$sessionidfound = '';
81
		return '';
82
	} else {
83
		// Found a session - return the serialized string
84
		$obj = $dbsession->fetch_object($resql);
85
		$sessionlastvalueread = $obj->session_variable;
86
		$sessionidfound = $obj->session_id;
87
		//var_dump($sessionlastvalueread);
88
		//var_dump($sessionidfound);
89
		return $obj->session_variable;
90
	}
91
}
92
93
/**
94
 * This function is called when a session is initialized with a session_start(  ) call, when variables are registered or unregistered,
95
 * and when session variables are modified. Returns true on success.
96
 *
97
 * @param	string		$sess_id		Session iDecodeStream
98
 * @param	string		$val			Content of session
99
 * @return	boolean						Always true
100
 */
101
function dolSessionWrite($sess_id, $val)
102
{
103
	global $dbsession;
104
	global $sessionlastvalueread;
105
	global $sessionidfound;
106
107
	/*var_dump('write '.$sess_id);
108
	var_dump($val);
109
	var_dump('sessionlastvalueread='.$sessionlastvalueread.' sessionidfound='.$sessionidfound);
110
	*/
111
112
	//$sessionlastvalueread='';
113
	if ($sessionlastvalueread != $val) {
114
		$time_stamp = dol_now();
115
116
		if (empty($sessionidfound)) {
117
			// No session found, insert a new one
118
			$insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
119
			$insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
120
			$insert_query .= " VALUES ('".$dbsession->escape($sess_id)."', '".$dbsession->escape($val)."', '".$dbsession->idate($time_stamp)."', 0, '".$dbsession->escape(getUserRemoteIP())."', '".$dbsession->escape($_SERVER['HTTP_USER_AGENT'])."')";
121
122
			$result = $dbsession->query($insert_query);
123
			if (!$result) {
124
				dol_print_error($dbsession);
125
				return false;
126
			}
127
		} else {
128
			if ($sessionidfound != $sess_id) {
129
				// oops. How can this happen ?
130
				dol_print_error($dbsession, 'Oops sess_id received in dolSessionWrite differs from the cache value $sessionidfound. How can this happen ?');
131
				return false;
132
			}
133
			/*$sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
134
			$sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
135
136
			// Execute the query
137
			$resql = $dbsession->query($sql);
138
			$num_rows = $dbsession->num_rows($resql);
139
			if ($num_rows == 0) {
140
			// No session found, insert a new one
141
			$insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
142
			$insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
143
			$insert_query .= " VALUES ('".$dbsession->escape($sess_id)."', '".$dbsession->escape($val)."', '".$dbsession->idate($time_stamp)."', 0, '".$dbsession->escape(getUserRemoteIP())."', '".$dbsession->escape($_SERVER['HTTP_USER_AGENT'])."')";
144
			var_dump($insert_query);
145
			$result = $dbsession->query($insert_query);
146
			if (!$result) {
147
				dol_print_error($dbsession);
148
				return false;
149
			}
150
			} else {
151
			*/
152
			// Existing session found - Update the session variables
153
			$update_query = "UPDATE ".MAIN_DB_PREFIX."session";
154
			$update_query .= " SET session_variable = '".$dbsession->escape($val)."',";
155
			$update_query .= " last_accessed = '".$dbsession->idate($time_stamp)."',";
156
			$update_query .= " remote_ip = '".$dbsession->escape(getUserRemoteIP())."',";
157
			$update_query .= " user_agent = '".$dbsession->escape($_SERVER['HTTP_USER_AGENT'])."'";
158
			$update_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
159
160
			$result = $dbsession->query($update_query);
161
			if (!$result) {
162
				dol_print_error($dbsession);
163
				return false;
164
			}
165
		}
166
	}
167
168
	return true;
169
}
170
171
/**
172
 * This function is executed on shutdown of the session.
173
 *
174
 * @return	boolean					Always returns true.
175
 */
176
function dolSessionClose()
177
{
178
	global $dbsession;
179
180
	//var_dump('close');
181
182
	$dbsession->close();
183
184
	return true;
185
}
186
187
/**
188
 * This is called whenever the session_destroy() function call is made. Returns true if the session has successfully been deleted.
189
 *
190
 * @param	string	$sess_id		Session iDecodeStream
191
 * @return	boolean					Always true
192
 */
193
function dolSessionDestroy($sess_id)
194
{
195
	global $dbsession;
196
197
	//var_dump('destroy');
198
199
	$delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
200
	$delete_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
201
	$dbsession->query($delete_query);
202
203
	return true;
204
}
205
206
/**
207
 * This function is called on a session's start up with the probability specified in session.gc_probability.
208
 * Performs garbage collection by removing all sessions that haven't been updated in the last $max_lifetime seconds as set in session.gc_maxlifetime.
209
 *
210
 * @param	int		$max_lifetime		Max lifetime
211
 * @return	boolean						true if the DELETE query succeeded.
212
 */
213
function dolSessionGC($max_lifetime)
214
{
215
	global $dbsession;
216
217
	$time_stamp = dol_now();
218
219
	$delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
220
	$delete_query .= " WHERE last_accessed < '".$dbsession->idate($time_stamp - $max_lifetime)."'";
221
222
	$resql = $dbsession->query($delete_query);
223
	if ($resql) {
224
		return true;
225
	} else {
226
		return false;
227
	}
228
}
229
230
// Call to register user call back functions.
231
session_set_save_handler("dolSessionOpen", "dolSessionClose", "dolSessionRead", "dolSessionWrite", "dolSessionDestroy", "dolSessionGC");
232