1<?php
2
3/**
4 * Script to manage sections
5 *************************************************/
6
7
8/* functions */
9require_once( dirname(__FILE__) . '/../../../functions/functions.php' );
10
11# initialize user object
12$Database 	= new Database_PDO;
13$User 		= new User ($Database);
14$Admin	 	= new Admin ($Database);
15$Subnets	= new Subnets ($Database);
16$Result 	= new Result ();
17
18# verify that user is logged in
19$User->check_user_session();
20# check maintaneance mode
21$User->check_maintaneance_mode ();
22
23//get size of subnets - $_POST/4
24$size = sizeof($_POST) / 4;
25
26//get unique keys for subnets because they are not sequential if deleted!!!
27foreach($_POST as $key=>$line) {
28	if (strlen(strstr($key,"subnet"))>0) {
29		$allSubnets[] = $key;
30	}
31}
32
33# format and verify each record
34foreach($allSubnets as $subnet) {
35	//get sequential number
36	$m = str_replace("subnet-", "", $subnet);
37
38	//reformat subnet
39	$_temp = explode("/", $_POST['subnet-' . $m]);
40
41	//set subnet details for importing
42	$subnet_import['subnet'] 	   = $Subnets->transform_to_decimal($_temp[0]);
43	$subnet_import['mask'] 	   	   = $_temp[1];
44	$subnet_import['sectionId']    = $_POST['section-' . $m];
45	$subnet_import['description']  = $_POST['description-' . $m];
46	$subnet_import['vlanId'] 	   = $_POST['vlan-' . $m];
47	$subnet_import['vrfId'] 	   = $_POST['vrf-' . $m];
48	$subnet_import['showName']	   = $_POST['showName-' . $m];
49
50	//cidr
51	if(strlen($err=$Subnets->verify_cidr($Subnets->transform_to_dotted($subnet_import['subnet'])."/".$subnet_import['mask']))>5) {
52		$errors[] = $err;
53	}
54	//overlapping, only root !
55	else if (strlen($err=$Subnets->verify_subnet_overlapping ($subnet_import['sectionId'], $Subnets->transform_to_dotted($subnet_import['subnet'])."/".$subnet_import['mask'], $subnet_import['vrfId']))>5) {
56		$errors[] = $err;
57	}
58	//set insert
59	else {
60		$subnets_to_insert[] = $subnet_import;
61	}
62}
63
64
65# print errors if they exist or success
66if(isset($errors)) {
67	print '<div class="alert alert-danger alert-absolute">'._('Please fix the following errors before inserting').':<hr>'. "\n";
68	foreach ($errors as $line) {
69		print $line.'<br>';
70	}
71	print '</div>';
72}
73else {
74	$errors_import_failed = 0;
75
76	//insert if all other is ok!
77	foreach($subnets_to_insert as $subnet_import) {
78		//formulate insert query
79		$values = array("sectionId"=>$subnet_import['sectionId'],
80						"subnet"=>$subnet_import['subnet'],
81						"mask"=>$subnet_import['mask'],
82						"description"=>$subnet_import['description'],
83						"vlanId"=>$subnet_import['vlanId'],
84						"vrfId"=>$subnet_import['vrfId'],
85						"masterSubnetId"=>0,
86						"showName"=>$subnet_import['showName']
87						);
88
89		if(!$Admin->object_modify("subnets", "add", "id", $values)) {
90			$Result->show("danger", _('Failed to import subnet').' '. $Subnets->transform_to_dotted($subnet_import['subnet'])."/".$subnet_import['mask'], false);
91			$errors_import_failed++;
92		}
93	}
94	//check if all is ok and print it!
95	if($errors_import_failed == 0) 	{ $Result->show("success", _("Import successfull")."!", false); }
96}
97?>