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

Revision 13091, 13.4 kB (checked in by p_lindheimer, 1 year ago)

adds FREEPBX_IS_AUTH checking to most module files re #5478

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