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

Revision 7583, 5.4 kB (checked in by xrobau, 4 years ago)

Realised that I was making this far too difficult.. the outrt-xxx is always the same length, so just stripping off the first 10 characters.

Line 
1 <?php
2 /* $Id$ */
3
4 // Original Release by Rob Thomas (xrobau@gmail.com)
5 // Copyright Rob Thomas (2009)
6 //
7 // Released under the GPL V2 Licence (only)
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of version 2 of the GNU General Public
11 // License as published by the Free Software Foundation.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18
19
20 // This MUST be "TheNameOfTheModule_configpageinit" as it's loaded automatically
21 function routepermissions_configpageinit($pagename) {
22         global $currentcomponent;
23
24         $action = isset($_REQUEST['action'])?$_REQUEST['action']:null;
25         $extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
26         $extension = isset($_REQUEST['extension'])?$_REQUEST['extension']:null;
27         $tech_hardware = isset($_REQUEST['tech_hardware'])?$_REQUEST['tech_hardware']:null;
28
29         // We only want to hook 'users' or 'extensions' pages.
30         if ($pagename != 'users' && $pagename != 'extensions')
31                 return true;
32         // On a 'new' user, 'tech_hardware' is set, and there's no extension. Hook into the page.
33         if ($tech_hardware != null || $pagename == 'users') {
34                 rp_applyhooks();
35                 $currentcomponent->addprocessfunc('rp_configprocess', 5);
36         } elseif ($action=="add") {
37                 // We don't need to display anything on an 'add', but we do need to handle returned data.
38                 $currentcomponent->addprocessfunc('rp_configprocess', 5);
39         } elseif ($extdisplay != '') {
40                 // We're now viewing an extension, so we need to display _and_ process.
41                 rp_applyhooks();
42                 $currentcomponent->addprocessfunc('rp_configprocess', 5);
43         }
44 }
45
46 // This MUST be "TheNameOfTheModule_hookGet_config" to be called when a reload (yellow bar) is clicked.
47 function routepermissions_hookGet_config($engine) {
48         global $ext;
49         global $version;
50         switch($engine) {
51                 case "asterisk":
52             $context="macro-dialout-trunk";
53             $ext->splice($context, 's', 1 ,new ext_agi('checkperms.agi'));
54             $ext->add($context, 'barred', '', new ext_noop('Route administratively banned for this user.'));
55                 break;
56         }
57 }
58
59 function rp_applyhooks() {
60         global $currentcomponent;
61
62         // Add Allow/Deny options
63         $currentcomponent->addoptlistitem('rpyn', 'YES', _('yes'));
64         $currentcomponent->addoptlistitem('rpyn', 'NO', _('no'));
65         $currentcomponent->setoptlistopts('rpyn', 'sort', false);
66
67         // Add the 'proces' function
68         $currentcomponent->addguifunc('rp_configpageload');
69 }
70
71 function rp_configpageload() {
72         global $currentcomponent;
73
74         // Init vars from $_REQUEST[]
75         $action = isset($_REQUEST['action'])?$_REQUEST['action']:null;
76         $extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
77
78         // Don't display if this is a 'This xtn has been deleted' page.
79         if ($action != 'del') {
80                 $section = _('Outbound Route Permssions');
81         $routes = rp_get_routes();
82         foreach ($routes as $route) {
83             $currentcomponent->addguielem($section, new gui_radio("rp_$route", $currentcomponent->getoptlist('rpyn'), rp_get_perm($extdisplay,$route), $route, "" , null));
84         }
85     }
86 }
87
88 function rp_configprocess() {
89     // Extract any variables from $REQUEST that start with rp_
90         $action = isset($_REQUEST['action'])?$_REQUEST['action']:null;
91         $extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null;
92
93     foreach ($_REQUEST as $r=>$val) {
94         if (!strncmp($_REQUEST[$r], "rp_", 3)) {
95             $rps[substr($r, 3)]=$val;
96         }
97     }
98         //if submitting form, update database
99         switch ($action) {
100                 case "add":
101                 case "edit":
102         rp_purge_ext($extdisplay);
103         rp_set_perm($extdisplay, $rps);
104                 break;
105                 case "del":
106         rp_purge_ext($extdisplay);
107                 break;
108         }
109 }
110
111
112
113 function rp_get_perm($ext, $route) {
114     global $db;
115     $Sext = mysql_real_escape_string($ext);
116     $Sroute = mysql_real_escape_string($route);
117     $sql = "SELECT allowed FROM routepermissions WHERE routename='$Sroute' AND exten='$Sext'";
118     $res = $db->getRow($sql);
119     if (PEAR::isError($res)) { die($res->getMessage()); }
120     if (isset($res[0])) {
121         return $res[0];
122     } else {
123         return "YES";
124     }
125 }
126
127 function rp_set_perm($ext, $rps) {
128     global $db;
129     $Sext = mysql_real_escape_string($ext);
130     foreach($rps as $r=>$p) {
131         $val = explode("=", $p);
132         $Sr =mysql_real_escape_string($r);
133         $Sval =mysql_real_escape_string($val[1]);
134         $sql = "INSERT INTO routepermissions (exten, routename, allowed) VALUES ('$Sext', '$Sr', '$Sval')";
135         sql($sql);
136     }
137 }
138
139 function rp_purge_ext($ext) {
140     global $db;
141     $Sext = mysql_real_escape_string($ext);
142     $sql = "DELETE FROM routepermissions WHERE exten='$Sext'";
143     sql($sql);
144 }
145     
146     
147
148 // When outbound routes is rewritten to no longer use the 'extensions' database, the following function will need to be changed
149
150 function rp_get_routes() {
151     global $db;
152     $sql = "SELECT DISTINCT context FROM extensions WHERE context LIKE 'outrt%';";
153     $res = $db->getAll($sql);
154     foreach ($res as $r) {
155         // $r[0] = 'outrt-NNN-routename-goes-here'
156         $arr[] = substr($r[0], 10);
157     }
158     return $arr;
159 }
160     
161 ?>
162
Note: See TracBrowser for help on using the browser.