<?PHP
/* Some notes about this script:
    1) Database named planeshift is the Crystal Blue database.
    2) The table planeshift.migration exists ( see migration.sql )    
 */
include "database.php";
include "../secure/db_config.php";


// This function will figure out what the player should get based on number
// of crystals.
function GenerateItem( $rubies, $emeralds, $diamonds )
{
    $script = mysql_escape_string("<item name='trias'  location='wallet' count='100' />");
    return $script;
}

$conn = new db_connection($db_hostname, $db_name, $db_username, $db_password);

$name      = $_POST[name];
$email     = $_POST[email];
$password  = $_POST[password];

// Check to see if already on list
$conn->useDB( "planeshift" );
$query = "SELECT id from migration WHERE username='$name'";
$result = $conn->query_db( $query );

if ( $email == "" )
{
    echo "Please enter a valid email address";
    exit;
}
// Check first to see if the name is already on the reserved list. 
if ( $conn->get_num_rows() != 0 )
{
    echo "This name is already on the reserved list";
    // Quit right away.
    exit;
}

// Validate this name/password with the molecular blue database and 
// get the number of rubies/emeralds/diamonds.
$queryURL="localhost/verify.php" . "?name=$name&password=$password";

$fp = fopen( $queryURL,"r");
if (!$fp) 
{
    echo "Cannot contact Fragnetics. Please contact acraig@planeshift.it";
}
else
{     
    $data = fread($fp, 1000000);
}

$crystals = explode(" ", $data);

$rubies   = $crystals[0];
if ( $rubies == -1 )
{
    echo "Name and password did not match";
    exit;
}
$emeralds = $crystals[1];
$diamonds = $crystals[2];

$script = GenerateItem( $rubies, $emeralds, $diamonds );
$time = time();

// Insert record into the migration table with a timestamp.
$query = "INSERT INTO migration(username, script, email, time) VALUES('$name', '$script', '$email', $time)";
$conn->query_db( $query );
echo "Your Character is now on the reserve list for Crystal Blue!";

?>
