annotate php/requestKey.php @ 3141:335bc77627e0 tip

fixing discrete interface to allow labels to display
author Dave Moffat <me@davemoffat.com>
date Mon, 26 Jul 2021 12:15:24 +0100
parents c7de85e03321
children
rev   line source
nicholas@2510 1 <?php
nicholas@3123 2 include_once("config.php");
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@3028 14 if (!file_exists("../saves")) {
nicholas@3028 15 mkdir("../saves");
nicholas@3028 16 }
nicholas@3028 17
nicholas@2510 18 // Request a new session key from the server
nicholas@2510 19 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
nicholas@2510 20 header("Cache-Control: post-check=0, pre-check=0", false);
nicholas@2510 21 header("Pragma: no-cache");
nicholas@2510 22
nicholas@2626 23 // Get the current test URL
nicholas@2940 24 // Load up the parameters
nicholas@2940 25 $saveFilenamePrefix = '';
nicholas@2940 26 if (isset($_GET['saveFilenamePrefix'])) {
nicholas@2940 27 $saveFilenamePrefix = $_GET['saveFilenamePrefix'].'-';
nicholas@2940 28 } else {
nicholas@2940 29 $saveFilenamePrefix = "save-";
nicholas@2940 30 }
nicholas@2626 31 $testURL = "";
nicholas@2626 32 if (isset($_GET['url'])) {
nicholas@2628 33 $testURL = "../".$_GET["url"];
nicholas@2626 34 }
nicholas@2626 35
nicholas@2510 36 $saves = glob("../saves/*.xml");
nicholas@2510 37
nicholas@2510 38 $key = "";
nicholas@2510 39
nicholas@2510 40 while ($key == "") {
nicholas@2510 41 $tempKey = generateRandomString(32);
nicholas@2510 42 $unique = true;
nicholas@2510 43 foreach($saves as $filename)
nicholas@2510 44 {
nicholas@2510 45 $xml_string = file_get_contents($filename, FILE_TEXT);
nicholas@2510 46 $xml_object = simplexml_load_string($xml_string);
nicholas@2510 47 if ($xml_object != false) {
nicholas@2510 48 if (isset($value['key']))
nicholas@2510 49 {
nicholas@2510 50 if ($value['key'] == $key_requested) {
nicholas@2510 51 $unique = false;
nicholas@2510 52 }
nicholas@2510 53 }
nicholas@2510 54 }
nicholas@2510 55 }
nicholas@2510 56 if ($unique) {
nicholas@2510 57 $key = $tempKey;
nicholas@2510 58 }
nicholas@2510 59 }
nicholas@3120 60 $saveLocation = getSaveLocation();
nicholas@3120 61 $filename = $saveLocation.$saveFilenamePrefix.$key.".xml";
nicholas@2510 62 $fileHandle = fopen($filename, 'w');
nicholas@2510 63 if ($fileHandle == FALSE) {
nicholas@2626 64 die("<response><state>ERROR</state><key>".$key."</key><message>Could not open file for writing</message></response>");
nicholas@2510 65 }
nicholas@2510 66 fclose($fileHandle);
nicholas@2510 67 // TODO:
nicholas@2510 68 // Generate the XML Base file and save it
nicholas@2630 69 $doc_struct = new DOMDocument;
nicholas@2630 70 $doc_struct->preserveWhiteSpace = false;
nicholas@2630 71 $doc_struct->formatOutput = true;
nicholas@2753 72 $doc_struct->loadXML("<waetresult />");
nicholas@2753 73 $doc_struct->documentElement->setAttribute("key", $key);
nicholas@2626 74 // Add the root
nicholas@2628 75 if (file_exists($testURL)) {
nicholas@2631 76 $test_proto_doc = new DOMDocument;
nicholas@2631 77 $test_proto_doc->loadXML(file_get_contents($testURL, FILE_TEXT));
nicholas@2631 78 $test_proto = $test_proto_doc->documentElement;
nicholas@2630 79 $test_proto = $doc_struct->importNode($test_proto, true);
nicholas@2630 80 $doc_struct->documentElement->appendChild($test_proto);
nicholas@2626 81 }
nicholas@2510 82 // Add start time
nicholas@2510 83 // Add IP Address information
nicholas@2510 84 // Save the file
nicholas@2633 85 $doc_struct->save($filename);
nicholas@2510 86 echo "<response><state>OK</state><key>".$key."</key></response>";
nicholas@2510 87 ?>