dolibarr  13.0.2
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 // 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 
36 function dolSessionOpen($database_name, $table_name)
37 {
38  global $conf, $dbsession;
39 
40  $dbsession = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, $conf->db->port);
41 
42  return true;
43 }
44 
51 function dolSessionRead($sess_id)
52 {
53  global $dbsession;
54 
55  $sql = "SELECT session_variable FROM ".MAIN_DB_PREFIX."session";
56  $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
57 
58  // Execute the query
59  $resql = $dbsession->query($sql);
60  $num_rows = $dbsession->num_rows($resql);
61  if ($num_rows == 0) {
62  // No session found - return an empty string
63  return '';
64  } else {
65  // Found a session - return the serialized string
66  $obj = $dbsession->fetch_object($resql);
67  return $obj->session_variable;
68  }
69 }
70 
79 function dolSessionWrite($sess_id, $val)
80 {
81  global $dbsession;
82 
83  $time_stamp = dol_now();
84 
85  $sql = "SELECT session_id FROM ".MAIN_DB_PREFIX."session";
86  $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
87 
88  // Execute the query
89  $resql = $dbsession->query($sql);
90  $num_rows = $dbsession->num_rows($resql);
91  if ($num_rows == 0) {
92  // No session found, insert a new one
93  $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
94  $insert_query .= "(session_id, session_variable, last_accessed)";
95  $insert_query .= " VALUES ('".$dbsession->escape($sess_id)."', '".$dbsession->escape($val)."', '".$dbsession->idate($time_stamp)."')";
96  $dbsession->query($insert_query);
97  } else {
98  // Existing session found - Update the session variables
99  $update_query = "UPDATE ".MAIN_DB_PREFIX."session";
100  $update_query .= "SET session_variable = '".$dbsession->escape($val)."',";
101  $update_query .= " last_accessed = '".$dbsession->idate($time_stamp)."'";
102  $update_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
103  $dbsession->query($update_query);
104  }
105  return true;
106 }
107 
114 function dolSessionClose($sess_id)
115 {
116  global $dbsession;
117 
118  $dbsession->close();
119 
120  return true;
121 }
122 
129 function dolSessionDestroy($sess_id)
130 {
131  global $dbsession;
132 
133  $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
134  $delete_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
135  $dbsession->query($delete_query);
136 
137  return true;
138 }
139 
147 function dolSessionGC($max_lifetime)
148 {
149  global $dbsession;
150 
151  $time_stamp = dol_now();
152 
153  $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
154  $delete_query .= " WHERE last_accessed < '".$dbsession->idate($time_stamp - $max_lifetime)."'";
155 
156  $resql = $dbsession->query($delete_query);
157  if ($resql) {
158  return true;
159  } else {
160  return false;
161  }
162 }
163 
164 // Call to register user call back functions.
165 session_set_save_handler("dolSessionOpen", "dolSessionClose", "dolSessionRead", "dolSessionWrite", "dolSessionDestroy", "dolSessionGC");
dolSessionClose($sess_id)
This function is executed on shutdown of the session.
getDoliDBInstance($type, $host, $user, $pass, $name, $port)
Return a DoliDB instance (database handler).
dolSessionRead($sess_id)
This function is called whenever a session_start() call is made and reads the session variables...
dol_now($mode= 'auto')
Return date for now.
dolSessionWrite($sess_id, $val)
This function is called when a session is initialized with a session_start( ) call, when variables are registered or unregistered, and when session variables are modified.
dolSessionDestroy($sess_id)
This is called whenever the session_destroy() function call is made.
dolSessionGC($max_lifetime)
This function is called on a session&#39;s start up with the probability specified in session...
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dolSessionOpen($database_name, $table_name)
The session open handler called by PHP whenever a session is initialized.