#!/usr/bin/php-cgi
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Origin: *");
/*********************************************************************************************8
NETWORK SCRIPT

WRITTEN BY JOSE MARI REYES
SMSGLOBAL

HISTORY: 
	10-26-2016		FINAL CODINGa
	09-25-2017		REDPORT Version (this)
	12-04-2017		BUG FIXES
	01-11-2018		ADDITION AUTO DETECT HARDWARE (OV, OP)
	02-21-2018		LINKSYS REDPORT SUPPORT
	01-14-2020		DROP SUPPORT FOR LINKSYS, LS IS NOW REDPORT ENTERPRISE.  
					APICONFIG IS NOW USE FOR ALL NETWORK CONFIGURATIONS
	02-20-2020		FIX NETWORK ISSUE FOR ENTERPRISE

MANPAGE
=========


WAN NETWORK INTERFACE	
----------------------

MODES
------

	mode = setwan | getwan


WAN FIELDS
----------
	proto   dhcp or static
    	ipaddr <static ip address>
        netmask <network mask>
        gateway <gateway address>
        broadcast <broadcast address>
	dns <alternate dns>

EXAMPLE
--------

SET DHCP
	
	192.168.32.1/cgi-bin/network?mode=setwan&proto=dhcp

SET STATIC 

	192.168.32.1/cgi-bin/network?mode=setwan&proto=static&ipaddr=192.168.10.10&netmask=255.255.255.0&gateway=192.168.10.1&broadcast=192.168.10.255&dns=8.8.8.8
	

TO GET CURRENT WAN SETTING

	192.168.32.1/cgi-bin/network?mode=getwan 
	
		this will return the setting


RETURN 

	0		no error
	-1		error writing configuration
	
	

FORWARDNG FUNCTION
===================

FORWARDING FIELDS
------------------

		proto			tcp, udp, tcp_udp, other
		src_port		source port number int
		dest_port		destination port number int
		dest_ip			destination ip default 192.168.32.1
		name			title or name of the forward rule
		
MODES
-----

		mode = setforward | getforward | delforward
		
		
		SET FORWARDING
		--------------
		
		SYPNOPSIS
		
		192.168.32.1/cgi-bin/network?mode=setforward&proto=tcp_udp&src_port=10990&dest_port=11777&dest_ip=192.168.32.1&name=myforwarding
		
		RETURN VALUE
		
			1		forwarding name already exist
			0		no error
			-1		error writing forwarding rule
		
		GET FORWARD
		-----------
		
		SYNOPSIS
		
		192.168.32.1/cgi-bin/network?mode=getforward
		
		RETURN 
		
			return all forwarding rules
		
		
		DELETE FORWARDING	
		-----------------
		
		SYNOPSIS
			
		192.168.32.1/cgi-bin/network?mode=delforward&name=myforwarding
		
		RETURN
		
		0		forwarding name not found
		1		success delete
		-1		error
		
		
**************************************************************************************************************/
//added by tom causes errors when including multiple files
if(!defined('SMSG_IS_INCLUDED')){
	include ('/www/cgi-bin/apiconfig');	
}


define("NETWORK_CFGFILE", "/etc/config/network");
define("FIREWALL_CFGFILE", "/etc/config/firewall");
define("LAN_IP", "192.168.32.1");
//define("WAN", "config interface 'wan2'\n\toption ifname '" . IFACE . "'\n");

echo "<pre>";

$proto 		= "";
$ipaddr 	= "";
$netmask	= "";
$gateway	= "";
$broadcast	= "";

$src_port  	= "";
$dest_port 	= "";
$dest_ip	= "192.168.32.1";
$name		= "";

	if (isset($_REQUEST['mode']))
	{
		if (strtoupper($_REQUEST['mode']) == "SETWAN")
		{
			if(isset($_REQUEST['proto'])) $proto = $_REQUEST['proto'];
			if(isset($_REQUEST['ipaddr'])) $ipaddr = $_REQUEST['ipaddr'];
			if(isset($_REQUEST['netmask'])) $netmask = $_REQUEST['netmask'];
			if(isset($_REQUEST['gateway'])) $gateway = $_REQUEST['gateway'];
			if(isset($_REQUEST['broadcast'])) $broadcast = $_REQUEST['broadcast'];
			if(isset($_REQUEST['dns'])) $dns = $_REQUEST['dns'];
			
		
			echo set_wan($proto, $ipaddr, $netmask, $gateway, $broadcast, $dns); 
		}
		elseif (strtoupper($_REQUEST['mode']) == "GETWAN")	
		{
			echo get_wan();
		}
		elseif (strtoupper($_REQUEST['mode']) == "SETFORWARD")
		{
			if(isset($_REQUEST['proto'])) $proto = $_REQUEST['proto'];
			if(isset($_REQUEST['src_port'])) $src_port = $_REQUEST['src_port'];
			if(isset($_REQUEST['dest_port'])) $dest_port = $_REQUEST['dest_port'];
			if(isset($_REQUEST['dest_ip'])) $dest_ip = $_REQUEST['dest_ip'];
			if(isset($_REQUEST['name'])) $name = $_REQUEST['name'];
			
			echo set_redirect($proto, $src_port, $dest_port, $dest_ip, $name);
		}
		elseif (strtoupper($_REQUEST['mode']) == "GETFORWARD")	
		{
			echo get_redirect();
		}
		elseif (strtoupper($_REQUEST['mode']) == "DELFORWARD")	
		{
			if(isset($_REQUEST['name'])) $name = $_REQUEST['name'];
			echo delete_redirect($name);
		}
		
		
	}

echo "</pre>";


function delete_redirect($name)
{
$newcfg = "";
	
	
	$fstr = file_get_contents(FIREWALL_CFGFILE);
	
	if (preg_match("~\b$name\b~",$fstr) == false) return 0;
	
	$v = explode("config", $fstr);
	
	for ($i = 0; $i < count($v); $i++)
	{
		//if (is_string_exist_exact("wan", $v[$i]) == true)
		if (preg_match("~\bredirect\b~",$v[$i]) == true)
		{
			 if (preg_match("~\b$name\b~",$v[$i]) == false)
			 	$newcfg .= "config" . $v[$i]; 		
		}
		else{
			
			if (strlen($v[$i]) > 3 ) $newcfg .= "config" . $v[$i]; // . "";
		}
	}

	$r = file_put_contents(FIREWALL_CFGFILE, $newcfg);
  	return (($r === false) ? "-1" : "1");
}



function get_redirect()
{
$fwdcfg = "\tFORWARD SET\n\n";
	
	$fstr = file_get_contents(FIREWALL_CFGFILE);
	
	$v = explode("config", $fstr);
	
	for ($i = 0; $i < count($v); $i++)
	{
		if (preg_match("~\bredirect\b~",$v[$i]) == true)
		{
			$cfg = explode("\n", $v[$i]);
			for ($ii = 0; $ii < count($cfg); $ii++)
			{
				if (preg_match("~\bsrc\b~",$cfg[$ii]) == true) $fwdcfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bdest\b~",$cfg[$ii]) == true) $fwdcfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bproto\b~",$cfg[$ii]) == true) $fwdcfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bsrc_dport\b~",$cfg[$ii]) == true) $fwdcfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bdest_ip\b~",$cfg[$ii]) == true) $fwdcfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bdest_port\b~",$cfg[$ii]) == true) $fwdcfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bname\b~",$cfg[$ii]) == true) $fwdcfg .= str_replace("option", " " , $cfg[$ii]) . "\n\n";
					
			}
			
		}
		
	}

  return $fwdcfg;

}

function set_redirect($proto, $src_port, $dest_port, $dest_ip, $name)
{
	
	if(strpos(file_get_contents(FIREWALL_CFGFILE), $name) !== false) return 1;
  
  	$proto = str_replace("_", " ", $proto);
		
	$newcfg  = "\nconfig redirect\n";
	$newcfg .= "\toption target 'DNAT'\n";
    	$newcfg .= "\toption src 'wan'\n";
    	$newcfg .= "\toption dest 'lan'\n";
	$newcfg .= "\toption proto '$proto'\n";
	$newcfg .= "\toption src_dport '$src_port'\n";
    	$newcfg .= "\toption dest_ip '$dest_ip'\n";
    	$newcfg .= "\toption dest_port '$dest_port'\n";
    	$newcfg .= "\toption name '$name'\n";
	
	$r = file_put_contents(FIREWALL_CFGFILE, $newcfg, FILE_APPEND);
  	return (($r === false) ? "-1" : "0");
}



function get_wan()
{
$wancfg = "\tWAN SET\n";


	
	$fstr = file_get_contents(NETWORK_CFGFILE);
	
	$v = explode("config", $fstr);
	
	for ($i = 0; $i < count($v); $i++)
	{
		if (preg_match(CONFWAN,$v[$i]) == true)
		{
			$cfg = explode("\n", $v[$i]);
			for ($ii = 0; $ii < count($cfg); $ii++)
			{
				if (preg_match("~\bipaddr\b~",$cfg[$ii]) == true) $wancfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bgateway\b~",$cfg[$ii]) == true) $wancfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bnetmask\b~",$cfg[$ii]) == true) $wancfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bproto\b~",$cfg[$ii]) == true) $wancfg .= str_replace("option", " " , $cfg[$ii]) . "\n";	
				if (preg_match("~\bdns\b~",$cfg[$ii]) == true) $wancfg .= str_replace("option", " " , $cfg[$ii]) . "\n";
				if (preg_match("~\bbroadcast\b~",$cfg[$ii]) == true) $wancfg .= str_replace("option", " " , $cfg[$ii]) . "\n";					
			}
			
		}
		
	}

  return $wancfg;


}

function set_wan($mode, $static_ip = "", $netmask = "", $gateway = "", $broadcast = "", $dns = "")
{
	$newcfg = "";
	
	$fstr = file_get_contents(NETWORK_CFGFILE);
	
	$v = explode("config", $fstr);
	
	for ($i = 0; $i < count($v); $i++)
	{
		//if (is_string_exist_exact("wan", $v[$i]) == true)
		if (preg_match(CONFWAN,$v[$i]) == true)
		{
		
			if (strtoupper($mode) == "DHCP")
			{
        		$newcfg .= WAN . "\toption proto 'dhcp'\n";
			$newcfg .= "\toption hostname 'voiptimizer'\n";
 			$newcfg .= "\toption metric '40'\n\n";
			} 
			elseif(strtoupper($mode) == "STATIC")
			{
			$newcfg .= WAN. "\toption _orig_ifname '" . IFACE . "'\n";
        		$newcfg .= "\toption _orig_bridge 'false'\n";
        		$newcfg .= "\toption proto 'static'\n";
        		$newcfg .= "\toption ipaddr '$static_ip'\n";
        		$newcfg .= "\toption netmask '$netmask'\n";
        		$newcfg .= "\toption gateway '$gateway'\n";
			$newcfg .= "\toption dns '$dns'\n";
        		$newcfg .= "\toption broadcast '$broadcast'\n";
			$newcfg .= "\toption hostname 'voiptimizer'\n";
			$newcfg .= "\toption metric '40'\n\n";	
        	}
			
		}
		else{
			
			if (strlen($v[$i]) > 3 ) $newcfg .= "config" . $v[$i]; // . "";
		}
	}

	
	$r = file_put_contents(NETWORK_CFGFILE, $newcfg);
  	return (($r === false) ? "-1" : "0");

}


function is_string_exist_exact($needle, $string)
{
	if ( preg_match("~\b$needle\b~",$string) )
	  return true;
	else
	  return false;
}
  
?>
 
