function xtc_db_test_create_db_permission($database, $link = 'db_link')
{
    global $db_error;
    global ${$link};
    $db_created = false;
    $db_error = false;
    if (!$database) {
        $db_error = 'No Database selected.';
        return false;
    }
    if (!$db_error) {
        if (!@xtc_db_select_db($database)) {
            $db_created = true;
            // BOF - Dokuman - 2009-05-27 - xtc_db_query_installer typo
            //        if (!@xtc_db_query_installer_installer('create database ' . $database)) {
            if (!@xtc_db_query_installer('create database ' . $database)) {
                // EOF - Dokuman - 2009-05-27 - xtc_db_query_installer typo
                $db_error = mysqli_error(${$link});
            }
        } else {
            $db_error = mysqli_error(${$link});
        }
        if (!$db_error) {
            if (@xtc_db_select_db($database)) {
                if (@xtc_db_query_installer('create table temp ( temp_id int(5) )')) {
                    if (@xtc_db_query_installer('drop table temp')) {
                        if ($db_created) {
                            if (@xtc_db_query_installer('drop database ' . $database)) {
                            } else {
                                $db_error = mysqli_error(${$link});
                            }
                        }
                    } else {
                        $db_error = mysqli_error(${$link});
                    }
                } else {
                    $db_error = mysqli_error(${$link});
                }
            } else {
                $db_error = mysqli_error(${$link});
            }
        }
    }
    if ($db_error) {
        return false;
    } else {
        return true;
    }
}
function xtc_db_test_connection($database, $link = 'db_link')
{
    global $db_error;
    global ${$link};
    $db_error = false;
    if (!$db_error) {
        if (!@xtc_db_select_db($database)) {
            $db_error = mysqli_error(${$link});
        } else {
            if (!@xtc_db_query_installer('select count(*) from configuration')) {
                $db_error = mysqli_error(${$link});
            }
        }
    }
    if ($db_error) {
        return false;
    } else {
        return true;
    }
}
function xtc_db_install($database, $sql_file, $link = 'db_link')
{
    global $db_error;
    global ${$link};
    $db_error = false;
    if (!@xtc_db_select_db($database)) {
        if (@xtc_db_query_installer('create database `' . $database . '`')) {
            xtc_db_select_db($database);
        } else {
            $db_error = mysqli_error(${$link});
        }
    }
    if (!$db_error) {
        if (file_exists($sql_file)) {
            $fd = fopen($sql_file, 'rb');
            $restore_query = fread($fd, filesize($sql_file));
            fclose($fd);
        } else {
            $db_error = 'SQL file does not exist: ' . $sql_file;
            return false;
        }
        $sql_array = array();
        $sql_length = strlen($restore_query);
        $pos = strpos($restore_query, ';');
        for ($i = $pos; $i < $sql_length; $i++) {
            if ($restore_query[0] == '#') {
                $restore_query = ltrim(substr($restore_query, strpos($restore_query, "\n")));
                $sql_length = strlen($restore_query);
                $i = strpos($restore_query, ';') - 1;
                continue;
            }
            if ($restore_query[$i + 1] == "\n") {
                for ($j = $i + 2; $j < $sql_length; $j++) {
                    if (trim($restore_query[$j]) != '') {
                        $next = substr($restore_query, $j, 6);
                        if ($next[0] == '#') {
                            // find out where the break position is so we can remove this line (#comment line)
                            for ($k = $j; $k < $sql_length; $k++) {
                                if ($restore_query[$k] == "\n") {
                                    break;
                                }
                            }
                            $query = substr($restore_query, 0, $i + 1);
                            $restore_query = substr($restore_query, $k);
                            // join the query before the comment appeared, with the rest of the dump
                            $restore_query = $query . $restore_query;
                            $sql_length = strlen($restore_query);
                            $i = strpos($restore_query, ';') - 1;
                            continue 2;
                        }
                        break;
                    }
                }
                if ($next == '') {
                    // get the last insert query
                    $next = 'insert';
                }
                if (preg_match('/create/i', $next) || preg_match('/insert/i', $next) || preg_match('/drop t/i', $next)) {
                    // Hetfield - 2009-08-19 - replaced deprecated function eregi with preg_match to be ready for PHP >= 5.3
                    $next = '';
                    $sql_array[] = substr($restore_query, 0, $i);
                    $restore_query = ltrim(substr($restore_query, $i + 1));
                    $sql_length = strlen($restore_query);
                    $i = strpos($restore_query, ';') - 1;
                }
            }
        }
        for ($i = 0; $i < sizeof($sql_array); $i++) {
            xtc_db_query_installer($sql_array[$i]);
        }
    } else {
        return false;
    }
}