Send timestamp with request

This commit is contained in:
Mike Schwörer 2018-11-25 18:02:20 +01:00
parent f5eef7563b
commit 77f571de7d
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 52 additions and 28 deletions

View File

@ -21,6 +21,7 @@ user_key="????????????????????????????????????????????????????????????????"
title=$1 title=$1
content="" content=""
sendtime=$(date +%s)
if [ "$#" -gt 1 ]; then if [ "$#" -gt 1 ]; then
content=$2 content=$2
@ -37,7 +38,7 @@ usr_msg_id=$(uuidgen)
while true ; do while true ; do
curlresp=$(curl -s -o /dev/null -w "%{http_code}" \ curlresp=$(curl -s -o /dev/null -w "%{http_code}" \
-d "user_id=$user_id" -d "user_key=$user_key" -d "title=$title" \ -d "user_id=$user_id" -d "user_key=$user_key" -d "title=$title" -d "timestamp=$sendtime" \
-d "content=$content" -d "priority=$priority" -d "msg_id=$usr_msg_id" \ -d "content=$content" -d "priority=$priority" -d "msg_id=$usr_msg_id" \
https://scn.blackforestbytes.com/send.php) https://scn.blackforestbytes.com/send.php)

View File

@ -4,29 +4,30 @@ include('lib/httpful.phar');
class ERR class ERR
{ {
const NO_ERROR = 0000; const NO_ERROR = 0000;
const MISSING_UID = 1101; const MISSING_UID = 1101;
const MISSING_TOK = 1102; const MISSING_TOK = 1102;
const MISSING_TITLE = 1103; const MISSING_TITLE = 1103;
const INVALID_PRIO = 1104; const INVALID_PRIO = 1104;
const REQ_METHOD = 1105; const REQ_METHOD = 1105;
const NO_TITLE = 1201; const NO_TITLE = 1201;
const TITLE_TOO_LONG = 1202; const TITLE_TOO_LONG = 1202;
const CONTENT_TOO_LONG = 1203; const CONTENT_TOO_LONG = 1203;
const USR_MSG_ID_TOO_LONG = 1204; const USR_MSG_ID_TOO_LONG = 1204;
const TIMESTAMP_OUT_OF_RANGE = 1205;
const USER_NOT_FOUND = 1301; const USER_NOT_FOUND = 1301;
const USER_AUTH_FAILED = 1302; const USER_AUTH_FAILED = 1302;
const NO_DEVICE_LINKED = 1401; const NO_DEVICE_LINKED = 1401;
const QUOTA_REACHED = 2101; const QUOTA_REACHED = 2101;
const FIREBASE_COM_FAILED = 9901; const FIREBASE_COM_FAILED = 9901;
const FIREBASE_COM_ERRORED = 9902; const FIREBASE_COM_ERRORED = 9902;
const INTERNAL_EXCEPTION = 9903; const INTERNAL_EXCEPTION = 9903;
} }
class Statics class Statics

View File

@ -23,12 +23,13 @@ CREATE TABLE `messages`
`scn_message_id` INT(11) NOT NULL AUTO_INCREMENT, `scn_message_id` INT(11) NOT NULL AUTO_INCREMENT,
`sender_user_id` INT(11) NOT NULL, `sender_user_id` INT(11) NOT NULL,
`timestamp` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `timestamp_real` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ack` BIT NOT NULL DEFAULT 0, `ack` BIT NOT NULL DEFAULT 0,
`title` VARCHAR(256) NOT NULL, `title` VARCHAR(256) NOT NULL,
`content` VARCHAR(12288) NULL, `content` VARCHAR(12288) NULL,
`priority` INT(11) NOT NULL, `priority` INT(11) NOT NULL,
`sendtime` BIGINT UNSIGNED NOT NULL,
`fcm_message_id` VARCHAR(256) NULL, `fcm_message_id` VARCHAR(256) NULL,
`usr_message_id` VARCHAR(256) NULL, `usr_message_id` VARCHAR(256) NULL,

View File

@ -198,6 +198,25 @@ if (file_exists('/var/www/openwebanalytics/owa_php.php'))
</p> </p>
</div> </div>
<h2>Custom Time</h2>
<div class="section">
<p>
You can modify the displayed timestamp of a message by sending the <code>timestamp</code> parameter. The format must be a valid UNIX timestamp (elapsed seconds since 1970-01-01 GMT)
</p>
<p>
But the custom timestamp must be within 48 hoours of the current time. This parameter is only intended to supply a more precise value in case the message sending was delayed
</p>
<pre>curl \
--data "user_id={userid}" \
--data "user_key={userkey}" \
--data "title={message_title}" \
--data "timestamp={unix_timestamp}" \
https://scn.blackforestbytes.com/send.php</pre>
<p>
Be aware that the server only saves send messages for a short amount of time. Because of that you can only use this to prevent duplicates in a short time-frame, older messages with the same ID are probably already deleted and the message will be send again.
</p>
</div>
<h2>Bash script example</h2> <h2>Bash script example</h2>
<div class="section"> <div class="section">
<p> <p>
@ -227,6 +246,7 @@ user_key=<span style="color:#2a00ff; ">"????????????????????????????????????????
title=$1 title=$1
content=<span style="color:#2a00ff; ">""</span> content=<span style="color:#2a00ff; ">""</span>
sendtime=$(date +%s)
<span style="color:#7f0055; font-weight:bold; ">if</span> [ <span style="color:#2a00ff; ">"$#"</span> -gt 1 ]; <span style="color:#7f0055; font-weight:bold; ">then</span> <span style="color:#7f0055; font-weight:bold; ">if</span> [ <span style="color:#2a00ff; ">"$#"</span> -gt 1 ]; <span style="color:#7f0055; font-weight:bold; ">then</span>
content=$2 content=$2
@ -243,7 +263,7 @@ usr_msg_id=$(uuidgen)
<span style="color:#7f0055; font-weight:bold; ">while</span> true ; <span style="color:#7f0055; font-weight:bold; ">do</span> <span style="color:#7f0055; font-weight:bold; ">while</span> true ; <span style="color:#7f0055; font-weight:bold; ">do</span>
curlresp=$(curl -s -o <span style="color:#3f3fbf; ">/dev/null</span> -w <span style="color:#2a00ff; ">"%{http_code}"</span> <span style="color:#2a00ff; ">\</span> curlresp=$(curl -s -o <span style="color:#3f3fbf; ">/dev/null</span> -w <span style="color:#2a00ff; ">"%{http_code}"</span> <span style="color:#2a00ff; ">\</span>
-d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">user_id</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$user_id</span><span style="color:#2a00ff; ">"</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">user_key</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$user_key</span><span style="color:#2a00ff; ">"</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">title</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$title</span><span style="color:#2a00ff; ">"</span> <span style="color:#2a00ff; ">\</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">user_id</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$user_id</span><span style="color:#2a00ff; ">"</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">user_key</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$user_key</span><span style="color:#2a00ff; ">"</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">title</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$title</span><span style="color:#2a00ff; ">"</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">timestamp</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$sendtime</span><span style="color:#2a00ff; ">"</span> <span style="color:#2a00ff; ">\</span>
-d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">content</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$content</span><span style="color:#2a00ff; ">"</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">priority</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$priority</span><span style="color:#2a00ff; ">"</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">msg_id</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$usr_msg_id</span><span style="color:#2a00ff; ">"</span> <span style="color:#2a00ff; ">\</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">content</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$content</span><span style="color:#2a00ff; ">"</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">priority</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$priority</span><span style="color:#2a00ff; ">"</span> -d <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">msg_id</span><span style="color:#2a00ff; ">=</span><span style="color:#2a00ff; ">$usr_msg_id</span><span style="color:#2a00ff; ">"</span> <span style="color:#2a00ff; ">\</span>
https:<span style="color:#3f3fbf; ">/</span><span style="color:#3f3fbf; ">/scn.blackforestbytes.com/send.php</span>) https:<span style="color:#3f3fbf; ">/</span><span style="color:#3f3fbf; ">/scn.blackforestbytes.com/send.php</span>)

View File

@ -5,8 +5,6 @@ include_once 'api/model.php';
try try
{ {
//------------------------------------------------------------------
//sleep(1);
//------------------------------------------------------------------ //------------------------------------------------------------------
if ($_SERVER['REQUEST_METHOD'] !== 'POST') api_return(400, ['success' => false, 'error' => ERR::REQ_METHOD, 'errhighlight' => -1, 'message' => 'Invalid request method (must be POST)']); if ($_SERVER['REQUEST_METHOD'] !== 'POST') api_return(400, ['success' => false, 'error' => ERR::REQ_METHOD, 'errhighlight' => -1, 'message' => 'Invalid request method (must be POST)']);
@ -19,16 +17,18 @@ try
//------------------------------------------------------------------ //------------------------------------------------------------------
$user_id = $INPUT['user_id']; $user_id = $INPUT['user_id'];
$user_key = $INPUT['user_key']; $user_key = $INPUT['user_key'];
$message = $INPUT['title']; $message = $INPUT['title'];
$content = isset($INPUT['content']) ? $INPUT['content'] : ''; $content = isset($INPUT['content']) ? $INPUT['content'] : '';
$priority = isset($INPUT['priority']) ? $INPUT['priority'] : '1'; $priority = isset($INPUT['priority']) ? $INPUT['priority'] : '1';
$usrmsgid = isset($INPUT['msg_id']) ? $INPUT['msg_id'] : null; $usrmsgid = isset($INPUT['msg_id']) ? $INPUT['msg_id'] : null;
$time = isset($INPUT['timestamp']) ? $INPUT['timestamp'] : time();
//------------------------------------------------------------------ //------------------------------------------------------------------
if (abs($time - time()) > 60*60*24*2) api_return(400, ['success' => false, 'error' => ERR::TIMESTAMP_OUT_OF_RANGE, 'errhighlight' => -1, 'message' => 'The timestamp mus be within 24 hours of now()']);
if ($priority !== '0' && $priority !== '1' && $priority !== '2') api_return(400, ['success' => false, 'error' => ERR::INVALID_PRIO, 'errhighlight' => 105, 'message' => 'Invalid priority']); if ($priority !== '0' && $priority !== '1' && $priority !== '2') api_return(400, ['success' => false, 'error' => ERR::INVALID_PRIO, 'errhighlight' => 105, 'message' => 'Invalid priority']);
if (strlen(trim($message)) == 0) api_return(400, ['success' => false, 'error' => ERR::NO_TITLE, 'errhighlight' => 103, 'message' => 'No title specified']); if (strlen(trim($message)) == 0) api_return(400, ['success' => false, 'error' => ERR::NO_TITLE, 'errhighlight' => 103, 'message' => 'No title specified']);
@ -95,13 +95,14 @@ try
$pdo->beginTransaction(); $pdo->beginTransaction();
$stmt = $pdo->prepare('INSERT INTO messages (sender_user_id, title, content, priority, fcm_message_id, usr_message_id) VALUES (:suid, :t, :c, :p, :fmid, :umid)'); $stmt = $pdo->prepare('INSERT INTO messages (sender_user_id, title, content, priority, sendtime, fcm_message_id, usr_message_id) VALUES (:suid, :t, :c, :p, :ts, :fmid, :umid)');
$stmt->execute( $stmt->execute(
[ [
'suid' => $user_id, 'suid' => $user_id,
't' => $message, 't' => $message,
'c' => $content, 'c' => $content,
'p' => $priority, 'p' => $priority,
'ts' => $time,
'fmid' => null, 'fmid' => null,
'umid' => $usrmsgid, 'umid' => $usrmsgid,
]); ]);
@ -125,7 +126,7 @@ try
'body' => str_limit($content, 1900), 'body' => str_limit($content, 1900),
'trimmed' => (strlen($content) > 1900), 'trimmed' => (strlen($content) > 1900),
'priority' => $priority, 'priority' => $priority,
'timestamp' => time(), 'timestamp' => $time,
'usr_msg_id' => $usrmsgid, 'usr_msg_id' => $usrmsgid,
'scn_msg_id' => $scn_msg_id, 'scn_msg_id' => $scn_msg_id,
] ]