<?php
include('config.inc.php');
function OutputResult($res)
{
echo json_encode($res, JSON_UNESCAPED_UNICODE);
exit();
}
function getIPAddress()
{
//whether ip is from the share internet
if(!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from the proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from the remote address
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['database'], $config['dbuser'], $config['password'], [
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'"
]);
$body = @file_get_contents('php://input');
$debugMode = isset($_GET['dbg']) & & ($_GET['dbg'] != 0);
header ('Content-type: application/json; charset=utf-8');
$result = array('yw' => 0, 'kxt' => 0, 'sj' => 0, 'isUpgrade' => 0);
// error_log($body);
$request = json_decode($body, true);
if ($debugMode)
{
$request['id'] = 'XY-SIMULATOR-0001';
}
if (!$debugMode & & !$request)
{
// $res = $stmt10->execute(array('termId' => 0, 'cmdName' => 'err1_' . time(), 'cmd' => '', 'cmdDesc' => ''));
OutputResult ($result);
exit();
}
$sql = 'SELECT `id` FROM terminals WHERE cmdid=:cmdid';
$stmt = $db->prepare($sql);
if (!$stmt->execute(array('cmdid' => $request['id'])))
{
// $res = $stmt10->execute(array('termId' => 0, 'cmdName' => 'err2_' . time(), 'cmd' => '', 'cmdDesc' => ''));
OutputResult($result);
exit();
}
$term = $stmt->fetch();
if (!$term)
{
// $res = $stmt10->execute(array('termId' => 0, 'cmdName' => 'err3_' . time(), 'cmd' => '', 'cmdDesc' => ''));
OutputResult($result);
exit();
}
$stmt = $db->prepare("SELECT in_maintain,quick_hb,raw_report_time FROM mntn_status WHERE term_id=:termid");
$stmt->execute(array('termid' => $term['id']));
$termStatus = $stmt->fetch();
$stmt = null;
if (!empty($termStatus['raw_report_time']))
{
$mntnHb = time() - intval($termStatus['raw_report_time']);
apcu_store('mntn_hb.' . $term['id'], $mntnHb);
}
$result['yw'] = (isset($termStatus['in_maintain']) & & ($termStatus['in_maintain'] != 0)) ? 1 : 0;
$result['kxt'] = (isset($termStatus['quick_hb']) & & ($termStatus['quick_hb'] != 0)) ? 1 : 0;
$stmt = $db->prepare("INSERT INTO mntn_raw_reports(term_id, ip,content) VALUES(:termid,:ip,:content)");
$stmt->execute(array('termid' => $term['id'], 'content' => $body, 'ip' => getIPAddress()));
$sql = "SELECT * FROM mntn_cmds WHERE term_id=:termid ORDER BY id LIMIT 1";
$stmt = $db->prepare($sql);
if (!$stmt->execute(array('termid' => $term['id'])))
{
// $res = $stmt10->execute(array('termId' => 0, 'cmdName' => 'err5_' . time(), 'cmd' => print_r($stmt->errorInfo(), true), 'cmdDesc' => ''));
OutputResult($result);
exit();
}
$rows = $stmt->fetchAll();
if (!$rows || count($rows) == 0)
{
OutputResult($result);
exit();
}
$sql = "INSERT INTO mntn_cmd_history(`id`,term_id,`name`,`cmd`,`desc`,`status`,`create_time`,`publish_time`) SELECT `id`,`term_id`,`name`,`cmd`,`desc`,1,create_time,NOW() FROM mntn_cmds WHERE `id`=:id";
$stmt2 = $db->prepare($sql);
$sql = "DELETE FROM mntn_cmds WHERE id=:id";
$stmt = $db->prepare($sql);
foreach ($rows as $row)
{
// kxt: quick heartbeat
$cmdData = empty($row['cmd']) ? array() : json_decode($row['cmd'], true);
$result['cid'] = $row['id'];
if ($row['name'] == 'upgrade')
{
if (!empty($cmdData['url']))
{
$result['isUpgrade'] = 1;
$result['sj'] = 1;
$result['url'] = $cmdData['url'];
}
}
else
{
if ($row['name'] == 'upload-logs')
{
if (!empty($cmdData['url']))
{
$pos = strpos($cmdData['url'], '?');
if ($pos !== false)
{
$cmdData['url'] .= '?termId=' . urlencode($row['term_id']);
}
else
{
$cmdData['url'] .= '& termId=' . urlencode($row['term_id']);
}
}
}
$result['cmd'] = $row['name'];
if (!empty($row['cmd']))
{
$result = array_merge($result, $cmdData);
}
}
if (!$debugMode)
{
$stmt2->execute(array('id' => $row['id']));
$stmt->execute(array('id' => $row['id']));
}
}
// {"kxt":1,"yw":1,"sj":1,"cmd"."*,"isUpgrade":1, "url":"http://180.166.218.222:40011/ds yunWei_0826value str".vv4.2.50.apk,NN"value int:2}
OutputResult($result);