diff --git a/oertliche.php b/oertliche.php new file mode 100644 index 0000000..0799643 --- /dev/null +++ b/oertliche.php @@ -0,0 +1,83 @@ +'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','ct'=>'City' +function splitAddress($address) { + $res = array('st' => '', 'nr' => '', 'ct' => ''); + if (preg_match('/^((.+) (\d+.*),)?(\d+) (.+)$/', $address, $m)) { + $res['st'] = $m[2]; + $res['nr'] = str_replace(' ', '', $m[3]); + $res['ct'] = strtok($m[5], ','); # Remove suburb from name + } + return $res; +} + +// Output Gigaset error response +function printError($id) { + echo '' . $id . ''; +} + +// Output Gigaset phonebook search response +function printResponse($entry) { + echo ''; + } + // Person found: + else { + echo 'total="1" first="1" last="1">'; + echo '' . $entry['ln'] . ''; + if (array_key_exists('fn', $entry) && !empty($entry['fn'])) + echo '' . $entry['fn'] . ''; + if (array_key_exists('st', $entry) && !empty($entry['st'])) + echo '' . $entry['st'] . ''; + if (array_key_exists('nr', $entry) && !empty($entry['nr'])) + echo '' . $entry['nr'] . ''; + if (array_key_exists('ct', $entry) && !empty($entry['ct'])) + echo '' . $entry['ct'] . ''; + if (array_key_exists('hm', $entry) && !empty($entry['hm'])) + echo '' . $entry['hm'] . ''; + echo ''; + } +} + +// 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; + $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 "\r\n\r\n"; +if (isset($_GET['hm']) && is_numeric($_GET['hm'])) { + $caller = lookupCaller($_GET['hm']); + if (is_array($caller)) + printResponse($caller); + else + printError(6); # Service not available +} +else { + printResponse(NULL); +} + +?>