dolibarr  20.0.0-beta
phpsessionindb.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
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 
25 // This 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
28 // - create table ll_session from the llx_session-disabled.sql file
29 // - uncomment the include DOL_DOCUMENT_ROOT.'/core/lib/phpsessionindb.inc.php into main.inc.php
30 // - in your PHP.ini, set: session.save_handler = user
31 // The session_set_save_handler() at end of this file will replace default session management.
32 
33 
41 function dolSessionOpen($save_path, $session_name)
42 {
43  global $dbsession;
44 
45  global $dolibarr_main_db_type, $dolibarr_main_db_host;
46  global $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name, $dolibarr_main_db_port;
47 
48  global $dolibarr_session_db_type, $dolibarr_session_db_host;
49  global $dolibarr_session_db_user, $dolibarr_session_db_pass, $dolibarr_session_db_name, $dolibarr_session_db_port;
50 
51  if (empty($dolibarr_session_db_type)) {
52  $dolibarr_session_db_type = $dolibarr_main_db_type;
53  }
54  if (empty($dolibarr_session_db_host)) {
55  $dolibarr_session_db_host = $dolibarr_main_db_host;
56  }
57  if (empty($dolibarr_session_db_user)) {
58  $dolibarr_session_db_user = $dolibarr_main_db_user;
59  }
60  if (empty($dolibarr_session_db_pass)) {
61  $dolibarr_session_db_pass = $dolibarr_main_db_pass;
62  }
63  if (empty($dolibarr_session_db_name)) {
64  $dolibarr_session_db_name = $dolibarr_main_db_name;
65  }
66  if (empty($dolibarr_session_db_port)) {
67  $dolibarr_session_db_port = $dolibarr_main_db_port;
68  }
69  //var_dump('open '.$database_name.' '.$table_name);
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 
82 function 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 
118 function dolSessionWrite($sess_id, $val)
119 {
120  global $dbsession;
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  // No session found, insert a new one
134  $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
135  $insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
136  $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))."')";
137 
138  $result = $dbsession->query($insert_query);
139  if (!$result) {
140  dol_print_error($dbsession);
141  return false;
142  }
143  } else {
144  if ($sessionidfound != $sess_id) {
145  // oops. How can this happen ?
146  dol_print_error($dbsession, 'Oops sess_id received in dolSessionWrite differs from the cache value $sessionidfound. How can this happen ?');
147  return false;
148  }
149  /*$sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
150  $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
151 
152  // Execute the query
153  $resql = $dbsession->query($sql);
154  $num_rows = $dbsession->num_rows($resql);
155  if ($num_rows == 0) {
156  // No session found, insert a new one
157  $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
158  $insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
159  $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)."')";
160  //var_dump($insert_query);
161  $result = $dbsession->query($insert_query);
162  if (!$result) {
163  dol_print_error($dbsession);
164  return false;
165  }
166  } else {
167  */
168  // Existing session found - Update the session variables
169  $update_query = "UPDATE ".MAIN_DB_PREFIX."session";
170  $update_query .= " SET session_variable = '".$dbsession->escape($val)."',";
171  $update_query .= " last_accessed = '".$dbsession->idate($time_stamp)."',";
172  $update_query .= " remote_ip = '".$dbsession->escape(getUserRemoteIP())."',";
173  $update_query .= " user_agent = '".$dbsession->escape($_SERVER['HTTP_USER_AGENT'])."'";
174  $update_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
175 
176  $result = $dbsession->query($update_query);
177  if (!$result) {
178  dol_print_error($dbsession);
179  return false;
180  }
181  }
182  }
183 
184  return true;
185 }
186 
192 function dolSessionClose()
193 {
194  global $dbsession;
195 
196  //var_dump('close');
197 
198  $dbsession->close();
199 
200  return true;
201 }
202 
209 function dolSessionDestroy($sess_id)
210 {
211  global $dbsession;
212 
213  //var_dump('destroy');
214 
215  $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
216  $delete_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
217  $dbsession->query($delete_query);
218 
219  return true;
220 }
221 
229 function dolSessionGC($max_lifetime)
230 {
231  global $dbsession;
232 
233  $time_stamp = dol_now();
234 
235  $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
236  $delete_query .= " WHERE last_accessed < '".$dbsession->idate($time_stamp - $max_lifetime)."'";
237 
238  $resql = $dbsession->query($delete_query);
239  if ($resql) {
240  return true;
241  } else {
242  return false;
243  }
244 }
245 
246 // Call to register user call back functions.
247 session_set_save_handler("dolSessionOpen", "dolSessionClose", "dolSessionRead", "dolSessionWrite", "dolSessionDestroy", "dolSessionGC");
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
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.