<?php
/*
*-------------------------------phpMyBitTorrent--------------------------------*
*--- The Ultimate BitTorrent Tracker and BMS (Bittorrent Management System) ---*
*--------------   Created By Antonio Anzivino (aka DJ Echelon)   --------------*
*-------------               http://www.p2pmania.it               -------------*
*------------ Based on the Bit Torrent Protocol made by Bram Cohen ------------*
*-------------              http://www.bittorrent.com             -------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*--   This program is free software; you can redistribute it and/or modify   --*
*--   it under the terms of the GNU General Public License as published by   --*
*--   the Free Software Foundation; either version 2 of the License, or      --*
*--   (at your option) any later version.                                    --*
*--                                                                          --*
*--   This program is distributed in the hope that it will be useful,        --*
*--   but WITHOUT ANY WARRANTY; without even the implied warranty of         --*
*--   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          --*
*--   GNU General Public License for more details.                           --*
*--                                                                          --*
*--   You should have received a copy of the GNU General Public License      --*
*--   along with this program; if not, write to the Free Software            --*
*-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA --*
*--                                                                          --*
*------------------------------------------------------------------------------*
*------              ©2005 phpMyBitTorrent Development Team              ------*
*-----------               http://phpmybittorrent.com               -----------*
*------------------------------------------------------------------------------*
*/

require_once("../include/configdata.php");
require_once("udl/database.php");


function btsqlerror($sql) { //Returns SQL Error
        global $db;
        $err = Array();
        $err = $db->sql_error();
        echo "<br />\n";
        echo "<font class=\"err\">";
        echo _btsqlerror1.$sql;
        echo "<br />";
        echo _btsqlerror2.$err["code"];
        echo "<br />";
        echo _btsqlerror3.$err["message"];
        echo "</font>";
        $db->sql_query("",END_TRANSACTION);
}

echo "<p align=\"center\"><font size=\"5\">"._step4."</font></p>\n";
echo "<p>&nbsp</p>";

$db = new sql_db($db_host, $db_user, $db_pass, $db_name, $db_persistency);

$can_proceed = true;
$db->sql_query("",BEGIN_TRANSACTION);

echo "<p>"._checkingfiles;
foreach(explode(":","install-".$db_type.":smiles:categories-".$language) as $sqlscript) {
        if (!is_readable("sql/".$sqlscript.".sql")) {
                $can_proceed = false;
                echo "<br /><font class=\"err\">".str_replace("**file**","install-".$db_type.".sql",_step4fnotfound)."</font>";
        }
}
if ($can_proceed) echo "<font class=\ok\">OK</font>";
echo "</p>";


#Creating Tables
if ($can_proceed) {
        $fp = @fopen("sql/install-".$db_type.".sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>";
        //preg_match_all('/^(?P<query>CREATE TABLE `(?P<table>[\\w]*)`[^;]*;)$/sim', $installscript, $scripts);
        $scripts = explode(";",$installscript);
        //print_r($scripts);
        unset($installscript);
        foreach ($scripts as $script) {
                if (!preg_match('/^CREATE TABLE `#prefix#_([\\w]*)`[^;]*$/sim', $script, $matches)) continue;
                $script .= ";"; //Splitting string removes semicolon
                $script = str_replace("#prefix#",$db_prefix,$script);
                echo "<p>".str_replace("**table**",$matches[1],_tblcreating);
                if (!$db->sql_query($script)) {
                        $can_proceed = false;
                        btsqlerror($script);
                        break;
                } else echo "<font class=\"ok\">OK</font>";
                echo "</p>\n";
                unset($script, $matches);
        }
        unset($scripts);
}

#Inserting default smileys
if ($can_proceed) {
        $fp = @fopen("sql/smiles.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>"._installsmiles;
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}
#Inserting default Pay-pal
if ($can_proceed) {
        $fp = @fopen("sql/pay-pal.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>"._installsmiles;
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}

if ($can_proceed) {
        $fp = @fopen("sql/categories-".$language.".sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>"._installcategories;
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}

if ($can_proceed) {
        $fp = @fopen("sql/bonus-config.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Adding Bonuse Configs";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}

if ($can_proceed) {
        $fp = @fopen("sql/bonus-settings.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Creating Bonuse Settings";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}
if ($can_proceed) {
        $fp = @fopen("sql/img-bucket-config.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Adding Image Bucket Configs";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}

if ($can_proceed) {
        $fp = @fopen("sql/cards.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Creating Black Jack Cards";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}

if ($can_proceed) {
        $fp = @fopen("sql/casino_config.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Creating default Casino Configs";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}

if ($can_proceed) {
        $fp = @fopen("sql/countries.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Creating Country Flags";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}

if ($can_proceed) {
        $fp = @fopen("sql/forum-configs.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Creating default Forum Configs";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}

if ($can_proceed) {
        $fp = @fopen("sql/lottery_config.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Creating default Lottery Configs";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}
if ($can_proceed) {
        $fp = @fopen("sql/offres_config.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Creating default Offers Configs";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}
if ($can_proceed) {
        $fp = @fopen("sql/time_offset.sql","r");
        $installscript = "";
        while (!feof($fp)) $installscript .= @fgets($fp,1000);
        @fclose($fp);
        unset($fp);
        $installscript = str_replace("#prefix#",$db_prefix,$installscript);
        //echo "<p><textarea rows=\"4\" cols=\"65\">".htmlspecialchars($installscript)."</textarea></p>\n";
        echo "<p>Creating Time zone Offset";
        if (!$db->sql_query($installscript)) {
                $can_proceed = false;
                btsqlerror(substr($installscript,0,100)."..."); //Shorted for better display
        } else echo "<font class=\"ok\">OK</font>";
        echo "</p>\n";
        unset($installscript);
}



if ($can_proceed) {
        echo "<p>"._step4complete."</p>";
        echo "<p><input type=\"submit\" value=\""._nextstep."\" /></p>\n";
        echo "<input type=\"hidden\" name=\"step\" value=\"5\" />\n";
} else {
        echo "<p>"._step4failed."</p>";
}

$db->sql_query("",END_TRANSACTION);
$db->sql_close();

?>