<?php
	include("smsdb-conf.php");
	require "sms/Services/Twilio.php";
	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
	mysql_select_db($dbname);
	$aar=$_REQUEST;
//	$aar=array(
//	    'Body' => 'gl 17 # fdfdff df',
//	    'To' => '+14156399480',
//	    'SmsStatus' => 'received',
//	    'From' => '+14156399480',
//	);
//	
	$sFromPhone=substr($aar['From'],2);
	mysql_query("INSERT INTO wsoftsms_in (originator, text, receive_date) VALUES ('1".$sFromPhone."', '".$aar['Body']."', '".date("Y-m-d H:i:s")."')");
	
	if(!strpos($aar['Body'],'#'))
	{
		$userSql='SELECT u.id FROM `users` AS u WHERE u.`number` = "'.mysql_escape_string($sFromPhone).'" AND u.number_verify ="'.mysql_escape_string($aar['Body']).'"';
		$result = mysql_query($userSql);
		if(mysql_num_rows($result))
		{
			$aUser = mysql_fetch_array($result,MYSQL_ASSOC);
			mysql_query("UPDATE users SET number_verify='' WHERE id=".$aUser['id']);
			header("content-type: text/xml");
			echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response><Sms>Thank you! Now you can login in www.grouplingo.com</Sms></Response>";
			exit();
		}
		else 
		{
			header("content-type: text/xml");
			echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response><Sms>We cant find your data. You not site member or send incorrect SMS.</Sms></Response>";	
			exit();
		}	
	}
	preg_match("/GL(.*?)#/ui",$aar['Body'],$matches);
	$iGroupId=(int)$matches[1];
	preg_match("/#(.*?)$/",$aar['Body'],$matches);
	$sSMS=mysql_escape_string($matches['1']);
	mysql_query("INSERT INTO wsoftsms_in (originator, text, receive_date) VALUES ('1".$sFromPhone."', '".$aar['Body']."', '".date("Y-m-d H:i:s")."')");

	$userSql='SELECT `username`,u.id FROM `users` AS u
	          LEFT JOIN members AS mem ON u.id = mem.member_uid
		      WHERE u.`number` = "'.mysql_escape_string($sFromPhone).'" AND mem.group_id ='.$iGroupId.' AND mem.pending1 =0';
	$result = mysql_query($userSql);
	
	if(mysql_num_rows($result))
	{
		$aUser = mysql_fetch_array($result,MYSQL_ASSOC);
		$sMessage='GL'.$iGroupId.'#'.$aUser['username'].'#'.$sSMS;
		mysql_query("INSERT INTO messages (id, txt, member_uid, group_id, time_stamp) VALUES (NULL,'".$sMessage."', '".$aUser['id']."', '".$iGroupId."', '".time()."')");

		$sGroupMembersPhones='SELECT DISTINCT `number` FROM `users` AS u
							  LEFT JOIN members AS mem ON u.id = mem.member_uid
							  WHERE u.`number` != "'.mysql_escape_string($sFromPhone).'" AND mem.group_id ='.$iGroupId.' AND mem.pending1 =0';
			
		$result = mysql_query($sGroupMembersPhones);
		if(mysql_num_rows($result))
		{
			while ($aUser = mysql_fetch_array($result,MYSQL_ASSOC))
			{
				mysql_query("INSERT INTO wsoftsms_out (recipient, text, create_date) VALUES ('1".$aUser['number']."', '".$sMessage."', '".date("Y-m-d H:i:s")."')");
			}
		}
	}
	else 
	{
		header("content-type: text/xml");
		echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response><Sms>You not site or group member. We cant parse you SMS.</Sms></Response>";
	}
	
	
?>