From 2163ae4dbfb7430106516c0e940654fdb151cab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 12 Nov 2018 12:41:59 +0100 Subject: [PATCH] index_more.php --- web/css/style.css | 37 ++++++++- web/index.php | 4 +- web/index_api.php | 18 +++-- web/index_more.php | 186 +++++++++++++++++++++++++++++++++++++++++++ web/index_sent.php | 8 +- web/model.php | 30 ++++++- web/send.php | 193 ++++++++++++++++++++++++--------------------- 7 files changed, 366 insertions(+), 110 deletions(-) create mode 100644 web/index_more.php diff --git a/web/css/style.css b/web/css/style.css index 2325942..e0dee93 100644 --- a/web/css/style.css +++ b/web/css/style.css @@ -22,12 +22,24 @@ body #mainpnl { box-shadow: 0 0 32px #DDD; - animation:blink-shadow ease-in-out 4s infinite; + //animation:blink-shadow ease-in-out 4s infinite; width: 87%; min-width: 300px; max-width: 900px; position: relative; - min-height: 485px; + min-height: 570px; + + background: var(--form-back-color); + color: var(--form-fore-color); + border: .0625rem solid var(--form-border-color); + border-radius: var(--universal-border-radius); + margin: 32px .5rem; + padding: calc(2 * var(--universal-padding)) var(--universal-padding); +} + +.red-code +{ + border-left: .25rem solid #E53935; } #mainpnl input, @@ -178,6 +190,7 @@ input::-webkit-inner-spin-button { justify-content: center; align-items: center; align-content: center; + pointer-events: none; } a.card, @@ -192,4 +205,24 @@ a.card:hover a.card:hover { box-shadow: 0 0 16px #AAA; +} + +table.scode_table { + max-height: none; +} + +table.scode_table td:nth-child(2) { + flex-grow: 3; +} + +table.scode_table th:nth-child(2) { + flex-grow: 3; +} + +#mainpnl h2 { + margin-top: 1.75rem; +} + +.linkcaption:hover { + text-decoration: none; } \ No newline at end of file diff --git a/web/index.php b/web/index.php index 858ddde..cebb314 100644 --- a/web/index.php +++ b/web/index.php @@ -16,7 +16,7 @@ API -

Simple Cloud Notifier

+

Simple Cloud Notifier

@@ -46,7 +46,7 @@
-
+
diff --git a/web/index_api.php b/web/index_api.php index a2717af..692b082 100644 --- a/web/index_api.php +++ b/web/index_api.php @@ -2,7 +2,7 @@ - + Simple Cloud Notifications - API @@ -10,27 +10,31 @@ -
+
Send -

Simple Cloud Notifier

+

Simple Cloud Notifier

Get your user-id and user-key from the app and send notifications to your phone by performing a POST request against https://simplecloudnotifier.blackforestbytes.com/send.php

curl                                          \
     --data "user_id={userid}"                 \
     --data "user_key={userkey}"               \
-    --data "priority={0|1|2}"                 \
     --data "title={message_title}"            \
-    --data "content={message_content}"        \
+    --data "content={message_body}"           \
+    --data "priority={0|1|2}"                 \
+    --data "msg_id={unique_message_id}"       \
     https://scn.blackforestbytes.com/send.php
-

The content and priority parameters are optional, you can also send message with only a title and the default priority

+

The content, priority and msg_id parameters are optional, you can also send message with only a title and the default priority

curl                                          \
     --data "user_id={userid}"                 \
     --data "user_key={userkey}"               \
     --data "title={message_title}"            \
     https://scn.blackforestbytes.com/send.php
- + + More + +
© blackforestbytes diff --git a/web/index_more.php b/web/index_more.php new file mode 100644 index 0000000..95a8fc3 --- /dev/null +++ b/web/index_more.php @@ -0,0 +1,186 @@ + + + + + + Simple Cloud Notifications - API + + + + + + +
+ + Send + +

Simple Cloud Notifier

+ +

Introduction

+
+

+ With this API you can send push notifications to your phone. +

+

+ To recieve them you will need to install the SimpleCloudNotifier app from the play store. + When you open the app you can click on the account tab to see you unique user_id and user_key. + These two values are used to identify and authenticate your device so that send messages can be routed to your phone. +

+

+ You can at any time generate a new user_key in the app and invalidate the old one. +

+

+ There is also a web interface for this API to manually send notifications to your phone or to test your setup. +

+
+ +

Quota

+
+

+ By default you can send up to 100 messages per day per device. + If you need more you can upgrade your account in the app to get 1000 messages per day, this has the additional benefit of removing ads and supporting the development of the app (and making sure I can pay the server costs). +

+
+ +

API Requests

+
+

+ To send a new notification you send a POST request to the URL https://scn.blackforestbytes.com/send.php. + All Parameters can either directly be submitted as URL parameters or they can be put into the POST body. +

+

+ You need to supply a valid user_id - user_key pair and a title for your message, all other parameter are optional. +

+
+ +

API Response

+
+

+ If the operation was successful the API will respond with an HTTP statuscode 200 and an JSON payload indicating the send message and your remaining quota +

+
{
+    "success":true,
+    "message":"Message sent",
+    "response":
+    {
+        "multicast_id":8000000000000000006,
+        "success":1,
+        "failure":0,
+        "canonical_ids":0,
+        "results": [{"message_id":"0:10000000000000000000000000000000d"}]
+    },
+    "messagecount":623,
+    "quota":17,
+    "quota_max":100
+}
+

+ If the operation is not successful the API will respond with an 4xx HTTP statuscode. +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
StatuscodeExplanation
200 (OK)Message sent
400 (Bad Request)The request is invalid (missing parameters or wrong values)
401 (Unauthorized)The user_id was not found or the user_key is wrong
403 (Forbidden)The user has exceeded its daily quota - wait 24 hours or upgrade your account
500 (Internal Server Error)There was an internal error while sending your data - try again later
+

+ There is also always a JSON payload with additional information. + The success field is always there and in the error state you the message field to get a descritpion of the problem. +

+
{
+    "success":false,
+    "error":2101,
+    "errhighlight":-1,
+    "message":"Daily quota reached (100)"
+}
+
+ +

Message Content

+
+

+ Every message must have a title set. + But you also (optionally) add more content, while the title has a max length of 120 characters, the conntent can be up to 10.000 characters. + You can see the whole message with title and content in the app or when clicking on the notification. +

+

+ If needed the content can be supplied in the content parameter. +

+
curl                                          \
+    --data "user_id={userid}"                 \
+    --data "user_key={userkey}"               \
+    --data "title={message_title}"            \
+    --data "content={message_content}"        \
+    https://scn.blackforestbytes.com/send.php
+
+ +

Message Priority

+
+

+ Currently you can send a message with three different priorities: 0 (low), 1 (normal) and 2 (high). + In the app you can then configure a different behaviour for different priorities, e.g. only playing a sound if the notification is high priority. +

+

+ Priorites are either 0, 1 or 2 and are supplied in the priority parameter. + If no priority is supplied the message will get the default priority of 1. +

+
curl                                          \
+    --data "user_id={userid}"                 \
+    --data "user_key={userkey}"               \
+    --data "title={message_title}"            \
+    --data "priority={0|1|2}"                 \
+    https://scn.blackforestbytes.com/send.php
+
+ +

Message Uniqueness

+
+

+ Sometimes your script can run in an environment with an unstable connection and you want to implement an automatic re-try mechanism to send a message again if the last try failed due to bad connectivity. +

+

+ To ensure that a message is only send once you can generate a unique id for your message (I would recommend a simple uuidgen). + If you send a message with an UUID that was already used in the near past the API still returns OK, but no new message is sent. +

+

+ The message_id is optional - but if you want to use it you need to supply it via the msg_id parameter. +

+
curl                                          \
+    --data "user_id={userid}"                 \
+    --data "user_key={userkey}"               \
+    --data "title={message_title}"            \
+    --data "msg_id={message_id}"              \
+    https://scn.blackforestbytes.com/send.php
+

+ 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. +

+
+ +
+ + + + \ No newline at end of file diff --git a/web/index_sent.php b/web/index_sent.php index 68f2cf7..4149689 100644 --- a/web/index_sent.php +++ b/web/index_sent.php @@ -3,7 +3,7 @@ Simple Cloud Notifications - + @@ -12,7 +12,7 @@ -
+
@@ -42,9 +42,9 @@ Send -

Simple Cloud Notifier

+

Simple Cloud Notifier

- +
© blackforestbytes diff --git a/web/model.php b/web/model.php index d06d3e9..3af5cbe 100644 --- a/web/model.php +++ b/web/model.php @@ -17,8 +17,14 @@ function getConfig() return Statics::$CFG = require "config.php"; } -function reportError($msg) +/** + * @param String $msg + * @param Exception $e + */ +function reportError($msg, $e = null) { + if ($e != null) $msg = ($msg."\n\n[[EXCEPTION]]\n" . $e . "\n" . $e->getMessage() . "\n" . $e->getTraceAsString()); + $subject = "SCN_Server has encountered an Error at " . date("Y-m-d H:i:s") . "] "; $content = ""; @@ -34,7 +40,17 @@ function reportError($msg) $content .= '$_POST:' . "\n" . print_r($_POST, true) . "\n"; $content .= '$_FILES:' . "\n" . print_r($_FILES, true) . "\n"; - if (getConfig()['error_reporting']['send-mail'])sendMail($subject, $content, getConfig()['error_reporting']['email-error-target'], getConfig()['error_reporting']['email-error-sender']); + if (getConfig()['error_reporting']['send-mail']) sendMail($subject, $content, getConfig()['error_reporting']['email-error-target'], getConfig()['error_reporting']['email-error-sender']); +} + +/** + * @param string $subject + * @param string $content + * @param string $to + * @param string $from + */ +function sendMail($subject, $content, $to, $from) { + mail($to, $subject, $content, 'From: ' . $from); } /** @@ -93,6 +109,7 @@ function generateRandomAuthKey() * @param $header * @return array|object|string * @throws \Httpful\Exception\ConnectionErrorException + * @throws Exception */ function sendPOST($url, $body, $header) { @@ -153,7 +170,7 @@ function verifyOrderToken($tok) } catch (Exception $e) { - reportError("VerifyOrder token threw exception: " . $e . "\n" . $e->getMessage() . "\n" . $e->getTraceAsString()); + reportError("VerifyOrder token threw exception", $e); return false; } } @@ -172,4 +189,11 @@ function refreshVerifyToken() file_put_contents('.verify_accesstoken', $obj['access_token']); return $obj->access_token; +} + +function api_return($http_code, $message) +{ + http_response_code($http_code); + echo $message; + die(); } \ No newline at end of file diff --git a/web/send.php b/web/send.php index d13acc1..be0fe83 100644 --- a/web/send.php +++ b/web/send.php @@ -2,100 +2,109 @@ include_once 'model.php'; -//------------------------------------------------------------------ -sleep(1); -//------------------------------------------------------------------ - -$INPUT = array_merge($_GET, $_POST); - -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]]'])); - -//------------------------------------------------------------------ - -$user_id = $INPUT['user_id']; -$user_key = $INPUT['user_key']; -$message = $INPUT['title']; -$content = isset($INPUT['content']) ? $INPUT['content'] : ''; -$priority = isset($INPUT['priority']) ? $INPUT['priority'] : '1'; - -//------------------------------------------------------------------ - -if ($priority !== '0' && $priority !== '1' && $priority !== '2') die(json_encode(['success' => false, 'errhighlight' => 105, 'message' => 'Invalid priority'])); - -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)'])); - -//------------------------------------------------------------------ - -$pdo = getDatabase(); - -$stmt = $pdo->prepare('SELECT user_id, user_key, fcm_token, messages_sent, quota_today, is_pro, quota_day FROM users WHERE user_id = :uid LIMIT 1'); -$stmt->execute(['uid' => $user_id]); - -$datas = $stmt->fetchAll(PDO::FETCH_ASSOC); -if (count($datas)<=0) die(json_encode(['success' => false, 'errhighlight' => 101, 'message' => 'User not found'])); -$data = $datas[0]; - -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'])); - -$fcm = $data['fcm_token']; - -$new_quota = $data['quota_today'] + 1; -if ($data['quota_day'] === null || $data['quota_day'] !== date("Y-m-d")) $new_quota=1; -if ($new_quota > Statics::quota_max($data['is_pro'])) die(json_encode(['success' => false, 'errhighlight' => -1, 'message' => 'Daily quota reached ('.Statics::quota_max($data['is_pro']).')'])); - -//------------------------------------------------------------------ - -$url = "https://fcm.googleapis.com/fcm/send"; -$payload = json_encode( -[ - 'to' => $fcm, - //'dry_run' => true, - 'android' => [ 'priority' => 'high' ], - //'notification' => - //[ - // 'title' => $message, - // 'body' => $content, - //], - 'data' => - [ - 'title' => $message, - 'body' => $content, - 'priority' => $priority, - 'timestamp' => time(), - ] -]); -$header= -[ - 'Authorization' => 'key=' . getConfig()['firebase']['server_key'], - 'Content-Type' => 'application/json', -]; - try { - $httpresult = sendPOST($url, $payload, $header); + +//------------------------------------------------------------------ +//sleep(1); +//------------------------------------------------------------------ + + $INPUT = array_merge($_GET, $_POST); + + if (!isset($INPUT['user_id'])) api_return(400, json_encode(['success' => false, 'error' => 1101, 'errhighlight' => 101, 'message' => 'Missing parameter [[user_id]]'])); + if (!isset($INPUT['user_key'])) api_return(400, json_encode(['success' => false, 'error' => 1102, 'errhighlight' => 102, 'message' => 'Missing parameter [[user_token]]'])); + if (!isset($INPUT['title'])) api_return(400, json_encode(['success' => false, 'error' => 1103, 'errhighlight' => 103, 'message' => 'Missing parameter [[title]]'])); + +//------------------------------------------------------------------ + + + $user_id = $INPUT['user_id']; + $user_key = $INPUT['user_key']; + $message = $INPUT['title']; + $content = isset($INPUT['content']) ? $INPUT['content'] : ''; + $priority = isset($INPUT['priority']) ? $INPUT['priority'] : '1'; + +//------------------------------------------------------------------ + + if ($priority !== '0' && $priority !== '1' && $priority !== '2') api_return(400, json_encode(['success' => false, 'error' => 1104, 'errhighlight' => 105, 'message' => 'Invalid priority'])); + + if (strlen(trim($message)) == 0) api_return(400, json_encode(['success' => false, 'error' => 1201, 'errhighlight' => 103, 'message' => 'No title specified'])); + if (strlen($message) > 120) api_return(400, json_encode(['success' => false, 'error' => 1202, 'errhighlight' => 103, 'message' => 'Title too long (120 characters)'])); + if (strlen($content) > 10000) api_return(400, json_encode(['success' => false, 'error' => 1203, 'errhighlight' => 104, 'message' => 'Content too long (10000 characters)'])); + +//------------------------------------------------------------------ + + $pdo = getDatabase(); + + $stmt = $pdo->prepare('SELECT user_id, user_key, fcm_token, messages_sent, quota_today, is_pro, quota_day FROM users WHERE user_id = :uid LIMIT 1'); + $stmt->execute(['uid' => $user_id]); + + $datas = $stmt->fetchAll(PDO::FETCH_ASSOC); + if (count($datas)<=0) die(json_encode(['success' => false, 'error' => 1301, 'errhighlight' => 101, 'message' => 'User not found'])); + $data = $datas[0]; + + if ($data === null) api_return(401, json_encode(['success' => false, 'error' => 1301, 'errhighlight' => 101, 'message' => 'User not found'])); + if ($data['user_id'] !== (int)$user_id) api_return(401, json_encode(['success' => false, 'error' => 1302, 'errhighlight' => 101, 'message' => 'UserID not found'])); + if ($data['user_key'] !== $user_key) api_return(401, json_encode(['success' => false, 'error' => 1303, 'errhighlight' => 102, 'message' => 'Authentification failed'])); + + $fcm = $data['fcm_token']; + + $new_quota = $data['quota_today'] + 1; + if ($data['quota_day'] === null || $data['quota_day'] !== date("Y-m-d")) $new_quota=1; + if ($new_quota > Statics::quota_max($data['is_pro'])) api_return(403, json_encode(['success' => false, 'error' => 2101, 'errhighlight' => -1, 'message' => 'Daily quota reached ('.Statics::quota_max($data['is_pro']).')'])); + +//------------------------------------------------------------------ + + $url = "https://fcm.googleapis.com/fcm/send"; + $payload = json_encode( + [ + 'to' => $fcm, + //'dry_run' => true, + 'android' => [ 'priority' => 'high' ], + //'notification' => + //[ + // 'title' => $message, + // 'body' => $content, + //], + 'data' => + [ + 'title' => $message, + 'body' => $content, + 'priority' => $priority, + 'timestamp' => time(), + ] + ]); + $header= + [ + 'Authorization' => 'key=' . getConfig()['firebase']['server_key'], + 'Content-Type' => 'application/json', + ]; + + try + { + $httpresult = sendPOST($url, $payload, $header); + } + catch (Exception $e) + { + reportError("FCM communication failed", $e); + api_return(403, json_encode(['success' => false, 'error' => 9901, 'errhighlight' => -1, 'message' => 'Communication with firebase service failed.'."\n\n".'Exception: ' . $e->getMessage()])); + } + + $stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), messages_sent=messages_sent+1, quota_today=:q, quota_day=NOW() WHERE user_id = :uid'); + $stmt->execute(['uid' => $user_id, 'q' => $new_quota]); + + api_return(200, json_encode( + [ + 'success' => true, + 'message' => 'Message sent', + 'response' => $httpresult, + 'messagecount' => $data['messages_sent']+1, + 'quota' => $new_quota, + 'is_pro' => $data['is_pro'], + 'quota_max' => Statics::quota_max($data['is_pro']), + ])); } -catch (Exception $e) +catch (Exception $mex) { - die(json_encode(['success' => false, 'message' => 'Exception: ' . $e->getMessage()])); + reportError("Root try-catch triggered", $mex); } - -$stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), messages_sent=messages_sent+1, quota_today=:q, quota_day=NOW() WHERE user_id = :uid'); -$stmt->execute(['uid' => $user_id, 'q' => $new_quota]); - -echo (json_encode( -[ - 'success' => true, - 'message' => 'Message sent', - 'response' => $httpresult, - 'messagecount' => $data['messages_sent']+1, - 'quota' => $new_quota, - 'is_pro' => $data['is_pro'], - 'quota_max' => Statics::quota_max($data['is_pro']), -])); -return 0; \ No newline at end of file