quota
This commit is contained in:
parent
543f359acd
commit
a4cc8752ff
@ -21,7 +21,7 @@
|
||||
<div class="section">
|
||||
<h3 class="doc">Message sent</h3>
|
||||
<p class="doc">Message succesfully sent<br>
|
||||
<?php echo isset($_GET['quota'])?$_GET['quota']:'ERR';?>/100 remaining</p>
|
||||
<?php echo isset($_GET['quota'])?$_GET['quota']:'ERR';?>/<?php echo isset($_GET['quota_max'])?$_GET['quota_max']:'ERR';?> remaining</p>
|
||||
</div>
|
||||
</a>
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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`)
|
||||
);
|
||||
|
20
web/send.php
20
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;
|
Loading…
Reference in New Issue
Block a user