rswrapper /rs-data.php

Language PHP Lines 450
MD5 Hash 60eb50328e2045e74107e6586bff2ddc
Repository https://bitbucket.org/wragge/rswrapper View Raw File
  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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
include 'XmlDomConstruct.php';
function getPage($url) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
	curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
	curl_setopt($ch, CURLOPT_URL, $url);
	$html= curl_exec($ch);
	if (!$html) {
		echo "cURL error number:" .curl_errno($ch);
		echo "cURL error:" . curl_error($ch);
		exit;
	}
	curl_close($ch);
	return $html;
}
//Deal with the query parameters
$id = $_GET['id'];
$format = $_GET['f'];
//Check the id to see what type of item this is
if ($id){
	if (preg_match('/^[0-9]+$/', $id) == 1) {
		$type="item";
    } else if (preg_match('/^CP[\s\+]{1}\d+$/i', $id) == 1) {
		$type = "person";
	} else if (preg_match('/^CA[\s\+]{1}\d+$/i', $id) == 1) {
		$type = "agency";
	} else if (preg_match('/^[A-Za-z0-9\/]+$/', $id) == 1) {
		$type = "series";
	} else {
		unset($id);
	}
}
if ($id) {
	//Get item details
	if ($type == 'item') {
		$url = "http://www.naa.gov.au/cgi-bin/Search?O=I&Number=$id";
		$result = getPage($url);
		if ($result) {
			preg_match('/<B>Title<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$title = htmlspecialchars($matches[1]);
			preg_match('/<B>Control symbol<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$control = $matches[1];
			preg_match('/HREF="SeriesDetail.asp\?M=0\&B=(.+?)"\>/', $result, $matches);
			$series = $matches[1];
			preg_match('/<B>Contents date range<\/B><BR>\r\n\t(.+?)<\/TD>/', $result, $matches);
			$cdates = preg_split('/ - /', $matches[1]);
			$contentsDates[] = array("start"=>$cdates[0], "end"=>$cdates[1]);
			preg_match('/<B>Location<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$location = $matches[1];
			preg_match('/<B>Access status<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$access = strtolower($matches[1]);
			preg_match('/<B>Extent<\/B><BR>(.*?)<\/TD>/', $result, $matches);
			$extent = $matches[1];
			//If it's been digitised find out how many pages are in the file
			if (strpos($result, "View digital copy")) {
				$digitised = "yes";
				$imgUrl = "http://naa12.naa.gov.au/scripts/imagine.asp?B=$id&I=1&SE=1";
				$imgResult = getPage($imgUrl, $ch);
				preg_match('/NAME="C" VALUE="(\d+)" ID="Hidden3"/', $imgResult, $matches);
				$pages = $matches[1];
			} else {
				$digitised = "no";
				$pages = "unknown";
			}
			$itemDetails = array("item"=>array(
				"title"=>$title,
				"series"=>$series,
				"controlSymbol"=>$control,
				"contentsDates"=>$contentsDates,
				"location"=>$location,
				"accessStatus"=>$access,
				"extent"=>$extent,
				"isDigitised"=>$digitised,
				"numberOfPages"=>$pages));
		} else {
			$error = 'Could not contact server';
		}
	} else if ($type == 'series') {
		$url = "http://www.naa.gov.au/cgi-bin/Search?Number=$id";
		$result = getPage($url);
		if ($result) {
			preg_match('/<B>Title<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$title = htmlspecialchars($matches[1]);
			preg_match('/<B>Accumulation dates<\/B><BR>\r\n\t(.+?)<\/TD>/', $result, $matches);
			$adates = preg_split('/ - /', $matches[1]);
			$accumulationDates[] = array("start"=>$adates[0], "end"=>$adates[1]);
			preg_match('/<B>Contents dates<\/B><BR>\r\n\t(.+?)<\/TD>/', $result, $matches);
			$cdates = preg_split('/ - /', $matches[1]);
			$contentsDates[] = array("start"=>$cdates[0], "end"=>$cdates[1]);
			preg_match('/<B>Predominant form<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$form = strtolower($matches[1]);
			preg_match('/<B>Quantity and location<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$quantities = preg_split('/<br>/', $matches[1]);
			foreach ($quantities as $q) {
				preg_match('/(.+?) held in (.+)/', $q, $matches);
				$quantity = $matches[1];
				$location = $matches[2];
				$holdings[] = array("location"=>$location, "quantity"=>$quantity);
			}
			//Get details of recording agencies
			preg_match('/<B>Agency \/ person recording<\/B><BR><TABLE width=100\% >(.*?)<\/TABLE>/', $result, $matches);
			preg_match_all('/<td width=30\% VALIGN=TOP >(.+?)<\/td><td width=70\% ><A HREF="AgencyDetail.asp\?M=1\&B=CA\+(\d+)">CA \d+<\/a>, (.+?)<\/td>/', $matches[1], $agencies, PREG_SET_ORDER);
			foreach ($agencies as $agency) {
				preg_match('/(.+?) - (.+)/', $agency[1], $matches);
				$start = $matches[1];
				$end = $matches [2];
				$agId = "CA $agency[2]";
				$recAgencies[] = array("start"=>$start, "end"=>$end, "agencyId"=>"CA $agency[2]", "agencyTitle"=>$agency[3]);
			}
			//Get details of controlling agencies
			preg_match('/<B>Agency \/ person controlling<\/B><BR><TABLE width=100\% >(.*?)<\/TABLE>/', $result, $matches);
			preg_match_all('/<td width=30\% VALIGN=TOP >(.+?)<\/td><td width=70\% ><A HREF="AgencyDetail.asp\?M=1\&B=CA\+(\d+)">CA \d+<\/a>, (.+?)<\/td>/', $matches[1], $agencies, PREG_SET_ORDER);
			foreach ($agencies as $agency) {
				preg_match('/(.+?) - (.+)/', $agency[1], $matches);
				$start = $matches[1];
				$end = $matches [2];
				$agId = "CA $agency[2]";
				$conAgencies[] = array("start"=>$start, "end"=>$end, "agencyId"=>"CA $agency[2]", "agencyTitle"=>$agency[3]);
			}
			//Follow the 'Find items in this series' link
			$itemsUrl = "http://recordsearch.naa.gov.au/scripts/SearchOF.asp?DP=2&Q=SER_SERIES_NO=QT" . $id . "QT";
			$items = getPage($itemsUrl);
			if ($items) {
				//Get the number of items in this series described on RS
				preg_match('/<TD>Items\r\n\t<\/td><td>(.*?)<\/td>/', $items, $matches);
				$numItems = $matches[1];
				//Trim the asterix of the 'More than 20.000' message
				$numItems = str_replace('*', '', $numItems);
			}
			//open the series links page and harvest the details
			$linksUrl = "http://recordsearch.naa.gov.au/scripts/SeriesDetail.asp?M=2&B=" . $id;
			$links = getPage($linksUrl);
			if ($links) {
				//Get rid of line breaks and tabs
				$links = preg_replace('/\r/', '', $links);
				$links = preg_replace('/\n/', '', $links);
				$links = preg_replace('/\t/', '', $links);
				//Previous series
				preg_match('/<B>Previous series<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/', $links, $matches);
				preg_match_all('/<TR><td width=15% VALIGN=TOP >(.*?)<\/td><td width=85% ><A HREF="SeriesDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/', $matches[1], $pseries, PREG_SET_ORDER);
				foreach ($pseries as $ps) {
					if (strpos($ps[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $s[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $ps[1];
						$end = "";
					}
					$prevSeries[] = array("start"=>$start, "end"=>$end, "seriesId"=>$ps[2], "seriesTitle"=>$ps[3]);
				}
				//Subsequent series
				preg_match('/<B>Subsequent series<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=15% VALIGN=TOP >(.*?)<\/td><td width=85% ><A HREF="SeriesDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $sseries, PREG_SET_ORDER);
				foreach ($sseries as $ss) {
					if (strpos($ss[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $s[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $ss[1];
						$end = "";
					}
					$subSeries[] = array("start"=>$start, "end"=>$end, "seriesId"=>$ss[2], "seriesTitle"=>$ss[3]);
				}
				//Controlling series
				preg_match('/<B>Controlling series<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=30% VALIGN=TOP >(.*?)<\/td><td width=70% ><A HREF="SeriesDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $cseries, PREG_SET_ORDER);
				foreach ($cseries as $cs) {
					if (strpos($cs[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $cs[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $cs[1];
						$end = "";
					}
					$conSeries[] = array("start"=>$start, "end"=>$end, "seriesId"=>$cs[2], "seriesTitle"=>$cs[3]);
				}
				//Controlled series
				preg_match('/<B>Controlled series<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=30% VALIGN=TOP >(.*?)<\/td><td width=70% ><A HREF="SeriesDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $conseries, PREG_SET_ORDER);
				foreach ($conseries as $cons) {
					if (strpos($cons[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $cons[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $cons[1];
						$end = "";
					}
					$contSeries[] = array("start"=>$start, "end"=>$end, "seriesId"=>$cons[2], "seriesTitle"=>$cons[3]);
				}
				//Related series
				preg_match('/<B>Related series<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=30% VALIGN=TOP >(.*?)<\/td><td width=70% ><A HREF="SeriesDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $rseries, PREG_SET_ORDER);
				foreach ($rseries as $rs) {
					if (strpos($rs[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $rs[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $rs[1];
						$end = "";
					}
					$relSeries[] = array("start"=>$start, "end"=>$end, "seriesId"=>$rs[2], "seriesTitle"=>$rs[3]);
				}
			}
			$itemDetails = array("series"=>array(
				"title"=>$title,
				"accumulationDates"=>$accumulationDates,
				"contentsDates"=>$contentsDates,
				"form"=>$form,
				"holdings"=>array("holding"=>$holdings),
				"numberOfItems"=>$numItems,
				"recordingAgencies"=>array("agency"=>$recAgencies),
				"controllingAgencies"=>array("agency"=>$conAgencies)));
				if (count($prevSeries) != 0) {
					$itemDetails["series"]["previousSeries"] = array("series"=>$prevSeries);
				}
				if (count($subSeries) != 0) {
					$itemDetails["series"]["subsequentSeries"] = array("series"=>$subSeries);
				}
				if (count($conSeries) != 0) {
					$itemDetails["series"]["controllingSeries"] = array("series"=>$conSeries);
				}
				if (count($contSeries) != 0) {
					$itemDetails["series"]["controlledSeries"] = array("series"=>$contSeries);
				}
				if (count($relSeries) != 0) {
					$itemDetails["series"]["relatedSeries"] = array("series"=>$relSeries);
				}
		} else {
			$error = 'Could not contact server';
		}
	} else if ($type == "agency") {
		$url = "http://www.naa.gov.au/cgi-bin/Search?Number=" . urlencode($id);
		$result = getPage($url);
		if ($result) {
			preg_match('/<B>Title<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$title = $matches[1];
			preg_match('/<B>Date range<\/B><BR>\r\n\t(.+?)<\/TD>/', $result, $matches);
			$dateRange = preg_split('/ - /',$matches[1]);
			$startDate = $dateRange[0];
			$endDate = $dateRange[1];
			preg_match('/<B>Status code<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$statusCodes = preg_split('/; /', $matches[1]);
			preg_match('/<B>Location<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$location = $matches[1];
			preg_match('/<B>Function<\/B><BR>(.+?)<\/TD>/', $result, $matches);
			$funcs = preg_split('/<br>/',$matches[1]);
			foreach ($funcs as $func) {
				$funcParts = preg_split('/: /', $func);
				$funcDates = preg_split('/ - /', $funcParts[0]);
				$funcName = strtolower(strip_tags($funcParts[1]));
				$functions[] = array("functionStart"=>$funcDates[0], "functionEnd"=>$funcDates[1], "functionName"=>$funcName);
			}
			//Follow the 'Find series recorded by this agency' link
			$itemsUrl = "http://recordsearch.naa.gov.au/scripts/searchOF.asp?DP=3&Q=SERIES%5FNO+IN+%28SELECT+SER%5FSERIES%5FNO+FROM+NAAS%2EDBO%2ELINKAGES+WHERE+LINK%5FTYPE%3DQTPRSQT+AND+PRV%5FREG%5FNO%3DQT" . urlencode($id) . "QT%29";
			$items = getPage($itemsUrl);
			if ($items) {
				//Get the number of items in this series described on RS
				preg_match('/<TD>Series\r\n\t<\/td><td>(.*?)<\/td>/', $items, $matches);
				$numItems = $matches[1];
				//Trim the asterix of the 'More than 20.000' message
				$numItems = str_replace('*', '', $numItems);
			}
			$linksUrl = "http://recordsearch.naa.gov.au/scripts/AgencyDetail.asp?M=2&B=" . urlencode($id);
			$links = getPage($linksUrl);
			if ($links) {
				//Get rid of line breaks and tabs
				$links = preg_replace('/\r/', '', $links);
				$links = preg_replace('/\n/', '', $links);
				$links = preg_replace('/\t/', '', $links);
				//Organisation controlling
				preg_match('/<B>Organisation controlling<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=30% VALIGN=TOP >(.*?)<\/td><td width=70% ><A HREF="OrganisationDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $orgs, PREG_SET_ORDER);
				foreach ($orgs as $org) {
					if (strpos($org[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $org[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $org[1];
						$end = "";
					}
					$conOrgs[] = array("start"=>$start, "end"=>$end, "orgId"=>$org[2], "orgTitle"=>$org[3]);
				}
				//Superior agency
				preg_match('/<B>Superior agency<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=30% VALIGN=TOP >(.*?)<\/td><td width=70% ><A HREF="AgencyDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $sagencies, PREG_SET_ORDER);
				foreach ($sagencies as $sa) {
					if (strpos($sa[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $sa[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $sa[1];
						$end = "";
					}
					$supAgencies[] = array("start"=>$start, "end"=>$end, "agencyId"=>$sa[2], "agencyTitle"=>$sa[3]);
				}
				//Controlled agency
				preg_match('/<B>Controlled agency<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=30% VALIGN=TOP >(.*?)<\/td><td width=70% ><A HREF="AgencyDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $cagencies, PREG_SET_ORDER);
				foreach ($cagencies as $ca) {
					if (strpos($ca[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $ca[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $ca[1];
						$end = "";
					}
					$conAgencies[] = array("start"=>$start, "end"=>$end, "agencyId"=>$ca[2], "agencyTitle"=>$ca[3]);
				}
				//Previous agency
				preg_match('/<B>Previous agency<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=15% VALIGN=TOP >(.*?)<\/td><td width=85% ><A HREF="AgencyDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $pagencies, PREG_SET_ORDER);
				foreach ($pagencies as $pa) {
					if (strpos($pa[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $pa[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $pa[1];
						$end = "";
					}
					$prevAgencies[] = array("start"=>$start, "end"=>$end, "agencyId"=>$pa[2], "agencyTitle"=>$pa[3]);
				}
				//Subsequent agency
				preg_match('/<B>Subsequent agency<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=15% VALIGN=TOP >(.*?)<\/td><td width=85% ><A HREF="AgencyDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $nagencies, PREG_SET_ORDER);
				foreach ($nagencies as $na) {
					if (strpos($na[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $na[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $na[1];
						$end = "";
					}
					$nextAgencies[] = array("start"=>$start, "end"=>$end, "agencyId"=>$na[2], "agencyTitle"=>$na[3]);
				}
				//Person associated
				preg_match('/<B>Persons associated with agency<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=30% VALIGN=TOP >(.*?)<\/td><td width=70% ><A HREF="PersonDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $persons, PREG_SET_ORDER);
				foreach ($persons as $person) {
					if (strpos($person[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $person[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $person[1];
						$end = "";
					}
					$assocPersons[] = array("start"=>$start, "end"=>$end, "personId"=>$person[2], "personName"=>$person[3]);
				}
			}
			$itemDetails = array("agency"=>array(
				"title"=>$title,
				"startDate"=>$startDate,
				"endDate"=>$endDate,
				"statusCodes"=>array("statusCode"=>$statusCodes),
				"location"=>$location,
				"functions"=>array("function"=>$functions),
				"numberOfSeries"=>$numItems));
			if (count($conOrgs) != 0) {
				$itemDetails["agency"]["controllingOrganisations"] = array("organisation"=>$conOrgs);
			}
			if (count($supAgencies) != 0) {
				$itemDetails["agency"]["superiorAgencies"] = array("agency"=>$supAgencies);
			}
			if (count($conAgencies) != 0) {
				$itemDetails["agency"]["controlledAgencies"] = array("agency"=>$conAgencies);
			}
			if (count($prevAgencies) != 0) {
				$itemDetails["agency"]["previousAgencies"] = array("agency"=>$prevAgencies);
			}
			if (count($nextAgencies) != 0) {
				$itemDetails["agency"]["subsequentAgencies"] = array("agency"=>$nextAgencies);
			}
			if (count($assocPersons) != 0) {
				$itemDetails["agency"]["associatedPersons"] = array("person"=>$assocPersons);
			}
		} else {
			$error = 'Could not contact server';
		}
	} else if ($type == "person") {
		$url = "http://www.naa.gov.au/cgi-bin/Search?Number=" . urlencode($id);
		$result = getPage($url);
		if ($result) {
			preg_match('/<B>Name<\/B><BR>(.+?)<\/TD>/i', $result, $matches);
			$name = $matches[1];
			preg_match('/<B>Date range<\/B><BR>\r\n\t(.+?)<\/TD>/', $result, $matches);
			$dateRange = preg_split('/ - /',$matches[1]);
			$birth = $dateRange[0];
			$death = $dateRange[1];

			$linksUrl = "http://recordsearch.naa.gov.au/scripts/PersonDetail.asp?M=2&B=" . urlencode($id);
			$links = getPage($linksUrl);
			if ($links) {
				//Get rid of line breaks and tabs
				$links = preg_replace('/\r/', '', $links);
				$links = preg_replace('/\n/', '', $links);
				$links = preg_replace('/\t/', '', $links);
				preg_match('/<B>Agencies associated with person<\/B><BR><TABLE width=100% >(.+?)<\/TABLE>/i', $links, $matches);
				preg_match_all('/<TR><td width=30% VALIGN=TOP >(.*?)<\/td><td width=70% ><A HREF="AgencyDetail.asp\?M=1\&B=.+?">(.+?)<\/a>, (.+?)<\/td><\/TR>/i', $matches[1], $agencies, PREG_SET_ORDER);
				foreach ($agencies as $a) {
					if (strpos($a[1], "-") !== false) {
						preg_match('/(.+?) - (.*)/', $a[1], $matches);
						$start = $matches[1];
						$end = $matches [2];
					} else {
						$start = $a[1];
						$end = "";
					}
					$assocAgencies[] = array("start"=>$start, "end"=>$end, "agencyId"=>$a[2], "agencyTitle"=>$a[3]);
				}
			}
			$itemDetails = array("person"=>array(
				"name"=>$name,
				"birthDate"=>$birth,
				"deathDate"=>$death));
			if (count($assocAgencies) != 0) {
				$itemDetails["person"]["associatedAgencies"] = array("agency"=>$assocAgencies);
			}
		}
	}
} else {
	$error = 'Invalid id';
}

if (!$error) {
	if ($format == "json") {
		echo json_encode($itemDetails);
	} else {
		header ("content-type: text/xml");
		$dom = new XmlDomConstruct('1.0', 'utf-8');
		$dom->fromMixed($itemDetails);
		echo $dom->saveXML();
	}
} else {
	echo $error;
}
?>
Back to Top