root/modules/branches/2.10/core/agi-bin/enumlookup.agi

Revision 11665, 5.5 kB (checked in by p_lindheimer, 2 years ago)

remove most eregi() re #4001

  • Property svn:mime-type set to text/html
  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1 #!/usr/bin/php -q
2 <?php
3 // Replacement for Asterisk's ENUMLOOKUP function.
4 // Written by Rob Thomas <xrobau@gmail.com>
5 //
6 //    This file is part of FreePBX. http://www.freepbx.org
7 //
8 //    FreePBX is free software: you can redistribute it and/or modify
9 //    it under the terms of the GNU Affero General Public License as
10 //    published by the Free Software Foundation, either version 3 of the
11 //    License, or (at your option) any later version.
12 //
13 //    FreePBX is distributed in the hope that it will be useful,
14 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //    GNU Affero General Public License for more details.
17 //
18 //    You should have received a copy of the GNU Affero General Public License
19 //    along with FreePBX.  If not, see <http://www.gnu.org/licenses/>.
20 //
21 //    Copyright 2009 Rob Thomas
22 //    Portions Copyright 2010 Mikael Carlsson
23 //    Modified to use Google DNS
24 //    Additional patch to support enum.conf by matka
25 //
26 //    Originally based on e164.org's enum.php script, available from
27 //        http://www.e164.org/enum.phps
28
29 /* --------WARNING---------
30  *
31  * This script is automatically copied from $ASTMODULES/core/agi-bin and will be
32  * overwritten any time you do any module changes. To avoid this, either set this
33  * file to READ ONLY (chmod a-w enumlookup.agi) or alter the file in core.
34  * It's probably better to submit any changes or enhancements to the freepbx tracker
35  * and they'll be rolled into core!
36  *
37  * --------WARNING---------
38 */
39
40 require_once "phpagi.php";
41
42 $AGI = new AGI();
43 $enums = Array('e164.org','e164.arpa','e164.info');
44 $dialstr = "";
45 $lookup = get_var( $AGI, "DIAL_NUMBER" );
46
47 @exec('dig -h > /dev/null 2>&1 ', $res, $var);
48 if ($var != 127) {
49     $diginstalled = 'true';
50     }
51 // Are we going to use Google's DNS for ENUM? If not use standard DNS.
52 // Note, for this to work you need to set USEGOOGLEDNSFORENUM = TRUE in amportal.conf
53 $usegdns = get_var( $AGI, "ENUMUSEGOOGLEDNS" );
54
55 // Go through the ENUM providers and look for the number.
56 foreach ($enums as $enum) {
57 // If we are using Google DNS, do not use php dns_get_record
58         if ($usegdns != "TRUE") {
59         // Are we using php5 and can use get_dns_record?
60           if (function_exists("dns_get_record")) {
61                   $arr = get_php5($lookup, $enum);
62                 } else {
63                 $AGI->verbose("ENUM lookup via php dns_get_record failed due to php5 not being installed", "0");
64                 }
65          } else {
66                 if($diginstalled) {
67                     $arr = get_dig($lookup, $enum);
68                 } else {
69       $AGI->verbose("ENUM LOOKUPS DISABLED due to no dig command", "0");
70                 }
71             }
72   if (isset($arr[0])) {
73     foreach($arr as $key => $row) {
74       $order[$key] = $row["order"];
75       $prio[$key] = $row["prio"];
76       $nextURI[] = $row['URI'];
77       $row['URI'] = count($nextURI) - 1;
78       $arr[$key] = $row;
79     }
80     array_multisort($order, SORT_ASC, SORT_NUMERIC, $prio, SORT_ASC, SORT_NUMERIC, $arr);
81     foreach ($arr as $key => $row) {
82       if(preg_match('/sip|iax/', strtolower($row['tech']))) {
83         $URI = $row['URI'];
84         $dialstr .= $nextURI[$URI]."%";
85       }
86     }
87   }
88 }
89
90 $AGI->verbose("Setting DIALARR to $dialstr", 3);
91 $AGI->set_variable("DIALARR", $dialstr);
92 if ($dialstr == '') {
93   $AGI->set_variable("DIALSTATUS", 'CONGESTION');
94 }
95
96 function get_dig($lookup, $enum) {
97   global $AGI;
98         global $usegdns;
99
100         $arpa = "";
101         for ($i = 0; $i < strlen($lookup); $i++) {
102                 $arpa = $lookup[$i].".".$arpa;
103         }
104         if ($usegdns == TRUE) {
105                 $AGI->verbose("Looking up $lookup on $enum via shell command DIG and use google dns",3);
106                 $lines = trim(`/usr/bin/dig @8.8.8.8 +short ${arpa}${enum} naptr`);
107         } else {
108                 $AGI->verbose("Looking up $lookup on $enum via shell command DIG",3);
109                 $lines = trim(`/usr/bin/dig +short ${arpa}${enum} naptr`);
110         }
111   $lines = explode("\n", $lines);
112   foreach($lines as $line) {
113     $line = trim($line);
114     if (preg_match("/^;;/", $line))
115       continue;
116     if (!isset($arr)) $arr = array();
117     $line = str_replace("\t", " ", $line);
118     while(strstr($line, "  "))
119       $line = str_replace("  ", " ", $line);
120     $line = str_replace("\"", "", $line);
121     $line = str_replace("\'", "", $line);
122     $line = str_replace(" ", "|", $line);
123     $bits = explode("|", $line);
124     $bit = explode("!", stripslashes($bits[4]));
125     $URI = preg_replace('/' . $bit[1] . '/', $bit[2], "+".$lookup);
126     if($URI[3] == ":")
127       $URI[3] = "/";
128     if($URI[4] == ":")
129     $URI[4] = "/";
130     $arr[] = array("order" => $bits[0], "prio" => $bits[1], "tech" => $bits[3], "URI" => $URI);
131   }
132   if (isset($arr[0])) {
133     return $arr;
134   } else {
135     return null;
136   }
137 }
138
139 function get_php5($lookup, $enum) {
140   global $AGI;
141
142   $AGI->verbose("Looking up $lookup on $enum via dns_get_record",3);
143
144   $arpa = "";
145   for ($i = 0; $i < strlen($lookup); $i++) {
146     $arpa = $lookup[$i].".".$arpa;
147   }
148   $res = dns_get_record("$arpa$enum", DNS_NAPTR);
149   foreach ($res as $entry) {
150     if (!isset($arr)) $arr = array();
151     $bit = explode("!", $entry['regex']);
152     $URI = preg_replace('/' . $bit[1] . '/', $bit[2], "+".$lookup);
153     if($URI[3] == ":") $URI[3] = "/";
154     if($URI[4] == ":") $URI[4] = "/";
155     $arr[] = array("order" => $entry['order'], "prio" => $entry['pref'], "tech" => $entry['services'], "URI" => $URI);
156   }
157   if (isset($arr[0])) {
158     return $arr;
159   } else {
160     return null;
161   }
162 }
163
164
165 // helper functions
166 function get_var( $agi, $value)
167 {
168   $r = $agi->get_variable( $value );
169
170   if ($r['result'] == 1)
171   {
172     $result = $r['data'];
173     return $result;
174   }
175   else return '';
176 }
177
178 ?>
Note: See TracBrowser for help on using the browser.