<?php include 'functions.php'; include 'head.php'; ?>
<body id="vlan">
<div class="main">
<div class="page">
<div class="header" >
<!--<h1>White-Rabbit Switch Tool</h1>-->
<div class="header-ports" ><?php wrs_header_ports(); ?></div>
<div class="topmenu">
	<?php include 'topmenu.php' ?>
</div>
</div>
<div class="content">
<div class="leftpanel">
<h2>Main Menu</h2>
	<?php include 'menu.php' ?>
</div>
<div class="rightpanel">
<div class="rightbody">
<h1 class="title">Port-VLAN Assigment <a href='help.php?help_id=vlanassignment' onClick='showPopup(this.href);return(false);'><img align=right src="./img/question.png"></a></h1> 

	<?php session_is_started() ?>
	<?php $_SESSION['advance']=""; ?>
	
	<?php
	
		// Get VLANS
		echo '<center><strong>Port-VLAN List</strong></center><hr>';
		$tmp_vlan_file="/tmp/vlans.conf";
		$vlans = shell_exec("/wr/bin/wrsw_vlans --list >".$tmp_vlan_file);
		$vlans = shell_exec("cat ".$tmp_vlan_file." |  sed -n '/ /s/ \+/ /gp'");
		$vlans = explode("\n", $vlans);
		$name_vlans="";
		$counter = 0;
		foreach($vlans as $line){
			$counter++;
			
			if($counter>=3 && !empty($line)){
				$line = explode(" ", $line);
				$name_vlans .= $line[1]." ";
			}
         
		}
		$name_vlans = explode(" ", $name_vlans);
		
		// Get Previous assignment
		$tmp_assign_file="/tmp/port2vlan.conf";
		$vlans_assignment = shell_exec("/wr/bin/wrsw_vlans --elist >".$tmp_assign_file);
		$vlans_assignment = shell_exec("cat ".$tmp_assign_file." |  sed -n '/ /s/ \+/ /gp'");
		$vlans_assignment = explode("\n", $vlans_assignment);
		
		
		echo '<table align=center border="1" class="altrowstable" id="alternatecolor">';
		echo '<tr align=center><th><font color="blue">Endpoint</font></th><th><font color="blue">VLAN ID</font></th><th><font color="blue">Mode</font></th><th><font color="blue">Priority</font></th><th><font color="blue">Mask</font></th></tr>';
		echo '<form method=POST>';
		
		for($i = 0; $i < 18; $i++){
			$single_line = explode(" ",$vlans_assignment[$i+1]); //info per endpoint line
			
			
			echo '<tr>';
			echo '<th><center><b>wr'.($i+1).'</b></center></th>';
			
			//Show the Vlan option button
			echo '<th>';
			//echo '<select name=vlan'.($i).'>';
								
			/*foreach($name_vlans as $vlan){
				if(!empty($vlan[0])){	
					echo '<option class="btn" value="'.$vlan[0].$vlan[1].'"><center>ID'.$vlan[0].$vlan[1].'</center></option>';	
				}						
				
			}*/
			echo '<input  STYLE="background-color:'.$vlancolor[$single_line[6]].';text-align:center;" size="5" type="text" value="'.$single_line[6].'" name="vlan'.($i).'">';	
			
			//echo '<option class="btn" selected="selected" value="disabled"><center>Disabled</center></option>';	
			//echo '</select>'; // end Vlan ID assignation
					
			echo '</th>';
			
			echo '<th>'; // Mode selection
				echo '<select name=mode'.($i).'>'; 
				echo '<option class="btn" '; echo (!strcmp($single_line[2],"0")) ? 'selected="selected"' : ''; echo ' value="0"><center>Access</center></option>';	
				echo '<option class="btn" '; echo (!strcmp($single_line[2],"1")) ? 'selected="selected"' : ''; echo ' value="1"><center>Trunk</center></option>';	
				echo '<option class="btn" '; echo (!strcmp($single_line[2],"2")) ? 'selected="selected"' : ''; echo ' value="2"><center>VLAN Disabled</center></option>';	
				echo '<option class="btn" '; echo (!strcmp($single_line[2],"3")) ? 'selected="selected"' : ''; echo ' value="3"><center>Unqualified port</center></option>';	
				echo '</select>'; // end mode 
			
			echo '</th>';
			
			echo '<th>'; // Priority selection
				echo '<select name=prio'.($i).'>';
				echo '<option class="btn"  '; echo (!strcmp($single_line[5],"0")) ? 'selected="selected"' : ''; echo ' value="0"><center>0</center></option>';	
				echo '<option class="btn"  '; echo (!strcmp($single_line[5],"1")) ? 'selected="selected"' : ''; echo 'value="1"><center>1</center></option>';	
				echo '<option class="btn"  '; echo (!strcmp($single_line[5],"2")) ? 'selected="selected"' : ''; echo ' value="2"><center>2</center></option>';	
				echo '<option class="btn"  '; echo (!strcmp($single_line[5],"3")) ? 'selected="selected"' : ''; echo 'value="3"><center>3</center></option>';	
				echo '<option class="btn"  '; echo (!strcmp($single_line[5],"4")) ? 'selected="selected"' : ''; echo 'value="4"><center>4</center></option>';	
				echo '</select>'; // end Priority 
			
			echo '</th>';
			
			echo '<th align=center><INPUT type="text" size="8" name="mask'.$i.'" ></th>';

			echo '</tr>';
		}
		
		echo '<tr><th></th><th></th><th></th><th></th><th align=center><input type="submit" value="Update" class="btn" name="updatevlan" ></th></tr>
					</form>';	
			
		
		echo '</table>';
		
		echo '<br>'.$_POST['mode0'];
		//Parse input and run the command
		if (!empty($_POST['updatevlan'])){
			$vlan_cmd= "/wr/bin/wrsw_vlans ";
			
			for($i = 0; $i < 18; $i++){
				//if(strcmp($_POST['vlan'.$i],"disabled")){ //VLAN selected
					$vlan_cmd .= " --ep ".$i;
					$vlan_cmd .= " --emode ".$_POST['mode'.$i];
					$vlan_cmd .= " --eprio ".$_POST['prio'.$i];
					if(!empty($_POST['vlan'.$i])){$vlan_cmd .= " --evid ".$_POST['vlan'.$i];}
					if(!empty($_POST['mask'.$i])){$vlan_cmd .= " --eumask ".$_POST['mask'.$i];}
					$output = shell_exec($vlan_cmd);
					echo $vlan_cmd;
					echo '<br><p><center>Port WR'.($i+1).' added to VLAN'.$_POST['vlan'.$i].'</center></p>';
					
				//}else{
					if(!strcmp($_POST['mode'.$i],"2")){ //Disable VLAN for endpoint
						$vlan_cmd .= " --ep ".$i;
						$vlan_cmd .= " --emode ".$_POST['mode'.$i];
						$output = shell_exec($vlan_cmd);
						echo '<br><p><center>VLAN removed for port WR'.($i+1).'</center></p>';
					}
						
				//}
				
				$vlan_cmd= "/wr/bin/wrsw_vlans ";
			}
			header('Location: vlan.php');
		}
		
		
	
	?>
	<br><br><FORM align="right" method="POST" action="vlan.php" ENCTYPE="multipart/form-data">
			<INPUT type=submit value="Go back" class="btn" >
	</form>

</div>
</div>
</div>
<div class="footer">
	<?php include 'footer.php' ?>
</div>
</div>
</div>
</body>
</html>
