Mercurial > hg > webaudioevaluationtool
view php/requestKey.php @ 2695:211364181d16
JSHint ABX (#180)
author | Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk> |
---|---|
date | Sat, 11 Mar 2017 18:38:12 +0000 |
parents | 7270da0c8647 |
children | 7d619727130c |
line wrap: on
line source
<?php function generateRandomString($length = 32) { // from http://stackoverflow.com/questions/4356289/php-random-string-generator $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } // Request a new session key from the server header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); // Get the current test URL $testURL = ""; if (isset($_GET['url'])) { $testURL = "../".$_GET["url"]; } $saves = glob("../saves/*.xml"); $key = ""; while ($key == "") { $tempKey = generateRandomString(32); $unique = true; foreach($saves as $filename) { $xml_string = file_get_contents($filename, FILE_TEXT); $xml_object = simplexml_load_string($xml_string); if ($xml_object != false) { if (isset($value['key'])) { if ($value['key'] == $key_requested) { $unique = false; } } } } if ($unique) { $key = $tempKey; } } $filename = "../saves/save-".$key.".xml"; $fileHandle = fopen($filename, 'w'); if ($fileHandle == FALSE) { die("<response><state>ERROR</state><key>".$key."</key><message>Could not open file for writing</message></response>"); } fclose($fileHandle); // TODO: // Generate the XML Base file and save it $doc_struct = new DOMDocument; $doc_struct->preserveWhiteSpace = false; $doc_struct->formatOutput = true; $doc_struct->loadXML("<waetresult/>"); // Add the root if (file_exists($testURL)) { $test_proto_doc = new DOMDocument; $test_proto_doc->loadXML(file_get_contents($testURL, FILE_TEXT)); $test_proto = $test_proto_doc->documentElement; $test_proto = $doc_struct->importNode($test_proto, true); $doc_struct->documentElement->appendChild($test_proto); } // Add start time // Add IP Address information // Save the file $doc_struct->save($filename); echo "<response><state>OK</state><key>".$key."</key></response>"; ?>