<?php

define(DBTYPE, "MYSQL"); // or
//define(DBTYPE, "ORACLE");

if( DBTYPE == "ORACLE" )
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/oci8inc.php");
else if( DBTYPE == "MYSQL" )
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/mysql.inc");

require_once ($_SERVER['DOCUMENT_ROOT']."/inc/pgsql.inc");
require_once ($_SERVER['DOCUMENT_ROOT']."/inc/sendmail.inc");


$id = "ALL";
$pw = "1234";
$to_mails = "infra.storage@solbox.com"; 
$from_mail = "storage@solbox.com";


// For SKBB
define(DB_HOST, "222.239.93.143:13306");
define(DB_USER, "lghv_user");
define(DB_PWD, "solbox@#12");

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

	$dbConnMySql = new dbconnect(DB_HOST, DB_USER , DB_PWD, 'lghv_db', __FILE__, __LINE__);

	while($dbConnPgSql->next_record())
	{						
		$cellphone = $dbConnPgSql->f("member_cellphone");

		$tmpSqlmy  = "INSERT INTO lghv_db.TBL_SEND_TRAN( fmsgtype, fmessage, fsenddate, fdestine, fcallback ) ";
		$tmpSqlmy .= "VALUES ( '0', '$msg', now(),  '$cellphone', '0221823688' ); ";

		//echo "mysql query : " . $tmpSqlmy; echo" <br><br>";

		$res=$dbConnMySql->query($tmpSqlmy, __FILE__, __LINE__);

		if(!$res->result)
		{   
			echo "fail to send SMS ";  echo "<br><br>";
			makelog(false, $tmpSqlmy);

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

	}

	$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)
	{
		echo "fail to pgsql querty : $strSqlpg "; echo "<br><br>";
		return false;
	}

	$dbConnOra = new Oracle_OCI_log;
	$dbConnOra->connect("xcdnuser", "solbox123", "SMS");
	$dbConnOra->connect($DB_HOST, $DB_USER, "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;
}


$mseg = $_GET['msg'];
$time = $_GET['time'];
$raddr = $_SERVER["REMOTE_ADDR"];
echo $mseg."<br>";

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

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


$body = trim(htmlBody($raddr));
#$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($id != "" && $msg != "")
{
    $sms_host = strtok( $_GET['host'], "." );

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

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

    //$msg = "[RAID:".$sms_host." ".$raddr."] ".$sms_time." ".$sms_msg;
	$msg = "[RAID:".$sms_host."] ".$sms_time." ".$sms_msg;
    echo $msg."<br>";

	$dbConnPgSql = new DB_Sql;
	$strSqlpg  = "SELECT group_seq ";
	$strSqlpg .= "FROM t_group ";
	$strSqlpg .= "WHERE group_id = '".$id."' AND group_password = '".$pw."' AND deleted_yn = 'N'";
#$strSqlpg .= "WHERE group_id = '".$id."' 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( DBTYPE == "MYSQL" )
                        $SMS_send_Success = sendsms_mysql( $msg, $dbConnPgSql, $strSqlpg, $group_seq, $id);
                    else if( DBTYPE == "ORACLE")
                        $SMS_send_Success = sendsms_oracle();
                    
                    if( $SMS_send_Success ) 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 "id or msg NULL!!!"; echo "<br><br>";
}else 
echo "Unexpected error was occured. id : \"$id\" msg :\"$msg\"\n";  echo "<br><br>";

exit;




?>
