<?
	$filesystem_path = dirname(__FILE__);
	$url_path = "http://" . $HTTP_HOST . dirname($_SERVER['SCRIPT_NAME']);
	echo <<<END
<head>
<style type="text/css">
TEXTAREA, .bginput {
	font-family: v; 
	font-size: 10pt; 
	color: #5E6A7B; 
	border: 1px solid #5E6A7B; 
	padding-left: 4; 
	padding-right: 4; 
	padding-top: 1; 
	padding-bottom: 1
}
</style>

</head>

<title>ByteMonsoon v2.0.5 Tracker Setup</title>
<font face=arial size=2>
END;

$SQL_SERVER = $_POST[ "hostname" ];
$SQL_USER = $_POST[ "username" ];
$SQL_PASS = $_POST[ "password" ];
$SQL_DB = $_POST[ "dbname" ];
$TRACKER_TITLE = $_POST[ "trackertitle" ];
$TRACKER_URL = $_POST[ "trackerurl" ];
$TRACKER_ADMIN = $_POST[ "trackeradmin" ];
$TRACKER_ADMIN_EMAIL = $_POST[ "trackeradminemail" ];
$TORRENT_DIR = $_POST[ "torrentdir" ];

switch($step) {
    case "2":
	if (file_exists("include/secrets.inc.php")) {
		echo "This tracker has already been configured. If you want to reinstall it, please first delete <b>include/secrets.inc.php</b> and remove all sql tables on your sql server !";
		exit("This Tracker has already been configured.");
	}
	if( ( $db = mysql_connect( $SQL_SERVER , $SQL_USER , $SQL_PASS ) ) && mysql_select_db( $SQL_DB , $db ) ) {
		echo 'Connection has been established to your MySQL server.<BR>';
		echo 'Press next to create the configuration file.';
		echo_hidden_fields("install.php?step=3");
	} else {
		echo 'Connection Failed, please go back and correct.  Database must exist.';
	}
	break;

    case "3":
	if (file_exists("include/secrets.inc.php")) {
				echo "This tracker has already been configured. If you want to reinstall it, please first delete <b>include/secrets.inc.php</b> and remove all sql tables on your sql server !";
		exit("This Tracker has already been configured.");
	}
	$stuff = '<?' . "\n"
		. '$mysql_host = "' . $SQL_SERVER . "\";\n"
		. '$mysql_user = "' . $SQL_USER . "\";\n"
		. '$mysql_pass = "' . $SQL_PASS . "\";\n"
		. '$mysql_db   = "' . $SQL_DB . "\";\n"
		. "\n"
		. '$tracker_title = "' . $TRACKER_TITLE . "\";\n"
		. '$tracker_url   = "' . $TRACKER_URL . "\";\n"
		. '$tracker_admin = "' . $TRACKER_ADMIN . "\";\n"
		. '$tracker_admin_email = "' . $TRACKER_ADMIN_EMAIL . "\";\n"
		. '$torrent_dir   = "' . $TORRENT_DIR . "\";\n"
		. '?>';

	$out = fopen("include/secrets.inc.php", "w");
	fwrite($out, $stuff);
	fclose($out);
	echo 'Setup created include/secrets.inc.php -- Next Step will create the database tables.';
  	echo_hidden_fields("install.php?step=4");
	break;

    case "4":
	create_tables();
	echo '<BR>Setup has created the tables -- Next Step will populate the tables.';
	echo_hidden_fields("install.php?step=5", false);
	echo '<BR><BR>Please Enter Your Desired Admin Username: <input type="text" name="adminuser">';
	echo '<BR>Please Enter Your Desired Admin password: <input type="text" name="adminpass">';
	echo '<BR><BR>';
	echo '<input type="submit" value="Next Step"><BR></form>';
	break;

    case "5":
	populate_tables();
	break;

    default:
	if (file_exists("include/secrets.inc.php")) {
		echo "This tracker has already been configured. If you want to reinstall it, please first delete <b>include/secrets.inc.php</b> and remove all sql tables on your sql server !";
		exit("This Tracker has already been configured.");
	}
 	echo "Welcome to the setup wizard of the Bytemonsoon Tracker.<BR>";
 	if (version_compare( phpversion(), "4.3") < 0) {
		echo "Minimum PHP version (4.3.0) not met!<BR>";
		echo "Unable to complete installation.<BR>";
		exit();
 	}
 	echo "This will get your tracker up and running in no time!<BR>"; 
 	echo <<<END
		<form enctype="multipart/form-data" action="install.php?step=2" method="post">
		<BR>MySQL database hostname(IP or domain name of the server, type localhost if you don't know): 
		<input type="text" name="hostname">
		<BR>MySQL User name: <input type="text" name="username">
		<BR>MySQL Password: <input type="password" name="password"> 
		<BR>MySQL Database name: <input type="text" name="dbname">
		<BR>
		<BR>Tracker Title: <input type="text" name="trackertitle" size=75 value="MyTracker">
		<BR>Tracker URL: <input type="text" name="trackerurl" size=75 value="$url_path">
		<BR>Tracker Admin: <input type="text" name="trackeradmin" size=40>
		<BR>Tracker Admin Email: <input type="text" name="trackeradminemail" size=75>
		<BR>Torrent Dir: <input type="text" name="torrentdir" size=75 value="$filesystem_path/torrents">
		<BR><BR>
		<input type="submit" value="Next Step"><BR></form>
		</span>
END;
	break;
}

echo "<BR><BR><BR>Install script coded by woolly and updated by SqrtBoy .";


function echo_hidden_fields($target, $finish_form=TRUE) {
	global $SQL_SERVER, $SQL_USER, $SQL_PASS, $SQL_DB, $TRACKER_TITLE;
	global $TRACKER_URL, $TORRENT_DIR, $TRACKER_ADMIN, $TRACKER_ADMIN_EMAIL;

	echo '<form enctype="multipart/form-data" action="' . $target . '" method="post">';
	echo '<input type="hidden" value="' . $SQL_SERVER . '" name="hostname">';
	echo '<input type="hidden" value="' . $SQL_USER . '" name="username">';
	echo '<input type="hidden" value="' . $SQL_PASS . '" name="password">';
	echo '<input type="hidden" value="' . $SQL_DB . '" name="dbname">';
	echo '<input type="hidden" value="' . $TRACKER_TITLE . '" name="trackertitle">'; 
	echo '<input type="hidden" value="' . $TRACKER_URL . '" name="trackerurl">';
	echo '<input type="hidden" value="' . $TRACKER_ADMIN . '" name="trackeradmin">';
	echo '<input type="hidden" value="' . $TRACKER_ADMIN_EMAIL . '" name="trackeradminemail">';
	echo '<input type="hidden" value="' . $TORRENT_DIR . '" name="torrentdir">';
	if ($finish_form) {
		echo '<input type="submit" value="Next Step">';
		echo '</form>';
	}
}

function create_tables() {
    global $SQL_SERVER, $SQL_USER, $SQL_PASS, $SQL_DB;

    if( ( $db = mysql_connect( $SQL_SERVER , $SQL_USER , $SQL_PASS ) ) && mysql_select_db( $SQL_DB , $db ) ) {
	if(mysql_query("CREATE TABLE avps (  arg varchar(20) NOT NULL default '',  value_i int(11) NOT NULL default '0',  value_u int(10) unsigned NOT NULL default '0',  PRIMARY KEY  (arg)) TYPE=HEAP")){
		echo 'Created table avps<BR>';
	} else {
		die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
	}

	if(mysql_query("CREATE TABLE categories (  id int(10) unsigned NOT NULL auto_increment,  name varchar(30) NOT NULL default '',  sort_index int(10) unsigned NOT NULL default '0',  image varchar(255) NOT NULL default '',  PRIMARY KEY  (id)) TYPE=MyISAM")){
		echo 'Created table categories<BR>';
	} else {
		die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
	}

	if(mysql_query("CREATE TABLE comments (  id int(10) unsigned NOT NULL auto_increment,  user int(10) unsigned NOT NULL default '0',  torrent int(10) unsigned NOT NULL default '0',  added datetime NOT NULL default '0000-00-00 00:00:00',  text text NOT NULL,  ori_text text NOT NULL,  PRIMARY KEY  (id),  KEY user (user),  KEY torrent (torrent)) TYPE=MyISAM")){
		echo 'Created table comments<BR>';
	} else {
		die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
	}

	if(mysql_query("CREATE TABLE files (  id int(10) unsigned NOT NULL auto_increment,  torrent int(10) unsigned NOT NULL default '0',  filename varchar(255) NOT NULL default '',  size bigint(20) unsigned NOT NULL default '0',  PRIMARY KEY  (id),  KEY torrent (torrent)) TYPE=MyISAM")){
		echo 'Created table files<BR>';
	} else {
		die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
	}

	if(mysql_query("CREATE TABLE hits (  id int(10) unsigned NOT NULL auto_increment,  page varchar(20) NOT NULL default '',  period datetime NOT NULL default '0000-00-00 00:00:00',  count bigint(20) unsigned NOT NULL default '0',  runs bigint(20) unsigned NOT NULL default '0',  runtime double NOT NULL default '0',  user_cpu double NOT NULL default '0',  sys_cpu double NOT NULL default '0',  PRIMARY KEY  (id),  UNIQUE KEY hit (period,page)) TYPE=MyISAM")){
		echo 'Created table hits<BR>';
	} else {
		die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
	}

	if(mysql_query("CREATE TABLE peers (  torrent int(10) unsigned NOT NULL default '0',  peer_id char(20) binary NOT NULL default '',  ip char(15) NOT NULL default '',  port smallint(5) unsigned NOT NULL default '0',  uploaded bigint(20) unsigned NOT NULL default '0',  downloaded bigint(20) unsigned NOT NULL default '0',  to_go bigint(20) unsigned NOT NULL default '0',  seeder enum('yes','no') NOT NULL default 'no',  started datetime NOT NULL default '0000-00-00 00:00:00',  last_action datetime NOT NULL default '0000-00-00 00:00:00',  connectable enum('yes','no') NOT NULL default 'yes',  PRIMARY KEY  (torrent,peer_id),  KEY torrent (torrent),  KEY connectable (connectable)) TYPE=HEAP")){
		echo 'Created table peers<BR>';
	} else {
		die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
	}

	if(mysql_query("CREATE TABLE torrents (  id int(10) unsigned NOT NULL auto_increment,  info_hash varchar(20) binary NOT NULL default '',  name varchar(255) NOT NULL default '',  filename varchar(255) NOT NULL default '',  save_as varchar(255) NOT NULL default '',  search_text text NOT NULL,  descr text NOT NULL,  ori_descr text NOT NULL,"
	  ."category int(10) unsigned NOT NULL default '0',  size bigint(20) unsigned NOT NULL default '0',  added datetime NOT NULL default '0000-00-00 00:00:00',  type enum('single','multi') NOT NULL default 'single',  numfiles int(10) unsigned NOT NULL default '0',  views int(10) unsigned NOT NULL default '0',  hits int(10) unsigned NOT NULL default '0',  times_completed int(10) unsigned NOT NULL default '0',  last_action datetime NOT NULL default '0000-00-00 00:00:00',scrape_url varchar(255) NOT NULL default '',"
	  ."visible enum('yes','no') NOT NULL default 'yes',  owner int(10) unsigned NOT NULL default '0',  seeders int(10) unsigned NOT NULL default '0',  leechers int(10) unsigned NOT NULL default '0',  comments int(10) unsigned NOT NULL default '0',  banned enum('yes','no') NOT NULL default 'no',  PRIMARY KEY  (id),  UNIQUE KEY info_hash (info_hash),  KEY owner (owner),  KEY visible (visible),  KEY category_visible (category,visible),  KEY filename (filename),  KEY name (name),  KEY added (added),  KEY last_action (last_action),  FULLTEXT KEY ft_search (search_text,ori_descr)) TYPE=MyISAM")){

		echo 'Created table torrents<BR>';
	} else {
		die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
	}

	if(mysql_query("CREATE TABLE users (  id int(10) unsigned NOT NULL auto_increment,  username varchar(40) NOT NULL default '',  password varchar(40) NOT NULL default '',  secret varchar(20) binary NOT NULL default '',  email varchar(80) NOT NULL default '',  status enum('pending','confirmed') NOT NULL default 'pending',  added datetime NOT NULL default '0000-00-00 00:00:00',  last_login datetime NOT NULL default '0000-00-00 00:00:00',  last_access datetime NOT NULL default '0000-00-00 00:00:00',  admin enum('yes','no') NOT NULL default 'no',  editsecret varchar(20) binary NOT NULL default '',  privacy enum('strong','normal','low') NOT NULL default 'normal',  descr text NOT NULL,  PRIMARY KEY  (id),  UNIQUE KEY username (username),  KEY status_added (status,added)) TYPE=MyISAM")){
		echo 'Created table users<BR>';
	} else {
		die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
	}
    } else {
	echo 'Fatal Error has occured, Please restart Setup.';
    }
}


function populate_tables() {
    global $SQL_SERVER, $SQL_USER, $SQL_PASS, $SQL_DB, $TRACKER_ADMIN_EMAIL, $filesystem_path;

require_once("include/bittorrent.inc.php");
$wantusername = $_POST[ "adminuser" ];
$wantpassword = $_POST[ "adminpass" ];
$secret = mksecret();
$thishost = $_SERVER["HTTP_HOST"];
$email = $TRACKER_ADMIN_EMAIL;

if( ( $db = mysql_connect( $SQL_SERVER , $SQL_USER , $SQL_PASS ) ) && mysql_select_db( $SQL_DB , $db ) )
{

if(mysql_query("INSERT INTO categories VALUES (1,'Movies',10,'film.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (2,'Music',20,'music.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (3,'TV',30,'tv3.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (4,'Games',40,'games.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (5,'Applications',50,'apps.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (6,'Miscellaneous',1000,'misc.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (7,'Pr0n',70,'pr0n.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (8,'Pictures',80,'pics.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (9,'Anime',90,'anime.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (10,'Comics',100,'comics.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (11,'Books',110,'books2.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO categories VALUES (13,'Music Videos',25,'mvid6.jpg')")){
	echo 'Inserted Data into categories<BR>';
} else {
	die("Fatal Error, drop all ByteMonsoon tables and run setup again.");
}

if(mysql_query("INSERT INTO users (username, password, secret, email, status, added) VALUES (" .
		implode(",", array_map("sqlesc", array($wantusername, $wantpassword, $secret, $email , 'confirmed'))) .
		", NOW())")){
	echo '<BR>Admin account created';
	echo '<BR>Congrats! Your tracker is now running and ready to go.';
	echo "<BR>Please see " . $filesystem_path . "/include/secrets.inc.php to modify these settings";
	
	echo "<BR>Visit <a href=\"http://www.phpsoft.org\" target=\"_new\">www.phpsoft.org</a> for the latest version .";
} else {
	echo 'Error';
}

echo_hidden_fields("index.php");

} else {
	die("Connection Failed, please go back and correct.");
}

}

?>
