SimpleCloudNotifier/web/api/info.php

51 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2018-09-23 20:00:10 +02:00
<?php
include_once 'model.php';
$INPUT = array_merge($_GET, $_POST);
if (!isset($INPUT['user_id'])) die(json_encode(['success' => 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'];
2018-09-26 23:13:50 +02:00
$user_key = $INPUT['user_key'];
2018-09-23 20:00:10 +02:00
//----------------------
$pdo = getDatabase();
2018-11-12 15:19:10 +01:00
$stmt = $pdo->prepare('SELECT user_id, user_key, quota_today, is_pro, quota_day, fcm_token FROM users WHERE user_id = :uid LIMIT 1');
2018-09-23 20:00:10 +02:00
$stmt->execute(['uid' => $user_id]);
$datas = $stmt->fetchAll(PDO::FETCH_ASSOC);
2018-10-20 14:57:05 +02:00
if (count($datas)<=0) die(json_encode(['success' => false, 'errid'=>201, 'message' => 'User not found']));
2018-09-23 20:00:10 +02:00
$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']));
2018-11-17 02:06:44 +01:00
$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);
2018-09-23 20:00:10 +02:00
$quota = $data['quota_today'];
2018-11-12 00:15:50 +01:00
$is_pro = $data['is_pro'];
2018-09-23 20:00:10 +02:00
if ($data['quota_day'] === null || $data['quota_day'] !== date("Y-m-d")) $quota=0;
2018-11-17 02:06:44 +01:00
api_return(200,
2018-09-23 20:00:10 +02:00
[
2018-11-12 15:19:10 +01:00
'success' => true,
2018-11-17 02:06:44 +01:00
'message' => 'ok',
2018-11-12 15:19:10 +01:00
'user_id' => $user_id,
'quota' => $quota,
'quota_max' => Statics::quota_max($is_pro),
'is_pro' => $is_pro,
'fcm_token_set' => ($data['fcm_token'] != null),
2018-11-17 02:06:44 +01:00
'unack_count' => $nack_count,
]);