File size: 2,298 Bytes
dd34dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f802f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd34dfb
9f802f0
dd34dfb
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
<?php
/**
 * BizimHesap ham JSON aynasi — SALT-OKUR snapshot sunucusu.
 *
 * BizimHesap'a ASLA cagri yapmaz. bh-collector.php'nin saatte 1 yazdigi
 * cache/bh-raw/ snapshot'larini servis eder. Boylece kullanici trafigi
 * hicbir kosulda BizimHesap trafigi uretemez (Cloudflare 1015 onlemi).
 *
 * Kullanim:
 *   ?kind=products
 *   ?kind=warehouses
 *   ?kind=inventory&id=<warehouse_id>
 */

header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

define('SNAP_DIR', __DIR__ . '/cache/bh-raw/');

$VALID = ['products', 'warehouses', 'inventory'];
$kind = $_GET['kind'] ?? '';
$wid = $_GET['id'] ?? '';

if (!in_array($kind, $VALID, true) || ($kind === 'inventory' && !preg_match('/^[A-F0-9]{32}$/i', $wid))) {
    http_response_code(400);
    echo json_encode(['resultCode' => 0, 'errorText' => 'gecersiz kind/id parametresi', 'data' => null]);
    exit;
}

$file = SNAP_DIR . ($kind === 'inventory' ? "inventory_{$wid}.json" : "{$kind}.json");

if (!file_exists($file)) {
    http_response_code(503);
    header('X-Cache: no-snapshot');
    echo json_encode(['resultCode' => 0, 'errorText' => 'snapshot henuz yok (collector bekleniyor)', 'data' => null]);
    exit;
}

$age = time() - filemtime($file);

// Bayatlik tetigi: snapshot 70dk'dan eskiyse toplayiciyi YEREL olarak durt.
// Bu istek BizimHesap'a degil ayni sunucudaki bh-collector.php'ye gider;
// collector kendini kisitlar (50dk tazelik + flock + 429 devre kesici).
// 200ms'de koparilir — collector ignore_user_abort ile calismaya devam eder.
// Boylece tetikleme HF Space'e muhtac degil: sistemi kullanan herkes canli tutar.
if ($age > 4200) {
    $ch = curl_init('https://video.trek-turkey.com/bh-collector.php?key=trkbh2026x');
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT_MS => 200,
        CURLOPT_NOSIGNAL => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
    ]);
    @curl_exec($ch);
    curl_close($ch);
}

header('X-Cache: snapshot');
header('X-Snapshot-Age: ' . $age);
readfile($file);