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