<?php

// kt : mysql / lgu : oracle
// example)
// LGU : http://211.233.68.197:8080/alert/send_alert.php?order=lgu&type=dblb&sendhost=nh01-mdslb02.x-cdn.com&smsgroup=TEST&mailgroup=trozan@solbox.com&time=20111012092823&msg=shutdown-host:nh01-mds02.x-cdn.com
// KT : http://sh-raid.solbox.com/alert/send_alert.php?order=kt&type=dblb&sendhost=bd04-mdslb01.ktsh.com&smsgroup=TEST&mailgroup=trozan@solbox.com&time=20111012141300&msg=asdfasdfasasfasd

$order		= strtoupper($_GET['order']);	// kt | lgu
$type		= strtoupper($_GET['type']);	// raid | dblb
$sendhost	= $_GET['sendhost'];			// host
$smsgroup	= $_GET['smsgroup'];			// test | all etc...
$mailgroup	= $_GET['mailgroup'];			// scv1@solbox.com;scv2@solbox.com;op@solbox....
$time		= $_GET['time'];				// time
$message	= $_GET['msg'];					// msg
$trace		= $_GET['trace'];				// print ment ('true' is detail log)

$from_mail	= "svc1@solbox.com";

define(ORDER_TYPE, $order);
require_once ("../inc/pgsql.inc");
require_once ("../inc/sendmail.inc");


if($order == "KT")
{
	define(DB_HOST  , "222.122.18.22");
	define(DB_USER  , "smsuser");
	define(DB_PWD   , "sms5emffld)");
	define(PGDB_HOST, "127.0.0.1");
	require_once ("../inc/mysql.inc");
}
else if($order == "LGU")
{
	define(DB_HOST  , "xcdnuser");
	define(DB_USER  , "solbox123");
	define(DB_SID   , "SMS");
	define(PGDB_HOST, "61.111.52.4");
	require_once ("../inc/oci8inc.php");
}
else
{
	echo "Order not found";
	exit;
}


function TRACE($msg, $trace)
{
	if($trace == "true")
		echo $msg;
}

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 sendsms_mysql($msg, $dbConnPgSql, $strSqlpg, $group_seq, $sid)
{
	$tmpSqlmy       = "";
	$tmpSqlpg       = "";
	$user_count     = 0;
	$cellphone_list = "";

	$dbConnPgSql->query($strSqlpg);
	if($dbConnPgSql->nf() == 0)
	{
		TRACE("fail to pgsql querty : $strSqlpg <br><br>", $trace);
		return false;
	}
	
	// 2010-09-15 huibong SMS RELAY 서버 변경 및 관련 코드 수정 처리함.
	$dbConnMySql = new dbconnect(DB_HOST, DB_USER , DB_PWD, 'sms', __FILE__, __LINE__);
	
	while($dbConnPgSql->next_record())
	{						
		$cellphone = $dbConnPgSql->f("member_cellphone");

		if( strlen($cellphone_list) > 0 )
		{
			$cellphone_list .= "|ktics1^" . $cellphone;
		}
		else
		{
			$cellphone_list = "ktics1^" . $cellphone;
		}

		$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_".$smsgroup."', '".$cellphone."', '".$msg."', now(), now()); ";	
	}

	$tmpSqlmy  = "INSERT INTO SDK_SMS_SEND ( USER_ID, SCHEDULE_TYPE, SUBJECT, NOW_DATE,SEND_DATE, CALLBACK, DEST_COUNT, DEST_INFO, SMS_MSG) ";
	$tmpSqlmy .= "VALUES ('ktics1', 0,'ktics1',DATE_FORMAT(now(),'%Y%m%d%H%i%s'),DATE_FORMAT(now(),'%Y%m%d%H%i%s'),'0317810344',$user_count,'$cellphone_list','$msg')";
	$res = $dbConnMySql->query($tmpSqlmy,__FILE__, __LINE__);

	if( !$res->result )
	{
		TRACE("fail to send SMS<br><br>", $trace);
		return false;
	}

	$dbConnMySql->close();
	$dbConnPgSql->query($tmpSqlpg);

	return true;
}

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

	$dbConnPgSql->query($strSqlpg);

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

	$dbConnOra = new Oracle_OCI_log;
	
	$dbConnOra->connect(DB_HOST, DB_USER, DB_SID);
	if( !$dbConnOra->conn )
		TRACE("Oracle Connect Failed<br><br>", $trace);

	TRACE("Oracle DB SERVER Connecting...<br><br>", $trace);

	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) {
			TRACE("fail to send SMS<br><br>", $trace);
			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())
	{
		TRACE("Success to All send SMS<br><br>", $trace);
		
		$dbConnPgSql->query($tmpSqlpg);
		return true;
	}

	return false;
}

$raddr = $_SERVER["REMOTE_ADDR"];

// CHG 2010-06-17 huibong Message 에서 구분자 : 을 제거처리
//$msg = "[RAID:".$_GET['host']."]::".$time.":".$message;

if($time != "")
{
	$time = sprintf("%s-%s %s:%s:%s"
				, substr($time,   4, 2)
				, substr($time,   6, 2)
				, substr($time,   8, 2)
				, substr($time,  10, 2)
				, substr($time,  12, 2)
			);
}

$msg = "[".$type.":".$sendhost."] ".$time." ".$message;

TRACE("1. MAIL : " . $msg . "<br>", $trace);

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

if($type != "RAID")
	$body = $msg;
else
	$body = trim(htmlBody($raddr));
#$body = trim(MakeHtmlBodyFromArcmondClient($raddr, 8787, 2)); // default value of arcmond

if($mailgroup != "")
{
	$to_mail = explode(";", $mailgroup);
	$cnt = count($to_mail);

	for($i=0; $i < $cnt; $i++)
	{
		if(ereg("(^[_0-9a-zA-Z-]+(\.[_0-9a-zA-Z-]+)*@[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*$)", $to_mail[$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($smsgroup != "" && $msg != "")
{
	$sms_host = strtok( $sendhost, "." );

	$sms_time = $time;
	if( strlen($sms_time) > 14 )
	{
		$sms_time = substr($time, 5);
	}

	$sms_msg = $message;
	if( strncmp( $message, "Controller#", 11) == 0 )
	{
		$sms_msg = substr( $message, 10 );
	}

//	$msg = "[RAID:".$sms_host." ".$raddr."] ".$sms_time." ".$sms_msg;
	$msg = "[".$type.":".$sms_host."] ".$sms_time." ".$sms_msg;
    
	$dbConnPgSql = new DB_Sql;
	$dbConnPgSql->setHostIP(PGDB_HOST);

	$strSqlpg  = "SELECT group_seq ";
	$strSqlpg .= "FROM t_group ";
	$strSqlpg .= "WHERE group_id = '".$smsgroup."' AND deleted_yn = 'N'";

	$nLength=strlen($msg); 

	TRACE("2. SMS : $msg (size :$nLength)\n<br><br>", $trace);
	
	$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($nLength > 80)
			{
				$strtmp  = $msg;
				$msg     = cutstr($strtmp,80);
				$nLength = strlen($msg); 
				TRACE("CutSTR Message : $msg  Size :$nLength \n<br><br>", $trace);
			}

			$bSendSMS = false;

			for($i = 0; $i< 2; $i++)
			{
				if(!$bSendSMS)
				{
					if( ORDER_TYPE == "KT" )
						$bSendSMS = sendsms_mysql( $msg, $dbConnPgSql, $strSqlpg, $group_seq, $smsgroup );
					else if( ORDER_TYPE == "LGU")
						$bSendSMS = sendsms_oracle( $msg, $dbConnPgSql, $strSqlpg, $group_seq, $smsgroup );

					if( $bSendSMS ) break;
				}
			}

			if(!$bSendSMS)
			{
				TRACE("Unexpected error was occured<br><br>", $trace);
			}
		}
		else
			TRACE("PGSQL `group_seq` failed<br><br>", $trace);

		$dbConnPgSql->close();

		echo "success";
	}
	else	// end if($dbConnPgSql->nf() == 1)
		TRACE("smsgroup or msg NULL!!!<br><br>", $trace);
}
else
	TRACE("Unexpected error was occured. id : \"$smsgroup\" msg :\"$msg\"\n<br><br>", $trace);

exit;

?>

