From a4cc8752ff5d7c5e52fb81ddc9dae058b8d649be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 22 Sep 2018 19:57:00 +0200 Subject: [PATCH] quota --- web/index_sent.php | 2 +- web/js/logic.js | 1 + web/schema.sql | 5 +++++ web/send.php | 20 +++++++++++++------- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/web/index_sent.php b/web/index_sent.php index 7844f5f..8a9795b 100644 --- a/web/index_sent.php +++ b/web/index_sent.php @@ -21,7 +21,7 @@

Message sent

Message succesfully sent
- /100 remaining

+ / remaining

diff --git a/web/js/logic.js b/web/js/logic.js index dc3c682..1c5135b 100644 --- a/web/js/logic.js +++ b/web/js/logic.js @@ -55,6 +55,7 @@ function send() '?ok=' + 1 + '&message_count=' + resp.messagecount + '"a=' + resp.quota + + '"a_max=' + resp.quota_max + '&preset_user_id=' + uid.value + '&preset_user_key=' + key.value; } diff --git a/web/schema.sql b/web/schema.sql index 048e43d..5a8f95b 100644 --- a/web/schema.sql +++ b/web/schema.sql @@ -6,5 +6,10 @@ CREATE TABLE `users` `messages_sent` INT(11) NOT NULL DEFAULT '0', `timestamp_created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `timestamp_accessed` DATETIME NULL DEFAULT NULL, + + `quota_today` INT(11) NOT NULL DEFAULT '0', + `quota_day` DATE NULL DEFAULT NULL, + `quota_max` INT(11) NOT NULL DEFAULT '100', + PRIMARY KEY (`id`) ); diff --git a/web/send.php b/web/send.php index fab9b88..7aa0090 100644 --- a/web/send.php +++ b/web/send.php @@ -22,15 +22,15 @@ if ($content === null || $content === false) $content = ''; //------------------------------------------------------------------ -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)'])); +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 FROM users WHERE user_id = :uid LIMIT 1'); +$stmt = $pdo->prepare('SELECT user_id, user_key, fcm_token, messages_sent, quota_today, quota_max, quota_day FROM users WHERE user_id = :uid LIMIT 1'); $stmt->execute(['uid' => $user_id]); $datas = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -43,6 +43,10 @@ if ($data['user_key'] !== $user_key) die(json_encode(['success' => false, 'errhi $fcm = $data['fcm_token']; +$new_quota = $data['quota_today'] + 1; +if ($data['quota_day'] === null || $data['quota_day'] !== date("Y-m-d")) $new_quota=0; +if ($new_quota > $data['quota_max']) die(json_encode(['success' => false, 'errhighlight' => -1, 'message' => 'Daily quota reached ('.$data['quota_max'].')'])); + //------------------------------------------------------------------ $url = "https://fcm.googleapis.com/fcm/send"; @@ -77,14 +81,16 @@ 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]); +$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 + 'messagecount' => $data['messages_sent']+1, + 'quota'=>$new_quota, + 'quota_max'=>$data['quota_max'], ])); return 0; \ No newline at end of file