From 9cf5133469f70c90a65941a32c45570d00d85458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 17 Nov 2018 02:06:44 +0100 Subject: [PATCH] get nonack on info-call --- .../simplecloudnotifier/SCNApp.java | 1 - .../model/CMessageList.java | 18 +++ .../model/SCNSettings.java | 4 +- .../model/ServerCommunication.java | 124 +++++++++++++++++- .../service/FBMService.java | 36 +++-- web/api/ack.php | 5 +- web/api/info.php | 15 ++- web/api/model.php | 7 +- web/api/register.php | 6 +- web/api/requery.php | 56 ++++++++ web/api/update.php | 6 +- web/api/upgrade.php | 6 +- web/{api => }/send.php | 44 +++---- 13 files changed, 265 insertions(+), 63 deletions(-) create mode 100644 web/api/requery.php rename web/{api => }/send.php (60%) diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/SCNApp.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/SCNApp.java index 51086d6..7d686ec 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/SCNApp.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/SCNApp.java @@ -105,7 +105,6 @@ public class SCNApp extends Application implements LifecycleObserver [ ] - test notification channels [ ] - startup time [ ] - Delete single message (swipe right) -[ ] - Query non-ack-ed messages in app [ ] - periodically get non-ack (option - even when not in-app) [ ] - publish (+ HN post ?) diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java index 655c4d4..f425898 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java @@ -3,15 +3,19 @@ package com.blackforestbytes.simplecloudnotifier.model; import android.content.Context; import android.content.SharedPreferences; +import com.blackforestbytes.simplecloudnotifier.lib.string.Str; import com.blackforestbytes.simplecloudnotifier.view.MessageAdapter; import com.blackforestbytes.simplecloudnotifier.SCNApp; import java.lang.ref.WeakReference; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; public class CMessageList { public ArrayList Messages; + public Set AllAcks; private ArrayList> _listener = new ArrayList<>(); @@ -29,6 +33,7 @@ public class CMessageList private CMessageList() { Messages = new ArrayList<>(); + AllAcks = new HashSet<>(); SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE); int count = sharedPref.getInt("message_count", 0); @@ -42,6 +47,8 @@ public class CMessageList Messages.add(new CMessage(scnid, time, title, content, prio)); } + + AllAcks = sharedPref.getStringSet("acks", new HashSet<>()); } public CMessage add(final long scnid, final long time, final String title, final String content, final PriorityEnum pe) @@ -56,6 +63,7 @@ public class CMessageList SharedPreferences.Editor e = sharedPref.edit(); Messages.add(msg); + AllAcks.add(Long.toHexString(msg.SCN_ID)); while (Messages.size()>SCNSettings.inst().LocalCacheSize) Messages.remove(0); @@ -66,6 +74,8 @@ public class CMessageList e.putInt( "message["+count+"].priority", pe.ID); e.putLong( "message["+count+"].scnid", scnid); + e.putStringSet("acks", AllAcks); + e.apply(); for (WeakReference ref : _listener) @@ -81,6 +91,7 @@ public class CMessageList if (!run) { Messages.add(new CMessage(scnid, time, title, content, pe)); + AllAcks.add(Long.toHexString(msg.SCN_ID)); fullSave(); } @@ -119,6 +130,8 @@ public class CMessageList e.putLong( "message["+i+"].scnid", Messages.get(i).SCN_ID); } + e.putStringSet("acks", AllAcks); + e.apply(); } @@ -151,4 +164,9 @@ public class CMessageList if (_listener.get(i).get() == null) _listener.remove(i); } } + + public boolean isAck(long id) + { + return AllAcks.contains(Long.toHexString(id)); + } } diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/SCNSettings.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/SCNSettings.java index a059397..c628bd4 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/SCNSettings.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/SCNSettings.java @@ -167,7 +167,7 @@ public class SCNSettings { fcm_token_local = token; save(); - if (!fcm_token_local.equals(fcm_token_server)) ServerCommunication.update(user_id, user_key, fcm_token_local, loader); + if (!fcm_token_local.equals(fcm_token_server)) ServerCommunication.updateFCMToken(user_id, user_key, fcm_token_local, loader); } else { @@ -199,7 +199,7 @@ public class SCNSettings { if (!isConnected()) return; - ServerCommunication.update(user_id, user_key, loader); + ServerCommunication.resetSecret(user_id, user_key, loader); } // refresh account data diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/ServerCommunication.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/ServerCommunication.java index 9e9d45e..7f829b3 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/ServerCommunication.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/ServerCommunication.java @@ -5,7 +5,9 @@ import android.view.View; import com.blackforestbytes.simplecloudnotifier.SCNApp; import com.blackforestbytes.simplecloudnotifier.lib.string.Str; +import com.blackforestbytes.simplecloudnotifier.service.FBMService; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; @@ -94,12 +96,12 @@ public class ServerCommunication } } - public static void update(int id, String key, String token, View loader) + public static void updateFCMToken(int id, String key, String token, View loader) { try { Request request = new Request.Builder() - .url(BASE_URL + "update.php?user_id="+id+"&user_key="+key+"&fcm_token="+token) + .url(BASE_URL + "updateFCMToken.php?user_id="+id+"&user_key="+key+"&fcm_token="+token) .build(); client.newCall(request).enqueue(new Callback() @@ -160,12 +162,12 @@ public class ServerCommunication } } - public static void update(int id, String key, View loader) + public static void resetSecret(int id, String key, View loader) { try { Request request = new Request.Builder() - .url(BASE_URL + "update.php?user_id=" + id + "&user_key=" + key) + .url(BASE_URL + "updateFCMToken.php?user_id=" + id + "&user_key=" + key) .build(); client.newCall(request).enqueue(new Callback() { @@ -280,6 +282,12 @@ public class ServerCommunication SCNSettings.inst().save(); SCNApp.refreshAccountTab(); + + if (json_int(json, "unack_count")>0) + { + ServerCommunication.requery(id, key, loader); + } + } catch (Exception e) { Log.e("SC:info", e.toString()); SCNApp.showToast("Communication with server failed", 4000); @@ -298,6 +306,89 @@ public class ServerCommunication } } + public static void requery(int id, String key, View loader) + { + try + { + Request request = new Request.Builder() + .url(BASE_URL + "requery.php?user_id=" + id + "&user_key=" + key) + .build(); + + client.newCall(request).enqueue(new Callback() { + @Override + public void onFailure(Call call, IOException e) { + Log.e("SC:requery", e.toString()); + SCNApp.showToast("Communication with server failed", 4000); + SCNApp.runOnUiThread(() -> { + if (loader != null) loader.setVisibility(View.GONE); + }); + } + + @Override + public void onResponse(Call call, Response response) { + try (ResponseBody responseBody = response.body()) { + if (!response.isSuccessful()) + throw new IOException("Unexpected code " + response); + if (responseBody == null) throw new IOException("No response"); + + String r = responseBody.string(); + Log.d("Server::Response", r); + + JSONObject json = (JSONObject) new JSONTokener(r).nextValue(); + + if (!json_bool(json, "success")) + { + SCNApp.showToast(json_str(json, "message"), 4000); + return; + } + + int count = json_int(json, "count"); + JSONArray arr = json.getJSONArray("data"); + for (int i = 0; i < count; i++) + { + JSONObject o = arr.getJSONObject(0); + + long time = json_lng(o, "timestamp"); + String title = json_str(o, "title"); + String content = json_str(o, "body"); + PriorityEnum prio = PriorityEnum.parseAPI(json_int(o, "priority")); + long scn_id = json_lng(o, "scn_msg_id"); + + FBMService.recieveData(time, title, content, prio, scn_id, true); + } + + SCNSettings.inst().user_id = json_int(json, "user_id"); + SCNSettings.inst().quota_curr = json_int(json, "quota"); + SCNSettings.inst().quota_max = json_int(json, "quota_max"); + SCNSettings.inst().promode_server = json_bool(json, "is_pro"); + if (!json_bool(json, "fcm_token_set")) SCNSettings.inst().fcm_token_server = ""; + SCNSettings.inst().save(); + + SCNApp.refreshAccountTab(); + + if (json_int(json, "unack_count")>0) + { + ServerCommunication.requery(id, key, loader); + } + + } catch (Exception e) { + Log.e("SC:info", e.toString()); + SCNApp.showToast("Communication with server failed", 4000); + } finally { + SCNApp.runOnUiThread(() -> { + if (loader != null) loader.setVisibility(View.GONE); + }); + } + } + }); + } + catch (Exception e) + { + Log.e("SC:requery", e.toString()); + SCNApp.showToast("Communication with server failed", 4000); + } + } + public static void upgrade(int id, String key, View loader, boolean pro, String pro_token) { try @@ -370,8 +461,24 @@ public class ServerCommunication } @Override - public void onResponse(Call call, Response response) { - // ???? + public void onResponse(Call call, Response response) + { + try (ResponseBody responseBody = response.body()) { + if (!response.isSuccessful()) + throw new IOException("Unexpected code " + response); + if (responseBody == null) throw new IOException("No response"); + + String r = responseBody.string(); + Log.d("Server::Response", r); + + JSONObject json = (JSONObject) new JSONTokener(r).nextValue(); + + if (!json_bool(json, "success")) SCNApp.showToast(json_str(json, "message"), 4000); + + } catch (Exception e) { + Log.e("SC:ack", e.toString()); + SCNApp.showToast("Communication with server failed", 4000); + } } }); } @@ -396,6 +503,11 @@ public class ServerCommunication return o.getInt(key); } + private static long json_lng(JSONObject o, String key) throws JSONException + { + return o.getLong(key); + } + private static String json_str(JSONObject o, String key) throws JSONException { return o.getString(key); diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/FBMService.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/FBMService.java index 434a31d..d3c612c 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/FBMService.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/FBMService.java @@ -39,18 +39,7 @@ public class FBMService extends FirebaseMessagingService PriorityEnum prio = PriorityEnum.parseAPI(remoteMessage.getData().get("priority")); long scn_id = Long.parseLong(remoteMessage.getData().get("scn_msg_id")); - CMessage msg = CMessageList.inst().add(scn_id, time, title, content, prio); - - if (SCNApp.isBackground()) - { - NotificationService.inst().showBackground(msg); - } - else - { - NotificationService.inst().showForeground(msg); - } - - ServerCommunication.ack(SCNSettings.inst().user_id, SCNSettings.inst().user_key, msg); + recieveData(time, title, content, prio, scn_id, false); } catch (Exception e) { @@ -58,4 +47,27 @@ public class FBMService extends FirebaseMessagingService SCNApp.showToast("Recieved invalid message from server", Toast.LENGTH_LONG); } } + + public static void recieveData(long time, String title, String content, PriorityEnum prio, long scn_id, boolean alwaysAck) + { + CMessage msg = CMessageList.inst().add(scn_id, time, title, content, prio); + + if (CMessageList.inst().isAck(scn_id)) + { + Log.w("FB::MessageReceived", "Recieved ack-ed message: " + scn_id); + if (alwaysAck) ServerCommunication.ack(SCNSettings.inst().user_id, SCNSettings.inst().user_key, msg); + return; + } + + if (SCNApp.isBackground()) + { + NotificationService.inst().showBackground(msg); + } + else + { + NotificationService.inst().showForeground(msg); + } + + ServerCommunication.ack(SCNSettings.inst().user_id, SCNSettings.inst().user_key, msg); + } } \ No newline at end of file diff --git a/web/api/ack.php b/web/api/ack.php index 39ce81d..a4f333d 100644 --- a/web/api/ack.php +++ b/web/api/ack.php @@ -37,11 +37,10 @@ if (count($datas)<=0) die(json_encode(['success' => false, 'errid'=>301, 'messag $stmt = $pdo->prepare('UPDATE messages SET ack=1 WHERE scn_message_id=:smid AND sender_user_id=:uid'); $stmt->execute(['smid' => $scn_msg_id, 'uid' => $user_id]); -echo json_encode( +api_return(200, [ 'success' => true, 'prev_ack' => $datas[0]['ack'], 'new_ack' => 1, 'message' => 'ok' -]); -return 0; \ No newline at end of file +]); \ No newline at end of file diff --git a/web/api/info.php b/web/api/info.php index 0a2c64e..29dfa36 100644 --- a/web/api/info.php +++ b/web/api/info.php @@ -26,19 +26,26 @@ if ($data === null) die(json_encode(['success' => false, 'errid'=>202, 'message' if ($data['user_id'] !== (int)$user_id) die(json_encode(['success' => false, 'errid'=>203, 'message' => 'UserID not found'])); if ($data['user_key'] !== $user_key) die(json_encode(['success' => false, 'errid'=>204, 'message' => 'Authentification failed'])); + + +$stmt = $pdo->prepare('SELECT COUNT(*) FROM messages WHERE ack=0 AND sender_user_id=:uid'); +$stmt->execute(['uid' => $user_id]); +$nack_count = $stmt->fetch(PDO::FETCH_NUM); + + $quota = $data['quota_today']; $is_pro = $data['is_pro']; if ($data['quota_day'] === null || $data['quota_day'] !== date("Y-m-d")) $quota=0; -echo json_encode( +api_return(200, [ 'success' => true, + 'message' => 'ok', 'user_id' => $user_id, 'quota' => $quota, 'quota_max' => Statics::quota_max($is_pro), 'is_pro' => $is_pro, 'fcm_token_set' => ($data['fcm_token'] != null), - 'message' => 'ok' -]); -return 0; \ No newline at end of file + 'unack_count' => $nack_count, +]); \ No newline at end of file diff --git a/web/api/model.php b/web/api/model.php index 841143a..b72b6d6 100644 --- a/web/api/model.php +++ b/web/api/model.php @@ -217,10 +217,15 @@ function refreshVerifyToken() return $obj['access_token']; } +/** + * @param int $http_code + * @param array $message + */ function api_return($http_code, $message) { http_response_code($http_code); - echo $message; + header('Content-Type: application/json'); + echo json_encode($message); die(); } diff --git a/web/api/register.php b/web/api/register.php index d0600e8..e105f63 100644 --- a/web/api/register.php +++ b/web/api/register.php @@ -38,7 +38,7 @@ $stmt->execute(['uid' => $user_id, 'ft' => $fcm_token]); $pdo->commit(); -echo json_encode( +api_return(200, [ 'success' => true, 'user_id' => $user_id, @@ -47,6 +47,4 @@ echo json_encode( 'quota_max' => Statics::quota_max($ispro), 'is_pro' => $ispro, 'message' => 'New user registered' -]); - -return 0; \ No newline at end of file +]); \ No newline at end of file diff --git a/web/api/requery.php b/web/api/requery.php new file mode 100644 index 0000000..6e618e7 --- /dev/null +++ b/web/api/requery.php @@ -0,0 +1,56 @@ + false, 'errid'=>101, 'message' => 'Missing parameter [[user_id]]'])); +if (!isset($INPUT['user_key'])) die(json_encode(['success' => false, 'errid'=>102, 'message' => 'Missing parameter [[user_key]]'])); + +$user_id = $INPUT['user_id']; +$user_key = $INPUT['user_key']; + +//---------------------- + +$pdo = getDatabase(); + +$stmt = $pdo->prepare('SELECT user_id, user_key, quota_today, is_pro, quota_day, fcm_token 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, 'errid'=>201, 'message' => 'User not found'])); +$data = $datas[0]; + +if ($data === null) die(json_encode(['success' => false, 'errid'=>202, 'message' => 'User not found'])); +if ($data['user_id'] !== (int)$user_id) die(json_encode(['success' => false, 'errid'=>203, 'message' => 'UserID not found'])); +if ($data['user_key'] !== $user_key) die(json_encode(['success' => false, 'errid'=>204, 'message' => 'Authentification failed'])); + +//------------------- + +$stmt = $pdo->prepare('SELECT * FROM messages WHERE ack=0 AND sender_user_id=:uid ORDER BY `timestamp` DESC LIMIT 16'); +$stmt->execute(['uid' => $user_id]); +$nonacks_sql = $stmt->fetchAll(PDO::FETCH_ASSOC); + +$nonacks = []; + +foreach ($nonacks_sql as $nack) +{ + $nonacks []= + [ + 'title' => $nack['title'], + 'body' => $nack['content'], + 'priority' => $nack['priority'], + 'timestamp' => $nack['timestamp'], + 'usr_msg_id' => $nack['usr_message_id'], + 'scn_msg_id' => $nack['scn_message_id'], + ]; +} + +api_return(200, +[ + 'success' => true, + 'message' => 'ok', + 'count' => count($nonacks), + 'data' => $nonacks, +]); diff --git a/web/api/update.php b/web/api/update.php index edd6205..ab7da9b 100644 --- a/web/api/update.php +++ b/web/api/update.php @@ -39,7 +39,7 @@ if ($fcm_token === null) $stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), user_key=:at WHERE user_id = :uid'); $stmt->execute(['uid' => $user_id, 'at' => $new_userkey]); - echo json_encode( + api_return(200, [ 'success' => true, 'user_id' => $user_id, @@ -49,7 +49,6 @@ if ($fcm_token === null) 'is_pro' => $is_pro, 'message' => 'user updated' ]); - return 0; } else { @@ -61,7 +60,7 @@ else $stmt = $pdo->prepare('UPDATE users SET fcm_token=NULL WHERE user_id <> :uid AND fcm_token=:ft'); $stmt->execute(['uid' => $user_id, 'ft' => $fcm_token]); - echo json_encode( + api_return(200, [ 'success' => true, 'user_id' => $user_id, @@ -71,5 +70,4 @@ else 'is_pro' => $is_pro, 'message' => 'user updated' ]); - return 0; } \ No newline at end of file diff --git a/web/api/upgrade.php b/web/api/upgrade.php index 091679c..130dd30 100644 --- a/web/api/upgrade.php +++ b/web/api/upgrade.php @@ -45,7 +45,7 @@ if ($ispro) $stmt = $pdo->prepare('UPDATE users SET is_pro=0, pro_token=NULL WHERE user_id <> :uid AND pro_token = :ptk'); $stmt->execute(['uid' => $user_id, 'ptk' => $pro_token]); - echo json_encode( + api_return(200, [ 'success' => true, 'user_id' => $user_id, @@ -54,7 +54,6 @@ if ($ispro) 'is_pro' => true, 'message' => 'user updated' ]); - return 0; } else { @@ -63,7 +62,7 @@ else $stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), is_pro=0, pro_token=NULL WHERE user_id = :uid'); $stmt->execute(['uid' => $user_id]); - echo json_encode( + api_return(200, [ 'success' => true, 'user_id' => $user_id, @@ -72,5 +71,4 @@ else 'is_pro' => false, 'message' => 'user updated' ]); - return 0; } diff --git a/web/api/send.php b/web/send.php similarity index 60% rename from web/api/send.php rename to web/send.php index dd6ca22..36eb451 100644 --- a/web/api/send.php +++ b/web/send.php @@ -1,6 +1,6 @@ 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)']); $INPUT = array_merge($_GET, $_POST); - if (!isset($INPUT['user_id'])) api_return(400, json_encode(['success' => false, 'error' => ERR::MISSING_UID, 'errhighlight' => 101, 'message' => 'Missing parameter [[user_id]]'])); - if (!isset($INPUT['user_key'])) api_return(400, json_encode(['success' => false, 'error' => ERR::MISSING_TOK, 'errhighlight' => 102, 'message' => 'Missing parameter [[user_token]]'])); - if (!isset($INPUT['title'])) api_return(400, json_encode(['success' => false, 'error' => ERR::MISSING_TITLE, 'errhighlight' => 103, 'message' => 'Missing parameter [[title]]'])); + if (!isset($INPUT['user_id'])) api_return(400, ['success' => false, 'error' => ERR::MISSING_UID, 'errhighlight' => 101, 'message' => 'Missing parameter [[user_id]]']); + if (!isset($INPUT['user_key'])) api_return(400, ['success' => false, 'error' => ERR::MISSING_TOK, 'errhighlight' => 102, 'message' => 'Missing parameter [[user_token]]']); + if (!isset($INPUT['title'])) api_return(400, ['success' => false, 'error' => ERR::MISSING_TITLE, 'errhighlight' => 103, 'message' => 'Missing parameter [[title]]']); //------------------------------------------------------------------ @@ -29,12 +29,12 @@ try //------------------------------------------------------------------ - if ($priority !== '0' && $priority !== '1' && $priority !== '2') api_return(400, json_encode(['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, json_encode(['success' => false, 'error' => ERR::NO_TITLE, 'errhighlight' => 103, 'message' => 'No title specified'])); - if (strlen($message) > 120) api_return(400, json_encode(['success' => false, 'error' => ERR::TITLE_TOO_LONG, 'errhighlight' => 103, 'message' => 'Title too long (120 characters)'])); - if (strlen($content) > 10000) api_return(400, json_encode(['success' => false, 'error' => ERR::CONTENT_TOO_LONG, 'errhighlight' => 104, 'message' => 'Content too long (10000 characters)'])); - if ($usrmsgid != null && strlen($usrmsgid) > 64) api_return(400, json_encode(['success' => false, 'error' => ERR::USR_MSG_ID_TOO_LONG, 'errhighlight' => -1, 'message' => 'MessageID too long (64 characters)'])); + if (strlen(trim($message)) == 0) api_return(400, ['success' => false, 'error' => ERR::NO_TITLE, 'errhighlight' => 103, 'message' => 'No title specified']); + if (strlen($message) > 120) api_return(400, ['success' => false, 'error' => ERR::TITLE_TOO_LONG, 'errhighlight' => 103, 'message' => 'Title too long (120 characters)']); + if (strlen($content) > 10000) api_return(400, ['success' => false, 'error' => ERR::CONTENT_TOO_LONG, 'errhighlight' => 104, 'message' => 'Content too long (10000 characters)']); + if ($usrmsgid != null && strlen($usrmsgid) > 64) api_return(400, ['success' => false, 'error' => ERR::USR_MSG_ID_TOO_LONG, 'errhighlight' => -1, 'message' => 'MessageID too long (64 characters)']); //------------------------------------------------------------------ @@ -47,19 +47,19 @@ try if (count($datas)<=0) die(json_encode(['success' => false, 'error' => ERR::USER_NOT_FOUND, 'errhighlight' => 101, 'message' => 'User not found'])); $data = $datas[0]; - if ($data === null) api_return(401, json_encode(['success' => false, 'error' => ERR::USER_NOT_FOUND, 'errhighlight' => 101, 'message' => 'User not found'])); - if ($data['user_id'] !== (int)$user_id) api_return(401, json_encode(['success' => false, 'error' => ERR::USER_NOT_FOUND, 'errhighlight' => 101, 'message' => 'UserID not found'])); - if ($data['user_key'] !== $user_key) api_return(401, json_encode(['success' => false, 'error' => ERR::USER_AUTH_FAILED, 'errhighlight' => 102, 'message' => 'Authentification failed'])); + if ($data === null) api_return(401, ['success' => false, 'error' => ERR::USER_NOT_FOUND, 'errhighlight' => 101, 'message' => 'User not found']); + if ($data['user_id'] !== (int)$user_id) api_return(401, ['success' => false, 'error' => ERR::USER_NOT_FOUND, 'errhighlight' => 101, 'message' => 'UserID not found']); + if ($data['user_key'] !== $user_key) api_return(401, ['success' => false, 'error' => ERR::USER_AUTH_FAILED, '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' => ERR::QUOTA_REACHED, 'errhighlight' => -1, 'message' => 'Daily quota reached ('.Statics::quota_max($data['is_pro']).')'])); + if ($new_quota > Statics::quota_max($data['is_pro'])) api_return(403, ['success' => false, 'error' => ERR::QUOTA_REACHED, 'errhighlight' => -1, 'message' => 'Daily quota reached ('.Statics::quota_max($data['is_pro']).')']); if ($fcm == null || $fcm == '' || $fcm == false) { - api_return(412, json_encode(['success' => false, 'error' => ERR::NO_DEVICE_LINKED, 'errhighlight' => -1, 'message' => 'No device linked with this account'])); + api_return(412, ['success' => false, 'error' => ERR::NO_DEVICE_LINKED, 'errhighlight' => -1, 'message' => 'No device linked with this account']); } //------------------------------------------------------------------ @@ -71,7 +71,7 @@ try if (count($stmt->fetchAll(PDO::FETCH_ASSOC))>0) { - api_return(200, json_encode( + api_return(200, [ 'success' => true, 'message' => 'Message already sent', @@ -81,7 +81,7 @@ try 'quota' => $data['quota_today'], 'is_pro' => $data['is_pro'], 'quota_max' => Statics::quota_max($data['is_pro']), - ])); + ]); } } @@ -138,14 +138,14 @@ try { reportError("FCM communication failed (success_1 <> true)\n\n".$httpresult); $pdo->rollBack(); - api_return(500, json_encode(['success' => false, 'error' => ERR::FIREBASE_COM_ERRORED, 'errhighlight' => -1, 'message' => 'Communication with firebase service failed.'])); + api_return(500, ['success' => false, 'error' => ERR::FIREBASE_COM_ERRORED, 'errhighlight' => -1, 'message' => 'Communication with firebase service failed.']); } } catch (Exception $e) { reportError("FCM communication failed", $e); $pdo->rollBack(); - api_return(500, json_encode(['success' => false, 'error' => ERR::FIREBASE_COM_FAILED, 'errhighlight' => -1, 'message' => 'Communication with firebase service failed.'."\n\n".'Exception: ' . $e->getMessage()])); + api_return(500, ['success' => false, 'error' => ERR::FIREBASE_COM_FAILED, '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'); @@ -156,7 +156,7 @@ try $pdo->commit(); - api_return(200, json_encode( + api_return(200, [ 'success' => true, 'error' => ERR::NO_ERROR, @@ -169,11 +169,11 @@ try 'is_pro' => $data['is_pro'], 'quota_max' => Statics::quota_max($data['is_pro']), 'scn_msg_id' => $scn_msg_id, - ])); + ]); } catch (Exception $mex) { reportError("Root try-catch triggered", $mex); if ($pdo->inTransaction()) $pdo->rollBack(); - api_return(500, json_encode(['success' => false, 'error' => ERR::INTERNAL_EXCEPTION, 'errhighlight' => -1, 'message' => 'PHP script threw exception.'."\n\n".'Exception: ' . $e->getMessage()])); + api_return(500, ['success' => false, 'error' => ERR::INTERNAL_EXCEPTION, 'errhighlight' => -1, 'message' => 'PHP script threw exception.'."\n\n".'Exception: ' . $e->getMessage()]); }