root/modules/branches/2.4/findmefollow/functions.inc.php

Revision 6293, 17.2 kB (checked in by p_lindheimer, 4 years ago)

revert r6292 checked in by error

  • Property svn:mime-type set to text/html
  • Property svn:eol-style set to native
Line 
1 <?php /* $Id: functions.inc.php 175 2006-10-03 19:12:39Z plindheimer $ */
2
3 /*  Generates dialplan for findmefollow
4   We call this with retrieve_conf
5 */
6 function findmefollow_get_config($engine) {
7   global $ext;  // is this the best way to pass this?
8   switch($engine) {
9     case "asterisk":
10       $ext->addInclude('from-internal-additional','ext-findmefollow');
11       $ext->addInclude('from-internal-additional','fmgrps');
12       $contextname = 'ext-findmefollow';
13       $ringlist = findmefollow_full_list();
14       if (is_array($ringlist)) {
15         foreach($ringlist as $item) {
16           $grpnum = ltrim($item['0']);
17           $grp = findmefollow_get($grpnum);
18           
19           $strategy = $grp['strategy'];
20           $grptime = $grp['grptime'];
21           $grplist = $grp['grplist'];
22           $postdest = $grp['postdest'];
23           $grppre = (isset($grp['grppre'])?$grp['grppre']:'');
24           $annmsg = $grp['annmsg'];
25           $dring = $grp['dring'];
26
27           $needsconf = $grp['needsconf'];
28           $remotealert = $grp['remotealert'];
29           $toolate = $grp['toolate'];
30           $ringing = $grp['ringing'];
31           $pre_ring = $grp['pre_ring'];
32
33           if($ringing == 'Ring' || empty($ringing) ) {
34             $dialopts = '${DIAL_OPTIONS}';
35           } else {
36             // We need the DIAL_OPTIONS variable
37             $sops = sql("SELECT value from globals where variable='DIAL_OPTIONS'", "getRow");
38             $dialopts = "m(${ringing})".str_replace('r', '', $sops[0]);
39           }
40
41
42           // Direct target to Follow-Me come here bypassing the followme/ddial conditional check
43           //
44           $ext->add($contextname, 'FM'.$grpnum, '', new ext_goto("$grpnum,FM$grpnum"));
45
46           //
47           // If the followme is configured for extension dialing to go to the the extension and not followme then
48           // go there. This is often used in VmX Locater functionality when the user does not want the followme
49           // to automatically be called but only if chosen by the caller as an alternative to going to voicemail
50           //
51           $ext->add($contextname, $grpnum, '', new ext_gotoif('$[ "${DB(AMPUSER/'.$grpnum.'/followme/ddial)}" = "EXTENSION" ]', 'ext-local,'.$grpnum.',1'));
52           $ext->add($contextname, $grpnum, 'FM'.$grpnum, new ext_macro('user-callerid'));
53
54           // block voicemail until phone is answered at which point a macro should be called on the answering
55           // line to clear this flag so that subsequent transfers can occur, if already set by a the caller
56           // then don't change.
57           //
58           $ext->add($contextname, $grpnum, '', new ext_gotoif('$["foo${BLKVM_OVERRIDE}" = "foo"]', 'skipdb'));
59           $ext->add($contextname, $grpnum, '', new ext_gotoif('$["${DB(${BLKVM_OVERRIDE})}" = "TRUE"]', 'skipov'));
60
61           $ext->add($contextname, $grpnum, 'skipdb', new ext_setvar('__NODEST', ''));
62           $ext->add($contextname, $grpnum, '', new ext_setvar('__BLKVM_OVERRIDE', 'BLKVM/${EXTEN}/${CHANNEL}'));
63           $ext->add($contextname, $grpnum, '', new ext_setvar('__BLKVM_BASE', '${EXTEN}'));
64           $ext->add($contextname, $grpnum, '', new ext_setvar('DB(${BLKVM_OVERRIDE})', 'TRUE'));
65
66           // Remember if NODEST was set later, but clear it in case the call is answered so that subsequent
67           // transfers work.
68           //
69           $ext->add($contextname, $grpnum, 'skipov', new ext_setvar('RRNODEST', '${NODEST}'));
70           $ext->add($contextname, $grpnum, 'skipvmblk', new ext_setvar('__NODEST', '${EXTEN}'));
71
72           // deal with group CID prefix
73           // but strip only if you plan on setting a new one
74           if ($grppre != '') {
75             $ext->add($contextname, $grpnum, '', new ext_gotoif('$["foo${RGPREFIX}" = "foo"]', 'REPCID'));
76             $ext->add($contextname, $grpnum, '', new ext_gotoif('$["${RGPREFIX}" != "${CALLERID(name):0:${LEN(${RGPREFIX})}}"]', 'REPCID'));
77             $ext->add($contextname, $grpnum, '', new ext_noop('Current RGPREFIX is ${RGPREFIX}....stripping from Caller ID'));
78             $ext->add($contextname, $grpnum, '', new ext_setvar('CALLERID(name)', '${CALLERID(name):${LEN(${RGPREFIX})}}'));
79             $ext->add($contextname, $grpnum, '', new ext_setvar('_RGPREFIX', ''));
80             $ext->add($contextname, $grpnum, 'REPCID', new ext_noop('CALLERID(name) is ${CALLERID(name)}'));
81             $ext->add($contextname, $grpnum, '', new ext_setvar('_RGPREFIX', $grppre));
82             $ext->add($contextname, $grpnum, '', new ext_setvar('CALLERID(name)','${RGPREFIX}${CALLERID(name)}'));
83           }
84
85           // MODIFIED (PL)
86           // Add Alert Info if set but don't override and already set value (could be from ringgroup, directdid, etc.)
87           //
88           if ((isset($dring) ? $dring : '') != '') {
89             // If ALERTINFO is set, then skip to the next set command. This was modified to two lines because the previous
90             // IF() couldn't handle ':' as part of the string. The jump to PRIORITY+2 allows for now destination label
91             // which is needed in the 2.3 version.
92             $ext->add($contextname, $grpnum, '', new ext_gotoif('$["x${ALERT_INFO}"!="x"]','$[${PRIORITY}+2])}'));
93             $ext->add($contextname, $grpnum, '', new ext_setvar("__ALERT_INFO", str_replace(';', '\;', $dring) ));
94           }
95           // If pre_ring is set, then ring this number of seconds prior to moving on
96           if ((isset($strategy) ? substr($strategy,0,strlen('ringallv2')) : '') != 'ringallv2') {
97             $ext->add($contextname, $grpnum, '', new ext_gotoif('$[$[ "${DB(AMPUSER/'.$grpnum.'/followme/prering)}" = "0" ] | $[ "${DB(AMPUSER/'.$grpnum.'/followme/prering)}" = "" ]] ', 'skipsimple'));
98             $ext->add($contextname, $grpnum, '', new ext_macro('simple-dial',$grpnum.',${DB(AMPUSER/'."$grpnum/followme/prering)}"));
99           }
100
101           // recording stuff
102           $ext->add($contextname, $grpnum, 'skipsimple', new ext_setvar('RecordMethod','Group'));
103           $ext->add($contextname, $grpnum, '', new ext_macro('record-enable','${DB(AMPUSER/'."$grpnum/followme/grplist)}".',${RecordMethod}'));
104
105           // group dial
106           $ext->add($contextname, $grpnum, '', new ext_setvar('RingGroupMethod',$strategy));
107           $ext->add($contextname, $grpnum, '', new ext_setvar('_FMGRP',$grpnum));
108           if ((isset($annmsg) ? $annmsg : '') != '') {
109             // should always answer before playing anything, shouldn't we ?
110             $ext->add($contextname, $grpnum, '', new ext_gotoif('$[$["${DIALSTATUS}" = "ANSWER"] | $["foo${RRNODEST}" != "foo"]]','DIALGRP'));     
111             $ext->add($contextname, $grpnum, '', new ext_answer(''));
112             $ext->add($contextname, $grpnum, '', new ext_wait(1));
113             $ext->add($contextname, $grpnum, '', new ext_playback($annmsg));
114           }
115
116           // Create the confirm target
117           $len=strlen($grpnum)+4;
118           $ext->add("fmgrps", "_RG-${grpnum}-.", '', new ext_macro('dial','${DB(AMPUSER/'."$grpnum/followme/grptime)},".
119             "M(confirm^${remotealert}^${toolate}^${grpnum})$dialopts".',${EXTEN:'.$len.'}'));
120
121           // If grpconf == ENABLED call with confirmation ELSE call normal
122           $ext->add($contextname, $grpnum, 'DIALGRP', new
123               ext_gotoif('$[ "${DB(AMPUSER/'.$grpnum.'/followme/grpconf)}" = "ENABLED" ]', 'doconfirm'));
124
125           // Normal call
126           if ((isset($strategy) ? substr($strategy,0,strlen('ringallv2')) : '') != 'ringallv2') {
127             $ext->add($contextname, $grpnum, '', new ext_macro('dial','${DB(AMPUSER/'."$grpnum/followme/grptime)},$dialopts,".'${DB(AMPUSER/'."$grpnum/followme/grplist)}"));
128           } else {
129             $ext->add($contextname, $grpnum, '', new ext_macro('dial','$[ ${DB(AMPUSER/'.$grpnum.'/followme/grptime)} + ${DB(AMPUSER/'.$grpnum.'/followme/prering)} ],'.$dialopts.',${DB(AMPUSER/'.$grpnum.'/followme/grplist)}'));
130           }
131           $ext->add($contextname, $grpnum, '', new ext_goto('nextstep'));
132
133           // Call Confirm call
134           if ((isset($strategy) ? substr($strategy,0,strlen('ringallv2')) : '') != 'ringallv2') {
135             $ext->add($contextname, $grpnum, 'doconfirm', new ext_macro('dial-confirm','${DB(AMPUSER/'."$grpnum/followme/grptime)},$dialopts,".'${DB(AMPUSER/'."$grpnum/followme/grplist)},".$grpnum));
136           } else {
137             $ext->add($contextname, $grpnum, 'doconfirm', new ext_macro('dial-confirm','$[ ${DB(AMPUSER/'.$grpnum.'/followme/grptime)} + ${DB(AMPUSER/'.$grpnum.'/followme/prering)} ],'.$dialopts.',${DB(AMPUSER/'.$grpnum.'/followme/grplist)},'.$grpnum));
138           }
139
140           $ext->add($contextname, $grpnum, 'nextstep', new ext_setvar('RingGroupMethod',''));
141
142           // Did the call come from a queue or ringgroup, if so, don't go to the destination, just end and let
143           // the queue or ringgroup decide what to do next
144           //
145           $ext->add($contextname, $grpnum, '', new ext_gotoif('$["foo${RRNODEST}" != "foo"]', 'nodest'));
146           $ext->add($contextname, $grpnum, '', new ext_setvar('__NODEST', ''));
147
148           $ext->add($contextname, $grpnum, '', new ext_dbdel('${BLKVM_OVERRIDE}'));
149
150           // where next?
151           if ((isset($postdest) ? $postdest : '') != '') {
152             $ext->add($contextname, $grpnum, '', new ext_goto($postdest));
153           } else {
154             $ext->add($contextname, $grpnum, '', new ext_hangup(''));
155           }
156           $ext->add($contextname, $grpnum, 'nodest', new ext_noop('SKIPPING DEST, CALL CAME FROM Q/RG: ${RRNODEST}'));
157         }
158       }
159     break;
160   }
161 }
162
163 function findmefollow_add($grpnum,$strategy,$grptime,$grplist,$postdest,$grppre='',$annmsg='',$dring,$needsconf,$remotealert,$toolate,$ringing,$pre_ring,$ddial) {
164   global $amp_conf;
165   global $astman;
166
167   $sql = "INSERT INTO findmefollow (grpnum, strategy, grptime, grppre, grplist, annmsg, postdest, dring, needsconf, remotealert, toolate, ringing, pre_ring) VALUES ('".str_replace("'", "''",$grpnum)."', '".str_replace("'", "''", $strategy)."', ".str_replace("'", "''", $grptime).", '".str_replace("'", "''", $grppre)."', '".str_replace("'", "''", $grplist)."', '".str_replace("'", "''", $annmsg)."', '".str_replace("'", "''", $postdest)."', '".str_replace("'", "''", $dring)."', '$needsconf', '$remotealert', '$toolate', '$ringing', '$pre_ring')";
168   $results = sql($sql);
169
170   if ($astman) {
171     $astman->database_put("AMPUSER",$grpnum."/followme/prering",isset($pre_ring)?$pre_ring:'');
172     $astman->database_put("AMPUSER",$grpnum."/followme/grptime",isset($grptime)?$grptime:'');
173     $astman->database_put("AMPUSER",$grpnum."/followme/grplist",isset($grplist)?$grplist:'');
174
175     $needsconf = isset($needsconf)?$needsconf:'';
176     $confvalue = ($needsconf == 'CHECKED')?'ENABLED':'DISABLED';
177     $astman->database_put("AMPUSER",$grpnum."/followme/grpconf",$confvalue);
178
179     $ddial      = isset($ddial)?$ddial:'';
180     $ddialvalue = ($ddial == 'CHECKED')?'EXTENSION':'DIRECT';
181     $astman->database_put("AMPUSER",$grpnum."/followme/ddial",$ddialvalue);
182   } else {
183     fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
184   }
185 }
186
187 function findmefollow_del($grpnum) {
188   global $amp_conf;
189   global $astman;
190
191   $results = sql("DELETE FROM findmefollow WHERE grpnum = '".str_replace("'", "''", $grpnum)."'","query");
192
193   if ($astman) {
194     $astman->database_deltree("AMPUSER/".$grpnum."/followme");
195   } else {
196     fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
197   }
198 }
199
200 function findmefollow_full_list() {
201   $results = sql("SELECT grpnum FROM findmefollow ORDER BY CAST(grpnum as UNSIGNED)","getAll",DB_FETCHMODE_ASSOC);
202   foreach ($results as $result) {
203     if (isset($result['grpnum']) && checkRange($result['grpnum'])) {
204       $grps[] = array($result['grpnum']);
205     }
206   }
207   if (isset($grps))
208     return $grps;
209   else
210     return null;
211 }
212
213 function findmefollow_list() {
214
215         global $db;
216         $sql = "SELECT grpnum FROM findmefollow ORDER BY CAST(grpnum as UNSIGNED)";
217         $results = $db->getCol($sql);
218         if(DB::IsError($results)) {
219                 $results = null;
220         }
221         if (isset($results)) {
222           foreach($results as $result) {
223             if (checkRange($result)){
224               $grps[] = $result;
225             }
226           }
227         }
228         if (isset($grps)) {
229     sort($grps); // hmm, should be sorted already
230           return $grps;
231   }
232   else {
233     return null;
234   }
235 }
236
237 // This gets the list of all active users so that the Find Me Follow display can limit the options to only created users.
238 // the returned arrays contain [0]:extension [1]:name
239 //
240 // This was pulled straight out of previous 1.x version, might need cleanup laster
241 //
242 function findmefollow_allusers() {
243         global $db;
244         $sql = "SELECT extension,name FROM users ORDER BY extension";
245         $results = $db->getAll($sql);
246         if(DB::IsError($results)) {
247                 $results = null;
248         }
249         foreach($results as $result){
250                 if (checkRange($result[0])){
251                         $users[] = array($result[0],$result[1]);
252                 }
253         }
254         if (isset($users)) sort($users);
255         return $users;
256 }
257
258 // Only check astdb if check_astdb is not 0. For some reason, this fails if the asterisk manager code
259 // is included (executed) by all calls to this function. This results in silently not generating the
260 // extensions_additional.conf file. page.findmefollow.php does set it to 1 which means that when running
261 // the GUI, any changes not reflected in SQL will be detected and written back to SQL so that they are
262 // in sync. Ideally, anything that changes the astdb should change SQL. (in some ways, these should both
263 // not be here but ...
264 //
265 // Need to go back and confirm at some point that the $check_astdb error is still there and deal with it.
266 // as variables like $ddial get introduced to only be in astdb, the result array will not include them
267 // if not able to get to astdb. (I suspect in 2.2 and beyond this may all be fixed).
268 //
269 function findmefollow_get($grpnum, $check_astdb=0) {
270   global $amp_conf;
271   global $astman;
272
273   $results = sql("SELECT grpnum, strategy, grptime, grppre, grplist, annmsg, postdest, dring, needsconf, remotealert, toolate, ringing, pre_ring FROM findmefollow WHERE grpnum = '".str_replace("'", "''", $grpnum)."'","getRow",DB_FETCHMODE_ASSOC);
274
275   if ($check_astdb) {
276     if ($astman) {
277       $astdb_prering = $astman->database_get("AMPUSER",$grpnum."/followme/prering");
278       $astdb_grptime = $astman->database_get("AMPUSER",$grpnum."/followme/grptime");
279       $astdb_grplist = $astman->database_get("AMPUSER",$grpnum."/followme/grplist");
280       $astdb_grpconf = $astman->database_get("AMPUSER",$grpnum."/followme/grpconf");
281     } else {
282       fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
283     }
284       $astdb_ddial   = $astman->database_get("AMPUSER",$grpnum."/followme/ddial");                                     
285     // If the values are different then use what is in astdb as it may have been changed.
286     //
287     $changed=0;
288     if (($astdb_prering != $results['pre_ring']) && ($astdb_prering >= 0)) {
289       $results['pre_ring'] = $astdb_prering;
290       $changed=1;
291     }
292     if (($astdb_grptime != $results['grptime']) && ($astdb_grptime > 0)) {
293       $results['grptime'] = $astdb_grptime;
294       $changed=1;
295     }
296     if ((trim($astdb_grplist) != trim($results['grplist'])) && (trim($astdb_grplist) != '')) {
297       $results['grplist'] = $astdb_grplist;
298       $changed=1;
299     }
300
301     if (trim($astdb_grpconf) == 'ENABLED') {
302       $confvalue = 'CHECKED';
303     } elseif (trim($astdb_grpconf) == 'DISABLED') {
304       $confvalue = '';
305     } else {
306       //Bogus value, should not get here but treat as disabled
307       $confvalue = '';
308     }
309     if ($confvalue != trim($results['needsconf'])) {
310       $results['needsconf'] = $confvalue;
311       $changed=1;
312     }
313
314     // Not in sql so no sanity check needed
315     //
316     if (trim($astdb_ddial) == 'EXTENSION') {
317       $ddial = 'CHECKED';
318     } elseif (trim($astdb_ddial) == 'DIRECT') {
319       $ddial = '';
320     } else {
321       //Bogus value, should not get here but treat as disabled
322       $ddial = '';
323     }
324     $results['ddial'] = $ddial;
325
326     if ($changed) {
327       $sql = "UPDATE findmefollow SET grptime = '".$results['grptime']."', grplist = '".
328         str_replace("'", "''", trim($results['grplist']))."', pre_ring = '".$results['pre_ring'].
329         "', needsconf = '".$results['needsconf']."' WHERE grpnum = '".str_replace("'", "''", $grpnum)."' LIMIT 1";
330       $sql_results = sql($sql);
331     }
332   } // if check_astdb
333
334   return $results;
335 }
336
337 function findmefollow_configpageinit($dispnum) {
338   global $currentcomponent;
339
340   if ( ($dispnum == 'users' || $dispnum == 'extensions') ) {
341     $currentcomponent->addguifunc('findmefollow_configpageload');
342   }
343 }
344
345 function findmefollow_configpageload() {
346   global $currentcomponent;
347
348   $viewing_itemid =  isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
349   $action =  isset($_REQUEST['action'])?$_REQUEST['action']:null;
350   if ( $viewing_itemid != '' && $action != 'del') {
351     $set_findmefollow = findmefollow_list();
352     $grpURL = $_SERVER['PHP_SELF'].'?'.'display=findmefollow&extdisplay=GRP-'.$viewing_itemid;
353     if (is_array($set_findmefollow)) {
354       $grpTEXT = (in_array($viewing_itemid,$set_findmefollow) ? "Edit" : "Add")." Follow Me Settings";
355     } else {
356       $grpTEXT = "Add Follow Me Settings";
357     }
358     $currentcomponent->addguielem('_top', new gui_link('findmefollowlink', $grpTEXT, $grpURL));
359   }
360 }
361
362 function findmefollow_check_destinations($dest=true) {
363   global $active_modules;
364
365   $destlist = array();
366   if (is_array($dest) && empty($dest)) {
367     return $destlist;
368   }
369   $sql = "SELECT grpnum, postdest, name FROM findmefollow INNER JOIN users ON grpnum = extension ";
370   if ($dest !== true) {
371     $sql .= "WHERE postdest in ('".implode("','",$dest)."')";
372   }
373   $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
374
375   //$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup';
376
377   foreach ($results as $result) {
378     $thisdest = $result['postdest'];
379     $thisid   = $result['grpnum'];
380     $destlist[] = array(
381       'dest' => $thisdest,
382       'description' => 'Follow-Me: '.$thisid.' ('.$result['name'].')',
383       'edit_url' => 'config.php?display=findmefollow&extdisplay=GRP-'.urlencode($thisid),
384     );
385   }
386   return $destlist;
387 }
388
389 ?>
Note: See TracBrowser for help on using the browser.