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

Last change on this file since 1750 was 1750, checked in by rvelices, 17 years ago
  • plugins with own independent scripts work now (cookie_path and url root are

correct)

  • prepare a bit some url functions so that later we can fully embed pwg in

scripts located outside pwg

  • remove some unnecessary language strings
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 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-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2007-01-24 05:07:08 +0000 (Wed, 24 Jan 2007) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1750 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28// The function generate_key creates a string with pseudo random characters.
29// the size of the string depends on the $conf['session_id_size'].
30// Characters used are a-z A-Z and numerical values. Examples :
31//                    "Er4Tgh6", "Rrp08P", "54gj"
32// input  : none (using global variable)
33// output : $key
34function generate_key($size)
35{
36  global $conf;
37
38  $md5 = md5(substr(microtime(), 2, 6));
39  $init = '';
40  for ( $i = 0; $i < strlen( $md5 ); $i++ )
41  {
42    if ( is_numeric( $md5[$i] ) ) $init.= $md5[$i];
43  }
44  $init = substr( $init, 0, 8 );
45  mt_srand( $init );
46  $key = '';
47  for ( $i = 0; $i < $size; $i++ )
48  {
49    $c = mt_rand( 0, 2 );
50    if ( $c == 0 )      $key .= chr( mt_rand( 65, 90 ) );
51    else if ( $c == 1 ) $key .= chr( mt_rand( 97, 122 ) );
52    else                $key .= mt_rand( 0, 9 );
53  }
54  return $key;
55}
56
57if (isset($conf['session_save_handler'])
58  and ($conf['session_save_handler'] == 'db')
59  and defined('PHPWG_INSTALLED'))
60{
61  session_set_save_handler('pwg_session_open',
62    'pwg_session_close',
63    'pwg_session_read',
64    'pwg_session_write',
65    'pwg_session_destroy',
66    'pwg_session_gc'
67  );
68  if ( function_exists('ini_set') )
69  {
70    ini_set('session.use_cookies', $conf['session_use_cookies']);
71    ini_set('session.use_only_cookies', $conf['session_use_only_cookies']);
72    ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid']));
73  }
74  session_name($conf['session_name']);
75  session_set_cookie_params(0, cookie_path());
76}
77
78// cookie_path returns the path to use for the PhpWebGallery cookie.
79// If PhpWebGallery is installed on :
80// http://domain.org/meeting/gallery/category.php
81// cookie_path will return : "/meeting/gallery"
82function cookie_path()
83{
84  if ( isset($_SERVER['REDIRECT_SCRIPT_NAME']) and
85       !empty($_SERVER['REDIRECT_SCRIPT_NAME']) )
86  {
87    $scr = $_SERVER['REDIRECT_SCRIPT_NAME'];
88  }
89  else if ( isset($_SERVER['REDIRECT_URL']) )
90  { // mod_rewrite is activated for upper level directories. we must set the
91    // cookie to the path shown in the browser otherwise it will be discarded.
92    if ( isset($_SERVER['PATH_INFO']) and !empty($_SERVER['PATH_INFO']) )
93    {
94      $idx = strpos( $_SERVER['REDIRECT_URL'], $_SERVER['PATH_INFO'] );
95      if ($idx !== false)
96      {
97        $scr = substr($_SERVER['REDIRECT_URL'], 0, $idx);
98      }
99      else
100      {//this should never happen
101        $scr='//';
102      }
103    }
104    else
105    {
106      $scr = $_SERVER['REDIRECT_URL'];
107    }
108  }
109  else
110  {
111    $scr = $_SERVER['SCRIPT_NAME'];
112  }
113  $scr = substr($scr,0,strrpos( $scr,'/'));
114
115  // add a trailing '/' if needed
116  $scr .= ($scr{strlen($scr)-1} == '/') ? '' : '/';
117 
118  if ( substr(PHPWG_ROOT_PATH,0,3)=='../')
119  { // this is maybe a plugin inside pwg directory
120    // TODO - what if it is an external script outside PWG ?
121    $scr = $scr.PHPWG_ROOT_PATH;
122    while (1)
123    {
124      $new = preg_replace('#[^/]+/\.\.(/|$)#', '', $scr);
125      if ($new==$scr)
126      {
127        break;
128      }
129      $scr=$new;
130    }
131  }
132  return $scr;
133}
134
135/**
136 * returns true; used when the session_start() function is called
137 *
138 * @params not use but useful for php engine
139 */
140function pwg_session_open($path, $name)
141{
142  return true;
143}
144
145/**
146 * returns true; used when the session is closed (unset($_SESSION))
147 *
148 */
149function pwg_session_close()
150{
151  return true;
152}
153
154/**
155 * this function returns
156 * a string corresponding to the value of the variable save in the session
157 * or an empty string when the variable doesn't exist
158 *
159 * @param string session id
160 */
161function pwg_session_read($session_id)
162{
163  $query = '
164SELECT data
165  FROM '.SESSIONS_TABLE.'
166  WHERE id = \''.$session_id.'\'
167;';
168  $result = pwg_query($query);
169  if ($result)
170  {
171    $row = mysql_fetch_assoc($result);
172    return $row['data'];
173  }
174  else
175  {
176    return '';
177  }
178}
179
180/**
181 * returns true; writes set a variable in the active session
182 *
183 * @param string session id
184 * @data string value of date to be saved
185 */
186function pwg_session_write($session_id, $data)
187{
188  $query = '
189UPDATE '.SESSIONS_TABLE.'
190  SET expiration = now(),
191  data = \''.$data.'\'
192  WHERE id = \''.$session_id.'\'
193;';
194  pwg_query($query);
195  if ( mysql_affected_rows()>0 )
196  {
197    return true;
198  }
199  $query = '
200INSERT INTO '.SESSIONS_TABLE.'
201  (id,data,expiration)
202  VALUES(\''.$session_id.'\',\''.$data.'\',now())
203;';
204  mysql_query($query);
205  return true;
206}
207
208/**
209 * returns true; delete the active session
210 *
211 * @param string session id
212 */
213function pwg_session_destroy($session_id)
214{
215  $query = '
216DELETE
217  FROM '.SESSIONS_TABLE.'
218  WHERE id = \''.$session_id.'\'
219;';
220  pwg_query($query);
221  return true;
222}
223
224/**
225 * returns true; delete expired sessions
226 * called each time a session is closed.
227 */
228function pwg_session_gc()
229{
230  global $conf;
231
232  $query = '
233DELETE
234  FROM '.SESSIONS_TABLE.'
235  WHERE UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(expiration) > '
236  .$conf['session_length'].'
237;';
238  pwg_query($query);
239  return true;
240}
241
242
243/**
244 * persistently stores a variable for the current session
245 * currently we use standard php sessions but it might change
246 * @return boolean true on success
247 * @see pwg_get_session_var, pwg_unset_session_var
248 */
249function pwg_set_session_var($var, $value)
250{
251  if ( !isset($_SESSION) )
252    return false;
253  $_SESSION['pwg_'.$var] = $value;
254  return true;
255}
256
257/**
258 * retrieves the value of a persistent variable for the current session
259 * currently we use standard php sessions but it might change
260 * @return mixed
261 * @see pwg_set_session_var, pwg_unset_session_var
262 */
263function pwg_get_session_var($var, $default = null)
264{
265  if (isset( $_SESSION['pwg_'.$var] ) )
266  {
267    return $_SESSION['pwg_'.$var];
268  }
269  return $default;
270}
271
272/**
273 * deletes a persistent variable for the current session
274 * currently we use standard php sessions but it might change
275 * @return boolean true on success
276 * @see pwg_set_session_var, pwg_get_session_var
277 */
278function pwg_unset_session_var($var)
279{
280  if ( !isset($_SESSION) )
281    return false;
282  unset( $_SESSION['pwg_'.$var] );
283  return true;
284}
285
286?>
Note: See TracBrowser for help on using the repository browser.