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)  

edit_subnet.inc.php
Go to the documentation of this file.
1 <?php
2 
3 
4 
6 // Function: ws_editor()
7 //
8 // Description:
9 // Displays a form for creating/editing a subnet record.
10 // If $form is a valid subnet_id, it is used to display an existing
11 // record for editing. "Save" button calls the ws_save() function.
13 function ws_editor($window_name, $form='') {
14  global $conf, $self, $onadb;
16  $window = array();
17 
18  // Check permissions
19  if (! (auth('subnet_modify') and auth('subnet_add')) ) {
20  $response = new xajaxResponse();
21  $response->addScript("alert('Permission denied!');");
22  return($response->getXML());
23  }
24 
25  // If the user supplied an array in a string, build the array and store it in $form
26  $form = parse_options_string($form);
27 
28  // If $form is a number, it's an record ID- so we transform $form into an array
29  if (is_numeric($form)) $form = array('subnet_id' => $form);
30  $subnet = array();
31 
32  // Load an existing record (and associated info) if $form is an id
33  if (is_numeric($form['subnet_id'])) {
34  list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $form['subnet_id']));
35  if ($rows) {
36  if (strlen($subnet['ip_addr']) > 11) {
37  $subnet['ip_mask'] = '/'.ip_mangle($subnet['ip_mask'], 'cidr');
38  } else {
39  $subnet['ip_mask'] = ip_mangle($subnet['ip_mask'], 'dotted');
40  }
41  $subnet['ip_addr'] = ip_mangle($subnet['ip_addr'], 'dotted');
42 
43  // Vlan Record
44  list($status, $rows, $vlan) = ona_get_vlan_record(array('id' => $subnet['vlan_id']));
45  $subnet['vlan_desc'] = $vlan['vlan_campus_name'] . ' / ' . $vlan['name'];
46  }
47  }
48  // If there is no subnet id in the form but we are passing in data, clean it up
49  else {
50  if (strlen($form['ip_addr']) > 1) $subnet['ip_addr'] = ip_mangle($form['ip_addr'], 'dotted');
51  if (strlen($form['ip_mask']) > 1) $subnet['ip_mask'] = ip_mangle($form['ip_mask'], 'dotted');
52  if (strlen($form['name']) > 1) $subnet['name'] = $form['name'];
53  }
54 
55  if (!$subnet['vlan_id']) $subnet['vlan_desc'] = 'None';
56 
57 
58  // Escape data for display in html
59  foreach(array_keys((array)$subnet) as $key) { $subnet[$key] = htmlentities($subnet[$key], ENT_QUOTES, $conf['php_charset']); }
60 
61  // Set the window title:
62  $window['title'] = "Add Subnet";
63  if ($subnet['id']) $window['title'] = "Edit Subnet";
64 
65 
66  // Build subnet type list
67  list($status, $rows, $subnettypes) = db_get_records($onadb, 'subnet_types', 'id > 0', 'display_name');
68  $subnet_type_list = '<option value="">&nbsp;</option>\n';
69  $subnettypes['subnet_type_name'] = htmlentities($subnettypes['display_name']);
70  foreach ($subnettypes as $record) {
71  $selected = "";
72  if ($record['id'] == $subnet['subnet_type_id']) { $selected = "SELECTED=\"selected\""; }
73  if ($record['id']) {$subnet_type_list .= "<option {$selected} value=\"{$record['id']}\">{$record['display_name']}</option>\n";}
74  }
75 
76 
77 
78  // Javascript to run after the window is built
79  $window['js'] = <<<EOL
80  /* Put a minimize icon in the title bar */
81  el('{$window_name}_title_r').innerHTML =
82  '&nbsp;<a onClick="toggle_window(\'{$window_name}\');" title="Minimize window" style="cursor: pointer;"><img src="{$images}/icon_minimize.gif" border="0" /></a>' +
83  el('{$window_name}_title_r').innerHTML;
84 
85  /* Put a help icon in the title bar */
86  el('{$window_name}_title_r').innerHTML =
87  '&nbsp;<a href="{$_ENV['help_url']}{$window_name}" target="null" title="Help" style="cursor: pointer;"><img src="{$images}/silk/help.png" border="0" /></a>' +
88  el('{$window_name}_title_r').innerHTML;
89 
90  el('{$window_name}_edit_form').onsubmit = function() { return false; };
91 
92 
93  /* Setup the Quick Find VLAN icon */
94  var _button = el('qf_vlan_{$window_name}');
95  _button.style.cursor = 'pointer';
96  _button.onclick =
97  function(ev) {
98  if (!ev) ev = event;
99  /* Create the popup div */
100  wwTT(this, ev,
101  'id', 'tt_qf_vlan_{$window_name}',
102  'type', 'static',
103  'direction', 'south',
104  'delay', 0,
105  'styleClass', 'wwTT_qf',
106  'javascript',
107  "xajax_window_submit('tooltips', '" +
108  "tooltip=>qf_vlan," +
109  "id=>tt_qf_vlan_{$window_name}," +
110  "text_id=>vlan_text_{$window_name}," +
111  "input_id=>set_vlan_{$window_name}');"
112  );
113  };
114 
115  suggest_setup('masks_{$window_name}', 'suggest_masks_{$window_name}');
116 
117  el('set_name').focus();
118 
119 EOL;
120 
121  // Define the window's inner html
122  $window['html'] = <<<EOL
123 
124  <!-- Subnet Edit Form -->
125  <form id="{$window_name}_edit_form" onSubmit="return false;">
126  <input type="hidden" name="subnet" value="{$subnet['id']}">
127  <input type="hidden" name="js" value="{$form['js']}">
128  <table cellspacing="0" border="0" cellpadding="0" style="background-color: {$color['window_content_bg']}; padding-left: 20px; padding-right: 20px; padding-top: 5px; padding-bottom: 5px;">
129 
130  <!-- SUBNET RECORD -->
131  <tr>
132  <td align="left" nowrap="true"><b><u>Subnet Record</u></b>&nbsp;</td>
133  <td class="padding" align="left" width="100%">&nbsp;</td>
134  </tr>
135 
136  <tr>
137  <td align="right" nowrap="true">
138  VLAN
139  </td>
140  <td class="padding" align="left" width="100%" nowrap="true">
141  <input
142  type="hidden"
143  id="set_vlan_{$window_name}"
144  name="set_vlan"
145  value="{$vlan['number']}">
146  <input
147  type="hidden"
148  id="vlan_campus_{$window_name}"
149  name="campus"
150  value="{$vlan['vlan_campus_id']}">
151 
152  <span id="qf_vlan_{$window_name}">
153  <a id="vlan_text_{$window_name}"
154  class="nav"
155  >{$subnet['vlan_desc']}</a>
156  <img src="{$images}/silk/find.png" border="0"
157  /></span>
158  </td>
159  </tr>
160 
161  <tr>
162  <td class="input_required" align="right" nowrap="true">
163  Name
164  </td>
165  <td class="padding" align="left" width="100%">
166  <input
167  id="set_name"
168  name="set_name"
169  alt="Subnet name"
170  value="{$subnet['name']}"
171  class="edit"
172  type="text"
173  size="35" maxlength="100"
174  >
175  </td>
176  </tr>
177 
178  <tr>
179  <td class="input_required" align="right" nowrap="true">
180  Subnet type
181  </td>
182  <td class="padding" align="left" width="100%">
183  <select name="set_type" class="edit" accesskey="t">
185  </select>
186  </td>
187  </tr>
188 
189  <tr>
190  <td class="input_required" align="right" nowrap="true">
191  IP Address
192  </td>
193  <td class="padding" align="left" width="100%">
194  <input
195  name="set_ip"
196  alt="IP Address"
197  value="{$subnet['ip_addr']}"
198  class="edit"
199  type="text"
200  size="35" maxlength="40"
201  >
202  </td>
203  </tr>
204 
205  <tr>
206  <td class="input_required" align="right" nowrap="true">
207  Netmask
208  </td>
209  <td class="padding" align="left" width="100%">
210  <input
211  id="masks_{$window_name}"
212  name="set_netmask"
213  alt="Netmask (i.e. 255.255.255.0 or /24)"
214  value="{$subnet['ip_mask']}"
215  class="edit"
216  type="text"
217  size="35" maxlength="17"
218  >
219  <div id="suggest_masks_{$window_name}" class="suggest"></div>
220  </td>
221  </tr>
222 
223 EOL;
224 
225 // Show a "keep adding" checkbox if they are adding records
226 if (!isset($subnet['id'])) {
227  $window['html'] .= <<<EOL
228  <tr>
229  <td align="right" nowrap="true">
230  &nbsp;
231  </td>
232  <td class="padding" align="left" width="100%">
233  <input
234  name="keepadding"
235  alt="Keep adding more subnets"
236  type="checkbox"
237  > Keep adding more subnets
238  </td>
239  </tr>
240 
241  <tr>
242  <td colspan="2" class="padding" align="center" width="100%">
243  <span id="statusinfo_{$window_name}" style="color: green;" ></span>
244  </td>
245  </tr>
246 
247 EOL;
248 }
249  $window['html'] .= <<<EOL
250 
251  <tr>
252  <td align="right" valign="top" nowrap="true">
253  &nbsp;
254  </td>
255  <td class="padding" align="right" width="100%">
256  <input class="edit" type="button" name="cancel" value="Cancel" onClick="removeElement('{$window_name}');">
257  <input class="edit" type="button"
258  name="submit"
259  value="Save"
260  accesskey=" "
261  onClick="xajax_window_submit('{$window_name}', xajax.getFormValues('{$window_name}_edit_form'), 'save');"
262  >
263  </td>
264  </tr>
265 
266  </table>
267  </form>
268 EOL;
269 
270  return(window_open($window_name, $window));
271 }
272 
273 
274 
275 
276 
277 
278 
279 
281 // Function:
282 // Save Form
283 //
284 // Description:
285 // Creates/updates a subnet record.
287 function ws_save($window_name, $form='') {
288  global $include, $conf, $self, $onadb;
289 
290  // Check permissions
291  if (! (auth('subnet_modify') and auth('subnet_add')) ) {
292  $response = new xajaxResponse();
293  $response->addScript("alert('Permission denied!');");
294  return($response->getXML());
295  }
296 
297  // Instantiate the xajaxResponse object
298  $response = new xajaxResponse();
299  $js = '';
300 
301  // Validate input
302  if ($form['set_name'] == '' or
303  $form['set_type'] == '' or
304  $form['set_ip'] == '' or
305  $form['set_netmask'] == ''
306  ) {
307  $response->addScript("alert('Please complete all fields to continue!');");
308  return($response->getXML());
309  }
310 
311  // Make sure the IP address specified is valid
312  $form['set_ip'] = ip_mangle($form['set_ip'], 'dotted');
313  if ($form['set_ip'] == -1) {
314  $response->addScript("alert('{$self['error']}');");
315  return($response->getXML());
316  }
317  // Make sure the netmask specified is valid
318  $form['set_netmask'] = ip_mangle($form['set_netmask'], 'cidr');
319  if ($form['set_netmask'] == -1) {
320  $self['error'] = preg_replace('/IP address/i', 'netmask', $self['error']);
321  $response->addScript("alert('{$self['error']}');");
322  return($response->getXML());
323  }
324 
325  // Before we go on, we must alert the user if this new subnet would require a new PTR zone.
326 // $ipflip = ip_mangle($form['set_ip'],'flip');
327 // $octets = explode(".",$ipflip);
328 // // Find a pointer zone for this ip to associate with.
329 // list($status, $rows, $ptrdomain) = ona_find_domain($ipflip.".in-addr.arpa");
330 // if (!$ptrdomain['id']) {
331 // $self['error'] = "ERROR => This subnet is the first in the {$octets[3]}.0.0.0 class A range. You must first create at least the following DNS domain: {$octets[3]}.in-addr.arpa\\n\\nSelect OK to create new DNS domain now.";
332 // $response->addScript("var doit=confirm('{$self['error']}');if (doit == true) {xajax_window_submit('edit_domain', 'newptrdomainname=>{$octets[3]}.in-addr.arpa', 'editor');} else {removeElement('{$window_name}');}");
333 // return($response->getXML());
334 // }
335 
336 
337  // Decide if we're editing or adding
338  $module = 'modify';
339  // If we're adding, re-map some the array names to match what the "add" module wants
340  if (!$form['subnet']) {
341  $module = 'add';
342 
343  // If there's no "refresh" javascript, add a command to view the new subnet
344  if (!preg_match('/\w/', $form['js'])) $form['js'] = "xajax_window_submit('work_space', 'xajax_window_submit(\'display_subnet\', \'ip=>{$form['set_ip']}\', \'display\')');";
345 
346  // Remap some of the option names for the subnet_add() module
347  $form['name'] = $form['set_name']; unset($form['set_name']);
348  $form['type'] = $form['set_type']; unset($form['set_type']);
349  $form['ip'] = $form['set_ip']; unset($form['set_ip']);
350  $form['netmask'] = $form['set_netmask']; unset($form['set_netmask']);
351  $form['vlan'] = $form['set_vlan']; unset($form['set_vlan']);
352  }
353 
354  // Run the module to ADD or MODIFY the SUBNET.
355  list($status, $output) = run_module('subnet_'.$module, $form);
356 
357  // If the module returned an error code display a popup warning
358  if ($status)
359  $js .= "alert('Save failed.\\n". preg_replace('/[\s\']+/', ' ', $self['error']) . "');";
360  else {
361  // Update the status to tell them what they just did if they just *added* a subnet and the "keep adding" box is checked.
362  // Otherwise just close the edit window.
363  if ($form['keepadding'] and $module == 'add')
364  $js .= "el('statusinfo_{$window_name}').innerHTML = 'Previously added: {$form['name']}';";
365  else
366  $js .= "removeElement('{$window_name}');";
367 
368  // If there is "refresh" javascript, send it to the browser to execute
369  // MP: FIXME.. there is an issue that if you add a new subnet, then imidiately modify its IP the JS refresh uses the old ip and fails. find out why
370  if ($form['js']) $js .= $form['js'];
371  }
372 
373  // Insert the new table into the window
374  $response->addScript($js);
375  return($response->getXML());
376 }
377 
378 
379 
380 
381 
382 
383 
385 // Function:
386 // Delete Form
387 //
388 // Description:
389 // Deletes a subnet. $form should be an array with a 'subnet_id'
390 // key defined and optionally a 'js' key with javascript to have the
391 // browser run after a successful delete.
393 function ws_delete($window_name, $form='') {
394  global $include, $conf, $self, $onadb;
395 
396  // Check permissions
397  if (!auth('subnet_del')) {
398  $response = new xajaxResponse();
399  $response->addScript("alert('Permission denied!');");
400  return($response->getXML());
401  }
402 
403  // If an array in a string was provided, build the array and store it in $form
404  $form = parse_options_string($form);
405 
406  // Instantiate the xajaxResponse object
407  $response = new xajaxResponse();
408  $js = '';
409 
410  // Run the module
411  list($status, $output) = run_module('subnet_del', array('subnet' => $form['subnet_id'], 'commit' => $form['commit']));
412 
413  // If commit was N, display the confirmation dialog box
414  if (!$form['commit']) {
415  $build_commit_html = 1;
416  $commit_function = 'delete';
417  include(window_find_include('module_results'));
418  return(window_open("{$window_name}_results", $window));
419  }
420 
421  // If the module returned an error code display a popup warning
422  if ($status)
423  $js .= "alert('Delete failed. " . preg_replace('/[\s\']+/', ' ', $self['error']) . "');";
424  else if ($form['js'])
425  $js .= $form['js']; // usually js will refresh the window we got called from
426 
427  // Return an XML response
428  $response->addScript($js);
429  return($response->getXML());
430 }
431 
432 
433 
434 
435 
436 ?>
Name
Full Name
Definition: app_user_info.inc.php:101
ip_mangle
ip_mangle($ip="", $format="default")
Definition: functions_general.inc.php:308
window_find_include
window_find_include($window_name)
Definition: webwin.inc.php:204
$record
$record['display_name']
Definition: app_advanced_search.inc.php:12
ws_editor
ws_editor($window_name, $form='')
Definition: edit_subnet.inc.php:13
ws_save
ws_save($window_name, $form='')
Definition: edit_subnet.inc.php:287
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_delete
ws_delete($window_name, $form='')
Definition: edit_subnet.inc.php:393
$window
$window['title']
Definition: install.php:26
_button
_button
Definition: app_advanced_search.inc.php:48
$include
$include
Definition: 2-to-3.php:10
window_open
window_open($window_name, $window=array())
Definition: webwin.inc.php:59
run_module
run_module($module='', $options='', $transaction=1)
Definition: functions_general.inc.php:1468
$images
$images
Definition: config.inc.php:26
ona_get_vlan_record
ona_get_vlan_record($array)
Definition: functions_db.inc.php:1241
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
$subnet_type_list
$subnet_type_list
Definition: app_advanced_search.inc.php:11
$style
$style['font-family']
Definition: config.inc.php:158
nbsp
& nbsp
Definition: install.php:36
$font_family
global $font_family
Definition: app_admin_tools.inc.php:4
$output
$output
Definition: dcm.php:16
$self
global $self
Definition: 2-to-3.php:15
auth
auth($resource, $msg_level=1)
Definition: functions_general.inc.php:1330
ona_get_subnet_record
ona_get_subnet_record($array)
Definition: functions_db.inc.php:1226
$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