<?php
// dasoertliche.de reverse lookup by ravage ( ravage {at} haxxors.com ) 12/2009
function getCIDData($cid) {
if ($cid == "") return 0;
if (!file_exists('./cid.sqlite')) createDB();
if (!is_writable('./cid.sqlite')) return 0;
$dbh = new PDO("sqlite:./cid.sqlite");
$sql = "SELECT * FROM contacts WHERE cid='$cid'";
foreach ($dbh->query($sql) as $row)
{
return $row;
$dbh = null;
}
return 0;
$dbh = null;
}
function createDB() {
$dbh = new PDO("sqlite:./cid.sqlite");
$count = $dbh->exec("CREATE TABLE contacts (cid TEXT, city TEXT, firstname TEXT, id INTEGER PRIMARY KEY, lastname TEXT, pc TEXT, street TEXT);");
$dbh = null;
}
function putCIDData($cid,$firstname,$lastname,$street,$pc,$city) {
if ($cid == "") return 0;
$dbh = new PDO("sqlite:./cid.sqlite");
$count = $dbh->exec("INSERT INTO contacts(cid, firstname, lastname, street, pc, city) VALUES ('$cid', '$firstname','$lastname','$street', '$pc', '$city')");
$dbh = null;
if ($count == 1) return 0;
return 1;
}
function remoteQuery($cid) {
# reverse Lookup via www.dasoertliche.de
$host = 'www3.dasoertliche.de';
$path = '/Controller';
$rdata = 'form_name=search_inv&la=de&page=5&context=4&action=43&buc=&ph=' . rawurlencode ($cid);
$fp = fsockopen ($host, 80);
if (!$fp)
$name = "Fehler in Abfrage $errstr ( $errno)";
else {
unset ($result);
fputs ($fp, 'POST ' . $path . ' HTTP/1.1' . "\r\n");
fputs ($fp, 'User-Agent: Mozilla/4.0 Compatible' . "\r\n");
fputs ($fp, 'Host: ' . $host . "\r\n");
fputs ($fp, 'Content-type: application/x-www-form-urlencoded' . "\r\n");
fputs ($fp, 'Content-length: ' . strlen ($rdata) . "\r\n");
fputs ($fp, 'Connection: close' . "\r\n\r\n");
fputs ($fp, $rdata);
while (!feof($fp))
$result .= fgets($fp, 128);
fclose ($fp);
}
// Name
$reg = '/na: "(.+)",/';
if (preg_match ($reg, $result, $matches)) {
$name = utf8_encode(trim(str_replace (' ', ' ', $matches[1])));
$name = explode(" ",$name);
}
// Street
unset($matches);
$reg = '/st: "(.+)",/';
if (preg_match ($reg, $result, $matches)) {
$st = utf8_encode(str_replace ('%20', ' ', $matches[1]));
}
// City
unset($matches);
$reg = '/ci: "(.+)",/';
if (preg_match ($reg, $result, $matches)) {
$ci = utf8_encode(str_replace ('%20', ' ', $matches[1]));
}
// PC
unset($matches);
$reg = '/pc: "(.+)",/';
if (preg_match ($reg, $result, $matches)) {
$pc = utf8_encode(str_replace ('%20', ' ', $matches[1]));
}
$data['firstname'] = $name[1];
$data['lastname'] = $name[0];
$data['street'] = $st;
$data['pc'] = $pc;
$data['city'] = $ci;
$dbh = null;
return $data;
}
$cid = $_GET['cid'];
if ($cid == "") {
die();
} else {
if (substr($cid,0,1) == ' ') $cid = substr($cid,1);
if (substr($cid,0,2) == '0049') $cid = '0'.substr($cid,2);
if (substr($cid,0,2) == '49') $cid = '0'.substr($cid,2);
// check for cache
$data = getCIDData($cid);
if ($data == 0) {
$data = remoteQuery($cid);
if ($data['firstname'] != "")
putCIDData($cid,$data['firstname'],$data['lastname'],$data['street'],$data['pc'],$data['city']);
}
// Output
$output = $data['firstname'].' '.$data['lastname'];
if ($data['street'] != "") $output .= ",".$data['street'];
if ($data['city'] != "") $output .= ",".$data['city'];
echo $output;
}
?>