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