IP addresses inherit tags from their parent networks (#375)
[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
7bbe121d 114function getTagSelectAJAX()
9f4f431c 115{
7bbe121d
AA
116 global $taglist;
117 $options = array();
118 $selected_id = '';
119 if (! isset($_REQUEST['tagid']))
120 $options['error'] = "Sorry, param 'tagid' is empty. Reload page and try again";
121 elseif (! preg_match("/tagid_(\d+)/i", $_REQUEST['tagid'], $m))
122 $options['error'] = "Sorry, wrong format tagid:'".$_REQUEST['tagid']."'. Reload page and try again";
123 else
9f4f431c 124 {
7bbe121d
AA
125 $current_tag_id = $m[1];
126 $selected_id = $taglist[$current_tag_id]['parent_id'];
127 echo $selected_id;
128 $options[0] = '-- NONE --';
129 foreach ($taglist as $tag_id => $taginfo)
130 if (! in_array ($current_tag_id, $taginfo['trace']) && $current_tag_id != $tag_id)
131 $options[$tag_id] = $taginfo['tag'];
132 }
133 foreach ($options as $tag_id => $value)
134 echo "<option value='$tag_id'" .
135 ($tag_id == $selected_id ? ' selected' : '') .
136 '>' . htmlspecialchars ($value) . '</option>';
137}
138
e1add254
AD
139function getLocationSelectAJAX()
140{
2056822d
AA
141 $locationlist = listCells ('location');
142 $locationtree = treeFromList ($locationlist); // adds ['trace'] keys into $locationlist items
e1add254
AD
143 $options = array();
144 $selected_id = '';
145 if (! isset($_REQUEST['locationid']))
146 $options['error'] = "Sorry, param 'locationid' is empty. Reload page and try again";
147 elseif (! preg_match("/locationid_(\d+)/i", $_REQUEST['locationid'], $m))
148 $options['error'] = "Sorry, wrong format locationid:'".$_REQUEST['locationid']."'. Reload page and try again";
149 else
150 {
151 $current_location_id = $m[1];
152 $selected_id = $locationlist[$current_location_id]['parent_id'];
153 echo $selected_id;
154 $options[0] = '-- NONE --';
155 foreach ($locationlist as $location_id => $locationinfo)
156 if (! in_array ($current_location_id, $locationinfo['trace']) && $current_location_id != $location_id)
157 $options[$location_id] = $locationinfo['name'];
158 }
159 foreach ($options as $location_id => $value)
160 echo "<option value='$location_id'" .
161 ($location_id == $selected_id ? ' selected' : '') .
162 '>' . htmlspecialchars ($value) . '</option>';
163}
164
7bbe121d
AA
165function verifyCodeAJAX()
166{
167 global $pageno, $tabno;
168 $pageno = 'perms';
169 $tabno = 'edit';
170 fixContext();
171 assertPermission();
172 genericAssertion ('code', 'string');
173 $result = getRackCode (dos2unix ($_REQUEST['code']));
174 if ($result['result'] == 'ACK')
175 echo "ACK\n";
176 else
177 echo "NAK\n" . $result['load'];
178}
179
180// echoes JSON-encoded text
181function getPortInfoAJAX()
182{
183 $funcmap = array
184 (
185 'get-port-link' => 'formatPortLinkHints',
186 'get-port-mac' => 'formatPortMacHints',
187 'get-port-conf' => 'formatPortConfigHints',
188 );
189 $opmap = array
190 (
191 'get-port-link' => 'get_link_status',
192 'get-port-mac' => 'get_mac_list',
193 'get-port-conf' => 'get_port_conf',
194 );
195 genericAssertion ('object_id', 'uint');
196 fixContext (spotEntity ('object', $_REQUEST['object_id']));
197 assertPermission ('object', 'liveports', $opmap[$_REQUEST['ac']]);
198 echo json_encode ($funcmap[$_REQUEST['ac']] ($_REQUEST['object_id']));
199}
200
201function updatePortRsvAJAX()
202{
203 global $sic;
204 assertUIntArg ('id');
0b8d8309 205 assertStringArg ('text', TRUE);
7bbe121d
AA
206 $port_info = getPortInfo ($sic['id']);
207 fixContext (spotEntity ('object', $port_info['object_id']));
0b8d8309 208 assertPermission ('object', 'ports', 'editPort');
7bbe121d
AA
209 if ($port_info['linked'])
210 throw new RackTablesError ('Cant update port comment: port is already linked');
211 if (! isset ($port_info['reservation_comment']))
212 $port_info['reservation_comment'] = '';
0b8d8309
AA
213 if ($port_info['reservation_comment'] !== $sic['text'])
214 commitUpdatePortComment ($sic['id'], $sic['text']);
215 echo 'OK';
7bbe121d
AA
216}
217
04370908 218function updateIPNameAJAX()
7bbe121d
AA
219{
220 global $sic;
0b8d8309 221 assertStringArg ('text', TRUE);
7bbe121d
AA
222 $ip_bin = assertIPArg ('id');
223 $addr = getIPAddress ($ip_bin);
224 if (! empty ($addr['allocs']) && empty ($addr['name']))
cc2fa820 225 throw new RackTablesError ('Cant update IP name: address is allocated');
7bbe121d
AA
226 $net = spotNetworkByIP ($ip_bin);
227 if (isset ($net))
228 fixContext ($net);
0b8d8309 229 assertPermission ('ipaddress', 'properties', 'editAddress');
cc2fa820
AD
230 $reserved = (empty ($sic['text']) ? 'no' : $addr['reserved']); // unset reservation if user clears name
231 $comment = (empty ($addr['comment']) ? '' : $addr['comment']);
18a77e3c 232 updateAddress ($ip_bin, $sic['text'], $reserved, $comment);
cc2fa820
AD
233 echo 'OK';
234}
235
04370908 236function updateIPCommentAJAX()
cc2fa820
AD
237{
238 global $sic;
239 assertStringArg ('text', TRUE);
240 $ip_bin = assertIPArg ('id');
241 $addr = getIPAddress ($ip_bin);
242 $net = spotNetworkByIP ($ip_bin);
243 if (isset ($net))
244 fixContext ($net);
245 assertPermission ('ipaddress', 'properties', 'editAddress');
18a77e3c 246 updateAddress ($ip_bin, $addr['name'], $addr['reserved'], $sic['text']);
0b8d8309
AA
247 echo 'OK';
248}
249
250function updateCableIdAJAX()
251{
252 global $sic;
253 assertUIntArg ('id');
254 assertStringArg ('text', TRUE);
255 $port_info = getPortInfo ($sic['id']);
256 fixContext (spotEntity ('object', $port_info['object_id']));
257 assertPermission ('object', 'ports', 'editPort');
258 if (! $port_info['linked'])
259 throw new RackTablesError ('Cant update cable ID: port is not linked');
260 if ($port_info['reservation_comment'] !== $sic['text'])
261 commitUpdatePortLink ($sic['id'], $sic['text']);
262 echo 'OK';
7bbe121d
AA
263}
264
42504426
AD
265function updateRackSortOrderAJAX()
266{
267 updateRackSortOrder ($_REQUEST['racks']);
268 echo 'OK';
269}
270
7bbe121d
AA
271function getNetUsageAJAX()
272{
273 assertStringArg ('net_id');
274 list ($ip, $mask) = explode ('/', $_REQUEST['net_id']);
275 $ip_bin = ip_parse ($ip);
276 $net = spotNetworkByIP ($ip_bin, $mask + 1);
277 if (! isset ($net) or $net['mask'] != $mask)
278 $net = constructIPRange ($ip_bin, $mask);
279 loadIPAddrList ($net);
280 echo getRenderedIPNetCapacity ($net);
9f4f431c
DO
281}
282
65e557dd 283?>