556709b8a2afab5f339fd97f4589b4315f9adc84
[racktables] / inc / interface.php
1 <?php
2 /*
3 *
4 * This file contains frontend functions for RackTables.
5 *
6 */
7
8 // Interface function's special.
9 $nextorder['odd'] = 'even';
10 $nextorder['even'] = 'odd';
11
12 // address allocation type
13 $aat = array
14 (
15 'regular' => 'Connected',
16 'virtual' => 'Loopback',
17 'shared' => 'Shared',
18 'router' => 'Router',
19 );
20 // address allocation code, IPv4 addresses and objects view
21 $aac = array
22 (
23 'regular' => '',
24 'virtual' => '<strong>L</strong>',
25 'shared' => '<strong>S</strong>',
26 'router' => '<strong>R</strong>',
27 );
28 // address allocation code, IPv4 networks view
29 $aac2 = array
30 (
31 'regular' => '',
32 'virtual' => '<strong>L:</strong>',
33 'router' => '<strong>R:</strong>',
34 );
35
36 // Let's have it here, so extensions can add their own images.
37 $image = array();
38 $image['error']['path'] = 'pix/error.png';
39 $image['error']['width'] = 76;
40 $image['error']['height'] = 17;
41 $image['logo']['path'] = 'pix/defaultlogo.png';
42 $image['logo']['width'] = 210;
43 $image['logo']['height'] = 40;
44 $image['rackspace']['path'] = 'pix/racks.png';
45 $image['rackspace']['width'] = 218;
46 $image['rackspace']['height'] = 200;
47 $image['objects']['path'] = 'pix/server.png';
48 $image['objects']['width'] = 218;
49 $image['objects']['height'] = 200;
50 $image['ipv4space']['path'] = 'pix/addressspace.png';
51 $image['ipv4space']['width'] = 218;
52 $image['ipv4space']['height'] = 200;
53 $image['ipv4slb']['path'] = 'pix/slb.png';
54 $image['ipv4slb']['width'] = 218;
55 $image['ipv4slb']['height'] = 200;
56 $image['config']['path'] = 'pix/configuration.png';
57 $image['config']['width'] = 218;
58 $image['config']['height'] = 200;
59 $image['reports']['path'] = 'pix/report.png';
60 $image['reports']['width'] = 218;
61 $image['reports']['height'] = 200;
62 $image['useup']['path'] = 'pix/tango-edit-clear.png';
63 $image['useup']['width'] = 16;
64 $image['useup']['height'] = 16;
65 $image['link']['path'] = 'pix/tango-network-wired.png';
66 $image['link']['width'] = 16;
67 $image['link']['height'] = 16;
68 $image['unlink']['path'] = 'pix/tango-edit-clear.png';
69 $image['unlink']['width'] = 16;
70 $image['unlink']['height'] = 16;
71 $image['add']['path'] = 'pix/tango-list-add.png';
72 $image['add']['width'] = 16;
73 $image['add']['height'] = 16;
74 $image['delete']['path'] = 'pix/tango-list-remove.png';
75 $image['delete']['width'] = 16;
76 $image['delete']['height'] = 16;
77 $image['nodelete']['path'] = 'pix/tango-list-remove-shadow.png';
78 $image['nodelete']['width'] = 16;
79 $image['nodelete']['height'] = 16;
80 $image['grant'] = $image['add'];
81 $image['revoke'] = $image['delete'];
82 $image['inservice']['path'] = 'pix/tango-emblem-system.png';
83 $image['inservice']['width'] = 16;
84 $image['inservice']['height'] = 16;
85 $image['notinservice']['path'] = 'pix/tango-dialog-error.png';
86 $image['notinservice']['width'] = 16;
87 $image['notinservice']['height'] = 16;
88 $image['blockuser'] = $image['inservice'];
89 $image['unblockuser'] = $image['notinservice'];
90 $image['find']['path'] = 'pix/tango-system-search.png';
91 $image['find']['width'] = 16;
92 $image['find']['height'] = 16;
93 $image['spacer']['path'] = 'pix/pixel.png';
94 $image['spacer']['width'] = 16;
95 $image['spacer']['height'] = 16;
96 $image['next']['path'] = 'pix/tango-go-next.png';
97 $image['next']['width'] = 32;
98 $image['next']['height'] = 32;
99 $image['prev']['path'] = 'pix/tango-go-previous.png';
100 $image['prev']['width'] = 32;
101 $image['prev']['height'] = 32;
102 $image['clear']['path'] = 'pix/tango-edit-clear.png';
103 $image['clear']['width'] = 16;
104 $image['clear']['height'] = 16;
105 $image['CLEAR']['path'] = 'pix/tango-edit-clear-big.png';
106 $image['CLEAR']['width'] = 32;
107 $image['CLEAR']['height'] = 32;
108 $image['save']['path'] = 'pix/tango-document-save.png';
109 $image['save']['width'] = 16;
110 $image['save']['height'] = 16;
111 $image['SAVE']['path'] = 'pix/tango-document-save-big.png';
112 $image['SAVE']['width'] = 32;
113 $image['SAVE']['height'] = 32;
114 $image['create']['path'] = 'pix/tango-document-new.png';
115 $image['create']['width'] = 16;
116 $image['create']['height'] = 16;
117 $image['CREATE']['path'] = 'pix/tango-document-new-big.png';
118 $image['CREATE']['width'] = 32;
119 $image['CREATE']['height'] = 32;
120 $image['DENIED']['path'] = 'pix/tango-dialog-error-big.png';
121 $image['DENIED']['width'] = 32;
122 $image['DENIED']['height'] = 32;
123 $image['apply']['path'] = 'pix/tango-emblem-system.png';
124 $image['apply']['width'] = 16;
125 $image['apply']['height'] = 16;
126 $image['node-collapsed']['path'] = 'pix/node-collapsed.png';
127 $image['node-collapsed']['width'] = 16;
128 $image['node-collapsed']['height'] = 16;
129 $image['node-expanded']['path'] = 'pix/node-expanded.png';
130 $image['node-expanded']['width'] = 16;
131 $image['node-expanded']['height'] = 16;
132 $image['node-expanded-static']['path'] = 'pix/node-expanded-static.png';
133 $image['node-expanded-static']['width'] = 16;
134 $image['node-expanded-static']['height'] = 16;
135
136 // This may be populated later onsite, report rendering function will use it.
137 // See the $systemreport for structure.
138 $localreports = array();
139
140 // Main menu.
141 function renderIndex ()
142 {
143 global $root;
144 ?>
145 <table border=0 cellpadding=0 cellspacing=0 width='100%'>
146 <tr>
147 <td>
148 <div style='text-align: center; margin: 10px; '>
149 <table width='100%' cellspacing=0 cellpadding=30 class=mainmenu border=0>
150 <tr>
151 <td>
152 <h1><a href='<?php echo $root; ?>?page=rackspace'>Rackspace<br>
153 <?php printImageHREF ('rackspace'); ?></a></h1>
154 </td>
155 <td>
156 <h1><a href='<?php echo $root; ?>?page=objects'>Objects<br>
157 <?php printImageHREF ('objects'); ?></a></h1>
158 </td>
159 <td>
160 <h1><a href='<?php echo $root; ?>?page=ipv4space'>IPv4 space<br>
161 <?php printImageHREF ('ipv4space'); ?></a></h1>
162 </td>
163 </tr>
164 </table>
165 <table width='100%' cellspacing=0 cellpadding=30 class=mainmenu border=0>
166 <tr>
167 <td>
168 <h1><a href='<?php echo $root; ?>?page=config'>Configuration<br>
169 <?php printImageHREF ('config'); ?></a></h1>
170 </td>
171 <td>
172 <h1><a href='<?php echo $root; ?>?page=reports'>Reports<br>
173 <?php printImageHREF ('reports'); ?></a></h1>
174 </td>
175 <td>
176 <h1><a href='<?php echo $root; ?>?page=ipv4slb'>IPv4 SLB<br>
177 <?php printImageHREF ('ipv4slb'); ?></a></h1>
178 </td>
179 </tr>
180 </table>
181 </div>
182 </td>
183 </tr>
184 </table>
185 <?php
186 }
187
188 function renderRackspace ()
189 {
190 $tagfilter = getTagFilter();
191 $tagfilter_str = getTagFilterStr ($tagfilter);
192 echo "<table class=objview border=0 width='100%'><tr><td class=pcleft>";
193 renderTagFilterPortlet ($tagfilter, 'rack');
194 echo '</td><td class=pcright>';
195 echo '<table border=0 cellpadding=10 cellpadding=1>';
196 // generate thumb gallery
197 $rackrowList = getRackspace ($tagfilter);
198 global $root, $nextorder;
199 $rackwidth = getRackImageWidth();
200 $order = 'odd';
201 foreach ($rackrowList as $rackrow)
202 {
203 echo "<tr class=row_${order}><th class=tdleft>";
204 echo "<a href='${root}?page=row&row_id=${rackrow['row_id']}${tagfilter_str}'>";
205 echo "${rackrow['row_name']}</a></th>";
206 $rackList = getRacksForRow ($rackrow['row_id'], $tagfilter);
207 echo "<td><table border=0 cellspacing=5><tr>";
208 foreach ($rackList as $rack)
209 {
210 echo "<td align=center><a href='${root}?page=rack&rack_id=${rack['id']}'>";
211 echo "<img border=0 width=${rackwidth} height=";
212 echo getRackImageHeight ($rack['height']);
213 echo " title='${rack['height']} units'";
214 echo "src='render_image.php?img=minirack&rack_id=${rack['id']}'>";
215 echo "<br>${rack['name']}</a></td>";
216 }
217 echo "</tr></table></tr>\n";
218 $order = $nextorder[$order];
219 }
220 echo "</table>\n";
221 echo "</td></tr></table>\n";
222 }
223
224 function renderRow ($row_id = 0)
225 {
226 if ($row_id == 0)
227 {
228 showError ('Invalid row_id', __FUNCTION__);
229 return;
230 }
231 if (($rowInfo = getRackRowInfo ($row_id)) == NULL)
232 {
233 showError ('getRackRowInfo() failed', __FUNCTION__);
234 return;
235 }
236 $tagfilter = getTagFilter();
237 $rackList = getRacksForRow ($row_id, $tagfilter);
238 // Main layout starts.
239 echo "<table border=0 class=objectview cellspacing=0 cellpadding=0>";
240
241 // Left portlet with row information.
242 echo "<tr><td class=pcleft>";
243 startPortlet ($rowInfo['name']);
244 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
245 echo "<tr><th width='50%' class=tdright>Racks:</th><td class=tdleft>${rowInfo['count']}</td></tr>\n";
246 echo "<tr><th width='50%' class=tdright>Units:</th><td class=tdleft>${rowInfo['sum']}</td></tr>\n";
247 echo "<tr><th width='50%' class=tdright>%% used:</th><td class=tdleft>";
248 renderProgressBar (getRSUforRackRow ($rackList));
249 echo "</td></tr>\n";
250 echo "</table><br>\n";
251 finishPortlet();
252
253 echo "</td><td class=pcright rowspan=2>";
254
255 global $root, $nextorder;
256 $rackwidth = getRackImageWidth() * getConfigVar ('ROW_SCALE');
257 $order = 'odd';
258 startPortlet ('Racks');
259 echo "<table border=0 cellspacing=5 align='center'><tr>";
260 foreach ($rackList as $rack)
261 {
262 echo "<td align=center class=row_${order}><a href='${root}?page=rack&rack_id=${rack['id']}'>";
263 echo "<img border=0 width=${rackwidth} height=" . (getRackImageHeight ($rack['height']) * getConfigVar ('ROW_SCALE'));
264 echo " title='${rack['height']} units'";
265 echo "src='render_image.php?img=minirack&rack_id=${rack['id']}'>";
266 echo "<br>${rack['name']}</a></td>";
267 $order = $nextorder[$order];
268 }
269 echo "</tr></table>\n";
270 finishPortlet();
271 echo "</td></tr>";
272
273 echo "<tr><td class=pcleft>";
274 renderTagFilterPortlet ($tagfilter, 'rack', 'row_id', $row_id);
275 echo "</td></tr></table>";
276 }
277
278 function showError ($info = '', $location = 'N/A')
279 {
280 global $root;
281 if (preg_match ('/\.php$/', $location))
282 $location = basename ($location);
283 elseif ($location != 'N/A')
284 $location = $location . '()';
285 echo "<div class=msg_error>An error has occured in [${location}]. ";
286 if (empty ($info))
287 echo 'No additional information is available.';
288 else
289 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
290 echo "Go back or try starting from <a href='${root}'>index page</a>.<br></div>\n";
291 }
292
293 // This function renders rack as HTML table.
294 function renderRack ($rack_id = 0, $hl_obj_id = 0)
295 {
296 if ($rack_id == 0)
297 {
298 showError ('Invalid rack_id', __FUNCTION__);
299 return;
300 }
301 if (($rackData = getRackData ($rack_id)) == NULL)
302 {
303 showError ('getRackData() failed', __FUNCTION__);
304 return;
305 }
306 global $root, $pageno, $tabno;
307 markAllSpans ($rackData);
308 if ($hl_obj_id > 0)
309 highlightObject ($rackData, $hl_obj_id);
310 markupObjectProblems ($rackData);
311 $prev_id = getPrevIDforRack ($rackData['row_id'], $rack_id);
312 $next_id = getNextIDforRack ($rackData['row_id'], $rack_id);
313 echo "<center><table border=0><tr valign=middle>";
314 echo "<td><h2><a href='${root}?page=row&row_id=${rackData['row_id']}'>${rackData['row_name']}</a> :</h2></td>";
315 // FIXME: use 'bypass'?
316 if ($prev_id != NULL)
317 {
318 echo "<td><a href='${root}?page=rack&rack_id=${prev_id}'>";
319 printImageHREF ('prev', 'previous rack');
320 echo "</a></td>";
321 }
322 echo "<td><h2><a href='${root}?page=rack&rack_id=${rackData['id']}'>${rackData['name']}</a></h2></td>";
323 if ($next_id != NULL)
324 {
325 echo "<td><a href='${root}?page=rack&rack_id=${next_id}'>";
326 printImageHREF ('next', 'next rack');
327 echo "</a></td>";
328 }
329 echo "</h2></td></tr></table>\n";
330 if ($rackData['left_is_front'] == 'yes')
331 $markup = array ('left' => 'Front', 'right' => 'Back');
332 else
333 $markup = array ('left' => 'Back', 'right' => 'Front');
334 echo "<table class=rack border=0 cellspacing=0 cellpadding=1>\n";
335 echo "<tr><th width='10%'>&nbsp;</th><th width='20%'>${markup['left']}</th>";
336 echo "<th width='50%'>Interior</th><th width='20%'>${markup['right']}</th></tr>\n";
337 for ($i = $rackData['height']; $i > 0; $i--)
338 {
339 echo '<tr><th>' . ($rackData['bottom_is_unit1'] == 'yes' ? $i : $rackData['height'] - $i + 1) . '</th>';
340 for ($locidx = 0; $locidx < 3; $locidx++)
341 {
342 if (isset ($rackData[$i][$locidx]['skipped']))
343 continue;
344 $state = $rackData[$i][$locidx]['state'];
345 echo "<td class=state_${state}";
346 if (isset ($rackData[$i][$locidx]['hl']))
347 echo $rackData[$i][$locidx]['hl'];
348 if (isset ($rackData[$i][$locidx]['colspan']))
349 echo ' colspan=' . $rackData[$i][$locidx]['colspan'];
350 if (isset ($rackData[$i][$locidx]['rowspan']))
351 echo ' rowspan=' . $rackData[$i][$locidx]['rowspan'];
352 echo ">";
353 switch ($state)
354 {
355 case 'T':
356 $objectData = getObjectInfo ($rackData[$i][$locidx]['object_id']);
357 if (!empty ($objectData['asset_no']))
358 $prefix = "<div title='${objectData['asset_no']}";
359 else
360 $prefix = "<div title='no asset tag";
361 // Don't tell about label, if it matches common name.
362 if ($objectData['name'] != $objectData['label'] and !empty ($objectData['label']))
363 $suffix = ", visible label is \"${objectData['label']}\"'>";
364 else
365 $suffix = "'>";
366 echo $prefix . $suffix;
367 echo "<a href='${root}?page=object&object_id=${objectData['id']}'>${objectData['dname']}</a></div>";
368 break;
369 case 'A':
370 echo '<div title="This rackspace does not exist">&nbsp;</div>';
371 break;
372 case 'F':
373 echo '<div title="Free rackspace">&nbsp;</div>';
374 break;
375 case 'U':
376 echo '<div title="Problematic rackspace, you CAN\'T mount here">&nbsp;</div>';
377 break;
378 default:
379 echo '<div title="No data">&nbsp;</div>';
380 break;
381 }
382 echo '</td>';
383 }
384 echo "</tr>\n";
385 }
386 echo "</table></center>\n";
387 }
388
389 function renderNewRackForm ($row_id)
390 {
391 showMessageOrError();
392
393 startPortlet ('Add one');
394 printOpFormIntro ('addRack', array ('got_data' => 'TRUE'));
395 echo '<table border=0 align=center>';
396 $defh = getConfigVar ('DEFAULT_RACK_HEIGHT');
397 if ($defh == 0)
398 $defh = '';
399 echo "<tr><th class=tdright>Rack name (*):</th><td class=tdleft><input type=text name=rack_name tabindex=1></td>";
400 echo "<td rowspan=4>Assign tags:<br>";
401 renderTagSelect();
402 echo "</td></tr>\n";
403 echo "<tr><th class=tdright>Height in units (*):</th><td class=tdleft><input type=text name=rack_height1 tabindex=2 value='${defh}'></td></tr>\n";
404 echo "<tr><th class=tdright>Comment:</th><td class=tdleft><input type=text name=rack_comment tabindex=3></td></tr>\n";
405 echo "<tr><td class=submit colspan=2>";
406 printImageHREF ('CREATE', 'Add', TRUE);
407 echo "</td></tr></table></form>";
408 finishPortlet();
409
410 startPortlet ('Add many');
411 printOpFormIntro ('addRack', array ('got_mdata' => 'TRUE'));
412 echo '<table border=0 align=center>';
413 $defh = getConfigVar ('DEFAULT_RACK_HEIGHT');
414 if ($defh == 0)
415 $defh = '';
416 echo "<tr><th class=tdright>Height in units (*):</th><td class=tdleft><input type=text name=rack_height2 value='${defh}'></td>";
417 echo "<td rowspan=3 valign=top>Assign tags:<br>";
418 renderTagSelect();
419 echo "</td></tr>\n";
420 echo "<tr><th class=tdright>Rack names (*):</th><td class=tdleft><textarea name=rack_names cols=40 rows=25></textarea></td></tr>\n";
421 echo "<tr><td class=submit colspan=2>";
422 printImageHREF ('CREATE', 'Add', TRUE);
423 echo '</form></table>';
424 finishPortlet();
425 }
426
427 function renderEditObjectForm ($object_id)
428 {
429 showMessageOrError();
430
431 global $pageno, $tabno, $root;
432 $object = getObjectInfo ($object_id);
433 if ($object == NULL)
434 {
435 showError ('getObjectInfo() failed', __FUNCTION__);
436 return;
437 }
438 echo '<table border=0 width=100%><tr>';
439
440 echo '<td class=pcleft>';
441 startPortlet ('Static attributes');
442 printOpFormIntro ('update');
443 echo '<table border=0 align=center>';
444 echo "<tr><th class=tdright>Type:</th><td class=tdleft>";
445 printSelect (getObjectTypeList(), 'object_type_id', $object['objtype_id']);
446 echo "</td></tr>\n";
447 // baseline info
448 echo "<tr><th class=tdright>Common name:</th><td class=tdleft><input type=text name=object_name value='${object['name']}'></td></tr>\n";
449 echo "<tr><th class=tdright>Visible label:</th><td class=tdleft><input type=text name=object_label value='${object['label']}'></td></tr>\n";
450 echo "<tr><th class=tdright>Asset tag:</th><td class=tdleft><input type=text name=object_asset_no value='${object['asset_no']}'></td></tr>\n";
451 echo "<tr><th class=tdright>Barcode:</th><td class=tdleft><input type=text name=object_barcode value='${object['barcode']}'></td></tr>\n";
452 echo "<tr><th class=tdright>Has problems:</th><td class=tdleft><input type=checkbox name=object_has_problems";
453 if ($object['has_problems'] == 'yes')
454 echo ' checked';
455 echo "></td></tr>\n";
456 echo "<tr><td colspan=2><b>Comment:</b><br><textarea name=object_comment rows=10 cols=80>${object['comment']}</textarea></td></tr>";
457 echo "<tr><th class=submit colspan=2>";
458 printImageHREF ('SAVE', 'Save changes', TRUE);
459 echo "</td></tr>\n";
460 echo '</form></table><br>';
461 finishPortlet();
462 echo '</td>';
463
464 // stickers
465 echo '<td class=pcright>';
466 startPortlet ('Optional attributes');
467 $values = getAttrValues ($object_id);
468 echo "<table cellspacing=0 cellpadding=5 align=center class=widetable>\n";
469 echo "<tr><th>&nbsp;</th><th>Attribute</th><th>Value</th><th>&nbsp;</th></tr>\n";
470 printOpFormIntro ('updateStickers');
471 echo '<input type=hidden name=num_attrs value=' . count($values) . ">\n";
472
473 $i = 0;
474 foreach ($values as $record)
475 {
476 echo "<input type=hidden name=${i}_attr_id value=${record['id']}>";
477 echo '<tr><td>';
478 if (!empty ($record['value']))
479 {
480 echo "<a href='${root}process.php?page=${pageno}&tab=${tabno}&op=clearSticker&object_id=${object_id}&attr_id=${record['id']}'>";
481 printImageHREF ('clear', 'Clear value');
482 echo '</a>';
483 }
484 else
485 echo '&nbsp;';
486 echo '</td>';
487 echo "<td class=tdright>${record['name']}:</td><td class=tdleft>";
488 switch ($record['type'])
489 {
490 case 'uint':
491 case 'float':
492 case 'string':
493 echo "<input type=text name=${i}_value value='${record['value']}'>";
494 break;
495 case 'dict':
496 $chapter = readChapter ($record['chapter_name']);
497 $chapter[0] = '-- NOT SET --';
498 printSelect ($chapter, "${i}_value", $record['key']);
499 break;
500 }
501 echo "</td></tr>\n";
502 $i++;
503 }
504 echo "<tr><td colspan=3>";
505 printImageHREF ('SAVE', 'Save changes', TRUE);
506 echo "</td></tr>\n";
507 echo "</form>";
508 echo "</table>\n";
509 finishPortlet();
510 echo '</td>';
511
512 echo '</tr><tr>';
513
514 echo '<td colspan=2>';
515 startPortlet ('history');
516 renderHistory ($pageno, $object_id);
517 finishPortlet();
518 echo '</td>';
519
520 echo '</tr></table>';
521 }
522
523 // This is a clone of renderEditObjectForm().
524 function renderEditRackForm ($rack_id)
525 {
526 showMessageOrError();
527 global $pageno;
528 $rack = getRackData ($rack_id);
529 if ($rack == NULL)
530 {
531 showError ('getRackData() failed', __FUNCTION__);
532 return;
533 }
534
535 startPortlet ('Rack attributes');
536 printOpFormIntro ('updateRack');
537 echo '<table border=0 align=center>';
538 echo "<tr><th class=tdright>Rack row:</th><td class=tdleft>";
539 printSelect (readChapter ('RackRow'), 'rack_row_id', $rack['row_id']);
540 echo "</td></tr>\n";
541 echo "<tr><th class=tdright>Name (required):</th><td class=tdleft><input type=text name=rack_name value='${rack['name']}'></td></tr>\n";
542 echo "<tr><th class=tdright>Height (required):</th><td class=tdleft><input type=text name=rack_height value='${rack['height']}'></td></tr>\n";
543 echo "<tr><th class=tdright>Comment:</th><td class=tdleft><input type=text name=rack_comment value='${rack['comment']}'></td></tr>\n";
544 echo "<tr><td class=submit colspan=2>";
545 printImageHREF ('SAVE', 'Save changes', TRUE);
546 echo "</td></tr>\n";
547 echo '</form></table><br>';
548 finishPortlet();
549
550 startPortlet ('History');
551 renderHistory ($pageno, $rack_id);
552 finishPortlet();
553 }
554
555 // This is a helper for creators and editors.
556 function printSelect ($rowList, $select_name, $selected_id = 1)
557 {
558 // First collect all data for OPTGROUPs, then ouput it and dump
559 // the rest of records as is.
560 $optgroup = array();
561 $other = array();
562 foreach ($rowList as $dict_key => $dict_value)
563 {
564 if (strpos ($dict_value, '%GSKIP%') !== FALSE)
565 {
566 $tmp = explode ('%GSKIP%', $dict_value, 2);
567 $optgroup[$tmp[0]][$dict_key] = $tmp[1];
568 }
569 elseif (strpos ($dict_value, '%GPASS%') !== FALSE)
570 {
571 $tmp = explode ('%GPASS%', $dict_value, 2);
572 $optgroup[$tmp[0]][$dict_key] = $tmp[1];
573 }
574 else
575 $other[$dict_key] = $dict_value;
576 }
577 echo "<select name=${select_name}>";
578 if (!count ($optgroup))
579 {
580 foreach ($other as $dict_key => $dict_value)
581 {
582 echo "<option value=${dict_key}";
583 if ($dict_key == $selected_id)
584 echo ' selected';
585 echo ">${dict_value}</option>";
586 }
587 }
588 else
589 {
590 foreach ($optgroup as $groupname => $groupdata)
591 {
592 echo "<optgroup label='${groupname}'>";
593 foreach ($groupdata as $dict_key => $dict_value)
594 {
595 echo "<option value=${dict_key}";
596 if ($dict_key == $selected_id)
597 echo ' selected';
598 echo ">${dict_value}</option>";
599 }
600 echo "</optgroup>\n";
601 }
602 if (count ($other))
603 {
604 echo "<optgroup label='other'>\n";
605 foreach ($other as $dict_key => $dict_value)
606 {
607 echo "<option value=${dict_key}";
608 if ($dict_key == $selected_id)
609 echo ' selected';
610 echo ">${dict_value}</option>";
611 }
612 echo "</optgroup>\n";
613 }
614 }
615 echo "</select>";
616 }
617
618 // used by renderGridForm() and renderRackPage()
619 function renderRackInfoPortlet ($rackData)
620 {
621 global $root;
622 startPortlet ('summary');
623 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
624 echo "<tr><th width='50%' class=tdright>Rack row:</th><td class=tdleft>${rackData['row_name']}</td></tr>\n";
625 echo "<tr><th width='50%' class=tdright>Name:</th><td class=tdleft>${rackData['name']}</td></tr>\n";
626 echo "<tr><th width='50%' class=tdright>Height:</th><td class=tdleft>${rackData['height']}</td></tr>\n";
627 echo "<tr><th width='50%' class=tdright>%% used:</th><td class=tdleft>";
628 renderProgressBar (getRSUforRack ($rackData));
629 echo "</td></tr>\n";
630 echo "<tr><th width='50%' class=tdright>Objects:</th><td class=tdleft>";
631 echo count (stuffInRackspace ($rackData));
632 echo "</td></tr>\n";
633 printTagTRs ("${root}?page=rackspace&");
634 if (!empty ($rackData['comment']))
635 echo "<tr><th width='50%' class=tdright>Comment:</th><td class=tdleft>${rackData['comment']}</td></tr>\n";
636 echo '</table>';
637 finishPortlet();
638 }
639
640 // This is a universal editor of rack design/waste.
641 // FIXME: switch to using printOpFormIntro()
642 function renderGridForm ($rack_id = 0, $filter, $header, $submit, $state1, $state2)
643 {
644 if ($rack_id == 0)
645 {
646 showError ('Invalid rack_id', __FUNCTION__);
647 return;
648 }
649 if (($rackData = getRackData ($rack_id)) == NULL)
650 {
651 showError ('getRackData() failed', __FUNCTION__);
652 return;
653 }
654
655 global $root, $pageno, $tabno;
656 $filter ($rackData);
657 markupObjectProblems ($rackData);
658
659 // Process form submit.
660 if (isset ($_REQUEST['do_update']))
661 {
662 $log[] = processGridForm ($rackData, $state1, $state2);
663 printLog ($log);
664 $rackData = getRackData ($rack_id);
665 $filter ($rackData);
666 markupObjectProblems ($rackData);
667 }
668
669 // Render the result whatever it is.
670 // Main layout.
671 echo "<table border=0 class=objectview cellspacing=0 cellpadding=0>";
672 echo "<tr><td colspan=2 align=center><h1>${rackData['name']}</h1></td></tr>\n";
673
674 // Left column with information portlet.
675 echo "<tr><td class=pcleft height='1%' width='50%'>";
676 renderRackInfoPortlet ($rackData);
677 echo "</td>\n";
678 echo "<td class=pcright>";
679
680 // Grid form.
681 startPortlet ($header);
682 echo "<center>\n";
683 echo "<table class=rack border=0 cellspacing=0 cellpadding=1>\n";
684 echo "<tr><th width='10%'>&nbsp;</th><th width='20%'>Front</th>";
685 echo "<th width='50%'>Interior</th><th width='20%'>Back</th></tr>\n";
686 echo "<form method=post action='${root}?'>\n";
687 echo "<input type=hidden name=page value=${pageno}>\n";
688 echo "<input type=hidden name=tab value=${tabno}>\n";
689 echo "<input type=hidden name=rack_id value=${rack_id}>\n";
690 markupAtomGrid ($rackData, $state2);
691 renderAtomGrid ($rackData);
692 echo "</table></center>\n";
693 echo "<br><input type=submit name=do_update value='${submit}'></form><br><br>\n";
694 finishPortlet();
695 echo "</td></tr></table>\n";
696 }
697
698 function renderRackDesign ($rack_id)
699 {
700 renderGridForm ($rack_id, 'applyRackDesignMask', 'Rack design', 'Set rack design', 'A', 'F');
701 }
702
703 function renderRackProblems ($rack_id = 0)
704 {
705 renderGridForm ($rack_id, 'applyRackProblemMask', 'Rack problems', 'Mark unusable atoms', 'F', 'U');
706 }
707
708 function startPortlet ($title = '')
709 {
710 echo "<div class=portlet><h2>${title}</h2>";
711 }
712
713 function finishPortlet ()
714 {
715 echo "</div>\n";
716 }
717
718 function printRefsOfType ($refs, $type, $eq)
719 {
720 global $root;
721 $gotone=0;
722 foreach ($refs as $ref)
723 {
724 if ($eq($ref['type'], $type))
725 {
726 if ($gotone) echo ', ';
727 echo "<a href='${root}?page=object&object_id=${ref['object_id']}'>";
728 if (!empty ($ref['name']))
729 echo $ref['name'] . '@';
730 echo "${ref['object_name']}</a>";
731 $gotone=1;
732 }
733 }
734 }
735
736 function renderRackObject ($object_id = 0)
737 {
738 global $root, $nextorder, $aac;
739 if ($object_id <= 0)
740 {
741 showError ('Invalid object_id', __FUNCTION__);
742 return;
743 }
744 $info = getObjectInfo ($object_id);
745 if ($info == NULL)
746 {
747 showError ('getObjectInfo() failed', __FUNCTION__);
748 return;
749 }
750 // Main layout starts.
751 echo "<table border=0 class=objectview cellspacing=0 cellpadding=0>";
752 echo "<tr><td colspan=2 align=center><h1>${info['dname']}</h1></td></tr>\n";
753 // left column with uknown number of portlets
754 echo "<tr><td class=pcleft>";
755 startPortlet ('summary');
756 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
757 if (!empty ($info['name']))
758 echo "<tr><th width='50%' class=tdright>Common name:</th><td class=tdleft>${info['name']}</td></tr>\n";
759 elseif (in_array ($info['objtype_id'], explode (',', getConfigVar ('NAMEFUL_OBJTYPES'))))
760 echo "<tr><td colspan=2 class=msg_error>Common name is missing.</td></tr>\n";
761 echo "<tr><th width='50%' class=tdright>Object type:</th>";
762 echo "<td class=tdleft><a href='${root}?page=objgroup&group_id=${info['objtype_id']}&hl_object_id=${object_id}'>${info['objtype_name']}</a></td></tr>\n";
763 if (!empty ($info['asset_no']))
764 echo "<tr><th width='50%' class=tdright>Asset tag:</th><td class=tdleft>${info['asset_no']}</td></tr>\n";
765 elseif (in_array ($info['objtype_id'], explode (',', getConfigVar ('REQUIRE_ASSET_TAG_FOR'))))
766 echo "<tr><td colspan=2 class=msg_error>Asset tag is missing.</td></tr>\n";
767 if (!empty ($info['label']))
768 echo "<tr><th width='50%' class=tdright>Visible label:</th><td class=tdleft>${info['label']}</td></tr>\n";
769 if (!empty ($info['barcode']))
770 echo "<tr><th width='50%' class=tdright>Barcode:</th><td class=tdleft>${info['barcode']}</td></tr>\n";
771 if ($info['has_problems'] == 'yes')
772 echo "<tr><td colspan=2 class=msg_error>Has problems</td></tr>\n";
773 foreach (getAttrValues ($object_id, TRUE) as $record)
774 if (!empty ($record['value']))
775 echo "<tr><th width='50%' class=opt_attr_th>${record['name']}:</th><td class=tdleft>${record['a_value']}</td></tr>\n";
776 printTagTRs ("${root}?page=objgroup&group_id=${info['objtype_id']}&");
777 echo "</table><br>\n";
778 finishPortlet();
779
780 if (!empty ($info['comment']))
781 {
782 startPortlet ('Comment');
783 echo '<div class=commentblock>' . string_insert_hrefs ($info['comment']) . '</div>';
784 finishPortlet ();
785 }
786
787 $ports = getObjectPortsAndLinks ($object_id);
788 if (count ($ports))
789 {
790 startPortlet ('ports and links');
791 usort($ports, 'sortByName');
792 if ($ports)
793 {
794 $hl_port_id = 0;
795 if (isset ($_REQUEST['hl_port_id']))
796 {
797 assertUIntArg ('hl_port_id', __FUNCTION__);
798 $hl_port_id = $_REQUEST['hl_port_id'];
799 }
800 echo "<table cellspacing=0 cellpadding='5' align='center' class='widetable'>\n";
801 echo "<tr><th>Local name</th><th>Visible label</th><th>Port type</th><th>L2 address</th>";
802 echo "<th>Rem. Object</th><th>Rem. port</th></tr>\n";
803 foreach ($ports as $port)
804 {
805 echo '<tr';
806 if ($hl_port_id == $port['id'])
807 echo ' class=port_highlight';
808 echo "><td>${port['name']}</td><td>${port['label']}</td><td>${port['type']}</td>";
809 echo "<td>${port['l2address']}</td>";
810 if ($port['remote_object_id'])
811 {
812 echo "<td><a href='${root}?page=object&object_id=${port['remote_object_id']}&hl_port_id=${port['remote_id']}'>${port['remote_object_name']}</a></td>";
813 echo "<td>${port['remote_name']}</td>";
814 }
815 elseif (!empty ($port['reservation_comment']))
816 {
817 echo "<td><b>Reserved;</b></td>";
818 echo "<td>${port['reservation_comment']}</td>";
819 }
820 else
821 echo '<td>&nbsp;</td><td>&nbsp;</td>';
822 echo "</tr>\n";
823 }
824 echo "</table><br>\n";
825 }
826 finishPortlet();
827 }
828
829 $alloclist = getObjectIPv4Allocations ($object_id);
830 if (count ($alloclist))
831 {
832 startPortlet ('IPv4 addresses');
833 echo "<table cellspacing=0 cellpadding='5' align='center' class='widetable'>\n";
834 if (getConfigVar ('EXT_IPV4_VIEW') == 'yes')
835 echo "<tr><th>OS interface</th><th>IP address</th><th colspan=2>network</th><th>routed by</th><th>peers</th></tr>\n";
836 else
837 echo "<tr><th>OS interface</th><th>IP address</th><th>peers</th></tr>\n";
838 $hl_ipv4_addr = '';
839 if (isset ($_REQUEST['hl_ipv4_addr']))
840 {
841 assertIPv4Arg ('hl_ipv4_addr', __FUNCTION__);
842 $hl_ipv4_addr = $_REQUEST['hl_ipv4_addr'];
843 }
844 foreach ($alloclist as $dottedquad => $alloc)
845 {
846 $address_name = niftyString ($alloc['addrinfo']['name']);
847 $class = $alloc['addrinfo']['class'];
848 $secondclass = ($hl_ipv4_addr == $dottedquad) ? 'tdleft port_highlight' : 'tdleft';
849 $netid = getIPv4AddressNetworkId ($dottedquad);
850 $netinfo = getIPv4NetworkInfo ($netid);
851 loadIPv4AddrList ($netinfo);
852 echo "<tr class='${class}' valign=top><td class=tdleft>${alloc['osif']}</td><td class='${secondclass}'>";
853 echo "<a href='${root}?page=ipaddress&ip=" . $dottedquad . "&hl_object_id=${object_id}'>${dottedquad}</a>";
854 if (getConfigVar ('EXT_IPV4_VIEW') != 'yes')
855 {
856 if (NULL === $netid)
857 $suffix = '/??';
858 else
859 echo '<small>/' . $netinfo['mask'] . '</small>';
860 }
861 echo '&nbsp;' . $aac[$alloc['type']];
862 if (!empty ($alloc['addrinfo']['name']))
863 echo '(' . niftyString ($alloc['addrinfo']['name']) . ')';
864 echo '</td>';
865 if (getConfigVar ('EXT_IPV4_VIEW') == 'yes')
866 {
867 if (NULL === $netid)
868 echo '<td colspan=2>?</td>';
869 else
870 printIPv4NetInfoTDs ($netinfo, $secondclass);
871 echo "<td class='${secondclass}'>";
872 // FIXME: These cals are really heavy, replace them with a more appropriate dedicated function.
873 $newline = '';
874 foreach (findRouters ($netinfo['addrlist']) as $router)
875 {
876 if ($router['id'] == $object_id)
877 continue;
878 echo $newline . $router['addr'] . ", <a href='${root}?page=object&object_id=${router['id']}&hl_ipv4_addr=${router['addr']}'>";
879 echo (empty ($router['iface']) ? '' : $router['iface'] . '@') . $router['dname'] . '</a>';
880 $routertags = loadRackObjectTags ($router['id']);
881 if (count ($routertags))
882 echo '<br><small>' . serializeTags ($routertags, "${root}?page=objects&tab=default&") . '</small>';
883 $newline = '<br>';
884 }
885 echo '</td>';
886 }
887 // peers
888 echo "<td class='${secondclass}'>\n";
889 $prefix = '';
890 if ($alloc['addrinfo']['reserved'] == 'yes')
891 {
892 echo $prefix . '<strong>RESERVED</strong>';
893 $prefix = '; ';
894 }
895 foreach ($alloc['addrinfo']['allocs'] as $allocpeer)
896 {
897 if ($allocpeer['object_id'] == $object_id)
898 continue;
899 echo $prefix . "<a href='${root}?page=object&object_id=${allocpeer['object_id']}'>";
900 if (!empty ($allocpeer['osif']))
901 echo $allocpeer['osif'] . '@';
902 echo $allocpeer['object_name'] . '</a>';
903 $prefix = '; ';
904 }
905 echo "</td></tr>\n";
906 }
907 echo "</table><br>\n";
908 finishPortlet();
909 }
910
911 $forwards = getNATv4ForObject ($object_id);
912 if (count($forwards['in']) or count($forwards['out']))
913 {
914 startPortlet('NATv4');
915
916 if (count($forwards['out']))
917 {
918
919 echo "<h3>locally performed NAT</h3>";
920
921 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center'>\n";
922 echo "<tr><th>Proto</th><th>Match endpoint</th><th>Translate to</th><th>Target object</th><th>Rule comment</th></tr>\n";
923
924 foreach ($forwards['out'] as $pf)
925 {
926 $class = 'trerror';
927 $osif = '';
928 if (isset ($alloclist [$pf['localip']]))
929 {
930 $class = $alloclist [$pf['localip']]['addrinfo']['class'];
931 $osif = $alloclist [$pf['localip']]['osif'] . ': ';
932 }
933 echo "<tr class='$class'>";
934 echo "<td>${pf['proto']}</td><td class=tdleft>${osif}<a href='${root}?page=ipaddress&tab=default&ip=${pf['localip']}'>${pf['localip']}</a>:${pf['localport']}</td>";
935 echo "<td class=tdleft><a href='${root}?page=ipaddress&tab=default&ip=${pf['remoteip']}'>${pf['remoteip']}</a>:${pf['remoteport']}</td>";
936 $address = getIPv4Address ($pf['remoteip']);
937 echo "<td class='description'>";
938 if (count ($address['allocs']))
939 foreach($address['allocs'] as $bond)
940 echo "<a href='${root}?page=object&tab=default&object_id=${bond['object_id']}'>${bond['object_name']}(${bond['name']})</a> ";
941 elseif (!empty ($pf['remote_addr_name']))
942 echo '(' . $pf['remote_addr_name'] . ')';
943 echo "</td><td class='description'>${pf['description']}</td></tr>";
944 }
945 echo "</table><br><br>";
946 }
947 if (count($forwards['in']))
948 {
949 echo "<h3>arriving NAT connections</h3>";
950 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center'>\n";
951 echo "<tr><th>Matched endpoint</th><th>Source object</th><th>Translated to</th><th>Rule comment</th></tr>\n";
952 foreach ($forwards['in'] as $pf)
953 {
954 echo "<tr>";
955 echo "<td>${pf['proto']}/<a href='${root}?page=ipaddress&tab=default&ip=${pf['localip']}'>${pf['localip']}</a>:${pf['localport']}</td>";
956 echo "<td class='description'><a href='${root}?page=object&tab=default&object_id=${pf['object_id']}'>${pf['object_name']}</a>";
957 echo "</td><td><a href='${root}?page=ipaddress&tab=default&ip=${pf['remoteip']}'>${pf['remoteip']}</a>:${pf['remoteport']}</td>";
958 echo "<td class='description'>${pf['description']}</td></tr>";
959 }
960 echo "</table><br><br>";
961 }
962 finishPortlet();
963 }
964
965 $pools = getRSPoolsForObject ($object_id);
966 if (count ($pools))
967 {
968 $order = 'odd';
969 startPortlet ('Real server pools');
970 echo "<table cellspacing=0 cellpadding=5 align=center class=widetable>\n";
971 echo "<tr><th>VS</th><th>RS pool</th><th>RS</th><th>VS config</th><th>RS config</th></tr>\n";
972 foreach ($pools as $vs_id => $info)
973 {
974 echo "<tr valign=top class=row_${order}><td class=tdleft><a href='${root}?page=ipv4vs&vs_id=${vs_id}'>";
975 echo buildVServiceName ($info);
976 echo '</a>';
977 if (!empty ($info['name']))
978 echo "<br>${info['name']}";
979 echo "</td><td class=tdleft><a href='${root}?page=ipv4rsp&pool_id=${info['pool_id']}'>";
980 echo (empty ($info['pool_name']) ? 'ANONYMOUS' : $info['pool_name']);
981 echo '</a></td><td class=tdleft>' . $info['rscount'] . '</td>';
982 echo "<td class=tdleft><pre>${info['vsconfig']}</pre></td>";
983 echo "<td class=tdleft><pre>${info['rsconfig']}</pre></td>";
984 echo "</tr>\n";
985 $order = $nextorder[$order];
986 }
987 echo "</table>\n";
988 finishPortlet();
989 }
990 echo "</td>\n";
991
992 // After left column we have (surprise!) right column with rackspace portlet only.
993 echo "<td class=pcright>";
994 // rackspace portlet
995 startPortlet ('rackspace allocation');
996 // FIXME: now we call getRackData() twice
997 $racks = getResidentRacksData ($object_id);
998 foreach ($racks as $rackData)
999 renderRack ($rackData['id'], $object_id);
1000 echo '<br>';
1001 finishPortlet();
1002 echo "</td></tr>";
1003 echo "</table>\n";
1004 }
1005
1006 function renderRackMultiSelect ($sname, $racks, $selected)
1007 {
1008 // Transform the given flat list into a list of groups, each representing a rack row.
1009 $rdata = array();
1010 foreach ($racks as $rack)
1011 if (!isset ($rdata[$rack['row_name']]))
1012 $rdata[$rack['row_name']] = array ($rack['id'] => $rack['name']);
1013 else
1014 $rdata[$rack['row_name']][$rack['id']] = $rack['name'];
1015 echo "<select name=${sname} multiple size=" . getConfigVar ('MAXSELSIZE') . " onchange='getElementById(\"racks\").submit()'>\n";
1016 foreach ($rdata as $optgroup => $racklist)
1017 {
1018 echo "<optgroup label='${optgroup}'>";
1019 foreach ($racklist as $rack_id => $rack_name)
1020 {
1021 echo "<option value=${rack_id}";
1022 if (!(array_search ($rack_id, $selected) === FALSE))
1023 echo ' selected';
1024 echo">${rack_name}</option>\n";
1025 }
1026 }
1027 echo "</select>\n";
1028 }
1029
1030 function showMessageOrError ()
1031 {
1032 if (isset ($_REQUEST['message']))
1033 echo "<div class=msg_success>${_REQUEST['message']}</div>";
1034 elseif (isset ($_REQUEST['error']))
1035 echo "<div class=msg_error>${_REQUEST['error']}</div>";
1036 elseif (isset ($_REQUEST['log']))
1037 printLog (unserialize (base64_decode ($_REQUEST['log'])));
1038 }
1039
1040 // This function renders a form for port edition.
1041 function renderPortsForObject ($object_id = 0)
1042 {
1043 global $root, $pageno, $tabno;
1044 if ($object_id <= 0)
1045 {
1046 showError ('Invalid object_id', __FUNCTION__);
1047 return;
1048 }
1049 showMessageOrError();
1050 startPortlet ('Ports and interfaces');
1051 $ports = getObjectPortsAndLinks ($object_id);
1052 usort($ports, 'sortByName');
1053 echo "<table cellspacing=0 cellpadding='5' align='center' class='widetable'>\n";
1054 echo "<tr><th>&nbsp;</th><th>Local name</th><th>Visible label</th><th>Port type</th><th>L2 address</th>";
1055 echo "<th>Rem. object</th><th>Rem. port</th><th>(Un)link or (un)reserve</th><th>&nbsp;</th></tr>\n";
1056 foreach ($ports as $port)
1057 {
1058 printOpFormIntro ('editPort', array ('port_id' => $port['id']));
1059 echo "<tr><td><a href='${root}process.php?op=delPort&page=${pageno}&tab=${tabno}&port_id=${port['id']}&object_id=$object_id&port_name=${port['name']}'>";
1060 printImageHREF ('delete', 'Unlink and Delete this port');
1061 echo "</a></td>\n";
1062 echo "<td><input type=text name=name value='${port['name']}' size=8></td>";
1063 echo "<td><input type=text name=label value='${port['label']}' size=24></td>";
1064 echo "<td>${port['type']}</td>\n";
1065 echo "<td><input type=text name=l2address value='${port['l2address']}'></td>\n";
1066 if ($port['remote_object_id'])
1067 {
1068 echo "<td><a href='${root}?page=object&object_id=${port['remote_object_id']}'>${port['remote_object_name']}</a></td>";
1069 echo "<td>${port['remote_name']}</td>";
1070 echo "<td><a href='${root}process.php?op=unlinkPort&page=${pageno}&tab=${tabno}&port_id=${port['id']}&object_id=$object_id&port_name=";
1071 echo urlencode ($port['name']);
1072 echo "&remote_port_name=${port['remote_name']}&remote_object_name=${port['remote_object_name']}'>";
1073 printImageHREF ('unlink', 'Unlink this port');
1074 echo "</a></td>";
1075 }
1076 elseif (!empty ($port['reservation_comment']))
1077 {
1078 echo "<td><b>Reserved;</b></td>";
1079 echo "<td><input type=text name=reservation_comment value='${port['reservation_comment']}'></td>";
1080 echo "<td><a href='${root}process.php?op=useup&page=${pageno}&tab=${tabno}&port_id=${port['id']}&object_id=${object_id}'>";
1081 printImageHREF ('useup', 'Use up this port');
1082 echo "</a></td>";
1083 }
1084 else
1085 {
1086 echo "<td>&nbsp;</td><td>&nbsp;</td>";
1087 echo "<td>";
1088 echo "<a href='javascript:;' onclick='window.open(\"${root}link_helper.php?port=${port['id']}&type=${port['type_id']}&object_id=$object_id&port_name=";
1089 echo urlencode ($port['name']);
1090 echo "\",\"findlink\",\"height=700, width=400, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, titlebar=no, toolbar=no\");'>";
1091 printImageHREF ('link', 'Link this port');
1092 echo "</a> <input type=text name=reservation_comment>";
1093 echo "</td>\n";
1094 }
1095 echo "<td>";
1096 printImageHREF ('save', 'Save changes', TRUE);
1097 echo "</td></form></tr>\n";
1098 }
1099 printOpFormIntro ('addPort');
1100 echo "<tr><td>";
1101 printImageHREF ('add', '', TRUE, 104);
1102 echo "</td><td><input type=text size=8 name=port_name tabindex=100></td>\n";
1103 echo "<td><input type=text size=24 name=port_label tabindex=101></td>";
1104 echo "<td><select name='port_type_id' tabindex=102>\n";
1105 $types = getPortTypes();
1106 $default_port_type = getConfigVar ('default_port_type');
1107 foreach ($types as $typeid => $typename)
1108 {
1109 echo "<option value='${typeid}'";
1110 if ($typeid == $default_port_type)
1111 echo " selected";
1112 echo ">${typename}</option>\n";
1113 }
1114 echo "</select></td>";
1115 echo "<td><input type=text name=port_l2address tabindex=103></td>\n";
1116 echo "<td colspan=4>&nbsp;</td></tr></form>";
1117 echo "</table><br>\n";
1118 finishPortlet();
1119
1120 startPortlet ('Add/update multiple ports');
1121 printOpFormIntro ('addMultiPorts');
1122 echo 'Format: <select name=format>';
1123 echo '<option value=c3600asy>Cisco 3600 async: sh line | inc TTY</option>';
1124 echo '<option value=fiwg selected>Foundry ServerIron/FastIron WorkGroup/Edge: sh int br</option>';
1125 echo '<option value=fisxii>Foundry FastIron SuperX/II4000: sh int br</option>';
1126 echo '<option value=ssv1>SSV:&lt;interface name&gt; &lt;MAC address&gt;</option>';
1127 echo "</select>";
1128 echo 'Default port type: ';
1129 echo "<select name=port_type>\n";
1130 foreach ($types as $typeid => $typename)
1131 {
1132 echo "<option value='${typeid}'";
1133 if ($typeid == $default_port_type)
1134 echo " selected";
1135 echo ">${typename}</option>\n";
1136 }
1137 echo "</select>";
1138 echo "<input type=submit value='Parse output'><br>\n";
1139 echo "<textarea name=input cols=100 rows=50></textarea><br>\n";
1140 echo '</form>';
1141 finishPortlet();
1142 }
1143
1144 function renderIPv4ForObject ($object_id = 0)
1145 {
1146 global $root, $pageno, $tabno, $aat;
1147 if ($object_id <= 0)
1148 {
1149 showError ('Invalid object_id', __FUNCTION__);
1150 return;
1151 }
1152 showMessageOrError();
1153 startPortlet ('Allocations');
1154 $alloclist = getObjectIPv4Allocations ($object_id);
1155 echo "<table cellspacing=0 cellpadding='5' align='center' class='widetable'>\n";
1156 echo '<tr><th>&nbsp;</th><th>OS interface</th><th>IP address</th>';
1157 if (getConfigVar ('EXT_IPV4_VIEW') == 'yes')
1158 echo '<th colspan=2>network</th><th>routed by</th>';
1159 echo '<th>type</th><th>misc</th><th>&nbsp</th></tr>';
1160 foreach ($alloclist as $dottedquad => $alloc)
1161 {
1162 $class = $alloc['addrinfo']['class'];
1163 $netid = getIPv4AddressNetworkId ($dottedquad);
1164 $netinfo = getIPv4NetworkInfo ($netid);
1165 loadIPv4AddrList ($netinfo);
1166 printOpFormIntro ('updIPv4Allocation', array ('ip' => $dottedquad));
1167 echo "<tr class='$class' valign=top><td><a href='${root}process.php?op=delIPv4Allocation&page=${pageno}&tab=${tabno}&ip=${dottedquad}&object_id=$object_id'>";
1168 printImageHREF ('delete', 'Delete this IPv4 address');
1169 echo "</a></td>";
1170 echo "<td class=tdleft><input type='text' name='bond_name' value='${alloc['osif']}' size=10></td>";
1171 echo "<td class=tdleft><a href='${root}?page=ipaddress&ip=${dottedquad}'>${dottedquad}</a>";
1172 if (getConfigVar ('EXT_IPV4_VIEW') != 'yes')
1173 {
1174 if (NULL === $netid)
1175 $suffix = '/??';
1176 else
1177 echo '<small>/' . $netinfo['mask'] . '</small>';
1178 }
1179 echo '&nbsp;' . $aac[$alloc['type']];
1180 if (!empty ($alloc['addrinfo']['name']))
1181 echo '(' . niftyString ($alloc['addrinfo']['name']) . ')';
1182 echo '</td>';
1183 // FIXME: this a copy-and-paste from renderRackObject()
1184 if (getConfigVar ('EXT_IPV4_VIEW') == 'yes')
1185 {
1186 if (NULL === $netid)
1187 echo '<td colspan=2>?</td>';
1188 else
1189 printIPv4NetInfoTDs ($netinfo);
1190 echo "<td class='${secondclass}'>";
1191 // FIXME: These cals are really heavy, replace them with a more appropriate dedicated function.
1192 $newline = '';
1193 foreach (findRouters ($netinfo['addrlist']) as $router)
1194 {
1195 if ($router['id'] == $object_id)
1196 continue;
1197 echo $newline . $router['addr'] . ", <a href='${root}?page=object&object_id=${router['id']}&hl_ipv4_addr=${router['addr']}'>";
1198 echo (empty ($router['iface']) ? '' : $router['iface'] . '@') . $router['dname'] . '</a>';
1199 $routertags = loadRackObjectTags ($router['id']);
1200 if (count ($routertags))
1201 echo '<br><small>' . serializeTags ($routertags, "${root}?page=objects&tab=default&") . '</small>';
1202 $newline = '<br>';
1203 }
1204 echo '</td>';
1205 }
1206 echo '<td>';
1207 printSelect ($aat, 'bond_type', $alloc['type']);
1208 echo "</td><td>";
1209 $prefix = '';
1210 if ($alloc['addrinfo']['reserved'] == 'yes')
1211 {
1212 echo $prefix . '<strong>RESERVED</strong>';
1213 $prefix = '; ';
1214 }
1215 foreach ($alloc['addrinfo']['allocs'] as $allocpeer)
1216 {
1217 if ($allocpeer['object_id'] == $object_id)
1218 continue;
1219 echo $prefix . "<a href='${root}?page=object&object_id=${allocpeer['object_id']}'>";
1220 if (!empty ($allocpeer['osif']))
1221 echo $allocpeer['osif'] . '@';
1222 echo $allocpeer['object_name'] . '</a>';
1223 $prefix = '; ';
1224 }
1225 echo "</td><td>";
1226 printImageHREF ('save', 'Save changes', TRUE);
1227 echo "</td></form></tr>\n";
1228 }
1229
1230 printOpFormIntro ('addIPv4Allocation');
1231 echo "<tr><td>";
1232 printImageHREF ('add', 'Allocate new address', TRUE, 99);
1233 echo "</td>";
1234 echo "<td class=tdleft><input type='text' size='10' name='bond_name' tabindex=100></td>\n";
1235 echo "<td class=tdleft><input type=text name='ip' tabindex=101></td>\n";
1236 echo "<td colspan=3>&nbsp;</td><td>";
1237 printSelect ($aat, 'bond_type');
1238 echo "</td><td colspan=2>&nbsp;</td></tr></form></table><br>\n";
1239 finishPortlet();
1240
1241 }
1242
1243 // Log structure versions:
1244 // 1: the whole structure is a list of code-message pairs
1245 // 2 and later: there's a "v" field set, which indicates the version
1246 // 2: there's a "m" list set to hold message code and optional arguments
1247 function printLog ($log)
1248 {
1249 switch (TRUE)
1250 {
1251 case !isset ($log['v']):
1252 case $log['v'] == 1:
1253 foreach ($log as $key => $record)
1254 if ($key !== 'v')
1255 echo "<div class=msg_${record['code']}>${record['message']}</div>";
1256 break;
1257 case $log['v'] == 2:
1258 $msginfo = array
1259 (
1260 0 => array ('code' => 'success', 'format' => 'Success: %s'),
1261 1 => array ('code' => 'success', 'format' => '%u new records done, %u already existed'),
1262 2 => array ('code' => 'success', 'format' => 'NATv4 rule was successfully added.'),
1263 3 => array ('code' => 'success', 'format' => 'NATv4 rule was successfully deleted.'),
1264 4 => array ('code' => 'success', 'format' => 'NATv4 rule was successfully updated'),
1265 5 => array ('code' => 'success', 'format' => 'Port %s was added successfully'),
1266 6 => array ('code' => 'success', 'format' => 'Port %s was updated successfully'),
1267 7 => array ('code' => 'success', 'format' => 'Port %s was deleted successfully'),
1268 8 => array ('code' => 'success', 'format' => 'Port %s successfully linked with port %s at object %s'),
1269 9 => array ('code' => 'success', 'format' => 'Port %s was successfully unlinked from %s@%s'),
1270 10 => array ('code' => 'success', 'format' => 'Added %u ports, updated %u ports, encountered %u errors.'),
1271 11 => array ('code' => 'success', 'format' => 'Reservation removed.'),
1272 12 => array ('code' => 'success', 'format' => 'allocation updated'),
1273 13 => array ('code' => 'success', 'format' => 'allocated'),
1274 14 => array ('code' => 'success', 'format' => 'deallocated'),
1275 15 => array ('code' => 'success', 'format' => 'Reset succeeded.'),
1276 16 => array ('code' => 'success', 'format' => 'Update done'),
1277 17 => array ('code' => 'success', 'format' => 'Update(s) succeeded.'),
1278 18 => array ('code' => 'success', 'format' => 'Load balancer was successfully added'),
1279 19 => array ('code' => 'success', 'format' => 'Load balancer was successfully deleted'),
1280 20 => array ('code' => 'success', 'format' => 'Load balancer info was successfully updated'),
1281 21 => array ('code' => 'success', 'format' => 'Generation complete'),
1282 22 => array ('code' => 'success', 'format' => 'Chained %u tags'),
1283 23 => array ('code' => 'success', 'format' => 'IPv4 prefix successfully added'),
1284 24 => array ('code' => 'success', 'format' => 'IPv4 prefix deleted'),
1285 25 => array ('code' => 'success', 'format' => 'IPv4 prefix updated'),
1286 26 => array ('code' => 'success', 'format' => '%u IP address(es) were successfully updated'),
1287 27 => array ('code' => 'success', 'format' => 'IPv4 address updated'),
1288 28 => array ('code' => 'success', 'format' => 'Virtual service was successfully created'),
1289 29 => array ('code' => 'success', 'format' => 'Virtual service was successfully deleted'),
1290 30 => array ('code' => 'success', 'format' => 'Virtual service was successfully updated'),
1291 31 => array ('code' => 'success', 'format' => 'RS pool was successfully created'),
1292 32 => array ('code' => 'success', 'format' => 'RS pool was successfully deleted'),
1293 33 => array ('code' => 'success', 'format' => 'RS pool was successfully updated'),
1294 34 => array ('code' => 'success', 'format' => 'Real server was successfully added'),
1295 35 => array ('code' => 'success', 'format' => 'Real server was successfully deleted'),
1296 36 => array ('code' => 'success', 'format' => 'Real server was successfully updated'),
1297 37 => array ('code' => 'success', 'format' => 'Successfully added %u real servers'),
1298 38 => array ('code' => 'success', 'format' => '%u real server(s) were successfully (de)activated'),
1299 39 => array ('code' => 'success', 'format' => 'User account %s updated.'),
1300 40 => array ('code' => 'success', 'format' => 'User account %s created.'),
1301 41 => array ('code' => 'success', 'format' => 'User account disabled.'),
1302 42 => array ('code' => 'success', 'format' => 'User account enabled.'),
1303 43 => array ('code' => 'success', 'format' => 'Saved successfully.'),
1304 44 => array ('code' => 'success', 'format' => '%s failures and %s successfull changes.'),
1305 45 => array ('code' => 'success', 'format' => "Attribute '%s' created."),
1306 46 => array ('code' => 'success', 'format' => 'Rename successful.'),
1307 47 => array ('code' => 'success', 'format' => 'Attribute was deleted.'),
1308 48 => array ('code' => 'success', 'format' => 'Supplement succeeded.'),
1309 49 => array ('code' => 'success', 'format' => 'Reduction succeeded.'),
1310 50 => array ('code' => 'success', 'format' => 'Reduction succeeded.'),
1311 51 => array ('code' => 'success', 'format' => 'Update succeeded.'),
1312 52 => array ('code' => 'success', 'format' => 'Supplement succeeded.'),
1313 53 => array ('code' => 'success', 'format' => 'Chapter was deleted.'),
1314 54 => array ('code' => 'success', 'format' => 'Chapter was updated.'),
1315 55 => array ('code' => 'success', 'format' => 'Chapter was added.'),
1316 56 => array ('code' => 'success', 'format' => 'Update succeeded.'),
1317 57 => array ('code' => 'success', 'format' => 'Reset complete'),
1318 58 => array ('code' => 'success', 'format' => "Successfully deleted tag ."),
1319 59 => array ('code' => 'success', 'format' => "Created tag '%s'."),
1320 60 => array ('code' => 'success', 'format' => "Updated tag '%s'."),
1321 61 => array ('code' => 'success', 'format' => 'Password changed successfully.'),
1322 62 => array ('code' => 'success', 'format' => 'gw: %s'),
1323 63 => array ('code' => 'success', 'format' => '%u change request(s) have been processed'),
1324 64 => array ('code' => 'success', 'format' => 'Port %s@%s has been assigned to VLAN %u'),
1325 65 => array ('code' => 'success', 'format' => "Added new rack '%s'"),
1326 66 => array ('code' => 'success', 'format' => "File sent Ok via handler '%s'"),
1327 67 => array ('code' => 'success', 'format' => "Tag rolling done, %u objects involved"),
1328 68 => array ('code' => 'success', 'format' => "Updated rack '%s'"),
1329
1330 100 => array ('code' => 'error', 'format' => 'Generic error: %s'),
1331 101 => array ('code' => 'error', 'format' => 'Port name cannot be empty'),
1332 102 => array ('code' => 'error', 'format' => "Error creating user account '%s'"),
1333 103 => array ('code' => 'error', 'format' => 'getHashByID() failed'),
1334 104 => array ('code' => 'error', 'format' => "Error updating user account '%s'"),
1335 105 => array ('code' => 'error', 'format' => 'Error enabling user account.'),
1336 106 => array ('code' => 'error', 'format' => 'Error disabling user account.'),
1337 107 => array ('code' => 'error', 'format' => 'Admin account cannot be disabled'),
1338 108 => array ('code' => 'error', 'format' => '%u failures and %u successfull changes.'),
1339 109 => array ('code' => 'error', 'format' => 'Update failed!'),
1340 110 => array ('code' => 'error', 'format' => 'Supplement failed!'),
1341 111 => array ('code' => 'error', 'format' => 'Reduction failed!'),
1342 112 => array ('code' => 'error', 'format' => 'Error adding chapter.'),
1343 113 => array ('code' => 'error', 'format' => 'Error updating chapter.'),
1344 114 => array ('code' => 'error', 'format' => 'Error deleting chapter.'),
1345 115 => array ('code' => 'error', 'format' => 'Error renaming attribute.'),
1346 116 => array ('code' => 'error', 'format' => 'Error creating attribute.'),
1347 117 => array ('code' => 'error', 'format' => 'Error deleting attribute.'),
1348 118 => array ('code' => 'error', 'format' => 'Supplement failed!'),
1349 119 => array ('code' => 'error', 'format' => 'Reduction failed!'),
1350 120 => array ('code' => 'error', 'format' => 'Reset failed!'),
1351 121 => array ('code' => 'error', 'format' => 'commitUpdateObject() failed'),
1352 122 => array ('code' => 'error', 'format' => 'One or more update(s) failed!'),
1353 123 => array ('code' => 'error', 'format' => 'Cannot process submitted data: unknown format code.'),
1354 124 => array ('code' => 'error', 'format' => 'Error removing reservation!'),
1355 125 => array ('code' => 'error', 'format' => "Update failed with error: '%s'"),
1356 126 => array ('code' => 'error', 'format' => 'addRStoRSPool() failed'),
1357 127 => array ('code' => 'error', 'format' => 'Added %u real servers and encountered %u errors'),
1358 128 => array ('code' => 'error', 'format' => 'commitDeleteRS() failed'),
1359 129 => array ('code' => 'error', 'format' => 'commitDeleteLB() failed'),
1360 130 => array ('code' => 'error', 'format' => 'commitDeleteVS() failed'),
1361 131 => array ('code' => 'error', 'format' => 'invalid format requested'),
1362 132 => array ('code' => 'error', 'format' => 'invalid protocol'),
1363 133 => array ('code' => 'error', 'format' => 'commitUpdateRS() failed'),
1364 134 => array ('code' => 'error', 'format' => 'commitUpdateLB() failed'),
1365 135 => array ('code' => 'error', 'format' => 'commitUpdateVS() failed'),
1366 136 => array ('code' => 'error', 'format' => 'addLBtoRSPool() failed'),
1367 137 => array ('code' => 'error', 'format' => 'addLBtoRSPool() failed'),
1368 138 => array ('code' => 'error', 'format' => 'commitDeleteRSPool() failed'),
1369 139 => array ('code' => 'error', 'format' => 'commitUpdateRSPool() failed'),
1370 140 => array ('code' => 'error', 'format' => 'Encountered %u errors, (de)activated %u real servers'),
1371 141 => array ('code' => 'error', 'format' => 'Encountered %u errors, updated %u IP address(es)'),
1372 142 => array ('code' => 'error', 'format' => 'executeAutoPorts() failed'),
1373 143 => array ('code' => 'error', 'format' => 'Tried chaining %u tags, but experienced %u errors.'),
1374 144 => array ('code' => 'error', 'format' => "Error deleting tag: '%s'"),
1375 145 => array ('code' => 'error', 'format' => "Invalid tag name '%s'"),
1376 146 => array ('code' => 'error', 'format' => "Tag '%s' (or similar name) already exists"),
1377 147 => array ('code' => 'error', 'format' => "Could not create tag '%s' because of error '%s'"),
1378 148 => array ('code' => 'error', 'format' => "Could not update tag '%s' because of error '%s'"),
1379 149 => array ('code' => 'error', 'format' => 'Turing test failed'),
1380 150 => array ('code' => 'error', 'format' => 'Can only change password under DB authentication.'),
1381 151 => array ('code' => 'error', 'format' => 'Old password doesn\'t match.'),
1382 152 => array ('code' => 'error', 'format' => 'New passwords don\'t match.'),
1383 153 => array ('code' => 'error', 'format' => 'Password change failed.'),
1384 154 => array ('code' => 'error', 'format' => "Verification error: %s"),
1385 155 => array ('code' => 'error', 'format' => 'Save failed.'),
1386 156 => array ('code' => 'error', 'format' => 'getSwitchVLANs() failed'),
1387 157 => array ('code' => 'error', 'format' => 'operation not permitted'),
1388 158 => array ('code' => 'error', 'format' => 'Ignoring malformed record #%u in form submit'),
1389 159 => array ('code' => 'error', 'format' => 'Permission denied moving port %s from VLAN%u to VLAN%u'),
1390 160 => array ('code' => 'error', 'format' => 'Invalid arguments'),
1391 161 => array ('code' => 'error', 'format' => 'Endpoint not found. Please either set FQDN attribute or assign an IP address to the object.'),
1392 162 => array ('code' => 'error', 'format' => 'More than one IP address is assigned to this object, please configure FQDN attribute.'),
1393 163 => array ('code' => 'error', 'format' => 'Failed to get any response from queryGateway() or the gateway died'),
1394 164 => array ('code' => 'error', 'format' => 'Gateway failure: %s.'),
1395 165 => array ('code' => 'error', 'format' => 'Gateway failure: malformed reply.'),
1396 166 => array ('code' => 'error', 'format' => 'gw: %s'),
1397 167 => array ('code' => 'error', 'format' => 'Could not find port %s'),
1398 168 => array ('code' => 'error', 'format' => 'Port %s is a trunk'),
1399 169 => array ('code' => 'error', 'format' => 'Failed to configure %s, connector returned code %u'),
1400 170 => array ('code' => 'error', 'format' => 'There is no network for IP address "%s"'),
1401 171 => array ('code' => 'error', 'format' => "Failed creating rack '%s'. Already exists in this row?"),
1402 172 => array ('code' => 'error', 'format' => 'Malformed request'),
1403 173 => array ('code' => 'error', 'format' => "Invalid IPv4 prefix '%s'"),
1404 174 => array ('code' => 'error', 'format' => 'Bad IPv4 address'),
1405 175 => array ('code' => 'error', 'format' => 'Invalid netmask'),
1406 176 => array ('code' => 'error', 'format' => 'This network already exists'),
1407 177 => array ('code' => 'error', 'format' => 'commitUpdateRack() failed'),
1408
1409 200 => array ('code' => 'warning', 'format' => 'generic warning: %s'),
1410 201 => array ('code' => 'warning', 'format' => 'nothing happened...'),
1411 202 => array ('code' => 'warning', 'format' => 'gw: %s'),
1412 203 => array ('code' => 'warning', 'format' => 'Port %s seems to be the first in VLAN %u at this switch.'),
1413 204 => array ('code' => 'warning', 'format' => 'Check uplink/downlink configuration for proper operation.'),
1414 205 => array ('code' => 'warning', 'format' => '%u change request(s) have been ignored'),
1415 );
1416 // Handle the arguments. Is there any better way to do it?
1417 foreach ($log['m'] as $record)
1418 {
1419 if (!isset ($record['c']) or !isset ($msginfo[$record['c']]))
1420 {
1421 echo '<div class=msg_neutral>(this message was lost)</div>';
1422 continue;
1423 }
1424 if (isset ($record['a']))
1425 switch (count ($record['a']))
1426 {
1427 case 1:
1428 $msgtext = sprintf
1429 (
1430 $msginfo[$record['c']]['format'],
1431 $record['a'][0]
1432 );
1433 break;
1434 case 2:
1435 $msgtext = sprintf
1436 (
1437 $msginfo[$record['c']]['format'],
1438 $record['a'][0],
1439 $record['a'][1]
1440 );
1441 break;
1442 case 3:
1443 $msgtext = sprintf
1444 (
1445 $msginfo[$record['c']]['format'],
1446 $record['a'][0],
1447 $record['a'][1],
1448 $record['a'][2]
1449 );
1450 break;
1451 case 4:
1452 default:
1453 $msgtext = sprintf
1454 (
1455 $msginfo[$record['c']]['format'],
1456 $record['a'][0],
1457 $record['a'][1],
1458 $record['a'][2],
1459 $record['a'][3]
1460 );
1461 break;
1462 }
1463 else
1464 $msgtext = $msginfo[$record['c']]['format'];
1465 echo '<div class=msg_' . $msginfo[$record['c']]['code'] . ">${msgtext}</div>";
1466 }
1467 break;
1468 default:
1469 echo '<div class=msg_error>' . __FUNCTION__ . ': internal error</div>';
1470 break;
1471 }
1472 }
1473
1474 /*
1475 The following conditions must be followed:
1476 1. We can mount onto free atoms only. This means: if any record for an atom
1477 already exists in RackSpace, it can't be used for mounting.
1478 2. We can't unmount from 'W' atoms. Operator should review appropriate comments
1479 and either delete them before unmounting or refuse to unmount the object.
1480 */
1481
1482 // We extensively use $_REQUEST in the function.
1483 // FIXME: move related code into ophandler
1484 function renderRackSpaceForObject ($object_id = 0)
1485 {
1486 if ($object_id <= 0)
1487 {
1488 showError ('Invalid object_id', __FUNCTION__);
1489 return;
1490 }
1491 $is_submit = isset ($_REQUEST['got_atoms']);
1492 $is_update = isset ($_REQUEST['rackmulti'][0]);
1493 $info = getObjectInfo ($object_id);
1494 if ($info == NULL)
1495 {
1496 showError ('getObjectInfo() failed', __FUNCTION__);
1497 return;
1498 }
1499 // Always process occupied racks plus racks chosen by user. First get racks with
1500 // already allocated rackspace...
1501 $workingRacksData = getResidentRacksData ($object_id);
1502 if ($workingRacksData === NULL)
1503 {
1504 print_r ($workingRacksData);
1505 showError ('getResidentRacksData() failed', __FUNCTION__);
1506 return;
1507 }
1508
1509 // ...and then add those chosen by user (if any).
1510 if ($is_update)
1511 foreach ($_REQUEST['rackmulti'] as $cand_id)
1512 {
1513 if (!isset ($workingRacksData[$cand_id]))
1514 {
1515 $rackData = getRackData ($cand_id);
1516 if ($rackData == NULL)
1517 {
1518 showError ('getRackData() failed', __FUNCTION__);
1519 return NULL;
1520 }
1521 $workingRacksData[$cand_id] = $rackData;
1522 }
1523 }
1524
1525 // Do it only once...
1526 foreach ($workingRacksData as &$rackData)
1527 applyObjectMountMask ($rackData, $object_id);
1528 // Now we workaround an old caveat: http://bugs.php.net/bug.php?id=37410
1529 unset ($rackData);
1530
1531 // Here we process form submit by trying to save all submitted info to database.
1532 if ($is_submit)
1533 {
1534 $oldMolecule = getMoleculeForObject ($object_id);
1535 $worldchanged = FALSE;
1536 $log = array();
1537 foreach ($workingRacksData as $rack_id => $rackData)
1538 {
1539 $logrecord = processGridForm ($rackData, 'F', 'T', $object_id);
1540 $log[] = $logrecord;
1541 if ($logrecord['code'] != 300)
1542 {
1543 $worldchanged = TRUE;
1544 // Reload our working copy after form processing.
1545 $rackData = getRackData ($rack_id);
1546 if ($rackData == NULL)
1547 $log[] = array ('code' => 500, 'message' => 'Working copy update failed in ', __FUNCTION__);
1548 applyObjectMountMask ($rackData, $object_id);
1549 $workingRacksData[$rack_id] = $rackData;
1550 }
1551 }
1552 if ($worldchanged)
1553 {
1554 // Log a record.
1555 $newMolecule = getMoleculeForObject ($object_id);
1556 $oc = count ($oldMolecule);
1557 $nc = count ($newMolecule);
1558 $omid = $oc ? createMolecule ($oldMolecule) : 'NULL';
1559 $nmid = $nc ? createMolecule ($newMolecule) : 'NULL';
1560 global $remote_username;
1561 $comment = empty ($_REQUEST['comment']) ? 'NULL' : "'${_REQUEST['comment']}'";
1562 $query =
1563 "insert into MountOperation(object_id, old_molecule_id, new_molecule_id, user_name, comment) " .
1564 "values (${object_id}, ${omid}, ${nmid}, '${remote_username}', ${comment})";
1565 global $dbxlink;
1566 $result = $dbxlink->query ($query);
1567 if ($result == NULL)
1568 $log[] = array ('code' => 'error', 'message' => 'SQL query failed during history logging.');
1569 else
1570 $log[] = array ('code' => 'success', 'message' => 'History logged.');
1571 }
1572 printLog ($log);
1573 }
1574
1575 // This is the time for rendering.
1576 global $root, $pageno, $tabno;
1577 echo "<form id='racks' action='${root}'>";
1578 echo "<input type=hidden name=page value='${pageno}'>\n";
1579 echo "<input type=hidden name=tab value='${tabno}'>\n";
1580 echo "<input type=hidden name=object_id value='${object_id}'>\n";
1581 // Main layout starts.
1582 echo "<table border=0 class=objectview cellspacing=0 cellpadding=0><tr>";
1583
1584 // Left portlet with rack list.
1585 echo "<td class=pcleft height='1%'>";
1586 startPortlet ('Racks');
1587 $allRacksData = getRacksForRow();
1588 if (count ($allRacksData) <= getConfigVar ('RACK_PRESELECT_THRESHOLD'))
1589 {
1590 foreach (array_keys ($allRacksData) as $rack_id)
1591 {
1592 $rackData = getRackData ($rack_id);
1593 if ($rackData == NULL)
1594 {
1595 showError ('getRackData() failed', __FUNCTION__);
1596 return NULL;
1597 }
1598 $workingRacksData[$rack_id] = $rackData;
1599 }
1600 foreach ($workingRacksData as &$rackData)
1601 applyObjectMountMask ($rackData, $object_id);
1602 unset ($rackData);
1603 }
1604 renderRackMultiSelect ('rackmulti[]', $allRacksData, array_keys ($workingRacksData));
1605 echo "<br>";
1606 echo "<br>";
1607 finishPortlet();
1608 echo "</td>";
1609
1610 // Middle portlet with comment and submit.
1611 echo "<td class=pcleft>";
1612 startPortlet ('Comment');
1613 echo "<textarea name=comment rows=10 cols=40></textarea><br>\n";
1614 echo "<input type=submit value='Save' name=got_atoms>\n";
1615 echo "<br>";
1616 echo "<br>";
1617 finishPortlet();
1618 echo "</td>";
1619
1620 // Right portlet with rendered racks. If this form submit is not final, we have to
1621 // reflect the former state of the grid in current form.
1622 echo "<td class=pcright rowspan=2 height='1%'>";
1623 startPortlet ('Working copy');
1624 echo '<table border=0 cellspacing=10 align=center><tr>';
1625 foreach ($workingRacksData as $rack_id => $rackData)
1626 {
1627 // Order is important here: only original allocation is highlighted.
1628 highlightObject ($rackData, $object_id);
1629 markupAtomGrid ($rackData, 'T');
1630 // If we have a form processed, discard user input and show new database
1631 // contents.
1632 if (!$is_submit and $is_update)
1633 mergeGridFormToRack ($rackData);
1634 echo "<td valign=top>";
1635 echo "<center>\n<h2>${rackData['name']}</h2>\n";
1636 echo "<table class=rack border=0 cellspacing=0 cellpadding=1>\n";
1637 echo "<tr><th width='10%'>&nbsp;</th><th width='20%'>Front</th>";
1638 echo "<th width='50%'>Interior</th><th width='20%'>Back</th></tr>\n";
1639 renderAtomGrid ($rackData);
1640 echo "<tr><th width='10%'>&nbsp;</th><th width='20%'>Front</th>";
1641 echo "<th width='50%'>Interior</th><th width='20%'>Back</th></tr>\n";
1642 echo "</table></center>\n";
1643 echo '</td>';
1644 }
1645 echo "</tr></table>";
1646 finishPortlet();
1647 echo "</td>\n";
1648
1649 echo "</form>\n";
1650 echo "</tr></table>\n";
1651 }
1652
1653 function renderMolecule ($mdata, $object_id)
1654 {
1655 // sort data out
1656 $rackpack = array();
1657 global $loclist;
1658 foreach ($mdata as $rua)
1659 {
1660 $rack_id = $rua['rack_id'];
1661 $unit_no = $rua['unit_no'];
1662 $atom = $rua['atom'];
1663 if (!isset ($rackpack[$rack_id]))
1664 {
1665 $rackData = getRackData ($rack_id);
1666 for ($i = $rackData['height']; $i > 0; $i--)
1667 for ($locidx = 0; $locidx < 3; $locidx++)
1668 $rackData[$i][$locidx]['state'] = 'F';
1669 $rackpack[$rack_id] = $rackData;
1670 }
1671 $rackpack[$rack_id][$unit_no][$loclist[$atom]]['state'] = 'T';
1672 $rackpack[$rack_id][$unit_no][$loclist[$atom]]['object_id'] = $object_id;
1673 }
1674 // now we have some racks to render
1675 foreach ($rackpack as $rackData)
1676 {
1677 markAllSpans ($rackData);
1678 echo "<table class=molecule cellspacing=0>\n";
1679 echo "<caption>${rackData['name']}</caption>\n";
1680 echo "<tr><th width='10%'>&nbsp;</th><th width='20%'>Front</th><th width='50%'>Interior</th><th width='20%'>Back</th></tr>\n";
1681 for ($i = $rackData['height']; $i > 0; $i--)
1682 {
1683 echo "<tr><th>$i</th>";
1684 for ($locidx = 0; $locidx < 3; $locidx++)
1685 {
1686 $state = $rackData[$i][$locidx]['state'];
1687 echo "<td class=state_${state}>&nbsp;</td>\n";
1688 }
1689 echo "</tr>\n";
1690 }
1691 echo "</table>\n";
1692 }
1693 }
1694
1695 function renderUnmountedObjectsPortlet ()
1696 {
1697 startPortlet ('Unmounted objects');
1698 $objs = getUnmountedObjects();
1699 if ($objs === NULL)
1700 {
1701 showError ('getUnmountedObjects() failed', __FUNCTION__);
1702 return;
1703 }
1704 global $root, $nextorder;
1705 $order = 'odd';
1706 echo '<br><br><table border=0 cellpadding=5 cellspacing=0 align=center class=cooltable>';
1707 echo '<tr><th>Common name</th><th>Visible label</th><th>Asset number</th><th>Barcode</th></tr>';
1708 foreach ($objs as $obj)
1709 {
1710 echo "<tr class=row_${order}><td><a href='${root}?page=object&object_id=${obj['id']}'>${obj['dname']}</a></td>";
1711 echo "<td>${obj['label']}</td>";
1712 echo "<td>${obj['asset_no']}</td>";
1713 echo "<td>${obj['barcode']}</td></tr>";
1714 $order = $nextorder[$order];
1715 }
1716 echo "</table><br>\n";
1717 finishPortlet();
1718 }
1719
1720 function renderProblematicObjectsPortlet ()
1721 {
1722 startPortlet ('Problematic objects');
1723 $objs = getProblematicObjects();
1724 if ($objs === NULL)
1725 {
1726 showError ('getProblematicObjects() failed', __FUNCTION__);
1727 return;
1728 }
1729 global $root, $nextorder;
1730 $order = 'odd';
1731 echo '<br><br><table border=0 cellpadding=5 cellspacing=0 align=center class=cooltable>';
1732 echo '<tr><th>Type</th><th>Common name</th></tr>';
1733 foreach ($objs as $obj)
1734 {
1735 echo "<tr class=row_${order}><td>${obj['objtype_name']}</td>";
1736 echo "<td><a href='${root}?page=object&object_id=${obj['id']}'>${obj['dname']}</a></tr>";
1737 $order = $nextorder[$order];
1738 }
1739 echo "</table><br>\n";
1740 finishPortlet();
1741 }
1742
1743 function renderObjectSpace ()
1744 {
1745 global $root, $taglist, $tagtree;
1746 echo "<table border=0 class=objectview>\n";
1747 echo "<tr><td class=pcleft width='50%'>";
1748 startPortlet ('View all by type');
1749 $groupInfo = getObjectGroupInfo();
1750 if ($groupInfo === NULL)
1751 {
1752 showError ('getObjectGroupInfo() failed', __FUNCTION__);
1753 return;
1754 }
1755 if (count ($groupInfo) == 0)
1756 echo "No objects exist in DB";
1757 else
1758 {
1759 echo '<div align=left><ul>';
1760 foreach ($groupInfo as $gi)
1761 echo "<li><a href='${root}?page=objgroup&group_id=${gi['id']}'>${gi['name']}</a> (${gi['count']})</li>";
1762 echo '</ul></div>';
1763 }
1764 finishPortlet();
1765
1766 echo '</td><td class=pcright>';
1767
1768 startPortlet ('View all by tag');
1769 if (count ($taglist) == 0)
1770 echo "No tags exist in DB";
1771 else
1772 renderTagCloud ('object');
1773 finishPortlet();
1774 echo "</td></tr></table>\n";
1775 }
1776
1777 function renderObjectGroup ()
1778 {
1779 global $root, $pageno, $tabno, $nextorder, $taglist, $tagtree;
1780 assertUIntArg ('group_id', __FUNCTION__, TRUE);
1781 $group_id = $_REQUEST['group_id'];
1782 $tagfilter = getTagFilter();
1783 $tagfilter_str = getTagFilterStr ($tagfilter);
1784 echo "<table border=0 class=objectview>\n";
1785 echo "<tr><td class=pcleft width='25%'>";
1786 startPortlet ('change type');
1787 $groupInfo = getObjectGroupInfo();
1788 if ($groupInfo === NULL)
1789 {
1790 showError ('getObjectGroupInfo() failed', __FUNCTION__);
1791 return;
1792 }
1793 if (count ($groupInfo) == 0)
1794 echo "No objects exist in DB";
1795 else
1796 {
1797 echo '<div align=left><ul>';
1798 foreach ($groupInfo as $gi)
1799 {
1800 echo "<li><a href='${root}?page=${pageno}&group_id=${gi['id']}${tagfilter_str}'>";
1801 if ($gi['id'] == $group_id)
1802 echo '<strong>';
1803 echo "${gi['name']}</a>";
1804 if ($gi['id'] == $group_id)
1805 echo '</strong>';
1806 echo " (${gi['count']})";
1807 if ($gi['id'] == $group_id)
1808 echo ' &larr;';
1809 echo "</li>";
1810 }
1811 echo '</ul></div>';
1812 }
1813 finishPortlet();
1814
1815 echo '</td><td class=pcleft>';
1816
1817 startPortlet ('Objects');
1818 $objects = getObjectList ($group_id, $tagfilter, getTFMode());
1819 if ($objects === NULL)
1820 {
1821 showError ('getObjectList() failed', __FUNCTION__);
1822 return;
1823 }
1824 echo '<br><br><table border=0 cellpadding=5 cellspacing=0 align=center class=cooltable>';
1825 echo '<tr><th>Common name</th><th>Visible label</th><th>Asset tag</th><th>Barcode</th><th>Rack</th></tr>';
1826 $order = 'odd';
1827 foreach ($objects as $obj)
1828 {
1829 if (isset ($_REQUEST['hl_object_id']) and $_REQUEST['hl_object_id'] == $obj['id'])
1830 $secondclass = 'tdleft port_highlight';
1831 else
1832 $secondclass = 'tdleft';
1833 echo "<tr class=row_${order}><td class='${secondclass}'><a href='${root}?page=object&object_id=${obj['id']}'>${obj['dname']}</a></td>";
1834 echo "<td class='${secondclass}'>${obj['label']}</td>";
1835 echo "<td class='${secondclass}'>${obj['asset_no']}</td>";
1836 echo "<td class='${secondclass}'>${obj['barcode']}</td>";
1837 if ($obj['rack_id'])
1838 echo "<td class='${secondclass}'><a href='${root}?page=rack&rack_id=${obj['rack_id']}'>${obj['Rack_name']}</a></td>";
1839 else
1840 echo "<td class='${secondclass}'>Unmounted</td>";
1841 echo '</tr>';
1842 $order = $nextorder[$order];
1843 }
1844 echo '</table>';
1845 finishPortlet();
1846
1847 echo "</td><td class=pcright width='25%'>";
1848
1849 renderTagFilterPortlet ($tagfilter, 'object', 'group_id', $group_id);
1850 echo "</td></tr></table>\n";
1851 }
1852
1853 function renderEmptyPortsSelect ($port_id, $type_id)
1854 {
1855 $ports = getEmptyPortsOfType($type_id);
1856 usort($ports, 'sortEmptyPorts');
1857 foreach ($ports as $port)
1858 {
1859 if ($port_id == $port['Port_id'])
1860 continue;
1861 echo "<option value='${port['Port_id']}' onclick='getElementById(\"remote_port_name\").value=\"${port['Port_name']}\"; getElementById(\"remote_object_name\").value=\"${port['Object_name']}\";'>${port['Object_name']} ${port['Port_name']}</option>\n";
1862 }
1863 }
1864
1865 function renderAllIPv4Allocations ()
1866 {
1867 $addresses = getAllIPv4Allocations();
1868 usort($addresses, 'sortObjectAddressesAndNames');
1869 foreach ($addresses as $address)
1870 {
1871 echo "<option value='${address['ip']}' onclick='getElementById(\"ip\").value=\"${address['ip']}\";'>${address['object_name']} ${address['name']} ${address['ip']}</option>\n";
1872 }
1873 }
1874
1875 // History viewer for history-enabled simple dictionaries.
1876 function renderHistory ($object_type, $object_id)
1877 {
1878 switch ($object_type)
1879 {
1880 case 'row':
1881 $query = "select ctime, user_name, name, deleted, comment from RackRowHistory where id = ${object_id} order by ctime";
1882 $header = '<tr><th>change time</th><th>author</th><th>rack row name</th><th>is deleted?</th><th>rack row comment</th></tr>';
1883 $extra = 4;
1884 break;
1885 case 'rack':
1886 $query =
1887 "select ctime, user_name, rh.name, rh.deleted, d.dict_value as name, rh.height, rh.comment " .
1888 "from RackHistory as rh left join Dictionary as d on rh.row_id = d.dict_key " .
1889 "natural join Chapter " .
1890 "where chapter_name = 'RackRow' and rh.id = ${object_id} order by ctime";
1891 $header = '<tr><th>change time</th><th>author</th><th>rack name</th><th>is deleted?</th><th>rack row name</th><th>rack height</th><th>rack comment</th></tr>';
1892 $extra = 6;
1893 break;
1894 case 'object':
1895 $query =
1896 "select ctime, user_name, name, label, barcode, asset_no, deleted, has_problems, dict_value, comment " .
1897 "from RackObjectHistory inner join Dictionary on objtype_id = dict_key natural join Chapter " .
1898 "where chapter_name = 'RackObjectType' and id=${object_id} order by ctime";
1899 $header = '<tr><th>change time</th><th>author</th><th>common name</th><th>visible label</th><th>barcode</th><th>asset no</th><th>is deleted?</th><th>has problems?</th><th>object type</th><th>comment</th></tr>';
1900 $extra = 9;
1901 break;
1902 default:
1903 showError ("Uknown object type '${object_type}'", __FUNCTION__);
1904 return;
1905 }
1906 global $dbxlink;
1907 $result = $dbxlink->query ($query);
1908 if ($result == NULL)
1909 {
1910 showError ('SQL query failed', __FUNCTION__);
1911 return;
1912 }
1913 echo '<table border=0 cellpadding=5 cellspacing=0 align=center class=cooltable>';
1914 $order = 'odd';
1915 global $nextorder;
1916 echo $header;
1917 while ($row = $result->fetch (PDO::FETCH_NUM))
1918 {
1919 echo "<tr class=row_${order}><td>${row[0]}</td>";
1920 for ($i = 1; $i <= $extra; $i++)
1921 echo "<td>" . $row[$i] . "</td>";
1922 echo "</tr>\n";
1923 $order = $nextorder[$order];
1924 }
1925 echo "</table><br>\n";
1926 }
1927
1928 function renderRackspaceHistory ()
1929 {
1930 global $root, $nextorder, $pageno, $tabno;
1931 $order = 'odd';
1932 $history = getRackspaceHistory();
1933 // Show the last operation by default.
1934 if (isset ($_REQUEST['op_id']))
1935 $op_id = $_REQUEST['op_id'];
1936 elseif (isset ($history[0]['mo_id']))
1937 $op_id = $history[0]['mo_id'];
1938 else $op_id = NULL;
1939
1940 $omid = NULL;
1941 $nmid = NULL;
1942 $object_id = 1;
1943 if ($op_id)
1944 list ($omid, $nmid) = getOperationMolecules ($op_id);
1945
1946 // Main layout starts.
1947 echo "<table border=0 class=objectview cellspacing=0 cellpadding=0>";
1948
1949 // Left top portlet with old allocation.
1950 echo "<tr><td class=pcleft>";
1951 startPortlet ('Old allocation');
1952 if ($omid)
1953 {
1954 $oldMolecule = getMolecule ($omid);
1955 renderMolecule ($oldMolecule, $object_id);
1956 }
1957 else
1958 echo "nothing";
1959 finishPortlet();
1960
1961 echo '</td><td class=pcright>';
1962
1963 // Right top portlet with new allocation
1964 startPortlet ('New allocation');
1965 if ($nmid)
1966 {
1967 $newMolecule = getMolecule ($nmid);
1968 renderMolecule ($newMolecule, $object_id);
1969 }
1970 else
1971 echo "nothing";
1972 finishPortlet();
1973
1974 echo '</td></tr><tr><td colspan=2>';
1975
1976 // Bottom portlet with list
1977
1978 startPortlet ('Rackspace allocation history');
1979 echo "<table border=0 cellpadding=5 cellspacing=0 align=center class=cooltable>\n";
1980 echo "<tr><th>timestamp</th><th>author</th><th>rack object ID</th><th>rack object type</th><th>rack object name</th><th>comment</th></tr>\n";
1981 foreach ($history as $row)
1982 {
1983 if ($row['mo_id'] == $op_id)
1984 $class = 'hl';
1985 else
1986 $class = "row_${order}";
1987 echo "<tr class=${class}><td><a href='${root}?page=${pageno}&tab=${tabno}&op_id=${row['mo_id']}'>${row['ctime']}</a></td>";
1988 echo "<td>${row['user_name']}</td>";
1989 echo "<td>${row['ro_id']}</td><td>${row['objtype_name']}</td><td>${row['name']}</td><td>${row['comment']}</td>\n";
1990 echo "</tr>\n";
1991 $order = $nextorder[$order];
1992 }
1993 echo "</table>\n";
1994 finishPortlet();
1995
1996 echo '</td></tr></table>';
1997 }
1998
1999 function renderIPv4SpaceRecords ($tree, &$tagcache, $baseurl, $level = 1)
2000 {
2001 $self = __FUNCTION__;
2002 foreach ($tree as $item)
2003 {
2004 $total = $item['addrt'];
2005 if (isset ($item['id']))
2006 {
2007 loadIPv4AddrList ($item);
2008 $used = $item['addrc'];
2009 if ($item['symbol'] == 'node-collapsed')
2010 $expandurl = "${baseurl}&eid=" . $item['id'];
2011 elseif ($item['symbol'] == 'node-expanded')
2012 $expandurl = "${baseurl}&eid=" . $item['parent_id'];
2013 else
2014 $expandurl = '';
2015 echo "<tr valign=top>";
2016 printIPv4NetInfoTDs ($item, 'tdleft', $level, $item['symbol'], $expandurl);
2017 echo "<td class=tdcenter>";
2018 renderProgressBar ($total ? $used/$total : 0);
2019 echo "<br><small>${used}/${total}</small></td>";
2020 if (getConfigVar ('EXT_IPV4_VIEW') == 'yes')
2021 printRoutersTD (findRouters ($item['addrlist']), $tagcache);
2022 echo "</tr>";
2023 if ($item['symbol'] == 'node-expanded' or $item['symbol'] == 'node-expanded-static')
2024 $self ($item['kids'], $tagcache, $baseurl, $level + 1);
2025 }
2026 else
2027 {
2028 $used = 0;
2029 echo "<tr valign=top>";
2030 printIPv4NetInfoTDs ($item, 'tdleft sparenetwork', $level, $item['symbol']);
2031 echo "<td class=tdcenter>";
2032 renderProgressBar ($used/$total, 'sparenetwork');
2033 echo "<br><small>${used}/${total}</small></td>";
2034 echo "<td>&nbsp;</td></tr>";
2035 }
2036 }
2037 }
2038
2039 function renderIPv4Space ()
2040 {
2041 global $root, $pageno, $tabno;
2042 $tagfilter = getTagFilter();
2043 $netlist = getIPv4NetworkList ($tagfilter, getTFMode());
2044 $netcount = count ($netlist);
2045 $tree = prepareIPv4Tree ($netlist, isset ($_REQUEST['eid']) ? $_REQUEST['eid'] : 0);
2046 unset ($netlist);
2047
2048 echo "<table border=0 class=objectview>\n";
2049 echo "<tr><td class=pcleft>";
2050 startPortlet ("networks (${netcount})");
2051 echo "<table class='widetable' border=0 cellpadding=5 cellspacing=0 align='center'>\n";
2052 echo "<tr><th>prefix</th><th>name/tags</th><th>%% used</th>";
2053 if (getConfigVar ('EXT_IPV4_VIEW') == 'yes')
2054 echo "<th>routed by</th>";
2055 echo "</tr>\n";
2056 $tagcache = array();
2057 $baseurl = "${root}?page=${pageno}&tab=${tabno}&tagfilter[]=" . getTagFilterStr();
2058 renderIPv4SpaceRecords ($tree, $tagcache, $baseurl);
2059 echo "</table>\n";
2060 finishPortlet();
2061 echo '</td><td class=pcright>';
2062 renderTagFilterPortlet ($tagfilter, 'ipv4net');
2063 echo "</td></tr></table>\n";
2064 }
2065
2066 function renderIPv4SLB ()
2067 {
2068 global $root, $page, $nextorder;
2069
2070 startPortlet ('SLB configuration');
2071 echo "<table border=0 width='100%'><tr>";
2072 foreach (array ('ipv4vslist', 'ipv4rsplist', 'rservers', 'lbs') as $pno)
2073 echo "<td><h3><a href='${root}?page=${pno}'>" . $page[$pno]['title'] . "</a></h3></td>";
2074 echo '</tr></table>';
2075 finishPortlet();
2076
2077 $summary = getSLBSummary();
2078 startPortlet ('SLB tactical overview');
2079 // A single id-keyed array isn't used here to preserve existing
2080 // order of LBs returned by getSLBSummary()
2081 $lblist = array();
2082 $lbdname = array();
2083 foreach ($summary as $vipdata)
2084 foreach (array_keys ($vipdata['lblist']) as $lb_object_id)
2085 if (!in_array ($lb_object_id, $lblist))
2086 {
2087 $oi = getObjectInfo ($lb_object_id);
2088 $lbdname[$lb_object_id] = $oi['dname'];
2089 $lblist[] = $lb_object_id;
2090 }
2091 if (!count ($summary))
2092 echo 'none configured';
2093 else
2094 {
2095 $order = 'odd';
2096 echo "<table class='widetable' border=0 cellpadding=5 cellspacing=0 align='center'>\n";
2097 echo "<tr><th>VS&nbsp;&darr; LB&nbsp;&rarr;</th>";
2098 foreach ($lblist as $lb_object_id)
2099 echo "<th><a href='${root}?page=object&tab=default&object_id=${lb_object_id}'>" . $lbdname[$lb_object_id] . "</a></th>";
2100 echo "</tr>\n";
2101 foreach ($summary as $vsid => $vsdata)
2102 {
2103 echo "<tr class=row_${order}><td class=tdleft><a href='$root?page=ipv4vs&tab=default&vs_id=${vsid}'>";
2104 echo buildVServiceName ($vsdata);
2105 echo '</a>';
2106 if (!empty ($vsdata['name']))
2107 echo "<br>${vsdata['name']}";
2108 echo "</td>";
2109 foreach ($lblist as $lb_object_id)
2110 {
2111 echo '<td class=tdleft>';
2112 if (!isset ($vsdata['lblist'][$lb_object_id]))
2113 echo '&nbsp;';
2114 else
2115 {
2116 echo $vsdata['lblist'][$lb_object_id]['size'];
2117 // echo " (<a href='${root}?page=ipv4rsp&pool_id=";
2118 // echo $vsdata['lblist'][$lb_object_id]['id'] . "'>";
2119 // echo $vsdata['lblist'][$lb_object_id]['name'] . '</a>)';
2120 }
2121 echo '</td>';
2122 }
2123 echo "</tr>\n";
2124 $order = $nextorder[$order];
2125 }
2126 echo "</table>\n";
2127 }
2128 finishPortlet ();
2129 }
2130
2131 function renderIPv4SpaceEditor ()
2132 {
2133 global $root, $pageno, $tabno;
2134 showMessageOrError();
2135
2136 // IPv4 validator
2137 ?>
2138 <script type="text/javascript">
2139 function init() {
2140 document.add_new_range.range.setAttribute('match', "^\\d\\d?\\d?\\.\\d\\d?\\d?\\.\\d\\d?\\d?\\.\\d\\d?\\d?\\/\\d\\d?$");
2141
2142 Validate.init();
2143 }
2144 window.onload=init;
2145 </script>
2146 <?php
2147
2148 startPortlet ("Add new");
2149 echo '<table border=0 cellpadding=10 align=center>';
2150 // This form requires a name, so JavaScript validator can find it.
2151 // No printOpFormIntro() hence
2152 echo "<form method=post name='add_new_range' action='${root}process.php'>\n";
2153 echo "<input type=hidden name=op value=addIPv4Prefix>\n";
2154 echo "<input type=hidden name=page value='${pageno}'>\n";
2155 echo "<input type=hidden name=tab value='${tabno}'>\n";
2156 // tags column
2157 echo '<tr><td rowspan=4><h3>assign tags</h3>';
2158 renderTagSelect();
2159 echo '</td>';
2160 // inputs column
2161 echo "<th class=tdright>prefix</th><td class=tdleft><input type=text name='range' size=18 class='live-validate' tabindex=1></td>";
2162 echo "<tr><th class=tdright>name</th><td class=tdleft><input type=text name='name' size='20' tabindex=2></td></tr>";
2163 echo "<tr><th class=tdright>connected network</th><td class=tdleft><input type=checkbox name='is_bcast' tabindex=3 checked></td></tr>";
2164 echo "<tr><td colspan=2>";
2165 printImageHREF ('CREATE', 'Add a new network', TRUE, 4);
2166 echo '</td></tr>';
2167 echo "</form></table><br><br>\n";
2168 finishPortlet();
2169
2170 $addrspaceList = getIPv4NetworkList();
2171 if (count ($addrspaceList))
2172 {
2173 startPortlet ('Manage existing (' . count ($addrspaceList) . ')');
2174 echo "<table class='widetable' border=0 cellpadding=5 cellspacing=0 align='center'>\n";
2175 echo "<tr><th>&nbsp;</th><th>prefix</th><th>name</th><th>&nbsp;</th></tr>";
2176 foreach ($addrspaceList as $iprange)
2177 {
2178 $netdata = getIPv4NetworkInfo ($iprange['id']);
2179 loadIPv4AddrList ($netdata);
2180 $usedips = count ($netdata['addrlist']);
2181 $totalips = binInvMaskFromDec ($netdata['mask']) + 1;
2182 echo "<form method=post action='${root}process.php?page=${pageno}&tab=${tabno}&op=updIPv4Prefix&id=${iprange['id']}'>";
2183 echo '<tr valign=top><td>';
2184 if ($usedips == 0)
2185 {
2186 echo "<a href='${root}process.php?op=delIPv4Prefix&page=${pageno}&tab=${tabno}&id=${iprange['id']}'>";
2187 printImageHREF ('delete', 'Delete this IP range');
2188 echo "</a>";
2189 }
2190 else
2191 printImageHREF ('nodelete', 'There are IP addresses allocated or reserved');
2192 echo "</td>\n<td class=tdleft>${netdata['ip']}/${netdata['mask']}</td>";
2193 echo "<td><input type=text name=name size=40 value='${netdata['name']}'>";
2194 echo "</td><td>";
2195 printImageHREF ('save', 'Save changes', TRUE);
2196 echo "</td></tr></form>\n";
2197 }
2198 echo "</table>";
2199 finishPortlet();
2200 }
2201 }
2202
2203 function renderIPv4Network ($id)
2204 {
2205 global $root, $pageno, $tabno, $aac2;
2206 $netmaskbylen = array
2207 (
2208 32 => '255.255.255.255',
2209 31 => '255.255.255.254',
2210 30 => '255.255.255.252',
2211 29 => '255.255.255.248',
2212 28 => '255.255.255.240',
2213 27 => '255.255.255.224',
2214 26 => '255.255.255.192',
2215 25 => '255.255.255.128',
2216 24 => '255.255.255.0',
2217 23 => '255.255.254.0',
2218 22 => '255.255.252.0',
2219 21 => '255.255.248.0',
2220 20 => '255.255.240.0',
2221 19 => '255.255.224.0',
2222 18 => '255.255.192.0',
2223 17 => '255.255.128.0',
2224 16 => '255.255.0.0',
2225 15 => '255.254.0.0',
2226 14 => '255.252.0.0',
2227 13 => '255.248.0.0',
2228 12 => '255.240.0.0',
2229 11 => '255.224.0.0',
2230 10 => '255.192.0.0',
2231 9 => '255.128.0.0',
2232 8 => '255.0.0.0',
2233 7 => '254.0.0.0',
2234 6 => '252.0.0.0',
2235 5 => '248.0.0.0',
2236 4 => '240.0.0.0',
2237 3 => '224.0.0.0',
2238 2 => '192.0.0.0',
2239 1 => '128.0.0.0'
2240 );
2241 $wildcardbylen = array
2242 (
2243 32 => '0.0.0.0',
2244 31 => '0.0.0.1',
2245 30 => '0.0.0.3',
2246 29 => '0.0.0.7',
2247 28 => '0.0.0.15',
2248 27 => '0.0.0.31',
2249 26 => '0.0.0.63',
2250 25 => '0.0.0.127',
2251 24 => '0.0.0.255',
2252 23 => '0.0.1.255',
2253 22 => '0.0.3.255',
2254 21 => '0.0.7.255',
2255 20 => '0.0.15.255',
2256 19 => '0.0.31.255',
2257 18 => '0.0.63.255',
2258 17 => '0.0.127.255',
2259 16 => '0.0.255.25',
2260 15 => '0.1.255.255',
2261 14 => '0.3.255.255',
2262 13 => '0.7.255.255',
2263 12 => '0.15.255.255',
2264 11 => '0.31.255.255',
2265 10 => '0.63.255.255',
2266 9 => '0.127.255.255',
2267 8 => '0.255.255.255',
2268 7 => '1.255.255.255',
2269 6 => '3.255.255.255',
2270 5 => '7.255.255.255',
2271 4 => '15.255.255.255',
2272 3 => '31.255.255.255',
2273 2 => '63.255.255.255',
2274 1 => '127.255.255.255'
2275 );
2276 $maxperpage = getConfigVar ('IPV4_ADDRS_PER_PAGE');
2277 if (isset($_REQUEST['pg']))
2278 $page = $_REQUEST['pg'];
2279 else
2280 $page=0;
2281
2282 $range = getIPv4NetworkInfo ($id);
2283 loadIPv4AddrList ($range);
2284 echo "<table border=0 class=objectview cellspacing=0 cellpadding=0>";
2285 echo "<tr><td colspan=2 align=center><h1>${range['ip']}/${range['mask']}</h1><h2>${range['name']}</h2></td></tr>\n";
2286
2287 echo "<tr><td class=pcleft width='50%'>";
2288 startPortlet ('summary');
2289 $total = ($range['ip_bin'] | $range['mask_bin_inv']) - ($range['ip_bin'] & $range['mask_bin']) + 1;
2290 $used = count ($range['addrlist']);
2291 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
2292
2293 echo "<tr><th width='50%' class=tdright>%% used:</th><td class=tdleft>";
2294 renderProgressBar ($used/$total);
2295 echo "&nbsp;${used}/${total}</td></tr>\n";
2296
2297 echo "<tr><th width='50%' class=tdright>Netmask:</th><td class=tdleft>";
2298 echo $netmaskbylen[$range['mask']];
2299 echo "</td></tr>\n";
2300
2301 echo "<tr><th width='50%' class=tdright>Wildcard bits:</th><td class=tdleft>";
2302 echo $wildcardbylen[$range['mask']];
2303 echo "</td></tr>\n";
2304
2305 $routers = findRouters ($range['addrlist']);
2306 if (getConfigVar ('EXT_IPV4_VIEW') == 'yes' and count ($routers))
2307 {
2308 echo "<tr><th width='50%' class=tdright>Routed by:</th>";
2309 printRoutersTD ($routers);
2310 echo "</tr>\n";
2311 }
2312
2313 printTagTRs ("${root}?page=ipv4space&");
2314 echo "</table><br>\n";
2315 finishPortlet();
2316 echo "</td>\n";
2317
2318 echo "<td class=pcright>";
2319 startPortlet ('details');
2320 $startip = $range['ip_bin'] & $range['mask_bin'];
2321 $endip = $range['ip_bin'] | $range['mask_bin_inv'];
2322 $realstartip = $startip;
2323 $realendip = $endip;
2324 $numpages = 0;
2325 if($endip - $startip > $maxperpage)
2326 {
2327 $numpages = ($endip - $startip)/$maxperpage;
2328 $startip = $startip + $page * $maxperpage;
2329 $endip = $startip + $maxperpage-1;
2330 }
2331 echo "<center>";
2332 if ($numpages)
2333 echo '<h3>' . long2ip ($startip) . ' ~ ' . long2ip ($endip) . '</h3>';
2334 for ($i=0; $i<$numpages; $i++)
2335 {
2336 if ($i == $page)
2337 echo "<b>$i</b> ";
2338 else
2339 echo "<a href='${root}?page=${pageno}&tab=${tabno}&id=$id&pg=$i'>$i</a> ";
2340 }
2341 echo "</center>";
2342
2343 echo "<table class='widetable' border=0 cellspacing=0 cellpadding=5 align='center' width='100%'>\n";
2344 echo "<tr><th>Address</th><th>Name</th><th>Allocation</th></tr>\n";
2345
2346
2347 for ($ip = $startip; $ip <= $endip; $ip++) :
2348 if (isset ($_REQUEST['hl_ipv4_addr']) and ip2long ($_REQUEST['hl_ipv4_addr']) == $ip)
2349 $secondstyle = 'tdleft port_highlight';
2350 else
2351 $secondstyle = 'tdleft';
2352 if (!isset ($range['addrlist'][$ip]))
2353 {
2354 echo "<tr><td class=tdleft><a href='${root}?page=ipaddress&ip=" . long2ip ($ip) . "'>" . long2ip ($ip);
2355 echo "</a></td><td class='${secondstyle}'>&nbsp;</td><td class='${secondstyle}'>&nbsp;</td></tr>\n";
2356 continue;
2357 }
2358 $addr = $range['addrlist'][$ip];
2359 echo "<tr class='${addr['class']}'>";
2360
2361 echo "<td class=tdleft><a href='${root}?page=ipaddress&ip=${addr['ip']}'>${addr['ip']}</a></td>";
2362 echo "<td class='${secondstyle}'>${addr['name']}</td><td class='${secondstyle}'>";
2363 $delim = '';
2364 $prologue = '';
2365 if ( $addr['reserved'] == 'yes')
2366 {
2367 echo "<strong>RESERVED</strong> ";
2368 $delim = '; ';
2369 }
2370 foreach ($range['addrlist'][$ip]['allocs'] as $ref)
2371 {
2372 echo $delim . $aac2[$ref['type']];
2373 echo "<a href='${root}?page=object&object_id=${ref['object_id']}";
2374 echo "&hl_ipv4_addr=${addr['ip']}'>";
2375 echo $ref['name'] . (empty ($ref['name']) ? '' : '@');
2376 echo "${ref['object_name']}</a>";
2377 $delim = '; ';
2378 }
2379 if ($delim != '')
2380 {
2381 $delim = '';
2382 $prologue = '<br>';
2383 }
2384 foreach ($range['addrlist'][$ip]['lblist'] as $ref)
2385 {
2386 echo $prologue;
2387 $prologue = '';
2388 echo "${delim}<a href='${root}?page=object&object_id=${ref['object_id']}'>";
2389 echo "${ref['object_name']}</a>:<a href='${root}?page=ipv4vs&vs_id=${ref['vs_id']}'>";
2390 echo "${ref['vport']}/${ref['proto']}</a>&rarr;";
2391 $delim = '; ';
2392 }
2393 if ($delim != '')
2394 {
2395 $delim = '';
2396 $prologue = '<br>';
2397 }
2398 foreach ($range['addrlist'][$ip]['rslist'] as $ref)
2399 {
2400 echo $prologue;
2401 $prologue = '';
2402 echo "${delim}&rarr;${ref['rsport']}@<a href='${root}?page=ipv4rsp&pool_id=${ref['rspool_id']}'>";
2403 echo "${ref['rspool_name']}</a>";
2404 $delim = '; ';
2405 }
2406 echo "</td></tr>\n";
2407 endfor;
2408 // end of iteration
2409
2410 echo "</table>";
2411 finishPortlet();
2412 echo "</td></tr></table>\n";
2413 }
2414
2415 function renderIPv4NetworkProperties ($id)
2416 {
2417 showMessageOrError();
2418 $netdata = getIPv4NetworkInfo ($id);
2419 echo "<center><h1>${netdata['ip']}/${netdata['mask']}</h1></center>\n";
2420 echo "<table border=0 cellpadding=10 cellpadding=1 align='center'>\n";
2421 printOpFormIntro ('editRange');
2422 echo "<tr><td class='tdright'>Name:</td><td class='tdleft'><input type=text name=name size=20 value='${netdata['name']}'></tr>";
2423 echo "<tr><td colspan=2 class=tdcenter>";
2424 printImageHREF ('SAVE', 'Save changes', TRUE);
2425 echo "</td></form></tr></table>\n";
2426 }
2427
2428 function renderIPv4Address ($dottedquad)
2429 {
2430 global $root, $aat;
2431 $address = getIPv4Address ($dottedquad);
2432 echo "<table border=0 class=objectview cellspacing=0 cellpadding=0>";
2433 echo "<tr><td colspan=2 align=center><h1>${dottedquad}</h1></td></tr>\n";
2434 if (!empty ($address['name']))
2435 echo "<tr><td colspan=2 align=center><h2>${address['name']}</h2></td></tr>\n";
2436
2437 echo "<tr><td class=pcleft>";
2438 startPortlet ('summary');
2439 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
2440 echo "<tr><th width='50%' class=tdright>Allocations:</th><td class=tdleft>" . count ($address['allocs']) . "</td></tr>\n";
2441 echo "<tr><th width='50%' class=tdright>Originated NAT connections:</th><td class=tdleft>" . count ($address['outpf']) . "</td></tr>\n";
2442 echo "<tr><th width='50%' class=tdright>Arriving NAT connections:</th><td class=tdleft>" . count ($address['inpf']) . "</td></tr>\n";
2443 echo "<tr><th width='50%' class=tdright>SLB virtual services:</th><td class=tdleft>" . count ($address['lblist']) . "</td></tr>\n";
2444 echo "<tr><th width='50%' class=tdright>SLB real servers:</th><td class=tdleft>" . count ($address['rslist']) . "</td></tr>\n";
2445 printTagTRs();
2446 echo "</table><br>\n";
2447 finishPortlet();
2448 echo "</td>\n";
2449
2450 echo "<td class=pcright>";
2451
2452 if (!empty ($address['class']))
2453 {
2454 startPortlet ('allocations');
2455 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center' width='100%'>\n";
2456 echo "<tr><th>object</th><th>OS interface</th><th>allocation type</th></tr>\n";
2457 $class = $address['class'];
2458 // render all allocation records for this address the same way
2459 if ($address['reserved'] == 'yes')
2460 echo "<tr class='${class}'><td colspan=2>&nbsp;</td><td class=tdleft><strong>RESERVED</strong></td></tr>";
2461 foreach ($address['allocs'] as $bond)
2462 {
2463 if (isset ($_REQUEST['hl_object_id']) and $_REQUEST['hl_object_id'] == $bond['object_id'])
2464 $secondclass = 'tdleft port_highlight';
2465 else
2466 $secondclass = 'tdleft';
2467 echo "<tr class='$class'><td class=tdleft><a href='${root}?page=object&object_id=${bond['object_id']}";
2468 echo "&hl_ipv4_addr=${dottedquad}'>${bond['object_name']}</td><td class='${secondclass}'>${bond['name']}</td><td class='${secondclass}'><strong>";
2469 echo $aat[$bond['type']];
2470 echo "</strong></td></tr>\n";
2471 }
2472 echo "</table><br><br>";
2473 finishPortlet();
2474 }
2475
2476 // FIXME: The returned list is structured differently, than we expect it to be. One of the sides
2477 // must be fixed.
2478 if (count ($address['lblist']))
2479 {
2480 startPortlet ('Virtual services (' . count ($address['lblist']) . ')');
2481 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center' width='100%'>\n";
2482 echo "<tr><th>VS</th><th>name</th></tr>\n";
2483 foreach ($address['lblist'] as $vsinfo)
2484 {
2485 echo "<tr><td class=tdleft><a href='${root}?page=ipv4vs&vs_id=${vsinfo['vs_id']}'>";
2486 echo buildVServiceName ($vsinfo) . "</a></td><td class=tdleft>";
2487 echo $vsinfo['name'] . "</td></tr>\n";
2488 }
2489 echo "</table><br><br>";
2490 finishPortlet();
2491 }
2492
2493 if (count ($address['rslist']))
2494 {
2495 startPortlet ('Real servers (' . count ($address['rslist']) . ')');
2496 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center' width='100%'>\n";
2497 echo "<tr><th>&nbsp;</th><th>port</th><th>RS pool</th></tr>\n";
2498 foreach ($address['rslist'] as $rsinfo)
2499 {
2500 echo "<tr><td>";
2501 if ($rsinfo['inservice'] == 'yes')
2502 printImageHREF ('inservice', 'in service');
2503 else
2504 printImageHREF ('notinservice', 'NOT in service');
2505 echo "</td><td class=tdleft>${rsinfo['rsport']}</td><td class=tdleft><a href='${root}?page=ipv4rsp&pool_id=${rsinfo['rspool_id']}'>";
2506 echo $rsinfo['rspool_name'] . "</a></td></tr>\n";
2507 }
2508 echo "</table><br><br>";
2509 finishPortlet();
2510 }
2511
2512 if (count ($address['outpf']))
2513 {
2514 startPortlet ('departing NAT rules');
2515 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center' width='100%'>\n";
2516 echo "<tr><th>proto</th><th>from</th><th>to</th><th>comment</th></tr>\n";
2517 foreach ($address['outpf'] as $rule)
2518 echo "<tr><td>${rule['proto']}</td><td>${rule['localip']}:${rule['localport']}</td><td>${rule['remoteip']}:${rule['remoteport']}</td><td>${rule['description']}</td></tr>";
2519 echo "</table>";
2520 finishPortlet();
2521 }
2522
2523 if (count ($address['inpf']))
2524 {
2525 startPortlet ('arriving NAT rules');
2526 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center' width='100%'>\n";
2527 echo "<tr><th>proto</th><th>from</th><th>to</th><th>comment</th></tr>\n";
2528 foreach ($address['inpf'] as $rule)
2529 echo "<tr><td>${rule['proto']}</td><td>${rule['localip']}:${rule['localport']}</td><td>${rule['remoteip']}:${rule['remoteport']}</td><td>${rule['description']}</td></tr>";
2530 echo "</table>";
2531 finishPortlet();
2532 }
2533
2534 echo "</td></tr>";
2535 echo "</table>\n";
2536 }
2537
2538 function renderIPv4AddressProperties ($dottedquad)
2539 {
2540 showMessageOrError();
2541 $address = getIPv4Address ($dottedquad);
2542 echo "<center><h1>$dottedquad</h1></center>\n";
2543
2544 startPortlet ('update');
2545 echo "<table border=0 cellpadding=10 cellpadding=1 align='center'>\n";
2546 printOpFormIntro ('editAddress');
2547 echo "<tr><td class='tdright'>Name:</td><td class='tdleft'><input type=text name=name size=20 value='${address['name']}'></tr>";
2548 echo "<td class='tdright'>Reserved:</td><td class='tdleft'><input type=checkbox name=reserved size=20 ";
2549 echo ($address['reserved']=='yes') ? 'checked' : '';
2550 echo "></tr><tr><td colspan=2 class='tdcenter'>";
2551 printImageHREF ('SAVE', 'Save changes', TRUE);
2552 echo "</td></form></tr></table>\n";
2553 finishPortlet();
2554 if (empty ($address['name']) and $address['reserved'] == 'no')
2555 return;
2556
2557 startPortlet ('release');
2558 printOpFormIntro ('editAddress', array ('name' => '', 'reserved' => ''));
2559 echo "<input type=submit value='release'></form>";
2560 finishPortlet();
2561 }
2562
2563 function renderIPv4AddressAllocations ($dottedquad)
2564 {
2565 showMessageOrError();
2566 global $pageno, $tabno, $root, $aat;
2567
2568 $address = getIPv4Address ($dottedquad);
2569 $class = $address['class'];
2570 echo "<center><h1>${dottedquad}</h1></center>\n";
2571 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center'>\n";
2572 echo "<tr><th>&nbsp;</th><th>object</th><th>OS interface</th><th>allocation type</th><th>&nbsp;</th></tr>\n";
2573
2574 if ($address['reserved'] == 'yes')
2575 echo "<tr class='${class}'><td colspan=3>&nbsp;</td><td class=tdleft><strong>RESERVED</strong></td><td>&nbsp;</td></tr>";
2576 foreach ($address['allocs'] as $bond)
2577 {
2578 echo "<tr class='$class'>";
2579 printOpFormIntro ('updIPv4Allocation', array ('object_id' => $bond['object_id']));
2580 echo "<td><a href='${root}process.php?op=delIPv4Allocation&page=${pageno}&tab=${tabno}&ip=${dottedquad}&object_id=${bond['object_id']}'>";
2581 printImageHREF ('delete', 'Unallocate address');
2582 echo "</a></td>";
2583 echo "<td><a href='${root}?page=object&object_id=${bond['object_id']}&hl_ipv4_addr=${dottedquad}'>${bond['object_name']}</td>";
2584 echo "<td><input type='text' name='bond_name' value='${bond['name']}' size=10></td><td>";
2585 printSelect ($aat, 'bond_type', $bond['type']);
2586 echo "</td><td>";
2587 printImageHREF ('save', 'Save changes', TRUE);
2588 echo "</td></form></tr>\n";
2589 }
2590 printOpFormIntro ('addIPv4Allocation');
2591 echo "<td>";
2592 printImageHREF ('add', 'new allocation', TRUE);
2593 echo "</td><td><select name='object_id'>";
2594
2595 foreach (explode (',', getConfigVar ('IPV4_PERFORMERS')) as $type)
2596 foreach (getNarrowObjectList ($type) as $object)
2597 echo "<option value='${object['id']}'>${object['dname']}</option>";
2598
2599 echo "</select></td><td><input type='text' name='bond_name' value='' size=10></td><td>";
2600 printSelect ($aat, 'bond_type');
2601 echo "</td><td>&nbsp;</td></form></tr>";
2602 echo "</table><br><br>";
2603
2604 }
2605
2606 function renderNATv4ForObject ($object_id = 0)
2607 {
2608 global $pageno, $tabno, $root;
2609
2610 $info = getObjectInfo ($object_id);
2611 $forwards = getNATv4ForObject ($object_id);
2612 $alloclist = getObjectIPv4Allocations ($object_id);
2613 showMessageOrError();
2614 echo "<center><h2>locally performed NAT</h2></center>";
2615
2616 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center'>\n";
2617 echo "<tr><th></th><th>Match endpoint</th><th>Translate to</th><th>Target object</th><th>Comment</th><th>&nbsp;</th></tr>\n";
2618
2619 foreach ($forwards['out'] as $pf)
2620 {
2621 $class = 'trerror';
2622 $osif = '';
2623 if (isset ($alloclist [$pf['localip']]))
2624 {
2625 $class = $alloclist [$pf['localip']]['addrinfo']['class'];
2626 $osif = $alloclist [$pf['localip']]['osif'] . ': ';
2627 }
2628
2629 echo "<tr class='$class'>";
2630 echo "<td><a href='${root}process.php?op=delNATv4Rule&localip=${pf['localip']}&localport=${pf['localport']}&remoteip=${pf['remoteip']}&remoteport=${pf['remoteport']}&proto=${pf['proto']}&object_id=$object_id&page=${pageno}&tab=${tabno}'>";
2631 printImageHREF ('delete', 'Delete NAT rule');
2632 echo "</a></td>";
2633 echo "<td>${pf['proto']}/${osif}<a href='${root}?page=ipaddress&tab=default&ip=${pf['localip']}'>${pf['localip']}</a>:${pf['localport']}";
2634 if (!empty ($pf['local_addr_name']))
2635 echo ' (' . $pf['local_addr_name'] . ')';
2636 echo "</td>";
2637 echo "<td><a href='${root}?page=ipaddress&tab=default&ip=${pf['remoteip']}'>${pf['remoteip']}</a>:${pf['remoteport']}</td>";
2638
2639 $address = getIPv4Address ($pf['remoteip']);
2640
2641 echo "<td class='description'>";
2642 if (count ($address['allocs']))
2643 foreach ($address['allocs'] as $bond)
2644 echo "<a href='${root}?page=object&tab=default&object_id=${bond['object_id']}'>${bond['object_name']}(${bond['name']})</a> ";
2645 elseif (!empty ($pf['remote_addr_name']))
2646 echo '(' . $pf['remote_addr_name'] . ')';
2647 printOpFormIntro
2648 (
2649 'updNATv4Rule',
2650 array
2651 (
2652 'localip' => $pf['localip'],
2653 'localport' => $pf['localport'],
2654 'remoteip' => $pf['remoteip'],
2655 'remoteport' => $pf['remoteport'],
2656 'proto' => $pf['proto']
2657 )
2658 );
2659 echo "</td><td class='description'>";
2660 echo "<input type='text' name='description' value='${pf['description']}'></td><td>";
2661 printImageHREF ('save', 'Save changes', TRUE);
2662 echo "</td></form></tr>";
2663 }
2664 printOpFormIntro ('addNATv4Rule');
2665 echo "<tr align='center'><td>";
2666 printImageHREF ('add', 'Add new NAT rule', TRUE);
2667 echo '</td><td>';
2668 printSelect (array ('TCP' => 'TCP', 'UDP' => 'UDP'), 'proto');
2669 echo "<select name='localip' tabindex=1>";
2670
2671 foreach ($alloclist as $dottedquad => $alloc)
2672 {
2673 $name = empty ($alloc['addrinfo']['name']) ? '' : (' (' . niftyString ($alloc['addrinfo']['name']) . ')');
2674 $osif = empty ($alloc['osif']) ? '' : ($alloc['osif'] . ': ');
2675 echo "<option value='${dottedquad}'>${osif}${dottedquad}${name}</option>";
2676 }
2677
2678 echo "</select>:<input type='text' name='localport' size='4' tabindex=2></td>";
2679 echo "<td><input type='text' name='remoteip' id='remoteip' size='10' tabindex=3>";
2680 echo "<a href='javascript:;' onclick='window.open(\"${root}/find_object_ip_helper.php\", \"findobjectip\", \"height=700, width=400, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, titlebar=no, toolbar=no\");'>";
2681 printImageHREF ('find', 'Find object');
2682 echo "</a>";
2683 echo ":<input type='text' name='remoteport' size='4' tabindex=4></td><td></td>";
2684 echo "<td colspan=1><input type='text' name='description' size='20' tabindex=5></td><td>&nbsp;</td></tr>";
2685 echo "</form>";
2686
2687 echo "</table><br><br>";
2688
2689 echo "<center><h2>arriving NAT connections</h2></center>";
2690 echo "<table class='widetable' cellpadding=5 cellspacing=0 border=0 align='center'>\n";
2691 echo "<tr><th></th><th>Source</th><th>Source objects</th><th>Target</th><th>Description</th></tr>\n";
2692
2693 foreach ($forwards['in'] as $pf)
2694 {
2695 echo "<tr><td><a href='${root}process.php?op=delNATv4Rule&localip=${pf['localip']}&localport=${pf['localport']}&remoteip=${pf['remoteip']}&remoteport=${pf['remoteport']}&proto=${pf['proto']}&object_id=${pf['object_id']}&page=${pageno}&tab=${tabno}'>";
2696 printImageHREF ('delete', 'Delete NAT rule');
2697 echo "</a></td>";
2698 echo "<td>${pf['proto']}/<a href='${root}?page=ipaddress&tab=default&ip=${pf['localip']}'>${pf['localip']}</a>:${pf['localport']}</td>";
2699 echo "<td class='description'><a href='${root}?page=object&tab=default&object_id=${pf['object_id']}'>${pf['object_name']}</a>";
2700 echo "</td><td><a href='${root}?page=ipaddress&tab=default&ip=${pf['remoteip']}'>${pf['remoteip']}</a>:${pf['remoteport']}</td>";
2701 echo "<td class='description'>${pf['description']}</td></tr>";
2702 }
2703
2704 echo "</table><br><br>";
2705 }
2706
2707 // FIXME: move related code away into ophandler(s)
2708 function renderAddMultipleObjectsForm ()
2709 {
2710 global $root, $pageno, $tabno, $nextorder;
2711
2712 $type_id = array();
2713 $global_type_id = 0;
2714 $name = array();
2715 $asset_no = array();
2716 $keepvalues1 = $keepvalues2 = FALSE;
2717 $log = array();
2718 // Look for current submit.
2719 if (isset ($_REQUEST['got_fast_data']))
2720 {
2721 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
2722 $keepvalues1 = TRUE;
2723 $max = getConfigVar ('MASSCOUNT');
2724 for ($i = 0; $i < $max; $i++)
2725 {
2726 if (!isset ($_REQUEST["${i}_object_type_id"]))
2727 {
2728 $log[] = array ('code' => 'error', 'message' => "Submitted form is invalid at line " . $i + 1);
2729 break;
2730 }
2731 assertUIntArg ("${i}_object_type_id", __FUNCTION__, TRUE);
2732 assertStringArg ("${i}_object_name", __FUNCTION__, TRUE);
2733 assertStringArg ("${i}_object_label", __FUNCTION__, TRUE);
2734 assertStringArg ("${i}_object_asset_no", __FUNCTION__, TRUE);
2735 assertStringArg ("${i}_object_barcode", __FUNCTION__, TRUE);
2736 $type_id[$i] = $_REQUEST["${i}_object_type_id"];
2737 // Save user input for possible rendering.
2738 $name[$i] = $_REQUEST["${i}_object_name"];
2739 $label[$i] = $_REQUEST["${i}_object_label"];
2740 $asset_no[$i] = $_REQUEST["${i}_object_asset_no"];
2741 $barcode[$i] = $_REQUEST["${i}_object_barcode"];
2742
2743 // It's better to skip silently, than to print a notice.
2744 if ($type_id[$i] == 0)
2745 continue;
2746 if (commitAddObject ($name[$i], $label[$i], $barcode[$i], $type_id[$i], $asset_no[$i], $taglist) === TRUE)
2747 $log[] = array ('code' => 'success', 'message' => "Added new object '${name[$i]}'");
2748 else
2749 $log[] = array ('code' => 'error', 'message' => __FUNCTION__ . ': commitAddObject() failed');
2750 }
2751 }
2752 elseif (isset ($_REQUEST['got_very_fast_data']))
2753 {
2754 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
2755 $keepvalues2 = TRUE;
2756 assertUIntArg ('global_type_id', __FUNCTION__, TRUE);
2757 assertStringArg ('namelist', __FUNCTION__, TRUE);
2758 $global_type_id = $_REQUEST['global_type_id'];
2759 if ($global_type_id == 0)
2760 {
2761 if (!empty ($_REQUEST['namelist']))
2762 $log[] = array ('code' => 'error', 'message' => 'Object type is not selected, check the form below');
2763 else
2764 $log[] = array ('code' => 'error', 'message' => 'Empty form has been ignored. Cheers.');
2765 }
2766 else
2767 {
2768 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
2769 $names1 = explode ('\n', $_REQUEST['namelist']);
2770 $names2 = array();
2771 foreach ($names1 as $line)
2772 {
2773 $parts = explode ('\r', $line);
2774 reset ($parts);
2775 if (empty ($parts[0]))
2776 continue;
2777 else
2778 $names2[] = rtrim ($parts[0]);
2779 }
2780 foreach ($names2 as $cname)
2781 if (commitAddObject ($cname, '', '', $global_type_id, '', $taglist) === TRUE)
2782 $log[] = array ('code' => 'success', 'message' => "Added new object '${cname}'");
2783 else
2784 $log[] = array ('code' => 'error', 'message' => "Could not add '${cname}'");
2785 }
2786 }
2787 printLog ($log);
2788
2789 // Render a form for the next.
2790 $typelist = getObjectTypeList();
2791 $typelist[0] = 'select type...';
2792
2793 startPortlet ('Distinct types, same tags');
2794 $max = getConfigVar ('MASSCOUNT');
2795 echo "<form name=fastform method=post action='${root}?page=${pageno}&tab=${tabno}'>";
2796 echo '<table border=0 align=center>';
2797 echo "<tr><th>Object type</th><th>Common name</th><th>Visible label</th>";
2798 echo "<th>Asset tag</th><th>Barcode</th><th>Tags</th></tr>\n";
2799 // If a user forgot to select object type on input, we keep his
2800 // previous input in the form.
2801 for ($i = 0; $i < $max; $i++)
2802 {
2803 echo '<tr><td>';
2804 // Don't employ DEFAULT_OBJECT_TYPE to avoid creating ghost records for pre-selected empty rows.
2805 printSelect ($typelist, "${i}_object_type_id", 0);
2806 echo '</td>';
2807 echo "<td><input type=text size=30 name=${i}_object_name";
2808 if ($keepvalues1 and isset ($name[$i]) and (!isset ($type_id[$i]) or $type_id[$i] == 0))
2809 echo " value='${name[$i]}'";
2810 echo "></td>";
2811 echo "<td><input type=text size=30 name=${i}_object_label";
2812 if ($keepvalues1 and isset ($label[$i]) and (!isset ($type_id[$i]) or $type_id[$i] == 0))
2813 echo " value='${label[$i]}'";
2814 echo "></td>";
2815 echo "<td><input type=text size=20 name=${i}_object_asset_no";
2816 if ($keepvalues1 and isset ($asset_no[$i]) and (!isset ($type_id[$i]) or $type_id[$i] == 0))
2817 echo " value='${asset_no[$i]}'";
2818 echo "></td>";
2819 echo "<td><input type=text size=10 name=${i}_object_barcode";
2820 if ($keepvalues1 and isset ($barcode[$i]) and (!isset ($type_id[$i]) or $type_id[$i] == 0))
2821 echo " value='${barcode[$i]}'";
2822 echo "></td>";
2823 if ($i == 0)
2824 {
2825 echo "<td valign=top rowspan=${max}>";
2826 renderTagSelect();
2827 echo "</td>\n";
2828 }
2829 echo "</tr>\n";
2830 }
2831 echo "<tr><td class=submit colspan=5><input type=submit name=got_fast_data value='Go!'></td></tr>\n";
2832 echo "</form></table>\n";
2833 finishPortlet();
2834
2835 startPortlet ('Same type, same tags');
2836 echo "<form name=veryfastform method=post action='${root}?page=${pageno}&tab=${tabno}'>";
2837 echo "<table border=0 align=center><tr><th>names</th><th>type</th></tr>";
2838 echo "<tr><td rowspan=3><textarea name=namelist cols=40 rows=25>\n";
2839 if ($keepvalues2 and $global_type_id == 0)
2840 echo $_REQUEST['namelist'];
2841 echo "</textarea></td><td valign=top>";
2842 printSelect ($typelist, "global_type_id", getConfigVar ('DEFAULT_OBJECT_TYPE'));
2843 echo "</td></tr>";
2844 echo "<tr><th>Tags</th></tr>";
2845 echo "<tr><td valign=top>";
2846 renderTagSelect();
2847 echo "</td></tr>";
2848 echo "<tr><td colspan=2><input type=submit name=got_very_fast_data value='Go!'></td></tr></table>\n";
2849 echo "</form>\n";
2850 finishPortlet();
2851 }
2852
2853 function printGreeting ()
2854 {
2855 global $remote_username, $accounts, $root;
2856 $account = $accounts[$remote_username];
2857 $person = empty ($account['user_realname']) ? $account['user_name'] : $account['user_realname'];
2858 echo "Hello, ${person}. This is RackTables " . CODE_VERSION . ". Click <a href='${root}?logout'>here</a> to logout.";
2859 }
2860
2861 function renderSearchResults ()
2862 {
2863 global $remote_username, $root;
2864 $terms = trim ($_REQUEST['q']);
2865 if (empty ($terms))
2866 {
2867 showError ('Search string cannot be empty.', __FUNCTION__);
2868 return;
2869 }
2870 if (!permitted ('objects', 'default'))
2871 {
2872 showError ('You are not authorized for viewing information about objects.', __FUNCTION__);
2873 return;
2874 }
2875 $nhits = 0;
2876 // If we search for L2 address, we can either find one or find none.
2877 if
2878 (
2879 preg_match ('/^[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?$/i', $terms) or
2880 preg_match ('/^[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i', $terms) or
2881 preg_match ('/^[0-9a-f][0-9a-f][0-9a-f][0-9a-f].[0-9a-f][0-9a-f][0-9a-f][0-9a-f].[0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i', $terms) or
2882 // STP bridge ID: bridge priotity + port MAC address. Cut off first 4 chars and look for MAC address.
2883 preg_match ('/^[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i', $terms)
2884 )
2885 // Search for L2 address.
2886 {
2887 $terms = str_replace ('.', '', $terms);
2888 $terms = str_replace (':', '', $terms);
2889 $terms = substr ($terms, -12);
2890 $result = searchByl2address ($terms);
2891 if ($result !== NULL)
2892 {
2893 $nhits++;
2894 $lasthit = 'port';
2895 $summary['port'][] = $result;
2896 }
2897 }
2898 elseif (preg_match ('/^[0-9][0-9]?[0-9]?\.[0-9]?[0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9]?[0-9]?[0-9]?$/i', $terms))
2899 // Search for IP address.
2900 {
2901 if (NULL !== getIPv4AddressNetworkId ($terms))
2902 {
2903 $nhits++;
2904 $lasthit = 'ipv4addressbydq';
2905 $summary['ipv4addressbydq'][] = $terms;
2906 }
2907 }
2908 else
2909 // Search for objects, addresses, networks, virtual services and RS pools by their description.
2910 {
2911 $tmp = getObjectSearchResults ($terms);
2912 if (count ($tmp))
2913 {
2914 $nhits += count ($tmp);
2915 $lasthit = 'object';
2916 $summary['object'] = $tmp;
2917 }
2918 $tmp = getIPv4AddressSearchResult ($terms);
2919 if (count ($tmp))
2920 {
2921 $nhits += count ($tmp);
2922 $lasthit = 'ipv4addressbydescr';
2923 $summary['ipv4addressbydescr'] = $tmp;
2924 }
2925 $tmp = getIPv4PrefixSearchResult ($terms);
2926 if (count ($tmp))
2927 {
2928 $nhits += count ($tmp);
2929 $lasthit = 'ipv4network';
2930 $summary['ipv4network'] = $tmp;
2931 }
2932 $tmp = getIPv4RSPoolSearchResult ($terms);
2933 if (count ($tmp))
2934 {
2935 $nhits += count ($tmp);
2936 $lasthit = 'ipv4rspool';
2937 $summary['ipv4rspool'] = $tmp;
2938 }
2939 $tmp = getIPv4VServiceSearchResult ($terms);
2940 if (count ($tmp))
2941 {
2942 $nhits += count ($tmp);
2943 $lasthit = 'ipv4vs';
2944 $summary['ipv4vs'] = $tmp;
2945 }
2946 $tmp = getAccountSearchResult ($terms);
2947 if (count ($tmp))
2948 {
2949 $nhits += count ($tmp);
2950 $lasthit = 'user';
2951 $summary['user'] = $tmp;
2952 }
2953 }
2954 if ($nhits == 0)
2955 echo "<center><h2>Nothing found for '${terms}'</h2></center>";
2956 elseif ($nhits == 1)
2957 {
2958 $record = current ($summary[$lasthit]);
2959 switch ($lasthit)
2960 {
2961 case 'port':
2962 echo "<script language='Javascript'>document.location='${root}?page=object";
2963 echo "&hl_port_id=" . $record['port_id'];
2964 echo "&object_id=" . $record['object_id'] . "';//</script>";
2965 break;
2966 case 'ipv4addressbydq':
2967 $parentnet = getIPv4AddressNetworkId ($record);
2968 if ($parentnet !== NULL)
2969 echo "<script language='Javascript'>document.location='${root}?page=iprange&tab=default&id=${parentnet}&hl_ipv4_addr=${record}';//</script>";
2970 else
2971 echo "<script language='Javascript'>document.location='${root}?page=ipaddress&ip=${record}';//</script>";
2972 break;
2973 case 'ipv4addressbydescr':
2974 $parentnet = getIPv4AddressNetworkId ($record['ip']);
2975 if ($parentnet !== NULL)
2976 echo "<script language='Javascript'>document.location='${root}?page=iprange&tab=default&id=${parentnet}&hl_ipv4_addr=${record['ip']}';//</script>";
2977 else
2978 echo "<script language='Javascript'>document.location='${root}?page=ipaddress&ip=${record['ip']}';//</script>";
2979 break;
2980 case 'ipv4network':
2981 echo "<script language='Javascript'>document.location='${root}?page=iprange";
2982 echo "&id=${record['id']}";
2983 echo "';//</script>";
2984 break;
2985 case 'object':
2986 echo "<script language='Javascript'>document.location='${root}?page=object&object_id=${record['id']}';//</script>";
2987 break;
2988 case 'ipv4rspool':
2989 echo "<script language='Javascript'>document.location='${root}?page=ipv4rsp&pool_id=${record['pool_id']}';//</script>";
2990 break;
2991 case 'ipv4vs':
2992 echo "<script language='Javascript'>document.location='${root}?page=ipv4vs&vs_id=${record['id']}';//</script>";
2993 break;
2994 case 'user':
2995 echo "<script language='Javascript'>document.location='${root}?page=user&user_id=${record['user_id']}';//</script>";
2996 break;
2997 }
2998 return;
2999 }
3000 else
3001 {
3002 global $nextorder;
3003 $order = 'odd';
3004 echo "<center><h2>${nhits} result(s) found for '${terms}'</h2></center>";
3005 foreach ($summary as $where => $what)
3006 switch ($where)
3007 {
3008 case 'object':
3009 startPortlet ("<a href='${root}?page=objects'>Objects</a>");