new-ip-range: spare IP range search page
[racktables-contribs] / new-ip-range.php
CommitLineData
0223607c
AA
1<?php
2
3$tab['ipv4space']['newiprange'] = 'Spare search';
4registerTabHandler ('ipv4space', 'newiprange', 'renderSearchNewIP4Range');
5
6/*
7SPARE_SEARCH_PREDICATE is a predicate name describing aggregates to search in.
8It is useful to define a predicate like '[RIPE allocation]' to search only there.
9Define this constant in your secret.php like this:
10
11define ('SPARE_SEARCH_PREDICATE', 'RIPE allocation');
12*/
13
14function renderSearchNewIP4Range()
15{
16 global $pTable;
17
18 // prepare $cellfilter
19 $cellfilter = getCellFilter();
20 if ($cellfilter['is_empty'] || ! isset ($_REQUEST['cfp']))
21 if (defined ('SPARE_SEARCH_PREDICATE') && isset ($pTable[SPARE_SEARCH_PREDICATE]))
22 {
23 $_REQUEST['cfp'] = array (SPARE_SEARCH_PREDICATE);
24 $cellfilter = getCellFilter();
25 }
26 $mask = NULL;
27 if (! empty ($_REQUEST['pref_len']))
28 $mask = intval($_REQUEST['pref_len']);
29
30 $nets = array();
31 foreach (filterCellList (listCells ('ipv4net'), $cellfilter['expression']) as $net)
32 {
33 if (! isset ($mask))
34 $nets[] = $net;
35 elseif ($net['mask'] <= $mask)
36 {
37 $is_aggregate = FALSE;
38 foreach ($net['atags'] as $atag)
39 if ($atag['tag'] == '$aggregate')
40 $is_aggregate = TRUE;
41 elseif (preg_match ('/^\$spare_(\d+)$/', $atag['tag'], $m) && $mask >= $m[1])
42 {
43 $nets[] = $net;
44 continue 2;
45 }
46 if (! $is_aggregate)
47 $nets[] = $net;
48 }
49 }
50 $filter = getOutputOf ('renderCellFilterPortlet', $cellfilter, 'ipv4net', $nets);
51
52 echo '<table width="100%"><tr valign=top>';
53 echo '<td>';
54 startPortlet ("Results (" . count ($nets) . ")");
55 echo '<ul class="spare-nets">';
56 foreach ($nets as $net)
57 {
58 echo '<li>';
59 renderNetCellForAlloc ($net, $mask);
60 echo '</li>';
61 }
62 echo '</ul>';
63 finishPortlet();
64 echo '</td>';
65
66 echo '<td width="33%">';
67 echo preg_replace_callback ('/(<form[^<>]*>)/', 'generatePrefixLengthInput', $filter);
68 echo '</td>';
69
70 echo '</tr></table>';
71
72 addCSS(<<<END
73ul.spare-nets {
74 list-style: none;
75 padding: 0px;
76}
77ul.spare-nets li {
78 margin: 5px 0px;
79}
80
81END
82 , TRUE);
83}
84
85function renderNetCellForAlloc ($cell, $needed_mask = NULL)
86{
87 if (empty ($cell['spare_ranges']) and $cell['kidc'] == 0)
88 {
89 $cell['spare_ranges'][$cell['mask'] + 1][] = $cell['ip_bin'];
90 $cell['spare_ranges'][$cell['mask'] + 1][] = (ip_last ($cell) & ip4_mask ($cell['mask'] + 1));
91 }
92 $ranges = array_keys ($cell['spare_ranges']);
93 sort ($ranges, SORT_NUMERIC);
94 foreach ($ranges as &$range)
95 {
96 $suffix = (count ($cell['spare_ranges'][$range]) <= 1) ? '' : '<small> x ' . count ($cell['spare_ranges'][$range]) . '</small>';
97 $range = '<a href="' .
98 makeHref (array
99 (
100 'page' => 'ipv4space',
101 'tab' => 'newrange',
102 'set-prefix' => ip_format ($cell['spare_ranges'][$range][0]) . '/' . $range,
103 )) .
104 '">/' . $range . '</a>' . $suffix;
105 }
106
107 $spare_cidr = NULL;
108 if (isset ($needed_mask))
109 for ($i = $needed_mask; $i > 0; $i--)
110 if (! empty ($cell['spare_ranges'][$i]))
111 {
112 $spare_cidr = ip_format ($cell['spare_ranges'][$i][0]) . '/' . $needed_mask;
113 break;
114 }
115
116 echo "<table class='slbcell vscell'><tr><td rowspan=3 width='5%'>";
117 printImageHREF ('NET');
118 echo '</td>';
119 echo "<td><a href='index.php?page={$cell['realm']}&id=${cell['id']}'>${cell['ip']}/${cell['mask']}</a>";
120 echo getRenderedIPNetCapacity ($cell);
121 echo '</td></tr>';
122
123 echo "<tr><td>";
124 if (strlen ($cell['name']))
125 echo "<strong>" . niftyString ($cell['name']) . "</strong>";
126 else
127 echo "<span class=sparenetwork>no name</span>";
128 // render VLAN
129 echo '<div class="vlan">' . implode(', ', $ranges) . '</div>';
130 renderNetVLAN ($cell);
131 echo "</td></tr>";
132 echo '<tr><td>';
133 echo count ($cell['etags']) ? ("<small>" . serializeTags ($cell['etags']) . "</small>") : '&nbsp;';
134 if (isset ($spare_cidr))
135 echo "<div class='vlan'><a href=\"" . makeHref (array ('page' => 'ipv4space', 'tab' => 'newrange', 'set-prefix' => $spare_cidr)) . "\">Allocate /$needed_mask</a></div>";
136 echo "</td></tr></table>";
137}
138
139function generatePrefixLengthInput($m)
140{
141 static $count = 0;
142 if (++$count > 1)
143 return $m[1] . '<input type="hidden" name="pref_len" value="">';
144 else
145 return $m[1] . '<label>Prefix length:<br><input type="text" name="pref_len" value="' . htmlspecialchars (@$_REQUEST['pref_len'], ENT_QUOTES) . '"></label><p>';
146}
147
148?>