dolibarr 21.0.0-alpha
phpsessionindb.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
26// This session handler file must be included just after the call of the master.inc.php into main.inc.php
27// The $conf is already defined from conf.php file.
28// To use it set
29// - create table ll_session from the llx_session-disabled.sql file
30// - uncomment the include DOL_DOCUMENT_ROOT.'/core/lib/phpsessionindb.inc.php into main.inc.php
31// - in your PHP.ini, set: session.save_handler = user
32// The session_set_save_handler() at end of this file will replace default session management.
33
34
42function dolSessionOpen($save_path, $session_name)
43{
44 global $dbsession;
45
46 global $dolibarr_main_db_type, $dolibarr_main_db_host;
47 global $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name, $dolibarr_main_db_port;
48
49 global $dolibarr_session_db_type, $dolibarr_session_db_host;
50 global $dolibarr_session_db_user, $dolibarr_session_db_pass, $dolibarr_session_db_name, $dolibarr_session_db_port;
51
52 if (empty($dolibarr_session_db_type)) {
53 $dolibarr_session_db_type = $dolibarr_main_db_type;
54 }
55 if (empty($dolibarr_session_db_host)) {
56 $dolibarr_session_db_host = $dolibarr_main_db_host;
57 }
58 if (empty($dolibarr_session_db_user)) {
59 $dolibarr_session_db_user = $dolibarr_main_db_user;
60 }
61 if (empty($dolibarr_session_db_pass)) {
62 $dolibarr_session_db_pass = $dolibarr_main_db_pass;
63 }
64 if (empty($dolibarr_session_db_name)) {
65 $dolibarr_session_db_name = $dolibarr_main_db_name;
66 }
67 if (empty($dolibarr_session_db_port)) {
68 $dolibarr_session_db_port = $dolibarr_main_db_port;
69 }
70 //var_dump('open '.$database_name.' '.$table_name);
71
72 $dbsession = getDoliDBInstance($dolibarr_session_db_type, $dolibarr_session_db_host, $dolibarr_session_db_user, $dolibarr_session_db_pass, $dolibarr_session_db_name, (int) $dolibarr_session_db_port);
73
74 return true;
75}
76
83function dolSessionRead($sess_id)
84{
85 global $dbsession;
86 global $sessionlastvalueread;
87 global $sessionidfound;
88
89 $sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
90 $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
91
92 // Execute the query
93 $resql = $dbsession->query($sql);
94 $num_rows = $dbsession->num_rows($resql);
95 if ($num_rows == 0) {
96 // No session found - return an empty string
97 $sessionlastvalueread = '';
98 $sessionidfound = '';
99 return '';
100 } else {
101 // Found a session - return the serialized string
102 $obj = $dbsession->fetch_object($resql);
103 $sessionlastvalueread = $obj->session_variable;
104 $sessionidfound = $obj->session_id;
105 //var_dump($sessionlastvalueread);
106 //var_dump($sessionidfound);
107 return $obj->session_variable;
108 }
109}
110
119function dolSessionWrite($sess_id, $val)
120{
121 global $dbsession;
122 global $sessionlastvalueread;
123 global $sessionidfound;
124
125 //var_dump('write '.$sess_id);
126 //var_dump($val);
127 //var_dump('sessionlastvalueread='.$sessionlastvalueread.' sessionidfound='.$sessionidfound);
128
129 //$sessionlastvalueread='';
130 if ($sessionlastvalueread != $val) {
131 $time_stamp = dol_now();
132
133 if (empty($sessionidfound)) {
134 // No session found, insert a new one
135 $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
136 $insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
137 $insert_query .= " VALUES ('".$dbsession->escape($sess_id)."', '".$dbsession->escape($val)."', '".$dbsession->idate($time_stamp)."', 0, '".$dbsession->escape(getUserRemoteIP())."', '".$dbsession->escape(substr($_SERVER['HTTP_USER_AGENT'], 0, 255))."')";
138
139 $result = $dbsession->query($insert_query);
140 if (!$result) {
141 dol_print_error($dbsession);
142 return false;
143 }
144 } else {
145 if ($sessionidfound != $sess_id) {
146 // oops. How can this happen ?
147 dol_print_error($dbsession, 'Oops sess_id received in dolSessionWrite differs from the cache value $sessionidfound. How can this happen ?');
148 return false;
149 }
150 /*$sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
151 $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
152
153 // Execute the query
154 $resql = $dbsession->query($sql);
155 $num_rows = $dbsession->num_rows($resql);
156 if ($num_rows == 0) {
157 // No session found, insert a new one
158 $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
159 $insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
160 $insert_query .= " VALUES ('".$dbsession->escape($sess_id)."', '".$dbsession->escape($val)."', '".$dbsession->idate($time_stamp)."', 0, '".$dbsession->escape(getUserRemoteIP())."', '".$dbsession->escape(substr($_SERVER['HTTP_USER_AGENT'], 0, 255)."')";
161 //var_dump($insert_query);
162 $result = $dbsession->query($insert_query);
163 if (!$result) {
164 dol_print_error($dbsession);
165 return false;
166 }
167 } else {
168 */
169 // Existing session found - Update the session variables
170 $update_query = "UPDATE ".MAIN_DB_PREFIX."session";
171 $update_query .= " SET session_variable = '".$dbsession->escape($val)."',";
172 $update_query .= " last_accessed = '".$dbsession->idate($time_stamp)."',";
173 $update_query .= " remote_ip = '".$dbsession->escape(getUserRemoteIP())."',";
174 $update_query .= " user_agent = '".$dbsession->escape($_SERVER['HTTP_USER_AGENT'])."'";
175 $update_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
176
177 $result = $dbsession->query($update_query);
178 if (!$result) {
179 dol_print_error($dbsession);
180 return false;
181 }
182 }
183 }
184
185 return true;
186}
187
194{
195 global $dbsession;
196
197 //var_dump('close');
198
199 $dbsession->close();
200
201 return true;
202}
203
210function dolSessionDestroy($sess_id)
211{
212 global $dbsession;
213
214 //var_dump('destroy');
215
216 $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
217 $delete_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
218 $dbsession->query($delete_query);
219
220 return true;
221}
222
230function dolSessionGC($max_lifetime)
231{
232 global $dbsession;
233
234 $time_stamp = dol_now();
235
236 $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
237 $delete_query .= " WHERE last_accessed < '".$dbsession->idate($time_stamp - $max_lifetime)."'";
238
239 $resql = $dbsession->query($delete_query);
240 if ($resql) {
241 return true;
242 } else {
243 return false;
244 }
245}
246
247// Call to register user call back functions.
248session_set_save_handler("dolSessionOpen", "dolSessionClose", "dolSessionRead", "dolSessionWrite", "dolSessionDestroy", "dolSessionGC"); // @phpstan-ignore-line
dol_now($mode='auto')
Return date for now.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getUserRemoteIP()
Return the IP of remote user.
getDoliDBInstance($type, $host, $user, $pass, $name, $port)
Return a DoliDB instance (database handler).
dolSessionOpen($save_path, $session_name)
The session open handler called by PHP whenever a session is initialized.
dolSessionWrite($sess_id, $val)
This function is called when a session is initialized with a session_start( ) call,...
dolSessionDestroy($sess_id)
This is called whenever the session_destroy() function call is made.
dolSessionClose()
This function is executed on shutdown of the session.
dolSessionGC($max_lifetime)
This function is called on a session's start up with the probability specified in session....
dolSessionRead($sess_id)
This function is called whenever a session_start() call is made and reads the session variables.