<?php

/***************************************************************************
 *
 *      Update to 2.0.21 by Alf (13.06.2006) 12:40
 *
 *	Title: xphpBBi - phpBB Complete Integration project 
 *	Description: Project to turn the phpBB forums into a fully functioning
 *		Xoops2 Module. Based on the phpBB 2.0.11 port from BBpixel.com - 
 *		(PBBoard v1.22).
 *	Credits: Xoops2 Module Development Team - Koudanshi - phpBB Group
 *	Thanks: To Koudanshi for a great start in his port, and phpBB Group for
 *		such a great forum software.
 *	License: GNU/GPL version 2 or later.
 ****************************************************************************/

/***************************************************************************
 *                                common.php
 *                            -------------------
 *   begin                : Saturday, Feb 23, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 *
 *   $Id: common.php,v 1.74.2.25 2006/05/26 17:46:59 grahamje Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
}

//
error_reporting  (E_ERROR | E_WARNING | E_PARSE); // This will NOT report uninitialized variables
set_magic_quotes_runtime(0); // Disable magic_quotes_runtime

// The following code (unsetting globals) was contributed by Matt Kavanagh
// Thanks to Matt Kavanagh and Stefan Esser for providing feedback as well as patch files

// PHP5 with register_long_arrays off?
if (@phpversion() >= '5.0.0' && (!ini_get('register_long_arrays') || @ini_get('register_long_arrays') == '0' || strtolower(@ini_get('register_long_arrays')) == 'off'))
{
	$HTTP_POST_VARS = $_POST;
	$HTTP_GET_VARS = $_GET;
	$HTTP_SERVER_VARS = $_SERVER;
	$HTTP_COOKIE_VARS = $_COOKIE;
	$HTTP_ENV_VARS = $_ENV;
	$HTTP_POST_FILES = $_FILES;

	// _SESSION is the only superglobal which is conditionally set
	if (isset($_SESSION))
	{
		$HTTP_SESSION_VARS = $_SESSION;
	}
}

// Protect against GLOBALS tricks
if (isset($HTTP_POST_VARS['GLOBALS']) || isset($HTTP_POST_FILES['GLOBALS']) || isset($HTTP_GET_VARS['GLOBALS']) || isset($HTTP_COOKIE_VARS['GLOBALS']))
{
 	die("Hacking attempt");
}
 
// Protect against HTTP_SESSION_VARS tricks
if (isset($HTTP_SESSION_VARS) && !is_array($HTTP_SESSION_VARS))
{
 	die("Hacking attempt");
}
 
if (@ini_get('register_globals') == '1' || strtolower(@ini_get('register_globals')) == 'on')
{
	// PHP4+ path
 	$not_unset = array('HTTP_GET_VARS', 'HTTP_POST_VARS', 'HTTP_COOKIE_VARS', 'HTTP_SERVER_VARS', 'HTTP_SESSION_VARS', 'HTTP_ENV_VARS', 'HTTP_POST_FILES', 'phpEx', 'phpbb_root_path');
	
	// Not only will array_merge give a warning if a parameter
	// is not an array, it will actually fail. So we check if
	// HTTP_SESSION_VARS has been initialised.
	if (!isset($HTTP_SESSION_VARS) || !is_array($HTTP_SESSION_VARS))
	{
		$HTTP_SESSION_VARS = array();
	}

	// Merge all into one extremely huge array; unset
	// this later
	$input = array_merge($HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES);

	unset($input['input']);
	unset($input['not_unset']);
	
	while (list($var,) = @each($input))
	{
		if (in_array($var, $not_unset))
		{
		    die('Hacking attempt!');
		}
		unset($$var);
	}
   
	unset($input);
}

//
// addslashes to vars if magic_quotes_gpc is off
// this is a security precaution to prevent someone
// trying to break out of a SQL statement.
//
if( !get_magic_quotes_gpc() )
{
	if( is_array($HTTP_GET_VARS) )
	{
		while( list($k, $v) = each($HTTP_GET_VARS) )
		{
			if( is_array($HTTP_GET_VARS[$k]) )
			{
				while( list($k2, $v2) = each($HTTP_GET_VARS[$k]) )
				{
					$HTTP_GET_VARS[$k][$k2] = addslashes($v2);
				}
				@reset($HTTP_GET_VARS[$k]);
			}
			else
			{
				$HTTP_GET_VARS[$k] = addslashes($v);
			}
		}
		@reset($HTTP_GET_VARS);
	}

	if( is_array($HTTP_POST_VARS) )
	{
		while( list($k, $v) = each($HTTP_POST_VARS) )
		{
			if( is_array($HTTP_POST_VARS[$k]) )
			{
				while( list($k2, $v2) = each($HTTP_POST_VARS[$k]) )
				{
					$HTTP_POST_VARS[$k][$k2] = addslashes($v2);
				}
				@reset($HTTP_POST_VARS[$k]);
			}
			else
			{
				$HTTP_POST_VARS[$k] = addslashes($v);
			}
		}
		@reset($HTTP_POST_VARS);
	}

	if( is_array($HTTP_COOKIE_VARS) )
	{
		while( list($k, $v) = each($HTTP_COOKIE_VARS) )
		{
			if( is_array($HTTP_COOKIE_VARS[$k]) )
			{
				while( list($k2, $v2) = each($HTTP_COOKIE_VARS[$k]) )
				{
					$HTTP_COOKIE_VARS[$k][$k2] = addslashes($v2);
				}
				@reset($HTTP_COOKIE_VARS[$k]);
			}
			else
			{
				$HTTP_COOKIE_VARS[$k] = addslashes($v);
			}
		}
		@reset($HTTP_COOKIE_VARS);
	}
}

//
// Define some basic configuration arrays this also prevents
// malicious rewriting of language and otherarray values via
// URI params
//
$board_config = array();
$userdata = array();
$theme = array();
$images = array();
$lang = array();
$nav_links = array();
$dss_seeded = false;
$gen_simple_header = FALSE;

//+----------------------------------------------
//| XOOPS mainfile include -- Koudanshi
//+----------------------------------------------
if (file_exists ('../../mainfile.php'))
	include('../../mainfile.php');
else
	include('../../../mainfile.php');
//$phpbb_root_path = XOOPS_ROOT_PATH.'/modules/xphpbbi/';

$dbms = 'mysql4';
$dbhost = XOOPS_DB_HOST;
$dbname = XOOPS_DB_NAME;
$dbuser = XOOPS_DB_USER;
$dbpasswd = XOOPS_DB_PASS;
$table_prefix = XOOPS_DB_PREFIX.'_phpbbi_';

//include($phpbb_root_path . 'includes/constants.php');
include($phpbb_root_path . 'includes/template.php');
include($phpbb_root_path . 'includes/sessions.php');
include($phpbb_root_path . 'includes/auth.php');
include($phpbb_root_path . 'includes/functions.php');
include($phpbb_root_path . 'includes/db.php');

// We do not need this any longer, unset for safety purposes
unset($dbpasswd);

//
// Obtain and encode users IP
//
// I'm removing HTTP_X_FORWARDED_FOR ... this may well cause other problems such as
// private range IP's appearing instead of the guilty routable IP, tough, don't
// even bother complaining ... go scream and shout at the idiots out there who feel
// "clever" is doing harm rather than good ... karma is a great thing ... :)
//
$client_ip = ( !empty($HTTP_SERVER_VARS['REMOTE_ADDR']) ) ? $HTTP_SERVER_VARS['REMOTE_ADDR'] : ( ( !empty($HTTP_ENV_VARS['REMOTE_ADDR']) ) ? $HTTP_ENV_VARS['REMOTE_ADDR'] : getenv('$REMOTE_ADDR') );
$user_ip = encode_ip($client_ip);

//
// Setup forum wide options, if this fails
// then we output a CRITICAL_ERROR since
// basic forum information is not available
//
$sql = "SELECT *
	FROM " . CONFIG_TABLE;
if( !($result = $db->sql_query($sql)) )
{
	message_die(CRITICAL_ERROR, "Could not query config information", "", __LINE__, __FILE__, $sql);
}

while ( $row = $db->sql_fetchrow($result) )
{
	$board_config[$row['config_name']] = $row['config_value'];
}

if (file_exists('contrib'))
{
	message_die(GENERAL_MESSAGE, 'Please ensure both the contrib/ directories are deleted');
}

//+----------------------------------------------
//| Update config table -- Koudanshi
//+----------------------------------------------
if ((is_object($xoopsUser) && $xoopsUserIsAdmin) && (empty($board_config['server_name']) || $board_config['server_name'] != $HTTP_SERVER_VARS['HTTP_HOST']))
{
	if( !($db->sql_query("UPDATE ".CONFIG_TABLE." SET config_value = '".$HTTP_SERVER_VARS['HTTP_HOST']."' WHERE config_name = 'server_name'")) )
		message_die(CRITICAL_ERROR, "Could not update <font color=red>server_name</font> config information in the first time", "", __LINE__, __FILE__, $sql);

	if( !($db->sql_query("UPDATE ".CONFIG_TABLE." SET config_value = '".time()."' WHERE config_name = 'board_startdate'")) )
		message_die(CRITICAL_ERROR, "Could not update <font color=red>Board start date</font> config information in the first time", "", __LINE__, __FILE__, $sql);
}

// --- Update script_path --- ALF --- 13.05.2005 --------------------------
/*
$scrpt_path = $_SERVER['PHP_SELF'];
$scrpt_path = preg_replace("/xphpbbi(.*)/","xphpbbi/",$scrpt_path);
if (empty($board_config['script_path']) || $board_config['script_path'] != $scrpt_path)
{
	if( !($db->sql_query("UPDATE ".CONFIG_TABLE." SET config_value = '".$scrpt_path."' WHERE config_name = 'script_path'")) )
		message_die(CRITICAL_ERROR, "Could not update <font color=red>script_path</font> config information in the first time", "", __LINE__, __FILE__, $sql);
}
*/
// --- Update script_path --- ALF --- 13.05.2005 --------------------------

//+----------------------------------------------
//| Update version
//+----------------------------------------------
if (defined("IN_ADMIN")){
    if (list($current_version_bb) = $xoopsDB->fetchRow($xoopsDB->query("SELECT config_value FROM ".CONFIG_TABLE." WHERE config_name = 'version_bb'"))){
	require XOOPS_ROOT_PATH.$board_config['script_path']."xoops_version.php";
 	$version_bb = $modversion['version'];
 	$version_phpbb = explode('.', $modversion['version_phpbb']);
 	$version_phpbb = ".".$version_phpbb[1].".".$version_phpbb[2];

	if ($current_version_bb != $version_bb){
	    message_die(GENERAL_MESSAGE, "Please pass on <a href=\"".XOOPS_URL.$board_config['script_path']."update/update.php\" target=\"_blank\">this hyperlink</a> and <font color=red>start a script of updating of your module up to last version</font>", "", "", "", "");

	    if( !($db->sql_query ("UPDATE ".CONFIG_TABLE." SET config_value = '".$version_phpbb."' WHERE config_name = 'version' "))){
		message_die(CRITICAL_ERROR, "Could not update <font color=red>version</font> config information in the first time", "", __LINE__, __FILE__, $sql);
 	    }
 	    if( !($db->sql_query ("UPDATE ".CONFIG_TABLE." SET config_value = '".$version_bb."' WHERE config_name = 'version_bb' "))){
		 message_die(CRITICAL_ERROR, "Could not update <font color=red>version_bb</font> config information in the first time", "", __LINE__, __FILE__, $sql);
 	    }
	}
    }else{
	message_die(CRITICAL_ERROR, "Could not load <font color=red>version</font> config information", "", __LINE__, __FILE__, $sql);
    }
}
//+----------------------------------------------
// Update Anonymous user ID
if (defined("IN_ADMIN")) {
 		if( !($db->sql_query ("UPDATE ".USERS_TABLE." SET uid='0' WHERE uname = 'Anonymous'"))){
		  message_die(CRITICAL_ERROR, "Could not update <font color=red>Anonymous ID</font>", "", __LINE__, __FILE__, $sql);
 		}
 		if( !($db->sql_query ("UPDATE ".USERS_TABLE_EXT." SET user_level='0' WHERE uid='0'"))){
		  message_die(CRITICAL_ERROR, "Could not update <font color=red>Anonymous ID</font>", "", __LINE__, __FILE__, $sql);
 		}
}

if (file_exists('install') || file_exists('contrib')) {
	message_die(GENERAL_MESSAGE, 'Please ensure both the install/ and contrib/ directories are deleted');
}
//
// Show 'Board is disabled' message if needed.
//
if( $board_config['board_disable'] && !defined("IN_ADMIN") && !defined("IN_LOGIN") )
{
	message_die(GENERAL_MESSAGE, 'Board_disable', 'Information');
}

?>