-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
351 lines (299 loc) · 13.4 KB
/
api.php
File metadata and controls
351 lines (299 loc) · 13.4 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* API.php
* Proje için tüm AJAX isteklerini (GET/POST) ve Veritabanı işlemlerini (CRUD) yönetir.
* Yanıtları JSON formatında döndürür.
*/
session_start();
// Veritabanı bağlantısını ve gerekli ayarları içeren dosyayı dahil et
include_once 'includes/db.php';
header('Content-Type: application/json'); // Yanıtın JSON olacağını belirt
$response = ['success' => false, 'message' => 'Geçersiz İşlem'];
$action = isset($_GET['action']) ? $_GET['action'] : '';
if (empty($action)) {
echo json_encode($response);
exit();
}
// Oturum değişkenlerinden kullanıcı ve rol bilgilerini al
$user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null; // Giriş yapmış kullanıcının ID'si
$user_role = isset($_SESSION['user_role']) ? $_SESSION['user_role'] : 'guest'; // Kullanıcının yetki rolü
// --- A. YAPIM LİSTESİNİ ÇEKME (GET) ---
if ($action === 'get_films' && $_SERVER['REQUEST_METHOD'] === 'GET') {
// yapimlar tablosundan ad ve id bilgilerini çek
$sql = "SELECT id, ad FROM yapimlar ORDER BY ad ASC";
$result = $conn->query($sql);
$films = [];
if ($result && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$films[] = $row;
}
$response = ['success' => true, 'films' => $films, 'message' => 'Yapımlar başarıyla çekildi.'];
} else {
$response['message'] = 'Veritabanında yapım bulunamadı.';
}
}
// --- B. MİSAFİR ÇEVİRME HAKKINI AZALTMA (POST)
elseif ($action === 'guest_spin' && $_SERVER['REQUEST_METHOD'] === 'POST') {
$max_spins_guest = 3;
$cookie_name = "wheel_spin_data";
$remaining_spins = 0;
if ($user_id !== null) {
$response = ['success' => true, 'message' => 'Girişli kullanıcı için kısıtlama yok.'];
} else {
$data = json_decode($_COOKIE[$cookie_name] ?? '{}', true);
$spins_used = $data['spins'] ?? 0;
$last_spin_date = $data['date'] ?? date('Y-m-d');
// Yeniden gün kontrolü (Tekrar eden istekler için güvenlik)
if ($last_spin_date !== date('Y-m-d')) {
$spins_used = 0;
}
if ($spins_used < $max_spins_guest) {
$spins_used++; // Hakkı kullan
$remaining_spins = $max_spins_guest - $spins_used;
// Yeni Çerez Verisini Ayarla
$new_data = json_encode(['spins' => $spins_used, 'date' => date('Y-m-d')]);
setcookie($cookie_name, $new_data, time() + (86400), "/");
$response = ['success' => true, 'remaining_spins' => $remaining_spins, 'message' => 'Çevirme hakkı başarıyla azaltıldı.'];
} else {
$response = ['success' => false, 'message' => 'Günlük çevirme hakkınız doldu.'];
}
}
}
// --- C. FAVORİ EKLEME (POST) ---
elseif ($action === 'add_favorite' && $_SERVER['REQUEST_METHOD'] === 'POST') {
if ($user_id === null) {
$response['message'] = 'Bu işlemi yapmak için giriş yapmalısınız.';
} else {
$yapim_id = isset($_POST['yapim_id']) ? (int)$_POST['yapim_id'] : 0;
if ($yapim_id > 0) {
// Favorilerde zaten var mı kontrol et
$stmt = $conn->prepare("SELECT id FROM favoriler WHERE kullanici_id = ? AND yapim_id = ?");
$stmt->bind_param("ii", $user_id, $yapim_id);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$response['message'] = 'Bu yapım zaten favorilerinizde.';
} else {
// Favorilere ekle
$insert_stmt = $conn->prepare("INSERT INTO favoriler (kullanici_id, yapim_id) VALUES (?, ?)");
$insert_stmt->bind_param("ii", $user_id, $yapim_id);
if ($insert_stmt->execute()) {
$response = ['success' => true, 'message' => 'Favorilere başarıyla eklendi.'];
} else {
$response['message'] = 'Favorilere eklenirken hata oluştu: ' . $insert_stmt->error;
}
$insert_stmt->close();
}
$stmt->close();
} else {
$response['message'] = 'Geçersiz yapım ID.';
}
}
}
// --- D. YORUM EKLEME (POST) ---
elseif ($action === 'add_comment' && $_SERVER['REQUEST_METHOD'] === 'POST') {
if ($user_id === null) {
$response['message'] = 'Yorum yapmak için giriş yapmalısınız.';
} else {
$yapim_id = isset($_POST['yapim_id']) ? (int)$_POST['yapim_id'] : 0;
$metin = isset($_POST['metin']) ? trim($_POST['metin']) : '';
$tarih = date('Y-m-d H:i:s');
// Kullanıcının adını oturumdan alıyoruz
$kullanici_ad = isset($_SESSION['username']) ? $_SESSION['username'] : 'Misafir Kullanıcı';
if ($yapim_id > 0 && !empty($metin)) {
// Hazırlanmış ifade ile yorumu veritabanına ekle
$stmt = $conn->prepare("INSERT INTO yorumlar (kullanici_id, yapim_id, metin, tarih) VALUES (?, ?, ?, ?)");
$stmt->bind_param("iiss", $user_id, $yapim_id, $metin, $tarih);
if ($stmt->execute()) {
// Yeni eklenen yorumun ID'sini al
$new_comment_id = $conn->insert_id;
$response = [
'success' => true,
'message' => 'Yorumunuz başarıyla eklendi.',
'comment_id' => $new_comment_id, // JS'e gönderiyoruz
'kullanici_ad' => $kullanici_ad // JS'e kullanıcı adını gönderiyoruz
];
} else {
$response['message'] = 'Yorum eklenirken hata oluştu: ' . $stmt->error;
}
$stmt->close();
} else {
$response['message'] = 'Geçersiz yapım ID veya yorum metni boş.';
}
}
}
// --- E. FİLM DETAYLARINI ÇEKME (GET) ---
elseif ($action === 'get_film_details' && $_SERVER['REQUEST_METHOD'] === 'GET') {
// 1. Gelen yapim_id'yi güvenli bir şekilde al
$yapim_id = isset($_GET['yapim_id']) ? (int)$_GET['yapim_id'] : 0;
if ($yapim_id === 0) {
$response['message'] = 'Geçersiz yapım ID.';
echo json_encode($response);
$conn->close();
exit();
}
$details = [];
$is_favorite = false;
$comments = [];
// 2. Yapım Detaylarını Çek (ad, tur, yil, aciklama, resim_url)
$stmt = $conn->prepare("SELECT ad, tur, yil, aciklama, resim_url FROM yapimlar WHERE id = ?");
$stmt->bind_param("i", $yapim_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$details = $result->fetch_assoc();
}
$stmt->close();
// 3. Kullanıcının Favori Durumunu Kontrol Et (Sadece giriş yapmışsa)
if ($user_id !== null) {
$fav_stmt = $conn->prepare("SELECT id FROM favoriler WHERE kullanici_id = ? AND yapim_id = ?");
$fav_stmt->bind_param("ii", $user_id, $yapim_id);
$fav_stmt->execute();
$fav_stmt->store_result(); // MySQL'da sonuç sayısını almak için kritik!
$is_favorite = ($fav_stmt->num_rows > 0);
$fav_stmt->close();
}
// 4. Yorumları Çek (Kullanıcı adlarıyla birlikte)
$comm_stmt = $conn->prepare("
SELECT y.metin, y.tarih, k.ad AS kullanici_ad
FROM yorumlar y
JOIN kullanicilar k ON y.kullanici_id = k.id
WHERE y.yapim_id = ?
ORDER BY y.tarih DESC
");
$comm_stmt->bind_param("i", $yapim_id);
$comm_stmt->execute();
$comm_result = $comm_stmt->get_result();
while ($row = $comm_result->fetch_assoc()) {
$comments[] = $row;
}
$comm_stmt->close();
// 5. Sonucu Hazırla
if (!empty($details)) {
$response = [
'success' => true,
'details' => $details,
'is_favorite' => $is_favorite,
'comments' => $comments
];
} else {
$response['message'] = 'Film/Dizi bulunamadı.';
}
}
// api.php - delete_comment aksiyonu
elseif ($action === 'delete_comment' && $_SERVER['REQUEST_METHOD'] === 'POST') {
$comment_id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
if ($user_id === null) {
$response['message'] = 'Giriş yapmalısınız.';
} elseif ($comment_id > 0) {
// Eğer admin ise, sadece ID'ye göre siler.
if ($user_role === 'admin') {
// Admin: Herkesin yorumunu silebilir
$stmt = $conn->prepare("DELETE FROM yorumlar WHERE id = ?");
$stmt->bind_param("i", $comment_id);
} else {
// Normal kullanıcı: Sadece kendine ait yorumu silebilir
$stmt = $conn->prepare("DELETE FROM yorumlar WHERE id = ? AND kullanici_id = ?");
$stmt->bind_param("ii", $comment_id, $user_id);
}
if ($stmt->execute() && $stmt->affected_rows > 0) {
$response = ['success' => true, 'message' => 'Yorum başarıyla silindi.'];
} else {
$response['message'] = 'Yorum silinemedi veya size ait değil.';
}
$stmt->close();
} else {
$response['message'] = 'Geçersiz yorum ID.';
}
}
// --- G. FAVORİ KALDIRMA (POST) ---
elseif ($action === 'remove_favorite' && $_SERVER['REQUEST_METHOD'] === 'POST') {
$fav_id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
if ($user_id === null) {
$response['message'] = 'Giriş yapmalısınız.';
} elseif ($fav_id > 0) {
// Yalnızca kullanıcının kendi favorisini kaldırmasına izin ver
$stmt = $conn->prepare("DELETE FROM favoriler WHERE id = ? AND kullanici_id = ?");
$stmt->bind_param("ii", $fav_id, $user_id);
if ($stmt->execute() && $stmt->affected_rows > 0) {
$response = ['success' => true, 'message' => 'Favori başarıyla kaldırıldı.'];
} else {
$response['message'] = 'Favori kaldırılamadı veya size ait değil.';
}
$stmt->close();
} else {
$response['message'] = 'Geçersiz favori ID.';
}
}
// --- H. YAPIM SİLME (POST) ---
elseif ($action === 'delete_yapim' && $_SERVER['REQUEST_METHOD'] === 'POST') {
$yapim_id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
// Güvenlik kontrolü: Kullanıcı admin mi? (Header'dan rol kontrolü yapılmış olsa da ekstra önlem)
if ($user_id === null || $user_role !== 'admin') {
$response['message'] = 'Bu işlemi yapmaya yetkiniz yok.';
goto finish_admin_api;
}
if ($yapim_id > 0) {
// 1. İlgili favorileri sil (Foreign Key kısıtlaması nedeniyle)
$conn->query("DELETE FROM favoriler WHERE yapim_id = $yapim_id");
// 2. İlgili yorumları sil (Foreign Key kısıtlaması nedeniyle)
$conn->query("DELETE FROM yorumlar WHERE yapim_id = $yapim_id");
// 3. Yapımı sil
$stmt = $conn->prepare("DELETE FROM yapimlar WHERE id = ?");
$stmt->bind_param("i", $yapim_id);
if ($stmt->execute() && $stmt->affected_rows > 0) {
$response = ['success' => true, 'message' => 'Yapım başarıyla silindi.'];
} else {
$response['message'] = 'Yapım silinemedi veya bulunamadı.';
}
$stmt->close();
} else {
$response['message'] = 'Geçersiz yapım ID.';
}
}
// --- I. KULLANICI SİLME (POST) ---
elseif ($action === 'delete_user' && $_SERVER['REQUEST_METHOD'] === 'POST') {
$target_user_id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
// Güvenlik Kontrolü: İşlemi yapan admin mi ve geçerli bir ID mi?
if ($user_id === null || $user_role !== 'admin' || $target_user_id === 0) {
$response['message'] = 'Bu işlemi yapmaya yetkiniz yok veya geçersiz kullanıcı.';
goto finish_admin_api; // Yetkisiz/Geçersiz durum
}
// Ek Güvenlik: Admin kendi kendini silemez!
if ($target_user_id === $user_id) {
$response['message'] = 'Kendi hesabınızı bu panelden silemezsiniz.';
goto finish_admin_api;
}
// Ek Güvenlik: Başka bir admini silemez!
$target_user_role_stmt = $conn->prepare("SELECT rol FROM kullanicilar WHERE id = ?");
$target_user_role_stmt->bind_param("i", $target_user_id);
$target_user_role_stmt->execute();
$target_user_role_result = $target_user_role_stmt->get_result();
$target_user_role = $target_user_role_result->fetch_assoc()['rol'] ?? 'user';
$target_user_role_stmt->close();
if ($target_user_role === 'admin') {
$response['message'] = 'Başka bir yöneticiyi silemezsiniz.';
goto finish_admin_api;
}
try {
// 1. İlişkili verileri temizle (Foreign Key temizliği)
$conn->query("DELETE FROM yorumlar WHERE kullanici_id = $target_user_id");
$conn->query("DELETE FROM favoriler WHERE kullanici_id = $target_user_id");
// 2. Kullanıcıyı sil
$stmt = $conn->prepare("DELETE FROM kullanicilar WHERE id = ?");
$stmt->bind_param("i", $target_user_id);
if ($stmt->execute() && $stmt->affected_rows > 0) {
$response = ['success' => true, 'message' => 'Kullanıcı ve tüm verileri başarıyla silindi.'];
} else {
$response['message'] = 'Kullanıcı silinemedi veya bulunamadı.';
}
$stmt->close();
} catch (Exception $e) {
$response['message'] = 'Veritabanı hatası: Silme işlemi tamamlanamadı.';
}
}
// Tüm işlemler bittiğinde yanıtı JSON olarak döndür ve bağlantıyı kapat
finish_admin_api:
echo json_encode($response);
$conn->close();
?>