Mercurial > hg > webaudioevaluationtool
comparison php/save.php @ 2634:7d2267e094ca
Major rework to save.php
| author | Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk> |
|---|---|
| date | Thu, 19 Jan 2017 13:01:25 +0000 |
| parents | 464c6c6692d6 |
| children | 325e1ca3e03b |
comparison
equal
deleted
inserted
replaced
| 2633:7270da0c8647 | 2634:7d2267e094ca |
|---|---|
| 1 <?php | 1 <?php |
| 2 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); | 2 |
| 3 header("Cache-Control: post-check=0, pre-check=0", false); | 3 function findNodeByAttribute($nodeList, $attributeName, $attributeValue) { |
| 4 header("Pragma: no-cache"); | 4 if (empty($attributeName) || empty($attributeValue) || empty($nodelist)) { |
| 5 error_reporting(0); | 5 die("Error: Empty findNodeByAttribute"); |
| 6 $saveFilenamePrefix = isset($_GET['saveFilenamePrefix']) ? $_GET['saveFilenamePrefix'].'-' : ''; | 6 } |
| 7 header('Access-Control-Allow-Origin: *'); | 7 |
| 8 header("Content-type: text/xml"); | 8 foreach($nodeList as $item) { |
| 9 $postText = file_get_contents('php://input'); | 9 if ($item->hasAttribute($attributeName)) { |
| 10 $file_key = $_GET['key']; | 10 if ($item->getAttribute($attributeValue) == $attributeValue) { |
| 11 $filename = '../saves/'.$saveFilenamePrefix.'save-'.$file_key.".xml"; | 11 return $item; |
| 12 $doc = new DOMDocument; | 12 } |
| 13 $doc->preserveWhiteSpace = false; | 13 } |
| 14 $doc->formatOutput = true; | 14 } |
| 15 $doc->loadXML($postText); | 15 return 0; |
| 16 $postText = $doc->saveXML(); | 16 } |
| 17 $fileHandle = fopen($filename, 'w'); | 17 |
| 18 if ($fileHandle == FALSE) | 18 // Set the response headers |
| 19 { | 19 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); |
| 20 // Filehandle failed | 20 header("Pragma: no-cache"); |
| 21 $xml = '<response state="error"><message>Could not open file</message></response>'; | 21 header('Access-Control-Allow-Origin: *'); |
| 22 echo $xml; | 22 header("Content-type: text/xml"); |
| 23 return; | 23 error_reporting(0); |
| 24 } | 24 |
| 25 $wbytes = fwrite($fileHandle, $postText); | 25 // Load up the parameters |
| 26 if ($wbytes === FALSE) | 26 $saveFilenamePrefix = ''; |
| 27 { | 27 if (isset($_GET['saveFilenamePrefix'])) { |
| 28 // FileWrite failed | 28 $saveFilenamePrefix = $_GET['saveFilenamePrefix'].'-'; |
| 29 $xml = '<response state="error"><message>Could not write file "'.$filename.'"</message></response>'; | 29 } |
| 30 echo $xml; | 30 $postText = file_get_contents('php://input'); |
| 31 return; | 31 $file_key = $_GET['key']; |
| 32 } | 32 $filename = '../saves/'.$saveFilenamePrefix.'save-'.$file_key.".xml"; |
| 33 fclose($fileHandle); | 33 |
| 34 | 34 if (!file_exists($filename)) { |
| 35 // Return XML confirmation data | 35 die('<response state="error"><message>Could not find save</message></response>'); |
| 36 $xml = '<response state="OK"><message>OK</message><file bytes="'.$wbytes.'">"'.$filename.'"</file></response>'; | 36 } |
| 37 echo $xml; | 37 |
| 38 // Open the save | |
| 39 $saved_doc = new DOMDocument; | |
| 40 $saved_doc->preserveWhiteSpace = false; | |
| 41 $saved_doc->formatOutput = true; | |
| 42 $saved_doc->loadXML(file_get_contents($filename, FILE_TEXT)); | |
| 43 $saved_root = $saved_doc->documentElement; | |
| 44 | |
| 45 // Construct the XML document into a new tree | |
| 46 $doc = new DOMDocument; | |
| 47 $doc->loadXML($postText); | |
| 48 $docRoot = $doc->documentElement; | |
| 49 | |
| 50 // Add the relavent nodes: | |
| 51 // <datetime> | |
| 52 $n1 = $docRoot->getElementsByTagName("datetime"); | |
| 53 $n2 = $saved_root->getElementsByTagName("datetime"); | |
| 54 if ($n1->length > 0 && $n2.length == 0) { | |
| 55 $n1 = $doc->importNode($n1->item(0), true); | |
| 56 $docRoot->appendChild($n1); | |
| 57 } | |
| 58 | |
| 59 //<navigator> | |
| 60 $n1 = $docRoot->getElementsByTagName("navigator"); | |
| 61 $n2 = $saved_root->getElementsByTagName("navigator"); | |
| 62 if ($n1->length > 0 && $n2.length == 0) { | |
| 63 $n1 = $doc->importNode($n1->item(0), true); | |
| 64 $docRoot->appendChild($n1); | |
| 65 } | |
| 66 | |
| 67 //<survey location="pre"> | |
| 68 $n1 = $docRoot->getElementsByTagName("survey"); | |
| 69 $n2 = $saved_root->getElementsByTagName("survey"); | |
| 70 if ($n1->length > 0) { | |
| 71 // Check if in save | |
| 72 if ($n2->length == 0) { | |
| 73 $n2 = 0; | |
| 74 } | |
| 75 $sn1 = findNodeByAttribute($n1, "location", "pre"); | |
| 76 $sn2 = findNodeByAttribute($n2, "location", "pre"); | |
| 77 if ($sn1 != 0) { | |
| 78 if ($sn2 != 0 && $sn2.getAttribute("state") != "complete") { | |
| 79 $saved_root->removeChild($sn2); | |
| 80 $sn2 = 0; | |
| 81 } | |
| 82 if ($sn2 == 0) { | |
| 83 $sn1 = $doc->importNode($sn1->item(0), true); | |
| 84 $docRoot->appendChild($sn1); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 $sn1 = findNodeByAttribute($n1, "location", "post"); | |
| 89 $sn2 = findNodeByAttribute($n2, "location", "post"); | |
| 90 if ($sn1 != 0) { | |
| 91 if ($sn2 != 0 && $sn2.getAttribute("state") != "complete") { | |
| 92 $saved_root->removeChild($sn2); | |
| 93 $sn2 = 0; | |
| 94 } | |
| 95 if ($sn2 == 0) { | |
| 96 $sn1 = $doc->importNode($sn1->item(0), true); | |
| 97 $docRoot->appendChild($sn1); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 //<page ref=""> | |
| 103 $n1 = $docRoot->getElementsByTagName("page"); | |
| 104 $n2 = $saved_root->getElementsByTagName("page"); | |
| 105 if ($n1->length > 0) { | |
| 106 if ($n2->length == 0) { | |
| 107 $n2 = 0; | |
| 108 } | |
| 109 foreach($n1 as $page) { | |
| 110 $ref = $page->getAttribute("ref"); | |
| 111 $pn2 = findNodeByAttribute($n2, "ref", $ref); | |
| 112 if ($pn2 != 0 && $pn2.getAttribute("state") != "complete") { | |
| 113 $saved_root->removeChild($pn2); | |
| 114 $pn2 = 0; | |
| 115 } | |
| 116 if ($pn2 == 0) { | |
| 117 $pn1 = $doc->importNode($page->item(0), true); | |
| 118 $docRoot->appendChild($pn1); | |
| 119 } | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 // Iterate through new doc | |
| 124 $wbytes = $doc_struct->save($filename); | |
| 125 | |
| 126 // Return XML confirmation data | |
| 127 $xml = '<response state="OK"><message>OK</message><file bytes="'.$wbytes.'">"'.$filename.'"</file></response>'; | |
| 128 echo $xml; | |
| 38 ?> | 129 ?> |
