[ Index ]
 

Code source de PHP NUKE 7.9

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/includes/ -> sessions.php (source)

   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  if ( !defined('IN_PHPBB') )
  23  {
  24          die("Hacking attempt");
  25          exit;
  26  }
  27  
  28  //

  29  // Adds/updates a new session to the database for the given userid.

  30  // Returns the new session ID on success.

  31  //

  32  function session_begin($user_id, $user_ip, $page_id, $auto_create = 0, $enable_autologin = 0, $admin = 0)
  33  {
  34      global $db, $board_config, $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
  35  
  36      $cookiename = $board_config['cookie_name'];
  37      $cookiepath = $board_config['cookie_path'];
  38      $cookiedomain = $board_config['cookie_domain'];
  39      $cookiesecure = $board_config['cookie_secure'];
  40  
  41      if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) )
  42      {
  43          $session_id = isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
  44          $sessiondata = isset($HTTP_COOKIE_VARS[$cookiename . '_data']) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array();
  45          $sessionmethod = SESSION_METHOD_COOKIE;
  46      }
  47      else
  48      {
  49          $sessiondata = array();
  50          $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
  51          $sessionmethod = SESSION_METHOD_GET;
  52      }
  53  
  54      //

  55      if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
  56      {
  57          $session_id = '';
  58      }
  59  
  60      $page_id = (int) $page_id;
  61  
  62      $last_visit = 0;
  63      $current_time = time();
  64      $expiry_time = $current_time - $board_config['session_length'];
  65  
  66      //

  67      // Try and pull the last time stored in a cookie, if it exists

  68      //

  69      $sql = "SELECT *
  70          FROM " . USERS_TABLE . "
  71          WHERE user_id = '$user_id'";
  72      if ( !($result = $db->sql_query($sql)) )
  73      {
  74          message_die(CRITICAL_ERROR, 'Could not obtain lastvisit data from user table', '', __LINE__, __FILE__, $sql);
  75      }
  76  
  77      $userdata = $db->sql_fetchrow($result);
  78  
  79      if ( $user_id != ANONYMOUS )
  80      {
  81          $auto_login_key = $userdata['user_password'];
  82  
  83          if ( $auto_create )
  84          {
  85              if ( isset($sessiondata['autologinid']) && $userdata['user_active'] )
  86              {
  87                  // We have to login automagically

  88                  if( $sessiondata['autologinid'] === $auto_login_key )
  89                  {
  90                      // autologinid matches password

  91                      $login = 1;
  92                      $enable_autologin = 1;
  93                  }
  94                  else
  95                  {
  96                      // No match; don't login, set as anonymous user

  97                      $login = 0;
  98                      $enable_autologin = 0;
  99                      $user_id = $userdata['user_id'] = ANONYMOUS;
 100                      $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . ANONYMOUS;
 101                      $result = $db->sql_query($sql);
 102                      $userdata = $db->sql_fetchrow($result);
 103                      $db->sql_freeresult($result);
 104                  }
 105              }
 106              else
 107              {
 108                  // Autologin is not set. Don't login, set as anonymous user

 109                  $login = 0;
 110                  $enable_autologin = 0;
 111                  $user_id = $userdata['user_id'] = ANONYMOUS;
 112  
 113                  $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . ANONYMOUS;
 114                  $result = $db->sql_query($sql);
 115                  $userdata = $db->sql_fetchrow($result);
 116                  $db->sql_freeresult($result);
 117              }
 118          }
 119          else
 120          {
 121              $login = 1;
 122          }
 123      }
 124      else
 125      {
 126          $login = 0;
 127          $enable_autologin = 0;
 128      }
 129  
 130      //

 131      // Initial ban check against user id, IP and email address

 132      //

 133      preg_match('/(..)(..)(..)(..)/', $user_ip, $user_ip_parts);
 134  
 135      $sql = "SELECT ban_ip, ban_userid, ban_email
 136          FROM " . BANLIST_TABLE . "
 137          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')
 138              OR ban_userid = '$user_id'";
 139      if ( $user_id != ANONYMOUS )
 140      {
 141          $sql .= " OR ban_email LIKE '" . str_replace("\'", "''", $userdata['user_email']) . "'
 142              OR ban_email LIKE '" . substr(str_replace("\'", "''", $userdata['user_email']), strpos(str_replace("\'", "''", $userdata['user_email']), "@")) . "'";
 143      }
 144      if ( !($result = $db->sql_query($sql)) )
 145      {
 146          message_die(CRITICAL_ERROR, 'Could not obtain ban information', '', __LINE__, __FILE__, $sql);
 147      }
 148  
 149      if ( $ban_info = $db->sql_fetchrow($result) )
 150      {
 151          if ( $ban_info['ban_ip'] || $ban_info['ban_userid'] || $ban_info['ban_email'] )
 152          {
 153              message_die(CRITICAL_MESSAGE, 'You_been_banned');
 154          }
 155      }
 156  
 157      //

 158      // Create or update the session

 159      //

 160      $sql = "UPDATE " . SESSIONS_TABLE . "
 161          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
 162          WHERE session_id = '" . $session_id . "'
 163              AND session_ip = '$user_ip'";
 164      if ( !$db->sql_query($sql) || !$db->sql_affectedrows() )
 165      {
 166          list($sec, $usec) = explode(' ', microtime());
 167          mt_srand((float) $sec + ((float) $usec * 100000));
 168          $session_id = md5(uniqid(mt_rand(), true));
 169  
 170          $sql = "INSERT INTO " . SESSIONS_TABLE . "
 171              (session_id, session_user_id, session_start, session_time, session_ip, session_page, session_logged_in, session_admin)
 172              VALUES ('$session_id', '$user_id', '$current_time', '$current_time', '$user_ip', '$page_id', '$login', '$admin')";
 173          if ( !$db->sql_query($sql) )
 174          {
 175                  $error = TRUE;
 176                  if (SQL_LAYER == "mysql" || SQL_LAYER == "mysql4")
 177                  {
 178                      $sql_error = $db->sql_error($result);
 179                      if ($sql_error["code"] == 1114)
 180                      {
 181                          $result = $db->sql_query('SHOW TABLE STATUS LIKE "'.SESSIONS_TABLE.'"');
 182                          $row = $db->sql_fetchrow($result);
 183                          if ($row["Type"] == "HEAP")
 184                          {
 185                              if ($row["Rows"] > 2500)
 186                              {
 187                                  $delete_order = (SQL_LAYER=="mysql4") ? " ORDER BY session_time ASC" : "";
 188                                  $db->sql_query("DELETE QUICK FROM ".SESSIONS_TABLE."$delete_order LIMIT 50");
 189                              }
 190                              else
 191                              {
 192                                  $db->sql_query("ALTER TABLE ".SESSIONS_TABLE." MAX_ROWS=".($row["Rows"]+50));
 193                              }
 194                              if ($db->sql_query($sql))
 195                              {
 196                                  $error = FALSE;
 197                              }
 198                          }
 199                      }
 200                  }
 201                  if ($error)
 202                  {
 203                      message_die(CRITICAL_ERROR, "Error creating new session", "", __LINE__, __FILE__, $sql);
 204                  }
 205                  }
 206      }
 207  
 208      if ( $user_id != ANONYMOUS )
 209      {// ( $userdata['user_session_time'] > $expiry_time && $auto_create ) ? $userdata['user_lastvisit'] : (
 210          $last_visit = ( $userdata['user_session_time'] > 0 ) ? $userdata['user_session_time'] : $current_time;
 211          if (!$admin)
 212          {
 213  
 214          $sql = "UPDATE " . USERS_TABLE . "
 215              SET user_session_time = $current_time, user_session_page = $page_id, user_lastvisit = $last_visit
 216              WHERE user_id = '$user_id'";
 217          if ( !$db->sql_query($sql) )
 218          {
 219              message_die(CRITICAL_ERROR, 'Error updating last visit time', '', __LINE__, __FILE__, $sql);
 220          }
 221  
 222          }
 223  
 224          $userdata['user_lastvisit'] = $last_visit;
 225  
 226          $sessiondata['autologinid'] = (!$admin) ? (( $enable_autologin && $sessionmethod == SESSION_METHOD_COOKIE ) ? $auto_login_key : '') : $sessiondata['autologinid'];
 227          $sessiondata['userid'] = $user_id;
 228      }
 229  
 230      $userdata['session_id'] = $session_id;
 231      $userdata['session_ip'] = $user_ip;
 232      $userdata['session_user_id'] = $user_id;
 233      $userdata['session_logged_in'] = $login;
 234      $userdata['session_page'] = $page_id;
 235      $userdata['session_start'] = $current_time;
 236      $userdata['session_time'] = $current_time;
 237      $userdata['session_admin'] = $admin;
 238  
 239      setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
 240      setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure);
 241  
 242      $SID = 'sid=' . $session_id;
 243  
 244      return $userdata;
 245  }
 246  
 247  //

 248  // Checks for a given user session, tidies session table and updates user

 249  // sessions at each page refresh

 250  //

 251  function session_pagestart($user_ip, $thispage_id, $nukeuser)
 252  {
 253      global $db, $lang, $board_config, $session_id, $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
 254  
 255      $cookiename = $board_config['cookie_name'];
 256      $cookiepath = $board_config['cookie_path'];
 257      $cookiedomain = $board_config['cookie_domain'];
 258      $cookiesecure = $board_config['cookie_secure'];
 259  
 260      $current_time = time();
 261      unset($userdata);
 262  
 263      if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) )
 264      {
 265          $sessiondata = isset( $HTTP_COOKIE_VARS[$cookiename . '_data'] ) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array();
 266          $session_id = isset( $HTTP_COOKIE_VARS[$cookiename . '_sid'] ) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
 267          $sessionmethod = SESSION_METHOD_COOKIE;
 268      }
 269      else
 270      {
 271          $sessiondata = array();
 272          $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
 273          $sessionmethod = SESSION_METHOD_GET;
 274      }
 275     if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
 276     {
 277        $session_id = '';
 278     }
 279          if ( ($nukeuser != "") && ($userdata['session_logged_in'] == "" )) {
 280                  bblogin($nukeuser, $session_id);
 281          } else {
 282      $thispage_id = (int) $thispage_id;
 283          }
 284  
 285      //

 286      // Does a session exist?

 287      //

 288      if ( !empty($session_id) )
 289      {
 290          //

 291          // session_id exists so go ahead and attempt to grab all

 292          // data in preparation

 293          //

 294          $sql = "SELECT u.*, s.*
 295              FROM " . SESSIONS_TABLE . " s, " . USERS_TABLE . " u
 296              WHERE s.session_id = '$session_id'
 297                  AND u.user_id = s.session_user_id";
 298          if ( !($result = $db->sql_query($sql)) )
 299          {
 300              message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
 301          }
 302  
 303          $userdata = $db->sql_fetchrow($result);
 304  
 305          //

 306          // Did the session exist in the DB?

 307          //

 308          if ( isset($userdata['user_id']) )
 309          {
 310              //

 311              // Do not check IP assuming equivalence, if IPv4 we'll check only first 24

 312              // bits ... I've been told (by vHiker) this should alleviate problems with

 313              // load balanced et al proxies while retaining some reliance on IP security.

 314              //

 315              $ip_check_s = substr($userdata['session_ip'], 0, 6);
 316              $ip_check_u = substr($user_ip, 0, 6);
 317  
 318              if ($ip_check_s == $ip_check_u)
 319              {
 320                  $SID = ($sessionmethod == SESSION_METHOD_GET || defined('IN_ADMIN')) ? 'sid=' . $session_id : '';
 321  
 322                  //

 323                  // Only update session DB a minute or so after last update

 324                  //

 325                  if ( $current_time - $userdata['session_time'] > 60 )
 326                  {
 327                      // A little trick to reset session_admin on session re-usage

 328                      $update_admin = (!defined('IN_ADMIN') && $current_time - $userdata['session_time'] > ($board_config['session_length']+60)) ? ', session_admin = 0' : '';
 329  
 330                      $sql = "UPDATE " . SESSIONS_TABLE . "
 331                          SET session_time = '$current_time', session_page = $thispage_id$update_admin
 332                          WHERE session_id = '" . $userdata['session_id'] . "'";
 333                      if ( !$db->sql_query($sql) )
 334                      {
 335                          message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql);
 336                      }
 337  
 338                      if ( $userdata['user_id'] != ANONYMOUS )
 339                      {
 340                          $sql = "UPDATE " . USERS_TABLE . "
 341                              SET user_session_time = '$current_time', user_session_page = '$thispage_id'
 342                              WHERE user_id = " . $userdata['user_id'];
 343                          if ( !$db->sql_query($sql) )
 344                          {
 345                              message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql);
 346                          }
 347                      }
 348  
 349                      //

 350                      // Delete expired sessions

 351                      //

 352                      $expiry_time = $current_time - $board_config['session_length'];
 353                      $sql = "DELETE FROM " . SESSIONS_TABLE . "
 354                          WHERE session_time < '$expiry_time'
 355                              AND session_id <> '$session_id'";
 356                      if ( !$db->sql_query($sql) )
 357                      {
 358                          message_die(CRITICAL_ERROR, 'Error clearing sessions table', '', __LINE__, __FILE__, $sql);
 359                      }
 360  
 361                      setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
 362                      setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure);
 363                  }
 364  
 365                  return $userdata;
 366              }
 367          }
 368      }
 369  
 370      //

 371      // If we reach here then no (valid) session exists. So we'll create a new one,

 372      // using the cookie user_id if available to pull basic user prefs.

 373      //

 374      $user_id = ( isset($sessiondata['userid']) ) ? intval($sessiondata['userid']) : ANONYMOUS;
 375  
 376      if ( !($userdata = session_begin($user_id, $user_ip, $thispage_id, TRUE)) )
 377      {
 378          message_die(CRITICAL_ERROR, 'Error creating user session', '', __LINE__, __FILE__, $sql);
 379      }
 380  
 381      return $userdata;
 382  
 383  }
 384  
 385  //

 386  // session_end closes out a session

 387  // deleting the corresponding entry

 388  // in the sessions table

 389  //

 390  function session_end($session_id, $user_id)
 391  {
 392      global $db, $lang, $board_config, $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
 393  
 394      $cookiename = $board_config['cookie_name'];
 395      $cookiepath = $board_config['cookie_path'];
 396      $cookiedomain = $board_config['cookie_domain'];
 397      $cookiesecure = $board_config['cookie_secure'];
 398  
 399      $current_time = time();
 400  
 401      //

 402      // Pull cookiedata or grab the URI propagated sid

 403      //

 404      if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) )
 405      {
 406          $session_id = isset( $HTTP_COOKIE_VARS[$cookiename . '_sid'] ) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
 407          $sessionmethod = SESSION_METHOD_COOKIE;
 408      }
 409      else
 410      {
 411          $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
 412          $sessionmethod = SESSION_METHOD_GET;
 413      }
 414  
 415      if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
 416      {
 417          return;
 418      }
 419      //

 420      // Delete existing session

 421      //

 422      $sql = "DELETE FROM " . SESSIONS_TABLE . "
 423          WHERE session_id = '$session_id'
 424              AND session_user_id = '$user_id'";
 425      if ( !$db->sql_query($sql) )
 426      {
 427          message_die(CRITICAL_ERROR, 'Error removing user session', '', __LINE__, __FILE__, $sql);
 428      }
 429  
 430      setcookie($cookiename . '_data', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure);
 431      setcookie($cookiename . '_sid', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure);
 432  
 433      return true;
 434  }
 435  
 436  //

 437  // Append $SID to a url. Borrowed from phplib and modified. This is an

 438  // extra routine utilised by the session code above and acts as a wrapper

 439  // around every single URL and form action. If you replace the session

 440  // code you must include this routine, even if it's empty.

 441  //

 442  function append_sid($url, $non_html_amp = false)
 443  {
 444      global $SID, $admin, $userdata;
 445      if (ereg("admin=1", $url) || ereg("admin_", $url) || ereg("pane=", $url)){
 446                                  //  The format is fine, don't change a thing.

 447      } else if (ereg("Your_Account", $url)){
 448              $url = str_replace(".php", "", $url);         //  Strip the .php from all the files,

 449              $url = str_replace("modules", "modules.php", $url); //  and put it back for the modules.php

 450      }
 451      else if (ereg("redirect", $url))
 452      {
 453              $url = str_replace("login.php", "modules.php?name=Your_Account", $url);         //  Strip the .php from all the files,

 454              $url = str_replace(".php", "", $url);         //  Strip the .php from all the files,

 455              $url = str_replace("?redirect", "&redirect", $url);         //  Strip the .php from all the files,

 456              $url = str_replace("modules", "modules.php", $url); //  and put it back for the modules.php

 457      }
 458      else if (ereg("menu=1", $url))
 459      {
 460              $url = str_replace("?", "&", $url); // As we are already in nuke, change the ? to &

 461              $url = str_replace(".php", "", $url);         //  Strip the .php from all the files,

 462          $url = "../../../modules.php?name=Forums&file=$url";
 463      }
 464      else if ((ereg("privmsg", $url)) && (!ereg("highlight=privmsg", $url)))
 465      {
 466              $url = str_replace("?", "&", $url); // As we are already in nuke, change the ? to &

 467              $url = str_replace("privmsg.php", "modules.php?name=Private_Messages&file=index", $url); //  and put it back for the modules.php

 468      }
 469      else if ((ereg("profile", $url)) && (!ereg("highlight", $url) && !ereg("profile", $url)))
 470      {
 471              $url = str_replace("?", "&", $url); // As we are already in nuke, change the ? to &

 472              $url = str_replace("profile.php", "modules.php?name=Forums&file=profile", $url); //  and put it back for the modules.php

 473          $dummy = 1;
 474      }
 475      else if ((ereg("memberlist", $url)) && (!ereg("highlight=memberlist", $url)))
 476      {
 477              $url = str_replace("?", "&", $url); // As we are already in nuke, change the ? to &

 478              $url = str_replace("memberlist.php", "modules.php?name=Members_List&file=index", $url); //  and put it back for the modules.php

 479      } else {
 480              $url = str_replace("?", "&", $url); // As we are already in nuke, change the ? to &

 481              $url = str_replace(".php", "", $url);
 482              $url = "modules.php?name=Forums&file=".$url; //Change to Nuke format

 483      }
 484  
 485     if ($userdata['user_level'] > 1) { 
 486      if ( !empty($SID) && !eregi('sid=', $url) )
 487      {
 488          if ( !empty($SID) && !eregi('sid=', $url) )    {
 489          $url .= ( ( strpos($url, '?') != false ) ?  ( ( $non_html_amp ) ? '&' : '&amp;' ) : '?' ) . $SID;
 490            } 
 491        }    
 492     }
 493      return($url);
 494  }
 495  function admin_sid($url, $non_html_amp = false)
 496  {
 497      global $SID;
 498          $url = "../../../modules.php?name=Forums&file=$url";
 499  
 500      if ( !empty($SID) && !preg_match('#sid=#', $url) )
 501      {
 502          $url .= ( ( strpos($url, '?') != false ) ?  ( ( $non_html_amp ) ? '&' : '&amp;' ) : '?' ) . $SID;
 503      }
 504  
 505      return $url;
 506  }
 507  
 508  ?>


Généré le : Sun Apr 1 11:11:59 2007 par Balluche grâce à PHPXref 0.7