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

Last change on this file since 28571 was 28571, checked in by rvelices, 10 years ago

bug 3082: random key generation algorithm

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