-
Notifications
You must be signed in to change notification settings - Fork 3
/
resolver.php
366 lines (323 loc) · 10.4 KB
/
resolver.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
<html>
<head>
<style>
.loader{
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: -1;
background: url('/front/images/loading.gif')
50% 50% no-repeat rgb(249,249,249);
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
pre {
white-space: pre-wrap;
}
</style>
</head>
<body>
</body>
</html>
<?php
require_once 'config/MysqlArkConf.php';
use Noid\Config\MysqlArkConf;
if (strpos($_SERVER['REQUEST_URI'], "/ark:/") === 0 || strpos($_SERVER['REQUEST_URI'], "/ark:") === 0) {
// processing the Ark URL
$params = str_replace("ark:/", "", $_GET['q']);
$parts = array_filter(explode("/", $params));
$parts_count = count($parts);
// get Ark ID
if ($parts_count > 1)
$arkid = $parts[0] . '/'. $parts[1];
else
$arkid = $parts[0];
if (strpos($arkid, "ark:") === 0) {
$arkid = str_replace("ark:", "", $arkid);
$naan = explode("/",$arkid)[0];
}
// get all database Ark related
$arkdbs = showArkDatabases();
// only proceed if already have ark_core db
if (is_array($arkdbs) && count($arkdbs) > 0) {
$url = "/404.php";
// loop through database and find matching one with prefix
foreach ($arkdbs as $db) {
$qualifier = "";
// if there is no arks ID, only NAAN, ie. /ark:61220/
if ($parts_count == 1) {
print(htmlspecialchars_decode(getNAA($db)));
return;
}
// if ark ID found, look for URL fields first.
// if there is Qualifier, look up for the qualifier, ignore the ark id
else if ($parts_count > 2) {
// establish qualifier from Ark URL
$total = count($parts);
$qualifier = "/";
for ($i = 2; $i < $total; $i++) {
$qualifier .= $parts[$i];
if ($i != $total - 1) {
$qualifier .= "/";
}
if (strpos($parts[$i], ".") !== false) {
// Qualifier has variants
$qualifier_parts = explode(".", $qualifier);
$hiarachy = $qualifier_parts[0];
array_shift($qualifier_parts);
$qualifier = $hiarachy . "\t" . "." . implode(".",$qualifier_parts);
}
}
// looking up for URL
$result = lookup($db, $arkid, $qualifier, "URL");
// Look up for Where?
if (empty($result)) {
$result = lookup($db, $arkid, $qualifier, "WHERE?");
}
// if found the results,
if (!empty($result)) {
$url = $result;
break;
}
}
// if there is only Arks ID in URL, just lookup based on the arkID
else if ($parts_count == 2) {
// do the lookup for URL
$result = lookup($db, $arkid, $qualifier, "URL");
// Look up for Where?
if (empty($result)) {
$result = lookup($db, $arkid, $qualifier, "WHERE?");
}
// if found the results,
if (!empty($result)) {
// found URL field bound associated with the ark id
$url = $result;
break;
}
}
else {
}
}
// New: Add a check for ? or ?? and the end of Ark URL
if ( (substr($_SERVER['REQUEST_URI'], -5) === "?info")
|| substr_compare($_SERVER['REQUEST_URI'], "?", -1) === 0 ) {
// if the Ark URLs ends with '?'
$medata = getMetdata($db, $arkid, $qualifier);
print_pre($medata);
}
if ( substr_compare($_SERVER['REQUEST_URI'], "??", -2) === 0
|| (substr($_SERVER['REQUEST_URI'], -5) === "?info") ) {
$medata = getMetdata($db, $arkid, $qualifier,"??");
print_pre($medata);
}
if (substr($_SERVER['REQUEST_URI'], -5) !== "?info"
&& substr_compare($_SERVER['REQUEST_URI'], "?", -1) !== 0 ) {
// add a counter here
increase_reidrection($db, $arkid, $qualifier);
// redirect
//print("Redirecting to " . $url);
header("Location: $url");
}
}
} else {
print "invalid argument";
}
/**
* Print content wrapped around <pre></pre>
*/
function print_pre($content) {
print("<pre>");
print($content);
print("<pre/>");
}
/**
* Get full metadata
*/
function getMetdata($db, $ark_id, $qualifier, $type=null)
{
$link = mysqli_connect(MysqlArkConf::$mysql_host, MysqlArkConf::$mysql_user, MysqlArkConf::$mysql_passwd, MysqlArkConf::$mysql_dbname);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
if (isset($type) && $type == "??") { // for ??
$label = "erc-support:";
if (isset($qualifier) && !empty($qualifier)) {
$where = 'WHERE _key regexp "(^|[[:space:]])'.$ark_id.'([[:space:]])'.$qualifier.'([[:space:]])[^\.].*\\\\?\\\\?.*"';
}
else {
// Query data with Ark ID and field name with ?? attached to it without qualifer
$where = 'WHERE _key regexp "(^|[[:space:]])'.$ark_id. '([[:space:]])[^/].*\\\\?\\\\?.*"';
}
}
else { // for ?
$label = "erc:";
if (isset($qualifier) && !empty($qualifier)) {
$where = 'WHERE _key regexp "(^|[[:space:]])'.$ark_id. '([[:space:]])'.$qualifier.'([[:space:]])[^\.]" AND _key NOT REGEXP ".*\\\\?\\\\?.*"';
}
else {
// Query data with Ark ID and field name with ? attached to it without qualifer.
$where = 'WHERE _key regexp "(^|[[:space:]])'.$ark_id. '([[:space:]])[^/]" AND _key NOT REGEXP ".*\\\\?\\\\?.*"';
}
}
if ($query = mysqli_query($link, "SELECT * FROM `$db` ". $where)) {
if (!mysqli_query($link, "SET @a:='this will not work'")) {
printf("Error: %s\n", mysqli_error($query));
}
$results = $query->fetch_all();
if (count($results) > 0) {
$medata = "<pre>";
$medata .= $label. "\n";
foreach($results as $pair) {
$field = trim(str_replace($ark_id, " ", $pair[0])) ;
$field = trim(str_replace("?", "", $field));
if (!in_array($field, [':/c', ":/h", "REDIRECT", ""])) {
if (preg_match("/\t/", $field)) {
$parts_field= explode("\t", $field);
$count_parts_field = count($parts_field);
$field = $parts_field[$count_parts_field-1];
}
$medata .= ucwords(strtolower($field)) . ": " . str_replace("_", " ", $pair[1]) . "\n";
}
}
$medata .= "</pre>";
return $medata;
}
$query->close();
}
mysqli_close($link);
return false;
}
/**
* Counting redirection
*/
function increase_reidrection($db, $ark_id, $qualifier) {
// TODO UPDATE REDIRECTION COUNT HERE
$link = mysqli_connect(MysqlArkConf::$mysql_host, MysqlArkConf::$mysql_user, MysqlArkConf::$mysql_passwd, MysqlArkConf::$mysql_dbname);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
// get existed redirection count.
$count = lookup($db, $ark_id, $qualifier, "REDIRECT");
if ($count == false) {
$count = 1;
// do insert
if (empty($qualifier)) {
$query = 'INSERT INTO `'. $db .'` (_key, _value) VALUES("'.$ark_id. " REDIRECT". '", '.$count .')';
}
else {
$query = 'INSERT INTO `'. $db .'` (_key, _value) VALUES("'.$ark_id. " $qualifier REDIRECT". '", '.$count .')';
}
}
else {
// increase the counter
$count++;
// do update
if (empty($qualifier)) {
$where = 'WHERE _key LIKE "'.$ark_id.'%REDIRECT"';
}
else {
$where = 'WHERE _key LIKE "'.$ark_id.'%'.$qualifier.'%REDIRECT"';
}
$query = "UPDATE `$db` SET _value = $count ". $where;
}
if (!mysqli_query($link, $query)) {
error_log(print_r("$query is failed to run", TRUE), 0);
}
mysqli_close($link);
}
/**
* Get Org registered info
*/
function lookup($db, $ark_id, $qualifier,$field = "")
{
$link = mysqli_connect(MysqlArkConf::$mysql_host, MysqlArkConf::$mysql_user, MysqlArkConf::$mysql_passwd, MysqlArkConf::$mysql_dbname);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
if (empty($qualifier)) {
$where = "where _key REGEXP '^".$ark_id . "\t".$field."'";
}
else {
$where = "where _key REGEXP '^".$ark_id . "\t" . $qualifier . "\t".$field."'";
}
if ($query = mysqli_query($link, "SELECT * FROM `$db` ". $where)) {
if (!mysqli_query($link, "SET @a:='this will not work'")) {
printf("Error: %s\n", mysqli_error($query));
}
$results = $query->fetch_all();
if (count($results) > 0) {
return $results[0][1];
}
$query->close();
}
mysqli_close($link);
return false;
}
/**
* Get NAA from of arksdb
*/
function getNAA($db) {
$link = mysqli_connect(MysqlArkConf::$mysql_host, MysqlArkConf::$mysql_user, MysqlArkConf::$mysql_passwd, MysqlArkConf::$mysql_dbname);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$where = "where _key = ':/naa'";
if ($query = mysqli_query($link, "SELECT * FROM `$db` ". $where)) {
if (!mysqli_query($link, "SET @a:='this will not work'")) {
printf("Error: %s\n", mysqli_error($query));
}
$results = $query->fetch_all();
if (count($results) > 0) {
return $results[0][1];
}
$query->close();
}
mysqli_close($link);
return false;
}
/**
* list all Arrk database
* @param string $name
*/
function showArkDatabases()
{
$link = mysqli_connect(MysqlArkConf::$mysql_host, MysqlArkConf::$mysql_user, MysqlArkConf::$mysql_passwd, MysqlArkConf::$mysql_dbname);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
if ($query = mysqli_query($link, "SHOW TABLES")) {
if (!mysqli_query($link, "SET @a:='this will not work'")) {
printf("Error: %s\n", mysqli_error($query));
}
$results = $query->fetch_all();
$arkdbs = [];
foreach ($results as $db) {
// It starts with 'http'
if (!in_array($db[0], ['system', 'user'])) {
array_push($arkdbs, $db[0]);
}
}
$query->close();
}
mysqli_close($link);
return $arkdbs;
}