2018-09-22 01:35:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
include_once 'model.php';
|
|
|
|
|
2018-09-22 19:40:50 +02:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
sleep(1);
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
2018-09-22 18:00:00 +02:00
|
|
|
$INPUT = array_merge($_GET, $_POST);
|
2018-09-22 01:35:41 +02:00
|
|
|
|
2018-09-22 19:40:50 +02:00
|
|
|
if (!isset($INPUT['user_id'])) die(json_encode(['success' => false, 'errhighlight' => 101, 'message' => 'Missing parameter [[user_id]]']));
|
|
|
|
if (!isset($INPUT['user_key'])) die(json_encode(['success' => false, 'errhighlight' => 102, 'message' => 'Missing parameter [[user_token]]']));
|
|
|
|
if (!isset($INPUT['title'])) die(json_encode(['success' => false, 'errhighlight' => 103, 'message' => 'Missing parameter [[title]]']));
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2018-09-22 01:35:41 +02:00
|
|
|
|
2018-09-22 18:00:00 +02:00
|
|
|
$user_id = $INPUT['user_id'];
|
|
|
|
$user_key = $INPUT['user_key'];
|
2018-09-22 19:40:50 +02:00
|
|
|
$message = $INPUT['title'];
|
|
|
|
$content = $INPUT['content'];
|
2018-09-22 18:00:00 +02:00
|
|
|
if ($content === null || $content === false) $content = '';
|
2018-09-22 01:35:41 +02:00
|
|
|
|
2018-09-22 19:40:50 +02:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
if (strlen(trim($message)) == 0) die(json_encode(['success' => false, 'errhighlight' => 103, 'message' => 'No title specified']));
|
|
|
|
if (strlen($message) > 120) die(json_encode(['success' => false, 'errhighlight' => 103, 'message' => 'Title too long (120 characters)']));
|
|
|
|
if (strlen($content) > 10000) die(json_encode(['success' => false, 'errhighlight' => 104, 'message' => 'Content too long (10000 characters)']));
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2018-09-22 01:35:41 +02:00
|
|
|
|
|
|
|
$pdo = getDatabase();
|
|
|
|
|
2018-09-22 18:00:00 +02:00
|
|
|
$stmt = $pdo->prepare('SELECT user_id, user_key, fcm_token, messages_sent FROM users WHERE user_id = :uid LIMIT 1');
|
2018-09-22 01:35:41 +02:00
|
|
|
$stmt->execute(['uid' => $user_id]);
|
|
|
|
|
|
|
|
$datas = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
2018-09-22 18:00:00 +02:00
|
|
|
if (count($datas)<=0) die(json_encode(['success' => false, 'errhighlight' => 101, 'message' => 'No User found']));
|
2018-09-22 01:35:41 +02:00
|
|
|
$data = $datas[0];
|
|
|
|
|
2018-09-22 18:00:00 +02:00
|
|
|
if ($data === null) die(json_encode(['success' => false, 'errhighlight' => 101, 'message' => 'User not found']));
|
|
|
|
if ($data['user_id'] !== (int)$user_id) die(json_encode(['success' => false, 'errhighlight' => 101, 'message' => 'UserID not found']));
|
|
|
|
if ($data['user_key'] !== $user_key) die(json_encode(['success' => false, 'errhighlight' => 102, 'message' => 'Authentification failed']));
|
2018-09-22 01:35:41 +02:00
|
|
|
|
|
|
|
$fcm = $data['fcm_token'];
|
|
|
|
|
2018-09-22 19:40:50 +02:00
|
|
|
//------------------------------------------------------------------
|
2018-09-22 01:35:41 +02:00
|
|
|
|
|
|
|
$url = "https://fcm.googleapis.com/fcm/send";
|
|
|
|
$payload = json_encode(
|
|
|
|
[
|
|
|
|
'to' => $fcm,
|
|
|
|
//'dry_run' => true,
|
|
|
|
'notification' =>
|
|
|
|
[
|
|
|
|
'title' => $message,
|
|
|
|
'body' => $content,
|
|
|
|
],
|
|
|
|
'data' =>
|
|
|
|
[
|
|
|
|
'title' => $message,
|
|
|
|
'body' => $content,
|
2018-09-22 03:06:09 +02:00
|
|
|
'timestamp' => time(),
|
2018-09-22 01:35:41 +02:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
$header=
|
|
|
|
[
|
|
|
|
'Authorization' => 'key=' . getConfig()['firebase']['server_key'],
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
];
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$httpresult = sendPOST($url, $payload, $header);
|
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
die(json_encode(['success' => false, 'message' => 'Exception: ' . $e->getMessage()]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), messages_sent=messages_sent+1 WHERE user_id = :uid');
|
|
|
|
$stmt->execute(['uid' => $user_id]);
|
|
|
|
|
2018-09-22 18:00:00 +02:00
|
|
|
echo (json_encode(
|
|
|
|
[
|
|
|
|
'success' => true,
|
|
|
|
'message' => 'Message sent',
|
|
|
|
'response' => $httpresult,
|
|
|
|
'messagecount' => $data['messages_sent']+1
|
|
|
|
]));
|
2018-09-22 01:35:41 +02:00
|
|
|
return 0;
|