Commit | Line | Data |
---|---|---|
b325120a | 1 | <?php |
e673ee24 DO |
2 | /* |
3 | * | |
4 | * This file is a library of database access functions for RackTables. | |
5 | * | |
6 | */ | |
7 | ||
a6e91ac2 DO |
8 | $SQLSchema = array |
9 | ( | |
10 | 'object' => array | |
11 | ( | |
12 | 'table' => 'RackObject', | |
13 | 'columns' => array | |
14 | ( | |
15 | 'id' => 'id', | |
16 | 'name' => 'name', | |
17 | 'label' => 'label', | |
a6e91ac2 DO |
18 | 'asset_no' => 'asset_no', |
19 | 'objtype_id' => 'objtype_id', | |
20 | 'rack_id' => '(select rack_id from RackSpace where object_id = id order by rack_id asc limit 1)', | |
21 | 'Rack_name' => '(select name from Rack where id = rack_id)', | |
22 | 'row_id' => '(select row_id from Rack where id = rack_id)', | |
23 | 'Row_name' => '(select name from RackRow where id = row_id)', | |
0d993afe | 24 | 'has_problems' => 'has_problems', |
6657739e | 25 | 'comment' => 'comment', |
6d472f26 | 26 | 'nports' => '(SELECT COUNT(*) FROM Port WHERE object_id = RackObject.id)', |
b48d8d61 | 27 | 'runs8021Q' => '(SELECT 1 FROM VLANSwitch WHERE object_id = id LIMIT 1)', |
a6e91ac2 DO |
28 | ), |
29 | 'keycolumn' => 'id', | |
978dc510 | 30 | 'ordcolumns' => array ('RackObject.name'), |
a6e91ac2 DO |
31 | ), |
32 | 'user' => array | |
33 | ( | |
34 | 'table' => 'UserAccount', | |
35 | 'columns' => array | |
36 | ( | |
37 | 'user_id' => 'user_id', | |
38 | 'user_name' => 'user_name', | |
39 | 'user_password_hash' => 'user_password_hash', | |
40 | 'user_realname' => 'user_realname', | |
41 | ), | |
42 | 'keycolumn' => 'user_id', | |
978dc510 | 43 | 'ordcolumns' => array ('UserAccount.user_name'), |
a6e91ac2 DO |
44 | ), |
45 | 'ipv4net' => array | |
46 | ( | |
47 | 'table' => 'IPv4Network', | |
48 | 'columns' => array | |
49 | ( | |
50 | 'id' => 'id', | |
51 | 'ip' => 'INET_NTOA(IPv4Network.ip)', | |
52 | 'mask' => 'mask', | |
53 | 'name' => 'name', | |
99ab184f | 54 | 'comment' => 'comment', |
b1e39212 | 55 | 'parent_id' => '(SELECT id FROM IPv4Network AS subt WHERE IPv4Network.ip & (4294967295 >> (32 - subt.mask)) << (32 - subt.mask) = subt.ip and subt.mask < IPv4Network.mask ORDER BY subt.mask DESC limit 1)', |
a5f9d1ca | 56 | 'vlanc' => '(SELECT COUNT(*) FROM VLANIPv4 WHERE ipv4net_id = id)', |
a6e91ac2 DO |
57 | ), |
58 | 'keycolumn' => 'id', | |
978dc510 | 59 | 'ordcolumns' => array ('IPv4Network.ip', 'IPv4Network.mask'), |
a6e91ac2 | 60 | ), |
21ee3351 AA |
61 | 'ipv6net' => array |
62 | ( | |
63 | 'table' => 'IPv6Network', | |
64 | 'columns' => array | |
65 | ( | |
66 | 'id' => 'id', | |
67 | 'ip_bin' => 'ip', | |
68 | 'mask' => 'mask', | |
69 | 'name' => 'name', | |
70 | 'comment' => 'comment', | |
71 | 'parent_id' => '(SELECT id FROM IPv6Network AS subt WHERE IPv6Network.ip >= subt.ip AND IPv6Network.last_ip <= subt.last_ip AND IPv6Network.mask > subt.mask ORDER BY subt.mask DESC limit 1)', | |
72 | 'vlanc' => '(SELECT COUNT(*) FROM VLANIPv6 WHERE ipv6net_id = id)', | |
73 | ), | |
74 | 'keycolumn' => 'id', | |
75 | 'ordcolumns' => array ('IPv6Network.ip', 'IPv6Network.mask'), | |
76 | ), | |
a6e91ac2 DO |
77 | 'file' => array |
78 | ( | |
79 | 'table' => 'File', | |
80 | 'columns' => array | |
81 | ( | |
82 | 'id' => 'id', | |
83 | 'name' => 'name', | |
84 | 'type' => 'type', | |
85 | 'size' => 'size', | |
86 | 'ctime' => 'ctime', | |
87 | 'mtime' => 'mtime', | |
88 | 'atime' => 'atime', | |
89 | 'comment' => 'comment', | |
90 | ), | |
91 | 'keycolumn' => 'id', | |
978dc510 | 92 | 'ordcolumns' => array ('File.name'), |
a6e91ac2 DO |
93 | ), |
94 | 'ipv4vs' => array | |
95 | ( | |
96 | 'table' => 'IPv4VS', | |
97 | 'columns' => array | |
98 | ( | |
99 | 'id' => 'id', | |
100 | 'vip' => 'INET_NTOA(vip)', | |
101 | 'vport' => 'vport', | |
102 | 'proto' => 'proto', | |
103 | 'name' => 'name', | |
104 | 'vsconfig' => 'vsconfig', | |
105 | 'rsconfig' => 'rsconfig', | |
106 | 'poolcount' => '(select count(vs_id) from IPv4LB where vs_id = id)', | |
107 | 'dname' => 'CONCAT_WS("/", CONCAT_WS(":", INET_NTOA(vip), vport), proto)', | |
108 | ), | |
109 | 'keycolumn' => 'id', | |
978dc510 | 110 | 'ordcolumns' => array ('IPv4VS.vip', 'IPv4VS.proto', 'IPv4VS.vport'), |
a6e91ac2 DO |
111 | ), |
112 | 'ipv4rspool' => array | |
113 | ( | |
114 | 'table' => 'IPv4RSPool', | |
115 | 'columns' => array | |
116 | ( | |
117 | 'id' => 'id', | |
118 | 'name' => 'name', | |
119 | 'refcnt' => '(select count(rspool_id) from IPv4LB where rspool_id = id)', | |
120 | 'rscount' => '(select count(rspool_id) from IPv4RS where rspool_id = IPv4RSPool.id)', | |
121 | 'vsconfig' => 'vsconfig', | |
122 | 'rsconfig' => 'rsconfig', | |
123 | ), | |
124 | 'keycolumn' => 'id', | |
978dc510 | 125 | 'ordcolumns' => array ('IPv4RSPool.name', 'IPv4RSPool.id'), |
a6e91ac2 DO |
126 | ), |
127 | 'rack' => array | |
128 | ( | |
129 | 'table' => 'Rack', | |
130 | 'columns' => array | |
131 | ( | |
132 | 'id' => 'id', | |
133 | 'name' => 'name', | |
134 | 'height' => 'height', | |
135 | 'comment' => 'comment', | |
136 | 'row_id' => 'row_id', | |
137 | 'row_name' => '(select name from RackRow where RackRow.id = row_id)', | |
138 | ), | |
139 | 'keycolumn' => 'id', | |
978dc510 | 140 | 'ordcolumns' => array ('row_name', 'Rack.name'), |
a6e91ac2 DO |
141 | 'pidcolumn' => 'row_id', |
142 | ), | |
143 | ); | |
144 | ||
1b4a0a6a DO |
145 | $searchfunc = array |
146 | ( | |
147 | 'object' => array | |
148 | ( | |
149 | 'by_sticker' => 'getStickerSearchResults', | |
150 | 'by_port' => 'getPortSearchResults', | |
151 | 'by_attr' => 'getObjectAttrsSearchResults', | |
152 | 'by_iface' => 'getObjectIfacesSearchResults', | |
153 | 'by_nat' => 'getObjectNATSearchResults', | |
154 | ), | |
155 | ); | |
156 | ||
be28b696 DO |
157 | $tablemap_8021q = array |
158 | ( | |
159 | 'desired' => array | |
160 | ( | |
161 | 'pvm' => 'PortVLANMode', | |
162 | 'pav' => 'PortAllowedVLAN', | |
163 | 'pnv' => 'PortNativeVLAN', | |
164 | ), | |
165 | 'cached' => array | |
166 | ( | |
167 | 'pvm' => 'CachedPVM', | |
168 | 'pav' => 'CachedPAV', | |
169 | 'pnv' => 'CachedPNV', | |
170 | ), | |
171 | ); | |
172 | ||
09ec2e59 AA |
173 | // VST roles |
174 | $port_role_options = array | |
175 | ( | |
176 | 'none' => 'none', | |
177 | 'access' => 'user: access only', | |
178 | 'trunk' => 'user: trunk only', | |
179 | 'anymode' => 'user: any mode', | |
180 | 'uplink' => 'system: uplink trunk', | |
181 | 'downlink' => 'system: downlink trunk', | |
182 | ); | |
183 | ||
1f54e1ba DO |
184 | $object_attribute_cache = array(); |
185 | ||
160a82e6 | 186 | function escapeString ($value, $do_db_escape = FALSE) |
e673ee24 | 187 | { |
b614498d | 188 | $ret = htmlspecialchars ($value, ENT_QUOTES, 'UTF-8'); |
5f54485b DO |
189 | if ($do_db_escape) |
190 | { | |
191 | global $dbxlink; | |
192 | $ret = substr ($dbxlink->quote ($ret), 1, -1); | |
193 | } | |
194 | return $ret; | |
e673ee24 DO |
195 | } |
196 | ||
a8ce7234 DO |
197 | // Return detailed information about one rack row. |
198 | function getRackRowInfo ($rackrow_id) | |
e673ee24 | 199 | { |
e673ee24 | 200 | $query = |
10bac82a | 201 | "select RackRow.id as id, RackRow.name as name, count(Rack.id) as count, " . |
5332840f | 202 | "if(isnull(sum(Rack.height)),0,sum(Rack.height)) as sum " . |
10bac82a | 203 | "from RackRow left join Rack on Rack.row_id = RackRow.id " . |
298d2375 | 204 | "where RackRow.id = ? " . |
1743cafe | 205 | "group by RackRow.id"; |
298d2375 | 206 | $result = usePreparedSelectBlade ($query, array ($rackrow_id)); |
a8ce7234 DO |
207 | if ($row = $result->fetch (PDO::FETCH_ASSOC)) |
208 | return $row; | |
e673ee24 | 209 | else |
0cc24e9a | 210 | throw new EntityNotFoundException ('rackrow', $rackrow_id); |
e673ee24 DO |
211 | } |
212 | ||
51690ad4 DY |
213 | function getRackRows () |
214 | { | |
650ef981 | 215 | $result = usePreparedSelectBlade ('SELECT id, name FROM RackRow ORDER BY name'); |
51690ad4 DY |
216 | $rows = array(); |
217 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
2a3f0501 | 218 | $rows[$row['id']] = $row['name']; |
51690ad4 DY |
219 | return $rows; |
220 | } | |
221 | ||
8d4f7d18 | 222 | // Return a simple object list w/o related information, so that the returned value |
c6bc0ac5 DO |
223 | // can be directly used by printSelect(). An optional argument is the name of config |
224 | // option with constraint in RackCode. | |
225 | function getNarrowObjectList ($varname = '') | |
e9132485 | 226 | { |
212c9d8a | 227 | $wideList = listCells ('object'); |
59dce363 | 228 | if (strlen ($varname) and strlen (getConfigVar ($varname))) |
e9132485 | 229 | { |
59dce363 DO |
230 | global $parseCache; |
231 | if (!isset ($parseCache[$varname])) | |
232 | $parseCache[$varname] = spotPayload (getConfigVar ($varname), 'SYNT_EXPR'); | |
233 | if ($parseCache[$varname]['result'] != 'ACK') | |
234 | return array(); | |
212c9d8a | 235 | $wideList = filterCellList ($wideList, $parseCache[$varname]['load']); |
e9132485 | 236 | } |
212c9d8a DO |
237 | $ret = array(); |
238 | foreach ($wideList as $cell) | |
239 | $ret[$cell['id']] = $cell['dname']; | |
e9132485 DO |
240 | return $ret; |
241 | } | |
242 | ||
d08d766d | 243 | // For a given realm return a list of entity records, each with |
212c9d8a | 244 | // enough information for judgeCell() to execute. |
a8efc03e | 245 | function listCells ($realm, $parent_id = 0) |
d08d766d | 246 | { |
7cfc98b1 DO |
247 | if (!$parent_id) |
248 | { | |
249 | global $entityCache; | |
250 | if (isset ($entityCache['complete'][$realm])) | |
251 | return $entityCache['complete'][$realm]; | |
252 | } | |
a6e91ac2 DO |
253 | global $SQLSchema; |
254 | if (!isset ($SQLSchema[$realm])) | |
3a089a44 | 255 | throw new InvalidArgException ('realm', $realm); |
a6e91ac2 | 256 | $SQLinfo = $SQLSchema[$realm]; |
1f54e1ba DO |
257 | $qparams = array (); |
258 | $query = 'SELECT '; | |
a6e91ac2 | 259 | foreach ($SQLinfo['columns'] as $alias => $expression) |
573214e0 | 260 | // Automatically prepend table name to each single column, but leave all others intact. |
1f54e1ba DO |
261 | $query .= ($alias == $expression ? "${SQLinfo['table']}.${alias}" : "${expression} as ${alias}") . ', '; |
262 | $query = trim($query, ', '); | |
263 | $query .= " FROM ${SQLinfo['table']}"; | |
a6e91ac2 | 264 | if (isset ($SQLinfo['pidcolumn']) and $parent_id) |
a685e6d7 DO |
265 | { |
266 | $query .= " WHERE ${SQLinfo['table']}.${SQLinfo['pidcolumn']} = ?"; | |
267 | $qparams[] = $parent_id; | |
268 | } | |
a8efc03e | 269 | $query .= " ORDER BY "; |
a6e91ac2 | 270 | foreach ($SQLinfo['ordcolumns'] as $oc) |
978dc510 | 271 | $query .= "${oc}, "; |
1f54e1ba | 272 | $query = trim($query, ', '); |
a685e6d7 | 273 | $result = usePreparedSelectBlade ($query, $qparams); |
d08d766d | 274 | $ret = array(); |
d08d766d DO |
275 | // Index returned result by the value of key column. |
276 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
277 | { | |
a6e91ac2 | 278 | $entity_id = $row[$SQLinfo['keycolumn']]; |
1f54e1ba DO |
279 | $ret[$entity_id] = array ('realm' => $realm); |
280 | $ret[$entity_id]['etags'] = array(); | |
281 | foreach (array_keys ($SQLinfo['columns']) as $alias) | |
282 | $ret[$entity_id][$alias] = $row[$alias]; | |
283 | } | |
d2eb8399 | 284 | unset($result); |
1f54e1ba DO |
285 | |
286 | // select tags and link them to previosly fetched entities | |
287 | $query = 'SELECT entity_id, tag_id FROM TagStorage WHERE entity_realm = ?'; | |
288 | $result = usePreparedSelectBlade ($query, array($realm)); | |
289 | global $taglist; | |
290 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
291 | { | |
292 | if (array_key_exists($row['entity_id'], $ret)) | |
293 | $ret[$row['entity_id']]['etags'][] = array | |
d08d766d DO |
294 | ( |
295 | 'id' => $row['tag_id'], | |
296 | 'tag' => $taglist[$row['tag_id']]['tag'], | |
297 | 'parent_id' => $taglist[$row['tag_id']]['parent_id'], | |
298 | ); | |
299 | } | |
d2eb8399 | 300 | unset($result); |
7cfc98b1 DO |
301 | // Add necessary finish to the list before returning it. Maintain caches. |
302 | if (!$parent_id) | |
303 | unset ($entityCache['partial'][$realm]); | |
1f54e1ba DO |
304 | if ($realm == 'object') // cache all attributes of all objects to speed up autotags calculation |
305 | cacheAllObjectsAttributes(); | |
d08d766d DO |
306 | foreach (array_keys ($ret) as $entity_id) |
307 | { | |
6df2025d | 308 | $ret[$entity_id]['etags'] = getExplicitTagsOnly ($ret[$entity_id]['etags']); |
d08d766d | 309 | $ret[$entity_id]['itags'] = getImplicitTags ($ret[$entity_id]['etags']); |
d16af52f | 310 | $ret[$entity_id]['atags'] = generateEntityAutoTags ($ret[$entity_id]); |
23cdc7e9 DO |
311 | switch ($realm) |
312 | { | |
313 | case 'object': | |
cfa8f3cf | 314 | setDisplayedName ($ret[$entity_id]); |
23cdc7e9 | 315 | break; |
d724d290 DO |
316 | case 'ipv4net': |
317 | $ret[$entity_id]['ip_bin'] = ip2long ($ret[$entity_id]['ip']); | |
318 | $ret[$entity_id]['mask_bin'] = binMaskFromDec ($ret[$entity_id]['mask']); | |
319 | $ret[$entity_id]['mask_bin_inv'] = binInvMaskFromDec ($ret[$entity_id]['mask']); | |
2eb52ed1 AA |
320 | $ret[$entity_id]['db_first'] = sprintf ('%u', 0x00000000 + $ret[$entity_id]['ip_bin'] & $ret[$entity_id]['mask_bin']); |
321 | $ret[$entity_id]['db_last'] = sprintf ('%u', 0x00000000 + $ret[$entity_id]['ip_bin'] | ($ret[$entity_id]['mask_bin_inv'])); | |
21ee3351 AA |
322 | break; |
323 | case 'ipv6net': | |
324 | $ret[$entity_id]['ip_bin'] = new IPv6Address ($ret[$entity_id]['ip_bin']); | |
325 | $ret[$entity_id]['ip'] = $ret[$entity_id]['ip_bin']->format(); | |
2eb52ed1 AA |
326 | $ret[$entity_id]['db_first'] = $ret[$entity_id]['ip_bin']->get_first_subnet_address($ret[$entity_id]['mask']); |
327 | $ret[$entity_id]['db_last'] = $ret[$entity_id]['ip_bin']->get_last_subnet_address($ret[$entity_id]['mask']); | |
d724d290 | 328 | break; |
23cdc7e9 DO |
329 | default: |
330 | break; | |
331 | } | |
7cfc98b1 DO |
332 | if (!$parent_id) |
333 | $entityCache['complete'][$realm][$entity_id] = $ret[$entity_id]; | |
334 | else | |
335 | $entityCache['partial'][$realm][$entity_id] = $ret[$entity_id]; | |
d08d766d DO |
336 | } |
337 | return $ret; | |
338 | } | |
339 | ||
a6e91ac2 DO |
340 | // Very much like listCells(), but return only one record requested (or NULL, |
341 | // if it does not exist). | |
342 | function spotEntity ($realm, $id) | |
343 | { | |
7cfc98b1 DO |
344 | global $entityCache; |
345 | if (isset ($entityCache['complete'][$realm])) | |
346 | // Emphasize the absence of record, if listCells() has already been called. | |
b135a49d DY |
347 | if (isset ($entityCache['complete'][$realm][$id])) |
348 | return $entityCache['complete'][$realm][$id]; | |
349 | else | |
350 | throw new EntityNotFoundException ($realm, $id); | |
7cfc98b1 DO |
351 | elseif (isset ($entityCache['partial'][$realm][$id])) |
352 | return $entityCache['partial'][$realm][$id]; | |
a6e91ac2 DO |
353 | global $SQLSchema; |
354 | if (!isset ($SQLSchema[$realm])) | |
3a089a44 | 355 | throw new InvalidArgException ('realm', $realm); |
a6e91ac2 DO |
356 | $SQLinfo = $SQLSchema[$realm]; |
357 | $query = 'SELECT tag_id'; | |
358 | foreach ($SQLinfo['columns'] as $alias => $expression) | |
359 | // Automatically prepend table name to each single column, but leave all others intact. | |
360 | $query .= ', ' . ($alias == $expression ? "${SQLinfo['table']}.${alias}" : "${expression} as ${alias}"); | |
a685e6d7 DO |
361 | $query .= " FROM ${SQLinfo['table']} LEFT JOIN TagStorage on entity_realm = ? and entity_id = ${SQLinfo['table']}.${SQLinfo['keycolumn']}"; |
362 | $query .= " WHERE ${SQLinfo['table']}.${SQLinfo['keycolumn']} = ?"; | |
a6e91ac2 | 363 | $query .= " ORDER BY tag_id"; |
a685e6d7 | 364 | $result = usePreparedSelectBlade ($query, array ($realm, $id)); |
a6e91ac2 DO |
365 | $ret = array(); |
366 | global $taglist; | |
367 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
368 | if (!isset ($ret['realm'])) | |
369 | { | |
370 | $ret = array ('realm' => $realm); | |
371 | foreach (array_keys ($SQLinfo['columns']) as $alias) | |
372 | $ret[$alias] = $row[$alias]; | |
373 | $ret['etags'] = array(); | |
374 | if ($row['tag_id'] != NULL && isset ($taglist[$row['tag_id']])) | |
375 | $ret['etags'][] = array | |
376 | ( | |
377 | 'id' => $row['tag_id'], | |
378 | 'tag' => $taglist[$row['tag_id']]['tag'], | |
379 | 'parent_id' => $taglist[$row['tag_id']]['parent_id'], | |
380 | ); | |
381 | } | |
382 | elseif (isset ($taglist[$row['tag_id']])) | |
383 | $ret['etags'][] = array | |
384 | ( | |
385 | 'id' => $row['tag_id'], | |
386 | 'tag' => $taglist[$row['tag_id']]['tag'], | |
387 | 'parent_id' => $taglist[$row['tag_id']]['parent_id'], | |
388 | ); | |
389 | unset ($result); | |
390 | if (!isset ($ret['realm'])) // no rows were returned | |
b135a49d | 391 | throw new EntityNotFoundException ($realm, $id); |
6df2025d | 392 | $ret['etags'] = getExplicitTagsOnly ($ret['etags']); |
a6e91ac2 | 393 | $ret['itags'] = getImplicitTags ($ret['etags']); |
d16af52f | 394 | $ret['atags'] = generateEntityAutoTags ($ret); |
a6e91ac2 DO |
395 | switch ($realm) |
396 | { | |
397 | case 'object': | |
cfa8f3cf | 398 | setDisplayedName ($ret); |
a6e91ac2 | 399 | break; |
d724d290 DO |
400 | case 'ipv4net': |
401 | $ret['ip_bin'] = ip2long ($ret['ip']); | |
402 | $ret['mask_bin'] = binMaskFromDec ($ret['mask']); | |
403 | $ret['mask_bin_inv'] = binInvMaskFromDec ($ret['mask']); | |
2eb52ed1 AA |
404 | $ret['db_first'] = sprintf ('%u', 0x00000000 + $ret['ip_bin'] & $ret['mask_bin']); |
405 | $ret['db_last'] = sprintf ('%u', 0x00000000 + $ret['ip_bin'] | ($ret['mask_bin_inv'])); | |
21ee3351 AA |
406 | break; |
407 | case 'ipv6net': | |
408 | $ret['ip_bin'] = new IPv6Address ($ret['ip_bin']); | |
409 | $ret['ip'] = $ret['ip_bin']->format(); | |
2eb52ed1 AA |
410 | $ret['db_first'] = $ret['ip_bin']->get_first_subnet_address($ret['mask']); |
411 | $ret['db_last'] = $ret['ip_bin']->get_last_subnet_address($ret['mask']); | |
d724d290 | 412 | break; |
a6e91ac2 DO |
413 | default: |
414 | break; | |
415 | } | |
7cfc98b1 | 416 | $entityCache['partial'][$realm][$id] = $ret; |
a6e91ac2 DO |
417 | return $ret; |
418 | } | |
419 | ||
d08d766d | 420 | // This function can be used with array_walk(). |
573214e0 | 421 | function amplifyCell (&$record, $dummy = NULL) |
d08d766d | 422 | { |
d08d766d DO |
423 | switch ($record['realm']) |
424 | { | |
425 | case 'object': | |
426 | $record['ports'] = getObjectPortsAndLinks ($record['id']); | |
427 | $record['ipv4'] = getObjectIPv4Allocations ($record['id']); | |
21ee3351 | 428 | $record['ipv6'] = getObjectIPv6Allocations ($record['id']); |
d08d766d DO |
429 | $record['nat4'] = getNATv4ForObject ($record['id']); |
430 | $record['ipv4rspools'] = getRSPoolsForObject ($record['id']); | |
431 | $record['files'] = getFilesOfEntity ($record['realm'], $record['id']); | |
432 | break; | |
8bc5d1e4 DO |
433 | case 'file': |
434 | $record['links'] = getFileLinks ($record['id']); | |
435 | break; | |
a6e91ac2 DO |
436 | case 'ipv4rspool': |
437 | $record['lblist'] = array(); | |
1f54e1ba | 438 | $query = "select object_id, vs_id, lb.vsconfig, lb.rsconfig, lb.prio from " . |
a6e91ac2 | 439 | "IPv4LB as lb inner join IPv4VS as vs on lb.vs_id = vs.id " . |
a685e6d7 DO |
440 | "where rspool_id = ? order by object_id, vip, vport"; |
441 | $result = usePreparedSelectBlade ($query, array ($record['id'])); | |
a6e91ac2 DO |
442 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
443 | $record['lblist'][$row['object_id']][$row['vs_id']] = array | |
444 | ( | |
445 | 'rsconfig' => $row['rsconfig'], | |
446 | 'vsconfig' => $row['vsconfig'], | |
1f54e1ba | 447 | 'prio' => $row['prio'], |
a6e91ac2 DO |
448 | ); |
449 | unset ($result); | |
7c38462f | 450 | $record['rslist'] = array(); |
a6e91ac2 | 451 | $query = "select id, inservice, inet_ntoa(rsip) as rsip, rsport, rsconfig from " . |
a685e6d7 DO |
452 | "IPv4RS where rspool_id = ? order by IPv4RS.rsip, rsport"; |
453 | $result = usePreparedSelectBlade ($query, array ($record['id'])); | |
a6e91ac2 DO |
454 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
455 | $record['rslist'][$row['id']] = array | |
456 | ( | |
457 | 'inservice' => $row['inservice'], | |
458 | 'rsip' => $row['rsip'], | |
459 | 'rsport' => $row['rsport'], | |
460 | 'rsconfig' => $row['rsconfig'], | |
461 | ); | |
462 | unset ($result); | |
7c38462f DO |
463 | break; |
464 | case 'ipv4vs': | |
465 | // Get the detailed composition of a particular virtual service, namely the list | |
466 | // of all pools, each shown with the list of objects servicing it. VS/RS configs | |
467 | // will be returned as well. | |
468 | $record['rspool'] = array(); | |
469 | $query = "select pool.id, name, pool.vsconfig, pool.rsconfig, object_id, " . | |
1f54e1ba | 470 | "lb.vsconfig as lb_vsconfig, lb.rsconfig as lb_rsconfig, lb.prio from " . |
7c38462f | 471 | "IPv4RSPool as pool left join IPv4LB as lb on pool.id = lb.rspool_id " . |
a685e6d7 DO |
472 | "where vs_id = ? order by pool.name, object_id"; |
473 | $result = usePreparedSelectBlade ($query, array ($record['id'])); | |
7c38462f DO |
474 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
475 | { | |
476 | if (!isset ($record['rspool'][$row['id']])) | |
477 | $record['rspool'][$row['id']] = array | |
478 | ( | |
479 | 'name' => $row['name'], | |
480 | 'vsconfig' => $row['vsconfig'], | |
481 | 'rsconfig' => $row['rsconfig'], | |
482 | 'lblist' => array(), | |
483 | ); | |
484 | if ($row['object_id'] == NULL) | |
485 | continue; | |
486 | $record['rspool'][$row['id']]['lblist'][$row['object_id']] = array | |
487 | ( | |
488 | 'vsconfig' => $row['lb_vsconfig'], | |
489 | 'rsconfig' => $row['lb_rsconfig'], | |
1f54e1ba | 490 | 'prio' => $row['prio'], |
7c38462f DO |
491 | ); |
492 | } | |
493 | unset ($result); | |
494 | break; | |
61a1d996 DO |
495 | case 'rack': |
496 | $record['mountedObjects'] = array(); | |
497 | // start with default rackspace | |
498 | for ($i = $record['height']; $i > 0; $i--) | |
499 | for ($locidx = 0; $locidx < 3; $locidx++) | |
500 | $record[$i][$locidx]['state'] = 'F'; | |
501 | // load difference | |
502 | $query = | |
503 | "select unit_no, atom, state, object_id " . | |
a685e6d7 DO |
504 | "from RackSpace where rack_id = ? and " . |
505 | "unit_no between 1 and ? order by unit_no"; | |
506 | $result = usePreparedSelectBlade ($query, array ($record['id'], $record['height'])); | |
61a1d996 DO |
507 | global $loclist; |
508 | $mounted_objects = array(); | |
509 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
510 | { | |
511 | $record[$row['unit_no']][$loclist[$row['atom']]]['state'] = $row['state']; | |
512 | $record[$row['unit_no']][$loclist[$row['atom']]]['object_id'] = $row['object_id']; | |
513 | if ($row['state'] == 'T' and $row['object_id'] != NULL) | |
514 | $mounted_objects[$row['object_id']] = TRUE; | |
515 | } | |
516 | $record['mountedObjects'] = array_keys ($mounted_objects); | |
517 | unset ($result); | |
518 | break; | |
a5f9d1ca DO |
519 | case 'ipv4net': |
520 | $record['8021q'] = getIPv4Network8021QBindings ($record['id']); | |
521 | break; | |
21ee3351 AA |
522 | case 'ipv6net': |
523 | $record['8021q'] = getIPv6Network8021QBindings ($record['id']); | |
524 | break; | |
d08d766d DO |
525 | default: |
526 | } | |
527 | } | |
528 | ||
125b5f39 | 529 | function getObjectPortsAndLinks ($object_id) |
e673ee24 | 530 | { |
9f75e165 | 531 | $query = "SELECT id, name, label, l2address, iif_id, (SELECT iif_name FROM PortInnerInterface WHERE id = iif_id) AS iif_name, " . |
045bab87 | 532 | "type AS oif_id, (SELECT dict_value FROM Dictionary WHERE dict_key = type) AS oif_name, reservation_comment " . |
298d2375 | 533 | "FROM Port WHERE object_id = ?"; |
1919110a | 534 | // list and decode all ports of the current object |
298d2375 | 535 | $result = usePreparedSelectBlade ($query, array ($object_id)); |
e425f895 | 536 | $ret=array(); |
e425f895 | 537 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
e673ee24 | 538 | { |
1919110a DO |
539 | $row['l2address'] = l2addressFromDatabase ($row['l2address']); |
540 | $row['remote_id'] = NULL; | |
541 | $row['remote_name'] = NULL; | |
542 | $row['remote_object_id'] = NULL; | |
1919110a DO |
543 | $ret[] = $row; |
544 | } | |
545 | unset ($result); | |
546 | // now find and decode remote ends for all locally terminated connections | |
125b5f39 | 547 | // FIXME: can't this data be extracted in one pass with sub-queries? |
1919110a DO |
548 | foreach (array_keys ($ret) as $tmpkey) |
549 | { | |
550 | $portid = $ret[$tmpkey]['id']; | |
551 | $remote_id = NULL; | |
0c7c9f8b | 552 | $query = "select porta, portb, cable from Link where porta = ? or portb = ?"; |
298d2375 | 553 | $result = usePreparedSelectBlade ($query, array ($portid, $portid)); |
0c7c9f8b | 554 | $cable = "CableID n/a"; |
1919110a | 555 | if ($row = $result->fetch (PDO::FETCH_ASSOC)) |
e673ee24 | 556 | { |
1919110a DO |
557 | if ($portid != $row['porta']) |
558 | $remote_id = $row['porta']; | |
559 | elseif ($portid != $row['portb']) | |
560 | $remote_id = $row['portb']; | |
0c7c9f8b | 561 | $cable = $row['cable']; |
1919110a DO |
562 | } |
563 | unset ($result); | |
564 | if ($remote_id) // there's a remote end here | |
565 | { | |
298d2375 DO |
566 | $query = "SELECT name, object_id FROM Port WHERE id = ?"; |
567 | $result = usePreparedSelectBlade ($query, array ($remote_id)); | |
1919110a DO |
568 | if ($row = $result->fetch (PDO::FETCH_ASSOC)) |
569 | { | |
cd3775e9 | 570 | $ret[$tmpkey]['remote_name'] = $row['name']; |
1919110a | 571 | $ret[$tmpkey]['remote_object_id'] = $row['object_id']; |
0c7c9f8b | 572 | $ret[$tmpkey]['cableid'] = $cable; |
1919110a DO |
573 | } |
574 | $ret[$tmpkey]['remote_id'] = $remote_id; | |
575 | unset ($result); | |
e673ee24 DO |
576 | } |
577 | } | |
1ebbf889 | 578 | return sortPortList ($ret, TRUE); |
e673ee24 DO |
579 | } |
580 | ||
ef5bb52c | 581 | function commitAddRack ($name, $height, $row_id, $comment, $taglist) |
e673ee24 | 582 | { |
ef5bb52c | 583 | usePreparedInsertBlade |
406b54bd DO |
584 | ( |
585 | 'Rack', | |
586 | array | |
587 | ( | |
588 | 'row_id' => $row_id, | |
298d2375 | 589 | 'name' => $name, |
406b54bd | 590 | 'height' => $height, |
298d2375 | 591 | 'comment' => $comment |
406b54bd DO |
592 | ) |
593 | ); | |
c63a8d6e | 594 | $last_insert_id = lastInsertID(); |
ef5bb52c DO |
595 | produceTagsForLastRecord ('rack', $taglist, $last_insert_id); |
596 | recordHistory ('Rack', $last_insert_id); | |
e673ee24 DO |
597 | } |
598 | ||
81659c05 | 599 | function commitAddObject ($new_name, $new_label, $new_type_id, $new_asset_no, $taglist = array()) |
e673ee24 | 600 | { |
f60bb422 DO |
601 | // Maintain UNIQUE INDEX for common names and asset tags by |
602 | // filtering out empty strings (not NULLs). | |
298d2375 | 603 | $result1 = usePreparedInsertBlade |
f60bb422 DO |
604 | ( |
605 | 'RackObject', | |
606 | array | |
607 | ( | |
298d2375 | 608 | 'name' => !strlen ($new_name) ? NULL : $new_name, |
650ef981 | 609 | 'label' => $new_label, |
f60bb422 | 610 | 'objtype_id' => $new_type_id, |
298d2375 | 611 | 'asset_no' => !strlen ($new_asset_no) ? NULL : $new_asset_no, |
f60bb422 DO |
612 | ) |
613 | ); | |
c63a8d6e | 614 | $last_insert_id = lastInsertID(); |
ad0e4754 DO |
615 | // Do AutoPorts magic |
616 | executeAutoPorts ($last_insert_id, $new_type_id); | |
f60bb422 | 617 | // Now tags... |
ef5bb52c | 618 | produceTagsForLastRecord ('object', $taglist, $last_insert_id); |
fee7977b | 619 | |
32832c0e | 620 | recordHistory ('RackObject', $last_insert_id); |
fee7977b DO |
621 | |
622 | return $last_insert_id; | |
e673ee24 DO |
623 | } |
624 | ||
81659c05 | 625 | function commitUpdateObject ($object_id, $new_name, $new_label, $new_has_problems, $new_asset_no, $new_comment) |
e673ee24 | 626 | { |
730479cd | 627 | usePreparedUpdateBlade |
39eadd27 | 628 | ( |
730479cd DO |
629 | 'RackObject', |
630 | array | |
631 | ( | |
632 | 'name' => !mb_strlen ($new_name) ? NULL : $new_name, | |
993f5fa3 | 633 | 'label' => !mb_strlen ($new_label) ? NULL : $new_label, |
730479cd DO |
634 | 'has_problems' => $new_has_problems, |
635 | 'asset_no' => !mb_strlen ($new_asset_no) ? NULL : $new_asset_no, | |
636 | 'comment' => $new_comment, | |
637 | ), | |
39eadd27 DO |
638 | array |
639 | ( | |
730479cd | 640 | 'id' => $object_id |
39eadd27 DO |
641 | ) |
642 | ); | |
730479cd | 643 | return recordHistory ('RackObject', $object_id); |
e673ee24 DO |
644 | } |
645 | ||
a3e7b2a0 AD |
646 | // used by getEntityRelatives for sorting |
647 | function compare_name ($a, $b) | |
648 | { | |
649 | return strnatcmp($a['name'], $b['name']); | |
650 | } | |
651 | ||
0682218d AD |
652 | // find either parents or children of a record |
653 | function getEntityRelatives ($type, $entity_type, $entity_id) | |
654 | { | |
655 | if ($type == 'parents') | |
656 | { | |
657 | // searching for parents | |
658 | $sql = | |
659 | 'SELECT id, parent_entity_type AS entity_type, parent_entity_id AS entity_id FROM EntityLink ' . | |
660 | 'WHERE child_entity_type = ? AND child_entity_id = ?'; | |
661 | } | |
662 | else | |
663 | { | |
664 | // searching for children | |
665 | $sql = | |
666 | 'SELECT id, child_entity_type AS entity_type, child_entity_id AS entity_id FROM EntityLink ' . | |
667 | 'WHERE parent_entity_type = ? AND parent_entity_id = ?'; | |
668 | } | |
669 | $query = usePreparedSelectBlade ($sql, array ($entity_type, $entity_id)); | |
670 | $rows = $query->fetchAll (PDO::FETCH_ASSOC); | |
671 | $ret = array(); | |
672 | foreach ($rows as $row) | |
673 | { | |
674 | // get info of the relative (only objects supported now, others may be added later) | |
675 | switch ($row['entity_type']) | |
676 | { | |
677 | case 'object': | |
678 | $page = 'object'; | |
679 | $id_name = 'object_id'; | |
680 | $relative = spotEntity ($row['entity_type'], $row['entity_id']); | |
681 | $name = $relative['dname']; | |
682 | break; | |
683 | } | |
684 | ||
685 | // name needs to have some value for hrefs to work | |
686 | if (!strlen ($name)) | |
687 | $name = sprintf("[Unnamed %s]", formatEntityName($row['entity_type'])); | |
688 | ||
689 | $ret[$row['id']] = array( | |
690 | 'page' => $page, | |
691 | 'id_name' => $id_name, | |
692 | 'entity_type' => $row['entity_type'], | |
693 | 'entity_id' => $row['entity_id'], | |
694 | 'name' => $name | |
695 | ); | |
696 | } | |
0682218d | 697 | // sort by name |
0682218d | 698 | uasort($ret, 'compare_name'); |
0682218d AD |
699 | return $ret; |
700 | } | |
701 | ||
702 | function commitUnlinkEntities ($link_id) | |
703 | { | |
704 | return usePreparedDeleteBlade ('EntityLink', array ('id' => $link_id)); | |
705 | } | |
706 | ||
707 | // The following functions return stats about VM-related info. | |
708 | // TODO: simplify the queries | |
709 | function getVMClusterSummary () | |
710 | { | |
711 | $result = usePreparedSelectBlade | |
712 | ( | |
713 | "SELECT RO.id, RO.name, " . | |
714 | "(SELECT COUNT(*) FROM EntityLink EL " . | |
715 | "LEFT JOIN RackObject RO_H ON EL.child_entity_id = RO_H.id " . | |
716 | "LEFT JOIN AttributeValue AV ON RO_H.id = AV.object_id " . | |
717 | "WHERE EL.parent_entity_type = 'object' " . | |
718 | "AND EL.child_entity_type = 'object' " . | |
719 | "AND EL.parent_entity_id = RO.id " . | |
720 | "AND RO_H.objtype_id = 4 " . | |
721 | "AND AV.attr_id = 26 " . | |
722 | "AND AV.uint_value = 1501) AS hypervisors, " . | |
723 | "(SELECT COUNT(*) FROM EntityLink EL " . | |
724 | "LEFT JOIN RackObject RO_VM ON EL.child_entity_id = RO_VM.id " . | |
725 | "WHERE EL.parent_entity_type = 'object' " . | |
726 | "AND EL.child_entity_type = 'object' " . | |
727 | "AND EL.parent_entity_id = RO.id " . | |
728 | "AND RO_VM.objtype_id = 1504) AS VMs " . | |
729 | "FROM RackObject RO " . | |
730 | "WHERE RO.objtype_id = 1505 " . | |
731 | "ORDER BY RO.name" | |
732 | ); | |
733 | return $result->fetchAll (PDO::FETCH_ASSOC); | |
734 | } | |
735 | ||
736 | function getVMResourcePoolSummary () | |
737 | { | |
738 | $result = usePreparedSelectBlade | |
739 | ( | |
740 | "SELECT RO.id, RO.name, " . | |
741 | "(SELECT RO_C.id " . | |
742 | "FROM EntityLink EL " . | |
743 | "LEFT JOIN RackObject RO_C ON EL.parent_entity_id = RO_C.id " . | |
744 | "WHERE EL.child_entity_id = RO.id " . | |
745 | "AND EL.parent_entity_type = 'object' " . | |
746 | "AND EL.child_entity_type = 'object' " . | |
747 | "AND RO_C.objtype_id = 1505) AS cluster_id, " . | |
748 | "(SELECT RO_C.name " . | |
749 | "FROM EntityLink EL " . | |
750 | "LEFT JOIN RackObject RO_C ON EL.parent_entity_id = RO_C.id " . | |
751 | "WHERE EL.child_entity_id = RO.id " . | |
752 | "AND EL.parent_entity_type = 'object' " . | |
753 | "AND EL.child_entity_type = 'object' " . | |
754 | "AND RO_C.objtype_id = 1505) AS cluster_name, " . | |
755 | "(SELECT COUNT(*) FROM EntityLink EL " . | |
756 | "LEFT JOIN RackObject RO_VM ON EL.child_entity_id = RO_VM.id " . | |
757 | "WHERE EL.parent_entity_type = 'object' " . | |
758 | "AND EL.child_entity_type = 'object' " . | |
759 | "AND EL.parent_entity_id = RO.id " . | |
760 | "AND RO_VM.objtype_id = 1504) AS VMs " . | |
761 | "FROM RackObject RO " . | |
762 | "WHERE RO.objtype_id = 1506 " . | |
763 | "ORDER BY RO.name" | |
764 | ); | |
765 | return $result->fetchAll (PDO::FETCH_ASSOC); | |
766 | } | |
767 | ||
768 | function getVMHypervisorSummary () | |
769 | { | |
770 | $result = usePreparedSelectBlade | |
771 | ( | |
772 | "SELECT RO.id, RO.name, " . | |
773 | "(SELECT RO_C.id " . | |
774 | "FROM EntityLink EL " . | |
775 | "LEFT JOIN RackObject RO_C ON EL.parent_entity_id = RO_C.id " . | |
776 | "WHERE EL.child_entity_id = RO.id " . | |
777 | "AND EL.parent_entity_type = 'object' " . | |
778 | "AND EL.child_entity_type = 'object' " . | |
779 | "AND RO_C.objtype_id = 1505) AS cluster_id, " . | |
780 | "(SELECT RO_C.name " . | |
781 | "FROM EntityLink EL " . | |
782 | "LEFT JOIN RackObject RO_C ON EL.parent_entity_id = RO_C.id " . | |
783 | "WHERE EL.child_entity_id = RO.id " . | |
784 | "AND EL.parent_entity_type = 'object' " . | |
785 | "AND EL.child_entity_type = 'object' " . | |
786 | "AND RO_C.objtype_id = 1505) AS cluster_name, " . | |
787 | "(SELECT COUNT(*) FROM EntityLink EL " . | |
788 | "LEFT JOIN RackObject RO_VM ON EL.child_entity_id = RO_VM.id " . | |
789 | "WHERE EL.parent_entity_type = 'object' " . | |
790 | "AND EL.child_entity_type = 'object' " . | |
791 | "AND EL.parent_entity_id = RO.id " . | |
792 | "AND RO_VM.objtype_id = 1504) AS VMs " . | |
793 | "FROM RackObject RO " . | |
794 | "LEFT JOIN AttributeValue AV ON RO.id = AV.object_id " . | |
795 | "WHERE RO.objtype_id = 4 " . | |
796 | "AND AV.attr_id = 26 " . | |
797 | "AND AV.uint_value = 1501 " . | |
798 | "ORDER BY RO.name" | |
799 | ); | |
800 | return $result->fetchAll (PDO::FETCH_ASSOC); | |
801 | } | |
802 | ||
803 | function getVMSwitchSummary () | |
804 | { | |
805 | $result = usePreparedSelectBlade | |
806 | ( | |
807 | "SELECT RO.id, RO.name " . | |
808 | "FROM RackObject RO " . | |
809 | "WHERE RO.objtype_id = 1507 " . | |
810 | "ORDER BY RO.name" | |
811 | ); | |
812 | return $result->fetchAll (PDO::FETCH_ASSOC); | |
813 | } | |
814 | ||
6657739e DO |
815 | // Remove file links related to the entity, but leave the entity and file(s) intact. |
816 | function releaseFiles ($entity_realm, $entity_id) | |
817 | { | |
298d2375 | 818 | usePreparedDeleteBlade ('FileLink', array ('entity_type' => $entity_realm, 'entity_id' => $entity_id)); |
6657739e DO |
819 | } |
820 | ||
52b34485 AD |
821 | // There are times when you want to delete all traces of an object |
822 | function commitDeleteObject ($object_id = 0) | |
abd1e9ac DO |
823 | { |
824 | // Reset most of stuff | |
825 | commitResetObject ($object_id); | |
826 | // Object itself | |
827 | usePreparedDeleteBlade ('RackObject', array ('id' => $object_id)); | |
828 | } | |
829 | ||
830 | function commitResetObject ($object_id = 0) | |
52b34485 | 831 | { |
6657739e | 832 | releaseFiles ('object', $object_id); |
29c3a4d8 | 833 | destroyTagsForEntity ('object', $object_id); |
298d2375 DO |
834 | usePreparedDeleteBlade ('IPv4LB', array ('object_id' => $object_id)); |
835 | usePreparedDeleteBlade ('IPv4Allocation', array ('object_id' => $object_id)); | |
8c7b7381 | 836 | usePreparedDeleteBlade ('IPv6Allocation', array ('object_id' => $object_id)); |
298d2375 | 837 | usePreparedDeleteBlade ('IPv4NAT', array ('object_id' => $object_id)); |
06ac80a3 AD |
838 | // Parent-child relationships |
839 | usePreparedExecuteBlade | |
840 | ( | |
841 | 'DELETE FROM EntityLink WHERE ' . | |
842 | "(parent_entity_type = 'object' AND parent_entity_id = ?) OR (child_entity_type = 'object' AND child_entity_id = ?)", | |
843 | array ($object_id, $object_id) | |
844 | ); | |
abd1e9ac | 845 | // Rack space |
39eadd27 DO |
846 | usePreparedExecuteBlade ('DELETE FROM Atom WHERE molecule_id IN (SELECT new_molecule_id FROM MountOperation WHERE object_id = ?)', array ($object_id)); |
847 | usePreparedExecuteBlade ('DELETE FROM Molecule WHERE id IN (SELECT new_molecule_id FROM MountOperation WHERE object_id = ?)', array ($object_id)); | |
68a0bd8a DO |
848 | usePreparedDeleteBlade ('MountOperation', array ('object_id' => $object_id)); |
849 | usePreparedDeleteBlade ('RackSpace', array ('object_id' => $object_id)); | |
abd1e9ac | 850 | // 802.1Q |
56565437 | 851 | usePreparedDeleteBlade ('PortVLANMode', array ('object_id' => $object_id)); |
c6181618 AA |
852 | usePreparedDeleteBlade ('PortNativeVLAN', array ('object_id' => $object_id)); |
853 | usePreparedDeleteBlade ('PortAllowedVLAN', array ('object_id' => $object_id)); | |
abd1e9ac | 854 | usePreparedDeleteBlade ('CachedPVM', array ('object_id' => $object_id)); |
1f54e1ba | 855 | usePreparedDeleteBlade ('VLANSwitch', array ('object_id' => $object_id)); |
abd1e9ac DO |
856 | // Ports & links |
857 | usePreparedDeleteBlade ('Port', array ('object_id' => $object_id)); | |
858 | // CN | |
730479cd | 859 | usePreparedUpdateBlade ('RackObject', array ('name' => NULL, 'label' => ''), array ('id' => $object_id)); |
abd1e9ac DO |
860 | // FQDN |
861 | commitUpdateAttrValue ($object_id, 3, ""); | |
862 | // log history | |
863 | recordHistory ('RackObject', $object_id); | |
52b34485 AD |
864 | } |
865 | ||
c8187437 DY |
866 | function commitDeleteRack($rack_id) |
867 | { | |
6657739e | 868 | releaseFiles ('rack', $rack_id); |
29c3a4d8 | 869 | destroyTagsForEntity ('rack', $rack_id); |
298d2375 DO |
870 | usePreparedDeleteBlade ('RackSpace', array ('rack_id' => $rack_id)); |
871 | usePreparedDeleteBlade ('RackHistory', array ('id' => $rack_id)); | |
872 | usePreparedDeleteBlade ('Rack', array ('id' => $rack_id)); | |
c8187437 DY |
873 | return TRUE; |
874 | } | |
875 | ||
e673ee24 DO |
876 | function commitUpdateRack ($rack_id, $new_name, $new_height, $new_row_id, $new_comment) |
877 | { | |
7d70d643 | 878 | // Can't shrink a rack if rows being deleted contain mounted objects |
298d2375 | 879 | $check_result = usePreparedSelectBlade ('SELECT COUNT(*) AS count FROM RackSpace WHERE rack_id = ? AND unit_no > ?', array ($rack_id, $new_height)); |
7d70d643 | 880 | $check_row = $check_result->fetch (PDO::FETCH_ASSOC); |
de9067c2 | 881 | unset ($check_result); |
39eadd27 | 882 | if ($check_row['count'] > 0) |
0cc24e9a | 883 | throw new InvalidArgException ('new_height', $new_height, 'Cannot shrink rack, objects are still mounted there'); |
730479cd | 884 | usePreparedUpdateBlade |
39eadd27 | 885 | ( |
730479cd DO |
886 | 'Rack', |
887 | array | |
888 | ( | |
889 | 'name' => $new_name, | |
890 | 'height' => $new_height, | |
891 | 'comment' => $new_comment, | |
892 | 'row_id' => $new_row_id, | |
893 | ), | |
39eadd27 DO |
894 | array |
895 | ( | |
730479cd | 896 | 'id' => $rack_id, |
39eadd27 DO |
897 | ) |
898 | ); | |
32832c0e | 899 | return recordHistory ('Rack', $rack_id); |
e673ee24 DO |
900 | } |
901 | ||
61a1d996 | 902 | // This function accepts rack data returned by amplifyCell(), validates and applies changes |
e673ee24 DO |
903 | // supplied in $_REQUEST and returns resulting array. Only those changes are examined, which |
904 | // correspond to current rack ID. | |
905 | // 1st arg is rackdata, 2nd arg is unchecked state, 3rd arg is checked state. | |
906 | // If 4th arg is present, object_id fields will be updated accordingly to the new state. | |
907 | // The function returns the modified rack upon success. | |
908 | function processGridForm (&$rackData, $unchecked_state, $checked_state, $object_id = 0) | |
909 | { | |
32832c0e | 910 | global $loclist; |
e673ee24 DO |
911 | $rack_id = $rackData['id']; |
912 | $rack_name = $rackData['name']; | |
913 | $rackchanged = FALSE; | |
914 | for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--) | |
915 | { | |
916 | for ($locidx = 0; $locidx < 3; $locidx++) | |
917 | { | |
918 | if ($rackData[$unit_no][$locidx]['enabled'] != TRUE) | |
919 | continue; | |
920 | // detect a change | |
921 | $state = $rackData[$unit_no][$locidx]['state']; | |
922 | if (isset ($_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"]) and $_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"] == 'on') | |
923 | $newstate = $checked_state; | |
924 | else | |
925 | $newstate = $unchecked_state; | |
926 | if ($state == $newstate) | |
927 | continue; | |
928 | $rackchanged = TRUE; | |
929 | // and validate | |
930 | $atom = $loclist[$locidx]; | |
931 | // The only changes allowed are those introduced by checkbox grid. | |
932 | if | |
933 | ( | |
934 | !($state == $checked_state && $newstate == $unchecked_state) && | |
935 | !($state == $unchecked_state && $newstate == $checked_state) | |
936 | ) | |
937 | return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, 'atom ${atom}', cannot change state from '${state}' to '${newstate}'"); | |
938 | // Here we avoid using ON DUPLICATE KEY UPDATE by first performing DELETE | |
939 | // anyway and then looking for probable need of INSERT. | |
32832c0e | 940 | usePreparedDeleteBlade ('RackSpace', array ('rack_id' => $rack_id, 'unit_no' => $unit_no, 'atom' => $atom)); |
e673ee24 | 941 | if ($newstate != 'F') |
32832c0e | 942 | usePreparedInsertBlade ('RackSpace', array ('rack_id' => $rack_id, 'unit_no' => $unit_no, 'atom' => $atom, 'state' => $newstate)); |
e673ee24 DO |
943 | if ($newstate == 'T' and $object_id != 0) |
944 | { | |
945 | // At this point we already have a record in RackSpace. | |
68a0bd8a | 946 | $r = usePreparedUpdateBlade |
32832c0e | 947 | ( |
68a0bd8a DO |
948 | 'RackSpace', |
949 | array ('object_id' => $object_id), | |
950 | array | |
951 | ( | |
952 | 'rack_id' => $rack_id, | |
953 | 'unit_no' => $unit_no, | |
954 | 'atom' => $atom, | |
955 | ) | |
32832c0e DO |
956 | ); |
957 | if ($r === FALSE) | |
e673ee24 | 958 | return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, atom '${atom}' failed to set object_id to '${object_id}'"); |
32832c0e | 959 | $rackData[$unit_no][$locidx]['object_id'] = $object_id; |
e673ee24 DO |
960 | } |
961 | } | |
962 | } | |
963 | if ($rackchanged) | |
c7fe33be | 964 | { |
68a0bd8a | 965 | usePreparedUpdateBlade ('Rack', array ('thumb_data' => NULL), array ('id' => $rack_id)); |
e673ee24 | 966 | return array ('code' => 200, 'message' => "${rack_name}: All changes were successfully saved."); |
c7fe33be | 967 | } |
e673ee24 DO |
968 | else |
969 | return array ('code' => 300, 'message' => "${rack_name}: No changes."); | |
970 | } | |
971 | ||
972 | // This function builds a list of rack-unit-atom records, which are assigned to | |
973 | // the requested object. | |
298d2375 | 974 | function getMoleculeForObject ($object_id) |
e673ee24 | 975 | { |
298d2375 DO |
976 | $result = usePreparedSelectBlade |
977 | ( | |
978 | 'SELECT rack_id, unit_no, atom FROM RackSpace ' . | |
979 | 'WHERE state = "T" AND object_id = ? ORDER BY rack_id, unit_no, atom', | |
980 | array ($object_id) | |
981 | ); | |
982 | return $result->fetchAll (PDO::FETCH_ASSOC); | |
e673ee24 DO |
983 | } |
984 | ||
985 | // This function builds a list of rack-unit-atom records for requested molecule. | |
986 | function getMolecule ($mid = 0) | |
987 | { | |
298d2375 DO |
988 | $result = usePreparedSelectBlade ('SELECT rack_id, unit_no, atom FROM Atom WHERE molecule_id = ?', array ($mid)); |
989 | return $result->fetchAll (PDO::FETCH_ASSOC); | |
e673ee24 DO |
990 | } |
991 | ||
c63a8d6e DO |
992 | // returns exactly what is's named after |
993 | function lastInsertID () | |
994 | { | |
a685e6d7 | 995 | $result = usePreparedSelectBlade ('select last_insert_id()'); |
c63a8d6e DO |
996 | $row = $result->fetch (PDO::FETCH_NUM); |
997 | return $row[0]; | |
998 | } | |
999 | ||
e673ee24 DO |
1000 | // This function creates a new record in Molecule and number of linked |
1001 | // R-U-A records in Atom. | |
1002 | function createMolecule ($molData) | |
1003 | { | |
32832c0e | 1004 | usePreparedExecuteBlade ('INSERT INTO Molecule VALUES()'); |
c63a8d6e | 1005 | $molecule_id = lastInsertID(); |
64b95774 | 1006 | foreach ($molData as $rua) |
298d2375 DO |
1007 | usePreparedInsertBlade |
1008 | ( | |
1009 | 'Atom', | |
1010 | array | |
1011 | ( | |
1012 | 'molecule_id' => $molecule_id, | |
1013 | 'rack_id' => $rua['rack_id'], | |
1014 | 'unit_no' => $rua['unit_no'], | |
1015 | 'atom' => $rua['atom'], | |
1016 | ) | |
1017 | ); | |
e673ee24 DO |
1018 | return $molecule_id; |
1019 | } | |
1020 | ||
1021 | // History logger. This function assumes certain table naming convention and | |
1022 | // column design: | |
1023 | // 1. History table name equals to dictionary table name plus 'History'. | |
1024 | // 2. History table must have the same row set (w/o keys) plus one row named | |
1025 | // 'ctime' of type 'timestamp'. | |
32832c0e | 1026 | function recordHistory ($tableName, $orig_id) |
e673ee24 | 1027 | { |
32832c0e DO |
1028 | global $remote_username; |
1029 | return FALSE !== usePreparedExecuteBlade | |
1030 | ( | |
1031 | "INSERT INTO ${tableName}History SELECT *, CURRENT_TIMESTAMP(), ? " . | |
1032 | "FROM ${tableName} WHERE id=?", | |
1033 | array ($remote_username, $orig_id) | |
1034 | ); | |
e673ee24 DO |
1035 | } |
1036 | ||
1037 | function getRackspaceHistory () | |
1038 | { | |
298d2375 DO |
1039 | $result = usePreparedSelectBlade |
1040 | ( | |
7fa7047a | 1041 | "SELECT id as mo_id, object_id as ro_id, ctime, comment, user_name FROM " . |
298d2375 DO |
1042 | "MountOperation ORDER BY ctime DESC" |
1043 | ); | |
7fa7047a | 1044 | return $result->fetchAll (PDO::FETCH_ASSOC); |
e673ee24 DO |
1045 | } |
1046 | ||
1047 | // This function is used in renderRackspaceHistory() | |
1048 | function getOperationMolecules ($op_id = 0) | |
1049 | { | |
298d2375 | 1050 | $result = usePreparedSelectBlade ('SELECT old_molecule_id, new_molecule_id FROM MountOperation WHERE id = ?', array ($op_id)); |
e673ee24 DO |
1051 | // We expect one row. |
1052 | $row = $result->fetch (PDO::FETCH_ASSOC); | |
298d2375 | 1053 | return array ($row['old_molecule_id'], $row['new_molecule_id']); |
e673ee24 DO |
1054 | } |
1055 | ||
c7fe33be | 1056 | function getResidentRacksData ($object_id = 0, $fetch_rackdata = TRUE) |
e673ee24 | 1057 | { |
298d2375 | 1058 | $result = usePreparedSelectBlade ('SELECT DISTINCT rack_id FROM RackSpace WHERE object_id = ? ORDER BY rack_id', array ($object_id)); |
e673ee24 | 1059 | $rows = $result->fetchAll (PDO::FETCH_NUM); |
61a1d996 | 1060 | unset ($result); |
e673ee24 DO |
1061 | $ret = array(); |
1062 | foreach ($rows as $row) | |
1063 | { | |
b422aee6 DO |
1064 | if (!$fetch_rackdata) |
1065 | { | |
1066 | $ret[$row[0]] = $row[0]; | |
1067 | continue; | |
1068 | } | |
0cc24e9a | 1069 | $rackData = spotEntity ('rack', $row[0]); |
61a1d996 | 1070 | amplifyCell ($rackData); |
e673ee24 DO |
1071 | $ret[$row[0]] = $rackData; |
1072 | } | |
e673ee24 DO |
1073 | return $ret; |
1074 | } | |
1075 | ||
e673ee24 DO |
1076 | function commitAddPort ($object_id = 0, $port_name, $port_type_id, $port_label, $port_l2address) |
1077 | { | |
9b6e7bd1 | 1078 | $db_l2address = l2addressForDatabase ($port_l2address); |
029a14bc DO |
1079 | global $dbxlink; |
1080 | $dbxlink->exec ('LOCK TABLES Port WRITE'); | |
1081 | if (alreadyUsedL2Address ($db_l2address, $object_id)) | |
1082 | { | |
1083 | $dbxlink->exec ('UNLOCK TABLES'); | |
73a9a0a0 | 1084 | throw new InvalidRequestArgException ('port_l2address', $port_l2address, 'address belongs to another object'); |
029a14bc | 1085 | } |
3153a326 DO |
1086 | $matches = array(); |
1087 | switch (1) | |
1088 | { | |
1089 | case preg_match ('/^([[:digit:]]+)-([[:digit:]]+)$/', $port_type_id, $matches): | |
1090 | $iif_id = $matches[1]; | |
1091 | $oif_id = $matches[2]; | |
1092 | break; | |
1093 | case preg_match ('/^([[:digit:]]+)$/', $port_type_id, $matches): | |
1094 | $iif_id = 1; | |
1095 | $oif_id = $matches[1]; | |
1096 | break; | |
1097 | default: | |
1098 | $dbxlink->exec ('UNLOCK TABLES'); | |
73a9a0a0 | 1099 | throw new InvalidArgException ('port_type_id', $port_type_id, 'format error'); |
3153a326 | 1100 | } |
298d2375 | 1101 | $result = usePreparedInsertBlade |
e673ee24 DO |
1102 | ( |
1103 | 'Port', | |
1104 | array | |
1105 | ( | |
298d2375 | 1106 | 'name' => $port_name, |
3153a326 | 1107 | 'object_id' => $object_id, |
298d2375 | 1108 | 'label' => $port_label, |
3153a326 DO |
1109 | 'iif_id' => $iif_id, |
1110 | 'type' => $oif_id, | |
298d2375 | 1111 | 'l2address' => ($db_l2address === '') ? NULL : $db_l2address, |
e673ee24 DO |
1112 | ) |
1113 | ); | |
029a14bc | 1114 | $dbxlink->exec ('UNLOCK TABLES'); |
e673ee24 DO |
1115 | } |
1116 | ||
d0a69ce8 DO |
1117 | // The fifth argument may be either explicit 'NULL' or some (already quoted by the upper layer) |
1118 | // string value. In case it is omitted, we just assign it its current value. | |
1119 | // It would be nice to simplify this semantics later. | |
995e7d5b | 1120 | function commitUpdatePort ($object_id, $port_id, $port_name, $port_type_id, $port_label, $port_l2address, $port_reservation_comment) |
e673ee24 | 1121 | { |
9b6e7bd1 | 1122 | $db_l2address = l2addressForDatabase ($port_l2address); |
029a14bc DO |
1123 | global $dbxlink; |
1124 | $dbxlink->exec ('LOCK TABLES Port WRITE'); | |
1125 | if (alreadyUsedL2Address ($db_l2address, $object_id)) | |
1126 | { | |
1127 | $dbxlink->exec ('UNLOCK TABLES'); | |
d4fc0181 DO |
1128 | // FIXME: it is more correct to throw InvalidArgException here |
1129 | // and convert it to InvalidRequestArgException at upper level, | |
1130 | // when there is a mean to do that. | |
1131 | throw new InvalidRequestArgException ('port_l2address', $db_l2address, 'address belongs to another object'); | |
029a14bc | 1132 | } |
d4fc0181 | 1133 | usePreparedUpdateBlade |
a5c589d2 | 1134 | ( |
d4fc0181 | 1135 | 'Port', |
a5c589d2 DO |
1136 | array |
1137 | ( | |
d4fc0181 DO |
1138 | 'name' => $port_name, |
1139 | 'type' => $port_type_id, | |
1140 | 'label' => $port_label, | |
1141 | 'reservation_comment' => mb_strlen ($port_reservation_comment) ? $port_reservation_comment : NULL, | |
1142 | 'l2address' => ($db_l2address === '') ? NULL : $db_l2address, | |
1143 | ), | |
1144 | array | |
1145 | ( | |
1146 | 'id' => $port_id, | |
1147 | 'object_id' => $object_id | |
a5c589d2 DO |
1148 | ) |
1149 | ); | |
029a14bc | 1150 | $dbxlink->exec ('UNLOCK TABLES'); |
e673ee24 DO |
1151 | } |
1152 | ||
6ef9683b | 1153 | function getAllIPv4Allocations () |
e673ee24 | 1154 | { |
298d2375 DO |
1155 | $result = usePreparedSelectBlade |
1156 | ( | |
e673ee24 DO |
1157 | "select object_id as object_id, ". |
1158 | "RackObject.name as object_name, ". | |
706ce117 | 1159 | "IPv4Allocation.name as name, ". |
e673ee24 | 1160 | "INET_NTOA(ip) as ip ". |
298d2375 DO |
1161 | "from IPv4Allocation join RackObject on id=object_id " |
1162 | ); | |
e673ee24 | 1163 | $ret = array(); |
e673ee24 | 1164 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
298d2375 | 1165 | $ret[] = $row; |
e673ee24 DO |
1166 | return $ret; |
1167 | } | |
1168 | ||
0c7c9f8b | 1169 | function linkPorts ($porta, $portb, $cable = NULL) |
e673ee24 DO |
1170 | { |
1171 | if ($porta == $portb) | |
32832c0e | 1172 | throw new InvalidArgException ('porta/portb', $porta, "Ports can't be the same"); |
971619ad AD |
1173 | |
1174 | global $dbxlink; | |
1175 | $dbxlink->exec ('LOCK TABLES Link WRITE'); | |
1176 | $result = usePreparedSelectBlade | |
1177 | ( | |
1178 | 'SELECT COUNT(*) FROM Link WHERE porta IN (?,?) OR portb IN (?,?)', | |
1179 | array ($porta, $portb, $porta, $portb) | |
1180 | ); | |
1181 | if ($result->fetchColumn () != 0) | |
1182 | { | |
1183 | $dbxlink->exec ('UNLOCK TABLES'); | |
1184 | return "Port ${porta} or ${portb} is already linked"; | |
1185 | } | |
46aa3dbe | 1186 | $result->closeCursor (); |
e673ee24 DO |
1187 | if ($porta > $portb) |
1188 | { | |
1189 | $tmp = $porta; | |
1190 | $porta = $portb; | |
1191 | $portb = $tmp; | |
1192 | } | |
0c7c9f8b | 1193 | $ret = FALSE !== usePreparedInsertBlade ('Link', array ('porta' => $porta, 'portb' => $portb, 'cable' => $cable)); |
971619ad | 1194 | $dbxlink->exec ('UNLOCK TABLES'); |
32832c0e DO |
1195 | $ret = $ret and FALSE !== usePreparedExecuteBlade |
1196 | ( | |
1197 | 'UPDATE Port SET reservation_comment=NULL WHERE id IN(?, ?)', | |
1198 | array ($porta, $portb) | |
1199 | ); | |
1200 | return $ret ? '' : 'query failed'; | |
e673ee24 DO |
1201 | } |
1202 | ||
2c817354 DO |
1203 | // Return all IPv4 addresses allocated to the objects. Attach detailed |
1204 | // info about address to each alocation records. Index result by dotted-quad | |
1205 | // address. | |
0ab782bc DO |
1206 | function getObjectIPv4Allocations ($object_id = 0) |
1207 | { | |
1208 | $ret = array(); | |
298d2375 DO |
1209 | $result = usePreparedSelectBlade |
1210 | ( | |
1211 | 'SELECT name AS osif, type, inet_ntoa(ip) AS dottedquad FROM IPv4Allocation ' . | |
1212 | 'WHERE object_id = ? ORDER BY ip', | |
1213 | array ($object_id) | |
650ef981 | 1214 | ); |
85970da2 | 1215 | // don't spawn a sub-query with unfetched buffer, it may fail |
0ab782bc | 1216 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
85970da2 DO |
1217 | $ret[$row['dottedquad']] = array ('osif' => $row['osif'], 'type' => $row['type']); |
1218 | unset ($result); | |
1219 | foreach (array_keys ($ret) as $dottedquad) | |
1220 | $ret[$dottedquad]['addrinfo'] = getIPv4Address ($dottedquad); | |
0ab782bc DO |
1221 | return $ret; |
1222 | } | |
1223 | ||
21ee3351 AA |
1224 | // Return all IPv6 addresses allocated to the objects. Attach detailed |
1225 | // info about address to each alocation records. Index result by binary string of IPv6 | |
1226 | function getObjectIPv6Allocations ($object_id = 0) | |
1227 | { | |
1228 | $ret = array(); | |
1229 | $result = usePreparedSelectBlade | |
1230 | ( | |
1231 | 'SELECT name AS osif, type, ip AS ip FROM IPv6Allocation ' . | |
1232 | 'WHERE object_id = ? ORDER BY ip', | |
1233 | array ($object_id) | |
1234 | ); | |
1235 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
1236 | $ret[$row['ip']] = array ('osif' => $row['osif'], 'type' => $row['type']); | |
1237 | unset ($result); | |
1238 | foreach (array_keys ($ret) as $ip_bin) | |
1239 | $ret[$ip_bin]['addrinfo'] = getIPv6Address (new IPv6Address ($ip_bin)); | |
1240 | return $ret; | |
1241 | } | |
1242 | ||
e6ca609a DO |
1243 | // Return minimal IPv4 address, optionally with "ip" key set, if requested. |
1244 | function constructIPv4Address ($dottedquad = NULL) | |
a910829d | 1245 | { |
e6ca609a | 1246 | $ret = array |
a910829d | 1247 | ( |
21ee3351 | 1248 | 'version' => 4, |
a910829d DO |
1249 | 'name' => '', |
1250 | 'reserved' => 'no', | |
a910829d DO |
1251 | 'outpf' => array(), |
1252 | 'inpf' => array(), | |
a910829d | 1253 | 'rslist' => array(), |
2d318652 | 1254 | 'allocs' => array(), |
a910829d DO |
1255 | 'lblist' => array() |
1256 | ); | |
e6ca609a DO |
1257 | if ($dottedquad != NULL) |
1258 | $ret['ip'] = $dottedquad; | |
1259 | return $ret; | |
1260 | } | |
1261 | ||
21ee3351 AA |
1262 | // Return minimal IPv6 address, optionally with "ip" key set, if requested. |
1263 | function constructIPv6Address ($bin_ip = NULL) | |
1264 | { | |
1265 | $ret = array | |
1266 | ( | |
1267 | 'version' => 6, | |
1268 | 'name' => '', | |
1269 | 'reserved' => 'no', | |
1270 | 'allocs' => array(), | |
1271 | ); | |
1272 | if ($bin_ip != NULL) | |
1273 | $ret['ip'] = $bin_ip->format(); | |
1274 | return $ret; | |
1275 | } | |
1276 | ||
e6ca609a DO |
1277 | // Check the range requested for meaningful IPv4 records, build them |
1278 | // into a list and return. Return an empty list if nothing matched. | |
1279 | // Both arguments are expected in signed int32 form. The resulting list | |
1280 | // is keyed by uint32 form of each IP address, items aren't sorted. | |
f7414fa5 DO |
1281 | // LATER: accept a list of pairs and build WHERE sub-expression accordingly |
1282 | function scanIPv4Space ($pairlist) | |
e6ca609a DO |
1283 | { |
1284 | $ret = array(); | |
f7414fa5 | 1285 | if (!count ($pairlist)) // this is normal for a network completely divided into smaller parts |
178fda20 | 1286 | return $ret; |
f7414fa5 DO |
1287 | // FIXME: this is a copy-and-paste prototype |
1288 | $or = ''; | |
1289 | $whereexpr1 = '('; | |
1290 | $whereexpr2 = '('; | |
1291 | $whereexpr3 = '('; | |
1292 | $whereexpr4 = '('; | |
1293 | $whereexpr5a = '('; | |
1294 | $whereexpr5b = '('; | |
a5c589d2 | 1295 | $qparams = array(); |
f7414fa5 DO |
1296 | foreach ($pairlist as $tmp) |
1297 | { | |
1298 | $db_first = sprintf ('%u', 0x00000000 + $tmp['i32_first']); | |
1299 | $db_last = sprintf ('%u', 0x00000000 + $tmp['i32_last']); | |
a5c589d2 DO |
1300 | $whereexpr1 .= $or . "ip between ? and ?"; |
1301 | $whereexpr2 .= $or . "ip between ? and ?"; | |
1302 | $whereexpr3 .= $or . "vip between ? and ?"; | |
1303 | $whereexpr4 .= $or . "rsip between ? and ?"; | |
1304 | $whereexpr5a .= $or . "remoteip between ? and ?"; | |
1305 | $whereexpr5b .= $or . "localip between ? and ?"; | |
f7414fa5 | 1306 | $or = ' or '; |
a5c589d2 DO |
1307 | $qparams[] = $db_first; |
1308 | $qparams[] = $db_last; | |
f7414fa5 DO |
1309 | } |
1310 | $whereexpr1 .= ')'; | |
1311 | $whereexpr2 .= ')'; | |
1312 | $whereexpr3 .= ')'; | |
1313 | $whereexpr4 .= ')'; | |
1314 | $whereexpr5a .= ')'; | |
1315 | $whereexpr5b .= ')'; | |
a910829d DO |
1316 | |
1317 | // 1. collect labels and reservations | |
706ce117 | 1318 | $query = "select INET_NTOA(ip) as ip, name, reserved from IPv4Address ". |
f7414fa5 | 1319 | "where ${whereexpr1} and (reserved = 'yes' or name != '')"; |
a5c589d2 | 1320 | $result = usePreparedSelectBlade ($query, $qparams); |
a910829d DO |
1321 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
1322 | { | |
2d318652 DO |
1323 | $ip_bin = ip2long ($row['ip']); |
1324 | if (!isset ($ret[$ip_bin])) | |
e6ca609a | 1325 | $ret[$ip_bin] = constructIPv4Address ($row['ip']); |
2d318652 DO |
1326 | $ret[$ip_bin]['name'] = $row['name']; |
1327 | $ret[$ip_bin]['reserved'] = $row['reserved']; | |
a910829d DO |
1328 | } |
1329 | unset ($result); | |
1330 | ||
1331 | // 2. check for allocations | |
1332 | $query = | |
a1f97745 DO |
1333 | "select INET_NTOA(ip) as ip, object_id, name, type " . |
1334 | "from IPv4Allocation where ${whereexpr2} order by type"; | |
a5c589d2 | 1335 | $result = usePreparedSelectBlade ($query, $qparams); |
a1f97745 DO |
1336 | // release DBX early to avoid issues with nested spotEntity() calls |
1337 | $allRows = $result->fetchAll (PDO::FETCH_ASSOC); | |
1338 | unset ($result); | |
1339 | foreach ($allRows as $row) | |
a910829d | 1340 | { |
2d318652 DO |
1341 | $ip_bin = ip2long ($row['ip']); |
1342 | if (!isset ($ret[$ip_bin])) | |
e6ca609a | 1343 | $ret[$ip_bin] = constructIPv4Address ($row['ip']); |
a1f97745 DO |
1344 | $oinfo = spotEntity ('object', $row['object_id']); |
1345 | $ret[$ip_bin]['allocs'][] = array | |
1346 | ( | |
1347 | 'type' => $row['type'], | |
1348 | 'name' => $row['name'], | |
1349 | 'object_id' => $row['object_id'], | |
1350 | 'object_name' => $oinfo['dname'], | |
1351 | ); | |
a910829d | 1352 | } |
a910829d DO |
1353 | |
1354 | // 3. look for virtual services and related LB | |
a1f97745 DO |
1355 | $query = "select vs_id, inet_ntoa(vip) as ip, vport, proto, vs.name, object_id " . |
1356 | "from IPv4VS as vs inner join IPv4LB as lb on vs.id = lb.vs_id " . | |
1357 | "where ${whereexpr3} order by vport, proto, object_id"; | |
a5c589d2 | 1358 | $result = usePreparedSelectBlade ($query, $qparams); |
a1f97745 DO |
1359 | $allRows = $result->fetchAll (PDO::FETCH_ASSOC); |
1360 | unset ($result); | |
1361 | foreach ($allRows as $row) | |
a910829d | 1362 | { |
2d318652 | 1363 | $ip_bin = ip2long ($row['ip']); |
f3d0cb20 | 1364 | if (!isset ($ret[$ip_bin])) |
e6ca609a | 1365 | $ret[$ip_bin] = constructIPv4Address ($row['ip']); |
a1f97745 DO |
1366 | $oinfo = spotEntity ('object', $row['object_id']); |
1367 | $ret[$ip_bin]['lblist'][] = array | |
1368 | ( | |
1369 | 'vport' => $row['vport'], | |
1370 | 'proto' => $row['proto'], | |
1371 | 'vs_id' => $row['vs_id'], | |
1372 | 'name' => $row['name'], | |
1373 | 'vip' => $row['ip'], | |
1374 | 'object_id' => $row['object_id'], | |
1375 | 'object_name' => $oinfo['dname'], | |
1376 | ); | |
a910829d | 1377 | } |
a910829d DO |
1378 | |
1379 | // 4. don't forget about real servers along with pools | |
2d318652 | 1380 | $query = "select inet_ntoa(rsip) as ip, inservice, rsport, rspool_id, rsp.name as rspool_name from " . |
706ce117 | 1381 | "IPv4RS as rs inner join IPv4RSPool as rsp on rs.rspool_id = rsp.id " . |
f7414fa5 | 1382 | "where ${whereexpr4} " . |
a910829d | 1383 | "order by ip, rsport, rspool_id"; |
a5c589d2 | 1384 | $result = usePreparedSelectBlade ($query, $qparams); |
a910829d DO |
1385 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
1386 | { | |
2d318652 DO |
1387 | $ip_bin = ip2long ($row['ip']); |
1388 | if (!isset ($ret[$ip_bin])) | |
e6ca609a | 1389 | $ret[$ip_bin] = constructIPv4Address ($row['ip']); |
a910829d DO |
1390 | $tmp = array(); |
1391 | foreach (array ('rspool_id', 'rsport', 'rspool_name', 'inservice') as $cname) | |
1392 | $tmp[$cname] = $row[$cname]; | |
2d318652 | 1393 | $ret[$ip_bin]['rslist'][] = $tmp; |
a910829d DO |
1394 | } |
1395 | unset ($result); | |
1396 | ||
1397 | // 5. add NAT rules, part 1 | |
1398 | $query = | |
1399 | "select " . | |
1400 | "proto, " . | |
1401 | "INET_NTOA(localip) as localip, " . | |
1402 | "localport, " . | |
1403 | "INET_NTOA(remoteip) as remoteip, " . | |
1404 | "remoteport, " . | |
1405 | "description " . | |
706ce117 | 1406 | "from IPv4NAT " . |
51332e78 | 1407 | "where ${whereexpr5a} " . |
a910829d | 1408 | "order by localip, localport, remoteip, remoteport, proto"; |
a5c589d2 | 1409 | $result = usePreparedSelectBlade ($query, $qparams); |
a910829d DO |
1410 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
1411 | { | |
2d318652 | 1412 | $remoteip_bin = ip2long ($row['remoteip']); |
51332e78 DO |
1413 | if (!isset ($ret[$remoteip_bin])) |
1414 | $ret[$remoteip_bin] = constructIPv4Address ($row['remoteip']); | |
1415 | $ret[$remoteip_bin]['inpf'][] = $row; | |
1416 | } | |
1417 | unset ($result); | |
1418 | // 5. add NAT rules, part 2 | |
1419 | $query = | |
1420 | "select " . | |
1421 | "proto, " . | |
1422 | "INET_NTOA(localip) as localip, " . | |
1423 | "localport, " . | |
1424 | "INET_NTOA(remoteip) as remoteip, " . | |
1425 | "remoteport, " . | |
1426 | "description " . | |
706ce117 | 1427 | "from IPv4NAT " . |
51332e78 DO |
1428 | "where ${whereexpr5b} " . |
1429 | "order by localip, localport, remoteip, remoteport, proto"; | |
a5c589d2 | 1430 | $result = usePreparedSelectBlade ($query, $qparams); |
51332e78 DO |
1431 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
1432 | { | |
2d318652 | 1433 | $localip_bin = ip2long ($row['localip']); |
51332e78 DO |
1434 | if (!isset ($ret[$localip_bin])) |
1435 | $ret[$localip_bin] = constructIPv4Address ($row['localip']); | |
1436 | $ret[$localip_bin]['outpf'][] = $row; | |
a910829d | 1437 | } |
51332e78 | 1438 | unset ($result); |
a910829d DO |
1439 | return $ret; |
1440 | } | |
1441 | ||
21ee3351 AA |
1442 | // Check the range requested for meaningful IPv6 records, build them |
1443 | // into a list and return. Return an empty list if nothing matched. | |
1444 | // Both arguments are expected as instances of IPv6Address class. The resulting list | |
1445 | // is keyed by uint32 form of each IP address, items aren't sorted. | |
1446 | function scanIPv6Space ($pairlist) | |
1447 | { | |
1448 | $ret = array(); | |
1449 | $wheres = array(); | |
1450 | foreach ($pairlist as $pair) | |
1451 | { | |
1452 | $wheres[] = "ip >= ? AND ip <= ?"; | |
522b6f90 AA |
1453 | $qparams[] = $pair['first']->getBin(); |
1454 | $qparams[] = $pair['last']->getBin(); | |
21ee3351 AA |
1455 | } |
1456 | if (! count ($wheres)) // this is normal for a network completely divided into smaller parts | |
1457 | return $ret; | |
1458 | $whereexpr = '(' .implode (' OR ', $wheres) . ')'; | |
1459 | ||
1460 | // 1. collect labels and reservations | |
1461 | $query = "select ip, name, reserved from IPv6Address ". | |
1462 | "where ${whereexpr} and (reserved = 'yes' or name != '')"; | |
1463 | $result = usePreparedSelectBlade ($query, $qparams); | |
1464 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
1465 | { | |
1466 | $ip_bin = new IPv6Address ($row['ip']); | |
522b6f90 | 1467 | $key = $ip_bin->getBin(); |
21ee3351 AA |
1468 | if (!isset ($ret[$key])) |
1469 | $ret[$key] = constructIPv6Address ($ip_bin); | |
1470 | $ret[$key]['name'] = $row['name']; | |
1471 | $ret[$key]['reserved'] = $row['reserved']; | |
1472 | } | |
1473 | unset ($result); | |
1474 | ||
1475 | // 2. check for allocations | |
1476 | $query = | |
1477 | "select ip, object_id, name, type " . | |
1478 | "from IPv6Allocation where ${whereexpr} order by type"; | |
1479 | $result = usePreparedSelectBlade ($query, $qparams); | |
1480 | // release DBX early to avoid issues with nested spotEntity() calls | |
1481 | $allRows = $result->fetchAll (PDO::FETCH_ASSOC); | |
1482 | unset ($result); | |
1483 | foreach ($allRows as $row) | |
1484 | { | |
1485 | $ip_bin = new IPv6Address ($row['ip']); | |
522b6f90 | 1486 | $key = $ip_bin->getBin(); |
21ee3351 AA |
1487 | if (!isset ($ret[$key])) |
1488 | $ret[$key] = constructIPv6Address ($ip_bin); | |
1489 | $oinfo = spotEntity ('object', $row['object_id']); | |
1490 | $ret[$key]['allocs'][] = array | |
1491 | ( | |
1492 | 'type' => $row['type'], | |
1493 | 'name' => $row['name'], | |
1494 | 'object_id' => $row['object_id'], | |
1495 | 'object_name' => $oinfo['dname'], | |
1496 | ); | |
1497 | } | |
1498 | return $ret; | |
1499 | } | |
1500 | ||
1501 | // this is a wrapper around getIPv4Address and getIPv6Address | |
1502 | // You can pass dotted IPv4, human representation of IPv6, or instance of IPv6Address | |
1503 | function getIPAddress ($ip) | |
1504 | { | |
1505 | if (is_a ($ip, 'IPv6Address')) | |
1506 | return getIPv6Address ($ip); | |
1507 | $ipv6 = new IPv6Address; | |
1508 | if ($ipv6->parse ($ip)) | |
1509 | return getIPv6Address ($ipv6); | |
1510 | return getIPv4Address ($ip); | |
1511 | } | |
1512 | ||
2d318652 DO |
1513 | function getIPv4Address ($dottedquad = '') |
1514 | { | |
1515 | if ($dottedquad == '') | |
c5f84f48 | 1516 | throw new InvalidArgException ('$dottedquad', $dottedquad); |
2d318652 | 1517 | $i32 = ip2long ($dottedquad); // signed 32 bit |
f7414fa5 | 1518 | $scanres = scanIPv4Space (array (array ('i32_first' => $i32, 'i32_last' => $i32))); |
21ee3351 | 1519 | if (empty ($scanres)) |
8b980e24 | 1520 | //$scanres[$i32] = constructIPv4Address ($dottedquad); // XXX: this should be verified to not break things |
e6ca609a | 1521 | return constructIPv4Address ($dottedquad); |
21ee3351 | 1522 | markupIPAddrList ($scanres); |
2d318652 | 1523 | return $scanres[$i32]; |
a910829d DO |
1524 | } |
1525 | ||
21ee3351 AA |
1526 | // returns the array of structure described by constructIPv6Address |
1527 | function getIPv6Address ($v6addr) | |
1528 | { | |
1529 | if (! is_object ($v6addr)) | |
1530 | throw new InvalidArgException ('$v6addr', $v6addr); | |
1531 | $scanres = scanIPv6Space (array (array ('first' => $v6addr, 'last' => $v6addr))); | |
1532 | if (empty ($scanres)) | |
1533 | return constructIPv6Address ($v6addr); | |
1534 | markupIPAddrList ($scanres); | |
1535 | return array_shift ($scanres); | |
1536 | } | |
1537 | ||
5222f192 DO |
1538 | function bindIpToObject ($ip = '', $object_id = 0, $name = '', $type = '') |
1539 | { | |
fc2e1602 | 1540 | return usePreparedExecuteBlade |
5222f192 | 1541 | ( |
a5c589d2 DO |
1542 | 'INSERT INTO IPv4Allocation (ip, object_id, name, type) VALUES (INET_ATON(?), ?, ?, ?)', |
1543 | array ($ip, $object_id, $name, $type) | |
5222f192 | 1544 | ); |
5222f192 DO |
1545 | } |
1546 | ||
522b6f90 | 1547 | function bindIPv6ToObject ($ip, $object_id = 0, $name = '', $type = '') |
21ee3351 | 1548 | { |
68a0bd8a | 1549 | return usePreparedInsertBlade |
21ee3351 | 1550 | ( |
68a0bd8a | 1551 | 'IPv6Allocation', |
522b6f90 | 1552 | array ('ip' => $ip->getBin(), 'object_id' => $object_id, 'name' => $name, 'type' => $type) |
21ee3351 AA |
1553 | ); |
1554 | } | |
1555 | ||
bb0a44e9 | 1556 | // Return the id of the smallest IPv4 network containing the given IPv4 address |
d65353ad DO |
1557 | // or NULL, if nothing was found. When finding the covering network for |
1558 | // another network, it is important to filter out matched records with longer | |
1559 | // masks (they aren't going to be the right pick). | |
1560 | function getIPv4AddressNetworkId ($dottedquad, $masklen = 32) | |
e673ee24 | 1561 | { |
706ce117 | 1562 | $query = 'select id from IPv4Network where ' . |
650ef981 | 1563 | "inet_aton(?) & (4294967295 >> (32 - mask)) << (32 - mask) = ip " . |
298d2375 | 1564 | "and mask < ? " . |
bb0a44e9 | 1565 | 'order by mask desc limit 1'; |
298d2375 | 1566 | $result = usePreparedSelectBlade ($query, array ($dottedquad, $masklen)); |
bb0a44e9 DO |
1567 | if ($row = $result->fetch (PDO::FETCH_ASSOC)) |
1568 | return $row['id']; | |
e673ee24 DO |
1569 | return NULL; |
1570 | } | |
1571 | ||
21ee3351 AA |
1572 | // Return the id of the smallest IPv6 network containing the given IPv6 address |
1573 | // ($ip is an instance of IPv4Address class) or NULL, if nothing was found. | |
1574 | function getIPv6AddressNetworkId ($ip, $masklen = 128) | |
1575 | { | |
1576 | $query = 'select id from IPv6Network where ip <= ? AND last_ip >= ? and mask < ? order by mask desc limit 1'; | |
522b6f90 | 1577 | $result = usePreparedSelectBlade ($query, array ($ip->getBin(), $ip->getBin(), $masklen)); |
21ee3351 AA |
1578 | if ($row = $result->fetch (PDO::FETCH_ASSOC)) |
1579 | return $row['id']; | |
1580 | return NULL; | |
1581 | } | |
1582 | ||
21ee3351 AA |
1583 | // It is a wrapper around updateV4Address and updateV6Address. |
1584 | // You can pass dotted IPv4, human representation of IPv6, or instance of IPv6Address | |
1585 | function updateAddress ($ip = 0, $name = '', $reserved = 'no') | |
1586 | { | |
1587 | if (is_a ($ip, 'IPv6Address')) | |
1588 | return updateV6Address ($ip, $name, $reserved); | |
1589 | $ipv6 = new IPv6Address; | |
1590 | if ($ipv6->parse ($ip)) | |
1591 | return updateV6Address ($ipv6, $name, $reserved); | |
1592 | return updateV4Address ($ip, $name, $reserved); | |
1593 | } | |
1594 | ||
e673ee24 DO |
1595 | // This function is actually used not only to update, but also to create records, |
1596 | // that's why ON DUPLICATE KEY UPDATE was replaced by DELETE-INSERT pair | |
1597 | // (MySQL 4.0 workaround). | |
21ee3351 | 1598 | function updateV4Address ($ip = 0, $name = '', $reserved = 'no') |
e673ee24 | 1599 | { |
32832c0e | 1600 | usePreparedExecuteBlade ('DELETE FROM IPv4Address WHERE ip = INET_ATON(?)', array ($ip)); |
e673ee24 DO |
1601 | // INSERT may appear not necessary. |
1602 | if ($name == '' and $reserved == 'no') | |
1603 | return ''; | |
32832c0e DO |
1604 | $ret = usePreparedExecuteBlade |
1605 | ( | |
1606 | 'INSERT INTO IPv4Address (name, reserved, ip) VALUES (?, ?, INET_ATON(?))', | |
1607 | array ($name, $reserved, $ip) | |
1608 | ); | |
1609 | return $ret !== FALSE ? '' : (__FUNCTION__ . 'query failed'); | |
e673ee24 DO |
1610 | } |
1611 | ||
21ee3351 AA |
1612 | function updateV6Address ($ip, $name = '', $reserved = 'no') |
1613 | { | |
522b6f90 | 1614 | usePreparedDeleteBlade ('IPv6Address', array ('ip' => $ip->getBin())); |
21ee3351 AA |
1615 | // INSERT may appear not necessary. |
1616 | if ($name == '' and $reserved == 'no') | |
1617 | return ''; | |
68a0bd8a | 1618 | $ret = usePreparedInsertBlade |
21ee3351 | 1619 | ( |
68a0bd8a | 1620 | 'IPv6Address', |
522b6f90 | 1621 | array ('name' => $name, 'reserved' => $reserved, 'ip' => $ip->getBin()) |
21ee3351 AA |
1622 | ); |
1623 | return $ret !== FALSE ? '' : (__FUNCTION__ . 'query failed'); | |
1624 | } | |
1625 | ||
e673ee24 DO |
1626 | function updateBond ($ip='', $object_id=0, $name='', $type='') |
1627 | { | |
32832c0e DO |
1628 | return usePreparedExecuteBlade |
1629 | ( | |
1630 | 'UPDATE IPv4Allocation SET name=?, type=? WHERE ip=INET_ATON(?) AND object_id=?', | |
1631 | array ($name, $type, $ip, $object_id) | |
1632 | ); | |
e673ee24 DO |
1633 | } |
1634 | ||
522b6f90 | 1635 | function updateIPv6Bond ($ip, $object_id=0, $name='', $type='') |
21ee3351 | 1636 | { |
68a0bd8a | 1637 | return usePreparedUpdateBlade |
21ee3351 | 1638 | ( |
68a0bd8a DO |
1639 | 'IPv6Allocation', |
1640 | array | |
1641 | ( | |
1642 | 'name' => $name, | |
1643 | 'type' => $type, | |
1644 | ), | |
1645 | array | |
1646 | ( | |
522b6f90 | 1647 | 'ip' => $ip->getBin(), |
68a0bd8a DO |
1648 | 'object_id' => $object_id, |
1649 | ) | |
21ee3351 AA |
1650 | ); |
1651 | } | |
1652 | ||
1653 | function unbindIpFromObject ($ip, $object_id) | |
e673ee24 | 1654 | { |
32832c0e DO |
1655 | return usePreparedExecuteBlade |
1656 | ( | |
1657 | 'DELETE FROM IPv4Allocation WHERE ip=INET_ATON(?) AND object_id=?', | |
1658 | array ($ip, $object_id) | |
1659 | ); | |
e673ee24 DO |
1660 | } |
1661 | ||
21ee3351 AA |
1662 | function unbindIPv6FromObject ($ip, $object_id) |
1663 | { | |
68a0bd8a | 1664 | return usePreparedDeleteBlade |
21ee3351 | 1665 | ( |
68a0bd8a | 1666 | 'IPv6Allocation', |
522b6f90 | 1667 | array ('ip' => $ip->getBin(), 'object_id' => $object_id) |
21ee3351 AA |
1668 | ); |
1669 | } | |
1670 | ||
ea5fc465 DO |
1671 | function getIPv4PrefixSearchResult ($terms) |
1672 | { | |
6df2025d DO |
1673 | $byname = getSearchResultByField |
1674 | ( | |
1675 | 'IPv4Network', | |
1676 | array ('id'), | |
1677 | 'name', | |
1678 | $terms, | |
1679 | 'ip' | |
1680 | ); | |
ea5fc465 | 1681 | $ret = array(); |
6df2025d | 1682 | foreach ($byname as $row) |
521cd29a | 1683 | $ret[$row['id']] = spotEntity ('ipv4net', $row['id']); |
ea5fc465 DO |
1684 | return $ret; |
1685 | } | |
1686 | ||
21ee3351 AA |
1687 | function getIPv6PrefixSearchResult ($terms) |
1688 | { | |
1689 | $byname = getSearchResultByField | |
1690 | ( | |
1691 | 'IPv6Network', | |
1692 | array ('id'), | |
1693 | 'name', | |
1694 | $terms, | |
1695 | 'ip' | |
1696 | ); | |
1697 | $ret = array(); | |
1698 | foreach ($byname as $row) | |
521cd29a | 1699 | $ret[$row['id']] = spotEntity ('ipv6net', $row['id']); |
21ee3351 AA |
1700 | return $ret; |
1701 | } | |
1702 | ||
ea5fc465 DO |
1703 | function getIPv4AddressSearchResult ($terms) |
1704 | { | |
706ce117 | 1705 | $query = "select inet_ntoa(ip) as ip, name from IPv4Address where "; |
ea5fc465 | 1706 | $or = ''; |
a685e6d7 | 1707 | $qparams = array(); |
ea5fc465 DO |
1708 | foreach (explode (' ', $terms) as $term) |
1709 | { | |
a685e6d7 | 1710 | $query .= $or . "name like ?"; |
ea5fc465 | 1711 | $or = ' or '; |
a685e6d7 | 1712 | $qparams[] = "%${term}%"; |
ea5fc465 | 1713 | } |
a685e6d7 | 1714 | $result = usePreparedSelectBlade ($query, $qparams); |
ea5fc465 DO |
1715 | $ret = array(); |
1716 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
521cd29a | 1717 | $ret[$row['ip']] = $row; |
ea5fc465 DO |
1718 | return $ret; |
1719 | } | |
1720 | ||
21ee3351 AA |
1721 | function getIPv6AddressSearchResult ($terms) |
1722 | { | |
1723 | $query = "select ip, name from IPv6Address where "; | |
1724 | $or = ''; | |
1725 | $qparams = array(); | |
1726 | foreach (explode (' ', $terms) as $term) | |
1727 | { | |
1728 | $query .= $or . "name like ?"; | |
1729 | $or = ' or '; | |
1730 | $qparams[] = "%${term}%"; | |
1731 | } | |
1732 | $result = usePreparedSelectBlade ($query, $qparams); | |
1733 | $ret = array(); | |
1734 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
521cd29a | 1735 | $ret[$row['ip']] = $row; |
21ee3351 AA |
1736 | return $ret; |
1737 | } | |
1738 | ||
05411ccd DO |
1739 | function getIPv4RSPoolSearchResult ($terms) |
1740 | { | |
6cd32e3c DO |
1741 | $byname = getSearchResultByField |
1742 | ( | |
1743 | 'IPv4RSPool', | |
1744 | array ('id'), | |
1745 | 'name', | |
1746 | $terms, | |
1747 | 'name' | |
1748 | ); | |
05411ccd | 1749 | $ret = array(); |
6cd32e3c | 1750 | foreach ($byname as $row) |
521cd29a | 1751 | $ret[$row['id']] = spotEntity ('ipv4rspool', $row['id']); |
05411ccd DO |
1752 | return $ret; |
1753 | } | |
1754 | ||
1755 | function getIPv4VServiceSearchResult ($terms) | |
1756 | { | |
6df2025d DO |
1757 | $byname = getSearchResultByField |
1758 | ( | |
1759 | 'IPv4VS', | |
1760 | array ('id'), | |
1761 | 'name', | |
1762 | $terms, | |
1763 | 'vip' | |
1764 | ); | |
05411ccd | 1765 | $ret = array(); |
6df2025d | 1766 | foreach ($byname as $row) |
521cd29a | 1767 | $ret[$row['id']] = spotEntity ('ipv4vs', $row['id']); |
05411ccd DO |
1768 | return $ret; |
1769 | } | |
1770 | ||
6a88e734 DO |
1771 | function getAccountSearchResult ($terms) |
1772 | { | |
ea62d9dc | 1773 | $byUsername = getSearchResultByField |
6a88e734 DO |
1774 | ( |
1775 | 'UserAccount', | |
1776 | array ('user_id', 'user_name', 'user_realname'), | |
1777 | 'user_name', | |
1778 | $terms, | |
1779 | 'user_name' | |
1780 | ); | |
ea62d9dc DO |
1781 | $byRealname = getSearchResultByField |
1782 | ( | |
1783 | 'UserAccount', | |
1784 | array ('user_id', 'user_name', 'user_realname'), | |
1785 | 'user_realname', | |
1786 | $terms, | |
1787 | 'user_name' | |
1788 | ); | |
521cd29a | 1789 | // Merge it together, if duplicates persist, byUsername wins |
6d513f04 | 1790 | $ret = array(); |
521cd29a AA |
1791 | foreach (array ($byRealname, $byUsername) as $array) |
1792 | foreach ($array as $user) | |
1793 | { | |
1794 | $user['realm'] = 'user'; | |
1795 | $ret[$user['user_id']] = $user; | |
1796 | } | |
573214e0 | 1797 | return $ret; |
6a88e734 DO |
1798 | } |
1799 | ||
e1ae3fb4 AD |
1800 | function getFileSearchResult ($terms) |
1801 | { | |
e531b4d6 | 1802 | $byName = getSearchResultByField |
e1ae3fb4 AD |
1803 | ( |
1804 | 'File', | |
6df2025d | 1805 | array ('id'), |
e1ae3fb4 AD |
1806 | 'name', |
1807 | $terms, | |
1808 | 'name' | |
1809 | ); | |
1810 | $byComment = getSearchResultByField | |
1811 | ( | |
1812 | 'File', | |
6df2025d | 1813 | array ('id'), |
e1ae3fb4 AD |
1814 | 'comment', |
1815 | $terms, | |
1816 | 'name' | |
1817 | ); | |
1818 | // Filter out dupes. | |
e531b4d6 | 1819 | foreach ($byName as $res1) |
e1ae3fb4 AD |
1820 | foreach (array_keys ($byComment) as $key2) |
1821 | if ($res1['id'] == $byComment[$key2]['id']) | |
1822 | { | |
1823 | unset ($byComment[$key2]); | |
1824 | continue 2; | |
1825 | } | |
6df2025d | 1826 | $ret = array(); |
e531b4d6 | 1827 | foreach (array_merge ($byName, $byComment) as $row) |
521cd29a | 1828 | $ret[$row['id']] = spotEntity ('file', $row['id']); |
6df2025d | 1829 | return $ret; |
e1ae3fb4 AD |
1830 | } |
1831 | ||
e531b4d6 DO |
1832 | function getRackSearchResult ($terms) |
1833 | { | |
1834 | $byName = getSearchResultByField | |
1835 | ( | |
1836 | 'Rack', | |
1837 | array ('id'), | |
1838 | 'name', | |
1839 | $terms, | |
1840 | 'name' | |
1841 | ); | |
1842 | $byComment = getSearchResultByField | |
1843 | ( | |
1844 | 'Rack', | |
1845 | array ('id'), | |
1846 | 'comment', | |
1847 | $terms, | |
1848 | 'name' | |
1849 | ); | |
1850 | // Filter out dupes. | |
1851 | foreach ($byName as $res1) | |
1852 | foreach (array_keys ($byComment) as $key2) | |
1853 | if ($res1['id'] == $byComment[$key2]['id']) | |
1854 | { | |
1855 | unset ($byComment[$key2]); | |
1856 | continue 2; | |
1857 | } | |
1858 | $ret = array(); | |
1859 | foreach (array_merge ($byName, $byComment) as $row) | |
521cd29a | 1860 | $ret[$row['id']] = spotEntity ('rack', $row['id']); |
e531b4d6 DO |
1861 | return $ret; |
1862 | } | |
1863 | ||
1f54e1ba DO |
1864 | function getVLANSearchResult ($terms) |
1865 | { | |
1866 | $ret = array(); | |
1867 | $matches = array(); | |
9f178aa9 | 1868 | if (preg_match ('/^vlan\s*(\d+)$/i', $terms, $matches)) |
1f54e1ba DO |
1869 | { |
1870 | $byID = getSearchResultByField | |
1871 | ( | |
1872 | 'VLANDescription', | |
1873 | array ('domain_id', 'vlan_id'), | |
1874 | 'vlan_id', | |
1875 | $matches[1], | |
1876 | 'domain_id', | |
1877 | 1 | |
1878 | ); | |
1879 | foreach ($byID as $row) | |
521cd29a AA |
1880 | { |
1881 | $vlan_ck = $row['domain_id'] . '-' . $row['vlan_id']; | |
1882 | $ret[$vlan_ck] = $vlan_ck; | |
1883 | } | |
1f54e1ba | 1884 | } |
9f178aa9 | 1885 | else |
1f54e1ba | 1886 | { |
9f178aa9 AA |
1887 | $byDescr = getSearchResultByField |
1888 | ( | |
1889 | 'VLANDescription', | |
1890 | array ('domain_id', 'vlan_id'), | |
1891 | 'vlan_descr', | |
1892 | $terms | |
1893 | ); | |
1894 | foreach ($byDescr as $row) | |
1895 | { | |
1896 | $vlan_ck = $row['domain_id'] . '-' . $row['vlan_id']; | |
521cd29a | 1897 | $ret[$vlan_ck] = $vlan_ck; |
9f178aa9 | 1898 | } |
1f54e1ba DO |
1899 | } |
1900 | return $ret; | |
1901 | } | |
1902 | ||
dfd80d8e | 1903 | function getSearchResultByField ($tname, $rcolumns, $scolumn, $terms, $ocolumn = '', $exactness = 0) |
6a88e734 DO |
1904 | { |
1905 | $pfx = ''; | |
1906 | $query = 'select '; | |
a685e6d7 | 1907 | $qparams = array(); |
6a88e734 DO |
1908 | foreach ($rcolumns as $col) |
1909 | { | |
1910 | $query .= $pfx . $col; | |
1911 | $pfx = ', '; | |
1912 | } | |
1913 | $pfx = ''; | |
1914 | $query .= " from ${tname} where "; | |
1915 | foreach (explode (' ', $terms) as $term) | |
1916 | { | |
dfd80d8e DO |
1917 | switch ($exactness) |
1918 | { | |
1919 | case 2: // does this work as expected? | |
a685e6d7 DO |
1920 | $query .= $pfx . "binary ${scolumn} = ?"; |
1921 | $qparams[] = $term; | |
dfd80d8e DO |
1922 | break; |
1923 | case 1: | |
a685e6d7 DO |
1924 | $query .= $pfx . "${scolumn} = ?"; |
1925 | $qparams[] = $term; | |
dfd80d8e DO |
1926 | break; |
1927 | default: | |
a685e6d7 DO |
1928 | $query .= $pfx . "${scolumn} like ?"; |
1929 | $qparams[] = "%${term}%"; | |
dfd80d8e DO |
1930 | break; |
1931 | } | |
6a88e734 DO |
1932 | $pfx = ' or '; |
1933 | } | |
1934 | if ($ocolumn != '') | |
1935 | $query .= " order by ${ocolumn}"; | |
a685e6d7 | 1936 | $result = usePreparedSelectBlade ($query, $qparams); |
6a88e734 DO |
1937 | $ret = array(); |
1938 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
1939 | $ret[] = $row; | |
1940 | return $ret; | |
1941 | } | |
1942 | ||
b1f60545 | 1943 | function getObjectSearchResults ($what) |
323edbbf DO |
1944 | { |
1945 | $ret = array(); | |
1b4a0a6a DO |
1946 | global $searchfunc; |
1947 | foreach ($searchfunc['object'] as $method => $func) | |
1948 | foreach ($func ($what) as $objRecord) | |
1949 | { | |
1950 | $ret[$objRecord['id']]['id'] = $objRecord['id']; | |
1951 | $ret[$objRecord['id']][$method] = $objRecord[$method]; | |
1952 | } | |
b1f60545 DO |
1953 | return $ret; |
1954 | } | |
1955 | ||
1956 | function getObjectAttrsSearchResults ($what) | |
1957 | { | |
1958 | $ret = array(); | |
81659c05 | 1959 | foreach (array ('name', 'label', 'asset_no') as $column) |
b1f60545 DO |
1960 | { |
1961 | $tmp = getSearchResultByField | |
1962 | ( | |
1963 | 'RackObject', | |
1964 | array ('id'), | |
1965 | $column, | |
1966 | $what, | |
1967 | $column | |
1968 | ); | |
1969 | foreach ($tmp as $row) | |
1970 | { | |
1971 | $ret[$row['id']]['id'] = $row['id']; | |
1972 | $ret[$row['id']]['by_attr'][] = $column; | |
1973 | } | |
1974 | } | |
323edbbf DO |
1975 | return $ret; |
1976 | } | |
1977 | ||
1978 | // Look for EXACT value in stickers and return a list of pairs "object_id-attribute_id", | |
1979 | // which matched. A partilar object_id could be returned more, than once, if it has | |
1980 | // multiple matching stickers. Search is only performed on "string" attributes. | |
1981 | function getStickerSearchResults ($what, $exactness = 0) | |
1982 | { | |
1983 | $stickers = getSearchResultByField | |
1984 | ( | |
1985 | 'AttributeValue', | |
1986 | array ('object_id', 'attr_id'), | |
1987 | 'string_value', | |
1988 | $what, | |
1989 | 'object_id', | |
1990 | $exactness | |
1991 | ); | |
1992 | $map = getAttrMap(); | |
1993 | $ret = array(); | |
1994 | foreach ($stickers as $sticker) | |
1995 | if ($map[$sticker['attr_id']]['type'] == 'string') | |
1996 | { | |
1997 | $ret[$sticker['object_id']]['id'] = $sticker['object_id']; | |
1998 | $ret[$sticker['object_id']]['by_sticker'][] = $sticker['attr_id']; | |
1999 | } | |
2000 | return $ret; | |
2001 | } | |
2002 | ||
f1b5f68d | 2003 | // search in port "reservation comment", "label" and "L2 address" columns |
d516d719 | 2004 | function getPortSearchResults ($what) |
323edbbf | 2005 | { |
f1b5f68d | 2006 | $ret = array(); |
323edbbf DO |
2007 | $ports = getSearchResultByField |
2008 | ( | |
2009 | 'Port', | |
1b4a0a6a | 2010 | array ('object_id', 'id', 'reservation_comment'), |
323edbbf DO |
2011 | 'reservation_comment', |
2012 | $what, | |
2013 | 'object_id', | |
2014 | 0 | |
2015 | ); | |
323edbbf DO |
2016 | foreach ($ports as $port) |
2017 | { | |
2018 | $ret[$port['object_id']]['id'] = $port['object_id']; | |
1b4a0a6a | 2019 | $ret[$port['object_id']]['by_port'][$port['id']] = $port['reservation_comment']; |
d516d719 | 2020 | } |
f1b5f68d DO |
2021 | $ports = getSearchResultByField |
2022 | ( | |
2023 | 'Port', | |
2024 | array ('object_id', 'id', 'label'), | |
2025 | 'label', | |
2026 | $what, | |
2027 | 'object_id', | |
2028 | 0 | |
2029 | ); | |
2030 | foreach ($ports as $port) | |
2031 | { | |
2032 | $ret[$port['object_id']]['id'] = $port['object_id']; | |
2033 | $ret[$port['object_id']]['by_port'][$port['id']] = $port['label']; | |
2034 | } | |
9b6e7bd1 DO |
2035 | try |
2036 | { | |
2037 | $db_l2address = l2addressForDatabase ($what); | |
2038 | } | |
2039 | catch (InvalidArgException $e) | |
2040 | { | |
d516d719 | 2041 | return $ret; |
9b6e7bd1 | 2042 | } |
d516d719 DO |
2043 | $ports = getSearchResultByField |
2044 | ( | |
2045 | 'Port', | |
1b4a0a6a | 2046 | array ('object_id', 'id', 'l2address'), |
d516d719 DO |
2047 | 'l2address', |
2048 | $db_l2address, | |
2049 | 'object_id', | |
2050 | 2 | |
2051 | ); | |
2052 | foreach ($ports as $port) | |
2053 | { | |
2054 | $ret[$port['object_id']]['id'] = $port['object_id']; | |
1b4a0a6a | 2055 | $ret[$port['object_id']]['by_port'][$port['id']] = $port['l2address']; |
323edbbf DO |
2056 | } |
2057 | return $ret; | |
2058 | } | |
2059 | ||
5fd2a004 DO |
2060 | // search in IPv4 allocations |
2061 | function getObjectIfacesSearchResults ($what) | |
2062 | { | |
2063 | $ret = array(); | |
21ee3351 | 2064 | $ifaces4 = getSearchResultByField |
5fd2a004 DO |
2065 | ( |
2066 | 'IPv4Allocation', | |
2067 | array ('object_id', 'name'), | |
2068 | 'name', | |
2069 | $what, | |
2070 | 'object_id' | |
2071 | ); | |
21ee3351 AA |
2072 | $ifaces6 = getSearchResultByField |
2073 | ( | |
2074 | 'IPv6Allocation', | |
2075 | array ('object_id', 'name'), | |
2076 | 'name', | |
2077 | $what, | |
2078 | 'object_id' | |
2079 | ); | |
2080 | foreach (array_merge ($ifaces4, $ifaces6) as $row) | |
5fd2a004 DO |
2081 | { |
2082 | $ret[$row['object_id']]['id'] = $row['object_id']; | |
2083 | $ret[$row['object_id']]['by_iface'][] = $row['name']; | |
2084 | } | |
2085 | return $ret; | |
2086 | } | |
2087 | ||
2088 | function getObjectNATSearchResults ($what) | |
2089 | { | |
2090 | $ret = array(); | |
2091 | $ifaces = getSearchResultByField | |
2092 | ( | |
2093 | 'IPv4NAT', | |
2094 | array ('object_id', 'description'), | |
2095 | 'description', | |
2096 | $what, | |
2097 | 'object_id' | |
2098 | ); | |
2099 | foreach ($ifaces as $row) | |
2100 | { | |
2101 | $ret[$row['object_id']]['id'] = $row['object_id']; | |
2102 | $ret[$row['object_id']]['by_nat'][] = $row['description']; | |
2103 | } | |
2104 | return $ret; | |
2105 | } | |
2106 | ||
2107 | // This function returns either port ID or NULL for specified arguments. | |
e9d357e1 | 2108 | function getPortIDs ($object_id, $port_name) |
e673ee24 | 2109 | { |
e9d357e1 DO |
2110 | $ret = array(); |
2111 | $result = usePreparedSelectBlade ('SELECT id FROM Port WHERE object_id = ? AND name = ?', array ($object_id, $port_name)); | |
2112 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
2113 | $ret[] = $row['id']; | |
2114 | return $ret; | |
2115 | } | |
2116 | ||
2117 | // Search in "FQDN" attribute only, and return object ID, when there is exactly | |
2118 | // one result found (and NULL in any other case). | |
2119 | function searchByMgmtHostname ($string) | |
2120 | { | |
2121 | $result = usePreparedSelectBlade ('SELECT object_id FROM AttributeValue WHERE attr_id = 3 AND string_value = ? LIMIT 2', array ($string)); | |
e673ee24 DO |
2122 | $rows = $result->fetchAll (PDO::FETCH_NUM); |
2123 | if (count ($rows) != 1) | |
2124 | return NULL; | |
e9d357e1 | 2125 | return $rows[0][0]; |
e673ee24 DO |
2126 | } |
2127 | ||
2128 | function commitCreateUserAccount ($username, $realname, $password) | |
2129 | { | |
c9066b1c | 2130 | return usePreparedInsertBlade |
e673ee24 DO |
2131 | ( |
2132 | 'UserAccount', | |
2133 | array | |
2134 | ( | |
654e4636 DO |
2135 | 'user_name' => $username, |
2136 | 'user_realname' => $realname, | |
2137 | 'user_password_hash' => $password, | |
e673ee24 DO |
2138 | ) |
2139 | ); | |
2140 | } | |
2141 | ||
2142 | function commitUpdateUserAccount ($id, $new_username, $new_realname, $new_password) | |
2143 | { | |
68a0bd8a | 2144 | return usePreparedUpdateBlade |
32832c0e | 2145 | ( |
68a0bd8a DO |
2146 | 'UserAccount', |
2147 | array | |
2148 | ( | |
2149 | 'user_name' => $new_username, | |
2150 | 'user_realname' => $new_realname, | |
2151 | 'user_password_hash' => $new_password, | |
2152 | ), | |
2153 | array ('user_id' => $id) | |
32832c0e | 2154 | ); |
e673ee24 DO |
2155 | } |
2156 | ||
e673ee24 | 2157 | // This function returns an array of all port type pairs from PortCompat table. |
27ec21b2 | 2158 | function getPortOIFCompat () |
e673ee24 | 2159 | { |
e673ee24 DO |
2160 | $query = |
2161 | "select type1, type2, d1.dict_value as type1name, d2.dict_value as type2name from " . | |
2162 | "PortCompat as pc inner join Dictionary as d1 on pc.type1 = d1.dict_key " . | |
27ec21b2 DO |
2163 | "inner join Dictionary as d2 on pc.type2 = d2.dict_key " . |
2164 | 'ORDER BY type1name, type2name'; | |
c9066b1c | 2165 | $result = usePreparedSelectBlade ($query); |
27ec21b2 | 2166 | return $result->fetchAll (PDO::FETCH_ASSOC); |
e673ee24 DO |
2167 | } |
2168 | ||
0682218d AD |
2169 | // Returns an array of all object type pairs from the ObjectParentCompat table. |
2170 | function getObjectParentCompat () | |
2171 | { | |
2172 | $query = | |
2173 | 'SELECT parent_objtype_id, child_objtype_id, d1.dict_value AS parent_name, d2.dict_value AS child_name FROM ' . | |
2174 | 'ObjectParentCompat AS pc INNER JOIN Dictionary AS d1 ON pc.parent_objtype_id = d1.dict_key ' . | |
2175 | 'INNER JOIN Dictionary AS d2 ON pc.child_objtype_id = d2.dict_key ' . | |
2176 | 'ORDER BY parent_name, child_name'; | |
2177 | $result = usePreparedSelectBlade ($query); | |
2178 | return $result->fetchAll (PDO::FETCH_ASSOC); | |
2179 | } | |
2180 | ||
2181 | // Used to determine if a type of object may have a parent or not | |
2182 | function rackObjectTypeMayHaveParent ($objtype_id) | |
2183 | { | |
2184 | $result = usePreparedSelectBlade ('SELECT COUNT(*) FROM ObjectParentCompat WHERE child_objtype_id = ?', array ($objtype_id)); | |
2185 | $row = $result->fetch (PDO::FETCH_NUM); | |
2186 | if ($row[0] > 0) | |
2187 | return TRUE; | |
2188 | return FALSE; | |
2189 | } | |
2190 | ||
2191 | // Add a pair to the ObjectParentCompat table. | |
2192 | function commitSupplementOPC ($parent_objtype_id, $child_objtype_id) | |
2193 | { | |
2194 | if ($parent_objtype_id <= 0) | |
2195 | throw new InvalidArgException ('parent_objtype_id', $parent_objtype_id); | |
2196 | if ($child_objtype_id <= 0) | |
2197 | throw new InvalidArgException ('child_objtype_id', $child_objtype_id); | |
2198 | return usePreparedInsertBlade | |
2199 | ( | |
2200 | 'ObjectParentCompat', | |
2201 | array ('parent_objtype_id' => $parent_objtype_id, 'child_objtype_id' => $child_objtype_id) | |
2202 | ); | |
2203 | } | |
2204 | ||
2205 | // Remove a pair from the ObjectParentCompat table. | |
2206 | function commitReduceOPC ($parent_objtype_id, $child_objtype_id) | |
2207 | { | |
2208 | return usePreparedDeleteBlade ('ObjectParentCompat', array ('parent_objtype_id' => $parent_objtype_id, 'child_objtype_id' => $child_objtype_id)); | |
2209 | } | |
2210 | ||
da95280e DO |
2211 | function getDictStats () |
2212 | { | |
8d3e3c36 | 2213 | $stock_chapters = array (1, 2, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33); |
c9066b1c DO |
2214 | $result = usePreparedSelectBlade |
2215 | ( | |
10bac82a | 2216 | "select Chapter.id as chapter_no, Chapter.name as chapter_name, count(dict_key) as wc from " . |
c9066b1c DO |
2217 | "Chapter left join Dictionary on Chapter.id = Dictionary.chapter_id group by Chapter.id" |
2218 | ); | |
da95280e | 2219 | $tc = $tw = $uc = $uw = 0; |
89fa639f | 2220 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
da95280e DO |
2221 | { |
2222 | $tc++; | |
2223 | $tw += $row['wc'];; | |
2224 | if (in_array ($row['chapter_no'], $stock_chapters)) | |
2225 | continue; | |
2226 | $uc++; | |
2227 | $uw += $row['wc'];; | |
2228 | } | |
89fa639f | 2229 | unset ($result); |
c9066b1c DO |
2230 | $result = usePreparedSelectBlade |
2231 | ( | |
2232 | "select count(object_id) as attrc from RackObject as ro left join " . | |
2233 | "AttributeValue as av on ro.id = av.object_id group by ro.id" | |
2234 | ); | |
08b4cb24 | 2235 | $to = $ta = $so = 0; |
89fa639f | 2236 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
08b4cb24 DO |
2237 | { |
2238 | $to++; | |
2239 | if ($row['attrc'] != 0) | |
2240 | { | |
2241 | $so++; | |
2242 | $ta += $row['attrc']; | |
2243 | } | |
2244 | } | |
d2eb8399 | 2245 | unset ($result); |
da95280e DO |
2246 | $ret = array(); |
2247 | $ret['Total chapters in dictionary'] = $tc; | |
2248 | $ret['Total words in dictionary'] = $tw; | |
2249 | $ret['User chapters'] = $uc; | |
2250 | $ret['Words in user chapters'] = $uw; | |
08b4cb24 DO |
2251 | $ret['Total objects'] = $to; |
2252 | $ret['Objects with stickers'] = $so; | |
2253 | $ret['Total stickers attached'] = $ta; | |
da95280e DO |
2254 | return $ret; |
2255 | } | |
2256 | ||
6807f0be | 2257 | function getIPv4Stats () |
9ec5fdf1 | 2258 | { |
9ec5fdf1 DO |
2259 | $ret = array(); |
2260 | $subject = array(); | |
706ce117 DO |
2261 | $subject[] = array ('q' => 'select count(id) from IPv4Network', 'txt' => 'Networks'); |
2262 | $subject[] = array ('q' => 'select count(ip) from IPv4Address', 'txt' => 'Addresses commented/reserved'); | |
2263 | $subject[] = array ('q' => 'select count(ip) from IPv4Allocation', 'txt' => 'Addresses allocated'); | |
2264 | $subject[] = array ('q' => 'select count(*) from IPv4NAT', 'txt' => 'NAT rules'); | |
2265 | $subject[] = array ('q' => 'select count(id) from IPv4VS', 'txt' => 'Virtual services'); | |
2266 | $subject[] = array ('q' => 'select count(id) from IPv4RSPool', 'txt' => 'Real server pools'); | |
2267 | $subject[] = array ('q' => 'select count(id) from IPv4RS', 'txt' => 'Real servers'); | |
2268 | $subject[] = array ('q' => 'select count(distinct object_id) from IPv4LB', 'txt' => 'Load balancers'); | |
9ec5fdf1 DO |
2269 | |
2270 | foreach ($subject as $item) | |
2271 | { | |
c9066b1c | 2272 | $result = usePreparedSelectBlade ($item['q']); |
9ec5fdf1 DO |
2273 | $row = $result->fetch (PDO::FETCH_NUM); |
2274 | $ret[$item['txt']] = $row[0]; | |
9ec5fdf1 DO |
2275 | unset ($result); |
2276 | } | |
2277 | return $ret; | |
2278 | } | |
2279 | ||
21ee3351 AA |
2280 | function getIPv6Stats () |
2281 | { | |
2282 | $ret = array(); | |
2283 | $subject = array(); | |
2284 | $subject[] = array ('q' => 'select count(id) from IPv6Network', 'txt' => 'Networks'); | |
2285 | $subject[] = array ('q' => 'select count(ip) from IPv6Address', 'txt' => 'Addresses commented/reserved'); | |
2286 | $subject[] = array ('q' => 'select count(ip) from IPv6Allocation', 'txt' => 'Addresses allocated'); | |
2287 | ||
2288 | foreach ($subject as $item) | |
2289 | { | |
2290 | $result = usePreparedSelectBlade ($item['q']); | |
2291 | $row = $result->fetch (PDO::FETCH_NUM); | |
2292 | $ret[$item['txt']] = $row[0]; | |
2293 | unset ($result); | |
2294 | } | |
2295 | return $ret; | |
2296 | } | |
2297 | ||
6807f0be | 2298 | function getRackspaceStats () |
9ec5fdf1 | 2299 | { |
9ec5fdf1 DO |
2300 | $ret = array(); |
2301 | $subject = array(); | |
10bac82a | 2302 | $subject[] = array ('q' => 'select count(*) from RackRow', 'txt' => 'Rack rows'); |
9ec5fdf1 DO |
2303 | $subject[] = array ('q' => 'select count(*) from Rack', 'txt' => 'Racks'); |
2304 | $subject[] = array ('q' => 'select avg(height) from Rack', 'txt' => 'Average rack height'); | |
2305 | $subject[] = array ('q' => 'select sum(height) from Rack', 'txt' => 'Total rack units in field'); | |
2306 | ||
2307 | foreach ($subject as $item) | |
2308 | { | |
c9066b1c | 2309 | $result = usePreparedSelectBlade ($item['q']); |
9ec5fdf1 | 2310 | $row = $result->fetch (PDO::FETCH_NUM); |
59a83bd8 | 2311 | $ret[$item['txt']] = !strlen ($row[0]) ? 0 : $row[0]; |
9ec5fdf1 DO |
2312 | unset ($result); |
2313 | } | |
2314 | return $ret; | |
2315 | } | |
2316 | ||
6807f0be DO |
2317 | /* |
2318 | ||
2319 | The following allows figuring out records in TagStorage, which refer to non-existing entities: | |
2320 | ||
120e9ddd DO |
2321 | mysql> select entity_id from TagStorage left join Files on entity_id = id where entity_realm = 'file' and id is null; |
2322 | mysql> select entity_id from TagStorage left join IPv4Network on entity_id = id where entity_realm = 'ipv4net' and id is null; | |
2323 | mysql> select entity_id from TagStorage left join RackObject on entity_id = id where entity_realm = 'object' and id is null; | |
2324 | mysql> select entity_id from TagStorage left join Rack on entity_id = id where entity_realm = 'rack' and id is null; | |
2325 | mysql> select entity_id from TagStorage left join IPv4VS on entity_id = id where entity_realm = 'ipv4vs' and id is null; | |
2326 | mysql> select entity_id from TagStorage left join IPv4RSPool on entity_id = id where entity_realm = 'ipv4rspool' and id is null; | |
2327 | mysql> select entity_id from TagStorage left join UserAccount on entity_id = user_id where entity_realm = 'user' and user_id is null; | |
6807f0be DO |
2328 | |
2329 | Accordingly, these are the records, which refer to non-existent tags: | |
2330 | ||
2331 | mysql> select tag_id from TagStorage left join TagTree on tag_id = id where id is null; | |
2332 | ||
2333 | */ | |
2334 | ||
e673ee24 DO |
2335 | function commitDeleteChapter ($chapter_no = 0) |
2336 | { | |
c9066b1c | 2337 | return usePreparedDeleteBlade ('Chapter', array ('id' => $chapter_no, 'sticky' => 'no')); |
e673ee24 DO |
2338 | } |
2339 | ||
4c330a14 DO |
2340 | // This is a dictionary accessor. We perform link rendering, so the user sees |
2341 | // nice <select> drop-downs. | |
7fa7047a | 2342 | function readChapter ($chapter_id = 0, $style = '') |
e673ee24 | 2343 | { |
c9066b1c DO |
2344 | $result = usePreparedSelectBlade |
2345 | ( | |
8ad59489 | 2346 | "select dict_key, dict_value as value from Dictionary " . |
c9066b1c DO |
2347 | "where chapter_id = ?", |
2348 | array ($chapter_id) | |
2349 | ); | |
e673ee24 DO |
2350 | $chapter = array(); |
2351 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
8ad59489 AA |
2352 | { |
2353 | parseWikiLink ($row); | |
2354 | $chapter[$row['dict_key']] = ($style == 'a' ? $row['a_value'] : $row['o_value']); | |
2355 | } | |
4aa8609b DO |
2356 | // SQL ORDER BY had no sense, because we need to sort after link rendering, not before. |
2357 | asort ($chapter); | |
e673ee24 DO |
2358 | return $chapter; |
2359 | } | |
2360 | ||
7fa7047a DO |
2361 | // Return refcounters for all given keys of the given chapter. |
2362 | function getChapterRefc ($chapter_id, $keylist) | |
2363 | { | |
afdb3cf5 DO |
2364 | $ret = array(); |
2365 | foreach ($keylist as $key) | |
2366 | $ret[$key] = 0; | |
7fa7047a DO |
2367 | switch ($chapter_id) |
2368 | { | |
2369 | case CHAP_OBJTYPE: | |
2370 | // RackObjectType chapter is referenced by AttributeMap and RackObject tables | |
2371 | $query = 'select dict_key as uint_value, (select count(*) from AttributeMap where objtype_id = dict_key) + ' . | |
c9066b1c | 2372 | "(select count(*) from RackObject where objtype_id = dict_key) as refcnt from Dictionary where chapter_id = ?"; |
7fa7047a DO |
2373 | break; |
2374 | case CHAP_PORTTYPE: | |
93a83f51 | 2375 | // PortOuterInterface chapter is referenced by PortCompat, PortInterfaceCompat and Port tables |
7fa7047a | 2376 | $query = 'select dict_key as uint_value, (select count(*) from PortCompat where type1 = dict_key or type2 = dict_key) + ' . |
93a83f51 | 2377 | '(select count(*) from Port where type = dict_key) + (SELECT COUNT(*) FROM PortInterfaceCompat WHERE oif_id = dict_key) as refcnt ' . |
c9066b1c | 2378 | "from Dictionary where chapter_id = ?"; |
7fa7047a DO |
2379 | break; |
2380 | default: | |
2381 | // Find the list of all assigned values of dictionary-addressed attributes, each with | |
2382 | // chapter/word keyed reference counters. | |
71a73b12 AA |
2383 | $query = "select uint_value, count(object_id) as refcnt |
2384 | from AttributeMap am | |
2385 | inner join AttributeValue av on am.attr_id = av.attr_id | |
2386 | inner join RackObject ro on ro.id = av.object_id | |
2387 | where am.chapter_id = ? and ro.objtype_id = am.objtype_id | |
2388 | group by uint_value"; | |
7fa7047a DO |
2389 | break; |
2390 | } | |
c9066b1c | 2391 | $result = usePreparedSelectBlade ($query, array ($chapter_id)); |
7fa7047a DO |
2392 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
2393 | $ret[$row['uint_value']] = $row['refcnt']; | |
2394 | return $ret; | |
2395 | } | |
2396 | ||
e4e9e2a1 DO |
2397 | // Return a list of all stickers with sticker map applied. Each sticker records will |
2398 | // list all its ways on the map with refcnt set. | |
e673ee24 DO |
2399 | function getAttrMap () |
2400 | { | |
c9066b1c DO |
2401 | $result = usePreparedSelectBlade |
2402 | ( | |
978dc510 DO |
2403 | 'SELECT id, type, name, chapter_id, (SELECT dict_value FROM Dictionary WHERE dict_key = objtype_id) '. |
2404 | 'AS objtype_name, (SELECT name FROM Chapter WHERE id = chapter_id) ' . | |
7fa7047a | 2405 | 'AS chapter_name, objtype_id, (SELECT COUNT(object_id) FROM AttributeValue AS av INNER JOIN RackObject AS ro ' . |
e4e9e2a1 | 2406 | 'ON av.object_id = ro.id WHERE av.attr_id = Attribute.id AND ro.objtype_id = AttributeMap.objtype_id) ' . |
c9066b1c DO |
2407 | 'AS refcnt FROM Attribute LEFT JOIN AttributeMap ON id = attr_id ORDER BY Attribute.name, objtype_name' |
2408 | ); | |
e673ee24 DO |
2409 | $ret = array(); |
2410 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
2411 | { | |
e4e9e2a1 DO |
2412 | if (!isset ($ret[$row['id']])) |
2413 | $ret[$row['id']] = array | |
2414 | ( | |
2415 | 'id' => $row['id'], | |
2416 | 'type' => $row['type'], | |
2417 | 'name' => $row['name'], | |
2418 | 'application' => array(), | |
2419 | ); | |
e673ee24 DO |
2420 | if ($row['objtype_id'] == '') |
2421 | continue; | |
e4e9e2a1 DO |
2422 | $application = array |
2423 | ( | |
2424 | 'objtype_id' => $row['objtype_id'], | |
e4e9e2a1 DO |
2425 | 'refcnt' => $row['refcnt'], |
2426 | ); | |
2427 | if ($row['type'] == 'dict') | |
e673ee24 | 2428 | { |
50393983 | 2429 | $application['chapter_no'] = $row['chapter_id']; |
e673ee24 DO |
2430 | $application['chapter_name'] = $row['chapter_name']; |
2431 | } | |
e4e9e2a1 | 2432 | $ret[$row['id']]['application'][] = $application; |
e673ee24 | 2433 | } |
e673ee24 DO |
2434 | return $ret; |
2435 | } | |
2436 | ||
e673ee24 DO |
2437 | // FIXME: don't store garbage in chapter_no for non-dictionary types. |
2438 | function commitSupplementAttrMap ($attr_id = 0, $objtype_id = 0, $chapter_no = 0) | |
2439 | { | |
0cc24e9a DY |
2440 | if ($attr_id <= 0) |
2441 | throw new InvalidArgException ('$attr_id', $attr_id); | |
2442 | if ($objtype_id <= 0) | |
2443 | throw new InvalidArgException ('$objtype_id', $objtype_id); | |
2444 | ||
c9066b1c | 2445 | return usePreparedInsertBlade |
e673ee24 DO |
2446 | ( |
2447 | 'AttributeMap', | |
2448 | array | |
2449 | ( | |
2450 | 'attr_id' => $attr_id, | |
2451 | 'objtype_id' => $objtype_id, | |
10bac82a | 2452 | 'chapter_id' => $chapter_no |
e673ee24 DO |
2453 | ) |
2454 | ); | |
2455 | } | |
2456 | ||
1f54e1ba DO |
2457 | function cacheAllObjectsAttributes() |
2458 | { | |
2459 | global $object_attribute_cache; | |
2460 | $object_attribute_cache = fetchAttrsForObjects(NULL); | |
2461 | } | |
2462 | ||
2463 | // Fetches a list of attributes for each object in $object_set array. | |
2464 | // If $object_set is not set, returns attributes for all objects in DB | |
2465 | // Returns an array with object_id keys | |
2466 | function fetchAttrsForObjects($object_set) | |
e673ee24 | 2467 | { |
e673ee24 | 2468 | $ret = array(); |
1f54e1ba | 2469 | $query = |
98b52735 | 2470 | "select A.id as attr_id, A.name as attr_name, A.type as attr_type, C.name as chapter_name, " . |
1f54e1ba | 2471 | "C.id as chapter_id, AV.uint_value, AV.float_value, AV.string_value, D.dict_value, RO.id as object_id from " . |
98b52735 DO |
2472 | "RackObject as RO inner join AttributeMap as AM on RO.objtype_id = AM.objtype_id " . |
2473 | "inner join Attribute as A on AM.attr_id = A.id " . | |
e673ee24 | 2474 | "left join AttributeValue as AV on AV.attr_id = AM.attr_id and AV.object_id = RO.id " . |
10bac82a | 2475 | "left join Dictionary as D on D.dict_key = AV.uint_value and AM.chapter_id = D.chapter_id " . |
1f54e1ba DO |
2476 | "left join Chapter as C on AM.chapter_id = C.id"; |
2477 | if (is_array ($object_set) && !empty ($object_set)) | |
2478 | { | |
2479 | $query .= ' WHERE RO.id IN ('; | |
2480 | foreach ($object_set as $object_id) | |
2481 | $query .= intval ($object_id) . ', '; | |
2482 | $query = trim ($query, ', ') . ')'; | |
2483 | } | |
98b52735 | 2484 | $query .= " order by A.name, A.type"; |
1f54e1ba DO |
2485 | |
2486 | $result = usePreparedSelectBlade ($query); | |
e673ee24 DO |
2487 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
2488 | { | |
1f54e1ba DO |
2489 | $object_id = $row['object_id']; |
2490 | $attr_id = $row['attr_id']; | |
2491 | if (!array_key_exists ($object_id, $ret)) | |
2492 | $ret[$object_id] = array(); | |
1f54e1ba | 2493 | |
e673ee24 DO |
2494 | $record = array(); |
2495 | $record['id'] = $row['attr_id']; | |
2496 | $record['name'] = $row['attr_name']; | |
2497 | $record['type'] = $row['attr_type']; | |
2498 | switch ($row['attr_type']) | |
2499 | { | |
7fa7047a DO |
2500 | case 'dict': |
2501 | $record['chapter_id'] = $row['chapter_id']; | |
2502 | $record['chapter_name'] = $row['chapter_name']; | |
2503 | $record['key'] = $row['uint_value']; | |
2504 | // fall through | |
e673ee24 DO |
2505 | case 'uint': |
2506 | case 'float': | |
2507 | case 'string': | |
24cbe8af | 2508 | $record['value'] = $row[$row['attr_type'] . '_value']; |
8ad59489 | 2509 | parseWikiLink ($record); |
24cbe8af | 2510 | break; |
e673ee24 DO |
2511 | default: |
2512 | $record['value'] = NULL; | |
2513 | break; | |
2514 | } | |
1f54e1ba | 2515 | $ret[$object_id][$attr_id] = $record; |
e673ee24 | 2516 | } |
e673ee24 DO |
2517 | return $ret; |
2518 | } | |
2519 | ||
1f54e1ba | 2520 | // This function returns all optional attributes for requested object |
98b52735 DO |
2521 | // as an array of records. |
2522 | // Empty array is returned, if there are no attributes found. | |
1f54e1ba DO |
2523 | function getAttrValues ($object_id) |
2524 | { | |
2525 | global $object_attribute_cache; | |
2526 | if (isset ($object_attribute_cache[$object_id])) | |
2527 | return $object_attribute_cache[$object_id]; | |
2528 | ||
2529 | $ret = fetchAttrsForObjects(array($object_id)); | |
2530 | $attrs = array(); | |
98b52735 | 2531 | if (isset ($ret[$object_id])) |
1f54e1ba DO |
2532 | { |
2533 | $attrs = $ret[$object_id]; | |
2534 | $object_attribute_cache[$object_id] = $attrs; | |
1f54e1ba | 2535 | } |
98b52735 | 2536 | return $attrs; |
1f54e1ba DO |
2537 | } |
2538 | ||
e673ee24 DO |
2539 | function commitResetAttrValue ($object_id = 0, $attr_id = 0) |
2540 | { | |
c9066b1c | 2541 | return usePreparedDeleteBlade ('AttributeValue', array ('object_id' => $object_id, 'attr_id' => $attr_id)); |
e673ee24 DO |
2542 | } |
2543 | ||
e673ee24 DO |
2544 | function commitUpdateAttrValue ($object_id = 0, $attr_id = 0, $value = '') |
2545 | { | |
0cc24e9a DY |
2546 | if ($object_id <= 0) |
2547 | throw new InvalidArgException ('$objtype_id', $objtype_id); | |
2548 | if ($attr_id <= 0) | |
2549 | throw new InvalidArgException ('$attr_id', $attr_id); | |
59a83bd8 | 2550 | if (!strlen ($value)) |
e673ee24 | 2551 | return commitResetAttrValue ($object_id, $attr_id); |
c9066b1c | 2552 | $result = usePreparedSelectBlade ('select type as attr_type from Attribute where id = ?', array ($attr_id)); |
e673ee24 | 2553 | $row = $result->fetch (PDO::FETCH_NUM); |
e673ee24 | 2554 | $attr_type = $row[0]; |
c9066b1c | 2555 | unset ($result); |
e673ee24 DO |
2556 | switch ($attr_type) |
2557 | { | |
2558 | case 'uint': | |
2559 | case 'float': | |
2560 | case 'string': | |
2561 | $column = $attr_type . '_value'; | |
2562 | break; | |
2563 | case 'dict': | |
2564 | $column = 'uint_value'; | |
2565 | break; | |
2566 | default: | |
0cc24e9a | 2567 | throw new InvalidArgException ('$attr_type', $attr_type, 'Unknown attribute type found in object #'.$object_id.', attribute #'.$attr_id); |
e673ee24 | 2568 | } |
c9066b1c | 2569 | usePreparedDeleteBlade ('AttributeValue', array ('object_id' => $object_id, 'attr_id' => $attr_id)); |
e673ee24 | 2570 | // We know $value isn't empty here. |
c9066b1c DO |
2571 | usePreparedInsertBlade |
2572 | ( | |
2573 | 'AttributeValue', | |
2574 | array | |
2575 | ( | |
2576 | $column => $value, | |
2577 | 'object_id' => $object_id, | |
2578 | 'attr_id' => $attr_id, | |
2579 | ) | |
2580 | ); | |
e673ee24 DO |
2581 | return TRUE; |
2582 | } | |
2583 | ||
3fb56626 DO |
2584 | function convertPDOException ($e) |
2585 | { | |
ec523868 | 2586 | switch ($e->getCode() . '-' . $e->errorInfo[1]) |
3fb56626 | 2587 | { |
ec523868 DO |
2588 | case '23000-1062': |
2589 | $text = 'such record already exists'; | |
2590 | break; | |
2591 | case '23000-1205': | |
3fb56626 DO |
2592 | $text = 'such record already exists'; |
2593 | break; | |
ec523868 DO |
2594 | case '23000-1451': |
2595 | case '23000-1452': | |
3fb56626 DO |
2596 | $text = 'foreign key violation'; |
2597 | break; | |
ec523868 DO |
2598 | case 'HY000-1205': |
2599 | $text = 'lock wait timeout'; | |
3fb56626 | 2600 | break; |
ec523868 DO |
2601 | default: |
2602 | return $e; | |
3fb56626 | 2603 | } |
ec523868 | 2604 | return new RTDatabaseError ($text); |
3fb56626 DO |
2605 | } |
2606 | ||
e673ee24 DO |
2607 | // This is a swiss-knife blade to insert a record into a table. |
2608 | // The first argument is table name. | |
2609 | // The second argument is an array of "name" => "value" pairs. | |
357eb2ea DO |
2610 | function usePreparedInsertBlade ($tablename, $columns) |
2611 | { | |
2612 | global $dbxlink; | |
2613 | $query = "INSERT INTO ${tablename} (" . implode (', ', array_keys ($columns)); | |
ec523868 | 2614 | $query .= ') VALUES (' . questionMarks (count ($columns)) . ')'; |
357eb2ea DO |
2615 | // Now the query should be as follows: |
2616 | // INSERT INTO table (c1, c2, c3) VALUES (?, ?, ?) | |
5f054512 DO |
2617 | try |
2618 | { | |
3fb56626 | 2619 | $prepared = $dbxlink->prepare ($query); |
5f054512 DO |
2620 | if (!$prepared->execute (array_values ($columns))) |
2621 | return FALSE; | |
2622 | return $prepared->rowCount() == 1; | |
2623 | } | |
2624 | catch (PDOException $e) | |
2625 | { | |
3fb56626 | 2626 | throw convertPDOException ($e); |
5f054512 | 2627 | } |
357eb2ea DO |
2628 | } |
2629 | ||
9589714d DO |
2630 | // This swiss-knife blade deletes any number of records from the specified table |
2631 | // using the specified key names and values. | |
bbae3611 | 2632 | function usePreparedDeleteBlade ($tablename, $columns, $conjunction = 'AND') |
8198f2c6 DO |
2633 | { |
2634 | global $dbxlink; | |
bbae3611 DO |
2635 | $conj = ''; |
2636 | $query = "DELETE FROM ${tablename} WHERE "; | |
2637 | foreach ($columns as $colname => $colvalue) | |
2638 | { | |
de47b574 | 2639 | $query .= " ${conj} ${colname}=?"; |
bbae3611 DO |
2640 | $conj = $conjunction; |
2641 | } | |
5f054512 DO |
2642 | try |
2643 | { | |
3fb56626 | 2644 | $prepared = $dbxlink->prepare ($query); |
5f054512 DO |
2645 | if (!$prepared->execute (array_values ($columns))) |
2646 | return FALSE; | |
2647 | return $prepared->rowCount(); // FALSE !== 0 | |
2648 | } | |
2649 | catch (PDOException $e) | |
2650 | { | |
3fb56626 | 2651 | throw convertPDOException ($e); |
5f054512 | 2652 | } |
8198f2c6 DO |
2653 | } |
2654 | ||
d0dadd80 DO |
2655 | function usePreparedSelectBlade ($query, $args = array()) |
2656 | { | |
2657 | global $dbxlink; | |
2658 | $prepared = $dbxlink->prepare ($query); | |
ec523868 DO |
2659 | try |
2660 | { | |
2661 | if (!$prepared->execute ($args)) | |
2662 | return FALSE; | |
2663 | return $prepared; | |
2664 | } | |
2665 | catch (PDOException $e) | |
2666 | { | |
2667 | throw convertPDOException ($e); | |
2668 | } | |
d0dadd80 DO |
2669 | } |
2670 | ||
09ecff69 DO |
2671 | function usePreparedUpdateBlade ($tablename, $set_columns, $where_columns, $conjunction = 'AND') |
2672 | { | |
2673 | global $dbxlink; | |
2674 | $conj = ''; | |
2675 | $query = "UPDATE ${tablename} SET "; | |
2676 | foreach (array_keys ($set_columns) as $colname) | |
2677 | { | |
2678 | $query .= "${conj}${colname}=?"; | |
2679 | $conj = ', '; | |
2680 | } | |
2681 | $conj = ''; | |
2682 | $query .= ' WHERE '; | |
2683 | foreach (array_keys ($where_columns) as $colname) | |
2684 | { | |
58f694d4 | 2685 | $query .= " ${conj} ${colname}=?"; |
09ecff69 DO |
2686 | $conj = $conjunction; |
2687 | } | |
2688 | try | |
2689 | { | |
2690 | $prepared = $dbxlink->prepare ($query); | |
2691 | $prepared->execute (array_merge (array_values ($set_columns), array_values ($where_columns))); | |
2692 | } | |
2693 | catch (PDOException $e) | |
2694 | { | |
2695 | throw convertPDOException ($e); | |
2696 | } | |
2697 | } | |
2698 | ||
39eadd27 DO |
2699 | // Prepare and execute the statement with parameters, then return number of |
2700 | // rows affected (or FALSE in case of an error). | |
2701 | function usePreparedExecuteBlade ($query, $args = array()) | |
2702 | { | |
2703 | global $dbxlink; | |
3fb56626 DO |
2704 | try |
2705 | { | |
2706 | if (!$prepared = $dbxlink->prepare ($query)) | |
2707 | return FALSE; | |
2708 | if (!$prepared->execute ($args)) | |
2709 | return FALSE; | |
2710 | return $prepared->rowCount(); | |
2711 | } | |
2712 | catch (PDOException $e) | |
2713 | { | |
2714 | throw convertPDOException ($e); | |
2715 | } | |
39eadd27 DO |
2716 | } |
2717 | ||
9c0b0016 DO |
2718 | function loadConfigCache () |
2719 | { | |
c9066b1c | 2720 | $result = usePreparedSelectBlade ('SELECT varname, varvalue, vartype, is_hidden, emptyok, description, is_userdefined FROM Config ORDER BY varname'); |
3540d15c | 2721 | $cache = array(); |
c9066b1c | 2722 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
3540d15c | 2723 | $cache[$row['varname']] = $row; |
3540d15c DY |
2724 | return $cache; |
2725 | } | |
2726 | ||
2727 | function loadUserConfigCache ($username = NULL) | |
2728 | { | |
2729 | if (!strlen ($username)) | |
2730 | throw new InvalidArgException ('$username', $username); | |
c9066b1c | 2731 | $result = usePreparedSelectBlade ('SELECT varname, varvalue FROM UserConfig WHERE user = ?', array ($username)); |
9c0b0016 DO |
2732 | $cache = array(); |
2733 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
2734 | $cache[$row['varname']] = $row; | |
9c0b0016 DO |
2735 | return $cache; |
2736 | } | |
2737 | ||
da04825a DO |
2738 | // Return an array of virtual services. For each of them list real server pools |
2739 | // with their load balancers and other stats. | |
62a1dcb5 DO |
2740 | function getSLBSummary () |
2741 | { | |
c9066b1c DO |
2742 | $result = usePreparedSelectBlade |
2743 | ( | |
2744 | 'select vs.id as vsid, inet_ntoa(vip) as vip, vport, proto, vs.name, object_id, ' . | |
6fec9f39 | 2745 | 'lb.rspool_id, pool.name as pool_name, count(rs.id) as rscount ' . |
706ce117 DO |
2746 | 'from IPv4VS as vs inner join IPv4LB as lb on vs.id = lb.vs_id ' . |
2747 | 'inner join IPv4RSPool as pool on lb.rspool_id = pool.id ' . | |
2748 | 'left join IPv4RS as rs on rs.rspool_id = lb.rspool_id ' . | |
c9066b1c DO |
2749 | 'group by vs.id, object_id order by vs.vip, object_id' |
2750 | ); | |
c3bdc503 DO |
2751 | $ret = array(); |
2752 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
2753 | { | |
4cadac8f | 2754 | $vsid = $row['vsid']; |
da04825a | 2755 | $object_id = $row['object_id']; |
4cadac8f DO |
2756 | if (!isset ($ret[$vsid])) |
2757 | { | |
2758 | $ret[$vsid] = array(); | |
13bffb29 | 2759 | foreach (array ('vip', 'vport', 'proto', 'name') as $cname) |
4cadac8f | 2760 | $ret[$vsid][$cname] = $row[$cname]; |
da04825a | 2761 | $ret[$vsid]['lblist'] = array(); |
4cadac8f | 2762 | } |
6fec9f39 DO |
2763 | // There's only one assigned RS pool possible for each LB-VS combination. |
2764 | $ret[$vsid]['lblist'][$row['object_id']] = array | |
2765 | ( | |
2766 | 'id' => $row['rspool_id'], | |
2767 | 'size' => $row['rscount'], | |
2768 | 'name' => $row['pool_name'] | |
2769 | ); | |
c3bdc503 DO |
2770 | } |
2771 | return $ret; | |
62a1dcb5 DO |
2772 | } |
2773 | ||
7e7a8387 | 2774 | function addRStoRSPool ($pool_id = 0, $rsip = '', $rsport = 0, $inservice = 'no', $rsconfig = '') |
ca461127 | 2775 | { |
a5c589d2 | 2776 | return usePreparedExecuteBlade |
ca461127 | 2777 | ( |
a5c589d2 | 2778 | 'INSERT INTO IPv4RS (rsip, rsport, rspool_id, inservice, rsconfig) VALUES (INET_ATON(?), ?, ?, ?, ?)', |
ca461127 DO |
2779 | array |
2780 | ( | |
a5c589d2 DO |
2781 | $rsip, |
2782 | (!strlen ($rsport) or $rsport === 0) ? NULL : $rsport, | |
2783 | $pool_id, | |
2784 | $inservice == 'yes' ? 'yes' : 'no', | |
2785 | !strlen ($rsconfig) ? NULL : $rsconfig | |
3241551e DO |
2786 | ) |
2787 | ); | |
2788 | } | |
2789 | ||
abd1e9ac | 2790 | function addLBtoRSPool ($pool_id = 0, $object_id = 0, $vs_id = 0, $vsconfig = '', $rsconfig = '', $prio = '') |
3241551e | 2791 | { |
c9066b1c | 2792 | return usePreparedInsertBlade |
3241551e | 2793 | ( |
706ce117 | 2794 | 'IPv4LB', |
3241551e DO |
2795 | array |
2796 | ( | |
2797 | 'object_id' => $object_id, | |
2798 | 'rspool_id' => $pool_id, | |
c1ca768c | 2799 | 'vs_id' => $vs_id, |
c9066b1c | 2800 | 'vsconfig' => (!strlen ($vsconfig) ? NULL : $vsconfig), |
abd1e9ac DO |
2801 | 'rsconfig' => (!strlen ($rsconfig) ? NULL : $rsconfig), |
2802 | 'prio' => (!strlen ($prio) ? NULL : $prio), | |
3241551e | 2803 | ) |
ca461127 DO |
2804 | ); |
2805 | } | |
2806 | ||
d6517a21 DO |
2807 | function commitDeleteVS ($id = 0) |
2808 | { | |
6657739e | 2809 | releaseFiles ('ipv4vs', $id); |
0da3bf00 | 2810 | return FALSE !== usePreparedDeleteBlade ('IPv4VS', array ('id' => $id)) && FALSE !== destroyTagsForEntity ('ipv4vs', $id); |
d6517a21 DO |
2811 | } |
2812 | ||
fb1c4a54 DO |
2813 | function commitUpdateRS ($rsid = 0, $rsip = '', $rsport = 0, $rsconfig = '') |
2814 | { | |
fb1c4a54 | 2815 | if (long2ip (ip2long ($rsip)) !== $rsip) |
c5f84f48 | 2816 | throw new InvalidArgException ('$rsip', $rsip); |
a5c589d2 DO |
2817 | return usePreparedExecuteBlade |
2818 | ( | |
2819 | 'UPDATE IPv4RS SET rsip=INET_ATON(?), rsport=?, rsconfig=? WHERE id=?', | |
2820 | array | |
2821 | ( | |
2822 | $rsip, | |
2823 | (!strlen ($rsport) or $rsport === 0) ? NULL : $rsport, | |
2824 | !strlen ($rsconfig) ? NULL : $rsconfig, | |
2825 | $rsid, | |
2826 | ) | |
2827 | ); | |
fb1c4a54 DO |
2828 | } |
2829 | ||
d6517a21 DO |
2830 | function commitUpdateVS ($vsid = 0, $vip = '', $vport = 0, $proto = '', $name = '', $vsconfig = '', $rsconfig = '') |
2831 | { | |
c5f84f48 DY |
2832 | if (!strlen ($vip)) |
2833 | throw new InvalidArgException ('$vip', $vip); | |
2834 | if ($vport <= 0) | |
2835 | throw new InvalidArgException ('$vport', $vport); | |
2836 | if (!strlen ($proto)) | |
2837 | throw new InvalidArgException ('$proto', $proto); | |
a5c589d2 DO |
2838 | return usePreparedExecuteBlade |
2839 | ( | |
2840 | 'UPDATE IPv4VS SET vip=INET_ATON(?), vport=?, proto=?, name=?, vsconfig=?, rsconfig=? WHERE id=?', | |
2841 | array | |
2842 | ( | |
2843 | $vip, | |
2844 | $vport, | |
2845 | $proto, | |
2846 | !strlen ($name) ? NULL : $name, | |
2847 | !strlen ($vsconfig) ? NULL : $vsconfig, | |
2848 | !strlen ($rsconfig) ? NULL : $rsconfig, | |
2849 | $vsid, | |
2850 | ) | |
2851 | ); | |
d6517a21 DO |
2852 | } |
2853 | ||
5a1680d2 DO |
2854 | function loadThumbCache ($rack_id = 0) |
2855 | { | |
5a1680d2 | 2856 | $ret = NULL; |
c9066b1c | 2857 | $result = usePreparedSelectBlade ("SELECT thumb_data FROM Rack WHERE id = ? AND thumb_data IS NOT NULL", array ($rack_id)); |
5a1680d2 DO |
2858 | $row = $result->fetch (PDO::FETCH_ASSOC); |
2859 | if ($row) | |
2860 | $ret = base64_decode ($row['thumb_data']); | |
5a1680d2 DO |
2861 | return $ret; |
2862 | } | |
2863 | ||
748805bf | 2864 | // Return the list of attached RS pools for the given object. As long as we have |
706ce117 | 2865 | // the LB-VS UNIQUE in IPv4LB table, it is Ok to key returned records |
748805bf DO |
2866 | // by vs_id, because there will be only one RS pool listed for each VS of the |
2867 | // current object. | |
78e7b769 DO |
2868 | function getRSPoolsForObject ($object_id = 0) |
2869 | { | |
c9066b1c DO |
2870 | $result = usePreparedSelectBlade |
2871 | ( | |
2872 | 'select vs_id, inet_ntoa(vip) as vip, vport, proto, vs.name, pool.id as pool_id, ' . | |
1f54e1ba | 2873 | 'pool.name as pool_name, count(rsip) as rscount, lb.vsconfig, lb.rsconfig, lb.prio from ' . |
706ce117 DO |
2874 | 'IPv4LB as lb inner join IPv4RSPool as pool on lb.rspool_id = pool.id ' . |
2875 | 'inner join IPv4VS as vs on lb.vs_id = vs.id ' . | |
2876 | 'left join IPv4RS as rs on lb.rspool_id = rs.rspool_id ' . | |
c9066b1c DO |
2877 | 'where lb.object_id = ? ' . |
2878 | 'group by lb.rspool_id, lb.vs_id order by vs.vip, vport, proto, pool.name', | |
2879 | array ($object_id) | |
2880 | ); | |
748805bf | 2881 | $ret = array (); |
78e7b769 | 2882 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
1f54e1ba | 2883 | foreach (array ('vip', 'vport', 'proto', 'name', 'pool_id', 'pool_name', 'rscount', 'vsconfig', 'rsconfig', 'prio') as $cname) |
748805bf | 2884 | $ret[$row['vs_id']][$cname] = $row[$cname]; |
748805bf | 2885 | return $ret; |
78e7b769 DO |
2886 | } |
2887 | ||
c63a8d6e | 2888 | function commitCreateRSPool ($name = '', $vsconfig = '', $rsconfig = '', $taglist = array()) |
5ad76f01 | 2889 | { |
ef5bb52c | 2890 | usePreparedInsertBlade |
5ad76f01 | 2891 | ( |
706ce117 | 2892 | 'IPv4RSPool', |
5ad76f01 DO |
2893 | array |
2894 | ( | |
c9066b1c DO |
2895 | 'name' => (!strlen ($name) ? NULL : $name), |
2896 | 'vsconfig' => (!strlen ($vsconfig) ? NULL : $vsconfig), | |
2897 | 'rsconfig' => (!strlen ($rsconfig) ? NULL : $rsconfig) | |
5ad76f01 | 2898 | ) |
ef5bb52c DO |
2899 | ); |
2900 | produceTagsForLastRecord ('ipv4rspool', $taglist); | |
5ad76f01 DO |
2901 | } |
2902 | ||
2903 | function commitDeleteRSPool ($pool_id = 0) | |
2904 | { | |
6657739e | 2905 | releaseFiles ('ipv4rspool', $pool_id); |
c9066b1c | 2906 | return usePreparedDeleteBlade ('IPv4RSPool', array ('id' => $pool_id)) && destroyTagsForEntity ('ipv4rspool', $pool_id); |
5ad76f01 DO |
2907 | } |
2908 | ||
8253d9f0 DO |
2909 | function getRSList () |
2910 | { | |
c9066b1c DO |
2911 | $result = usePreparedSelectBlade |
2912 | ( | |
2913 | "select id, inservice, inet_ntoa(rsip) as rsip, rsport, rspool_id, rsconfig " . | |
2914 | "from IPv4RS order by rspool_id, IPv4RS.rsip, rsport" | |
2915 | ); | |
4b0932b6 | 2916 | $ret = array (); |
8253d9f0 | 2917 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
1f7d18fa | 2918 | foreach (array ('inservice', 'rsip', 'rsport', 'rspool_id', 'rsconfig') as $cname) |
4b0932b6 | 2919 | $ret[$row['id']][$cname] = $row[$cname]; |
4b0932b6 DO |
2920 | return $ret; |
2921 | } | |
2922 | ||
2923 | // Return the list of all currently configured load balancers with their pool count. | |
2924 | function getLBList () | |
2925 | { | |
c9066b1c DO |
2926 | $result = usePreparedSelectBlade |
2927 | ( | |
2928 | "select object_id, count(rspool_id) as poolcount " . | |
2929 | "from IPv4LB group by object_id order by object_id" | |
2930 | ); | |
4b0932b6 DO |
2931 | $ret = array (); |
2932 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
2933 | $ret[$row['object_id']] = $row['poolcount']; | |
4b0932b6 | 2934 | return $ret; |
8253d9f0 DO |
2935 | } |
2936 | ||
178fda20 | 2937 | // For the given object return: its vsconfig/rsconfig; the list of RS pools |
9e677cbd DO |
2938 | // attached (each with vsconfig/rsconfig in turn), each with the list of |
2939 | // virtual services terminating the pool. Each pool also lists all real | |
2940 | // servers with rsconfig. | |
2987fc1f | 2941 | function getSLBConfig ($object_id) |
9e677cbd | 2942 | { |
9e677cbd | 2943 | $ret = array(); |
e34dac4f DO |
2944 | $result = usePreparedSelectBlade |
2945 | ( | |
2946 | 'select vs_id, inet_ntoa(vip) as vip, vport, proto, vs.name as vs_name, ' . | |
d91f08f3 | 2947 | 'vs.vsconfig as vs_vsconfig, vs.rsconfig as vs_rsconfig, ' . |
1f54e1ba | 2948 | 'lb.vsconfig as lb_vsconfig, lb.rsconfig as lb_rsconfig, lb.prio as prio, pool.id as pool_id, pool.name as pool_name, ' . |
9e677cbd | 2949 | 'pool.vsconfig as pool_vsconfig, pool.rsconfig as pool_rsconfig, ' . |
d91f08f3 | 2950 | 'rs.id as rs_id, inet_ntoa(rsip) as rsip, rsport, rs.rsconfig as rs_rsconfig from ' . |
706ce117 DO |
2951 | 'IPv4LB as lb inner join IPv4RSPool as pool on lb.rspool_id = pool.id ' . |
2952 | 'inner join IPv4VS as vs on lb.vs_id = vs.id ' . | |
2953 | 'inner join IPv4RS as rs on lb.rspool_id = rs.rspool_id ' . | |
e34dac4f DO |
2954 | "where lb.object_id = ? and rs.inservice = 'yes' " . |
2955 | "order by vs.vip, vport, proto, pool.name, rs.rsip, rs.rsport", | |
2956 | array ($object_id) | |
2957 | ); | |
9e677cbd DO |
2958 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
2959 | { | |
d91f08f3 DO |
2960 | $vs_id = $row['vs_id']; |
2961 | if (!isset ($ret[$vs_id])) | |
2962 | { | |
1f54e1ba | 2963 | foreach (array ('vip', 'vport', 'proto', 'vs_name', 'vs_vsconfig', 'vs_rsconfig', 'lb_vsconfig', 'lb_rsconfig', 'pool_vsconfig', 'pool_rsconfig', 'pool_id', 'pool_name', 'prio') as $c) |
d91f08f3 DO |
2964 | $ret[$vs_id][$c] = $row[$c]; |
2965 | $ret[$vs_id]['rslist'] = array(); | |
2966 | } | |
2967 | foreach (array ('rsip', 'rsport', 'rs_rsconfig') as $c) | |
2968 | $ret[$vs_id]['rslist'][$row['rs_id']][$c] = $row[$c]; | |
9e677cbd | 2969 | } |
9e677cbd DO |
2970 | return $ret; |
2971 | } | |
1f7d18fa | 2972 | |
1f54e1ba DO |
2973 | function commitUpdateSLBDefConf ($data) |
2974 | { | |
2975 | return saveScript('DefaultVSConfig', $data['vs']) && | |
2976 | saveScript('DefaultRSConfig', $data['rs']); | |
2977 | } | |
2978 | ||
2979 | function getSLBDefaults ($do_cache_result = FALSE) { | |
2980 | static $ret = array(); | |
2981 | ||
2982 | if (! $do_cache_result) | |
2983 | $ret = array(); | |
2984 | elseif (! empty ($ret)) | |
2985 | return $ret; | |
2986 | ||
2987 | $ret['vs'] = loadScript('DefaultVSConfig'); | |
2988 | $ret['rs'] = loadScript('DefaultRSConfig'); | |
2989 | return $ret; | |
2990 | } | |
2991 | ||
1f7d18fa DO |
2992 | function commitSetInService ($rs_id = 0, $inservice = '') |
2993 | { | |
68a0bd8a | 2994 | if (! in_array ($inservice, array ('yes', 'no'))) |
c5f84f48 | 2995 | throw new InvalidArgException ('$inservice', $inservice); |
68a0bd8a | 2996 | return usePreparedUpdateBlade ('IPv4RS', array ('inservice' => $inservice), array ('id' => $rs_id)); |
1f7d18fa DO |
2997 | } |
2998 | ||
ad0e4754 DO |
2999 | function executeAutoPorts ($object_id = 0, $type_id = 0) |
3000 | { | |
c5f84f48 DY |
3001 | if ($object_id == 0) |
3002 | throw new InvalidArgException ('$object_id', $object_id); | |
3003 | if ($type_id == 0) | |
3004 | throw new InvalidArgException ('$type_id', $type_id); | |
118e4c38 | 3005 | foreach (getAutoPorts ($type_id) as $autoport) |
73a9a0a0 | 3006 | commitAddPort ($object_id, $autoport['name'], $autoport['type'], '', ''); |
ad0e4754 DO |
3007 | } |
3008 | ||
6e49bd1f | 3009 | // Return only implicitly listed tags, the rest of the chain will be |
a6e8d544 | 3010 | // generated/deducted later at higher levels. |
aab37bc0 | 3011 | // Result is a chain: randomly indexed taginfo list. |
f9bc186f | 3012 | function loadEntityTags ($entity_realm = '', $entity_id = 0) |
5967b83d | 3013 | { |
a6e8d544 | 3014 | $ret = array(); |
e34dac4f DO |
3015 | $result = usePreparedSelectBlade |
3016 | ( | |
3017 | "select tt.id, tag from " . | |
edf0993c | 3018 | "TagStorage as ts inner join TagTree as tt on ts.tag_id = tt.id " . |
e34dac4f DO |
3019 | "where entity_realm = ? and entity_id = ? " . |
3020 | "order by tt.tag", | |
3021 | array ($entity_realm, $entity_id) | |
3022 | ); | |
5967b83d | 3023 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
2fb24351 | 3024 | $ret[$row['id']] = $row; |
aab37bc0 | 3025 | return getExplicitTagsOnly ($ret); |
5967b83d DO |
3026 | } |
3027 | ||
351c0966 | 3028 | // Return a tag chain with all DB tags on it. |
5967b83d DO |
3029 | function getTagList () |
3030 | { | |
20c901a7 | 3031 | $ret = array(); |
e34dac4f DO |
3032 | $result = usePreparedSelectBlade |
3033 | ( | |
3034 | "select id, parent_id, tag, entity_realm as realm, count(entity_id) as refcnt " . | |
5b7bd02e | 3035 | "from TagTree left join TagStorage on id = tag_id " . |
e34dac4f DO |
3036 | "group by id, entity_realm order by tag" |
3037 | ); | |
3038 | // Collation index. The resulting rows are ordered according to default collation, | |
fb7a4967 | 3039 | // which is utf8_general_ci for UTF-8. |
e34dac4f | 3040 | $ci = 0; |
5967b83d | 3041 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) |
5b7bd02e | 3042 | { |
7cc02fc1 | 3043 | if (!isset ($ret[$row['id']])) |
20c901a7 | 3044 | $ret[$row['id']] = array |
5b7bd02e DO |
3045 | ( |
3046 | 'id' => $row['id'], | |
3047 | 'tag' => $row['tag'], | |
fb7a4967 | 3048 | 'ci' => $ci++, |
5b7bd02e | 3049 | 'parent_id' => $row['parent_id'], |
9e51318b | 3050 | 'refcnt' => array ('total' => 0) |
5b7bd02e DO |
3051 | ); |
3052 | if ($row['realm']) | |
9e51318b | 3053 | { |
20c901a7 | 3054 | $ret[$row['id']]['refcnt'][$row['realm']] = $row['refcnt']; |
9e51318b | 3055 | $ret[$row['id']]['refcnt']['total'] += $row['refcnt']; |
21ee3351 AA |
3056 | // introduce the 'pseudo'-ream 'ipnet' which combines 'ipv4net' and 'ipv6net' realms. |
3057 | if ($row['realm'] == 'ipv4net' || $row['realm'] == 'ipv6net') | |
3058 | if (isset ($ret[$row['id']]['refcnt']['ipnet'])) | |
3059 | $ret[$row['id']]['refcnt']['ipnet'] += $row['refcnt']; | |
3060 | else | |
3061 | $ret[$row['id']]['refcnt']['ipnet'] = $row['refcnt']; | |
9e51318b | 3062 | } |
5b7bd02e | 3063 | } |
20c901a7 | 3064 | return $ret; |
5967b83d DO |
3065 | } |
3066 | ||
01b6b4d6 DO |
3067 | // Drop the whole chain stored. |
3068 | function destroyTagsForEntity ($entity_realm, $entity_id) | |
aedf18af | 3069 | { |
e34dac4f | 3070 | return usePreparedDeleteBlade ('TagStorage', array ('entity_realm' => $entity_realm, 'entity_id' => $entity_id)); |
aedf18af DO |
3071 | } |
3072 | ||
01b6b4d6 | 3073 | // Drop only one record. This operation doesn't involve retossing other tags, unlike when adding. |
29c3a4d8 DO |
3074 | // FIXME: this function could be used by 3rd-party scripts, dismiss it at some later point, |
3075 | // but not now. | |
01b6b4d6 DO |
3076 | function deleteTagForEntity ($entity_realm, $entity_id, $tag_id) |
3077 | { | |
e34dac4f | 3078 | return usePreparedDeleteBlade ('TagStorage', array ('entity_realm' => $entity_realm, 'entity_id' => $entity_id, 'tag_id' => $tag_id)); |
01b6b4d6 DO |
3079 | } |
3080 | ||
351c0966 | 3081 | // Push a record into TagStorage unconditionally. |
e5074ada | 3082 | function addTagForEntity ($realm, $entity_id, $tag_id) |
eb6ea26f | 3083 | { |
21ee3351 | 3084 | global $SQLSchema; |
e5074ada DO |
3085 | if (! array_key_exists ($realm, $SQLSchema)) |
3086 | throw new InvalidArgException ('realm', $realm); | |
3087 | // spotEntity ($realm, $entity_id) would be a more expensive way | |
3088 | // to validate two parameters | |
e34dac4f | 3089 | return usePreparedInsertBlade |
eb6ea26f DO |
3090 | ( |
3091 | 'TagStorage', | |
3092 | array | |
3093 | ( | |
e34dac4f | 3094 | 'entity_realm' => $realm, |
120e9ddd | 3095 | 'entity_id' => $entity_id, |
eb6ea26f DO |
3096 | 'tag_id' => $tag_id, |
3097 | ) | |
3098 | ); | |
3099 | } | |
3100 | ||
351c0966 | 3101 | // Add records into TagStorage, if this makes sense (IOW, they don't appear |
abef7149 DO |
3102 | // on the implicit list already). Then remove any other records, which |
3103 | // appear on the "implicit" side of the chain. This will make sure, | |
351c0966 DO |
3104 | // that both the tag base is still minimal and all requested tags appear on |
3105 | // the resulting tag chain. | |
abef7149 DO |
3106 | // Return TRUE, if any changes were committed. |
3107 | function rebuildTagChainForEntity ($realm, $entity_id, $extrachain = array()) | |
3108 | { | |
3109 | // Put the current explicit sub-chain into a buffer and merge all tags from | |
3110 | // the extra chain, which aren't there yet. | |
3111 | $newchain = $oldchain = loadEntityTags ($realm, $entity_id); | |
3112 | foreach ($extrachain as $extratag) | |
3113 | if (!tagOnChain ($extratag, $newchain)) | |
3114 | $newchain[] = $extratag; | |
3115 | // Then minimize the working buffer and check if it differs from the original | |
3116 | // chain we started with. If it is so, save the work and signal the upper layer. | |
3117 | $newchain = getExplicitTagsOnly ($newchain); | |
3118 | if (tagChainCmp ($oldchain, $newchain)) | |
3119 | { | |
01b6b4d6 | 3120 | destroyTagsForEntity ($realm, $entity_id); |
abef7149 DO |
3121 | foreach ($newchain as $taginfo) |
3122 | addTagForEntity ($realm, $entity_id, $taginfo['id']); | |
3123 | return TRUE; | |
3124 | } | |
3125 | return FALSE; | |
351c0966 DO |
3126 | } |
3127 | ||
3128 | // Presume, that the target record has no tags attached. | |
3129 | function produceTagsForLastRecord ($realm, $tagidlist, $last_insert_id = 0) | |
3130 | { | |
c63a8d6e DO |
3131 | if (!$last_insert_id) |
3132 | $last_insert_id = lastInsertID(); | |
abef7149 | 3133 | foreach (getExplicitTagsOnly (buildTagChainFromIds ($tagidlist)) as $taginfo) |
ef5bb52c | 3134 | addTagForEntity ($realm, $last_insert_id, $taginfo['id']); |
c63a8d6e DO |
3135 | } |
3136 | ||
42023f03 DO |
3137 | function createIPv4Prefix ($range = '', $name = '', $is_bcast = FALSE, $taglist = array()) |
3138 | { | |
3139 | // $range is in x.x.x.x/x format, split into ip/mask vars | |
3140 | $rangeArray = explode('/', $range); | |
3141 | if (count ($rangeArray) != 2) | |
ef5bb52c | 3142 | throw new InvalidRequestArgException ('range', $range, 'Invalid IPv4 prefix'); |
42023f03 DO |
3143 | $ip = $rangeArray[0]; |
3144 | $mask = $rangeArray[1]; | |
3145 | ||
59a83bd8 | 3146 | if (!strlen ($ip) or !strlen ($mask)) |
ef5bb52c | 3147 | throw new InvalidRequestArgException ('range', $range, 'Invalid IPv4 prefix'); |
42023f03 DO |
3148 | $ipL = ip2long($ip); |
3149 | $maskL = ip2long($mask); | |
3150 | if ($ipL == -1 || $ipL === FALSE) | |
ef5bb52c | 3151 | throw new InvalidRequestArgException ('range', $range, 'Invalid IPv4 address'); |
42023f03 DO |
3152 | if ($mask < 32 && $mask > 0) |
3153 | $maskL = $mask; | |
3154 | else | |
3155 | { | |
3156 | $maskB = decbin($maskL); | |
3157 | if (strlen($maskB)!=32) | |
ef5bb52c | 3158 | throw new InvalidRequestArgException ('range', $range, 'Invalid netmask'); |
42023f03 DO |
3159 | $ones=0; |
3160 | $zeroes=FALSE; | |
3161 | foreach( str_split ($maskB) as $digit) | |
3162 | { | |
3163 | if ($digit == '0') | |
3164 | $zeroes = TRUE; | |
3165 | if ($digit == '1') | |
3166 | { | |
3167 | $ones++; | |
3168 | if ($zeroes == TRUE) | |
ef5bb52c | 3169 | throw new InvalidRequestArgException ('range', $range, 'Invalid netmask'); |
42023f03 DO |
3170 | } |
3171 | } | |
3172 | $maskL = $ones; | |
3173 | } | |
3174 | $binmask = binMaskFromDec($maskL); | |
3175 | $ipL = $ipL & $binmask; | |
ef5bb52c | 3176 | usePreparedInsertBlade |
42023f03 | 3177 | ( |
706ce117 | 3178 | 'IPv4Network', |
42023f03 DO |
3179 | array |
3180 | ( | |
3181 | 'ip' => sprintf ('%u', $ipL), | |
357eb2ea DO |
3182 | 'mask' => $maskL, |
3183 | 'name' => $name | |
42023f03 DO |
3184 | ) |
3185 | ); | |
3186 | ||
3187 | if ($is_bcast and $maskL < 31) | |
3188 | { | |
3189 | $network_addr = long2ip ($ipL); | |
3190 | $broadcast_addr = long2ip ($ipL | binInvMaskFromDec ($maskL)); | |
21ee3351 AA |
3191 | updateV4Address ($network_addr, 'network', 'yes'); |
3192 | updateV4Address ($broadcast_addr, 'broadcast', 'yes'); | |
42023f03 | 3193 | } |
ef5bb52c | 3194 | produceTagsForLastRecord ('ipv4net', $taglist); |
42023f03 DO |
3195 | } |
3196 | ||
21ee3351 AA |
3197 | function createIPv6Prefix ($range = '', $name = '', $taglist = array()) |
3198 | { | |
3199 | // $range is in aaa0:b::c:d/x format, split into ip/mask vars | |
3200 | $rangeArray = explode ('/', $range); | |
3201 | if (count ($rangeArray) != 2) | |
ef5bb52c | 3202 | throw new InvalidRequestArgException ('range', $range, 'Invalid IPv6 prefix'); |
21ee3351 AA |
3203 | $ip = $rangeArray[0]; |
3204 | $mask = $rangeArray[1]; | |
3205 | $address = new IPv6Address; | |
3206 | if (!strlen ($ip) or !strlen ($mask) or ! $address->parse ($ip)) | |
ef5bb52c | 3207 | throw new InvalidRequestArgException ('range', $range, 'Invalid IPv4 prefix'); |
21ee3351 AA |
3208 | $network_addr = $address->get_first_subnet_address ($mask); |
3209 | $broadcast_addr = $address->get_last_subnet_address ($mask); | |
3210 | if (! $network_addr || ! $broadcast_addr) | |
ef5bb52c DO |
3211 | throw new InvalidRequestArgException ('range', $range, 'Invalid netmask'); |
3212 | usePreparedInsertBlade | |
21ee3351 AA |
3213 | ( |
3214 | 'IPv6Network', | |
3215 | array | |
3216 | ( | |
522b6f90 AA |
3217 | 'ip' => $network_addr->getBin(), |
3218 | 'last_ip' => $broadcast_addr->getBin(), | |
21ee3351 AA |
3219 | 'mask' => $mask, |
3220 | 'name' => $name | |
3221 | ) | |
3222 | ); | |
ef5bb52c | 3223 | produceTagsForLastRecord ('ipv6net', $taglist); |
21ee3351 AA |
3224 | } |
3225 | ||
706ce117 | 3226 | // FIXME: This function doesn't wipe relevant records from IPv4Address table. |
ef5bb52c | 3227 | function destroyIPv4Prefix ($id) |
42023f03 | 3228 | { |
6657739e | 3229 | releaseFiles ('ipv4net', $id); |
ef5bb52c DO |
3230 | usePreparedDeleteBlade ('IPv4Network', array ('id' => $id)); |
3231 | destroyTagsForEntity ('ipv4net', $id); | |
42023f03 DO |
3232 | } |
3233 | ||
21ee3351 | 3234 | // FIXME: This function doesn't wipe relevant records from IPv6Address table. |
ef5bb52c | 3235 | function destroyIPv6Prefix ($id) |
21ee3351 AA |
3236 | { |
3237 | releaseFiles ('ipv6net', $id); | |
ef5bb52c DO |
3238 | usePreparedDeleteBlade ('IPv6Network', array ('id' => $id)); |
3239 | destroyTagsForEntity ('ipv6net', $id); | |
21ee3351 AA |
3240 | } |
3241 | ||
ebae41b3 DO |
3242 | function loadScript ($name) |
3243 | { | |
e34dac4f | 3244 | $result = usePreparedSelectBlade ("select script_text from Script where script_name = ?", array ($name)); |
ebae41b3 | 3245 | $row = $result->fetch (PDO::FETCH_NUM); |
4a6a28f1 DO |
3246 | if ($row !== FALSE) |
3247 | return $row[0]; | |
3248 | else | |
3249 | return NULL; | |
ebae41b3 DO |
3250 | } |
3251 | ||
178fda20 | 3252 | function saveScript ($name = '', $text) |
ebae41b3 | 3253 | { |
59a83bd8 | 3254 | if (!strlen ($name)) |
c5f84f48 | 3255 | throw new InvalidArgException ('$name', $name); |
bb09f49c | 3256 | return usePreparedExecuteBlade |
ebae41b3 | 3257 | ( |
bb09f49c DO |
3258 | 'INSERT INTO Script (script_name, script_text) VALUES (?, ?) ' . |
3259 | 'ON DUPLICATE KEY UPDATE script_text=?', | |
3260 | array ($name, $text, $text) | |
ebae41b3 DO |
3261 | ); |
3262 | } | |
3263 | ||
d983f70a DO |
3264 | function newPortForwarding ($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description) |
3265 | { | |
3266 | if (NULL === getIPv4AddressNetworkId ($localip)) | |
3267 | return "$localip: Non existant ip"; | |
0929db7f | 3268 | if (NULL === getIPv4AddressNetworkId ($remoteip)) |
d983f70a DO |
3269 | return "$remoteip: Non existant ip"; |
3270 | if ( ($localport <= 0) or ($localport >= 65536) ) | |
3271 | return "$localport: invaild port"; | |
3272 | if ( ($remoteport <= 0) or ($remoteport >= 65536) ) | |
3273 | return "$remoteport: invaild port"; | |
3274 | ||
a5c589d2 | 3275 | $result = usePreparedExecuteBlade |
d983f70a | 3276 | ( |
a5c589d2 DO |
3277 | 'INSERT INTO IPv4NAT (object_id, localip, remoteip, localport, remoteport, proto, description) ' . |
3278 | 'VALUES (?, INET_ATON(?), INET_ATON(?), ?, ?, ?, ?)', | |
d983f70a DO |
3279 | array |
3280 | ( | |
a5c589d2 DO |
3281 | $object_id, |
3282 | $localip, | |
3283 | $remoteip, | |
3284 | $localport, | |
3285 | $remoteport, | |
3286 | $proto, | |
3287 | $description, | |
d983f70a DO |
3288 | ) |
3289 | ); | |
a5c589d2 | 3290 | return $result !== FALSE ? '' : (__FUNCTION__ . '(): failed to insert the rule'); |
d983f70a DO |
3291 | } |
3292 | ||
3293 | function deletePortForwarding ($object_id, $localip, $localport, $remoteip, $remoteport, $proto) | |
3294 | { | |
a5c589d2 DO |
3295 | return usePreparedExecuteBlade |
3296 | ( | |
3297 | 'DELETE FROM IPv4NAT WHERE object_id=? AND localip=INET_ATON(?) AND ' . | |
3298 | 'remoteip=INET_ATON(?) AND localport=? AND remoteport=? AND proto=?', | |
3299 | array ($object_id, $localip, $remoteip, $localport, $remoteport, $proto) | |
3300 | ); | |
d983f70a DO |
3301 | } |
3302 | ||
3303 | function updatePortForwarding ($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description) | |
3304 | { | |
a5c589d2 DO |
3305 | return usePreparedExecuteBlade |
3306 | ( | |
3307 | 'UPDATE IPv4NAT SET description=? WHERE object_id=? AND localip=INET_ATON(?) AND remoteip=INET_ATON(?) ' . | |
3308 | 'AND localport=? AND remoteport=? AND proto=?', | |
3309 | array ($description, $object_id, $localip, $remoteip, $localport, $remoteport, $proto) | |
3310 | ); | |
d983f70a DO |
3311 | } |
3312 | ||
3313 | function getNATv4ForObject ($object_id) | |
3314 | { | |
3315 | $ret = array(); | |
3316 | $ret['out'] = array(); | |
3317 | $ret['in'] = array(); | |
e34dac4f DO |
3318 | $result = usePreparedSelectBlade |
3319 | ( | |
d983f70a DO |
3320 | "select ". |
3321 | "proto, ". | |
3322 | "INET_NTOA(localip) as localip, ". | |
3323 | "localport, ". | |
3324 | "INET_NTOA(remoteip) as remoteip, ". | |
3325 | "remoteport, ". | |
3326 | "ipa1.name as local_addr_name, " . | |
3327 | "ipa2.name as remote_addr_name, " . | |
3328 | "description ". | |
706ce117 DO |
3329 | "from IPv4NAT ". |
3330 | "left join IPv4Address as ipa1 on IPv4NAT.localip = ipa1.ip " . | |
3331 | "left join IPv4Address as ipa2 on IPv4NAT.remoteip = ipa2.ip " . | |
e34dac4f DO |
3332 | "where object_id=? ". |
3333 | "order by localip, localport, proto, remoteip, remoteport", | |
3334 | array ($object_id) | |
3335 | ); | |
d983f70a DO |
3336 | $count=0; |
3337 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
3338 | { | |
3339 | foreach (array ('proto', 'localport', 'localip', 'remoteport', 'remoteip', 'description', 'local_addr_name', 'remote_addr_name') as $cname) | |
3340 | $ret['out'][$count][$cname] = $row[$cname]; | |
3341 | $count++; | |
3342 | } | |
d983f70a DO |
3343 | unset ($result); |
3344 | ||
e34dac4f DO |
3345 | $result = usePreparedSelectBlade |
3346 | ( | |
d983f70a DO |
3347 | "select ". |
3348 | "proto, ". | |
3349 | "INET_NTOA(localip) as localip, ". | |
3350 | "localport, ". | |
3351 | "INET_NTOA(remoteip) as remoteip, ". | |
3352 | "remoteport, ". | |
706ce117 | 3353 | "IPv4NAT.object_id as object_id, ". |
d983f70a DO |
3354 | "RackObject.name as object_name, ". |
3355 | "description ". | |
706ce117 | 3356 | "from ((IPv4NAT join IPv4Allocation on remoteip=IPv4Allocation.ip) join RackObject on IPv4NAT.object_id=RackObject.id) ". |
e34dac4f DO |
3357 | "where IPv4Allocation.object_id=? ". |
3358 | "order by remoteip, remoteport, proto, localip, localport", | |
3359 | array ($object_id) | |
3360 | ); | |
d983f70a DO |
3361 | $count=0; |
3362 | while ($row = $result->fetch (PDO::FETCH_ASSOC)) | |
3363 | { | |
3364 | foreach (array ('proto', 'localport', 'localip', 'remoteport', 'remoteip', 'object_id', 'object_name', 'description') as $cname) | |
3365 | $ret['in'][$count][$cname] = $row[$cname]; | |
3366 | $count++; | |
3367 | } | |
d983f70a DO |
3368 | return $ret; |
3369 | } | |
3370 | ||
178fda20 | 3371 | // Return a list of files, which are not linked to the specified record. This list |
121496b6 | 3372 | // will be used by printSelect(). |
e1ae3fb4 AD |
3373 | function getAllUnlinkedFiles ($entity_type = NULL, $entity_id = 0) |
3374 | { | |
e34dac4f DO |
3375 | $query = usePreparedSelectBlade |
3376 | ( | |
121496b6 | 3377 | 'SELECT id, name FROM File ' . |
e1ae3fb4 | 3378 | 'WHERE id NOT IN (SELECT file_id FROM FileLink WHERE entity_type = ? AND entity_id = ?) ' . |
e34dac4f DO |
3379 | 'ORDER BY name, id', |
3380 | array ($entity_type, $entity_id) | |
3381 | ); | |
e1ae3fb4 | 3382 | $ret=array(); |
e1ae3fb4 | 3383 | while ($row = $query->fetch (PDO::FETCH_ASSOC)) |
121496b6 | 3384 | $ret[$row['id']] = $row['name']; |
e1ae3fb4 AD |
3385 | return $ret; |
3386 | } | |
3387 | ||
6df2025d DO |
3388 | // FIXME: return a standard cell list, so upper layer can iterate over |
3389 | // it conveniently. | |
e1ae3fb4 AD |
3390 | function getFilesOfEntity ($entity_type = NULL, $entity_id = 0) |
3391 | { | |
e34dac4f DO |
3392 | $query = usePreparedSelectBlade |
3393 | ( | |
e1ae3fb4 AD |
3394 | 'SELECT FileLink.file_id, FileLink.id AS link_id, name, type, size, ctime, mtime, atime, comment ' . |
3395 | 'FROM FileLink LEFT JOIN File ON FileLink.file_id = File.id ' . | |
e34dac4f DO |
3396 | 'WHERE FileLink.entity_type = ? AND FileLink.entity_id = ? ORDER BY name', |
3397 | array ($entity_type, $entity_id) | |
3398 | ); | |
e1ae3fb4 AD |
3399 | $ret = array(); |
3400 | while ($row = $query->fetch (PDO::FETCH_ASSOC)) | |
3401 | $ret[$row['file_id']] = array ( | |
b3567368 | 3402 | 'id' => $row['file_id'], |
e1ae3fb4 AD |
3403 | 'link_id' => $row['link_id'], |
3404 | 'name' => $row['name'], | |
3405 | 'type' => $row['type'], | |
3406 | 'size' => $row['size'], | |
3407 | 'ctime' => $row['ctime'], | |
3408 | 'mtime' => $row['mtime'], | |
3409 | 'atime' => $row['atime'], | |
3410 | 'comment' => $row['comment'], | |
3411 | ); | |
3412 | return $ret; | |
3413 | } | |
3414 | ||
04d16213 | 3415 | function getFile ($file_id) |
e1ae3fb4 | 3416 | { |
6cfedb71 DO |
3417 | $query = usePreparedSelectBlade |
3418 | ( | |
3419 | 'SELECT id, name, type, size, ctime, mtime, atime, contents, comment ' . | |
3420 | 'FROM File WHERE id = ?', | |
3421 | array ($file_id) | |
3422 | ); | |
e1ae3fb4 | 3423 | if (($row = $query->fetch (PDO::FETCH_ASSOC)) == NULL) |
04d16213 DO |
3424 | // FIXME: isn't this repeating the code already in spotEntity()? |
3425 | throw new EntityNotFoundException ('file', $file_id); | |
e1ae3fb4 AD |
3426 | else |
3427 | { | |
3428 | $ret = array(); | |
3429 | $ret['id'] = $row['id']; | |
3430 | $ret['name'] = $row['name']; | |
3431 | $ret['type'] = $row['type']; | |
3432 | $ret['size'] = $row['size']; | |
3433 | $ret['ctime'] = $row['ctime']; | |
3434 | $ret['mtime'] = $row['mtime']; | |
3435 | $ret['atime'] = $row['atime']; | |
3436 | $ret['contents'] = $row['contents']; | |
3437 | $ret['comment'] = $row['comment']; | |
d2eb8399 | 3438 | unset ($query); |
e1ae3fb4 | 3439 | |
deb3da1b | 3440 | // Someone accessed this file, update atime |
50ad8c87 | 3441 | usePreparedExecuteBlade ('UPDATE File SET atime = NOW() WHERE id = ?', array ($file_id)); |
e1ae3fb4 | 3442 | } |
e1ae3fb4 AD |
3443 | return $ret; |
3444 | } | |
3445 | ||
04d16213 | 3446 | function getFileCache ($file_id) |
d3346ce2 DO |
3447 | { |
3448 | $query = usePreparedSelectBlade | |
3449 | ( | |
3450 | 'SELECT File.thumbnail FROM File ' . | |
3451 | 'WHERE File.id = ? and File.thumbnail IS NOT NULL', | |
3452 | array ($file_id) | |
3453 | ); | |
3454 | if (($row = $query->fetch (PDO::FETCH_ASSOC)) == NULL) | |
3455 | return FALSE; | |
50ad8c87 | 3456 | $ret = $row['thumbnail']; |
d3346ce2 | 3457 | $query->CloseCursor(); |
50ad8c87 | 3458 | usePreparedExecuteBlade ('UPDATE File SET atime = NOW() WHERE id = ?', array ($file_id)); |
d3346ce2 DO |
3459 | return $ret; |
3460 | } | |
3461 | ||
3462 | function commitAddFileCache ($file_id, $contents) | |
3463 | { | |
3464 | global $dbxlink; | |
3465 | $query = $dbxlink->prepare('UPDATE File SET thumbnail = ? WHERE id = ?'); | |
3466 | $query->bindParam(1, $contents, PDO::PARAM_LOB); | |
3467 | $query->bindParam(2, $file_id); | |
3468 | try | |
3469 | { | |
3470 | return $query->execute(); | |
3471 | } | |
3472 | catch (PDOException $e) | |
3473 | { | |
3474 | throw convertPDOException ($e); | |
3475 | } | |
3476 | } | |
3477 | ||
04d16213 | 3478 | function getFileLinks ($file_id) |
e1ae3fb4 | 3479 | { |
6cfedb71 DO |
3480 | $query = usePreparedSelectBlade |
3481 | ( | |
3482 | 'SELECT id, file_id, entity_type, entity_id FROM FileLink ' . | |
3483 | 'WHERE file_id = ? ORDER BY entity_type, entity_id', | |
3484 | array ($file_id) | |
3485 | ); | |
da3302ab | 3486 | $rows = $query->fetchAll (PDO::FETCH_ASSOC); |
e1ae3fb4 | 3487 | $ret = array(); |
da3302ab | 3488 |