changeset 36:e12b34c8fde2 misc

Add code for submission form.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 18 Apr 2009 06:26:11 +0000
parents d4fef95c2f3e
children c4ce97a388ce
files materials/submit.php
diffstat 1 files changed, 253 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/materials/submit.php	Sat Apr 18 06:26:11 2009 +0000
@@ -0,0 +1,253 @@
+<?
+$pageTitle = "Alloy combo submitter";
+$pageIndex = "submit.php";
+require "materials.inc.php";
+require "alloys.inc.php";
+require "guids.inc.php";
+
+$formMode = -1;
+$hasGUID = FALSE;
+$hasData = FALSE;
+
+// Get form arguments, if they are set
+if (isset($_GET["guid"])) {
+  $formGUID = $_GET["guid"];
+  $hasGUID = TRUE;
+} elseif (isset($_POST["guid"])) {
+  $formGUID = stripslashes($_POST["guid"]);
+  $hasGUID = TRUE;
+}
+
+if (isset($_POST["data"])) {
+  $formData = stripslashes($_POST["data"]);
+  $hasData = TRUE;
+}
+
+
+// Check submitter GUID
+if ($hasGUID) {
+  if (strlen($formGUID) < 6 || strlen($formGUID) > 20) {
+    $formMode = -1;
+    $errorMsg = "Submitter GUID <b>'".htmlentities(substr($formGUID, 0, 20))."...'</b> is invalid.";
+  } else
+  if (!isset($submitGUIDS[$formGUID])) {
+    $formMode = -1;
+    $errorMsg = "Submitter GUID <b>'".htmlentities($formGUID)."'</b> is not known. ".
+    "Please check that you have entered your GUID code correctly (it is case-sensitive!)<br /><br />".
+    "Also, if you received your GUID recently (during last 10 minutes or so), it might not have ".
+    "yet been added to the system. Wait for a bit and try again.";
+  } else {
+    // GUID is ok
+    if ($hasData) {
+      // Got some data, go and check it for sanity
+      $formMode = 2;
+    } else {
+      // No data. Proceed to submission, except if below code triggers...
+      $formMode = 1;
+      
+      // This is a bit of a hack ...
+      $mat1Set = isset($_GET["mat1"]); $mat1 = $_GET["mat1"];
+      $mat2Set = isset($_GET["mat2"]); $mat2 = $_GET["mat2"];
+      $resSet = isset($_GET["res"]);   $res = $_GET["res"];
+      if ($mat1Set || $mat2Set || $resSet) {
+        if ($mat1Set && $mat2Set && $resSet) {
+          // Data set, process it.
+          $formData = "You mix ".$mat1." and ".$mat2." and create a quantity of ".$res;
+          $formMode = 2;
+        } else {
+          // Some data set, but not all.
+          $formMode = -1;
+          $errorMsg = "All required parameters not set. You need to send <b>mat1, mat2 and res</b>.";
+        }
+      }
+    }
+  }
+} else {
+  // By default, assume we are in GUID setup mode
+  $formMode = 0;
+}
+
+function getMaterial($name)
+{
+  global $matTable;
+  if (isset($matTable[$name]))
+    return $matTable[$name];
+  else
+    return FALSE;
+}
+
+function getMatClass($mat) {
+  $matClass = array(
+    0  => 0,
+    1  => 0,
+    
+    2  => 1,
+    
+    3  => 2,
+    5  => 2,
+
+    4  => 3,
+    7  => 3,
+    
+    6  => 4,
+    8  => 5,
+    9  => 6,
+    10 => 7,
+  );
+  return isset($matClass[$mat]) ? $matClass[$mat] : -1;
+}
+
+function pageHead()
+{
+  global $pageTitle;
+  require "../header.inc.php";
+  echo "<h1>".$pageTitle."</h1>\n";
+}
+
+function pageEnd()
+{
+  echo "</body>\n</html>\n";
+}
+
+// Page code begins here
+if ($formMode == -1) {
+  pageHead();
+  echo "<h2>An error occured</h2>\n<div>".$errorMsg."<br /></div>\n";
+  echo "<p>[ <a href=\"submit.php\">Try again</a> ]</p>\n";
+  pageEnd();
+} 
+elseif ($formMode == 0) {
+pageHead();
+?>
+<p>
+Welcome to the Pupunen alloy database submit form. Before you can proceed to submit
+your combinations, you will need to enter your personal submitter GUID in the
+form below. To get your own GUID code, ask for one via tell from <b>Ggr</b> @ BatMUD.
+This identification system is in place to lessen the possibility of fake submissions.
+</p>
+<form action="<? echo $pageIndex; ?>" method="get">
+<b>Submitter GUID code:</b> <input type="text" name="guid" size="30" class="itext"
+<? if ($hasGUID) echo "value=\"".$formGUID."\""; ?> />
+<input type="submit" value=" Continue " class="isubmit" />
+</form>
+<br />
+<div style="border: 1px solid white; padding: 8px; margin: 8px;">
+<b>Alternatively</b>, if you wish to use triggers or some other similar system
+to submit data, this form offers a simple submission "API". You can use an URL
+in following format to submit data automatically:
+<p>
+http://low.fi/~ccr/bat/mat/submit.php?guid=<b>your_guid</b>&amp;mat1=<b>material1</b>&amp;mat2=<b>material2</b>&amp;res=<b>result</b>
+</p>
+The parameters should be self-explanatory. The URL and arguments need to be properly encoded,
+of course (at least spaces replaced with "+", if nothing else).
+</div>
+<h2>WARNING</h2>
+<p>
+Only submit combinations that you have tested yourself RECENTLY (e.g. during 2009 or so).
+Submitting data from old logs is counterproductive, because this project's intention
+is to update the alloy data matrixes.
+</p>
+<?
+pageEnd();
+} elseif ($formMode == 1) {
+pageHead();
+?>
+<form action="<? echo $pageIndex; ?>" method="post">
+Submitter: <b><? echo htmlentities($submitGUIDS[$formGUID]); ?></b><br /><br />
+<textarea name="data" cols="80" rows="10"></textarea><br /><br />
+<div class="icenter">
+<input type="hidden" value="<? echo htmlentities($formGUID); ?>" name="guid" />
+<input type="submit" value=" Submit " class="isubmit" />
+</div>
+</form>
+<p>
+<b>Feel free to bookmark this page, to avoid entering the GUID each time.</b>
+</p>
+<p>
+With the above form, you can submit alloy combinations to be included in this
+alloy database project. The format for submissions should be the line
+<b>"You mix A and B and create a quantity of C"</b>. You can submit as many
+such lines as you wish. <b>Empty lines, and unidentified lines are discarded,
+validated data will be shown on next page!</b>
+</p>
+<p>
+Please notice, that even though the submissions are automatically checked
+for certain "sanity", they will not automatically go into the database,
+but require my (Ggr) manual intervention and may take some time before
+they are added.
+</p>
+<?
+pageEnd();
+} elseif ($formMode == 2) {
+  // Process and check the data
+  $errorSet = FALSE;
+  $errorStr = "";
+  $alloys = array();
+  $postData = explode("\n", $formData);
+  foreach ($postData as $line) {
+    if (preg_match("/You mix ([a-z ]+?) and ([a-z ]+?) and create a quantity of ([a-z ]+)/", $line, $m)) {
+      $mat1 = getMaterial($m[1]);
+      $mat2 = getMaterial($m[2]);
+      $res = getMaterial($m[3]);
+      if ($mat1 === FALSE || $mat2 === FALSE || $res === FALSE) {
+        $errorStr .= "<li>One or more of following materials were unknown: '".
+        $m[1]."', '".$m[2]."' or '".$m[3]."'.</li>";
+        $errorSet = TRUE;
+      } else {
+        if (getMatClass($mat1[14]) == getMatClass($mat2[14]) && getMatClass($mat2[14]) == getMatClass($res[14])) {
+          $alloys[] = array("mat1" => $m[1], "mat2" => $m[2], "res" => $m[3]);
+        } else {
+          $errorStr .= "<li>Material types of '".$m[1]."', '".$m[2].
+          "' and '".$m[3]."' do not match. Invalid submission.</li>";
+          $errorSet = TRUE;
+          break;
+        }
+      }
+    }
+  }
+  
+  // Open datafile and save accepted data
+  $filename = "data-".strtolower($submitGUIDS[$formGUID]).".log";
+  $outFile = fopen($filename, "a");
+  if ($outFile !== FALSE) {
+    chmod($filename, 0600);
+    foreach ($alloys as $val) {
+      $s = $formGUID."#".$val["mat1"]."+".$val["mat2"]."=".$val["res"]."\n";
+      if (fwrite($outFile, $s) === FALSE) {
+        $errorStr = "<li><b>FATAL ERROR! Error sending data to database server!</b> Data not daved!</li>";
+        $errorSet = TRUE;
+        break;
+      }
+    }
+    fclose($outFile);
+  } else {
+    $errorStr .= "<li><b>FATAL ERROR! Could not open database connection!</b> Data not saved!</li>";
+    $errroSet = TRUE;
+  }
+  
+  // Show output
+  if ($hasData) {
+    pageHead();
+    if ($errorSet) {
+      echo "<h3>Errors found!</h3>\n<ul>".$errorStr."</ul>\n";
+    }
+    
+    echo "<h3>Accepted alloys</h3>\n<ul>";
+    foreach ($alloys as $val) {
+      echo "<li><b>".$val["mat1"]."</b> + <b>".$val["mat2"]."</b> = <b>".$val["res"]."</b></li>\n";
+    }
+    echo "</ul>\n".
+    "<p><b>THANK YOU for your submission!</b></p>\n";
+    
+    pageEnd();
+  } else {
+    if ($errorSet) {
+      pageHead();
+      echo "<ol>".$errorStr."</ol>\n";
+      pageEnd();
+    } else
+      echo "OK";
+  }
+  
+}
+?>