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