Files
yogibook_aury_new/public/api/register_device.php
T
2026-08-15 12:00:28 +03:00

69 lines
1.8 KiB
PHP

<?php
require_once __DIR__ . '/_bootstrap.php';
$userId = (int) $user->id;
$metodo = $_SERVER['REQUEST_METHOD'] ?? 'POST';
$input = json_decode((string) file_get_contents('php://input'), true);
if (!is_array($input)) {
$input = [];
}
$deviceToken = trim((string) ($input['device_token'] ?? ''));
if ($deviceToken === '') {
http_response_code(422);
echo json_encode(['success' => false, 'error' => 'device_token mancante']);
exit;
}
if ($metodo === 'DELETE') {
$stmt = $db->prepare("
UPDATE userdevicetoken
SET active = 'N'
WHERE devicetoken = :token AND iduser = :uid
");
$stmt->execute([':token' => $deviceToken, ':uid' => $userId]);
echo json_encode(['success' => true, 'deactivated' => $stmt->rowCount()]);
exit;
}
$platform = strtolower(trim((string) ($input['platform'] ?? 'android')));
if (!in_array($platform, ['android', 'ios'], true)) {
$platform = 'android';
}
$appVersion = trim((string) ($input['app_version'] ?? ''));
if ($appVersion === '') {
$appVersion = null;
}
$stmt = $db->prepare("
INSERT INTO userdevicetoken (iduser, devicetoken, platform, appversion, active, lastseen_at)
VALUES (:uid, :token, :platform, :versione, 'Y', NOW())
ON DUPLICATE KEY UPDATE
iduser = VALUES(iduser),
platform = VALUES(platform),
appversion = COALESCE(VALUES(appversion), appversion),
active = 'Y',
lastseen_at = NOW()
");
$stmt->execute([
':uid' => $userId,
':token' => $deviceToken,
':platform' => $platform,
':versione' => $appVersion,
]);
$stmt = $db->prepare("SELECT pushnotification FROM auth_users WHERE id = :uid LIMIT 1");
$stmt->execute([':uid' => $userId]);
$pushEnabled = (string) ($stmt->fetchColumn() ?: 'Y');
echo json_encode([
'success' => true,
'push_enabled' => $pushEnabled,
]);