<?php

$www_dir = realpath(dirname(__FILE__));
include ("$www_dir/functions.php");
include ("$www_dir/conf.php");

$todo = $_REQUEST["todo"];
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
    <style type="text/css">
    <!--
     body, td {
      font-family: Verdana, Arial;
      font-size: 12px;
      color: #666666;
    }
    /* some error, success and infoboxes */
      .setuperror {
        padding: 10px;
        border: 2px #cc3300 solid;
        background-color: #f0e000;
        margin: 10px;
      }
      .setupsuccess {
        padding: 10px;
        border: 2px #00b000 solid;
        background-color: #c0f000;
        margin: 10px;
      }


    //-->
    </style>
  </head>
  <body>
  
<?php

/**** ERROR CHECKING [+] ****/

  //database
  if (!$error) {
    $db = @mysql_connect($db_host, $db_user, $db_pass) or $error = "<div class='setuperror'><b>Database-connection failed</b>,<br>please use the correct data for your database in the conf.php.</div>";
    if (!$error) {
      @mysql_select_db($db_name, $db) or $error = "<div class='setuperror'><b>Database $db_name not found or no rights for access.</div>";
    }
  }
  //table
  if (!$error) {
    $SQL_queries[] = "ALTER TABLE `" . $ftable_prefix . "config` ADD `recaptcha_pubkey` VARCHAR( 50 ) NULL AFTER `use_cap`,
        ADD `recaptcha_privkey` VARCHAR( 50 ) NULL AFTER `recaptcha_pubkey`,
        ADD `recaptcha_style` VARCHAR( 10 ) NULL DEFAULT 'red' AFTER `recaptcha_privkey`,
        ADD `socialrow` BOOL NULL DEFAULT '0' AFTER `recaptcha_style`,
        ADD `en_likes` BOOL NULL DEFAULT '0' AFTER `en_polls`,
        ADD `en_dislikes` BOOL NULL DEFAULT '0' AFTER `en_likes`;";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "ratings` (
        `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        `tid` INT NOT NULL DEFAULT '0',
        `pid` INT NOT NULL DEFAULT '0',
        `uid` INT NOT NULL ,
        `username` VARCHAR( 50 ) NULL ,
        `rating` BOOL NOT NULL DEFAULT '0',
        `created` DATETIME NOT NULL
        );";
    $SQL_queries[] = "UPDATE `" . $ftable_prefix . "config` SET `version` = '3.25' WHERE id=1;";
        
    if ($todo == "updatetables") {
      for ($i=0;$i<count($SQL_queries);$i++) {
        mysql_query($SQL_queries[$i],$db);
        if (mysql_error()) echo "<div class='setuperror'>Errors occured:<br>Query: " . $SQL_queries[$i] . "<br />Error: " . mysql_error() . "</div>";
      }
    }  
    $res = mysql_query("show tables from `$db_name`;",$db);
    $found_tables = 0;
    while ($row = mysql_fetch_object($res)) {
      if (in_array($row->{"Tables_in_".$db_name},array($ftable_prefix.'answer',$ftable_prefix.'attachments',$ftable_prefix.'categories',$ftable_prefix.'config',$ftable_prefix.'forums',$ftable_prefix.'pm',$ftable_prefix.'poll_options',$ftable_prefix.'polls',$ftable_prefix.'question',$ftable_prefix.'ranks',$ftable_prefix.'ratings',$ftable_prefix.'users',$ftable_prefix.'watchlist',$ftable_prefix.'whoisonline'))) $found_tables++;
    }
    if ($found_tables < 14) {
      $error = "<div class='setuperror'><b>necessary tables not found.</b><br>
        Please modify your database with these queries or click <a href='?todo=updatetables'>here</a> to make the changes automatically.<br>";
      for ($i=0;$i<count($SQL_queries);$i++) {
        $error .= "<pre>" . $SQL_queries[$i] . "</pre>\n";
      }
      $error .= "</div>";
    }
  } 
  //exit on errors
  if ($error) {
    exit ($error . "</body></html>");
  } else {
    echo "<div class='setupsuccess'>All database checks and settings are finished now.<br>
      More changes can be made in <b>conf.php</b><br><br>
      Now you have to <b>delete " . basename(__FILE__) . "</b> or just rename it</div>";
  
  }
  /**** ERROR CHECKING [-] ****/

?>
</body>
</html>
