r5023 ability to override and extend AJAX dispatchers in plugins.
[racktables] / wwwroot / inc / ajax-interface.php
CommitLineData
65e557dd 1<?php
cddbb9fd
DO
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
65e557dd
AA
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
16function formatPortLinkHints ($object_id)
17{
18 $result = array();
80019a2c 19 $linkStatus = queryDevice ($object_id, 'getportstatus');
65e557dd
AA
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);
19f22ad8 44 $visible_part = "<img width=16 height=16 src='?module=chrome&uri=pix/${img_filename}'>";
65e557dd
AA
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']]))
19f22ad8 52 $result[$port['name']]['inline'] = "<img width=16 height=16 src='?module=chrome&uri=pix/1x1t.gif'>";
65e557dd
AA
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
58function formatPortMacHints ($object_id)
59{
60 $result = array();
80019a2c 61 $macList = queryDevice ($object_id, 'getmaclist');
65e557dd
AA
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
80function formatPortConfigHints ($object_id, $R = NULL)
81{
82 $result = array();
83 if (! isset ($R))
84 $R = getRunning8021QConfig ($object_id);
fa9e68cd
AA
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 }
65e557dd
AA
92 return $result;
93}
94
5c6225b1
AA
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
100function 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
a8dc2982
AA
114// returns an array with two items - each is HTML-formatted <TD> tag
115function formatPortReservation ($port)
116{
117 $ret = array();
5c6225b1
AA
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>';
a8dc2982
AA
124 return $ret;
125}
126
7bbe121d 127function getTagSelectAJAX()
9f4f431c 128{
7bbe121d
AA
129 global $taglist;
130 $options = array();
131 $selected_id = '';
132 if (! isset($_REQUEST['tagid']))
133 $options['error'] = "Sorry, param 'tagid' is empty. Reload page and try again";
134 elseif (! preg_match("/tagid_(\d+)/i", $_REQUEST['tagid'], $m))
135 $options['error'] = "Sorry, wrong format tagid:'".$_REQUEST['tagid']."'. Reload page and try again";
136 else
9f4f431c 137 {
7bbe121d
AA
138 $current_tag_id = $m[1];
139 $selected_id = $taglist[$current_tag_id]['parent_id'];
140 echo $selected_id;
141 $options[0] = '-- NONE --';
142 foreach ($taglist as $tag_id => $taginfo)
143 if (! in_array ($current_tag_id, $taginfo['trace']) && $current_tag_id != $tag_id)
144 $options[$tag_id] = $taginfo['tag'];
145 }
146 foreach ($options as $tag_id => $value)
147 echo "<option value='$tag_id'" .
148 ($tag_id == $selected_id ? ' selected' : '') .
149 '>' . htmlspecialchars ($value) . '</option>';
150}
151
152function verifyCodeAJAX()
153{
154 global $pageno, $tabno;
155 $pageno = 'perms';
156 $tabno = 'edit';
157 fixContext();
158 assertPermission();
159 genericAssertion ('code', 'string');
160 $result = getRackCode (dos2unix ($_REQUEST['code']));
161 if ($result['result'] == 'ACK')
162 echo "ACK\n";
163 else
164 echo "NAK\n" . $result['load'];
165}
166
167// echoes JSON-encoded text
168function getPortInfoAJAX()
169{
170 $funcmap = array
171 (
172 'get-port-link' => 'formatPortLinkHints',
173 'get-port-mac' => 'formatPortMacHints',
174 'get-port-conf' => 'formatPortConfigHints',
175 );
176 $opmap = array
177 (
178 'get-port-link' => 'get_link_status',
179 'get-port-mac' => 'get_mac_list',
180 'get-port-conf' => 'get_port_conf',
181 );
182 genericAssertion ('object_id', 'uint');
183 fixContext (spotEntity ('object', $_REQUEST['object_id']));
184 assertPermission ('object', 'liveports', $opmap[$_REQUEST['ac']]);
185 echo json_encode ($funcmap[$_REQUEST['ac']] ($_REQUEST['object_id']));
186}
187
188function updatePortRsvAJAX()
189{
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']);
a8dc2982 203 $port_info = getPortInfo ($sic['id']);
9f4f431c 204 }
7bbe121d
AA
205 $tds = formatPortReservation ($port_info);
206 echo json_encode ($tds);
207}
208
209function updateIPRsvAJAX()
210{
211 global $sic;
212 assertStringArg ('comment', TRUE);
213 $ip_bin = assertIPArg ('id');
214 $addr = getIPAddress ($ip_bin);
215 if (! empty ($addr['allocs']) && empty ($addr['name']))
216 throw new RackTablesError ('Cant update IP comment: address is allocated');
217 $net = spotNetworkByIP ($ip_bin);
218 if (isset ($net))
219 fixContext ($net);
220 assertPermission ((strlen ($ip_bin) == 16 ? 'ipv6net' : 'ipv4net'), NULL, 'set_reserve_comment');
221 $reserved = (empty ($sic['comment']) ? 'no' : $addr['reserved']); // unset reservation if user clears comment
222 updateAddress ($ip_bin, $sic['comment'], $reserved);
223 echo json_encode ('OK');
224}
225
226function getNetUsageAJAX()
227{
228 assertStringArg ('net_id');
229 list ($ip, $mask) = explode ('/', $_REQUEST['net_id']);
230 $ip_bin = ip_parse ($ip);
231 $net = spotNetworkByIP ($ip_bin, $mask + 1);
232 if (! isset ($net) or $net['mask'] != $mask)
233 $net = constructIPRange ($ip_bin, $mask);
234 loadIPAddrList ($net);
235 echo getRenderedIPNetCapacity ($net);
9f4f431c
DO
236}
237
65e557dd 238?>