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