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)  

search_results_qf.inc.php
Go to the documentation of this file.
1 <?php
2 
3 
4 
6 // Function:
7 // subnet ($window_name, $form_id)
8 //
9 // Description:
10 // Does a quick subnet search and puts the results in the div specified
11 // in $form['content_id']. At most 250 results will be returned unless
12 // overridden in $form['max_results'].
13 // When a search result is clicked on, it's value will be put into the
14 // input element specified in $form['input_id'] and the quick filter
15 // popup window identified by $form['id'] is removed from the dom.
17 function ws_subnet($window_name, $form='') {
18  global $conf, $self, $onadb;
19  global $color, $style, $images;
20  $html = $js = '';
21 
22  // If the user supplied an array in a string, build the array and store it in $form
23  $form = parse_options_string($form);
24 
25  // Set max_results and max_usage_bars if we need to
26  if (!is_numeric($form['max_results']))
27  $form['max_results'] = 250;
28  if (!is_numeric($form['max_usage_bars']))
29  $form['max_usage_bars'] = 50;
30 
31 
32 
33 
34  //
35  // *** QF SUBNET SEARCH ***
36  //
37 
38  // Search results go in here
39  $results = array();
40  $count = 0;
41 
42  // Start building the "where" clause for the sql query to find the subnets to display
43  $where = "";
44  $and = "";
45 
46  // SUBNET TYPE
47  if ($form['nettype']) {
48  $where .= $and . "subnet_type_id = " . $onadb->qstr($form['nettype']);
49  $and = " AND ";
50  }
51 
52  // SUBNET DESCRIPTION
53  if ($form['netdesc']) {
54  // This field is always upper case
55  $form['netdesc'] = strtoupper($form['netdesc']);
56  $where .= $and . "name LIKE " . $onadb->qstr('%'.$form['netdesc'].'%');
57  $and = " AND ";
58  }
59 
60  // IP ADDRESS
61  if ($form['ip_subnet']) {
62  // Build $ip and $ip_end from $form['ip_subnet'] and $form['ip_subnet_thru']
63  $ip = ip_complete($form['ip_subnet'], '0');
64  if ($form['ip_subnet_thru']) { $ip_end = ip_complete($form['ip_subnet_thru'], '255'); }
65  else { $ip_end = ip_complete($form['ip_subnet'], '255'); }
66 
67  // Find out if $ip and $ip_end are valid
68  $ip = ip_mangle($ip, 'numeric');
69  $ip_end = ip_mangle($ip_end, 'numeric');
70  if ($ip != -1 and $ip_end != -1) {
71  // Find subnets between the specified ranges
72  $where .= "ip_addr >= " . $onadb->qstr($ip) . " AND ip_addr <= " . $onadb->qstr($ip_end);
73  }
74  }
75 
76 
77  // Do the SQL Query
78  $filter = '';
79  if ($form['filter']) {
80  // Subnet descriptions are always upper case
81  $form['filter'] = strtoupper($form['filter']);
82  $filter = $and . ' name LIKE ' . $onadb->qstr('%'.$form['filter'].'%');
83  }
84  list ($status, $rows, $results) =
86  $onadb,
87  'subnets',
88  $where . $filter,
89  "ip_addr ASC",
90  $form['max_results']
91  );
92 
93  // If there were more than $form['max_results'] find out how many records there really are
94  if ($rows >= $form['max_results']) {
95  list ($status, $rows, $records) =
97  $onadb,
98  'subnets',
99  $where . $filter,
100  "",
101  0
102  );
103  }
104  $count = $rows;
105 
106 
107  //
108  // *** BUTILD RESULTS HTML ***
109  //
110 
111  $html .= <<<EOL
112 <table style="cursor: pointer;" width="100%" cellspacing="0" border="0" cellpadding="0">
113 EOL;
114  if ($count > $form['max_results']) {
115  $html .= <<<EOL
116 <tr><td style="cursor: default; font-size: 12px; background-color: #FFCCCC; color: 000;" colspan="5" align="center">Displaying {$form['max_results']} of {$count} results</td></tr>
117 EOL;
118  }
119 
120  if ($count > $form['max_usage_bars']) {
121  $too_many_bars_message = <<<EOL
122 <tr><td style="cursor: default; font-size: 12px; background-color: #FFCCCC; color: 000;" colspan="5" align="center">Usage graph only displayed for the first {$form['max_usage_bars']} subnets</td></tr>
123 EOL;
124  }
125 
126  $i = 0;
127  foreach ($results as $record) {
128  $record['ip_addr'] = ip_mangle($record['ip_addr'], 'dotted');
129 
130  // Calculate the percentage of the subnet that's used (total size - allocated hosts - dhcp pool size)
131  if ($i++ <= $form['max_usage_bars'])
133  else if ($i == $form['max_usage_bars'] + 2) {
134  $html .= $too_many_bars_message;
135  $usage_html = '&nbsp;';
136  }
137  else $usage_html = '&nbsp;';
138 
139  // The onClick javascript is slightly different if we're looking for a subnet, or a free IP address
140  if ($form['next_action'] == 'free_ip')
141  $onclick = "el('qf_free_ip_subnet_id').value = '{$record['id']}'; xajax_window_submit('{$window_name}', xajax.getFormValues('{$form['form_id']}'), 'free_ip');";
142  else
143  $onclick = "el('{$form['input_id']}').value = '{$record['name']}'; removeElement('{$form['id']}');";
144 
145  $html .= <<<EOL
146 <tr onMouseOver="this.className='row-highlight';"
147  onMouseOut="this.className='row-normal';"
148  onClick="{$onclick}">
149  <td style="font-size: 10px; padding: 0px 2px;">{$record['ip_addr']}</td>
150  <td style="font-size: 10px; padding: 0px 2px;">{$usage_html}</td>
151  <td style="font-size: 10px; padding: 0px 2px;">{$record['name']}</td>
152 </tr>
153 EOL;
154  }
155 
156  $html .= <<<EOL
157 </table>
158 EOL;
159 
160  $js .= <<<EOL
161  el('{$form['content_id']}').style.display = 'block';
162  /* Reposition the popup if the new content is displayed off the screen */
163  wwTT_position('{$form['id']}');
164 EOL;
165 
166 if ($count == 1) {
167  $html = "";
168  $js .= "el('qf_free_ip_subnet_id').value = '{$record['id']}'; xajax_window_submit('{$window_name}', xajax.getFormValues('{$form['form_id']}'), 'free_ip');";
169 }
170  // Insert the new html into the window
171  // Instantiate the xajaxResponse object
172  $response = new xajaxResponse();
173  $response->addAssign($form['content_id'], "innerHTML", $html);
174  if ($js) { $response->addScript($js); }
175  return($response->getXML());
176 }
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
193 // Function:
194 // free_ip ($window_name, $form_id)
195 //
196 // Description:
197 // Does a quick search for availble IP addresses and puts the results
198 // in the div specified in $form['content_id']. At most 250 results will
199 // be returned unless overridden in $form['max_results'].
200 // When a search result is clicked on, it's value will be put into the
201 // input element specified in $form['input_id'] and the quick filter
202 // popup window identified by $form['id'] is removed from the dom.
204 function ws_free_ip($window_name, $form='') {
205  global $conf, $self, $onadb;
206  global $color, $style, $images;
207  $html = $js = '';
208 
209  // If the user supplied an array in a string, build the array and store it in $form
210  $form = parse_options_string($form);
211 
212  // Set max_results and max_usage_bars if we need to
213  if (!is_numeric($form['max_results']))
214  $form['max_results'] = 512;
215 
216 
217 
218  //
219  // *** QF AVAILBLE IP ADDRESS SEARCH ***
220  //
221 
222 
223  // Get the specified subnet record by id
224  list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $form['subnet_id']));
225  if ($status or !$rows) {
226  // Send a javascript popup error
227  $response = new xajaxResponse();
228  $response->addScript("alert('ERROR => Invalid subnet selected!');");
229  return($response->getXML());
230  }
231 
232  // Get a list of interfaces on the selected subnet
233  list($status, $rows, $interfaces) =
234  db_get_records($onadb, 'interfaces', array('subnet_id' => $subnet['id']));
235 
236  // Transform that list into a simple array of ip addresses
237  // NOTE: you must use a string as the used_ips array element as integers will get too large
238  $used_ips = array();
239  foreach ($interfaces as $interface) {
240  $used_ips["{$interface['ip_addr']}"] = $interface['host_id'];
241  }
242  unset($interfaces, $interface);
243 
244  // Get a list of dhcp pools on the selected subnet
245  list($status, $rows, $pools) =
246  db_get_records($onadb, 'dhcp_pools', array('subnet_id' => $subnet['id']));
247 
248  // Add DHCP pool addresses into the list of used ips
249  foreach ($pools as $pool)
250  for ($ip = $pool['ip_addr_start']; $ip <= $pool['ip_addr_end']; $ip++)
251  $used_ips["{$ip}"] = $pool['id'];
252 
253  // Create a few variables that will be handy later
254  if(strlen($subnet['ip_addr']) < 11) {
255  // echo "ipv4";
256  $num_ips = 0xffffffff - $subnet['ip_mask'];
257  if ($subnet['ip_mask'] < 4294967294) {
258  $last_ip = ($subnet['ip_addr'] + $num_ips) - 1;
259  } else {
260  $last_ip = ($subnet['ip_addr'] + $num_ips);
261  }
262  } else {
263  // echo "ipv6";
264  $sub = gmp_sub("340282366920938463463374607431768211455", $subnet['ip_mask']);
265  $num_ips = gmp_strval($sub);
266  $last_ip = ($subnet['ip_addr'] + $num_ips) - 1;
267  }
268 
269 
270  // Search results go in here
271  $results = array();
272  $allused = count($used_ips);
273  $count = gmp_strval(gmp_sub($num_ips, $allused));
274 
275  // Create a list of available IP's
276  if ($subnet['ip_mask'] = 4294967296) {
277  $ip = $subnet['ip_addr'];
278  } else {
279  $ip = gmp_strval(gmp_add($subnet['ip_addr'],'1'));
280  }
281  while ($ip <= $last_ip and count($results) <= $form['max_results']) {
282  if (!array_key_exists("{$ip}", $used_ips))
283  $results[] = $ip;
284  $plusone = gmp_add("1", $ip);
285  $ip = gmp_strval($plusone);
286  }
287 
288 
289  //
290  // *** BUTILD RESULTS HTML ***
291  //
292 
293  $html .= <<<EOL
294 <table style="cursor: pointer;" width="100%" cellspacing="0" border="0" cellpadding="0">
295 EOL;
296  if ($count > $form['max_results']) {
297  $html .= <<<EOL
298 <tr><td style="cursor: default; font-size: 12px; background-color: #FFCCCC; color: 000;" colspan="5" align="center">Displaying {$form['max_results']} of {$count} results</td></tr>
299 EOL;
300  }
301 
302  if ($count > $form['max_usage_bars']) {
303  $too_many_bars_message = <<<EOL
304 <tr><td style="cursor: default; font-size: 12px; background-color: #FFCCCC; color: 000;" colspan="5" align="center">Usage graph only displayed for the first {$form['max_usage_bars']} subnets</td></tr>
305 EOL;
306  }
307 
308  $subnet['name'] = htmlentities($subnet['name'], ENT_QUOTES, $conf['php_charset']);
309  foreach ($results as $ip) {
310  if (strlen($ip) > 11) {
311  $ip = ip_mangle($ip, 'ipv6gz');
312  } else {
313  $ip = ip_mangle($ip, 'dotted');
314  }
315  $html .= <<<EOL
316 <tr onMouseOver="this.className='row-highlight';"
317  onMouseOut="this.className='row-normal';"
318  onClick="el('{$form['input_id']}').value = '{$ip}'; el('{$form['text_id']}').innerHTML = '{$subnet['name']}'; removeElement('{$form['id']}');">
319  <td style="font-size: 10px; padding: 0px 2px;">{$ip}</td>
320 </tr>
321 EOL;
322  }
323 
324  $html .= <<<EOL
325 </table>
326 EOL;
327 
328  $js .= <<<EOL
329  el('{$form['content_id']}').style.display = 'block';
330  /* Reposition the popup if the new content is displayed off the screen */
331  wwTT_position('{$form['id']}');
332 EOL;
333 
334 
335  // Insert the new html into the window
336  // Instantiate the xajaxResponse object
337  $response = new xajaxResponse();
338  $response->addAssign($form['content_id'], "innerHTML", $html);
339  if ($js) { $response->addScript($js); }
340  return($response->getXML());
341 }
342 
343 
344 
345 
346 
347 
348 
349 
350 
351 
352 
353 
354 
355 
356 
357 
359 // Function:
360 // location ($window_name, $form_id)
361 //
362 // Description:
363 // Does a quick location search and puts the results in the div specified
364 // in $form['content_id']. At most 250 results will be returned unless
365 // overridden in $form['max_results'].
366 // When a search result is clicked on, it's value will be put into the
367 // input element specified in $form['input_id'] and the quick filter
368 // popup window identified by $form['id'] is removed from the dom.
370 function ws_location($window_name, $form='') {
371  global $conf, $self, $onadb;
372  global $color, $style, $images;
373  $html = $js = '';
374 
375  // If the user supplied an array in a string, build the array and store it in $form
376  $form = parse_options_string($form);
377 
378  // Set max_results and max_usage_bars if we need to
379  if (!is_numeric($form['max_results']))
380  $form['max_results'] = 250;
381 
382 
383 
384  //
385  // *** QF LOCATION SEARCH ***
386  //
387 
388  // Search results go in here
389  $results = array();
390  $count = 0;
391 
392  // Start building the "where" clause for the sql query to find the subnets to display
393  $where = "";
394  $and = "";
395 
396  // LOCATION REFERENCE
397  if ($form['reference']) {
398  // This field is always upper case
399  $form['reference'] = strtoupper($form['reference']);
400  $where .= $and . "reference LIKE " . $onadb->qstr('%'.$form['reference'].'%');
401  $and = " AND ";
402  }
403 
404  // LOCATION NAME
405  if ($form['name']) {
406  // This field is always upper case
407  $where .= $and . "name LIKE " . $onadb->qstr('%'.$form['name'].'%');
408  $and = " AND ";
409  }
410 
411  // LOCATION ADDRESS
412  if ($form['address']) {
413  $where .= $and . "address LIKE " . $onadb->qstr('%'.$form['address'].'%');
414  $and = " AND ";
415  }
416 
417  // CITY
418  if ($form['city']) {
419  // This field is always upper case
420  $form['city'] = strtoupper($form['city']);
421  $where .= $and . "city LIKE " . $onadb->qstr('%'.$form['city'].'%');
422  $and = " AND ";
423  }
424 
425  // STATE
426  if ($form['state']) {
427  // This field is always upper case
428  $form['state'] = strtoupper($form['state']);
429  $where .= $and . "state = " . $onadb->qstr($form['state']);
430  $and = " AND ";
431  }
432 
433  // ZIP CODE
434  if ($form['zip']) {
435  $where .= $and . "zip_code = " . $onadb->qstr($form['zip']);
436  $and = " AND ";
437  }
438 
439  // Wild card .. if $while is still empty, add a 'ID > 0' to it so you see everything.
440  if ($where == '')
441  $where = 'id > 0';
442 
443 
444  // Do the SQL Query
445  $filter = '';
446  if ($form['filter']) {
447  $form['filter'] = strtoupper($form['filter']);
448  $filter = $and . ' name LIKE ' . $onadb->qstr('%'.$form['filter'].'%');
449  }
450  list ($status, $rows, $results) =
452  $onadb,
453  'locations',
454  $where . $filter,
455  "reference ASC",
456  $form['max_results']
457  );
458 
459  // If there were more than $form['max_results'] find out how many records there really are
460  if ($rows >= $form['max_results']) {
461  list ($status, $rows, $records) =
463  $onadb,
464  'locations',
465  $where . $filter,
466  "",
467  0
468  );
469  }
470  $count = $rows;
471 
472 
473  //
474  // *** BUILD RESULTS HTML ***
475  //
476 
477  $html .= <<<EOL
478 <table style="cursor: pointer;" width="100%" cellspacing="0" border="0" cellpadding="0">
479 EOL;
480  if ($count > $form['max_results']) {
481  $html .= <<<EOL
482 <tr><td style="cursor: default; font-size: 12px; background-color: #FFCCCC; color: 000;" colspan="5" align="center">Displaying {$form['max_results']} of {$count} results</td></tr>
483 EOL;
484  }
485 
486  foreach ($results as $record) {
487  $html .= <<<EOL
488 <tr onMouseOver="this.className='row-highlight';"
489  onMouseOut="this.className='row-normal';"
490  onClick="el('{$form['input_id']}').value = '{$record['reference']}'; removeElement('{$form['id']}');"
491  title="{$record['name']}">
492  <td style="font-size: 10px; padding: 0px 2px;">{$record['reference']}</td>
493  <td style="font-size: 10px; padding: 0px 2px;">{$record['address']}</td>
494  <td style="font-size: 10px; padding: 0px 2px;">{$record['city']}, {$record['state']}</td>
495 </tr>
496 EOL;
497  }
498 
499  $html .= <<<EOL
500 </table>
501 EOL;
502 
503  $js .= <<<EOL
504  el('{$form['content_id']}').style.display = 'block';
505  /* Reposition the popup if the new content is displayed off the screen */
506  wwTT_position('{$form['id']}');
507 EOL;
508 
509 
510  // Insert the new html into the window
511  // Instantiate the xajaxResponse object
512  $response = new xajaxResponse();
513  $response->addAssign($form['content_id'], "innerHTML", $html);
514  if ($js) { $response->addScript($js); }
515  return($response->getXML());
516 }
517 
518 
519 
520 
521 
522 
523 
524 
525 
526 
527 
528 
530 // Function:
531 // vlan ($window_name, $form_id)
532 //
533 // Description:
534 // Does a quick vlan search and puts the results in the div specified
535 // in $form['content_id']. At most 250 results will be returned unless
536 // overridden in $form['max_results'].
537 // When a search result is clicked on, it's value will be put into the
538 // input element specified in $form['input_id'], the text in the
539 // element specified in $form['text_id'] will be updated, and the quick
540 // filter popup window identified by $form['id'] is removed from the dom.
542 function ws_vlan($window_name, $form='') {
543  global $conf, $self, $onadb;
544  global $color, $style, $images;
545  $html = $js = '';
546 
547  // If the user supplied an array in a string, build the array and store it in $form
548  $form = parse_options_string($form);
549 
550  // Set max_results and max_usage_bars if we need to
551  if (!is_numeric($form['max_results']))
552  $form['max_results'] = 250;
553 
554 
555 
556  //
557  // *** QF VLAN SEARCH ***
558  //
559 
560  // Get the specified VLAN Campus record by name
561  // If it isn't exact, don't accept it.
562  list($status, $rows, $campus) = ona_get_vlan_campus_record(array('name' => $form['campus']));
563  if ($status or !$rows) {
564  // Send a javascript popup error
565  $response = new xajaxResponse();
566  $response->addScript("alert('ERROR => Invalid VLAN campus!');");
567  return($response->getXML());
568  }
569 
570 
571  // Get a list of vlan's in the selected campus
572  list($status, $count, $results) =
574  $onadb,
575  'vlans',
576  array('vlan_campus_id' => $campus['id']),
577  'number',
578  $form['max_results']);
579 
580  // If there were more than $form['max_results'] find out how many records there really are
581  if ($count >= $form['max_results']) {
582  list ($status, $count, $tmp) =
584  $onadb,
585  'vlans',
586  array('vlan_campus_id' => $campus['id']),
587  '',
588  0
589  );
590  }
591 
592 
593  //
594  // *** BUILD RESULTS HTML ***
595  //
596 
597  $html .= <<<EOL
598 <table style="cursor: pointer;" width="100%" cellspacing="0" border="0" cellpadding="0">
599  <!-- The "None" option.. they need to be able to de-select a vlan -->
600  <tr onMouseOver="this.className='row-highlight';"
601  onMouseOut="this.className='row-normal';"
602  onClick="el('{$form['input_id']}').value = ''; el('{$form['text_id']}').innerHTML = 'None'; removeElement('{$form['id']}');">
603  <td style="font-size: 10px; padding: 0px 2px;">None (i.e. no vlan)</td>
604  </tr>
605 EOL;
606  if ($count > $form['max_results']) {
607  $html .= <<<EOL
608 <tr><td style="cursor: default; font-size: 12px; background-color: #FFCCCC; color: 000;" colspan="5" align="center">Displaying {$form['max_results']} of {$count} results</td></tr>
609 EOL;
610  }
611 
612  $campus['name'] = htmlentities($campus['name'], ENT_QUOTES, $conf['php_charset']);
613  foreach ($results as $record) {
614  // Escape data for display in html
615  foreach(array_keys($record) as $key) { $record[$key] = htmlentities($record[$key], ENT_QUOTES, $conf['php_charset']); }
616  $html .= <<<EOL
617 <tr onMouseOver="this.className='row-highlight';"
618  onMouseOut="this.className='row-normal';"
619  onClick="el('{$form['input_id']}').value = '{$record['number']}'; el('{$form['text_id']}').innerHTML = '{$campus['name']} / {$record['name']}'; removeElement('{$form['id']}');">
620  <td style="font-size: 10px; padding: 0px 2px;">{$campus['name']} / [{$record['number']}] {$record['name']}</td>
621 </tr>
622 EOL;
623  }
624 
625  $html .= <<<EOL
626 </table>
627 EOL;
628 
629  $js .= <<<EOL
630  el('{$form['content_id']}').style.display = 'block';
631  /* Reposition the popup if the new content is displayed off the screen */
632  wwTT_position('{$form['id']}');
633 EOL;
634 
635 
636  // Insert the new html into the window
637  // Instantiate the xajaxResponse object
638  $response = new xajaxResponse();
639  $response->addAssign($form['content_id'], "innerHTML", $html);
640  if ($js) { $response->addScript($js); }
641  return($response->getXML());
642 }
643 
644 
645 
646 
647 
648 
649 
650 
651 
652 
653 
654 
655 ?>
ip_mangle
ip_mangle($ip="", $format="default")
Definition: functions_general.inc.php:308
ws_subnet
ws_subnet($window_name, $form='')
Definition: search_results_qf.inc.php:17
$record
$record['display_name']
Definition: app_advanced_search.inc.php:12
ona_get_vlan_campus_record
ona_get_vlan_campus_record($array)
Definition: functions_db.inc.php:1254
$usage_html
$usage_html
Definition: main.inc.php:6
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_vlan
ws_vlan($window_name, $form='')
Definition: search_results_qf.inc.php:542
ip_complete
ip_complete($ip='', $filler=0)
Definition: functions_general.inc.php:1044
get_subnet_usage_html
get_subnet_usage_html($subnet_id, $width=30, $height=8)
Definition: functions_gui.inc.php:294
$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
ws_free_ip
ws_free_ip($window_name, $form='')
Definition: search_results_qf.inc.php:204
ona_get_subnet_record
ona_get_subnet_record($array)
Definition: functions_db.inc.php:1226
ws_location
ws_location($window_name, $form='')
Definition: search_results_qf.inc.php:370
$color
if($_SERVER['SERVER_PORT']==443) $color['bg']
Definition: config.inc.php:119
parse_options_string
parse_options_string($input)
Definition: xajax_setup.inc.php:88