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

Revision 4901, 11.3 kB (checked in by p_lindheimer, 5 years ago)

#2172 replace deprecated use of | with ,

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