-
Notifications
You must be signed in to change notification settings - Fork 1
/
oaipmh_target.php
227 lines (192 loc) · 8.07 KB
/
oaipmh_target.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
<?php
/**
* Handles incoming OAIPMH requests for the News module as per the OAIPMH specification.
*
* External metadata harvesters submit OAIPMH queries against this file for processing. The OAIPMH
* specification outlines a standard vocabulary for requests and responses are defined by the spec's
* XML schema, handled by an Archive object in the optional Sprockets module. If you don't want to
* enable the OAIPMH functionality of this module you can safely remove this file. But it is
* probably easier just to turn off OAIPMH functionality in the Sprockets module (option 1: don't
* create an Archive object for this module, option 2: each archive object has a kill switch).
* XML responses are assembled in a buffer and then flushed in a gzip compressed stream.
*
* For more information visit the Open Archives Initiative, http://www.openarchives.org
*
* @copyright Copyright Isengard.biz 2010, distributed under GNU GPL V2 or any later version
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License (GPL)
* @since 1.0
* @author Madfish (Simon Wilkinson) <simon@isengard.biz>
* @package News
* @version $Id$
*/
include_once 'header.php';
$xoopsOption['template_main'] = 'news_article.html';
include_once ICMS_ROOT_PATH . '/header.php';
// initialise
$dirty_vars = $allowed_vars = $clean_vars = array();
$verb = $identifier = $identification = $metadataPrefix = $from = $until = $set
= $resumptionToken = $identification = $getRecord = $listMetadataFormats = $listSets
= $listRecords = $badVerb = '';
$cursor = 0; // will be overriden if there is a valid $resumptionToken
//////////////////////////////////////////////
////////// BEGIN INPUT SANITISATION //////////
//////////////////////////////////////////////
// whitelist acceptable variables
$allowed_vars = array('verb' => 'plaintext', 'identifier' => 'plaintext',
'metadataPrefix' => 'plaintext', 'from' => 'plaintext', 'until' => 'plaintext',
'set' => 'plaintext', 'resumptionToken' => 'plaintext', 'cursor' => 'int');
// OAIPMH spec requires support for both GET and POST requests
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$dirty_vars = $_GET;
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
$dirty_vars = $_POST;
}
/*
* If there is a resumption token, restore state from that INSTEAD of from GET/POST variables.
* This will work so long as *all* the required state information is serialised in the
* resumption token. State is set in the Sprockets module /class/archive/lookup_records()
*/
if (!empty($dirty_vars['resumptionToken']) && ($dirty_vars['verb'] == 'ListIdentifiers'
|| $dirty_vars['verb'] == 'ListRecords' || $dirty_vars['verb'] == 'ListSets')) {
if(get_magic_quotes_gpc()) {
$dirty_vars = unserialize(stripslashes(urldecode($dirty_vars['resumptionToken'])));
} else {
$dirty_vars = unserialize(urldecode($dirty_vars['resumptionToken']));
}
$dirty_vars['resumptionToken'] = TRUE;
}
// channel whitelisted variables through the validator function
$clean_vars = news_validate($dirty_vars, $allowed_vars);
// extract the sanitised variables
extract($clean_vars);
//////////////////////////////////////////////////////////
////////// END INPUT SANITISATION ////////////////////////
//////////////////////////////////////////////////////////
// set up the relevant archive and handlers relevant to target object
$newsModule = icms_getModuleInfo(basename(dirname(__FILE__)));
$sprocketsModule = icms_getModuleInfo('sprockets');
if (icms_get_module_status("sprockets"))
{
$news_article_handler = icms_getModuleHandler('article', $newsModule->getVar('dirname'), 'news');
$sprockets_archive_handler = icms_getModuleHandler('archive', $sprocketsModule->getVar('dirname'),
'sprockets');
$criteria = new icms_db_criteria_Compo();
$criteria->add(new icms_db_criteria_Item('module_id', $newsModule->getVar('mid')));
$archive_array = $sprockets_archive_handler->getObjects($criteria);
$archiveObj = array_shift($archive_array);
// if no archive object has been created, issue a warning
if (!$archiveObj) {
echo _CO_NEWS_ARCHIVE_MUST_CREATE;
} else {
// check if this archive is enabled before processing any OAIPMH requests
if ($archiveObj->getVar('enable_archive', 'e') == 1 ) {
// IMPORTANT: need to disable the logger because it breaks XML responses
icms::$logger->disableLogger();
////////////////////////////////////////////////////////
////////// BEGIN OPEN ARCHIVES INITIATIVE API //////////
////////////////////////////////////////////////////////
switch ($verb) {
// retrieve basic information about the archive
case "Identify":
$identify_response = $archiveObj->identify();
$identification = simplexml_load_string($identify_response);
ob_start("ob_gzhandler");
header('Content-Type: text/xml');
print $identification->asXML();
ob_end_flush();
exit();
break;
// retrieve one record specified by a unique identifier
case "GetRecord":
$getRecord = simplexml_load_string($archiveObj->getRecord($news_article_handler,
$identifier, $metadataPrefix));
ob_start("ob_gzhandler");
header('Content-Type: text/xml');
print $getRecord->asXML();
ob_end_flush();
exit();
break;
// retrieves record headers rather than full records, time range can be specified
case "ListIdentifiers":
if (!empty($from)) {
if (strlen($from) == 10) {
$from .= 'T00:00:00Z'; // if granularity is day level, add time to avoid breaking code
}
}
if (!empty($until)) {
if (strlen($until) == 10) {
$until .= 'T23:59:59Z'; // if granularity is day level, add time to avoid breaking code
}
}
$listIdentifiers = simplexml_load_string($archiveObj->listIdentifiers($news_article_handler,
$metadataPrefix, $from, $until, $set, $resumptionToken, $cursor));
ob_start("ob_gzhandler");
header('Content-Type: text/xml');
print $listIdentifiers->asXML();
ob_end_flush();
exit();
break;
// list the metadata formats available from this archive
case "ListMetadataFormats":
$listMetadataFormats = simplexml_load_string($archiveObj->listMetadataFormats($news_article_handler,
$identifier));
ob_start("ob_gzhandler");
header('Content-Type: text/xml');
print $listMetadataFormats->asXML();
ob_end_flush();
exit();
break;
// retrieve multiple records from the repository, time range can be specified
case "ListRecords":
if (!empty($from)) {
if (strlen($from) == 10) {
$from .= 'T00:00:00Z'; // if granularity is day level, add time to avoid breaking code
}
}
if (!empty($until)) {
if (strlen($until) == 10) {
$until .= 'T23:59:59Z'; // if granularity is day level, add time to avoid breaking code
}
}
$listRecords = simplexml_load_string($archiveObj->listRecords($news_article_handler,
$metadataPrefix, $from, $until, $set, $resumptionToken, $cursor));
ob_start("ob_gzhandler");
header('Content-Type: text/xml');
print $listRecords->asXML();
ob_end_flush();
exit();
break;
// retrieve the set structure of this archive (sets are not implemented)
case "ListSets":
$listSets = simplexml_load_string($archiveObj->listSets($resumptionToken, $cursor));
ob_start("ob_gzhandler");
header('Content-Type: text/xml');
print $listSets->asXML();
ob_end_flush();
exit();
break;
// if we don't know what's going on, throw badVerb error, request is illegal
default:
$badVerb = simplexml_load_string($archiveObj->BadVerb());
ob_start("ob_gzhandler");
header('Content-Type: text/xml');
print $badVerb->asXML();
ob_end_flush();
exit();
break;
}
//////////////////////////////////////////////////////
////////// END OPEN ARCHIVES INITIATIVE API //////////
//////////////////////////////////////////////////////
} else {
// archive is disabled, can it
exit;
}
}
$icmsTpl->assign('archive_module_home', news_getModuleName(TRUE, TRUE));
}
else // Exit if Sprockets module is not installed and active
{
exit;
}
include_once 'footer.php';