source: trunk/include/functions_session.inc.php @ 27558

Last change on this file since 27558 was 26461, checked in by mistic100, 10 years ago

Update headers to 2014. Happy new year!!

  • Property svn:eol-style set to LF
File size: 6.0 KB
RevLine 
[2]1<?php
[362]2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[2]23
[25614]24/**
25 * @package functions\session
26 */
[1013]27
28
[1063]29if (isset($conf['session_save_handler'])
[1013]30  and ($conf['session_save_handler'] == 'db')
[1063]31  and defined('PHPWG_INSTALLED'))
[1007]32{
[25614]33  session_set_save_handler(
34    'pwg_session_open',
[1007]35    'pwg_session_close',
36    'pwg_session_read',
37    'pwg_session_write',
38    'pwg_session_destroy',
39    'pwg_session_gc'
40  );
[25614]41
42  if (function_exists('ini_set'))
[1217]43  {
44    ini_set('session.use_cookies', $conf['session_use_cookies']);
45    ini_set('session.use_only_cookies', $conf['session_use_only_cookies']);
46    ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid']));
[2757]47    ini_set('session.cookie_httponly', 1);
[1217]48  }
[25614]49
[1493]50  session_name($conf['session_name']);
51  session_set_cookie_params(0, cookie_path());
[4781]52  register_shutdown_function('session_write_close');
[1004]53}
54
[25614]55
[1010]56/**
[25614]57 * Generates a pseudo random string.
58 * Characters used are a-z A-Z and numerical values.
[1010]59 *
[25614]60 * @param int $size
61 * @return string
[1010]62 */
[25614]63function generate_key($size)
64{
65  global $conf;
66
67  $md5 = md5(substr(microtime(), 2, 6));
68  $init = '';
69  for ( $i = 0; $i < strlen( $md5 ); $i++ )
70  {
71    if ( is_numeric( $md5[$i] ) ) $init.= $md5[$i];
72  }
73  $init = substr( $init, 0, 8 );
74  mt_srand( $init );
75  $key = '';
76  for ( $i = 0; $i < $size; $i++ )
77  {
78    $c = mt_rand( 0, 2 );
79    if ( $c == 0 )      $key .= chr( mt_rand( 65, 90 ) );
80    else if ( $c == 1 ) $key .= chr( mt_rand( 97, 122 ) );
81    else                $key .= mt_rand( 0, 9 );
82  }
83  return $key;
84}
85
86/**
87 * Called by PHP session manager, always return true.
88 *
89 * @param string $path
90 * @param sring $name
91 * @return true
92 */
[1063]93function pwg_session_open($path, $name)
[2]94{
[1004]95  return true;
96}
[45]97
[1010]98/**
[25614]99 * Called by PHP session manager, always return true.
[1010]100 *
[25614]101 * @return true
[1010]102 */
[1063]103function pwg_session_close()
[1004]104{
105  return true;
[2]106}
[45]107
[25614]108/**
109 * Returns a hash from current user IP
110 *
111 * @return string
112 */
[2521]113function get_remote_addr_session_hash()
114{
[18850]115  global $conf;
116
117  if (!$conf['session_use_ip_address'])
118  {
119    return '';
120  }
121 
[12119]122  if (strpos($_SERVER['REMOTE_ADDR'],':')===false)
123  {//ipv4
124    return vsprintf(
125      "%02X%02X",
126      explode('.',$_SERVER['REMOTE_ADDR'])
127    );
128  }
129  return ''; //ipv6 not yet
130}
[3166]131
[1010]132/**
[25614]133 * Called by PHP session manager, retrieves data stored in the sessions table.
[1063]134 *
[25614]135 * @param string $session_id
136 * @return string
[1010]137 */
[1063]138function pwg_session_read($session_id)
[2]139{
[1007]140  $query = '
[1063]141SELECT data
[1010]142  FROM '.SESSIONS_TABLE.'
[2521]143  WHERE id = \''.get_remote_addr_session_hash().$session_id.'\'
[1010]144;';
[1004]145  $result = pwg_query($query);
[1063]146  if ($result)
[1007]147  {
[4325]148    $row = pwg_db_fetch_assoc($result);
[1004]149    return $row['data'];
[1063]150  }
151  else
[1007]152  {
[1004]153    return '';
[2]154  }
155}
156
[1010]157/**
[25614]158 * Called by PHP session manager, writes data in the sessions table.
[1063]159 *
[25614]160 * @param string $session_id
161 * @param sring $data
162 * @return true
[1010]163 */
[1063]164function pwg_session_write($session_id, $data)
[2]165{
[1007]166  $query = '
[2900]167REPLACE INTO '.SESSIONS_TABLE.'
[1010]168  (id,data,expiration)
[20281]169  VALUES(\''.get_remote_addr_session_hash().$session_id.'\',\''.pwg_db_real_escape_string($data).'\',now())
[1010]170;';
[2884]171  pwg_query($query);
[1004]172  return true;
173}
[808]174
[1010]175/**
[25614]176 * Called by PHP session manager, deletes data in the sessions table.
[1063]177 *
[25614]178 * @param string $session_id
179 * @return true
[1010]180 */
[1063]181function pwg_session_destroy($session_id)
[1004]182{
[1007]183  $query = '
[1063]184DELETE
[1010]185  FROM '.SESSIONS_TABLE.'
[2521]186  WHERE id = \''.get_remote_addr_session_hash().$session_id.'\'
[1010]187;';
[1004]188  pwg_query($query);
189  return true;
[2]190}
[45]191
[1010]192/**
[25614]193 * Called by PHP session manager, garbage collector for expired sessions.
194 *
195 * @return true
[1010]196 */
[1063]197function pwg_session_gc()
[45]198{
[1004]199  global $conf;
200
[1007]201  $query = '
[1063]202DELETE
[1010]203  FROM '.SESSIONS_TABLE.'
[6666]204  WHERE '.pwg_db_date_to_ts('NOW()').' - '.pwg_db_date_to_ts('expiration').' > '
[1010]205  .$conf['session_length'].'
206;';
[1004]207  pwg_query($query);
208  return true;
[45]209}
[1623]210
211/**
[25614]212 * Persistently stores a variable for the current session.
213 *
214 * @param string $var
215 * @param mixed $value
216 * @return bool
[1623]217 */
218function pwg_set_session_var($var, $value)
219{
220  if ( !isset($_SESSION) )
221    return false;
222  $_SESSION['pwg_'.$var] = $value;
223  return true;
224}
225
226/**
[25614]227 * Retrieves the value of a persistent variable for the current session.
228 *
229 * @param string $var
230 * @param mixed $default
[1623]231 * @return mixed
232 */
233function pwg_get_session_var($var, $default = null)
234{
235  if (isset( $_SESSION['pwg_'.$var] ) )
236  {
237    return $_SESSION['pwg_'.$var];
238  }
239  return $default;
240}
241
242/**
[25614]243 * Deletes a persistent variable for the current session.
244 *
245 * @param string $var
246 * @return bool
[1623]247 */
248function pwg_unset_session_var($var)
249{
250  if ( !isset($_SESSION) )
251    return false;
252  unset( $_SESSION['pwg_'.$var] );
253  return true;
254}
255
[25614]256?>
Note: See TracBrowser for help on using the repository browser.