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

Revision 6041, 10.2 kB (checked in by Nick_Lewis, 4 years ago)

called macro get-vmcontext to get correct vmcontext and adjusted for ext_vmauthenticate class implementation of ticket #2777

Line 
1 <?php
2
3
4 /*  Generates callerid checks for usersets
5   We call this with retrieve_conf
6 */
7 function usersets_get_config($engine) {
8   global $ext;  // is this the best way to pass this?
9   global $asterisk_conf;
10   global $usersets_conf; // our usersets object (created in retrieve_conf)
11   switch($engine) {
12     case "asterisk":
13       $allusersets = usersets_list();
14       if(is_array($allusersets)) {
15         foreach($allusersets as $item) {
16           $id = "macro-usersets-" . $item['usersets_id'];
17           $ext->add($id, 's', '', new ext_macro('user-callerid'));
18
19           // do trust list first - if in both lists this one will take priority
20           $trustarray = explode("\n",$item['trustlist']);
21           if ($trustarray[0] == "") {unset($trustarray[0]);}
22           foreach($trustarray as $trust) {
23             $ext->add($id, 's', '', new ext_gotoif('$["${CALLERID(number)}" = "'.trim($trust).'"]','trusted'));
24           }
25
26           $autharray = explode("\n",$item['authlist']);
27           if ($autharray[0] == "") {unset($autharray[0]);}
28           foreach($autharray as $auth) {
29             $ext->add($id, 's', '', new ext_gotoif('$["${CALLERID(number)}" = "'.trim($auth).'"]','needauth'));
30           }
31           $ext->add($id, 's', '', new ext_answer());
32           $ext->add($id, 's', '', new ext_wait(1));
33           $ext->add($id, 's', '', new ext_playback('cancelled'));
34           $ext->add($id, 's', '', new ext_macro('hangupcall'));
35           $ext->add($id, 's', 'needauth', new ext_answer());
36           $ext->add($id, 's', '', new ext_wait(1));
37           $ext->add($id, 's', '', new ext_macro('get-vmcontext','${CALLERID(number)}'));
38           $ext->add($id, 's', '', new ext_vmauthenticate('${CALLERID(number)}@${VMCONTEXT}'));
39           $ext->add($id, 's', 'trusted', new ext_noop('${CALLERID(number)} access approved'));
40
41         }
42       }
43
44     break;
45   }
46 }
47
48 function usersets_hookGet_config($engine) {
49   global $ext;
50   switch($engine) {
51     case "asterisk":
52       $hooklist = usersets_list();
53       if(is_array($hooklist)) {
54         foreach($hooklist as $thisitem) {
55
56           // get the used_by field
57           if(empty($thisitem['used_by'])) {
58             $usedby = "";
59           } else {
60             $usedby = $thisitem['used_by'];
61           }
62
63           // create an array from usedby
64           $arrUsedby = explode(',',$usedby);
65
66           if(is_array($arrUsedby)){
67             foreach($arrUsedby as $strUsedby){
68               // if it's an outbound route
69               if(strpos($strUsedby,'routing_') !== false) {
70                 $route = substr($strUsedby,8);
71                 $context = 'outrt-'.$route;
72
73                 // get all the routes that are in this context
74                 $routes = core_routing_getroutepatterns($route);
75
76                 // we need to manipulate each route/extension
77                 foreach($routes as $rt) {
78                   //strip the pipe out as that's what we use for the dialplan extension
79                   $extension = str_replace('|','',$rt);
80                   // If there are any wildcards in there, add a _ to the start
81                   if (preg_match("/\.|z|x|\[|\]/i", $extension)) { $extension = "_".$extension; }
82                   $ext->splice($context, $extension, 0, new ext_macro('usersets-'. $thisitem['usersets_id']));
83                 }
84
85               }
86             }
87           }
88
89         }
90       }
91     break;
92   }
93 }
94
95
96 //get the existing usersets
97 function usersets_list() {
98   $results = sql("SELECT * FROM usersets","getAll",DB_FETCHMODE_ASSOC);
99   if(is_array($results)){
100     foreach($results as $result){
101       // check to see if we have a dept match for the current AMP User.
102       if (checkDept($result['deptname'])){
103         // return this item's dialplan destination, and the description
104         $allowed[] = $result;
105       }
106     }
107   }
108   if (isset($allowed)) {
109     return $allowed;
110   } else {
111     return null;
112   }
113 }
114
115 function usersets_get($id){
116   $results = sql("SELECT * FROM usersets WHERE usersets_id = '$id'","getRow",DB_FETCHMODE_ASSOC);
117   return $results;
118 }
119
120 function usersets_del($id){
121   global $asterisk_conf;
122
123   $results = sql("DELETE FROM usersets WHERE usersets_id = '$id'","query");
124 }
125
126 function usersets_add($post){
127   if(!usersets_chk($post))
128     return false;
129   extract($post);
130   if(empty($description)) $description = 'Unnamed';
131   $results = sql("INSERT INTO usersets (description,deptname,trustlist,authlist) values (\"$description\",\"$deptname\",\"$trustlist\",\"$authlist\")");
132 }
133
134 function usersets_edit($id,$post){
135   if(!usersets_chk($post))
136     return false;
137   extract($post);
138   if(empty($description)) $description = 'Unnamed';
139   $results = sql("UPDATE usersets SET description = \"$description\", deptname = \"$deptname\", trustlist = \"$trustlist\", authlist = \"$authlist\" WHERE usersets_id = \"$id\"");
140 }
141
142
143 // ensures post vars is valid
144 function usersets_chk($post){
145   return true;
146 }
147
148 //removes a userset from a route and shifts priority for all outbound routing usersets
149 function usersets_adjustroute($route,$action,$routeuserset='',$direction='',$newname='') {
150     $priority = (int)substr($route,0,3);
151     //create a selection of available usersets
152     $usersets = usersets_list();
153   // loop through all the usersets
154   if(is_array($usersets)){
155     foreach($usersets as $userset) {
156
157       // get the used_by field
158       if(empty($userset['used_by'])) {
159         $usedby = "";
160       } else {
161         $usedby = $userset['used_by'];
162       }
163
164       // remove the target if it's already in this row's used_by field
165       //$usedby = str_replace("routing_{$route}","",$usedby);
166
167       // create an array from usedby
168       $arrUsedby = explode(',',$usedby);
169
170       for($i=0;$i<count($arrUsedby);$i++) {
171         if (substr($arrUsedby[$i],0,8)=='routing_') {
172                     switch($action){
173                         case 'delroute':
174                         if ($arrUsedby[$i] == "routing_{$route}") {
175                 unset($arrUsedby[$i]);
176                       }
177                         $usedbypriority = (int)substr($arrUsedby[$i],8,3);
178               $usedbyroute = substr($arrUsedby[$i],12);
179                         if ($usedbypriority > $priority) {
180                             $newpriority = str_pad($usedbypriority - 1, 3, "0", STR_PAD_LEFT);
181                             $arrUsedby[$i] = 'routing_'.$newpriority.'-'.$usedbyroute;
182               }
183             break;
184                         case 'prioritizeroute';
185                           $addpriority = ($direction=='up')?-1:1;
186                         $usedbypriority = (int)substr($arrUsedby[$i],8,3);
187               $usedbyroute = substr($arrUsedby[$i],12);
188                         if ($priority + $addpriority == $usedbypriority) {
189                             $newpriority = str_pad($priority, 3, "0", STR_PAD_LEFT);
190                             $arrUsedby[$i] = 'routing_'.$newpriority.'-'.$usedbyroute;
191               }
192                         if ($arrUsedby[$i] == "routing_{$route}") {
193                             $newpriority = str_pad($priority + $addpriority, 3, "0", STR_PAD_LEFT);
194                             $arrUsedby[$i] = 'routing_'.$newpriority.'-'.$usedbyroute;
195                       }
196
197             break;
198                         case 'renameroute';
199                         if ($arrUsedby[$i] == "routing_{$route}") {
200                             $newpriority = str_pad($priority, 3, "0", STR_PAD_LEFT);
201                             $arrUsedby[$i] = 'routing_'.$newpriority.'-'.$newname;
202                       }
203             break;
204                         case 'editroute';
205                           $usedbyroute = (int)substr($arrUsedby[$i],12);
206                         if ($arrUsedby[$i] == "routing_{$route}") {
207                 unset($arrUsedby[$i]);
208               }
209                         break;
210           }
211         }
212       }
213
214       // save the route in the selected user set
215       if ($routeuserset == $userset['usersets_id'] && $action == 'editroute') {
216         $arrUsedby[] = 'routing_'.$route;
217       }
218
219       // remove any duplicates
220       $arrUsedby = array_values(array_unique($arrUsedby));
221
222       // create a new string
223       $strUsedby = implode($arrUsedby,',');
224
225       // Insure there's no leading or trailing commas
226       $strUsedby = trim ($strUsedby, ',');
227
228
229       // store the used_by column in the DB
230       sql("UPDATE usersets SET used_by = \"{$strUsedby}\" WHERE usersets_id = \"{$userset['usersets_id']}\"");
231     }
232   }
233 }
234
235 // provide hook for routing
236 function usersets_hook_core($viewing_itemid, $target_menuid) {
237   switch ($target_menuid) {
238     // only provide display for outbound routing
239     case 'routing':
240       //create a selection of available usersets
241       $usersets = usersets_list();
242       $hookhtml = '
243         <tr>
244           <td><a href="#" class="info">'._("Permitted User Set").'<span>'._('Optional: Select a Permitted User set to use. If using this option, leave the Route Password field blank.').'</span></a>:</td>
245           <td>
246             <select name="usersets">
247               <option value="">&nbsp;</option>
248       ';
249
250       if (is_array($usersets))
251       {
252         foreach($usersets as $item) {
253           if (isset($viewing_itemid) && $viewing_itemid <> '' && strpos($item['used_by'], "routing_{$viewing_itemid}") !== false) {
254             $selected = "selected=\"selected\"";
255           } else {
256             $selected = '';
257           }
258           $hookhtml .= "<option value={$item['usersets_id']} ".$selected.">{$item['description']}</option>";
259         }
260       }
261       $hookhtml .= '
262             </select>
263           </td>
264         </tr>
265       ';
266       return $hookhtml;
267     break;
268     default:
269         return false;
270     break;
271   }
272 }
273
274 function usersets_hookProcess_core($viewing_itemid, $request) {
275
276   // Record any hook selections made by target modules
277   // We'll add these to the userset's "used_by" column in the format <targetmodule>_<viewing_itemid>
278   // multiple targets could select a single userset, so we'll comma delimiter them
279
280   // this is really a crappy way to store things.
281   // Any module that is hooked by usersets when submitted will result in all the "used_by" fields being re-written
282   switch ($request['display']) {
283         case 'routing':
284     // if routing was using post for the form (incl delete), i wouldn't need all these conditions
285     //    if(isset($request['Submit']) || (isset($request['action']) && ($request['action'] == "delroute" || $request['action'] == "prioritizeroute" || $request['action'] == "renameroute"))) {
286
287       $action = (isset($request['action']))?$request['action']:null;
288       $route = $viewing_itemid;
289       if (isset($request['reporoutekey']) && $action == 'prioritizeroute') {
290                 $outbound_routes = core_routing_getroutenames();
291         $route = $outbound_routes[(int)$request['reporoutekey']][0];
292             }
293       if (isset($request['Submit']) ) {
294         $action = (isset($action))?$action:'editroute';
295           }
296       if (isset($action)) {
297               $direction = (isset($request['reporoutedirection']))?$request['reporoutedirection']:null;
298                 $newname = (isset($request['newroutename']))?$request['newroutename']:null;
299         usersets_adjustroute($route,$action,$request['usersets'],$direction,$newname);
300       }
301
302         break;
303   }
304 }
305
306
307 ?>
Note: See TracBrowser for help on using the browser.