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@2754
|
31 } else {
|
nicholas@2755
|
32 $saveFilenamePrefix = "save-";
|
nicholas@2634
|
33 }
|
nicholas@2634
|
34 $postText = file_get_contents('php://input');
|
nicholas@2634
|
35 $file_key = $_GET['key'];
|
nicholas@2754
|
36 $filename = '../saves/'.$saveFilenamePrefix.$file_key.".xml";
|
nicholas@2634
|
37
|
nicholas@2634
|
38 if (!file_exists($filename)) {
|
nicholas@2634
|
39 die('<response state="error"><message>Could not find save</message></response>');
|
nicholas@2634
|
40 }
|
nicholas@2634
|
41
|
nicholas@2634
|
42 // Open the save
|
nicholas@2634
|
43 $saved_doc = new DOMDocument;
|
nicholas@2634
|
44 $saved_doc->preserveWhiteSpace = false;
|
nicholas@2634
|
45 $saved_doc->formatOutput = true;
|
nicholas@2634
|
46 $saved_doc->loadXML(file_get_contents($filename, FILE_TEXT));
|
nicholas@2634
|
47 $saved_root = $saved_doc->documentElement;
|
nicholas@2634
|
48
|
nicholas@2634
|
49 // Construct the XML document into a new tree
|
nicholas@2634
|
50 $doc = new DOMDocument;
|
nicholas@2634
|
51 $doc->loadXML($postText);
|
nicholas@2634
|
52 $docRoot = $doc->documentElement;
|
nicholas@2634
|
53
|
nicholas@2634
|
54 // Add the relavent nodes:
|
nicholas@2634
|
55 // <datetime>
|
nicholas@2634
|
56 $n1 = $docRoot->getElementsByTagName("datetime");
|
nicholas@2634
|
57 $n2 = $saved_root->getElementsByTagName("datetime");
|
nicholas@2636
|
58 if ($n1->length > 0 && $n2->length == 0) {
|
nicholas@2634
|
59 $n1 = $doc->importNode($n1->item(0), true);
|
nicholas@2634
|
60 $docRoot->appendChild($n1);
|
nicholas@2634
|
61 }
|
nicholas@2634
|
62
|
nicholas@2634
|
63 //<navigator>
|
nicholas@2634
|
64 $n1 = $docRoot->getElementsByTagName("navigator");
|
nicholas@2634
|
65 $n2 = $saved_root->getElementsByTagName("navigator");
|
nicholas@2636
|
66 if ($n1->length > 0 && $n2->length == 0) {
|
nicholas@2634
|
67 $n1 = $doc->importNode($n1->item(0), true);
|
nicholas@2634
|
68 $docRoot->appendChild($n1);
|
nicholas@2634
|
69 }
|
nicholas@2634
|
70
|
nicholas@2634
|
71 //<survey location="pre">
|
nicholas@2634
|
72 $n1 = $docRoot->getElementsByTagName("survey");
|
nicholas@2634
|
73 $n2 = $saved_root->getElementsByTagName("survey");
|
nicholas@2634
|
74 if ($n1->length > 0) {
|
nicholas@2634
|
75 // Check if in save
|
nicholas@2634
|
76 if ($n2->length == 0) {
|
nicholas@2634
|
77 $n2 = 0;
|
nicholas@2634
|
78 }
|
nicholas@2634
|
79 $sn1 = findNodeByAttribute($n1, "location", "pre");
|
nicholas@2634
|
80 $sn2 = findNodeByAttribute($n2, "location", "pre");
|
nicholas@2634
|
81 if ($sn1 != 0) {
|
nicholas@2634
|
82 if ($sn2 != 0 && $sn2.getAttribute("state") != "complete") {
|
nicholas@2634
|
83 $saved_root->removeChild($sn2);
|
nicholas@2634
|
84 $sn2 = 0;
|
nicholas@2634
|
85 }
|
nicholas@2634
|
86 if ($sn2 == 0) {
|
nicholas@2634
|
87 $sn1 = $doc->importNode($sn1->item(0), true);
|
nicholas@2634
|
88 $docRoot->appendChild($sn1);
|
nicholas@2634
|
89 }
|
nicholas@2634
|
90 }
|
nicholas@2634
|
91
|
nicholas@2634
|
92 $sn1 = findNodeByAttribute($n1, "location", "post");
|
nicholas@2634
|
93 $sn2 = findNodeByAttribute($n2, "location", "post");
|
nicholas@2634
|
94 if ($sn1 != 0) {
|
nicholas@2634
|
95 if ($sn2 != 0 && $sn2.getAttribute("state") != "complete") {
|
nicholas@2634
|
96 $saved_root->removeChild($sn2);
|
nicholas@2634
|
97 $sn2 = 0;
|
nicholas@2634
|
98 }
|
nicholas@2634
|
99 if ($sn2 == 0) {
|
nicholas@2634
|
100 $sn1 = $doc->importNode($sn1->item(0), true);
|
nicholas@2634
|
101 $docRoot->appendChild($sn1);
|
nicholas@2634
|
102 }
|
nicholas@2634
|
103 }
|
nicholas@2634
|
104 }
|
nicholas@2634
|
105
|
nicholas@2634
|
106 //<page ref="">
|
nicholas@2634
|
107 $n1 = $docRoot->getElementsByTagName("page");
|
nicholas@2634
|
108 $n2 = $saved_root->getElementsByTagName("page");
|
nicholas@2634
|
109 if ($n1->length > 0) {
|
nicholas@2634
|
110 if ($n2->length == 0) {
|
nicholas@2634
|
111 $n2 = 0;
|
nicholas@2634
|
112 }
|
nicholas@2634
|
113 foreach($n1 as $page) {
|
nicholas@2634
|
114 $ref = $page->getAttribute("ref");
|
nicholas@2639
|
115 if (!empty($ref)) {
|
nicholas@2639
|
116 $pn2 = findNodeByAttribute($n2, "ref", $ref);
|
nicholas@2639
|
117 if ($pn2 != 0 && $pn2.getAttribute("state") != "complete") {
|
nicholas@2639
|
118 $saved_root->removeChild($pn2);
|
nicholas@2639
|
119 $pn2 = 0;
|
nicholas@2639
|
120 }
|
nicholas@2639
|
121 if ($pn2 == 0) {
|
nicholas@2640
|
122 $pn1 = $doc->importNode($page, true);
|
nicholas@2639
|
123 $docRoot->appendChild($pn1);
|
nicholas@2639
|
124 }
|
nicholas@2634
|
125 }
|
nicholas@2634
|
126 }
|
nicholas@2634
|
127 }
|
nicholas@2634
|
128
|
nicholas@2634
|
129 // Iterate through new doc
|
nicholas@2641
|
130 $wbytes = $doc->save($filename);
|
nicholas@2634
|
131
|
nicholas@2634
|
132 // Return XML confirmation data
|
nicholas@2634
|
133 $xml = '<response state="OK"><message>OK</message><file bytes="'.$wbytes.'">"'.$filename.'"</file></response>';
|
nicholas@2634
|
134 echo $xml;
|
nicholas@2538
|
135 ?>
|