annotate php/save.php @ 2638:82b6b75a91bc

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