# HG changeset patch # User Nicholas Jillings # Date 1484830885 0 # Node ID 7d2267e094ca3af536e46a94bbc809bdcc570997 # Parent 7270da0c86472b8bcd6a18dfac59fda7278bace4 Major rework to save.php diff -r 7270da0c8647 -r 7d2267e094ca php/save.php --- a/php/save.php Thu Jan 19 12:13:24 2017 +0000 +++ b/php/save.php Thu Jan 19 13:01:25 2017 +0000 @@ -1,38 +1,129 @@ preserveWhiteSpace = false; - $doc->formatOutput = true; - $doc->loadXML($postText); - $postText = $doc->saveXML(); - $fileHandle = fopen($filename, 'w'); - if ($fileHandle == FALSE) - { - // Filehandle failed - $xml = 'Could not open file'; - echo $xml; - return; - } - $wbytes = fwrite($fileHandle, $postText); - if ($wbytes === FALSE) - { - // FileWrite failed - $xml = 'Could not write file "'.$filename.'"'; - echo $xml; - return; - } - fclose($fileHandle); - - // Return XML confirmation data - $xml = 'OK"'.$filename.'"'; - echo $xml; + +function findNodeByAttribute($nodeList, $attributeName, $attributeValue) { + if (empty($attributeName) || empty($attributeValue) || empty($nodelist)) { + die("Error: Empty findNodeByAttribute"); + } + + foreach($nodeList as $item) { + if ($item->hasAttribute($attributeName)) { + if ($item->getAttribute($attributeValue) == $attributeValue) { + return $item; + } + } + } + return 0; +} + +// Set the response headers +header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); +header("Pragma: no-cache"); +header('Access-Control-Allow-Origin: *'); +header("Content-type: text/xml"); +error_reporting(0); + +// Load up the parameters +$saveFilenamePrefix = ''; +if (isset($_GET['saveFilenamePrefix'])) { + $saveFilenamePrefix = $_GET['saveFilenamePrefix'].'-'; +} +$postText = file_get_contents('php://input'); +$file_key = $_GET['key']; +$filename = '../saves/'.$saveFilenamePrefix.'save-'.$file_key.".xml"; + +if (!file_exists($filename)) { + die('Could not find save'); +} + +// Open the save +$saved_doc = new DOMDocument; +$saved_doc->preserveWhiteSpace = false; +$saved_doc->formatOutput = true; +$saved_doc->loadXML(file_get_contents($filename, FILE_TEXT)); +$saved_root = $saved_doc->documentElement; + +// Construct the XML document into a new tree +$doc = new DOMDocument; +$doc->loadXML($postText); +$docRoot = $doc->documentElement; + +// Add the relavent nodes: +// +$n1 = $docRoot->getElementsByTagName("datetime"); +$n2 = $saved_root->getElementsByTagName("datetime"); +if ($n1->length > 0 && $n2.length == 0) { + $n1 = $doc->importNode($n1->item(0), true); + $docRoot->appendChild($n1); +} + +// +$n1 = $docRoot->getElementsByTagName("navigator"); +$n2 = $saved_root->getElementsByTagName("navigator"); +if ($n1->length > 0 && $n2.length == 0) { + $n1 = $doc->importNode($n1->item(0), true); + $docRoot->appendChild($n1); +} + +// +$n1 = $docRoot->getElementsByTagName("survey"); +$n2 = $saved_root->getElementsByTagName("survey"); +if ($n1->length > 0) { + // Check if in save + if ($n2->length == 0) { + $n2 = 0; + } + $sn1 = findNodeByAttribute($n1, "location", "pre"); + $sn2 = findNodeByAttribute($n2, "location", "pre"); + if ($sn1 != 0) { + if ($sn2 != 0 && $sn2.getAttribute("state") != "complete") { + $saved_root->removeChild($sn2); + $sn2 = 0; + } + if ($sn2 == 0) { + $sn1 = $doc->importNode($sn1->item(0), true); + $docRoot->appendChild($sn1); + } + } + + $sn1 = findNodeByAttribute($n1, "location", "post"); + $sn2 = findNodeByAttribute($n2, "location", "post"); + if ($sn1 != 0) { + if ($sn2 != 0 && $sn2.getAttribute("state") != "complete") { + $saved_root->removeChild($sn2); + $sn2 = 0; + } + if ($sn2 == 0) { + $sn1 = $doc->importNode($sn1->item(0), true); + $docRoot->appendChild($sn1); + } + } +} + +// +$n1 = $docRoot->getElementsByTagName("page"); +$n2 = $saved_root->getElementsByTagName("page"); +if ($n1->length > 0) { + if ($n2->length == 0) { + $n2 = 0; + } + foreach($n1 as $page) { + $ref = $page->getAttribute("ref"); + $pn2 = findNodeByAttribute($n2, "ref", $ref); + if ($pn2 != 0 && $pn2.getAttribute("state") != "complete") { + $saved_root->removeChild($pn2); + $pn2 = 0; + } + if ($pn2 == 0) { + $pn1 = $doc->importNode($page->item(0), true); + $docRoot->appendChild($pn1); + } + } +} + +// Iterate through new doc +$wbytes = $doc_struct->save($filename); + +// Return XML confirmation data +$xml = 'OK"'.$filename.'"'; +echo $xml; ?>