File "send_message.php"
Full path: /www/ansys/qgis/000_nasu/intercomm/Backup/send_message.php
File size: 10.29 KiB (10540 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
//-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*
// 自動でクラスを参照する
// 関数名 : __autoload()
// 備考 : 参照先は./class/XXX.phpとする
// 更新 : 2012/10/19 KMK-Teraoka
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function __autoload($className)
{
$fPath = "./class/" . str_replace("_", "/", $className) . ".php";
require_once $fPath;
}
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// GCMで指定端末にイベントを送る
// 関数名 : sendGcmEvent
// 備考 :
// 更新 : 2013/03/19 KMK-Teraoka
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function sendGcmEvent($gcmId_, $events_)
{
$url = "https://android.googleapis.com/gcm/send";
$header = array(
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
"Authorization: key=AIzaSyCtFNuvqkLxtwe0hfTp38Xk17Nq8_YHGVw", //API key
);
$post_list = array(
"registration_id" => $gcmId_,
"collapse_key" => time(),
"data.message" => time(). "#". (strlen(bin2hex($events_)) / 2). "#". $events_,
);
$post = http_build_query($post_list, "&");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$ret = curl_exec($ch);
var_dump($ret);
return $ret;
}
//===============================================
// DEBUG_MODE
// 備考 :
// 更新 : 2012/10/19 KMK-Teraoka
//===============================================
define("DEBUG_MODE", "1");
Debug::setMode(DEBUG_MODE);
if (DEBUG_MODE == 1)
{
Stopwatch::start();
}
//===============================================
// CommonUtilities
// 備考 :
// 更新 : 2012/11/27 KMK-Teraoka
//===============================================
$PENTA_OCEAN = "五洋建設株式会社";
$TAIL_PENTA_OCEAN = "penta-ocean";
$SCRIPT_TITLE = "アンドロイドシステム GCM送信試験";
$APP_ID = "6";
$CONST_ID = "2";
$HOST = "192.168.100.153";
$DB = "ansys";
$USER = "postgres";
$PASS = "am#71ZJ*";
$LOG_FILE = "./log/send_message.log";
//===============================================
// HTML-HEAD-
// 備考 :
// 更新 : 2012/11/27 KMK-Teraoka
//===============================================
print "<html>";
print "<head>";
print "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">";
print "<title>". $SCRIPT_TITLE. "</title>";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ansys/config/css/style.css\">";
print "<link rel=\"shortcut icon\" href=\"/ansys/config/favicon.ico\">";
print "</head>";
//===============================================
// HTML-BODY部開始-
// 備考 :
// 更新 : 2012/11/27 KMK-Teraoka
//===============================================
print "<body>";
print "<center>";
print "<font size=\"3\">";
print "<b>";
print "". $SCRIPT_TITLE. "<br>";
print "【". $PENTA_OCEAN. "】";
print "</b>";
print "</font>";
print "</center>";
print "<hr />";
try
{
//================================================
// データベース接続
// 備考 :
// 更新 : 2012/11/27 KMK-Teraoka
//================================================
$conn = pg_connect("
host = ". $HOST. "
dbname = ". $DB. "
user = ". $USER. "
password= ". $PASS. "
");
if (!$conn)
{
throw new Exception("データベースへの接続に失敗しました");
}
//===============================================
// POST、GETパラメータの受け取り
// 備考 :
// 更新 : 2012/11/27 KMK-Teraoka
//===============================================
if (DEBUG_MODE == 1)
{
extract($_POST);
extract($_GET);
Debug::writes($_POST);
}
$btnSend = isset($_POST["btnSend"]) ? htmlspecialchars($_POST["btnSend"]) : "";
if (strcmp($btnSend, "送信") == 0)
{
$term_info = isset($_POST["selTerm"]) ? htmlspecialchars($_POST["selTerm"]) : -1;
$terminals = explode(",", $term_info, 2);
$evId[0] = isset($_POST["selMsg1"]) ? htmlspecialchars($_POST["selMsg1"]) : -1;
$evId[1] = isset($_POST["selMsg2"]) ? htmlspecialchars($_POST["selMsg2"]) : -1;
$evId[2] = isset($_POST["selMsg3"]) ? htmlspecialchars($_POST["selMsg3"]) : -1;
$evId[3] = isset($_POST["selMsg4"]) ? htmlspecialchars($_POST["selMsg4"]) : -1;
$events = $evId[0]. ",". $evId[1]. ",". $evId[2]. ",". $evId[3];
Debug::writes($terminals[0], $terminals[1], $evId[0], $evId[1], $evId[2], $evId[3]);
$insGcmEv = "INSERT INTO qgis.gcm_event";
$insGcmEv .= " (event_id, app_id, const_id, from_android_id, to_android_id, to_gcm_id, ev_01, ev_02, ev_03, ev_04, snddatetime, upddatetime, regdatetime)";
$insGcmEv .= " VALUES (";
$insGcmEv .= " nextval('qgis.gcm_event_event_id_seq'),";
$insGcmEv .= "'". $APP_ID. "'";
$insGcmEv .= ",'". $CONST_ID. "'";
$insGcmEv .= ",''";
$insGcmEv .= ",'". $terminals[1]. "'";
$insGcmEv .= ",'". $terminals[0]. "'";
$insGcmEv .= ",'". $evId[0]. "'";
$insGcmEv .= ",'". $evId[1]. "'";
$insGcmEv .= ",'". $evId[2]. "'";
$insGcmEv .= ",'". $evId[3]. "'";
$insGcmEv .= ",current_timestamp";
$insGcmEv .= ",current_timestamp";
$insGcmEv .= ",current_timestamp";
$insGcmEv .= ");";
Debug::write($insGcmEv);
$resGcmEv = pg_query($conn, $insGcmEv);
$resLsSeqId = pg_query($conn, "SELECT currval('qgis.gcm_event_event_id_seq');");
$rowLsSeqId = pg_fetch_assoc($resLsSeqId);
$regEvId = $rowLsSeqId["currval"];
$ret = sendGcmEvent($terminals[0], $regEvId. ",". $events);
$updReqRet = "UPDATE qgis.gcm_event SET req_ret='". $ret. "',upddatetime=current_timestamp WHERE event_id='". $regEvId. "';";
//$_log->write("SQL->". $updReqRet. "\n");
$resReqRet = pg_query($conn, $updReqRet);
}
//===============================================
// 見出し
// 備考 : Table内TableでBorderを細くする
// 更新 : 2012/11/27 KMK-Teraoka
//===============================================
print "<table width=\"100%\" border=\"0\" bgcolor=\"#000000\" cellspacing=\"0\" cellpadding=\"0\">";
print "<tr>";
print "<td>";
print "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\">";
print "<tr bgcolor=\"#99CCFF\">";
print "<td align=\"left\">";
print "メッセージ送信";
print "</td>";
print "</tr>";
print "</table>";
print "</td>";
print "</tr>";
print "</table>";
print "<br />";
print "<form action=\"". $_SERVER['PHP_SELF']. "\" method=\"post\" style=\"display:inline\">";
//===============================================
// WHERE句の作成
// 備考 :
// 更新 : 2012/11/27 KMK-Teraoka
//===============================================
$whereObj = " const_id='2' AND app_id='6'";
$selTerminal = "SELECT * FROM qgis.ts_terminal";
$selTerminal .= " WHERE ". $whereObj. "";
$selTerminal .= ";";
$selectTerminal = "SELECT ts.android_id, gc.gcm_id, ts.terminal_name FROM qgis.ts_terminal as ts, qgis.gcm_terminals as gc WHERE";
$selectTerminal .= " ts.android_id = gc.android_id AND ts.app_id='". $APP_ID. "' AND ts.const_id='". $CONST_ID. "' AND gc.app_id='". $APP_ID. "' AND gc.const_id='". $CONST_ID. "';";
Debug::write($selectTerminal);
if (!($resTerminal = pg_query($conn, $selectTerminal)))
{
throw new Exception("読み込み失敗<br />". $selectTerminal. "<br />". pg_last_error(). "<br />");
}
$terminals = array();
$termIndex = 0;
$row = pg_num_rows($resTerminal);
for($iCount = 0; $iCount < $row; $iCount++)
{
$rowTerminal = pg_fetch_array($resTerminal, $iCount, PGSQL_ASSOC);
//print $rowTerminal["android_id"];
//print $rowTerminal["gcm_id"];
//print $rowTerminal["terminal_name"];
$terminals[$termIndex]["android_id"] = $rowTerminal["android_id"];
$terminals[$termIndex]["gcm_id"] = $rowTerminal["gcm_id"];
$terminals[$termIndex]["name"] = $rowTerminal["terminal_name"];
$termIndex++;
}
print "再生端末=>";
print "<select name=\"selTerm\">";
for($iCount = 0; $iCount < count($terminals); $iCount++)
{
$selOption = "";
if ($iCount == 0)
{
$selOption = " selected";
}
print "<option value=\"". $terminals[$iCount]["gcm_id"]. ",". $terminals[$iCount]["android_id"]. "\" ". $selOption. ">". $terminals[$iCount]["name"]. "</option>";
}
print "</select>";
print "<br />";
//===============================================
// メッセージリストを取得
// 備考 :
// 更新 : 2012/11/27 KMK-Teraoka
//===============================================
$msgList = array();
$msgRow = 0;
$msgList[0]["id"] = -1;
$msgList[0]["info"] = "指定なし";
$msgRow++;
$selMsgList = "SELECT * FROM apc_common.messages;";
if (!($resMsgList = pg_query($conn, $selMsgList)))
{
throw new Exception("読み込み失敗<br />". $selMsgList. "<br />". pg_last_error(). "<br />");
}
for($iCount = 0; $iCount < pg_num_rows($resMsgList); $iCount++)
{
$rowMsgList = pg_fetch_array($resMsgList, $iCount, PGSQL_ASSOC);
if (strcmp(substr($rowMsgList["msg_file_name"], 0, 3), "Ask") == 0)
{
$msgList[$msgRow]["id"] = $rowMsgList["msg_id"];
$msgList[$msgRow]["info"] = $rowMsgList["msg_info"];
$msgRow++;
}
}
print "<br />";
print "再生指定";
for($jCount = 0; $jCount < 4; $jCount++)
{
print "<select name=\"selMsg". ($jCount + 1). "\">";
for($iCount = 0; $iCount < count($msgList); $iCount++)
{
$selOption = "";
if ($iCount == 0)
{
$selOption = " selected";
}
print "<option value=\"". $msgList[$iCount]["id"]. "\" ". $selOption. ">". $msgList[$iCount]["info"]. "</option>";
}
print "</select>";
}
print "<input type=\"submit\" name=\"btnSend\" value=\"送信\">";
print "</form>";
}
catch(Exception $exc)
{
print "<font color=\"#FF0000\">";
print "【スクリプトエラー】<br />";
print $exc->getMessage(). "<br />";
print "</font>";
}
//===============================================
// Postgresql終了処理
// 備考 :
// 更新 : 2012/10/19 KMK-Teraoka
//===============================================
pg_close($conn);
//===============================================
// HTML終了処理
// 備考 :
// 更新 : 2012/10/18 KMK-Teraoka
//===============================================
print "<hr />";
print "<font size=\"3\" color=\"#000000\" face=\"Arial\">";
print "<center>";
print "<b>";
print "<i>";
print $TAIL_PENTA_OCEAN;
print "</i>";
print "</b>";
print "</center>";
print "</font>";
print "</body>";
print "</html>";
?>