Verzeichnisstruktur phpBB-2.0.0


Veröffentlicht
03.04.2002

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

sessions.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 18.34 KiB


001  <?php
002  /***************************************************************************
003   *                                sessions.php
004   *                            -------------------
005   *   begin                : Saturday, Feb 13, 2001
006   *   copyright            : (C) 2001 The phpBB Group
007   *   email                : support@phpbb.com
008   *
009   *   $Id$
010   *
011   *
012   ***************************************************************************/
013   
014  /***************************************************************************
015   *
016   *   This program is free software; you can redistribute it and/or modify
017   *   it under the terms of the GNU General Public License as published by
018   *   the Free Software Foundation; either version 2 of the License, or
019   *   (at your option) any later version.
020   *
021   ***************************************************************************/
022   
023  //
024  // Adds/updates a new session to the database for the given userid.
025  // Returns the new session ID on success.
026  //
027  function session_begin($user_id, $user_ip, $page_id, $auto_create = 0, $enable_autologin = 0, $admin = 0)
028  {
029      global $db, $board_config;
030      global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
031   
032      $cookiename = $board_config['cookie_name'];
033      $cookiepath = $board_config['cookie_path'];
034      $cookiedomain = $board_config['cookie_domain'];
035      $cookiesecure = $board_config['cookie_secure'];
036   
037      if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) )
038      {
039          $session_id = isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
040          $sessiondata = isset($HTTP_COOKIE_VARS[$cookiename . '_data']) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array();
041          $sessionmethod = SESSION_METHOD_COOKIE;
042      }
043      else
044      {
045          $sessiondata = array();
046          $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
047          $sessionmethod = SESSION_METHOD_GET;
048      }
049   
050      //
051      if (!preg_match('/^[A-Za-z0-9]*$/', $session_id)) 
052      {
053          $session_id = '';
054      }
055   
056      $page_id = (int) $page_id;
057   
058      $last_visit = 0;
059      $current_time = time();
060   
061      //
062      // Are auto-logins allowed?
063      // If allow_autologin is not set or is true then they are
064      // (same behaviour as old 2.0.x session code)
065      //
066      if (isset($board_config['allow_autologin']) && !$board_config['allow_autologin'])
067      {
068          $enable_autologin = $sessiondata['autologinid'] = false;
069      }
070   
071      // 
072      // First off attempt to join with the autologin value if we have one
073      // If not, just use the user_id value
074      //
075      $userdata = array();
076   
077      if ($user_id != ANONYMOUS)
078      {
079          if (isset($sessiondata['autologinid']) && (string) $sessiondata['autologinid'] != '' && $user_id)
080          {
081              $sql = 'SELECT u.* 
082                  FROM ' . USERS_TABLE . ' u, ' . SESSIONS_KEYS_TABLE . ' k
083                  WHERE u.user_id = ' . (int) $user_id . "
084                      AND u.user_active = 1
085                      AND k.user_id = u.user_id
086                      AND k.key_id = '" . md5($sessiondata['autologinid']) . "'";
087              if (!($result = $db->sql_query($sql)))
088              {
089                  message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
090              }
091   
092              $userdata = $db->sql_fetchrow($result);
093              $db->sql_freeresult($result);
094          
095              $enable_autologin = $login = 1;
096          }
097          else if (!$auto_create)
098          {
099              $sessiondata['autologinid'] = '';
100              $sessiondata['userid'] = $user_id;
101   
102              $sql = 'SELECT *
103                  FROM ' . USERS_TABLE . '
104                  WHERE user_id = ' . (int) $user_id . '
105                      AND user_active = 1';
106              if (!($result = $db->sql_query($sql)))
107              {
108                  message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
109              }
110   
111              $userdata = $db->sql_fetchrow($result);
112              $db->sql_freeresult($result);
113   
114              $login = 1;
115          }
116      }
117   
118      //
119      // At this point either $userdata should be populated or
120      // one of the below is true
121      // * Key didn't match one in the DB
122      // * User does not exist
123      // * User is inactive
124      //
125      if (!sizeof($userdata) || !is_array($userdata) || !$userdata)
126      {
127          $sessiondata['autologinid'] = '';
128          $sessiondata['userid'] = $user_id = ANONYMOUS;
129          $enable_autologin = $login = 0;
130   
131          $sql = 'SELECT *
132              FROM ' . USERS_TABLE . '
133              WHERE user_id = ' . (int) $user_id;
134          if (!($result = $db->sql_query($sql)))
135          {
136              message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
137          }
138   
139          $userdata = $db->sql_fetchrow($result);
140          $db->sql_freeresult($result);
141      }
142   
143   
144      //
145      // Initial ban check against user id, IP and email address
146      //
147      preg_match('/(..)(..)(..)(..)/', $user_ip, $user_ip_parts);
148   
149      $sql = "SELECT ban_ip, ban_userid, ban_email 
150          FROM " . BANLIST_TABLE . 
151          WHERE ban_ip IN ('" . $user_ip_parts[1] . $user_ip_parts[2] . $user_ip_parts[3] . $user_ip_parts[4] . "', '" . $user_ip_parts[1] . $user_ip_parts[2] . $user_ip_parts[3] . "ff', '" . $user_ip_parts[1] . $user_ip_parts[2] . "ffff', '" . $user_ip_parts[1] . "ffffff')
152              OR ban_userid = $user_id";
153      if ( $user_id != ANONYMOUS )
154      {
155          $sql .= " OR ban_email LIKE '" . str_replace("\'", "''", $userdata['user_email']) . "' 
156              OR ban_email LIKE '" . substr(str_replace("\'", "''", $userdata['user_email']), strpos(str_replace("\'", "''", $userdata['user_email']), "@")) . "'";
157      }
158      if ( !($result = $db->sql_query($sql)) )
159      {
160          message_die(CRITICAL_ERROR, 'Could not obtain ban information', '', __LINE__, __FILE__, $sql);
161      }
162   
163      if ( $ban_info = $db->sql_fetchrow($result) )
164      {
165          if ( $ban_info['ban_ip'] || $ban_info['ban_userid'] || $ban_info['ban_email'] )
166          {
167              message_die(CRITICAL_MESSAGE, 'You_been_banned');
168          }
169      }
170   
171      //
172      // Create or update the session
173      //
174      $sql = "UPDATE " . SESSIONS_TABLE . "
175          SET session_user_id = $user_id, session_start = $current_time, session_time = $current_time, session_page = $page_id, session_logged_in = $login, session_admin = $admin
176          WHERE session_id = '" . $session_id . "
177              AND session_ip = '$user_ip'";
178      if ( !$db->sql_query($sql) || !$db->sql_affectedrows() )
179      {
180          $session_id = md5(dss_rand());
181          $priv_session_id = md5(dss_rand());
182   
183          $sql = "INSERT INTO " . SESSIONS_TABLE . "
184              (session_id, session_user_id, session_start, session_time, session_ip, session_page, session_logged_in, session_admin, priv_session_id)
185              VALUES ('$session_id', $user_id$current_time$current_time, '$user_ip', $page_id$login$admin, '$priv_session_id')";
186          if ( !$db->sql_query($sql) )
187          {
188              message_die(CRITICAL_ERROR, 'Error creating new session', '', __LINE__, __FILE__, $sql);
189          }
190      }
191   
192      if ( $user_id != ANONYMOUS )
193      {
194          $last_visit = ( $userdata['user_session_time'] > 0 ) ? $userdata['user_session_time'] : $current_time; 
195   
196          if (!$admin)
197          {
198              $sql = "UPDATE " . USERS_TABLE . " 
199                  SET user_session_time = $current_time, user_session_page = $page_id, user_lastvisit = $last_visit
200                  WHERE user_id = $user_id";
201              if ( !$db->sql_query($sql) )
202              {
203                  message_die(CRITICAL_ERROR, 'Error updating last visit time', '', __LINE__, __FILE__, $sql);
204              }
205          }
206   
207          $userdata['user_lastvisit'] = $last_visit;
208   
209          //
210          // Regenerate the auto-login key
211          //
212          if ($enable_autologin)
213          {
214              $auto_login_key = dss_rand() . dss_rand();
215              
216              if (isset($sessiondata['autologinid']) && (string) $sessiondata['autologinid'] != '')
217              {
218                  $sql = 'UPDATE ' . SESSIONS_KEYS_TABLE . "
219                      SET last_ip = '$user_ip', key_id = '" . md5($auto_login_key) . "', last_login = $current_time
220                      WHERE key_id = '" . md5($sessiondata['autologinid']) . "'";
221              }
222              else
223              {
224                  $sql = 'INSERT INTO ' . SESSIONS_KEYS_TABLE . "(key_id, user_id, last_ip, last_login)
225                      VALUES ('" . md5($auto_login_key) . "', $user_id, '$user_ip', $current_time)";
226              }
227   
228              if ( !$db->sql_query($sql) )
229              {
230                  message_die(CRITICAL_ERROR, 'Error updating session key', '', __LINE__, __FILE__, $sql);
231              }
232              
233              $sessiondata['autologinid'] = $auto_login_key;
234              unset($auto_login_key);
235          }
236          else
237          {
238              $sessiondata['autologinid'] = '';
239          }
240   
241  //        $sessiondata['autologinid'] = (!$admin) ? (( $enable_autologin && $sessionmethod == SESSION_METHOD_COOKIE ) ? $auto_login_key : '') : $sessiondata['autologinid'];
242          $sessiondata['userid'] = $user_id;
243      }
244   
245      $userdata['session_id'] = $session_id;
246      $userdata['priv_session_id'] = $priv_session_id;
247      $userdata['session_ip'] = $user_ip;
248      $userdata['session_user_id'] = $user_id;
249      $userdata['session_logged_in'] = $login;
250      $userdata['session_page'] = $page_id;
251      $userdata['session_start'] = $current_time;
252      $userdata['session_time'] = $current_time;
253      $userdata['session_admin'] = $admin;
254      $userdata['session_key'] = $sessiondata['autologinid'];
255   
256      setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
257      setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure);
258   
259      $SID = 'sid=' . $session_id;
260   
261      return $userdata;
262  }
263   
264  //
265  // Checks for a given user session, tidies session table and updates user
266  // sessions at each page refresh
267  //
268  function session_pagestart($user_ip, $thispage_id)
269  {
270      global $db, $lang, $board_config;
271      global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID, $P_SID;
272   
273      $cookiename = $board_config['cookie_name'];
274      $cookiepath = $board_config['cookie_path'];
275      $cookiedomain = $board_config['cookie_domain'];
276      $cookiesecure = $board_config['cookie_secure'];
277   
278      $current_time = time();
279      unset($userdata);
280   
281      if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) )
282      {
283          $sessiondata = isset( $HTTP_COOKIE_VARS[$cookiename . '_data'] ) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array();
284          $session_id = isset( $HTTP_COOKIE_VARS[$cookiename . '_sid'] ) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
285          $sessionmethod = SESSION_METHOD_COOKIE;
286      }
287      else
288      {
289          $sessiondata = array();
290          $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
291          $sessionmethod = SESSION_METHOD_GET;
292      }
293   
294      // 
295      if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
296      {
297          $session_id = '';
298      }
299   
300      $thispage_id = (int) $thispage_id;
301   
302      //
303      // Does a session exist?
304      //
305      if ( !empty($session_id) )
306      {
307          //
308          // session_id exists so go ahead and attempt to grab all
309          // data in preparation
310          //
311          $sql = "SELECT u.*, s.*
312              FROM " . SESSIONS_TABLE . " s, " . USERS_TABLE . " u
313              WHERE s.session_id = '$session_id'
314                  AND u.user_id = s.session_user_id";
315          if ( !($result = $db->sql_query($sql)) )
316          {
317              message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
318          }
319   
320          $userdata = $db->sql_fetchrow($result);
321   
322          //
323          // Did the session exist in the DB?
324          //
325          if ( isset($userdata['user_id']) )
326          {
327              //
328              // Do not check IP assuming equivalence, if IPv4 we'll check only first 24
329              // bits ... I've been told (by vHiker) this should alleviate problems with 
330              // load balanced et al proxies while retaining some reliance on IP security.
331              //
332              $ip_check_s = substr($userdata['session_ip'], 0, 6);
333              $ip_check_u = substr($user_ip, 0, 6);
334   
335              if ($ip_check_s == $ip_check_u)
336              {
337                  $SID = ($sessionmethod == SESSION_METHOD_GET || defined('IN_ADMIN')) ? 'sid=' . $session_id : '';
338                   $P_SID = (defined('IN_ADMIN')) ? 'p_sid=' . $userdata['priv_session_id'] : '';
339                  //
340                  // Only update session DB a minute or so after last update
341                  //
342                  if ( $current_time - $userdata['session_time'] > 60 )
343                  {
344                      // A little trick to reset session_admin on session re-usage
345                      $update_admin = (!defined('IN_ADMIN') && $current_time - $userdata['session_time'] > ($board_config['session_length']+60)) ? ', session_admin = 0' : '';
346   
347                      $sql = "UPDATE " . SESSIONS_TABLE . " 
348                          SET session_time = $current_time, session_page = $thispage_id$update_admin
349                          WHERE session_id = '" . $userdata['session_id'] . "'";
350                      if ( !$db->sql_query($sql) )
351                      {
352                          message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql);
353                      }
354   
355                      if ( $userdata['user_id'] != ANONYMOUS )
356                      {
357                          $sql = "UPDATE " . USERS_TABLE . " 
358                              SET user_session_time = $current_time, user_session_page = $thispage_id
359                              WHERE user_id = " . $userdata['user_id'];
360                          if ( !$db->sql_query($sql) )
361                          {
362                              message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql);
363                          }
364                      }
365   
366                      session_clean($userdata['session_id']);
367   
368                      setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
369                      setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure);
370                  }
371   
372                  // Add the session_key to the userdata array if it is set
373                  if ( isset($sessiondata['autologinid']) && $sessiondata['autologinid'] != '' )
374                  {
375                      $userdata['session_key'] = $sessiondata['autologinid'];
376                  }
377   
378                  return $userdata;
379              }
380          }
381      }
382   
383      //
384      // If we reach here then no (valid) session exists. So we'll create a new one,
385      // using the cookie user_id if available to pull basic user prefs.
386      //
387      $user_id = ( isset($sessiondata['userid']) ) ? intval($sessiondata['userid']) : ANONYMOUS;
388   
389      if ( !($userdata = session_begin($user_id, $user_ip, $thispage_id, TRUE)) )
390      {
391          message_die(CRITICAL_ERROR, 'Error creating user session', '', __LINE__, __FILE__, $sql);
392      }
393   
394      return $userdata;
395   
396  }
397   
398  /**
399  * Terminates the specified session
400  * It will delete the entry in the sessions table for this session,
401  * remove the corresponding auto-login key and reset the cookies
402  */
403  function session_end($session_id, $user_id)
404  {
405      global $db, $lang, $board_config, $userdata;
406      global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
407   
408      $cookiename = $board_config['cookie_name'];
409      $cookiepath = $board_config['cookie_path'];
410      $cookiedomain = $board_config['cookie_domain'];
411      $cookiesecure = $board_config['cookie_secure'];
412   
413      $current_time = time();
414   
415      if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
416      {
417          return;
418      }
419      
420      //
421      // Delete existing session
422      //
423      $sql = 'DELETE FROM ' . SESSIONS_TABLE . " 
424          WHERE session_id = '$session_id
425              AND session_user_id = $user_id";
426      if ( !$db->sql_query($sql) )
427      {
428          message_die(CRITICAL_ERROR, 'Error removing user session', '', __LINE__, __FILE__, $sql);
429      }
430   
431      //
432      // Remove this auto-login entry (if applicable)
433      //
434      if ( isset($userdata['session_key']) && $userdata['session_key'] != '' )
435      {
436          $autologin_key = md5($userdata['session_key']);
437          $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
438              WHERE user_id = ' . (int) $user_id . "
439                  AND key_id = '$autologin_key'";
440          if ( !$db->sql_query($sql) )
441          {
442              message_die(CRITICAL_ERROR, 'Error removing auto-login key', '', __LINE__, __FILE__, $sql);
443          }
444      }
445   
446      //
447      // We expect that message_die will be called after this function,
448      // but just in case it isn't, reset $userdata to the details for a guest
449      //
450      $sql = 'SELECT *
451          FROM ' . USERS_TABLE . '
452          WHERE user_id = ' . ANONYMOUS;
453      if ( !($result = $db->sql_query($sql)) )
454      {
455          message_die(CRITICAL_ERROR, 'Error obtaining user details', '', __LINE__, __FILE__, $sql);
456      }
457      if ( !($userdata = $db->sql_fetchrow($result)) )
458      {
459          message_die(CRITICAL_ERROR, 'Error obtaining user details', '', __LINE__, __FILE__, $sql);
460      }
461      $db->sql_freeresult($result);
462   
463   
464      setcookie($cookiename . '_data', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure);
465      setcookie($cookiename . '_sid', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure);
466   
467      return true;
468  }
469   
470  /**
471  * Removes expired sessions and auto-login keys from the database
472  */
473  function session_clean($session_id)
474  {
475      global $board_config, $db;
476   
477      //
478      // Delete expired sessions
479      //
480      $sql = 'DELETE FROM ' . SESSIONS_TABLE . 
481          WHERE session_time < ' . (time() - (int) $board_config['session_length']) . " 
482              AND session_id <> '$session_id'";
483      if ( !$db->sql_query($sql) )
484      {
485          message_die(CRITICAL_ERROR, 'Error clearing sessions table', '', __LINE__, __FILE__, $sql);
486      }
487   
488      //
489      // Delete expired auto-login keys
490      // If max_autologin_time is not set then keys will never be deleted
491      // (same behaviour as old 2.0.x session code)
492      //
493      if (!empty($board_config['max_autologin_time']) && $board_config['max_autologin_time'] > 0)
494      {
495          $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
496              WHERE last_login < ' . (time() - (86400 * (int) $board_config['max_autologin_time']));
497          $db->sql_query($sql);
498      }
499   
500      return true;
501  }
502   
503  /**
504  * Reset all login keys for the specified user
505  * Called on password changes
506  */
507  function session_reset_keys($user_id, $user_ip)
508  {
509      global $db, $userdata, $board_config;
510   
511      $key_sql = ($user_id == $userdata['user_id'] && !empty($userdata['session_key'])) ? "AND key_id != '" . md5($userdata['session_key']) . "'" : '';
512   
513      $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
514          WHERE user_id = ' . (int) $user_id . "
515              $key_sql";
516   
517      if ( !$db->sql_query($sql) )
518      {
519          message_die(CRITICAL_ERROR, 'Error removing auto-login keys', '', __LINE__, __FILE__, $sql);
520      }
521   
522      $where_sql = 'session_user_id = ' . (int) $user_id;
523      $where_sql .= ($user_id == $userdata['user_id']) ? " AND session_id <> '" . $userdata['session_id'] . "'" : '';
524      $sql = 'DELETE FROM ' . SESSIONS_TABLE . "
525          WHERE $where_sql";
526      if ( !$db->sql_query($sql) )
527      {
528          message_die(CRITICAL_ERROR, 'Error removing user session(s)', '', __LINE__, __FILE__, $sql);
529      }
530   
531      if ( !empty($key_sql) )
532      {
533          $auto_login_key = dss_rand() . dss_rand();
534   
535          $current_time = time();
536          
537          $sql = 'UPDATE ' . SESSIONS_KEYS_TABLE . "
538              SET last_ip = '$user_ip', key_id = '" . md5($auto_login_key) . "', last_login = $current_time
539              WHERE key_id = '" . md5($userdata['session_key']) . "'";
540          
541          if ( !$db->sql_query($sql) )
542          {
543              message_die(CRITICAL_ERROR, 'Error updating session key', '', __LINE__, __FILE__, $sql);
544          }
545   
546          // And now rebuild the cookie
547          $sessiondata['userid'] = $user_id;
548          $sessiondata['autologinid'] = $auto_login_key;
549          $cookiename = $board_config['cookie_name'];
550          $cookiepath = $board_config['cookie_path'];
551          $cookiedomain = $board_config['cookie_domain'];
552          $cookiesecure = $board_config['cookie_secure'];
553   
554          setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
555          
556          $userdata['session_key'] = $auto_login_key;
557          unset($sessiondata);
558          unset($auto_login_key);
559      }
560  }
561   
562  //
563  // Append $SID to a url. Borrowed from phplib and modified. This is an
564  // extra routine utilised by the session code above and acts as a wrapper
565  // around every single URL and form action. If you replace the session
566  // code you must include this routine, even if it's empty.
567  //
568  function append_sid($url, $non_html_amp = false)
569  {
570      global $SID, $P_SID;
571   
572      if ( !empty($SID) && !preg_match('#sid=#', $url) )
573      {
574          $url .= ( ( strpos($url, '?') !== false ) ?  ( ( $non_html_amp ) ? '&' : '&amp;' ) : '?' ) . $SID;
575      }
576      if ( !empty($P_SID) && !preg_match('#p_sid=#', $url) )
577      {
578          $url .= ( ( strpos($url, '?') !== false ) ?  ( ( $non_html_amp ) ? '&' : '&amp;' ) : '?' ) . $P_SID;
579      }
580   
581      return $url;
582  }
583   
584  ?>