35 lines
1.2 KiB
PHP
35 lines
1.2 KiB
PHP
<?php
|
|
require_once 'db.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (isset($data['endpoint'], $data['keys']['p256dh'], $data['keys']['auth'])) {
|
|
$endpoint = $data['endpoint'];
|
|
$p256dh = $data['keys']['p256dh'];
|
|
$auth = $data['keys']['auth'];
|
|
|
|
try {
|
|
// Check if subscription already exists
|
|
$stmt = $pdo->prepare("SELECT id FROM subscriptions WHERE endpoint = ?");
|
|
$stmt->execute([$endpoint]);
|
|
|
|
if (!$stmt->fetch()) {
|
|
$stmt = $pdo->prepare("INSERT INTO subscriptions (endpoint, p256dh, auth) VALUES (?, ?, ?)");
|
|
$stmt->execute([$endpoint, $p256dh, $auth]);
|
|
}
|
|
|
|
echo json_encode(['status' => 'success']);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Database error']);
|
|
}
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid subscription data']);
|
|
}
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
|
|
}
|
|
?>
|