r3186 - maintenance->trunk sync of changesets 3182~3185
[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 $query = "SELECT id, object_id, name FROM Port WHERE " .
38 "id <> ${port_id} " .
39 "AND type IN (SELECT type2 FROM PortCompat WHERE type1 = (SELECT type FROM Port WHERE id = ${port_id})) " .
40 "AND reservation_comment IS NULL " .
41 "AND id NOT IN (SELECT porta FROM Link) " .
42 "AND id NOT IN (SELECT portb FROM Link) ";
43 if (count ($only_racks))
44 $query .= 'AND object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
45 implode (', ', $only_racks) . '))';
46 $result = useSelectBlade ($query);
47 // avoid nested queries
48 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
49 unset ($result);
50 $ret = array();
51 foreach (array_keys ($rows) as $rkey)
52 {
53 $object = spotEntity ('object', $rows[$rkey]['object_id']);
54 $ret[$rows[$rkey]['id']] = $object['dname'] . ' ' . $rows[$rkey]['name'];
55 unset ($rows[$rkey]);
56 }
57 return $ret;
58 }
59
60 ?>
61 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
62 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
63 <head>
64 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
65 <?php
66 echo "<title>RackTables pop-up</title>\n";
67 echo "<link rel=stylesheet type='text/css' href=pi.css />\n";
68 echo "<link rel=icon href='" . getFaviconURL() . "' type='image/x-icon' />";
69 echo '</head><body style="height: 100%;">';
70 assertStringArg ('helper');
71 switch ($_REQUEST['helper'])
72 {
73 case 'portlist':
74 $pageno = 'depot';
75 $tabno = 'default';
76 fixContext();
77 if (!permitted())
78 renderAccessDenied();
79 assertUIntArg ('port');
80 assertStringArg ('in_rack');
81 $localchoice = $_REQUEST['in_rack'] == 'y';
82 $port_id = $_REQUEST['port'];
83 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;"><h2>';
84 echo $localchoice ?
85 ('Nearest spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=n">show all</a>)') :
86 ('All spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=y">show nearest</a>)');
87 echo '</h2><form action="javascript:;">';
88 $only_racks = array();
89 $port_info = getPortInfo ($port_id);
90 if ($_REQUEST['in_rack'] == 'y' and $port_info['object_id'])
91 {
92 $object = spotEntity ('object', $port_info['object_id']);
93 if ($object['rack_id'])
94 $only_racks = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
95 }
96 $spare_ports = findSparePorts ($port_id, $only_racks);
97 printSelect ($spare_ports, array ('name' => 'ports', 'id' => 'ports', 'size' => getConfigVar ('MAXSELSIZE')));
98 echo '<br><br>';
99 echo "<input type='submit' value='Link' onclick='".
100 "if (getElementById(\"ports\").value != \"\") {".
101 " 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; ".
102 " window.close();}'>";
103 echo '</form></div>';
104 break;
105 case 'inet4list':
106 $pageno = 'ipv4space';
107 $tabno = 'default';
108 fixContext();
109 if (!permitted())
110 renderAccessDenied();
111 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
112 echo '<h2>Choose a port:</h2><br><br>';
113 echo '<form action="javascript:;">';
114 echo '<input type=hidden id=ip>';
115 echo '<select size=' . getConfigVar ('MAXSELSIZE') . ' id=addresses>';
116 $addresses = getAllIPv4Allocations();
117 usort ($addresses, 'sortObjectAddressesAndNames');
118 foreach ($addresses as $address)
119 echo "<option value='${address['ip']}' onclick='getElementById(\"ip\").value=\"${address['ip']}\";'>" .
120 "${address['object_name']} ${address['name']} ${address['ip']}</option>\n";
121 echo '</select><br><br>';
122 echo "<input type=submit value='Proceed' onclick='".
123 "if (getElementById(\"ip\")!=\"\") {".
124 " opener.document.getElementById(\"remoteip\").value=getElementById(\"ip\").value;".
125 " window.close();}'>";
126 echo '</form></div>';
127 break;
128 default:
129 throw new RuntimeException ('Invalid parameter or internal error');
130 }
131 ?>
132 </body>
133 </html>
134 <?php
135 ob_end_flush();
136 }
137 catch (Exception $e)
138 {
139 ob_end_clean();
140 printException($e);
141 }
142 ?>