phpsessionindb.lib.php 8.6 KB

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