-
Notifications
You must be signed in to change notification settings - Fork 3
/
output.php
66 lines (51 loc) · 1.93 KB
/
output.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
<?php
namespace oaiprovider\output;
use oaiprovider as oai;
use oaiprovider\xml as xml;
function escape($str) {
return htmlspecialchars($str);
}
function date($tstamp) {
return gmdate('Y-m-d', $tstamp);
}
function datetime($tstamp=null) {
if ($tstamp) {
return gmdate("Y-m-d\TH:i:s\Z", $tstamp);
} else {
return gmdate("Y-m-d\TH:i:s\Z", 0);
}
}
function _get_url() {
preg_match('@^(https?).*@', strtolower($_SERVER['SERVER_PROTOCOL']), $matches);
$prot = $matches[1];
return $prot . "://" . $_SERVER['HTTP_HOST'] . _stripquery($_SERVER['REQUEST_URI']);
}
function _stripquery($uri) {
return substr($uri, 0, strpos($uri, "?"));
}
function getBaseDocument($params) {
$doc = new \DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$root = $doc->createElementNS("http://www.openarchives.org/OAI/2.0/", "OAI-PMH");
$root->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
$root->setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", "http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd");
$doc->appendChild($root);
xml\appendElement($root, "", "responseDate", datetime(time()));
$request = xml\appendElement($root, "", "request", _get_url());
$p = oai\parseParams($params, array(oai\P_VERB, oai\P_FROM, oai\P_UNTIL, oai\P_SET, oai\P_IDENTIFIER, oai\P_METADATAPREFIX, oai\P_RESUMPTIONTOKEN));
foreach($p as $param => $val) {
$request->setAttribute($param, escape($val));
}
return $root;
}
function send_error($params, $code, $message) {
$root = getBaseDocument($params);
$error = xml\appendElement($root, "", "error", $message);
$error->setAttribute("code", escape($code));
header("Content-Type: text/xml; charset=utf-8");
send($root);
}
function send($doc) {
header("Content-Type: text/xml; charset=utf-8");
echo $doc->ownerDocument->saveXML();
}