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

Last change on this file since 2178 was 1992, checked in by rub, 17 years ago

Issue 0000684: History [Search] - Add a thumbnail display

o Display choice can be selected
o Display choice is saved on on cookie
o Small improvement picture link (hoverbox on all the link, alt&title on classic mode)
o New cookie functions and use

Enhance computing method of script_basename function.

http://forum.phpwebgallery.net/viewtopic.php?pid=58258#p58258

Merge BSF 1988:1989 into branch-1_7

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