'idle']; echo json_encode($prog); exit; } // ---------- handle AJAX start-download request ---------- if (isset($_GET['action']) && $_GET['action'] === 'start') { header('Content-Type: application/json'); if (!isset($_POST['password']) || $_POST['password'] !== SECRET_PASSWORD) { echo json_encode(['ok' => false, 'error' => 'Wrong password.']); exit; } // Mark session so the download script knows it's authorised $_SESSION['dl_auth'] = true; $_SESSION['dl_progress'] = ['status' => 'starting', 'bytes' => 0, 'total' => 0, 'pct' => 0]; echo json_encode(['ok' => true]); exit; } // ---------- handle AJAX run-download request (streams via ignore_user_abort) ---------- if (isset($_GET['action']) && $_GET['action'] === 'run') { // Must be authenticated if (empty($_SESSION['dl_auth'])) { exit('Unauthorised'); } ignore_user_abort(true); set_time_limit(0); ini_set('memory_limit', '128M'); $update = function(array $data) { $_SESSION['dl_progress'] = $data; session_write_close(); session_start(); }; // --- open remote file --- $ctx = stream_context_create([ 'http' => ['timeout' => 30], 'ssl' => ['verify_peer' => false], ]); $remote = @fopen(SOURCE_URL, 'rb', false, $ctx); if (!$remote) { $update(['status' => 'error', 'message' => 'Could not open remote URL.']); exit; } // Try to get Content-Length from headers $meta = stream_get_meta_data($remote); $total = 0; foreach ($meta['wrapper_data'] ?? [] as $h) { if (stripos($h, 'Content-Length:') === 0) { $total = (int) trim(substr($h, 15)); } } // --- open local file --- $local = @fopen(SAVE_AS, 'wb'); if (!$local) { fclose($remote); $update(['status' => 'error', 'message' => 'Cannot write to destination: ' . SAVE_AS]); exit; } $downloaded = 0; $start = time(); while (!feof($remote)) { $chunk = fread($remote, CHUNK_BYTES); if ($chunk === false) break; fwrite($local, $chunk); $downloaded += strlen($chunk); $pct = $total > 0 ? round($downloaded / $total * 100, 1) : 0; $elapsed = max(1, time() - $start); $speed = $downloaded / $elapsed; // bytes/sec $update([ 'status' => 'downloading', 'bytes' => $downloaded, 'total' => $total, 'pct' => $pct, 'speed' => $speed, 'elapsed' => $elapsed, ]); } fclose($remote); fclose($local); $update([ 'status' => 'done', 'bytes' => $downloaded, 'total' => $total, 'pct' => 100, 'elapsed' => max(1, time() - $start), ]); unset($_SESSION['dl_auth']); exit; } // ---------- default: serve the HTML page ---------- ?>
successlifecreation.com / file receiver