From aec014f3775e43d2a07b000faa73f04c0cb77a5b Mon Sep 17 00:00:00 2001
From: Matthieu Marcillaud <marcimat@rezo.net>
Date: Sat, 3 May 2008 13:41:31 +0000
Subject: [PATCH] =?UTF-8?q?Certaines=20compressions=20gz=20des=20pages=20d?=
 =?UTF-8?q?istantes=20lues=20avec=20recuperer=5Fpage()=20n'arrivaient=20pa?=
 =?UTF-8?q?s=20=C3=A0=20=C3=AAtre=20lues.=20Remplacement=20de=20gzinflate(?=
 =?UTF-8?q?)=20par=20spip=5Fgzinflate=5Fbody()=20d=C3=A9crite=20sur=20php.?=
 =?UTF-8?q?net=20(http://fr2.php.net/manual/fr/function.gzinflate.php)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ecrire/inc/distant.php | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/ecrire/inc/distant.php b/ecrire/inc/distant.php
index 4e9035be6f..958d53525f 100644
--- a/ecrire/inc/distant.php
+++ b/ecrire/inc/distant.php
@@ -225,7 +225,7 @@ function recuperer_lapage($url, $trans=false, $get='GET', $taille_max = 1048576,
 
 	// Decompresser au besoin
 	if (preg_match(",\bContent-Encoding: .*gzip,i", $headers)) {
-		$result = gzinflate(substr($result,10));
+		$result = spip_gzinflate_body($result);
 	}
 	// Faut-il l'importer dans notre charset local ?
 	if ($trans) {
@@ -236,6 +236,29 @@ function recuperer_lapage($url, $trans=false, $get='GET', $taille_max = 1048576,
 	return array($headers, $result);
 }
 
+// Certaines decompressions gz sont subtiles
+// cf. http://fr2.php.net/manual/fr/function.gzinflate.php#77336
+// (cas de http://files.spip.org/spip-zone/paquets.xml.gz qui ne pouvait etre recupere)
+function spip_gzinflate_body($gzData){
+	// return gzinflate(substr($gzData,10));
+    if(substr($gzData,0,3)=="\x1f\x8b\x08"){
+        $i=10;
+        $flg=ord(substr($gzData,3,1));
+        if($flg>0){
+            if($flg&4){
+                list($xlen)=unpack('v',substr($gzData,$i,2));
+                $i=$i+2+$xlen;
+            }
+            if($flg&8) $i=strpos($gzData,"\0",$i)+1;
+            if($flg&16) $i=strpos($gzData,"\0",$i)+1;
+            if($flg&2) $i=$i+2;
+        }
+        return gzinflate(substr($gzData,$i,-8));
+    }
+    else return false;
+}
+
+
 // http://doc.spip.org/@recuperer_body
 function recuperer_body($f, $taille_max=1048576)
 {
-- 
GitLab