<?php
	require_once ($_SERVER['DOCUMENT_ROOT']."/inc/mysql.inc2");
	require_once ($_SERVER['DOCUMENT_ROOT']."/inc/pgsql.inc");

	$id = $_GET['id'];
	$msg = $_GET['msg'];
	$passwd ="sms5emffld)";

    $is_send_sms_success_flag = 1;   // is success 
	
	// rcid is not RC004 ( thinkware )
	if ($id == "" || $id != "THINKWARE")
		$sid = "ALL";

	if ($id == "THINKWARE") {
		$sid = "BD-RC004";
	} else if ($id =="ADMIN") {
		$sid = "ADMIN";	
	} else if ($id =="AION") {
		$sid = "AION-NC";
	} else if ($id =="WARHAM") {
		$sid = "WARHAM";
	} else if ($id =="LINPATCH") {
		$sid ="LINPATCH";
	} else if ($id =="NHNIDO") {
		$sid ="NHNIDO";
	} else if ($id =="SILVER") {
		$sid ="SILVER";
	}

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

		$sizelen=strlen($msg); 

		echo "Message : $msg  Size :$sizelen \n";  echo "<br><br>";
		
		$dbConnPgSql->query($strSqlpg);
		// FIXME:errchk
		
		if($dbConnPgSql->nf() == 1)
		{
			$dbConnPgSql->next_record();
			// FIXME:errchk
			$group_seq = $dbConnPgSql->f("group_seq");
			// FIXME:errchk
			
			if($group_seq != "")
			{
				$strSqlpg  = "SELECT member_cellphone ";
				$strSqlpg .= "FROM t_group_member ";
				$strSqlpg .= "WHERE group_seq = ".$group_seq;
		
				$dbConnPgSql->query($strSqlpg);
				// FIXME:errchk
			

				if($dbConnPgSql->nf() != 0)
				{
					$tmpSqlmy = "";
					$tmpSqlpg = "";

					if($sizelen > 80){
						$is_send_sms_success_flag =0;
					}

					if($is_send_sms_success_flag == 1){

						$dbConnMySql = new dbconnect('222.122.18.22', 'smsuser', $passwd, 'sms', __FILE__, __LINE__);
						// FIXME:errchk
						echo " connect 1 bconnect  =\"$dbConnMySql->bConnect\"";  echo "<br><br>";
						if($dbConnMySql->bConnect == false){
							$is_send_sms_success_flag = 0;
							echo "SMS  Server 1 (222.122.18.22) Connect Failed!"; echo "<br><br>";
						}else{
							echo "SMS Server 1 (222.122.18.22) connecting!"; echo "<br><br>";
						}

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

							$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'),'$cellphone',1,'ktics1^$cellphone','$msg')";
							//echo $tmpSqlmy;  echo "<br><br>";

							//Is Query failed  routing
							$dbConnMySql->query($tmpSqlmy,__FILE__, __LINE__);
							// FIXME:errchk


							if ($dbConnMySql->bQueryrun == true) {
								echo "success to send SMS"; echo "<br><br>";
							}else {
								echo "fail to send SMS ";  echo "<br><br>";
								$is_send_sms_success_flag = 0;
								//FIXME: logging more.. sms fail target, msg
								//FIXME: keep going?? break?? continue??
							}
							$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()); ";
						}
						$dbConnMySql->close();

					}

					if($is_send_sms_success_flag == 0 ) 
					{
						$dbConnPgSql->query($strSqlpg);
						if($dbConnPgSql->nf() != 0)
						{
							$tmpSqlmy = "";
							$tmpSqlpg = "";

							$dbConnMySql = new dbconnect('211.39.134.240', 'sms', '', 'sms', __FILE__, __LINE__);
							echo " SMS SERVER 2 (211.39.134.240) Connectng...";  echo "<br><br>";

							if(!$dbConnMySql->bConnect){
								echo "SMS Server 2 (211.39.134.240) Connect Failed "; echo "<br><br>";
							}else{
								echo "SMS Server 2 (211.39.134.240) Connect Success "; echo "<br><br>";
							}

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

								// TEST 
								//$msg= "Server SMS Test!! Ignore SMS";

								$tmpSqlmy  = "INSERT INTO sms_msg (phone, callback, reqdate, msg,etc1 )  ";
								$tmpSqlmy .= "VALUES ( '$cellphone', '$cellphone', sysdate(), '$msg', '$cli_ip')";

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

								if ($dbConnMySql->bQueryrun == true) {
									echo "SMS Server (211.39.134.240) success to send SMS"; echo "<br><br>";
								} else {
									echo "SMS Server (211.39.134.240) fail to send SMS "; echo "<br><br>";
									continue;
									//FIXME: logging more.. sms fail target, msg
									//FIXME: keep going?? break?? continue??
								}

								$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()); ";
							}
						} else {
							// FIXME: 
							// echo " ~~~ ";
						}
						$dbConnMySql->close();
					} //flag end 

					$dbConnPgSql->query($tmpSqlpg);
				} else {
				// FIXME: 
				// echo " ~~~ ";
				}
			} else {
				// FIXME: 
				// echo " ~~~ ";
			}
		} else {
			// FIXME: 
			// echo " ~~~ ";
		}
	} else {
		echo "Unexpected error was occured. sid : \"$sid\" msg :\"$msg\"\n";  echo "<br><br>";
	}
?>
