root/modules/branches/2.3/pinsets/functions.inc.php

Revision 4901, 11.1 kB (checked in by p_lindheimer, 6 years ago)

#2172 replace deprecated use of | with ,

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 <?php /* $Id */
2
3 // a class for generating passwdfile
4 // retrieve_conf will create an object of and <modulename>_conf classes,
5 // which can be used in <modulename>_get_conf below.
6 class pinsets_conf {
7   // return an array of filenames to write
8   // files named like pinset_N
9   function get_filename() {
10     $files = array();
11     if (isset($this->_pinsets) && is_array($this->_pinsets)) {
12       foreach (array_keys($this->_pinsets) as $pinset) {
13         $files[] = 'pinset_'.$pinset;
14       }
15       return $files;
16     } else {
17       // jusr return an empty array
18       return array();
19     }
20   }
21  
22   function addPinsets($setid, $pins) {
23     $this->_pinsets[$setid] = $pins;
24   }
25  
26   // return the output that goes in each of the files
27   function generateConf($file) {
28     $setid = ltrim($file,'pinset_');
29     $output = $this->_pinsets[$setid];
30     return $output;
31   }
32 }
33
34 /*  Generates passwd files for pinsets
35   We call this with retrieve_conf
36 */
37 function pinsets_get_config($engine) {
38   global $ext;  // is this the best way to pass this?
39   global $asterisk_conf;
40   global $pinsets_conf; // our pinsets object (created in retrieve_conf)
41   switch($engine) {
42     case "asterisk":
43       $allpinsets = pinsets_list();
44       if(is_array($allpinsets)) {
45         foreach($allpinsets as $item) {
46           // write our own pin list files
47           $pinsets_conf->addPinsets($item['pinsets_id'],$item['passwords']);
48         }
49        
50         // write out a macro that handles the authenticate
51         $ext->add('macro-pinsets', 's', '', new ext_gotoif('${ARG2} = 1','cdr,1'));
52         $ext->add('macro-pinsets', 's', '', new ext_authenticate($asterisk_conf['astetcdir'].'/pinset_${ARG1}'));
53         // authenticate with the CDR option (a)
54         $ext->add('macro-pinsets', 'cdr', '', new ext_authenticate($asterisk_conf['astetcdir'].'/pinset_${ARG1}','a'));
55       }
56     break;
57   }
58 }
59
60 function pinsets_hookGet_config($engine) {
61   global $ext;
62   switch($engine) {
63     case "asterisk":
64       $hooklist = pinsets_list();
65       if(is_array($hooklist)) {
66         foreach($hooklist as $thisitem) {
67          
68           // get the used_by field
69           if(empty($thisitem['used_by'])) {
70             $usedby = "";
71           } else {
72             $usedby = $thisitem['used_by'];
73           }
74          
75           // create an array from usedby
76           $arrUsedby = explode(',',$usedby);
77          
78           if(is_array($arrUsedby)){
79             foreach($arrUsedby as $strUsedby){
80               // if it's an outbound route
81               if(strpos($strUsedby,'routing_') !== false) {
82                 $route = substr($strUsedby,8);
83                 $context = 'outrt-'.$route;
84                
85                 // get all the routes that are in this context
86                 $routes = core_routing_getroutepatterns($route);
87
88                 // we need to manipulate each route/extension
89                 foreach($routes as $rt) {
90                   //strip the pipe out as that's what we use for the dialplan extension
91                   $extension = str_replace('|','',$rt);
92                   // If there are any wildcards in there, add a _ to the start
93                   if (preg_match("/\.|z|x|\[|\]/i", $extension)) { $extension = "_".$extension; }
94                   $ext->splice($context, $extension, 0, new ext_macro('pinsets', $thisitem['pinsets_id'].','.$thisitem['addtocdr']));
95                 }           
96                
97               }
98             }
99           }
100          
101         }
102       }
103     break;
104   }
105 }
106
107
108 //get the existing meetme extensions
109 function pinsets_list() {
110   $results = sql("SELECT * FROM pinsets","getAll",DB_FETCHMODE_ASSOC);
111   if(is_array($results)){
112     foreach($results as $result){
113       // check to see if we have a dept match for the current AMP User.
114       if (checkDept($result['deptname'])){
115         // return this item's dialplan destination, and the description
116         $allowed[] = $result;
117       }
118     }
119   }
120   if (isset($allowed)) {
121     return $allowed;
122   } else {
123     return null;
124   }
125 }
126
127 function pinsets_get($id){
128   $results = sql("SELECT * FROM pinsets WHERE pinsets_id = '$id'","getRow",DB_FETCHMODE_ASSOC);
129   return $results;
130 }
131
132 function pinsets_del($id){
133   global $asterisk_conf;
134  
135   $filename = $asterisk_conf['astetcdir'].'/pinset_'.$id;
136   if (file_exists($filename)) {
137     unlink($filename);
138   }
139
140   $results = sql("DELETE FROM pinsets WHERE pinsets_id = '$id'","query");
141 }
142
143 function pinsets_add($post){
144   if(!pinsets_chk($post))
145     return false;
146   extract($post);
147   $passwords = pinsets_clean($passwords);
148   if(empty($description)) $description = 'Unnamed';
149   $results = sql("INSERT INTO pinsets (description,passwords,addtocdr,deptname) values (\"$description\",\"$passwords\",\"$addtocdr\",\"$deptname\")");
150 }
151
152 function pinsets_edit($id,$post){
153   if(!pinsets_chk($post))
154     return false;
155   extract($post);
156   $passwords = pinsets_clean($passwords);
157   if(empty($description)) $description = 'Unnamed';
158   $results = sql("UPDATE pinsets SET description = \"$description\", passwords = \"$passwords\", addtocdr = \"$addtocdr\", deptname = \"$deptname\" WHERE pinsets_id = \"$id\"");
159 }
160
161 // clean and remove duplicates
162 function pinsets_clean($passwords) {
163
164   $passwords = explode("\n",$passwords);
165
166   if (!$passwords) {
167     $passwords = null;
168   }
169  
170   foreach (array_keys($passwords) as $key) {
171     //trim it
172     $passwords[$key] = trim($passwords[$key]);
173    
174     // remove invalid chars
175     $passwords[$key] = preg_replace("/[^0-9#*]/", "", $passwords[$key]);
176    
177     // remove blanks
178     if ($passwords[$key] == "") unset($passwords[$key]);
179   }
180  
181   // check for duplicates, and re-sequence
182   $passwords = array_values(array_unique($passwords));
183  
184   if (is_array($passwords))
185     return implode($passwords,"\n");
186   else
187     return "";
188 }
189
190 // ensures post vars is valid
191 function pinsets_chk($post){
192   return true;
193 }
194
195 //removes a pinset from a route and shifts priority for all outbound routing pinsets
196 function pinsets_adjustroute($route,$action,$routepinset='',$direction='',$newname='') {
197     $priority = (int)substr($route,0,3);
198     //create a selection of available pinsets
199     $pinsets = pinsets_list();
200   // loop through all the pinsets
201   if(is_array($pinsets)){
202     foreach($pinsets as $pinset) {
203        
204       // get the used_by field
205       if(empty($pinset['used_by'])) {
206         $usedby = "";
207       } else {
208         $usedby = $pinset['used_by'];
209       }
210          
211       // remove the target if it's already in this row's used_by field
212       //$usedby = str_replace("routing_{$route}","",$usedby);
213          
214       // create an array from usedby
215       $arrUsedby = explode(',',$usedby);
216      
217       for($i=0;$i<count($arrUsedby);$i++) {
218         if (substr($arrUsedby[$i],0,8)=='routing_') {
219                     switch($action){
220                         case 'delroute':
221                         if ($arrUsedby[$i] == "routing_{$route}") {
222                 unset($arrUsedby[$i]);
223                       }
224                         $usedbypriority = (int)substr($arrUsedby[$i],8,3);
225               $usedbyroute = substr($arrUsedby[$i],12);
226                         if ($usedbypriority > $priority) {
227                             $newpriority = str_pad($usedbypriority - 1, 3, "0", STR_PAD_LEFT);
228                             $arrUsedby[$i] = 'routing_'.$newpriority.'-'.$usedbyroute;
229               }
230             break;
231                         case 'prioritizeroute';
232                           $addpriority = ($direction=='up')?-1:1;
233                         $usedbypriority = (int)substr($arrUsedby[$i],8,3);
234               $usedbyroute = substr($arrUsedby[$i],12);
235                         if ($priority + $addpriority == $usedbypriority) {
236                             $newpriority = str_pad($priority, 3, "0", STR_PAD_LEFT);
237                             $arrUsedby[$i] = 'routing_'.$newpriority.'-'.$usedbyroute;
238               }
239                         if ($arrUsedby[$i] == "routing_{$route}") {
240                             $newpriority = str_pad($priority + $addpriority, 3, "0", STR_PAD_LEFT);
241                             $arrUsedby[$i] = 'routing_'.$newpriority.'-'.$usedbyroute;
242                       }
243
244             break;
245                         case 'renameroute';
246                         if ($arrUsedby[$i] == "routing_{$route}") {
247                             $newpriority = str_pad($priority, 3, "0", STR_PAD_LEFT);
248                             $arrUsedby[$i] = 'routing_'.$newpriority.'-'.$newname;
249                       }
250             break;
251                         case 'editroute';
252                           $usedbyroute = (int)substr($arrUsedby[$i],12);
253                         if ($arrUsedby[$i] == "routing_{$route}") {
254                 unset($arrUsedby[$i]);
255               }
256                         break;
257           }
258         }
259       }
260            
261       // save the route in the selected pin
262       if ($routepinset == $pinset['pinsets_id'] && $action == 'editroute') {
263         $arrUsedby[] = 'routing_'.$route;
264       }
265
266       // remove any duplicates
267       $arrUsedby = array_values(array_unique($arrUsedby));
268        
269       // create a new string
270       $strUsedby = implode($arrUsedby,',');
271  
272       // Insure there's no leading or trailing commas
273       $strUsedby = trim ($strUsedby, ',');
274
275          
276       // store the used_by column in the DB
277       sql("UPDATE pinsets SET used_by = \"{$strUsedby}\" WHERE pinsets_id = \"{$pinset['pinsets_id']}\"");
278     }
279   }
280 }
281
282 // provide hook for routing
283 function pinsets_hook_core($viewing_itemid, $target_menuid) {
284   switch ($target_menuid) {
285     // only provide display for outbound routing
286     case 'routing':
287       //create a selection of available pinsets
288       $pinsets = pinsets_list();
289       $hookhtml = '
290         <tr>
291           <td><a href="#" class="info">'._("PIN Set").'<span>'._('Optional: Select a PIN set to use. If using this option, leave the Route Password field blank.').'</span></a>:</td>
292           <td>
293             <select name="pinsets">
294               <option value=>'._('None').'</option>
295       ';
296
297       if (is_array($pinsets))
298       {
299         foreach($pinsets as $item) {
300           if (isset($viewing_itemid) && $viewing_itemid <> '' && strpos($item['used_by'], "routing_{$viewing_itemid}") !== false) {
301             $selected = 'selected';
302           } else {
303             $selected = '';
304           }
305           $hookhtml .= "<option value={$item['pinsets_id']} ".$selected.">{$item['description']}</option>";
306         }
307       }
308       $hookhtml .= '
309             </select>
310           </td>
311         </tr>
312       ';
313       return $hookhtml;
314     break;
315     default:
316         return false;
317     break;
318   }
319 }
320
321 function pinsets_hookProcess_core($viewing_itemid, $request) {
322
323   // Record any hook selections made by target modules
324   // We'll add these to the pinset's "used_by" column in the format <targetmodule>_<viewing_itemid>
325   // multiple targets could select a single pinset, so we'll comma delimiter them
326  
327   // this is really a crappy way to store things. 
328   // Any module that is hooked by pinsets when submitted will result in all the "used_by" fields being re-written
329   switch ($request['display']) {
330         case 'routing':
331     // if routing was using post for the form (incl delete), i wouldn't need all these conditions
332     //    if(isset($request['Submit']) || (isset($request['action']) && ($request['action'] == "delroute" || $request['action'] == "prioritizeroute" || $request['action'] == "renameroute"))) {       
333
334       $action = (isset($request['action']))?$request['action']:null;
335       $route = $viewing_itemid;
336       if (isset($request['reporoutekey']) && $action == 'prioritizeroute') {
337                 $outbound_routes = core_routing_getroutenames();
338         $route = $outbound_routes[(int)$request['reporoutekey']][0];
339             }
340       if (isset($request['Submit']) ) {
341         $action = (isset($action))?$action:'editroute';
342           }
343       if (isset($action)) {           
344               $direction = (isset($request['reporoutedirection']))?$request['reporoutedirection']:null;
345                 $newname = (isset($request['newroutename']))?$request['newroutename']:null;
346         pinsets_adjustroute($route,$action,$request['pinsets'],$direction,$newname);
347       }
348
349         break;
350   }
351 }
352
353
354 ?>
Note: See TracBrowser for help on using the browser.