File size: 2,642 Bytes
84e853d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
```php
<?php
require_once 'config.php';

// JWT verification middleware
function verifyToken() {
    $headers = apache_request_headers();
    if(!isset($headers['Authorization'])) {
        http_response_code(401);
        echo json_encode(array("message" => "Access Denied. No token provided."));
        exit;
    }

    try {
        $token = str_replace('Bearer ', '', $headers['Authorization']);
        $decoded = JWT::decode($token, JWT_SECRET, array('HS256'));
        return $decoded;
    } catch(Exception $e) {
        http_response_code(401);
        echo json_encode(array("message" => "Invalid token", "error" => $e->getMessage()));
        exit;
    }
}

// GET all products
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $stmt = $pdo->query("SELECT * FROM products");
    $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($products);
}

// POST new product (Admin only)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verifyToken();
    
    $data = json_decode(file_get_contents("php://input"));
    
    $stmt = $pdo->prepare("INSERT INTO products (name, description, price, stock, image_url) VALUES (?, ?, ?, ?, ?)");
    $stmt->execute([$data->name, $data->description, $data->price, $data->stock, $data->image_url]);
    
    $product_id = $pdo->lastInsertId();
    $stmt = $pdo->query("SELECT * FROM products WHERE id = $product_id");
    $product = $stmt->fetch(PDO::FETCH_ASSOC);
    
    echo json_encode($product);
}

// UPDATE product (Admin only)
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
    verifyToken();
    
    $data = json_decode(file_get_contents("php://input"));
    $id = $_GET['id'] ?? null;
    
    if($id) {
        $stmt = $pdo->prepare("UPDATE products SET name = ?, description = ?, price = ?, stock = ?, image_url = ? WHERE id = ?");
        $stmt->execute([$data->name, $data->description, $data->price, $data->stock, $data->image_url, $id]);
        
        echo json_encode(array("success" => true, "message" => "Product updated"));
    } else {
        http_response_code(400);
        echo json_encode(array("success" => false, "message" => "Product ID required"));
    }
}

// DELETE product (Admin only)
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
    verifyToken();
    
    $id = $_GET['id'] ?? null;
    
    if($id) {
        $stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
        $stmt->execute([$id]);
        
        echo json_encode(array("success" => true, "message" => "Product deleted"));
    } else {
        http_response_code(400);
        echo json_encode(array("success" => false, "message" => "Product ID required"));
    }
}
?>
```