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

Revision 12895, 7.4 kB (checked in by xrobau, 7 months ago)

Somehow a pile of spaces got in there. Fixed back to tabs.

Line 
1 <?php
2 /* $Id$ */
3
4 // Original Release by Rob Thomas (xrobau@gmail.com)
5 // Copyright Rob Thomas (2009)
6 //
7 /*  This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU Affero General Public License as
9     published by the Free Software Foundation, either version 3 of the
10     License, or (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU Affero General Public License for more details.
16
17     You should have received a copy of the GNU Affero General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22
23 // This MUST be "TheNameOfTheModule_configpageinit" as it's loaded automatically
24 function routepermissions_configpageinit($pagename) {
25   global $currentcomponent;
26
27   $action = isset($_REQUEST['action'])?$_REQUEST['action']:null;
28   $extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
29   $extension = isset($_REQUEST['extension'])?$_REQUEST['extension']:null;
30   $tech_hardware = isset($_REQUEST['tech_hardware'])?$_REQUEST['tech_hardware']:null;
31   // We only want to hook 'users' or 'extensions' pages.
32   if ($pagename != 'users' && $pagename != 'extensions')
33     return true;
34   // On a 'new' user, 'tech_hardware' is set, and there's no extension. Hook into the page.
35   if ($tech_hardware != null || $pagename == 'users') {
36     rp_applyhooks();
37     $currentcomponent->addprocessfunc('rp_configprocess', 5);
38   } elseif ($action=="add") {
39     // We don't need to display anything on an 'add', but we do need to handle returned data.
40     $currentcomponent->addprocessfunc('rp_configprocess', 5);
41   } elseif ($extdisplay != '') {
42     // We're now viewing an extension, so we need to display _and_ process.
43     rp_applyhooks();
44     $currentcomponent->addprocessfunc('rp_configprocess', 5);
45   }
46 }
47
48 // This MUST be "TheNameOfTheModule_hookGet_config" to be called when a reload (yellow bar) is clicked.
49 function routepermissions_hookGet_config($engine) {
50   global $ext;
51   global $version;
52   switch($engine) {
53     case "asterisk":
54       $context="macro-dialout-trunk";
55       $ext->splice($context, 's', 1 ,new ext_agi('checkperms.agi'));
56       $ext->add($context, 'barred', '', new ext_noop('Route administratively banned for this user.'));
57       $ext->add($context, 'reroute', '', new ext_goto('1','${ARG2}','from-internal'));
58
59       $context="macro-dialout-dundi";
60       $ext->splice($context, 's', 1 ,new ext_agi('checkperms.agi'));
61       $ext->add($context, 'barred', '', new ext_noop('Route administratively banned for this user.'));
62       $ext->add($context, 'reroute', '', new ext_goto('1','${ARG2}','from-internal'));
63
64       $context="macro-dialout-enum";
65       $ext->splice($context, 's', 0 ,new ext_agi('checkperms.agi'));
66       $ext->add($context, 'barred', '', new ext_noop('Route administratively banned for this user.'));
67       $ext->add($context, 'reroute', '', new ext_goto('1','${ARG2}','from-internal'));
68
69       // Insert the ROUTENAME into each route
70       //
71       $names = core_routing_list();
72       foreach($names as $name) {
73         $context = 'outrt-'.$name['route_id'];
74         $routename = $name['name'];
75         $routes = core_routing_getroutepatternsbyid($name['route_id']);
76         foreach ($routes as $rt) {
77           $extension = $rt['match_pattern_prefix'].$rt['match_pattern_pass'];
78           // If there are any wildcards in there, add a _ to the start
79           //
80           if (preg_match("/\.|z|x|\[|\]/i", $extension)) {
81             $extension = "_".$extension;
82           }
83           $ext->splice($context, $extension, 1, new ext_setvar('__ROUTENAME',$routename));
84         }
85       }
86     break;
87   }
88 }
89
90 function rp_applyhooks() {
91   global $currentcomponent;
92
93   // Add Allow/Deny options
94   $currentcomponent->addoptlistitem('rpyn', 'YES', _('yes'));
95   $currentcomponent->addoptlistitem('rpyn', 'NO', _('no'));
96   $currentcomponent->setoptlistopts('rpyn', 'sort', false);
97
98   // Add the 'process' function
99   $currentcomponent->addguifunc('rp_configpageload');
100 }
101
102 function rp_configpageload() {
103   global $currentcomponent;
104
105   // Init vars from $_REQUEST[]
106   $action = isset($_REQUEST['action'])?$_REQUEST['action']:null;
107   $extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
108
109   // Don't display if this is a 'This xtn has been deleted' page.
110   if ($action != 'del') {
111     $section = _('Outbound Route Permssions');
112     $routes = rp_get_routes();
113     foreach ($routes as $route) {
114       $currentcomponent->addguielem($section, new gui_radio("rp_$route", $currentcomponent->getoptlist('rpyn'), rp_get_perm($extdisplay,$route), $route, "" , null));
115       $currentcomponent->addguielem($section, new gui_textbox("rp-redir_$route", rp_get_redir($extdisplay, $route), "'".$route."' "._('Redirect Prefix'), _("Add this prefex and try again if denied. READ THE INSTRUCTIONS on the Outbound Permissions page"), "", "", true, 0, null));
116     }
117   }
118 }
119
120 function rp_configprocess() {
121   // Extract any variables from $REQUEST that start with rp_
122   $action = isset($_REQUEST['action'])?$_REQUEST['action']:null;
123   $extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
124   if(isset($_REQUEST['extension']) ){
125     $extdisplay=$_REQUEST['extension'];
126   }
127   $rps = array();
128   $redir = array();
129
130   foreach ($_REQUEST as $r=>$val) {
131     if (!strncmp($_REQUEST[$r], "rp_", 3)) {
132       $rps[substr($r, 3)]=$val;
133     }
134     if (!strncmp($r, "rp-redir_", 9)) {
135       $redir[substr($r, 9)]=$val;
136     }
137   }
138   //if submitting form, update database
139   switch ($action) {
140     case "add":
141     case "edit":
142     rp_purge_ext($extdisplay);
143     rp_set_perm($extdisplay, $rps);
144     rp_set_redir($extdisplay, $redir);
145     break;
146     case "del":
147     rp_purge_ext($extdisplay);
148     break;
149   }
150 }
151
152
153
154 function rp_get_perm($ext, $route) {
155   global $db;
156   $Sext = mysql_real_escape_string($ext);
157   $Sroute = mysql_real_escape_string($route);
158   $sql = "SELECT allowed FROM routepermissions WHERE routename='$Sroute' AND exten='$Sext'";
159   $res = $db->getRow($sql);
160   if (PEAR::isError($res)) { die($res->getMessage()); }
161   if (isset($res[0])) {
162     return $res[0];
163   } else {
164     return "YES";
165   }
166 }
167
168 function rp_get_redir($ext, $route) {
169   global $db;
170   $Sext = mysql_real_escape_string($ext);
171   $Sroute = mysql_real_escape_string($route);
172   $sql = "SELECT faildest FROM routepermissions WHERE routename='$Sroute' AND exten='$Sext'";
173   $res = $db->getRow($sql);
174   if (PEAR::isError($res)) { die($res->getMessage()); }
175   if (isset($res[0])) {
176     return $res[0];
177   } else {
178     return "";
179   }
180 }
181
182 function rp_set_perm($ext, $rps) {
183   global $db;
184   $Sext = mysql_real_escape_string($ext);
185   foreach($rps as $r=>$p) {
186     $val = explode("=", $p);
187     $Sr =mysql_real_escape_string($r);
188     $Sval =mysql_real_escape_string($val[1]);
189     $sql = "INSERT INTO routepermissions (exten, routename, allowed) VALUES ('$Sext', '$Sr', '$Sval')";
190     sql($sql);
191   }
192 }
193
194 function rp_set_redir($ext, $rps) {
195   global $db;
196   $Sext = mysql_real_escape_string($ext);
197   foreach($rps as $r=>$p) {
198     $Sr =mysql_real_escape_string($r);
199     $Sval =mysql_real_escape_string($p);
200     $sql = "UPDATE routepermissions SET faildest='$Sval' where exten='$Sext' and routename='$Sr'";
201     sql($sql);
202   }
203 }
204
205
206 function rp_purge_ext($ext) {
207   global $db;
208   $Sext = mysql_real_escape_string($ext);
209   $sql = "DELETE FROM routepermissions WHERE exten='$Sext'";
210   sql($sql);
211 }
212
213 function rp_get_routes() {
214   global $db;
215   $sql = "SELECT DISTINCT a.name FROM `outbound_routes` a JOIN `outbound_route_sequence` b ON a.route_id = b.route_id ORDER BY `seq`;";
216   $res = $db->getAll($sql);
217   foreach ($res as $r) {
218     $arr[] = $r[0];
219   }
220   return $arr;
221 }
Note: See TracBrowser for help on using the browser.