r1765 + more tag functions
[racktables] / inc / database.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3*
4* This file is a library of database access functions for RackTables.
5*
6*/
7
5f54485b 8function escapeString ($value, $do_db_escape = TRUE)
e673ee24 9{
6c6f62e8 10 $ret = htmlentities ($value, ENT_QUOTES, 'UTF-8');
5f54485b
DO
11 if ($do_db_escape)
12 {
13 global $dbxlink;
14 $ret = substr ($dbxlink->quote ($ret), 1, -1);
15 }
16 return $ret;
e673ee24
DO
17}
18
19// This function returns detailed information about either all or one
20// rack row depending on its argument.
21function getRackRowInfo ($rackrow_id = 0)
22{
e673ee24 23 $query =
5332840f
DO
24 "select dict_key, dict_value, count(Rack.id) as count, " .
25 "if(isnull(sum(Rack.height)),0,sum(Rack.height)) as sum " .
26 "from Chapter natural join Dictionary left join Rack on Rack.row_id = dict_key " .
e673ee24
DO
27 "where chapter_name = 'RackRow' " .
28 ($rackrow_id > 0 ? "and dict_key = ${rackrow_id} " : '') .
29 "group by dict_key order by dict_value";
e425f895 30 $result = useSelectBlade ($query);
e673ee24
DO
31 $ret = array();
32 $clist = array ('dict_key', 'dict_value', 'count', 'sum');
33 while ($row = $result->fetch (PDO::FETCH_ASSOC))
34 foreach ($clist as $dummy => $cname)
35 $ret[$row['dict_key']][$cname] = $row[$cname];
36 $result->closeCursor();
37 if ($rackrow_id > 0)
38 return current ($ret);
39 else
40 return $ret;
41}
42
43// This function returns id->name map for all object types. The map is used
44// to build <select> input for objects.
45function getObjectTypeList ()
46{
47 return readChapter ('RackObjectType');
48}
49
50function getObjectList ($type_id = 0)
51{
e673ee24
DO
52 $query =
53 "select distinct RackObject.id as id , RackObject.name as name, dict_value as objtype_name, " .
54 "RackObject.label as label, RackObject.barcode as barcode, " .
55 "dict_key as objtype_id, asset_no, rack_id, Rack.name as Rack_name from " .
56 "((RackObject inner join Dictionary on objtype_id=dict_key natural join Chapter) " .
57 "left join RackSpace on RackObject.id = object_id) " .
58 "left join Rack on rack_id = Rack.id " .
59 "where objtype_id = '${type_id}' and RackObject.deleted = 'no' " .
60 "and chapter_name = 'RackObjectType' order by name";
e425f895 61 $result = useSelectBlade ($query);
e673ee24 62 $ret = array();
e673ee24
DO
63 while ($row = $result->fetch (PDO::FETCH_ASSOC))
64 {
2ac47ae4
DO
65 foreach (array (
66 'id',
67 'name',
68 'label',
69 'barcode',
70 'objtype_name',
71 'objtype_id',
72 'asset_no',
73 'rack_id',
74 'Rack_name'
75 ) as $cname)
e673ee24
DO
76 $ret[$row['id']][$cname] = $row[$cname];
77 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
78 }
79 $result->closeCursor();
80 return $ret;
81}
82
83function getRacksForRow ($row_id = 0)
84{
e673ee24 85 $query =
1c81a02c 86 "select Rack.id, Rack.name, height, Rack.comment, row_id, " .
2d7698a2 87 "'yes' as left_is_front, 'yes' as bottom_is_unit1, dict_value as row_name " .
e673ee24
DO
88 "from Rack left join Dictionary on row_id = dict_key natural join Chapter " .
89 "where chapter_name = 'RackRow' and Rack.deleted = 'no' " .
90 (($row_id == 0) ? "" : "and row_id = ${row_id} ") .
91 "order by row_name, Rack.id";
e425f895 92 $result = useSelectBlade ($query);
e673ee24 93 $ret = array();
1c81a02c
DO
94 $clist = array
95 (
96 'id',
97 'name',
98 'height',
99 'comment',
100 'row_id',
101 'left_is_front',
102 'bottom_is_unit1',
103 'row_name'
104 );
e673ee24 105 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1c81a02c 106 foreach ($clist as $cname)
e673ee24
DO
107 $ret[$row['id']][$cname] = $row[$cname];
108 $result->closeCursor();
6a3a37b2 109 usort ($ret, 'sortRacks');
e673ee24
DO
110 $ret = restoreRackIDs ($ret);
111 return $ret;
112}
113
114// This is a popular helper for getting information about
115// a particular rack and its rackspace at once.
116function getRackData ($rack_id = 0, $silent = FALSE)
117{
118 if ($rack_id == 0)
119 {
120 if ($silent == FALSE)
f41b492f 121 showError ('Invalid rack_id', __FUNCTION__);
e673ee24
DO
122 return NULL;
123 }
e673ee24 124 $query =
1c81a02c 125 "select Rack.id, Rack.name, row_id, height, Rack.comment, " .
2d7698a2 126 "'yes' as left_is_front, 'yes' as bottom_is_unit1, dict_value as row_name from " .
e673ee24
DO
127 "Rack left join Dictionary on Rack.row_id = dict_key natural join Chapter " .
128 "where chapter_name = 'RackRow' and Rack.id='${rack_id}' and Rack.deleted = 'no' limit 1";
e425f895 129 $result1 = useSelectBlade ($query);
e673ee24
DO
130 if (($row = $result1->fetch (PDO::FETCH_ASSOC)) == NULL)
131 {
132 if ($silent == FALSE)
f41b492f 133 showError ('Query #1 succeded, but returned no data', __FUNCTION__);
e673ee24
DO
134 return NULL;
135 }
136
137 // load metadata
1c81a02c
DO
138 $clist = array
139 (
140 'id',
141 'name',
142 'height',
143 'comment',
144 'row_id',
145 'left_is_front',
146 'bottom_is_unit1',
147 'row_name'
148 );
149 foreach ($clist as $cname)
150 $rack[$cname] = $row[$cname];
e673ee24
DO
151 $result1->closeCursor();
152
153 // start with default rackspace
154 for ($i = $rack['height']; $i > 0; $i--)
155 for ($locidx = 0; $locidx < 3; $locidx++)
156 $rack[$i][$locidx]['state'] = 'F';
157
158 // load difference
159 $query =
160 "select unit_no, atom, state, object_id " .
161 "from RackSpace where rack_id = ${rack_id} and " .
162 "unit_no between 1 and " . $rack['height'] . " order by unit_no";
e425f895 163 $result2 = useSelectBlade ($query);
e673ee24
DO
164 global $loclist;
165 while ($row = $result2->fetch (PDO::FETCH_ASSOC))
166 {
167 $rack[$row['unit_no']][$loclist[$row['atom']]]['state'] = $row['state'];
168 $rack[$row['unit_no']][$loclist[$row['atom']]]['object_id'] = $row['object_id'];
169 }
170 $result2->closeCursor();
171 return $rack;
172}
173
174// This is a popular helper.
175function getObjectInfo ($object_id = 0)
176{
177 if ($object_id == 0)
178 {
f41b492f 179 showError ('Invalid object_id', __FUNCTION__);
e673ee24
DO
180 return;
181 }
e673ee24
DO
182 $query =
183 "select id, name, label, barcode, dict_value as objtype_name, asset_no, dict_key as objtype_id, has_problems, comment from " .
184 "RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter " .
185 "where id = '${object_id}' and deleted = 'no' and chapter_name = 'RackObjectType' limit 1";
e425f895 186 $result = useSelectBlade ($query);
e673ee24
DO
187 if (($row = $result->fetch (PDO::FETCH_ASSOC)) == NULL)
188 {
f41b492f 189 showError ('Query succeded, but returned no data', __FUNCTION__);
e673ee24
DO
190 $ret = NULL;
191 }
192 else
193 {
194 $ret['id'] = $row['id'];
195 $ret['name'] = $row['name'];
196 $ret['label'] = $row['label'];
197 $ret['barcode'] = $row['barcode'];
198 $ret['objtype_name'] = $row['objtype_name'];
199 $ret['objtype_id'] = $row['objtype_id'];
200 $ret['has_problems'] = $row['has_problems'];
201 $ret['asset_no'] = $row['asset_no'];
202 $ret['dname'] = displayedName ($ret);
203 $ret['comment'] = $row['comment'];
204 }
205 $result->closeCursor();
206 return $ret;
207}
208
209function getPortTypes ()
210{
fe6e3bd7 211 return readChapter ('PortType');
e673ee24
DO
212}
213
214function getObjectPortsAndLinks ($object_id = 0)
215{
216 if ($object_id == 0)
217 {
f41b492f 218 showError ('Invalid object_id', __FUNCTION__);
e673ee24
DO
219 return;
220 }
e673ee24
DO
221 $query =
222 "select Port.id as Port_id, ".
223 "Port.name as Port_name, ".
224 "Port.label as Port_label, ".
225 "Port.l2address as Port_l2address, ".
226 "Port.type as Port_type, ".
227 "Port.reservation_comment as Port_reservation_comment, " .
228 "dict_value as PortType_name, ".
229 "RemotePort.id as RemotePort_id, ".
230 "RemotePort.name as RemotePort_name, ".
231 "RemotePort.object_id as RemotePort_object_id, ".
232 "RackObject.name as RackObject_name ".
233 "from (".
234 "(".
235 "(".
236 "Port inner join Dictionary on Port.type = dict_key natural join Chapter".
237 ") ".
238 "left join Link on Port.id=Link.porta or Port.id=Link.portb ".
239 ") ".
240 "left join Port as RemotePort on Link.portb=RemotePort.id or Link.porta=RemotePort.id ".
241 ") ".
242 "left join RackObject on RemotePort.object_id=RackObject.id ".
243 "where chapter_name = 'PortType' and Port.object_id=${object_id} ".
244 "and (Port.id != RemotePort.id or RemotePort.id is null) ".
245 "order by Port_name";
e425f895
DO
246 $result = useSelectBlade ($query);
247 $ret=array();
248 $count=0;
249 while ($row = $result->fetch (PDO::FETCH_ASSOC))
e673ee24 250 {
e425f895
DO
251 $ret[$count]['id'] = $row['Port_id'];
252 $ret[$count]['name'] = $row['Port_name'];
253 $ret[$count]['l2address'] = l2addressFromDatabase ($row['Port_l2address']);
254 $ret[$count]['label'] = $row['Port_label'];
255 $ret[$count]['type_id'] = $row['Port_type'];
256 $ret[$count]['type'] = $row['PortType_name'];
257 $ret[$count]['reservation_comment'] = $row['Port_reservation_comment'];
258 $ret[$count]['remote_id'] = $row['RemotePort_id'];
259 $ret[$count]['remote_name'] = htmlentities ($row['RemotePort_name'], ENT_QUOTES);
260 $ret[$count]['remote_object_id'] = $row['RemotePort_object_id'];
261 $ret[$count]['remote_object_name'] = $row['RackObject_name'];
262 // Save on displayedName() calls.
263 if (empty ($row['RackObject_name']) and !empty ($row['RemotePort_object_id']))
e673ee24 264 {
e425f895
DO
265 $oi = getObjectInfo ($row['RemotePort_object_id']);
266 $ret[$count]['remote_object_name'] = displayedName ($oi);
e673ee24 267 }
e425f895 268 $count++;
e673ee24
DO
269 }
270 $result->closeCursor();
271 return $ret;
272}
273
274function commitAddRack ($name, $height, $row_id, $comment)
275{
276 global $dbxlink;
277 $query = "insert into Rack(row_id, name, height, comment) values('${row_id}', '${name}', '${height}', '${comment}')";
278 $result1 = $dbxlink->query ($query);
279 if ($result1 == NULL)
280 {
f41b492f 281 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
282 return FALSE;
283 }
284 // last_insert_id() is MySQL-specific
285 $query = 'select last_insert_id()';
286 $result2 = $dbxlink->query ($query);
287 if ($result2 == NULL)
288 {
f41b492f 289 showError ('Cannot get last ID', __FUNCTION__);
e673ee24
DO
290 return FALSE;
291 }
292 // we always have a row
293 $row = $result2->fetch (PDO::FETCH_NUM);
294 $last_insert_id = $row[0];
295 $result2->closeCursor();
296 return recordHistory ('Rack', "id = ${last_insert_id}");
297}
298
299function commitAddObject ($new_name, $new_label, $new_barcode, $new_type_id, $new_asset_no)
300{
301 global $dbxlink;
302 // Maintain UNIQUE INDEX for common names and asset tags.
303 $new_asset_no = empty ($new_asset_no) ? 'NULL' : "'${new_asset_no}'";
304 $new_barcode = empty ($new_barcode) ? 'NULL' : "'${new_barcode}'";
305 $new_name = empty ($new_name) ? 'NULL' : "'${new_name}'";
306 $query =
307 "insert into RackObject(name, label, barcode, objtype_id, asset_no) " .
308 "values(${new_name}, '${new_label}', ${new_barcode}, '${new_type_id}', ${new_asset_no})";
309 $result1 = $dbxlink->query ($query);
310 if ($result1 == NULL)
311 {
312 $errorInfo = $dbxlink->errorInfo();
f41b492f 313 showError ("SQL query '${query}' failed: ${errorInfo[2]}", __FUNCTION__);
e673ee24
DO
314 die;
315 }
316 if ($result1->rowCount() != 1)
317 {
f41b492f 318 showError ('Adding new object failed', __FUNCTION__);
e673ee24
DO
319 return FALSE;
320 }
321 $query = 'select last_insert_id()';
322 $result2 = $dbxlink->query ($query);
323 if ($result2 == NULL)
324 {
325 $errorInfo = $dbxlink->errorInfo();
f41b492f 326 showError ("SQL query '${query}' failed: ${errorInfo[2]}", __FUNCTION__);
e673ee24
DO
327 die;
328 }
329 // we always have a row
330 $row = $result2->fetch (PDO::FETCH_NUM);
331 $last_insert_id = $row[0];
332 $result2->closeCursor();
ad0e4754
DO
333 // Do AutoPorts magic
334 executeAutoPorts ($last_insert_id, $new_type_id);
e673ee24
DO
335 return recordHistory ('RackObject', "id = ${last_insert_id}");
336}
337
338function commitUpdateObject ($object_id = 0, $new_name = '', $new_label = '', $new_barcode = '', $new_type_id = 0, $new_has_problems = 'no', $new_asset_no = '', $new_comment = '')
339{
340 if ($object_id == 0 || $new_type_id == 0)
341 {
f41b492f 342 showError ('Not all required args are present.', __FUNCTION__);
e673ee24
DO
343 return FALSE;
344 }
345 global $dbxlink;
346 $new_asset_no = empty ($new_asset_no) ? 'NULL' : "'${new_asset_no}'";
347 $new_barcode = empty ($new_barcode) ? 'NULL' : "'${new_barcode}'";
348 $new_name = empty ($new_name) ? 'NULL' : "'${new_name}'";
349 $query = "update RackObject set name=${new_name}, label='${new_label}', barcode=${new_barcode}, objtype_id='${new_type_id}', " .
350 "has_problems='${new_has_problems}', asset_no=${new_asset_no}, comment='${new_comment}' " .
351 "where id='${object_id}' limit 1";
352 $result = $dbxlink->query ($query);
353 if ($result == NULL)
354 {
f41b492f 355 showError ("SQL query '${query}' failed", __FUNCTION__);
e673ee24
DO
356 return FALSE;
357 }
e673ee24
DO
358 $result->closeCursor();
359 return recordHistory ('RackObject', "id = ${object_id}");
360}
361
362function commitUpdateRack ($rack_id, $new_name, $new_height, $new_row_id, $new_comment)
363{
364 if (empty ($rack_id) || empty ($new_name) || empty ($new_height))
365 {
f41b492f 366 showError ('Not all required args are present.', __FUNCTION__);
e673ee24
DO
367 return FALSE;
368 }
369 global $dbxlink;
370 $query = "update Rack set name='${new_name}', height='${new_height}', comment='${new_comment}', row_id=${new_row_id} " .
371 "where id='${rack_id}' limit 1";
372 $result1 = $dbxlink->query ($query);
373 if ($result1->rowCount() != 1)
374 {
f41b492f 375 showError ('Error updating rack information', __FUNCTION__);
e673ee24
DO
376 return FALSE;
377 }
378 return recordHistory ('Rack', "id = ${rack_id}");
379}
380
381// This function accepts rack data returned by getRackData(), validates and applies changes
382// supplied in $_REQUEST and returns resulting array. Only those changes are examined, which
383// correspond to current rack ID.
384// 1st arg is rackdata, 2nd arg is unchecked state, 3rd arg is checked state.
385// If 4th arg is present, object_id fields will be updated accordingly to the new state.
386// The function returns the modified rack upon success.
387function processGridForm (&$rackData, $unchecked_state, $checked_state, $object_id = 0)
388{
389 global $loclist, $dbxlink;
390 $rack_id = $rackData['id'];
391 $rack_name = $rackData['name'];
392 $rackchanged = FALSE;
393 for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--)
394 {
395 for ($locidx = 0; $locidx < 3; $locidx++)
396 {
397 if ($rackData[$unit_no][$locidx]['enabled'] != TRUE)
398 continue;
399 // detect a change
400 $state = $rackData[$unit_no][$locidx]['state'];
401 if (isset ($_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"]) and $_REQUEST["atom_${rack_id}_${unit_no}_${locidx}"] == 'on')
402 $newstate = $checked_state;
403 else
404 $newstate = $unchecked_state;
405 if ($state == $newstate)
406 continue;
407 $rackchanged = TRUE;
408 // and validate
409 $atom = $loclist[$locidx];
410 // The only changes allowed are those introduced by checkbox grid.
411 if
412 (
413 !($state == $checked_state && $newstate == $unchecked_state) &&
414 !($state == $unchecked_state && $newstate == $checked_state)
415 )
416 return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, 'atom ${atom}', cannot change state from '${state}' to '${newstate}'");
417 // Here we avoid using ON DUPLICATE KEY UPDATE by first performing DELETE
418 // anyway and then looking for probable need of INSERT.
419 $query =
420 "delete from RackSpace where rack_id = ${rack_id} and " .
421 "unit_no = ${unit_no} and atom = '${atom}' limit 1";
422 $r = $dbxlink->query ($query);
423 if ($r == NULL)
4d2e93f2 424 return array ('code' => 500, 'message' => __FUNCTION__ . ": ${rack_name}: SQL DELETE query failed");
e673ee24
DO
425 if ($newstate != 'F')
426 {
427 $query =
428 "insert into RackSpace(rack_id, unit_no, atom, state) " .
429 "values(${rack_id}, ${unit_no}, '${atom}', '${newstate}') ";
430 $r = $dbxlink->query ($query);
431 if ($r == NULL)
4d2e93f2 432 return array ('code' => 500, 'message' => __FUNCTION__ . ": ${rack_name}: SQL INSERT query failed");
e673ee24
DO
433 }
434 if ($newstate == 'T' and $object_id != 0)
435 {
436 // At this point we already have a record in RackSpace.
437 $query =
438 "update RackSpace set object_id=${object_id} " .
439 "where rack_id=${rack_id} and unit_no=${unit_no} and atom='${atom}' limit 1";
440 $r = $dbxlink->query ($query);
441 if ($r->rowCount() == 1)
442 $rackData[$unit_no][$locidx]['object_id'] = $object_id;
443 else
444 return array ('code' => 500, 'message' => "${rack_name}: Rack ID ${rack_id}, unit ${unit_no}, atom '${atom}' failed to set object_id to '${object_id}'");
445 }
446 }
447 }
448 if ($rackchanged)
c7fe33be
DO
449 {
450 resetThumbCache ($rack_id);
e673ee24 451 return array ('code' => 200, 'message' => "${rack_name}: All changes were successfully saved.");
c7fe33be 452 }
e673ee24
DO
453 else
454 return array ('code' => 300, 'message' => "${rack_name}: No changes.");
455}
456
457// This function builds a list of rack-unit-atom records, which are assigned to
458// the requested object.
459function getMoleculeForObject ($object_id = 0)
460{
461 if ($object_id == 0)
462 {
f41b492f 463 showError ("object_id == 0", __FUNCTION__);
e673ee24
DO
464 return NULL;
465 }
e673ee24
DO
466 $query =
467 "select rack_id, unit_no, atom from RackSpace " .
468 "where state = 'T' and object_id = ${object_id} order by rack_id, unit_no, atom";
e425f895 469 $result = useSelectBlade ($query);
e673ee24
DO
470 $ret = $result->fetchAll (PDO::FETCH_ASSOC);
471 $result->closeCursor();
472 return $ret;
473}
474
475// This function builds a list of rack-unit-atom records for requested molecule.
476function getMolecule ($mid = 0)
477{
478 if ($mid == 0)
479 {
f41b492f 480 showError ("mid == 0", __FUNCTION__);
e673ee24
DO
481 return NULL;
482 }
e673ee24
DO
483 $query =
484 "select rack_id, unit_no, atom from Atom " .
485 "where molecule_id=${mid}";
e425f895 486 $result = useSelectBlade ($query);
e673ee24
DO
487 $ret = $result->fetchAll (PDO::FETCH_ASSOC);
488 $result->closeCursor();
489 return $ret;
490}
491
492// This function creates a new record in Molecule and number of linked
493// R-U-A records in Atom.
494function createMolecule ($molData)
495{
496 global $dbxlink;
497 $query = "insert into Molecule values()";
498 $result1 = $dbxlink->query ($query);
499 if ($result1->rowCount() != 1)
500 {
f41b492f 501 showError ('Error inserting into Molecule', __FUNCTION__);
e673ee24
DO
502 return NULL;
503 }
504 $query = 'select last_insert_id()';
505 $result2 = $dbxlink->query ($query);
506 if ($result2 == NULL)
507 {
f41b492f 508 showError ('Cannot get last ID.', __FUNCTION__);
e673ee24
DO
509 return NULL;
510 }
511 $row = $result2->fetch (PDO::FETCH_NUM);
512 $molecule_id = $row[0];
513 $result2->closeCursor();
514 foreach ($molData as $dummy => $rua)
515 {
516 $rack_id = $rua['rack_id'];
517 $unit_no = $rua['unit_no'];
518 $atom = $rua['atom'];
519 $query =
520 "insert into Atom(molecule_id, rack_id, unit_no, atom) " .
521 "values (${molecule_id}, ${rack_id}, ${unit_no}, '${atom}')";
522 $result3 = $dbxlink->query ($query);
523 if ($result3 == NULL or $result3->rowCount() != 1)
524 {
f41b492f 525 showError ('Error inserting into Atom', __FUNCTION__);
e673ee24
DO
526 return NULL;
527 }
528 }
529 return $molecule_id;
530}
531
532// History logger. This function assumes certain table naming convention and
533// column design:
534// 1. History table name equals to dictionary table name plus 'History'.
535// 2. History table must have the same row set (w/o keys) plus one row named
536// 'ctime' of type 'timestamp'.
537function recordHistory ($tableName, $whereClause)
538{
539 global $dbxlink, $remote_username;
540 $query = "insert into ${tableName}History select *, current_timestamp(), '${remote_username}' from ${tableName} where ${whereClause}";
541 $result = $dbxlink->query ($query);
542 if ($result == NULL or $result->rowCount() != 1)
543 {
863b199a 544 showError ("SQL query '${query}' failed for table ${tableName}", __FUNCTION__);
e673ee24
DO
545 return FALSE;
546 }
547 return TRUE;
548}
549
550function getRackspaceHistory ()
551{
e673ee24
DO
552 $query =
553 "select mo.id as mo_id, ro.id as ro_id, ro.name, mo.ctime, mo.comment, dict_value as objtype_name, user_name from " .
554 "MountOperation as mo inner join RackObject as ro on mo.object_id = ro.id " .
555 "inner join Dictionary on objtype_id = dict_key natural join Chapter " .
556 "where chapter_name = 'RackObjectType' order by ctime desc";
e425f895 557 $result = useSelectBlade ($query);
e673ee24
DO
558 $ret = $result->fetchAll(PDO::FETCH_ASSOC);
559 $result->closeCursor();
560 return $ret;
561}
562
563// This function is used in renderRackspaceHistory()
564function getOperationMolecules ($op_id = 0)
565{
566 if ($op_id <= 0)
567 {
f41b492f 568 showError ("Missing argument", __FUNCTION__);
e673ee24
DO
569 return;
570 }
e673ee24 571 $query = "select old_molecule_id, new_molecule_id from MountOperation where id = ${op_id}";
e425f895 572 $result = useSelectBlade ($query);
e673ee24
DO
573 // We expect one row.
574 $row = $result->fetch (PDO::FETCH_ASSOC);
575 if ($row == NULL)
576 {
f41b492f 577 showError ("SQL query succeded, but returned no results.", __FUNCTION__);
e673ee24
DO
578 return;
579 }
580 $omid = $row['old_molecule_id'];
581 $nmid = $row['new_molecule_id'];
582 $result->closeCursor();
583 return array ($omid, $nmid);
584}
585
c7fe33be 586function getResidentRacksData ($object_id = 0, $fetch_rackdata = TRUE)
e673ee24
DO
587{
588 if ($object_id <= 0)
589 {
f41b492f 590 showError ('Invalid object_id', __FUNCTION__);
e673ee24
DO
591 return;
592 }
593 $query = "select distinct rack_id from RackSpace where object_id = ${object_id} order by rack_id";
e425f895 594 $result = useSelectBlade ($query);
e673ee24
DO
595 $rows = $result->fetchAll (PDO::FETCH_NUM);
596 $result->closeCursor();
597 $ret = array();
598 foreach ($rows as $row)
599 {
b422aee6
DO
600 if (!$fetch_rackdata)
601 {
602 $ret[$row[0]] = $row[0];
603 continue;
604 }
e673ee24
DO
605 $rackData = getRackData ($row[0]);
606 if ($rackData == NULL)
607 {
f41b492f 608 showError ('getRackData() failed', __FUNCTION__);
e673ee24
DO
609 return NULL;
610 }
611 $ret[$row[0]] = $rackData;
612 }
613 $result->closeCursor();
614 return $ret;
615}
616
617function getObjectGroupInfo ($group_id = 0)
618{
619 $query =
620 'select dict_key as id, dict_value as name, count(id) as count from ' .
621 'Dictionary natural join Chapter left join RackObject on dict_key = objtype_id ' .
622 'where chapter_name = "RackObjectType" ' .
623 (($group_id > 0) ? "and dict_key = ${group_id} " : '') .
624 'group by dict_key';
e425f895 625 $result = useSelectBlade ($query);
e673ee24
DO
626 $ret = array();
627 $clist = array ('id', 'name', 'count');
628 while ($row = $result->fetch (PDO::FETCH_ASSOC))
629 foreach ($clist as $dummy => $cname)
630 $ret[$row['id']][$cname] = $row[$cname];
631 $result->closeCursor();
632 if ($group_id > 0)
633 return current ($ret);
634 else
635 return $ret;
636}
637
638// This function returns objects, which have no rackspace assigned to them.
639// Additionally it keeps rack_id parameter, so we can silently pre-select
640// the rack required.
641function getUnmountedObjects ()
642{
e673ee24
DO
643 $query =
644 'select dict_value as objtype_name, dict_key as objtype_id, name, label, barcode, id, asset_no from ' .
645 'RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter ' .
646 'left join RackSpace on id = object_id '.
068ffc0c 647 'where rack_id is null and chapter_name = "RackObjectType" order by dict_value, name, label, asset_no, barcode';
e425f895 648 $result = useSelectBlade ($query);
e673ee24
DO
649 $ret = array();
650 $clist = array ('id', 'name', 'label', 'barcode', 'objtype_name', 'objtype_id', 'asset_no');
651 while ($row = $result->fetch (PDO::FETCH_ASSOC))
652 {
653 foreach ($clist as $dummy => $cname)
654 $ret[$row['id']][$cname] = $row[$cname];
655 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
656 }
657 $result->closeCursor();
658 return $ret;
659}
660
661function getProblematicObjects ()
662{
e673ee24
DO
663 $query =
664 'select dict_value as objtype_name, dict_key as objtype_id, name, id, asset_no from ' .
665 'RackObject inner join Dictionary on objtype_id = dict_key natural join Chapter '.
666 'where has_problems = "yes" and chapter_name = "RackObjectType" order by objtype_name, name';
e425f895 667 $result = useSelectBlade ($query);
e673ee24
DO
668 $ret = array();
669 $clist = array ('id', 'name', 'objtype_name', 'objtype_id', 'asset_no');
670 while ($row = $result->fetch (PDO::FETCH_ASSOC))
671 {
672 foreach ($clist as $dummy => $cname)
673 $ret[$row['id']][$cname] = $row[$cname];
674 $ret[$row['id']]['dname'] = displayedName ($ret[$row['id']]);
675 }
676 $result->closeCursor();
677 return $ret;
678}
679
680function commitAddPort ($object_id = 0, $port_name, $port_type_id, $port_label, $port_l2address)
681{
682 if ($object_id <= 0)
683 {
f41b492f 684 showError ('Invalid object_id', __FUNCTION__);
e673ee24
DO
685 return;
686 }
687 $port_l2address = l2addressForDatabase ($port_l2address);
688 $result = useInsertBlade
689 (
690 'Port',
691 array
692 (
693 'name' => "'${port_name}'",
694 'object_id' => "'${object_id}'",
695 'label' => "'${port_label}'",
696 'type' => "'${port_type_id}'",
697 'l2address' => "${port_l2address}"
698 )
699 );
700 if ($result)
701 return '';
702 else
703 return 'SQL query failed';
704}
705
706function commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_reservation_comment)
707{
708 global $dbxlink;
709 $port_l2address = l2addressForDatabase ($port_l2address);
710 $query =
711 "update Port set name='$port_name', label='$port_label', " .
712 "reservation_comment = ${port_reservation_comment}, l2address=${port_l2address} " .
713 "where id='$port_id'";
714 $result = $dbxlink->exec ($query);
715 if ($result == 1)
716 return '';
717 $errorInfo = $dbxlink->errorInfo();
718 // We could update nothing.
719 if ($errorInfo[0] == '00000')
720 return '';
721 return $errorInfo[2];
722}
723
724function delObjectPort ($port_id)
725{
726 if (unlinkPort ($port_id) != '')
4d2e93f2 727 return __FUNCTION__ . ': unlinkPort() failed';
e673ee24 728 if (useDeleteBlade ('Port', 'id', $port_id) != TRUE)
4d2e93f2 729 return __FUNCTION__ . ': useDeleteBlade() failed';
e673ee24
DO
730 return '';
731}
732
733function getObjectAddressesAndNames ()
734{
e673ee24
DO
735 $query =
736 "select object_id as object_id, ".
737 "RackObject.name as object_name, ".
738 "IPBonds.name as name, ".
739 "INET_NTOA(ip) as ip ".
740 "from IPBonds join RackObject on id=object_id ";
e425f895 741 $result = useSelectBlade ($query);
e673ee24
DO
742 $ret = array();
743 $count=0;
744 while ($row = $result->fetch (PDO::FETCH_ASSOC))
745 {
746 $ret[$count]['object_id']=$row['object_id'];
747 $ret[$count]['object_name']=$row['object_name'];
748 $ret[$count]['name']=$row['name'];
749 $ret[$count]['ip']=$row['ip'];
750 $count++;
751 }
752 $result->closeCursor();
753 return $ret;
754}
755
756
757function getEmptyPortsOfType ($type_id)
758{
e673ee24
DO
759 $query =
760 "select distinct Port.id as Port_id, ".
761 "Port.object_id as Port_object_id, ".
762 "RackObject.name as Object_name, ".
763 "Port.name as Port_name, ".
764 "Port.type as Port_type_id, ".
765 "dict_value as Port_type_name ".
766 "from ( ".
767 " ( ".
768 " Port inner join Dictionary on Port.type = dict_key natural join Chapter ".
769 " ) ".
770 " join RackObject on Port.object_id = RackObject.id ".
771 ") ".
772 "left join Link on Port.id=Link.porta or Port.id=Link.portb ".
773 "inner join PortCompat on Port.type = PortCompat.type2 ".
774 "where chapter_name = 'PortType' and PortCompat.type1 = '$type_id' and Link.porta is NULL ".
775 "and Port.reservation_comment is null order by Object_name, Port_name";
e425f895 776 $result = useSelectBlade ($query);
e673ee24
DO
777 $ret = array();
778 $count=0;
779 while ($row = $result->fetch (PDO::FETCH_ASSOC))
780 {
781 $ret[$count]['Port_id']=$row['Port_id'];
782 $ret[$count]['Port_object_id']=$row['Port_object_id'];
783 $ret[$count]['Object_name']=$row['Object_name'];
784 $ret[$count]['Port_name']=$row['Port_name'];
785 $ret[$count]['Port_type_id']=$row['Port_type_id'];
786 $ret[$count]['Port_type_name']=$row['Port_type_name'];
787 $count++;
788 }
789 $result->closeCursor();
790 return $ret;
791}
792
793function linkPorts ($porta, $portb)
794{
795 if ($porta == $portb)
796 return "Ports can't be the same";
797 if ($porta > $portb)
798 {
799 $tmp = $porta;
800 $porta = $portb;
801 $portb = $tmp;
802 }
803 global $dbxlink;
804 $query1 = "insert into Link set porta='${porta}', portb='{$portb}'";
805 $query2 = "update Port set reservation_comment = NULL where id = ${porta} or id = ${portb} limit 2";
806 // FIXME: who cares about the return value?
807 $result = $dbxlink->exec ($query1);
808 $result = $dbxlink->exec ($query2);
809 return '';
810
811}
812
813function unlinkPort ($port)
814{
815 global $dbxlink;
816 $query =
817 "delete from Link where porta='$port' or portb='$port'";
818 $result = $dbxlink->exec ($query);
819 return '';
820
821}
822
4b2bf583
DO
823// FIXME: after falling back to using existing getObjectInfo we don't
824// need that large query. Shrink it some later.
e673ee24
DO
825function getObjectAddresses ($object_id = 0)
826{
827 if ($object_id == 0)
828 {
f41b492f 829 showError ('Invalid object_id', __FUNCTION__);
e673ee24
DO
830 return;
831 }
e673ee24
DO
832 $query =
833 "select ".
834 "IPAddress.name as IPAddress_name, ".
835 "IPAddress.reserved as IPAddress_reserved, ".
836 "IPBonds.name as IPBonds_name, ".
837 "INET_NTOA(IPBonds.ip) as IPBonds_ip, ".
838 "IPBonds.type as IPBonds_type, ".
839 "RemoteBonds.name as RemoteBonds_name, ".
840 "RemoteBonds.type as RemoteBonds_type, ".
841 "RemoteBonds.object_id as RemoteBonds_object_id, ".
842 "RemoteObject.name as RemoteObject_name from IPBonds " .
843 "left join IPBonds as RemoteBonds on IPBonds.ip=RemoteBonds.ip " .
844 "and IPBonds.object_id!=RemoteBonds.object_id " .
845 "left join IPAddress on IPBonds.ip=IPAddress.ip " .
846 "left join RackObject as RemoteObject on RemoteBonds.object_id=RemoteObject.id ".
847 "where ".
848 "IPBonds.object_id = ${object_id} ".
849 "order by IPBonds.ip, RemoteObject.name";
e425f895
DO
850 $result = useSelectBlade ($query);
851 $ret=array();
852 $count=0;
853 $refcount=0;
854 $prev_ip = 0;
855 // We are going to call getObjectInfo() for some rows,
856 // hence the connector must be unloaded from the
857 // current data.
858 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
859 $result->closeCursor();
860 foreach ($rows as $row)
e673ee24 861 {
e425f895 862 if ($prev_ip != $row['IPBonds_ip'])
e673ee24 863 {
e425f895
DO
864 $count++;
865 $refcount=0;
866 $prev_ip = $row['IPBonds_ip'];
867 $ret[$count]['address_name'] = $row['IPAddress_name'];
868 $ret[$count]['address_reserved'] = $row['IPAddress_reserved'];
869 $ret[$count]['ip'] = $row['IPBonds_ip'];
870 $ret[$count]['name'] = $row['IPBonds_name'];
871 $ret[$count]['type'] = $row['IPBonds_type'];
872 $ret[$count]['references'] = array();
873 }
e673ee24 874
e425f895
DO
875 if ($row['RemoteBonds_type'])
876 {
877 $ret[$count]['references'][$refcount]['type'] = $row['RemoteBonds_type'];
878 $ret[$count]['references'][$refcount]['name'] = $row['RemoteBonds_name'];
879 $ret[$count]['references'][$refcount]['object_id'] = $row['RemoteBonds_object_id'];
880 if (empty ($row['RemoteBonds_object_id']))
881 $ret[$count]['references'][$refcount]['object_name'] = $row['RemoteObject_name'];
882 else
e673ee24 883 {
e425f895
DO
884 $oi = getObjectInfo ($row['RemoteBonds_object_id']);
885 $ret[$count]['references'][$refcount]['object_name'] = displayedName ($oi);
e673ee24 886 }
e425f895 887 $refcount++;
e673ee24
DO
888 }
889 }
e673ee24
DO
890 return $ret;
891}
892
893function getAddressspaceList ()
894{
e673ee24
DO
895 $query =
896 "select ".
897 "id as IPRanges_id, ".
898 "INET_NTOA(ip) as IPRanges_ip, ".
899 "mask as IPRanges_mask, ".
900 "name as IPRanges_name ".
901 "from IPRanges ".
902 "order by ip";
e425f895
DO
903 $result = useSelectBlade ($query);
904 $ret=array();
905 $count=0;
906 while ($row = $result->fetch (PDO::FETCH_ASSOC))
e673ee24 907 {
e425f895
DO
908 $ret[$count]['id'] = $row['IPRanges_id'];
909 $ret[$count]['ip'] = $row['IPRanges_ip'];
910 $ret[$count]['ip_bin'] = ip2long($row['IPRanges_ip']);
911 $ret[$count]['name'] = $row['IPRanges_name'];
912 $ret[$count]['mask'] = $row['IPRanges_mask'];
913 $ret[$count]['mask_bin'] = binMaskFromDec($row['IPRanges_mask']);
914 $ret[$count]['mask_bin_inv'] = binInvMaskFromDec($row['IPRanges_mask']);
915 $count++;
e673ee24
DO
916 }
917 $result->closeCursor();
918 return $ret;
919
920}
921
922function getRangeByIp ($ip = '', $id = 0)
923{
e673ee24
DO
924 if ($id == 0)
925 $query =
926 "select ".
927 "id, INET_NTOA(ip) as ip, mask, name ".
928 "from IPRanges ";
929 else
930 $query =
931 "select ".
932 "id, INET_NTOA(ip) as ip, mask, name ".
933 "from IPRanges where id='$id'";
934
e425f895
DO
935 $result = useSelectBlade ($query);
936 $ret=array();
937 while ($row = $result->fetch (PDO::FETCH_ASSOC))
e673ee24 938 {
e425f895
DO
939 $binmask=binMaskFromDec($row['mask']);
940 if ((ip2long($ip) & $binmask) == ip2long($row['ip']))
e673ee24 941 {
e425f895
DO
942 $ret['id'] = $row['id'];
943 $ret['ip'] = $row['ip'];
944 $ret['ip_bin'] = ip2long($row['ip']);
945 $ret['name'] = $row['name'];
946 $ret['mask'] = $row['mask'];
947 $result->closeCursor();
948 return $ret;
e673ee24
DO
949 }
950 }
951 $result->closeCursor();
952 return NULL;
953}
954
955function updateRange ($id=0, $name='')
956{
957 global $dbxlink;
958 $query =
959 "update IPRanges set name='$name' where id='$id'";
960 $result = $dbxlink->exec ($query);
961 return '';
962
963}
964
965// This function is actually used not only to update, but also to create records,
966// that's why ON DUPLICATE KEY UPDATE was replaced by DELETE-INSERT pair
967// (MySQL 4.0 workaround).
968function updateAddress ($ip=0, $name='', $reserved='no')
969{
970 // DELETE may safely fail.
971 $r = useDeleteBlade ('IPAddress', 'ip', "INET_ATON('${ip}')", FALSE);
972 // INSERT may appear not necessary.
973 if ($name == '' and $reserved == 'no')
974 return '';
975 if (useInsertBlade ('IPAddress', array ('name' => "'${name}'", 'reserved' => "'${reserved}'", 'ip' => "INET_ATON('${ip}')")))
976 return '';
977 else
978 return 'useInsertBlade() failed in updateAddress()';
979}
980
399214af 981// FIXME: This function doesn't wipe relevant records from IPAddress table.
e673ee24
DO
982function commitDeleteRange ($id = 0)
983{
984 if ($id <= 0)
4d2e93f2 985 return __FUNCTION__ . ': Invalid range ID';
e673ee24
DO
986 if (useDeleteBlade ('IPRanges', 'id', $id))
987 return '';
988 else
4d2e93f2 989 return __FUNCTION__ . ': SQL query failed';
e673ee24
DO
990}
991
992function updateBond ($ip='', $object_id=0, $name='', $type='')
993{
994 global $dbxlink;
995
996 $query =
997 "update IPBonds set name='$name', type='$type' where ip=INET_ATON('$ip') and object_id='$object_id'";
998 $result = $dbxlink->exec ($query);
999 return '';
1000}
1001
1002function unbindIpFromObject ($ip='', $object_id=0)
1003{
1004 global $dbxlink;
1005
1006 $query =
1007 "delete from IPBonds where ip=INET_ATON('$ip') and object_id='$object_id'";
1008 $result = $dbxlink->exec ($query);
1009 return '';
1010}
1011
1012// This function returns either all or one user account. Array key is user name.
1013function getUserAccounts ()
1014{
e673ee24
DO
1015 $query =
1016 'select user_id, user_name, user_password_hash, user_realname, user_enabled ' .
4e9ea83c 1017 'from UserAccount order by user_name';
e425f895 1018 $result = useSelectBlade ($query);
e673ee24
DO
1019 $ret = array();
1020 $clist = array ('user_id', 'user_name', 'user_realname', 'user_password_hash', 'user_enabled');
1021 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1022 foreach ($clist as $dummy => $cname)
1023 $ret[$row['user_name']][$cname] = $row[$cname];
1024 $result->closeCursor();
1025 return $ret;
1026}
1027
1028// This function returns permission array for all user accounts. Array key is user name.
1029function getUserPermissions ()
1030{
e673ee24
DO
1031 $query =
1032 "select UserPermission.user_id, user_name, page, tab, access from " .
1033 "UserPermission natural left join UserAccount where (user_name is not null) or " .
7bf342fd 1034 "(user_name is null and UserPermission.user_id = 0) order by user_name, page, tab";
e425f895 1035 $result = useSelectBlade ($query);
e673ee24
DO
1036 $ret = array();
1037 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1038 {
1039 if ($row['user_id'] == 0)
1040 $row['user_name'] = '%';
1041 $ret[$row['user_name']][$row['page']][$row['tab']] = $row['access'];
1042 }
1043 $result->closeCursor();
1044 return $ret;
1045}
1046
1047function searchByl2address ($l2addr)
1048{
e673ee24
DO
1049 $l2addr = l2addressForDatabase ($l2addr);
1050 $query = "select object_id, Port.id as port_id from RackObject as ro inner join Port on ro.id = Port.object_id " .
1051 "where l2address = ${l2addr}";
e425f895 1052 $result = useSelectBlade ($query);
e673ee24
DO
1053 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
1054 $result->closeCursor();
1055 if (count ($rows) == 0) // No results.
1056 return NULL;
1057 if (count ($rows) == 1) // Target found.
1058 return $rows[0];
f41b492f 1059 showError ('More than one results was found. This is probably a broken unique key.', __FUNCTION__);
e673ee24
DO
1060 return NULL;
1061}
1062
1063// This function returns either port ID or NULL for specified arguments.
1064function getPortID ($object_id, $port_name)
1065{
e673ee24 1066 $query = "select id from Port where object_id=${object_id} and name='${port_name}' limit 2";
e425f895 1067 $result = useSelectBlade ($query);
e673ee24
DO
1068 $rows = $result->fetchAll (PDO::FETCH_NUM);
1069 if (count ($rows) != 1)
1070 return NULL;
1071 $ret = $rows[0][0];
1072 $result->closeCursor();
1073 return $ret;
1074}
1075
1076function commitCreateUserAccount ($username, $realname, $password)
1077{
1078 return useInsertBlade
1079 (
1080 'UserAccount',
1081 array
1082 (
1083 'user_name' => "'${username}'",
1084 'user_realname' => "'${realname}'",
1085 'user_password_hash' => "'${password}'"
1086 )
1087 );
1088}
1089
1090function commitUpdateUserAccount ($id, $new_username, $new_realname, $new_password)
1091{
1092 global $dbxlink;
1093 $query =
1094 "update UserAccount set user_name = '${new_username}', user_realname = '${new_realname}', " .
1095 "user_password_hash = '${new_password}' where user_id = ${id} limit 1";
1096 $result = $dbxlink->query ($query);
1097 if ($result == NULL)
1098 {
f41b492f 1099 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1100 die;
1101 }
1102 return TRUE;
1103}
1104
1105function commitEnableUserAccount ($id, $new_enabled_value)
1106{
1107 global $dbxlink;
1108 $query =
1109 "update UserAccount set user_enabled = '${new_enabled_value}' " .
1110 "where user_id = ${id} limit 1";
1111 $result = $dbxlink->query ($query);
1112 if ($result == NULL)
1113 {
f41b492f 1114 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1115 die;
1116 }
1117 return TRUE;
1118}
1119
1120function commitGrantPermission ($userid, $page, $tab, $value)
1121{
1122 return useInsertBlade
1123 (
1124 'UserPermission',
1125 array
1126 (
1127 'user_id' => $userid,
1128 'page' => "'${page}'",
1129 'tab' => "'${tab}'",
1130 'access' => "'${value}'"
1131 )
1132 );
1133}
1134
1135function commitRevokePermission ($userid, $page, $tab)
1136{
1137 global $dbxlink;
1138 $query =
1139 "delete from UserPermission where user_id = '${userid}' and page = '${page}' " .
1140 "and tab = '$tab' limit 1";
1141 $result = $dbxlink->query ($query);
1142 if ($result == NULL)
1143 {
f41b492f 1144 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1145 die;
1146 }
1147 return TRUE;
1148}
1149
1150// This function returns an array of all port type pairs from PortCompat table.
1151function getPortCompat ()
1152{
e673ee24
DO
1153 $query =
1154 "select type1, type2, d1.dict_value as type1name, d2.dict_value as type2name from " .
1155 "PortCompat as pc inner join Dictionary as d1 on pc.type1 = d1.dict_key " .
1156 "inner join Dictionary as d2 on pc.type2 = d2.dict_key " .
1157 "inner join Chapter as c1 on d1.chapter_no = c1.chapter_no " .
1158 "inner join Chapter as c2 on d2.chapter_no = c2.chapter_no " .
1159 "where c1.chapter_name = 'PortType' and c2.chapter_name = 'PortType'";
e425f895 1160 $result = useSelectBlade ($query);
e673ee24
DO
1161 $ret = $result->fetchAll (PDO::FETCH_ASSOC);
1162 $result->closeCursor();
1163 return $ret;
1164}
1165
1166function removePortCompat ($type1 = 0, $type2 = 0)
1167{
1168 global $dbxlink;
1169 if ($type1 == 0 or $type2 == 0)
1170 {
f41b492f 1171 showError ('Invalid arguments', __FUNCTION__);
e673ee24
DO
1172 die;
1173 }
1174 $query = "delete from PortCompat where type1 = ${type1} and type2 = ${type2} limit 1";
1175 $result = $dbxlink->query ($query);
1176 if ($result == NULL)
1177 {
f41b492f 1178 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1179 die;
1180 }
1181 return TRUE;
1182}
1183
1184function addPortCompat ($type1 = 0, $type2 = 0)
1185{
1186 if ($type1 <= 0 or $type2 <= 0)
1187 {
f41b492f 1188 showError ('Invalid arguments', __FUNCTION__);
e673ee24
DO
1189 die;
1190 }
1191 return useInsertBlade
1192 (
1193 'PortCompat',
1194 array ('type1' => $type1, 'type2' => $type2)
1195 );
1196}
1197
1198// This function returns the dictionary as an array of trees, each tree
1199// representing a single chapter. Each element has 'id', 'name', 'sticky'
1200// and 'word' keys with the latter holding all the words within the chapter.
1201function getDict ()
1202{
6bb8e41d 1203 $query1 =
e673ee24
DO
1204 "select chapter_name, Chapter.chapter_no, dict_key, dict_value, sticky from " .
1205 "Chapter natural left join Dictionary order by chapter_name, dict_value";
e425f895 1206 $result1 = useSelectBlade ($query1);
e673ee24 1207 $dict = array();
6bb8e41d 1208 while ($row = $result1->fetch (PDO::FETCH_ASSOC))
e673ee24
DO
1209 {
1210 $chapter_no = $row['chapter_no'];
1211 if (!isset ($dict[$chapter_no]))
1212 {
1213 $dict[$chapter_no]['no'] = $chapter_no;
1214 $dict[$chapter_no]['name'] = $row['chapter_name'];
1215 $dict[$chapter_no]['sticky'] = $row['sticky'] == 'yes' ? TRUE : FALSE;
1216 $dict[$chapter_no]['word'] = array();
1217 }
1218 if ($row['dict_key'] != NULL)
6bb8e41d 1219 {
e673ee24 1220 $dict[$chapter_no]['word'][$row['dict_key']] = $row['dict_value'];
6bb8e41d
DO
1221 $dict[$chapter_no]['refcnt'][$row['dict_key']] = 0;
1222 }
e673ee24 1223 }
6bb8e41d
DO
1224 $result1->closeCursor();
1225// Find the list of all assigned values of dictionary-addressed attributes, each with
1226// chapter/word keyed reference counters. Use the structure to adjust reference counters
1227// of the returned disctionary words.
1228 $query2 = "select a.attr_id, am.chapter_no, uint_value, count(object_id) as refcnt " .
1229 "from Attribute as a inner join AttributeMap as am on a.attr_id = am.attr_id " .
1230 "inner join AttributeValue as av on a.attr_id = av.attr_id " .
1231 "inner join Dictionary as d on am.chapter_no = d.chapter_no and av.uint_value = d.dict_key " .
1232 "where attr_type = 'dict' group by a.attr_id, am.chapter_no, uint_value " .
1233 "order by a.attr_id, am.chapter_no, uint_value";
e425f895 1234 $result2 = useSelectBlade ($query2);
6bb8e41d
DO
1235 $refcnt = array();
1236 while ($row = $result2->fetch (PDO::FETCH_ASSOC))
1237 $dict[$row['chapter_no']]['refcnt'][$row['uint_value']] = $row['refcnt'];
1238 $result2->closeCursor();
e673ee24
DO
1239 return $dict;
1240}
1241
da95280e
DO
1242function getDictStats ()
1243{
82f7e2e8 1244 $stock_chapters = array (1, 2, 3, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23);
da95280e 1245 $query =
5aa28abb
DO
1246 "select Chapter.chapter_no, chapter_name, count(dict_key) as wc from " .
1247 "Chapter natural left join Dictionary group by Chapter.chapter_no";
e425f895 1248 $result1 = useSelectBlade ($query);
da95280e 1249 $tc = $tw = $uc = $uw = 0;
08b4cb24 1250 while ($row = $result1->fetch (PDO::FETCH_ASSOC))
da95280e
DO
1251 {
1252 $tc++;
1253 $tw += $row['wc'];;
1254 if (in_array ($row['chapter_no'], $stock_chapters))
1255 continue;
1256 $uc++;
1257 $uw += $row['wc'];;
1258 }
08b4cb24
DO
1259 $result1->closeCursor();
1260 $query = "select count(attr_id) as attrc from RackObject as ro left join " .
1261 "AttributeValue as av on ro.id = av.object_id group by ro.id";
e425f895 1262 $result2 = useSelectBlade ($query);
08b4cb24
DO
1263 $to = $ta = $so = 0;
1264 while ($row = $result2->fetch (PDO::FETCH_ASSOC))
1265 {
1266 $to++;
1267 if ($row['attrc'] != 0)
1268 {
1269 $so++;
1270 $ta += $row['attrc'];
1271 }
1272 }
1273 $result2->closeCursor();
da95280e
DO
1274 $ret = array();
1275 $ret['Total chapters in dictionary'] = $tc;
1276 $ret['Total words in dictionary'] = $tw;
1277 $ret['User chapters'] = $uc;
1278 $ret['Words in user chapters'] = $uw;
08b4cb24
DO
1279 $ret['Total objects'] = $to;
1280 $ret['Objects with stickers'] = $so;
1281 $ret['Total stickers attached'] = $ta;
da95280e
DO
1282 return $ret;
1283}
1284
9ec5fdf1
DO
1285function getIPv4Stats()
1286{
9ec5fdf1
DO
1287 $ret = array();
1288 $subject = array();
1289 $subject[] = array ('q' => 'select count(id) from IPRanges', 'txt' => 'Networks');
1290 $subject[] = array ('q' => 'select count(ip) from IPAddress', 'txt' => 'Addresses commented/reserved');
1291 $subject[] = array ('q' => 'select count(ip) from IPBonds', 'txt' => 'Addresses allocated');
1292 $subject[] = array ('q' => 'select count(*) from PortForwarding', 'txt' => 'NAT rules');
1293 $subject[] = array ('q' => 'select count(id) from IPVirtualService', 'txt' => 'Virtual services');
1294 $subject[] = array ('q' => 'select count(id) from IPRSPool', 'txt' => 'Real server pools');
1295 $subject[] = array ('q' => 'select count(id) from IPRealServer', 'txt' => 'Real servers');
1296 $subject[] = array ('q' => 'select count(distinct object_id) from IPLoadBalancer', 'txt' => 'Load balancers');
1297
1298 foreach ($subject as $item)
1299 {
e425f895 1300 $result = useSelectBlade ($item['q']);
9ec5fdf1
DO
1301 $row = $result->fetch (PDO::FETCH_NUM);
1302 $ret[$item['txt']] = $row[0];
1303 $result->closeCursor();
1304 unset ($result);
1305 }
1306 return $ret;
1307}
1308
1309function getRackspaceStats()
1310{
9ec5fdf1
DO
1311 $ret = array();
1312 $subject = array();
1313 $subject[] = array ('q' => 'select count(*) from Dictionary where chapter_no = 3', 'txt' => 'Rack rows');
1314 $subject[] = array ('q' => 'select count(*) from Rack', 'txt' => 'Racks');
1315 $subject[] = array ('q' => 'select avg(height) from Rack', 'txt' => 'Average rack height');
1316 $subject[] = array ('q' => 'select sum(height) from Rack', 'txt' => 'Total rack units in field');
1317
1318 foreach ($subject as $item)
1319 {
e425f895 1320 $result = useSelectBlade ($item['q']);
9ec5fdf1 1321 $row = $result->fetch (PDO::FETCH_NUM);
82f7e2e8 1322 $ret[$item['txt']] = empty ($row[0]) ? 0 : $row[0];
9ec5fdf1
DO
1323 $result->closeCursor();
1324 unset ($result);
1325 }
1326 return $ret;
1327}
1328
e673ee24
DO
1329function commitUpdateDictionary ($chapter_no = 0, $dict_key = 0, $dict_value = '')
1330{
1331 if ($chapter_no <= 0 or $dict_key <= 0 or empty ($dict_value))
1332 {
f41b492f 1333 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1334 die;
1335 }
1336 global $dbxlink;
1337 $query =
1338 "update Dictionary set dict_value = '${dict_value}' where chapter_no=${chapter_no} " .
1339 "and dict_key=${dict_key} limit 1";
1340 $result = $dbxlink->query ($query);
1341 if ($result == NULL)
1342 {
f41b492f 1343 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1344 die;
1345 }
1346 return TRUE;
1347}
1348
1349function commitSupplementDictionary ($chapter_no = 0, $dict_value = '')
1350{
1351 if ($chapter_no <= 0 or empty ($dict_value))
1352 {
f41b492f 1353 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1354 die;
1355 }
1356 return useInsertBlade
1357 (
1358 'Dictionary',
1359 array ('chapter_no' => $chapter_no, 'dict_value' => "'${dict_value}'")
1360 );
1361}
1362
1363function commitReduceDictionary ($chapter_no = 0, $dict_key = 0)
1364{
1365 if ($chapter_no <= 0 or $dict_key <= 0)
1366 {
f41b492f 1367 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1368 die;
1369 }
1370 global $dbxlink;
1371 $query =
1372 "delete from Dictionary where chapter_no=${chapter_no} " .
1373 "and dict_key=${dict_key} limit 1";
1374 $result = $dbxlink->query ($query);
1375 if ($result == NULL)
1376 {
f41b492f 1377 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1378 die;
1379 }
1380 return TRUE;
1381}
1382
1383function commitAddChapter ($chapter_name = '')
1384{
1385 if (empty ($chapter_name))
1386 {
f41b492f 1387 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1388 die;
1389 }
1390 return useInsertBlade
1391 (
1392 'Chapter',
1393 array ('chapter_name' => "'${chapter_name}'")
1394 );
1395}
1396
1397function commitUpdateChapter ($chapter_no = 0, $chapter_name = '')
1398{
1399 if ($chapter_no <= 0 or empty ($chapter_name))
1400 {
f41b492f 1401 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1402 die;
1403 }
1404 global $dbxlink;
1405 $query =
1406 "update Chapter set chapter_name = '${chapter_name}' where chapter_no = ${chapter_no} " .
1407 "and sticky = 'no' limit 1";
1408 $result = $dbxlink->query ($query);
1409 if ($result == NULL)
1410 {
f41b492f 1411 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1412 die;
1413 }
1414 return TRUE;
1415}
1416
1417function commitDeleteChapter ($chapter_no = 0)
1418{
1419 if ($chapter_no <= 0)
1420 {
f41b492f 1421 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1422 die;
1423 }
1424 global $dbxlink;
1425 $query =
1426 "delete from Chapter where chapter_no = ${chapter_no} and sticky = 'no' limit 1";
1427 $result = $dbxlink->query ($query);
1428 if ($result == NULL)
1429 {
f41b492f 1430 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1431 die;
1432 }
1433 return TRUE;
1434}
1435
4c330a14
DO
1436// This is a dictionary accessor. We perform link rendering, so the user sees
1437// nice <select> drop-downs.
e673ee24
DO
1438function readChapter ($chapter_name = '')
1439{
1440 if (empty ($chapter_name))
1441 {
f41b492f 1442 showError ('invalid argument', __FUNCTION__);
e673ee24
DO
1443 return NULL;
1444 }
e673ee24
DO
1445 $query =
1446 "select dict_key, dict_value from Dictionary natural join Chapter " .
4aa8609b 1447 "where chapter_name = '${chapter_name}'";
e425f895 1448 $result = useSelectBlade ($query);
e673ee24
DO
1449 $chapter = array();
1450 while ($row = $result->fetch (PDO::FETCH_ASSOC))
4c330a14 1451 $chapter[$row['dict_key']] = parseWikiLink ($row['dict_value'], 'o');
e673ee24 1452 $result->closeCursor();
4aa8609b
DO
1453 // SQL ORDER BY had no sense, because we need to sort after link rendering, not before.
1454 asort ($chapter);
e673ee24
DO
1455 return $chapter;
1456}
1457
1458function getAttrMap ()
1459{
e673ee24
DO
1460 $query =
1461 "select a.attr_id, a.attr_type, a.attr_name, am.objtype_id, " .
1462 "d.dict_value as objtype_name, am.chapter_no, c2.chapter_name from " .
1463 "Attribute as a natural left join AttributeMap as am " .
1464 "left join Dictionary as d on am.objtype_id = d.dict_key " .
1465 "left join Chapter as c1 on d.chapter_no = c1.chapter_no " .
1466 "left join Chapter as c2 on am.chapter_no = c2.chapter_no " .
1467 "where c1.chapter_name = 'RackObjectType' or c1.chapter_name is null " .
1468 "order by attr_name";
e425f895 1469 $result = useSelectBlade ($query);
e673ee24
DO
1470 $ret = array();
1471 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1472 {
1473 $attr_id = $row['attr_id'];
1474 if (!isset ($ret[$attr_id]))
1475 {
1476 $ret[$attr_id]['id'] = $attr_id;
1477 $ret[$attr_id]['type'] = $row['attr_type'];
1478 $ret[$attr_id]['name'] = $row['attr_name'];
1479 $ret[$attr_id]['application'] = array();
1480 }
1481 if ($row['objtype_id'] == '')
1482 continue;
1483 $application['objtype_id'] = $row['objtype_id'];
1484 $application['objtype_name'] = $row['objtype_name'];
1485 if ($row['attr_type'] == 'dict')
1486 {
1487 $application['chapter_no'] = $row['chapter_no'];
1488 $application['chapter_name'] = $row['chapter_name'];
1489 }
1490 $ret[$attr_id]['application'][] = $application;
1491 }
1492 $result->closeCursor();
1493 return $ret;
1494}
1495
1496function commitUpdateAttribute ($attr_id = 0, $attr_name = '')
1497{
1498 if ($attr_id <= 0 or empty ($attr_name))
1499 {
f41b492f 1500 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1501 die;
1502 }
1503 global $dbxlink;
1504 $query =
1505 "update Attribute set attr_name = '${attr_name}' " .
1506 "where attr_id = ${attr_id} limit 1";
1507 $result = $dbxlink->query ($query);
1508 if ($result == NULL)
1509 {
f41b492f 1510 showError ("SQL query '${query}' failed", __FUNCTION__);
e673ee24
DO
1511 die;
1512 }
1513 return TRUE;
1514}
1515
1516function commitAddAttribute ($attr_name = '', $attr_type = '')
1517{
1518 if (empty ($attr_name))
1519 {
f41b492f 1520 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1521 die;
1522 }
1523 switch ($attr_type)
1524 {
1525 case 'uint':
1526 case 'float':
1527 case 'string':
1528 case 'dict':
1529 break;
1530 default:
f41b492f 1531 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1532 die;
1533 }
1534 return useInsertBlade
1535 (
1536 'Attribute',
1537 array ('attr_name' => "'${attr_name}'", 'attr_type' => "'${attr_type}'")
1538 );
1539}
1540
1541function commitDeleteAttribute ($attr_id = 0)
1542{
1543 if ($attr_id <= 0)
1544 {
f41b492f 1545 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1546 die;
1547 }
1548 return useDeleteBlade ('Attribute', 'attr_id', $attr_id);
1549}
1550
1551// FIXME: don't store garbage in chapter_no for non-dictionary types.
1552function commitSupplementAttrMap ($attr_id = 0, $objtype_id = 0, $chapter_no = 0)
1553{
1554 if ($attr_id <= 0 or $objtype_id <= 0 or $chapter_no <= 0)
1555 {
f41b492f 1556 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1557 die;
1558 }
1559 return useInsertBlade
1560 (
1561 'AttributeMap',
1562 array
1563 (
1564 'attr_id' => $attr_id,
1565 'objtype_id' => $objtype_id,
1566 'chapter_no' => $chapter_no
1567 )
1568 );
1569}
1570
1571function commitReduceAttrMap ($attr_id = 0, $objtype_id)
1572{
1573 if ($attr_id <= 0 or $objtype_id <= 0)
1574 {
f41b492f 1575 showError ('Invalid args', __FUNCTION__);
e673ee24
DO
1576 die;
1577 }
1578 global $dbxlink;
1579 $query =
1580 "delete from AttributeMap where attr_id=${attr_id} " .
1581 "and objtype_id=${objtype_id} limit 1";
1582 $result = $dbxlink->query ($query);
1583 if ($result == NULL)
1584 {
f41b492f 1585 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1586 die;
1587 }
1588 return TRUE;
1589}
1590
1591// This function returns all optional attributes for requested object
1592// as an array of records. NULL is returned on error and empty array
1593// is returned, if there are no attributes found.
1594function getAttrValues ($object_id)
1595{
1596 if ($object_id <= 0)
1597 {
f41b492f 1598 showError ('Invalid argument', __FUNCTION__);
e673ee24
DO
1599 return NULL;
1600 }
e673ee24
DO
1601 $ret = array();
1602 $query =
1603 "select A.attr_id, A.attr_name, A.attr_type, C.chapter_name, " .
1604 "AV.uint_value, AV.float_value, AV.string_value, D.dict_value from " .
1605 "RackObject as RO inner join AttributeMap as AM on RO.objtype_id = AM.objtype_id " .
1606 "inner join Attribute as A using (attr_id) " .
1607 "left join AttributeValue as AV on AV.attr_id = AM.attr_id and AV.object_id = RO.id " .
1608 "left join Dictionary as D on D.dict_key = AV.uint_value and AM.chapter_no = D.chapter_no " .
1609 "left join Chapter as C on AM.chapter_no = C.chapter_no " .
1610 "where RO.id = ${object_id} order by A.attr_type";
e425f895 1611 $result = useSelectBlade ($query);
e673ee24
DO
1612 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1613 {
1614 $record = array();
1615 $record['id'] = $row['attr_id'];
1616 $record['name'] = $row['attr_name'];
1617 $record['type'] = $row['attr_type'];
1618 switch ($row['attr_type'])
1619 {
1620 case 'uint':
1621 case 'float':
1622 case 'string':
1623 case 'dict':
50a954f6
DO
1624 $record['value'] = parseWikiLink ($row[$row['attr_type'] . '_value'], 'o');
1625 $record['a_value'] = parseWikiLink ($row[$row['attr_type'] . '_value'], 'a');
e673ee24
DO
1626 $record['chapter_name'] = $row['chapter_name'];
1627 $record['key'] = $row['uint_value'];
1628 break;
1629 default:
1630 $record['value'] = NULL;
1631 break;
1632 }
1633 $ret[$row['attr_id']] = $record;
1634 }
1635 $result->closeCursor();
1636 return $ret;
1637}
1638
1639function commitResetAttrValue ($object_id = 0, $attr_id = 0)
1640{
1641 if ($object_id <= 0 or $attr_id <= 0)
1642 {
f41b492f 1643 showError ('Invalid arguments', __FUNCTION__);
e673ee24
DO
1644 die;
1645 }
1646 global $dbxlink;
1647 $query = "delete from AttributeValue where object_id = ${object_id} and attr_id = ${attr_id} limit 1";
1648 $result = $dbxlink->query ($query);
1649 if ($result == NULL)
1650 {
f41b492f 1651 showError ('SQL query failed', __FUNCTION__);
e673ee24
DO
1652 die;
1653 }
1654 return TRUE;
1655}
1656
1657// FIXME: don't share common code with use commitResetAttrValue()
1658function commitUpdateAttrValue ($object_id = 0, $attr_id = 0, $value = '')
1659{
1660 if ($object_id <= 0 or $attr_id <= 0)
1661 {
f41b492f 1662 showError ('Invalid arguments', __FUNCTION__);
e673ee24
DO
1663 die;
1664 }
1665 if (empty ($value))
1666 return commitResetAttrValue ($object_id, $attr_id);
1667 global $dbxlink;
1668 $query1 = "select attr_type from Attribute where attr_id = ${attr_id}";
1669 $result = $dbxlink->query ($query1);
1670 if ($result == NULL)
1671 {
f41b492f 1672 showError ('SQL query #1 failed', __FUNCTION__);
e673ee24
DO
1673 die;
1674 }
1675 $row = $result->fetch (PDO::FETCH_NUM);
1676 if ($row == NULL)
1677 {
f41b492f 1678 showError ('SQL query #1 returned no results', __FUNCTION__);
e673ee24
DO
1679 die;
1680 }
1681 $attr_type = $row[0];
1682 $result->closeCursor();
1683 switch ($attr_type)
1684 {
1685 case 'uint':
1686 case 'float':
1687 case 'string':
1688 $column = $attr_type . '_value';
1689 break;
1690 case 'dict':
1691 $column = 'uint_value';
1692 break;
1693 default:
f41b492f 1694 showError ("Unknown attribute type '${attr_type}' met", __FUNCTION__);
e673ee24
DO
1695 die;
1696 }
1697 $query2 =
1698 "delete from AttributeValue where " .
1699 "object_id = ${object_id} and attr_id = ${attr_id} limit 1";
1700 $result = $dbxlink->query ($query2);
1701 if ($result == NULL)
1702 {
f41b492f 1703 showError ('SQL query #2 failed', __FUNCTION__);
e673ee24
DO
1704 die;
1705 }
1706 // We know $value isn't empty here.
1707 $query3 =
1708 "insert into AttributeValue set ${column} = '${value}', " .
1709 "object_id = ${object_id}, attr_id = ${attr_id} ";
1710 $result = $dbxlink->query ($query3);
1711 if ($result == NULL)
1712 {
f41b492f 1713 showError ('SQL query #3 failed', __FUNCTION__);
e673ee24
DO
1714 die;
1715 }
1716 return TRUE;
1717}
1718
1719function commitUseupPort ($port_id = 0)
1720{
1721 if ($port_id <= 0)
1722 {
f41b492f 1723 showError ("Invalid argument", __FUNCTION__);
e673ee24
DO
1724 die;
1725 }
1726 global $dbxlink;
1727 $query = "update Port set reservation_comment = NULL where id = ${port_id} limit 1";
1728 $result = $dbxlink->exec ($query);
1729 if ($result == NULL)
1730 {
f41b492f 1731 showError ("SQL query failed", __FUNCTION__);
e673ee24
DO
1732 die;
1733 }
1734 return TRUE;
1735
1736}
1737
1738// This is a swiss-knife blade to insert a record into a table.
1739// The first argument is table name.
1740// The second argument is an array of "name" => "value" pairs.
1741// The function returns either TRUE or FALSE (we expect one row
1742// to be inserted).
1743function useInsertBlade ($tablename, $values)
1744{
1745 global $dbxlink;
1746 $namelist = $valuelist = '';
1747 foreach ($values as $name => $value)
1748 {
1749 $namelist = $namelist . ($namelist == '' ? "(${name}" : ", ${name}");
1750 $valuelist = $valuelist . ($valuelist == '' ? "(${value}" : ", ${value}");
1751 }
1752 $query = "insert into ${tablename} ${namelist}) values ${valuelist})";
1753 $result = $dbxlink->exec ($query);
1754 if ($result != 1)
1755 return FALSE;
1756 return TRUE;
1757}
1758
1759// This swiss-knife blade deletes one record from the specified table
1760// using the specified key name and value.
1761function useDeleteBlade ($tablename, $keyname, $keyvalue, $quotekey = TRUE)
1762{
1763 global $dbxlink;
1764 if ($quotekey == TRUE)
1765 $query = "delete from ${tablename} where ${keyname}='$keyvalue' limit 1";
1766 else
1767 $query = "delete from ${tablename} where ${keyname}=$keyvalue limit 1";
1768 $result = $dbxlink->exec ($query);
1769 if ($result === NULL)
1770 return FALSE;
1771 elseif ($result != 1)
1772 return FALSE;
1773 else
1774 return TRUE;
1775}
1776
5967b83d
DO
1777function useSelectBlade ($query, $caller = 'N/A')
1778{
1779 global $dbxlink;
1780 $result = $dbxlink->query ($query);
1781 if ($result == NULL)
1782 {
1783 $ei = $dbxlink->errorInfo();
1784 showError ("SQL query '${query}' failed in useSelectBlade with error ${ei[1]} (${ei[2]})", $caller);
1785 return NULL;
1786 }
1787 return $result;
1788}
1789
9c0b0016
DO
1790function loadConfigCache ()
1791{
4fe32e78 1792 $query = 'SELECT varname, varvalue, vartype, is_hidden, emptyok, description FROM Config ORDER BY varname';
e425f895 1793 $result = useSelectBlade ($query);
9c0b0016
DO
1794 $cache = array();
1795 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1796 $cache[$row['varname']] = $row;
1797 $result->closeCursor();
1798 return $cache;
1799}
1800
1801// setConfigVar() is expected to perform all necessary filtering
1802function storeConfigVar ($varname = NULL, $varvalue = NULL)
1803{
c461c579 1804 global $dbxlink;
bb6b9880 1805 if (empty ($varname) || $varvalue === NULL)
9c0b0016 1806 {
f41b492f 1807 showError ('Invalid arguments', __FUNCTION__);
9c0b0016
DO
1808 return FALSE;
1809 }
1810 $query = "update Config set varvalue='${varvalue}' where varname='${varname}' limit 1";
1811 $result = $dbxlink->query ($query);
1812 if ($result == NULL)
1813 {
f41b492f 1814 showError ("SQL query '${query}' failed", __FUNCTION__);
9c0b0016
DO
1815 return FALSE;
1816 }
1817 $rc = $result->rowCount();
1818 $result->closeCursor();
f7ee57a1 1819 if ($rc == 0 or $rc == 1)
9c0b0016 1820 return TRUE;
f41b492f 1821 showError ("Something went wrong for args '${varname}', '${varvalue}'", __FUNCTION__);
9c0b0016
DO
1822 return FALSE;
1823}
1824
fbbb74fb
DO
1825// Database version detector. Should behave corretly on any
1826// working dataset a user might have.
1827function getDatabaseVersion ()
1828{
1829 global $dbxlink;
1830 $query = "select varvalue from Config where varname = 'DB_VERSION' and vartype = 'string'";
1831 $result = $dbxlink->query ($query);
1832 if ($result == NULL)
1833 {
1834 $errorInfo = $dbxlink->errorInfo();
1835 if ($errorInfo[0] == '42S02') // ER_NO_SUCH_TABLE
1836 return '0.14.4';
4d2e93f2 1837 die (__FUNCTION__ . ': SQL query #1 failed with error ' . $errorInfo[2]);
fbbb74fb
DO
1838 }
1839 $rows = $result->fetchAll (PDO::FETCH_NUM);
1840 if (count ($rows) != 1 || empty ($rows[0][0]))
1841 {
1842 $result->closeCursor();
4d2e93f2 1843 die (__FUNCTION__ . ': Cannot guess database version. Config table is present, but DB_VERSION is missing or invalid. Giving up.');
fbbb74fb
DO
1844 }
1845 $ret = $rows[0][0];
1846 $result->closeCursor();
1847 return $ret;
1848}
1849
da04825a
DO
1850// Return an array of virtual services. For each of them list real server pools
1851// with their load balancers and other stats.
62a1dcb5
DO
1852function getSLBSummary ()
1853{
6fec9f39
DO
1854 $query = 'select vs.id as vsid, inet_ntoa(vip) as vip, vport, proto, vs.name, object_id, ' .
1855 'lb.rspool_id, pool.name as pool_name, count(rs.id) as rscount ' .
1856 'from IPVirtualService as vs inner join IPLoadBalancer as lb on vs.id = lb.vs_id ' .
3e56d972 1857 'inner join IPRSPool as pool on lb.rspool_id = pool.id ' .
6fec9f39
DO
1858 'left join IPRealServer as rs on rs.rspool_id = lb.rspool_id ' .
1859 'group by vs.id, object_id order by vs.vip, object_id';
e425f895 1860 $result = useSelectBlade ($query);
c3bdc503
DO
1861 $ret = array();
1862 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1863 {
4cadac8f 1864 $vsid = $row['vsid'];
da04825a 1865 $object_id = $row['object_id'];
4cadac8f
DO
1866 if (!isset ($ret[$vsid]))
1867 {
1868 $ret[$vsid] = array();
13bffb29 1869 foreach (array ('vip', 'vport', 'proto', 'name') as $cname)
4cadac8f 1870 $ret[$vsid][$cname] = $row[$cname];
da04825a 1871 $ret[$vsid]['lblist'] = array();
4cadac8f 1872 }
6fec9f39
DO
1873 // There's only one assigned RS pool possible for each LB-VS combination.
1874 $ret[$vsid]['lblist'][$row['object_id']] = array
1875 (
1876 'id' => $row['rspool_id'],
1877 'size' => $row['rscount'],
1878 'name' => $row['pool_name']
1879 );
c3bdc503 1880 }
060ba26e 1881 $result->closeCursor();
c3bdc503 1882 return $ret;
62a1dcb5
DO
1883}
1884
da04825a
DO
1885// Get the detailed composition of a particular virtual service, namely the list
1886// of all pools, each shown with the list of objects servicing it. VS/RS configs
1887// will be returned as well.
060ba26e
DO
1888function getVServiceInfo ($vsid = 0)
1889{
2b4eee17
DO
1890 $query1 = "select inet_ntoa(vip) as vip, vport, proto, name, vsconfig, rsconfig " .
1891 "from IPVirtualService where id = ${vsid}";
e425f895 1892 $result1 = useSelectBlade ($query1);
060ba26e 1893 $vsinfo = array ();
2b4eee17
DO
1894 $row = $result1->fetch (PDO::FETCH_ASSOC);
1895 if (!$row)
1896 return NULL;
1897 foreach (array ('vip', 'vport', 'proto', 'name', 'vsconfig', 'rsconfig') as $cname)
1898 $vsinfo[$cname] = $row[$cname];
1899 $vsinfo['rspool'] = array();
1900 $result1->closeCursor();
1901 $query2 = "select pool.id, name, pool.vsconfig, pool.rsconfig, object_id, " .
1902 "lb.vsconfig as lb_vsconfig, lb.rsconfig as lb_rsconfig from " .
1903 "IPRSPool as pool left join IPLoadBalancer as lb on pool.id = lb.rspool_id " .
1904 "where vs_id = ${vsid} order by pool.name, object_id";
e425f895 1905 $result2 = useSelectBlade ($query2);
2b4eee17 1906 while ($row = $result2->fetch (PDO::FETCH_ASSOC))
060ba26e 1907 {
2b4eee17 1908 if (!isset ($vsinfo['rspool'][$row['id']]))
060ba26e 1909 {
2b4eee17
DO
1910 $vsinfo['rspool'][$row['id']]['name'] = $row['name'];
1911 $vsinfo['rspool'][$row['id']]['vsconfig'] = $row['vsconfig'];
1912 $vsinfo['rspool'][$row['id']]['rsconfig'] = $row['rsconfig'];
1913 $vsinfo['rspool'][$row['id']]['lblist'] = array();
060ba26e 1914 }
2b4eee17
DO
1915 if ($row['object_id'] == NULL)
1916 continue;
1917 $vsinfo['rspool'][$row['id']]['lblist'][$row['object_id']] = array
1918 (
1919 'vsconfig' => $row['lb_vsconfig'],
1920 'rsconfig' => $row['lb_rsconfig']
1921 );
060ba26e 1922 }
2b4eee17 1923 $result2->closeCursor();
060ba26e
DO
1924 return $vsinfo;
1925}
1926
71b8bda1
DO
1927// Collect and return the following info about the given real server pool:
1928// basic information
1929// parent virtual service information
c1ca768c 1930// load balancers list (each with a list of VSes)
71b8bda1
DO
1931// real servers list
1932
1933function getRSPoolInfo ($id = 0)
1934{
c1ca768c 1935 $query1 = "select id, name, vsconfig, rsconfig from " .
481a7f8b 1936 "IPRSPool where id = ${id}";
e425f895 1937 $result1 = useSelectBlade ($query1);
71b8bda1
DO
1938 $ret = array();
1939 $row = $result1->fetch (PDO::FETCH_ASSOC);
1940 if (!$row)
1941 return NULL;
70c24883 1942 foreach (array ('id', 'name', 'vsconfig', 'rsconfig') as $c)
71b8bda1
DO
1943 $ret[$c] = $row[$c];
1944 $result1->closeCursor();
1945 $ret['lblist'] = array();
1946 $ret['rslist'] = array();
c1ca768c 1947 $query2 = "select object_id, vs_id, vsconfig, rsconfig from IPLoadBalancer where rspool_id = ${id} order by object_id";
e425f895 1948 $result2 = useSelectBlade ($query2);
71b8bda1
DO
1949 while ($row = $result2->fetch (PDO::FETCH_ASSOC))
1950 foreach (array ('vsconfig', 'rsconfig') as $c)
c1ca768c 1951 $ret['lblist'][$row['object_id']][$row['vs_id']][$c] = $row[$c];
71b8bda1 1952 $result2->closeCursor();
1f7d18fa 1953 $query3 = "select id, inservice, inet_ntoa(rsip) as rsip, rsport, rsconfig from " .
676a8268 1954 "IPRealServer where rspool_id = ${id} order by IPRealServer.rsip, rsport";
e425f895 1955 $result3 = useSelectBlade ($query3);
71b8bda1 1956 while ($row = $result3->fetch (PDO::FETCH_ASSOC))
1f7d18fa 1957 foreach (array ('inservice', 'rsip', 'rsport', 'rsconfig') as $c)
71b8bda1
DO
1958 $ret['rslist'][$row['id']][$c] = $row[$c];
1959 $result3->closeCursor();
1960 return $ret;
1961}
1962
7e7a8387 1963function addRStoRSPool ($pool_id = 0, $rsip = '', $rsport = 0, $inservice = 'no', $rsconfig = '')
ca461127
DO
1964{
1965 if ($pool_id <= 0 or $rsport <= 0)
1966 {
1967 showError ('Invalid arguments', __FUNCTION__);
1968 die;
1969 }
ca461127
DO
1970 return useInsertBlade
1971 (
1972 'IPRealServer',
1973 array
1974 (
1975 'rsip' => "inet_aton('${rsip}')",
1976 'rsport' => $rsport,
1977 'rspool_id' => $pool_id,
bcbeadb4 1978 'inservice' => ($inservice == 'yes' ? "'yes'" : "'no'"),
3241551e
DO
1979 'rsconfig' => (empty ($rsconfig) ? 'NULL' : "'${rsconfig}'")
1980 )
1981 );
1982}
1983
d6517a21
DO
1984function commitCreateVS ($vip = '', $vport = 0, $proto = '', $name = '', $vsconfig, $rsconfig)
1985{
1986 if (empty ($vip) or $vport <= 0 or empty ($proto))
1987 {
1988 showError ('Invalid arguments', __FUNCTION__);
1989 die;
1990 }
1991 return useInsertBlade
1992 (
1993 'IPVirtualService',
1994 array
1995 (
1996 'vip' => "inet_aton('${vip}')",
1997 'vport' => $vport,
1998 'proto' => "'${proto}'",
1999 'name' => (empty ($name) ? 'NULL' : "'${name}'"),
2000 'vsconfig' => (empty ($vsconfig) ? 'NULL' : "'${vsconfig}'"),
2001 'rsconfig' => (empty ($rsconfig) ? 'NULL' : "'${rsconfig}'")
2002 )
2003 );
2004}
2005
c1ca768c 2006function addLBtoRSPool ($pool_id = 0, $object_id = 0, $vs_id = 0, $vsconfig = '', $rsconfig = '')
3241551e 2007{
c1ca768c 2008 if ($pool_id <= 0 or $object_id <= 0 or $vs_id <= 0)
3241551e
DO
2009 {
2010 showError ('Invalid arguments', __FUNCTION__);
2011 die;
2012 }
2013 return useInsertBlade
2014 (
2015 'IPLoadBalancer',
2016 array
2017 (
2018 'object_id' => $object_id,
2019 'rspool_id' => $pool_id,
c1ca768c 2020 'vs_id' => $vs_id,
3241551e
DO
2021 'vsconfig' => (empty ($vsconfig) ? 'NULL' : "'${vsconfig}'"),
2022 'rsconfig' => (empty ($rsconfig) ? 'NULL' : "'${rsconfig}'")
2023 )
ca461127
DO
2024 );
2025}
2026
fb1c4a54
DO
2027function commitDeleteRS ($id = 0)
2028{
2029 if ($id <= 0)
2030 return FALSE;
2031 return useDeleteBlade ('IPRealServer', 'id', $id);
2032}
2033
d6517a21
DO
2034function commitDeleteVS ($id = 0)
2035{
2036 if ($id <= 0)
2037 return FALSE;
2038 return useDeleteBlade ('IPVirtualService', 'id', $id);
2039}
2040
c1ca768c 2041function commitDeleteLB ($object_id = 0, $pool_id = 0, $vs_id = 0)
3241551e
DO
2042{
2043 global $dbxlink;
c4d85fd1 2044 if ($object_id <= 0 or $pool_id <= 0 or $vs_id <= 0)
3241551e 2045 return FALSE;
c1ca768c
DO
2046 $query = "delete from IPLoadBalancer where object_id = ${object_id} and " .
2047 "rspool_id = ${pool_id} and vs_id = ${vs_id} limit 1";
3241551e
DO
2048 $result = $dbxlink->exec ($query);
2049 if ($result === NULL)
2050 return FALSE;
2051 elseif ($result != 1)
2052 return FALSE;
2053 else
2054 return TRUE;
2055}
2056
fb1c4a54
DO
2057function commitUpdateRS ($rsid = 0, $rsip = '', $rsport = 0, $rsconfig = '')
2058{
2059 if ($rsid <= 0 or $rsport <= 0)
2060 {
2061 showError ('Invalid args', __FUNCTION__);
2062 die;
2063 }
2064 if (long2ip (ip2long ($rsip)) !== $rsip)
2065 {
2066 showError ("Invalid IP address '${rsip}'", __FUNCTION__);
2067 die;
2068 }
2069 global $dbxlink;
2070 $query =
2071 "update IPRealServer set rsip = inet_aton('${rsip}'), rsport = ${rsport}, rsconfig = " .
3241551e 2072 (empty ($rsconfig) ? 'NULL' : "'${rsconfig}'") .
fb1c4a54
DO
2073 " where id = ${rsid} limit 1";
2074 $result = $dbxlink->query ($query);
2075 if ($result == NULL)
2076 {
2077 showError ("SQL query '${query}' failed", __FUNCTION__);
2078 die;
2079 }
2080 return TRUE;
2081}
2082
c1ca768c 2083function commitUpdateLB ($object_id = 0, $pool_id = 0, $vs_id = 0, $vsconfig = '', $rsconfig = '')
3241551e 2084{
c1ca768c 2085 if ($object_id <= 0 or $pool_id <= 0 or $vs_id <= 0)
3241551e
DO
2086 {
2087 showError ('Invalid args', __FUNCTION__);
2088 die;
2089 }
2090 global $dbxlink;
2091 $query =
2092 "update IPLoadBalancer set vsconfig = " .
2093 (empty ($vsconfig) ? 'NULL' : "'${vsconfig}'") .
2094 ', rsconfig = ' .
2095 (empty ($rsconfig) ? 'NULL' : "'${rsconfig}'") .
c1ca768c
DO
2096 " where object_id = ${object_id} and rspool_id = ${pool_id} " .
2097 "and vs_id = ${vs_id} limit 1";
3241551e
DO
2098 $result = $dbxlink->exec ($query);
2099 if ($result === NULL)
2100 return FALSE;
3241551e
DO
2101 else
2102 return TRUE;
2103}
2104
d6517a21
DO
2105function commitUpdateVS ($vsid = 0, $vip = '', $vport = 0, $proto = '', $name = '', $vsconfig = '', $rsconfig = '')
2106{
2107 if ($vsid <= 0 or empty ($vip) or $vport <= 0 or empty ($proto))
2108 {
2109 showError ('Invalid args', __FUNCTION__);
2110 die;
2111 }
2112 global $dbxlink;
2113 $query = "update IPVirtualService set " .
2114 "vip = inet_aton('${vip}'), " .
2115 "vport = ${vport}, " .
2116 "proto = '${proto}', " .
2117 'name = ' . (empty ($name) ? 'NULL,' : "'${name}', ") .
2118 'vsconfig = ' . (empty ($vsconfig) ? 'NULL,' : "'${vsconfig}', ") .
5ad76f01
DO
2119 'rsconfig = ' . (empty ($rsconfig) ? 'NULL' : "'${rsconfig}'") .
2120 " where id = ${vsid} limit 1";
d6517a21
DO
2121 $result = $dbxlink->exec ($query);
2122 if ($result === NULL)
2123 return FALSE;
d6517a21
DO
2124 else
2125 return TRUE;
2126}
2127
e57dca7f 2128// Return the list of virtual services, indexed by vs_id.
70c24883 2129// Each record will be shown with its basic info plus RS pools counter.
e57dca7f
DO
2130function getVSList ()
2131{
70c24883
DO
2132 $query = "select vs.id, inet_ntoa(vip) as vip, vport, proto, vs.name, vs.vsconfig, vs.rsconfig, count(rspool_id) as poolcount " .
2133 "from IPVirtualService as vs left join IPLoadBalancer as lb on vs.id = lb.vs_id " .
d6517a21 2134 "group by vs.id order by vs.vip, proto, vport";
e425f895 2135 $result = useSelectBlade ($query);
4b0932b6 2136 $ret = array ();
e57dca7f 2137 while ($row = $result->fetch (PDO::FETCH_ASSOC))
d6517a21 2138 foreach (array ('vip', 'vport', 'proto', 'name', 'vsconfig', 'rsconfig', 'poolcount') as $cname)
4b0932b6 2139 $ret[$row['id']][$cname] = $row[$cname];
e57dca7f 2140 $result->closeCursor();
4b0932b6 2141 return $ret;
e57dca7f
DO
2142}
2143
9a81d416
DO
2144// Return the list of RS pool, indexed by pool id.
2145function getRSPoolList ()
2146{
70c24883
DO
2147 $query = "select pool.id, pool.name, count(rspool_id) as refcnt, pool.vsconfig, pool.rsconfig " .
2148 "from IPRSPool as pool left join IPLoadBalancer as lb on pool.id = lb.rspool_id " .
2149 "group by pool.id order by pool.id, name";
e425f895 2150 $result = useSelectBlade ($query);
4b0932b6 2151 $ret = array ();
9a81d416 2152 while ($row = $result->fetch (PDO::FETCH_ASSOC))
70c24883 2153 foreach (array ('name', 'refcnt', 'vsconfig', 'rsconfig') as $cname)
4b0932b6 2154 $ret[$row['id']][$cname] = $row[$cname];
9a81d416 2155 $result->closeCursor();
4b0932b6 2156 return $ret;
9a81d416
DO
2157}
2158
5a1680d2
DO
2159function loadThumbCache ($rack_id = 0)
2160{
5a1680d2
DO
2161 $ret = NULL;
2162 $query = "select thumb_data from Rack where id = ${rack_id} and thumb_data is not null limit 1";
e425f895 2163 $result = useSelectBlade ($query);
5a1680d2
DO
2164 $row = $result->fetch (PDO::FETCH_ASSOC);
2165 if ($row)
2166 $ret = base64_decode ($row['thumb_data']);
2167 $result->closeCursor();
2168 return $ret;
2169}
2170
2171function saveThumbCache ($rack_id = 0, $cache = NULL)
2172{
2173 global $dbxlink;
2174 if ($rack_id == 0 or $cache == NULL)
2175 {
2176 showError ('Invalid arguments', __FUNCTION__);
2177 return;
2178 }
2179 $data = base64_encode ($cache);
2180 $query = "update Rack set thumb_data = '${data}' where id = ${rack_id} limit 1";
2181 $result = $dbxlink->exec ($query);
2182}
9a81d416 2183
c7fe33be
DO
2184function resetThumbCache ($rack_id = 0)
2185{
2186 global $dbxlink;
2187 if ($rack_id == 0)
2188 {
2189 showError ('Invalid argument', __FUNCTION__);
2190 return;
2191 }
2192 $query = "update Rack set thumb_data = NULL where id = ${rack_id} limit 1";
2193 $result = $dbxlink->exec ($query);
2194}
2195
748805bf
DO
2196// Return the list of attached RS pools for the given object. As long as we have
2197// the LB-VS UNIQUE in IPLoadBalancer table, it is Ok to key returned records
2198// by vs_id, because there will be only one RS pool listed for each VS of the
2199// current object.
78e7b769
DO
2200function getRSPoolsForObject ($object_id = 0)
2201{
2202 if ($object_id <= 0)
2203 {
2204 showError ('Invalid object_id', __FUNCTION__);
2205 return NULL;
2206 }
748805bf 2207 $query = 'select vs_id, inet_ntoa(vip) as vip, vport, proto, vs.name, pool.id as pool_id, ' .
732e4578 2208 'pool.name as pool_name, count(rsip) as rscount, lb.vsconfig, lb.rsconfig from ' .
748805bf
DO
2209 'IPLoadBalancer as lb inner join IPRSPool as pool on lb.rspool_id = pool.id ' .
2210 'inner join IPVirtualService as vs on lb.vs_id = vs.id ' .
2211 'left join IPRealServer as rs on lb.rspool_id = rs.rspool_id ' .
2212 "where lb.object_id = ${object_id} " .
2213 'group by lb.rspool_id, lb.vs_id order by vs.vip, vport, proto, pool.name';
e425f895 2214 $result = useSelectBlade ($query);
748805bf 2215 $ret = array ();
78e7b769 2216 while ($row = $result->fetch (PDO::FETCH_ASSOC))
732e4578 2217 foreach (array ('vip', 'vport', 'proto', 'name', 'pool_id', 'pool_name', 'rscount', 'vsconfig', 'rsconfig') as $cname)
748805bf 2218 $ret[$row['vs_id']][$cname] = $row[$cname];
78e7b769 2219 $result->closeCursor();
748805bf 2220 return $ret;
78e7b769
DO
2221}
2222
70c24883 2223function commitCreateRSPool ($name = '', $vsconfig = '', $rsconfig = '')
5ad76f01 2224{
5ad76f01
DO
2225 return useInsertBlade
2226 (
2227 'IPRSPool',
2228 array
2229 (
5ad76f01
DO
2230 'name' => (empty ($name) ? 'NULL' : "'${name}'"),
2231 'vsconfig' => (empty ($vsconfig) ? 'NULL' : "'${vsconfig}'"),
2232 'rsconfig' => (empty ($rsconfig) ? 'NULL' : "'${rsconfig}'")
2233 )
2234 );
2235}
2236
2237function commitDeleteRSPool ($pool_id = 0)
2238{
2239 global $dbxlink;
2240 if ($pool_id <= 0)
2241 return FALSE;
2242 $query = "delete from IPRSPool where id = ${pool_id} limit 1";
2243 $result = $dbxlink->exec ($query);
2244 if ($result === NULL)
2245 return FALSE;
2246 elseif ($result != 1)
2247 return FALSE;
2248 else
2249 return TRUE;
2250}
2251
2252function commitUpdateRSPool ($pool_id = 0, $name = '', $vsconfig = '', $rsconfig = '')
2253{
2254 if ($pool_id <= 0)
2255 {
2256 showError ('Invalid arg', __FUNCTION__);
2257 die;
2258 }
2259 global $dbxlink;
2260 $query = "update IPRSPool set " .
2261 'name = ' . (empty ($name) ? 'NULL,' : "'${name}', ") .
2262 'vsconfig = ' . (empty ($vsconfig) ? 'NULL,' : "'${vsconfig}', ") .
2263 'rsconfig = ' . (empty ($rsconfig) ? 'NULL' : "'${rsconfig}'") .
2264 " where id = ${pool_id} limit 1";
2265 $result = $dbxlink->exec ($query);
2266 if ($result === NULL)
2267 return FALSE;
2268 elseif ($result != 1)
2269 return FALSE;
2270 else
2271 return TRUE;
2272}
2273
8253d9f0
DO
2274function getRSList ()
2275{
1f7d18fa 2276 $query = "select id, inservice, inet_ntoa(rsip) as rsip, rsport, rspool_id, rsconfig " .
8253d9f0 2277 "from IPRealServer order by rspool_id, IPRealServer.rsip, rsport";
e425f895 2278 $result = useSelectBlade ($query);
4b0932b6 2279 $ret = array ();
8253d9f0 2280 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1f7d18fa 2281 foreach (array ('inservice', 'rsip', 'rsport', 'rspool_id', 'rsconfig') as $cname)
4b0932b6
DO
2282 $ret[$row['id']][$cname] = $row[$cname];
2283 $result->closeCursor();
2284 return $ret;
2285}
2286
2287// Return the list of all currently configured load balancers with their pool count.
2288function getLBList ()
2289{
4b0932b6
DO
2290 $query = "select object_id, count(rspool_id) as poolcount " .
2291 "from IPLoadBalancer group by object_id order by object_id";
e425f895 2292 $result = useSelectBlade ($query);
4b0932b6
DO
2293 $ret = array ();
2294 while ($row = $result->fetch (PDO::FETCH_ASSOC))
2295 $ret[$row['object_id']] = $row['poolcount'];
8253d9f0 2296 $result->closeCursor();
4b0932b6 2297 return $ret;
8253d9f0
DO
2298}
2299
9e677cbd
DO
2300// For the given object return: it vsconfig/rsconfig; the list of RS pools
2301// attached (each with vsconfig/rsconfig in turn), each with the list of
2302// virtual services terminating the pool. Each pool also lists all real
2303// servers with rsconfig.
2304function buildLBConfig ($object_id)
2305{
2306 if ($object_id <= 0)
2307 {
2308 showError ('Invalid arg', __FUNCTION__);
2309 return NULL;
2310 }
9e677cbd 2311 $ret = array();
d91f08f3
DO
2312 $query = 'select vs_id, inet_ntoa(vip) as vip, vport, proto, vs.name as vs_name, ' .
2313 'vs.vsconfig as vs_vsconfig, vs.rsconfig as vs_rsconfig, ' .
2314 'lb.vsconfig as lb_vsconfig, lb.rsconfig as lb_rsconfig, pool.id as pool_id, pool.name as pool_name, ' .
9e677cbd 2315 'pool.vsconfig as pool_vsconfig, pool.rsconfig as pool_rsconfig, ' .
d91f08f3
DO
2316 'rs.id as rs_id, inet_ntoa(rsip) as rsip, rsport, rs.rsconfig as rs_rsconfig from ' .
2317 'IPLoadBalancer as lb inner join IPRSPool as pool on lb.rspool_id = pool.id ' .
2318 'inner join IPVirtualService as vs on lb.vs_id = vs.id ' .
9e677cbd 2319 'inner join IPRealServer as rs on lb.rspool_id = rs.rspool_id ' .
1f7d18fa
DO
2320 "where lb.object_id = ${object_id} and rs.inservice = 'yes' " .
2321 "order by vs.vip, vport, proto, pool.name, rs.rsip, rs.rsport";
e425f895 2322 $result = useSelectBlade ($query);
9e677cbd
DO
2323 while ($row = $result->fetch (PDO::FETCH_ASSOC))
2324 {
d91f08f3
DO
2325 $vs_id = $row['vs_id'];
2326 if (!isset ($ret[$vs_id]))
2327 {
2328 foreach (array ('vip', 'vport', 'proto', 'vs_name', 'vs_vsconfig', 'vs_rsconfig', 'lb_vsconfig', 'lb_rsconfig', 'pool_vsconfig', 'pool_rsconfig', 'pool_id', 'pool_name') as $c)
2329 $ret[$vs_id][$c] = $row[$c];
2330 $ret[$vs_id]['rslist'] = array();
2331 }
2332 foreach (array ('rsip', 'rsport', 'rs_rsconfig') as $c)
2333 $ret[$vs_id]['rslist'][$row['rs_id']][$c] = $row[$c];
9e677cbd
DO
2334 }
2335 $result->closeCursor();
2336 return $ret;
2337}
1f7d18fa
DO
2338
2339function commitSetInService ($rs_id = 0, $inservice = '')
2340{
2341 if ($rs_id <= 0 or empty ($inservice))
2342 {
2343 showError ('Invalid args', __FUNCTION__);
2344 return NULL;
2345 }
2346 global $dbxlink;
2347 $query = "update IPRealServer set inservice = '${inservice}' where id = ${rs_id} limit 1";
2348 $result = $dbxlink->exec ($query);
2349 if ($result === NULL)
2350 return FALSE;
2351 elseif ($result != 1)
2352 return FALSE;
2353 else
2354 return TRUE;
2355}
2356
ad0e4754
DO
2357function executeAutoPorts ($object_id = 0, $type_id = 0)
2358{
2359 if ($object_id == 0 or $type_id == 0)
2360 {
2361 showError ('Invalid arguments', __FUNCTION__);
2362 die;
2363 }
f3f0161f 2364 $ret = TRUE;
118e4c38 2365 foreach (getAutoPorts ($type_id) as $autoport)
f3f0161f
DO
2366 $ret = $ret and '' == commitAddPort ($object_id, $autoport['name'], $autoport['type'], '', '');
2367 return $ret;
ad0e4754
DO
2368}
2369
a6e8d544
DO
2370// Return only implicitly listed tags, the rest of the trail will be
2371// generated/deducted later at higher levels.
5967b83d
DO
2372function getObjectTags ($object_id = 0)
2373{
a6e8d544
DO
2374 $ret = array();
2375 $result = useSelectBlade ("select tt.id as tag_id, tag as tag_name from RackObject as ro inner join RackObjectTags as rot on ro.id = rot.object_id inner join TagTree as tt on rot.tag_id = tt.id");
5967b83d 2376 while ($row = $result->fetch (PDO::FETCH_ASSOC))
a6e8d544
DO
2377 $ret[$row['tag_id']] = $row['tag_name'];
2378 $result->closeCursor();
2379 return $ret;
5967b83d
DO
2380}
2381
2382function getTagList ()
2383{
2384 $taglist = array();
2385 $result = useSelectBlade ("select id, parent_id, tag from TagTree order by tag");
2386 while ($row = $result->fetch (PDO::FETCH_ASSOC))
2387 $taglist[$row['id']] = array ('tag' => $row['tag'], 'parent_id' => $row['parent_id']);
2388 $result->closeCursor();
2389 return $taglist;
2390}
2391
e673ee24 2392?>