home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2006 January / PCA126_DVD.iso / ADVISORS / phpBB-2.0.17 / phpBB2 / includes / sessions.php < prev    next >
Encoding:
PHP Script  |  2005-07-19  |  13.4 KB  |  424 lines

  1. <?php
  2. /***************************************************************************
  3.  *                                sessions.php
  4.  *                            -------------------
  5.  *   begin                : Saturday, Feb 13, 2001
  6.  *   copyright            : (C) 2001 The phpBB Group
  7.  *   email                : support@phpbb.com
  8.  *
  9.  *   $Id: sessions.php,v 1.58.2.14 2005/05/06 20:50:11 acydburn Exp $
  10.  *
  11.  *
  12.  ***************************************************************************/
  13.  
  14. /***************************************************************************
  15.  *
  16.  *   This program is free software; you can redistribute it and/or modify
  17.  *   it under the terms of the GNU General Public License as published by
  18.  *   the Free Software Foundation; either version 2 of the License, or
  19.  *   (at your option) any later version.
  20.  *
  21.  ***************************************************************************/
  22.  
  23. //
  24. // Adds/updates a new session to the database for the given userid.
  25. // Returns the new session ID on success.
  26. //
  27. function session_begin($user_id, $user_ip, $page_id, $auto_create = 0, $enable_autologin = 0, $admin = 0)
  28. {
  29.     global $db, $board_config;
  30.     global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
  31.  
  32.     $cookiename = $board_config['cookie_name'];
  33.     $cookiepath = $board_config['cookie_path'];
  34.     $cookiedomain = $board_config['cookie_domain'];
  35.     $cookiesecure = $board_config['cookie_secure'];
  36.  
  37.     if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) )
  38.     {
  39.         $session_id = isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
  40.         $sessiondata = isset($HTTP_COOKIE_VARS[$cookiename . '_data']) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array();
  41.         $sessionmethod = SESSION_METHOD_COOKIE;
  42.     }
  43.     else
  44.     {
  45.         $sessiondata = array();
  46.         $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
  47.         $sessionmethod = SESSION_METHOD_GET;
  48.     }
  49.  
  50.     //
  51.     if (!preg_match('/^[A-Za-z0-9]*$/', $session_id)) 
  52.     {
  53.         $session_id = '';
  54.     }
  55.  
  56.     $page_id = (int) $page_id;
  57.  
  58.     $last_visit = 0;
  59.     $current_time = time();
  60.     $expiry_time = $current_time - $board_config['session_length'];
  61.  
  62.     //
  63.     // Try and pull the last time stored in a cookie, if it exists
  64.     //
  65.     $sql = "SELECT * 
  66.         FROM " . USERS_TABLE . " 
  67.         WHERE user_id = $user_id";
  68.     if ( !($result = $db->sql_query($sql)) )
  69.     {
  70.         message_die(CRITICAL_ERROR, 'Could not obtain lastvisit data from user table', '', __LINE__, __FILE__, $sql);
  71.     }
  72.  
  73.     $userdata = $db->sql_fetchrow($result);
  74.  
  75.     if ( $user_id != ANONYMOUS )
  76.     {
  77.         $auto_login_key = $userdata['user_password'];
  78.  
  79.         if ( $auto_create )
  80.         {
  81.             if ( isset($sessiondata['autologinid']) && $userdata['user_active'] )
  82.             {
  83.                 // We have to login automagically
  84.                 if( $sessiondata['autologinid'] === $auto_login_key )
  85.                 {
  86.                     // autologinid matches password
  87.                     $login = 1;
  88.                     $enable_autologin = 1;
  89.                 }
  90.                 else
  91.                 {
  92.                     // No match; don't login, set as anonymous user
  93.                     $login = 0; 
  94.                     $enable_autologin = 0; 
  95.                     $user_id = $userdata['user_id'] = ANONYMOUS;
  96.                 
  97.                     $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . ANONYMOUS;
  98.                     $result = $db->sql_query($sql);
  99.                     $userdata = $db->sql_fetchrow($result);
  100.                     $db->sql_freeresult($result);
  101.                 }
  102.             }
  103.             else
  104.             {
  105.                 // Autologin is not set. Don't login, set as anonymous user
  106.                 $login = 0;
  107.                 $enable_autologin = 0;
  108.                 $user_id = $userdata['user_id'] = ANONYMOUS;
  109.  
  110.                 $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . ANONYMOUS;
  111.                 $result = $db->sql_query($sql);
  112.                 $userdata = $db->sql_fetchrow($result);
  113.                 $db->sql_freeresult($result);
  114.             }
  115.         }
  116.         else
  117.         {
  118.             $login = 1;
  119.         }
  120.     }
  121.     else
  122.     {
  123.         $login = 0;
  124.         $enable_autologin = 0;
  125.     }
  126.  
  127.     //
  128.     // Initial ban check against user id, IP and email address
  129.     //
  130.     preg_match('/(..)(..)(..)(..)/', $user_ip, $user_ip_parts);
  131.  
  132.     $sql = "SELECT ban_ip, ban_userid, ban_email 
  133.         FROM " . BANLIST_TABLE . " 
  134.         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')
  135.             OR ban_userid = $user_id";
  136.     if ( $user_id != ANONYMOUS )
  137.     {
  138.         $sql .= " OR ban_email LIKE '" . str_replace("\'", "''", $userdata['user_email']) . "' 
  139.             OR ban_email LIKE '" . substr(str_replace("\'", "''", $userdata['user_email']), strpos(str_replace("\'", "''", $userdata['user_email']), "@")) . "'";
  140.     }
  141.     if ( !($result = $db->sql_query($sql)) )
  142.     {
  143.         message_die(CRITICAL_ERROR, 'Could not obtain ban information', '', __LINE__, __FILE__, $sql);
  144.     }
  145.  
  146.     if ( $ban_info = $db->sql_fetchrow($result) )
  147.     {
  148.         if ( $ban_info['ban_ip'] || $ban_info['ban_userid'] || $ban_info['ban_email'] )
  149.         {
  150.             message_die(CRITICAL_MESSAGE, 'You_been_banned');
  151.         }
  152.     }
  153.  
  154.     //
  155.     // Create or update the session
  156.     //
  157.     $sql = "UPDATE " . SESSIONS_TABLE . "
  158.         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
  159.         WHERE session_id = '" . $session_id . "' 
  160.             AND session_ip = '$user_ip'";
  161.     if ( !$db->sql_query($sql) || !$db->sql_affectedrows() )
  162.     {
  163.         list($sec, $usec) = explode(' ', microtime());
  164.         mt_srand((float) $sec + ((float) $usec * 100000));
  165.         $session_id = md5(uniqid(mt_rand(), true));
  166.  
  167.         $sql = "INSERT INTO " . SESSIONS_TABLE . "
  168.             (session_id, session_user_id, session_start, session_time, session_ip, session_page, session_logged_in, session_admin)
  169.             VALUES ('$session_id', $user_id, $current_time, $current_time, '$user_ip', $page_id, $login, $admin)";
  170.         if ( !$db->sql_query($sql) )
  171.         {
  172.             message_die(CRITICAL_ERROR, 'Error creating new session', '', __LINE__, __FILE__, $sql);
  173.         }
  174.     }
  175.  
  176.     if ( $user_id != ANONYMOUS )
  177.     {// ( $userdata['user_session_time'] > $expiry_time && $auto_create ) ? $userdata['user_lastvisit'] : ( 
  178.         $last_visit = ( $userdata['user_session_time'] > 0 ) ? $userdata['user_session_time'] : $current_time; 
  179.  
  180.         if (!$admin)
  181.         {
  182.             $sql = "UPDATE " . USERS_TABLE . " 
  183.                 SET user_session_time = $current_time, user_session_page = $page_id, user_lastvisit = $last_visit
  184.                 WHERE user_id = $user_id";
  185.             if ( !$db->sql_query($sql) )
  186.             {
  187.                 message_die(CRITICAL_ERROR, 'Error updating last visit time', '', __LINE__, __FILE__, $sql);
  188.             }
  189.         }
  190.  
  191.         $userdata['user_lastvisit'] = $last_visit;
  192.  
  193.         $sessiondata['autologinid'] = (!$admin) ? (( $enable_autologin && $sessionmethod == SESSION_METHOD_COOKIE ) ? $auto_login_key : '') : $sessiondata['autologinid'];
  194.         $sessiondata['userid'] = $user_id;
  195.     }
  196.  
  197.     $userdata['session_id'] = $session_id;
  198.     $userdata['session_ip'] = $user_ip;
  199.     $userdata['session_user_id'] = $user_id;
  200.     $userdata['session_logged_in'] = $login;
  201.     $userdata['session_page'] = $page_id;
  202.     $userdata['session_start'] = $current_time;
  203.     $userdata['session_time'] = $current_time;
  204.     $userdata['session_admin'] = $admin;
  205.  
  206.     setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
  207.     setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure);
  208.  
  209.     $SID = 'sid=' . $session_id;
  210.  
  211.     return $userdata;
  212. }
  213.  
  214. //
  215. // Checks for a given user session, tidies session table and updates user
  216. // sessions at each page refresh
  217. //
  218. function session_pagestart($user_ip, $thispage_id)
  219. {
  220.     global $db, $lang, $board_config;
  221.     global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
  222.  
  223.     $cookiename = $board_config['cookie_name'];
  224.     $cookiepath = $board_config['cookie_path'];
  225.     $cookiedomain = $board_config['cookie_domain'];
  226.     $cookiesecure = $board_config['cookie_secure'];
  227.  
  228.     $current_time = time();
  229.     unset($userdata);
  230.  
  231.     if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) )
  232.     {
  233.         $sessiondata = isset( $HTTP_COOKIE_VARS[$cookiename . '_data'] ) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array();
  234.         $session_id = isset( $HTTP_COOKIE_VARS[$cookiename . '_sid'] ) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
  235.         $sessionmethod = SESSION_METHOD_COOKIE;
  236.     }
  237.     else
  238.     {
  239.         $sessiondata = array();
  240.         $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
  241.         $sessionmethod = SESSION_METHOD_GET;
  242.     }
  243.  
  244.     // 
  245.     if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
  246.     {
  247.         $session_id = '';
  248.     }
  249.  
  250.     $thispage_id = (int) $thispage_id;
  251.  
  252.     //
  253.     // Does a session exist?
  254.     //
  255.     if ( !empty($session_id) )
  256.     {
  257.         //
  258.         // session_id exists so go ahead and attempt to grab all
  259.         // data in preparation
  260.         //
  261.         $sql = "SELECT u.*, s.*
  262.             FROM " . SESSIONS_TABLE . " s, " . USERS_TABLE . " u
  263.             WHERE s.session_id = '$session_id'
  264.                 AND u.user_id = s.session_user_id";
  265.         if ( !($result = $db->sql_query($sql)) )
  266.         {
  267.             message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
  268.         }
  269.  
  270.         $userdata = $db->sql_fetchrow($result);
  271.  
  272.         //
  273.         // Did the session exist in the DB?
  274.         //
  275.         if ( isset($userdata['user_id']) )
  276.         {
  277.             //
  278.             // Do not check IP assuming equivalence, if IPv4 we'll check only first 24
  279.             // bits ... I've been told (by vHiker) this should alleviate problems with 
  280.             // load balanced et al proxies while retaining some reliance on IP security.
  281.             //
  282.             $ip_check_s = substr($userdata['session_ip'], 0, 6);
  283.             $ip_check_u = substr($user_ip, 0, 6);
  284.  
  285.             if ($ip_check_s == $ip_check_u)
  286.             {
  287.                 $SID = ($sessionmethod == SESSION_METHOD_GET || defined('IN_ADMIN')) ? 'sid=' . $session_id : '';
  288.  
  289.                 //
  290.                 // Only update session DB a minute or so after last update
  291.                 //
  292.                 if ( $current_time - $userdata['session_time'] > 60 )
  293.                 {
  294.                     // A little trick to reset session_admin on session re-usage
  295.                     $update_admin = (!defined('IN_ADMIN') && $current_time - $userdata['session_time'] > ($board_config['session_length']+60)) ? ', session_admin = 0' : '';
  296.  
  297.                     $sql = "UPDATE " . SESSIONS_TABLE . " 
  298.                         SET session_time = $current_time, session_page = $thispage_id$update_admin
  299.                         WHERE session_id = '" . $userdata['session_id'] . "'";
  300.                     if ( !$db->sql_query($sql) )
  301.                     {
  302.                         message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql);
  303.                     }
  304.  
  305.                     if ( $userdata['user_id'] != ANONYMOUS )
  306.                     {
  307.                         $sql = "UPDATE " . USERS_TABLE . " 
  308.                             SET user_session_time = $current_time, user_session_page = $thispage_id
  309.                             WHERE user_id = " . $userdata['user_id'];
  310.                         if ( !$db->sql_query($sql) )
  311.                         {
  312.                             message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql);
  313.                         }
  314.                     }
  315.  
  316.                     //
  317.                     // Delete expired sessions
  318.                     //
  319.                     $expiry_time = $current_time - $board_config['session_length'];
  320.  
  321.                     $sql = "DELETE FROM " . SESSIONS_TABLE . " 
  322.                         WHERE session_time < $expiry_time 
  323.                             AND session_id <> '$session_id'";
  324.                     if ( !$db->sql_query($sql) )
  325.                     {
  326.                         message_die(CRITICAL_ERROR, 'Error clearing sessions table', '', __LINE__, __FILE__, $sql);
  327.                     }
  328.  
  329.                     setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
  330.                     setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure);
  331.                 }
  332.  
  333.                 return $userdata;
  334.             }
  335.         }
  336.     }
  337.  
  338.     //
  339.     // If we reach here then no (valid) session exists. So we'll create a new one,
  340.     // using the cookie user_id if available to pull basic user prefs.
  341.     //
  342.     $user_id = ( isset($sessiondata['userid']) ) ? intval($sessiondata['userid']) : ANONYMOUS;
  343.  
  344.     if ( !($userdata = session_begin($user_id, $user_ip, $thispage_id, TRUE)) )
  345.     {
  346.         message_die(CRITICAL_ERROR, 'Error creating user session', '', __LINE__, __FILE__, $sql);
  347.     }
  348.  
  349.     return $userdata;
  350.  
  351. }
  352.  
  353. //
  354. // session_end closes out a session
  355. // deleting the corresponding entry
  356. // in the sessions table
  357. //
  358. function session_end($session_id, $user_id)
  359. {
  360.     global $db, $lang, $board_config;
  361.     global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
  362.  
  363.     $cookiename = $board_config['cookie_name'];
  364.     $cookiepath = $board_config['cookie_path'];
  365.     $cookiedomain = $board_config['cookie_domain'];
  366.     $cookiesecure = $board_config['cookie_secure'];
  367.  
  368.     $current_time = time();
  369.  
  370.     //
  371.     // Pull cookiedata or grab the URI propagated sid
  372.     //
  373.     if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) )
  374.     {
  375.         $session_id = isset( $HTTP_COOKIE_VARS[$cookiename . '_sid'] ) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
  376.         $sessionmethod = SESSION_METHOD_COOKIE;
  377.     }
  378.     else
  379.     {
  380.         $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
  381.         $sessionmethod = SESSION_METHOD_GET;
  382.     }
  383.  
  384.     if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
  385.     {
  386.         return;
  387.     }
  388.     
  389.     //
  390.     // Delete existing session
  391.     //
  392.     $sql = "DELETE FROM " . SESSIONS_TABLE . " 
  393.         WHERE session_id = '$session_id' 
  394.             AND session_user_id = $user_id";
  395.     if ( !$db->sql_query($sql) )
  396.     {
  397.         message_die(CRITICAL_ERROR, 'Error removing user session', '', __LINE__, __FILE__, $sql);
  398.     }
  399.  
  400.     setcookie($cookiename . '_data', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure);
  401.     setcookie($cookiename . '_sid', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure);
  402.  
  403.     return true;
  404. }
  405.  
  406. //
  407. // Append $SID to a url. Borrowed from phplib and modified. This is an
  408. // extra routine utilised by the session code above and acts as a wrapper
  409. // around every single URL and form action. If you replace the session
  410. // code you must include this routine, even if it's empty.
  411. //
  412. function append_sid($url, $non_html_amp = false)
  413. {
  414.     global $SID;
  415.  
  416.     if ( !empty($SID) && !preg_match('#sid=#', $url) )
  417.     {
  418.         $url .= ( ( strpos($url, '?') != false ) ?  ( ( $non_html_amp ) ? '&' : '&' ) : '?' ) . $SID;
  419.     }
  420.  
  421.     return $url;
  422. }
  423.  
  424. ?>