<?php
/*
 * enumlookup. Lookup contact information in e164.org ENUM tree
 *
 * Copyright (C) 2006-2009, Michiel van Baak
 * Thanks to evilbunny for his example script
 *
 * Michiel van Baak 31708901086 in the e164.org
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */
/* the next line is just here for the example */
/* for real world you need to delete this */
$e164org "31708901086";
//echo "could not contact dns server";
//die();
/* reverse the number and put dots between every digit */
$arr $number "";
for (
$i strlen($e164org) - 1$i >= 0$i--) {
    
$num mysql_escape_string($e164org[$i]);
    if (
$number != "")
        
$number .= ".";
    
$number .= $num;
}
/* ask the dns for the records */
//$cmd = "dig @ns2.three-dimensional.net +short $number.e164.org naptr";
$cmd "dig +short $number.e164.org naptr";
//echo $cmd;
$result trim(`$cmd`);
$lines explode("\n"$result);
if (!
is_array($lines))
    
$lines = array();
$lines array_unique($lines);
$address_done 0;
foreach (
$lines as $line) {
    if (
$line == "")
        continue;

    while (
strstr($line"  "))
        
$line str_replace("  "" "$line);

    
/* parse the records into a usefull array */
    
$bits  explode(" "$line5);
    
$order $bits["0"];
    
$prio  $bits["1"];
    
$bits  explode("!"$bits["4"]);
    
$url   $bits["2"];
    
$arr[] = array("order" => $order"prio" => $prio"url" => $url);
}
unset(
$order);
unset(
$prio);
if (!
is_array($arr))
    
$arr = array();
/* sort the array according to order and priority fields in enum */
foreach ($arr as $key => $row) {
    
$tmporder[$key] = $row["order"];
    
$tmpprio[$key]  = $row["prio"];
}
if (
is_array($tmporder) && is_array($tmpprio))
    
array_multisort($tmporderSORT_ASC$tmpprioSORT_ASC$arr);

/* start building our return value. */
/* we will return a html string that can be displayed on the web */
/* for an example go to http://michiel.vanbaak.info */
/* the "contact" block there uses this function */
$output "";

foreach (
$arr as $key => $row) {
    
/* the Address. Get rid of the \; and IDENTIFIER= strings */
    
if (strtolower(substr($row["url"], 05)) == "data:" && strlen($row["url"]) > 10 && $address_done == 0) {
        
$addressdata substr($row["url"], 6);
        
$bits explode(";"$addressdata);
        
$address = array();
        foreach (
$bits as $line) {
            
$line preg_replace("/(^;)|(\\\\$)/si"""$line);
            if (
strstr($line"=")) {
                
$parts explode("="$line);
                
$address[$parts[0]] = $parts[1];
            }
        }
        if (
count($address)==5) {
            
$address array_unique($address);
            
$output .= $address["CN"]."<br />".$address["STREET"]."<br />".$address["PC"]."&nbsp;".$address["L"]."<br />".$address["C"]."<br />";
            unset(
$address);
            
$address_done 1;
        }
        
/* the url. Add an <a> tag around it so it's clickable */
    
} else if (strtolower(substr($row["url"], 05)) == "http:") {
        
$row["url"] = strtolower($row["url"]);
        
$output .= "<a href=\"".$row["url"]."\">".$row["url"]."</a><br />";
        
/* same with email address */
    
} else if (strtolower(substr($row["url"], 07)) == "mailto:") {
        
$output .= "<a href=\"".$row["url"]."\">".substr($row["url"], 7)."</a><br />";
        
/* we omit the dnc and gpg settings */
    
} else if (strtolower(substr($row["url"], 04)) == "dnc:" || strtolower(substr($row["url"], 04)) == "gpg:" || strtolower(substr($row["url"], 04)) == "data" || strtolower(substr($row["url"], 01)) == ",") {
        continue;
    } else if (
strtolower(substr($row["url"], 03)) == "tel") {
        
$output .= str_replace("\\"""$row["url"]);
        
$output .= "<br />";
        
/* everything else is printed as is */
    
} else {
        
$output .= $row["url"]."<br />";
    }
}
echo 
trim($output);
?>