| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * This file is a library of computational functions for RackTables. |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | $loclist[0] = 'front'; |
| 9 | $loclist[1] = 'interior'; |
| 10 | $loclist[2] = 'rear'; |
| 11 | $loclist['front'] = 0; |
| 12 | $loclist['interior'] = 1; |
| 13 | $loclist['rear'] = 2; |
| 14 | $template[0] = array (TRUE, TRUE, TRUE); |
| 15 | $template[1] = array (TRUE, TRUE, FALSE); |
| 16 | $template[2] = array (FALSE, TRUE, TRUE); |
| 17 | $template[3] = array (TRUE, FALSE, FALSE); |
| 18 | $template[4] = array (FALSE, TRUE, FALSE); |
| 19 | $template[5] = array (FALSE, FALSE, TRUE); |
| 20 | $templateWidth[0] = 3; |
| 21 | $templateWidth[1] = 2; |
| 22 | $templateWidth[2] = 2; |
| 23 | $templateWidth[3] = 1; |
| 24 | $templateWidth[4] = 1; |
| 25 | $templateWidth[5] = 1; |
| 26 | |
| 27 | // Entity type by page number mapping is 1:1 atm, but may change later. |
| 28 | $etype_by_pageno = array |
| 29 | ( |
| 30 | 'ipv4net' => 'ipv4net', |
| 31 | 'ipv4rspool' => 'ipv4rspool', |
| 32 | 'ipv4vs' => 'ipv4vs', |
| 33 | 'object' => 'object', |
| 34 | 'rack' => 'rack', |
| 35 | 'user' => 'user', |
| 36 | 'file' => 'file', |
| 37 | 'ipaddress' => 'ipaddress', |
| 38 | ); |
| 39 | |
| 40 | // Objects of some types should be explicitly shown as |
| 41 | // anonymous (labelless). This function is a single place where the |
| 42 | // decision about displayed name is made. |
| 43 | function displayedName ($objectData) |
| 44 | { |
| 45 | if ($objectData['name'] != '') |
| 46 | return $objectData['name']; |
| 47 | elseif (considerConfiguredConstraint ('object', $objectData['id'], 'NAMEWARN_LISTSRC')) |
| 48 | return "ANONYMOUS " . $objectData['objtype_name']; |
| 49 | else |
| 50 | return "[${objectData['objtype_name']}]"; |
| 51 | } |
| 52 | |
| 53 | // This function finds height of solid rectangle of atoms, which are all |
| 54 | // assigned to the same object. Rectangle base is defined by specified |
| 55 | // template. |
| 56 | function rectHeight ($rackData, $startRow, $template_idx) |
| 57 | { |
| 58 | $height = 0; |
| 59 | // The first met object_id is used to match all the folowing IDs. |
| 60 | $object_id = 0; |
| 61 | global $template; |
| 62 | do |
| 63 | { |
| 64 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 65 | { |
| 66 | // At least one value in template is TRUE, but the following block |
| 67 | // can meet 'skipped' atoms. Let's ensure we have something after processing |
| 68 | // the first row. |
| 69 | if ($template[$template_idx][$locidx]) |
| 70 | { |
| 71 | if (isset ($rackData[$startRow - $height][$locidx]['skipped'])) |
| 72 | break 2; |
| 73 | if (isset ($rackData[$startRow - $height][$locidx]['rowspan'])) |
| 74 | break 2; |
| 75 | if (isset ($rackData[$startRow - $height][$locidx]['colspan'])) |
| 76 | break 2; |
| 77 | if ($rackData[$startRow - $height][$locidx]['state'] != 'T') |
| 78 | break 2; |
| 79 | if ($object_id == 0) |
| 80 | $object_id = $rackData[$startRow - $height][$locidx]['object_id']; |
| 81 | if ($object_id != $rackData[$startRow - $height][$locidx]['object_id']) |
| 82 | break 2; |
| 83 | } |
| 84 | } |
| 85 | // If the first row can't offer anything, bail out. |
| 86 | if ($height == 0 and $object_id == 0) |
| 87 | break; |
| 88 | $height++; |
| 89 | } |
| 90 | while ($startRow - $height > 0); |
| 91 | # echo "for startRow==${startRow} and template==(" . ($template[$template_idx][0] ? 'T' : 'F'); |
| 92 | # echo ', ' . ($template[$template_idx][1] ? 'T' : 'F') . ', ' . ($template[$template_idx][2] ? 'T' : 'F'); |
| 93 | # echo ") height==${height}<br>\n"; |
| 94 | return $height; |
| 95 | } |
| 96 | |
| 97 | // This function marks atoms to be avoided by rectHeight() and assigns rowspan/colspan |
| 98 | // attributes. |
| 99 | function markSpan (&$rackData, $startRow, $maxheight, $template_idx) |
| 100 | { |
| 101 | global $template, $templateWidth; |
| 102 | $colspan = 0; |
| 103 | for ($height = 0; $height < $maxheight; $height++) |
| 104 | { |
| 105 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 106 | { |
| 107 | if ($template[$template_idx][$locidx]) |
| 108 | { |
| 109 | // Add colspan/rowspan to the first row met and mark the following ones to skip. |
| 110 | // Explicitly show even single-cell spanned atoms, because rectHeight() |
| 111 | // is expeciting this data for correct calculation. |
| 112 | if ($colspan != 0) |
| 113 | $rackData[$startRow - $height][$locidx]['skipped'] = TRUE; |
| 114 | else |
| 115 | { |
| 116 | $colspan = $templateWidth[$template_idx]; |
| 117 | if ($colspan >= 1) |
| 118 | $rackData[$startRow - $height][$locidx]['colspan'] = $colspan; |
| 119 | if ($maxheight >= 1) |
| 120 | $rackData[$startRow - $height][$locidx]['rowspan'] = $maxheight; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | // This function sets rowspan/solspan/skipped atom attributes for renderRack() |
| 129 | // What we actually have to do is to find _all_ possible rectangles for each unit |
| 130 | // and then select the widest of those with the maximal square. |
| 131 | function markAllSpans (&$rackData = NULL) |
| 132 | { |
| 133 | if ($rackData == NULL) |
| 134 | { |
| 135 | showError ('Invalid rackData', __FUNCTION__); |
| 136 | return; |
| 137 | } |
| 138 | for ($i = $rackData['height']; $i > 0; $i--) |
| 139 | while (markBestSpan ($rackData, $i)); |
| 140 | } |
| 141 | |
| 142 | // Calculate height of 6 possible span templates (array is presorted by width |
| 143 | // descending) and mark the best (if any). |
| 144 | function markBestSpan (&$rackData, $i) |
| 145 | { |
| 146 | global $template, $templateWidth; |
| 147 | for ($j = 0; $j < 6; $j++) |
| 148 | { |
| 149 | $height[$j] = rectHeight ($rackData, $i, $j); |
| 150 | $square[$j] = $height[$j] * $templateWidth[$j]; |
| 151 | } |
| 152 | // find the widest rectangle of those with maximal height |
| 153 | $maxsquare = max ($square); |
| 154 | if (!$maxsquare) |
| 155 | return FALSE; |
| 156 | $best_template_index = 0; |
| 157 | for ($j = 0; $j < 6; $j++) |
| 158 | if ($square[$j] == $maxsquare) |
| 159 | { |
| 160 | $best_template_index = $j; |
| 161 | $bestheight = $height[$j]; |
| 162 | break; |
| 163 | } |
| 164 | // distribute span marks |
| 165 | markSpan ($rackData, $i, $bestheight, $best_template_index); |
| 166 | return TRUE; |
| 167 | } |
| 168 | |
| 169 | // We can mount 'F' atoms and unmount our own 'T' atoms. |
| 170 | function applyObjectMountMask (&$rackData, $object_id) |
| 171 | { |
| 172 | for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--) |
| 173 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 174 | switch ($rackData[$unit_no][$locidx]['state']) |
| 175 | { |
| 176 | case 'F': |
| 177 | $rackData[$unit_no][$locidx]['enabled'] = TRUE; |
| 178 | break; |
| 179 | case 'T': |
| 180 | $rackData[$unit_no][$locidx]['enabled'] = ($rackData[$unit_no][$locidx]['object_id'] == $object_id); |
| 181 | break; |
| 182 | default: |
| 183 | $rackData[$unit_no][$locidx]['enabled'] = FALSE; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // Design change means transition between 'F' and 'A' and back. |
| 188 | function applyRackDesignMask (&$rackData) |
| 189 | { |
| 190 | for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--) |
| 191 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 192 | switch ($rackData[$unit_no][$locidx]['state']) |
| 193 | { |
| 194 | case 'F': |
| 195 | case 'A': |
| 196 | $rackData[$unit_no][$locidx]['enabled'] = TRUE; |
| 197 | break; |
| 198 | default: |
| 199 | $rackData[$unit_no][$locidx]['enabled'] = FALSE; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // The same for 'F' and 'U'. |
| 204 | function applyRackProblemMask (&$rackData) |
| 205 | { |
| 206 | for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--) |
| 207 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 208 | switch ($rackData[$unit_no][$locidx]['state']) |
| 209 | { |
| 210 | case 'F': |
| 211 | case 'U': |
| 212 | $rackData[$unit_no][$locidx]['enabled'] = TRUE; |
| 213 | break; |
| 214 | default: |
| 215 | $rackData[$unit_no][$locidx]['enabled'] = FALSE; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // This function highlights specified object (and removes previous highlight). |
| 220 | function highlightObject (&$rackData, $object_id) |
| 221 | { |
| 222 | for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--) |
| 223 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 224 | if |
| 225 | ( |
| 226 | $rackData[$unit_no][$locidx]['state'] == 'T' and |
| 227 | $rackData[$unit_no][$locidx]['object_id'] == $object_id |
| 228 | ) |
| 229 | $rackData[$unit_no][$locidx]['hl'] = 'h'; |
| 230 | else |
| 231 | unset ($rackData[$unit_no][$locidx]['hl']); |
| 232 | } |
| 233 | |
| 234 | // This function marks atoms to selected or not depending on their current state. |
| 235 | function markupAtomGrid (&$data, $checked_state) |
| 236 | { |
| 237 | for ($unit_no = $data['height']; $unit_no > 0; $unit_no--) |
| 238 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 239 | { |
| 240 | if (!($data[$unit_no][$locidx]['enabled'] === TRUE)) |
| 241 | continue; |
| 242 | if ($data[$unit_no][$locidx]['state'] == $checked_state) |
| 243 | $data[$unit_no][$locidx]['checked'] = ' checked'; |
| 244 | else |
| 245 | $data[$unit_no][$locidx]['checked'] = ''; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // This function is almost a clone of processGridForm(), but doesn't save anything to database |
| 250 | // Return value is the changed rack data. |
| 251 | // Here we assume that correct filter has already been applied, so we just |
| 252 | // set or unset checkbox inputs w/o changing atom state. |
| 253 | function mergeGridFormToRack (&$rackData) |
| 254 | { |
| 255 | $rack_id = $rackData['id']; |
| 256 | for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--) |
| 257 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 258 | { |
| 259 | if ($rackData[$unit_no][$locidx]['enabled'] != TRUE) |
| 260 | continue; |
| 261 | $inputname = "atom_${rack_id}_${unit_no}_${locidx}"; |
| 262 | if (isset ($_REQUEST[$inputname]) and $_REQUEST[$inputname] == 'on') |
| 263 | $rackData[$unit_no][$locidx]['checked'] = ' checked'; |
| 264 | else |
| 265 | $rackData[$unit_no][$locidx]['checked'] = ''; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // netmask conversion from length to number |
| 270 | function binMaskFromDec ($maskL) |
| 271 | { |
| 272 | $map_straight = array ( |
| 273 | 0 => 0x00000000, |
| 274 | 1 => 0x80000000, |
| 275 | 2 => 0xc0000000, |
| 276 | 3 => 0xe0000000, |
| 277 | 4 => 0xf0000000, |
| 278 | 5 => 0xf8000000, |
| 279 | 6 => 0xfc000000, |
| 280 | 7 => 0xfe000000, |
| 281 | 8 => 0xff000000, |
| 282 | 9 => 0xff800000, |
| 283 | 10 => 0xffc00000, |
| 284 | 11 => 0xffe00000, |
| 285 | 12 => 0xfff00000, |
| 286 | 13 => 0xfff80000, |
| 287 | 14 => 0xfffc0000, |
| 288 | 15 => 0xfffe0000, |
| 289 | 16 => 0xffff0000, |
| 290 | 17 => 0xffff8000, |
| 291 | 18 => 0xffffc000, |
| 292 | 19 => 0xffffe000, |
| 293 | 20 => 0xfffff000, |
| 294 | 21 => 0xfffff800, |
| 295 | 22 => 0xfffffc00, |
| 296 | 23 => 0xfffffe00, |
| 297 | 24 => 0xffffff00, |
| 298 | 25 => 0xffffff80, |
| 299 | 26 => 0xffffffc0, |
| 300 | 27 => 0xffffffe0, |
| 301 | 28 => 0xfffffff0, |
| 302 | 29 => 0xfffffff8, |
| 303 | 30 => 0xfffffffc, |
| 304 | 31 => 0xfffffffe, |
| 305 | 32 => 0xffffffff, |
| 306 | ); |
| 307 | return $map_straight[$maskL]; |
| 308 | } |
| 309 | |
| 310 | // complementary value |
| 311 | function binInvMaskFromDec ($maskL) |
| 312 | { |
| 313 | $map_compl = array ( |
| 314 | 0 => 0xffffffff, |
| 315 | 1 => 0x7fffffff, |
| 316 | 2 => 0x3fffffff, |
| 317 | 3 => 0x1fffffff, |
| 318 | 4 => 0x0fffffff, |
| 319 | 5 => 0x07ffffff, |
| 320 | 6 => 0x03ffffff, |
| 321 | 7 => 0x01ffffff, |
| 322 | 8 => 0x00ffffff, |
| 323 | 9 => 0x007fffff, |
| 324 | 10 => 0x003fffff, |
| 325 | 11 => 0x001fffff, |
| 326 | 12 => 0x000fffff, |
| 327 | 13 => 0x0007ffff, |
| 328 | 14 => 0x0003ffff, |
| 329 | 15 => 0x0001ffff, |
| 330 | 16 => 0x0000ffff, |
| 331 | 17 => 0x00007fff, |
| 332 | 18 => 0x00003fff, |
| 333 | 19 => 0x00001fff, |
| 334 | 20 => 0x00000fff, |
| 335 | 21 => 0x000007ff, |
| 336 | 22 => 0x000003ff, |
| 337 | 23 => 0x000001ff, |
| 338 | 24 => 0x000000ff, |
| 339 | 25 => 0x0000007f, |
| 340 | 26 => 0x0000003f, |
| 341 | 27 => 0x0000001f, |
| 342 | 28 => 0x0000000f, |
| 343 | 29 => 0x00000007, |
| 344 | 30 => 0x00000003, |
| 345 | 31 => 0x00000001, |
| 346 | 32 => 0x00000000, |
| 347 | ); |
| 348 | return $map_compl[$maskL]; |
| 349 | } |
| 350 | |
| 351 | // This function looks up 'has_problems' flag for 'T' atoms |
| 352 | // and modifies 'hl' key. May be, this should be better done |
| 353 | // in amplifyCell(). We don't honour 'skipped' key, because |
| 354 | // the function is also used for thumb creation. |
| 355 | function markupObjectProblems (&$rackData) |
| 356 | { |
| 357 | for ($i = $rackData['height']; $i > 0; $i--) |
| 358 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 359 | if ($rackData[$i][$locidx]['state'] == 'T') |
| 360 | { |
| 361 | $object = getObjectInfo ($rackData[$i][$locidx]['object_id'], FALSE); |
| 362 | if ($object['has_problems'] == 'yes') |
| 363 | { |
| 364 | // Object can be already highlighted. |
| 365 | if (isset ($rackData[$i][$locidx]['hl'])) |
| 366 | $rackData[$i][$locidx]['hl'] = $rackData[$i][$locidx]['hl'] . 'w'; |
| 367 | else |
| 368 | $rackData[$i][$locidx]['hl'] = 'w'; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | function search_cmpObj ($a, $b) |
| 374 | { |
| 375 | return ($a['score'] > $b['score'] ? -1 : 1); |
| 376 | } |
| 377 | |
| 378 | function getObjectSearchResults ($terms) |
| 379 | { |
| 380 | $objects = array(); |
| 381 | mergeSearchResults ($objects, $terms, 'name'); |
| 382 | mergeSearchResults ($objects, $terms, 'label'); |
| 383 | mergeSearchResults ($objects, $terms, 'asset_no'); |
| 384 | mergeSearchResults ($objects, $terms, 'barcode'); |
| 385 | if (count ($objects) == 1) |
| 386 | usort ($objects, 'search_cmpObj'); |
| 387 | return $objects; |
| 388 | } |
| 389 | |
| 390 | // This function removes all colons and dots from a string. |
| 391 | function l2addressForDatabase ($string) |
| 392 | { |
| 393 | $string = strtoupper ($string); |
| 394 | switch (TRUE) |
| 395 | { |
| 396 | case ($string == '' or preg_match (RE_L2_SOLID, $string)): |
| 397 | return $string; |
| 398 | case (preg_match (RE_L2_IFCFG, $string)): |
| 399 | $pieces = explode (':', $string); |
| 400 | // This workaround is for SunOS ifconfig. |
| 401 | foreach ($pieces as &$byte) |
| 402 | if (strlen ($byte) == 1) |
| 403 | $byte = '0' . $byte; |
| 404 | // And this workaround is for PHP. |
| 405 | unset ($byte); |
| 406 | return implode ('', $pieces); |
| 407 | case (preg_match (RE_L2_CISCO, $string)): |
| 408 | return implode ('', explode ('.', $string)); |
| 409 | case (preg_match (RE_L2_IPCFG, $string)): |
| 410 | return implode ('', explode ('-', $string)); |
| 411 | default: |
| 412 | return NULL; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | function l2addressFromDatabase ($string) |
| 417 | { |
| 418 | switch (strlen ($string)) |
| 419 | { |
| 420 | case 12: // Ethernet |
| 421 | case 16: // FireWire |
| 422 | $ret = implode (':', str_split ($string, 2)); |
| 423 | break; |
| 424 | default: |
| 425 | $ret = $string; |
| 426 | break; |
| 427 | } |
| 428 | return $ret; |
| 429 | } |
| 430 | |
| 431 | // The following 2 functions return previous and next rack IDs for |
| 432 | // a given rack ID. The order of racks is the same as in renderRackspace() |
| 433 | // or renderRow(). |
| 434 | function getPrevIDforRack ($row_id = 0, $rack_id = 0) |
| 435 | { |
| 436 | if ($row_id <= 0 or $rack_id <= 0) |
| 437 | { |
| 438 | showError ('Invalid arguments passed', __FUNCTION__); |
| 439 | return NULL; |
| 440 | } |
| 441 | $rackList = listCells ('rack', $row_id); |
| 442 | doubleLink ($rackList); |
| 443 | if (isset ($rackList[$rack_id]['prev_key'])) |
| 444 | return $rackList[$rack_id]['prev_key']; |
| 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | function getNextIDforRack ($row_id = 0, $rack_id = 0) |
| 449 | { |
| 450 | if ($row_id <= 0 or $rack_id <= 0) |
| 451 | { |
| 452 | showError ('Invalid arguments passed', __FUNCTION__); |
| 453 | return NULL; |
| 454 | } |
| 455 | $rackList = listCells ('rack', $row_id); |
| 456 | doubleLink ($rackList); |
| 457 | if (isset ($rackList[$rack_id]['next_key'])) |
| 458 | return $rackList[$rack_id]['next_key']; |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | // This function finds previous and next array keys for each array key and |
| 463 | // modifies its argument accordingly. |
| 464 | function doubleLink (&$array) |
| 465 | { |
| 466 | $prev_key = NULL; |
| 467 | foreach (array_keys ($array) as $key) |
| 468 | { |
| 469 | if ($prev_key) |
| 470 | { |
| 471 | $array[$key]['prev_key'] = $prev_key; |
| 472 | $array[$prev_key]['next_key'] = $key; |
| 473 | } |
| 474 | $prev_key = $key; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | function sortTokenize ($a, $b) |
| 479 | { |
| 480 | $aold=''; |
| 481 | while ($a != $aold) |
| 482 | { |
| 483 | $aold=$a; |
| 484 | $a = ereg_replace('[^a-zA-Z0-9]',' ',$a); |
| 485 | $a = ereg_replace('([0-9])([a-zA-Z])','\\1 \\2',$a); |
| 486 | $a = ereg_replace('([a-zA-Z])([0-9])','\\1 \\2',$a); |
| 487 | } |
| 488 | |
| 489 | $bold=''; |
| 490 | while ($b != $bold) |
| 491 | { |
| 492 | $bold=$b; |
| 493 | $b = ereg_replace('[^a-zA-Z0-9]',' ',$b); |
| 494 | $b = ereg_replace('([0-9])([a-zA-Z])','\\1 \\2',$b); |
| 495 | $b = ereg_replace('([a-zA-Z])([0-9])','\\1 \\2',$b); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | |
| 500 | $ar = explode(' ', $a); |
| 501 | $br = explode(' ', $b); |
| 502 | for ($i=0; $i<count($ar) && $i<count($br); $i++) |
| 503 | { |
| 504 | $ret = 0; |
| 505 | if (is_numeric($ar[$i]) and is_numeric($br[$i])) |
| 506 | $ret = ($ar[$i]==$br[$i])?0:($ar[$i]<$br[$i]?-1:1); |
| 507 | else |
| 508 | $ret = strcasecmp($ar[$i], $br[$i]); |
| 509 | if ($ret != 0) |
| 510 | return $ret; |
| 511 | } |
| 512 | if ($i<count($ar)) |
| 513 | return 1; |
| 514 | if ($i<count($br)) |
| 515 | return -1; |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | function sortByName ($a, $b) |
| 520 | { |
| 521 | return sortTokenize($a['name'], $b['name']); |
| 522 | } |
| 523 | |
| 524 | function sortEmptyPorts ($a, $b) |
| 525 | { |
| 526 | $objname_cmp = sortTokenize($a['Object_name'], $b['Object_name']); |
| 527 | if ($objname_cmp == 0) |
| 528 | { |
| 529 | return sortTokenize($a['Port_name'], $b['Port_name']); |
| 530 | } |
| 531 | return $objname_cmp; |
| 532 | } |
| 533 | |
| 534 | function sortObjectAddressesAndNames ($a, $b) |
| 535 | { |
| 536 | $objname_cmp = sortTokenize($a['object_name'], $b['object_name']); |
| 537 | if ($objname_cmp == 0) |
| 538 | { |
| 539 | $name_a = (isset ($a['port_name'])) ? $a['port_name'] : ''; |
| 540 | $name_b = (isset ($b['port_name'])) ? $b['port_name'] : ''; |
| 541 | $objname_cmp = sortTokenize($name_a, $name_b); |
| 542 | if ($objname_cmp == 0) |
| 543 | sortTokenize($a['ip'], $b['ip']); |
| 544 | return $objname_cmp; |
| 545 | } |
| 546 | return $objname_cmp; |
| 547 | } |
| 548 | |
| 549 | function sortAddresses ($a, $b) |
| 550 | { |
| 551 | $name_cmp = sortTokenize($a['name'], $b['name']); |
| 552 | if ($name_cmp == 0) |
| 553 | { |
| 554 | return sortTokenize($a['ip'], $b['ip']); |
| 555 | } |
| 556 | return $name_cmp; |
| 557 | } |
| 558 | |
| 559 | // This function expands port compat list into a matrix. |
| 560 | function buildPortCompatMatrixFromList ($portTypeList, $portCompatList) |
| 561 | { |
| 562 | $matrix = array(); |
| 563 | // Create type matrix and markup compatible types. |
| 564 | foreach (array_keys ($portTypeList) as $type1) |
| 565 | foreach (array_keys ($portTypeList) as $type2) |
| 566 | $matrix[$type1][$type2] = FALSE; |
| 567 | foreach ($portCompatList as $pair) |
| 568 | $matrix[$pair['type1']][$pair['type2']] = TRUE; |
| 569 | return $matrix; |
| 570 | } |
| 571 | |
| 572 | // This function returns an array of single element of object's FQDN attribute, |
| 573 | // if FQDN is set. The next choice is object's common name, if it looks like a |
| 574 | // hostname. Otherwise an array of all 'regular' IP addresses of the |
| 575 | // object is returned (which may appear 0 and more elements long). |
| 576 | function findAllEndpoints ($object_id, $fallback = '') |
| 577 | { |
| 578 | $values = getAttrValues ($object_id); |
| 579 | foreach ($values as $record) |
| 580 | if ($record['name'] == 'FQDN' && !empty ($record['value'])) |
| 581 | return array ($record['value']); |
| 582 | $regular = array(); |
| 583 | foreach (getObjectIPv4Allocations ($object_id) as $dottedquad => $alloc) |
| 584 | if ($alloc['type'] == 'regular') |
| 585 | $regular[] = $dottedquad; |
| 586 | if (!count ($regular) && !empty ($fallback)) |
| 587 | return array ($fallback); |
| 588 | return $regular; |
| 589 | } |
| 590 | |
| 591 | // Some records in the dictionary may be written as plain text or as Wiki |
| 592 | // link in the following syntax: |
| 593 | // 1. word |
| 594 | // 2. [[word URL]] // FIXME: this isn't working |
| 595 | // 3. [[word word word | URL]] |
| 596 | // This function parses the line and returns text suitable for either A |
| 597 | // (rendering <A HREF>) or O (for <OPTION>). |
| 598 | function parseWikiLink ($line, $which, $strip_optgroup = FALSE) |
| 599 | { |
| 600 | if (preg_match ('/^\[\[.+\]\]$/', $line) == 0) |
| 601 | { |
| 602 | if ($strip_optgroup) |
| 603 | return ereg_replace ('^.+%GSKIP%', '', ereg_replace ('^(.+)%GPASS%', '\\1 ', $line)); |
| 604 | else |
| 605 | return $line; |
| 606 | } |
| 607 | $line = preg_replace ('/^\[\[(.+)\]\]$/', '$1', $line); |
| 608 | $s = explode ('|', $line); |
| 609 | $o_value = trim ($s[0]); |
| 610 | if ($strip_optgroup) |
| 611 | $o_value = ereg_replace ('^.+%GSKIP%', '', ereg_replace ('^(.+)%GPASS%', '\\1 ', $o_value)); |
| 612 | $a_value = trim ($s[1]); |
| 613 | if ($which == 'a') |
| 614 | return "<a href='${a_value}'>${o_value}</a>"; |
| 615 | if ($which == 'o') |
| 616 | return $o_value; |
| 617 | } |
| 618 | |
| 619 | // FIXME: only renderIPv4Address() is using this function, consider |
| 620 | // phasing it out. |
| 621 | function buildVServiceName ($vsinfo = NULL) |
| 622 | { |
| 623 | if ($vsinfo == NULL) |
| 624 | { |
| 625 | showError ('NULL argument', __FUNCTION__); |
| 626 | return NULL; |
| 627 | } |
| 628 | return $vsinfo['vip'] . ':' . $vsinfo['vport'] . '/' . $vsinfo['proto']; |
| 629 | } |
| 630 | |
| 631 | // rackspace usage for a single rack |
| 632 | // (T + W + U) / (height * 3 - A) |
| 633 | function getRSUforRack ($data = NULL) |
| 634 | { |
| 635 | if ($data == NULL) |
| 636 | { |
| 637 | showError ('Invalid argument', __FUNCTION__); |
| 638 | return NULL; |
| 639 | } |
| 640 | $counter = array ('A' => 0, 'U' => 0, 'T' => 0, 'W' => 0, 'F' => 0); |
| 641 | for ($unit_no = $data['height']; $unit_no > 0; $unit_no--) |
| 642 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 643 | $counter[$data[$unit_no][$locidx]['state']]++; |
| 644 | return ($counter['T'] + $counter['W'] + $counter['U']) / ($counter['T'] + $counter['W'] + $counter['U'] + $counter['F']); |
| 645 | } |
| 646 | |
| 647 | // Same for row. |
| 648 | function getRSUforRackRow ($rowData = NULL) |
| 649 | { |
| 650 | if ($rowData === NULL) |
| 651 | { |
| 652 | showError ('Invalid argument', __FUNCTION__); |
| 653 | return NULL; |
| 654 | } |
| 655 | if (!count ($rowData)) |
| 656 | return 0; |
| 657 | $counter = array ('A' => 0, 'U' => 0, 'T' => 0, 'W' => 0, 'F' => 0); |
| 658 | $total_height = 0; |
| 659 | foreach (array_keys ($rowData) as $rack_id) |
| 660 | { |
| 661 | $data = spotEntity ('rack', $rack_id); |
| 662 | amplifyCell ($data); |
| 663 | $total_height += $data['height']; |
| 664 | for ($unit_no = $data['height']; $unit_no > 0; $unit_no--) |
| 665 | for ($locidx = 0; $locidx < 3; $locidx++) |
| 666 | $counter[$data[$unit_no][$locidx]['state']]++; |
| 667 | } |
| 668 | return ($counter['T'] + $counter['W'] + $counter['U']) / ($counter['T'] + $counter['W'] + $counter['U'] + $counter['F']); |
| 669 | } |
| 670 | |
| 671 | // Make sure the string is always wrapped with LF characters |
| 672 | function lf_wrap ($str) |
| 673 | { |
| 674 | $ret = trim ($str, "\r\n"); |
| 675 | if (!empty ($ret)) |
| 676 | $ret .= "\n"; |
| 677 | return $ret; |
| 678 | } |
| 679 | |
| 680 | // Adopted from Mantis BTS code. |
| 681 | function string_insert_hrefs ($s) |
| 682 | { |
| 683 | if (getConfigVar ('DETECT_URLS') != 'yes') |
| 684 | return $s; |
| 685 | # Find any URL in a string and replace it by a clickable link |
| 686 | $s = preg_replace( '/(([[:alpha:]][-+.[:alnum:]]*):\/\/(%[[:digit:]A-Fa-f]{2}|[-_.!~*\';\/?%^\\\\:@&={\|}+$#\(\),\[\][:alnum:]])+)/se', |
| 687 | "'<a href=\"'.rtrim('\\1','.').'\">\\1</a> [<a href=\"'.rtrim('\\1','.').'\" target=\"_blank\">^</a>]'", |
| 688 | $s); |
| 689 | $s = preg_replace( '/\b' . email_regex_simple() . '\b/i', |
| 690 | '<a href="mailto:\0">\0</a>', |
| 691 | $s); |
| 692 | return $s; |
| 693 | } |
| 694 | |
| 695 | // Idem. |
| 696 | function email_regex_simple () |
| 697 | { |
| 698 | return "(([a-z0-9!#*+\/=?^_{|}~-]+(?:\.[a-z0-9!#*+\/=?^_{|}~-]+)*)" . # recipient |
| 699 | "\@((?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?))"; # @domain |
| 700 | } |
| 701 | |
| 702 | // Parse AUTOPORTS_CONFIG and return a list of generated pairs (port_type, port_name) |
| 703 | // for the requested object_type_id. |
| 704 | function getAutoPorts ($type_id) |
| 705 | { |
| 706 | $ret = array(); |
| 707 | $typemap = explode (';', str_replace (' ', '', getConfigVar ('AUTOPORTS_CONFIG'))); |
| 708 | foreach ($typemap as $equation) |
| 709 | { |
| 710 | $tmp = explode ('=', $equation); |
| 711 | if (count ($tmp) != 2) |
| 712 | continue; |
| 713 | $objtype_id = $tmp[0]; |
| 714 | if ($objtype_id != $type_id) |
| 715 | continue; |
| 716 | $portlist = $tmp[1]; |
| 717 | foreach (explode ('+', $portlist) as $product) |
| 718 | { |
| 719 | $tmp = explode ('*', $product); |
| 720 | if (count ($tmp) != 3) |
| 721 | continue; |
| 722 | $nports = $tmp[0]; |
| 723 | $port_type = $tmp[1]; |
| 724 | $format = $tmp[2]; |
| 725 | for ($i = 0; $i < $nports; $i++) |
| 726 | $ret[] = array ('type' => $port_type, 'name' => @sprintf ($format, $i)); |
| 727 | } |
| 728 | } |
| 729 | return $ret; |
| 730 | } |
| 731 | |
| 732 | // Use pre-served trace to traverse the tree, then place given node where it belongs. |
| 733 | function pokeNode (&$tree, $trace, $key, $value, $threshold = 0) |
| 734 | { |
| 735 | // This function needs the trace to be followed FIFO-way. The fastest |
| 736 | // way to do so is to use array_push() for putting values into the |
| 737 | // list and array_shift() for getting them out. This exposed up to 11% |
| 738 | // performance gain compared to other patterns of array_push/array_unshift/ |
| 739 | // array_reverse/array_pop/array_shift conjunction. |
| 740 | $myid = array_shift ($trace); |
| 741 | if (!count ($trace)) // reached the target |
| 742 | { |
| 743 | if (!$threshold or ($threshold and $tree[$myid]['kidc'] + 1 < $threshold)) |
| 744 | $tree[$myid]['kids'][$key] = $value; |
| 745 | // Reset accumulated records once, when the limit is reached, not each time |
| 746 | // after that. |
| 747 | if (++$tree[$myid]['kidc'] == $threshold) |
| 748 | $tree[$myid]['kids'] = array(); |
| 749 | } |
| 750 | else // not yet |
| 751 | { |
| 752 | $self = __FUNCTION__; |
| 753 | $self ($tree[$myid]['kids'], $trace, $key, $value, $threshold); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | // Build a tree from the item list and return it. Input and output data is |
| 758 | // indexed by item id (nested items in output are recursively stored in 'kids' |
| 759 | // key, which is in turn indexed by id. Functions, which are ready to handle |
| 760 | // tree collapsion/expansion themselves, may request non-zero threshold value |
| 761 | // for smaller resulting tree. |
| 762 | function treeFromList ($nodelist, $threshold = 0, $return_main_payload = TRUE) |
| 763 | { |
| 764 | $tree = array(); |
| 765 | // Array equivalent of traceEntity() function. |
| 766 | $trace = array(); |
| 767 | // set kidc and kids only once |
| 768 | foreach (array_keys ($nodelist) as $nodeid) |
| 769 | { |
| 770 | $nodelist[$nodeid]['kidc'] = 0; |
| 771 | $nodelist[$nodeid]['kids'] = array(); |
| 772 | } |
| 773 | do |
| 774 | { |
| 775 | $nextpass = FALSE; |
| 776 | foreach (array_keys ($nodelist) as $nodeid) |
| 777 | { |
| 778 | // When adding a node to the working tree, book another |
| 779 | // iteration, because the new item could make a way for |
| 780 | // others onto the tree. Also remove any item added from |
| 781 | // the input list, so iteration base shrinks. |
| 782 | // First check if we can assign directly. |
| 783 | if ($nodelist[$nodeid]['parent_id'] == NULL) |
| 784 | { |
| 785 | $tree[$nodeid] = $nodelist[$nodeid]; |
| 786 | $trace[$nodeid] = array(); // Trace to root node is empty |
| 787 | unset ($nodelist[$nodeid]); |
| 788 | $nextpass = TRUE; |
| 789 | } |
| 790 | // Now look if it fits somewhere on already built tree. |
| 791 | elseif (isset ($trace[$nodelist[$nodeid]['parent_id']])) |
| 792 | { |
| 793 | // Trace to a node is a trace to its parent plus parent id. |
| 794 | $trace[$nodeid] = $trace[$nodelist[$nodeid]['parent_id']]; |
| 795 | $trace[$nodeid][] = $nodelist[$nodeid]['parent_id']; |
| 796 | pokeNode ($tree, $trace[$nodeid], $nodeid, $nodelist[$nodeid], $threshold); |
| 797 | // path to any other node is made of all parent nodes plus the added node itself |
| 798 | unset ($nodelist[$nodeid]); |
| 799 | $nextpass = TRUE; |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | while ($nextpass); |
| 804 | if ($return_main_payload) |
| 805 | return $tree; |
| 806 | else |
| 807 | return $nodelist; |
| 808 | } |
| 809 | |
| 810 | // Build a tree from the tag list and return everything _except_ the tree. |
| 811 | // IOW, return taginfo items, which have parent_id set and pointing outside |
| 812 | // of the "normal" tree, which originates from the root. |
| 813 | function getOrphanedTags () |
| 814 | { |
| 815 | global $taglist; |
| 816 | return treeFromList ($taglist, 0, FALSE); |
| 817 | } |
| 818 | |
| 819 | function serializeTags ($chain, $baseurl = '') |
| 820 | { |
| 821 | $comma = ''; |
| 822 | $ret = ''; |
| 823 | foreach ($chain as $taginfo) |
| 824 | { |
| 825 | $ret .= $comma . |
| 826 | ($baseurl == '' ? '' : "<a href='${baseurl}cft[]=${taginfo['id']}'>") . |
| 827 | $taginfo['tag'] . |
| 828 | ($baseurl == '' ? '' : '</a>'); |
| 829 | $comma = ', '; |
| 830 | } |
| 831 | return $ret; |
| 832 | } |
| 833 | |
| 834 | // For each tag add all its parent tags onto the list. Don't expect anything |
| 835 | // except user's tags on the chain. |
| 836 | function getTagChainExpansion ($chain, $tree = NULL) |
| 837 | { |
| 838 | $self = __FUNCTION__; |
| 839 | if ($tree === NULL) |
| 840 | { |
| 841 | global $tagtree; |
| 842 | $tree = $tagtree; |
| 843 | } |
| 844 | // For each tag find its path from the root, then combine items |
| 845 | // of all paths and add them to the chain, if they aren't there yet. |
| 846 | $ret = array(); |
| 847 | foreach ($tree as $taginfo1) |
| 848 | { |
| 849 | $hit = FALSE; |
| 850 | foreach ($chain as $taginfo2) |
| 851 | if ($taginfo1['id'] == $taginfo2['id']) |
| 852 | { |
| 853 | $hit = TRUE; |
| 854 | break; |
| 855 | } |
| 856 | if (count ($taginfo1['kids']) > 0) |
| 857 | { |
| 858 | $subsearch = $self ($chain, $taginfo1['kids']); |
| 859 | if (count ($subsearch)) |
| 860 | { |
| 861 | $hit = TRUE; |
| 862 | $ret = array_merge ($ret, $subsearch); |
| 863 | } |
| 864 | } |
| 865 | if ($hit) |
| 866 | $ret[] = $taginfo1; |
| 867 | } |
| 868 | return $ret; |
| 869 | } |
| 870 | |
| 871 | // Return the list of missing implicit tags. |
| 872 | function getImplicitTags ($oldtags) |
| 873 | { |
| 874 | $ret = array(); |
| 875 | $newtags = getTagChainExpansion ($oldtags); |
| 876 | foreach ($newtags as $newtag) |
| 877 | { |
| 878 | $already_exists = FALSE; |
| 879 | foreach ($oldtags as $oldtag) |
| 880 | if ($newtag['id'] == $oldtag['id']) |
| 881 | { |
| 882 | $already_exists = TRUE; |
| 883 | break; |
| 884 | } |
| 885 | if ($already_exists) |
| 886 | continue; |
| 887 | $ret[] = array ('id' => $newtag['id'], 'tag' => $newtag['tag'], 'parent_id' => $newtag['parent_id']); |
| 888 | } |
| 889 | return $ret; |
| 890 | } |
| 891 | |
| 892 | // Minimize the chain: exclude all implicit tags and return the result. |
| 893 | function getExplicitTagsOnly ($chain, $tree = NULL) |
| 894 | { |
| 895 | $self = __FUNCTION__; |
| 896 | global $tagtree; |
| 897 | if ($tree === NULL) |
| 898 | $tree = $tagtree; |
| 899 | $ret = array(); |
| 900 | foreach ($tree as $taginfo) |
| 901 | { |
| 902 | if (isset ($taginfo['kids'])) |
| 903 | { |
| 904 | $harvest = $self ($chain, $taginfo['kids']); |
| 905 | if (count ($harvest) > 0) |
| 906 | { |
| 907 | $ret = array_merge ($ret, $harvest); |
| 908 | continue; |
| 909 | } |
| 910 | } |
| 911 | // This tag isn't implicit, test is for being explicit. |
| 912 | foreach ($chain as $testtag) |
| 913 | if ($taginfo['id'] == $testtag['id']) |
| 914 | { |
| 915 | $ret[] = $testtag; |
| 916 | break; |
| 917 | } |
| 918 | } |
| 919 | return $ret; |
| 920 | } |
| 921 | |
| 922 | // Maximize the chain: for each tag add all tags, for which it is direct or indirect parent. |
| 923 | // Unlike other functions, this one accepts and returns a list of integer tag IDs, not |
| 924 | // a list of tag structures. Same structure (tag ID list) is returned after processing. |
| 925 | function complementByKids ($idlist, $tree = NULL, $getall = FALSE) |
| 926 | { |
| 927 | $self = __FUNCTION__; |
| 928 | global $tagtree; |
| 929 | if ($tree === NULL) |
| 930 | $tree = $tagtree; |
| 931 | $getallkids = $getall; |
| 932 | $ret = array(); |
| 933 | foreach ($tree as $taginfo) |
| 934 | { |
| 935 | foreach ($idlist as $test_id) |
| 936 | if ($getall or $taginfo['id'] == $test_id) |
| 937 | { |
| 938 | $ret[] = $taginfo['id']; |
| 939 | // Once matched node makes all sub-nodes match, but don't make |
| 940 | // a mistake of matching every other node at the current level. |
| 941 | $getallkids = TRUE; |
| 942 | break; |
| 943 | } |
| 944 | if (isset ($taginfo['kids'])) |
| 945 | $ret = array_merge ($ret, $self ($idlist, $taginfo['kids'], $getallkids)); |
| 946 | $getallkids = FALSE; |
| 947 | } |
| 948 | return $ret; |
| 949 | } |
| 950 | |
| 951 | // Universal autotags generator, a complementing function for loadEntityTags(). |
| 952 | // An important extension is that 'ipaddress' quasi-realm is also handled. |
| 953 | // Bypass key isn't strictly typed, but interpreted depending on the realm. |
| 954 | function generateEntityAutoTags ($entity_realm = '', $bypass_value = '') |
| 955 | { |
| 956 | $ret = array(); |
| 957 | switch ($entity_realm) |
| 958 | { |
| 959 | case 'rack': |
| 960 | $ret[] = array ('tag' => '$rackid_' . $bypass_value); |
| 961 | $ret[] = array ('tag' => '$any_rack'); |
| 962 | break; |
| 963 | case 'object': |
| 964 | $oinfo = getObjectInfo ($bypass_value, FALSE); |
| 965 | $ret[] = array ('tag' => '$id_' . $bypass_value); |
| 966 | $ret[] = array ('tag' => '$typeid_' . $oinfo['objtype_id']); |
| 967 | $ret[] = array ('tag' => '$any_object'); |
| 968 | if (validTagName ('$cn_' . $oinfo['name'])) |
| 969 | $ret[] = array ('tag' => '$cn_' . $oinfo['name']); |
| 970 | if (!count (getResidentRacksData ($bypass_value, FALSE))) |
| 971 | $ret[] = array ('tag' => '$unmounted'); |
| 972 | break; |
| 973 | case 'ipv4net': |
| 974 | $netinfo = getIPv4NetworkInfo ($bypass_value); |
| 975 | $ret[] = array ('tag' => '$ip4netid_' . $bypass_value); |
| 976 | $ret[] = array ('tag' => '$ip4net-' . str_replace ('.', '-', $netinfo['ip']) . '-' . $netinfo['mask']); |
| 977 | $ret[] = array ('tag' => '$any_ip4net'); |
| 978 | $ret[] = array ('tag' => '$any_net'); |
| 979 | break; |
| 980 | case 'ipaddress': |
| 981 | $netinfo = getIPv4NetworkInfo (getIPv4AddressNetworkId ($bypass_value)); |
| 982 | $ret[] = array ('tag' => '$ip4net-' . str_replace ('.', '-', $bypass_value) . '-32'); |
| 983 | $ret[] = array ('tag' => '$ip4net-' . str_replace ('.', '-', $netinfo['ip']) . '-' . $netinfo['mask']); |
| 984 | $ret[] = array ('tag' => '$any_ip4net'); |
| 985 | $ret[] = array ('tag' => '$any_net'); |
| 986 | break; |
| 987 | case 'ipv4vs': |
| 988 | $ret[] = array ('tag' => '$ipv4vsid_' . $bypass_value); |
| 989 | $ret[] = array ('tag' => '$any_ipv4vs'); |
| 990 | $ret[] = array ('tag' => '$any_vs'); |
| 991 | break; |
| 992 | case 'ipv4rspool': |
| 993 | $ret[] = array ('tag' => '$ipv4rspid_' . $bypass_value); |
| 994 | $ret[] = array ('tag' => '$any_ipv4rsp'); |
| 995 | $ret[] = array ('tag' => '$any_rsp'); |
| 996 | break; |
| 997 | case 'user': |
| 998 | // {$username_XXX} autotag is generated always, but {$userid_XXX} |
| 999 | // appears only for accounts, which exist in local database. |
| 1000 | $ret[] = array ('tag' => '$username_' . $bypass_value); |
| 1001 | if (NULL !== ($userid = getUserIDByUsername ($bypass_value))) |
| 1002 | $ret[] = array ('tag' => '$userid_' . $userid); |
| 1003 | break; |
| 1004 | case 'file': |
| 1005 | $ret[] = array ('tag' => '$fileid_' . $bypass_value); |
| 1006 | $ret[] = array ('tag' => '$any_file'); |
| 1007 | break; |
| 1008 | default: |
| 1009 | break; |
| 1010 | } |
| 1011 | return $ret; |
| 1012 | } |
| 1013 | |
| 1014 | // Check, if the given tag is present on the chain (will only work |
| 1015 | // for regular tags with tag ID set. |
| 1016 | function tagOnChain ($taginfo, $tagchain) |
| 1017 | { |
| 1018 | if (!isset ($taginfo['id'])) |
| 1019 | return FALSE; |
| 1020 | foreach ($tagchain as $test) |
| 1021 | if ($test['id'] == $taginfo['id']) |
| 1022 | return TRUE; |
| 1023 | return FALSE; |
| 1024 | } |
| 1025 | |
| 1026 | function tagNameOnChain ($tagname, $tagchain) |
| 1027 | { |
| 1028 | foreach ($tagchain as $test) |
| 1029 | if ($test['tag'] == $tagname) |
| 1030 | return TRUE; |
| 1031 | return FALSE; |
| 1032 | } |
| 1033 | |
| 1034 | // Idem, but use ID list instead of chain. |
| 1035 | function tagOnIdList ($taginfo, $tagidlist) |
| 1036 | { |
| 1037 | if (!isset ($taginfo['id'])) |
| 1038 | return FALSE; |
| 1039 | foreach ($tagidlist as $tagid) |
| 1040 | if ($taginfo['id'] == $tagid) |
| 1041 | return TRUE; |
| 1042 | return FALSE; |
| 1043 | } |
| 1044 | |
| 1045 | // Return TRUE, if two tags chains differ (order of tags doesn't matter). |
| 1046 | // Assume, that neither of the lists contains duplicates. |
| 1047 | // FIXME: a faster, than O(x^2) method is possible for this calculation. |
| 1048 | function tagChainCmp ($chain1, $chain2) |
| 1049 | { |
| 1050 | if (count ($chain1) != count ($chain2)) |
| 1051 | return TRUE; |
| 1052 | foreach ($chain1 as $taginfo1) |
| 1053 | if (!tagOnChain ($taginfo1, $chain2)) |
| 1054 | return TRUE; |
| 1055 | return FALSE; |
| 1056 | } |
| 1057 | |
| 1058 | // If the page-tab-op triplet is final, make $expl_tags and $impl_tags |
| 1059 | // hold all appropriate (explicit and implicit) tags respectively. |
| 1060 | // Otherwise some limited redirection is necessary (only page and tab |
| 1061 | // names are preserved, ophandler name change isn't handled). |
| 1062 | function fixContext () |
| 1063 | { |
| 1064 | global |
| 1065 | $pageno, |
| 1066 | $tabno, |
| 1067 | $auto_tags, |
| 1068 | $expl_tags, |
| 1069 | $impl_tags, |
| 1070 | $target_given_tags, |
| 1071 | $user_given_tags, |
| 1072 | $etype_by_pageno, |
| 1073 | $page; |
| 1074 | |
| 1075 | $pmap = array |
| 1076 | ( |
| 1077 | 'accounts' => 'userlist', |
| 1078 | 'rspools' => 'ipv4rsplist', |
| 1079 | 'rspool' => 'ipv4rsp', |
| 1080 | 'vservices' => 'ipv4vslist', |
| 1081 | 'vservice' => 'ipv4vs', |
| 1082 | 'objects' => 'depot', |
| 1083 | 'objgroup' => 'depot', |
| 1084 | ); |
| 1085 | $tmap = array(); |
| 1086 | $tmap['objects']['newmulti'] = 'addmore'; |
| 1087 | $tmap['objects']['newobj'] = 'addmore'; |
| 1088 | $tmap['object']['switchvlans'] = 'livevlans'; |
| 1089 | $tmap['object']['slb'] = 'editrspvs'; |
| 1090 | $tmap['object']['portfwrd'] = 'nat4'; |
| 1091 | $tmap['object']['network'] = 'ipv4'; |
| 1092 | if (isset ($pmap[$pageno])) |
| 1093 | redirectUser ($pmap[$pageno], $tabno); |
| 1094 | if (isset ($tmap[$pageno][$tabno])) |
| 1095 | redirectUser ($pageno, $tmap[$pageno][$tabno]); |
| 1096 | |
| 1097 | // Don't reset autochain, because auth procedures could push stuff there in. |
| 1098 | // Another important point is to ignore 'user' realm, so we don't infuse effective |
| 1099 | // context with autotags of the displayed account and don't try using uint |
| 1100 | // bypass, where string is expected. |
| 1101 | if |
| 1102 | ( |
| 1103 | $pageno != 'user' and |
| 1104 | isset ($etype_by_pageno[$pageno]) and |
| 1105 | isset ($page[$pageno]['bypass']) and |
| 1106 | isset ($_REQUEST[$page[$pageno]['bypass']]) |
| 1107 | ) |
| 1108 | $auto_tags = array_merge ($auto_tags, generateEntityAutoTags ($etype_by_pageno[$pageno], $_REQUEST[$page[$pageno]['bypass']])); |
| 1109 | if |
| 1110 | ( |
| 1111 | isset ($page[$pageno]['bypass']) and |
| 1112 | isset ($page[$pageno]['bypass_type']) and |
| 1113 | $page[$pageno]['bypass_type'] == 'uint' and |
| 1114 | isset ($_REQUEST[$page[$pageno]['bypass']]) |
| 1115 | ) |
| 1116 | $target_given_tags = loadEntityTags ($pageno, $_REQUEST[$page[$pageno]['bypass']]); |
| 1117 | // Explicit and implicit chains should be normally empty at this point, so |
| 1118 | // overwrite the contents anyway. |
| 1119 | $expl_tags = mergeTagChains ($user_given_tags, $target_given_tags); |
| 1120 | $impl_tags = getImplicitTags ($expl_tags); |
| 1121 | } |
| 1122 | |
| 1123 | // Take a list of user-supplied tag IDs to build a list of valid taginfo |
| 1124 | // records indexed by tag IDs (tag chain). |
| 1125 | function buildTagChainFromIds ($tagidlist) |
| 1126 | { |
| 1127 | global $taglist; |
| 1128 | $ret = array(); |
| 1129 | foreach (array_unique ($tagidlist) as $tag_id) |
| 1130 | if (isset ($taglist[$tag_id])) |
| 1131 | $ret[] = $taglist[$tag_id]; |
| 1132 | return $ret; |
| 1133 | } |
| 1134 | |
| 1135 | // Process a given tag tree and return only meaningful branches. The resulting |
| 1136 | // (sub)tree will have refcnt leaves on every last branch. |
| 1137 | function getObjectiveTagTree ($tree, $realm) |
| 1138 | { |
| 1139 | $self = __FUNCTION__; |
| 1140 | $ret = array(); |
| 1141 | foreach ($tree as $taginfo) |
| 1142 | { |
| 1143 | $subsearch = array(); |
| 1144 | $pick = FALSE; |
| 1145 | if (count ($taginfo['kids'])) |
| 1146 | { |
| 1147 | $subsearch = $self ($taginfo['kids'], $realm); |
| 1148 | $pick = count ($subsearch) > 0; |
| 1149 | } |
| 1150 | if (isset ($taginfo['refcnt'][$realm])) |
| 1151 | $pick = TRUE; |
| 1152 | if (!$pick) |
| 1153 | continue; |
| 1154 | $ret[] = array |
| 1155 | ( |
| 1156 | 'id' => $taginfo['id'], |
| 1157 | 'tag' => $taginfo['tag'], |
| 1158 | 'parent_id' => $taginfo['parent_id'], |
| 1159 | 'refcnt' => $taginfo['refcnt'], |
| 1160 | 'kids' => $subsearch |
| 1161 | ); |
| 1162 | } |
| 1163 | return $ret; |
| 1164 | } |
| 1165 | |
| 1166 | // Get taginfo record by tag name, return NULL, if record doesn't exist. |
| 1167 | function getTagByName ($target_name) |
| 1168 | { |
| 1169 | global $taglist; |
| 1170 | foreach ($taglist as $taginfo) |
| 1171 | if ($taginfo['tag'] == $target_name) |
| 1172 | return $taginfo; |
| 1173 | return NULL; |
| 1174 | } |
| 1175 | |
| 1176 | // Merge two chains, filtering dupes out. Return the resulting superset. |
| 1177 | function mergeTagChains ($chainA, $chainB) |
| 1178 | { |
| 1179 | // $ret = $chainA; |
| 1180 | // Reindex by tag id in any case. |
| 1181 | $ret = array(); |
| 1182 | foreach ($chainA as $tag) |
| 1183 | $ret[$tag['id']] = $tag; |
| 1184 | foreach ($chainB as $tag) |
| 1185 | if (!isset ($ret[$tag['id']])) |
| 1186 | $ret[$tag['id']] = $tag; |
| 1187 | return $ret; |
| 1188 | } |
| 1189 | |
| 1190 | function getCellFilter () |
| 1191 | { |
| 1192 | if (isset ($_REQUEST['tagfilter']) and is_array ($_REQUEST['tagfilter'])) |
| 1193 | { |
| 1194 | $_REQUEST['cft'] = $_REQUEST['tagfilter']; |
| 1195 | unset ($_REQUEST['tagfilter']); |
| 1196 | } |
| 1197 | $ret = array |
| 1198 | ( |
| 1199 | 'tagidlist' => array(), |
| 1200 | 'tnamelist' => array(), |
| 1201 | 'pnamelist' => array(), |
| 1202 | 'andor' => '', |
| 1203 | 'text' => '', |
| 1204 | 'extratext' => '', |
| 1205 | 'expression' => array(), |
| 1206 | 'urlextra' => '', // Just put text here and let makeHref call urlencode(). |
| 1207 | ); |
| 1208 | switch (TRUE) |
| 1209 | { |
| 1210 | case (!isset ($_REQUEST['andor'])): |
| 1211 | $andor2 = getConfigVar ('FILTER_DEFAULT_ANDOR'); |
| 1212 | break; |
| 1213 | case ($_REQUEST['andor'] == 'and'): |
| 1214 | case ($_REQUEST['andor'] == 'or'): |
| 1215 | $ret['andor'] = $andor2 = $_REQUEST['andor']; |
| 1216 | $ret['urlextra'] .= '&andor=' . $ret['andor']; |
| 1217 | break; |
| 1218 | default: |
| 1219 | showError ('Invalid and/or switch value in submitted form', __FUNCTION__); |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | $andor1 = ''; |
| 1223 | // Both tags and predicates, which don't exist, should be |
| 1224 | // handled somehow. Discard them silently for now. |
| 1225 | if (isset ($_REQUEST['cft']) and is_array ($_REQUEST['cft'])) |
| 1226 | { |
| 1227 | global $taglist; |
| 1228 | foreach ($_REQUEST['cft'] as $req_id) |
| 1229 | if (isset ($taglist[$req_id])) |
| 1230 | { |
| 1231 | $ret['tagidlist'][] = $req_id; |
| 1232 | $ret['tnamelist'][] = $taglist[$req_id]['tag']; |
| 1233 | $ret['text'] .= $andor1 . '{' . $taglist[$req_id]['tag'] . '}'; |
| 1234 | $andor1 = ' ' . $andor2 . ' '; |
| 1235 | $ret['urlextra'] .= '&cft[]=' . $req_id; |
| 1236 | } |
| 1237 | } |
| 1238 | if (isset ($_REQUEST['cfp']) and is_array ($_REQUEST['cfp'])) |
| 1239 | { |
| 1240 | global $pTable; |
| 1241 | foreach ($_REQUEST['cfp'] as $req_name) |
| 1242 | if (isset ($pTable[$req_name])) |
| 1243 | { |
| 1244 | $ret['pnamelist'][] = $req_name; |
| 1245 | $ret['text'] .= $andor1 . '[' . $req_name . ']'; |
| 1246 | $andor1 = ' ' . $andor2 . ' '; |
| 1247 | $ret['urlextra'] .= '&cfp[]=' . $req_name; |
| 1248 | } |
| 1249 | } |
| 1250 | if (isset ($_REQUEST['cfe'])) |
| 1251 | { |
| 1252 | $ret['extratext'] = trim ($_REQUEST['cfe']); |
| 1253 | $ret['urlextra'] .= '&cfe=' . $ret['extratext']; |
| 1254 | } |
| 1255 | $finaltext = array(); |
| 1256 | if (strlen ($ret['text'])) |
| 1257 | $finaltext[] = '(' . $ret['text'] . ')'; |
| 1258 | if (strlen ($ret['extratext'])) |
| 1259 | $finaltext[] = '(' . $ret['extratext'] . ')'; |
| 1260 | $finaltext = implode (' ' . $andor2 . ' ', $finaltext); |
| 1261 | if (strlen ($finaltext)) |
| 1262 | { |
| 1263 | $parse = spotPayload ($finaltext, 'SYNT_EXPR'); |
| 1264 | $ret['expression'] = $parse['result'] == 'ACK' ? $parse['load'] : NULL; |
| 1265 | // It's not quite fair enough to put the blame of the whole text onto |
| 1266 | // non-empty "extra" portion of it, but it's the only user-generated portion |
| 1267 | // of it, thus the most probable cause of parse error. |
| 1268 | if (strlen ($ret['extratext'])) |
| 1269 | $ret['extraclass'] = $parse['result'] == 'ACK' ? 'validation-success' : 'validation-error'; |
| 1270 | } |
| 1271 | return $ret; |
| 1272 | } |
| 1273 | |
| 1274 | // Return an empty message log. |
| 1275 | function emptyLog () |
| 1276 | { |
| 1277 | return array |
| 1278 | ( |
| 1279 | 'v' => 2, |
| 1280 | 'm' => array() |
| 1281 | ); |
| 1282 | } |
| 1283 | |
| 1284 | // Return a message log consisting of only one message. |
| 1285 | function oneLiner ($code, $args = array()) |
| 1286 | { |
| 1287 | $ret = emptyLog(); |
| 1288 | $ret['m'][] = count ($args) ? array ('c' => $code, 'a' => $args) : array ('c' => $code); |
| 1289 | return $ret; |
| 1290 | } |
| 1291 | |
| 1292 | // Merge message payload from two message logs given and return the result. |
| 1293 | function mergeLogs ($log1, $log2) |
| 1294 | { |
| 1295 | $ret = emptyLog(); |
| 1296 | $ret['m'] = array_merge ($log1['m'], $log2['m']); |
| 1297 | return $ret; |
| 1298 | } |
| 1299 | |
| 1300 | function validTagName ($s, $allow_autotag = FALSE) |
| 1301 | { |
| 1302 | if (1 == mb_ereg (TAGNAME_REGEXP, $s)) |
| 1303 | return TRUE; |
| 1304 | if ($allow_autotag and 1 == mb_ereg (AUTOTAGNAME_REGEXP, $s)) |
| 1305 | return TRUE; |
| 1306 | return FALSE; |
| 1307 | } |
| 1308 | |
| 1309 | function redirectUser ($p, $t) |
| 1310 | { |
| 1311 | global $page, $root; |
| 1312 | $l = "{$root}?page=${p}&tab=${t}"; |
| 1313 | if (isset ($page[$p]['bypass']) and isset ($_REQUEST[$page[$p]['bypass']])) |
| 1314 | $l .= '&' . $page[$p]['bypass'] . '=' . $_REQUEST[$page[$p]['bypass']]; |
| 1315 | header ("Location: " . $l); |
| 1316 | die; |
| 1317 | } |
| 1318 | |
| 1319 | function getRackCodeStats () |
| 1320 | { |
| 1321 | global $rackCode; |
| 1322 | $defc = $grantc = $modc = 0; |
| 1323 | foreach ($rackCode as $s) |
| 1324 | switch ($s['type']) |
| 1325 | { |
| 1326 | case 'SYNT_DEFINITION': |
| 1327 | $defc++; |
| 1328 | break; |
| 1329 | case 'SYNT_GRANT': |
| 1330 | $grantc++; |
| 1331 | break; |
| 1332 | case 'SYNT_CTXMOD': |
| 1333 | $modc++; |
| 1334 | break; |
| 1335 | default: |
| 1336 | break; |
| 1337 | } |
| 1338 | $ret = array |
| 1339 | ( |
| 1340 | 'Definition sentences' => $defc, |
| 1341 | 'Grant sentences' => $grantc, |
| 1342 | 'Context mod sentences' => $modc |
| 1343 | ); |
| 1344 | return $ret; |
| 1345 | } |
| 1346 | |
| 1347 | function getRackImageWidth () |
| 1348 | { |
| 1349 | global $rtwidth; |
| 1350 | return 3 + $rtwidth[0] + $rtwidth[1] + $rtwidth[2] + 3; |
| 1351 | } |
| 1352 | |
| 1353 | function getRackImageHeight ($units) |
| 1354 | { |
| 1355 | return 3 + 3 + $units * 2; |
| 1356 | } |
| 1357 | |
| 1358 | // Perform substitutions and return resulting string |
| 1359 | // used solely by buildLVSConfig() |
| 1360 | function apply_macros ($macros, $subject) |
| 1361 | { |
| 1362 | $ret = $subject; |
| 1363 | foreach ($macros as $search => $replace) |
| 1364 | $ret = str_replace ($search, $replace, $ret); |
| 1365 | return $ret; |
| 1366 | } |
| 1367 | |
| 1368 | function buildLVSConfig ($object_id = 0) |
| 1369 | { |
| 1370 | if ($object_id <= 0) |
| 1371 | { |
| 1372 | showError ('Invalid argument', __FUNCTION__); |
| 1373 | return; |
| 1374 | } |
| 1375 | $oInfo = getObjectInfo ($object_id, FALSE); |
| 1376 | $lbconfig = getSLBConfig ($object_id); |
| 1377 | if ($lbconfig === NULL) |
| 1378 | { |
| 1379 | showError ('getSLBConfig() failed', __FUNCTION__); |
| 1380 | return; |
| 1381 | } |
| 1382 | $newconfig = "#\n#\n# This configuration has been generated automatically by RackTables\n"; |
| 1383 | $newconfig .= "# for object_id == ${object_id}\n# object name: ${oInfo['name']}\n#\n#\n\n\n"; |
| 1384 | foreach ($lbconfig as $vs_id => $vsinfo) |
| 1385 | { |
| 1386 | $newconfig .= "########################################################\n" . |
| 1387 | "# VS (id == ${vs_id}): " . (empty ($vsinfo['vs_name']) ? 'NO NAME' : $vsinfo['vs_name']) . "\n" . |
| 1388 | "# RS pool (id == ${vsinfo['pool_id']}): " . (empty ($vsinfo['pool_name']) ? 'ANONYMOUS' : $vsinfo['pool_name']) . "\n" . |
| 1389 | "########################################################\n"; |
| 1390 | # The order of inheritance is: VS -> LB -> pool [ -> RS ] |
| 1391 | $macros = array |
| 1392 | ( |
| 1393 | '%VIP%' => $vsinfo['vip'], |
| 1394 | '%VPORT%' => $vsinfo['vport'], |
| 1395 | '%PROTO%' => $vsinfo['proto'], |
| 1396 | '%VNAME%' => $vsinfo['vs_name'], |
| 1397 | '%RSPOOLNAME%' => $vsinfo['pool_name'] |
| 1398 | ); |
| 1399 | $newconfig .= "virtual_server ${vsinfo['vip']} ${vsinfo['vport']} {\n"; |
| 1400 | $newconfig .= "\tprotocol ${vsinfo['proto']}\n"; |
| 1401 | $newconfig .= apply_macros |
| 1402 | ( |
| 1403 | $macros, |
| 1404 | lf_wrap ($vsinfo['vs_vsconfig']) . |
| 1405 | lf_wrap ($vsinfo['lb_vsconfig']) . |
| 1406 | lf_wrap ($vsinfo['pool_vsconfig']) |
| 1407 | ); |
| 1408 | foreach ($vsinfo['rslist'] as $rs) |
| 1409 | { |
| 1410 | if (empty ($rs['rsport'])) |
| 1411 | $rs['rsport'] = $vsinfo['vport']; |
| 1412 | $macros['%RSIP%'] = $rs['rsip']; |
| 1413 | $macros['%RSPORT%'] = $rs['rsport']; |
| 1414 | $newconfig .= "\treal_server ${rs['rsip']} ${rs['rsport']} {\n"; |
| 1415 | $newconfig .= apply_macros |
| 1416 | ( |
| 1417 | $macros, |
| 1418 | lf_wrap ($vsinfo['vs_rsconfig']) . |
| 1419 | lf_wrap ($vsinfo['lb_rsconfig']) . |
| 1420 | lf_wrap ($vsinfo['pool_rsconfig']) . |
| 1421 | lf_wrap ($rs['rs_rsconfig']) |
| 1422 | ); |
| 1423 | $newconfig .= "\t}\n"; |
| 1424 | } |
| 1425 | $newconfig .= "}\n\n\n"; |
| 1426 | } |
| 1427 | // FIXME: deal somehow with Mac-styled text, the below replacement will screw it up |
| 1428 | return str_replace ("\r", '', $newconfig); |
| 1429 | } |
| 1430 | |
| 1431 | // Indicate occupation state of each IP address: none, ordinary or problematic. |
| 1432 | function markupIPv4AddrList (&$addrlist) |
| 1433 | { |
| 1434 | foreach (array_keys ($addrlist) as $ip_bin) |
| 1435 | { |
| 1436 | $refc = array |
| 1437 | ( |
| 1438 | 'shared' => 0, // virtual |
| 1439 | 'virtual' => 0, // loopback |
| 1440 | 'regular' => 0, // connected host |
| 1441 | 'router' => 0 // connected gateway |
| 1442 | ); |
| 1443 | foreach ($addrlist[$ip_bin]['allocs'] as $a) |
| 1444 | $refc[$a['type']]++; |
| 1445 | $nvirtloopback = ($refc['shared'] + $refc['virtual'] > 0) ? 1 : 0; // modulus of virtual + shared |
| 1446 | $nreserved = ($addrlist[$ip_bin]['reserved'] == 'yes') ? 1 : 0; // only one reservation is possible ever |
| 1447 | $nrealms = $nreserved + $nvirtloopback + $refc['regular'] + $refc['router']; // latter two are connected and router allocations |
| 1448 | |
| 1449 | if ($nrealms == 1) |
| 1450 | $addrlist[$ip_bin]['class'] = 'trbusy'; |
| 1451 | elseif ($nrealms > 1) |
| 1452 | $addrlist[$ip_bin]['class'] = 'trerror'; |
| 1453 | else |
| 1454 | $addrlist[$ip_bin]['class'] = ''; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | // Scan the given address list (returned by scanIPv4Space) and return a list of all routers found. |
| 1459 | function findRouters ($addrlist) |
| 1460 | { |
| 1461 | $ret = array(); |
| 1462 | foreach ($addrlist as $addr) |
| 1463 | foreach ($addr['allocs'] as $alloc) |
| 1464 | if ($alloc['type'] == 'router') |
| 1465 | $ret[] = array |
| 1466 | ( |
| 1467 | 'id' => $alloc['object_id'], |
| 1468 | 'iface' => $alloc['name'], |
| 1469 | 'dname' => $alloc['object_name'], |
| 1470 | 'addr' => $addr['ip'] |
| 1471 | ); |
| 1472 | return $ret; |
| 1473 | } |
| 1474 | |
| 1475 | // Assist in tag chain sorting. |
| 1476 | function taginfoCmp ($tagA, $tagB) |
| 1477 | { |
| 1478 | return $tagA['ci'] - $tagB['ci']; |
| 1479 | } |
| 1480 | |
| 1481 | // Compare networks. When sorting a tree, the records on the list will have |
| 1482 | // distinct base IP addresses. |
| 1483 | // "The comparison function must return an integer less than, equal to, or greater |
| 1484 | // than zero if the first argument is considered to be respectively less than, |
| 1485 | // equal to, or greater than the second." (c) PHP manual |
| 1486 | function IPv4NetworkCmp ($netA, $netB) |
| 1487 | { |
| 1488 | // There's a problem just substracting one u32 integer from another, |
| 1489 | // because the result may happen big enough to become a negative i32 |
| 1490 | // integer itself (PHP tries to cast everything it sees to signed int) |
| 1491 | // The comparison below must treat positive and negative values of both |
| 1492 | // arguments. |
| 1493 | // Equal values give instant decision regardless of their [equal] sign. |
| 1494 | if ($netA['ip_bin'] == $netB['ip_bin']) |
| 1495 | return 0; |
| 1496 | // Same-signed values compete arithmetically within one of i32 contiguous ranges: |
| 1497 | // 0x00000001~0x7fffffff 1~2147483647 |
| 1498 | // 0 doesn't have any sign, and network 0.0.0.0 isn't allowed |
| 1499 | // 0x80000000~0xffffffff -2147483648~-1 |
| 1500 | $signA = $netA['ip_bin'] / abs ($netA['ip_bin']); |
| 1501 | $signB = $netB['ip_bin'] / abs ($netB['ip_bin']); |
| 1502 | if ($signA == $signB) |
| 1503 | { |
| 1504 | if ($netA['ip_bin'] > $netB['ip_bin']) |
| 1505 | return 1; |
| 1506 | else |
| 1507 | return -1; |
| 1508 | } |
| 1509 | else // With only one of two values being negative, it... wins! |
| 1510 | { |
| 1511 | if ($netA['ip_bin'] < $netB['ip_bin']) |
| 1512 | return 1; |
| 1513 | else |
| 1514 | return -1; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | // Modify the given tag tree so, that each level's items are sorted alphabetically. |
| 1519 | function sortTree (&$tree, $sortfunc = '') |
| 1520 | { |
| 1521 | if (empty ($sortfunc)) |
| 1522 | return; |
| 1523 | $self = __FUNCTION__; |
| 1524 | usort ($tree, $sortfunc); |
| 1525 | // Don't make a mistake of directly iterating over the items of current level, because this way |
| 1526 | // the sorting will be performed on a _copy_ if each item, not the item itself. |
| 1527 | foreach (array_keys ($tree) as $tagid) |
| 1528 | $self ($tree[$tagid]['kids'], $sortfunc); |
| 1529 | } |
| 1530 | |
| 1531 | function iptree_fill (&$netdata) |
| 1532 | { |
| 1533 | if (!isset ($netdata['kids']) or empty ($netdata['kids'])) |
| 1534 | return; |
| 1535 | // If we really have nested prefixes, they must fit into the tree. |
| 1536 | $worktree = array |
| 1537 | ( |
| 1538 | 'ip_bin' => $netdata['ip_bin'], |
| 1539 | 'mask' => $netdata['mask'] |
| 1540 | ); |
| 1541 | foreach ($netdata['kids'] as $pfx) |
| 1542 | iptree_embed ($worktree, $pfx); |
| 1543 | $netdata['kids'] = iptree_construct ($worktree); |
| 1544 | $netdata['kidc'] = count ($netdata['kids']); |
| 1545 | } |
| 1546 | |
| 1547 | function iptree_construct ($node) |
| 1548 | { |
| 1549 | $self = __FUNCTION__; |
| 1550 | |
| 1551 | if (!isset ($node['right'])) |
| 1552 | { |
| 1553 | if (!isset ($node['ip'])) |
| 1554 | { |
| 1555 | $node['ip'] = long2ip ($node['ip_bin']); |
| 1556 | $node['kids'] = array(); |
| 1557 | $node['kidc'] = 0; |
| 1558 | $node['name'] = ''; |
| 1559 | } |
| 1560 | return array ($node); |
| 1561 | } |
| 1562 | else |
| 1563 | return array_merge ($self ($node['left']), $self ($node['right'])); |
| 1564 | } |
| 1565 | |
| 1566 | function iptree_embed (&$node, $pfx) |
| 1567 | { |
| 1568 | $self = __FUNCTION__; |
| 1569 | |
| 1570 | // hit? |
| 1571 | if ($node['ip_bin'] == $pfx['ip_bin'] and $node['mask'] == $pfx['mask']) |
| 1572 | { |
| 1573 | $node = $pfx; |
| 1574 | return; |
| 1575 | } |
| 1576 | if ($node['mask'] == $pfx['mask']) |
| 1577 | { |
| 1578 | showError ('Internal error, the recurring loop lost control', __FUNCTION__); |
| 1579 | die; |
| 1580 | } |
| 1581 | |
| 1582 | // split? |
| 1583 | if (!isset ($node['right'])) |
| 1584 | { |
| 1585 | // Fill in db_first/db_last to make it possible to run scanIPv4Space() on the node. |
| 1586 | $node['left']['mask'] = $node['mask'] + 1; |
| 1587 | $node['left']['ip_bin'] = $node['ip_bin']; |
| 1588 | $node['left']['db_first'] = sprintf ('%u', $node['left']['ip_bin']); |
| 1589 | $node['left']['db_last'] = sprintf ('%u', $node['left']['ip_bin'] | binInvMaskFromDec ($node['left']['mask'])); |
| 1590 | |
| 1591 | $node['right']['mask'] = $node['mask'] + 1; |
| 1592 | $node['right']['ip_bin'] = $node['ip_bin'] + binInvMaskFromDec ($node['mask'] + 1) + 1; |
| 1593 | $node['right']['db_first'] = sprintf ('%u', $node['right']['ip_bin']); |
| 1594 | $node['right']['db_last'] = sprintf ('%u', $node['right']['ip_bin'] | binInvMaskFromDec ($node['right']['mask'])); |
| 1595 | } |
| 1596 | |
| 1597 | // repeat! |
| 1598 | if (($node['left']['ip_bin'] & binMaskFromDec ($node['left']['mask'])) == ($pfx['ip_bin'] & binMaskFromDec ($node['left']['mask']))) |
| 1599 | $self ($node['left'], $pfx); |
| 1600 | elseif (($node['right']['ip_bin'] & binMaskFromDec ($node['right']['mask'])) == ($pfx['ip_bin'] & binMaskFromDec ($node['left']['mask']))) |
| 1601 | $self ($node['right'], $pfx); |
| 1602 | else |
| 1603 | { |
| 1604 | showError ('Internal error, cannot decide between left and right', __FUNCTION__); |
| 1605 | die; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | function treeApplyFunc (&$tree, $func = '', $stopfunc = '') |
| 1610 | { |
| 1611 | if (empty ($func)) |
| 1612 | return; |
| 1613 | $self = __FUNCTION__; |
| 1614 | foreach (array_keys ($tree) as $key) |
| 1615 | { |
| 1616 | $func ($tree[$key]); |
| 1617 | if (!empty ($stopfunc) and $stopfunc ($tree[$key])) |
| 1618 | continue; |
| 1619 | $self ($tree[$key]['kids'], $func); |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | function loadIPv4AddrList (&$netinfo) |
| 1624 | { |
| 1625 | loadOwnIPv4Addresses ($netinfo); |
| 1626 | markupIPv4AddrList ($netinfo['addrlist']); |
| 1627 | } |
| 1628 | |
| 1629 | function countOwnIPv4Addresses (&$node) |
| 1630 | { |
| 1631 | $toscan = array(); |
| 1632 | $node['addrt'] = 0; |
| 1633 | $node['mask_bin'] = binMaskFromDec ($node['mask']); |
| 1634 | $node['mask_bin_inv'] = binInvMaskFromDec ($node['mask']); |
| 1635 | $node['db_first'] = sprintf ('%u', 0x00000000 + $node['ip_bin'] & $node['mask_bin']); |
| 1636 | $node['db_last'] = sprintf ('%u', 0x00000000 + $node['ip_bin'] | ($node['mask_bin_inv'])); |
| 1637 | if (empty ($node['kids'])) |
| 1638 | { |
| 1639 | $toscan[] = array ('i32_first' => $node['db_first'], 'i32_last' => $node['db_last']); |
| 1640 | $node['addrt'] = binInvMaskFromDec ($node['mask']) + 1; |
| 1641 | } |
| 1642 | else |
| 1643 | foreach ($node['kids'] as $nested) |
| 1644 | if (!isset ($nested['id'])) // spare |
| 1645 | { |
| 1646 | $toscan[] = array ('i32_first' => $nested['db_first'], 'i32_last' => $nested['db_last']); |
| 1647 | $node['addrt'] += binInvMaskFromDec ($nested['mask']) + 1; |
| 1648 | } |
| 1649 | // Don't do anything more, because the displaying function will load the addresses anyway. |
| 1650 | return; |
| 1651 | $node['addrc'] = count (scanIPv4Space ($toscan)); |
| 1652 | } |
| 1653 | |
| 1654 | function nodeIsCollapsed ($node) |
| 1655 | { |
| 1656 | return $node['symbol'] == 'node-collapsed'; |
| 1657 | } |
| 1658 | |
| 1659 | function loadOwnIPv4Addresses (&$node) |
| 1660 | { |
| 1661 | $toscan = array(); |
| 1662 | if (empty ($node['kids'])) |
| 1663 | $toscan[] = array ('i32_first' => $node['db_first'], 'i32_last' => $node['db_last']); |
| 1664 | else |
| 1665 | foreach ($node['kids'] as $nested) |
| 1666 | if (!isset ($nested['id'])) // spare |
| 1667 | $toscan[] = array ('i32_first' => $nested['db_first'], 'i32_last' => $nested['db_last']); |
| 1668 | $node['addrlist'] = scanIPv4Space ($toscan); |
| 1669 | $node['addrc'] = count ($node['addrlist']); |
| 1670 | } |
| 1671 | |
| 1672 | function prepareIPv4Tree ($netlist, $expanded_id = 0) |
| 1673 | { |
| 1674 | // treeFromList() requires parent_id to be correct for an item to get onto the tree, |
| 1675 | // so perform necessary pre-processing to make orphans belong to root. This trick |
| 1676 | // was earlier performed by getIPv4NetworkList(). |
| 1677 | $netids = array_keys ($netlist); |
| 1678 | foreach ($netids as $cid) |
| 1679 | if (!in_array ($netlist[$cid]['parent_id'], $netids)) |
| 1680 | $netlist[$cid]['parent_id'] = NULL; |
| 1681 | $tree = treeFromList ($netlist); // medium call |
| 1682 | sortTree ($tree, 'IPv4NetworkCmp'); |
| 1683 | // complement the tree before markup to make the spare networks have "symbol" set |
| 1684 | treeApplyFunc ($tree, 'iptree_fill'); |
| 1685 | iptree_markup_collapsion ($tree, getConfigVar ('TREE_THRESHOLD'), $expanded_id); |
| 1686 | // count addresses after the markup to skip computation for hidden tree nodes |
| 1687 | treeApplyFunc ($tree, 'countOwnIPv4Addresses', 'nodeIsCollapsed'); |
| 1688 | return $tree; |
| 1689 | } |
| 1690 | |
| 1691 | // Check all items of the tree recursively, until the requested target id is |
| 1692 | // found. Mark all items leading to this item as "expanded", collapsing all |
| 1693 | // the rest, which exceed the given threshold (if the threshold is given). |
| 1694 | function iptree_markup_collapsion (&$tree, $threshold = 1024, $target = 0) |
| 1695 | { |
| 1696 | $self = __FUNCTION__; |
| 1697 | $ret = FALSE; |
| 1698 | foreach (array_keys ($tree) as $key) |
| 1699 | { |
| 1700 | $here = ($target === 'ALL' or ($target > 0 and isset ($tree[$key]['id']) and $tree[$key]['id'] == $target)); |
| 1701 | $below = $self ($tree[$key]['kids'], $threshold, $target); |
| 1702 | if (!$tree[$key]['kidc']) // terminal node |
| 1703 | $tree[$key]['symbol'] = 'spacer'; |
| 1704 | elseif ($tree[$key]['kidc'] < $threshold) |
| 1705 | $tree[$key]['symbol'] = 'node-expanded-static'; |
| 1706 | elseif ($here or $below) |
| 1707 | $tree[$key]['symbol'] = 'node-expanded'; |
| 1708 | else |
| 1709 | $tree[$key]['symbol'] = 'node-collapsed'; |
| 1710 | $ret = ($ret or $here or $below); // parentheses are necessary for this to be computed correctly |
| 1711 | } |
| 1712 | return $ret; |
| 1713 | } |
| 1714 | |
| 1715 | // Convert entity name to human-readable value |
| 1716 | function formatEntityName ($name) { |
| 1717 | switch ($name) |
| 1718 | { |
| 1719 | case 'ipv4net': |
| 1720 | return 'IPv4 Network'; |
| 1721 | case 'ipv4rspool': |
| 1722 | return 'IPv4 RS Pool'; |
| 1723 | case 'ipv4vs': |
| 1724 | return 'IPv4 Virtual Service'; |
| 1725 | case 'object': |
| 1726 | return 'Object'; |
| 1727 | case 'rack': |
| 1728 | return 'Rack'; |
| 1729 | case 'user': |
| 1730 | return 'User'; |
| 1731 | } |
| 1732 | return 'invalid'; |
| 1733 | } |
| 1734 | |
| 1735 | // Take a MySQL or other generic timestamp and make it prettier |
| 1736 | function formatTimestamp ($timestamp) { |
| 1737 | return date('n/j/y g:iA', strtotime($timestamp)); |
| 1738 | } |
| 1739 | |
| 1740 | // Display hrefs for all of a file's parents. If scissors are requested, |
| 1741 | // prepend cutting button to each of them. |
| 1742 | function serializeFileLinks ($links, $scissors = FALSE) |
| 1743 | { |
| 1744 | global $root; |
| 1745 | |
| 1746 | $comma = ''; |
| 1747 | $ret = ''; |
| 1748 | foreach ($links as $link_id => $li) |
| 1749 | { |
| 1750 | switch ($li['entity_type']) |
| 1751 | { |
| 1752 | case 'ipv4net': |
| 1753 | $params = "page=ipv4net&id="; |
| 1754 | break; |
| 1755 | case 'ipv4rspool': |
| 1756 | $params = "page=ipv4rspool&pool_id="; |
| 1757 | break; |
| 1758 | case 'ipv4vs': |
| 1759 | $params = "page=ipv4vs&vs_id="; |
| 1760 | break; |
| 1761 | case 'object': |
| 1762 | $params = "page=object&object_id="; |
| 1763 | break; |
| 1764 | case 'rack': |
| 1765 | $params = "page=rack&rack_id="; |
| 1766 | break; |
| 1767 | case 'user': |
| 1768 | $params = "page=user&user_id="; |
| 1769 | break; |
| 1770 | } |
| 1771 | $ret .= $comma; |
| 1772 | if ($scissors) |
| 1773 | { |
| 1774 | $ret .= "<a href='" . makeHrefProcess(array('op'=>'unlinkFile', 'link_id'=>$link_id)) . "'"; |
| 1775 | $ret .= getImageHREF ('cut') . '</a> '; |
| 1776 | } |
| 1777 | $ret .= sprintf("<a href='%s?%s%s'>%s</a>", $root, $params, $li['entity_id'], $li['name']); |
| 1778 | $comma = '<br>'; |
| 1779 | } |
| 1780 | return $ret; |
| 1781 | } |
| 1782 | |
| 1783 | // Convert filesize to appropriate unit and make it human-readable |
| 1784 | function formatFileSize ($bytes) { |
| 1785 | // bytes |
| 1786 | if($bytes < 1024) // bytes |
| 1787 | return "${bytes} bytes"; |
| 1788 | |
| 1789 | // kilobytes |
| 1790 | if ($bytes < 1024000) |
| 1791 | return sprintf ("%.1fk", round (($bytes / 1024), 1)); |
| 1792 | |
| 1793 | // megabytes |
| 1794 | return sprintf ("%.1f MB", round (($bytes / 1024000), 1)); |
| 1795 | } |
| 1796 | |
| 1797 | // Reverse of formatFileSize, it converts human-readable value to bytes |
| 1798 | function convertToBytes ($value) { |
| 1799 | $value = trim($value); |
| 1800 | $last = strtolower($value[strlen($value)-1]); |
| 1801 | switch ($last) |
| 1802 | { |
| 1803 | case 'g': |
| 1804 | $value *= 1024; |
| 1805 | case 'm': |
| 1806 | $value *= 1024; |
| 1807 | case 'k': |
| 1808 | $value *= 1024; |
| 1809 | } |
| 1810 | |
| 1811 | return $value; |
| 1812 | } |
| 1813 | |
| 1814 | function ip_quad2long ($ip) |
| 1815 | { |
| 1816 | return sprintf("%u", ip2long($ip)); |
| 1817 | } |
| 1818 | |
| 1819 | function ip_long2quad ($quad) |
| 1820 | { |
| 1821 | return long2ip($quad); |
| 1822 | } |
| 1823 | |
| 1824 | function makeHref($params = array()) |
| 1825 | { |
| 1826 | global $head_revision, $numeric_revision, $root; |
| 1827 | $ret = $root.'?'; |
| 1828 | $first = true; |
| 1829 | if (!isset($params['r']) and ($numeric_revision != $head_revision)) |
| 1830 | { |
| 1831 | $params['r'] = $numeric_revision; |
| 1832 | } |
| 1833 | foreach($params as $key=>$value) |
| 1834 | { |
| 1835 | if (!$first) |
| 1836 | $ret.='&'; |
| 1837 | $ret .= urlencode($key).'='.urlencode($value); |
| 1838 | $first = false; |
| 1839 | } |
| 1840 | return $ret; |
| 1841 | } |
| 1842 | |
| 1843 | function makeHrefProcess($params = array()) |
| 1844 | { |
| 1845 | global $head_revision, $numeric_revision, $root, $pageno, $tabno; |
| 1846 | $ret = $root.'process.php'.'?'; |
| 1847 | $first = true; |
| 1848 | if ($numeric_revision != $head_revision) |
| 1849 | { |
| 1850 | error_log("Can't make a process link when not in head revision"); |
| 1851 | die(); |
| 1852 | } |
| 1853 | if (!isset($params['page'])) |
| 1854 | $params['page'] = $pageno; |
| 1855 | if (!isset($params['tab'])) |
| 1856 | $params['tab'] = $tabno; |
| 1857 | foreach($params as $key=>$value) |
| 1858 | { |
| 1859 | if (!$first) |
| 1860 | $ret.='&'; |
| 1861 | $ret .= urlencode($key).'='.urlencode($value); |
| 1862 | $first = false; |
| 1863 | } |
| 1864 | return $ret; |
| 1865 | } |
| 1866 | |
| 1867 | function makeHrefForHelper ($helper_name, $params = array()) |
| 1868 | { |
| 1869 | global $head_revision, $numeric_revision, $root; |
| 1870 | $ret = $root.'popup.php'.'?helper='.$helper_name; |
| 1871 | if ($numeric_revision != $head_revision) |
| 1872 | { |
| 1873 | error_log("Can't make a process link when not in head revision"); |
| 1874 | die(); |
| 1875 | } |
| 1876 | foreach($params as $key=>$value) |
| 1877 | $ret .= '&'.urlencode($key).'='.urlencode($value); |
| 1878 | return $ret; |
| 1879 | } |
| 1880 | |
| 1881 | // Process the given list of records to build data suitable for printNiftySelect() |
| 1882 | // (like it was formerly executed by printSelect()). Screen out vendors according |
| 1883 | // to VENDOR_SIEVE, if object type ID is provided. However, the OPTGROUP with already |
| 1884 | // selected OPTION is protected from being screened. |
| 1885 | function cookOptgroups ($recordList, $object_type_id = 0, $existing_value = 0) |
| 1886 | { |
| 1887 | $ret = array(); |
| 1888 | // Always keep "other" OPTGROUP at the SELECT bottom. |
| 1889 | $therest = array(); |
| 1890 | foreach ($recordList as $dict_key => $dict_value) |
| 1891 | if (strpos ($dict_value, '%GSKIP%') !== FALSE) |
| 1892 | { |
| 1893 | $tmp = explode ('%GSKIP%', $dict_value, 2); |
| 1894 | $ret[$tmp[0]][$dict_key] = $tmp[1]; |
| 1895 | } |
| 1896 | elseif (strpos ($dict_value, '%GPASS%') !== FALSE) |
| 1897 | { |
| 1898 | $tmp = explode ('%GPASS%', $dict_value, 2); |
| 1899 | $ret[$tmp[0]][$dict_key] = $tmp[1]; |
| 1900 | } |
| 1901 | else |
| 1902 | $therest[$dict_key] = $dict_value; |
| 1903 | if ($object_type_id != 0) |
| 1904 | { |
| 1905 | $screenlist = array(); |
| 1906 | foreach (explode (';', getConfigVar ('VENDOR_SIEVE')) as $sieve) |
| 1907 | if (FALSE !== mb_ereg ("^([^@]+)(@${object_type_id})?\$", trim ($sieve), $regs)) |
| 1908 | $screenlist[] = $regs[1]; |
| 1909 | foreach (array_keys ($ret) as $vendor) |
| 1910 | if (in_array ($vendor, $screenlist)) |
| 1911 | { |
| 1912 | $ok_to_screen = TRUE; |
| 1913 | if ($existing_value) |
| 1914 | foreach (array_keys ($ret[$vendor]) as $recordkey) |
| 1915 | if ($recordkey == $existing_value) |
| 1916 | { |
| 1917 | $ok_to_screen = FALSE; |
| 1918 | break; |
| 1919 | } |
| 1920 | if ($ok_to_screen) |
| 1921 | unset ($ret[$vendor]); |
| 1922 | } |
| 1923 | } |
| 1924 | $ret['other'] = $therest; |
| 1925 | return $ret; |
| 1926 | } |
| 1927 | |
| 1928 | function dos2unix ($text) |
| 1929 | { |
| 1930 | return str_replace ("\r\n", "\n", $text); |
| 1931 | } |
| 1932 | |
| 1933 | function buildPredicateTable ($parsetree) |
| 1934 | { |
| 1935 | $ret = array(); |
| 1936 | foreach ($parsetree as $sentence) |
| 1937 | if ($sentence['type'] == 'SYNT_DEFINITION') |
| 1938 | $ret[$sentence['term']] = $sentence['definition']; |
| 1939 | // Now we have predicate table filled in with the latest definitions of each |
| 1940 | // particular predicate met. This isn't as chik, as on-the-fly predicate |
| 1941 | // overloading during allow/deny scan, but quite sufficient for this task. |
| 1942 | return $ret; |
| 1943 | } |
| 1944 | |
| 1945 | // Take a list of records and filter against given RackCode expression. Return |
| 1946 | // the original list intact, if there was no filter requested, but return an |
| 1947 | // empty list, if there was an error. |
| 1948 | function filterEntityList ($list_in, $realm, $expression = array()) |
| 1949 | { |
| 1950 | if ($expression === NULL) |
| 1951 | return array(); |
| 1952 | if (!count ($expression)) |
| 1953 | return $list_in; |
| 1954 | $list_out = array(); |
| 1955 | foreach ($list_in as $item_key => $item_value) |
| 1956 | if (TRUE === judgeEntity ($realm, $item_key, $expression)) |
| 1957 | $list_out[$item_key] = $item_value; |
| 1958 | return $list_out; |
| 1959 | } |
| 1960 | |
| 1961 | function filterCellList ($list_in, $expression = array()) |
| 1962 | { |
| 1963 | if ($expression === NULL) |
| 1964 | return array(); |
| 1965 | if (!count ($expression)) |
| 1966 | return $list_in; |
| 1967 | $list_out = array(); |
| 1968 | foreach ($list_in as $item_key => $item_value) |
| 1969 | if (TRUE === judgeCell ($item_value, $expression)) |
| 1970 | $list_out[$item_key] = $item_value; |
| 1971 | return $list_out; |
| 1972 | } |
| 1973 | |
| 1974 | // Tell, if the given expression is true for the given entity. |
| 1975 | function judgeEntity ($realm, $id, $expression) |
| 1976 | { |
| 1977 | $item_explicit_tags = loadEntityTags ($realm, $id); |
| 1978 | global $pTable; |
| 1979 | return eval_expression |
| 1980 | ( |
| 1981 | $expression, |
| 1982 | array_merge |
| 1983 | ( |
| 1984 | $item_explicit_tags, |
| 1985 | getImplicitTags ($item_explicit_tags), |
| 1986 | generateEntityAutoTags ($realm, $id) |
| 1987 | ), |
| 1988 | $pTable, |
| 1989 | TRUE |
| 1990 | ); |
| 1991 | } |
| 1992 | |
| 1993 | // Idem, but use complete record instead of key. |
| 1994 | function judgeCell ($cell, $expression) |
| 1995 | { |
| 1996 | global $pTable; |
| 1997 | return eval_expression |
| 1998 | ( |
| 1999 | $expression, |
| 2000 | array_merge |
| 2001 | ( |
| 2002 | $cell['etags'], |
| 2003 | $cell['itags'], |
| 2004 | $cell['atags'] |
| 2005 | ), |
| 2006 | $pTable, |
| 2007 | TRUE |
| 2008 | ); |
| 2009 | } |
| 2010 | |
| 2011 | // If the requested predicate exists, return its [last] definition. |
| 2012 | // Otherwise return NULL (to signal filterEntityList() about error). |
| 2013 | // Also detect "not set" option selected. |
| 2014 | function interpretPredicate ($pname) |
| 2015 | { |
| 2016 | if ($pname == '_') |
| 2017 | return array(); |
| 2018 | global $pTable; |
| 2019 | if (isset ($pTable[$pname])) |
| 2020 | return $pTable[$pname]; |
| 2021 | return NULL; |
| 2022 | } |
| 2023 | |
| 2024 | // Tell, if a constraint from config option permits given record. |
| 2025 | function considerConfiguredConstraint ($entity_realm, $entity_id, $varname) |
| 2026 | { |
| 2027 | if (!strlen (getConfigVar ($varname))) |
| 2028 | return TRUE; // no restriction |
| 2029 | global $parseCache; |
| 2030 | if (!isset ($parseCache[$varname])) |
| 2031 | // getConfigVar() doesn't re-read the value from DB because of its |
| 2032 | // own cache, so there is no race condition here between two calls. |
| 2033 | $parseCache[$varname] = spotPayload (getConfigVar ($varname), 'SYNT_EXPR'); |
| 2034 | if ($parseCache[$varname]['result'] != 'ACK') |
| 2035 | return FALSE; // constraint set, but cannot be used due to compilation error |
| 2036 | return judgeEntity ($entity_realm, $entity_id, $parseCache[$varname]['load']); |
| 2037 | } |
| 2038 | |
| 2039 | // Return list of records in the given realm, which conform to |
| 2040 | // the given RackCode expression. If the realm is unknown or text |
| 2041 | // doesn't validate as a RackCode expression, return NULL. |
| 2042 | // Otherwise (successful scan) return a list of all matched |
| 2043 | // records, even if the list is empty (array() !== NULL). If the |
| 2044 | // text is an empty string, return all found records in the given |
| 2045 | // realm. |
| 2046 | function scanRealmByText ($realm = NULL, $ftext = '') |
| 2047 | { |
| 2048 | switch ($realm) |
| 2049 | { |
| 2050 | case 'object': |
| 2051 | case 'user': |
| 2052 | case 'ipv4net': |
| 2053 | case 'file': |
| 2054 | case 'ipv4vs': |
| 2055 | case 'ipv4rspool': |
| 2056 | if (!strlen ($ftext = trim ($ftext))) |
| 2057 | $fexpr = array(); |
| 2058 | else |
| 2059 | { |
| 2060 | $fparse = spotPayload ($ftext, 'SYNT_EXPR'); |
| 2061 | if ($fparse['result'] != 'ACK') |
| 2062 | return NULL; |
| 2063 | $fexpr = $fparse['load']; |
| 2064 | } |
| 2065 | return filterCellList (listCells ($realm), $fexpr); |
| 2066 | default: |
| 2067 | return NULL; |
| 2068 | } |
| 2069 | |
| 2070 | } |
| 2071 | |
| 2072 | function getIPv4VSOptions () |
| 2073 | { |
| 2074 | $ret = array(); |
| 2075 | foreach (listCells ('ipv4vs') as $vsid => $vsinfo) |
| 2076 | $ret[$vsid] = $vsinfo['dname'] . (empty ($vsinfo['name']) ? '' : " (${vsinfo['name']})"); |
| 2077 | return $ret; |
| 2078 | } |
| 2079 | |
| 2080 | function getIPv4RSPoolOptions () |
| 2081 | { |
| 2082 | $ret = array(); |
| 2083 | foreach (listCells ('ipv4rspool') as $pool_id => $poolInfo) |
| 2084 | $ret[$pool_id] = $poolInfo['name']; |
| 2085 | return $ret; |
| 2086 | } |
| 2087 | |
| 2088 | ?> |