<?php
// MySQL to MySQLi Shim for vBulletin 3 on PHP 8.0+

if (!function_exists('mysql_connect')) {
    function mysql_connect($server, $username, $password, $new_link = false, $client_flags = 0) {
        $host = $server;
        $port = ini_get("mysqli.default_port");
        
        if (strpos($server, ':') !== false) {
            list($host, $port) = explode(':', $server);
        }
        
        $link = mysqli_connect($host, $username, $password, "", $port);
        return $link;
    }
}

if (!function_exists('mysql_pconnect')) {
    function mysql_pconnect($server, $username, $password, $client_flags = 0) {
        $host = $server;
        $port = ini_get("mysqli.default_port");
        
        if (strpos($server, ':') !== false) {
            list($host, $port) = explode(':', $server);
        }
        
        $link = mysqli_connect("p:" . $host, $username, $password, "", $port);
        return $link;
    }
}

if (!function_exists('mysql_select_db')) {
    function mysql_select_db($database_name, $link_identifier = null) {
        return mysqli_select_db($link_identifier, $database_name);
    }
}

if (!function_exists('mysql_query')) {
    function mysql_query($query, $link_identifier = null) {
        return mysqli_query($link_identifier, $query);
    }
}

if (!function_exists('mysql_unbuffered_query')) {
    function mysql_unbuffered_query($query, $link_identifier = null) {
        return mysqli_query($link_identifier, $query, MYSQLI_USE_RESULT);
    }
}

if (!function_exists('mysql_fetch_array')) {
    function mysql_fetch_array($result, $result_type = MYSQLI_BOTH) {
        return mysqli_fetch_array($result, $result_type);
    }
}

if (!function_exists('mysql_fetch_row')) {
    function mysql_fetch_row($result) {
        return mysqli_fetch_row($result);
    }
}

if (!function_exists('mysql_fetch_assoc')) {
    function mysql_fetch_assoc($result) {
        return mysqli_fetch_assoc($result);
    }
}

if (!function_exists('mysql_fetch_field')) {
    function mysql_fetch_field($result, $field_offset = 0) {
        // mysqli_fetch_field does not support offset, so we might need to seek or loop
        // vBulletin 3 might rely on offset.
        // Simplified implementation:
        return mysqli_fetch_field($result);
    }
}

if (!function_exists('mysql_num_rows')) {
    function mysql_num_rows($result) {
        return mysqli_num_rows($result);
    }
}

if (!function_exists('mysql_num_fields')) {
    function mysql_num_fields($result) {
        return mysqli_num_fields($result);
    }
}

if (!function_exists('mysql_field_name')) {
    function mysql_field_name($result, $field_offset) {
        $field = mysqli_fetch_field_direct($result, $field_offset);
        return $field ? $field->name : false;
    }
}

if (!function_exists('mysql_insert_id')) {
    function mysql_insert_id($link_identifier = null) {
        return mysqli_insert_id($link_identifier);
    }
}

if (!function_exists('mysql_error')) {
    function mysql_error($link_identifier = null) {
        return mysqli_error($link_identifier);
    }
}

if (!function_exists('mysql_errno')) {
    function mysql_errno($link_identifier = null) {
        return mysqli_errno($link_identifier);
    }
}

if (!function_exists('mysql_escape_string')) {
    function mysql_escape_string($unescaped_string) {
        // Deprecated and dangerous, but commonly used in old code sans link
        // We can't easily implement this without a link in mysqli.
        // Returning as is or using str_replace is risky but better than crash.
        // Ideally should be mysql_real_escape_string with a link.
        // vBulletin uses it in vB_Database::escape_string only if real_escape_string is not available.
        return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $unescaped_string);
    }
}

if (!function_exists('mysql_real_escape_string')) {
    function mysql_real_escape_string($unescaped_string, $link_identifier = null) {
        return mysqli_real_escape_string($link_identifier, $unescaped_string);
    }
}

if (!function_exists('mysql_close')) {
    function mysql_close($link_identifier = null) {
        return mysqli_close($link_identifier);
    }
}

if (!function_exists('mysql_client_encoding')) {
    function mysql_client_encoding($link_identifier = null) {
        return mysqli_character_set_name($link_identifier);
    }
}

if (!function_exists('mysql_get_server_info')) {
    function mysql_get_server_info($link_identifier = null) {
        return mysqli_get_server_info($link_identifier);
    }
}

if (!function_exists('mysql_free_result')) {
    function mysql_free_result($result) {
        return mysqli_free_result($result);
    }
}

if (!function_exists('mysql_data_seek')) {
    function mysql_data_seek($result, $row_number) {
        return mysqli_data_seek($result, $row_number);
    }
}

if (!function_exists('mysql_affected_rows')) {
    function mysql_affected_rows($link_identifier = null) {
        return mysqli_affected_rows($link_identifier);
    }
}


// Constants
if (!defined('MYSQL_ASSOC')) define('MYSQL_ASSOC', MYSQLI_ASSOC);
if (!defined('MYSQL_NUM')) define('MYSQL_NUM', MYSQLI_NUM);
if (!defined('MYSQL_BOTH')) define('MYSQL_BOTH', MYSQLI_BOTH);
if (!defined('MYSQL_CLIENT_COMPRESS')) define('MYSQL_CLIENT_COMPRESS', 32);
