#!/usr/local/cpanel/3rdparty/bin/php
<?php
define('PHPBB_ROOT_PATH', './');
define('IN_PHPBB', 'yeah... sure');

$phpbb_root_path = PHPBB_ROOT_PATH;
$phpEx = 'php';

#$acm_type = '';
$dbms = 'mysql';
include("common.php");

$errored = false;
$error_ary = array();

//
// What follows was extracted from the install/database_upgrade.php file
// supplied with phpBB, release 3.0.12.
//

if (!function_exists('phpbb_require_updated'))
{
	function phpbb_require_updated($path, $optional = false)
	{
		global $phpbb_root_path;

		$new_path = $phpbb_root_path . 'install/update/new/' . $path;
		$old_path = $phpbb_root_path . $path;

		if (file_exists($new_path))
		{
			require($new_path);
		}
		else if (!$optional || file_exists($old_path))
		{
			require($old_path);
		}
	}
}

//
// ... more of database_upgrade.php ...
//

/**
* Function for triggering an sql statement
*/
function _sql($sql, &$errored, &$error_ary, $echo_dot = true)
{
	global $db;

	if (defined('DEBUG_EXTRA'))
	{
		echo "<br />\n{$sql}\n<br />";
	}

	$db->sql_return_on_error(true);

	if ($sql === 'begin')
	{
		$result = $db->sql_transaction('begin');
	}
	else if ($sql === 'commit')
	{
		$result = $db->sql_transaction('commit');
	}
	else
	{
		$result = $db->sql_query($sql);
		if ($db->sql_error_triggered)
		{
			$errored = true;
			$error_ary['sql'][] = $db->sql_error_sql;
			$error_ary['error_code'][] = $db->sql_error_returned;
		}
	}

	$db->sql_return_on_error(false);

	if ($echo_dot)
	{
		echo ". \n";
		flush();
	}

	return $result;
}

//
// ... and what follows is from inside the switch statement in change_database_data()
// from database_upgrade.php, specifically the entry for "3.0.11".  If you intend to
// reuse this script for a later version upgrade, replace the below with the contents
// from that version's case statement.
//
// You may also need to add code to handle the values in database_update_info().
//
// And now, back to the code...
//

// Changes from 3.0.11 to 3.0.12-RC1
$sql = 'UPDATE ' . MODULES_TABLE . '
    SET module_auth = \'acl_u_sig\'
    WHERE module_class = \'ucp\'
        AND module_basename = \'profile\'
        AND module_mode = \'signature\'';
_sql($sql, $errored, $error_ary);

// Update bots
if (!function_exists('user_delete'))
{
    include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
}

$bots_updates = array(
    // Bot Deletions
    'NG-Search [Bot]'		=> false,
    'Nutch/CVS [Bot]'		=> false,
    'OmniExplorer [Bot]'	=> false,
    'Seekport [Bot]'		=> false,
    'Synoo [Bot]'			=> false,
    'WiseNut [Bot]'			=> false,

    // Bot Updates
    // Bot name to bot user agent map
    'Baidu [Spider]'	=> 'Baiduspider',
    'Exabot [Bot]'		=> 'Exabot',
    'Voyager [Bot]'		=> 'voyager/',
    'W3C [Validator]'	=> 'W3C_Validator',
);

foreach ($bots_updates as $bot_name => $bot_agent)
{
    $sql = 'SELECT user_id
        FROM ' . USERS_TABLE . '
        WHERE user_type = ' . USER_IGNORE . "
            AND username_clean = '" . $db->sql_escape(utf8_clean_string($bot_name)) . "'";
    $result = $db->sql_query($sql);
    $bot_user_id = (int) $db->sql_fetchfield('user_id');
    $db->sql_freeresult($result);

    if ($bot_user_id)
    {
        if ($bot_agent === false)
        {
            $sql = 'DELETE FROM ' . BOTS_TABLE . "
                WHERE user_id = $bot_user_id";
            _sql($sql, $errored, $error_ary);

            user_delete('remove', $bot_user_id);
        }
        else
        {
            $sql = 'UPDATE ' . BOTS_TABLE . "
                SET bot_agent = '" .  $db->sql_escape($bot_agent) . "'
                WHERE user_id = $bot_user_id";
            _sql($sql, $errored, $error_ary);
        }
    }
}

// Disable receiving pms for bots
$sql = 'SELECT user_id
    FROM ' . BOTS_TABLE;
$result = $db->sql_query($sql);

$bot_user_ids = array();
while ($row = $db->sql_fetchrow($result))
{
    $bot_user_ids[] = (int) $row['user_id'];
}
$db->sql_freeresult($result);

if (!empty($bot_user_ids))
{
    $sql = 'UPDATE ' . USERS_TABLE . '
        SET user_allow_pm = 0
        WHERE ' . $db->sql_in_set('user_id', $bot_user_ids);
    _sql($sql, $errored, $error_ary);
}

/**
* Update BBCodes that currently use the LOCAL_URL tag
*
* To fix http://tracker.phpbb.com/browse/PHPBB3-8319 we changed
* the second_pass_replace value, so that needs updating for existing ones
*/
$sql = 'SELECT *
    FROM ' . BBCODES_TABLE . '
    WHERE bbcode_match ' . $db->sql_like_expression($db->any_char . 'LOCAL_URL' . $db->any_char);
$result = $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result))
{
    if (!class_exists('acp_bbcodes'))
    {
        phpbb_require_updated('includes/acp/acp_bbcodes.' . $phpEx);
    }
    $bbcode_match = $row['bbcode_match'];
    $bbcode_tpl = $row['bbcode_tpl'];

    $acp_bbcodes = new acp_bbcodes();
    $sql_ary = $acp_bbcodes->build_regexp($bbcode_match, $bbcode_tpl);

    $sql = 'UPDATE ' . BBCODES_TABLE . '
        SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
        WHERE bbcode_id = ' . (int) $row['bbcode_id'];
    $db->sql_query($sql);
}
$db->sql_freeresult($result);

# Update version number.
$sql = 'UPDATE ' . CONFIG_TABLE . ' SET config_value=\'3.0.12\' WHERE config_name=\'version\'';
_sql($sql, $errored, $error_ary);
?>
