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

Revision 9416, 12.9 kB (checked in by p_lindheimer, 3 years ago)

really fixes #3979 glad I haven't published for real yet

  • 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   global $version;
121   switch($engine) {
122     case "asterisk":
123       $sources = cidlookup_list(true);
124       if(is_array($sources)) {
125         foreach($sources as $item) {
126
127           // Search for number in the cache, if found lookupcidnum and return
128           if ($item['cidlookup_id'] != 0) {
129             if ($item['cache'] == 1 && $item['sourcetype'] != 'internal') {
130               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_gotoif('$[${DB_EXISTS(cidname/${CALLERID(num)})} = 1]', 'cidlookup,cidlookup_return,1'));
131             }
132           }
133
134           switch($item['sourcetype']) {
135
136             case "internal":
137               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_lookupcidname(''));
138             break;
139
140             case "enum":
141               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_setvar('CALLERID(name)', '${TXTCIDNAME(${CALLERID(num)})}'));
142
143
144             break;
145
146             case "http":
147               if (!empty($item['http_username']) && !empty($item['http_password']))
148                 $auth = sprintf('%s:%s@', $item['http_username'], $item['http_password']);
149               else
150                 $auth = '';
151                
152               if (!empty($item['http_port']))
153                 $host = sprintf('%s:%d', $item['http_host'], $item['http_port']);
154               else
155                 $host = $item['http_host'].':80';
156
157               if (substr($item['http_path'], 0, 1) == '/')
158                 $path = substr($item['http_path'], 1);
159               else
160                 $path = $item['http_path'];
161                
162               $query = str_replace('[NUMBER]', '${CALLERID(num)}', $item['http_query']);
163               $url = sprintf('http://%s%s/%s?%s', $auth, $host, $path, $query);
164               $curl = sprintf('${CURL(%s)}', $url);
165              
166               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_setvar('CALLERID(name)', $curl));
167             break;
168
169             case "mysql":
170               if (version_compare($version, "1.6", "lt")) {
171                 //Escaping MySQL query - thanks to http://www.asteriskgui.com/index.php?get=utilities-mysqlscape
172                 $replacements = array (
173                   '\\' => '\\\\',
174                   '"' => '\\"',
175                   '\'' => '\\\'',
176                   ' ' => '\\ ',
177                   ',' => '\\,',
178                   '(' => '\\(',
179                   ')' => '\\)',
180                   '.' => '\\.',
181                   '|' => '\\|'
182                 );
183                 $query = str_replace(array_keys($replacements), array_values($replacements), $item['mysql_query']);
184               } else {
185                 $query = $item['mysql_query'];
186               }
187               $query = str_replace('[NUMBER]', '${CALLERID(num)}', $query);
188
189               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_connect('connid', $item['mysql_host'],  $item['mysql_username'],  $item['mysql_password'],  $item['mysql_dbname']));             
190               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_query('resultid', 'connid', $query));
191               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_fetch('fetchid', 'resultid', 'CALLERID(name)'));
192               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_clear('resultid'));             
193               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_disconnect('connid'));
194             break;
195
196             // TODO: implement SugarCRM lookup, look at code snippet at http://nerdvittles.com/index.php?p=82
197             case "sugarcrm":
198               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_noop('SugarCRM not yet implemented'));
199               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_return(''));
200             break;
201           }
202
203           // Put numbers in the cache
204           if ($item['cidlookup_id'] != 0) {
205             if ($item['cache'] == 1 && $item['sourcetype'] != 'internal') {
206               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_db_put('cidname', '${CALLERID(num)}', '${CALLERID(name)}' ));
207             }
208             $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_return(''));
209           }
210         }
211
212         $ext->add('cidlookup', 'cidlookup_return', '', new ext_lookupcidname(''));
213         $ext->add('cidlookup', 'cidlookup_return', '', new ext_return(''));
214       }
215     break;
216   }
217 }
218
219
220 function cidlookup_did_get($did){
221   $extarray = explode('/', $did, 2);
222   if(count($extarray) == 2) { // differentiate beetween '//' (Any did / any cid and '' empty string)
223     $sql = sprintf("SELECT cidlookup_id FROM cidlookup_incoming WHERE extension = '%s' AND cidnum = '%s'", $extarray[0], $extarray[1]);
224     $result = sql($sql, "getRow", DB_FETCHMODE_ASSOC);
225     if(is_array($result)){
226       return $result['cidlookup_id'];
227     } else
228       return null;
229   } else { // $did is an empty string (for example when adding a new did)
230     return 0;
231   }
232 }
233
234 function cidlookup_did_list() {
235   $sql = "
236   SELECT cidlookup_id, a.extension extension, a.cidnum cidnum, pricid FROM cidlookup_incoming a
237   INNER JOIN incoming b
238   ON a.extension = b.extension AND a.cidnum = b.cidnum
239   ";
240
241   $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
242   return is_array($results)?$results:null;
243 }
244
245 function cidlookup_list($all=false) {
246   $allowed = array(array('cidlookup_id' => 0, 'description' => _("None"), 'sourcetype' => null));
247   $results = sql("SELECT * FROM cidlookup","getAll",DB_FETCHMODE_ASSOC);
248   if(is_array($results)){
249     foreach($results as $result){
250       // check to see if we have a dept match for the current AMP User.
251       if ($all || checkDept($result['deptname'])){
252         // return this item
253         $allowed[] = $result;
254       }
255     }
256   }
257   return isset($allowed)?$allowed:null;
258 }
259
260 function cidlookup_get($id){
261   $results = sql("SELECT * FROM cidlookup WHERE cidlookup_id = '$id'","getRow",DB_FETCHMODE_ASSOC);
262   return isset($results)?$results:null;
263 }
264
265 function cidlookup_del($id){
266   // Deleting source and its associations
267   $results = sql("DELETE FROM cidlookup WHERE cidlookup_id = '$id'","query");
268   $results = sql("DELETE FROM cidlookup_incoming WHERE cidlookup_id = '$id'","query");
269 }
270
271 function cidlookup_add($post){
272   global $db;
273
274   $description = $db->escapeSimple($post['description']);
275   $sourcetype = $db->escapeSimple($post['sourcetype']);
276   $deptname = $db->escapeSimple($post['deptname']);
277   $http_host = $db->escapeSimple($post['http_host']);
278   $http_port = $db->escapeSimple($post['http_port']);
279   $http_username = $db->escapeSimple($post['http_username']);
280   $http_password = $db->escapeSimple($post['http_password']);
281   $http_path = $db->escapeSimple($post['http_path']);
282   $http_query = $db->escapeSimple($post['http_query']);
283   $mysql_host = $db->escapeSimple($post['mysql_host']);
284   $mysql_dbname = $db->escapeSimple($post['mysql_dbname']);
285   $mysql_query = $db->escapeSimple($post['mysql_query']);
286   $mysql_username = $db->escapeSimple($post['mysql_username']);
287   $mysql_password = $db->escapeSimple($post['mysql_password']);
288
289   $cache = isset($post['cache']) ? $db->escapeSimple($post['cache']) : 0;
290
291   $results = sql("
292     INSERT INTO cidlookup
293       (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)
294     VALUES
295       ('$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')
296     ");
297 }
298
299 function cidlookup_edit($id,$post){
300   global $db;
301
302   $description = $db->escapeSimple($post['description']);
303   $sourcetype = $db->escapeSimple($post['sourcetype']);
304   $deptname = $db->escapeSimple($post['deptname']);
305   $http_host = $db->escapeSimple($post['http_host']);
306   $http_port = $db->escapeSimple($post['http_port']);
307   $http_username = $db->escapeSimple($post['http_username']);
308   $http_password = $db->escapeSimple($post['http_password']);
309   $http_path = $db->escapeSimple($post['http_path']);
310   $http_query = $db->escapeSimple($post['http_query']);
311   $mysql_host = $db->escapeSimple($post['mysql_host']);
312   $mysql_dbname = $db->escapeSimple($post['mysql_dbname']);
313   $mysql_query = $db->escapeSimple($post['mysql_query']);
314   $mysql_username = $db->escapeSimple($post['mysql_username']);
315   $mysql_password = $db->escapeSimple($post['mysql_password']);
316
317   if (isset($post['cache']) && $post['cache'] != 1) {
318     $cache = 0;
319   }
320
321   $results = sql("
322     UPDATE cidlookup
323     SET
324       description = '$description',
325       deptname = '$deptname',
326       sourcetype = '$sourcetype' ,
327       cache = '$cache',
328       http_host = '$http_host',
329       http_port = '$http_port',
330       http_username = '$http_username',
331       http_password = '$http_password',
332       http_path = '$http_path',
333       http_query = '$http_query',
334       mysql_host = '$mysql_host',
335       mysql_dbname = '$mysql_dbname',
336       mysql_query = '$mysql_query',
337       mysql_username = '$mysql_username',
338       mysql_password  = '$mysql_password'
339     WHERE cidlookup_id = '$id'");
340 }
341 ?>
Note: See TracBrowser for help on using the browser.