annotate php/requestKey.php @ 2827:cf1a3a529177

#209 minor typos
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Tue, 09 May 2017 15:42:59 +0100
parents 1ba5ac9d88e7
children 8805556c0f42
rev   line source
nicholas@2510 1 <?php
nicholas@2510 2
nicholas@2510 3 function generateRandomString($length = 32) {
nicholas@2510 4 // from http://stackoverflow.com/questions/4356289/php-random-string-generator
nicholas@2510 5 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
nicholas@2510 6 $charactersLength = strlen($characters);
nicholas@2510 7 $randomString = '';
nicholas@2510 8 for ($i = 0; $i < $length; $i++) {
nicholas@2510 9 $randomString .= $characters[rand(0, $charactersLength - 1)];
nicholas@2510 10 }
nicholas@2510 11 return $randomString;
nicholas@2510 12 }
nicholas@2510 13
nicholas@2510 14 // Request a new session key from the server
nicholas@2510 15 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
nicholas@2510 16 header("Cache-Control: post-check=0, pre-check=0", false);
nicholas@2510 17 header("Pragma: no-cache");
nicholas@2510 18
nicholas@2626 19 // Get the current test URL
nicholas@2626 20 $testURL = "";
nicholas@2626 21 if (isset($_GET['url'])) {
nicholas@2628 22 $testURL = "../".$_GET["url"];
nicholas@2626 23 }
nicholas@2626 24
nicholas@2510 25 $saves = glob("../saves/*.xml");
nicholas@2510 26
nicholas@2510 27 $key = "";
nicholas@2510 28
nicholas@2510 29 while ($key == "") {
nicholas@2510 30 $tempKey = generateRandomString(32);
nicholas@2510 31 $unique = true;
nicholas@2510 32 foreach($saves as $filename)
nicholas@2510 33 {
nicholas@2510 34 $xml_string = file_get_contents($filename, FILE_TEXT);
nicholas@2510 35 $xml_object = simplexml_load_string($xml_string);
nicholas@2510 36 if ($xml_object != false) {
nicholas@2510 37 if (isset($value['key']))
nicholas@2510 38 {
nicholas@2510 39 if ($value['key'] == $key_requested) {
nicholas@2510 40 $unique = false;
nicholas@2510 41 }
nicholas@2510 42 }
nicholas@2510 43 }
nicholas@2510 44 }
nicholas@2510 45 if ($unique) {
nicholas@2510 46 $key = $tempKey;
nicholas@2510 47 }
nicholas@2510 48 }
nicholas@2510 49
nicholas@2513 50 $filename = "../saves/save-".$key.".xml";
nicholas@2510 51 $fileHandle = fopen($filename, 'w');
nicholas@2510 52 if ($fileHandle == FALSE) {
nicholas@2626 53 die("<response><state>ERROR</state><key>".$key."</key><message>Could not open file for writing</message></response>");
nicholas@2510 54 }
nicholas@2510 55 fclose($fileHandle);
nicholas@2510 56 // TODO:
nicholas@2510 57 // Generate the XML Base file and save it
nicholas@2630 58 $doc_struct = new DOMDocument;
nicholas@2630 59 $doc_struct->preserveWhiteSpace = false;
nicholas@2630 60 $doc_struct->formatOutput = true;
nicholas@2753 61 $doc_struct->loadXML("<waetresult />");
nicholas@2753 62 $doc_struct->documentElement->setAttribute("key", $key);
nicholas@2626 63 // Add the root
nicholas@2628 64 if (file_exists($testURL)) {
nicholas@2631 65 $test_proto_doc = new DOMDocument;
nicholas@2631 66 $test_proto_doc->loadXML(file_get_contents($testURL, FILE_TEXT));
nicholas@2631 67 $test_proto = $test_proto_doc->documentElement;
nicholas@2630 68 $test_proto = $doc_struct->importNode($test_proto, true);
nicholas@2630 69 $doc_struct->documentElement->appendChild($test_proto);
nicholas@2626 70 }
nicholas@2510 71 // Add start time
nicholas@2510 72 // Add IP Address information
nicholas@2510 73 // Save the file
nicholas@2633 74 $doc_struct->save($filename);
nicholas@2510 75 echo "<response><state>OK</state><key>".$key."</key></response>";
nicholas@2510 76 ?>