<?phpnamespace App\Media;/** * A big file downloaded straight to the disk in the background, with its progress: the new WinZik (the splash shows * its percentage), yt-dlp (~30 Mo), ffmpeg (~100 Mo), Whisper's model, YouTube's streams without yt-dlp (by ranges: * RangedDownload). Desktop and phone app (not the browser). * * A socket of PHP itself, never waiting (the exported WinZik has no curl): connection, TLS, request, answer, each * frame doing what is ready; redirections followed (the forge's releases); the body written as it comes, never held in * memory. HTTP/1.0: the answer is the file as it is (no chunks), its length in Content-Length. */final class FileDownload{ /** 0..1 */ public float $progress = 0.0; public string $error = ''; /** bytes received, and how many there are (0: not known yet) */ public int $received = 0; public int $total = 0; private bool $done = false; /** @var resource|null */ private $socket = null; /** @var resource|null */ private $file = null; private string $state = 'connect'; private string $buffer = ''; private string $host = ''; private string $path = '/'; private bool $tls = false; private int $redirects = 0; private float $since = 0.0; private string $currentUrl = ''; private int $port = 80; private int $attempts = 0; private float $retryAt = 0.0; /** the biggest file taken by default (ffmpeg ~100 Mo, Whisper's model ~150 Mo): a server sending more is cut */ public const MAX_BYTES = 1024 * 1024 * 1024; /** * @param int $expected its size when known beforehand (update.json): the progress before the answer says it, and * no byte more is taken * @param int $maxBytes the most it may weigh (a bigger answer is an error, its part file removed) */ public function __construct(public readonly string $url, public readonly string $target, int $expected = 0, private int $maxBytes = self::MAX_BYTES) { @mkdir(dirname($target), 0777, true); $file = @fopen($target . '.part', 'wb'); if ($file === false) { $this->fail('impossible d\'écrire ' . $target); return; } $this->file = $file; $this->total = max(0, $expected); if ($this->total > 0) { $this->maxBytes = min($this->maxBytes, $this->total); } $this->open($url); } private function open(string $url): void { $this->currentUrl = $url; if (preg_match('/[\x00-\x20\x7f]/', $url)) { // spaces or line ends would end up in the request line (another request, other headers) $this->fail('adresse invalide : ' . rawurlencode($url)); return; } $u = parse_url($url); $scheme = strtolower((string)($u['scheme'] ?? '')); if (!is_array($u) || !isset($u['host']) || !in_array($scheme, ['http', 'https'], true)) { $this->fail('adresse invalide : ' . $url); return; } $this->tls = $scheme === 'https'; $this->host = (string)$u['host']; $port = (int)($u['port'] ?? ($this->tls ? 443 : 80)); $this->port = $port; $this->path = ($u['path'] ?? '/') . (isset($u['query']) ? '?' . $u['query'] : ''); $ssl = ['peer_name' => $this->host, 'SNI_enabled' => true, 'verify_peer' => true, 'verify_peer_name' => true]; $ca = getenv('SSL_CERT_FILE'); if (is_string($ca) && is_file($ca)) { $ssl['cafile'] = $ca; } $context = stream_context_create(['ssl' => $ssl]); $socket = @stream_socket_client("tcp://{$this->host}:$port", $no, $message, 20, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT, $context); if ($socket === false) { $this->fail("connexion à {$this->host} impossible : $message"); return; } stream_set_blocking($socket, false); $this->socket = $socket; $this->state = 'connect'; $this->buffer = ''; $this->since = microtime(true); } /** Each frame. @return bool true once it ended (ok() or error) */ public function poll(): bool { if ($this->done) { return true; } $until = microtime(true) + 0.008; // a few milliseconds a frame at most while (!$this->finished() && microtime(true) < $until) { if (!$this->step()) { break; // waiting for the network } } if (!$this->finished() && microtime(true) - $this->since > 60) { $this->fail('plus de réponse du serveur depuis 60 s'); } return $this->done; } /** One step if it can be done now. @return bool false: nothing to do before the network answers */ private function step(): bool { $s = $this->socket; if ($s === null) { return false; } switch ($this->state) { case 'retry': if (microtime(true) < $this->retryAt) { return false; } @fclose($s); $this->socket = null; $this->open($this->currentUrl); return true; case 'connect': $r = null; $w = [$s]; $e = null; if (@stream_select($r, $w, $e, 0) < 1) { return false; } if (self::connectFailed($s)) { // "ready" because it failed (refused, unreachable): again a little later, a few times return $this->retry("connexion à {$this->host} refusée"); } $this->state = $this->tls ? 'tls' : 'send'; return true; case 'tls': $ok = @stream_socket_enable_crypto($s, true, STREAM_CRYPTO_METHOD_TLS_CLIENT); if ($ok === 0) { return false; // the handshake goes on } if ($ok !== true) { $this->fail("connexion sécurisée à {$this->host} impossible"); return false; } $this->state = 'send'; return true; case 'send': $request = "GET {$this->path} HTTP/1.0\r\nHost: {$this->host}\r\nUser-Agent: WinZik\r\nAccept: */*\r\nAccept-Encoding: identity\r\nConnection: close\r\n\r\n"; if (@fwrite($s, $request) === false) { return $this->retry('envoi de la demande impossible'); } $this->state = 'head'; return true; case 'head': case 'body': $chunk = @fread($s, 262144); if ($chunk === false || $chunk === '') { if (feof($s)) { $this->ended(); return false; } return false; } $this->since = microtime(true); if ($this->state === 'head') { $this->buffer .= $chunk; $end = strpos($this->buffer, "\r\n\r\n"); if ($end === false) { if (strlen($this->buffer) > 65536) { $this->fail('réponse invalide'); } return true; } $body = (string)substr($this->buffer, $end + 4); if (!$this->headers(substr($this->buffer, 0, $end))) { return true; // redirected (or failed) } $this->state = 'body'; $chunk = $body; } if ($chunk !== '' && $this->file !== null) { if ($this->received + strlen($chunk) > $this->maxBytes || ($this->total > 0 && $this->received + strlen($chunk) > $this->total)) { // more than it said (or than it may be): never written, the disk is not filled $this->fail(sprintf('fichier plus gros que prévu (plus de %d octets)', $this->total > 0 ? min($this->total, $this->maxBytes) : $this->maxBytes)); return false; } fwrite($this->file, $chunk); $this->received += strlen($chunk); $this->progress = $this->total > 0 ? min(0.999, $this->received / $this->total) : 0.0; if ($this->total > 0 && $this->received >= $this->total) { // all it announced: done now (a server that keeps the connection open, YouTube's on a phone, never closes) $this->ended(); return false; } } return true; } return false; } /** The connection again in a moment (a few times), else the error. */ private function retry(string $why): bool { if (++$this->attempts >= 5) { $this->fail($why); return false; } $this->state = 'retry'; $this->retryAt = microtime(true) + 0.5; $this->since = microtime(true); return false; } /** @param resource $s whether its connection failed (the socket's own error, when PHP has the sockets extension) */ private static function connectFailed($s): bool { if (!function_exists('socket_import_stream')) { return false; // the request's write will tell } $socket = @socket_import_stream($s); if ($socket === false) { return false; } $error = @socket_get_option($socket, SOL_SOCKET, SO_ERROR); return is_int($error) && $error !== 0; } /** The answer's head: its status, a redirection, its length. @return bool true: its body follows */ private function headers(string $head): bool { $lines = explode("\r\n", $head); $code = preg_match('#^HTTP/\S+\s+(\d{3})#', (string)$lines[0], $m) ? (int)$m[1] : 0; $fields = []; foreach (array_slice($lines, 1) as $line) { if (str_contains($line, ':')) { [$k, $v] = explode(':', $line, 2); $fields[strtolower(trim($k))] = trim($v); } } if ($code >= 300 && $code < 400 && isset($fields['location'])) { if (++$this->redirects > 8) { $this->fail('trop de redirections'); return false; } $next = $fields['location']; if ($this->tls && preg_match('#^http://#i', $next)) { // https then http: anyone on the way could change the file $this->fail('redirection non sécurisée vers ' . $next); return false; } if (!preg_match('#^https?://#i', $next)) { // a path on the same server $default = $this->port === ($this->tls ? 443 : 80); $next = ($this->tls ? 'https' : 'http') . '://' . $this->host . ($default ? '' : ':' . $this->port) . (str_starts_with($next, '/') ? '' : '/') . $next; } if ($this->socket !== null) { @fclose($this->socket); $this->socket = null; } $this->open($next); return false; } if ($code < 200 || $code >= 300) { $this->fail('HTTP ' . $code); return false; } if (isset($fields['content-length']) && ctype_digit($fields['content-length'])) { $length = (int)$fields['content-length']; if ($length > $this->maxBytes) { $this->fail(sprintf('fichier trop gros (%d octets)', $length)); return false; } $this->total = $length; } return true; } /** The server closed: the whole file, unless it is shorter than it said. */ private function ended(): void { if ($this->state !== 'body') { $this->fail('le serveur a coupé avant de répondre'); return; } if ($this->total > 0 && $this->received < $this->total) { $this->fail(sprintf('téléchargement coupé (%d / %d octets)', $this->received, $this->total)); return; } $this->close(); @unlink($this->target); if (!@rename($this->target . '.part', $this->target)) { $this->fail('impossible d\'écrire ' . $this->target); return; } $this->progress = 1.0; $this->done = true; } private function finished(): bool { return $this->done; } public function ok(): bool { return $this->done && $this->error === ''; } private function close(): void { if ($this->socket !== null) { @fclose($this->socket); $this->socket = null; } if ($this->file !== null) { fclose($this->file); $this->file = null; } } /** Stopped for good (a line of the queue removed, WinZik quitting): its part file removed. */ public function cancel(): void { if (!$this->done) { $this->fail('arrêté'); } } private function fail(string $message): void { $this->close(); $this->error = $message; $this->done = true; @unlink($this->target . '.part'); }}