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

EMAIL API (XGATE)
AUTHOR: JOSE MARI REYES

HISTORY

10-26-2017		INIT VERSION
03-06-2018		Additional Restart Function

USAGE:


	FIELDS:
	
			enabled		1 or 0
			username
			password
			interval		integer
			sat_domain		
			description
			primary
			
	ERROR CODES:
	
		-10		Missing Enabled
		-11 	Missing username
		-12		Missing password
		-14		Missing sat_domain
		-15		Missing description
		-16		Missing primary
		
	
	SHOW GENNERAL SETTING
	
		http://10.1.5.1/cgi-bin/email?showgen
		
	RETURN:
	
		general
		sat_domain='redportglobal.com'
		mail_concurrency='0'
		webmail_banner='RedPort Webmail'
		webmail_idletimeout='30'
		mail_pop3port='110'
		mail_smtpport='25'
		mail_logrotate='weekly'
		misc_logdepth='4'
		mail_poplog='0'
		mail_smtplog='0'
		misc_bigmailnotifyenum='0'
		misc_allowplusbig='0'
		misc_usewhitelistonsend='0'
		default_spool_folder='test'
		webmail_signature='Sent via Satellite Phone using RedPort Email'
		enabled='1'
		password='smsglobal'
		username='smsglobal'
		interval='60'
 	
	
 
 	SET GENERAL
	===========
		http://10.1.5.1/cgi-bin/email?setgen&enabled=1&username=smsglobal&password=smsglobal&interval=60&sat_domain=redportglobal.com
	
	RETURN:
		1 	success	
	
	
	
	PRIMARY ACCOUNT
	===============
	
		SHOW PRIMARY ACCOUNT(S)
		-----------------------
		
			http://10.1.5.1/cgi-bin/email?showpa
			
		RETURN:
		
			[0]=primaryacct
			[0].username='test'
			[0].password='123456789'
			[0].description='test user primary'
			[1]=primaryacct
			[1].username='tester2'
			[1].password='123456789'
			[1].description='test user secondary'
			[2]=primaryacct
			[2].username='gabby'
			[2].password='123456789'
			[2].description='789'
			[2].xgatesubaccountsprimary='smsglobal'
 
 		ADD PRIMARY ACCOUNT
		-------------------
		
		http://10.1.5.1/cgi-bin/email?addpa&username=testuser&password=testuser&description=primary2
		
		
		DELETE A PRIMARY ACCOUNT
		------------------------
		
		http://10.1.5.1/cgi-bin/email?delpa&username=gabby
		
		
	SUB ACCOUNT
	===========
	
	
		SHOW SUB ACCOUNT
		----------------
		
			http://10.1.5.1/cgi-bin/email?showsa
			
			RETURN
			
				[0]=subacct
				[0].primary='test'
				[0].username='joey'
				[0].password='123456789'
				[0].description='crewuser1'
				[1]=subacct
				[1].primary='test'
				[1].username='joey2'
				[1].password='123456789'
				[1].description='crewuser2'
				[2]=subacct
				[2].primary='smsglobal'
				[2].username='joey3'
				[2].password='123456789'
				[2].description='crewuser'
				
	
		ADD SUB ACCOUNT
		---------------
		
			http://10.1.5.1/cgi-bin/email?addsa&username=subuser&password=subuser&description=subuser2&primary=test
			
		
		DELETE SUB ACCOUNT
		
			http://10.1.5.1/cgi-bin/email?delsa&username=subuser
						
			
**************************************************************/


echo "<pre>";

$username = "";
$password = "";
$interval = "";
$sat_domain = "";


	if (isset($_REQUEST['showgen'])) echo show_general();
	
	if (isset($_REQUEST['setgen']))
	{
	 	$enabled = $_REQUEST['enabled'];
		if (strlen($enabled) < 1) { echo "-10"; exit; }
		
		if (isset($_REQUEST['username']))
		{
			$username = $_REQUEST['username'];
			if (strlen($username) < 1) { echo "-11"; exit; }
		}
		
		if (isset($_REQUEST['password']))
		{
			$password = $_REQUEST['password'];
			if (strlen($password) < 1) { echo "-12"; exit; }
		}
		
		if (isset($_REQUEST['interval']))
		{
			$interval = $_REQUEST['interval'];
			if (strlen($interval) < 1) { echo "-13"; exit; }
		}
		
		if (isset($_REQUEST['sat_domain']))
		{
			$sat_domain = $_REQUEST['sat_domain'];
			if (strlen($sat_domain) < 1) { echo "-14"; exit; }
		}
		
		set_general($enabled, $username, $password, $interval, $sat_domain);
		echo "1";	
	}
	


	if (isset($_REQUEST['addpa']))
	{	
		$username = $_REQUEST['username'];
		if (strlen($username) < 1) { echo "-11"; exit; }
		
		$password = $_REQUEST['password'];
		if (strlen($password) < 1) { echo "-12"; exit; }
		
		$description = $_REQUEST['description'];
		if (strlen($description) < 1) { echo "-15"; exit; }
				
		add_primary_account($username, $password, $description);
		echo "1";
	}
	
	
	
	if (isset($_REQUEST['delpa']))
    {
		$username = $_REQUEST['username'];
		if (strlen($username) < 1) { echo "-11"; exit; }
		
		delete_primary_account($username);
		echo "1";
	}
	
	
	
	if (isset($_REQUEST['addsa']))
	{	
		$username = $_REQUEST['username'];
		if (strlen($username) < 1) { echo "-11"; exit; }
		
		$password = $_REQUEST['password'];
		if (strlen($password) < 1) { echo "-12"; exit; }
		
		$description = $_REQUEST['description'];
		if (strlen($description) < 1) { echo "-15"; exit; }
	
		$primary = $_REQUEST['primary'];
		if (strlen($primary) < 1) { echo "-16"; exit; }
	
		add_sub_account($primary, $username, $password, $description);
		echo "1";
	}
	
	
	if (isset($_REQUEST['delsa']))
    {
		$username = $_REQUEST['username'];
		if (strlen($username) < 1) { echo "-11"; exit; }
		
		delete_sub_account($username);
		echo "1";
	}	
	
	
	if (isset($_REQUEST['showpa'])) echo show_primary_accounts();
	
	if (isset($_REQUEST['showsa'])) echo show_sub_accounts();
	
	
	
function set_general($enabled, $username, $password, $interval, $sat_domain)
{

	$cmd = "uci set xgate.@general[0].misc_enabled='$enabled'";
	exec($cmd);

	if (strlen($username) > 0)
	{	
		$cmd = "uci set xgate.@general[0].gateway_username='$username'";
		exec($cmd);
	}
	
	if (strlen($password) > 0)
	{
		$cmd = "uci set xgate.@general[0].gateway_password='$password'";
		exec($cmd);
	}
	
	if (strlen($interval) > 0)
	{
		$cmd = "uci set xgate.@general[0].tracking_automatic_updates='$interval'";
		exec($cmd);
	}
	
	if (strlen($sat_domain) > 0)
	{
		$cmd = "uci set xgate.@general[0].sat_domain='$sat_domain'";
		exec($cmd);
	}
	
	
	exec("uci commit");
	restart();
    
}



function show_general()
{
	 $cmd = "uci show xgate | grep '\<general\>'";
	 $v = shell_exec($cmd);
	 
	 $v = str_replace("xgate.@general[0]=", "", $v);
	 $v = str_replace("xgate.@general[0].", "", $v);
	 $v = str_replace("misc_enabled", "enabled", $v);
	 $v = str_replace("gateway_username", "username", $v);
	 $v = str_replace("gateway_password", "password", $v);
	 $v = str_replace("tracking_automatic_updates", "interval", $v);
	  
	 return $v;	 

}



function add_primary_account($username, $password, $description)
{

	$cmd = "uci add xgate primaryacct";
	$ext = trim(shell_exec($cmd));
	
	$cmd = "uci set xgate.$ext.username='$username'";
	exec($cmd);
	
	$cmd = "uci set xgate.$ext.password='$password'";
	exec($cmd);
	
	$cmd = "uci set xgate.$ext.description='$description'";
	exec($cmd);
	
	exec("uci commit");
	restart();
	
}

function delete_primary_account($username)
{
	$idx = get_primary_acct_idx($username);
	if (strlen($idx) < 1) return false;

	$cmd = "uci delete xgate.@primaryacct[$idx]";
	exec($cmd);
	exec("uci commit");
	restart();
	return true;	
}


function add_sub_account($primary, $username, $password, $description)
{
/*xgate.@subacct[0].xgatesubaccountsprimary='test'
xgate.@subacct[0].username='joey'
xgate.@subacct[0].password='123456789'
xgate.@subacct[0].description='crewuser1'
*/

	$cmd = "uci add xgate subacct";
	$ext = trim(shell_exec($cmd));
	
	$cmd = "uci set xgate.$ext.xgatesubaccountsprimary='$primary'";
	exec($cmd);

	$cmd = "uci set xgate.$ext.username='$username'";
	exec($cmd);

	$cmd = "uci set xgate.$ext.password='$password'";
	exec($cmd);
	
	$cmd = "uci set xgate.$ext.description='$description'";
	exec($cmd);
	
	exec("uci commit");
	restart();
}


function delete_sub_account($username)
{
	$idx = get_subacct_idx($username);
	if (strlen($idx) < 1) return false;

	$cmd = "uci delete xgate.@subacct[$idx]";
	exec($cmd);
	exec("uci commit");
	restart();
	return true;	
}




function show_primary_accounts()
{

 	$cmd = "uci show xgate | grep '\<primaryacct\>'";
	$v = shell_exec($cmd);
	
/*$v = "xgate.@primaryacct[0]=primaryacct
xgate.@primaryacct[0].username='test'
xgate.@primaryacct[0].password='123456789'
xgate.@primaryacct[0].description='test user primary'
xgate.@primaryacct[1]=primaryacct
xgate.@primaryacct[1].username='tester2'
xgate.@primaryacct[1].password='123456789'
xgate.@primaryacct[1].description='test user secondary'";
*/
	$v = str_replace("xgate.@primaryacct", "", $v);
	
	return $v;
}


function show_sub_accounts()
{
	$cmd = "uci show xgate | grep '\<subacct\>'";
	$v = shell_exec($cmd);
	
/*$v = "xgate.@subacct[0]=subacct
xgate.@subacct[0].xgatesubaccountsprimary='test'
xgate.@subacct[0].username='joey'
xgate.@subacct[0].password='123456789'
xgate.@subacct[0].description='crewuser1'
xgate.@subacct[1]=subacct
xgate.@subacct[1].xgatesubaccountsprimary='test'
xgate.@subacct[1].username='joey2'
xgate.@subacct[1].password='123456789'
xgate.@subacct[1].description='crewuser2'
xgate.@subacct[2]=subacct
xgate.@subacct[2].xgatesubaccountsprimary='smsglobal'
xgate.@subacct[2].username='joey3'
xgate.@subacct[2].password='123456789'
xgate.@subacct[2].description='crewuser'";*/

	$v = str_replace("xgate.@subacct", "", $v);
	$v = str_replace("xgatesubaccountsprimary", "primary", $v);
	
	return $v;

}







function get_subacct_idx($username)
{
	$cmd = "uci show xgate | grep '\<username\>'";
	$v = shell_exec($cmd);

/*$v = "xgate.@primaryacct[0].username='test'
xgate.@subacct[0].username='joey'
xgate.@subacct[1].username='joey2'
xgate.@primaryacct[1].username='tester2'
xgate.@subacct[2].username='joey3";
*/
	
	$v = str_replace("xgate.@primaryacct[", "", $v);
	$v = str_replace("xgate.@subacct[", ">", $v);
	$v = str_replace("].username", "", $v);
	$v = str_replace("'", "", $v);
	$v = explode("\n", $v);
	
	for ($i = 0; $i < count($v); $i++)
	{
		//echo $v[$i];
		if (strpos($v[$i], ">") !== false)
		{
			if (strpos($v[$i], "$username") !== false)
			{
				//echo $v[$i];
				$tmp = explode("=", $v[$i]);
				return str_replace(">", "", $tmp[0]);
			}
		}
	}

	return "";
	
}


function get_primary_acct_idx($username)
{
	$cmd = "uci show xgate | grep '\<username\>'";
	$v = shell_exec($cmd);

/*$v = "xgate.@primaryacct[0].username='test'
xgate.@subacct[0].username='joey'
xgate.@subacct[1].username='joey2'
xgate.@primaryacct[1].username='tester2'
xgate.@subacct[2].username='joey3";
*/	
	
	$v = str_replace("xgate.@primaryacct[", ">", $v);
	$v = str_replace("xgate.@subacct[", "", $v);
	$v = str_replace("].username", "", $v);
	$v = str_replace("'", "", $v);
	$v = explode("\n", $v);
	
	for ($i = 0; $i < count($v); $i++)
	{
		//echo $v[$i];
		if (strpos($v[$i], ">") !== false)
		{
			if (strpos($v[$i], "$username") !== false)
			{
				//echo $v[$i];
				$tmp = explode("=", $v[$i]);
				return str_replace(">", "", $tmp[0]);
			}
		}
	}

	return "";
}


 

function restart()
{
	$cmd = "/etc/init.d/xgate restart";
	exec($cmd);
}


?> 
