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