root/modules/branches/2.11/ringgroups/functions.inc.php

Revision 15512, 19.8 kB (checked in by p_lindheimer, 1 month ago)

proper fix re #6477 ... never grab values out of freepbx_settings table

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php /* $Id$ */
2 if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
3
4 // The destinations this module provides
5 // returns a associative arrays with keys 'destination' and 'description'
6 function ringgroups_destinations() {
7     //get the list of ringgroups
8     $results = ringgroups_list();
9     
10     // return an associative array with destination and description
11     if (isset($results)) {
12         foreach($results as $result){
13                 $extens[] = array('destination' => 'ext-group,'.$result['grpnum'].',1', 'description' => $result['description'].' <'.$result['grpnum'].'>');
14         }
15     }
16     
17     if (isset($extens))
18         return $extens;
19     else
20         return null;
21 }
22
23 function ringgroups_getdest($exten) {
24     return array("ext-group,$exten,1");
25 }
26
27 function ringgroups_getdestinfo($dest) {
28     if (substr(trim($dest),0,10) == 'ext-group,') {
29         $grp = explode(',',$dest);
30         $grp = $grp[1];
31         $thisgrp = ringgroups_get($grp);
32         if (empty($thisgrp)) {
33             return array();
34         } else {
35             return array('description' => sprintf(_("Ring Group %s: "),$grp).$thisgrp['description'],
36                          'edit_url' => 'config.php?display=ringgroups&extdisplay=GRP-'.urlencode($grp),
37                                   );
38         }
39     } else {
40         return false;
41     }
42 }
43
44 function ringgroups_recordings_usage($recording_id) {
45     global $active_modules;
46
47     $results = sql("SELECT `grpnum`, `description` FROM `ringgroups` WHERE `annmsg_id` = '$recording_id' OR `remotealert_id` = '$recording_id' OR `toolate_id` = '$recording_id'","getAll",DB_FETCHMODE_ASSOC);
48     if (empty($results)) {
49         return array();
50     } else {
51         //$type = isset($active_modules['ivr']['type'])?$active_modules['ivr']['type']:'setup';
52         foreach ($results as $result) {
53             $usage_arr[] = array(
54                 'url_query' => 'config.php?display=ringgroups&extdisplay=GRP-'.urlencode($result['grpnum']),
55                 'description' => sprintf(_("Ring Group: %s"),$result['description']),
56             );
57         }
58         return $usage_arr;
59     }
60 }
61
62 /*     Generates dialplan for ringgroups
63     We call this with retrieve_conf
64 */
65 function ringgroups_get_config($engine) {
66     global $ext// is this the best way to pass this?
67     switch($engine) {
68         case "asterisk":
69             $ext->addInclude('from-internal-additional','ext-group');
70             $ext->addInclude('from-internal-additional','grps');
71             $contextname = 'ext-group';
72             $ringlist = ringgroups_list(true);
73             if (is_array($ringlist)) {
74                 foreach($ringlist as $item) {
75                     $grpnum = ltrim($item['0']);
76                     $grp = ringgroups_get($grpnum);
77                     
78                     $strategy = $grp['strategy'];
79                     $grptime = $grp['grptime'];
80                     $grplist = $grp['grplist'];
81                     $postdest = $grp['postdest'];
82                     $grppre = (isset($grp['grppre'])?$grp['grppre']:'');
83                     $annmsg_id = (isset($grp['annmsg_id'])?$grp['annmsg_id']:'');
84                     $alertinfo = $grp['alertinfo'];
85                     $needsconf = $grp['needsconf'];
86                     $cwignore = $grp['cwignore'];
87                     $cpickup = $grp['cpickup'];
88                     $cfignore = $grp['cfignore'];
89                     $remotealert_id = $grp['remotealert_id'];
90                     $toolate_id = $grp['toolate_id'];
91                     $ringing = $grp['ringing'];
92                     $recording = $grp['recording'] == '' ? 'dontcare' : $grp['recording'];
93
94                     // TODO: this looks potentially problematic given the new per-user DIAL_OPTIONS. Need to further
95                     //       evaluate/understand if there are implications. The issue may be that we are trying to
96                     //       avoid getting a 'polluted' version of the DIAL_OPTIONS in which case we may need to modify
97                     //       macro-user-callerid (where it is set) to preserve the version we should be using.
98                     //
99                     if($ringing == 'Ring' || empty($ringing) ) {
100                         $dialopts = '${DIAL_OPTIONS}';
101                     } else {
102                         $dialopts = "m(${ringing})".str_replace('r', '', $amp_conf['DIAL_OPTIONS']);
103                     }
104                         
105
106                     $ext->add($contextname, $grpnum, '', new ext_macro('user-callerid'));
107
108                     // block voicemail until phone is answered at which point a macro should be called on the answering
109                     // line to clear this flag so that subsequent transfers can occur, if already set by a the caller
110                     // then don't change.
111                     //
112                     $ext->add($contextname, $grpnum, '', new ext_macro('blkvm-setifempty'));
113                     $ext->add($contextname, $grpnum, '', new ext_gotoif('$["${GOSUB_RETVAL}" = "TRUE"]', 'skipov'));
114                     $ext->add($contextname, $grpnum, '', new ext_macro('blkvm-set','reset'));
115                     $ext->add($contextname, $grpnum, '', new ext_setvar('__NODEST', ''));
116
117                     // Remember if NODEST was set later, but clear it in case the call is answered so that subsequent
118                     // transfers work.
119                     //
120                     $ext->add($contextname, $grpnum, 'skipov', new ext_setvar('RRNODEST', '${NODEST}'));
121                     $ext->add($contextname, $grpnum, 'skipvmblk', new ext_setvar('__NODEST', '${EXTEN}'));
122
123                     $ext->add($contextname, $grpnum, '', new ext_gosubif('$[${DB_EXISTS(RINGGROUP/'.$grpnum.'/changecid)} = 1 & "${DB(RINGGROUP/'.$grpnum.'/changecid)}" != "default" & "${DB(RINGGROUP/'.$grpnum.'/changecid)}" != ""]', 'sub-rgsetcid,s,1'));
124                     
125                     // deal with group CID prefix
126                     if ($grppre != '') {
127                         $ext->add($contextname, $grpnum, '', new ext_macro('prepend-cid', $grppre));
128                     }
129                     
130                     // Set Alert_Info
131                     if ($alertinfo != '') {
132                         $ext->add($contextname, $grpnum, '', new ext_setvar('__ALERT_INFO', str_replace(';', '\;', $alertinfo)));
133                     }
134                     if ($cwignore != '') {
135                          $ext->add($contextname, $grpnum, '', new ext_setvar('__CWIGNORE', 'TRUE'));
136                     }
137                     if ($cfignore != '') {
138                          $ext->add($contextname, $grpnum, '', new ext_setvar('_CFIGNORE', 'TRUE'));
139                          $ext->add($contextname, $grpnum, '', new ext_setvar('_FORWARD_CONTEXT', 'block-cf'));
140                     }
141                     if ($cpickup != '') {
142                       $ext->add($contextname, $grpnum, '', new ext_set('__PICKUPMARK','${EXTEN}'));
143                     }
144
145                     // recording stuff
146                     //$ext->add($contextname, $grpnum, '', new ext_setvar('RecordMethod','Group'));
147                     //$ext->add($contextname, $grpnum, '', new ext_macro('record-enable',$grplist.',${RecordMethod}'));
148
149           //TODO: hardcoded needs to be configurable in the ringgroup
150           $ext->add($contextname, $grpnum, '', new ext_gosub('1','s','sub-record-check',"rg,$grpnum,$recording"));
151
152                     // group dial
153                     $ext->add($contextname, $grpnum, '', new ext_setvar('RingGroupMethod',$strategy));
154                     if ($annmsg_id) {
155                         $annmsg = recordings_get_file($annmsg_id);
156                         $ext->add($contextname, $grpnum, '', new ext_gotoif('$["foo${RRNODEST}" != "foo"]','DIALGRP'));           
157                         $ext->add($contextname, $grpnum, '', new ext_answer(''));
158                         $ext->add($contextname, $grpnum, '', new ext_wait(1));
159                         $ext->add($contextname, $grpnum, '', new ext_playback($annmsg));
160                     }
161                     if ($needsconf == "CHECKED") {
162                         $remotealert = recordings_get_file($remotealert_id);
163                         $toolate = recordings_get_file($toolate_id);
164                         $len=strlen($grpnum)+4;
165                       $ext->add("grps", "_RG-${grpnum}-.", '', new ext_nocdr(''));
166                         $ext->add("grps", "_RG-${grpnum}-.", '', new ext_macro('dial', "$grptime,$dialopts" . "M(confirm^${remotealert}^${toolate}^${grpnum})" . ',${EXTEN:' . $len . '}'));
167                         $ext->add($contextname, $grpnum, 'DIALGRP', new ext_macro('dial-confirm',"$grptime,$dialopts,$grplist,$grpnum"));
168                     } else {
169                         $ext->add($contextname, $grpnum, 'DIALGRP', new ext_macro('dial',$grptime.",$dialopts,".$grplist));
170                     }
171           $ext->add($contextname, $grpnum, '', new ext_gosub('1','s','sub-record-cancel'));
172                     $ext->add($contextname, $grpnum, '', new ext_setvar('RingGroupMethod',''));
173
174
175                     // Now if we were told to skip the destination, do so now. Otherwise reset NODEST and proceed to our destination.
176                     //
177                     $ext->add($contextname, $grpnum, '', new ext_gotoif('$["foo${RRNODEST}" != "foo"]', 'nodest'));
178                     if ($cwignore != '') {
179                          $ext->add($contextname, $grpnum, '', new ext_setvar('__CWIGNORE', ''));
180                     }
181                     if ($cpickup != '') {
182                       $ext->add($contextname, $grpnum, '', new ext_set('__PICKUPMARK',''));
183                     }
184                     // TODO: Asterisk uses a blank FORWARD_CONTEXT as a literal at the time of this change. A better solution would be
185                     //       if it would ignore blank, since it is possible in a customcontext setup you would not want this set to
186                     //       from-internal
187                     //
188                     if ($cfignore != '') {
189                          $ext->add($contextname, $grpnum, '', new ext_setvar('_CFIGNORE', ''));
190                          $ext->add($contextname, $grpnum, '', new ext_setvar('_FORWARD_CONTEXT', 'from-internal'));
191                     }
192                     $ext->add($contextname, $grpnum, '', new ext_setvar('__NODEST', ''));
193                     $ext->add($contextname, $grpnum, '', new ext_macro('blkvm-clr'));
194
195                     // where next?
196                     if ((isset($postdest) ? $postdest : '') != '') {
197                         $ext->add($contextname, $grpnum, '', new ext_goto($postdest));
198                     } else {
199                         $ext->add($contextname, $grpnum, '', new ext_hangup(''));
200                     }
201                     $ext->add($contextname, $grpnum, 'nodest', new ext_noop('SKIPPING DEST, CALL CAME FROM Q/RG: ${RRNODEST}'));
202                 }
203                 // We need to have a hangup here, if call is ended by the caller during Playback it will end in the
204                 // h context and do a proper hangup and clean the blkvm keys if set, see #4671
205                 $ext->add($contextname, 'h', '', new ext_macro('hangupcall'));
206         /*
207           ASTDB Settings:
208           RINGGROUP/nnn/changecid default | did | fixed | extern
209           RINGGROUP/nnn/fixedcid XXXXXXXX
210
211           changecid:
212             default   - works as always, same as if not present
213             fixed     - set to the fixedcid
214             extern    - set to the fixedcid if the call is from the outside only
215             did       - set to the DID that the call came in on or leave alone, treated as foreign
216             forcedid  - set to the DID that the call came in on or leave alone, not treated as foreign
217           
218           NODEST      - has the exten num called, hoaky if that goes away but for now use it
219         */
220         if (count($ringlist)) {
221           $contextname = 'sub-rgsetcid';
222           $exten = 's';
223           $ext->add($contextname, $exten, '', new ext_goto('1','s-${DB(RINGGROUP/${NODEST}/changecid)}'));
224
225           $exten = 's-fixed';
226           $ext->add($contextname, $exten, '', new ext_execif('$["${REGEX("^[\+]?[0-9]+$" ${DB(RINGGROUP/${NODEST}/fixedcid)})}" = "1"]', 'Set', '__TRUNKCIDOVERRIDE=${DB(RINGGROUP/${NODEST}/fixedcid)}'));
227           $ext->add($contextname, $exten, '', new ext_return(''));
228
229           $exten = 's-extern';
230           $ext->add($contextname, $exten, '', new ext_execif('$["${REGEX("^[\+]?[0-9]+$" ${DB(RINGGROUP/${NODEST}/fixedcid)})}" == "1" & "${FROM_DID}" != ""]', 'Set', '__TRUNKCIDOVERRIDE=${DB(RINGGROUP/${NODEST}/fixedcid)}'));
231           $ext->add($contextname, $exten, '', new ext_return(''));
232
233           $exten = 's-did';
234           $ext->add($contextname, $exten, '', new ext_execif('$["${REGEX("^[\+]?[0-9]+$" ${FROM_DID})}" = "1"]', 'Set', '__REALCALLERIDNUM=${FROM_DID}'));
235           $ext->add($contextname, $exten, '', new ext_return(''));
236
237           $exten = 's-forcedid';
238           $ext->add($contextname, $exten, '', new ext_execif('$["${REGEX("^[\+]?[0-9]+$" ${FROM_DID})}" = "1"]', 'Set', '__TRUNKCIDOVERRIDE=${FROM_DID}'));
239           $ext->add($contextname, $exten, '', new ext_return(''));
240
241           $exten = '_s-.';
242           $ext->add($contextname, $exten, '', new ext_noop('Unknown value for RINGGROUP/${NODEST}/changecid of ${DB(RINGGROUP/${NODEST}/changecid)} set to "default"'));
243           $ext->add($contextname, $exten, '', new ext_setvar('DB(RINGGROUP/${NODEST}/changecid)', 'default'));
244           $ext->add($contextname, $exten, '', new ext_return(''));
245         }
246             }
247         break;
248     }
249 }
250
251 function ringgroups_add($grpnum,$strategy,$grptime,$grplist,$postdest,$desc,$grppre='',$annmsg_id='',$alertinfo,$needsconf,$remotealert_id,$toolate_id,$ringing,$cwignore,$cfignore,$changecid='default',$fixedcid='',$cpickup='', $recording='dontcare') {
252     global $db;
253     global $astman;
254
255     $extens = ringgroups_list();
256     if(is_array($extens)) {
257         foreach($extens as $exten) {
258             if ($exten[0]===$grpnum) {
259                 echo "<script>javascript:alert('"._("This ringgroup")." ({$grpnum}) "._("is already in use")."');</script>";
260                 return false;
261             }
262         }
263     }
264     print_r($extens);
265
266     $sql = "INSERT INTO ringgroups (grpnum, strategy, grptime, grppre, grplist, annmsg_id, postdest, description, alertinfo, needsconf, remotealert_id, toolate_id, ringing, cwignore, cfignore, cpickup, recording) VALUES ('".$db->escapeSimple($grpnum)."', '".$db->escapeSimple($strategy)."', ".$db->escapeSimple($grptime).", '".$db->escapeSimple($grppre)."', '".$db->escapeSimple($grplist)."', '".$annmsg_id."', '".$db->escapeSimple($postdest)."', '".$db->escapeSimple($desc)."', '".$db->escapeSimple($alertinfo)."', '$needsconf', '$remotealert_id', '$toolate_id', '$ringing', '$cwignore', '$cfignore', '$cpickup', '$recording')";
267     $results = sql($sql);
268
269   // from followme, put these in astdb, should migrate more settings to astdb from sql so that user portal control can be
270   // added. So consider this a start.
271     if ($astman) {
272         $astman->database_put("RINGGROUP",$grpnum."/changecid",$changecid);
273       $fixedcid = preg_replace("/[^0-9\+]/" ,"", trim($fixedcid));
274         $astman->database_put("RINGGROUP",$grpnum."/fixedcid",$fixedcid);
275     } else {
276         die_freepbx("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
277     }
278     return true;
279 }
280
281 function ringgroups_del($grpnum) {
282     global $db;
283     global $astman;
284
285     $results = sql("DELETE FROM ringgroups WHERE grpnum = '".$db->escapeSimple($grpnum)."'","query");
286     if ($astman) {
287         $astman->database_deltree("RINGGROUP/".$grpnum);
288     } else {
289         die_freepbx("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
290     }
291 }
292
293 function ringgroups_list($get_all=false) {
294     $results = sql("SELECT grpnum, description FROM ringgroups ORDER BY CAST(grpnum as UNSIGNED)","getAll",DB_FETCHMODE_ASSOC);
295     foreach ($results as $result) {
296         if ($get_all || (isset($result['grpnum']) && checkRange($result['grpnum']))) {
297             $grps[] = array(
298                 0 => $result['grpnum'],
299                 1 => $result['description'],
300                 'grpnum' => $result['grpnum'],
301                 'description' => $result['description'],
302             );
303         }
304     }
305     if (isset($grps))
306         return $grps;
307     else
308         return array();
309 }
310
311 function ringgroups_check_extensions($exten=true) {
312     $extenlist = array();
313     if (is_array($exten) && empty($exten)) {
314         return $extenlist;
315     }
316     $sql = "SELECT grpnum ,description FROM ringgroups ";
317     if (is_array($exten)) {
318         $sql .= "WHERE grpnum in ('".implode("','",$exten)."')";
319     }
320     $sql .= " ORDER BY CAST(grpnum AS UNSIGNED)";
321     $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
322
323     foreach ($results as $result) {
324         $thisexten = $result['grpnum'];
325         $extenlist[$thisexten]['description'] = sprintf(_("Ring Group: %s"),$result['description']);
326         $extenlist[$thisexten]['status'] = _('INUSE');
327         $extenlist[$thisexten]['edit_url'] = 'config.php?display=ringgroups&extdisplay=GRP-'.urlencode($thisexten);
328     }
329     return $extenlist;
330 }
331
332 function ringgroups_check_destinations($dest=true) {
333     global $active_modules;
334
335     $destlist = array();
336     if (is_array($dest) && empty($dest)) {
337         return $destlist;
338     }
339     $sql = "SELECT grpnum, postdest, description FROM ringgroups ";
340     if ($dest !== true) {
341         $sql .= "WHERE postdest in ('".implode("','",$dest)."')";
342     }
343     $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
344
345     //$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup';
346
347     foreach ($results as $result) {
348         $thisdest = $result['postdest'];
349         $thisid   = $result['grpnum'];
350         $destlist[] = array(
351             'dest' => $thisdest,
352             'description' => sprintf(_("Ring Group: %s (%s)"),$result['description'],$thisid),
353             'edit_url' => 'config.php?display=ringgroups&extdisplay=GRP-'.urlencode($thisid),
354         );
355     }
356     return $destlist;
357 }
358
359 function ringgroups_change_destination($old_dest, $new_dest) {
360     $sql = 'UPDATE ringgroups SET postdest = "' . $new_dest . '" WHERE postdest = "' . $old_dest . '"';
361     sql($sql, "query");
362 }
363
364 function ringgroups_get($grpnum) {
365     global $db;
366     global $astman;
367
368     $results = sql("SELECT grpnum, strategy, grptime, grppre, grplist, annmsg_id, postdest, description, alertinfo, needsconf, remotealert_id, toolate_id, ringing, cwignore, cfignore, cpickup, recording FROM ringgroups WHERE grpnum = '".$db->escapeSimple($grpnum)."'","getRow",DB_FETCHMODE_ASSOC);
369   if ($astman) {
370     $astdb_changecid = strtolower($astman->database_get("RINGGROUP",$grpnum."/changecid"));
371     switch($astdb_changecid) {
372       case 'default':
373       case 'did':
374       case 'forcedid':
375       case 'fixed':
376       case 'extern':
377         break;
378       default:
379         $astdb_changecid = 'default';
380     }
381     $results['changecid'] = $astdb_changecid;
382     $fixedcid = $astman->database_get("RINGGROUP",$grpnum."/fixedcid");
383     $results['fixedcid'] = preg_replace("/[^0-9\+]/" ,"", trim($fixedcid));
384     } else {
385         die_freepbx("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
386     }
387     return $results;
388 }
389 /* Get a list of all extensions that belongs to a ringgroup */
390 function ringgroups_get_extensions($grpnum) {
391         global $db;
392         $results = sql("SELECT grplist FROM ringgroups WHERE grpnum = '".$db->escapeSimple($grpnum)."'","getRow",DB_FETCHMODE_ASSOC);
393         return $results;
394 }
395
396 /* Update the list of extensions for the specific ringgroup */
397 function ringgroups_update_extensions($grpnum, $extensions) {
398         global $db;
399         $sql = 'UPDATE ringgroups SET grplist = "' . $extensions . '" WHERE grpnum = "' . $grpnum . '"';
400         sql($sql, "query");
401 }
402
403 function ringgroups_hook_core($viewing_itemid, $request) {
404 // This is empty. Need to be here for the ringgroups_hookProcess_core function to work
405 }
406
407 function ringgroups_hookProcess_core($viewing_itemid, $request) {
408
409         if (!isset($request['action']))
410                 return;
411         switch ($request['action']) {
412             case 'del':
413                 // Get all ringgroups
414                 $grouplist = ringgroups_list();
415                 if(isset($grouplist)) {
416                     foreach($grouplist as $list => $group){
417                         // Get the extension list for the ringgroup
418                         $extensionlist = ringgroups_get_extensions($group['grpnum']);
419                         $extensions = explode('-', $extensionlist['grplist']);
420                         $key = array_search($viewing_itemid, $extensions);
421                         if($key !== FALSE) {
422                             unset($extensions[$key]);
423                             $new_grplist = implode('-',$extensions);
424                             ringgroups_update_extensions($group['grpnum'], $new_grplist);
425                         }
426                     }
427                 }
428             break;
429         }
430 }
431
432 if ($amp_conf['EXTENSION_LIST_RINGGROUPS']) {
433
434     function ringgroups_configpageinit($pagename) {
435         global $currentcomponent;
436         // On a 'new' user, 'tech_hardware' is set, and there's no extension.
437         if (($_REQUEST['display'] == 'users'||$_REQUEST['display'] == 'extensions')&& isset($_REQUEST['extdisplay']) && $_REQUEST['extdisplay'] != '') {
438         $currentcomponent->addprocessfunc('ringgroups_configpageload', 1);
439         }
440     }
441
442     // This is called before the page is actually displayed, so we can use addguielem(). draws hook on the extensions/users page
443     function ringgroups_configpageload() {
444         global $currentcomponent;
445         global $display;
446         $extdisplay=isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:'';
447     
448         if ($display == 'extensions' || $display == 'users') {
449             $section = _('Ring Group Membership');
450         
451             $sql = "SELECT grpnum, description FROM ringgroups WHERE grplist LIKE '$extdisplay-%' OR grplist LIKE '%-$extdisplay-%' OR grplist LIKE '%-$extdisplay' OR grplist = '$extdisplay'";
452             $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
453         
454             $ringgroup_count = 0;
455             foreach($results as $result) {
456                 $addURL = $_SERVER['PHP_SELF'].'?display=ringgroups&extdisplay='.$result['grpnum'];
457                 $ringgroup_icon = 'images/email_edit.png';
458                 $ringgroup_label = $result['grpnum']." ".$result['description'];
459                 $ringgroup_label = '&nbsp;<span>
460                     <img width="16" height="16" border="0" title="'.$ringgroup_label.'" alt="" src="'.$ringgroup_icon.'"/> '.$ringgroup_label.
461                 '</span> ';
462                 $currentcomponent->addguielem($section, new gui_link('ringgroup_'.$ringgroup_count++, $ringgroup_label, $addURL, true, false), 9);
463             }
464         }
465     }
466
467 } // only included if feature enabled
468 ?>
469
Note: See TracBrowser for help on using the browser.