<?php
	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 = $_GET['id'];
	$pw = $_GET['pw'];
	$mseg = $_GET['msg'];
	$time = $_GET['time'];
	
	$id = "ALL";
	$pw = "1234";
	$raddr = $_SERVER["REMOTE_ADDR"];
/*
	$command = "../bin/arcmond_client ".$raddr." 8787 1";
	sleep(2);
	$raid_res = exec($command);
	$raid_str = explode(":", $raid_res);
	$last_num = count($raid_str) - 1;

	if($raid_str[0] == "0") {
		$strTmp = "No event";
	} else if($raid_str[0] == "1") {
		$strTmp = $raid_str[$last_num];
	} else {
		$strTmp = "check err $raid_str[2]$raid_str[3]";
	}
*/

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

	$to_mails = "sh_raid@solbox.com";
####$to_mails = "linuxser@solbox.com,dev@netomi.co.kr,bs@netomi.co.kr,lyj94@netomi.co.kr,2020kid@netomi.co.kr,redeye@netomi.co.kr,ssantacho@netomi.co.kr,hnanna@kt.co.kr,tukim99@kt.co.kr,dgkim06@kt.co.kr,leehj@kt.co.kr,js4c@netomi.co.kr,waiting@netomi.co.kr";
	$from_mail = "nuhonda@solbox.com";
	$body = trim(htmlBody($raddr));

	$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 != "")
	{
		//$msg = "[RAID:".$_GET['host']."] ".$sms_time." ".$mseg;

		// NEW 2010-06-18 huibong RAID SMS 가 다음과 같은 형식으로 처리되도록 수정처리함.
		// [RAID:ics15-qcfhs1 222.122.162.111] 07-16 10:28:41 #1 IDE Channel #16 Device Removed

		// 1. host 부분에서 ktsh.co.kr 제거처리
		$sms_host = strtok( $_GET['host'], "." );

		// 2. 시간 정보 중 년도 부분을 제거처리
		$sms_time = $time;
		if( strlen($sms_time) > 14 )
		{
			$sms_time = substr($time, 5);
		}

		// 3. 메시지 중 Controller#1 부분을 #1 으로 변경처리
		$sms_msg = $mseg;
		if( strncmp( $mseg, "Controller#", 11) == 0 )
		{
			$sms_msg = substr( $mseg, 10 );
		}

		// 4. 최종 SMS 메시지 조립 (IP 정보 부분 추가 처리)
		$msg = "[RAID:".$sms_host." ".$raddr."] ".$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'";

		$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;
				//echo $strSqlpg."<br>";
				
				$dbConnPgSql->query($strSqlpg);
				//echo $dbConnPgSql->nf()."<br>";
			
				if($dbConnPgSql->nf() != 0)
				{					
					$tmpSqlmy = "";
					$tmpSqlpg = "";
					//$dbConnMySql = new dbconnect('211.39.134.240', 'ktsh', 'ktsh!@#', 'sms', __FILE__, __LINE__);
					$dbConnMySql = new dbconnect('211.39.134.240', 'sms', '', 'sms', __FILE__, __LINE__);
					
					while($dbConnPgSql->next_record())
					{						
						$cellphone = $dbConnPgSql->f("member_cellphone");
						$cli_ip = $_SERVER['REMOTE_ADDR'];
						$tmpSqlmy  = "INSERT INTO sms_msg (phone, callback, reqdate, msg,etc1 )  ";
						//$tmpSqlmy .= "VALUES ( '$cellphone', '', sysdate(), '$msg', '$cli_ip')";
						$tmpSqlmy .= "VALUES ( '$cellphone', '$cellphone', sysdate(), '$msg', '$cli_ip')";

						$dbConnMySql->query($tmpSqlmy,__FILE__, __LINE__);
						
						$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()); ";						
					}
					$dbConnPgSql->query($tmpSqlpg);
				}					
			}
		}		
	}	
?>
