<?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
  //directorychecks
  
  if (!file_exists($www_data_dir)) {
    $error = "<div class='setuperror'><b>$www_data_dir</b> not found.<br>Please create your upload-directory <b>\$www_data_dir</b> or correct path in conf.php</div>";
  } elseif (!is_writable($www_data_dir)) {
    $error = "<div class='setuperror'><b>$www_data_dir</b> isn't writeable.<br>Please use <b>chmod 757</b> to fix it.</b></div>";
  }
  
//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[] = "CREATE TABLE `" . $ftable_prefix . "answer` (
          `question_id` int(4) NOT NULL default '0',
          `a_uid` int(11) NOT NULL default '0',
          `a_id` int(4) NOT NULL default '0',
          `a_name` varchar(65) NOT NULL default '',
          `a_email` varchar(65) NOT NULL default '',
          `a_topic` varchar(255) default NULL,
          `a_answer` longtext NOT NULL,
          `a_datetime` datetime NOT NULL default '0000-00-00 00:00:00',
          `a_form_ts` int(11) NOT NULL default '0',
          KEY `a_id` (`a_id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "attachments` (
          `id` int(11) NOT NULL auto_increment,
          `tid` int(11) NOT NULL default '0',
          `pid` int(11) NOT NULL default '0',
          `uid` int(11) NOT NULL,
          `descr` varchar(150) default NULL,
          `file_name` varchar(100) NOT NULL,
          `created` datetime NOT NULL,
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "categories` (
          `id` int(11) NOT NULL auto_increment,
          `title` varchar(200) NOT NULL,
          `descr` text,
          `pos` int(11) NOT NULL default '0',
          `created` datetime default NULL,
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "config` (
          `id` int(11) NOT NULL auto_increment,
          `template` varchar(50) NOT NULL default 'defaultXHTML',
          `en_registering` tinyint(1) NOT NULL default '0',
          `en_guest_viewing` tinyint(1) NOT NULL default '1',
          `en_user_mess` tinyint(1) NOT NULL default '1',
          `en_board_notification` tinyint(1) NOT NULL default '1',
          `en_avatar` tinyint(1) NOT NULL default '1',
          `seo` tinyint(1) NOT NULL default '0',
          `max_img_width` int(11) NOT NULL default '0',
          `use_cap` tinyint(4) NOT NULL default '1',
          `recaptcha_pubkey` VARCHAR( 50 ) NULL,
          `recaptcha_privkey` VARCHAR( 50 ) NULL,
          `recaptcha_style` VARCHAR( 10 ) NULL DEFAULT 'red',
          `socialrow` BOOL NULL DEFAULT '0',
          `topics_per_page` int(11) NOT NULL default '15',
          `posts_per_page` int(11) NOT NULL default '10',
          `users_per_page` int(11) NOT NULL default '30',
          `results_per_page` int(11) NOT NULL default '10',
          `en_gravatar` tinyint(1) NOT NULL default '0',
          `en_polls` tinyint(1) NOT NULL default '1',
          `en_likes` BOOL NULL DEFAULT '0',
          `en_dislikes` BOOL NULL DEFAULT '0',
          `max_poll_options` int(11) NOT NULL default '15',
          `default_avatar` tinyint(1) NOT NULL default '1',
          `mod_minutes` int(11) NOT NULL default '15',
          `en_pms` tinyint(1) NOT NULL default '1',
          `max_pms` int(11) NOT NULL default '50',
          `en_attachments` tinyint(1) NOT NULL default '1',
          `max_attachment_size` float NOT NULL default '1',
          `activation` tinyint(4) NOT NULL default '0',
          `main_script` varchar(100) default NULL,
          `main_path` varchar(100) default NULL,
          `lang` varchar(10) NOT NULL default 'en',
          `avatar_height` int(11) NOT NULL default '80',
          `avatar_width` int(11) NOT NULL default '80',
          `date_format` varchar(20) NOT NULL default 'Y-m-d',
          `date_time_format` varchar(20) NOT NULL default 'Y-m-d H:i',
          `date_md_format` varchar(10) NOT NULL default 'M d',
          `default_timezone` varchar(50) NOT NULL default 'Europe/Vienna',
          `reserved_users` text,
          `title` varchar(150) default 'SimpleBulletinBoard',
          `descr` text,
          `email_board_admin` varchar(150) NOT NULL default 'admin@yourdomain.tld',
          `email_board_mailfrom` varchar(150) NOT NULL default 'board@yourdomain.tld',
          `version` float NOT NULL default '3.25',
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "INSERT INTO `" . $ftable_prefix . "config` (`id`, `template`, `en_registering`, `en_guest_viewing`, `en_user_mess`, `en_board_notification`, `seo`, `max_img_width`, `use_cap`, `topics_per_page`, `lang`, `avatar_height`, `avatar_width`, `date_format`, `date_time_format`, `date_md_format`, `default_timezone`, `reserved_users`, `title`, `descr`, `email_board_admin`, `email_board_mailfrom`, `version`) VALUES 
          (1, 'defaultXHTML', 1, 1, 1, 1, 0, 0, 1, 15, 'en', 80, 80, 'Y-m-d', 'Y-m-d H:i', 'M d', '" . (function_exists(date_default_timezone_get) ? date_default_timezone_get() : "Australia/Brisbane") . "', 'admin, support, administrator, guest, gast, anonymous, root, anonym', 'SBB - User Forum', 'The completely new Bulletin Board\r\nwith <b>Categories</b>', 'admin@yourdomain.tld', 'board@yourdomain.tld', 3.25);";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "forums` (
          `id` int(11) NOT NULL auto_increment,
          `cat_id` int(11) NOT NULL default '0',
          `title` varchar(200) NOT NULL,
          `descr` text,
          `pos` int(11) NOT NULL default '0',
          `viewing_permission` tinyint(4) NOT NULL default '0',
          `posting_permission` tinyint(4) NOT NULL default '1',
          `reply_permission` tinyint(4) NOT NULL default '1',
          `created` datetime default NULL,
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "INSERT INTO `" . $ftable_prefix . "forums` (`id`, `title`, `descr`, `pos`, `created`) VALUES 
          (1, 'This is the Main Forum', 'Just a few lines to describe what can be found within this seperated and categorized forum.', 1, now());"; 
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "poll_options` (
          `id` int(11) NOT NULL auto_increment,
          `tid` int(11) NOT NULL,
          `poll_option` varchar(100) NOT NULL,
          `pos` int(11) NOT NULL default '1',
          `votes` int(11) NOT NULL default '0',
          `created` datetime NOT NULL,
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "polls` (
          `id` int(11) NOT NULL auto_increment,
          `poll_option_id` int(11) NOT NULL,
          `uid` int(11) NOT NULL,
          `created` datetime NOT NULL,
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "pm` (
          `id` int(11) NOT NULL auto_increment,
          `from_uid` int(11) NOT NULL,
          `to_uid` int(11) NOT NULL,
          `subject` varchar(150) NOT NULL,
          `message` text NOT NULL,
          `sent` datetime NOT NULL,
          `seen` tinyint(1) NOT NULL default '0',
          `deleted_from` tinyint(1) NOT NULL default '0',
          `deleted_to` tinyint(1) NOT NULL default '0',
          `from_name` varchar(65) default NULL,
          `to_name` varchar(65) default NULL,
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "question` (
          `id` int(11) NOT NULL auto_increment,
          `topic` varchar(255) NOT NULL default '',
          `detail` longtext NOT NULL,
          `uid` int(11) NOT NULL default '0',
          `a_uid` int(11) NOT NULL default '0',
          `a_name` varchar(65) default NULL,
          `fid` int(11) NOT NULL default '1',
          `name` varchar(65) NOT NULL default '',
          `email` varchar(65) NOT NULL default '',
          `datetime` datetime NOT NULL default '0000-00-00 00:00:00',
          `a_datetime` datetime default NULL,
          `view` int(4) NOT NULL default '0',
          `reply` int(4) NOT NULL default '0',
          `sticky` tinyint(1) NOT NULL default '0',
          `form_ts` int(11) NOT NULL default '0',
          `locked` tinyint(1) NOT NULL default '0',
          `poll_title` varchar(120) default NULL,
          `poll_multiple` tinyint(1) NOT NULL default '0',
          `poll_duration` int(11) NOT NULL default '0',
          `poll_created` date default NULL,
          `attachment` int(11) NOT NULL default '0',
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "ranks` (
          `id` int(11) NOT NULL auto_increment,
          `descr` varchar(65) NOT NULL,
          `img` varchar(150) NOT NULL,
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "ratings` (
          `id` INT NOT NULL auto_increment,
          `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,
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "users` (
          `id` int(11) NOT NULL auto_increment,
          `user` varchar(25) NOT NULL default '',
          `pwd` varchar(60) NOT NULL,
          `email` varchar(100) default NULL,
          `forummail` tinyint(1) NOT NULL default '0',
          `usermail` tinyint(1) NOT NULL default '0',
          `signature` varchar(250) default NULL,
          `website` varchar(200) default NULL,
          `interests` varchar(255) default NULL,
          `occupation` varchar(255) default NULL,
          `location` varchar(100) default NULL,
          `birthday` date default NULL,
          `avatar` varchar(25) default NULL,
          `gravatar` tinyint(1) NOT NULL default '0',
          `sbb_avatar` varchar(150) default NULL,
          `is_mod` tinyint(1) NOT NULL default '0',
          `is_admin` tinyint(1) NOT NULL default '0',
          `rank_id` int(11) NOT NULL default '0',
          `status` tinyint(1) NOT NULL default '1',
          `deleteable` tinyint(1) NOT NULL default '1',
          `last_login` datetime default NULL,
          `last_visit` datetime default NULL,
          `created` datetime NOT NULL default '0000-00-00 00:00:00',
          `edited` datetime default NULL,
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "INSERT INTO `" . $ftable_prefix . "users` (`id`, `user`, `pwd`, `forummail`, `usermail`, `is_mod`, `is_admin`, `status`, `deleteable`, `created`) VALUES 
          (1, 'admin', password('pass'), 1, 1, 0, 1, 1, 0, now());";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "watchlist` (
          `id` int(11) NOT NULL auto_increment,
          `uid` int(11) NOT NULL,
          `tid` int(11) NOT NULL default '0',
          `fid` int(11) NOT NULL default '0',
          PRIMARY KEY  (`id`));";
    $SQL_queries[] = "CREATE TABLE `" . $ftable_prefix . "whoisonline` (
          `id` int(11) NOT NULL auto_increment,
          `uid` int(11) NOT NULL default '0',
          `ts` datetime NOT NULL,
          `key` varchar(32) default NULL,
          PRIMARY KEY  (`id`));";
  
    if ($todo == "createtables") {
      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 create these table with these queries or click <a href='?todo=createtables'>here</a> to create the tables 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 setup.php</b> or just rename it</div>";
  
  }

if ($db) mysql_close($db);

?>
</body>
</html>