<?php
$reviewer_fields = array(
	"reviewer_name" => "Reviewer name",
	"reviewer_mail" => "Reviewer e-mail");
$review_fields = array(
	"title" => "Title", "author" => "Author",
	"e_mail" => "E-mail", "date" => "Date",
	"system" => "System", "url" => "URL",
	"version" => "Version");

function assemble_message($source) {
	global $reviewer_fields, $review_fields;

	$message = "";
	foreach ($reviewer_fields as $field => $label) {
		if (!isset($source[$field]))
			$source[$field] = "";
		$message .= "$label: " . $source[$field] . "\n";
	}
	$message .= "\n" . $source["review_comments"] . "\n\n";

	foreach ($review_fields as $field => $label) {
		if (!isset($source[$field]))
			$source[$field] = "";
		$message .= "$label: " . $source[$field] . "\n";
	}
	$message .= "\n" . $source["review"];
	return $message;
}

function send_message($message) {
  if (strpos($_POST["title"],'Good site')===false)
	$to = "davidm.spag@gmail.com";
	//$to = "felixp7@yahoo.com";
	$subject = "SPAG review submission";
	$headers = "From: SPAG@sparkynet.com";
	$headers .= "\r\nReply-To: davidm.spag@gmail.com";
	if (!empty($_POST["reviewer_mail"]))
		$headers .= "\r\nCc: " . $_POST["reviewer_mail"];
	$headers .= "\r\n\r\n";
	return @mail($to, $subject, $message, $headers);
}

$success = false;
if (isset($_POST["submit_review"])) {
  	if (!empty($_POST["review"])) {
  		$success = send_message(assemble_message($_POST));
  	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>Submit a review - SPAG</title>
  <link rel="stylesheet" type="text/css" href="style/spag.css"/>

  <link rel="top" type="text/html" href="index.html"/>
  <link rel="up" type="text/html" href="index.html"/>
  <link rel="prev" type="text/html" href="reviews.html"/>
  <link rel="next" type="text/html" href="newreleases.html"/>
 </head>

 <body>
  <h1 align="center">
   <a href="index.html"><span id="title"><img src="spag.png" alt="SPAG" width="349" height="94" border="0"/></span></a>
  <br/>
   <span id="subtitle">Submit a review</span>
  </h1>

<div>

 <?php if (isset($_POST["submit_review"])): ?>
  <?php if ($success): ?>
   <p><strong>Review sent successfully.</strong></p>
   <p>Return to the <a href="index.html">SPAG site index</a>.</p>
   <p>Thank you for keeping the text adventures alive!</p>
   <script type="text/javascript"><!--
    alert('Review sent successfully!');
    window.location = "http://www.sparkynet.com/spag/";
   // --></script>
  <?php elseif (empty($_POST["review"])): ?>
   <p>Can't send review: you didn't type it!</p>
  <?php else: ?>
   <p>Failed to send review. Please try again later.</p>
  <?php endif; ?>
 <?php endif; ?>

 <?php if (!$success): ?>
 <p>
  When submitting reviews, try to fill in as much of the info below
  as you can. A copy of your submission will be CCed to you immediately, and you should receive a personal reply from the editor within a few days at the most.  If you do not, something may have gone wrong.  <a href="mailto:davidm.spag@gmail.com">Email the editor</a> to inquire.
 </p>

 <form method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
  <h2>Reviewer information</h2>
  <table>
   <tr>
    <th align="left">Reviewer name:</th>
    <td><input name="reviewer_name" size="48"
     value="<?php if (isset($_POST['reviewer_name'])) print $_POST['reviewer_name']; ?>"/>
    </td>
   </tr>
   <tr>
    <th align="left">Reviewer e-mail:</th>
    <td><input name="reviewer_mail" size="48"
     value="<?php if (isset($_POST['reviewer_mail'])) print $_POST['reviewer_mail']; ?>"/>
    </td>
   </tr>
  </table>
  <p>Comments to the editor on this review:<br/>
   <textarea name="review_comments" cols="48" rows="10"><?php
    if (isset($_POST['review_comments'])) print $_POST['review_comments']; ?></textarea></p>
  
  <h2>Game information</h2>
  <table>
   <tr>
    <th align="left">Title:</th>
    <td><input name="title" size="64"
     value="<?php if (isset($_POST['title'])) print $_POST['title']; ?>"/>
    </td>
   </tr>
   <tr>
    <th align="left">Author:</th>
    <td><input name="author" size="64"
     value="<?php if (isset($_POST['author'])) print $_POST['author']; ?>"/>
    </td>
   </tr>
   <tr>
    <th align="left">E-mail:</th>
    <td><input name="e_mail" size="64"
     value="<?php if (isset($_POST['e_mail'])) print $_POST['e_mail']; ?>"/>
    </td>
   </tr>
   <tr>
    <th align="left">Date:</th>
    <td><input name="date" size="64"
     value="<?php if (isset($_POST['date'])) print $_POST['date']; ?>"/>
    </td>
   </tr>
   <tr>
    <th align="left">System:</th>
    <td><input name="system" size="64"
     value="<?php if (isset($_POST['system'])) print $_POST['system']; ?>"/>
    </td>
   </tr>
   <tr>
    <th align="left">URL:</th>
    <td><input name="url" size="64"
     value="<?php if (isset($_POST['url'])) print $_POST['url']; ?>"/>
    </td>
   </tr>
   <tr>
    <th align="left">Version:</th>
    <td><input name="version" size="64"
     value="<?php if (isset($_POST['version'])) print $_POST['version']; ?>"/>
    </td>
   </tr>
  </table>
  <p>The review:<br/>
   <textarea name="review" cols="64" rows="20"><?php
    if (isset($_POST['review'])) print $_POST['review']; ?></textarea></p>
  <p align="center">
   <input type="submit" name="submit_review" value="Submit"/></p>
 </form>
 <?php endif; ?>

</div>
</body>
</html>
