ona  18.1.1
About: OpenNetAdmin provides a database managed inventory of your IP network (with Web and CLI interface).
  Fossies Dox: ona-18.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

functions_network_map.inc.php
Go to the documentation of this file.
1 <?php
2 // This is to be included in files that need a subnet block map
3 // Brandon Zehm
4 
5 
6 // Returns javascript to set the portal
7 function get_portal_js($window_name, $ip,$version=4) {
8 
9  $zoom = array();
10  if ($version == 4) {
11  $zoom[7]['row_height'] = 8;
12  $zoom[7]['ips_per_row'] = 256;
13  $zoom[7]['row_type'] = "class_c";
14  $zoom[7]['row_zoom'] = 1;
15 
16  $zoom[8]['row_height'] = 16;
17  $zoom[8]['ips_per_row'] = 256;
18  $zoom[8]['row_type'] = "class_c";
19  $zoom[8]['row_zoom'] = 2;
20 
21  $zoom[9]['row_height'] = 24;
22  $zoom[9]['ips_per_row'] = 256;
23  $zoom[9]['row_type'] = "class_c";
24  $zoom[9]['row_zoom'] = 3;
25  }
26  else {
27 
28  $zoom[7]['row_height'] = 8;
29  $zoom[7]['ips_per_row'] = "18446744073709551616";
30  $zoom[7]['row_type'] = "ipv6_64";
31  $zoom[7]['row_zoom'] = 1;
32 
33  $zoom[8]['row_height'] = 16;
34  $zoom[8]['ips_per_row'] = "18446744073709551616";
35  $zoom[8]['row_type'] = "ipv6_64";
36  $zoom[8]['row_zoom'] = 2;
37 
38  $zoom[9]['row_height'] = 24;
39  $zoom[9]['ips_per_row'] = "18446744073709551616";
40  $zoom[9]['row_type'] = "ipv6_64";
41  $zoom[9]['row_zoom'] = 3;
42 
43  }
44  $js .= <<<EOL
45 
46  /*** Setup the Portal ***/
47  var _el = el('{$window_name}_portal');
48 
49  /* Finally reposition/resize the window and hide any overflow */
50  _el.style.border = '2px solid #000000';
51  _el.style.overflow = 'hidden';
52 
53  /* Portal Mouse Down Handler */
54  _el.onmousedown =
55  function(ev) {
56  if (typeof(ev) == 'undefined') ev = event;
57  document.body.style.cursor = 'move';
58  dragStart(ev, '{$window_name}_substrate',
59  'savePosition', 0,
60  'drag', 'vertical',
61  'detectEdge', 0,
62  'opacity', 1
63  );
64 
65  /* Capture mouseup events .. when the document gets an onmouseup, call the myonmouseup function */
66  if (browser.isNS || browser.isKONQ) {
67  document.addEventListener("mouseup", el('{$window_name}_portal').myonmouseup, true);
68  ev.preventDefault();
69  }
70  else if (browser.isIE) {
71  document.attachEvent("onmouseup", el('{$window_name}_portal').myonmouseup);
72  window.event.cancelBubble = true;
73  window.event.returnValue = false;
74  }
75 
76  return false;
77  };
78 
79 
80  /* Portal Mouse Up Handler */
81  _el.myonmouseup =
82  function(ev) {
83  if (typeof(ev) == 'undefined') ev = event;
84  document.body.style.cursor = 'default';
85 
86  /* Stop capturing mouseup events */
87  if (browser.isNS || browser.isKONQ)
88  document.removeEventListener("mouseup", el('{$window_name}_portal').myonmouseup, true);
89  else if (browser.isIE)
90  document.detachEvent("onmouseup", el('{$window_name}_portal').myonmouseup);
91 
92  /* Global variable (bad!) to store which blocks have been requested but not built yet */
93  if (typeof(_map_requests_) == 'undefined') _map_requests_ = new Object;
94 
95  /* Determine which IP blocks should be currently displayed */
96 
97  /* Find some variables */
98  var base_ip = new BigNumber("{$ip}");
99  var portal_height = parseInt(el('{$window_name}_portal').offsetHeight);
100  var substrate_offset = parseInt(el('{$window_name}_substrate').style.top);
101  var row_padding = 4;
102  var row_height = 0;
103  var row_zoom = 0;
104  var row_type = '';
105  var ips_per_row = 0;
106  var zoom = el('{$window_name}_zoom').value;
107  switch (zoom) {
108  case '7': row_type = '{$zoom[7]["row_type"]}'; row_height = {$zoom[7]['row_height']}; row_zoom = {$zoom[7]['row_zoom']}; ips_per_row = new BigNumber("{$zoom[7]['ips_per_row']}"); break;
109  case '8': row_type = '{$zoom[8]["row_type"]}'; row_height = {$zoom[8]['row_height']}; row_zoom = {$zoom[8]['row_zoom']}; ips_per_row = new BigNumber("{$zoom[8]['ips_per_row']}"); break;
110  case '9': row_type = '{$zoom[9]["row_type"]}'; row_height = {$zoom[9]['row_height']}; row_zoom = {$zoom[9]['row_zoom']}; ips_per_row = new BigNumber("{$zoom[9]['ips_per_row']}"); break;
111 
112  default : alert("Zoom level " + zoom + " doesn't work yet.");
113  }
114 
115  var substrate_distance = new BigNumber(parseInt(substrate_offset / (row_height + row_padding)));
116  var total_blocks = parseInt( portal_height / (row_height + row_padding) );
117  var first_block = base_ip.subtract(substrate_distance.multiply(ips_per_row));
118  // alert ('\$ip: {$ip} base_ip:' + base_ip +' first_block: ' + first_block + " substrate_distance: " + substrate_distance);
119 
120 
123  var blocks = Array();
124  for (var block=0; block<(total_blocks*2); block++) blocks.push(block);
125  for (var block=-1; block>=(-1 * total_blocks); block--) blocks.push(block);
126 
128  for (var x=0; x<blocks.length; x++) {
129  var block = blocks[x];
130  var next_block = first_block.add(ips_per_row.multiply(block));
131  var _block = el(next_block + '_row');
132  // alert("_block " + block + ": " + _block + " next_block: " + next_block + " " +_map_requests_[next_block + '_row']);
133  if (!_map_requests_[next_block + '_row'] && !_block) {
134  _map_requests_[next_block + '_row'] = 1;
135  // var block_offset = parseInt( -1 * ((base_ip - next_block) / ips_per_row) * (row_height + row_padding) );
136  // BAD var block_offset = (((base_ip.subtract(next_block)).divide(ips_per_row)).multiply(row_height + row_padding)).negate.intPart;
137  var temp1 = base_ip.subtract(next_block);
138  var temp2 = temp1.divide(ips_per_row);
139  var temp3 = temp2.multiply(row_height + row_padding);
140  var temp4 = temp3.intPart();
141  var block_offset = temp4.negate();
142  xajax_window_submit(
143  '{$window_name}',
144  'ip => ' + next_block + ',' +
145  'row_offset => ' + block_offset + ',' +
146  'row_type => ' + row_type + ',' +
147  'row_height => ' + row_height + ',' +
148  'row_zoom => ' + row_zoom,
149  'draw_block'
150  );
151  }
152  }
153 
154 
164  var cache_boundary_top = first_block.subtract(total_blocks * ips_per_row);
165  var cache_boundary_bottom = first_block.add(total_blocks * 2 * ips_per_row);
166  nodes = el('{$window_name}_substrate').childNodes;
167  var toremove = Array();
168  for (var counter=0; counter < nodes.length; counter++) {
169  var ip = parseInt(nodes[counter].id);
170  if (ip < cache_boundary_top || ip > cache_boundary_bottom)
171  toremove.push(nodes[counter]);
172  }
173  /* Remove the elements we decided to remove */
174  for (var counter=0; counter < toremove.length; counter++)
175  toremove[counter].parentNode.removeChild(toremove[counter]);
176 
177  return false;
178  };
179 
180  /*** Setup the Substrate ***/
181  var _el = el('{$window_name}_substrate');
182  _el.style.position = 'absolute';
183  _el.style.top = '0px';
184  _el.style.left = '0px';
185 
186 
187 EOL;
188  return($js);
189 }
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
201 // Function: ws_draw_block()
202 //
203 // Description:
204 // Redraws the contents of the portal div given proper input
205 // We get:
206 // * What the base IP address is
207 // * What the row offset is
208 // * What the row type is
209 // * What the row height is
210 // * What the zoom level is
211 //
212 // Zoom details: (only class c for now!)
213 // 1-3 :: Class A views
214 // 4-6 :: Class B views
215 // 7-9 :: Class C views
217 function ws_draw_block($window_name, $form='') {
218  global $conf, $self, $ona;
219  global $images, $color, $style;
220  $html = '';
221  $js = '';
222 
223  // Instantiate the xajaxResponse object
224  $response = new xajaxResponse();
225 
226  // If the user supplied an array in a string, build the array and store it in $form
227  $form = parse_options_string($form);
228 
229  // Find out if $ip is valid
230  $ip = ip_mangle($form['ip'], 'numeric');
231  if ($ip == -1) {
232  $js .= "alert('Invalid IP address!');";
233  $response->addScript($js);
234  return($response->getXML());
235  }
236 
237  // Build a few variables
238  $label = ip_mangle($ip, 'dotted');
239  if (is_ipv4($ip)) {
240  $string_size = 15;
241  }
242  else {
243  $string_size = 20;
244  }
245 
246  $function = "get_{$form['row_type']}_html";
247  $row_zoom = $form['row_zoom'];
248  $row_height = $form['row_height'];
249  $row_offset = $form['row_offset'];
250  // This is the number of pixels to leave for the label on the left (.45 * row height, * $string_size possible characters)
251  $label_width = round($form['row_height'] * 0.45 * $string_size);
252  $font_height = $form['row_height'] - 2; // Label font-size
253  if ($font_height < 8) $font_height = 8;
254  $row_left = $label_width + 10; // Left offset for the block row
255 
256 
257  // Get some html (the html that goes inside the div)
258  $html = $function($ip, $row_zoom, $row_height);
259  $el_name = $ip . '_row';
260 
261  // Add a row label
262  $response->addCreate("{$window_name}_substrate", "div", $el_name . '_label');
263  $response->addScript(<<<EOL
264  var _el = el('{$el_name}_label');
265  _el.style.visibility = 'hidden';
266  _el.style.position = 'absolute';
267  _el.style.textAlign = 'right';
268  _el.style.color = '#000000';
269  _el.style.fontSize = '{$font_height}px';
270  _el.style.top = '{$row_offset}px';
271  _el.style.left = '2px';
272  _el.style.width = '{$label_width}px';
273  _el.style.overflow = 'visible';
274  _el.style.whiteSpace = 'nowrap';
275 EOL
276  );
277 
278  // Add the row
279  $response->addCreate("{$window_name}_substrate", "div", $el_name);
280  $response->addScript(<<<EOL
281  var _el = el('{$el_name}');
282  _el.style.visibility = 'hidden';
283  _el.style.position = 'absolute';
284  _el.style.top = '{$row_offset}px';
285  _el.style.left = '{$row_left}px';
286  _el.style.borderTop = '1px solid #000000';
287  _el.style.borderBottom = '1px solid #000000';
288  _el.style.borderLeft = '1px solid #000000';
289  if (browser.isIE)
290  _el.style.fontSize = ({$font_height} - 2) + 'px';
291 EOL
292  );
293 
294  // Fill the label and row
295  $response->addAssign($el_name, "innerHTML", $html);
296  $response->addAssign($el_name . '_label', "innerHTML", $label);
297 
298  // Javascript to make sure the container (row) div is the exact length to hold it's contents in one row
299  $response->addScript(<<<EOL
300  var nodes = _el.childNodes;
301  var width = 0;
302  for (var counter=0; counter < nodes.length; counter++)
303  if (nodes[counter].nodeType == 1)
304  width += parseInt(nodes[counter].offsetWidth);
305  if (browser.isIE) width += 1; /* for the left border */
306  _el.style.width = width + 'px';
307 EOL
308  );
309 
310  // Display the label and block
311  $response->addScript(<<<EOL
312  el('{$el_name}_label').style.visibility = 'visible';
313  el('{$el_name}').style.visibility = 'visible';
314  /* Tell the browser we've sent it this block so it knows it can re-request it if it needs to */
315  _map_requests_['{$el_name}'] = undefined;
316 EOL
317  );
318  if ($js) { $response->addScript($js); }
319 
320  return($response->getXML());
321 }
322 
323 
324 
325 
326 
327 
328 
329 
330 // This function needs the folowing colors defined in the global variable $color
331 // $color['bgcolor_map_empty']
332 // $color['bgcolor_map_subnet']
333 
334 // IPv6:
335 // /96: 4294967296
336 // /64: 18446744073709551617
337 
338 function get_ipv6_64_html($ip=0, $zoom=2, $row_height) {
339  global $conf, $self, $onadb, $color, $style, $images;
340  $html = '';
341  if ($ip == 0) { return($html); }
342 
343  $ips_per_block=gmp_init("18446744073709551615");
344  $ip_end = gmp_add(gmp_init($ip),$ips_per_block);
345  $display_factor = gmp_div($ips_per_block,"255");
346 
347  $x_px_per_ip = $zoom;
348 
349  // Select all subnet records
350  //$where = "ip_addr >= {$ip} AND ip_addr <= " . $ona->qstr($ip_end);
351  $where = "ip_addr >= {$ip} AND ip_addr <= ".gmp_strval($ip_end);
352  list ($status, $num_subnets, $subnets) =
354  $onadb,
355  'subnets',
356  $where,
357  "ip_addr ASC"
358  );
359 
360  // If the first record isn't a subnet, see if the first IP is in another subnet
361  if ($num_subnets > 0 && gmp_cmp(gmp_init($subnets[0]['ip_addr']), $ip) !=0) {
362  $where = "ip_addr < {$ip} AND ((340282366920938463463374607431768211454 - ip_mask) + ip_addr) >= {$ip} AND ip_mask > 4294967296";
363  list ($status, $rows, $temp_subnet) = db_get_record($onadb, 'subnets', $where);
364  if ($rows) {
365  $num_subnets++;
366  array_unshift($subnets, $temp_subnet);
367  }
368  }
369 
370  $block_start = gmp_init($ip);
371 
372  // Find the next block of addresses
373  while (gmp_cmp($block_start,$ip_end) < 0) {
374  if ( (!is_array($subnet)) or (gmp_cmp($block_start, $subnet['ip_addr']) > 0 ) ) {
375  $subnet = array_shift($subnets);
376  if (is_array($subnet)) {
377  $subnet['SIZE'] = gmp_add(gmp_sub(gmp_init("0xffffffffffffffffffffffffffffffff"), $subnet['ip_mask']) , "1");
378  $subnet['ip_addr_end'] = gmp_sub(gmp_add(gmp_init($subnet['ip_addr']),$subnet['SIZE']), "1");
379  $subnet['ip_addr'] = gmp_init($subnet['ip_addr']);
380  }
381  else {
382  // pretend like the next subnet record is the next /64
383  $subnet['SIZE'] = gmp_add(gmp_sub($ip_end, "$block_start"),"1");
384  $subnet['ip_addr'] = gmp_add($ip_end,"1");
385  }
386  }
387 
388  // If it's unallocated space
389  if (gmp_cmp($block_start, $subnet['ip_addr']) < 0) {
390  $block_end = gmp_sub($subnet['ip_addr'],"1");
391  $block_color = $color['bgcolor_map_empty'];
392  }
393  // If it's allocated space
394  else {
395  $block_end = $subnet['ip_addr_end'];
396  if (gmp_cmp($block_end, $ip_end) > 0) { $block_end = $ip_end; }
397  $block_color = $color['bgcolor_map_subnet'];
398  }
399  $block_size = gmp_add(gmp_sub($block_end , $block_start) , "1");
400  // $block_size_total += $block_size;
401  // $block_title = htmlentities($subnet['DESCRIPTION'] . " :: Size={$block_size}", ENT_QUOTES) . ' :: ' . ip_mangle($block_start, 'dotted') . " -> " . ip_mangle($block_end, 'dotted');
402 
403  // Display the current block (-1 for px border unless it's IE)
404  $x = gmp_sub(gmp_div(gmp_mul($block_size, "$x_px_per_ip"),$display_factor), "1");
405  if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') != false) { $x=gmp_add($x,"1"); }
406  $str_block_start = gmp_strval($block_start);
407  $str_x = gmp_strval($x);
408  $html .= <<<EOL
409 <div id="{$str_block_start}_block"
410  style="
411  clear: none; float: left;
412  border-right: 1px solid #000000; background-color: {$block_color};
413  width: {$str_x}px; height: {$row_height}px;"
414  onMouseOver="
415  wwTT(this, event,
416  'id', 'tt_subnet_{$str_block_start}',
417  'type', 'velcro',
418  'styleClass', 'wwTT_niceTitle',
419  'direction', 'south',
420  'javascript', 'xajax_window_submit(\'tooltips\', \'tooltip=>subnet,id=>tt_subnet_{$str_block_start},subnet_ip=>{$str_block_start}\');'
421  );"
422 ></div>
423 EOL;
424 
425  $block_start = gmp_add($block_end,"1");
426  }
427 
428 
429 
430  return($html);
431 }
432 
433 
434 
435 
436 
437 
438 function get_class_c_html($ip=0, $zoom=2, $row_height) {
439  global $conf, $self, $onadb, $color, $style, $images;
440  $html = '';
441  if ($ip == 0) { return($html); }
442  $ip_end = $ip + 255;
443 
444  $x_px_per_ip = $zoom;
445 
446  // Select all subnet records in this class C
447  //$where = "ip_addr >= {$ip} AND ip_addr <= " . $ona->qstr($ip_end);
448  $where = "ip_addr >= {$ip} AND ip_addr <= {$ip_end}";
449  list ($status, $num_subnets, $subnets) =
451  $onadb,
452  'subnets',
453  $where,
454  "ip_addr ASC"
455  );
456 
457  // If the first record isn't a subnet, see if the first IP is in another subnet
458  if ($subnets[0]['ip_addr'] != $ip) {
459  $where = "ip_addr < {$ip} AND ((4294967295 - ip_mask) + ip_addr) >= {$ip}";
460  list ($status, $rows, $subnet) = db_get_record($onadb, 'subnets', $where);
461  if ($rows) {
462  $num_subnets++;
463  array_unshift($subnets, $subnet);
464  }
465  }
466 
467  $block_start = $ip;
468 
469  // Find the next block of addresses
470  while ($block_start < $ip_end) {
471  if ( (!is_array($subnet)) or ($block_start > $subnet['ip_addr']) ) {
472  $subnet = array_shift($subnets);
473  if (is_array($subnet)) {
474  $subnet['SIZE'] = (0xffffffff - $subnet['ip_mask']) + 1;
475  $subnet['ip_addr_end'] = $subnet['ip_addr'] + $subnet['SIZE'] - 1;
476  }
477  else {
478  // pretend like the next subnet record is the next class C
479  $subnet['SIZE'] = $ip_end - $block_start + 1;
480  $subnet['ip_addr'] = $ip_end + 1;
481  }
482  }
483 
484  // If it's unallocated space
485  if ($block_start < $subnet['ip_addr']) {
486  $block_end = $subnet['ip_addr'] - 1;
487  $block_color = $color['bgcolor_map_empty'];
488  }
489  // If it's allocated space
490  else {
491  $block_end = $subnet['ip_addr_end'];
492  if ($block_end > $ip_end) { $block_end = $ip_end; }
493  $block_color = $color['bgcolor_map_subnet'];
494  }
495  $block_size = ($block_end - $block_start + 1);
496  $block_size_total += $block_size;
497  // $block_title = htmlentities($subnet['DESCRIPTION'] . " :: Size={$block_size}", ENT_QUOTES) . ' :: ' . ip_mangle($block_start, 'dotted') . " -> " . ip_mangle($block_end, 'dotted');
498 
499  // Display the current block (-1 for px border unless it's IE)
500  $x = ($block_size * $x_px_per_ip) - 1;
501  if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') != false) { $x++; }
502  $html .= <<<EOL
503 <div id="{$block_start}_block"
504  style="
505  clear: none; float: left;
506  border-right: 1px solid #000000; background-color: {$block_color};
507  width: {$x}px; height: {$row_height}px;"
508  onMouseOver="
509  wwTT(this, event,
510  'id', 'tt_subnet_{$block_start}',
511  'type', 'velcro',
512  'styleClass', 'wwTT_niceTitle',
513  'direction', 'south',
514  'javascript', 'xajax_window_submit(\'tooltips\', \'tooltip=>subnet,id=>tt_subnet_{$block_start},subnet_ip=>{$block_start}\');'
515  );"
516 ></div>
517 EOL;
518  $block_start = $block_end + 1;
519  }
520 
521 
522 
523  return($html);
524 }
525 
526 
527 
528 
529 
530 
531 
532 ?>
get_portal_js
get_portal_js($window_name, $ip, $version=4)
Definition: functions_network_map.inc.php:7
ip_mangle
ip_mangle($ip="", $format="default")
Definition: functions_general.inc.php:308
db_get_records
db_get_records($dbh=0, $table="", $where="", $order="", $rows=-1, $offset=-1)
Definition: functions_db.inc.php:891
$status
$status
Definition: install.php:12
$onadb
global $onadb
Definition: 2-to-3.php:15
ws_draw_block
ws_draw_block($window_name, $form='')
Definition: functions_network_map.inc.php:217
$images
$images
Definition: config.inc.php:26
EOL
< a style="text-decoration: none;" href="/">< input class='edit' type="button" value="I don't like free stuff?" onclick=""/></a ></center ></div > EOL
Definition: install.php:40
$conf
global $conf
Definition: 2-to-3.php:15
xajaxResponse
Definition: xajaxResponse.inc.php:68
$style
$style['font-family']
Definition: config.inc.php:158
$ip
$ip
Definition: main.inc.php:24
$self
global $self
Definition: 2-to-3.php:15
get_class_c_html
get_class_c_html($ip=0, $zoom=2, $row_height)
Definition: functions_network_map.inc.php:438
db_get_record
db_get_record($dbh=0, $table="", $where="", $order="")
Definition: functions_db.inc.php:708
get_ipv6_64_html
get_ipv6_64_html($ip=0, $zoom=2, $row_height)
Definition: functions_network_map.inc.php:338
$color
if($_SERVER['SERVER_PORT']==443) $color['bg']
Definition: config.inc.php:119
$version
$version
Definition: main.inc.php:22
parse_options_string
parse_options_string($input)
Definition: xajax_setup.inc.php:88