Mercurial > hg > webaudioevaluationtool
changeset 1239:a54422902bbd
Adding PHP Keygen
author | Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk> |
---|---|
date | Tue, 08 Mar 2016 14:35:51 +0000 |
parents | 8c56eda17acd |
children | 33e684e3ea81 |
files | core.js keygen.php |
diffstat | 2 files changed, 114 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/core.js Sun Mar 06 20:55:00 2016 +0000 +++ b/core.js Tue Mar 08 14:35:51 2016 +0000 @@ -166,38 +166,10 @@ //var decode = $.parseXML(response); //projectXML = $(decode); - // First perform XML schema validation - var Module = { - xml: response, - schema: schemaXSD, - arguments:["--noout", "--schema", 'test-schema.xsd','document.xml'] - }; - - var xmllint = validateXML(Module); - console.log(xmllint); - if(xmllint != 'document.xml validates\n') - { - document.getElementsByTagName('body')[0].innerHTML = null; - var msg = document.createElement("h3"); - msg.textContent = "FATAL ERROR"; - var span = document.createElement("h4"); - span.textContent = "The XML validator returned the following errors when decoding your XML file"; - document.getElementsByTagName('body')[0].appendChild(msg); - document.getElementsByTagName('body')[0].appendChild(span); - xmllint = xmllint.split('\n'); - for (var i in xmllint) - { - document.getElementsByTagName('body')[0].appendChild(document.createElement('br')); - var span = document.createElement("span"); - span.textContent = xmllint[i]; - document.getElementsByTagName('body')[0].appendChild(span); - } - return; - } - - var parse = new DOMParser(); - projectXML = parse.parseFromString(response,'text/xml'); - var errorNode = projectXML.getElementsByTagName('parsererror'); + // Check if XML is new or a resumption + var parse = new DOMParser(); + var responseDocument = parse.parseFromString(response,'text/xml'); + var errorNode = responseDocument.getElementsByTagName('parsererror'); if (errorNode.length >= 1) { var msg = document.createElement("h3"); @@ -210,10 +182,51 @@ document.getElementsByTagName('body')[0].appendChild(errorNode[0]); return; } + if (responseDocument.children[0].nodeName == "waet") { + // document is a specification + + // Perform XML schema validation + var Module = { + xml: response, + schema: schemaXSD, + arguments:["--noout", "--schema", 'test-schema.xsd','document.xml'] + }; + projectXML = responseDocument; + var xmllint = validateXML(Module); + console.log(xmllint); + if(xmllint != 'document.xml validates\n') + { + document.getElementsByTagName('body')[0].innerHTML = null; + var msg = document.createElement("h3"); + msg.textContent = "FATAL ERROR"; + var span = document.createElement("h4"); + span.textContent = "The XML validator returned the following errors when decoding your XML file"; + document.getElementsByTagName('body')[0].appendChild(msg); + document.getElementsByTagName('body')[0].appendChild(span); + xmllint = xmllint.split('\n'); + for (var i in xmllint) + { + document.getElementsByTagName('body')[0].appendChild(document.createElement('br')); + var span = document.createElement("span"); + span.textContent = xmllint[i]; + document.getElementsByTagName('body')[0].appendChild(span); + } + return; + } + + // Generate the session-key + storage.initialise(); + + } else if (responseDocument.children[0].nodeName == "waetresult") { + // document is a result + projectXML = responseDocument.getElementsByTagName('waet')[0]; + // Use the session-key + var sessionKey = responseDocument.children[0].getAttribute(key); + storage.initialise(sessionKey); + } // Build the specification specification.decode(projectXML); - storage.initialise(); /// CHECK FOR SAMPLE RATE COMPATIBILITY if (specification.sampleRate != undefined) { if (Number(specification.sampleRate) != audioContext.sampleRate) { @@ -432,6 +445,10 @@ return Math.pow(10,gain/20.0); } +function randomString(length) { + return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1); +} + function interfacePopup() { // Creates an object to manage the popup this.popup = null; @@ -3097,11 +3114,38 @@ this.root = this.document.childNodes[0]; this.state = 0; - this.initialise = function() + this.initialise = function(sessionKey) { if (specification.preTest != undefined){this.globalPreTest = new this.surveyNode(this,this.root,specification.preTest);} if (specification.postTest != undefined){this.globalPostTest = new this.surveyNode(this,this.root,specification.postTest);} + if (sessionKey == undefined) { + // We need to get the sessionKey + this.SessionKey.generateKey(); + } else { + this.SessionKey.key = sessionKey; + } }; + + this.SessionKey = { + key: null, + request: new XMLHttpRequest(), + parent: this, + handleEvent: function() { + var parse = new DOMParser(); + var xml = parse.parseFromString(this.request.response,"text/xml"); + if (xml.getAllElementsByTagName("state")[0].textContent == "OK") { + this.key = xml.getAllElementsByTagName("key")[0].textContent; + } else { + this.generateKey(); + } + }, + generateKey: function() { + var temp_key = randomString(32); + this.request.open("GET","keygen.php?key="+temp_key,true); + this.request.addEventListener("load",this); + this.request.send(); + } + } this.createTestPageStore = function(specification) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keygen.php Tue Mar 08 14:35:51 2016 +0000 @@ -0,0 +1,36 @@ +<?php +// This checks the key sent by the JavaScript against the current bunch of saves +// XML Saves location - assumes it will be saves/ +$saves = glob("../saves/*.xml"); + +$key_requested = $_GET['key']; + +$xml_good = "<response><state>OK</state><key>".$key_requested."</key></response>"; +$xml_bad = "<response><state>NO</state><key>".$key_requested."</key></response>"; +$xml_bad = "<response><state>ERROR</state><key>".$key_requested."</key></response>"; +if (is_array($saves)) +{ + 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) { + echo $xml_bad; + return; + } + } + } + } + echo $xml_good; + // TODO: + // Generate the XML Base file and save it + // Add start time + // Add IP Address information + return; +} else { + echo $xml_error; + return; +} +?> \ No newline at end of file