apkService = new ApkService(); } public function index() { $db = db_pdo(); $category = $_GET['category'] ?? null; $sql = "SELECT * FROM apks WHERE status = 'published'"; $params = []; if ($category) { $sql .= " AND category_id = (SELECT id FROM categories WHERE slug = ?)"; $params[] = $category; } $sql .= " ORDER BY display_order ASC, created_at DESC LIMIT 12"; $stmt = $db->prepare($sql); $stmt->execute($params); $apks = $stmt->fetchAll(); return $this->view('home', [ 'apks' => $apks, 'title' => 'ApkNusa - Professional APK Download Portal' ]); } public function apkDetail($params) { $slug = $params['slug']; $db = db_pdo(); $stmt = $db->prepare("SELECT * FROM apks WHERE slug = ?"); $stmt->execute([$slug]); $apk = $stmt->fetch(); if (!$apk) { $this->redirect('/'); } // Store referral code if present if (isset($_GET['ref'])) { $_SESSION['ref_download_' . $apk['id']] = $_GET['ref']; } $this->view('apk_detail', ['apk' => $apk]); } public function download($params) { $slug = $params['slug']; $db = db_pdo(); $stmt = $db->prepare("SELECT * FROM apks WHERE slug = ?"); $stmt->execute([$slug]); $apk = $stmt->fetch(); if (!$apk) { $this->redirect('/'); } // Check for referral earnings $ref_code = $_SESSION['ref_download_' . $apk['id']] ?? null; if ($ref_code) { $stmt = $db->prepare("SELECT id FROM users WHERE referral_code = ?"); $stmt->execute([$ref_code]); $referrer = $stmt->fetch(); if ($referrer) { $referrer_id = $referrer['id']; $ip = $_SERVER['REMOTE_ADDR']; // Check if this IP already earned for this APK today (prevent abuse) $stmt = $db->prepare("SELECT id FROM referral_downloads WHERE referrer_id = ? AND apk_id = ? AND ip_address = ? AND created_at > DATE_SUB(NOW(), INTERVAL 1 DAY)"); $stmt->execute([$referrer_id, $apk['id'], $ip]); if (!$stmt->fetch()) { // Credit 500 IDR $stmt = $db->prepare("UPDATE users SET balance = balance + 500 WHERE id = ?"); $stmt->execute([$referrer_id]); // Log download $stmt = $db->prepare("INSERT INTO referral_downloads (referrer_id, apk_id, ip_address, amount) VALUES (?, ?, ?, 500)"); $stmt->execute([$referrer_id, $apk['id'], $ip]); } } // Clear session after processing unset($_SESSION['ref_download_' . $apk['id']]); } // Increment total downloads $stmt = $db->prepare("UPDATE apks SET total_downloads = total_downloads + 1 WHERE id = ?"); $stmt->execute([$apk['id']]); // Redirect to actual file $this->redirect($apk['download_url']); } }