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)  

block.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 // Make sure we have necessary functions & DB connectivity
5 require_once($conf['inc_functions_db']);
6 
7 
9 // Function: block_add (string $options='')
10 //
11 // $options = key=value pairs of options for this function.
12 // multiple sets of key=value pairs should be separated
13 // by an "&" symbol.
14 //
15 // Input Options:
16 // name=STRING
17 // start=IP
18 // end=IP
19 // notes=STRING
20 //
21 // Output:
22 // Adds a block into the database called 'name'
23 // Returns a two part list:
24 // 1. The exit status of the function. 0 on success, non-zero on
25 // error. All errors messages are stored in $self['error'].
26 // 2. A textual message for display on the console or web interface.
27 //
28 // Example: list($status, $result) = block_add('name=test&start=blah&end=1');
30 function block_add($options="") {
31 
32  // The important globals
33  global $conf, $self, $onadb;
34 
35  // Version - UPDATE on every edit!
36  $version = '1.00';
37 
38  printmsg("DEBUG => block_add({$options}) called", 3);
39 
40  // Parse incoming options string to an array
41  $options = parse_options($options);
42 
43  // Return the usage summary if we need to
44  if ($options['help'] or !($options['name'] and $options['start'] and $options['end']) ) {
45  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
46  $self['error'] = 'ERROR => Insufficient parameters';
47  return(array(1,
48 <<<EOM
49 
51 Adds a block into the database
52 
53  Synopsis: block_add [KEY=VALUE] ...
54 
55  Required:
56  name=STRING block name
57  start=IP block start IP
58  end=IP block end IP
59 
60  Optional:
61  notes=STRING notes
62 
63 \n
64 EOM
65 
66  ));
67  }
68 
69 
70  // The formatting rule on block names is all upper and trim it
71  $options['name'] = trim($options['name']);
72  $options['name'] = preg_replace('/\s+/', '-', $options['name']);
73  $options['name'] = strtoupper($options['name']);
74 
75  $options['start'] = ip_mangle($options['start'], 1);
76  $options['end'] = ip_mangle($options['end'], 1);
77 
78  // There is an issue with escaping '=' and '&'. We need to avoid adding escape characters
79  $options['notes'] = str_replace('\\=','=',$options['notes']);
80  $options['notes'] = str_replace('\\&','&',$options['notes']);
81 
82  // check to see if the campus already exists
83  list($status, $rows, $block) = ona_get_block_record(array('name' => $options['name']));
84 
85  if ($status or $rows) {
86  $self['error'] = "ERROR => The block {$options['name']} already exists!";
87  printmsg("DEBUG => The block {$options['name']} already exists!",3);
88  return(array(3, $self['error'] . "\n"));
89  }
90 
91 
92  // Check permissions
93  if (!auth('advanced')) {
94  $self['error'] = "Permission denied!";
95  printmsg($self['error'], 0);
96  return(array(10, $self['error'] . "\n"));
97  }
98 
99  // Get the next ID for the new block
100  $id = ona_get_next_id('blocks');
101  if (!$id) {
102  $self['error'] = "ERROR => The ona_get_next_id() call failed!";
103  printmsg($self['error'],0);
104  return(array(5, $self['error'] . "\n"));
105  }
106  printmsg("DEBUG => ID for new block: $id", 3);
107 
108  // Add the block
109  list($status, $rows) =
111  $onadb,
112  'blocks',
113  array(
114  'id' => $id,
115  'name' => $options['name'],
116  'ip_addr_start' => $options['start'],
117  'ip_addr_end' => $options['end'],
118  'notes' => $options['notes']
119  )
120  );
121  if ($status or !$rows) {
122  $self['error'] = "ERROR => block_add() SQL Query failed: " . $self['error'];
123  printmsg($self['error'],0);
124  return(array(6, $self['error'] . "\n"));
125  }
126 
127  // Return the success notice
128  $self['error'] = "INFO => Block ADDED: {$options['name']}";
129  printmsg($self['error'],0);
130  return(array(0, $self['error'] . "\n"));
131 }
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
145 // Function: block_del (string $options='')
146 //
147 // $options = key=value pairs of options for this function.
148 // multiple sets of key=value pairs should be separated
149 // by an "&" symbol.
150 //
151 // Input Options:
152 // block=name or ID
153 //
154 // Output:
155 // Deletes a block from the database.
156 // Returns a two part list:
157 // 1. The exit status of the function. 0 on success, non-zero on
158 // error. All errors messages are stored in $self['error'].
159 // 2. A textual message for display on the console or web interface.
160 //
161 // Example: list($status, $result) = block_del('id=1223543');
163 function block_del($options="") {
164 
165  // The important globals
166  global $conf, $self, $onadb;
167 
168  // Version - UPDATE on every edit!
169  $version = '1.00';
170 
171  printmsg("DEBUG => block_del({$options}) called", 3);
172 
173  // Parse incoming options string to an array
174  $options = parse_options($options);
175 
176  // Return the usage summary if we need to
177  if ($options['help'] or !$options['block'] ) {
178  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
179  $self['error'] = 'ERROR => Insufficient parameters';
180  return(array(1,
181 <<<EOM
182 
184 Deletes a block from the database
185 
186  Synopsis: block_del [KEY=VALUE] ...
187 
188  Required:
189  block=name or ID name or ID of the block to delete
190 
191  Optional:
192  commit=[yes|no] commit db transaction (no)
193 
194 \n
195 EOM
196 
197  ));
198  }
199 
200 
201  // Sanitize options[commit] (default is no)
202  $options['commit'] = sanitize_YN($options['commit'], 'N');
203 
204  // If the block provided is numeric, check to see if it's an block
205  if (is_numeric($options['block'])) {
206 
207  // See if it's an block_id
208  list($status, $rows, $block) = ona_get_block_record(array('id' => $options['block']));
209 
210  if (!$block['id']) {
211  $self['error'] = "ERROR => Unable to find block using the ID {$options['block']}!";
212  printmsg("DEBUG => Unable to find block using the ID {$options['block']}!",3);
213  return(array(2, $self['error'] . "\n"));
214  }
215  }
216  else {
217  list($status, $rows, $block) = ona_get_block_record(array('name' => $options['block']));
218  if (!$block['id']) {
219  $self['error'] = "ERROR => Unable to find block using the name {$options['block']}!";
220  printmsg("DEBUG => Unable to find block using the name {$options['block']}!",3);
221  return(array(2, $self['error'] . "\n"));
222  }
223  }
224 
225 
226  // If "commit" is yes, delete the record
227  if ($options['commit'] == 'Y') {
228 
229  // Check permissions
230  if (!auth('advanced')) {
231  $self['error'] = "Permission denied!";
232  printmsg($self['error'], 0);
233  return(array(10, $self['error'] . "\n"));
234  }
235 
236  list($status, $rows) = db_delete_records($onadb, 'blocks', array('id' => $block['id']));
237  if ($status or !$rows) {
238  $self['error'] = "ERROR => block_del() SQL Query failed: " . $self['error'];
239  printmsg($self['error'],0);
240  return(array(4, $self['error'] . "\n"));
241  }
242 
243  // Return the success notice
244  $self['error'] = "INFO => Block DELETED: {$block['name']}";
245  printmsg($self['error'],0);
246  return(array(0, $self['error'] . "\n"));
247  }
248 
249  $start = ip_mangle($block['ip_addr_start'], 'dotted');
250  $end = ip_mangle($block['ip_addr_end'], 'dotted');
251 
252 
253  // Otherwise display the record that would have been deleted
254  $text = <<<EOL
255 Record(s) NOT DELETED (see "commit" option)
256 Displaying record(s) that would have been deleted:
257 
258  DESCRIPTION: {$block['name']}
259  IP START: {$start}
260  IP END: {$end}
261  Notes: {$block['notes']}
262 
263 
264 EOL;
265 
266  return(array(6, $text));
267 
268 }
269 
270 
271 
272 
273 
274 
275 
276 
277 
279 // Function: block_modify (string $options='')
280 //
281 // $options = key=value pairs of options for this function.
282 // multiple sets of key=value pairs should be separated
283 // by an "&" symbol.
284 //
285 // Input Options:
286 // set_name=NAME change block name
287 // set_start=STRING change block starting IP address
288 // set_end=STRING change block end IP
289 // set_notes=STRING change block notes
290 //
291 // Output:
292 // Updates a block record in the IP database.
293 // Returns a two part list:
294 // 1. The exit status of the function. 0 on success, non-zero on
295 // error. All errors messages are stored in $self['error'].
296 // 2. A textual message for display on the console or web interface.
297 //
298 // Example: list($status, $result) = block_modify('name=23452&notes=blah stuff default');
300 function block_modify($options="") {
301 
302  // The important globals
303  global $conf, $self, $onadb;
304 
305  // Version - UPDATE on every edit!
306  $version = '1.02';
307 
308  printmsg("DEBUG => block_modify({$options}) called", 3);
309 
310  // Parse incoming options string to an array
311  $options = parse_options($options);
312 
313  // Return the usage summary if we need to
314  if ($options['help'] or !$options['block'] or
315  !($options['set_name'] or
316  $options['set_start'] or
317  $options['set_end'] or
318  $options['set_notes']
319  )
320  ) {
321  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
322  $self['error'] = 'ERROR => Insufficient parameters';
323  return(array(1,
324 <<<EOM
325 
327 Modifies a block entry in the database
328 
329  Synopsis: block_modify [KEY=VALUE] ...
330 
331  Where:
332  block=NAME or ID block Name or ID
333 
334  Update:
335  set_name=NAME change block name
336  set_start=STRING change block starting IP address
337  set_end=STRING change block end
338  set_notes=STRING change block notes
339 
340 \n
341 EOM
342  ));
343  }
344 
345  // The formatting rule on block names is all upper and trim it
346  $options['set_name'] = trim($options['set_name']);
347  $options['set_name'] = preg_replace('/\s+/', '-', $options['set_name']);
348  $options['set_name'] = strtoupper($options['set_name']);
349 
350 
351  // If the block provided is numeric, check to see if it's an block
352  if (is_numeric($options['block'])) {
353 
354  // See if it's an block_id
355  list($status, $rows, $block) = ona_get_block_record(array('id' => $options['block']));
356 
357  if (!$block['id']) {
358  printmsg("DEBUG => Unable to find block using the ID {$options['block']}!",3);
359  $self['error'] = "ERROR => Unable to find block using the ID {$options['block']}!";
360  return(array(2, $self['error'] . "\n"));
361  }
362  }
363  else {
364  list($status, $rows, $block) = ona_get_block_record(array('name' => $options['block']));
365  if (!$block['id']) {
366  $self['error'] = "ERROR => Unable to find block using the name {$options['block']}!";
367  printmsg("DEBUG => Unable to find block using the name {$options['block']}!",3);
368  return(array(2, $self['error'] . "\n"));
369  }
370  }
371 
372  printmsg("DEBUG => Found block: {$block['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 block with this name
382  list($status, $rows, $record) = db_get_records($onadb, 'blocks', "id != {$block['id']} AND name LIKE '{$options['set_name']}'");
383 
384  if ($status or $rows) {
385  printmsg("DEBUG => The block {$options['set_name']} already exists!",3);
386  $self['error'] = "ERROR => The block {$options['set_name']} already exists!";
387  return(array(4, $self['error'] . "\n"));
388  }
389  $SET['name'] = $options['set_name'];
390  }
391 
392  if ($options['set_start']) {
393  $SET['ip_addr_start'] = ip_mangle($options['set_start'], 'numeric');
394  }
395 
396  if ($options['set_end']) {
397  $SET['ip_addr_end'] = ip_mangle($options['set_end'], 'numeric');
398  }
399 
400  if (array_key_exists('set_notes', $options)) {
401  // There is an issue with escaping '=' and '&'. We need to avoid adding escape characters
402  $options['set_notes'] = str_replace('\\=','=',$options['set_notes']);
403  $options['set_notes'] = str_replace('\\&','&',$options['set_notes']);
404  if ($options['set_notes'] != $block['notes']) $SET['notes'] = $options['set_notes'];
405  }
406 
407  // Check permissions
408  if (!auth('advanced')) {
409  $self['error'] = "Permission denied!";
410  printmsg($self['error'], 0);
411  return(array(10, $self['error'] . "\n"));
412  }
413 
414  // Get the block record before updating (logging)
415  list($status, $rows, $original_block) = ona_get_block_record(array('id' => $block['id']));
416 
417  // Update the record
418  list($status, $rows) = db_update_record($onadb, 'blocks', array('id' => $block['id']), $SET);
419  if ($status or !$rows) {
420  $self['error'] = "ERROR => block_modify() SQL Query failed: " . $self['error'];
421  printmsg($self['error'],0);
422  return(array(6, $self['error'] . "\n"));
423  }
424  // Get the block record before updating (logging)
425  list($status, $rows, $new_block) = ona_get_block_record(array('id' => $block['id']));
426 
427  if ($SET['name'] != $block['name'])
428  $new_name = " => {$SET['name']}";
429 
430  // Return the success notice
431  $self['error'] = "INFO => Block UPDATED:{$block['id']}: {$block['name']} {$new_name}";
432 
433  $log_msg = "INFO => Block UPDATED:{$block['id']}: ";
434  $more="";
435  foreach(array_keys($original_block) as $key) {
436  if($original_block[$key] != $new_block[$key]) {
437  $log_msg .= $more . $key . "[" .$original_block[$key] . "=>" . $new_block[$key] . "]";
438  $more= ";";
439  }
440  }
441 
442  // only print to logfile if a change has been made to the record
443  if($more != '') {
444  printmsg($self['error'], 0);
445  printmsg($log_msg, 0);
446  }
447 
448  return(array(0, $self['error'] . "\n"));
449 }
450 
451 
452 
453 
454 
455 
456 
457 
458 
460 // Function: block_display (string $options='')
461 //
462 // $options = key=value pairs of options for this function.
463 // multiple sets of key=value pairs should be separated
464 // by an "&" symbol.
465 //
466 // Input Options:
467 // block=NAME or ID
468 //
469 // Output:
470 // Returns a two part list:
471 // 1. The exit status of the function. 0 on success, non-zero on
472 // error. All errors messages are stored in $self['error'].
473 // 2. A textual message for display on the console or web interface.
474 //
475 // Example: list($status, $result) = block_display('block=test');
477 function block_display($options="") {
478  global $conf, $self, $onadb;
479 
480  // Version - UPDATE on every edit!
481  $version = '1.00';
482 
483  printmsg("DEBUG => block_display({$options}) called", 3);
484 
485  // Parse incoming options string to an array
486  $options = parse_options($options);
487 
488  // Sanitize options[verbose] (default is yes)
489  $options['verbose'] = sanitize_YN($options['verbose'], 'Y');
490 
491  // Return the usage summary if we need to
492  if ($options['help'] or !$options['block'] ) {
493  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
494  $self['error'] = 'ERROR => Insufficient parameters';
495  return(array(1,
496 <<<EOM
497 
499 Displays a block record from the database
500 
501  Synopsis: block_display [KEY=VALUE] ...
502 
503  Required:
504  block=NAME or ID Block name or ID of the block display
505 
506  Optional:
507  verbose=[yes|no] Display additional info (DEFAULT: yes)
508 
509 
510 EOM
511 
512  ));
513  }
514 
515  // The formatting rule on block names is all upper and trim it
516  $options['block'] = trim($options['block']);
517  $options['block'] = preg_replace('/\s+/', '-', $options['block']);
518  $options['block'] = strtoupper($options['block']);
519 
520 
521  // If the block provided is numeric, check to see if it's an block
522  if (is_numeric($options['block'])) {
523 
524  // See if it's an block_id
525  list($status, $rows, $block) = ona_get_block_record(array('id' => $options['block']));
526 
527  if (!$block['id']) {
528  printmsg("DEBUG => Unable to find block using the ID {$options['block']}!",3);
529  $self['error'] = "ERROR => Unable to find block using the ID {$options['block']}!";
530  return(array(2, $self['error'] . "\n"));
531  }
532  }
533  else {
534  list($status, $rows, $block) = ona_get_block_record(array('name' => $options['block']));
535  if (!$block['id']) {
536  $self['error'] = "ERROR => Unable to find block using the name {$options['block']}!";
537  printmsg("DEBUG => Unable to find block using the name {$options['block']}!",3);
538  return(array(2, $self['error'] . "\n"));
539  }
540  }
541 
542  printmsg("DEBUG => Found block: {$block['name']}", 3);
543 
544  // Build text to return
545  $text = "BLOCK RECORD\n";
546  $text .= format_array($block);
547 
548  // If 'verbose' is enabled, grab some additional info to display
549  if ($options['verbose'] == 'Y') {
550  $where .= " ip_addr >= " . $block['ip_addr_start'] . " AND ip_addr <= " . $block['ip_addr_end'];
551  list ($status, $netrows, $nets) = db_get_records($onadb, 'subnets', $where, "ip_addr");
552 
553  // subnet record(s)
554  $i = 0;
555  foreach($nets as $record) {
556  list($status, $rows, $subnet) = ona_get_subnet_record(array('id' => $record['id']));
557  if ($rows == 0) { break; }
558  $i++;
559  $text .= "\nASSOCIATED SUBNET RECORD ({$i} of {$netrows})\n";
560  $text .= format_array($subnet);
561  }
562 
563  }
564 
565  // Return the success notice
566  return(array(0, $text));
567 
568 }
569 
570 
571 
572 
573 
574 // DON'T put whitespace at the beginning or end of this file!!!
575 ?>
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
block_del
block_del($options="")
Definition: block.inc.php:163
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
ip_mangle
ip_mangle($ip="", $format="default")
Definition: functions_general.inc.php:308
$record
$record['display_name']
Definition: app_advanced_search.inc.php:12
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
ona_get_block_record
ona_get_block_record($array='', $order='')
Definition: functions_db.inc.php:1118
db_delete_records
db_delete_records($dbh=0, $table="", $where="")
Definition: functions_db.inc.php:582
block_add
block_add($options="")
Definition: block.inc.php:30
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
format_array
format_array($array=array())
Definition: functions_general.inc.php:1745
block_modify
block_modify($options="")
Definition: block.inc.php:300
db_update_record
db_update_record($dbh=0, $table="", $where="", $insert="")
Definition: functions_db.inc.php:474
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
block_display
block_display($options="")
Definition: block.inc.php:477
$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
ona_get_subnet_record
ona_get_subnet_record($array)
Definition: functions_db.inc.php:1226
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22