nicholas@2224: hasAttribute($attributeName)) {
nicholas@2634: if ($item->getAttribute($attributeValue) == $attributeValue) {
nicholas@2634: return $item;
nicholas@2634: }
nicholas@2634: }
nicholas@2634: }
nicholas@2634: return 0;
nicholas@2634: }
nicholas@2634:
nicholas@2634: // Set the response headers
nicholas@2634: header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
nicholas@2634: header("Pragma: no-cache");
nicholas@2634: header('Access-Control-Allow-Origin: *');
nicholas@2634: header("Content-type: text/xml");
nicholas@2635: //error_reporting(0);
nicholas@2634:
nicholas@2634: // Load up the parameters
nicholas@2634: $saveFilenamePrefix = '';
nicholas@2634: if (isset($_GET['saveFilenamePrefix'])) {
nicholas@2634: $saveFilenamePrefix = $_GET['saveFilenamePrefix'].'-';
nicholas@2754: } else {
nicholas@2755: $saveFilenamePrefix = "save-";
nicholas@2634: }
nicholas@2634: $postText = file_get_contents('php://input');
nicholas@2634: $file_key = $_GET['key'];
n@2997:
n@2997: $update = false;
n@2997: if (isset($_GET["update"])) {
n@2997: $update = $_GET["update"] == "update";
n@2997: }
n@2997:
nicholas@3120: $saveLocation = getSaveLocation();
nicholas@3120:
n@2997: if ($update) {
nicholas@3120: $filename = $saveLocation.'update-'.$saveFilenamePrefix.$file_key.".xml";
n@2997: } else {
nicholas@3120: $filename = $saveLocation.$saveFilenamePrefix.$file_key.".xml";
n@2997: }
nicholas@2634:
nicholas@2634: if (!file_exists($filename)) {
nicholas@2634: die('Could not find save');
nicholas@2634: }
nicholas@2634:
nicholas@2634: // Open the save
nicholas@2634: $saved_doc = new DOMDocument;
nicholas@2634: $saved_doc->preserveWhiteSpace = false;
nicholas@2634: $saved_doc->formatOutput = true;
nicholas@2634: $saved_doc->loadXML(file_get_contents($filename, FILE_TEXT));
nicholas@2634: $saved_root = $saved_doc->documentElement;
nicholas@2634:
nicholas@2634: // Construct the XML document into a new tree
nicholas@2634: $doc = new DOMDocument;
nicholas@2634: $doc->loadXML($postText);
nicholas@2634: $docRoot = $doc->documentElement;
nicholas@2634:
nicholas@2634: // Add the relavent nodes:
nicholas@2634: //
nicholas@2634: $n1 = $docRoot->getElementsByTagName("datetime");
nicholas@2634: $n2 = $saved_root->getElementsByTagName("datetime");
nicholas@2636: if ($n1->length > 0 && $n2->length == 0) {
nicholas@2634: $n1 = $doc->importNode($n1->item(0), true);
nicholas@2634: $docRoot->appendChild($n1);
nicholas@2634: }
nicholas@2634:
nicholas@2634: //
nicholas@2634: $n1 = $docRoot->getElementsByTagName("navigator");
nicholas@2634: $n2 = $saved_root->getElementsByTagName("navigator");
nicholas@2636: if ($n1->length > 0 && $n2->length == 0) {
nicholas@2634: $n1 = $doc->importNode($n1->item(0), true);
nicholas@2634: $docRoot->appendChild($n1);
nicholas@2634: }
nicholas@2634:
nicholas@2634: //
nicholas@2634: $n1 = $docRoot->getElementsByTagName("survey");
nicholas@2634: $n2 = $saved_root->getElementsByTagName("survey");
nicholas@2634: if ($n1->length > 0) {
nicholas@2634: // Check if in save
nicholas@2634: if ($n2->length == 0) {
nicholas@2634: $n2 = 0;
nicholas@2634: }
nicholas@2634: $sn1 = findNodeByAttribute($n1, "location", "pre");
nicholas@2634: $sn2 = findNodeByAttribute($n2, "location", "pre");
nicholas@2634: if ($sn1 != 0) {
n@2966: if ($sn2 != 0) {
nicholas@2634: $saved_root->removeChild($sn2);
nicholas@2634: $sn2 = 0;
nicholas@2634: }
nicholas@2634: if ($sn2 == 0) {
nicholas@2634: $sn1 = $doc->importNode($sn1->item(0), true);
nicholas@2634: $docRoot->appendChild($sn1);
nicholas@2634: }
nicholas@2634: }
nicholas@2634:
nicholas@2634: $sn1 = findNodeByAttribute($n1, "location", "post");
nicholas@2634: $sn2 = findNodeByAttribute($n2, "location", "post");
nicholas@2634: if ($sn1 != 0) {
n@2966: if ($sn2 != 0) {
nicholas@2634: $saved_root->removeChild($sn2);
nicholas@2634: $sn2 = 0;
nicholas@2634: }
nicholas@2634: if ($sn2 == 0) {
nicholas@2634: $sn1 = $doc->importNode($sn1->item(0), true);
nicholas@2634: $docRoot->appendChild($sn1);
nicholas@2634: }
nicholas@2634: }
nicholas@2634: }
nicholas@2634:
nicholas@2634: //
nicholas@2634: $n1 = $docRoot->getElementsByTagName("page");
nicholas@2634: $n2 = $saved_root->getElementsByTagName("page");
nicholas@2634: if ($n1->length > 0) {
nicholas@2634: if ($n2->length == 0) {
nicholas@2634: $n2 = 0;
nicholas@2634: }
nicholas@2634: foreach($n1 as $page) {
nicholas@2634: $ref = $page->getAttribute("ref");
nicholas@2639: if (!empty($ref)) {
nicholas@2639: $pn2 = findNodeByAttribute($n2, "ref", $ref);
n@2966: if ($pn2 != 0) {
nicholas@2639: $saved_root->removeChild($pn2);
nicholas@2639: $pn2 = 0;
nicholas@2639: }
nicholas@2639: if ($pn2 == 0) {
nicholas@2640: $pn1 = $doc->importNode($page, true);
nicholas@2639: $docRoot->appendChild($pn1);
nicholas@2639: }
nicholas@2634: }
nicholas@2634: }
nicholas@2634: }
nicholas@2634:
nicholas@2634: // Iterate through new doc
nicholas@2641: $wbytes = $doc->save($filename);
nicholas@2634:
nicholas@2634: // Return XML confirmation data
nicholas@2634: $xml = 'OK"'.$filename.'"';
nicholas@2634: echo $xml;
n@2997:
n@2997: if (!$update) {
nicholas@3120: unlink($saveLocation.'update-'.$saveFilenamePrefix.$file_key.".xml");
n@2997: }
nicholas@2538: ?>