<!DOCTYPE html>
<html>
<title>Sonus Generated Script</title>
<body>

<?php 

//variables
$BU = cleanInput($_GET['BU']);
$tgType = cleanInput($_GET['tgType']);
$direction = cleanInput($_GET['direction']);
$threeLetterPrefix = cleanInput($_GET['threeLetterPrefix']);
$primaryIP = cleanInput($_GET['primaryIP']);
$secondaryIP = cleanInput($_GET['secondaryIP']);
$callLimit = cleanInput($_GET['callLimit']);
$presedence = "1099";
$tgName = "-1";

//arrays
$sonus = ($_GET['sonus']);

/*$lax_Nbs = <<<EOT
  _        _    __  __  _   _ ____ ____ 
 | |      / \   \ \/ / | \ | | __ ) ___| 
 | |     / _ \   \  /  |  \| |  _ \___ \ 
 | |___ / ___ \  /  \  | |\  | |_) |__) |
 |_____/_/   \_\/_/\_\ |_| \_|____/____/ 
	
EOT;
*/

//makes TG names
$zonePubNumber = "ZONEPUBLIC_".$tgType;
$TGName = $threeLetterPrefix."_".$direction."_01_10".$zoneNumber;
$sbcIP = "0";


//updates TG Name based on type
tgNameFunction($tgType);


//changes the IP's depending on the SBC's Selected
foreach ($sonus as $i)
{
                //checks which Sonus it is and prints the IP
                switch ($i)
                {
                        case "laxnbs":
                                $sbcIP = "UNTRUST_216_20_237_0";
                                break;
                        case "dalnbs":
                                $sbcIP = "UNTRUST_216_20_235_0";
                                break;
                        case "munnbs":
                                $sbcIP = "UNTRUST_216_20_255_0";
                                break;
                        case "hknbs":
                                $sbcIP = "UNTRUST_205_252_219_0";
                                break;
                        case "munnbs":
                                $sbcIP = "MUN IP Missing";
                                break;
                        case "lonnbs":
                                $sbcIP = "LONDON IP Missing";
                                break;
                        default:
                                $sbcIP = "Hello";
                }

        Print_Commands($i);
}

//depdeinding how many sonusus it connects to it will print one for each
function Print_Commands()
{
        global $zonePubNumber;
	global $primaryIP;
	global	$secondaryIP;
	global $presedence;
	global $sbcIP;
	global $tgName;
	global $callLimit;
	global $threeLetterPrefix;

	$args = func_get_args();
       
 	//for each sonus selected it will print the commands
	foreach ($args as $i)
	{

		$addressConxt = "set addressContext default zone ";
		$sipTrunk = " sipTrunkGroup ";
		$beginingTG = $addressConxt.$zonePubNumber.$sipTrunk." ".$tgName;

		echo "------------------------------------------------------";
		echo "<br />";
		echo $i;
		echo "<br />";
		echo "------------------------------------------------------";
		echo "<br />";

		//start commands
		echo $beginingTG." media mediaIpInterfaceGroupName ".$sbcIP;
		echo "<br />";

		//wont print if Carrier == VCC
		if ($threeLetterPrefix != "VCC")	
		{
			echo $beginingTG." ingressIpPrefix ". $primaryIP;
			echo "<br />";
		}

		//only prints second IP if not empty
		if (!empty($secondaryIP) and ($threeLetterPrefix != "VCC"))
		{
			echo $beginingTG." ingressIpPrefix ". $secondaryIP;
			echo "<br />";
		}

		echo $beginingTG." signaling retryCounters invite 2";
		echo "<br />";
		echo $beginingTG." signaling rel100Support disabled";
		echo "<br />";
		echo $beginingTG." methods subscribe reject register reject";
		echo "<br />";
		echo $beginingTG." signaling causeCodeMapping sipCpcCauseMappingProfile inContactSipCpc";
		echo "<br />";
		echo $beginingTG." callRouting crankBackProfile INCONTACT001";
		echo "<br />";
		
		//only prints if call limit is set
                if (!empty($callLimit))
                {
                        echo $beginingTG." cac callLimit ". $callLimit;
                        echo "<br />";                                                                                                                          
                }   

		echo $beginingTG." state enabled mode inService";
		echo "<br />";
		echo "<br />";
		echo "ACL for ".$i;
		echo "<br />";
	
		//If Carrier == VCC we dont need ACL's	
		if ($threeLetterPrefix != "VCC")
		{
			echo "set addressContext default ipAccessControlList rule "."MISSING VARIABLE ". "action accept ipInterfaceGroup ".$sbcIP." protocol udp destinationPort 5060 precedence ".$presedence." sourceIpAddress ".$primaryIP." sourceAddressPrefixLength 32 state enabled";
			echo "<br />";
		
		}
		//only print this ACL if secondary IP is avail.
		if (!empty($secondaryIP) and ($threeLetterPrefix != "VCC"))
		{
		echo "set addressContext default ipAccessControlList rule "."MISSING VARIABLE ". "action accept ipInterfaceGroup ".$sbcIP." protocol udp destinationPort 5060 precedence ".$presedence." sourceIpAddress ".$secondaryIP." sourceAddressPrefixLength 32 state enabled";
		echo "<br />";
		}
		
		echo "<br />";
		echo "<br />";
		echo "<br />";
		echo "<br />";
	}

}

//sanitizes the variables
function cleanInput($data) 
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

//get Trunk Group name based on type
//33 = Carrier
//40 = Customer
function tgNameFunction($type)
{
	global $BU;
	global $threeLetterPrefix;
	global $direction;
	global $tgType;
	global $tgName;


	switch ($type)
                {
                        case '33':
				if ($threeLetterPrefix == "VCC")
				{
					$tgName = $threeLetterPrefix."_".$direction."_".$BU."_01_10".$tgType;                                                                   
                                }
				else
				{
					$tgName = $threeLetterPrefix."_".$direction."_01_10".$tgType;
                                	break;
				}
				break;

                        case '40':
				$tgName = "0".$BU."_01_10".$tgType;
                                break;
                        default:
				$tgName = "-10";
                }

}

/*reads prcedence file
file has three lines
first line is for precedence for customers
second line is precedence for carriers
third line is PrefixType for VCC Carriers
*/
function getPresedence()
{	

	$oldLine = "-1";
	$myFile = "precedence.txt";
	$line = file($myFile);
	$oldLine = $line[0];
	$newLine = $oldLine + 1;
	
	$updatedFile = file_get_contents($myFile);
	$newData = str_replace($oldLine, $newLine, $updatedFile);
	file_put_contents($myFile, $newData);


	//read the entire string
	//$str=implode("\n",file($myFile));

	//$fp=fopen($myFile,'w');
	//replace something in the file string - this is a VERY simple example
	//$str=str_replace($oldLine,$newLine,$str);

	//now, TOTALLY rewrite the file
	//fwrite($fp,$str,strlen($str));
	
	//fclose($fp);

	return $newLine;
}

echo getPresedence();
	
?>
</body>
</html>
