<?php
	function cleanup($str)
	{
		return(substr(stripslashes($str), 1, -1));
	}

	$validcountries = array("AU" => "Australia", "TH" => "Thailand", "IL" => "Israel", "JO" => "Jordan", "CN" => "China",
				"JP" => "Japan", "US" => "USA/Canada/New Zealand", "DE" => "Europe", "All" => "All channels");
	$gmodes = array(0 => "802.11b Only", 1 => "Auto", 2 => "802.11g only", 3 => "802.11b Deferred", 4 => "Performance", 5 => "Limited Rate Support");

	$proto = trim(`/usr/sbin/nvram get wan_proto`);
	$ip = trim(`/usr/sbin/nvram get wan_ipaddr`);
	$netmask = trim(`/usr/sbin/nvram get wan_netmask`);
	$gateway = trim(`/usr/sbin/nvram get wan_gateway`);
	$dns = trim(`/usr/sbin/nvram get wan_dns`);
	$username = trim(`/usr/sbin/nvram get ppp_username`);
	$password = trim(`/usr/sbin/nvram get ppp_passwd`);
	$wanif = trim(`/usr/sbin/nvram get wan_ifname`);
	$ssid = trim(`/usr/sbin/nvram get wl0_ssid`);

	$closed	= intval(trim(`/usr/sbin/nvram get wl0_closed`));
	$gmode = intval(trim(`/usr/sbin/nvram get wl0_gmode`));
	$channel = intval(trim(`/usr/sbin/nvram get wl0_channel`));
	$radio = intval(trim(`/usr/sbin/nvram get wl0_radio`));
	$country_code = trim(`/usr/sbin/nvram get wl0_country_code`);
	$antdiv = intval(trim(`/usr/sbin/nvram get wl0_antdiv`));
	$txant = intval(trim(`/usr/sbin/nvram get wl0_txant`));
	$txpwr1 = intval(trim(`/usr/sbin/nvram get wl0_txpwr1`));

	if($country_code == "")
		$country_code = "All";

	$fp = fopen("/etc/openvpn/access-control.txt", "r");
	while(!feof($fp))
	{
		$line = trim(fgets($fp, 4096));
		if($line == "")
			continue;
		if($subjects != "")
			$subjects .= "\n";
		$subjects .= $line;
	}
	fclose($fp);

	if($_REQUEST['reboot'] == "Reboot")
	{
		$do = `/sbin/reboot`;
		$errmsg = "Please wait while the router is rebooted";
	}

	if($_REQUEST['process'] == "1")
	{
		$wanifnew = escapeshellarg(trim($_REQUEST['wanif']));


		if(trim($_REQUEST['ssid']) != $ssid)
		{
			$ssid = escapeshellarg(trim($_REQUEST['ssid']));
			$do = `/usr/sbin/nvram set wl0_ssid=$ssid`;
			$ssid = cleanup($ssid);
		}

		if(cleanup($wanifnew) != $wanif)
		{
			if(cleanup($wanifnew) == "vlan1")
			{
				$channel = escapeshellarg(trim($_REQUEST['channel']));
				$do = `/usr/sbin/nvram set lan_ifnames=vlan0`;
				$do = `/usr/sbin/nvram set wan_ifname=vlan1`;
				$do = `/usr/sbin/nvram set wan_proto=dhcp`;
				$do = `/usr/sbin/nvram set wifi_proto=static`;
				$do = `/usr/sbin/nvram set wl0_mode=ap`;
				$do = `/usr/sbin/nvram set wl0_channel=$channel`;
				$do = `ln -sf /etc/init.d/openvpn /etc/init.d/S75openvpn`;
				$proto = "dhcp";
				$wanif = "vlan1";
				$channel = cleanup($channel);
			}

			if(cleanup($wanifnew) == "tap0")
			{
				$do = `/usr/sbin/nvram set lan_ifnames='vlan0 vlan1'`;
				$do = `/usr/sbin/nvram set wan_ifname=tap0`;
				$do = `/usr/sbin/nvram set wan_proto=dhcp`;
				$do = `/usr/sbin/nvram set wifi_proto=dhcp`;
				$do = `/usr/sbin/nvram set wl0_mode=sta`;
				$do = `/usr/sbin/nvram unset wl0_channel`;
				$do = `ln -sf /etc/init.d/openvpn-client /etc/init.d/S75openvpn`;
				$proto = "dhcp";
				$wanif = "tap0";
			}
		}

		if(trim($_REQUEST['subjects']) != $subjects)
		{
			$subjects = trim($_REQUEST['subjects']);
			$fp = fopen("/etc/openvpn/access-control.txt", "r");
			fputs($fp, $subjects);
			fclose($fp);
		}

		if($_REQUEST['proto'] != "")
		{
			$proto = escapeshellarg(trim($_REQUEST['proto']));
			$do = `/usr/sbin/nvram set wan_proto=$proto`;
			$proto = cleanup($proto);
		}
		if($_REQUEST['proto'] == "pppoe")
		{
			$username = escapeshellarg(trim($_REQUEST['username']));
			$password = escapeshellarg(trim($_REQUEST['password']));
			$do = `/usr/sbin/nvram set wan_ifname=ppp0`;
			$do = `/usr/sbin/nvram set ppp_mtu=1492`;
			$do = `/usr/sbin/nvram set ppp_redialperiod=30`;
			$do = `/usr/sbin/nvram set ppp_username=$username`;
			$do = `/usr/sbin/nvram set ppp_passwd=$password`;
			$do = `/usr/sbin/nvram set pppoe_ifname=$wanifnew`;
			$do = `/usr/sbin/nvram set pppoe_username=$username`;
			$do = `/usr/sbin/nvram set pppoe_passwd=$password`;
			$username = cleanup($username);
			$password = cleanup($password);
			$do = `ln -sf /etc/init.d/pppoe-client /etc/init.d/S51pppoe-client`;
		} else {
			$do = `/usr/sbin/nvram set wan_ifname=$wanifnew`;
			$do = `rm -f /etc/init.d/S51pppoe-client`;
		}

		if($_REQUEST['proto'] == "static")
		{
			$ip = escapeshellarg(trim($_REQUEST['IP']));
			$netmask = escapeshellarg(trim($_REQUEST['netmask']));
			$gateway = escapeshellarg(trim($_REQUEST['gateway']));
			$dns = escapeshellarg(trim($_REQUEST['dns']));

			$do = `/usr/sbin/nvram set wan_ipaddr=$ip`;
			$do = `/usr/sbin/nvram set wan_netmask=$netmask`;
			$do = `/usr/sbin/nvram set wan_gateway=$gateway`;
			$do = `/usr/sbin/nvram set wan_dns=$dns`;

			$ip = cleanup($ip);
			$netmask = cleanup($netmask);
			$gateway = cleanup($gateway);
			$dns = cleanup($dns);
		}

		if($wanif == "vlan1" && $channel != trim($_REQUEST['channel']))
		{
			$channel = escapeshellarg(trim($_REQUEST['channel']));
			$do = `/usr/sbin/nvram set wl0_channel=$channel`;
			$channel = cleanup($channel);
		}

		if($closed != trim($_REQUEST['closed']))
		{
			$closed = escapeshellarg(trim($_REQUEST['closed']));
			$do = `/usr/sbin/nvram set wl0_closed=$closed`;
			$closed = cleanup($closed);
		}

		if($gmode != trim($_REQUEST['gmode']))
		{
			$gmode = escapeshellarg(trim($_REQUEST['gmode']));
			$do = `/usr/sbin/nvram set wl0_gmode=$gmode`;
			$gmode = cleanup($gmode);
		}

		if($radio != trim($_REQUEST['radio']))
		{
			$radio = escapeshellarg(trim($_REQUEST['radio']));
			$do = `/usr/sbin/nvram set wl0_radio=$radio`;
			$radio = cleanup($radio);
		}

		if($country_code != trim($_REQUEST['country_code']))
		{
			$country_code = escapeshellarg(trim($_REQUEST['country_code']));
			$do = `/usr/sbin/nvram set wl0_country_code=$country_code`;
			$country_code = cleanup($country_code);
		}

		if($antdiv != trim($_REQUEST['antdiv']))
		{
			$antdiv = escapeshellarg(trim($_REQUEST['antdiv']));
			$do = `/usr/sbin/nvram set wl0_antdiv=$antdiv`;
			$antdiv = cleanup($antdiv);
		}

		if($txant != trim($_REQUEST['txant']))
		{
			$txant = escapeshellarg(trim($_REQUEST['txant']));
			$do = `/usr/sbin/nvram set wl0_txant=$txant`;
			$txant = cleanup($txant);
		}

		if($txpwr1 != trim($_REQUEST['txpwr1']))
		{
			
			$txpwr1 = intval(trim($_REQUEST['txpwr1']));
			if($txpwr1 < 1)
				$txpwr1 = 1;
			if($txpwr1 > 255)
				$txpwr1 = 255;
			$do = `/usr/sbin/nvram set wl0_txpwr1=$txpwr1`;
			$txpwr2 = $txpwr1;
			if($txpwr1 > 251)
				$txpwr2 = -1;
			$do = `/usr/sbin/wl txpwr1 -m $txpwr2`;
		}

		$do = `/usr/sbin/nvram commit`;
		$errmsg = "Updates commited, please reboot to make changes active";
	}
?>
<html>
<style>
table.stat th, table.stat td {
  font-family:	Verdana, Geneva, sans-serif;
  font-size : 11px;
  color: blue;
  border: 0px solid;
  white-space: nowrap;
}
.inputw{
  font-family:	Verdana, Geneva, sans-serif;
  font-size : 11px;
  border : 0px;
  color : darkblue;
}

.inputc {
  font-family:	Verdana, Geneva, sans-serif;
  font-size : 11px;
  border : 0px;
  color : darkblue;
}

.swalft {
  font-family : Arial;
  font-size : 9px;
  color : blue;
}

.swdlft {
  font-family : Arial;
  font-size : 9px;
  color : black;
}

.labelft {
  font-family:	Verdana, Geneva, sans-serif;
  font-size : 12px;
  color : #990000;
}

.cpft {
  font-family : Arial;
  font-size : 9px;
  color : black;
}

.navbar .tab {
	font-family:	Verdana, Geneva, sans-serif;
	font-size:		12px;
	display:		inline;
	margin:			1px -5px 1px 5px;
	float:			left;
	padding:		3px 6px 3px 6px;
	background:		rgb(234,242,255);
	border:			1px solid;
	border-color:	rgb(120,172,255);
	border-left:	0;
	border-bottom:	0;
	border-top:		0;
	
	z-index:		1;
	position:		relative;
	top:			0;
	font-weight:	normal;
}
.navbar .tab.selected {
	border:			1px solid rgb(120,172,255);
	border-bottom:	0;
	z-index:		3;
	padding:		2px 6px 11px 6px;
	margin:			1px -6px -2px 0px;
	top:			-2px;
	background:		white;
	font-weight:	bold;
}
.navbar .tab a {
	font-family:	Verdana, Geneva, sans-serif;
	font-size:			13px;
	color:				rgb(153,0,0);
	text-decoration:	none;
	cursor:			    hand;
}
.navbar .hover a {
	color:	rgb(0,73,150);
}
.tab-page {
	clear:			both;
	border:			1px solid rgb(120,172,255);
	background:		White;
	z-index:		2;
	position:		relative;
	top:			-2px;
	color:			Blue;
	font-family:	Verdana, Geneva, sans-serif;
	font-size:		11px;
	padding:		0px;
	width:			100%;
	height:			400px;
	min-height:		400px;
}
.navbar {
	z-index:		1;
	white-space:	nowrap;
	background:		rgb(234,242,255);
	height:			30px;
	width:			100%;
}

.tablayout {
	position:		relative;	
	top:			10px;
	height:			370px;
	background:		#dcdcdc;
	padding:		10px;
}
</style>
<script type="text/javascript">
var check = false; 
var currentPage = null;
var ns4 = false;
var ie4 = false;
var dom = false;
var hideStr = "";
var showStr = "";

function init() 
{
  styleStr=".style.display";
  hideStr="='none'";
  showStr="='block'";
  check = true; 
  if (document.getElementById){
    dom = true;
  }
  else if (document.all){
    ie4 = true;
  }
  else if (document.layers) {  
    ns4 = true;
    styleStr=".visibility";  
    hideStr="='hide'";
    showStr="='show'";
  }
  else check=false;
}

function getAllTags(tags)
{
  if (check){
    if (document.getElementsByTagName)
      return document.getElementsByTagName(tags);
    if (document.document.all)
      return document.all.tags(tags);
    if (document.layers)
      return document.lalyers;
    if (document.divs)
      return document.divs;
  }
  return null;
}

function getElement(tabName)
{
  if (check){
    if (dom)
      return document.getElementById(tabName);
    if (ie4)
      return document.all[tabName];  
    if (ns4)
      return document.layers[tabName];
  }
  return null; 
}


function onMouseOver(tabName)
{
  var el;

  el = getElement("nav"+tabName);
  if (el==null)
    return;

  el.className = el.className + " hover";
}

function onMouseOut(tabName)
{
  var el;

  el = getElement("nav"+tabName);
  if (el==null)
    return;
  
  el.className = el.className.replace(/ hover/g, "");
}

function showTab(tabName)
{
  var el;

  el = getElement(tabName);
  if (el==null)
    return;
	
  eval("el"+styleStr+showStr);

  el = getElement("nav"+tabName);
  if (el==null)
    return;

  el.className = el.className.replace(/tab/g, "tab selected");
}
function hideTab(tabName) 
{
  var el;

  el = getElement(tabName);
  if (el==null)
    return;
	
  eval("el"+styleStr+hideStr);

  el = getElement("nav"+tabName);
  if (el==null)
    return;

  el.className = el.className.replace(/ selected/g, "");
}
function switchPage(newpage) 
{
  if (currentPage!=null)
	hideTab(currentPage);
  currentPage=newpage;
  showTab(newpage);
}
function hideOthers()
{
  var all; 
  var i;
  var first=1;

  all = getAllTags("div");
  if (all==null)
    return;
  for (i=0; i<all.length; i++){
    if (all[i].className=="tab-page"){
      if (first==1){
	    first=0;
        switchPage(all[i].id);
      }
	  else eval("all[i]"+styleStr+hideStr);
    }
  }
}

function ict_change(dobj)
{
  var si_obj;
  
  si_obj = getElement("si_obj");
  if (si_obj==null)
    return;
  if (dobj.value=='0'){
    eval("si_obj"+styleStr+showStr);
  }
  else
    eval("si_obj"+styleStr+hideStr);   	
}

function onLoad()
{
  init();
  hideOthers();
  ict_change(getElement("30383"));
}
</script>
<head>
  <title>OpenWRT/OpenVPN Web Interface</title>
</head>
<body>
<?php if($errmsg != "") { ?><div id="errmsg"><?php echo $errmsg; ?></div><?php } ?>
<form method="post" action="index.php">
    <input type="hidden" name="process" value="1">

<div class="navbar">
	<h2 class="tab" id="navConnection" onclick="switchPage('Connection');"><a href="#" onmouseover="onMouseOver('Connection');" onmouseout="onMouseOut('Connection');" onclick="return false;">Uplink Connection</a></h2>
	<h2 class="tab" id="navWifiSettings" onclick="switchPage('WifiSettings');"><a href="#" onmouseover="onMouseOver('WifiSettings');" onmouseout="onMouseOut('WifiSettings');" onclick="return false;">WIFI Settings</a></h2>
	<h2 class="tab" id="navOpenVPN" onclick="switchPage('OpenVPN');"><a href="#" onmouseover="onMouseOver('OpenVPN');" onmouseout="onMouseOut('OpenVPN');" onclick="return false;">OpenVPN</a></h2>
	<h2 class="tab" id="navSysLog" onclick="switchPage('SysLog');"><a href="#" onmouseover="onMouseOver('SysLog');" onmouseout="onMouseOut('SysLog');" onclick="return false;">SysLog</a></h2>
	<p align=right>OpenWRT/OpenVPN Web Interface</p>
</div>

<div class="tab-page" id="Connection">
<div class="tablayout">
<input <?php if($proto == "dhcp") { echo "checked "; } ?>type="radio" name="proto" value="dhcp"><strong>Automatic - DHCP</strong><br />
WAN Interface: <input type="radio" name="wanif" value="vlan1" <?php if($wanif == "vlan1") { echo "checked "; } ?>/> WAN Port <input type="radio" name="wanif" value="tap0" <?php if($wanif == "tap0") { echo "checked "; } ?>/> Wifi Port<br />
(The WAN port will be another LAN port when you use Wifi for uplink connectivity - 5 instead of 4 LAN ports)<br />
<hr />
<input <?php if($proto == "static") { echo "checked "; } ?>type="radio" name="proto" value="static"><strong>Static IP</strong><br />
IP: <input type="text" name="IP" value="<?php echo $ip; ?>" style="margin-bottom: 0.4em"><br />
Netmask: <input type="text" name="netmask" value="<?php echo $netmask; ?>" style="margin-bottom: 0.4em"><br />
Gateway: <input type="text" name="gateway" value="<?php echo $gateway; ?>" style="margin-bottom: 0.4em"><br />
DNS: <input type="text" name="dns" value="<?php echo $dns; ?>" style="margin-bottom: 0.4em"><br />
<hr />
<input <?php if($proto == "pppoe") { echo "checked "; } ?>type="radio" name="proto" value="pppoe"><strong>PPPoE</strong><br />
Username: <input type="text" name="username" value="<?php echo $username; ?>" style="margin-bottom: 0.4em"><br />
Password: <input type="text" name="password" value="<?php echo $password; ?>" style="margin-bottom: 0.4em"><br />
</div>
</div>
<div class="tab-page" id="WifiSettings">
<div class="tablayout">
Radio Enabled: <input <?php if($radio == "1") { echo "checked "; } ?>type="radio" name="radio" value="1">Enable
		 <input <?php if($radio == "0") { echo "checked "; } ?>type="radio" name="radio" value="0">Disable<br />
WIFI channel: <select name="channel">
<option value="0">Auto Selection</option>
<?php
	for($i = 1; $i <= 14; $i++)
	{
		echo "<option value='$i'";
		if($i == $channel)
			echo " selected";
		echo ">$i</option>\n";
	}
?></select><br />
WIFI SSID: <input type="text" name="ssid" value="<?php echo $ssid?>" /><br />
SSID broadcasts: <input <?php if($closed == "1") { echo "checked "; } ?>type="radio" name="closed" value="1">Disable
		 <input <?php if($closed == "0") { echo "checked "; } ?>type="radio" name="closed" value="0">Enable<br />
G-Mode: <select name="gmode">
<?php
	foreach($gmodes as $key => $val)
	{
		echo "<option value='$key'";
		if($key == $gmode)
			echo " selected";
		echo ">$val</option>\n";
	}
?></select><br />
Country Selection: <select name="country_code">
<?php
	foreach($validcountries as $key => $val)
	{
		echo "<option value='$key'";
		if($key == $country_code)
			echo " selected";
		echo ">$val</option>\n";
	}
?></select><br />
RX Antenna Diversity: <select name="antdiv">
<option value="3"<?php if($antdiv == 3) echo "selected"; ?>>Auto Selection</option>
<option value="0"<?php if($antdiv == 0) echo "selected"; ?>>Antenna near Power</option>
<option value="1"<?php if($antdiv == 1) echo "selected"; ?>>Opposite Antenna</option>
</select><br />
TX Antenna Diversity: <select name="txant">
<option value="3"<?php if($txant == 3) echo "selected"; ?>>Use RX Selection</option>
<option value="0"<?php if($txant == 0) echo "selected"; ?>>Antenna near Power</option>
<option value="1"<?php if($txant == 1) echo "selected"; ?>>Opposite Antenna</option>
</select><br />
TX Power: <input type="text" name="txpwr1" value="<?php echo $txpwr1?>" /> (1-255mW)<br />
</div>
</div>
<div class="tab-page" id="OpenVPN">
<div class="tablayout">
	Certificate Subjects Allowed to Connect:<br />
	<textarea name="subjects" rows="10" cols="80"><?php echo $subjects."\n"; ?></textarea><br />
	Connection Requests:<br />
	<textarea name="connreq" rows="10" cols="80"><?php
		$lines = explode("\n", trim(`grep "VERIFY SCRIPT OK: depth=0" /tmp/openvpn.log`));
		foreach($lines as $line)
		{
			$bits = explode(",", $line, 2);
			echo trim($bits['1'])."\n";
		}
	?></textarea>
</div>
</div>
<div class="tab-page" id="SysLog">
<div class="tablayout">
	<textarea name="syslog" rows="23" cols="150"><?php echo trim(`/sbin/logread 2>&1`); ?></textarea>
</div>
</div>
<div class="submitreboot">
<input type="submit" class="submit" name="submit" value="Sumit All Changes">
<input type="submit" class="submit" name="reboot" value="Reboot">
</div>
</form>
</center>
</body>
    <script type="text/javascript">
	onLoad();
    </script>
</form>
</body>
</html>
