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)  

vlan.inc.php
Go to the documentation of this file.
1 <?php
2 // DON'T put whitespace at the beginning or end of this file!!!
3 
4 
5 
7 // Function: vlan_add (string $options='')
8 //
9 // $options = key=value pairs of options for this function.
10 // multiple sets of key=value pairs should be separated
11 // by an "&" symbol.
12 //
13 // Input Options:
14 // campus=STRING
15 // name=STRING
16 // number=NUMBER
17 //
18 // Output:
19 // Adds an vlan into the database called 'name' that is part of
20 // the campus 'campus'.
21 // Returns a two part list:
22 // 1. The exit status of the function. 0 on success, non-zero on
23 // error. All errors messages are stored in $self['error'].
24 // 2. A textual message for display on the console or web interface.
25 //
26 // Example: list($status, $result) = vlan_add('campus=test&name=blah&number=1');
28 function vlan_add($options="") {
29 
30  // The important globals
31  global $conf, $self, $onadb;
32 
33  // Version - UPDATE on every edit!
34  $version = '1.01';
35 
36  printmsg("DEBUG => vlan_add({$options}) called", 3);
37 
38  // Parse incoming options string to an array
39  $options = parse_options($options);
40 
41  // Return the usage summary if we need to
42  if ($options['help'] or !($options['campus'] and $options['name'] and $options['number']) ) {
43  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
44  $self['error'] = 'ERROR => Insufficient parameters';
45  return(array(1,
46 <<<EOM
47 
49 Adds a vlan into the database assigned to the specified campus
50 
51  Synopsis: vlan_add [KEY=VALUE] ...
52 
53  Required:
54  campus=STRING|ID Campus name or row ID
55  name=STRING Name of new VLAN
56  number=NUMBER VLAN number to be assigned
57 
58 \n
59 EOM
60 
61  ));
62  }
63 
64 
65  // The formatting rule on vlan names/campus names is all upper and trim it, spaces to -
66  $options['name'] = strtoupper(trim($options['name']));
67  $options['name'] = preg_replace('/\s+/', '-', $options['name']);
68  $options['campus'] = strtoupper(trim($options['campus']));
69  $options['number'] = trim($options['number']);
70 
71  if (is_numeric($options['campus']))
72  list($status, $rows, $campus) = ona_get_vlan_campus_record(array('id' => $options['campus']));
73  if(!$rows)
74  list($status, $rows, $campus) = ona_get_vlan_campus_record(array('name' => $options['campus']));
75 
76  if ($status or !$rows) {
77  $self['error'] = "ERROR => Unable to find campus";
78  printmsg($self['error'], 0);
79  return(array(6, $self['error'] . "\n"));
80  }
81 
82  // Debugging
83  printmsg("DEBUG => Using VLAN campus: {$campus['name']}", 3);
84 
85  // check that the number option is a number
86  if (!is_numeric($options['number'])) {
87  printmsg("DEBUG => The VLAN number ({$options['number']}) must be numeric!",3);
88  $self['error'] = "ERROR => The VLAN number ({$options['number']}) must be numeric!";
89  return(array(3, $self['error'] . "\n"));
90  }
91 
92  // Validate that there isn't already an vlan on this campus with this vlan number
93  list($status, $rows, $record) = ona_get_vlan_record(array('vlan_campus_id' => $campus['id'],
94  'number' => $options['number']));
95  if ($status or $rows) {
96  printmsg("DEBUG => The vlan campus ({$campus['name']}) already has a vlan with the number ({$options['number']})!",3);
97  $self['error'] = "ERROR => The vlan campus {$campus['name']} already has a vlan with the number {$options['number']}!";
98  return(array(3, $self['error'] . "\n"));
99  }
100 
101 
102  // Validate that there isn't already an vlan
103  list($v_status, $v_rows, $v_record) = ona_get_vlan_record(array('vlan_campus_id' => $campus['id'],
104  'name' => $options['name']));
105  if ($v_status or $v_rows) {
106  printmsg("DEBUG => The vlan ({$options['name']}) already exists on campus ({$campus['name']})!",3);
107  $self['error'] = "ERROR => The vlan {$options['name']} already exists on campus {$campus['name']}!";
108  return(array(3, $self['error'] . "\n"));
109  }
110 
111 
112  // Check permissions
113  if (!auth('vlan_add')) {
114  $self['error'] = "Permission denied!";
115  printmsg($self['error'], 0);
116  return(array(10, $self['error'] . "\n"));
117  }
118 
119  // Get the next ID for the new vlan
120  $id = ona_get_next_id('vlans');
121  if (!$id) {
122  $self['error'] = "ERROR => The ona_get_next_id() call failed!";
123  printmsg($self['error'],0);
124  return(array(5, $self['error'] . "\n"));
125  }
126  printmsg("DEBUG => ID for new VLAN: $id", 3);
127 
128  // Add the vlan
129  list($status, $rows) =
131  $onadb,
132  'vlans',
133  array(
134  'id' => $id,
135  'number' => $options['number'],
136  'name' => $options['name'],
137  'vlan_campus_id' => $campus['id']
138  )
139  );
140  if ($status or !$rows) {
141  $self['error'] = "ERROR => vlan_add() SQL Query failed: " . $self['error'];
142  printmsg($self['error'], 0);
143  return(array(6, $self['error'] . "\n"));
144  }
145 
146  // Return the success notice
147  $self['error'] = "INFO => VLAN ADDED: {$options['name']} to {$campus['name']}.";
148  printmsg($self['error'],0);
149  return(array(0, $self['error'] . "\n"));
150 }
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
164 // Function: vlan_del (string $options='')
165 //
166 // $options = key=value pairs of options for this function.
167 // multiple sets of key=value pairs should be separated
168 // by an "&" symbol.
169 //
170 // Input Options:
171 // vlan=ID
172 //
173 // Output:
174 // Deletes an vlan from the database.
175 // Returns a two part list:
176 // 1. The exit status of the function. 0 on success, non-zero on
177 // error. All errors messages are stored in $self['error'].
178 // 2. A textual message for display on the console or web interface.
179 //
180 // Example: list($status, $result) = vlan_add('vlan=19328');
182 function vlan_del($options="") {
183 
184  // The important globals
185  global $conf, $self, $onadb;
186 
187  // Version - UPDATE on every edit!
188  $version = '1.00';
189 
190  printmsg("DEBUG => vlan_del({$options}) called", 3);
191 
192  // Parse incoming options string to an array
193  $options = parse_options($options);
194 
195  // Return the usage summary if we need to
196  if ($options['help'] or !$options['vlan'] ) {
197  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
198  $self['error'] = 'ERROR => Insufficient parameters';
199  return(array(1,
200 <<<EOM
201 
203 Deletes an vlan from the database
204 
205  Synopsis: vlan_del [KEY=VALUE] ...
206 
207  Required:
208  vlan=ID ID of the vlan to delete
209 
210  Optional:
211  commit=[yes|no] commit db transaction (no)
212 
213 \n
214 EOM
215 
216  ));
217  }
218 
219 
220  // Sanitize options[commit] (default is no)
221  $options['commit'] = sanitize_YN($options['commit'], 'N');
222 
223  // If the vlan provided is numeric, check to see if it's an vlan
224  if (is_numeric($options['vlan'])) {
225 
226  // See if it's an vlan_id
227  list($status, $rows, $vlan) = ona_get_vlan_record(array('id' => $options['vlan']));
228  }
229 
230  if (!$vlan['id']) {
231  printmsg("DEBUG => Unable to find VLAN ({$options['vlan']})!",3);
232  $self['error'] = "ERROR => Unable to find VLAN ({$options['vlan']})!";
233  return(array(2, $self['error'] . "\n"));
234  }
235 
236 
237  list($status, $rows, $network) = db_get_records($onadb, 'subnets', array('vlan_id' => $vlan['id']), '' ,0);
238  if ($rows != 0) {
239  printmsg("DEBUG => This VLAN ({$vlan['name']}) is in use by {$rows} network(s)!",3);
240  $self['error'] = "ERROR => This VLAN ({$vlan['name']}) is in use by {$rows} network(s)!";
241  return(array(6, $self['error'] . "\n" .
242  "INFO => Please dis-associate those networks from this vlan before deleting.\n"));
243  }
244 
245  // If "commit" is yes, delete the record
246  if ($options['commit'] == 'Y') {
247 
248  // Check permissions
249  if (!auth('vlan_del')) {
250  $self['error'] = "Permission denied!";
251  printmsg($self['error'], 0);
252  return(array(10, $self['error'] . "\n"));
253  }
254 
255  list($status, $rows) = db_delete_records($onadb, 'vlans', array('id' => $vlan['id']));
256  if ($status or !$rows) {
257  $self['error'] = "ERROR => vlan_del() SQL Query failed: " . $self['error'];
258  printmsg($self['error'], 0);
259  return(array(4, $self['error'] . "\n"));
260  }
261 
262  // Return the success notice
263  $self['error'] = "INFO => VLAN DELETED: {$vlan['name']}";
264  printmsg($self['error'],0);
265  return(array(0, $self['error'] . "\n"));
266  }
267 
268 
269  list($status, $rows, $campus) = ona_get_vlan_campus_record(array('id' => $vlan['vlan_campus_id']));
270 
271  // Otherwise display the record that would have been deleted
272  $text = <<<EOL
273 Record(s) NOT DELETED (see "commit" option)
274 Displaying record(s) that would have been deleted:
275 
276  NAME: {$vlan['name']}
277  NUMBER: {$vlan['number']}
278  CAMPUS: {$campus['name']}
279 
280 
281 EOL;
282 
283  return(array(6, $text));
284 
285 }
286 
287 
288 
289 
290 
291 
292 
293 
294 
296 // Function: vlan_modify (string $options='')
297 //
298 // $options = key=value pairs of options for this function.
299 // multiple sets of key=value pairs should be separated
300 // by an "&" symbol.
301 //
302 // Input Options:
303 // vlan=ID vlan ID
304 // set_name=NAME change vlan name
305 // set_number=NUMBER change vlan number
306 // set_campus=NAME or ID change campus the vlan belongs to
307 //
308 // Output:
309 // Updates a vlan record in the IP database.
310 // Returns a two part list:
311 // 1. The exit status of the function. 0 on success, non-zero on
312 // error. All errors messages are stored in $self['error'].
313 // 2. A textual message for display on the console or web interface.
314 //
315 // Example: list($status, $result) = vlan_modify('vlan=23452&set_name=default');
317 function vlan_modify($options="") {
318 
319  // The important globals
320  global $conf, $self, $onadb;
321 
322  // Version - UPDATE on every edit!
323  $version = '1.01';
324 
325  printmsg("DEBUG => vlan_modify({$options}) called", 3);
326 
327  // Parse incoming options string to an array
328  $options = parse_options($options);
329 
330  // Return the usage summary if we need to
331  if ($options['help'] or (!$options['vlan']) or !($options['set_name'] or $options['set_number'] or $options['set_campus']) ) {
332  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
333  $self['error'] = 'ERROR => Insufficient parameters';
334  return(array(1,
335 <<<EOM
336 
338 Modifies an vlan entry in the database
339 
340  Synopsis: vlan_modify [KEY=VALUE] ...
341 
342  Where:
343  vlan=ID vlan ID
344 
345  Update:
346  set_name=NAME change vlan name
347  set_number=NUMBER change vlan number
348  set_campus=NAME or ID change campus the vlan belongs to
349 
350 \n
351 EOM
352  ));
353  }
354 
355 
356  // The formatting rule on vlan names/campus names is all upper and trim it, spaces to -
357  $options['set_name'] = strtoupper(trim($options['set_name']));
358  $options['set_name'] = preg_replace('/\s+/', '-', $options['set_name']);
359  $options['set_campus'] = strtoupper(trim($options['set_campus']));
360  $options['set_number'] = trim($options['set_number']);
361 
362  // Load the record we will be modifying
363  list($status, $rows, $vlan) = ona_get_vlan_record(array('id' => $options['vlan']));
364 
365  // Validate that we got a record back, or return an error
366  if (!$vlan['id']) {
367  printmsg("DEBUG => The VLAN ID specified ({$options['vlan']}) does not exist!",3);
368  $self['error'] = "ERROR => The VLAN ID specified, {$options['vlan']}, does not exist!";
369  return(array(2, $self['error'] . "\n"));
370  }
371 
372  printmsg("DEBUG => Found VLAN: {$vlan['name']}", 3);
373 
374 
375  // This variable will contain the updated info we'll insert into the DB
376  $SET = array();
377 
378 
379  // If they are specifying a new name, process it.
380  if ($options['set_name']) {
381  // Validate that there isn't already an vlan with this name
382  $options['set_name'] = strtoupper(trim($options['set_name']));
383 
384  list($status, $rows, $record) = db_get_records($onadb, 'vlans', "vlan_campus_id = {$vlan['vlan_campus_id']} AND name LIKE '{$options['set_name']}' AND number != {$vlan['number']}");
385 
386  if ($status or $rows) {
387  printmsg("DEBUG => The VLAN ({$options['set_name']}) already exists on this campus!",3);
388  $self['error'] = "ERROR => The VLAN {$options['set_name']} already exists on this campus!";
389  return(array(4, $self['error'] . "\n"));
390  }
391  if ($vlan['name'] != $options['set_name']) $SET['name'] = $options['set_name'];
392  }
393 
394  if ($options['set_number']) {
395  // Validate that there isn't already an vlan on this campus with this vlan number
396  list($status, $rows, $record) = db_get_records($onadb, 'vlans', "vlan_campus_id = {$vlan['vlan_campus_id']} AND number = {$options['set_number']} AND name NOT LIKE '{$vlan['name']}'");
397  if ($status or $rows) {
398  printmsg("DEBUG => The VLAN with the number ({$options['set_number']}) already exists on this campus!",3);
399  $self['error'] = "ERROR => The vlan with the number {$options['set_number']} already exists on this campus!";
400  return(array(3, $self['error'] . "\n"));
401  }
402  // Add the new info to $SET
403  if ($vlan['number'] != $options['set_number']) $SET['number'] = $options['set_number'];
404  }
405 
406  // FIXME: yes I'm lazy.. test that the new campus does not have the vlan name or number already on it.
407 
408  // If they are changing the campus the vlan points to, process it
409  if ($options['set_campus']) {
410  $options['set_campus'] = strtoupper(trim($options['set_campus']));
411 
412  if (is_numeric($options['set_campus']))
413  list($status, $rows, $record) = ona_get_vlan_campus_record(array('id' => $options['set_campus']));
414  if( !array_key_exists('id',$record) )
415  list($status, $rows, $record) = ona_get_vlan_campus_record(array('name' => $options['set_campus']));
416 
417  // Make sure that worked - or return an error
418  if (!$record['id']) {
419  printmsg("DEBUG => The campus ({$options['set_campus']}) does not exist!",3);
420  $self['error'] = "ERROR => The campus specified, {$options['set_campus']}, does not exist!";
421  return(array(5, $self['error'] . "\n"));
422  }
423 
424  // test that the new campus does not have the vlan name or number already on it.
425  // only check if the campus has changed
426  if($record['id'] != $vlan['vlan_campus_id']) {
427 
428  // build where clause for checking the new campus for the vlan name/number
429  $where='';
430  $OR='';
431  if(array_key_exists('number',$SET)) {
432  $where .= " number = {$SET['number']} ";
433  $OR=" OR ";
434  }
435  if(array_key_exists('name',$SET))
436  $where .= "{$OR} name LIKE '{$SET['name']}' ";
437 
438  list($status, $rows, $new_campus_record) = db_get_records($onadb, 'vlans', "vlan_campus_id = {$record['id']} AND ({$where})");
439  if($rows > 0) {
440  printmsg("DEBUG => The campus ({$options['set_campus']}) already contains this VLAN name or number ({$SET['name']} {$SET['number']})!",3);
441  $self['error'] = "ERROR => The campus specified, {$options['set_campus']}, already contains this VLAN name or number ({$SET['name']} {$SET['number']})!";
442  return(array(7, $self['error'] . "\n"));
443  }
444  }
445 
446  // Add the new info to $SET
447  if ($vlan['vlan_campus_id'] != $record['id']) $SET['vlan_campus_id'] = $record['id'];
448  }
449 
450  // Check permissions
451  if (!auth('vlan_modify')) {
452  $self['error'] = "Permission denied!";
453  printmsg($self['error'], 0);
454  return(array(10, $self['error'] . "\n"));
455  }
456 
457 
458 
459 
460  if ($SET) {
461  // Update the record
462  list($status, $rows) = db_update_record($onadb, 'vlans', array('id' => $vlan['id']), $SET);
463  if ($status or !$rows) {
464  $self['error'] = "ERROR => vlan_modify() SQL Query failed: " . $self['error'];
465  printmsg($self['error'], 0);
466  return(array(6, $self['error'] . "\n"));
467  }
468  }
469 
470  // Get the VLAN record after updating (logging)
471  list($status, $rows, $new_vlan) = ona_get_vlan_record(array('id' => $options['vlan']));
472  list($status, $rows, $campus) = ona_get_vlan_campus_record(array('id' => $new_vlan['vlan_campus_id']));
473 
474 $text = <<<EOL
475 
476  NAME: {$new_vlan['name']}
477  NUMBER: {$new_vlan['number']}
478  CAMPUS: {$campus['name']}
479 
480 
481 EOL;
482  // Return the success notice
483  $renamed = '';
484  if($new_vlan['name'] != $vlan['name']) $renamed .= "{$vlan['name']} => {$new_vlan['name']} ";
485  if($new_vlan['number'] != $vlan['number']) $renamed .= "VLAN Num {$vlan['number']} => {$new_vlan['number']} ";
486  if($new_vlan['vlan_campus_id'] != $vlan['vlan_campus_id']) $renamed .= "Campus ID {$vlan['vlan_campus_id']} => {$new_vlan['vlan_campus_id']}";
487  $self['error'] = "INFO => VLAN UPDATED: {$renamed}";
488 
489 
490 
491  return(array(0, $self['error'] . "\n {$text}"));
492 }
493 
494 
495 
496 
497 
498 
499 
500 
501 
502 
503 
504 
505 
506 
507 // DON'T put whitespace at the beginning or end of this file!!!
508 ?>
db_insert_record
db_insert_record($dbh=0, $table="", $insert="")
Definition: functions_db.inc.php:375
sanitize_YN
sanitize_YN($string="", $default="Y")
Definition: functions_general.inc.php:1637
ona_get_next_id
ona_get_next_id($tablename)
Definition: functions_db.inc.php:1369
Name
Full Name
Definition: app_user_info.inc.php:101
$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
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
db_delete_records
db_delete_records($dbh=0, $table="", $where="")
Definition: functions_db.inc.php:582
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
db_update_record
db_update_record($dbh=0, $table="", $where="", $insert="")
Definition: functions_db.inc.php:474
vlan_add
vlan_add($options="")
Definition: vlan.inc.php:28
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
vlan_modify
vlan_modify($options="")
Definition: vlan.inc.php:317
$conf
global $conf
Definition: 2-to-3.php:15
$text
$text
Definition: install.php:11
$self
global $self
Definition: 2-to-3.php:15
auth
auth($resource, $msg_level=1)
Definition: functions_general.inc.php:1330
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22
vlan_del
vlan_del($options="")
Definition: vlan.inc.php:182