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