dolibarr 23.0.3
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-2026 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
71 $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);
72
73 return true;
74}
75
82function dolSessionRead($sess_id)
83{
84 global $dbsession;
85 global $sessionlastvalueread;
86 global $sessionidfound;
87
88 $sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
89 $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
90
91 // Execute the query
92 $resql = $dbsession->query($sql);
93 $num_rows = $dbsession->num_rows($resql);
94 if ($num_rows == 0) {
95 // No session found - return an empty string
96 $sessionlastvalueread = '';
97 $sessionidfound = '';
98 return '';
99 } else {
100 // Found a session - return the serialized string
101 $obj = $dbsession->fetch_object($resql);
102 $sessionlastvalueread = $obj->session_variable;
103 $sessionidfound = $obj->session_id;
104 //var_dump($sessionlastvalueread);
105 //var_dump($sessionidfound);
106 return $obj->session_variable;
107 }
108}
109
118function dolSessionWrite($sess_id, $val)
119{
120 global $dbsession, $user;
121 global $sessionlastvalueread;
122 global $sessionidfound;
123
124 //var_dump('write '.$sess_id);
125 //var_dump($val);
126 //var_dump('sessionlastvalueread='.$sessionlastvalueread.' sessionidfound='.$sessionidfound);
127
128 //$sessionlastvalueread='';
129 if ($sessionlastvalueread != $val) {
130 $time_stamp = dol_now();
131
132 if (empty($sessionidfound)) {
133 if ((int) ini_get('session.gc_probability') == 0) {
134 // dolSessionGC will be never called
135 $max_lifetime = max(getDolGlobalInt('MAIN_SESSION_TIMEOUT'), (int) ini_get('session.gc_maxlifetime'));
136 $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
137 $delete_query .= " WHERE last_accessed < '".$dbsession->idate($time_stamp - $max_lifetime)."'";
138 $dbsession->query($delete_query);
139 }
140
141 // No session found, insert a new one
142 $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
143 $insert_query .= "(session_id, session_variable, date_creation, last_accessed, fk_user, remote_ip, user_agent)";
144 $insert_query .= " VALUES ('".$dbsession->escape($sess_id)."', '".$dbsession->escape($val)."', '".$dbsession->idate($time_stamp)."', '".$dbsession->idate($time_stamp)."', 0, '".$dbsession->escape(getUserRemoteIP())."', '".$dbsession->escape(substr($_SERVER['HTTP_USER_AGENT'], 0, 255))."')";
145
146 $result = $dbsession->query($insert_query);
147 if (!$result) {
148 dol_print_error($dbsession);
149 return false;
150 }
151 } else {
152 if ($sessionidfound != $sess_id) {
153 // oops. How can this happen ?
154 dol_print_error($dbsession, 'Oops sess_id received in dolSessionWrite differs from the cache value $sessionidfound. How can this happen ?');
155 return false;
156 }
157 /*$sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
158 $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
159
160 // Execute the query
161 $resql = $dbsession->query($sql);
162 $num_rows = $dbsession->num_rows($resql);
163 if ($num_rows == 0) {
164 // No session found, insert a new one
165 $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
166 $insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
167 $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)."')";
168 $result = $dbsession->query($insert_query);
169 if (!$result) {
170 dol_print_error($dbsession);
171 return false;
172 }
173 } else {
174 */
175 // Existing session found - Update the session variables
176 $update_query = "UPDATE ".MAIN_DB_PREFIX."session";
177 $update_query .= " SET session_variable = '".$dbsession->escape($val)."',";
178 $update_query .= " last_accessed = '".$dbsession->idate($time_stamp)."',";
179 $update_query .= " fk_user = ".(int) (!empty($user->id) ? $user->id : 0).",";
180 $update_query .= " remote_ip = '".$dbsession->escape(getUserRemoteIP())."',";
181 $update_query .= " user_agent = '".$dbsession->escape($_SERVER['HTTP_USER_AGENT'])."'";
182 $update_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
183
184 $result = $dbsession->query($update_query);
185 if (!$result) {
186 dol_print_error($dbsession);
187 return false;
188 }
189 }
190 }
191
192 return true;
193}
194
201{
202 global $dbsession;
203
204 //var_dump('close');
205
206 $dbsession->close();
207
208 return true;
209}
210
217function dolSessionDestroy($sess_id)
218{
219 global $dbsession;
220
221 //var_dump('destroy');
222
223 $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
224 $delete_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
225 $dbsession->query($delete_query);
226
227 return true;
228}
229
237function dolSessionGC($max_lifetime)
238{
239 global $dbsession;
240
241 $time_stamp = dol_now();
242
243 $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
244 $delete_query .= " WHERE last_accessed < '".$dbsession->idate($time_stamp - $max_lifetime)."'";
245
246 $resql = $dbsession->query($delete_query);
247 if ($resql) {
248 return true;
249 } else {
250 return false;
251 }
252}
253
254// Call to register user call back functions.
255session_set_save_handler("dolSessionOpen", "dolSessionClose", "dolSessionRead", "dolSessionWrite", "dolSessionDestroy", "dolSessionGC"); // @phpstan-ignore-line
256
263{
264 global $dbsession;
265
266 $arrayofsessions = [];
267 $sql = "SELECT s.session_id, s.session_variable, s.fk_user, s.date_creation, s.last_accessed, s.remote_ip, s.user_agent";
268 $sql .= ", u.login";
269 $sql .= " FROM ".MAIN_DB_PREFIX."session as s";
270 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid=s.fk_user";
271 $sql .= " LIMIT 500";
272 $resql = $dbsession->query($sql);
273 while ($resql && $obj = $dbsession->fetch_object($resql)) {
274 $arrayofsessions[$obj->session_id] = [
275 "login" => (string) $obj->login,
276 "age" => dol_now() - (int) $dbsession->jdate($obj->date_creation),
277 "creation" => $dbsession->idate($obj->date_creation),
278 "modification" => $dbsession->idate($obj->last_accessed),
279 "remote_ip" => $obj->remote_ip,
280 "user_agent" => $obj->user_agent,
281 "raw" => "",
282 ];
283 }
284
285 return $arrayofsessions;
286}
dol_now($mode='gmt')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getUserRemoteIP($trusted=0)
Return the real IP of remote user.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
dolListSessions()
List sessions in db.
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.