annotate php/requestKey.php @ 3082:396b9aac3bb9

Merge branch 'master' into vnext
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Wed, 22 Nov 2017 10:08:26 +0000
parents 8805556c0f42
children 9a201c63a0eb
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@2940 20 // Load up the parameters
nicholas@2940 21 $saveFilenamePrefix = '';
nicholas@2940 22 if (isset($_GET['saveFilenamePrefix'])) {
nicholas@2940 23 $saveFilenamePrefix = $_GET['saveFilenamePrefix'].'-';
nicholas@2940 24 } else {
nicholas@2940 25 $saveFilenamePrefix = "save-";
nicholas@2940 26 }
nicholas@2626 27 $testURL = "";
nicholas@2626 28 if (isset($_GET['url'])) {
nicholas@2628 29 $testURL = "../".$_GET["url"];
nicholas@2626 30 }
nicholas@2626 31
nicholas@2510 32 $saves = glob("../saves/*.xml");
nicholas@2510 33
nicholas@2510 34 $key = "";
nicholas@2510 35
nicholas@2510 36 while ($key == "") {
nicholas@2510 37 $tempKey = generateRandomString(32);
nicholas@2510 38 $unique = true;
nicholas@2510 39 foreach($saves as $filename)
nicholas@2510 40 {
nicholas@2510 41 $xml_string = file_get_contents($filename, FILE_TEXT);
nicholas@2510 42 $xml_object = simplexml_load_string($xml_string);
nicholas@2510 43 if ($xml_object != false) {
nicholas@2510 44 if (isset($value['key']))
nicholas@2510 45 {
nicholas@2510 46 if ($value['key'] == $key_requested) {
nicholas@2510 47 $unique = false;
nicholas@2510 48 }
nicholas@2510 49 }
nicholas@2510 50 }
nicholas@2510 51 }
nicholas@2510 52 if ($unique) {
nicholas@2510 53 $key = $tempKey;
nicholas@2510 54 }
nicholas@2510 55 }
nicholas@2510 56
nicholas@2940 57 $filename = "../saves/".$saveFilenamePrefix.$key.".xml";
nicholas@2510 58 $fileHandle = fopen($filename, 'w');
nicholas@2510 59 if ($fileHandle == FALSE) {
nicholas@2626 60 die("<response><state>ERROR</state><key>".$key."</key><message>Could not open file for writing</message></response>");
nicholas@2510 61 }
nicholas@2510 62 fclose($fileHandle);
nicholas@2510 63 // TODO:
nicholas@2510 64 // Generate the XML Base file and save it
nicholas@2630 65 $doc_struct = new DOMDocument;
nicholas@2630 66 $doc_struct->preserveWhiteSpace = false;
nicholas@2630 67 $doc_struct->formatOutput = true;
nicholas@2753 68 $doc_struct->loadXML("<waetresult />");
nicholas@2753 69 $doc_struct->documentElement->setAttribute("key", $key);
nicholas@2626 70 // Add the root
nicholas@2628 71 if (file_exists($testURL)) {
nicholas@2631 72 $test_proto_doc = new DOMDocument;
nicholas@2631 73 $test_proto_doc->loadXML(file_get_contents($testURL, FILE_TEXT));
nicholas@2631 74 $test_proto = $test_proto_doc->documentElement;
nicholas@2630 75 $test_proto = $doc_struct->importNode($test_proto, true);
nicholas@2630 76 $doc_struct->documentElement->appendChild($test_proto);
nicholas@2626 77 }
nicholas@2510 78 // Add start time
nicholas@2510 79 // Add IP Address information
nicholas@2510 80 // Save the file
nicholas@2633 81 $doc_struct->save($filename);
nicholas@2510 82 echo "<response><state>OK</state><key>".$key."</key></response>";
nicholas@2510 83 ?>