root/apps/hosted_embed/trunk/htdocs/lookup/telLookup.php
@
718
| Revision 718, 9.6 kB (checked in by nadya, 3 years ago) |
|---|
| Line | |
|---|---|
| 1 | <?php |
| 2 | header("Content-Type: application/json;charset=utf-8"); |
| 3 | require_once 'Net/DNS.php'; |
| 4 | |
| 5 | $stderr = fopen("php://stderr","w"); |
| 6 | |
| 7 | $domain = $_GET["domain"]; |
| 8 | |
| 9 | $data = array(); |
| 10 | $data['domain'] = $domain; |
| 11 | |
| 12 | function cmp($a, $b) { |
| 13 | if ($a == $b) { |
| 14 | return 0; |
| 15 | } |
| 16 | return ($a < $b) ? -1 : 1; |
| 17 | } |
| 18 | |
| 19 | # Sorts NAPTR records using each attribute as specified, in order |
| 20 | function naptr_sort($a, $b) { |
| 21 | $answer; |
| 22 | $attributes = array('preference', 'order', 'services', 'regexp', 'flags', 'replacement'); |
| 23 | |
| 24 | foreach ($attributes as $attribute) { |
| 25 | $answer = cmp($a->$attribute, $b->$attribute); |
| 26 | if ($answer == 0) { |
| 27 | # Keep on sorting if these fields are equal |
| 28 | continue; |
| 29 | } |
| 30 | # /else/ We have an answer, so break out early |
| 31 | break; |
| 32 | } |
| 33 | return $answer; |
| 34 | } |
| 35 | |
| 36 | function get_naptrs($resolver, $domain) { |
| 37 | $services = $resolver->query($domain, 'NAPTR')->answer; |
| 38 | $plain_services = array(); |
| 39 | if (count($services)) { |
| 40 | usort($services, "naptr_sort"); |
| 41 | |
| 42 | $attributes = array('preference', 'order', 'services', 'regexp', 'flags', 'replacement', 'regex'); |
| 43 | |
| 44 | foreach ($services as $service) { |
| 45 | $plain_service = array(); |
| 46 | foreach ($attributes as $attribute) { |
| 47 | $plain_service[$attribute] = $service->$attribute; |
| 48 | } |
| 49 | |
| 50 | array_push($plain_services, $plain_service); |
| 51 | } |
| 52 | } |
| 53 | return $plain_services; |
| 54 | } |
| 55 | |
| 56 | $types = array( |
| 57 | 'email:mailto' => 'email', |
| 58 | 'voice:tel' => 'phone', |
| 59 | 'fax:tel' => 'fax', |
| 60 | 'web:http' => 'website', |
| 61 | ); |
| 62 | |
| 63 | # Nameage |
| 64 | $interesting_name_keywords = array( |
| 65 | # 'nl' => 'Name', |
| 66 | 's' => 'salutation', |
| 67 | 'cn' => 'common_name', |
| 68 | 'fn' => 'first_name', |
| 69 | 'ln' => 'last_name', |
| 70 | ); |
| 71 | |
| 72 | # Postalage |
| 73 | $interesting_address_keywords = array( |
| 74 | # 'pa' => 'Address', |
| 75 | 'a1' => 'address_1', |
| 76 | 'a2' => 'address_2', |
| 77 | 'a3' => 'address_3', |
| 78 | 'tc' => 'town_city', |
| 79 | 'sp' => 'state_province', |
| 80 | 'pc' => 'postal_code', |
| 81 | 'c' => 'country', |
| 82 | ); |
| 83 | |
| 84 | # Companyage |
| 85 | $interesting_company_keywords = array( |
| 86 | 'o' => 'company_name', |
| 87 | 'cn' => 'company_name', |
| 88 | 'bn' => 'company_name', |
| 89 | ); |
| 90 | |
| 91 | # Helper function for development, allows to hide data in HTML comments |
| 92 | function comment ($str) { |
| 93 | if ($_GET["debug"]) { |
| 94 | if (is_array($str)) { |
| 95 | echo "<!--\n"; |
| 96 | print_r($str); |
| 97 | echo "-->\n"; |
| 98 | } |
| 99 | elseif (is_object($str)) { |
| 100 | echo "<!--\n"; |
| 101 | print_r($str); |
| 102 | echo "-->\n"; |
| 103 | } |
| 104 | else { |
| 105 | echo "<!-- ".$str." -->\n"; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | # |
| 113 | #- - M A I N - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 114 | # |
| 115 | |
| 116 | if ($domain) { |
| 117 | |
| 118 | $data['message'] = ""; |
| 119 | |
| 120 | $resolver = new Net_DNS_Resolver(); |
| 121 | if (array_key_exists('nameserver', $_GET)) { |
| 122 | $resolver->nameservers = array($_GET['nameserver']); |
| 123 | } |
| 124 | |
| 125 | # Need to use TCP to query Telnic domains |
| 126 | $resolver->usevc = 1; |
| 127 | $response = $resolver->query($domain); |
| 128 | |
| 129 | if ($response) { |
| 130 | |
| 131 | $txt_response = $resolver->query($domain, 'TXT'); |
| 132 | |
| 133 | # These are arrays of arrays |
| 134 | $system_records = array(); |
| 135 | $keyword_records = array(); |
| 136 | |
| 137 | # This is an array of strings |
| 138 | $txt_records = array(); |
| 139 | |
| 140 | if (count($txt_response) && $txt_response->answer) { |
| 141 | { |
| 142 | foreach ($txt_response->answer as $txt_response_entry) { |
| 143 | $txt_record = $txt_response_entry->text; |
| 144 | |
| 145 | if ($txt_record[0] == '.tkw') { |
| 146 | array_shift($txt_record); |
| 147 | array_shift($txt_record); |
| 148 | array_push($keyword_records, $txt_record); |
| 149 | } |
| 150 | else if ($txt_record[0] == '.tsm') { |
| 151 | // Ignore system records? |
| 152 | array_shift($txt_record); |
| 153 | array_shift($txt_record); |
| 154 | array_push($system_records, $txt_record); |
| 155 | } |
| 156 | else if ($txt_record[0] == '.tlb') { |
| 157 | // do nothing, it's a label |
| 158 | } |
| 159 | else { |
| 160 | // Ignore plain TXT records? |
| 161 | # array_push($txt_records, $txt_record[0]); |
| 162 | # comment($txt_record[0]); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | # $data['txt_records' ] = $txt_records; |
| 168 | # $data['system_records' ] = $system_records; |
| 169 | $data['keyword_records'] = $keyword_records; |
| 170 | |
| 171 | foreach ($system_records as $system_record) { |
| 172 | #comment("Checking system record: " . $system_record[0] . " = " . $system_record[1]); |
| 173 | if (strcmp($system_record[0], "dds") == 0) { |
| 174 | #comment("Got DDS"); |
| 175 | $data['dds'] = $system_record[1]; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (!isset($data['dds'])) { |
| 181 | #comment("DDS not set"); |
| 182 | $data['dds'] = $domain; |
| 183 | } |
| 184 | |
| 185 | //echo "Getting NAPTRS\n"; |
| 186 | $services = get_naptrs($resolver, $domain); |
| 187 | $data['naptr_records'] = $services; |
| 188 | |
| 189 | foreach ($services as $service) { |
| 190 | # print_r($service); |
| 191 | # echo "Flags = ".$service['flags']."\n"; |
| 192 | if ($service['flags'] == 'u') { |
| 193 | # echo "Regular NAPTR\n"; |
| 194 | $details = array(); |
| 195 | $service_hash = split('\+', $service['services']); |
| 196 | # echo "Services are:\n"; |
| 197 | # print_r($service_hash); |
| 198 | |
| 199 | # print_r($service_hash); |
| 200 | if ($service_hash[0] == 'E2U') { |
| 201 | foreach ($service_hash as $service_part) { |
| 202 | # echo "Checking $service_part\n"; |
| 203 | if ($types[ $service_part ]) { |
| 204 | # fprintf($stderr, "Is a ".$types[$service_part]); |
| 205 | $details['type'] = $types[$service_part]; |
| 206 | # print_r($details); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | fprintf($stderr, "Type is ".$details['type']."\n"); |
| 213 | comment("Type is ".$details['type']); |
| 214 | if ($details['type']) { |
| 215 | # Apply the regex to 'services' |
| 216 | preg_match("/^(.)/", $service['regex'], $regex_delims); |
| 217 | preg_match("/^".$regex_delims[1]."(.+?)".$regex_delims[1]."(.+?)".$regex_delims[1]."$/", $service['regex'], $regex_parts); |
| 218 | |
| 219 | # We're tring to get '!^.*!foo!' to just 'foo' |
| 220 | $uri = preg_replace($regex_delims[1].$regex_parts[1].$regex_delims[1], $regex_parts[2], $service['services']); |
| 221 | $uri = htmlspecialchars($uri); |
| 222 | |
| 223 | $details['value'] = preg_replace("!^.+\:(//)?!", "", $uri); |
| 224 | |
| 225 | if (is_array($data['form'][ $details['type'] ]) == false) { |
| 226 | $data['form'][ $details['type'] ] = array(); |
| 227 | } |
| 228 | array_push($data['form'][ $details['type'] ], $details['value']); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | foreach ($keyword_records as $keyword) { |
| 234 | $keys = array(); |
| 235 | $values = array(); |
| 236 | $remade = array(); |
| 237 | |
| 238 | for ($i = 0; $i < count($keyword); $i+=2) { |
| 239 | array_push($keys, $keyword[$i]); |
| 240 | array_push($values, $keyword[$i+1]); |
| 241 | $remade[ $keyword[$i] ] = $keyword[$i+1]; |
| 242 | } |
| 243 | |
| 244 | comment($remade); |
| 245 | |
| 246 | if ($keys[0] == 'pa') { |
| 247 | fprintf($stderr, "Postal Address found\n"); |
| 248 | |
| 249 | if (count($data['form']['address']) == 0) { |
| 250 | $data['form']['address'] = array(); |
| 251 | } |
| 252 | |
| 253 | $address_hash; |
| 254 | |
| 255 | foreach ($keys as $address) { |
| 256 | if (array_key_exists($address, $interesting_address_keywords)) { |
| 257 | $address_hash[ $interesting_address_keywords[$address] ] = $remade[ $address ]; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (count($address_hash)) { |
| 262 | array_push($data['form']['address'], $address_hash); |
| 263 | } |
| 264 | } |
| 265 | else if ($keys[0] == 'bpa') { |
| 266 | fprintf($stderr, "Business Postal Address found\n"); |
| 267 | |
| 268 | if (count($data['form']['business_address']) == 0) { |
| 269 | $data['form']['business_address'] = array(); |
| 270 | } |
| 271 | |
| 272 | $address_hash; |
| 273 | |
| 274 | foreach ($keys as $address) { |
| 275 | if (array_key_exists($address, $interesting_address_keywords)) { |
| 276 | $address_hash[ $interesting_address_keywords[$address] ] = $remade[ $address ]; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if (count($address_hash)) { |
| 281 | array_push($data['form']['business_address'], $address_hash); |
| 282 | } |
| 283 | } |
| 284 | else if ($keys[0] == 'nl') { |
| 285 | fprintf($stderr, "Name found\n"); |
| 286 | |
| 287 | if (count($data['form']['name']) == 0) { |
| 288 | $data['form']['name'] = array(); |
| 289 | } |
| 290 | |
| 291 | $name_hash; |
| 292 | |
| 293 | foreach ($keys as $name) { |
| 294 | if (array_key_exists($name, $interesting_name_keywords)) { |
| 295 | $name_hash[ $interesting_name_keywords[$name] ] = $remade[ $name ]; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if ($name_hash['first_name'] && $name_hash['last_name']) { |
| 300 | $name_hash['name'] = preg_replace("/\ +/", " ", preg_replace("/\(.+?\)/", "", $name_hash['first_name'].' '.$name_hash['last_name'])); |
| 301 | } |
| 302 | |
| 303 | if (count($name_hash)) { |
| 304 | array_push($data['form']['name'], $name_hash); |
| 305 | } |
| 306 | } |
| 307 | else if ($keys[0] == 'di') { |
| 308 | fprintf($stderr, "Directory information found\n"); |
| 309 | |
| 310 | if (count($data['form']['directory_information']) == 0) { |
| 311 | $data['form']['directory_information'] = array(); |
| 312 | } |
| 313 | |
| 314 | $di_hash; |
| 315 | |
| 316 | foreach ($keys as $key) { |
| 317 | if (array_key_exists($key, $interesting_company_keywords)) {# |
| 318 | $di_hash[ $interesting_company_keywords[$key] ] = $remade[ $key ]; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (count($di_hash)) { |
| 323 | array_push($data['form']['directory_information'], $di_hash); |
| 324 | } |
| 325 | } |
| 326 | else if ($keys[0] == 'bi') { |
| 327 | fprintf($stderr, "Business information found\n"); |
| 328 | |
| 329 | if (count($data['form']['business_information']) == 0) { |
| 330 | $data['form']['business_information'] = array(); |
| 331 | } |
| 332 | |
| 333 | $bi_hash; |
| 334 | |
| 335 | foreach ($keys as $key) { |
| 336 | if (array_key_exists($key, $interesting_company_keywords)) {# |
| 337 | $bi_hash[ $interesting_company_keywords[$key] ] = $remade[ $key ]; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (count($bi_hash)) { |
| 342 | array_push($data['form']['business_information'], $bi_hash); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | $data['status'] = 'pass'; |
| 347 | } |
| 348 | else { |
| 349 | $data['message'] = "Domain not found"; |
| 350 | $data['status'] = 'fail'; |
| 351 | } |
| 352 | } |
| 353 | else { |
| 354 | $data['message'] = "No domain"; |
| 355 | $data['status'] = 'fail'; |
| 356 | } |
| 357 | |
| 358 | unset( $data['naptr_records' ] ); |
| 359 | unset( $data['keyword_records'] ); |
| 360 | |
| 361 | if (is_array($data['form']) && count($data['form'])) { |
| 362 | foreach (array_keys($data['form']) as $form_entry) { |
| 363 | if (count( $data['form'][ $form_entry ] ) == 0) { |
| 364 | unset( $data['form'][ $form_entry ] ); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | print json_encode($data); |
| 370 | |
| 371 | fprintf($stderr, "Done\n\n"); |
| 372 | ?> |
Note: See TracBrowser
for help on using the browser.








