Location: PHPKode > projects > Trunk.net > install/install.php
<? 
# IMPORTANT: DO NOT EDIT THIS FILE!!!

define ('THIS_ROOT_PATH', './');
define ('ROOT_PATH', './../');
define ('TRACKER_VERSION', 'Trunk SE 0.5'); // DO NOT CHANGE !
define ('INSTALL_VERSION', '1.0');
// Needed functions from bittorrent.php



function mksecret($len = 20) {
 $ret = "";
 for ($i = 0; $i < $len; $i++)
     $ret .= chr(mt_rand(0, 255));
 return $ret;
}
function get_date_time($timestamp = 0)
{
  if ($timestamp)
    return date("Y-m-d H:i:s", $timestamp);
  else
    return gmdate("Y-m-d H:i:s");
}
function sqlesc($x) {
 return "'".mysql_real_escape_string($x)."'";
}


?>
<!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" lang="en" xml:lang="en">
<head>
<META http-equiv="Content-Type" content="text/html; charset=WINDOWS-1251">
<title><?=TRACKER_VERSION ?> INSTALLATION <?=INSTALL_VERSION ?></title>
<link rel="stylesheet" href="default.css" type="text/css">
</head>
<body>
<?

if (file_exists(THIS_ROOT_PATH.'install.lock'))
{

	die("<p><table border=1 cellspacing=0 cellpadding=10 bgcolor=black width=900 align=center><tr><td style='padding: 10px; background: black' class=text>
<font color=white><center><b>Installation ERROR!</b> <div align=right>Install: ERROR!</div>
</font></center></td></tr></table></p>
<table border=1 cellspacing=0 cellpadding=10 width=900 align=center><tr><td style='padding: 10px;' class=text><div align=justify>
	  <center><font color=red><b>For security reasons, this installer is locked!</font></b><br><br>Please (via FTP) remove the 'install/install.lock' file before continue.</center>");	
//}else {
//	version_check();
}

// Form
function step_1()
{
	echo'
		<center><font color=red>1. <b>We asume you have filled everything.<br>
	2. You must create a DB(data base) by yourself</b></font>
	<form method="post" action="install.php">
	<table>
	<th>
		<td colspan=2><font color=green>Database configuration</font></td>
	</th>
	<tr>
		<td>MySQL server</td>
		<td><input type="text" name="server" value="localhost" /></td>
	</tr>
	<tr>
		<td>MySQL database</td>
		<td><input type="text" name="dbname" value="torrentbits" /></td>
	</tr>
	<tr>
		<td>MySQL user</td>
		<td><input type="text" name="dbuser" value="root" /></td>
	</tr>
	<tr>
		<td>MySQL password</td>
		<td><input type="password" name="dbpass" value="" /></td>
	</tr>
	<th>
		<td colspan=2><font color=green>Account configuration</font></td>
	</th>
	<tr>
		<td>Your username</td>
		<td><input type="text" name="adminuser" value="admin" /></td>
	</tr>
	<tr>
		<td>Your password</td>
		<td><input type="password" name="adminpass" value="" /></td>
	</tr>
	<tr>
		<td>Repeat ypur password</td>
		<td><input type="password" name="adminpass2" value="" /></td>
	</tr>
		<tr>
		<td>Your e-mail addres</td>
		<td><input type="text" name="adminmail" value="" /></td>
	</tr>
	<th>
		<td colspan=2><font color=green>Site configuration</font></td>
	</th>
	<tr>
		<td>Site name</td>
		<td><input type="text" name="sitename" value="Mydomain" /></td>
	</tr>	
	<tr>
		<td>Domain no ending slash(/)</td>
		<td><input type="text" name="domain" value="http://www.mydomain.org" /></td>
	</tr>
	<tr>
		<td>Announce addres(Announce url)</td>
		<td><input type="text" name="announce" value="http://www.mydomain.org/announce.php" /></td>
	</tr>
	<tr>
		<td>Site email</td>
		<td><input type="text" name="sitemail" value="hide@address.com" /></td>
	</tr>
	<tr>
<center><td collspan=2><input type="submit" name="install" value="Èíñòàëèðàé" /></td>
	</center>
	';
}

function update_config()
{
	
	$config_data = '<?php'."\n\n";
	$config_data .= '$mysql_host = \'' . $_POST['server'] . '\';' . "\n";
	$config_data .= '$mysql_db = \'' . $_POST['dbname'] . '\';' . "\n";
	$config_data .= '$mysql_user = \'' . $_POST['dbuser'] . '\';' . "\n";
	$config_data .= '$mysql_pass = \'' . $_POST['dbpass'] . '\';' . "\n\n";
	$config_data .= 'define(\'TB_INSTALLED\', true);'."\n\n";	
	$config_data .= '?' . '>'; // Done this to prevent highlighting editors getting confused!
	if(!($fp = fopen('../include/core.php', 'w')))
	{
		die('Make core.php writable -> 666');
	}
	else
	{
			$result = @fputs($fp, $config_data, strlen($config_data));

			@fclose($fp);
	}
	
	if( !mysql_connect($_POST['server'],$_POST['dbuser'],$_POST['dbpass']) )
	{
		die('Cant connect to databaseserver');
	}
	if( !mysql_select_db($_POST['dbname']) )
	{
		die('Cant select database');
	}
}
 
	
function basic_query()
{
	// $sql_query = @fread(@fopen('install.sql', 'r'), @filesize('install.sql'));
	$sql_lines = implode(' ', file(dirname(__FILE__) . '/trunk_0.5.sql'));
	$sql_lines = explode("\n", $sql_lines);
	
	include('../include/core.php');
	
	if( !mysql_connect($mysql_host,$mysql_user,$mysql_pass) )
	{
		die('Cant connect to databaseserver');
	}
	if( !mysql_select_db($mysql_db) )
	{
		die('Cant select database');
	}

	// Execute the SQL.
	$current_statement = '';
	$failures = array();
	$exists = array();
	foreach ($sql_lines as $count => $line)
	{
		// No comments allowed!
		if (substr($line, 0, 1) != '#')
			$current_statement .= "\n" . rtrim($line);

		// Is this the end of the query string?
		if (empty($current_statement) || (preg_match('~;[\s]*$~s', $line) == 0 && $count != count($sql_lines)))
			continue;

		// Does this table already exist?  If so, don't insert more data into it!
		if (preg_match('~^\s*INSERT INTO ([^\s\n\r]+?)~', $current_statement, $match) != 0 && in_array($match[1], $exists))
		{
			$current_statement = '';
			continue;
		}

		if (!mysql_query($current_statement))
		{
			$error_message = mysql_error($db_connection);

			// Error 1050: Table already exists!
			if (strpos($error_message, 'already exists') === false)
				$failures[$count] = $error_message;
			elseif (preg_match('~^\s*CREATE TABLE ([^\s\n\r]+?)~', $current_statement, $match) != 0)
				$exists[] = $match[1];
		}

		$current_statement = '';
	}
}
	
function insert_admin()
{
	if( $_POST['adminpass'] != $_POST['adminpass2'] )
	{
		die('The 2 passwords are incorrect');
	}
	
	$username = $_POST['adminuser'];
	$usermail = $_POST['adminmail'];
	

	
	$secret = mksecret();
	$wantpasshash = md5($secret . $_POST['adminpass'] . $secret);
	$editsecret = mksecret();

	$ret = mysql_query("INSERT INTO users (username, class, passhash, secret, editsecret, email, status, added) VALUES (" .
		implode(",", array_map("sqlesc", array($username, 6, $wantpasshash, $secret, $editsecret, $usermail, 'confirmed'))) .
		",'" . get_date_time() . "')");
	
	
}

function config()
{
	$online = gmdate("Y-m-d");
	$added = sqlesc(get_date_time());
	mysql_query("INSERT INTO config (name,value) VALUES ('siteonline','true')");
	mysql_query("INSERT INTO config (name,value) VALUES ('onlinesince','$online')");
	mysql_query("INSERT INTO config (name,value) VALUES ('sitename','".$_POST['sitename']."')");
	mysql_query("INSERT INTO config (name,value) VALUES ('domain','".$_POST['domain']."')");
	mysql_query("INSERT INTO config (name,value) VALUES ('announce_url','".$_POST['announce']."')");
	mysql_query("INSERT INTO config (name,value) VALUES ('sitemail','".$_POST['sitemail']."')");
	mysql_query("INSERT INTO config (name,value) VALUES ('funds',0 )");	
	mysql_query("INSERT INTO config (name,value) VALUES ('peerlimit',1000 )");
	mysql_query("INSERT INTO news (userid,body,added) VALUES (1,'Welcome and Enjoy,<br>Greetz Forgern',$added)");
}
	
function finale()
{
	echo'CONGRATULATIONS <BR>
	1. CHMod you the torrents dir to 777<BR>
	goto index.php and enjoy torrentbits sourcecode<BR>
	<a href="../index.php">GO HERE</a>
	';
}

include('../include/core.php');
if( defined("TB_INSTALLED") )
{
	die('Already installed <a href="../index.php">INDEX</a>');
	exit;
}	

if( $_POST['install'] || $_GET['install'] )
{
	update_config();
	basic_query();
	insert_admin();
	config();
	finale();
}
else
{
	step_1();
}
?>
Return current item: Trunk.net