root/modules/branches/2.6/cidlookup/functions.inc.php

Revision 7186, 12.8 kB (checked in by p_lindheimer, 5 years ago)

Merged revisions 7081-7083,7085-7097,7100-7125,7128-7133,7135-7152,7155-7178,7180,7182-7185 via svnmerge from
http://svn.freepbx.org/modules/branches/2.5

........

r7130 | sasargen | 2008-10-24 13:39:42 -0700 (Fri, 24 Oct 2008) | 1 line


fixed dialplan for vm-callme to handle vm-starmain option

........

r7169 | mickecarlsson | 2008-10-28 08:09:42 -0700 (Tue, 28 Oct 2008) | 1 line


Fixes a couple of spelling errors in varoious module.xml files

........

r7170 | mickecarlsson | 2008-10-28 11:57:18 -0700 (Tue, 28 Oct 2008) | 1 line


Added Spanish language files for callback, closes #3340

........

r7175 | ethans | 2008-10-29 10:24:51 -0700 (Wed, 29 Oct 2008) | 5 lines


Changes sox to use vol nomenclature for adjusting volume of output file, rather than -v which was deprecated. The vol switch will work on
older and newer versions of sox.


Closes #3346

........

r7180 | p_lindheimer | 2008-10-29 11:11:06 -0700 (Wed, 29 Oct 2008) | 1 line


fixes #3348 wrong registry count with Asteisk 1.6+ and counting iax2 registrations as sip

........

r7182 | p_lindheimer | 2008-10-29 17:31:27 -0700 (Wed, 29 Oct 2008) | 1 line


fixes #3345 requires core, blacklist and cidlookup to all be updated

........

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 <?php /* $Id */
2 //Copyright (C) 2006 WeBRainstorm S.r.l. (ask@webrainstorm.it)
3 //
4 //This program is free software; you can redistribute it and/or
5 //modify it under the terms of the GNU General Public License
6 //as published by the Free Software Foundation; either version 2
7 //of the License, or (at your option) any later version.
8 //
9 //This program is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //GNU General Public License for more details.
13
14 function cidlookup_hook_core($viewing_itemid, $target_menuid) {
15   // TODO: add option to avoid callerid lookup if the telco already supply a callerid name (Overwrite checkbox ? )
16   $html = '';
17   if ($target_menuid == 'did')  {
18     $html = '<tr><td colspan="2"><h5>';
19     $html .= _("CID Lookup Source");
20     $html .= '<hr></h5></td></tr>';
21     $html .= '<tr>';
22     $html .= '<td><a href="#" class="info">';
23     $html .= _("Source").'<span>'._("Sources can be added in Caller Name Lookup Sources section").'.</span></a>:</td>';
24     $html .= '<td><select name="cidlookup_id">';
25     $sources = cidlookup_list();
26     $current = cidlookup_did_get($viewing_itemid);
27     foreach ($sources as $source)
28       $html .= sprintf('<option value="%d" %s>%s</option>', $source['cidlookup_id'], ($current == $source['cidlookup_id']?'selected':''), $source['description']);
29     $html .= '</select></td></tr>';
30 /*
31     // Not yet fully implemented
32     $html .= '<tr>';
33     $html .= '<td><a href="#" class="info">';
34     $html .= _("Overwrite Caller Name").'<span>'._("This option let the source specified overwrite the caller name if already supplied from telco").'.</span></a>:</td>';
35     $html .= '<td><input type="checkbox" name="overwrite" value="1"></td>';
36     $html .= '</tr>';
37 */
38
39   }
40
41   return $html;
42  
43 }
44
45 function cidlookup_hookProcess_core($viewing_itemid, $request) {
46  
47   // TODO: move sql to functions cidlookup_did_(add, del, edit)
48   if (!isset($request['action']))
49     return;
50   switch ($request['action']) {
51     case 'addIncoming':
52       $results = sql(sprintf('INSERT INTO cidlookup_incoming (cidlookup_id, extension, cidnum) VALUES ("%d", "%s", "%s")',
53         $request['cidlookup_id'], $request['extension'], $request['cidnum']));
54     break;
55     case 'delIncoming':
56       $extarray = explode('/', $request['extdisplay'], 2);
57       $results = sql(sprintf("DELETE FROM cidlookup_incoming WHERE extension = '%s' AND cidnum = '%s'",
58         $extarray[0], $extarray[1]));
59     break;
60     case 'edtIncoming': // deleting and adding as in core module
61       $extarray = explode('/', $request['extdisplay'], 2);
62       $results = sql(sprintf("DELETE FROM cidlookup_incoming WHERE extension = '%s' AND cidnum = '%s'",
63         $extarray[0], $extarray[1]));
64       $results = sql(sprintf('INSERT INTO cidlookup_incoming (cidlookup_id, extension, cidnum) VALUES ("%d", "%s", "%s")',
65         $request['cidlookup_id'], $request['extension'], $request['cidnum']));
66     break;
67   }
68 }
69
70
71 function cidlookup_hookGet_config($engine) {
72   // TODO: integrating with direct extension <-> DID association
73   // TODO: add option to avoid callerid lookup if the telco already supply a callerid name (GosubIf)
74   global $ext;  // is this the best way to pass this?
75   switch($engine) {
76     case "asterisk":
77       $pairing = cidlookup_did_list();
78       if(is_array($pairing)) {
79         foreach($pairing as $item) {
80           if ($item['cidlookup_id'] != 0) {
81
82             // Code from modules/core/functions.inc.php core_get_config inbound routes
83             $exten = trim($item['extension']);
84             $cidnum = trim($item['cidnum']);
85            
86             if ($cidnum != '' && $exten == '') {
87               $exten = 's';
88               $pricid = ($item['pricid']) ? true:false;
89             } else if (($cidnum != '' && $exten != '') || ($cidnum == '' && $exten == '')) {
90               $pricid = true;
91             } else {
92               $pricid = false;
93             }
94             $context = ($pricid) ? "ext-did-0001":"ext-did-0002";
95
96             $exten = (empty($exten)?"s":$exten);
97             $exten = $exten.(empty($cidnum)?"":"/".$cidnum); //if a CID num is defined, add it
98
99             $ext->splice($context, $exten, 1, new ext_gosub('1', 'cidlookup_'.$item['cidlookup_id'], 'cidlookup'));
100          
101           }
102         }
103       }
104     break;
105   }
106
107 }
108
109 /*
110
111 //  Generates dialplan for cidlookup
112 //  We call this with retrieve_conf
113
114 */
115
116 function cidlookup_get_config($engine) {
117   // TODO: discuss if mysql and http lookup should be implemented in dialplan or in an external AGI
118   global $ext;  // is this the best way to pass this?
119   global $asterisk_conf;
120   switch($engine) {
121     case "asterisk":
122       $sources = cidlookup_list();
123       if(is_array($sources)) {
124         foreach($sources as $item) {
125
126           // Search for number in the cache, if found lookupcidnum and return
127           if ($item['cidlookup_id'] != 0) {
128             if ($item['cache'] == 1 && $item['sourcetype'] != 'internal') {
129               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_gotoif('$[${DB_EXISTS(cidname/${CALLERID(num)})} = 1]', 'cidlookup,cidlookup_return,1'));
130             }
131           }
132
133           switch($item['sourcetype']) {
134
135             case "internal":
136               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_lookupcidname(''));
137             break;
138
139             case "enum":
140               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_txtcidname('${CALLERID(num)}'));
141               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_setvar('CALLERID(name)', '${TXTCIDNAME}'));
142             break;
143
144             case "http":
145               if (!empty($item['http_username']) && !empty($item['http_password']))
146                 $auth = sprintf('%s:%s@', $item['http_username'], $item['http_password']);
147               else
148                 $auth = '';
149                
150               if (!empty($item['http_port']))
151                 $host = sprintf('%s:%d', $item['http_host'], $item['http_port']);
152               else
153                 $host = $item['http_host'].':80';
154
155               if (substr($item['http_path'], 0, 1) == '/')
156                 $path = substr($item['http_path'], 1);
157               else
158                 $path = $item['http_path'];
159                
160               $query = str_replace('[NUMBER]', '${CALLERID(num)}', $item['http_query']);
161               $url = sprintf('http://%s%s/%s?%s', $auth, $host, $path, $query);
162               $curl = sprintf('${CURL(%s)}', $url);
163              
164               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_setvar('CALLERID(name)', $curl));
165             break;
166
167             case "mysql":
168               //Escaping MySQL query - thanks to http://www.asteriskgui.com/index.php?get=utilities-mysqlscape
169
170               $replacements = array (
171                   '\\' => '\\\\',
172                   '"' => '\\"',
173                   '\'' => '\\\'',
174                   ' ' => '\\ ',
175                   ',' => '\\,',
176                   '(' => '\\(',
177                   ')' => '\\)',
178                   '.' => '\\.',
179                   '|' => '\\|'
180               );
181              
182               $query = str_replace(array_keys($replacements), array_values($replacements), $item['mysql_query']);
183               $query = str_replace('[NUMBER]', '${CALLERID(num)}', $query);
184
185               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_connect('connid', $item['mysql_host'],  $item['mysql_username'],  $item['mysql_password'],  $item['mysql_dbname']));             
186               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_query('resultid', 'connid', $query));
187               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_fetch('fetchid', 'resultid', 'CALLERID(name)'));
188               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_clear('resultid'));             
189               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_disconnect('connid'));
190             break;
191
192             // TODO: implement SugarCRM lookup, look at code snippet at http://nerdvittles.com/index.php?p=82
193             case "sugarcrm":
194               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_noop('SugarCRM not yet implemented'));
195               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_return(''));
196             break;
197           }
198
199           // Put numbers in the cache
200           if ($item['cidlookup_id'] != 0) {
201             if ($item['cache'] == 1 && $item['sourcetype'] != 'internal') {
202               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_db_put('cidname', '${CALLERID(num)}', '${CALLERID(name)}' ));
203             }
204             $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_return(''));
205           }
206         }
207
208         $ext->add('cidlookup', 'cidlookup_return', '', new ext_lookupcidname(''));
209         $ext->add('cidlookup', 'cidlookup_return', '', new ext_return(''));
210       }
211     break;
212   }
213 }
214
215
216 function cidlookup_did_get($did){
217   $extarray = explode('/', $did, 2);
218   if(count($extarray) == 2) { // differentiate beetween '//' (Any did / any cid and '' empty string)
219     $sql = sprintf("SELECT cidlookup_id FROM cidlookup_incoming WHERE extension = '%s' AND cidnum = '%s'", $extarray[0], $extarray[1]);
220     $result = sql($sql, "getRow", DB_FETCHMODE_ASSOC);
221     if(is_array($result)){
222       return $result['cidlookup_id'];
223     } else
224       return null;
225   } else { // $did is an empty string (for example when adding a new did)
226     return 0;
227   }
228 }
229
230 function cidlookup_did_list() {
231   $sql = "
232   SELECT cidlookup_id, a.extension extension, a.cidnum cidnum, pricid FROM cidlookup_incoming a
233   INNER JOIN incoming b
234   ON a.extension = b.extension AND a.cidnum = b.cidnum
235   ";
236
237   $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
238   return is_array($results)?$results:null;
239 }
240
241 function cidlookup_list() {
242   // TODO: discuss department isolation of sources
243   $allowed = array(array('cidlookup_id' => 0, 'description' => _("None"), 'sourcetype' => null));
244   $results = sql("SELECT * FROM cidlookup","getAll",DB_FETCHMODE_ASSOC);
245   if(is_array($results)){
246     foreach($results as $result){
247       // check to see if we have a dept match for the current AMP User.
248       if (checkDept($result['deptname'])){
249         // return this item
250         $allowed[] = $result;
251       }
252     }
253   }
254   return isset($allowed)?$allowed:null;
255 }
256
257 function cidlookup_get($id){
258   $results = sql("SELECT * FROM cidlookup WHERE cidlookup_id = '$id'","getRow",DB_FETCHMODE_ASSOC);
259   return isset($results)?$results:null;
260 }
261
262 function cidlookup_del($id){
263   // Deleting source and its associations
264   $results = sql("DELETE FROM cidlookup WHERE cidlookup_id = '$id'","query");
265   $results = sql("DELETE FROM cidlookup_incoming WHERE cidlookup_id = '$id'","query");
266 }
267
268 function cidlookup_add($post){
269   global $db;
270
271   $description = $db->escapeSimple($post['description']);
272   $sourcetype = $db->escapeSimple($post['sourcetype']);
273   $deptname = $db->escapeSimple($post['deptname']);
274   $http_host = $db->escapeSimple($post['http_host']);
275   $http_port = $db->escapeSimple($post['http_port']);
276   $http_username = $db->escapeSimple($post['http_username']);
277   $http_password = $db->escapeSimple($post['http_password']);
278   $http_path = $db->escapeSimple($post['http_path']);
279   $http_query = $db->escapeSimple($post['http_query']);
280   $mysql_host = $db->escapeSimple($post['mysql_host']);
281   $mysql_dbname = $db->escapeSimple($post['mysql_dbname']);
282   $mysql_query = $db->escapeSimple($post['mysql_query']);
283   $mysql_username = $db->escapeSimple($post['mysql_username']);
284   $mysql_password = $db->escapeSimple($post['mysql_password']);
285
286   $cache = isset($post['cache']) ? $db->escapeSimple($post['cache']) : 0;
287
288   $results = sql("
289     INSERT INTO cidlookup
290       (description, sourcetype, cache, deptname, http_host, http_port, http_username, http_password, http_path, http_query, mysql_host, mysql_dbname, mysql_query, mysql_username, mysql_password)
291     VALUES
292       ('$description', '$sourcetype', '$cache', '$deptname', '$http_host', '$http_port', '$http_username', '$http_password', '$http_path', '$http_query', '$mysql_host', '$mysql_dbname', '$mysql_query', '$mysql_username', '$mysql_password')
293     ");
294 }
295
296 function cidlookup_edit($id,$post){
297   global $db;
298
299   $description = $db->escapeSimple($post['description']);
300   $sourcetype = $db->escapeSimple($post['sourcetype']);
301   $deptname = $db->escapeSimple($post['deptname']);
302   $http_host = $db->escapeSimple($post['http_host']);
303   $http_port = $db->escapeSimple($post['http_port']);
304   $http_username = $db->escapeSimple($post['http_username']);
305   $http_password = $db->escapeSimple($post['http_password']);
306   $http_path = $db->escapeSimple($post['http_path']);
307   $http_query = $db->escapeSimple($post['http_query']);
308   $mysql_host = $db->escapeSimple($post['mysql_host']);
309   $mysql_dbname = $db->escapeSimple($post['mysql_dbname']);
310   $mysql_query = $db->escapeSimple($post['mysql_query']);
311   $mysql_username = $db->escapeSimple($post['mysql_username']);
312   $mysql_password = $db->escapeSimple($post['mysql_password']);
313
314   if (isset($post['cache']) && $post['cache'] != 1) {
315     $cache = 0;
316   }
317
318   $results = sql("
319     UPDATE cidlookup
320     SET
321       description = '$description',
322       deptname = '$deptname',
323       sourcetype = '$sourcetype' ,
324       cache = '$cache',
325       http_host = '$http_host',
326       http_port = '$http_port',
327       http_username = '$http_username',
328       http_password = '$http_password',
329       http_path = '$http_path',
330       http_query = '$http_query',
331       mysql_host = '$mysql_host',
332       mysql_dbname = '$mysql_dbname',
333       mysql_query = '$mysql_query',
334       mysql_username = '$mysql_username',
335       mysql_password  = '$mysql_password'
336     WHERE cidlookup_id = '$id'");
337 }
338 ?>
Note: See TracBrowser for help on using the browser.