comparison php/requestKey.php @ 2510:8536e978ab6f

Work for #158
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Mon, 24 Oct 2016 10:39:20 +0100
parents
children 177cbd750610
comparison
equal deleted inserted replaced
2508:9b536838a962 2510:8536e978ab6f
1 <?php
2
3 function generateRandomString($length = 32) {
4 // from http://stackoverflow.com/questions/4356289/php-random-string-generator
5 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
6 $charactersLength = strlen($characters);
7 $randomString = '';
8 for ($i = 0; $i < $length; $i++) {
9 $randomString .= $characters[rand(0, $charactersLength - 1)];
10 }
11 return $randomString;
12 }
13
14 // Request a new session key from the server
15 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
16 header("Cache-Control: post-check=0, pre-check=0", false);
17 header("Pragma: no-cache");
18
19 $saves = glob("../saves/*.xml");
20
21 $key = "";
22
23 while ($key == "") {
24 $tempKey = generateRandomString(32);
25 $unique = true;
26 foreach($saves as $filename)
27 {
28 $xml_string = file_get_contents($filename, FILE_TEXT);
29 $xml_object = simplexml_load_string($xml_string);
30 if ($xml_object != false) {
31 if (isset($value['key']))
32 {
33 if ($value['key'] == $key_requested) {
34 $unique = false;
35 }
36 }
37 }
38 }
39 if ($unique) {
40 $key = $tempKey;
41 }
42 }
43
44 $filename = "saves/save-".$key.".xml"
45 $fileHandle = fopen($filename, 'w');
46 if ($fileHandle == FALSE) {
47 echo "<response><state>ERROR</state><key>".$key."</key></response>";
48 }
49 fclose($fileHandle);
50 // TODO:
51 // Generate the XML Base file and save it
52 $doc_struct = new SimpleXMLElement('<waetresult/>');
53 $doc_struct->addAttribute("key",$key);
54 // Add start time
55 // Add IP Address information
56 // Save the file
57 $doc_struct->asXML($filename);
58 echo "<response><state>OK</state><key>".$key."</key></response>";
59 return;
60 ?>