$message], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; } function package_download(string $url, int $limit): ?string { if (function_exists('curl_init')) { $body = ''; $handle = curl_init($url); if ($handle === false) { return null; } curl_setopt_array($handle, [ CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 35, CURLOPT_FOLLOWLOCATION => false, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_USERAGENT => 'Webftts-LaTeX-Packages/5', CURLOPT_WRITEFUNCTION => static function ($curl, string $chunk) use (&$body, $limit): int { if (strlen($body) + strlen($chunk) > $limit) { return 0; } $body .= $chunk; return strlen($chunk); } ]); $ok = curl_exec($handle); $status = (int) curl_getinfo($handle, CURLINFO_HTTP_CODE); curl_close($handle); return $ok !== false && $status === 200 ? $body : null; } if (!filter_var(ini_get('allow_url_fopen'), FILTER_VALIDATE_BOOLEAN)) { return null; } $context = stream_context_create([ 'http' => [ 'timeout' => 35, 'follow_location' => 0, 'max_redirects' => 0, 'ignore_errors' => true, 'header' => "User-Agent: Webftts-LaTeX-Packages/5\r\n" ], 'ssl' => ['verify_peer' => true, 'verify_peer_name' => true] ]); $body = @file_get_contents($url, false, $context, 0, $limit + 1); $status = isset($http_response_header[0]) ? $http_response_header[0] : ''; return is_string($body) && strlen($body) <= $limit && preg_match('~^HTTP/\S+ 200(?: |$)~', $status) ? $body : null; } if (!in_array($_SERVER['REQUEST_METHOD'] ?? 'GET', ['GET', 'HEAD'], true)) { header('Allow: GET, HEAD'); package_error(405, 'Utilise une requête GET pour télécharger un paquet.'); } $name = isset($_GET['package']) && is_string($_GET['package']) ? $_GET['package'] : ''; if (!preg_match('/\A[a-z0-9][a-z0-9+_.-]{0,100}\z/D', $name)) { package_error(400, 'Nom de paquet invalide.'); } $json = @file_get_contents(__DIR__ . '/vendor/packages/repository.json'); $repository = is_string($json) ? json_decode($json, true) : null; if (!is_array($repository) || ($repository['version'] ?? 0) !== 1 || ($repository['snapshot'] ?? '') !== 'texlive-2019-final') { package_error(503, 'Le catalogue de paquets manque sur le site. Transfère le dossier vendor/packages en entier.'); } $metadata = $repository['packages'][$name] ?? null; if (!is_array($metadata)) { package_error(404, 'Ce paquet ne figure pas dans le dépôt compatible avec le moteur.'); } $size = (int) ($metadata['size'] ?? 0); $checksum = $metadata['sha512'] ?? ''; if ($size < 1 || $size > 80 * 1024 * 1024 || !is_string($checksum) || !preg_match('/\A[0-9a-f]{128}\z/', $checksum)) { package_error(422, 'Les métadonnées du paquet sont invalides.'); } $mirrors = [ 'https://ftp.math.utah.edu/pub/tex/historic/systems/texlive/2019/tlnet-final/archive/', 'https://ftp.tu-chemnitz.de/pub/tug/historic/systems/texlive/2019/tlnet-final/archive/' ]; foreach ($mirrors as $mirror) { $body = package_download($mirror . rawurlencode($name) . '.tar.xz', $size); if (!is_string($body) || strlen($body) !== $size || !hash_equals($checksum, hash('sha512', $body))) { continue; } $etag = '"' . $checksum . '"'; header('Cache-Control: public, max-age=31536000, immutable'); header('ETag: ' . $etag); header('X-Content-Type-Options: nosniff'); if (trim($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') === $etag) { http_response_code(304); exit; } header('Content-Type: application/x-xz'); header('Content-Disposition: inline; filename="' . $name . '.tar.xz"'); header('Content-Length: ' . strlen($body)); if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'HEAD') { echo $body; } exit; } package_error(502, 'Le paquet ne peut pas être récupéré. Vérifie la connexion du serveur et active cURL ou allow_url_fopen dans PHP. Réessaie ensuite la compilation.');