SimpleCloudNotifier/web/api/update.php

75 lines
2.3 KiB
PHP
Raw Normal View History

2018-09-22 01:35:41 +02:00
<?php
include_once 'model.php';
2018-09-22 18:00:00 +02:00
$INPUT = array_merge($_GET, $_POST);
2018-09-22 01:35:41 +02:00
2018-09-22 18:00:00 +02:00
if (!isset($INPUT['user_id'])) die(json_encode(['success' => false, 'message' => 'Missing parameter [[user_id]]']));
if (!isset($INPUT['user_key'])) die(json_encode(['success' => false, 'message' => 'Missing parameter [[user_key]]']));
$user_id = $INPUT['user_id'];
2018-09-23 20:00:10 +02:00
$user_key = $INPUT['user_key'];
$fcm_token = isset($INPUT['fcm_token']) ? $INPUT['fcm_token'] : null;
2018-09-22 01:35:41 +02:00
//----------------------
$pdo = getDatabase();
2018-11-12 00:15:50 +01:00
$stmt = $pdo->prepare('SELECT user_id, user_key, quota_today, quota_day, is_pro FROM users WHERE user_id = :uid LIMIT 1');
2018-09-22 01:35:41 +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, 'message' => 'User not found']));
2018-09-22 01:35:41 +02:00
$data = $datas[0];
if ($data === null) die(json_encode(['success' => false, 'message' => 'User not found']));
if ($data['user_id'] !== (int)$user_id) die(json_encode(['success' => false, 'message' => 'UserID not found']));
if ($data['user_key'] !== $user_key) die(json_encode(['success' => false, 'message' => 'Authentification failed']));
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
2018-09-22 01:35:41 +02:00
$new_userkey = generateRandomAuthKey();
if ($fcm_token === null)
{
2018-11-12 15:19:10 +01:00
// only gen new user_secret
2018-09-22 01:35:41 +02:00
$stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), user_key=:at WHERE user_id = :uid');
$stmt->execute(['uid' => $user_id, 'at' => $new_userkey]);
2018-09-23 20:00:10 +02:00
echo json_encode(
[
2018-11-12 00:15:50 +01:00
'success' => true,
'user_id' => $user_id,
2018-09-23 20:00:10 +02:00
'user_key' => $new_userkey,
'quota' => $quota,
2018-11-12 00:15:50 +01:00
'quota_max'=> Statics::quota_max($data['is_pro']),
'is_pro' => $is_pro,
'message' => 'user updated'
2018-09-23 20:00:10 +02:00
]);
2018-09-22 01:35:41 +02:00
return 0;
}
else
{
2018-11-12 15:19:10 +01:00
// update fcm and gen new user_secret
2018-09-22 01:35:41 +02:00
$stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), fcm_token=:ft, user_key=:at WHERE user_id = :uid');
$stmt->execute(['uid' => $user_id, 'ft' => $fcm_token, 'at' => $new_userkey]);
2018-11-12 15:19:10 +01:00
$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]);
2018-09-23 20:00:10 +02:00
echo json_encode(
[
'success' => true,
'user_id' => $user_id,
'user_key' => $new_userkey,
'quota' => $quota,
2018-11-12 00:15:50 +01:00
'quota_max'=> Statics::quota_max($data['is_pro']),
'is_pro' => $is_pro,
2018-09-23 20:00:10 +02:00
'message' => 'user updated'
]);
2018-09-22 01:35:41 +02:00
return 0;
}