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

Revision 11781, 13.4 kB (checked in by mickecarlsson, 2 years ago)

cidlookup, spelling errors

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