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

Last change on this file since 541 was 541, checked in by z0rglub, 20 years ago
  • deletion of session_time and session_id_size as config parameter
  • new feature : "remember me" creates a long time cookie
  • possibility to set the default authentication method to URI or cookie
  • really technical parameters (session identifier size, session duration) are set in the config file and not in database + configuration.php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                       functions_session.inc.php                       |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-10-02 23:12:50 +0000 (Sat, 02 Oct 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 541 $
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
57/**
58 * create a new session and returns the session identifier
59 *
60 * - find a non-already-used session key
61 * - create a session in database
62 * - return session identifier
63 *
64 * @param int userid
65 * @param string method : cookie or URI
66 * @param int session_lentgh : in seconds
67 * @return string
68 */
69function session_create($userid, $method, $session_length)
70{
71  global $conf;
72
73  // 1. searching an unused session key
74  $id_found = false;
75  while (!$id_found)
76  {
77    $generated_id = generate_key($conf['session_id_size_'.$method]);
78    $query = '
79SELECT id
80  FROM '.SESSIONS_TABLE.'
81  WHERE id = \''.$generated_id.'\'
82;';
83    $result = mysql_query($query);
84    if (mysql_num_rows($result) == 0)
85    {
86      $id_found = true;
87    }
88  }
89  // 3. inserting session in database
90  $expiration = $session_length + time();
91  $query = '
92INSERT INTO '.SESSIONS_TABLE.'
93  (id,user_id,expiration,ip)
94  VALUES
95  (\''.$generated_id.'\','.$userid.','.$expiration.',
96   \''.$_SERVER['REMOTE_ADDR'].'\')
97;';
98  mysql_query($query);
99
100  if ($method == 'cookie')
101  {
102    setcookie('id', $generated_id, $session_length+time(), cookie_path());
103  }
104               
105  return $generated_id;
106}
107
108// add_session_id adds the id of the session to the string given in
109// parameter as $url. If the session id is the first parameter to the url,
110// it is preceded by a '?', else it is preceded by a '&amp;'. If the
111// parameter $redirect is set to true, '&' is used instead of '&'.
112function add_session_id( $url, $redirect = false )
113{
114  global $page, $user;
115
116  if ( $user['has_cookie'] ) return $url;
117
118  $amp = '&amp;';
119  if ( $redirect )
120  {
121    $amp = '&';
122  }
123  if ( !$user['is_the_guest'] )
124  {
125    if ( preg_match( '/\.php\?/',$url ) )
126    {
127      return $url.$amp.'id='.$page['session_id'];
128    }
129    else
130    {
131      return $url.'?id='.$page['session_id'];
132    }
133  }
134  else
135  {
136    return $url;
137  }
138}
139
140// cookie_path returns the path to use for the PhpWebGallery cookie.
141// If PhpWebGallery is installed on :
142// http://domain.org/meeting/gallery/category.php
143// cookie_path will return : "/meeting/gallery"
144function cookie_path()
145{
146  return substr($_SERVER['PHP_SELF'],0,strrpos( $_SERVER['PHP_SELF'],'/'));
147}
148?>
Note: See TracBrowser for help on using the repository browser.