-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish.php
400 lines (320 loc) · 13.9 KB
/
publish.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
session_start();
// error tracking
$error = array();
// name and path of the configuration file for this script
$config_file = dirname(__FILE__) . "/app/config/config.ini";
// check that the configuration file is readable
if (file_exists($config_file) && is_readable($config_file)) {
$config = parse_ini_file($config_file);
// metadata document to be published
$gvq_doc = new DOMDocument();
if ($_FILES["publish-metadata"]["size"] > 0 && $_FILES["publish-metadata"]["error"] === UPLOAD_ERR_OK) {
// read the contents of the uploaded file
$xml = file_get_contents($_FILES["publish-metadata"]["tmp_name"]);
// simple check to see if it's a well-formed XML document, suppressing warnings
$previous_errors = libxml_use_internal_errors(true);
$valid = $gvq_doc->loadXML($xml);
libxml_clear_errors();
libxml_use_internal_errors($previous_errors);
if ($valid === false) {
$error = array(
"status" => "error",
"message" => "The XML document could not be parsed, please check that the XML is well-formed."
);
}
else {
// begin the upload process
if (authenticateWithGeoNetwork($config["editor_username"], $config["editor_password"])) {
// track the number of iterations to prevent an infinite loop
$upload_attempts = 0;
// get the original UUID
$xpath = new DOMXPath($gvq_doc);
$uuid_query = $xpath->query('//gmd:fileIdentifier/gco:CharacterString');
$original_uuid = $uuid_query->item(0)->nodeValue;
do {
// try to upload the document
$response = uploadToGeoNetwork($gvq_doc->saveXML());
$error = errorCheck($response["body"]);
$uuid_violation = strpos($error['message'], 'Unique index or primary key violation: "CONSTRAINT_INDEX_1 ON PUBLIC.METADATA(UUID)";');
if ($uuid_violation !== false) {
// there was a UUID violation so we have to generate a new one
// http://stackoverflow.com/a/2040279/1983684
mt_srand(crc32(serialize(microtime(true))));
$new_uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
// update the UUID for the next upload attempt
$uuid_query->item(0)->nodeValue = $new_uuid;
}
$upload_attempts++;
}
while (($uuid_violation !== false) && ($upload_attempts < 5));
if (empty($error) &&
authenticateWithGeoNetwork($config["admin_username"], $config["admin_password"])) {
// parse the reponse for the metadata's catalog id and uuid
$parsed = new SimpleXMLElement($response["body"]);
// create a privileges request
$xml = new DOMDocument("1.0");
$xml->formatOutput = true;
$root_node = $xml->createElement("request");
$xml->appendChild($root_node);
// id element
$id_node = $xml->createElement("id");
$id_node_value = $xml->createTextNode($parsed->id);
$id_node->appendChild($id_node_value);
// append privilege elements to root
$root_node->appendChild($xml->createElement("_1_0")); // group 1 (all) can view
$root_node->appendChild($xml->createElement("_1_1")); // group 1 (all) can download
$root_node->appendChild($xml->createElement("_" . $config["editor_group_id"] . "_2")); // editor's user group can edit
// append all other child elements to root
$root_node->appendChild($id_node);
// save XML as string
$xml_request = $xml->saveXML();
// send the request to GeoNetwork
$response = sendXMLRequest("xml.metadata.privileges", $xml_request);
$error = errorCheck($response["body"]);
if (empty($error)) {
// parse the reponse for the metadata's catalog id
$parsed = new SimpleXMLElement($response["body"]);
// job done, send a success response
$json = array(
"status" => "success",
"data" => array(
"geonetwork" => array(
"username" => $config["editor_username"],
"password" => $config["editor_password"],
"url" => $config["geonetwork_baseURL"],
"metadata_id" => (int) $parsed->id
)
)
);
die(json_encode($json));
}
}
}
}
}
else {
$path_parts = pathinfo($_FILES['publish-metadata']['name']);
$filename = $path_parts['filename'];
$message = "";
// something went wrong
switch($_FILES["publish-metadata"]["error"]) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$message = "$filename must be smaller than " . ini_get("upload_max_filesize") . " in size.";
break;
case UPLOAD_ERR_PARTIAL:
$message = "$filename was only partially uploaded.";
break;
case UPLOAD_ERR_NO_FILE:
$message = "No file was selected for upload.";
break;
case UPLOAD_ERR_NO_TMP_DIR:
case UPLOAD_ERR_CANT_WRITE:
case UPLOAD_ERR_EXTENSION:
$message = "Unable to write $filename to disk.";
break;
default:
$message = "Please select your transformed metadata document.";
break;
}
$error = array(
"status" => "error",
"message" => $message
);
}
}
else {
$error = array(
"status" => "error",
"message" => "Could not load configuration file $config_file"
);
}
// execution was not successful
if (!empty($error)) {
header("HTTP/1.1 500 Internal Server Error");
die(json_encode($error));
}
/**
* Creates and sends a login request to the configured GeoNetwork instance,
* using the provided username and password.
* If a successful authentication has previously occurred during the session
* a logout request will first be made to invalidate that token.
*/
function authenticateWithGeoNetwork($username, $password) {
// :(
global $config, $error;
if (isset($_SESSION["auth_cookie"])) {
// log out first
$xml = new DOMDocument("1.0");
$xml->formatOutput = true;
$root_node = $xml->createElement("request");
$xml->appendChild($root_node);
$xml_request = $xml->saveXML();
$response = sendXMLRequest("xml.user.logout", $xml_request);
$error = errorCheck($response["body"]);
if (empty($error)) {
unset($_SESSION["auth_cookie"]);
}
else {
return false;
}
}
// create a login request
$xml = new DOMDocument("1.0");
$xml->formatOutput = true;
$root_node = $xml->createElement("request");
$xml->appendChild($root_node);
// username element
$username_node = $xml->createElement("username");
$username_node_value = $xml->createTextNode($config["admin_username"]);
$username_node->appendChild($username_node_value);
// password element
$password_node = $xml->createElement("password");
$password_node_value = $xml->createTextNode($config["admin_password"]);
$password_node->appendChild($password_node_value);
// append child elements to root
$root_node->appendChild($username_node);
$root_node->appendChild($password_node);
// save XML as string
$xml_request = $xml->saveXML();
// send the request to GeoNetwork
$response = sendXMLRequest("xml.user.login", $xml_request);
$error = errorCheck($response["body"]);
if (empty($error)) {
// we're authenticated, keep for future requests
$_SESSION["auth_cookie"] = $response["header"]["Set-Cookie"];
}
else {
return false;
}
return true;
}
/**
* Helper method to upload an XML document to GeoNetwork.
*/
function uploadToGeoNetwork($xml_doc) {
// create a metadata insert request
$xml = new DOMDocument("1.0");
$xml->formatOutput = true;
$root_node = $xml->createElement("request");
$xml->appendChild($root_node);
// group element
$group_node = $xml->createElement("group");
$group_node_value = $xml->createTextNode("1"); // all
$group_node->appendChild($group_node_value);
// category element
$category_node = $xml->createElement("category");
$category_node_value = $xml->createTextNode("_none_");
$category_node->appendChild($category_node_value);
// stylesheet element
$stylesheet_node = $xml->createElement("stylesheet");
$stylesheet_node_value = $xml->createTextNode("_none_");
$stylesheet_node->appendChild($stylesheet_node_value);
// data element
$data_node = $xml->createElement("data");
$data_node_value = $xml->createCDATASection($xml_doc); // the uploaded document embedded as CDATA
$data_node->appendChild($data_node_value);
// append child elements to root
$root_node->appendChild($group_node);
$root_node->appendChild($category_node);
$root_node->appendChild($stylesheet_node);
$root_node->appendChild($data_node);
// save XML as string
$xml_request = $xml->saveXML();
// send the request to GeoNetwork
$response = sendXMLRequest("xml.metadata.insert", $xml_request);
return $response;
}
/**
* Helper method to send an XML request to GeoNetwork. The base URL is defined
* in config.ini and the required XML service is then passed as a parameter,
* forming a valid endpoint for the targeted GeoNetwork instance.
* Some requests require authentication, in which case a cookie should be provided
* via a session variable.
*/
function sendXMLRequest($xml_service, $xml_request) {
// :(
global $config;
// start off with an empty response
$response = array("header" => false, "body" => false);
// configure cURL to request the desired XML service
$ch = curl_init($config["geonetwork_baseURL"] . $xml_service);
// POST with correct content type & length, returns HTTP header and response as a string
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/xml",
"Content-Length: " . strlen($xml_request))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_request);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// provide authentication if we have it
if (isset($_SESSION["auth_cookie"])) {
curl_setopt($ch, CURLOPT_COOKIE, $_SESSION["auth_cookie"]);
}
// send the request
$output = curl_exec($ch);
if (curl_errno($ch)) {
// emulate a GeoNetwork error response and provide cURL's error
$response["body"] = '<error id="curl"><message>' . curl_error($ch) . '</message></error>';
}
else {
// separate the head from the body
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header_text = substr($output, 0, $header_size);
$response["body"] = substr($output, $header_size);
// explode the header into an array
foreach (explode("\r\n", $header_text) as $i => $line) {
if ($i === 0) {
$response["header"]["HTTP_Code"] = $line;
}
else {
list($key, $val) = explode(": ", $line);
$response["header"][$key] = $val;
}
}
}
return $response;
}
/**
* Checks the response body for a standard GeoNetwork error structure [1].
* If the response is an error, encode the relevant information as a JSON response and terminate the script.
*
* [1] http://www.geonetwork-opensource.org/manuals/2.8.0/eng/developer/xml_services/services_calling.html
*/
function errorCheck($response) {
$xml = new DOMDocument("1.0");
$xml->loadXML($response);
$errors = $xml->getElementsByTagName("error");
$return_error = array();
if (!empty($errors)) {
foreach ($errors as $error) {
$error_message = $error->getElementsByTagName("message")->item(0)->nodeValue;
$return_error = array(
"status" => "error",
"message" => $error_message,
"data" => array(
"id" => $error->getAttribute('id'),
"request" => $error->getElementsByTagName("object")->item(0)->nodeValue
)
);
}
}
return $return_error;
}
?>