<?php
/*
// ------------------------------------------------------------------------
-+ Date: 02-Feb-2007
-+ Version: xphpbbi-1.07
-+ ========================================
-+ Made by Alf
-+ E-mail: alf@uzlovaya.ru
-+ Homepage: www.uzlovaya.ru
-+ ICQ: 104457969
-+ ========================================
-+ Any Problems please email or ICQ me.
-+ 
-+ ========================================
\\ ------------------------------------------------------------------------
*/
include "../../../mainfile.php";
include XOOPS_ROOT_PATH."/header.php";

if (!defined('IN_PHPBB')) define('IN_PHPBB', true);
$phpbb_root_path = '../';
include($phpbb_root_path . "common.php");

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];

echo "<html><head><title></title></head><body>";

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
//
// End session management
//

$IsAdmin = false;
if ($xoopsUserIsAdmin || $userdata['user_level'] == ADMIN) $IsAdmin = true;
if (!$IsAdmin)
    message_die(GENERAL_ERROR, "You are not Authorised to do this"); 

if (isset($HTTP_POST_VARS['submit'])) {

    if (!$xoopsDB->query("SELECT uid FROM ".AUTH_ACCESS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".AUTH_ACCESS_TABLE." ADD uid MEDIUMINT(8) NOT NULL DEFAULT '0'");
    }
    if (!$xoopsDB->query("SELECT auth_ban FROM ".AUTH_ACCESS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".AUTH_ACCESS_TABLE." ADD auth_ban TINYINT (1) not null DEFAULT '0'");
    }
    if (!$xoopsDB->query("SELECT auth_greencard FROM ".AUTH_ACCESS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".AUTH_ACCESS_TABLE." ADD auth_greencard TINYINT (1) not null DEFAULT '0'");
    }
    if (!$xoopsDB->query("SELECT auth_bluecard FROM ".AUTH_ACCESS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".AUTH_ACCESS_TABLE." ADD auth_bluecard TINYINT (1) not null DEFAULT '0'");
    }

    if (!$xoopsDB->query("SELECT config_value FROM ".CONFIG_TABLE." WHERE config_name='bluecard_limit'")) {
	$xoopsDB->query("INSERT INTO ".CONFIG_TABLE." (config_name, config_value) VALUES ('bluecard_limit', '3')");
    }
    if (!$xoopsDB->query("SELECT config_value FROM ".CONFIG_TABLE." WHERE config_name='bluecard_limit_2'")) {
	$xoopsDB->query("INSERT INTO ".CONFIG_TABLE." (config_name, config_value) VALUES ('bluecard_limit_2', '1')");
    }
    if (!$xoopsDB->query("SELECT config_value FROM ".CONFIG_TABLE." WHERE config_name='max_user_bancard'")) {
	$xoopsDB->query("INSERT INTO ".CONFIG_TABLE." (config_name, config_value) VALUES ('max_user_bancard', '10')");
    }
    if (!$xoopsDB->query("SELECT config_value FROM ".CONFIG_TABLE." WHERE config_name='report_forum'")) {
	$xoopsDB->query("INSERT INTO ".CONFIG_TABLE." (config_name, config_value) VALUES ('report_forum', '0')");
    }
    if (!$xoopsDB->query("SELECT config_value FROM ".CONFIG_TABLE." WHERE config_name='bin_forum'")) {
	$xoopsDB->query("INSERT INTO ".CONFIG_TABLE." (config_name, config_value) VALUES ('bin_forum', '0')");
    }
    if (!$xoopsDB->query("SELECT config_value FROM ".CONFIG_TABLE." WHERE config_name='search_flood_interval'")) {
	$xoopsDB->query("INSERT INTO ".CONFIG_TABLE." (config_name, config_value) VALUES ('search_flood_interval', '15')");
    }
    if (!$xoopsDB->query("SELECT config_value FROM ".CONFIG_TABLE." WHERE config_name='rand_seed'")) {
	$xoopsDB->query("INSERT INTO ".CONFIG_TABLE." (config_name, config_value) VALUES ('rand_seed', '0')");
    }
    if (!$xoopsDB->query("SELECT config_value FROM ".CONFIG_TABLE." WHERE config_name='search_min_chars'")) {
	$xoopsDB->query("INSERT INTO ".CONFIG_TABLE." (config_name, config_value) VALUES ('search_min_chars', '3')");
    }

    if (!$xoopsDB->query("SELECT auth_ban FROM ".FORUMS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".FORUMS_TABLE." ADD auth_ban TINYINT (2) not null DEFAULT '3'");
    }
    if (!$xoopsDB->query("SELECT auth_greencard FROM ".FORUMS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".FORUMS_TABLE." ADD auth_greencard TINYINT (2) not null DEFAULT '5'");
    }
    if (!$xoopsDB->query("SELECT auth_bluecard FROM ".FORUMS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".FORUMS_TABLE." ADD auth_bluecard TINYINT (2) not null DEFAULT '1'");
    }

    if (!$xoopsDB->query("SELECT post_bluecard FROM ".POSTS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".POSTS_TABLE." ADD post_bluecard TINYINT (1)");
    }
    if (!$xoopsDB->query("SELECT post_created FROM ".POSTS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".POSTS_TABLE." ADD post_created INT (11) not null DEFAULT '0'");
	$xoopsDB->queryF("UPDATE ".POSTS_TABLE." SET post_created = post_time");
    }

    if (!$xoopsDB->query("SELECT search_time FROM ".SEARCH_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".SEARCH_TABLE." ADD `search_time` TINYINT( 11 ) NOT NULL");
    }
    $xoopsDB->query("ALTER TABLE ".SEARCH_TABLE." MODIFY COLUMN `search_array` MEDIUMTEXT NOT NULL");

    if (!$xoopsDB->query("SELECT session_admin FROM ".SESSIONS_TABLE_EXT)) {
	$xoopsDB->query("ALTER TABLE ".SESSIONS_TABLE_EXT." ADD `session_admin` TINYINT( 2 ) NOT NULL");
    }
    if ($xoopsDB->query("SELECT user_warnings FROM ".SESSIONS_TABLE_EXT)) {
	$xoopsDB->query("ALTER TABLE ".SESSIONS_TABLE_EXT." DROP `user_warnings`");
    }

    if (!$xoopsDB->query("SELECT user_warnings FROM ".USERS_TABLE_EXT)) {
	$xoopsDB->query("ALTER TABLE ".USERS_TABLE_EXT." ADD user_warnings SMALLINT (5)");
    }
    if (!$xoopsDB->query("SELECT user_login_tries FROM ".USERS_TABLE_EXT)) {
	$xoopsDB->query("ALTER TABLE ".USERS_TABLE_EXT." ADD user_login_tries SMALLINT (5) UNSIGNED not null");
    }
    if (!$xoopsDB->query("SELECT user_last_login_try FROM ".USERS_TABLE_EXT)) {
	$xoopsDB->query("ALTER TABLE ".USERS_TABLE_EXT." ADD user_last_login_try INT (11) not null");
    }
    if (!$xoopsDB->query("SELECT user_view_log FROM ".USERS_TABLE_EXT)) {
	$xoopsDB->query("ALTER TABLE ".USERS_TABLE_EXT." ADD user_view_log TINYINT NOT NULL DEFAULT '0'");
    }

    if (!$xoopsDB->query("SELECT vote_cast FROM ".VOTE_USERS_TABLE)) {
	$xoopsDB->query("ALTER TABLE ".VOTE_USERS_TABLE." ADD vote_cast TINYINT (4) UNSIGNED not null");
    }

	$xoopsDB->query("CREATE TABLE IF NOT EXISTS ".SESSIONS_KEYS_TABLE." (
			`key_id` varchar(32) NOT NULL, 
			`user_id` mediumint(8) NOT NULL, 
			`last_ip` varchar(8) NOT NULL, 
			`last_login` int(11) NOT NULL, 
			PRIMARY KEY (`key_id`, `user_id`), KEY `last_login` (`last_login`))");

	$xoopsDB->query("CREATE TABLE IF NOT EXISTS ".LOGS_TABLE." (
			id_log MEDIUMINT(10) NOT NULL AUTO_INCREMENT,
			mode VARCHAR(50) NULL DEFAULT '', 
			topic_id MEDIUMINT(10) NULL DEFAULT '0',
			user_id MEDIUMINT(8) NULL DEFAULT '0',
			username VARCHAR(255) NULL DEFAULT '',
			user_ip CHAR(8) DEFAULT '0' NOT NULL,
			time INT(11) NULL DEFAULT '0',
			PRIMARY KEY (id_log))");

	$xoopsDB->query("CREATE TABLE IF NOT EXISTS ".LOGS_CONFIG_TABLE." (
			config_name varchar(255) NOT NULL,
			config_value varchar(255) NOT NULL,
			PRIMARY KEY (config_name))");

    if (!$xoopsDB->query("SELECT config_value FROM ".LOGS_CONFIG_TABLE." WHERE config_name='all_admin'")) {
	$xoopsDB->query("INSERT INTO ".LOGS_CONFIG_TABLE." (config_name, config_value) VALUES ('all_admin', '0')");
    }
    if (!$xoopsDB->query("SELECT config_value FROM ".LOGS_CONFIG_TABLE." WHERE config_name='time_to_merge'")) {
	$xoopsDB->query("INSERT INTO ".LOGS_CONFIG_TABLE." (config_name, config_value) VALUES ('time_to_merge', '0')");
    }
    if (!$xoopsDB->query("SELECT config_value FROM ".LOGS_CONFIG_TABLE." WHERE config_name='merge_flood_interval'")) {
	$xoopsDB->query("INSERT INTO ".LOGS_CONFIG_TABLE." (config_name, config_value) VALUES ('merge_flood_interval', '0')");
    }

	$xoopsDB->query("DELETE FROM ".AUTH_ACCESS_TABLE." WHERE `uid`='0'"); // delete Anonymous from Moderators!
	$xoopsDB->query("DELETE FROM ".SESSIONS_TABLE_EXT);
	$xoopsDB->query("DELETE FROM ".SESSIONS_KEYS_TABLE);

	$xoopsDB->query("UPDATE ".CONFIG_TABLE." SET config_value='".$version_bb."' WHERE config_name='version_bb'");
	$xoopsDB->query("UPDATE ".CONFIG_TABLE." SET config_value='".$version_phpbb."' WHERE config_name='version'");

	echo $xoopsLogger->dumpQueries();
	echo "<h3>Updated module XPHPBBI to version ".$version_bb." (phpBB-".$modversion['version_phpbb'].")</h3>";
} else {

	echo "<h3>Press the button to update XPHPBBI v.".$version_bb."!!!</h3><br />

<form action=\"update.php\" method=\"post\">
<input type=\"submit\" name=\"submit\" value=\""._SUBMIT."\" />
</form>";
}

echo "</body></html>";

include XOOPS_ROOT_PATH."/footer.php";

?>