dolibarr 22.0.5
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-2025 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, $user;
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 if ((int) ini_get('session.gc_probability') == 0) {
135 // dolSessionGC will be never called
136 $max_lifetime = max(getDolGlobalInt('MAIN_SESSION_TIMEOUT'), (int) ini_get('session.gc_maxlifetime'));
137 $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
138 $delete_query .= " WHERE last_accessed < '".$dbsession->idate($time_stamp - $max_lifetime)."'";
139 $dbsession->query($delete_query);
140 }
141
142 // No session found, insert a new one
143 $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
144 $insert_query .= "(session_id, session_variable, date_creation, last_accessed, fk_user, remote_ip, user_agent)";
145 $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))."')";
146
147 $result = $dbsession->query($insert_query);
148 if (!$result) {
149 dol_print_error($dbsession);
150 return false;
151 }
152 } else {
153 if ($sessionidfound != $sess_id) {
154 // oops. How can this happen ?
155 dol_print_error($dbsession, 'Oops sess_id received in dolSessionWrite differs from the cache value $sessionidfound. How can this happen ?');
156 return false;
157 }
158 /*$sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
159 $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
160
161 // Execute the query
162 $resql = $dbsession->query($sql);
163 $num_rows = $dbsession->num_rows($resql);
164 if ($num_rows == 0) {
165 // No session found, insert a new one
166 $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
167 $insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
168 $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)."')";
169 //var_dump($insert_query);
170 $result = $dbsession->query($insert_query);
171 if (!$result) {
172 dol_print_error($dbsession);
173 return false;
174 }
175 } else {
176 */
177 // Existing session found - Update the session variables
178 $update_query = "UPDATE ".MAIN_DB_PREFIX."session";
179 $update_query .= " SET session_variable = '".$dbsession->escape($val)."',";
180 $update_query .= " last_accessed = '".$dbsession->idate($time_stamp)."',";
181 $update_query .= " fk_user = ".(int) $user->id.",";
182 $update_query .= " remote_ip = '".$dbsession->escape(getUserRemoteIP())."',";
183 $update_query .= " user_agent = '".$dbsession->escape($_SERVER['HTTP_USER_AGENT'])."'";
184 $update_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
185
186 $result = $dbsession->query($update_query);
187 if (!$result) {
188 dol_print_error($dbsession);
189 return false;
190 }
191 }
192 }
193
194 return true;
195}
196
203{
204 global $dbsession;
205
206 //var_dump('close');
207
208 $dbsession->close();
209
210 return true;
211}
212
219function dolSessionDestroy($sess_id)
220{
221 global $dbsession;
222
223 //var_dump('destroy');
224
225 $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
226 $delete_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
227 $dbsession->query($delete_query);
228
229 return true;
230}
231
239function dolSessionGC($max_lifetime)
240{
241 global $dbsession;
242
243 $time_stamp = dol_now();
244
245 $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
246 $delete_query .= " WHERE last_accessed < '".$dbsession->idate($time_stamp - $max_lifetime)."'";
247
248 $resql = $dbsession->query($delete_query);
249 if ($resql) {
250 return true;
251 } else {
252 return false;
253 }
254}
255
256// Call to register user call back functions.
257session_set_save_handler("dolSessionOpen", "dolSessionClose", "dolSessionRead", "dolSessionWrite", "dolSessionDestroy", "dolSessionGC"); // @phpstan-ignore-line
258
265{
266 global $dbsession;
267
268 $arrayofsessions = [];
269 $sql = "SELECT s.session_id, s.session_variable, s.fk_user, s.date_creation, s.last_accessed, s.remote_ip, s.user_agent";
270 $sql .= ", u.login";
271 $sql .= " FROM ".MAIN_DB_PREFIX."session as s";
272 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid=s.fk_user";
273 $sql .= " LIMIT 500";
274 $resql = $dbsession->query($sql);
275 while ($resql && $obj = $dbsession->fetch_object($resql)) {
276 $arrayofsessions[$obj->session_id] = [
277 "login" => (string) $obj->login,
278 "age" => dol_now() - (int) $dbsession->jdate($obj->date_creation),
279 "creation" => $dbsession->idate($obj->date_creation),
280 "modification" => $dbsession->idate($obj->last_accessed),
281 "remote_ip" => $obj->remote_ip,
282 "user_agent" => $obj->user_agent,
283 "raw" => "",
284 ];
285 }
286
287 return $arrayofsessions;
288}
dol_now($mode='auto')
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.