root/modules/branches/2.4/manager/functions.inc.php

Revision 2521, 4.3 kB (checked in by qldrob, 6 years ago)

More #999 fixes

  • Property svn:mime-type set to text/html
  • Property svn:eol-style set to native
Line 
1 <?php
2
3 function manager_gen_conf() {
4   $file = "/tmp/manager_additional_".rand().".conf";
5   $content = "";
6   $managers = manager_list();
7   if (is_array($managers)) {
8     foreach ($managers as $manager) {
9       $res = manager_get($manager['name']);
10       $content .= "[".$res['name']."]\n";
11       $content .= "secret = ".$res['secret']."\n";
12       $tmp = explode("&", $res['deny']);
13       foreach ($tmp as $item) {
14         $content .= "deny=$item\n";
15       }
16       $tmp = explode("&", $res['permit']);
17       foreach ($tmp as $item) {
18         $content .= "permit=$item\n";
19       }
20       $content .= "read = ".$res['read']."\n";
21       $content .= "write = ".$res['write']."\n";
22       $content .= "\n";
23     }
24   }
25   $fd = fopen($file, "w");
26   fwrite($fd, $content);
27   fclose($fd);
28   if (!rename($file, "/etc/asterisk/manager_additional.conf")) {
29     echo "<script>javascript:alert('"._("Error writing the manager additional file.")."');</script>";
30   }
31 }
32
33 // Get the manager list
34 function manager_list() {
35   global $db;
36   $sql = "SELECT name, secret FROM manager ORDER BY name";
37   $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
38   if(DB::IsError($res)) {
39     return null;
40   }
41   return $res;
42 }
43
44 // Get manager infos
45 function manager_get($p_name) {
46   global $db;
47   $sql = "SELECT name,secret,deny,permit,`read`,`write` FROM manager WHERE name = '$p_name'";
48   $res = $db->getRow($sql, DB_FETCHMODE_ASSOC);
49   return $res;
50 }
51
52 // Used to set the correct values for the html checkboxes
53 function manager_format_out($p_tab) {
54   $res['name'] = $p_tab['name'];
55   $res['secret'] = $p_tab['secret'];
56   $res['deny'] = $p_tab['deny'];
57   $res['permit'] = $p_tab['permit'];
58
59   $tmp = explode(',', $p_tab['read']);
60   foreach($tmp as $item) {
61     $res['r'.$item] = true;
62   }
63
64   $tmp = explode(',', $p_tab['write']);
65   foreach($tmp as $item) {
66     $res['w'.$item] = true;
67   }
68
69   return $res;
70 }
71
72 // Delete a manager
73 function manager_del($p_name) {
74   $results = sql("DELETE FROM manager WHERE name = \"$p_name\"","query");
75 }
76
77 function manager_format_in($p_tab) {
78   if (!isset($res['read'])) {
79     $res['read'] = "";
80   }
81   if (!isset($res['write'])) {
82     $res['write'] = "";
83   }
84   if (isset($p_tab['rsystem']))
85     $res['read'] .= "system,";
86   if (isset($p_tab['rcall']))
87     $res['read'] .= "call,";
88   if (isset($p_tab['rlog']))
89     $res['read'] .= "log,";
90   if (isset($p_tab['rverbose']))
91     $res['read'] .= "verbose,";
92   if (isset($p_tab['rcommand']))
93     $res['read'] .= "command,";
94   if (isset($p_tab['ragent']))
95     $res['read'] .= "agent,";
96   if (isset($p_tab['ruser']))
97     $res['read'] .= "user";
98
99   if (isset($p_tab['wsystem']))
100     $res['write'] .= "system,";
101   if (isset($p_tab['wcall']))
102     $res['write'] .= "call,";
103   if (isset($p_tab['wlog']))
104     $res['write'] .= "log,";
105   if (isset($p_tab['wverbose']))
106     $res['write'] .= "verbose,";
107   if (isset($p_tab['wcommand']))
108     $res['write'] .= "command,";
109   if (isset($p_tab['wagent']))
110     $res['write'] .= "agent,";
111   if (isset($p_tab['wuser']))
112     $res['write'] .= "user";
113  
114   return $res;
115 }
116
117 // Add a manager
118 function manager_add($p_name, $p_secret, $p_deny, $p_permit, $p_read, $p_write) {
119   $managers = manager_list();
120   if (is_array($managers)) {
121     foreach ($managers as $manager) {
122       if ($manager['name'] === $p_name) {
123         echo "<script>javascript:alert('"._("This manager already exists")."');</script>";
124         return false;
125       }
126     }
127   }
128   $results = sql("INSERT INTO manager set name='$p_name' , secret='$p_secret' , deny='$p_deny' , permit='$p_permit' , `read`='$p_read' , `write`='$p_write'");
129 }
130
131
132 // Asterisk API Module hooking
133 // Input:
134 //   $p_manager = default selected user
135 //   $dummy = unused
136 // $viewing_itemid, $target_menuid
137 function manager_hook_phpagiconf($viewing_itemid, $target_menuid) {
138         global $db;
139
140   switch($target_menuid) {
141     case 'phpagiconf':
142       $sql = "SELECT asman_user FROM phpagiconf";
143       $res = $db->getRow($sql, DB_FETCHMODE_ASSOC);
144       if(DB::IsError($res)) {
145         return null;
146       }
147       $selectedmanager = $res['asman_user'];
148     break;
149   }
150   $output = "<tr><td><a href=\"#\" class=\"info\">"._("Choose Manager:")."<span>"._("Choose the user that PHPAGI will use to connect the Asterisk API.")."</span></a></td><td><select name=\"asmanager\">";
151   $selected = "";
152   $managers = manager_list();
153   foreach ($managers as $manager) {
154     ($manager['name'] === $selectedmanager) ? $selected="selected=\"selected\"" : $selected="";
155     $output .= "<option value=\"".$manager['name']."/".$manager['secret']."\" $selected>".$manager['name'];
156   }
157   $output .="</select></td></tr>";
158   return $output;
159 }
160
161 ?>
Note: See TracBrowser for help on using the browser.