PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/fsn-site-central/mediatheque/include/functions_session.inc.php

https://gitlab.com/team_fsn/fsn-php
PHP | 274 lines | 152 code | 23 blank | 99 comment | 9 complexity | 847d89e80212397603ed05f179b6e41f MD5 | raw file
  1. <?php
  2. // +-----------------------------------------------------------------------+
  3. // | Piwigo - a PHP based photo gallery |
  4. // +-----------------------------------------------------------------------+
  5. // | Copyright(C) 2008-2016 Piwigo Team http://piwigo.org |
  6. // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
  7. // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
  8. // +-----------------------------------------------------------------------+
  9. // | This program is free software; you can redistribute it and/or modify |
  10. // | it under the terms of the GNU General Public License as published by |
  11. // | the Free Software Foundation |
  12. // | |
  13. // | This program is distributed in the hope that it will be useful, but |
  14. // | WITHOUT ANY WARRANTY; without even the implied warranty of |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  16. // | General Public License for more details. |
  17. // | |
  18. // | You should have received a copy of the GNU General Public License |
  19. // | along with this program; if not, write to the Free Software |
  20. // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
  21. // | USA. |
  22. // +-----------------------------------------------------------------------+
  23. /**
  24. * @package functions\session
  25. */
  26. if (isset($conf['session_save_handler'])
  27. and ($conf['session_save_handler'] == 'db')
  28. and defined('PHPWG_INSTALLED'))
  29. {
  30. session_set_save_handler(
  31. 'pwg_session_open',
  32. 'pwg_session_close',
  33. 'pwg_session_read',
  34. 'pwg_session_write',
  35. 'pwg_session_destroy',
  36. 'pwg_session_gc'
  37. );
  38. if (function_exists('ini_set'))
  39. {
  40. ini_set('session.use_cookies', $conf['session_use_cookies']);
  41. ini_set('session.use_only_cookies', $conf['session_use_only_cookies']);
  42. ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid']));
  43. ini_set('session.cookie_httponly', 1);
  44. }
  45. session_name($conf['session_name']);
  46. session_set_cookie_params(0, cookie_path());
  47. register_shutdown_function('session_write_close');
  48. }
  49. /**
  50. * Generates a pseudo random string.
  51. * Characters used are a-z A-Z and numerical values.
  52. *
  53. * @param int $size
  54. * @return string
  55. */
  56. function generate_key($size)
  57. {
  58. include_once(PHPWG_ROOT_PATH.'include/random_compat/random.php');
  59. try
  60. {
  61. $bytes = random_bytes($size+10);
  62. }
  63. catch (Exception $ex)
  64. {
  65. include_once(PHPWG_ROOT_PATH.'include/srand.php');
  66. $bytes = secure_random_bytes($size+10);
  67. }
  68. return substr(
  69. str_replace(
  70. array('+', '/'),
  71. '',
  72. base64_encode($bytes)
  73. ),
  74. 0,
  75. $size
  76. );
  77. }
  78. /**
  79. * Called by PHP session manager, always return true.
  80. *
  81. * @param string $path
  82. * @param sring $name
  83. * @return true
  84. */
  85. function pwg_session_open($path, $name)
  86. {
  87. return true;
  88. }
  89. /**
  90. * Called by PHP session manager, always return true.
  91. *
  92. * @return true
  93. */
  94. function pwg_session_close()
  95. {
  96. return true;
  97. }
  98. /**
  99. * Returns a hash from current user IP
  100. *
  101. * @return string
  102. */
  103. function get_remote_addr_session_hash()
  104. {
  105. global $conf;
  106. if (!$conf['session_use_ip_address'])
  107. {
  108. return '';
  109. }
  110. if (strpos($_SERVER['REMOTE_ADDR'],':')===false)
  111. {//ipv4
  112. return vsprintf(
  113. "%02X%02X",
  114. explode('.',$_SERVER['REMOTE_ADDR'])
  115. );
  116. }
  117. return ''; //ipv6 not yet
  118. }
  119. /**
  120. * Called by PHP session manager, retrieves data stored in the sessions table.
  121. *
  122. * @param string $session_id
  123. * @return string
  124. */
  125. function pwg_session_read($session_id)
  126. {
  127. $query = '
  128. SELECT data
  129. FROM '.SESSIONS_TABLE.'
  130. WHERE id = \''.get_remote_addr_session_hash().$session_id.'\'
  131. ;';
  132. $result = pwg_query($query);
  133. if ($result)
  134. {
  135. $row = pwg_db_fetch_assoc($result);
  136. return $row['data'];
  137. }
  138. else
  139. {
  140. return '';
  141. }
  142. }
  143. /**
  144. * Called by PHP session manager, writes data in the sessions table.
  145. *
  146. * @param string $session_id
  147. * @param sring $data
  148. * @return true
  149. */
  150. function pwg_session_write($session_id, $data)
  151. {
  152. $query = '
  153. REPLACE INTO '.SESSIONS_TABLE.'
  154. (id,data,expiration)
  155. VALUES(\''.get_remote_addr_session_hash().$session_id.'\',\''.pwg_db_real_escape_string($data).'\',now())
  156. ;';
  157. pwg_query($query);
  158. return true;
  159. }
  160. /**
  161. * Called by PHP session manager, deletes data in the sessions table.
  162. *
  163. * @param string $session_id
  164. * @return true
  165. */
  166. function pwg_session_destroy($session_id)
  167. {
  168. $query = '
  169. DELETE
  170. FROM '.SESSIONS_TABLE.'
  171. WHERE id = \''.get_remote_addr_session_hash().$session_id.'\'
  172. ;';
  173. pwg_query($query);
  174. return true;
  175. }
  176. /**
  177. * Called by PHP session manager, garbage collector for expired sessions.
  178. *
  179. * @return true
  180. */
  181. function pwg_session_gc()
  182. {
  183. global $conf;
  184. $query = '
  185. DELETE
  186. FROM '.SESSIONS_TABLE.'
  187. WHERE '.pwg_db_date_to_ts('NOW()').' - '.pwg_db_date_to_ts('expiration').' > '
  188. .$conf['session_length'].'
  189. ;';
  190. pwg_query($query);
  191. return true;
  192. }
  193. /**
  194. * Persistently stores a variable for the current session.
  195. *
  196. * @param string $var
  197. * @param mixed $value
  198. * @return bool
  199. */
  200. function pwg_set_session_var($var, $value)
  201. {
  202. if ( !isset($_SESSION) )
  203. return false;
  204. $_SESSION['pwg_'.$var] = $value;
  205. return true;
  206. }
  207. /**
  208. * Retrieves the value of a persistent variable for the current session.
  209. *
  210. * @param string $var
  211. * @param mixed $default
  212. * @return mixed
  213. */
  214. function pwg_get_session_var($var, $default = null)
  215. {
  216. if (isset( $_SESSION['pwg_'.$var] ) )
  217. {
  218. return $_SESSION['pwg_'.$var];
  219. }
  220. return $default;
  221. }
  222. /**
  223. * Deletes a persistent variable for the current session.
  224. *
  225. * @param string $var
  226. * @return bool
  227. */
  228. function pwg_unset_session_var($var)
  229. {
  230. if ( !isset($_SESSION) )
  231. return false;
  232. unset( $_SESSION['pwg_'.$var] );
  233. return true;
  234. }
  235. /**
  236. * delete all sessions for a given user (certainly deleted)
  237. *
  238. * @since 2.8
  239. * @param int $user_id
  240. * @return null
  241. */
  242. function delete_user_sessions($user_id)
  243. {
  244. $query = '
  245. DELETE
  246. FROM '.SESSIONS_TABLE.'
  247. WHERE data LIKE \'%pwg_uid|i:'.(int)$user_id.';%\'
  248. ;';
  249. pwg_query($query);
  250. }
  251. ?>