changeset 2510:8536e978ab6f

Work for #158
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Mon, 24 Oct 2016 10:39:20 +0100
parents 9b536838a962
children 177cbd750610
files js/core.js php/keygen.php php/requestKey.php python/pythonServer.py
diffstat 4 files changed, 106 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/js/core.js	Tue Oct 18 10:33:17 2016 +0100
+++ b/js/core.js	Mon Oct 24 10:39:20 2016 +0100
@@ -3101,7 +3101,7 @@
     this.initialise = function (existingStore) {
         if (existingStore == undefined) {
             // We need to get the sessionKey
-            this.SessionKey.generateKey();
+            this.SessionKey.requestKey();
             this.document = document.implementation.createDocument(null, "waetresult", null);
             this.root = this.document.childNodes[0];
             var projectDocument = specification.projectXML;
@@ -3159,6 +3159,18 @@
             this.request.addEventListener("load", this);
             this.request.send();
         },
+        requestKey: function () {
+            // For new servers, request a new key from the server
+            var returnURL = "";
+            if (typeof specification.projectReturn == "string") {
+                if (specification.projectReturn.substr(0, 4) == "http") {
+                    returnURL = specification.projectReturn;
+                }
+            }
+            this.request.open("GET", returnURL + "php/requestKey.php", true);
+            this.request.addEventListener("load", this);
+            this.request.send();
+        },
         update: function () {
             if (this.key == null) {
                 console.log("Cannot save as key == null");
--- a/php/keygen.php	Tue Oct 18 10:33:17 2016 +0100
+++ b/php/keygen.php	Mon Oct 24 10:39:20 2016 +0100
@@ -26,7 +26,12 @@
             }
         }
     }
-    echo $xml_good;
+    $filename = "saves/save-".$key_requested.".xml";
+    $fileHandle = fopen($filename, 'w');
+    if ($fileHandle == FALSE) {
+        echo $xml_error;
+    }
+    fclose($fileHandle);
     // TODO:
     //  Generate the XML Base file and save it
     $doc_struct = new SimpleXMLElement('<waetresult/>');
@@ -34,10 +39,11 @@
     //  Add start time
     //  Add IP Address information
     //  Save the file
-    $doc_struct->asXML("saves/save-".$key_requested.".xml");
+    $doc_struct->asXML($filename);
+    echo $xml_good;
     return;
 } else {
     echo $xml_error;
     return;
 }
-?>
\ No newline at end of file
+?>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/php/requestKey.php	Mon Oct 24 10:39:20 2016 +0100
@@ -0,0 +1,60 @@
+<?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");
+
+$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) {
+    echo "<response><state>ERROR</state><key>".$key."</key></response>";
+}
+fclose($fileHandle);
+// TODO:
+//  Generate the XML Base file and save it
+$doc_struct = new SimpleXMLElement('<waetresult/>');
+$doc_struct->addAttribute("key",$key);
+//  Add start time
+//  Add IP Address information
+//  Save the file
+$doc_struct->asXML($filename);
+echo "<response><state>OK</state><key>".$key."</key></response>";
+return;
+?>
--- a/python/pythonServer.py	Tue Oct 18 10:33:17 2016 +0100
+++ b/python/pythonServer.py	Mon Oct 24 10:39:20 2016 +0100
@@ -11,6 +11,8 @@
 import operator
 import xml.etree.ElementTree as ET
 import copy
+import string
+import random
 
 if sys.version_info[0] == 2:
     # Version 2.x
@@ -130,6 +132,26 @@
 	file = open("../saves/save-"+key+".xml",'w')
 	file.write("<waetresult key="+key+"/>")
 	file.close();
+    
+def requestKey(s):
+    reply = ""
+    key = ''
+    while key == '':
+        tempKey = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(32));
+        if (os.path.isfile("saves/save-"+tempKey+".xml") == False):
+            key = tempKey
+    s.send_response(200)
+    s.send_header("Content-type", "application/xml");
+    s.end_headers()
+    reply = "<response><state>OK</state><key>"+key+"</key></response>"
+    if sys.version_info[0] == 2:
+        s.wfile.write(reply)
+    elif sys.version_info[0] == 3:
+        s.wfile.write(bytes(reply, "utf-8"))
+    file = open("../saves/save-"+key+".xml",'w')
+    file.write("<waetresult key="+key+"/>")
+    file.close()
+    
 
 def saveFile(self):
     global curFileName
@@ -228,6 +250,8 @@
             send404(request)
         elif (request.path.split('?',1)[0] == "/php/keygen.php"):
             keygen(request);
+        elif (request.path.split('?',1)[0] == "/php/requestKey.php"):
+            requestKey(request);
         elif (request.path.split('?',1)[0] == "/php/pool.php"):
             poolXML(request);
         else: