-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate.php
More file actions
65 lines (57 loc) · 1.98 KB
/
Copy pathrate.php
File metadata and controls
65 lines (57 loc) · 1.98 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
defined('AWAN') or die();
// This file is routed via _router.php as POST /plugins/rate
require_once __DIR__ . '/../_bootstrap.php';
header('Content-Type: application/json');
if (!$auth->check()) {
http_response_code(401);
echo json_encode(['error' => 'Login required']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
Security::verifyCsrf();
$pluginId = (int)($_POST['plugin_id'] ?? 0);
$rating = (int)($_POST['rating'] ?? 0);
if ($pluginId <= 0 || $rating < 1 || $rating > 5) {
http_response_code(400);
echo json_encode(['error' => 'Invalid rating (1-5) or plugin ID']);
exit;
}
$plugin = $db->fetch("SELECT id, name FROM plugins WHERE id = ? AND status = 'active'", [$pluginId]);
if (!$plugin) {
http_response_code(404);
echo json_encode(['error' => 'Plugin not found']);
exit;
}
try {
$existing = $db->fetch(
"SELECT id FROM plugin_ratings WHERE plugin_id = ? AND user_id = ?",
[$pluginId, $auth->id()]
);
if ($existing) {
$db->update('plugin_ratings', ['rating' => $rating, 'updated_at' => date('Y-m-d H:i:s')], 'id = ?', [$existing['id']]);
$action = 'updated';
} else {
$db->insert('plugin_ratings', [
'plugin_id' => $pluginId,
'user_id' => $auth->id(),
'rating' => $rating,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
$action = 'created';
}
// Fetch updated average
$avg = $db->fetch(
"SELECT ROUND(AVG(rating),1) AS avg, COUNT(*) AS cnt FROM plugin_ratings WHERE plugin_id = ?",
[$pluginId]
);
echo json_encode(['ok' => true, 'action' => $action, 'avg' => (float)($avg['avg'] ?? 0), 'count' => (int)($avg['cnt'] ?? 0)]);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}