<?php
// include what we needs
//require_once ("../inc/pgsql.inc");
//require_once ("../inc//oci8inc.php");
//require_once ("../inc/sendmail.inc");
    require_once ($_SERVER['DOCUMENT_ROOT']."/inc/mysql.inc");
	    require_once ($_SERVER['DOCUMENT_ROOT']."/inc/pgsql.inc");

function cutstr($str,$size) { 
	if(!$size or (strlen($str) <= $size)) return $str; 
	else for($i = 0;$i < $size;$i++) if(ord($str[$i]) > 127) $over++; 
	return chop(substr($str,0,$size - $over%2)); 
} 

function SMSserver($msg,$dbConnPgSql,$strSqlpg,$group_seq, $sid )
{
	$tmpSqlmy = "";
	$tmpSqlpg = "";
	$user_count =0;

	$dbConnPgSql->query($strSqlpg);

	if($dbConnPgSql->nf() == 0)
	{
		echo "fail to pgsql querty : $strSqlpg "; echo "<br><br>";
		return false;
	}

	$dbConnOra = new Oracle_OCI_log;
	$dbConnOra->connect("xcdnuser", "solbox123", "SMS");
	if( !$dbConnOra->conn ) {
		echo "Oracle Connect Failed "; echo "<br><br>"; 
	}
	echo " Oracle DB SERVER Connecting...";  echo "<br><br>";

	while($dbConnPgSql->next_record())
	{
		$cellphone = $dbConnPgSql->f("member_cellphone");
		$cli_ip = $_SERVER['REMOTE_ADDR'];

		$tmpSqlmy  = "INSERT INTO SC_TRAN(TR_NUM, TR_SENDDATE, TR_SENDSTAT, TR_PHONE, TR_CALLBACK, TR_MSG, TR_MSGTYPE)";
		$tmpSqlmy .= "VALUES ( SC_SEQUENCE.NEXTVAL, SYSDATE, '0', '$cellphone', '$cellphone', '$msg', '0')";

		$dbConnOra->parseExec($tmpSqlmy);
		if($dbConnOra->error) {
			echo "fail to send SMS ";  echo "<br><br>";
			continue;
		}   
		$user_count ++;

		$tmpSqlpg .= "INSERT INTO t_sms_log (group_seq, sms_id, sms_cellphone, sms_message, reg_datetime, update_datetime) ";
		$tmpSqlpg .= "VALUES (".$group_seq.", 'SH_".$sid."', '".$cellphone."', '".$msg."', now(), now()); ";
	}

	$dbConnOra->disconnect();


	if($user_count == $dbConnPgSql->nf()){
		echo "Success to All send SMS  " ; echo "<br><br>";
		
		$dbConnPgSql->query($tmpSqlpg);
		return true;
	}

	return false;
}

//$id = $_GET['id'];
//$pw = $_GET['pw'];
$rcid = "ALL";
$pw = "1234";
$msg = $_GET['msg'];
$time = $_GET['time'];
$raddr = $_SERVER["REMOTE_ADDR"];
$host = $_GET['host'];
$msg = "[RAID:".$host."]:".$time.":".$msg;

$cmd = 'echo `date +"%Y%m%d %H:%M:%S"`'.": $host";
system("$cmd >> logs/raid.send.sms.mail.`date +%Y%m%d`.log");
//exit;
//system("$cmd >> raid.send.sms.mail.`date +%Y%m%d`.log");
//system("$cmd >> test.log");

echo $msg;
$to_mails = "svc1@solbox.com";
$from_mail = "svc1@solbox.com";
################ FIXME:TEST VALUE #######################
//$raddr = "61.111.52.21";
//$to_mails = "tael@solbox.com, huibong@solbox.com";
//$from_mail = "tael@solbox.com";
#########################################################
$body = trim(MakeHtmlBodyFromArcmondClient($raddr, 8787, 2)); // default value of arcmond

$to_mail = explode(",", $to_mails);
$cnt = count($to_mail);
for($i=0; $i < $cnt; $i++) {
	$sendmail = new sendmail("127.0.0.1", 25, 30);
	$sendmail->open();
	$sendmail->send($to_mail[$i], $from_mail, trim($msg), $body);
	$sendmail->close();
}


if($rcid != "" && $msg != "")
{
	$dbConnPgSql = new DB_Sql;
	$strSqlpg  = "SELECT group_seq ";
	$strSqlpg .= "FROM t_group ";
	$strSqlpg .= "WHERE group_id = '".$rcid."' AND deleted_yn = 'N'";

	$sizelen=strlen($msg); 
	echo "Message : $msg  Size :$sizelen \n";  echo "<br><br>";
	$dbConnPgSql->query($strSqlpg);

	if($dbConnPgSql->nf() == 1)
	{
		$dbConnPgSql->next_record();
		$group_seq = $dbConnPgSql->f("group_seq");
		if($group_seq != "")
		{
			$strSqlpg  = "SELECT member_cellphone ";
			$strSqlpg .= "FROM t_group_member ";
			$strSqlpg .= "WHERE group_seq = ".$group_seq;

			if($sizelen > 80){
				$strtmp = $msg;
				$msg=cutstr($strtmp,80);
				$sizelen=strlen($msg); 
				echo "CutSTR Message : $msg  Size :$sizelen \n";  echo "<br><br>";
			}

			$SMS_send_Success = false;

			for($i = 0; $i< 2; $i++) {
				if(!$SMS_send_Success) {
					if(SMSserver( $msg, $dbConnPgSql, $strSqlpg, $group_seq, $rcid )){   //  send sms success !!
						$SMS_send_Success = true;
						break;
					}
				}
			}
			if(!$SMS_send_Success ){
				echo "Unexpected error was occured "; echo "<br><br>";
			}

		}else 
			echo "PGSQL `group_seq` failed"; echo "<br><br>";

		$dbConnPgSql->close();
	}else 
		echo "rcid or msg NULL!!!"; echo "<br><br>";
}else 
echo "Unexpected error was occured. rcid : \"$rcid\" msg :\"$msg\"\n";  echo "<br><br>";

exit;
