phpsessionindb.lib.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /**
  19. * \file htdocs/core/lib/phpsessionindb.lib.php
  20. * \ingroup core
  21. * \brief Set function handlers for PHP session management in DB.
  22. */
  23. // The session handler file must be included just after the call of the master.inc.php into main.inc.php
  24. // The $conf is already defined from conf.php file.
  25. // To use it set in your PHP.ini: session.save_handler = user
  26. /**
  27. * The session open handler called by PHP whenever a session is initialized.
  28. *
  29. * @param string $save_path Value of session.save_path into php.ini
  30. * @param string $session_name Session name (Example: 'DOLSESSID_xxxxxx')
  31. * @return boolean Always true
  32. */
  33. function dolSessionOpen($save_path, $session_name)
  34. {
  35. global $dbsession;
  36. global $dolibarr_main_db_type, $dolibarr_main_db_host;
  37. global $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name, $dolibarr_main_db_port;
  38. global $dolibarr_session_db_type, $dolibarr_session_db_host;
  39. global $dolibarr_session_db_user, $dolibarr_session_db_pass, $dolibarr_session_db_name, $dolibarr_session_db_port;
  40. if (empty($dolibarr_session_db_type)) { $dolibarr_session_db_type = $dolibarr_main_db_type; }
  41. if (empty($dolibarr_session_db_host)) { $dolibarr_session_db_host = $dolibarr_main_db_host; }
  42. if (empty($dolibarr_session_db_user)) { $dolibarr_session_db_user = $dolibarr_main_db_user; }
  43. if (empty($dolibarr_session_db_pass)) { $dolibarr_session_db_pass = $dolibarr_main_db_pass; }
  44. if (empty($dolibarr_session_db_name)) { $dolibarr_session_db_name = $dolibarr_main_db_name; }
  45. if (empty($dolibarr_session_db_port)) { $dolibarr_session_db_port = $dolibarr_main_db_port; }
  46. //var_dump('open '.$database_name.' '.$table_name);
  47. $dbsession = getDoliDBInstance($dolibarr_session_db_type, $dolibarr_session_db_host, $dolibarr_session_db_user, $dolibarr_session_db_pass, $dolibarr_session_db_name, $dolibarr_session_db_port);
  48. return true;
  49. }
  50. /**
  51. * This function is called whenever a session_start() call is made and reads the session variables.
  52. *
  53. * @param string $sess_id Session ID
  54. * @return string Returns "" when a session is not found or (serialized)string if session exists
  55. */
  56. function dolSessionRead($sess_id)
  57. {
  58. global $dbsession;
  59. global $sessionlastvalueread;
  60. global $sessionidfound;
  61. $sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
  62. $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
  63. // Execute the query
  64. $resql = $dbsession->query($sql);
  65. $num_rows = $dbsession->num_rows($resql);
  66. if ($num_rows == 0) {
  67. // No session found - return an empty string
  68. $sessionlastvalueread = '';
  69. $sessionidfound = '';
  70. return '';
  71. } else {
  72. // Found a session - return the serialized string
  73. $obj = $dbsession->fetch_object($resql);
  74. $sessionlastvalueread = $obj->session_variable;
  75. $sessionidfound = $obj->session_id;
  76. //var_dump($sessionlastvalueread);
  77. //var_dump($sessionidfound);
  78. return $obj->session_variable;
  79. }
  80. }
  81. /**
  82. * This function is called when a session is initialized with a session_start( ) call, when variables are registered or unregistered,
  83. * and when session variables are modified. Returns true on success.
  84. *
  85. * @param string $sess_id Session iDecodeStream
  86. * @param string $val Content of session
  87. * @return boolean Always true
  88. */
  89. function dolSessionWrite($sess_id, $val)
  90. {
  91. global $dbsession;
  92. global $sessionlastvalueread;
  93. global $sessionidfound;
  94. //var_dump('write '.$sess_id);
  95. //var_dump($val);
  96. //var_dump('sessionlastvalueread='.$sessionlastvalueread.' sessionidfound='.$sessionidfound);
  97. //$sessionlastvalueread='';
  98. if ($sessionlastvalueread != $val) {
  99. $time_stamp = dol_now();
  100. if (empty($sessionidfound)) {
  101. // No session found, insert a new one
  102. $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
  103. $insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
  104. $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))."')";
  105. $result = $dbsession->query($insert_query);
  106. if (!$result) {
  107. dol_print_error($dbsession);
  108. return false;
  109. }
  110. } else {
  111. if ($sessionidfound != $sess_id) {
  112. // oops. How can this happen ?
  113. dol_print_error($dbsession, 'Oops sess_id received in dolSessionWrite differs from the cache value $sessionidfound. How can this happen ?');
  114. return false;
  115. }
  116. /*$sql = "SELECT session_id, session_variable FROM ".MAIN_DB_PREFIX."session";
  117. $sql .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
  118. // Execute the query
  119. $resql = $dbsession->query($sql);
  120. $num_rows = $dbsession->num_rows($resql);
  121. if ($num_rows == 0) {
  122. // No session found, insert a new one
  123. $insert_query = "INSERT INTO ".MAIN_DB_PREFIX."session";
  124. $insert_query .= "(session_id, session_variable, last_accessed, fk_user, remote_ip, user_agent)";
  125. $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)."')";
  126. //var_dump($insert_query);
  127. $result = $dbsession->query($insert_query);
  128. if (!$result) {
  129. dol_print_error($dbsession);
  130. return false;
  131. }
  132. } else {
  133. */
  134. // Existing session found - Update the session variables
  135. $update_query = "UPDATE ".MAIN_DB_PREFIX."session";
  136. $update_query .= " SET session_variable = '".$dbsession->escape($val)."',";
  137. $update_query .= " last_accessed = '".$dbsession->idate($time_stamp)."',";
  138. $update_query .= " remote_ip = '".$dbsession->escape(getUserRemoteIP())."',";
  139. $update_query .= " user_agent = '".$dbsession->escape($_SERVER['HTTP_USER_AGENT'])."'";
  140. $update_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
  141. $result = $dbsession->query($update_query);
  142. if (!$result) {
  143. dol_print_error($dbsession);
  144. return false;
  145. }
  146. }
  147. }
  148. return true;
  149. }
  150. /**
  151. * This function is executed on shutdown of the session.
  152. *
  153. * @return boolean Always returns true.
  154. */
  155. function dolSessionClose()
  156. {
  157. global $dbsession;
  158. //var_dump('close');
  159. $dbsession->close();
  160. return true;
  161. }
  162. /**
  163. * This is called whenever the session_destroy() function call is made. Returns true if the session has successfully been deleted.
  164. *
  165. * @param string $sess_id Session iDecodeStream
  166. * @return boolean Always true
  167. */
  168. function dolSessionDestroy($sess_id)
  169. {
  170. global $dbsession;
  171. //var_dump('destroy');
  172. $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
  173. $delete_query .= " WHERE session_id = '".$dbsession->escape($sess_id)."'";
  174. $dbsession->query($delete_query);
  175. return true;
  176. }
  177. /**
  178. * This function is called on a session's start up with the probability specified in session.gc_probability.
  179. * Performs garbage collection by removing all sessions that haven't been updated in the last $max_lifetime seconds as set in session.gc_maxlifetime.
  180. *
  181. * @param int $max_lifetime Max lifetime
  182. * @return boolean true if the DELETE query succeeded.
  183. */
  184. function dolSessionGC($max_lifetime)
  185. {
  186. global $dbsession;
  187. $time_stamp = dol_now();
  188. $delete_query = "DELETE FROM ".MAIN_DB_PREFIX."session";
  189. $delete_query .= " WHERE last_accessed < '".$dbsession->idate($time_stamp - $max_lifetime)."'";
  190. $resql = $dbsession->query($delete_query);
  191. if ($resql) {
  192. return true;
  193. } else {
  194. return false;
  195. }
  196. }
  197. // Call to register user call back functions.
  198. session_set_save_handler("dolSessionOpen", "dolSessionClose", "dolSessionRead", "dolSessionWrite", "dolSessionDestroy", "dolSessionGC");