<?php
session_start();
include('global.php');
if(!$_SESSION['rand'] && $image == "yes")
{
header('Location: new.php');
exit;
}
function amfrerror($error, $template)
{
    if(!$template)
    {
        $template = 'default';
    }
	$template_content = file_get_contents('templates/'.$template.'/signup_error.tpl');
	$template_content = str_replace('<*ERROR*>', $error, $template_content);
	$template_content = str_replace('<*COPYRIGHT*>', '<a href="http://www.amfrservices.com">AMFR Account Creator version 1.0 Final</a>', $template_content);
	die($template_content);
}
$domain2 = $_POST['domain2'];
$domain = $_POST['domain'];
$user = $_POST['user'];
$pass = rand();
$email = $_POST['email'];
$key = $_POST['c2'];
if(!$user || !$domain || !$domain2 || !$email)
{
	amfrerror('Not all field were filled in!', $template);
}
$image2 = $_POST['image2'];
if($image == "yes")
{
if($image2 != $_SESSION['rand'])
{
	amfrerror('Number does not match one in picture!', $template);
}
}
if(strlen($user) > 8)
{
	amfrerror('Username cannot be longer that 8 characters!', $template);
}
if(eregi('www\.', $domain2))
{
	amfrerror('Invalid domain!', $template);
}
$link = mysql_connect($host, $dbuser, $dbpass);
if($skeys == "yes") {
$result = mysql_db_query($dbname, "SELECT * FROM amfr_keys WHERE amfrkey='$key'");
if (mysql_num_rows($result) == 0) {
	amfrerror('Invalid signup key!', $template);
}
}
$domain3 = $domain2.".".$domain;
$result = mysql_db_query($dbname, "SELECT * FROM amfr_users WHERE user='$user'");
if (!mysql_num_rows($result) == 0) {
	amfrerror('Username already taken!', $template);
}
$result = mysql_db_query($dbname, "SELECT * FROM amfr_users WHERE domain='$domain2"."."."$domain'");
if (!mysql_num_rows($result) == 0) {
	amfrerror('Subdomain/domain already in use!', $template);
}
$result = mysql_db_query($dbname, "SELECT * FROM amfr_users WHERE email='$email'");
if (!mysql_num_rows($result) == 0) {
	amfrerror('Email already used!');
}
$file = file_get_contents("http://".$cpuser.":".$cppass."@localhost:2086/scripts/wwwacct?sign=&plan=".$cppackage."&domain=".$domain3."&username=".$user."&password=".$pass."&contactemail=".urlencode($email));
if(ereg('<img src=\/icons\/error\.gif>', $file)) {
if(ereg(' username ', $file)) {
	amfrerror('Username already taken!', $template);
}
else {
	amfrerror('There was an error with your signup!', $template);
}
}
mail($email, "Free hosting account set up", "A free cPanel hosting account has been set up for you.\nAccount details:\nUsername: $user\nPassword: $pass\nDomain: ".$domain2.".".$domain."\nTo login to cpanel go to http://".$domain2.".".$domain."/cpanel\nIf you are using your own domain please set your name servers to '$ns1' and '$ns2'".".", "From: Automailer");
$logyear = date('Y');
$logmonth = date('n');
$logday = date('j');
$result = mysql_db_query($dbname, "INSERT INTO amfr_logs values('$user', $logmonth, $logday, $logyear)");
$result = mysql_db_query($dbname, "INSERT INTO amfr_users values('$user', '$domain2"."."."$domain', '$email')");
if($skeys =="yes") {
	$result = mysql_db_query($dbname, "INSERT INTO amfr_used_keys values('$key', '$user')");
	if($mkeys != "yes")
	{
		$sql = "DELETE FROM amfr_keys WHERE amfrkey='$key'";
		$result = mysql_db_query($dbname, $sql);
	}
}
if(!$template)
{
	$template = 'default';
}
$template_content = file_get_contents('templates/'.$template.'/signup_finished.tpl');
$template_content = str_replace('<*USERNAME*>', $user, $template_content);
$template_content = str_replace('<*PASSWORD*>', $pass, $template_content);
$template_content = str_replace('<*EMAIL*>', $email, $template_content);
$template_content = str_replace('<*COPYRIGHT*>', '<a href="http://www.amfrservices.com">AMFR Account Creator version 1.0 Final</a>', $template_content);
echo $template_content;
mysql_close($link);
?>
