-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoaipmh_target.php
362 lines (314 loc) · 12.6 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
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
<?php
/**
* Handles incoming OAIPMH requests for a Sprockets-compatible 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) <[email protected]>
* @package Library
* @version $Id$
*/
/**
* Basic validation and sanitation of user input but NOT range checking (calling method should do)
*
* This can be expanded for all different types of input: email, URL, filenames, media/mimetypes
*
* @param array $input_var Array of user input, gathered from $_GET, $_POST or $_SESSION
* @param array $valid_vars Array of valid variables and data type (integer, boolean, string,)
* @return array Array of validated and sanitized variables
*/
function validate($input_var, $valid_vars) {
$clean_var = array();
foreach ($valid_vars as $key => $type) {
if (empty($input_var[$key])) {
$input_var[$key] = NULL;
continue;
}
switch ($type) {
case 'int':
case 'integer':
$clean_var[$key] = $dirty_int = $clean_int = 0;
if (filter_var($input_var[$key], FILTER_VALIDATE_INT) == TRUE) {
$dirty_int = filter_var($input_var[$key], FILTER_SANITIZE_NUMBER_INT);
$clean_int = mysql_real_escape_string($dirty_int);
$clean_var[$key] = (int)$clean_int;
}
break;
case 'html': // Tolerate (but encode) html tags and entities
// Initialise
$dirty_html = $clean_html = $clean_var[$key] = '';
// Test for string
if (is_string($input_var[$key])) {
// Trim fore and aft whitespace
$dirty_html = trim($input_var[$key]);
// Keep html tags but encode entities and special characters
$dirty_html = filter_var($dirty_html, FILTER_SANITIZE_SPECIAL_CHARS);
$clean_html = mysql_real_escape_string($dirty_html);
$clean_var[$key] = (string)$clean_html;
}
break;
case 'plaintext': // Stripped down plaintext with tags removed
// Initialise
$dirty_text = $clean_text = $clean_var[$key] = '';
// Test for string (in PHP, what isn't??)
if (is_string($input_var[$key])) {
// Trim fore and aft whitespace
$dirty_text = trim($input_var[$key]);
// Strip html tags, encode quotes and special characters
$dirty_text = filter_var($dirty_text, FILTER_SANITIZE_STRING);
$clean_text = mysql_real_escape_string($dirty_text);
$clean_var[$key] = (string)$clean_text;
}
break;
case 'name':
// Initialise
$clean_var[$key] = $clean_name = $dirty_name = '';
$pattern = '^[a-zA-Z\-\']{1,60}$';
// Test for string + alphanumeric
if (is_string($input_var[$key]) && preg_match($pattern, $input_var[$key])) {
// Trim fore and aft whitespace
$dirty_name = trim($input_var[$key]);
// Strip html tags, encode quotes and special characters
$dirty_name = filter_var($dirty_name, FILTER_SANITIZE_STRING);
$clean_name = mysql_real_escape_string($dirty_name);
$clean_var[$key] = (string)$clean_name;
}
break;
case 'email':
$clean_var[$key] = $dirty_email = $clean_email = '';
if (filter_var($input_var[$key], FILTER_VALIDATE_EMAIL) == TRUE) {
$dirty_email = filter_var($input_var[$key], FILTER_SANITIZE_EMAIL);
$clean_email = mysql_real_escape_string($dirty_email);
$clean_var[$key] = (string)$clean_email;
}
break;
case 'url':
// Initialise
$clean_var[$key] = $dirty_url = $clean_url = '';
// Validate and sanitise URL
if (filter_var($input_var[$key], FILTER_VALIDATE_URL) == TRUE) {
$dirty_url = filter_var($input_var[$key], FILTER_SANITIZE_URL);
$clean_url = mysql_real_escape_string($dirty_url);
$clean_var[$key] = $clean_url;
}
case 'float':
case 'double':
case 'real':
// Initialise
$clean_var[$key] = $clean_float = 0;
// Validate and sanitise float
if (filter_var($input_var[$key], FILTER_VALIDATE_FLOAT) == TRUE) {
$clean_float = filter_var($input_var[$key], FILTER_SANITIZE_NUMBER_FLOAT);
$clean_var[$key] = (float)$clean_float;
}
break;
case 'bool':
case 'boolean':
$clean_var[$key] = FALSE;
if (is_bool($input_var[$key])) {
$clean_var[$key] = (bool) $input_var[$key];
}
break;
case 'binary':/* Only PHP6 - for now
if (is_string($input_var[$key])) {
$clean_var[$key] = htmlspecialchars(trim($input_var[$key]));
}*/
break;
case 'array': // Note: doesn't inspect array *contents*, each must be inspected separately
if (is_array($input_var[$key]) && !empty($input_var[$key])) {
$clean_var[$key] = $input_var[$key];
} else {
$clean_var[$key] = $input_var[$key];
}
break;
case 'object': // Note: doesn't inspect object *properties*, each must be inspected separately
if (is_object($input_var[$key])) {
$clean_var[$key] = (object)$input_var[$key];
}
break;
}
}
return $clean_var;
}
include_once 'header.php';
$xoopsOption['template_main'] = 'podcast_soundtrack.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 = 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
$module = icms_getModuleInfo(basename(dirname(__FILE__)));
$sprocketsModule = icms_getModuleInfo('sprockets');
if (icms_get_module_status("sprockets"))
{
$module_object_handler = icms_getModuleHandler('soundtrack', $module->getVar('dirname'),
$module->getVar('dirname'));
$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', $module->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_PODCAST_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($module_object_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($module_object_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($module_object_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($module_object_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, baby...
exit;
}
}
$icmsTpl->assign('archive_module_home', podcast_getModuleName(TRUE, TRUE));
}
else { // Exit if Sprockets module is not installed and active
exit;
}
include_once 'footer.php';