70756696ff527de16a5d46106737de342c57858c
[racktables] / wwwroot / inc / ajax-interface.php
1 <?php
2
3 # This file is a part of RackTables, a datacenter and server room management
4 # framework. See accompanying file "COPYING" for the full copyright and
5 # licensing information.
6
7 /*
8 *
9 * This file contains HTML-generating funcitons which are needed in ajax handler
10 *
11 *
12 */
13
14 // retrieves ports link status from switch and formats results to dynamic-HTML
15 // returns array which could be packed into json and passed to client's browser
16 function formatPortLinkHints ($object_id)
17 {
18 $result = array();
19 $linkStatus = queryDevice ($object_id, 'getportstatus');
20 foreach ($linkStatus as $portname => $link_info)
21 {
22 $link_info = $linkStatus[$portname];
23 switch ($link_info['status'])
24 {
25 case 'up':
26 $img_filename = 'link-up.png';
27 break;
28 case 'down':
29 $img_filename = 'link-down.png';
30 break;
31 case 'disabled':
32 $img_filename = 'link-disabled.png';
33 break;
34 }
35
36 $hidden_lines = array();
37 $hidden_lines[] = $portname . ': ' . $link_info['status'];
38 if (isset ($link_info['speed']))
39 $hidden_lines[] = 'Speed: ' . $link_info['speed'];
40 if (isset ($link_info['duplex']))
41 $hidden_lines[] = 'Duplex: ' . $link_info['duplex'];
42 if (count ($hidden_lines))
43 $result[$portname]['popup'] = implode ('<br>', $hidden_lines);
44 $visible_part = "<img width=16 height=16 src='?module=chrome&uri=pix/${img_filename}'>";
45 $result[$portname]['inline'] = $visible_part;
46 }
47 // put empty pictures for not-found ports
48 $object = spotEntity ('object', $object_id);
49 amplifyCell ($object);
50 foreach ($object['ports'] as $port)
51 if (! isset ($result[$port['name']]))
52 $result[$port['name']]['inline'] = "<img width=16 height=16 src='?module=chrome&uri=pix/1x1t.gif'>";
53 return $result;
54 }
55
56 // retrieves MAC address list from switch and formats results to dynamic-HTML
57 // returns array which could be packed into json and passed to client's browser
58 function formatPortMacHints ($object_id)
59 {
60 $result = array();
61 $macList = queryDevice ($object_id, 'getmaclist');
62 foreach ($macList as $portname => $list)
63 {
64 $list = $macList[$portname];
65 $visible_part = count ($list) . ' MACs';
66 $result[$portname]['inline'] = $visible_part;
67 if (count ($list))
68 {
69 $hidden_part = '<table width="100%"><tr><th>MAC<th>VID</tr>';
70 foreach ($list as $mac)
71 $hidden_part .= '<tr><td>' . $mac['mac'] . '<td>' . $mac['vid'] . '</tr>';
72 $result[$portname]['popup'] = $hidden_part;
73 }
74 }
75 return $result;
76 }
77
78 // retrieves port configs from switch and formats results to dynamic-HTML
79 // returns array which could be packed into json and passed to client's browser
80 function formatPortConfigHints ($object_id, $R = NULL)
81 {
82 $result = array();
83 if (! isset ($R))
84 $R = getRunning8021QConfig ($object_id);
85 foreach ($R['portconfig'] as $portname => $portconfig)
86 {
87 $hidden_part = '';
88 foreach ($portconfig as $line)
89 $hidden_part .= '<span class="'. $line['type'] . '">' . htmlentities ($line['line']) . '</span><br />';
90 $result[$portname]['popup'] = $hidden_part;
91 }
92 return $result;
93 }
94
95 // returns html-formatted span tag with last changed in title
96 // takes 3 args:
97 // log_item - array with keys 'user', 'time'. Could be empty
98 // text - the text placed into the span
99 // html_class - the additional css class
100 function formatLoggedSpan ($log_item, $text, $html_class = '')
101 {
102 $title = '';
103 if (! empty ($log_item))
104 {
105 $html_class = trim ($html_class . ' hover-history');
106 $title = htmlspecialchars ($log_item['user'] . ', ' . formatAge ($log_item['time']), ENT_QUOTES);
107 }
108 return "<span" .
109 (strlen ($html_class) ? " class='$html_class'" : '') .
110 (strlen ($title) ? " title='$title'" : '') .
111 ">$text</span>";
112 }
113
114 // returns an array with two items - each is HTML-formatted <TD> tag
115 function formatPortReservation ($port)
116 {
117 $ret = array();
118 $ret[] = '<td class=tdleft>' .
119 (strlen ($port['reservation_comment']) ? formatLoggedSpan ($port['last_log'], 'Reserved:', 'strong underline') : '').
120 '</td>';
121 $ret[] = '<td class="rsv-port tdleft">' .
122 formatLoggedSpan ($port['last_log'], $port['reservation_comment'], 'rsvtext') .
123 '</td>';
124 return $ret;
125 }
126
127 function dispatchAJAXRequest()
128 {
129 genericAssertion ('ac', 'string');
130 switch ($_REQUEST['ac'])
131 {
132 case 'get-tag-select':
133 global $taglist;
134 $options = array();
135 $selected_id = '';
136 if (! isset($_REQUEST['tagid']))
137 $options['error'] = "Sorry, param 'tagid' is empty. Reload page and try again";
138 elseif (! preg_match("/tagid_(\d+)/i", $_REQUEST['tagid'], $m))
139 $options['error'] = "Sorry, wrong format tagid:'".$_REQUEST['tagid']."'. Reload page and try again";
140 else
141 {
142 $current_tag_id = $m[1];
143 $selected_id = $taglist[$current_tag_id]['parent_id'];
144 echo $selected_id;
145 $options[0] = '-- NONE --';
146 foreach ($taglist as $tag_id => $taginfo)
147 if (! in_array ($current_tag_id, $taginfo['trace']) && $current_tag_id != $tag_id)
148 $options[$tag_id] = $taginfo['tag'];
149 }
150 foreach ($options as $tag_id => $value)
151 echo "<option value='$tag_id'" .
152 ($tag_id == $selected_id ? ' selected' : '') .
153 '>' . htmlspecialchars ($value) . '</option>';
154 break;
155 case 'verifyCode':
156 global $pageno, $tabno;
157 $pageno = 'perms';
158 $tabno = 'edit';
159 fixContext();
160 assertPermission();
161 genericAssertion ('code', 'string');
162 $result = getRackCode (dos2unix ($_REQUEST['code']));
163 if ($result['result'] == 'ACK')
164 echo "ACK\n";
165 else
166 echo "NAK\n" . $result['load'];
167 break;
168 # returns JSON-encoded text
169 case 'get-port-link':
170 case 'get-port-mac':
171 case 'get-port-conf':
172 $funcmap = array
173 (
174 'get-port-link' => 'formatPortLinkHints',
175 'get-port-mac' => 'formatPortMacHints',
176 'get-port-conf' => 'formatPortConfigHints',
177 );
178 $opmap = array
179 (
180 'get-port-link' => 'get_link_status',
181 'get-port-mac' => 'get_mac_list',
182 'get-port-conf' => 'get_port_conf',
183 );
184 genericAssertion ('object_id', 'uint');
185 fixContext (spotEntity ('object', $_REQUEST['object_id']));
186 assertPermission ('object', 'liveports', $opmap[$_REQUEST['ac']]);
187 echo json_encode ($funcmap[$_REQUEST['ac']] ($_REQUEST['object_id']));
188 break;
189 case 'upd-reservation-port':
190 global $sic;
191 assertUIntArg ('id');
192 assertStringArg ('comment', TRUE);
193 $port_info = getPortInfo ($sic['id']);
194 fixContext (spotEntity ('object', $port_info['object_id']));
195 assertPermission ('object', 'ports', 'set_reserve_comment');
196 if ($port_info['linked'])
197 throw new RackTablesError ('Cant update port comment: port is already linked');
198 if (! isset ($port_info['reservation_comment']))
199 $port_info['reservation_comment'] = '';
200 if ($port_info['reservation_comment'] !== $sic['comment'])
201 {
202 commitUpdatePortComment ($sic['id'], $sic['comment']);
203 $port_info = getPortInfo ($sic['id']);
204 }
205 $tds = formatPortReservation ($port_info);
206 echo json_encode ($tds);
207 break;
208 case 'upd-reservation-ip':
209 global $sic;
210 assertStringArg ('comment', TRUE);
211 $ip_bin = assertIPArg ('id');
212 $addr = getIPAddress ($ip_bin);
213 if (! empty ($addr['allocs']) && empty ($addr['name']))
214 throw new RackTablesError ('Cant update IP comment: address is allocated');
215 $net = spotNetworkByIP ($ip_bin);
216 if (isset ($net))
217 fixContext ($net);
218 assertPermission ((strlen ($ip_bin) == 16 ? 'ipv6net' : 'ipv4net'), NULL, 'set_reserve_comment');
219 $reserved = (empty ($sic['comment']) ? 'no' : $addr['reserved']); // unset reservation if user clears comment
220 updateAddress ($ip_bin, $sic['comment'], $reserved);
221 echo json_encode ('OK');
222 break;
223 case 'net-usage':
224 assertStringArg ('net_id');
225 list ($ip, $mask) = explode ('/', $_REQUEST['net_id']);
226 $ip_bin = ip_parse ($ip);
227 $net = spotNetworkByIP ($ip_bin, $mask + 1);
228 if (! isset ($net) or $net['mask'] != $mask)
229 $net = constructIPRange ($ip_bin, $mask);
230 loadIPAddrList ($net);
231 echo getRenderedIPNetCapacity ($net);
232 break;
233 default:
234 throw new InvalidRequestArgException ('ac', $_REQUEST['ac']);
235 }
236 }
237
238 ?>