<?php
@set_time_limit(10000);
require('../functions.inc.php');
db_login();
$sql_file = "main.sql";

function split_sql_file($sql, $delimiter)
    {
        $sql               = trim($sql);
        $char              = '';
        $last_char         = '';
        $ret               = array();
        $string_start      = '';
        $in_string         = FALSE;
        $escaped_backslash = FALSE;

        for ($i = 0; $i < strlen($sql); ++$i) {
            $char = $sql[$i];

            if ($char == $delimiter && !$in_string) {
                $ret[]     = substr($sql, 0, $i);
                $sql       = substr($sql, $i + 1);
                $i         = 0;
                $last_char = '';
            }

            if ($in_string) {
                if ($char == '\\') {
                    if ($last_char != '\\') {
                        $escaped_backslash = FALSE;
                    } else {
                        $escaped_backslash = !$escaped_backslash;
                    }
                }
                if (($char == $string_start)
                    && ($char == '`' || !(($last_char == '\\') && !$escaped_backslash))) {
                    $in_string    = FALSE;
                    $string_start = '';
                }
            } else {
                if (($char == '"') || ($char == '\'') || ($char == '`')) {
                    $in_string    = TRUE;
                    $string_start = $char;
                }
            }
            $last_char = $char;
        } 

        if (!empty($sql)) {
            $ret[] = $sql;
        }
        return $ret;
    }


     
     
     
     if (file_exists($sql_file)) {
        $sql_query = fread(fopen($sql_file, 'r'), filesize($sql_file));
        if (get_magic_quotes_runtime() == 1) {
            $sql_query = stripslashes($sql_query);
        }
    }

else if (empty($id_bookmark) && get_magic_quotes_gpc() == 1) {
     $sql_query = stripslashes($sql_query);
}
$sql_query = trim($sql_query);
if (!empty($prev_sql_query)) {
    $prev_sql_query = urldecode($prev_sql_query);
    if ($sql_query == trim(htmlspecialchars($prev_sql_query))) {
        $sql_query  = $prev_sql_query;
    }
}

if ($sql_query != '') {
    $pieces       = split_sql_file($sql_query, ';');
    $pieces_count = count($pieces);

       for ($i = 0; $i < $pieces_count; $i++) {
            $a_sql_query = trim($pieces[$i]);
            if (!empty($a_sql_query) && $a_sql_query[0] != '#') {
                @$result = mysql_query($a_sql_query);
                if ($result == FALSE) { // readdump failed
                    die("<font color=red><b>MYSQL ERROR. PLEASE MAIL TO <a href=mailto:webmaster@nukestore.com >DEVELOPER.</a></b></font>");
                }
            }
            if (!isset($reload) && eregi('^(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)', $a_sql_query)) {
                $reload = 'true';
            }
        } 
  } 
echo "<b>Database successfully initialized. All queries executed.</b>";
echo "<BR><BR><BR><BR><BR>";
echo "Note: now you can safely delete this file from server. It is needed only for mysql tables initialization";

?>