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

Last change on this file since 2297 was 2297, checked in by plg, 17 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | file          : $Id: functions_session.inc.php 2297 2008-04-04 22:57:23Z plg $
29// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
30// | last modifier : $Author: plg $
31// | revision      : $Revision: 2297 $
32// +-----------------------------------------------------------------------+
33// | This program is free software; you can redistribute it and/or modify  |
34// | it under the terms of the GNU General Public License as published by  |
35// | the Free Software Foundation                                          |
36// |                                                                       |
37// | This program is distributed in the hope that it will be useful, but   |
38// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
39// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
40// | General Public License for more details.                              |
41// |                                                                       |
42// | You should have received a copy of the GNU General Public License     |
43// | along with this program; if not, write to the Free Software           |
44// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
45// | USA.                                                                  |
46// +-----------------------------------------------------------------------+
47
48// The function generate_key creates a string with pseudo random characters.
49// the size of the string depends on the $conf['session_id_size'].
50// Characters used are a-z A-Z and numerical values. Examples :
51//                    "Er4Tgh6", "Rrp08P", "54gj"
52// input  : none (using global variable)
53// output : $key
54function generate_key($size)
55{
56  global $conf;
57
58  $md5 = md5(substr(microtime(), 2, 6));
59  $init = '';
60  for ( $i = 0; $i < strlen( $md5 ); $i++ )
61  {
62    if ( is_numeric( $md5[$i] ) ) $init.= $md5[$i];
63  }
64  $init = substr( $init, 0, 8 );
65  mt_srand( $init );
66  $key = '';
67  for ( $i = 0; $i < $size; $i++ )
68  {
69    $c = mt_rand( 0, 2 );
70    if ( $c == 0 )      $key .= chr( mt_rand( 65, 90 ) );
71    else if ( $c == 1 ) $key .= chr( mt_rand( 97, 122 ) );
72    else                $key .= mt_rand( 0, 9 );
73  }
74  return $key;
75}
76
77if (isset($conf['session_save_handler'])
78  and ($conf['session_save_handler'] == 'db')
79  and defined('PHPWG_INSTALLED'))
80{
81  session_set_save_handler('pwg_session_open',
82    'pwg_session_close',
83    'pwg_session_read',
84    'pwg_session_write',
85    'pwg_session_destroy',
86    'pwg_session_gc'
87  );
88  if ( function_exists('ini_set') )
89  {
90    ini_set('session.use_cookies', $conf['session_use_cookies']);
91    ini_set('session.use_only_cookies', $conf['session_use_only_cookies']);
92    ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid']));
93  }
94  session_name($conf['session_name']);
95  session_set_cookie_params(0, cookie_path());
96}
97
98/**
99 * returns true; used when the session_start() function is called
100 *
101 * @params not use but useful for php engine
102 */
103function pwg_session_open($path, $name)
104{
105  return true;
106}
107
108/**
109 * returns true; used when the session is closed (unset($_SESSION))
110 *
111 */
112function pwg_session_close()
113{
114  return true;
115}
116
117/**
118 * this function returns
119 * a string corresponding to the value of the variable save in the session
120 * or an empty string when the variable doesn't exist
121 *
122 * @param string session id
123 */
124function pwg_session_read($session_id)
125{
126  $query = '
127SELECT data
128  FROM '.SESSIONS_TABLE.'
129  WHERE id = \''.$session_id.'\'
130;';
131  $result = pwg_query($query);
132  if ($result)
133  {
134    $row = mysql_fetch_assoc($result);
135    return $row['data'];
136  }
137  else
138  {
139    return '';
140  }
141}
142
143/**
144 * returns true; writes set a variable in the active session
145 *
146 * @param string session id
147 * @data string value of date to be saved
148 */
149function pwg_session_write($session_id, $data)
150{
151  $query = '
152UPDATE '.SESSIONS_TABLE.'
153  SET expiration = now(),
154  data = \''.$data.'\'
155  WHERE id = \''.$session_id.'\'
156;';
157  pwg_query($query);
158  if ( mysql_affected_rows()>0 )
159  {
160    return true;
161  }
162  $query = '
163INSERT INTO '.SESSIONS_TABLE.'
164  (id,data,expiration)
165  VALUES(\''.$session_id.'\',\''.$data.'\',now())
166;';
167  mysql_query($query);
168  return true;
169}
170
171/**
172 * returns true; delete the active session
173 *
174 * @param string session id
175 */
176function pwg_session_destroy($session_id)
177{
178  $query = '
179DELETE
180  FROM '.SESSIONS_TABLE.'
181  WHERE id = \''.$session_id.'\'
182;';
183  pwg_query($query);
184  return true;
185}
186
187/**
188 * returns true; delete expired sessions
189 * called each time a session is closed.
190 */
191function pwg_session_gc()
192{
193  global $conf;
194
195  $query = '
196DELETE
197  FROM '.SESSIONS_TABLE.'
198  WHERE UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(expiration) > '
199  .$conf['session_length'].'
200;';
201  pwg_query($query);
202  return true;
203}
204
205
206/**
207 * persistently stores a variable for the current session
208 * currently we use standard php sessions but it might change
209 * @return boolean true on success
210 * @see pwg_get_session_var, pwg_unset_session_var
211 */
212function pwg_set_session_var($var, $value)
213{
214  if ( !isset($_SESSION) )
215    return false;
216  $_SESSION['pwg_'.$var] = $value;
217  return true;
218}
219
220/**
221 * retrieves the value of a persistent variable for the current session
222 * currently we use standard php sessions but it might change
223 * @return mixed
224 * @see pwg_set_session_var, pwg_unset_session_var
225 */
226function pwg_get_session_var($var, $default = null)
227{
228  if (isset( $_SESSION['pwg_'.$var] ) )
229  {
230    return $_SESSION['pwg_'.$var];
231  }
232  return $default;
233}
234
235/**
236 * deletes a persistent variable for the current session
237 * currently we use standard php sessions but it might change
238 * @return boolean true on success
239 * @see pwg_set_session_var, pwg_get_session_var
240 */
241function pwg_unset_session_var($var)
242{
243  if ( !isset($_SESSION) )
244    return false;
245  unset( $_SESSION['pwg_'.$var] );
246  return true;
247}
248
249?>
Note: See TracBrowser for help on using the repository browser.