root/modules/branches/2.9/blacklist/functions.inc.php

Revision 11765, 10.3 kB (checked in by mickecarlsson, 2 years ago)

Close #4922, fix multiple issues with blacklist module

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 <?php /* $Id */
2 //This file is part of FreePBX.
3 //
4 //    FreePBX is free software: you can redistribute it and/or modify
5 //    it under the terms of the GNU General Public License as published by
6 //    the Free Software Foundation, either version 2 of the License, or
7 //    (at your option) any later version.
8 //
9 //    FreePBX 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 //    You should have received a copy of the GNU General Public License
15 //    along with FreePBX.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 //  Copyright (C) 2006 Magnus Ullberg (magnus@ullberg.us)
18 //  Portions Copyright (C) 2010 Mikael Carlsson (mickecamino@gmail.com)
19 //
20
21
22 function blacklist_get_config($engine) {
23   global $ext;
24   global $version;
25   global $astman;
26
27   switch($engine) {
28     case "asterisk":
29
30       $id = "app-blacklist";
31       $ext->addInclude('from-internal-additional', $id); // Add the include from from-internal
32
33       $id = "app-blacklist-check";
34       $c = "s";
35       // LookupBlackList doesn't seem to match empty astdb entry for "blacklist/", so we
36       // need to check for the setting and if set, send to the blacklisted area
37       // The gotoif below is not a typo.  For some reason, we've seen the CID number set to Unknown or Unavailable
38       // don't generate the dialplan if they are not using the function
39       //
40       if ($astman->database_get("blacklist","blocked") == '1') {
41         $ext->add($id, $c, '', new ext_gotoif('$["${CALLERID(number)}" = "Unknown"]','check-blocked'));
42         $ext->add($id, $c, '', new ext_gotoif('$["${CALLERID(number)}" = "Unavailable"]','check-blocked'));
43         $ext->add($id, $c, '', new ext_gotoif('$["foo${CALLERID(number)}" = "foo"]','check-blocked','check'));
44         $ext->add($id, $c, 'check-blocked', new ext_gotoif('$["${DB(blacklist/blocked)}" = "1"]','blacklisted'));
45       }
46      
47       if (version_compare($version, "1.6", "ge")) {
48         $ext->add($id, $c, 'check', new ext_gotoif('$["${BLACKLIST()}"="1"]', 'blacklisted'));
49       } else {
50         $ext->add($id, $c, 'check', new ext_lookupblacklist(''));
51         $ext->add($id, $c, '', new ext_gotoif('$["${LOOKUPBLSTATUS}"="FOUND"]', 'blacklisted'));
52       }
53           $ext->add($id, $c, '', new ext_setvar('CALLED_BLACKLIST','1'));
54       $ext->add($id, $c, '', new ext_return(''));
55       $ext->add($id, $c, 'blacklisted', new ext_answer(''));
56       $ext->add($id, $c, '', new ext_wait(1));
57       $ext->add($id, $c, '', new ext_zapateller(''));
58       $ext->add($id, $c, '', new ext_playback('ss-noservice'));
59       $ext->add($id, $c, '', new ext_hangup(''));
60
61       $modulename = 'blacklist';
62
63       if (is_array($featurelist = featurecodes_getModuleFeatures($modulename))) {
64         foreach($featurelist as $item) {
65           $featurename = $item['featurename'];
66           $fname = $modulename.'_'.$featurename;
67           if (function_exists($fname)) {
68             $fcc = new featurecode($modulename, $featurename);
69             $fc = $fcc->getCodeActive();
70             unset($fcc);
71
72             if ($fc != '') {
73               $fname($fc);
74             }
75           } else {
76             $ext->add('from-internal-additional', 'debug', '', new ext_noop($modulename.": No func $fname"));
77             var_dump($item);
78           }
79         }
80       }
81
82       break;
83   }
84 }
85
86 function blacklist_blacklist_add($fc) {
87   global $ext;
88
89   $ext->add('app-blacklist', $fc, '', new ext_goto('1', 's', 'app-blacklist-add'));
90
91   $id = "app-blacklist-add";
92   $c = "s";
93   $ext->add($id, $c, '', new ext_answer);
94   $ext->add($id, $c, '', new ext_wait(1));
95   $ext->add($id, $c, '', new ext_set('NumLoops', 0));
96   $ext->add($id, $c, 'start', new ext_playback('enter-num-blacklist'));
97   $ext->add($id, $c, '', new ext_digittimeout(5));
98   $ext->add($id, $c, '', new ext_responsetimeout(60));
99   $ext->add($id, $c, '', new ext_read('blacknr', 'then-press-pound'));
100   $ext->add($id, $c, '', new ext_saydigits('${blacknr}'));
101   $ext->add($id, $c, '', new ext_playback('if-correct-press&digits/1'));
102   $ext->add($id, $c, '', new ext_noop('Waiting for input'));
103   $ext->add($id, $c, 'end', new ext_waitexten(60));
104   $ext->add($id, $c, '', new ext_playback('sorry-youre-having-problems&goodbye'));
105   $c = "1";
106   $ext->add($id, $c, '', new ext_gotoif('$[ "${blacknr}" != ""]','','app-blacklist-add-invalid,s,1'));
107   $ext->add($id, $c, '', new ext_set('DB(blacklist/${blacknr})', 1));
108   $ext->add($id, $c, '', new ext_playback('num-was-successfully&added'));
109   $ext->add($id, $c, '', new ext_wait(1));
110   $ext->add($id, $c, '', new ext_hangup);
111
112   $id = "app-blacklist-add-invalid";
113   $c = "s";
114   $ext->add($id, $c, '', new ext_set('NumLoops','$[${NumLoops} + 1]'));
115   $ext->add($id, $c, '', new ext_playback('pm-invalid-option'));
116   $ext->add($id, $c, '', new ext_gotoif('$[${NumLoops} < 3]','app-blacklist-add,s,start'));
117   $ext->add($id, $c, '', new ext_playback('goodbye'));
118   $ext->add($id, $c, '', new ext_hangup);
119
120 }
121
122 function blacklist_blacklist_remove($fc) {
123   global $ext;
124
125   $ext->add('app-blacklist', $fc, '', new ext_goto('1', 's', 'app-blacklist-remove'));
126
127   $id = "app-blacklist-remove";
128   $c = "s";
129   $ext->add($id, $c, '', new ext_answer);
130   $ext->add($id, $c, '', new ext_wait(1));
131   $ext->add($id, $c, '', new ext_playback('entr-num-rmv-blklist'));
132   $ext->add($id, $c, '', new ext_digittimeout(5));
133   $ext->add($id, $c, '', new ext_responsetimeout(60));
134   $ext->add($id, $c, '', new ext_read('blacknr', 'then-press-pound'));
135   $ext->add($id, $c, '', new ext_saydigits('${blacknr}'));
136   $ext->add($id, $c, '', new ext_playback('if-correct-press&digits/1'));
137   $ext->add($id, $c, '', new ext_noop('Waiting for input'));
138   $ext->add($id, $c, 'end', new ext_waitexten(60));
139   $ext->add($id, $c, '', new ext_playback('sorry-youre-having-problems&goodbye'));
140   $c = "1";
141   $ext->add($id, $c, '', new ext_dbdel('blacklist/${blacknr}'));
142   $ext->add($id, $c, '', new ext_playback('num-was-successfully&removed'));
143   $ext->add($id, $c, '', new ext_wait(1));
144   $ext->add($id, $c, '', new ext_hangup);
145 }
146
147 function blacklist_blacklist_last($fc) {
148   global $ext;
149
150   $ext->add('app-blacklist', $fc, '', new ext_goto('1', 's', 'app-blacklist-last'));
151
152   $id = "app-blacklist-last";
153   $c = "s";
154   $ext->add($id, $c, '', new ext_answer);
155   $ext->add($id, $c, '', new ext_wait(1));
156   $ext->add($id, $c, '', new ext_setvar('lastcaller', '${DB(CALLTRACE/${CALLERID(number)})}'));
157   $ext->add($id, $c, '', new ext_gotoif('$[ $[ "${lastcaller}" = "" ] | $[ "${lastcaller}" = "unknown" ] ]', 'noinfo'));
158   $ext->add($id, $c, '', new ext_playback('privacy-to-blacklist-last-caller&telephone-number'));
159   $ext->add($id, $c, '', new ext_saydigits('${lastcaller}'));
160   $ext->add($id, $c, '', new ext_setvar('TIMEOUT(digit)', '3'));
161   $ext->add($id, $c, '', new ext_setvar('TIMEOUT(response)', '7'));
162   $ext->add($id, $c, '', new ext_playback('if-correct-press&digits/1'));
163   $ext->add($id, $c, '', new ext_goto('end'));
164   $ext->add($id, $c, 'noinfo', new ext_playback('unidentified-no-callback'));
165   $ext->add($id, $c, '', new ext_hangup);
166   $ext->add($id, $c, '', new ext_noop('Waiting for input'));
167   $ext->add($id, $c, 'end', new ext_waitexten(60));
168   $ext->add($id, $c, '', new ext_playback('sorry-youre-having-problems&goodbye'));
169   $c = "1";
170   $ext->add($id, $c, '', new ext_set('DB(blacklist/${lastcaller})', 1));
171   $ext->add($id, $c, '', new ext_playback('num-was-successfully'));
172   $ext->add($id, $c, '', new ext_playback('added'));
173   $ext->add($id, $c, '', new ext_wait(1));
174   $ext->add($id, $c, '', new ext_hangup);
175 }
176
177 function blacklist_hookGet_config($engine) {
178   global $ext;
179   switch($engine) {
180     case "asterisk":
181       // Code from modules/core/functions.inc.php core_get_config inbound routes
182       $didlist = core_did_list();
183       if (is_array($didlist)) {
184         foreach ($didlist as $item) {
185
186           $exten = trim($item['extension']);
187           $cidnum = trim($item['cidnum']);
188            
189           if ($cidnum != '' && $exten == '') {
190             $exten = 's';
191             $pricid = ($item['pricid']) ? true:false;
192           } else if (($cidnum != '' && $exten != '') || ($cidnum == '' && $exten == '')) {
193             $pricid = true;
194           } else {
195             $pricid = false;
196           }
197           $context = ($pricid) ? "ext-did-0001":"ext-did-0002";
198
199           $exten = (empty($exten)?"s":$exten);
200           $exten = $exten.(empty($cidnum)?"":"/".$cidnum); //if a CID num is defined, add it
201
202           $ext->splice($context, $exten, 1, new ext_gosub('1', 's', 'app-blacklist-check'));
203         }
204       } // else no DID's defined. Not even a catchall.
205       break;
206   }
207 }
208
209 function blacklist_list() {
210   global $amp_conf;
211   global $astman;
212
213 $ast_ge_16 =  version_compare($amp_conf['ASTVERSION'], "1.6", "ge");
214         if ($astman) {
215     $list = $astman->database_show('blacklist');
216     if($ast_ge_16) {
217         foreach ($list as $k => $v) {
218       $numbers = substr($k, 11);
219       $blacklisted[] = array('number' => $numbers, 'description' => $v);
220       }
221         if (isset($blacklisted) && is_array($blacklisted))
222       // Why this sorting? When used it does not yield the result I want
223       //    natsort($blacklisted);
224         return isset($blacklisted)?$blacklisted:null;
225     } else {
226         foreach ($list as $k => $v) {
227       $numbers[substr($k, 11)] = substr($k, 11);
228       }
229       if (isset($numbers) && is_array($numbers))
230           natcasesort($numbers);
231       return isset($numbers)?$numbers:null;
232       }
233         } else {
234                 fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
235         }
236 }
237
238 function blacklist_del($number){
239   global $amp_conf;
240   global $astman;
241   if ($astman) {
242     $astman->database_del("blacklist",$number);
243   } else {
244     fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
245   }
246 }
247
248 function blacklist_add($post){
249   global $amp_conf;
250   global $astman;
251
252 $ast_ge_16 =  version_compare($amp_conf['ASTVERSION'], "1.6", "ge");
253
254   if(!blacklist_chk($post))
255     return false;
256
257   extract($post);
258   if ($astman) {
259     if ($ast_ge_16) {
260     $post['description']==""?$post['description'] = '1':$post['description'];
261     $astman->database_put("blacklist",$post['number'], '"'.$post['description'].'"');
262         } else {
263               $astman->database_put("blacklist",$number, '1');
264               }
265     // Remove filtering for blocked/unknown cid
266     $astman->database_del("blacklist","blocked");
267     // Add it back if it's checked
268     if($post['blocked'] == "1")  {
269       $astman->database_put("blacklist","blocked", "1");
270       needreload();
271     }
272   } else {
273     fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
274   }
275 }
276
277
278 // ensures post vars is valid
279 function blacklist_chk($post){
280   return true;
281 }
282
283 ?>
Note: See TracBrowser for help on using the browser.