-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle_subscription.php
More file actions
49 lines (39 loc) · 1.46 KB
/
Copy pathtoggle_subscription.php
File metadata and controls
49 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
// Define a constant to protect included files from direct access
define('INCLUDED_VIA_APP', true);
// Include initialization (handles session and db)
require_once __DIR__ . '/includes/init.php';
global $mysqli, $conn;
// Set response header to JSON
header('Content-Type: application/json');
// Ensure user is logged in
if (!isset($_SESSION['user_id'])) {
echo json_encode(['status' => 'error', 'message' => 'Access Denied: You must be logged in.']);
exit;
}
$user_id = $_SESSION['user_id'];
// Accept POST requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['status' => 'error', 'message' => 'Invalid request method. Only POST is allowed.']);
exit;
}
// CSRF Validation
$csrf_token = $_POST['csrf_token'] ?? '';
if (!validateCSRFToken($csrf_token)) {
echo json_encode(['status' => 'error', 'message' => 'Security check failed. Please refresh and try again.']);
exit;
}
$item_id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
$item_type = $_POST['type'] ?? '';
// Validate parameters
if ($item_id <= 0 || ($item_type !== 'wine' && $item_type !== 'tnote' && $item_type !== 'blog')) {
echo json_encode(['status' => 'error', 'message' => 'Invalid parameters provided.']);
exit;
}
try {
$action = toggleSubscription($conn, $user_id, $item_id, $item_type);
echo json_encode(['status' => 'success', 'action' => $action]);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]);
}
exit;