-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.php
More file actions
483 lines (411 loc) · 17.2 KB
/
config.php
File metadata and controls
483 lines (411 loc) · 17.2 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
date_default_timezone_set('Europe/Istanbul');
// Kick.com API Kimlik Bilgileri
define('KICK_CLIENT_ID', 'YOUROWNID');
define('KICK_CLIENT_SECRET', 'YOUROWNKEY');
define('KICK_BROADCASTER_ID', 'YOUROWNBROADCESTERID'); // Kanal sahibinin (Broadcaster) User ID'si
define('KICK_OAUTH_SCOPES', 'user:read channel:read chat:write events:subscribe moderation:ban moderation:chat_message:manage');
// Kendi local veya sunucu adresine göre burayı düzenle. Kick App ayarlarındakiyle BİREBİR aynı olmalıdır.
define('KICK_REDIRECT_URI', 'https://callbackurlhere');
// Veritabanı Ayarları
define('DB_HOST', 'localhost');
define('DB_NAME', 'DBNAME');
define('DB_USER', 'USER');
define('DB_PASS', 'PASSWORDHERE');
try {
$db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4", DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET time_zone = '+03:00'");
$db->exec("CREATE TABLE IF NOT EXISTS chat_messages (
id INT AUTO_INCREMENT PRIMARY KEY,
message_id VARCHAR(255) UNIQUE,
sender_id VARCHAR(255),
sender_username VARCHAR(255),
sender_badges TEXT,
content TEXT,
created_at DATETIME
)");
$db->exec("CREATE TABLE IF NOT EXISTS channel_events (
id INT AUTO_INCREMENT PRIMARY KEY,
event_type VARCHAR(50),
username VARCHAR(255),
description TEXT,
created_at DATETIME
)");
// Kullanıcı istatistikleri ve durum tablosu
$db->exec("CREATE TABLE IF NOT EXISTS chat_users (
user_id VARCHAR(255) PRIMARY KEY,
username VARCHAR(255),
follow_date DATETIME NULL,
message_count INT DEFAULT 0,
ban_count INT DEFAULT 0,
timeout_count INT DEFAULT 0,
is_banned TINYINT(1) DEFAULT 0,
timeout_expires_at DATETIME NULL,
deleted_message_count INT DEFAULT 0,
is_subscriber TINYINT(1) DEFAULT 0,
is_vip TINYINT(1) DEFAULT 0,
is_og TINYINT(1) DEFAULT 0,
is_moderator TINYINT(1) DEFAULT 0
)");
try {
$db->exec("ALTER TABLE chat_users ADD COLUMN is_subscriber TINYINT(1) DEFAULT 0");
$db->exec("ALTER TABLE chat_users ADD COLUMN is_vip TINYINT(1) DEFAULT 0");
$db->exec("ALTER TABLE chat_users ADD COLUMN is_og TINYINT(1) DEFAULT 0");
$db->exec("ALTER TABLE chat_users ADD COLUMN is_moderator TINYINT(1) DEFAULT 0");
} catch(PDOException $e) {}
// Ban/Timeout geçmişi tablosu
$db->exec("CREATE TABLE IF NOT EXISTS ban_records (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(255),
username VARCHAR(255),
action_type VARCHAR(50), -- 'ban' veya 'timeout'
created_at DATETIME,
expires_at DATETIME NULL,
moderator_name VARCHAR(255),
reason TEXT
)");
// bot_commands tablosuna cooldown ve last_used_at sütunlarını ekle
try {
$db->exec("ALTER TABLE bot_commands ADD COLUMN cooldown INT DEFAULT 60");
$db->exec("ALTER TABLE bot_commands ADD COLUMN last_used_at DATETIME DEFAULT NULL");
} catch(PDOException $e) {
// Zaten varsa hata verecek, görmezden gelebiliriz.
}
// Bot ayarları tablosu (token vb. güvenli saklama)
$db->exec("CREATE TABLE IF NOT EXISTS bot_settings (
setting_key VARCHAR(100) PRIMARY KEY,
setting_value TEXT NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
// Ortak ban havuzu kanalları
$db->exec("CREATE TABLE IF NOT EXISTS shared_channels (
id INT AUTO_INCREMENT PRIMARY KEY,
channel_name VARCHAR(100) NOT NULL UNIQUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
try {
$db->exec("ALTER TABLE shared_channels ADD COLUMN accepted_reasons JSON DEFAULT NULL");
} catch(PDOException $e) {}
// Ortak ban havuzu listesi
$db->exec("CREATE TABLE IF NOT EXISTS shared_bans (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL,
reason TEXT,
moderator_name VARCHAR(100),
original_channel VARCHAR(100),
evidence_messages JSON,
ban_date DATETIME DEFAULT CURRENT_TIMESTAMP,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY(username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
// Bot bildirim ayarları
$db->exec("CREATE TABLE IF NOT EXISTS chat_notifications (
id INT AUTO_INCREMENT PRIMARY KEY,
event_type VARCHAR(50) NOT NULL UNIQUE,
message_template TEXT NOT NULL,
is_active TINYINT(1) DEFAULT 1,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
// Sadakat sistemi ayarları
$db->exec("CREATE TABLE IF NOT EXISTS loyalty_settings (
id TINYINT PRIMARY KEY,
follow_days_step INT NOT NULL DEFAULT 30,
follow_days_bonus_pct DECIMAL(8,3) NOT NULL DEFAULT 1.000,
message_step INT NOT NULL DEFAULT 100,
message_bonus_pct DECIMAL(8,3) NOT NULL DEFAULT 5.000,
timeout_step INT NOT NULL DEFAULT 5,
timeout_penalty_pct DECIMAL(8,3) NOT NULL DEFAULT 0.500,
ban_step INT NOT NULL DEFAULT 1,
ban_penalty_pct DECIMAL(8,3) NOT NULL DEFAULT 10.000,
deleted_step INT NOT NULL DEFAULT 20,
deleted_penalty_pct DECIMAL(8,3) NOT NULL DEFAULT 1.000,
subscriber_bonus_pct DECIMAL(8,3) NOT NULL DEFAULT 10.000,
vip_bonus_pct DECIMAL(8,3) NOT NULL DEFAULT 15.000,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
// Sadakat seviyeleri
$db->exec("CREATE TABLE IF NOT EXISTS loyalty_levels (
id INT AUTO_INCREMENT PRIMARY KEY,
level_name VARCHAR(100) NOT NULL,
required_score DECIMAL(12,2) NOT NULL,
min_follow_days INT NULL DEFAULT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
try {
$db->exec("ALTER TABLE loyalty_levels ADD COLUMN min_follow_days INT NULL DEFAULT NULL");
} catch(PDOException $e) {}
// Tek satırlık ayar kaydı
$db->exec("INSERT IGNORE INTO loyalty_settings (id) VALUES (1)");
// Varsayılan seviye kaydı
$levelCount = (int)$db->query("SELECT COUNT(*) FROM loyalty_levels")->fetchColumn();
if ($levelCount === 0) {
$db->exec("INSERT INTO loyalty_levels (level_name, required_score) VALUES
('Seviye 1', 0),
('Seviye 2', 500),
('Seviye 3', 1500)");
}
// Subathon Tabloları
$db->exec("CREATE TABLE IF NOT EXISTS subathon (
id TINYINT PRIMARY KEY,
is_active TINYINT(1) DEFAULT 0,
end_time DATETIME NULL,
sec_sub INT DEFAULT 300,
sec_resub INT DEFAULT 300,
sec_gift INT DEFAULT 300,
kicks_req INT DEFAULT 100,
sec_kicks INT DEFAULT 60,
timer_style VARCHAR(50) DEFAULT 'neon',
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
$db->exec("INSERT IGNORE INTO subathon (id) VALUES (1)");
try {
$db->exec("ALTER TABLE subathon ADD COLUMN timer_style VARCHAR(50) DEFAULT 'neon'");
} catch(PDOException $e) {}
$db->exec("CREATE TABLE IF NOT EXISTS subathon_events (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255),
action_type VARCHAR(50),
seconds_added INT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
// Şarkı İstek Tabloları
$db->exec("CREATE TABLE IF NOT EXISTS song_settings (
id TINYINT PRIMARY KEY,
is_active TINYINT(1) DEFAULT 0,
command_name VARCHAR(50) DEFAULT '!istek',
request_cost DECIMAL(12,2) DEFAULT 0,
is_playing TINYINT(1) DEFAULT 0,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
$db->exec("INSERT IGNORE INTO song_settings (id) VALUES (1)");
$db->exec("CREATE TABLE IF NOT EXISTS song_requests (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255),
video_id VARCHAR(50),
video_title VARCHAR(255),
status ENUM('pending', 'playing', 'played') DEFAULT 'pending',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
try {
$db->exec("ALTER TABLE song_settings ADD COLUMN trigger_action VARCHAR(50) DEFAULT NULL");
$db->exec("ALTER TABLE song_settings ADD COLUMN trigger_time BIGINT DEFAULT 0");
} catch(PDOException $e) {}
try {
$db->exec("ALTER TABLE chat_users ADD COLUMN spent_score DECIMAL(12,2) DEFAULT 0");
} catch(PDOException $e) {}
// Repertuar Tabloları (Canlı Müzik / Peçete)
$db->exec("CREATE TABLE IF NOT EXISTS repertoire_settings (
id TINYINT PRIMARY KEY,
is_active TINYINT(1) DEFAULT 0,
request_command VARCHAR(50) DEFAULT '!peçete',
list_command VARCHAR(50) DEFAULT '!repertuar',
request_cost DECIMAL(12,2) DEFAULT 0,
is_playing TINYINT(1) DEFAULT 0,
trigger_action VARCHAR(50) DEFAULT NULL,
trigger_time BIGINT DEFAULT 0,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
$db->exec("INSERT IGNORE INTO repertoire_settings (id) VALUES (1)");
$db->exec("CREATE TABLE IF NOT EXISTS repertoire_songs (
id INT AUTO_INCREMENT PRIMARY KEY,
song_name VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
$db->exec("CREATE TABLE IF NOT EXISTS repertoire_requests (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255),
song_id INT,
song_name VARCHAR(255),
status ENUM('pending', 'playing', 'played') DEFAULT 'pending',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
} catch (PDOException $e) {
die("Veritabanı bağlantı hatası: " . $e->getMessage());
}
/**
* Veritabanından access_token'ı çeker
*/
function getAccessToken() {
global $db;
try {
$stmt = $db->prepare("SELECT setting_value FROM bot_settings WHERE setting_key = 'access_token' LIMIT 1");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row ? $row['setting_value'] : null;
} catch (Exception $e) {
return null;
}
}
/**
* Token'ı veritabanına kaydeder
*/
function saveAccessToken($accessToken, $refreshToken = null) {
global $db;
$stmt = $db->prepare("INSERT INTO bot_settings (setting_key, setting_value) VALUES ('access_token', ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
$stmt->execute([$accessToken]);
if ($refreshToken) {
$stmt2 = $db->prepare("INSERT INTO bot_settings (setting_key, setting_value) VALUES ('refresh_token', ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
$stmt2->execute([$refreshToken]);
}
}
define('KICK_SCOPES', 'user:read channel:read chat:write events:subscribe moderation:ban moderation:chat_message:manage');
define('KICK_OAUTH_URL', 'https://id.kick.com/oauth/authorize');
define('KICK_TOKEN_URL', 'https://id.kick.com/oauth/token');
/**
* Rastgele bir PKCE Code Verifier oluşturur
*/
function generateCodeVerifier($length = 64)
{
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~';
$verifier = '';
for ($i = 0; $i < $length; $i++) {
$verifier .= $chars[random_int(0, strlen($chars) - 1)];
}
return $verifier;
}
/**
* Code Verifier kullanarak S256 Code Challenge oluşturur
*/
function generateCodeChallenge($verifier)
{
$hash = hash('sha256', $verifier, true);
return rtrim(strtr(base64_encode($hash), '+/', '-_'), '=');
}
/**
* Kick OAuth Login URL'sini oluşturur
*/
function getKickLoginUrl()
{
// Güvenlik için CSRF state oluştur
$state = bin2hex(random_bytes(16));
$_SESSION['oauth2state'] = $state;
// PKCE (Proof Key for Code Exchange) oluştur
$codeVerifier = generateCodeVerifier();
$_SESSION['pkce_verifier'] = $codeVerifier;
$codeChallenge = generateCodeChallenge($codeVerifier);
$params = [
'response_type' => 'code',
'client_id' => KICK_CLIENT_ID,
'redirect_uri' => KICK_REDIRECT_URI,
'scope' => KICK_SCOPES,
'state' => $state,
'code_challenge' => $codeChallenge,
'code_challenge_method' => 'S256'
];
return KICK_OAUTH_URL . '?' . http_build_query($params);
}
/**
* Geri dönen Authorization Code'u Access Token'a çevirir
*/
function exchangeCodeForToken($code, $verifier)
{
$postData = http_build_query([
'grant_type' => 'authorization_code',
'client_id' => KICK_CLIENT_ID,
'client_secret' => KICK_CLIENT_SECRET,
'redirect_uri' => KICK_REDIRECT_URI,
'code_verifier' => $verifier,
'code' => $code
]);
$ch = curl_init(KICK_TOKEN_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
return json_decode($response, true);
} else {
error_log("Kick OAuth Token Error: " . $response);
return false;
}
}
/**
* Mevcut Refresh Token'ı kullanarak yeni bir Access Token alır
*/
function refreshKickToken()
{
global $db;
try {
$stmt = $db->prepare("SELECT setting_value FROM bot_settings WHERE setting_key = 'refresh_token' LIMIT 1");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row || empty($row['setting_value'])) return false;
$refreshToken = $row['setting_value'];
$postData = http_build_query([
'grant_type' => 'refresh_token',
'client_id' => KICK_CLIENT_ID,
'client_secret' => KICK_CLIENT_SECRET,
'refresh_token' => $refreshToken
]);
$ch = curl_init(KICK_TOKEN_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$data = json_decode($response, true);
if (isset($data['access_token'])) {
saveAccessToken($data['access_token'], $data['refresh_token'] ?? null);
$_SESSION['kick_access_token'] = $data['access_token']; // Session'ı da güncelle
return $data['access_token'];
}
}
return false;
} catch (Exception $e) {
return false;
}
}
/**
* Kick API'sine istek atar, 401 alırsa otomatik token yeniler ve tekrar dener.
*/
function kickApiRequest($endpoint, $method = 'GET', $data = null) {
$accessToken = getAccessToken();
if (!$accessToken && isset($_SESSION['kick_access_token'])) {
$accessToken = $_SESSION['kick_access_token'];
}
$url = 'https://api.kick.com/public/v1/' . ltrim($endpoint, '/');
$attemptRequest = function($token) use ($url, $method, $data) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$headers = [
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
];
if ($data !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ['code' => $httpCode, 'response' => $response];
};
// İlk deneme
$result = $attemptRequest($accessToken);
// 401 hatası (Unauthorized) alınırsa token yenile ve tekrar dene
if ($result['code'] === 401) {
$newToken = refreshKickToken();
if ($newToken) {
$result = $attemptRequest($newToken);
}
}
return $result;
}
?>