-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeolabel.php
252 lines (199 loc) · 7.79 KB
/
geolabel.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
<?php
$is_cors_request = array_key_exists("cors", $_GET);
if ($is_cors_request) {
// enable cross-origin resource sharing
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
}
// 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);
$feedback_api_url = $config["feedback_endpoint"] . '/collections/?format=xml&target_code=%1$s&target_codespace=%2$s';
// cross-domain request made by the schema plugin to call the GEO label service
if ($is_cors_request) {
if (isset($_POST["metadata"])) {
$metadata = trim($_POST["metadata"]);
$size = trim($_POST["size"]);
$target_code = trim($_POST["code"]);
$target_codespace = trim($_POST["codespace"]);
// data to POST to the GEO label API
$post_vars = array(
"metadata" => $metadata
);
if ($target_code !== "" && $target_codespace !== "") {
// get feedback collection to upload
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf($feedback_api_url, $target_code, $target_codespace));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post_vars["feedback"] = curl_exec($ch);
curl_close($ch);
if (curl_errno($ch)) {
// discard any output from the request if there was an error
unset($post_vars["feedback"]);
}
}
if (is_numeric($size) && $size > 0) {
$post_vars["size"] = $size;
}
$svg = call_geolabel_service($config["geolabel_endpoint"], $post_vars, "POST");
// job done, send a success response
send_response(array(
"status" => "success",
"data" => array(
"label_svg" => $svg
)
));
}
}
// usual request made by the tutorial
else {
// validate user input
$validation_error = "";
$geonetwork_id = trim($_POST["geonetwork_id"]);
$target_code = trim($_POST["target_code"]);
$target_codespace = trim($_POST["target_codespace"]);
if ($geonetwork_id === "") {
$validation_error = "Please enter the GeoNetwork ID of the published document";
}
else if (!is_numeric($geonetwork_id)) {
$validation_error = "Invalid ID provided: IDs must be numeric";
}
if ($validation_error !== "") {
send_response(array(
"status" => "error",
"message" => $validation_error
));
}
// fetch the XML document from GeoNetwork
$metadata_url = $config["geonetwork_baseURL"] . "xml_geoviqua?id=" . urlencode($geonetwork_id) . "&styleSheet=xml_iso19139.geoviqua.xsl";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $metadata_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
// something went wrong
if (curl_errno($ch)) {
send_response(array(
"status" => "error",
"message" => curl_error($ch)
));
}
else {
// check that the output is an XML document
$xml = new DOMDocument();
$previous_errors = libxml_use_internal_errors(true);
$valid = $xml->loadXML($output);
libxml_clear_errors();
libxml_use_internal_errors($previous_errors);
if ($valid === false || strpos($output, "<h2>Privileges Error</h2>") !== false) {
send_response(array(
"status" => "error",
"message" => 'The <a href="' . $metadata_url . '" target="_blank">requested metadata document</a> for ID <strong>' . $geonetwork_id . '</strong> could not be found or is malformed'
));
}
else {
// data to POST to the GEO label API
$post_vars = array(
"metadata" => $metadata_url
);
// submit a feedback URL if code & codespace is provided
if ($target_code !== "" && $target_codespace !== "") {
$post_vars["feedback"] = sprintf($feedback_api_url, $target_code, $target_codespace);
}
$svg = call_geolabel_service($config["geolabel_endpoint"], $post_vars);
// job done, send a success response
send_response(array(
"status" => "success",
"data" => array(
"id" => $geonetwork_id,
"label_svg" => $svg
)
));
}
}
}
}
else {
send_response(array(
"status" => "error",
"message" => "Could not load configuration file $config_file"
));
}
function call_geolabel_service($url, $data, $method = "GET") {
// build the curl request
$ch = curl_init();
switch ($method) {
case "GET":
curl_setopt($ch, CURLOPT_URL, $url . "?" . http_build_query($data));
break;
case "POST":
// create a tmp file containing the XML for upload
$metadata_file = tempnam(sys_get_temp_dir(), "geolabel_POST_");
file_put_contents($metadata_file, $data["metadata"]);
$data["metadata"] = "@".$metadata_file;
// attach the feedback collection as well
if (array_key_exists("feedback", $data)) {
$feedback_file = tempnam(sys_get_temp_dir(), "geolabel_POST_");
file_put_contents($feedback_file, $data["feedback"]);
$data["feedback"] = "@".$feedback_file;
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
}
// request a GEO label
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// remove the tmp files
if ($method == "POST") {
unlink($metadata_file);
unlink($feedback_file);
}
// something went wrong
if (curl_errno($ch)) {
send_response(array(
"status" => "error",
"message" => curl_error($ch)
));
}
else {
// check that the output is valid XML
$svg = new DOMDocument();
$previous_errors = libxml_use_internal_errors(true);
$valid = $svg->loadXML($output);
libxml_clear_errors();
libxml_use_internal_errors($previous_errors);
if ($valid === false) {
send_response(array(
"status" => "error",
"message" => "Unable to parse response (" . htmlentities($output) . ")"
));
}
else {
return $output;
}
}
}
function send_response($response) {
if ($response["status"] === "error") {
header("HTTP/1.1 500 Internal Server Error");
}
// not an ajax request
if(empty($_SERVER["HTTP_X_REQUESTED_WITH"]) || strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) !== "xmlhttprequest") {
if ($response["status"] === "error") {
die($response["message"]);
}
else {
die($response["data"]["label_svg"]);
}
}
else {
header("Content-type: application/json");
die(json_encode($response));
}
}
?>