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