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

Revision 5483, 11.0 kB (checked in by p_lindheimer, 4 years ago)

#2541 migrate from channel routing and re-enable functionality, remove channel field

  • 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 = $item['extension'];
84             $cidnum = $item['cidnum'];
85            
86             $exten = (empty($exten)?"s":$exten);
87             $exten = $exten.(empty($cidnum)?"":"/".$cidnum); //if a CID num is defined, add it
88
89             $context = "ext-did";
90
91             $ext->splice($context, $exten, 1, new ext_gosub('1', 'cidlookup_'.$item['cidlookup_id'], 'cidlookup'));
92          
93           }
94         }
95       }
96     break;
97   }
98
99 }
100
101 /*
102
103 //  Generates dialplan for cidlookup
104 //  We call this with retrieve_conf
105
106 */
107
108 function cidlookup_get_config($engine) {
109   // TODO: discuss if mysql and http lookup should be implemented in dialplan or in an external AGI
110   global $ext;  // is this the best way to pass this?
111   global $asterisk_conf;
112   switch($engine) {
113     case "asterisk":
114       $sources = cidlookup_list();
115       if(is_array($sources)) {
116         foreach($sources as $item) {
117
118           // Search for number in the cache, if found lookupcidnum and return
119           if ($item['cidlookup_id'] != 0) {
120             if ($item['cache'] == 1 && $item['sourcetype'] != 'internal') {
121               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_gotoif('$[${DB_EXISTS(cidname/${CALLERID(num)})} = 1]', 'cidlookup,cidlookup_return,1'));
122             }
123           }
124
125           switch($item['sourcetype']) {
126
127             case "internal":
128               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_lookupcidname(''));
129             break;
130
131             case "enum":
132               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_txtcidname('${CALLERID(num)}'));
133               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_setvar('CALLERID(name)', '${TXTCIDNAME}'));
134             break;
135
136             case "http":
137               if (!empty($item['http_username']) && !empty($item['http_password']))
138                 $auth = sprintf('%s:%s@', $item['http_username'], $item['http_password']);
139               else
140                 $auth = '';
141                
142               if (!empty($item['http_port']))
143                 $host = sprintf('%s:%d', $item['http_host'], $item['http_port']);
144               else
145                 $host = $item['http_host'].':80';
146
147               if (substr($item['http_path'], 0, 1) == '/')
148                 $path = substr($item['http_path'], 1);
149               else
150                 $path = $item['http_path'];
151                
152               $query = str_replace('[NUMBER]', '${CALLERID(num)}', $item['http_query']);
153               $url = sprintf('http://%s%s/%s?%s', $auth, $host, $path, $query);
154               $curl = sprintf('${CURL(%s)}', $url);
155              
156               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_setvar('CALLERID(name)', $curl));
157             break;
158
159             case "mysql":
160               //Escaping MySQL query - thanks to http://www.asteriskgui.com/index.php?get=utilities-mysqlscape
161
162               $replacements = array (
163                   '\\' => '\\\\',
164                   '"' => '\\"',
165                   '\'' => '\\\'',
166                   ' ' => '\\ ',
167                   ',' => '\\,',
168                   '(' => '\\(',
169                   ')' => '\\)',
170                   '.' => '\\.',
171                   '|' => '\\|'
172               );
173              
174               $query = str_replace(array_keys($replacements), array_values($replacements), $item['mysql_query']);
175               $query = str_replace('[NUMBER]', '${CALLERID(num)}', $query);
176
177               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_connect('connid', $item['mysql_host'],  $item['mysql_username'],  $item['mysql_password'],  $item['mysql_dbname']));             
178               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_query('resultid', 'connid', $query));
179               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_fetch('fetchid', 'resultid', 'CALLERID(name)'));
180               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_clear('resultid'));             
181               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_mysql_disconnect('connid'));
182             break;
183
184             // TODO: implement SugarCRM lookup, look at code snippet at http://nerdvittles.com/index.php?p=82
185             case "sugarcrm":
186               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_noop('SugarCRM not yet implemented'));
187               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_return(''));
188             break;
189           }
190
191           // Put numbers in the cache
192           if ($item['cidlookup_id'] != 0) {
193             if ($item['cache'] == 1 && $item['sourcetype'] != 'internal') {
194               $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_db_put('cidname', '${CALLERID(num)}', '${CALLERID(name)}' ));
195             }
196             $ext->add('cidlookup', 'cidlookup_'.$item['cidlookup_id'], '', new ext_return(''));
197           }
198         }
199
200         $ext->add('cidlookup', 'cidlookup_return', '', new ext_lookupcidname(''));
201         $ext->add('cidlookup', 'cidlookup_return', '', new ext_return(''));
202       }
203     break;
204   }
205 }
206
207
208 function cidlookup_did_get($did){
209   $extarray = explode('/', $did, 2);
210   if(count($extarray) == 2) { // differentiate beetween '//' (Any did / any cid and '' empty string)
211     $sql = sprintf("SELECT cidlookup_id FROM cidlookup_incoming WHERE extension = '%s' AND cidnum = '%s'", $extarray[0], $extarray[1]);
212     $result = sql($sql, "getRow", DB_FETCHMODE_ASSOC);
213     if(is_array($result)){
214       return $result['cidlookup_id'];
215     } else
216       return null;
217   } else { // $did is an empty string (for example when adding a new did)
218     return 0;
219   }
220 }
221
222 function cidlookup_did_list() {
223   $results = sql("SELECT * FROM cidlookup_incoming","getAll",DB_FETCHMODE_ASSOC);
224   return is_array($results)?$results:null;
225 }
226
227 function cidlookup_list() {
228   // TODO: discuss department isolation of sources
229   $allowed = array(array('cidlookup_id' => 0, 'description' => _("None"), 'sourcetype' => null));
230   $results = sql("SELECT * FROM cidlookup","getAll",DB_FETCHMODE_ASSOC);
231   if(is_array($results)){
232     foreach($results as $result){
233       // check to see if we have a dept match for the current AMP User.
234       if (checkDept($result['deptname'])){
235         // return this item
236         $allowed[] = $result;
237       }
238     }
239   }
240   return isset($allowed)?$allowed:null;
241 }
242
243 function cidlookup_get($id){
244   $results = sql("SELECT * FROM cidlookup WHERE cidlookup_id = '$id'","getRow",DB_FETCHMODE_ASSOC);
245   return isset($results)?$results:null;
246 }
247
248 function cidlookup_del($id){
249   // Deleting source and its associations
250   $results = sql("DELETE FROM cidlookup WHERE cidlookup_id = '$id'","query");
251   $results = sql("DELETE FROM cidlookup_incoming WHERE cidlookup_id = '$id'","query");
252 }
253
254 function cidlookup_add($post){
255   if(!cidlookup_chk($post))
256     return false;
257   extract($post);
258   if (!isset($cache))
259     $cache = 0;
260   $results = sql("
261     INSERT INTO cidlookup
262       (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)
263     VALUES
264       (\"$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\")
265     ");
266 }
267
268 function cidlookup_edit($id,$post){
269   if(!cidlookup_chk($post))
270     return false;
271   extract($post);
272   if ($cache != 1)
273     $cache = 0;
274   $results = sql("
275     UPDATE cidlookup
276     SET
277       description = \"$description\",
278       deptname = \"$deptname\",
279       sourcetype = \"$sourcetype\" ,
280       cache = \"$cache\",
281       http_host = \"$http_host\",
282       http_port = \"$http_port\",
283       http_username = \"$http_username\",
284       http_password = \"$http_password\",
285       http_path = \"$http_path\",
286       http_query = \"$http_query\",
287       mysql_host = \"$mysql_host\",
288       mysql_dbname = \"$mysql_dbname\",
289       mysql_query = \"$mysql_query\",
290       mysql_username = \"$mysql_username\",
291       mysql_password  = \"$mysql_password\"
292     WHERE cidlookup_id = \"$id\"");
293 }
294
295 // ensures post vars is valid
296 function cidlookup_chk($post){
297   // TODO: Add sanity checks on $_POST
298   return true;
299 }
300 ?>
Note: See TracBrowser for help on using the browser.