HEX
Server: Apache/2.4.59 (Debian)
System: Linux skycube.cz 4.19.0-25-amd64 #1 SMP Debian 4.19.289-2 (2023-08-08) x86_64
User: ilya (534)
PHP: 7.3.31-1~deb10u7
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: /var/www/ilya/data/www/kamforum.ru/sources/classes/class_session.php
<?php
/*
+--------------------------------------------------------------------------
|   Invision Power Board v2.1.5
|   =============================================
|   by Matthew Mecham
|   (c) 2001 - 2004 Invision Power Services, Inc.
|   http://www.invisionpower.com
|   =============================================
|   Web: http://www.invisionboard.com
|   Time: Sun, 09 Oct 2005 11:51:26 GMT
|   Release: 1a47e28f0443faa9f14d0c0a45151e54
|   Licence Info: http://www.invisionboard.com/?license
+---------------------------------------------------------------------------
|
|   > SESSION CLASS
|   > Module written by Matt Mecham
|   > Date started: 26th January 2004
|
|	> Module Version Number: 1.0.0
|   > DBA Checked: Wed 19 May 2004
+--------------------------------------------------------------------------
*/

class session
{
	# Global
	var $ipsclass;
	
    var $ip_address = 0;
    var $user_agent = "";
    
    # Flag
    var $session_recorded = FALSE;
    
    var $time_now          = 0;
    var $session_id        = 0;
    
    var $session_dead_id   = 0;
    var $session_user_id   = 0;
    var $session_user_pass = "";
    
    var $last_click        = 0;
    var $location          = "";
    
    var $member            = array();
	var $botmap            = array();
	
	var $do_update         = 1;
    
    /*-------------------------------------------------------------------------*/
    // Authorise
    /*-------------------------------------------------------------------------*/
    
    function authorise()
    {
    	//-----------------------------------------
        // INIT
        //-----------------------------------------
        
        $this->member     = array( 'id' => 0, 'name' => "", 'members_display_name' => "", 'mgroup' => $this->ipsclass->vars['guest_group'], 'member_forum_markers' => array() );
        $this->time_now   = time();
        $cookie           = array();
        
        # TMP:
        $this->ipsclass->vars['match_ipaddress'] = 1;
        
        //-----------------------------------------
        // Before we go any lets check the load settings..
        //-----------------------------------------
        
        if ($this->ipsclass->vars['load_limit'] > 0)
        {
	        # @ supressor fixes warning in >4.3.2 with open_basedir restrictions
        	if ( @file_exists('/proc/loadavg') )
        	{
        		if ( $fh = @fopen( '/proc/loadavg', 'r' ) )
        		{
        			$data = @fread( $fh, 6 );
        			@fclose( $fh );
        			
        			$load_avg = explode( " ", $data );
        			
        			$this->ipsclass->server_load = trim($load_avg[0]);
        			
        			if ($this->ipsclass->server_load > $this->ipsclass->vars['load_limit'])
        			{
        				$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'server_too_busy', 'INIT' => 1 ) );
        			}
        		}
        	}
        	else
        	{
				if ( $serverstats = @exec("uptime") )
				{
					preg_match( "/(?:averages)?\: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/", $serverstats, $load );
					
					$this->ipsclass->server_load = $load[1];
					
					if ($this->ipsclass->server_load > $this->ipsclass->vars['load_limit'])
        			{
        				$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'server_too_busy', 'INIT' => 1 ) );
        			}
				}
			}
        }
       
        //-----------------------------------------
		// Are they banned?
		//-----------------------------------------
		
		if ( is_array( $this->ipsclass->cache['banfilters'] ) and count( $this->ipsclass->cache['banfilters'] ) )
		{
			foreach ($this->ipsclass->cache['banfilters'] as $ip)
			{
				$ip = str_replace( '\*', '.*', preg_quote($ip, "/") );
				
				if ( preg_match( "/^$ip$/", $this->ipsclass->input['IP_ADDRESS'] ) )
				{
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'you_are_banned', 'INIT' => 1 ) );
				}
			}
		}
        
        //-----------------------------------------
        // Return as guest if running a task
        //-----------------------------------------
        
        if ( $this->ipsclass->input['act'] == 'task' )
        {
        	$this->member = $this->ipsclass->set_up_guest();
        	$this->member['mgroup'] = $this->ipsclass->vars['guest_group'];
        	$this->ipsclass->input['last_activity'] = time();
			$this->ipsclass->input['last_visit']    = time();
			
			return $this->member;
        }
         
        //-----------------------------------------
        // no new headers if we're simply viewing an attachment..
        //-----------------------------------------
        
        if ( $this->ipsclass->input['act'] == 'Attach' or $this->ipsclass->input['act'] == 'Reg' )
        {
        	$this->ipsclass->no_print_header = 1;
        }
        
        //-----------------------------------------
        // no new headers if we're updating chat
        //-----------------------------------------
        
        if ( ($this->ipsclass->input['act'] == 'chat' and $this->ipsclass->input['CODE'] == 'update')
          OR $this->ipsclass->input['act'] == 'xmlout'
          OR $this->ipsclass->input['act'] == 'rssout' )
        {
        	$this->ipsclass->no_print_header = 1;
        	$this->do_update                 = 0;
        }
        
        //-----------------------------------------
        // Manage bots? (tee-hee)
        //-----------------------------------------
        // Gotta love Mac's ;)
        $this->ipsclass->vars['search_engine_bots'] = str_replace( "\r", '', $this->ipsclass->vars['search_engine_bots'] );
        //-----------------------------------------
        
        if ( $this->ipsclass->vars['spider_sense'] == 1 and $this->ipsclass->vars['search_engine_bots'] )
        {
        	foreach( explode( "\n", $this->ipsclass->vars['search_engine_bots'] ) as $bot )
        	{
        		list($ua, $n) = explode( "=", $bot );
        		
        		if ( $ua and $n )
        		{
        			$this->bot_map[ strtolower($ua) ] = $n;
        			$this->bot_safe[] = preg_quote( $ua, "/" );
        		}
        	}
        	
        	if ( preg_match( '/('.implode( '|', $this->bot_safe ) .')/i', $_SERVER['HTTP_USER_AGENT'], $match ) )
        	{
        		$this->ipsclass->DB->simple_construct( array( 'select' => '*',
															  'from'   => 'groups',
															  'where'  =>" g_id=".intval($this->ipsclass->vars['spider_group'])
													 )      );
        		$this->ipsclass->DB->simple_exec();
        	
        		$group = $this->ipsclass->DB->fetch_row();
        
				foreach ($group as $k => $v)
				{
					$this->member[ $k ] = $v;
				}
				
				$this->member['restrict_post']    = 1;
				$this->member['g_use_search']     = 0;
				$this->member['g_email_friend']   = 0;
				$this->member['g_edit_profile']   = 0;
				$this->member['g_use_pm']         = 0;
				$this->member['g_is_supmod']      = 0;
				$this->member['g_access_cp']      = 0;
				$this->member['g_access_offline'] = 0;
				$this->member['g_avoid_flood']    = 0;
				$this->member['id']               = 0;
				
				$this->ipsclass->perm_id       = $this->member['g_perm_id'];
       			$this->ipsclass->perm_id_array = explode( ",", $this->ipsclass->perm_id );
       			$this->ipsclass->session_type  = 'cookie';
       			$this->ipsclass->is_bot        = 1;
       			$this->session_id              = "";
       			
       			$agent = trim($match[0]);
       			$agent = str_replace( '\r', '', $agent );
       			
       			//-----------------------------------------
       			// Using lofi?
       			//-----------------------------------------
       			
       			if ( strstr( $_SERVER['PHP_SELF'], 'lofiversion' ) )
       			{
       				$qstring = "Lo-Fi: ".str_replace( "/", "", strrchr( $_SERVER['PHP_SELF'], "/" ) );
       			}
       			else
       			{
       				$qstring = str_replace( "'", "", $_SERVER['QUERY_STRING']);
       			}
       			
       			if ( $this->ipsclass->vars['spider_visit'] )
       			{
       				$this->ipsclass->DB->do_shutdown_insert( 'spider_logs', array (
																	'bot'          => $agent,
																	'query_string' => $qstring,
																	'ip_address'   => $_SERVER['REMOTE_ADDR'],
																	'entry_date'   => time(),
														)        );
       			}
       			
       			if ( $this->ipsclass->vars['spider_active'] )
       			{
       				$this->ipsclass->DB->simple_construct( array( 'delete' => 'sessions',
												  				  'where'  => "id='".$agent.'='.str_replace('.','',$this->ipsclass->ip_address )."_session'"
										 				 )      );
					$this->ipsclass->DB->simple_shutdown_exec();
        		
       				$this->create_bot_session($agent, $this->bot_map[ strtolower($agent) ]);
       			}
       			
       			return $this->member;
        	}
        }
        
        //-----------------------------------------
        // Continue!
        //-----------------------------------------
        
        $cookie['session_id']   = $this->ipsclass->my_getcookie('session_id');
        $cookie['member_id']    = $this->ipsclass->my_getcookie('member_id');
        $cookie['pass_hash']    = $this->ipsclass->my_getcookie('pass_hash');
        
        if ( $cookie['session_id'] )
        {
        	$this->get_session($cookie['session_id']);
        	$this->ipsclass->session_type = 'cookie';
        }
        elseif ( $this->ipsclass->input['s'] )
        {
        	$this->get_session($this->ipsclass->input['s']);
        	$this->ipsclass->session_type = 'url';
        }
        else
        {
        	$this->session_id = 0;
        }
        
		//-----------------------------------------
		// Do we have a valid session ID?
		//-----------------------------------------
		
		if ( $this->session_id )
		{
			//-----------------------------------------
			// We've checked the IP addy and browser, so we can assume that this is
			// a valid session.
			//-----------------------------------------
			
			if ( ($this->session_user_id != 0) and ( ! empty($this->session_user_id) ) )
			{
				//-----------------------------------------
				// It's a member session, so load the member.
				//-----------------------------------------
				
				$this->load_member($this->session_user_id);
				
				//-----------------------------------------
				// Did we get a member?
				//-----------------------------------------
				
				if ( (! $this->member['id']) or ($this->member['id'] == 0) )
				{
					$this->unload_member();
					$this->update_guest_session();
				}
				else
				{
					$this->update_member_session();
				}
			}
			else
			{
				$this->update_guest_session();
			}
		}
		else
		{
			//-----------------------------------------
			// We didn't have a session, or the session didn't validate
			// Do we have cookies stored?
			//-----------------------------------------
			
			if ($cookie['member_id'] != "" and $cookie['pass_hash'] != "")
			{
				$this->load_member($cookie['member_id']);
				
				if ( (! $this->member['id']) or ($this->member['id'] == 0) )
				{
					$this->unload_member();
					$this->create_guest_session();
				}
				else
				{
					if ($this->member['member_login_key'] == $cookie['pass_hash'])
					{
						$this->create_member_session();
					}
					else
					{
						$this->unload_member();
						$this->create_guest_session();
					}
				}
			}
			else
			{
				$this->create_guest_session();
			}
		}
		
		//-----------------------------------------
        // Knock out Google Web Accelerator
        //-----------------------------------------
       
        if ( strstr( strtolower($_SERVER['HTTP_X_MOZ']), 'prefetch' ) AND $this->member['id'] )
		{
			if ( IPB_PHP_SAPI == 'cgi-fcgi' OR IPB_PHP_SAPI == 'cgi' )
			{
				@header('Status: 403 Forbidden');
			}
			else
			{
				@header('HTTP/1.1 403 Forbidden');
			}
			
			print "Prefetching or precaching is not allowed";
			exit();
		}
		
		//-----------------------------------------
        // Are we a member of several groups?
        //-----------------------------------------
		
        if ( ! $this->member['id'] AND ! $this->ipsclass->is_bot )
        {
        	$this->member                           = $this->ipsclass->set_up_guest();
        	$this->member['mgroup']                 = $this->ipsclass->vars['guest_group'];
        	$this->ipsclass->input['last_activity'] = time();
			$this->ipsclass->input['last_visit']    = time();
        }
		
        //-----------------------------------------
        // Do we have a cache?
        //-----------------------------------------
        
        if ( ! is_array( $this->ipsclass->cache['group_cache'] ) )
		{
			$this->ipsclass->cache['group_cache'] = array();
			
			$this->ipsclass->DB->simple_construct( array( 'select' => "*",
														  'from'   => 'groups'
												 )      );
			
			$this->ipsclass->DB->simple_exec();
			
			while ( $i = $this->ipsclass->DB->fetch_row() )
			{
				$this->ipsclass->cache['group_cache'][ $i['g_id'] ] = $i;
			}
			
			$this->ipsclass->update_cache( array( 'name' => 'group_cache', 'array' => 1, 'deletefirst' => 1 ) );
		}
		
		//-----------------------------------------
        // Set up main 'display' group
        //-----------------------------------------
        
        $this->member = array_merge( $this->member, $this->ipsclass->cache['group_cache'][ $this->member['mgroup'] ] );
		
		//-----------------------------------------
        // Are we a member of several groups?
        //-----------------------------------------
		
		$this->build_group_permissions();
		
		//-----------------------------------------
		// Are we a mod?
		//-----------------------------------------
		
		if ( $this->member['mgroup'] != $this->ipsclass->vars['guest_group'] )
		{
			//-----------------------------------------
			// Sprinkle on some moderator stuff...
			//-----------------------------------------
		
			if ( $this->member['g_is_supmod'] == 1 )
			{
				$this->member['is_mod'] = 1;
			}
			else if ( count($this->ipsclass->cache['moderators']) )
			{
				foreach( $this->ipsclass->cache['moderators'] as $i => $r )
				{
					if ( $r['member_id'] == $this->member['id'] or $r['group_id'] == $this->member['mgroup'] )
					{
						$this->member['_moderator'][ $r['forum_id'] ] = $r;
						$this->member['is_mod'] = 1;
					}
				}
			}
			
			//-----------------------------------------
			// Forum markers
			//-----------------------------------------
			
			$this->member['members_markers'] = unserialize(stripslashes( $this->member['members_markers'] ) );
		}
		
		//header('content-type:text/plain'); print_r($this->member); exit();
		
        //-----------------------------------------
        // Synchronise the last visit and activity times if
        // we have some in the member profile
        //-----------------------------------------
        
        if ( $this->member['id'] )
        {
        	if ( ! $this->ipsclass->input['last_activity'] )
        	{
        		$this->ipsclass->input['last_activity'] = $this->member['last_activity'] ? $this->member['last_activity'] : $this->time_now;
				
        	}
        	
        	if ( ! $this->ipsclass->input['last_visit'] )
        	{
				$this->ipsclass->input['last_visit'] = $this->member['last_visit'] ? $this->member['last_visit'] : $this->time_now;
        	}
        
			//-----------------------------------------
			// If there hasn't been a cookie update in 2 hours,
			// we assume that they've gone and come back
			//-----------------------------------------
			
			if ( ! $this->member['last_visit'] )
			{
				//-----------------------------------------
				// No last visit set, do so now!
				//-----------------------------------------
				
				$this->ipsclass->DB->simple_construct( array( 'update' => 'members',
															  'set'    => "last_visit=".$this->time_now.", last_activity=".$this->time_now,
															  'where'  => "id=".$this->member['id']
													 )      );
				
				$this->ipsclass->DB->simple_shutdown_exec();
				
			}
			else if ( (time() - $this->ipsclass->input['last_activity']) > 300 )
			{
				//-----------------------------------------
				// If the last click was longer than 5 mins ago and this is a member
				// Update their profile.
				//-----------------------------------------
				
				list( $be_anon, $loggedin ) = explode( '&', $this->member['login_anonymous'] );
				
				$this->ipsclass->DB->simple_construct( array( 'update' => 'members',
															  'set'    => "login_anonymous='$be_anon&1', last_activity=".$this->time_now,
															  'where'  => "id=".$this->member['id']
													 )      );
				
				$this->ipsclass->DB->simple_shutdown_exec();
			}
			
			//-----------------------------------------
			// Check ban status
			//-----------------------------------------
			
			if ( $this->member['temp_ban'] )
			{
				$ban_arr = $this->ipsclass->hdl_ban_line(  $this->member['temp_ban'] );
				
				if ( time() >= $ban_arr['date_end'] )
				{
					//-----------------------------------------
					// Update this member's profile
					//-----------------------------------------
					
					$this->ipsclass->DB->simple_construct( array( 'update' => 'members',
																  'set'    => "temp_ban=''",
																  'where'  => "id=".$this->member['id']
														 )      );
					
					$this->ipsclass->DB->simple_shutdown_exec();
				}
				else
				{
					$this->ipsclass->member = $this->member;
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'account_susp', 'INIT' => 1, 'EXTRA' => $this->ipsclass->get_date($ban_arr['date_end'],'LONG', 1) ) );
				}
			}
		}
		
		//-----------------------------------------
        // Set a session ID cookie
        //-----------------------------------------
       
        $this->ipsclass->my_setcookie("session_id", $this->session_id, -1);
        
        return $this->member;
    }
    
    /*-------------------------------------------------------------------------*/
    // Build group permissions
    /*-------------------------------------------------------------------------*/
    
    function build_group_permissions()
    {
    	if ( $this->member['mgroup_others'] )
		{
			$groups_id    = explode( ',', $this->member['mgroup_others'] );
			$exclude      = array( 'g_title', 'g_icon', 'prefix', 'suffix', 'g_promotion', 'g_photo_max_vars' );
			$less_is_more = array( 'g_search_flood' );
			$zero_is_best = array( 'g_attach_max', 'g_edit_cutoff' );
			
			if ( count( $groups_id ) )
			{
				foreach( $groups_id as $pid )
				{
					if ( ! $this->ipsclass->cache['group_cache'][ $pid ]['g_id'] )
					{
						continue;
					}
					
					//-----------------------------------------
					// Loop through and mix
					//-----------------------------------------
					
					foreach( $this->ipsclass->cache['group_cache'][ $pid ] as $k => $v )
					{
						if ( ! in_array( $k, $exclude ) )
						{
							//-----------------------------------------
							// Add to perm id list
							//-----------------------------------------
							
							if ( $k == 'g_perm_id' )
							{
								$this->member['g_perm_id'] .= ','.$v;
							}
							else if ( in_array( $k, $zero_is_best ) )
							{
								if( $this->member[ $k ] == 0 )
								{
									continue;
								}
								else if( $v > $this->member[ $k ] )
								{
									$this->member[ $k ] = $v;
								}
							}							
							else if ( in_array( $k, $less_is_more ) )
							{
								if ( $v < $this->member[ $k ] )
								{
									$this->member[ $k ] = $v;
								}
							}
							else
							{
								if ( $v > $this->member[ $k ] )
								{
									$this->member[ $k ] = $v;
								}
							}
						}
					}
				}
			}
			
			//-----------------------------------------
			// Tidy perms_id
			//-----------------------------------------
			
			$rmp = array();
			$tmp = explode( ',', $this->ipsclass->clean_perm_string($this->member['g_perm_id']) );
			
			if ( count( $tmp ) )
			{
				foreach( $tmp as $t )
				{
					$rmp[ $t ] = $t;
				}
			}
			
			if ( count( $rmp ) )
			{
				$this->member['g_perm_id'] = implode( ',', $rmp );
			}
		}
		
		$this->ipsclass->perm_id       = ( $this->member['org_perm_id'] ) ? $this->member['org_perm_id'] : $this->member['g_perm_id'];
        
        $this->ipsclass->perm_id_array = explode( ",", $this->ipsclass->perm_id );
    }
    
    /*-------------------------------------------------------------------------*/
	// Attempt to load a member
	/*-------------------------------------------------------------------------*/
	
    function load_member($member_id=0)
    {
    	$member_id = intval($member_id);
    	
     	if ($member_id != 0)
        {
            $this->ipsclass->DB->build_query( array( 'select' => "id, name, mgroup, member_login_key, email, restrict_post, view_sigs, view_avs, view_pop, view_img, auto_track, rep_allow, rep_notify,
																  mod_posts, language, skin, new_msg, show_popup, msg_total, time_offset, posts, joined, last_post, subs_pkg_chosen,
																  ignored_users, login_anonymous, last_visit, last_activity, dst_in_use, view_prefs, org_perm_id, mgroup_others, temp_ban, sub_end,
																  has_blog, members_markers, members_editor_choice, members_auto_dst, members_display_name, members_created_remote,
																  members_cache, members_disable_pm",
													 'from'   => 'members',
													 'where'  => 'id='.$member_id ) );
													 
			$this->ipsclass->DB->exec_query();
			
            if ( $this->ipsclass->DB->get_num_rows() )
            {
            	$this->member = $this->ipsclass->DB->fetch_row();
            }
            
            //-----------------------------------------
            // Unless they have a member id, log 'em in as a guest
            //-----------------------------------------
            
            if ( ($this->member['id'] == 0) or (empty($this->member['id'])) )
            {
				$this->unload_member();
            }
		}
		
		//-----------------------------------------
		// Unpack cache
		//-----------------------------------------
		
		if ( $this->member['members_cache'] )
		{
			$this->member['_cache'] = $this->ipsclass->unpack_member_cache( $this->member['members_cache'] );
		}
		else
		{
			$this->member['_cache'] = array();
		}
		
		unset($member_id);
	}
	
	/*-------------------------------------------------------------------------*/
	// Remove the users cookies
	/*-------------------------------------------------------------------------*/
	
	function unload_member()
	{
		$this->ipsclass->my_setcookie( "member_id" , "0", -1  );
		$this->ipsclass->my_setcookie( "pass_hash" , "0", -1  );
		
		$this->member['id']       = 0;
		$this->member['name']     				= "";
		$this->member['members_display_name']   = "";
	}
    
    /*-------------------------------------------------------------------------*/
    // Updates a current session.
    /*-------------------------------------------------------------------------*/
    
    function update_member_session()
    {
        //-----------------------------------------
        // Make sure we have a session id.
        //-----------------------------------------
        
        if ( ! $this->session_id )
        {
        	$this->create_member_session();
        	return;
        }
        
        if ( empty($this->member['id']) )
        {
        	$this->unload_member();
        	$this->create_guest_session();
        	return;
        }
        
        //-----------------------------------------
        // Get module settings
        //-----------------------------------------
        
        $vars = $this->_get_location_settings();
        
        //-----------------------------------------
        // Still update?
        //-----------------------------------------
         
        if ( ! $this->do_update )
        {
        	return;
        }
        
        $this->ipsclass->DB->do_shutdown_update( 'sessions',
												 array(
														'member_name'        => $this->member['members_display_name'],
														'member_id'          => intval($this->member['id']),
														'member_group'       => $this->member['mgroup'],
														'login_type'         => substr($this->member['login_anonymous'],0, 1),
														'running_time'       => $this->time_now,
														'location'           => $vars['location'],
														'in_error'           => 0,
														'location_1_type'    => $vars['1_type'],
														'location_1_id'   => intval($vars['1_id']),
														'location_2_type'    => $vars['2_type'],
														'location_2_id'   => intval($vars['2_id']),
														'location_3_type'    => $vars['3_type'],
														'location_3_id'   => intval($vars['3_id']),
													  ),
												"id='{$this->session_id}'"
							  					);
    }        
    
    /*-------------------------------------------------------------------------*/
    // Update guest session
    /*-------------------------------------------------------------------------*/
    
    function update_guest_session()
    {
        //-----------------------------------------
        // Make sure we have a session id.
        //-----------------------------------------
        
        if ( ! $this->session_id )
        {
        	$this->create_guest_session();
        	return;
        }
        
        //-----------------------------------------
        // Get module settings
        //-----------------------------------------
        
        $vars = $this->_get_location_settings();
        
        //-----------------------------------------
        // Still update?
        //-----------------------------------------
         
        if ( ! $this->do_update )
        {
        	return;
        }
        
        $this->ipsclass->DB->do_shutdown_update( 'sessions',
												 array(
														'member_name'        => "",
														'member_id'          => 0,
														'member_group'       => $this->ipsclass->vars['guest_group'],
														'login_type'         => substr($this->member['login_anonymous'],0, 1),
														'running_time'       => $this->time_now,
														'location'           => $vars['location'],
														'in_error'           => 0,
														'location_1_type'    => $vars['1_type'],
														'location_1_id'   => intval($vars['1_id']),
														'location_2_type'    => $vars['2_type'],
														'location_2_id'   => intval($vars['2_id']),
														'location_3_type'    => $vars['3_type'],
														'location_3_id'   => intval($vars['3_id']),
													  ),
												"id='{$this->session_id}'"
											  );
    } 
                    
    
    /*-------------------------------------------------------------------------*/
    // Get a session based on the current session ID
    /*-------------------------------------------------------------------------*/
    
    function get_session($session_id="")
    {
		//-----------------------------------------
		// INIT
    	//-----------------------------------------
    	
        $result     = array();
        $query      = "";
        $session_id = preg_replace("/([^a-zA-Z0-9])/", "", $session_id);
        
        if ( $session_id )
        {
			if ( $this->ipsclass->vars['match_browser'] == 1 )
			{
				$query = " AND browser='".$this->ipsclass->user_agent."'";
			}
			
			if ( $this->ipsclass->vars['match_ipaddress'] == 1 )
			{
				$query .= " AND ip_address='".$this->ipsclass->ip_address."'";
			}
			
			$this->ipsclass->DB->simple_construct( array( 'select' => 'id, member_id, running_time, location',
														  'from'   => 'sessions',
														  'where'  => "id='".$session_id."'".$query
												 )      );
				
			$this->ipsclass->DB->simple_exec();
			
			if ( $this->ipsclass->DB->get_num_rows() != 1 )
			{
				//-----------------------------------------
				// Either there is no session, or we have more than one session..
				//-----------------------------------------
				
				$this->session_dead_id   = $session_id;
				$this->session_id        = 0;
        		$this->session_user_id   = 0;
        		return;
			}
			else
			{
				$result = $this->ipsclass->DB->fetch_row();
				
				if ($result['id'] == "")
				{
					$this->session_dead_id   = $session_id;
					$this->session_id        = 0;
					$this->session_user_id   = 0;
					unset($result);
					return;
				}
				else
				{
					$this->session_id        = $result['id'];
					$this->session_user_id   = $result['member_id'];
					$this->last_click        = $result['running_time'];
        			$this->location          = $result['location'];
        			unset($result);
					return;
				}
			}
		}
    }
    
    /*-------------------------------------------------------------------------*/
    //
    // Creates a member session.
    //
    /*-------------------------------------------------------------------------*/
    
    function create_member_session()
    {
        if ($this->member['id'])
        {
        	//-----------------------------------------
        	// Remove the defunct sessions
        	//-----------------------------------------
        	
			$this->ipsclass->vars['session_expiration'] = $this->ipsclass->vars['session_expiration'] ? (time() - $this->ipsclass->vars['session_expiration']) : (time() - 3600);
			
			$this->ipsclass->DB->simple_construct( array( 'delete' => 'sessions', 'where' => "member_id=".$this->member['id'] ) );
			$this->ipsclass->DB->simple_exec();
			
			$this->session_id  = md5( uniqid(microtime()) );
			
			//-----------------------------------------
			// Get module settings
			//-----------------------------------------
			
			$vars = $this->_get_location_settings();
        	
        	//-----------------------------------------
			// Still update?
			//-----------------------------------------
			 
			if ( ! $this->do_update )
			{
				return;
			}
        
			//-----------------------------------------
        	// Insert the new session
        	//-----------------------------------------
        	
        	$this->ipsclass->DB->do_shutdown_insert( 'sessions',
													 array(
															'id'                 => $this->session_id,
															'member_name'        => $this->member['members_display_name'],
															'member_id'          => intval($this->member['id']),
															'member_group'       => $this->member['mgroup'],
															'login_type'         => substr($this->member['login_anonymous'],0, 1),
															'running_time'       => $this->time_now,
															'ip_address'         => $this->ipsclass->ip_address,
															'browser'            => $this->ipsclass->user_agent,
															'location'           => $vars['location'],
															'in_error'           => 0,
															'location_1_type'    => $vars['1_type'],
															'location_1_id'   => intval($vars['1_id']),
															'location_2_type'    => $vars['2_type'],
															'location_2_id'   => intval($vars['2_id']),
															'location_3_type'    => $vars['3_type'],
															'location_3_id'   => intval($vars['3_id']),
														  )
												  );
			
			//-----------------------------------------
			// If this is a member, update their last visit times, etc.
			//-----------------------------------------
			
			if ( time() - $this->member['last_activity'] > 3600 )
			{
				//-----------------------------------------
				// Reset the topics read cookie..
				//-----------------------------------------
				
				$this->ipsclass->my_setcookie('topicsread', '');
				
				list( $be_anon, $loggedin ) = explode( '&', $this->member['login_anonymous'] );
				
				$this->ipsclass->DB->simple_construct( array( 'update' => 'members',
															  'set'    => "login_anonymous='$be_anon&1', last_visit=last_activity, last_activity=".$this->time_now,
															  'where'  => "id=".$this->member['id']
													 )      );
					
				$this->ipsclass->DB->simple_shutdown_exec();
				
				//-----------------------------------------
				// Fix up the last visit/activity times.
				//-----------------------------------------
				
				$this->ipsclass->input['last_visit']    = $this->member['last_activity'];
				$this->ipsclass->input['last_activity'] = $this->time_now;
			}
		}
		else
		{
			$this->create_guest_session();
		}
    }
    
    /*-------------------------------------------------------------------------*/
    //
    // Create guest session
    //
    /*-------------------------------------------------------------------------*/
    
    function create_guest_session()
    {
		//-----------------------------------------
    	// INIT
    	//-----------------------------------------
    	
    	$query = array();
    	
    	$this->ipsclass->vars['session_expiration'] = $this->ipsclass->vars['session_expiration'] ? (time() - $this->ipsclass->vars['session_expiration']) : (time() - 3600);
		
		//-----------------------------------------
		// Remove the defunct sessions
		//-----------------------------------------
		
		if ( ($this->session_dead_id != 0) and ( ! empty($this->session_dead_id) ) )
		{
			$query[] = "id='".$this->session_dead_id."'";
		}
		else if ( $this->ipsclass->vars['match_ipaddress'] == 1 )
		{
			$query[] = "ip_address='".$this->ipsclass->ip_address."'";
		}
		
		if ( count( $query ) )
		{
			$this->ipsclass->DB->simple_construct( array( 'delete' => 'sessions', 'where'  => implode( " OR ", $query ) ) );
			$this->ipsclass->DB->simple_exec();
		}
								 
		$this->session_id  = md5( uniqid(microtime()) );
		
		//-----------------------------------------
        // Get module settings
        //-----------------------------------------
        
        $vars = $this->_get_location_settings();
        
        //-----------------------------------------
        // Still update?
        //-----------------------------------------
         
        if ( ! $this->do_update )
        {
        	return;
        }
        
		//-----------------------------------------
		// Insert the new session
		//-----------------------------------------
		
		$this->ipsclass->DB->do_shutdown_insert( 'sessions',
												 array(
														'id'                 => $this->session_id,
														'member_name'        => '',
														'member_id'          => 0,
														'member_group'       => $this->ipsclass->vars['guest_group'],
														'login_type'         => 0,
														'running_time'       => $this->time_now,
														'ip_address'         => $this->ipsclass->ip_address,
														'browser'            => $this->ipsclass->user_agent,
														'location'           => $vars['location'],
														'in_error'           => 0,
														'location_1_type'    => $vars['1_type'],
														'location_1_id'   => intval($vars['1_id']),
														'location_2_type'    => $vars['2_type'],
														'location_2_id'   => intval($vars['2_id']),
														'location_3_type'    => $vars['3_type'],
														'location_3_id'   => intval($vars['3_id']),
													  )
											  );
    }
    
    /*-------------------------------------------------------------------------*/
    // Creates a BOT session
    /*-------------------------------------------------------------------------*/
    
    function create_bot_session($bot, $name="")
    {
    	//-----------------------------------------
        // Get module settings
        //-----------------------------------------
        
        $vars = $this->_get_location_settings();
        
        $this->ipsclass->DB->do_shutdown_insert( 'sessions',
												 array(
														'id'                 => $bot.'='.str_replace('.','',$this->ipsclass->ip_address ).'_session',
														'member_name'        => $name ? $name : $bot,
														'member_id'          => 0,
														'member_group'       => $this->ipsclass->vars['spider_group'],
														'login_type'         => $this->ipsclass->vars['spider_anon'],
														'running_time'       => $this->time_now,
														'ip_address'         => $this->ipsclass->ip_address,
														'browser'            => $this->ipsclass->user_agent,
														'location'           => $vars['location'],
														'in_error'           => 0,
														'location_1_type'    => $vars['1_type'],
														'location_1_id'   => intval($vars['1_id']),
														'location_2_type'    => $vars['2_type'],
														'location_2_id'   => intval($vars['2_id']),
														'location_3_type'    => $vars['3_type'],
														'location_3_id'   => intval($vars['3_id']),
													  )
											  );
    }
    
    /*-------------------------------------------------------------------------*/
    // Updates a BOT current session.
    /*-------------------------------------------------------------------------*/
    
    function update_bot_session($bot, $name="")
    {
    	//-----------------------------------------
        // Get module settings
        //-----------------------------------------
        
        $vars = $this->_get_location_settings();
        
		$this->ipsclass->DB->do_shutdown_update( 'sessions',
												 array(
														'member_name'        => $name ? $name : $bot,
														'member_id'          => 0,
														'member_group'       => $this->ipsclass->vars['spider_group'],
														'login_type'         => $this->ipsclass->vars['spider_anon'],
														'running_time'       => $this->time_now,
														'location'           => $vars['location'],
														'in_error'           => 0,
														'location_1_type'    => $vars['1_type'],
														'location_1_id'   => intval($vars['1_id']),
														'location_2_type'    => $vars['2_type'],
														'location_2_id'   => intval($vars['2_id']),
														'location_3_type'    => $vars['3_type'],
														'location_3_id'   => intval($vars['3_id']),
													  ),
												  "id='".$bot.'='.str_replace('.','',$this->ipsclass->ip_address )."_session'"
											  );
    }
    
    /*-------------------------------------------------------------------------*/
    // Returns a "clean" query string
    /*-------------------------------------------------------------------------*/
    
    function _get_location_settings()
    {
    	//-----------------------------------------
    	// INIT
    	//-----------------------------------------
    	
    	$return = array();
    	$module = $this->ipsclass->input['automodule'] ? $this->ipsclass->input['automodule'] : $this->ipsclass->input['module'];
    	
    	//-----------------------------------------
    	// MODULE?
    	//-----------------------------------------
    	
    	if ( $module )
    	{
    		$filename = ROOT_PATH.'sources/components_location/'.$this->ipsclass->txt_alphanumerical_clean( $module ).'.php';
				
			if ( file_exists( $filename ) )
			{
				require_once( $filename );
				$toload           = 'components_location_'.$module;
				$loader           = new $toload;
				$loader->ipsclass =& $this->ipsclass;
				
				$return = $loader->get_session_variables();
				$return['location'] = 'mod:'.$module;
			}
    	}
    	
    	//-----------------------------------------
    	// FORUM
    	//-----------------------------------------
    	
    	else if ( $this->ipsclass->input['act'] == 'sf' AND $this->ipsclass->input['f'] )
    	{
    		$return = array( 'location' => 'sf',
    						 '2_type'   => 'forum',
    						 '2_id'     => intval($this->ipsclass->input['f']) );
    	}
    	
    	//-----------------------------------------
    	// TOPIC
    	//-----------------------------------------
    	
    	else if ( $this->ipsclass->input['act'] == 'st' AND $this->ipsclass->input['t'] )
    	{
    		$return = array( 'location' => 'st',
    						 '1_type'   => 'topic',
    						 '1_id'     => intval($this->ipsclass->input['t']),
    						 '2_type'   => 'forum',
    						 '2_id'     => intval($this->ipsclass->input['f']) );
    	}
    	
    	//-----------------------------------------
    	// POST
    	//-----------------------------------------
    	
    	else if ( $this->ipsclass->input['act'] == 'post' AND $this->ipsclass->input['f'] )
    	{
    		$return = array( 'location' => 'post',
    			 			 '1_type'   => 'topic',
    						 '1_id'     => intval($this->ipsclass->input['t']),
    						 '2_type'   => 'forum',
    						 '2_id'     => intval($this->ipsclass->input['f']) );
    	}
    	
    	//-----------------------------------------
    	// OTHER
    	//-----------------------------------------
    	
    	else
    	{
    		$return = array( 'location' => $this->ipsclass->input['act'].",".$this->ipsclass->input['p'].",".$this->ipsclass->input['CODE'] );
    	}
    	
    	return $return;
    }
    
        
}

?>