This repository has been archived by the owner on Oct 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oertliche.php
86 lines (78 loc) · 2.96 KB
/
oertliche.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
<?php
// Remove and leading/trailing whitespace
function tidyString($s) {
return trim(str_replace("\xc2\xa0", '', $s));
}
// Split 'Lastname [Firstname1 [Firstname2]]' into key-value pairs 'fn'=>'Firstname1 Firstname2','ln'=>'Lastname'
function splitName($name) {
$res = array('fn' => '', 'ln' => '');
$a = explode(' ', $name, 2);
$res['ln'] = $a[0];
if (count($a) > 1)
$res['fn'] = $a[1];
return $res;
}
// Split '[Street 1 a,]12345 City' into key-value pairs 'st'=>'Street','nr'=>'1a','zc'=>'12345','ct'=>'City'
function splitAddress($address) {
$res = array('st' => '', 'nr' => '', 'zc' => '', 'ct' => '');
if (preg_match('/^((.+) (\d+.*),)?(\d+) (.+)$/', $address, $m)) {
$res['st'] = $m[2];
$res['nr'] = str_replace(' ', '', $m[3]);
$res['zc'] = $m[4];
$res['ct'] = strtok($m[5], ','); # Remove suburb from name
}
return $res;
}
// Output Gigaset error response
function printError($id) {
echo '<error repsonse="get_list" type="pb"><errorid>' . $id . '</errorid></error>';
}
// Output Gigaset phonebook search response
function printResponse($entry) {
echo '<list response="get_list" type="pb" ';
// No result:
if (!is_array($entry) || !array_key_exists('ln', $entry) || empty($entry['ln'])) {
echo 'notfound="hm" total="0"/>';
}
// Person found:
else {
echo 'total="1" first="1" last="1"><entry>';
echo '<ln>' . $entry['ln'] . '</ln>';
if (array_key_exists('fn', $entry) && !empty($entry['fn']))
echo '<fn>' . $entry['fn'] . '</fn>';
if (array_key_exists('st', $entry) && !empty($entry['st']))
echo '<st>' . $entry['st'] . '</st>';
if (array_key_exists('nr', $entry) && !empty($entry['nr']))
echo '<nr>' . $entry['nr'] . '</nr>';
if (array_key_exists('zc', $entry) && !empty($entry['zc']))
echo '<zc>' . $entry['zc'] . '</zc>';
if (array_key_exists('ct', $entry) && !empty($entry['ct']))
echo '<ct>' . $entry['ct'] . '</ct>';
if (array_key_exists('hm', $entry) && !empty($entry['hm']))
echo '<hm>' . $entry['hm'] . '</hm>';
echo '</entry></list>';
}
}
// Reverse phone number search on DasOertliche.de
function lookupCaller($number) {
$dom = new DOMDocument();
if (!@$dom->loadHTML(file_get_contents('http://www.dasoertliche.de/Controller?form_name=search_inv&ph=' . $number)))
return; # HTML file unparseable
$xp = new DomXPath($dom);
$name = tidyString($xp->evaluate('string(//div[@id="entry_1"]//a[normalize-space(@class)="name"]/span[1])'));
$addr = tidyString($xp->evaluate('string(//div[@id="entry_1"]//address[1])'));
return array_merge(splitName($name), splitAddress($addr), array('hm' => $number));
}
header('Content-Type: text/xml; charset=utf-8');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n";
if (isset($_GET['hm']) && preg_match('/^\d+$/', $_GET['hm'])) {
$caller = lookupCaller($_GET['hm']);
if (is_array($caller))
printResponse($caller);
else
printError(6); # Service not available
}
else {
printResponse(NULL);
}
?>