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

Revision 13160, 30.6 kB (checked in by mbrevda, 1 year ago)

upstream changes (bug fixes/formating)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /* $Id $ */
3 if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
4
5 /* paging_init - Is run every time the page is loaded, checks
6    to make sure that the database is current and loaded, if not,
7    it propogates it. I expect that extra code will go here to
8    check for version upgrades, etc, of the paging database, to
9    allow for easy upgrades. */
10
11 //    Generates dialplan for paging  - is called from retrieve_conf
12
13 function paging_get_config($engine) {
14     global $db, $ext, $chan_dahdi, $version, $amp_conf;
15     switch($engine) {
16         case "asterisk":
17             $ast_ge_14 = version_compare($version, "1.4", "ge");
18
19             // setup for intercom
20             $fcc = new featurecode('paging', 'intercom-prefix');
21             $intercom_code = $fcc->getCodeActive();
22             unset($fcc);
23
24             // Since these are going down channel local, set ALERT_INFO and SIPADDHEADER which will be set in dialparties.agi
25             // no point in even setting the headers here they will get lost in channel local
26             //
27
28             /* Set these up once here and in intercom so that autoanswer macro does not have
29              * to go through this for every single extension which causes a lot of extra overhead
30              * with big page groups
31              */
32
33             $has_answermacro = false;
34
35             $alertinfo = 'Alert-Info: Ring Answer';
36             $callinfo  = 'Call-Info: <uri>\;answer-after=0';
37             $sipuri    = 'intercom=true';
38             $doptions = 'A(beep)';
39             $vxml_url = '';
40             $dtime = '5';
41             $custom_vars = array();
42             $autoanswer_arr = paging_get_autoanswer_defaults();
43             foreach ($autoanswer_arr as $autosetting) {
44                 switch (trim($autosetting['var'])) {
45                     case 'ALERTINFO':
46                         $alertinfo = trim($autosetting['setting']);
47                         break;
48                     case 'CALLINFO':
49                         $callinfo = trim($autosetting['setting']);
50                         break;
51                     case 'SIPURI':
52                         $sipuri = trim($autosetting['setting']);
53                         break;
54                     case 'VXML_URL':
55                         $vxml_url = trim($autosetting['setting']);
56                         break;
57                     case 'DOPTIONS':
58                         $doptions = trim($autosetting['setting']);
59                         break;
60                     case 'DTIME':
61                         $dtime = trim($autosetting['setting']);
62                         break;
63                     default:
64                         $key = trim($autosetting['var']);
65                         $custom_vars[$key] = trim($autosetting['setting']);
66                         if (ltrim($custom_vars[$key],'_') == "ANSWERMACRO") {
67                             $has_answermacro = true;
68                         }
69                         break;
70                 }
71             }
72
73             $apppaging = 'app-paging';
74             if (!empty($intercom_code)) {
75                 $code = '_'.$intercom_code.'.';
76                 $context = 'ext-intercom';
77                 $ext->add($context, $code, '', new ext_macro('user-callerid'));
78                 $ext->add($context, $code, '', new ext_setvar('dialnumber', '${EXTEN:'.strlen($intercom_code).'}'));
79                 $ext->add($context, $code, '', new ext_setvar('INTERCOM_CALL', 'TRUE'));
80                 $ext->add($context, $code, '', new ext_gotoif('$["${DB(AMPUSER/${AMPUSER}/intercom/block)}" = "blocked"]', 'end'));
81                 $ext->add($context, $code, '', new ext_gotoif('$["${DB(DND/${dialnumber})}" = "YES"]', 'end'));
82                 $ext->add($context, $code, '', new ext_gotoif('$["${DB(AMPUSER/${dialnumber}/intercom/${AMPUSER})}" = "allow" ]', 'allow'));
83                 $ext->add($context, $code, '', new ext_gotoif('$["${DB(AMPUSER/${dialnumber}/intercom/${AMPUSER})}" = "deny" ]', 'nointercom'));
84                 $ext->add($context, $code, '', new ext_gotoif('$["${DB(AMPUSER/${dialnumber}/intercom)}" = "disabled" ]', 'nointercom'));
85                 $ext->add($context, $code, 'allow', new ext_dbget('DEVICES','AMPUSER/${dialnumber}/device'));
86                 $ext->add($context, $code, '', new ext_gotoif('$["${DEVICES}" = "" ]', 'end'));
87                 $ext->add($context, $code, '', new ext_setvar('LOOPCNT', '${FIELDQTY(DEVICES,&)}'));
88
89                 /* Set these up so that macro-autoanswer doesn't have to
90                  */
91                 $ext->add($context, $code, '', new ext_setvar('_SIPURI', ''));
92                 if (trim($alertinfo) != "") {
93                     $ext->add($context, $code, '', new ext_setvar('_ALERTINFO', $alertinfo));
94                 }
95                 if (trim($callinfo) != "") {
96                     $ext->add($context, $code, '', new ext_setvar('_CALLINFO', $callinfo));
97                 }
98                 if (trim($sipuri) != "") {
99                     $ext->add($context, $code, '', new ext_setvar('_SIPURI', $sipuri));
100                 }
101                 if (trim($vxml_url) != "") {
102                     $ext->add($context, $code, '', new ext_setvar('_VXML_URL', $vxml_url));
103                 }
104                 if (trim($doptions) != "") {
105                     $ext->add($context, $code, '', new ext_setvar('_DOPTIONS', $doptions));
106                 }
107                 foreach ($custom_vars as $key => $value) {
108                     $ext->add($context, $code, '', new ext_setvar('_'.ltrim($key,'_'), $value));
109                 }
110                 $ext->add($context, $code, '', new ext_setvar('_DTIME', $dtime));
111                 $ext->add($context, $code, '', new ext_setvar('_ANSWERMACRO', ''));
112
113                 $ext->add($context, $code, '', new ext_gotoif('$[${LOOPCNT} > 1 ]', 'pagemode'));
114                 $ext->add($context, $code, '', new ext_macro('autoanswer','${DEVICES}'));
115
116         if ($ast_ge_14) {
117                   $ext->add($context, $code, 'check', new ext_chanisavail('${DIAL}', 's'));
118                 $ext->add($context, $code, '', new ext_gotoif('$["${AVAILORIGCHAN}" = ""]', 'end'));           
119         } else {
120                   $ext->add($context, $code, 'check', new ext_chanisavail('${DIAL}', 'sj'));
121         }
122                 $ext->add($context, $code, '', new ext_noop_trace('AVAILCHAN: ${AVAILCHAN}, AVAILORIGCHAN: ${AVAILORIGCHAN}, AVAILSTATUS: ${AVAILSTATUS}',5));
123                 $dopt = '';
124                 if ($amp_conf['AST_FUNC_CONNECTEDLINE']) {
125                     $len = strlen($code)-2;
126                     $dopt = 'I';
127                     $ext->add($context, $code, '', new ext_gotoif('$["${DB(AMPUSER/${EXTEN:' . $len . '}/cidname)}" = ""]','godial'));
128                     $ext->add($context, $code, '', new ext_set('CONNECTEDLINE(name,i)', '${DB(AMPUSER/${EXTEN:' . $len . '}/cidname)}'));
129                     $ext->add($context, $code, '', new ext_set('CONNECTEDLINE(num)', '${EXTEN:' . $len . '}'));
130                 }
131                 $ext->add($context, $code, 'godial', new ext_dial('${DIAL}','${DTIME},' . $dopt . '${DOPTIONS}${INTERCOM_EXT_DOPTIONS}'));
132
133
134         $ext->add($context, $code, 'end', new ext_execif('$[${INTERCOM_RETURN}]', 'Return'));
135                 $ext->add($context, $code, '', new ext_busy());
136                 $ext->add($context, $code, '', new ext_macro('hangupcall'));
137         if (!$ast_ge_14) {
138                   $ext->add($context, $code, '', new ext_execif('$[${INTERCOM_RETURN}]', 'Return'),'check',101);
139                 $ext->add($context, $code, '', new ext_busy());
140                 $ext->add($context, $code, '', new ext_macro('hangupcall'));
141         }
142                 $ext->add($context, $code, 'pagemode', new ext_setvar('ITER', '1'));
143                 $ext->add($context, $code, '', new ext_setvar('DIALSTR', ''));
144                 $ext->add($context, $code, 'begin', new ext_setvar('DIALSTR', '${DIALSTR}&LOCAL/PAGE${CUT(DEVICES,&,${ITER})}@'.$apppaging));
145                 $ext->add($context, $code, '', new ext_setvar('ITER', '$[${ITER} + 1]'));
146                 $ext->add($context, $code, '', new ext_gotoif('$[${ITER} <= ${LOOPCNT}]', 'begin'));
147                 $ext->add($context, $code, '', new ext_setvar('DIALSTR', '${DIALSTR:1}'));
148                 $ext->add($context, $code, '', new ext_setvar('_AMPUSER', '${AMPUSER}'));
149                 $ext->add($context, $code, '', new ext_page('${DIALSTR},d'));
150                 $ext->add($context, $code, '', new ext_execif('$[${INTERCOM_RETURN}]', 'Return'));
151                 $ext->add($context, $code, '', new ext_busy());
152                 $ext->add($context, $code, '', new ext_macro('hangupcall'));
153
154                 $ext->add($context, $code, 'nointercom', new ext_noop('Intercom disallowed by ${dialnumber}'));
155                 $ext->add($context, $code, '', new ext_execif('$[${INTERCOM_RETURN}]', 'Return'));
156                 $ext->add($context, $code, '', new ext_playback('intercom&for&extension'));
157                 $ext->add($context, $code, '', new ext_saydigits('${dialnumber}'));
158                 $ext->add($context, $code, '', new ext_playback('is&disabled'));
159                 $ext->add($context, $code, '', new ext_congestion());
160
161                 $extintercomusers = 'ext-intercom-users';
162                 $userlist = core_users_list();
163                 if (is_array($userlist)) {
164                     foreach($userlist as $item) {
165                         $ext_intercom_code = $intercom_code.$item[0];
166                         $ext->add($extintercomusers, $ext_intercom_code, '', new ext_goto($context.',${EXTEN},1'));
167                     }
168                 }
169
170                 $context = $extintercomusers;
171                 $ext->addInclude('from-internal-additional', $context);
172             }
173             
174             $fcc = new featurecode('paging', 'intercom-on');
175             $oncode = $fcc->getCodeActive();
176             unset($fcc);
177
178             if ($oncode) {
179                 $ext->add($context, $oncode, '', new ext_answer(''));
180                 $ext->add($context, $oncode, '', new ext_wait('1'));
181                 $ext->add($context, $oncode, '', new ext_macro('user-callerid'));
182                 $ext->add($context, $oncode, '', new ext_setvar('DB(AMPUSER/${AMPUSER}/intercom)', 'enabled'));
183                 $ext->add($context, $oncode, '', new ext_playback('intercom&enabled'));
184                 $ext->add($context, $oncode, '', new ext_macro('hangupcall'));
185
186                 $target = '${EXTEN:'.strlen($oncode).'}';
187                 $oncode = "_".$oncode.".";
188                 $ext->add($context, $oncode, '', new ext_answer(''));
189                 $ext->add($context, $oncode, '', new ext_wait('1'));
190                 $ext->add($context, $oncode, '', new ext_macro('user-callerid'));
191                 $ext->add($context, $oncode, '', new ext_gotoif('$["${DB(AMPUSER/${AMPUSER}/intercom/'.$target.')}" = "allow" ]}','unset'));
192                 $ext->add($context, $oncode, '', new ext_gotoif('$[${DB_EXISTS(AMPUSER/${EXTEN:3}/device)} != 1]','invaliduser'));
193                 $ext->add($context, $oncode, '', new ext_dbput('AMPUSER/${AMPUSER}/intercom/'.$target, 'allow'));
194                 $ext->add($context, $oncode, '', new ext_playback('intercom&enabled&for&extension&number'));
195                 $ext->add($context, $oncode, '', new ext_saydigits($target));
196                 $ext->add($context, $oncode, '', new ext_macro('hangupcall'));
197                 $ext->add($context, $oncode, 'unset', new ext_dbdeltree('AMPUSER/${AMPUSER}/intercom/'.$target));
198                 $ext->add($context, $oncode, '', new ext_playback('intercom&enabled&cancelled&for&extension&number'));
199                 $ext->add($context, $oncode, '', new ext_saydigits($target));
200                 $ext->add($context, $oncode, '', new ext_macro('hangupcall'));
201                 $ext->add($context, $oncode, 'invaliduser', new ext_playback('extension&number'));
202                 $ext->add($context, $oncode, '', new ext_saydigits($target));
203                 $ext->add($context, $oncode, '', new ext_playback('is&invalid'));
204                 $ext->add($context, $oncode, '', new ext_macro('hangupcall'));
205             }
206             
207             $fcc = new featurecode('paging', 'intercom-off');
208             $offcode = $fcc->getCodeActive();
209             unset($fcc);
210     
211             if ($offcode) {
212                 $ext->add($context, $offcode, '', new ext_answer(''));
213                 $ext->add($context, $offcode, '', new ext_wait('1'));
214                 $ext->add($context, $offcode, '', new ext_macro('user-callerid'));
215                 $ext->add($context, $offcode, '', new ext_setvar('DB(AMPUSER/${AMPUSER}/intercom)', 'disabled'));
216                 $ext->add($context, $offcode, '', new ext_playback('intercom&disabled'));
217                 $ext->add($context, $offcode, '', new ext_macro('hangupcall'));
218
219                 $target = '${EXTEN:'.strlen($offcode).'}';
220                 $offcode = "_".$offcode.".";
221                 $ext->add($context, $offcode, '', new ext_answer(''));
222                 $ext->add($context, $offcode, '', new ext_wait('1'));
223                 $ext->add($context, $offcode, '', new ext_macro('user-callerid'));
224                 $ext->add($context, $offcode, '', new ext_gotoif('$["${DB(AMPUSER/${AMPUSER}/intercom/'.$target.')}" = "deny" ]}','unset2'));
225                 $ext->add($context, $offcode, '', new ext_gotoif('$[${DB_EXISTS(AMPUSER/${EXTEN:3}/device)} != 1]','invaliduser2'));
226                 $ext->add($context, $offcode, '', new ext_dbput('AMPUSER/${AMPUSER}/intercom/'.$target, 'deny'));
227                 $ext->add($context, $offcode, '', new ext_playback('intercom&disabled&for&extension&number'));
228                 $ext->add($context, $offcode, '', new ext_saydigits($target));
229                 $ext->add($context, $offcode, '', new ext_macro('hangupcall'));
230                 $ext->add($context, $offcode, 'unset2', new ext_dbdeltree('AMPUSER/${AMPUSER}/intercom/'.$target));
231                 $ext->add($context, $offcode, '', new ext_playback('intercom&disabled&cancelled&for&extension&number'));
232                 $ext->add($context, $offcode, '', new ext_saydigits($target));
233                 $ext->add($context, $offcode, '', new ext_macro('hangupcall'));
234                 $ext->add($context, $offcode, 'invaliduser2', new ext_playback('extension&number'));
235                 $ext->add($context, $offcode, '', new ext_saydigits($target));
236                 $ext->add($context, $offcode, '', new ext_playback('is&invalid'));
237                 $ext->add($context, $offcode, '', new ext_macro('hangupcall'));
238             }
239
240             /* Create macro-autoanswer that will try to intelligently set the
241                required parameters to handle paging. Eventually it will use
242                  known device information.
243
244                 This macro does the following:
245
246                 Input:  FreePBX Device number to be called requiring autoanswer
247                 Output: ${DIAL} Channel Variable with the dial string to be called
248                         Appropriate SIP headers added
249                                 Other special requirements that may be custom for this device
250
251                 1. Set ${DIAL} to the device's dial string
252                 2. If there is a device specific macro defined in the DEVICE's object
253                    (DEVICE/<devicenum>/autoanswer/macro) then execute that macro and end
254                 3. Try to identify endpoints by their useragents that may need known
255                    changes and make those changes. These are generated from the
256                      paging_autoanswer table so users can extend them, if any are present
257                 5. Set the variables and end unless a useragent specific ANSWERMACRO is
258                    defined in which case call it and end.
259
260                 This macro is called for intercoming and paging to try and enable the
261                 target device to auto-answer. Devices with special needs can be handled
262                 with the device specific macro. For example, if you have a device that
263                 can not auto-answer except by specifically configuring a line key on
264                 the device that always answers, you could use a device specific macro
265                 to change the dialstring. If you had a set of such devices, you could
266                 standardize on the device numbers (e.g. nnnn for normal calls and 2nnnn
267                 for auto-answer calls). You could then create a general purpose macro
268                 to modify the dial string accordingly. Provisioning tools will be able
269                 to take advantage of setting and creating such an ability.
270                 If you have a set of devices that can be identified with a SIP useragent
271                 then you can use a general macro without setting info in each device.
272              */
273
274             $autoanswer_arr = paging_get_autoanswer_useragents();
275
276             $macro = 'macro-autoanswer';
277             $ext->add($macro, "s", '', new ext_setvar('DIAL', '${DB(DEVICE/${ARG1}/dial)}'));
278
279             // If we are in DAHDI compat mode, then we need to substitute DAHDI for ZAP
280             if ($chan_dahdi) {
281                 $ext->add($macro, "s", '', new ext_execif('$["${DIAL:0:3}" = "ZAP"]', 'Set','DIAL=DAHDI${DIAL:3}'));
282             }
283             $ext->add($macro, "s", '', new ext_gotoif('$["${DB(DEVICE/${ARG1}/autoanswer/macro)}" != "" ]', 'macro'));
284
285             // If there are no phone specific auto-answer vars, then we don't care what the phone is below
286             //
287             if (!empty($autoanswer_arr)) {
288                 $ext->add($macro, "s", '', new ext_setvar('phone', '${SIPPEER(${CUT(DIAL,/,2)}:useragent)}'));
289             }
290             // We used to set all the variables here (ALERTINFO, CALLINFO, etc. That has been moved to each
291             // paging group and the intercom main macro, since it was redundant for every phone causing a lot
292             // of overhead with large page groups.
293             //
294
295             // Defaults are setup, now make specific adjustments for detected phones
296             // These come from the SQL table as well where installations can make customizations
297             //
298             foreach ($autoanswer_arr as $autosetting) {
299                 $useragent   = trim($autosetting['useragent']);
300                 $autovar     = trim($autosetting['var']);
301                 $data        = trim($autosetting['setting']);
302                 switch (ltrim($autovar,'_')) {
303                     case 'ANSWERMACRO':
304                         $has_answermacro = true;
305                         // fall through - no break on purpose
306                     case 'ALERTINFO':
307                     case 'CALLINFO':
308                     case 'SIPURI':
309                     case 'VXML_URL':
310                     case 'DOPTIONS':
311                     case 'DTIME':
312                     default:
313                         if (trim($data) != "") {
314                             $ext->add($macro, "s", '', new ext_execif('$["${phone:0:'.strlen($useragent).'}" = "'.$useragent.'"]', 'Set',$autovar.'='.$data));
315                         }
316                         break;
317                 }
318             }
319
320             // Now any adjustments have been made, set the headers and done
321             //
322             if ($has_answermacro) {
323                 $ext->add($macro, "s", '', new ext_gotoif('$["${ANSWERMACRO}" != ""]','macro2'));
324             }
325             $ext->add($macro, "s", '', new ext_execif('$["${ALERTINFO}" != ""]', 'SipAddHeader','${ALERTINFO}'));
326             $ext->add($macro, "s", '', new ext_execif('$["${CALLINFO}" != ""]', 'SipAddHeader','${CALLINFO}'));
327             $ext->add($macro, "s", '', new ext_execif('$["${SIPURI}" != ""]', 'Set','__SIP_URI_OPTIONS=${SIPURI}'));
328             $ext->add($macro, "s", 'macro', new ext_macro('${DB(DEVICE/${ARG1}/autoanswer/macro)}','${ARG1}'), 'n',2);
329             if ($has_answermacro) {
330                 $ext->add($macro, "s", 'macro2', new ext_macro('${ANSWERMACRO}','${ARG1}'), 'n',2);
331             }
332             
333             //auto answer stuff
334             //set autoanswer variables
335             if (!empty($custom_vars)) {
336                 foreach ($custom_vars as $key => $value) {
337                     $ext->add($apppaging, '_AUTOASWER.', '', new ext_setvar('_'.ltrim($key,'_'), $value));
338                 }
339                 $ext->add($apppaging, '_AUTOASWER.', '', new ext_macro('autoanswer','${EXTEN:9}'));
340                 $ext->add($apppaging, '_AUTOASWER.', '', new ext_return());
341             }
342
343             // Setup Variables before AGI script
344             //
345             $ext->add($apppaging, 'ssetup', '', new ext_set('_SIPURI', ''));
346             if (isset($alertinfo) && trim($alertinfo) != "") {
347                 $ext->add($apppaging, 'ssetup', '', new ext_set('_ALERTINFO', $alertinfo));
348             }
349             
350             if (isset($callinfo) && trim($callinfo) != "") {
351                 $ext->add($apppaging, 'ssetup', '', new ext_set('_CALLINFO', $callinfo));
352             }
353             if (isset($sipuri) && trim($sipuri) != "") {
354                 $ext->add($apppaging, 'ssetup', '', new ext_set('_SIPURI', $sipuri));
355             }
356             if (isset($vxml_url) && trim($vxml_url) != "") {
357                 $ext->add($apppaging, 'ssetup', '', new ext_set('_VXML_URL', $vxml_url));
358             }
359             if (isset($doptions) && trim($doptions) != "") {
360                 $ext->add($apppaging, 'ssetup', '', new ext_set('_DOPTIONS', $doptions));
361             }
362             $ext->add($apppaging, 'ssetup', '', new ext_set('_DTIME', $dtime));
363             $ext->add($apppaging, 'ssetup', '', new ext_set('_ANSWERMACRO', ''));
364
365             $page_opts = $amp_conf['ASTCONFAPP'] == 'app_confbridge' ? '1qs' : '1doqsx';
366             $ext->add($apppaging, 'ssetup', '', new ext_set('PAGE_CONF', '${EPOCH}${RAND(100,999)}'));
367             $ext->add($apppaging, 'ssetup', '', new ext_return());
368                 
369             // Normal page version (now used for Force also)
370             // If we had any custom_vars then call the AUTOASWER subroutine first, otherwise go
371             // straight to macro-autoanswer
372             if (!empty($custom_vars)) {
373                 $ext->add($apppaging, "_PAGE.", 'SKIPCHECK', new ext_gosub('AUTOASWER${EXTEN:4},1'));
374             } else {
375                 $ext->add($apppaging, "_PAGE.", 'SKIPCHECK', new ext_macro('autoanswer', '${EXTEN:4}'));
376             }
377             $ext->add($apppaging, "_PAGE.", '', new ext_dial('${DIAL}','${DTIME},${DOPTIONS}'));
378             $ext->add($apppaging, "_PAGE.", 'skipself', new ext_hangup());
379
380             // Try ChanSpy Version
381             $ext->add($apppaging, "_SPAGE.", 'chanspy', new ext_chanspy('${SP_DEVICE}-','qW'));
382             $ext->add($apppaging, "_SPAGE.", '', new ext_hangup());
383
384             $apppagegroups = 'app-pagegroups';
385             // Now get a list of all the paging groups...
386             $sql = "SELECT page_group, force_page, duplex FROM paging_config";
387             $paging_groups = $db->getAll($sql, DB_FETCHMODE_ASSOC);
388             foreach ($paging_groups as $thisgroup) {
389                 $grp=trim($thisgroup['page_group']);
390                 switch ($thisgroup['force_page']) {
391                 case 1:
392                     $pagemode = 'FPAGE';
393                 break;
394                 case 2:
395                     $pagemode = 'SPAGE';
396                 break;
397                 case 0:
398                 default:
399                     $pagemode = 'PAGE';
400                 break;
401                 }
402
403                 $sql = "SELECT ext FROM paging_groups WHERE page_number='$grp'";
404                 $all_exts = $db->getCol($sql);
405                     
406                 // Create the paging context that is used in the paging application for each phone to auto-answer
407                 //add ext-paging with goto's to our app-paging context and a hint for the page
408                 $extpaging = 'ext-paging';
409                 $ext->add($extpaging, $grp, '', new ext_goto($apppagegroups . ',' . $grp . ',1'));
410                 $ext->addInclude('from-internal-noxfer-additional',$extpaging);
411                 $ext->addHint($extpaging, $grp, 'Custom:PAGE' . $grp);
412                     
413                 //app-page dialplan
414                     
415                 $ext->add($apppagegroups, $grp, '', new ext_macro('user-callerid'));
416                 $ext->add($apppagegroups, $grp, '', new ext_set('_PAGEGROUP', $grp));
417                     
418                 //if page group it in use, goto to busy
419                 $ext->add($apppagegroups, $grp, '', new ext_gotoif('$[${TRYLOCK(apppagegroups'. $grp .')}]', '', 'busy'));
420                     
421                 //set blf to in use
422                 $ext->add($apppagegroups, $grp, 'devstate', new ext_setvar('DEVICE_STATE(Custom:PAGE' . $grp .')', 'INUSE'));
423                     
424                 $ext->add($apppagegroups, $grp, '', new ext_answer(''));
425                 $ext->add($apppagegroups, $grp, '', new ext_gosub('1','ssetup', $apppaging));
426                 $ext->add($apppagegroups, $grp, '', new ext_set('PAGEMODE', $pagemode));
427                 $ext->add($apppagegroups, $grp, '', new ext_set('PAGE_MEMBERS', implode('-', $all_exts)));
428                 $ext->add($apppagegroups, $grp, '', new ext_set('PAGE_CONF_OPTS', $page_opts . (!$thisgroup['duplex'] ? 'm' : '')));
429                 $ext->add($apppagegroups, $grp, 'agi', new ext_agi('page.agi'));           
430
431                 //we cant use originate from the dialplan as the dialplan command is not asynchronous
432                 //we would like to though...
433                 //this code here as a sign of hope -MB
434                 /*foreach ($page_members as $member) {
435                         $ext->add($apppagegroups, $grp, 'page', new ext_originate($member,'app','meetme', '${PAGE_CONF}\,${PAGE_CONF_OPTS}'));
436                 }*/                   
437                 $ext->add($apppagegroups, $grp, 'page', new ext_meetme('${PAGE_CONF}', 'doqwxAG'));
438                 $ext->add($apppagegroups, $grp, '', new ext_hangup());
439                 $ext->add($apppagegroups, $grp, 'busy', new ext_set('PAGE${PAGEGROUP}BUSY', 'TRUE'));
440                 $ext->add($apppagegroups, $grp, 'play-busy', new ext_busy(3));
441                 $ext->add($apppagegroups, $grp, 'busy-hang', new ext_goto('ext-paging,h,1'));
442             }
443             //h
444             $ext->add($apppagegroups, 'h', '',
445                 new ext_execif('$[${ISNULL(${PAGE${PAGEGROUP}BUSY})}]', 'Set', 'DEVICE_STATE(Custom:PAGE${PAGEGROUP})=NOT_INUSE'));
446             
447             //page playback
448             $c = 'app-page-stream';
449             $ext->add($c, 's', '', new ext_wait(1));
450             $ext->add($c, 's', '', new ext_answer());
451             $ext->add($c, 's', '', new ext_meetme('${PAGE_CONF}', '${PAGE_CONF_OPTS}'));
452             $ext->add($c, 's', '', new ext_hangup());
453             
454         break;
455     }
456 }
457
458 function paging_get_autoanswer_defaults() {
459     global $db;
460
461     $sql = "SELECT * FROM paging_autoanswer WHERE useragent = 'default'";
462     $results = $db->getAll($sql,DB_FETCHMODE_ASSOC);
463     if(DB::IsError($results)) {
464         $results = array();
465     }
466     return $results;
467 }
468
469 function paging_get_autoanswer_useragents($useragent = '') {
470     global $db;
471
472     $sql = "SELECT * FROM paging_autoanswer WHERE useragent != 'default' ";
473     if ($useragent != "") {
474         $sql .= "AND useragent = $useragent ";
475     }
476     $results = $db->getAll($sql,DB_FETCHMODE_ASSOC);
477     if(DB::IsError($results)) {
478         $results = array();
479     }
480     return $results;
481 }
482
483 function paging_list() {
484     global $db;
485
486     $sql = "SELECT page_group, description FROM paging_config ORDER BY page_group";
487     $results = $db->getAll($sql,DB_FETCHMODE_ASSOC);
488     if(DB::IsError($results)) {
489         $results = null;
490     } else {
491         foreach ($results as $key => $list) {
492             $results[$key][0] = $list['page_group'];
493         }
494     }
495     // There should be a checkRange here I think, but I haven't looked into it yet.
496     //    return array('999', '998', '997');
497     return $results;
498 }
499
500 function paging_check_extensions($exten=true) {
501     global $active_modules;
502
503     $extenlist = array();
504     if (is_array($exten) && empty($exten)) {
505         return $extenlist;
506     }
507
508     $sql = "SELECT page_group, description FROM paging_config ";
509     if (is_array($exten)) {
510         $sql .= "WHERE page_group in ('".implode("','",$exten)."')";
511     }
512     $sql .= " ORDER BY page_group";
513     $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
514
515     $type = isset($active_modules['paging']['type'])?$active_modules['paging']['type']:'setup';
516     foreach ($results as $result) {
517         $thisexten = $result['page_group'];
518         $extenlist[$thisexten]['description'] = _("Page Group: ").$result['description'];
519         $extenlist[$thisexten]['status'] = 'INUSE';
520         $extenlist[$thisexten]['edit_url'] = 'config.php?type='.urlencode($type).'setup&display=paging&selection='.urlencode($thisexten).'&action=modify';
521     }
522     return $extenlist;
523 }
524
525 function paging_get_devs($grp) {
526     global $db;
527
528     // Just in case someone's trying to be smart with a SQL injection.
529     $grp = $db->escapeSimple($grp);
530
531     $sql = "SELECT ext FROM paging_groups where page_number='$grp'";
532     $results = $db->getAll($sql);
533     if(DB::IsError($results))
534         $results = array();
535     foreach ($results as $val)
536         $tmparray[] = $val[0];
537     return $tmparray;
538 }
539
540 function paging_get_pagingconfig($grp) {
541     global $db;
542
543     // Just in case someone's trying to be smart with a SQL injection.
544     $grp = $db->escapeSimple($grp);
545
546     $sql = "SELECT * FROM paging_config WHERE page_group='$grp'";
547     $results = $db->getRow($sql, DB_FETCHMODE_ASSOC);
548     if(DB::IsError($results)) {
549         $results = null;
550     }
551     $sql = "SELECT * FROM admin WHERE variable='default_page_grp' AND value='$grp'";
552     $default_group = $db->getRow($sql, DB_FETCHMODE_ASSOC);
553     if(DB::IsError($default_group)) {
554         $results['default_group'] = 0;
555     } else {
556         $results['default_group'] = empty($default_group) ? 0 : $default_group['value'];
557     }
558     return $results;
559 }
560
561 function paging_modify($oldxtn, $xtn, $plist, $force_page, $duplex, $description='', $default_group=0) {
562     global $db;
563
564     // Just in case someone's trying to be smart with a SQL injection.
565     $xtn = $db->escapeSimple($xtn);
566
567     // Delete it if it's there.
568     paging_del($oldxtn);
569
570     // Now add it all back in.
571     paging_add($xtn, $plist, $force_page, $duplex, $description, $default_group);
572
573     // Aaad we need a reload.
574     needreload();
575
576 }
577
578 function paging_del($xtn) {
579     global $db;
580     $sql = "DELETE FROM paging_groups WHERE page_number='$xtn'";
581     $res = $db->query($sql);
582     if (DB::isError($res)) {
583         var_dump($res);
584         die_freepbx("Error in paging_del(): ");
585     }
586     
587     $sql = "DELETE FROM paging_config WHERE page_group='$xtn'";
588     $res = $db->query($sql);
589     if (DB::isError($res)) {
590         var_dump($res);
591         die_freepbx("Error in paging_del(): ");
592     }
593     sql("DELETE FROM `admin` WHERE variable = 'default_page_grp' AND value = '$xtn'");
594     
595     needreload();
596 }
597
598 function paging_add($xtn, $plist, $force_page, $duplex, $description='', $default_group) {
599     global $db;
600
601     // $plist contains a string of extensions, with \n as a seperator.
602     // Split that up first.
603     if (is_array($plist)) {
604         $xtns = $plist;
605     } else {
606         $xtns = explode("\n",$plist);
607     }
608     foreach (array_keys($xtns) as $val) {
609         $val = $db->escapeSimple(trim($xtns[$val]));
610         // Sanity check input.
611         
612         $sql = "INSERT INTO paging_groups(page_number, ext) VALUES ('$xtn', '$val')";
613         $db->query($sql);
614     }
615     
616     $description = $db->escapeSimple(trim($description));
617     $sql = "INSERT INTO paging_config(page_group, force_page, duplex, description) VALUES ('$xtn', '$force_page', '$duplex', '$description')";
618     $db->query($sql);
619     
620     if ($default_group) {
621         sql("DELETE FROM `admin` WHERE variable = 'default_page_grp'");
622         sql("INSERT INTO `admin` (variable, value) VALUES ('default_page_grp', '$xtn')");
623     } else {
624         sql("DELETE FROM `admin` WHERE variable = 'default_page_grp' AND value = '$xtn'");
625     }
626     
627     needreload();
628 }
629
630 function paging_check_default($extension) {
631     $sql = "SELECT ext FROM paging_groups WHERE ext = '$extension' AND page_number = (SELECT value FROM admin WHERE variable = 'default_page_grp' limit 1)";
632     $results = sql($sql,"getAll");
633     return (count($results) ? 1 : 0);
634 }
635
636 function paging_get_default() {
637     $default_group = sql("SELECT value FROM `admin` WHERE variable = 'default_page_grp' limit 1", "getOne");
638   return $default_group;
639 }
640
641 function paging_set_default($extension, $value) {
642     $default_group = sql("SELECT value FROM `admin` WHERE variable = 'default_page_grp' limit 1", "getOne");
643     if ($default_group == '') {
644         return false;
645     }
646     sql("DELETE FROM paging_groups WHERE ext = '$extension' AND page_number = '$default_group'");
647     if ($value == 1) {
648         sql("INSERT INTO paging_groups (page_number, ext) VALUES ('$default_group', '$extension')");
649     }
650 }
651     
652 function paging_configpageinit($pagename) {
653     global $currentcomponent;
654
655     $action = isset($_REQUEST['action'])?$_REQUEST['action']:null;
656     $extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
657     $extension = isset($_REQUEST['extension'])?$_REQUEST['extension']:null;
658     $tech_hardware = isset($_REQUEST['tech_hardware'])?$_REQUEST['tech_hardware']:null;
659
660     // We only want to hook 'devices' or 'extensions' pages.
661     if ($pagename != 'devices' && $pagename != 'extensions') {
662         return true;
663     }
664
665     if ($tech_hardware != null && ($pagename == 'extensions' || $pagename == 'devices')) {
666         paging_applyhooks();
667         $currentcomponent->addprocessfunc('paging_configprocess', 8);
668     } elseif ($action=="add") {
669         // We don't need to display anything on an 'add', but we do need to handle returned data.
670         $currentcomponent->addprocessfunc('paging_configprocess', 8);
671     } elseif ($extdisplay != '') {
672         // We're now viewing an extension, so we need to display _and_ process.
673         paging_applyhooks();
674         $currentcomponent->addprocessfunc('paging_configprocess', 8);
675     }
676 }
677
678 function paging_applyhooks() {
679     global $currentcomponent;
680
681     // Add the 'process' function - this gets called when the page is loaded, to hook into
682     // displaying stuff on the page.
683     $currentcomponent->addoptlistitem('page_group', '0', _("Exclude"));
684     $currentcomponent->addoptlistitem('page_group', '1', _("Include"));
685     $currentcomponent->setoptlistopts('page_group', 'sort', false);
686
687     $currentcomponent->addguifunc('paging_configpageload');
688 }
689
690 /*
691 */
692 // This is called before the page is actually displayed, so we can use addguielem().
693 function paging_configpageload() {
694     global $currentcomponent;
695
696     // Init vars from $_REQUEST[]
697     $action = isset($_REQUEST['action']) ? $_REQUEST['action']:null;
698     $extdisplay = isset($_REQUEST['extdisplay']) ? $_REQUEST['extdisplay']:null;
699     $tech_hardware = isset($_REQUEST['tech_hardware']) ? $_REQUEST['tech_hardware']:'';
700     
701     // Don't display this stuff it it's on a 'This xtn has been deleted' page.
702     if ($action != 'del' && $tech_hardware != 'virtual') {
703
704         $default_group = sql("SELECT value FROM `admin` WHERE variable = 'default_page_grp'", "getOne");
705         $section = _("Default Group Inclusion");
706         if ($default_group != "") {
707             $in_default_page_grp = paging_check_default($extdisplay);
708             $currentcomponent->addguielem($section, new gui_selectbox('in_default_page_grp', $currentcomponent->getoptlist('page_group'), $in_default_page_grp, _('Default Page Group'), _('You can include or exclude this extension/device from being part of the default page group when creating or editing.'), false));
709         }
710     }
711 }
712
713 function paging_configprocess() {
714     global $db;
715
716     //create vars from the request
717     //
718     $action = isset($_REQUEST['action'])?$_REQUEST['action']:null;
719     $ext = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
720     $extn = isset($_REQUEST['extension'])?$_REQUEST['extension']:null;
721     $in_default_page_grp = isset($_REQUEST['in_default_page_grp'])?$_REQUEST['in_default_page_grp']:false;
722
723     if (($_REQUEST['display'] == 'devices') && $action == 'add') {
724         $extdisplay = $_REQUEST['deviceid'];
725     } else {
726         $extdisplay = ($ext=='') ? $extn : $ext;
727     }
728
729     if ($action == "add" || $action == "edit") {
730         if (!isset($GLOBALS['abort']) || $GLOBALS['abort'] !== true) {
731             if ($in_default_page_grp !== false) {
732                 paging_set_default($extdisplay, $in_default_page_grp);
733             }
734         }
735     } elseif ($action == "del") {
736         $sql = "DELETE FROM paging_groups WHERE ext = '$extdisplay'";
737         sql($sql);
738     }
739 }
740
741 ?>
742
Note: See TracBrowser for help on using the browser.