-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoaidp-util.php
332 lines (297 loc) · 9.51 KB
/
oaidp-util.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
<?php
/**
* \file
* \brief Utilities for the OAI Data Provider
*
* A collection of functions used.
*/
/** Dump information of a varible for debugging,
* only works when SHOW_QUERY_ERROR is true.
* \param $var_name Type: string Name of variable is being debugded
* \param $var Type: mix Any type of varibles used in PHP
* \see SHOW_QUERY_ERROR in oaidp-config.php
*/
function debug_var_dump($var_name, $var) {
if (SHOW_QUERY_ERROR) {
echo "Dumping \${$var_name}: \n";
var_dump($var)."\n";
}
}
/** Prints human-readable information about a variable for debugging,
* only works when SHOW_QUERY_ERROR is true.
* \param $var_name Type: string Name of variable is being debugded
* \param $var Type: mix Any type of varibles used in PHP
* \see SHOW_QUERY_ERROR in oaidp-config.php
*/
function debug_print_r($var_name, $var) {
if (SHOW_QUERY_ERROR) {
echo "Printing \${$var_name}: \n";
print_r($var)."\n";
}
}
/** Prints a message for debugging,
* only works when SHOW_QUERY_ERROR is true.
* PHP function print_r can be used to construct message with <i>return</i> parameter sets to true.
* \param $msg Type: string Message needs to be shown
* \see SHOW_QUERY_ERROR in oaidp-config.php
*/
function debug_message($msg) {
if (!SHOW_QUERY_ERROR) return;
echo $msg,"\n";
}
/** Check if provided correct arguments for a request.
*
* Only number of parameters is checked.
* metadataPrefix has to be checked before it is used.
* set has to be checked before it is used.
* resumptionToken has to be checked before it is used.
* from and until can easily checked here because no extra information
* is needed.
*/
function checkArgs($args, $checkList) {
// global $errors, $TOKEN_VALID, $METADATAFORMATS;
global $errors, $METADATAFORMATS;
// $verb = $args['verb'];
unset($args["verb"]);
debug_print_r('checkList',$checkList);
debug_print_r('args',$args);
// "verb" has been checked before, no further check is needed
if(isset($checkList['required'])) {
for($i = 0; $i < count($checkList["required"]); $i++) {
debug_message("Checking: par$i: ". $checkList['required'][$i] . " in ");
debug_var_dump("isset(\$args[\$checkList['required'][\$i]])",isset($args[$checkList['required'][$i]]));
// echo "key exists". array_key_exists($checkList["required"][$i],$args)."\n";
if(isset($args[$checkList['required'][$i]])==false) {
// echo "caught\n";
$errors[] = oai_error('missingArgument', $checkList["required"][$i]);
} else {
// if metadataPrefix is set, it is in required section
if(isset($args['metadataPrefix'])) {
$metadataPrefix = $args['metadataPrefix'];
// Check if the format is supported, it has enough infor (an array), last if a handle has been defined.
if (!array_key_exists ($metadataPrefix, $METADATAFORMATS) || !(is_array($METADATAFORMATS[$metadataPrefix])
|| !isset($METADATAFORMATS[$metadataPrefix]['myhandler']))) {
$errors[] = oai_error('cannotDisseminateFormat', 'metadataPrefix', $metadataPrefix);
}
}
unset($args[$checkList["required"][$i]]);
}
}
}
debug_message('Before return');
debug_print_r('errors',$errors);
if (!empty($errors)) return;
// check to see if there is unwanted
foreach($args as $key => $val) {
debug_message("checkArgs: $key");
if(!in_array($key, $checkList["ops"])) {
debug_message("Wrong\n".print_r($checkList['ops'],true));
$errors[] = oai_error('badArgument', $key, $val);
}
switch ($key) {
case 'from':
case 'until':
if(!checkDateFormat($val)) {
$errors[] = oai_error('badGranularity', $key, $val);
}
break;
case 'resumptionToken':
// only check for expairation
if((int)$val+TOKEN_VALID < time())
$errors[] = oai_error('badResumptionToken');
break;
}
}
}
/** Validates an identifier. The pattern is: '/^[-a-z\.0-9]+$/i' which means
* it accepts -, letters and numbers.
* Used only by function <B>oai_error</B> code idDoesNotExist.
* \param $url Type: string
*/
function is_valid_uri($url)
{
return((bool)preg_match('/^[-a-z\.0-9]+$/i', $url));
}
/** Validates attributes come with the query.
* It accepts letters, numbers, ':', '_', '.' and -.
* Here there are few more match patterns than is_valid_uri(): ':_'.
* \param $attrb Type: string
*/
function is_valid_attrb($attrb) {
return preg_match("/^[_a-zA-Z0-9\-\:\.]+$/",$attrb);
}
/** All datestamps used in this system are GMT even
* return value from database has no TZ information
*/
function formatDatestamp($datestamp)
{
return date("Y-m-d\TH:i:s\Z",strtotime($datestamp));
}
/** The database uses datastamp without time-zone information.
* It needs to clean all time-zone informaion from time string and reformat it
*/
function checkDateFormat($date) {
$date = str_replace(array("T","Z")," ",$date);
$time_val = strtotime($date);
if (SHOW_QUERY_ERROR) { echo "timeval: $time_val\n"; }
if(!$time_val) return false;
if(strstr($date,":")) {
return date("Y-m-d H:i:s",$time_val);
} else {
return date("Y-m-d",$time_val);
}
}
/** Retrieve all defined 'setSpec' from configuraiton of $SETS.
* It is used by ANDS_TPA::create_obj_node();
*/
function prepare_set_names() {
global $SETS;
$n = count($SETS);
$a = array_fill(0,$n,'');
for ($i = 0; $i <$n; $i++) {
$a[$i] = $SETS[$i]['setSpec'];
}
return $a;
}
/** Finish a request when there is an error: send back errors. */
function oai_exit()
{
// global $CONTENT_TYPE;
header(CONTENT_TYPE);
global $args,$errors,$compress;
$e = new ANDS_Error_XML($args,$errors);
if ($compress) {
ob_start('ob_gzhandler');
}
$e->display();
if ($compress) {
ob_end_flush();
}
exit();
}
// ResumToken section
/** Generate a string based on the current Unix timestamp in microseconds for creating resumToken file name. */
function get_token()
{
list($usec, $sec) = explode(" ", microtime());
return ((int)($usec*1000) + (int)($sec*1000));
}
/** Create a token file.
* It has three parts which is separated by '#': cursor, extension of query, metadataPrefix.
* Called by listrecords.php.
*/
function createResumToken($cursor, $extquery, $metadataPrefix) {
$token = get_token();
$fp = fopen (TOKEN_PREFIX.$token, 'w');
if($fp==false) {
exit("Cannot write. Writer permission needs to be changed.");
}
fputs($fp, "$cursor#");
fputs($fp, "$extquery#");
fputs($fp, "$metadataPrefix#");
fclose($fp);
return $token;
}
/** Read a saved ResumToken */
function readResumToken($resumptionToken) {
$rtVal = false;
$fp = fopen($resumptionToken, 'r');
if ($fp!=false) {
$filetext = fgets($fp, 255);
$textparts = explode('#', $filetext);
fclose($fp);
unlink ($resumptionToken);
$rtVal = array((int)$textparts[0], $textparts[1], $textparts[2]);
}
return $rtVal;
}
// Here are a couple of queries which might need to be adjusted to
// your needs. Normally, if you have correctly named the columns above,
// this does not need to be done.
/** this function should generate a query which will return
* all records
* the useless condition id_column = id_column is just there to ease
* further extensions to the query, please leave it as it is.
*/
function selectallQuery ($metadPrefix = "rif", $id = '')
{
global $SQL;
$query = "SELECT * FROM ".$SQL['table'] . " WHERE ".$SQL['metadataPrefix']." LIKE '%$metadPrefix%'";
if ($id != '') {
$query .= " AND ".$SQL['identifier']." ='$id'";
}
return $query;
}
/** this function will return metadataFormat of a record */
function idFormatQuery($id)
{
global $SQL;
return 'select '.$SQL['metadataPrefix'].' FROM '.$SQL['table']. " WHERE ".$SQL['identifier']." = '".$id."'";
}
/** this function will return identifier and datestamp for all records
* not very useful
*/
function idQuery ($metadPrefix = "rif", $id = '')
{
global $SQL;
if ($SQL['set'] != '') {
$query = 'select '.$SQL['identifier'].','.$SQL['datestamp'].','.$SQL['set'].' FROM '.$SQL['table']. " WHERE ".$SQL['metadataPrefix']." LIKE '%$metadPrefix%'";
} else {
$query = 'select '.$SQL['identifier'].','.$SQL['datestamp'].' FROM '.$SQL['table']. " WHERE ".$SQL['metadataPrefix']." LIKE '%$metadPrefix%'";
}
if ($id != '') {
$query .= " AND ".$SQL['identifier']." = '$id'";
}
return $query;
}
/** filter for until, appends to the end of SQL query */
function untilQuery($until)
{
global $SQL;
return ' AND '.$SQL['datestamp']." <= '$until'";
}
/** filter for from , appends to the end of SQL query */
function fromQuery($from)
{
global $SQL;
return ' AND '.$SQL['datestamp']." >= '$from'";
}
/** filter for sets, appends to the end of SQL query */
function setQuery($set)
{
global $SQL;
// strip off "class:" which is not saved in database
if(strstr($set,"class:")) $set = substr($set,6);
return ' AND '.$SQL['set']." LIKE '%$set%'";
}
/** for accurately to assess how many records satisfy conditions for all DBs */
function rowCount($metadataPrefix, $extQuery, $db) {
global $SQL;
$n = 0;
$sql = "SELECT COUNT(*) FROM ".$SQL['table'] . " WHERE ".$SQL['metadataPrefix']." LIKE '%$metadataPrefix%'" . $extQuery;
if ($res = $db->query($sql)) {
$n = $res->fetchColumn();
}
return $n;
}
/** A worker function for processing an error when a query was executed
* \param $query string, original query
* \param $e PDOException, the PDOException object
*/
function process_pdo_error($query, $e) {
echo $query.' was failed\n';
echo $e->getMessage();
}
/** When query return no result, throw an Exception of Not found.
* \param $db PDO
* \param $query string
* \return $res PDOStatement
*/
function exec_pdo_query($db, $query)
{
$res = $db->query($query);
if ($res===false) {
throw new Exception($query.":\nIt found nothing.\n");
} else return $res;
}
?>