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@2510
|
19 $saves = glob("../saves/*.xml");
|
nicholas@2510
|
20
|
nicholas@2510
|
21 $key = "";
|
nicholas@2510
|
22
|
nicholas@2510
|
23 while ($key == "") {
|
nicholas@2510
|
24 $tempKey = generateRandomString(32);
|
nicholas@2510
|
25 $unique = true;
|
nicholas@2510
|
26 foreach($saves as $filename)
|
nicholas@2510
|
27 {
|
nicholas@2510
|
28 $xml_string = file_get_contents($filename, FILE_TEXT);
|
nicholas@2510
|
29 $xml_object = simplexml_load_string($xml_string);
|
nicholas@2510
|
30 if ($xml_object != false) {
|
nicholas@2510
|
31 if (isset($value['key']))
|
nicholas@2510
|
32 {
|
nicholas@2510
|
33 if ($value['key'] == $key_requested) {
|
nicholas@2510
|
34 $unique = false;
|
nicholas@2510
|
35 }
|
nicholas@2510
|
36 }
|
nicholas@2510
|
37 }
|
nicholas@2510
|
38 }
|
nicholas@2510
|
39 if ($unique) {
|
nicholas@2510
|
40 $key = $tempKey;
|
nicholas@2510
|
41 }
|
nicholas@2510
|
42 }
|
nicholas@2510
|
43
|
nicholas@2513
|
44 $filename = "../saves/save-".$key.".xml";
|
nicholas@2510
|
45 $fileHandle = fopen($filename, 'w');
|
nicholas@2510
|
46 if ($fileHandle == FALSE) {
|
nicholas@2514
|
47 echo "<response><state>ERROR</state><key>".$key."</key><message>Could not open file for writing</message></response>";
|
nicholas@2511
|
48 return;
|
nicholas@2510
|
49 }
|
nicholas@2510
|
50 fclose($fileHandle);
|
nicholas@2510
|
51 // TODO:
|
nicholas@2510
|
52 // Generate the XML Base file and save it
|
nicholas@2510
|
53 $doc_struct = new SimpleXMLElement('<waetresult/>');
|
nicholas@2510
|
54 $doc_struct->addAttribute("key",$key);
|
nicholas@2510
|
55 // Add start time
|
nicholas@2510
|
56 // Add IP Address information
|
nicholas@2510
|
57 // Save the file
|
nicholas@2510
|
58 $doc_struct->asXML($filename);
|
nicholas@2510
|
59 echo "<response><state>OK</state><key>".$key."</key></response>";
|
nicholas@2510
|
60 return;
|
nicholas@2510
|
61 ?>
|