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