<?php

    set_time_limit (0);

    $database1  = '';
    $plus_nr    = '';

    $tmp_folder     = "/tmp";
    $include_dir    = "../include";
    $settings_dir   = "../settings";
    $image_dir      = "$include_dir/images";
    $textcache_dir  = "$include_dir/textcache";
    $mediacache_dir = "$include_dir/mediacache";

    //include "$include_dir/commonfuncs.php";
    include "$settings_dir/database.php";
    if (!$database1) {
        echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
    <html>
        <header>
            <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
            <title>Sphider-plus administrator warning</title>
            <meta http-equiv='X-UA-Compatible' content='IE=9' />
            <link href='../templates/html/sphider-plus.ico' rel='shortcut icon' type='image/x-icon' />
        </header>
        <body>
            <br /><br />
            <div style=\"text-align:center;\">
                <strong>Attention:</strong> Unable to load the database connfiguration file.<br />
                Pleaase reinstall Sphider-plus by using the original scripts as per download.<br />
                <br /><br />
            </div>
        </body>
    </html>
                ";
        die ();
    }

    //      get active database for Admin
    if ($dba_act == '1') {
        $db_con     = adb1_connect() ;
        $success    = @mysql_select_db ($database1, $db_con);
        $mysql_table_prefix = $mysql_table_prefix1;
        $tables     = @mysql_query("select * from ".$mysql_table_prefix."addurl");
    }

    if ($dba_act == '2') {
        $db_con = adb2_connect() ;
        $success    = @mysql_select_db ($database2, $db_con);
        $mysql_table_prefix = $mysql_table_prefix2;
        $tables     = @mysql_query("select * from ".$mysql_table_prefix."addurl");
    }

    if ($dba_act == '3') {
        $db_con = adb3_connect() ;
        $success    = @mysql_select_db ($database3, $db_con);
        $mysql_table_prefix = $mysql_table_prefix3;
        $tables     = @mysql_query("select * from ".$mysql_table_prefix."addurl");
    }

    if ($dba_act == '4') {
        $db_con = adb4_connect() ;
        $success    = @mysql_select_db ($database4, $db_con);
        $mysql_table_prefix = $mysql_table_prefix4;
        $tables     = @mysql_query("select * from ".$mysql_table_prefix."addurl");
    }

    if ($dba_act == '5') {
        $db_con = adb5_connect() ;
        $success    = @mysql_select_db ($database5, $db_con);
        $mysql_table_prefix = $mysql_table_prefix5;
        $tables     = @mysql_query("select * from ".$mysql_table_prefix."addurl");
    }

    $default = '';
    @include "".$settings_dir."/db".$dba_act."/conf_".$mysql_table_prefix.".php";
    if (!$plus_nr) {
        include "./settings/backup/Sphider-plus_default-configuration.php";
        $default = '1';
    }

    include "$include_dir/commonfuncs.php";

    if ($debug == '0') {
        if (function_exists("ini_set")) {
            ini_set("display_errors", "0");
        }
        error_reporting(0);  //     suppress  PHP messages
    } else {
        error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
    }

    //  check if multibyte functions are available
    //  this check is required only for first call of admin.php
    //  later on this check is also performed by configset.php together with a warning message
    $mb = '';
    if (function_exists('mb_internal_encoding')) {
        if(function_exists('mb_stripos')) {
            $mb = '1';
        }
    }
    if ($mb != 1) {
        $mb = '0';
    }

    $template_dir   = "../".$templ_dir."";
    $template_path  = "$template_dir/$template";

    //require_once('phpSecInfo/PhpSecInfo.php');        //   (might not work on shared hosting systems)
    include("geoip.php");

    echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
        <head>
            <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
            <meta http-equiv='expires' content='0'>
            <meta http-equiv='pragma' content='no-cache'>
            <title>Sphider-plus administrator tools</title>
            <meta http-equiv='X-UA-Compatible' content='IE=9' />
            <link href='../templates/html/sphider-plus.ico' rel='shortcut icon' type='image/x-icon' />
            <link rel='stylesheet' type='text/css' href='$template_path/adminstyle.css' />
            <script type='text/javascript' src='confirm.js'></script>
            <script type='text/javascript'>
                function JumpBottom() {
                    window.scrollTo(0,1000);
                }
            </script>
        </head>
        <body>
        ";

    include "auth.php";

    $php_vers = phpversion();
    if (preg_match('/^4\./', trim($php_vers)) == '1') {
        echo "<br />
                <div id='main'>
                <h1 class='cntr'>
                Sphider-plus. The PHP Search Engine
                </h1>
                    <div class='cntr warnadmin'>
                        <br />
                        <p>Your current PHP version is $php_vers</p>
                        <p>Sorry, but Sphider-plus v. $plus_nr requires PHP 5.x</p>
                        <br /><br />
                    </div>
                </div>
                </body>
                </html>
            ";
        die ('');
    }

    if ($default == '1') {
        echo "  <br />
                    <p class='warnadmin cntr'><br />
                    <strong>Attention:</strong> The configuration file for database <strong>$dba_act</strong> and the table set <strong>$mysql_table_prefix</strong> does not yet exist.<br /><br />
                    Alternatively using the Sphider-plus default configuration.<br />
                    <br /><br />
                    </p>
                    <br />
                ";
    }

    function adb1_connect() {
        global $mysql_host1, $mysql_user1, $mysql_password1, $database1;

        $db_con1 = @mysql_pconnect ($mysql_host1, $mysql_user1, $mysql_password1);
        if (!$db_con1) {
            echo "<p><span class='red'>&nbsp;Mysql Server for database '$database1' not available!&nbsp;<br /></font></p>
                <p><span class='blue sml'>&nbsp;&nbsp;Trying to reconnect to database . . .<br />&nbsp;&nbsp;Cannot connect to database.<br /></p>";
        } else {
            $success1 = @mysql_select_db ($database1, $db_con1);
            if (!$success1) {
                echo "<p><span class='blue sml'><br />&nbsp;&nbsp;Cannot choose database 1.<br /></p>";
            } else {
                return ($db_con1);
            }
        }
    }

    // Database2 connection
    function adb2_connect() {
        global $mysql_host2, $mysql_user2, $mysql_password2, $database2;

        $db_con2 = @mysql_pconnect ($mysql_host2, $mysql_user2, $mysql_password2);
        if (!$db_con2) {
            echo "<p><span class='red'>&nbsp;Mysql Server for database '$database2' not available!&nbsp;<br /></font></p>
                <p><span class='blue sml'>&nbsp;&nbsp;Trying to reconnect to database . . .<br />&nbsp;&nbsp;Cannot connect to database.<br /></p>";
        } else {
            $success2 = @mysql_select_db ($database2, $db_con2);
            if (!$success2) {
                echo "<p><span class='blue sml'><br />&nbsp;&nbsp;Cannot choose database 2.<br /></p>";
            } else {
                return ($db_con2);
            }
        }
    }

    // Database3 connection
    function adb3_connect() {
        global $mysql_host3, $mysql_user3, $mysql_password3, $database3;

        $db_con3 = @mysql_pconnect ($mysql_host3, $mysql_user3, $mysql_password3);
        if (!$db_con3) {
            echo "<p><span class='red'>&nbsp;Mysql Server for database '$database3' not available!&nbsp;<br /></font></p>
                <p><span class='blue sml'>&nbsp;&nbsp;Trying to reconnect to database . . .<br />&nbsp;&nbsp;Cannot connect to database.<br /></p>";
        } else {
            $success3 = @mysql_select_db ($database3, $db_con3);
            if (!$success3) {
                echo "<p><span class='blue sml'><br />&nbsp;&nbsp;Cannot choose database 3.<br /></p>";
            } else {
                return ($db_con3);
            }
        }
    }

    // Database4 connection
    function adb4_connect() {
        global $mysql_host4, $mysql_user4, $mysql_password4, $database4;

        $db_con4 = @mysql_pconnect ($mysql_host4, $mysql_user4, $mysql_password4);
        if (!$db_con4) {
            echo "<p><span class='red'>&nbsp;Mysql Server for database '$database4' not available!&nbsp;<br /></font></p>
                <p><span class='blue sml'>&nbsp;&nbsp;Trying to reconnect to database . . .<br />&nbsp;&nbsp;Cannot connect to database.<br /></p>";
        } else {
            $success4 = @mysql_select_db ($database4, $db_con4);
            if (!$success4) {
                echo "<p><span class='blue sml'><br />&nbsp;&nbsp;Cannot choose database 4.<br /></p>";
            } else {
                return ($db_con4);
            }
        }
    }

    // Database5 connection
    function adb5_connect() {
        global $mysql_host5, $mysql_user5, $mysql_password5, $database5;

        $db_con5 = @mysql_pconnect ($mysql_host5, $mysql_user5, $mysql_password5);
        if (!$db_con5) {
            echo "<p><span class='red'>&nbsp;Mysql Server for database '$database5' not available!&nbsp;<br /></font></p>
                <p><span class='blue sml'>&nbsp;&nbsp;Trying to reconnect to database . . .<br />&nbsp;&nbsp;Cannot connect to database.<br /></p>";
        } else {
            $success5 = @mysql_select_db ($database5, $db_con5);
            if (!$success5) {
                echo "<p><span class='blue sml'><br />&nbsp;&nbsp;Cannot choose database 5.<br /></p>";
            } else {
                return ($db_con5);
            }
        }
    }

?>
