root/contributed_modules/modules/ldapcidlookup/functions.inc.php

Revision 5062, 12.3 kB (checked in by p_lindheimer, 5 years ago)

#2389 add ldapcidlookup to contributed module repository

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