<?php
    error_reporting(E_ALL);
    define("_SECURE",1);    // define secure constant

    include "admin_header.php";
    include "$settings_dir/database.php";

    $f  = '0';
    $db_num = '0';
    if (isset($_GET['f']))
    $f = $_GET['f'];
    if (isset($_GET['db_num']))
    $db_num = $_GET['db_num'];


    if ($db_num == '1') {
        $mysql_table_prefix = $mysql_table_prefix1;
        db1_connect();
    }
    if ($db_num == '2') {
        $mysql_table_prefix = $mysql_table_prefix2;
        db2_connect();
    }
    if ($db_num == '3') {
        $mysql_table_prefix = $mysql_table_prefix3;
        db3_connect();
    }
    if ($db_num == '4') {
        $mysql_table_prefix = $mysql_table_prefix4;
        db4_connect();
    }
    if ($db_num == '5') {
        $mysql_table_prefix = $mysql_table_prefix5;
        db5_connect();
    }

    echo "    <h1>Sphider-plus installation script to create all tables for database ".$db_num."</h1>
            <p>&nbsp;</p>
            ";
    if ($f == '0') {
        echo "<div id='settings'>
                <form class='cntr' name='form10' method='post' action='db_config.php'>
                    <fieldset>
                        <label for='submit'class='sml'>
                            Up to now, nothing worth happened.<br />
                            If you want to return without installing the tables:
                        </label>
                        <br />
                        <input class='sbmt' type='submit' value='Cancel' id='submit' title='Click once to return to database settings'/>
                    </fieldset>
                </form>
                <br />
                <form class='cntr' name='form11' method='post' action='install_tables.php?f=1&db_num=".$db_num."'>
                    <fieldset>
                        <label for='submit'class='sml'>
                            If you really want to create all tables for database ".$db_num."<br /><br />
                            <span class='red'>Attention:</span> Already existing tables with the prefix <strong>'".$mysql_table_prefix."'</strong> <br />
                            will be destroyed and the content of all tables will be lost !<br />
                            Also the default configuration will be placed into<br />
                            the 'Settings' menue for this set of tables!
                        </label>
                        <br /><br /><br /><input class='sbmt' type='submit' value='Install now' id='submit' title='Click once to install all tables' />
                    </fieldset>
                </form>
                <br />
            </div>
        </body>
    </html>
            ";
    } else {

        $error = 0;

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."addurl`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."addurl`(
                          url varchar(1024) not null,
                          title varchar(255),
                          description varchar(255),
                          category_id int(11),
                          account varchar(255),
                          authent varchar(255),
                          created timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP)
                          ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."banned`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."banned` (
                          domain varchar(1024) not null,
                          created timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP)
                          ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }


        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."real_log`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."real_log`(
                          url varchar(1024) not null,
                          real_log mediumtext,
                          refresh integer not null primary key,
                          created timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP)
                          ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."sites`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."sites`(
                          site_id int auto_increment not null primary key,
                          url varchar(1024),
                          title varchar(255),
                          short_desc text,
                          indexdate date,
                          spider_depth int default -1,
                          required text not null,
                          disallowed text not null,
                          can_leave_domain bool,
                          db bool,
                          smap_url varchar(1024),
                          authent varchar(255),
                          use_prefcharset int default 0 )
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."links`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."links` (
                          link_id int auto_increment primary key not null,
                          site_id int,
                          url varchar(1024) not null,
                          title varchar(255),
                          description varchar(255),
                          fulltxt mediumtext,
                          indexdate date,
                          size float(2),
                          md5sum varchar(32),
                          key url (url(1024)),
                          key md5key (md5sum),
                          visible int default 0,
                          level int,
                          click_counter INT NULL DEFAULT 0,
                          last_click INT NULL DEFAULT 0,
                          last_query VARCHAR(255),
                          ip varchar(255)
                          CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL)
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."keywords`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."keywords`	(
                          keyword_id int primary key not null auto_increment,
                          keyword varchar(255) not null,
                          unique kw (keyword),
                          key keyword (keyword(10)))
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        for ($i=0;$i<=15; $i++) {
            $char = dechex($i);
            mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."link_keyword$char`");
            if (mysql_errno() > 0) {
                print "Error: ";
                print mysql_error();
                print "<br />\n";
                $error += mysql_errno();
            }
            mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."link_keyword$char` (
                              link_id int not null,
                              keyword_id int not null,
                              weight int(3),
                              domain int(4),
                              hits int(3),
                              indexdate datetime,
                              key linkid(link_id),
                              key keyid(keyword_id))
                            ");
            if (mysql_errno() > 0) {
                print "Error: ";
                print mysql_error();
                print "<br />\n";
                $error += mysql_errno();
            }
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."link_details`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."link_details` (
                          link_id int not null,
                          link_cont varchar(1024),
                          url varchar(1024),
                          title varchar(255),
                          indexdate datetime,
                          domain varchar(1024))
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."categories`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."categories` (
                          category_id integer not null auto_increment primary key,
                          category text,
                          parent_num integer)
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."site_category`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."site_category` (
                          site_id integer,
                          category_id integer)
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."temp`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."temp` (
                          link varchar(1024),
                          level integer,
                          id varchar (32))
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."pending`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."pending` (
                          site_id integer,
                          temp_id varchar(32),
                          level integer,
                          count integer,
                          num integer)
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."query_log`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."query_log` (
                          query varchar(255),
                          time timestamp default 0,
                          elapsed float(2),
                          results int,
                          ip varchar(255),
                          media int,
                          key query_key(query))
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."domains`");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."domains` (
                          domain_id int auto_increment primary key not null,
                          domain varchar(1024))
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        mysql_query("DROP TABLE IF EXISTS `".$mysql_table_prefix."media`
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }
        mysql_query("CREATE TABLE IF NOT EXISTS `".$mysql_table_prefix."media` (
                          media_id int auto_increment not null primary key,
                          link_id int(11) NOT NULL,
                          link_addr varchar(1024) COLLATE utf8_bin DEFAULT NULL,
                          media_link varchar(1024) COLLATE utf8_bin DEFAULT NULL,
                          thumbnail blob,
                          title varchar(255) COLLATE utf8_bin NOT NULL,
                          type varchar(255) COLLATE utf8_bin NOT NULL,
                          size_x int(11) NOT NULL,
                          size_y int(11) NOT NULL,
                          click_counter int(11) DEFAULT '0',
                          last_click int(11) DEFAULT '0',
                          last_query varchar(255) COLLATE utf8_bin DEFAULT NULL,
                          id3 mediumtext COLLATE utf8_bin NOT NULL,
                          md5sum varchar(32),
                          name varchar(1024),
                          suffix varchar(32),
                          ip varchar(255))
                        ");
        if (mysql_errno() > 0) {
            print "Error: ";
            print mysql_error();
            print "<br />\n";
            $error += mysql_errno();
        }

        if ($error >0) {    //  if creating of tables failed, enter here
            echo "<br />
                        <p class='warn em cntr'>Creating of tables failed. Consult the above error messages.</p>
                        <br />";
        } else {
            //  now copy the default configuration to the according folder
            $source = "./settings/backup/Sphider-plus_default-configuration.php";
            $desti  = "".$settings_dir."/db".$db_num."/conf_".$mysql_table_prefix.".php";
            if (!copy($source, $desti)) {   //  in case that cpying of configuration failed
                echo "<p class='warnok em cntr'>
                    Creating of tables successfully completed.</p>
                    <br /><br />
                    <p class='red em cntr'><br />Copying of default configuration failed.</p>
                    <p class='red em cntr'><br />Unable to proceed with this database.<br /><br /></p>
                    <br />
            <br /><br /><br />
        </body>
    </html>
                ";
                die (); //  unable to proceed
            }
            //  successfully created the tables and the configuration file
            echo "<p class='warnok em cntr'>Creating of database$db_num table set <span class='red'>$mysql_table_prefix</span> and default configuration successfully completed.</p>
            <br /><br /><br />";
        }
        echo "
            <div id='settings'>
                <form class='cntr txt' name='form10' method='post' action='db_config.php'>
                    <fieldset>
                        <label for='submit'class='sml'>
                            If you want to return to Database configuration:
                        </label>
                        <input class='sbmt' type='submit' value='Config' id='submit' title='Click once to return to Database configuration'/>
                    </fieldset>
                </form>
            </div>
            <br />
            <div id='settings'>
                <form class='cntr txt' name='form11' method='post' action='admin.php'>
                    <fieldset>
                        <label for='submit'class='sml'>If you want to return to Admin:</label>
                        <input class='sbmt' type='submit' value='Admin' id='submit' title='Click once to return to Admin interface' />
                    </fieldset>
                </form>
            </div>
            <br />
        </body>
    </html>
                    ";
    }

?>