r4178 bugfix: PHP warnings were appearing before rackspace on SLB object page, defaul...
[racktables] / popup.php
1 <?php
2 ob_start();
3 try {
4
5 require 'inc/interface.php';
6 require 'inc/init.php';
7
8 // Return a list of rack IDs, which are P or less positions
9 // far from the given rack in its row.
10 function getProximateRacks ($rack_id, $proximity = 0)
11 {
12 $rack = spotEntity ('rack', $rack_id);
13 $rackList = listCells ('rack', $rack['row_id']);
14 doubleLink ($rackList);
15 $ret = array ($rack_id);
16 $todo = $proximity;
17 $cur_item = $rackList[$rack_id];
18 while ($todo and array_key_exists ('prev_key', $cur_item))
19 {
20 $cur_item = $rackList[$cur_item['prev_key']];
21 $ret[] = $cur_item['id'];
22 $todo--;
23 }
24 $todo = $proximity;
25 $cur_item = $rackList[$rack_id];
26 while ($todo and array_key_exists ('next_key', $cur_item))
27 {
28 $cur_item = $rackList[$cur_item['next_key']];
29 $ret[] = $cur_item['id'];
30 $todo--;
31 }
32 return $ret;
33 }
34
35 function findSparePorts ($port_id, $only_racks = array())
36 {
37 $qparams = array ($port_id, $port_id);
38 $query = "SELECT id, object_id, name FROM Port WHERE " .
39 "id <> ? " .
40 "AND type IN (SELECT type2 FROM PortCompat WHERE type1 = (SELECT type FROM Port WHERE id = ?)) " .
41 "AND reservation_comment IS NULL " .
42 "AND id NOT IN (SELECT porta FROM Link) " .
43 "AND id NOT IN (SELECT portb FROM Link) ";
44 if (count ($only_racks))
45 {
46 $query .= 'AND object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
47 questionMarks (count ($only_racks)) . '))';
48 $qparams = array_merge ($qparams, $only_racks);
49 }
50 $query .= ' ORDER BY object_id, name';
51 $result = usePreparedSelectBlade ($query, $qparams);
52 // avoid nested queries
53 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
54 unset ($result);
55 $ret = array();
56 foreach (array_keys ($rows) as $rkey)
57 {
58 $object = spotEntity ('object', $rows[$rkey]['object_id']);
59 $ret[$rows[$rkey]['id']] = $object['dname'] . ' ' . $rows[$rkey]['name'];
60 unset ($rows[$rkey]);
61 }
62 natsort($ret);
63 return $ret;
64 }
65
66 // Return a list of all objects which are possible parents
67 // Special case for VMs and VM Virtual Switches
68 // - only select Servers with the Hypervisor attribute set to Yes
69 function findRackObjectParentCandidates ($object_id)
70 {
71 $object = spotEntity ('object', $object_id);
72 $args = array ($object['objtype_id'], $object_id, $object_id);
73
74 $query = "SELECT RO.id, RO.name FROM RackObject RO ";
75 $query .= "LEFT JOIN ObjectParentCompat OPC ON RO.objtype_id = OPC.parent_objtype_id ";
76 $query .= "WHERE OPC.child_objtype_id = ? ";
77 $query .= "AND RO.id != ? ";
78 // exclude existing parents
79 $query .= "AND RO.id NOT IN (SELECT parent_entity_id FROM EntityLink WHERE parent_entity_type = 'object' AND child_entity_type = 'object' AND child_entity_id = ?) ";
80 if ($object['objtype_id'] == 1504 || $object['objtype_id'] == 1507)
81 {
82 array_push($args, $object['objtype_id'], $object_id, $object_id);
83 $query .= "AND OPC.parent_objtype_id != 4 ";
84 $query .= "UNION ";
85 $query .= "SELECT RO.id, RO.name FROM RackObject RO ";
86 $query .= "LEFT JOIN ObjectParentCompat OPC ON RO.objtype_id = OPC.parent_objtype_id ";
87 $query .= "LEFT JOIN AttributeValue AV ON RO.id = AV.object_id ";
88 $query .= "WHERE OPC.child_objtype_id = ? ";
89 $query .= "AND (RO.objtype_id = 4 AND AV.attr_id = 26 AND AV.uint_value = 1501) ";
90 $query .= "AND RO.id != ? ";
91 // exclude existing parents
92 $query .= "AND RO.id NOT IN (SELECT parent_entity_id FROM EntityLink WHERE parent_entity_type = 'object' AND child_entity_type = 'object' AND child_entity_id = ?) ";
93 }
94 $query .= "ORDER BY 2";
95
96 $result = usePreparedSelectBlade ($query, $args);
97 $ret = array();
98 while ($row = $result->fetch (PDO::FETCH_ASSOC))
99 $ret[$row['id']] = $row['name'];
100 return $ret;
101 }
102
103 function sortObjectAddressesAndNames ($a, $b)
104 {
105 $objname_cmp = sortTokenize($a['object_name'], $b['object_name']);
106 if ($objname_cmp == 0)
107 {
108 $name_a = (isset ($a['port_name'])) ? $a['port_name'] : '';
109 $name_b = (isset ($b['port_name'])) ? $b['port_name'] : '';
110 $objname_cmp = sortTokenize($name_a, $name_b);
111 if ($objname_cmp == 0)
112 sortTokenize($a['ip'], $b['ip']);
113 return $objname_cmp;
114 }
115 return $objname_cmp;
116 }
117
118 header ('Content-Type: text/html; charset=UTF-8');
119 ?>
120 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
121 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
122 <head>
123 <?php
124 echo "<title>RackTables pop-up</title>\n";
125 printPageHeaders();
126 echo '</head><body style="height: 100%;">';
127 assertStringArg ('helper');
128 switch ($_REQUEST['helper'])
129 {
130 case 'objlist':
131 $pageno = 'object';
132 $tabno = 'default';
133 fixContext();
134 if (!permitted())
135 renderAccessDenied();
136 assertUIntArg ('object_id');
137 $object_id = $_REQUEST['object_id'];
138 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
139 echo '<h2>Choose a container:</h2>';
140 echo '<form action="javascript:;">';
141 $parents = findRackObjectParentCandidates($object_id);
142 printSelect ($parents, array ('name' => 'parents', 'size' => getConfigVar ('MAXSELSIZE')));
143 echo '<br>';
144 echo "<input type=submit value='Proceed' onclick='".
145 "if (getElementById(\"parents\").value != \"\") {".
146 " opener.location=\"process.php?page=object&tab=edit&op=linkEntities&object_id=${object_id}&child_entity_type=object&child_entity_id=${object_id}&parent_entity_type=object&parent_entity_id=\"+getElementById(\"parents\").value; ".
147 " window.close();}'>";
148 echo '</form></div>';
149 break;
150 case 'portlist':
151 $pageno = 'depot';
152 $tabno = 'default';
153 fixContext();
154 if (!permitted())
155 renderAccessDenied();
156 assertUIntArg ('port');
157 assertStringArg ('in_rack');
158 $localchoice = $_REQUEST['in_rack'] == 'y';
159 $port_id = $_REQUEST['port'];
160 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;"><h2>';
161 echo $localchoice ?
162 ('Nearest spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=n">show all</a>)') :
163 ('All spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=y">show nearest</a>)');
164 echo '</h2><form action="javascript:;" id="portform">';
165 $only_racks = array();
166 global $sic;
167 $port_info = getPortInfo ($sic['port']);
168 if ($_REQUEST['in_rack'] == 'y' and $port_info['object_id'])
169 {
170 $object = spotEntity ('object', $port_info['object_id']);
171 if ($object['rack_id'])
172 $only_racks = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
173 }
174 $spare_ports = findSparePorts ($port_id, $only_racks);
175
176 printSelect ($spare_ports, array ('name' => 'ports', 'size' => getConfigVar ('MAXSELSIZE')));
177 echo '<br>';
178 echo "Cable ID: <input type=text id=cable>";
179 echo '<br>';
180 echo "<input type='submit' value='Link' onclick='".
181 "if (getElementById(\"ports\").value != \"\") {".
182 " opener.location=\"process.php?page=object&tab=ports&op=linkPort&object_id=${port_info['object_id']}&port_id=$port_id&remote_port_id=\"+getElementById(\"ports\").value+\"&cable=\"+getElementById(\"cable\").value; ".
183 " window.close();}'>";
184 echo '</form></div>';
185 break;
186 case 'inet4list':
187 $pageno = 'ipv4space';
188 $tabno = 'default';
189 fixContext();
190 if (!permitted())
191 renderAccessDenied();
192 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
193 echo '<h2>Choose a port:</h2><br><br>';
194 echo '<form action="javascript:;">';
195 echo '<input type=hidden id=ip>';
196 echo '<select size=' . getConfigVar ('MAXSELSIZE') . ' id=addresses>';
197 $addresses = getAllIPv4Allocations();
198 usort ($addresses, 'sortObjectAddressesAndNames');
199 foreach ($addresses as $address)
200 echo "<option value='${address['ip']}' onclick='getElementById(\"ip\").value=\"${address['ip']}\";'>" .
201 "${address['object_name']} ${address['name']} ${address['ip']}</option>\n";
202 echo '</select><br><br>';
203 echo "<input type=submit value='Proceed' onclick='".
204 "if (getElementById(\"ip\")!=\"\") {".
205 " opener.document.getElementById(\"remoteip\").value=getElementById(\"ip\").value;".
206 " window.close();}'>";
207 echo '</form></div>';
208 break;
209 default:
210 throw new InvalidRequestArgException ('helper', $_REQUEST['helper']);
211 }
212 ?>
213 </body>
214 </html>
215 <?php
216 ob_end_flush();
217 }
218 catch (Exception $e)
219 {
220 ob_end_clean();
221 printException($e);
222 }
223 ?>