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