@@ -147,7 +147,7 @@  | 
                                                    ||
| 147 | 147 | /**  | 
                                                        
| 148 | 148 | * Verifier qu'une url est absolue et que son protocole est bien parmi une liste autorisee  | 
                                                        
| 149 | 149 | * @param string $url_absolue  | 
                                                        
| 150 | - * @param array $protocoles_autorises  | 
                                                        |
| 150 | + * @param string[] $protocoles_autorises  | 
                                                        |
| 151 | 151 | * @return bool  | 
                                                        
| 152 | 152 | */  | 
                                                        
| 153 | 153 |  function protocole_verifier($url_absolue, $protocoles_autorises = array('http','https')) { | 
                                                        
@@ -24,16 +24,16 @@ discard block  | 
                                                    ||
| 24 | 24 | //  | 
                                                        
| 25 | 25 | // http://doc.spip.org/@resolve_path  | 
                                                        
| 26 | 26 |  function resolve_path($url) { | 
                                                        
| 27 | -	list($url, $query) = array_pad(explode('?', $url, 2), 2, null); | 
                                                        |
| 28 | -	while (preg_match(',/\.?/,', $url, $regs)		# supprime // et /./ | 
                                                        |
| 29 | -	OR preg_match(',/[^/]*/\.\./,S', $url, $regs)	# supprime /toto/../ | 
                                                        |
| 30 | -	OR preg_match(',^/\.\./,S', $url, $regs))		# supprime les /../ du haut | 
                                                        |
| 31 | - $url = str_replace($regs[0], '/', $url);  | 
                                                        |
| 27 | +    list($url, $query) = array_pad(explode('?', $url, 2), 2, null); | 
                                                        |
| 28 | +    while (preg_match(',/\.?/,', $url, $regs)		# supprime // et /./ | 
                                                        |
| 29 | +    OR preg_match(',/[^/]*/\.\./,S', $url, $regs)	# supprime /toto/../ | 
                                                        |
| 30 | +    OR preg_match(',^/\.\./,S', $url, $regs))		# supprime les /../ du haut | 
                                                        |
| 31 | + $url = str_replace($regs[0], '/', $url);  | 
                                                        |
| 32 | 32 | |
| 33 | - if ($query)  | 
                                                        |
| 34 | - $url .= '?'.$query;  | 
                                                        |
| 33 | + if ($query)  | 
                                                        |
| 34 | + $url .= '?'.$query;  | 
                                                        |
| 35 | 35 | |
| 36 | -	return '/'.preg_replace(',^/,S', '', $url); | 
                                                        |
| 36 | +    return '/'.preg_replace(',^/,S', '', $url); | 
                                                        |
| 37 | 37 | }  | 
                                                        
| 38 | 38 | |
| 39 | 39 | //  | 
                                                        
@@ -44,46 +44,46 @@ discard block  | 
                                                    ||
| 44 | 44 | // http://doc.spip.org/@suivre_lien  | 
                                                        
| 45 | 45 |  function suivre_lien($url, $lien) { | 
                                                        
| 46 | 46 | |
| 47 | -	if (preg_match(',^(mailto|javascript|data):,iS', $lien)) | 
                                                        |
| 48 | - return $lien;  | 
                                                        |
| 49 | -	if (preg_match(';^((?:[a-z]{3,7}:)?//.*?)(/.*)?$;iS', $lien, $r)) | 
                                                        |
| 50 | - return $r[1].resolve_path($r[2]);  | 
                                                        |
| 51 | -  | 
                                                        |
| 52 | - # L'url site spip est un lien absolu aussi  | 
                                                        |
| 53 | -	if ($lien == $GLOBALS['meta']['adresse_site']){ | 
                                                        |
| 54 | - return $lien;  | 
                                                        |
| 55 | - }  | 
                                                        |
| 56 | -  | 
                                                        |
| 57 | - # lien relatif, il faut verifier l'url de base  | 
                                                        |
| 58 | - # commencer par virer la chaine de get de l'url de base  | 
                                                        |
| 59 | -	if (preg_match(';^((?:[a-z]{3,7}:)?//[^/]+)(/.*?/?)?([^/#?]*)([?][^#]*)?(#.*)?$;S', $url, $regs)) { | 
                                                        |
| 60 | - $debut = $regs[1];  | 
                                                        |
| 61 | - $dir = !strlen($regs[2]) ? '/' : $regs[2];  | 
                                                        |
| 62 | - $mot = $regs[3];  | 
                                                        |
| 63 | - $get = isset($regs[4])?$regs[4]:"";  | 
                                                        |
| 64 | - $hash = isset($regs[5])?$regs[5]:"";  | 
                                                        |
| 65 | - }  | 
                                                        |
| 66 | -	switch (substr($lien,0,1)) { | 
                                                        |
| 67 | - case '/':  | 
                                                        |
| 68 | - return $debut . resolve_path($lien);  | 
                                                        |
| 69 | - case '#':  | 
                                                        |
| 70 | - return $debut . resolve_path($dir.$mot.$get.$lien);  | 
                                                        |
| 71 | - case '':  | 
                                                        |
| 72 | - return $debut . resolve_path($dir.$mot.$get.$hash);  | 
                                                        |
| 73 | - default:  | 
                                                        |
| 74 | - return $debut . resolve_path($dir.$lien);  | 
                                                        |
| 75 | - }  | 
                                                        |
| 47 | +    if (preg_match(',^(mailto|javascript|data):,iS', $lien)) | 
                                                        |
| 48 | + return $lien;  | 
                                                        |
| 49 | +    if (preg_match(';^((?:[a-z]{3,7}:)?//.*?)(/.*)?$;iS', $lien, $r)) | 
                                                        |
| 50 | + return $r[1].resolve_path($r[2]);  | 
                                                        |
| 51 | +  | 
                                                        |
| 52 | + # L'url site spip est un lien absolu aussi  | 
                                                        |
| 53 | +    if ($lien == $GLOBALS['meta']['adresse_site']){ | 
                                                        |
| 54 | + return $lien;  | 
                                                        |
| 55 | + }  | 
                                                        |
| 56 | +  | 
                                                        |
| 57 | + # lien relatif, il faut verifier l'url de base  | 
                                                        |
| 58 | + # commencer par virer la chaine de get de l'url de base  | 
                                                        |
| 59 | +    if (preg_match(';^((?:[a-z]{3,7}:)?//[^/]+)(/.*?/?)?([^/#?]*)([?][^#]*)?(#.*)?$;S', $url, $regs)) { | 
                                                        |
| 60 | + $debut = $regs[1];  | 
                                                        |
| 61 | + $dir = !strlen($regs[2]) ? '/' : $regs[2];  | 
                                                        |
| 62 | + $mot = $regs[3];  | 
                                                        |
| 63 | + $get = isset($regs[4])?$regs[4]:"";  | 
                                                        |
| 64 | + $hash = isset($regs[5])?$regs[5]:"";  | 
                                                        |
| 65 | + }  | 
                                                        |
| 66 | +    switch (substr($lien,0,1)) { | 
                                                        |
| 67 | + case '/':  | 
                                                        |
| 68 | + return $debut . resolve_path($lien);  | 
                                                        |
| 69 | + case '#':  | 
                                                        |
| 70 | + return $debut . resolve_path($dir.$mot.$get.$lien);  | 
                                                        |
| 71 | + case '':  | 
                                                        |
| 72 | + return $debut . resolve_path($dir.$mot.$get.$hash);  | 
                                                        |
| 73 | + default:  | 
                                                        |
| 74 | + return $debut . resolve_path($dir.$lien);  | 
                                                        |
| 75 | + }  | 
                                                        |
| 76 | 76 | }  | 
                                                        
| 77 | 77 | |
| 78 | 78 | // un filtre pour transformer les URLs relatives en URLs absolues ;  | 
                                                        
| 79 | 79 | // ne s'applique qu'aux #URL_XXXX  | 
                                                        
| 80 | 80 | // http://doc.spip.org/@url_absolue  | 
                                                        
| 81 | 81 |  function url_absolue($url, $base='') { | 
                                                        
| 82 | - if (strlen($url = trim($url)) == 0)  | 
                                                        |
| 83 | - return '';  | 
                                                        |
| 84 | - if (!$base)  | 
                                                        |
| 85 | - $base = url_de_base() . (_DIR_RACINE ? _DIR_RESTREINT_ABS : '');  | 
                                                        |
| 86 | - return suivre_lien($base, $url);  | 
                                                        |
| 82 | + if (strlen($url = trim($url)) == 0)  | 
                                                        |
| 83 | + return '';  | 
                                                        |
| 84 | + if (!$base)  | 
                                                        |
| 85 | + $base = url_de_base() . (_DIR_RACINE ? _DIR_RESTREINT_ABS : '');  | 
                                                        |
| 86 | + return suivre_lien($base, $url);  | 
                                                        |
| 87 | 87 | }  | 
                                                        
| 88 | 88 | |
| 89 | 89 | /**  | 
                                                        
@@ -93,7 +93,7 @@ discard block  | 
                                                    ||
| 93 | 93 | * @return string  | 
                                                        
| 94 | 94 | */  | 
                                                        
| 95 | 95 |  function protocole_implicite($url_absolue){ | 
                                                        
| 96 | -	return preg_replace(";^[a-z]{3,7}://;i","//",$url_absolue); | 
                                                        |
| 96 | +    return preg_replace(";^[a-z]{3,7}://;i","//",$url_absolue); | 
                                                        |
| 97 | 97 | }  | 
                                                        
| 98 | 98 | |
| 99 | 99 | /**  | 
                                                        
@@ -104,40 +104,40 @@ discard block  | 
                                                    ||
| 104 | 104 | */  | 
                                                        
| 105 | 105 |  function protocole_verifier($url_absolue, $protocoles_autorises = array('http','https')) { | 
                                                        
| 106 | 106 | |
| 107 | -	if (preg_match(';^([a-z]{3,7})://;i', $url_absolue, $m)) { | 
                                                        |
| 108 | - $protocole = $m[1];  | 
                                                        |
| 109 | - if (in_array($protocole, $protocoles_autorises)  | 
                                                        |
| 110 | -		  or in_array(strtolower($protocole), array_map('strtolower', $protocoles_autorises))) { | 
                                                        |
| 111 | - return true;  | 
                                                        |
| 112 | - }  | 
                                                        |
| 113 | - }  | 
                                                        |
| 114 | - return false;  | 
                                                        |
| 107 | +    if (preg_match(';^([a-z]{3,7})://;i', $url_absolue, $m)) { | 
                                                        |
| 108 | + $protocole = $m[1];  | 
                                                        |
| 109 | + if (in_array($protocole, $protocoles_autorises)  | 
                                                        |
| 110 | +          or in_array(strtolower($protocole), array_map('strtolower', $protocoles_autorises))) { | 
                                                        |
| 111 | + return true;  | 
                                                        |
| 112 | + }  | 
                                                        |
| 113 | + }  | 
                                                        |
| 114 | + return false;  | 
                                                        |
| 115 | 115 | }  | 
                                                        
| 116 | 116 | |
| 117 | 117 | // un filtre pour transformer les URLs relatives en URLs absolues ;  | 
                                                        
| 118 | 118 | // ne s'applique qu'aux textes contenant des liens  | 
                                                        
| 119 | 119 | // http://doc.spip.org/@liens_absolus  | 
                                                        
| 120 | 120 |  function liens_absolus($texte, $base='') { | 
                                                        
| 121 | -	if (preg_match_all(',(<(a|link|image|img|script)\s[^<>]*(href|src)=[^<>]*>),imsS',  | 
                                                        |
| 122 | -	$texte, $liens, PREG_SET_ORDER)) { | 
                                                        |
| 123 | -		if (!function_exists('extraire_attribut')) { | 
                                                        |
| 124 | -			include_spip('inc/filtres'); | 
                                                        |
| 125 | - }  | 
                                                        |
| 126 | -		foreach ($liens as $lien) { | 
                                                        |
| 127 | -			foreach(array('href', 'src') as $attr) { | 
                                                        |
| 128 | - $href = extraire_attribut($lien[0], $attr);  | 
                                                        |
| 129 | -				if (strlen($href)>0) { | 
                                                        |
| 130 | - $abs = url_absolue($href, $base);  | 
                                                        |
| 131 | -					if ($href != $abs and !preg_match('/^#/',$href)) { | 
                                                        |
| 132 | - $texte_lien = inserer_attribut($lien[0], $attr, $abs);  | 
                                                        |
| 133 | - $texte = str_replace($lien[0],$texte_lien,$texte);  | 
                                                        |
| 134 | - }  | 
                                                        |
| 135 | - }  | 
                                                        |
| 136 | - }  | 
                                                        |
| 137 | - }  | 
                                                        |
| 138 | - }  | 
                                                        |
| 139 | -  | 
                                                        |
| 140 | - return $texte;  | 
                                                        |
| 121 | +    if (preg_match_all(',(<(a|link|image|img|script)\s[^<>]*(href|src)=[^<>]*>),imsS',  | 
                                                        |
| 122 | +    $texte, $liens, PREG_SET_ORDER)) { | 
                                                        |
| 123 | +        if (!function_exists('extraire_attribut')) { | 
                                                        |
| 124 | +            include_spip('inc/filtres'); | 
                                                        |
| 125 | + }  | 
                                                        |
| 126 | +        foreach ($liens as $lien) { | 
                                                        |
| 127 | +            foreach(array('href', 'src') as $attr) { | 
                                                        |
| 128 | + $href = extraire_attribut($lien[0], $attr);  | 
                                                        |
| 129 | +                if (strlen($href)>0) { | 
                                                        |
| 130 | + $abs = url_absolue($href, $base);  | 
                                                        |
| 131 | +                    if ($href != $abs and !preg_match('/^#/',$href)) { | 
                                                        |
| 132 | + $texte_lien = inserer_attribut($lien[0], $attr, $abs);  | 
                                                        |
| 133 | + $texte = str_replace($lien[0],$texte_lien,$texte);  | 
                                                        |
| 134 | + }  | 
                                                        |
| 135 | + }  | 
                                                        |
| 136 | + }  | 
                                                        |
| 137 | + }  | 
                                                        |
| 138 | + }  | 
                                                        |
| 139 | +  | 
                                                        |
| 140 | + return $texte;  | 
                                                        |
| 141 | 141 | }  | 
                                                        
| 142 | 142 | |
| 143 | 143 | //  | 
                                                        
@@ -145,10 +145,10 @@ discard block  | 
                                                    ||
| 145 | 145 | //  | 
                                                        
| 146 | 146 | // http://doc.spip.org/@abs_url  | 
                                                        
| 147 | 147 |  function abs_url($texte, $base='') { | 
                                                        
| 148 | - if ($GLOBALS['mode_abs_url'] == 'url')  | 
                                                        |
| 149 | - return url_absolue($texte, $base);  | 
                                                        |
| 150 | - else  | 
                                                        |
| 151 | - return liens_absolus($texte, $base);  | 
                                                        |
| 148 | + if ($GLOBALS['mode_abs_url'] == 'url')  | 
                                                        |
| 149 | + return url_absolue($texte, $base);  | 
                                                        |
| 150 | + else  | 
                                                        |
| 151 | + return liens_absolus($texte, $base);  | 
                                                        |
| 152 | 152 | }  | 
                                                        
| 153 | 153 | |
| 154 | 154 | /**  | 
                                                        
@@ -161,17 +161,17 @@ discard block  | 
                                                    ||
| 161 | 161 | * @return string  | 
                                                        
| 162 | 162 | */  | 
                                                        
| 163 | 163 |  function spip_htmlspecialchars($string, $flags=null, $encoding='ISO-8859-1', $double_encode = true){ | 
                                                        
| 164 | -	if (is_null($flags)) { | 
                                                        |
| 165 | -		if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50400) | 
                                                        |
| 166 | - $flags = ENT_COMPAT;  | 
                                                        |
| 167 | - else  | 
                                                        |
| 168 | - $flags = ENT_COMPAT|ENT_HTML401;  | 
                                                        |
| 169 | - }  | 
                                                        |
| 170 | -  | 
                                                        |
| 171 | -	if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50203) | 
                                                        |
| 172 | - return htmlspecialchars($string,$flags,$encoding);  | 
                                                        |
| 173 | - else  | 
                                                        |
| 174 | - return htmlspecialchars($string,$flags,$encoding,$double_encode);  | 
                                                        |
| 164 | +    if (is_null($flags)) { | 
                                                        |
| 165 | +        if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50400) | 
                                                        |
| 166 | + $flags = ENT_COMPAT;  | 
                                                        |
| 167 | + else  | 
                                                        |
| 168 | + $flags = ENT_COMPAT|ENT_HTML401;  | 
                                                        |
| 169 | + }  | 
                                                        |
| 170 | +  | 
                                                        |
| 171 | +    if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50203) | 
                                                        |
| 172 | + return htmlspecialchars($string,$flags,$encoding);  | 
                                                        |
| 173 | + else  | 
                                                        |
| 174 | + return htmlspecialchars($string,$flags,$encoding,$double_encode);  | 
                                                        |
| 175 | 175 | }  | 
                                                        
| 176 | 176 | |
| 177 | 177 | /**  | 
                                                        
@@ -184,16 +184,16 @@ discard block  | 
                                                    ||
| 184 | 184 | * @return string  | 
                                                        
| 185 | 185 | */  | 
                                                        
| 186 | 186 |  function spip_htmlentities($string,$flags=null,$encoding = 'ISO-8859-1',$double_encode = true){ | 
                                                        
| 187 | -	if (is_null($flags)) { | 
                                                        |
| 188 | -		if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50400) | 
                                                        |
| 189 | - $flags = ENT_COMPAT;  | 
                                                        |
| 190 | - else  | 
                                                        |
| 191 | - $flags = ENT_COMPAT|ENT_HTML401;  | 
                                                        |
| 192 | - }  | 
                                                        |
| 193 | -  | 
                                                        |
| 194 | -	if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50203) | 
                                                        |
| 195 | - return htmlentities($string,$flags,$encoding);  | 
                                                        |
| 196 | - else  | 
                                                        |
| 197 | - return htmlentities($string,$flags,$encoding,$double_encode);  | 
                                                        |
| 187 | +    if (is_null($flags)) { | 
                                                        |
| 188 | +        if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50400) | 
                                                        |
| 189 | + $flags = ENT_COMPAT;  | 
                                                        |
| 190 | + else  | 
                                                        |
| 191 | + $flags = ENT_COMPAT|ENT_HTML401;  | 
                                                        |
| 192 | + }  | 
                                                        |
| 193 | +  | 
                                                        |
| 194 | +    if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50203) | 
                                                        |
| 195 | + return htmlentities($string,$flags,$encoding);  | 
                                                        |
| 196 | + else  | 
                                                        |
| 197 | + return htmlentities($string,$flags,$encoding,$double_encode);  | 
                                                        |
| 198 | 198 | }  | 
                                                        
| 199 | 199 | ?>  | 
                                                        
@@ -50,7 +50,7 @@ discard block  | 
                                                    ||
| 50 | 50 | return $r[1].resolve_path($r[2]);  | 
                                                        
| 51 | 51 | |
| 52 | 52 | # L'url site spip est un lien absolu aussi  | 
                                                        
| 53 | -	if ($lien == $GLOBALS['meta']['adresse_site']){ | 
                                                        |
| 53 | +	if ($lien == $GLOBALS['meta']['adresse_site']) { | 
                                                        |
| 54 | 54 | return $lien;  | 
                                                        
| 55 | 55 | }  | 
                                                        
| 56 | 56 | |
@@ -60,29 +60,29 @@ discard block  | 
                                                    ||
| 60 | 60 | $debut = $regs[1];  | 
                                                        
| 61 | 61 | $dir = !strlen($regs[2]) ? '/' : $regs[2];  | 
                                                        
| 62 | 62 | $mot = $regs[3];  | 
                                                        
| 63 | - $get = isset($regs[4])?$regs[4]:"";  | 
                                                        |
| 64 | - $hash = isset($regs[5])?$regs[5]:"";  | 
                                                        |
| 63 | + $get = isset($regs[4]) ? $regs[4] : "";  | 
                                                        |
| 64 | + $hash = isset($regs[5]) ? $regs[5] : "";  | 
                                                        |
| 65 | 65 | }  | 
                                                        
| 66 | -	switch (substr($lien,0,1)) { | 
                                                        |
| 66 | +	switch (substr($lien, 0, 1)) { | 
                                                        |
| 67 | 67 | case '/':  | 
                                                        
| 68 | - return $debut . resolve_path($lien);  | 
                                                        |
| 68 | + return $debut.resolve_path($lien);  | 
                                                        |
| 69 | 69 | case '#':  | 
                                                        
| 70 | - return $debut . resolve_path($dir.$mot.$get.$lien);  | 
                                                        |
| 70 | + return $debut.resolve_path($dir.$mot.$get.$lien);  | 
                                                        |
| 71 | 71 | case '':  | 
                                                        
| 72 | - return $debut . resolve_path($dir.$mot.$get.$hash);  | 
                                                        |
| 72 | + return $debut.resolve_path($dir.$mot.$get.$hash);  | 
                                                        |
| 73 | 73 | default:  | 
                                                        
| 74 | - return $debut . resolve_path($dir.$lien);  | 
                                                        |
| 74 | + return $debut.resolve_path($dir.$lien);  | 
                                                        |
| 75 | 75 | }  | 
                                                        
| 76 | 76 | }  | 
                                                        
| 77 | 77 | |
| 78 | 78 | // un filtre pour transformer les URLs relatives en URLs absolues ;  | 
                                                        
| 79 | 79 | // ne s'applique qu'aux #URL_XXXX  | 
                                                        
| 80 | 80 | // http://doc.spip.org/@url_absolue  | 
                                                        
| 81 | -function url_absolue($url, $base='') { | 
                                                        |
| 81 | +function url_absolue($url, $base = '') { | 
                                                        |
| 82 | 82 | if (strlen($url = trim($url)) == 0)  | 
                                                        
| 83 | 83 | return '';  | 
                                                        
| 84 | 84 | if (!$base)  | 
                                                        
| 85 | - $base = url_de_base() . (_DIR_RACINE ? _DIR_RESTREINT_ABS : '');  | 
                                                        |
| 85 | + $base = url_de_base().(_DIR_RACINE ? _DIR_RESTREINT_ABS : '');  | 
                                                        |
| 86 | 86 | return suivre_lien($base, $url);  | 
                                                        
| 87 | 87 | }  | 
                                                        
| 88 | 88 | |
@@ -92,8 +92,8 @@ discard block  | 
                                                    ||
| 92 | 92 | * @param string $url_absolue  | 
                                                        
| 93 | 93 | * @return string  | 
                                                        
| 94 | 94 | */  | 
                                                        
| 95 | -function protocole_implicite($url_absolue){ | 
                                                        |
| 96 | -	return preg_replace(";^[a-z]{3,7}://;i","//",$url_absolue); | 
                                                        |
| 95 | +function protocole_implicite($url_absolue) { | 
                                                        |
| 96 | +	return preg_replace(";^[a-z]{3,7}://;i", "//", $url_absolue); | 
                                                        |
| 97 | 97 | }  | 
                                                        
| 98 | 98 | |
| 99 | 99 | /**  | 
                                                        
@@ -102,7 +102,7 @@ discard block  | 
                                                    ||
| 102 | 102 | * @param array $protocoles_autorises  | 
                                                        
| 103 | 103 | * @return bool  | 
                                                        
| 104 | 104 | */  | 
                                                        
| 105 | -function protocole_verifier($url_absolue, $protocoles_autorises = array('http','https')) { | 
                                                        |
| 105 | +function protocole_verifier($url_absolue, $protocoles_autorises = array('http', 'https')) { | 
                                                        |
| 106 | 106 | |
| 107 | 107 |  	if (preg_match(';^([a-z]{3,7})://;i', $url_absolue, $m)) { | 
                                                        
| 108 | 108 | $protocole = $m[1];  | 
                                                        
@@ -117,20 +117,20 @@ discard block  | 
                                                    ||
| 117 | 117 | // un filtre pour transformer les URLs relatives en URLs absolues ;  | 
                                                        
| 118 | 118 | // ne s'applique qu'aux textes contenant des liens  | 
                                                        
| 119 | 119 | // http://doc.spip.org/@liens_absolus  | 
                                                        
| 120 | -function liens_absolus($texte, $base='') { | 
                                                        |
| 120 | +function liens_absolus($texte, $base = '') { | 
                                                        |
| 121 | 121 |  	if (preg_match_all(',(<(a|link|image|img|script)\s[^<>]*(href|src)=[^<>]*>),imsS',  | 
                                                        
| 122 | 122 |  	$texte, $liens, PREG_SET_ORDER)) { | 
                                                        
| 123 | 123 |  		if (!function_exists('extraire_attribut')) { | 
                                                        
| 124 | 124 |  			include_spip('inc/filtres'); | 
                                                        
| 125 | 125 | }  | 
                                                        
| 126 | 126 |  		foreach ($liens as $lien) { | 
                                                        
| 127 | -			foreach(array('href', 'src') as $attr) { | 
                                                        |
| 127 | +			foreach (array('href', 'src') as $attr) { | 
                                                        |
| 128 | 128 | $href = extraire_attribut($lien[0], $attr);  | 
                                                        
| 129 | -				if (strlen($href)>0) { | 
                                                        |
| 129 | +				if (strlen($href) > 0) { | 
                                                        |
| 130 | 130 | $abs = url_absolue($href, $base);  | 
                                                        
| 131 | -					if ($href != $abs and !preg_match('/^#/',$href)) { | 
                                                        |
| 131 | +					if ($href != $abs and !preg_match('/^#/', $href)) { | 
                                                        |
| 132 | 132 | $texte_lien = inserer_attribut($lien[0], $attr, $abs);  | 
                                                        
| 133 | - $texte = str_replace($lien[0],$texte_lien,$texte);  | 
                                                        |
| 133 | + $texte = str_replace($lien[0], $texte_lien, $texte);  | 
                                                        |
| 134 | 134 | }  | 
                                                        
| 135 | 135 | }  | 
                                                        
| 136 | 136 | }  | 
                                                        
@@ -144,7 +144,7 @@ discard block  | 
                                                    ||
| 144 | 144 | // Ce filtre public va traiter les URL ou les <a href>  | 
                                                        
| 145 | 145 | //  | 
                                                        
| 146 | 146 | // http://doc.spip.org/@abs_url  | 
                                                        
| 147 | -function abs_url($texte, $base='') { | 
                                                        |
| 147 | +function abs_url($texte, $base = '') { | 
                                                        |
| 148 | 148 | if ($GLOBALS['mode_abs_url'] == 'url')  | 
                                                        
| 149 | 149 | return url_absolue($texte, $base);  | 
                                                        
| 150 | 150 | else  | 
                                                        
@@ -160,18 +160,18 @@ discard block  | 
                                                    ||
| 160 | 160 | * @param bool $double_encode  | 
                                                        
| 161 | 161 | * @return string  | 
                                                        
| 162 | 162 | */  | 
                                                        
| 163 | -function spip_htmlspecialchars($string, $flags=null, $encoding='ISO-8859-1', $double_encode = true){ | 
                                                        |
| 163 | +function spip_htmlspecialchars($string, $flags = null, $encoding = 'ISO-8859-1', $double_encode = true) { | 
                                                        |
| 164 | 164 |  	if (is_null($flags)) { | 
                                                        
| 165 | 165 |  		if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50400) | 
                                                        
| 166 | 166 | $flags = ENT_COMPAT;  | 
                                                        
| 167 | 167 | else  | 
                                                        
| 168 | - $flags = ENT_COMPAT|ENT_HTML401;  | 
                                                        |
| 168 | + $flags = ENT_COMPAT | ENT_HTML401;  | 
                                                        |
| 169 | 169 | }  | 
                                                        
| 170 | 170 | |
| 171 | 171 |  	if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50203) | 
                                                        
| 172 | - return htmlspecialchars($string,$flags,$encoding);  | 
                                                        |
| 172 | + return htmlspecialchars($string, $flags, $encoding);  | 
                                                        |
| 173 | 173 | else  | 
                                                        
| 174 | - return htmlspecialchars($string,$flags,$encoding,$double_encode);  | 
                                                        |
| 174 | + return htmlspecialchars($string, $flags, $encoding, $double_encode);  | 
                                                        |
| 175 | 175 | }  | 
                                                        
| 176 | 176 | |
| 177 | 177 | /**  | 
                                                        
@@ -183,17 +183,17 @@ discard block  | 
                                                    ||
| 183 | 183 | * @param bool $double_encode  | 
                                                        
| 184 | 184 | * @return string  | 
                                                        
| 185 | 185 | */  | 
                                                        
| 186 | -function spip_htmlentities($string,$flags=null,$encoding = 'ISO-8859-1',$double_encode = true){ | 
                                                        |
| 186 | +function spip_htmlentities($string, $flags = null, $encoding = 'ISO-8859-1', $double_encode = true) { | 
                                                        |
| 187 | 187 |  	if (is_null($flags)) { | 
                                                        
| 188 | 188 |  		if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50400) | 
                                                        
| 189 | 189 | $flags = ENT_COMPAT;  | 
                                                        
| 190 | 190 | else  | 
                                                        
| 191 | - $flags = ENT_COMPAT|ENT_HTML401;  | 
                                                        |
| 191 | + $flags = ENT_COMPAT | ENT_HTML401;  | 
                                                        |
| 192 | 192 | }  | 
                                                        
| 193 | 193 | |
| 194 | 194 |  	if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50203) | 
                                                        
| 195 | - return htmlentities($string,$flags,$encoding);  | 
                                                        |
| 195 | + return htmlentities($string, $flags, $encoding);  | 
                                                        |
| 196 | 196 | else  | 
                                                        
| 197 | - return htmlentities($string,$flags,$encoding,$double_encode);  | 
                                                        |
| 197 | + return htmlentities($string, $flags, $encoding, $double_encode);  | 
                                                        |
| 198 | 198 | }  | 
                                                        
| 199 | 199 | ?>  | 
                                                        
@@ -24,8 +24,8 @@ discard block  | 
                                                    ||
| 24 | 24 |  if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) define('_INC_DISTANT_CONNECT_TIMEOUT',10); | 
                                                        
| 25 | 25 | |
| 26 | 26 |  define('_REGEXP_COPIE_LOCALE', ',' . | 
                                                        
| 27 | -       preg_replace('@^https?:@', 'https?:', $GLOBALS['meta']['adresse_site']) | 
                                                        |
| 28 | - . "/?spip.php[?]action=acceder_document.*file=(.*)$,");  | 
                                                        |
| 27 | +        preg_replace('@^https?:@', 'https?:', $GLOBALS['meta']['adresse_site']) | 
                                                        |
| 28 | + . "/?spip.php[?]action=acceder_document.*file=(.*)$,");  | 
                                                        |
| 29 | 29 | |
| 30 | 30 |  //@define('_COPIE_LOCALE_MAX_SIZE',2097152); // poids (inc/utils l'a fait) | 
                                                        
| 31 | 31 | |
@@ -51,62 +51,62 @@ discard block  | 
                                                    ||
| 51 | 51 | */  | 
                                                        
| 52 | 52 |  function copie_locale($source, $mode='auto', $local=null, $taille_max=null){ | 
                                                        
| 53 | 53 | |
| 54 | - // si c'est la protection de soi-meme, retourner le path  | 
                                                        |
| 55 | -	if ($mode !== 'force' AND preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { | 
                                                        |
| 56 | - $source = substr(_DIR_IMG,strlen(_DIR_RACINE)) . urldecode($match[1]);  | 
                                                        |
| 57 | - return @file_exists($source) ? $source : false;  | 
                                                        |
| 58 | - }  | 
                                                        |
| 59 | -  | 
                                                        |
| 60 | - if (is_null($local))  | 
                                                        |
| 61 | - $local = fichier_copie_locale($source);  | 
                                                        |
| 62 | -	else { | 
                                                        |
| 63 | -		if (_DIR_RACINE AND strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE))==0) { | 
                                                        |
| 64 | - $local = substr($local, strlen(_DIR_RACINE));  | 
                                                        |
| 65 | - }  | 
                                                        |
| 66 | - }  | 
                                                        |
| 67 | -  | 
                                                        |
| 68 | - // si $local = '' c'est un fichier refuse par fichier_copie_locale(),  | 
                                                        |
| 69 | - // par exemple un fichier qui ne figure pas dans nos documents ;  | 
                                                        |
| 70 | - // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer  | 
                                                        |
| 71 | - if (!$local) return false;  | 
                                                        |
| 72 | -  | 
                                                        |
| 73 | - $localrac = _DIR_RACINE.$local;  | 
                                                        |
| 74 | - $t = ($mode=='force') ? false : @file_exists($localrac);  | 
                                                        |
| 75 | -  | 
                                                        |
| 76 | - // test d'existence du fichier  | 
                                                        |
| 77 | - if ($mode=='test') return $t ? $local : '';  | 
                                                        |
| 78 | -  | 
                                                        |
| 79 | - // sinon voir si on doit/peut le telecharger  | 
                                                        |
| 80 | -	if ($local==$source OR !preg_match(',^\w+://,', $source)) | 
                                                        |
| 81 | - return $local;  | 
                                                        |
| 82 | -  | 
                                                        |
| 83 | -	if ($mode=='modif' OR !$t){ | 
                                                        |
| 84 | - // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation  | 
                                                        |
| 85 | - // et des eventuelles recuperations concurantes  | 
                                                        |
| 86 | -		include_spip("inc/acces"); | 
                                                        |
| 87 | - if (!$taille_max) $taille_max = _COPIE_LOCALE_MAX_SIZE;  | 
                                                        |
| 88 | - $res = recuperer_page($source, $localrac, false, $taille_max, '', '', false, $t ? filemtime($localrac) : '');  | 
                                                        |
| 89 | -		if (!$res) { | 
                                                        |
| 90 | - if (!$t) // si $t c'est sans doute juste un not-modified-since qui fait renvoyer false  | 
                                                        |
| 91 | -				spip_log("copie_locale : Echec recuperation $source sur $localrac",_LOG_INFO_IMPORTANTE); | 
                                                        |
| 92 | - return $t ? $local : false;  | 
                                                        |
| 93 | - }  | 
                                                        |
| 94 | -		spip_log("copie_locale : recuperation $source sur $localrac taille $res OK"); | 
                                                        |
| 95 | -  | 
                                                        |
| 96 | - // pour une eventuelle indexation  | 
                                                        |
| 97 | -		pipeline('post_edition', | 
                                                        |
| 98 | - array(  | 
                                                        |
| 99 | - 'args' => array(  | 
                                                        |
| 100 | - 'operation' => 'copie_locale',  | 
                                                        |
| 101 | - 'source' => $source,  | 
                                                        |
| 102 | - 'fichier' => $local  | 
                                                        |
| 103 | - ),  | 
                                                        |
| 104 | - 'data' => null  | 
                                                        |
| 105 | - )  | 
                                                        |
| 106 | - );  | 
                                                        |
| 107 | - }  | 
                                                        |
| 108 | -  | 
                                                        |
| 109 | - return $local;  | 
                                                        |
| 54 | + // si c'est la protection de soi-meme, retourner le path  | 
                                                        |
| 55 | +    if ($mode !== 'force' AND preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { | 
                                                        |
| 56 | + $source = substr(_DIR_IMG,strlen(_DIR_RACINE)) . urldecode($match[1]);  | 
                                                        |
| 57 | + return @file_exists($source) ? $source : false;  | 
                                                        |
| 58 | + }  | 
                                                        |
| 59 | +  | 
                                                        |
| 60 | + if (is_null($local))  | 
                                                        |
| 61 | + $local = fichier_copie_locale($source);  | 
                                                        |
| 62 | +    else { | 
                                                        |
| 63 | +        if (_DIR_RACINE AND strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE))==0) { | 
                                                        |
| 64 | + $local = substr($local, strlen(_DIR_RACINE));  | 
                                                        |
| 65 | + }  | 
                                                        |
| 66 | + }  | 
                                                        |
| 67 | +  | 
                                                        |
| 68 | + // si $local = '' c'est un fichier refuse par fichier_copie_locale(),  | 
                                                        |
| 69 | + // par exemple un fichier qui ne figure pas dans nos documents ;  | 
                                                        |
| 70 | + // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer  | 
                                                        |
| 71 | + if (!$local) return false;  | 
                                                        |
| 72 | +  | 
                                                        |
| 73 | + $localrac = _DIR_RACINE.$local;  | 
                                                        |
| 74 | + $t = ($mode=='force') ? false : @file_exists($localrac);  | 
                                                        |
| 75 | +  | 
                                                        |
| 76 | + // test d'existence du fichier  | 
                                                        |
| 77 | + if ($mode=='test') return $t ? $local : '';  | 
                                                        |
| 78 | +  | 
                                                        |
| 79 | + // sinon voir si on doit/peut le telecharger  | 
                                                        |
| 80 | +    if ($local==$source OR !preg_match(',^\w+://,', $source)) | 
                                                        |
| 81 | + return $local;  | 
                                                        |
| 82 | +  | 
                                                        |
| 83 | +    if ($mode=='modif' OR !$t){ | 
                                                        |
| 84 | + // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation  | 
                                                        |
| 85 | + // et des eventuelles recuperations concurantes  | 
                                                        |
| 86 | +        include_spip("inc/acces"); | 
                                                        |
| 87 | + if (!$taille_max) $taille_max = _COPIE_LOCALE_MAX_SIZE;  | 
                                                        |
| 88 | + $res = recuperer_page($source, $localrac, false, $taille_max, '', '', false, $t ? filemtime($localrac) : '');  | 
                                                        |
| 89 | +        if (!$res) { | 
                                                        |
| 90 | + if (!$t) // si $t c'est sans doute juste un not-modified-since qui fait renvoyer false  | 
                                                        |
| 91 | +                spip_log("copie_locale : Echec recuperation $source sur $localrac",_LOG_INFO_IMPORTANTE); | 
                                                        |
| 92 | + return $t ? $local : false;  | 
                                                        |
| 93 | + }  | 
                                                        |
| 94 | +        spip_log("copie_locale : recuperation $source sur $localrac taille $res OK"); | 
                                                        |
| 95 | +  | 
                                                        |
| 96 | + // pour une eventuelle indexation  | 
                                                        |
| 97 | +        pipeline('post_edition', | 
                                                        |
| 98 | + array(  | 
                                                        |
| 99 | + 'args' => array(  | 
                                                        |
| 100 | + 'operation' => 'copie_locale',  | 
                                                        |
| 101 | + 'source' => $source,  | 
                                                        |
| 102 | + 'fichier' => $local  | 
                                                        |
| 103 | + ),  | 
                                                        |
| 104 | + 'data' => null  | 
                                                        |
| 105 | + )  | 
                                                        |
| 106 | + );  | 
                                                        |
| 107 | + }  | 
                                                        |
| 108 | +  | 
                                                        |
| 109 | + return $local;  | 
                                                        |
| 110 | 110 | }  | 
                                                        
| 111 | 111 | |
| 112 | 112 | /**  | 
                                                        
@@ -121,173 +121,173 @@ discard block  | 
                                                    ||
| 121 | 121 | * url ou false en cas d'echec  | 
                                                        
| 122 | 122 | */  | 
                                                        
| 123 | 123 |  function valider_url_distante($url, $known_hosts = array()) { | 
                                                        
| 124 | -	if (!function_exists('protocole_verifier')){ | 
                                                        |
| 125 | -		include_spip('inc/filtres_mini'); | 
                                                        |
| 126 | - }  | 
                                                        |
| 124 | +    if (!function_exists('protocole_verifier')){ | 
                                                        |
| 125 | +        include_spip('inc/filtres_mini'); | 
                                                        |
| 126 | + }  | 
                                                        |
| 127 | 127 | |
| 128 | -	if (!protocole_verifier($url, array('http', 'https'))) { | 
                                                        |
| 129 | - return false;  | 
                                                        |
| 130 | - }  | 
                                                        |
| 128 | +    if (!protocole_verifier($url, array('http', 'https'))) { | 
                                                        |
| 129 | + return false;  | 
                                                        |
| 130 | + }  | 
                                                        |
| 131 | 131 | |
| 132 | - $parsed_url = parse_url($url);  | 
                                                        |
| 133 | -	if (!$parsed_url or empty($parsed_url['host']) ) { | 
                                                        |
| 134 | - return false;  | 
                                                        |
| 135 | - }  | 
                                                        |
| 136 | -  | 
                                                        |
| 137 | -	if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { | 
                                                        |
| 138 | - return false;  | 
                                                        |
| 139 | - }  | 
                                                        |
| 140 | -  | 
                                                        |
| 141 | -	if (false !== strpbrk($parsed_url['host'], ':#?[]')) { | 
                                                        |
| 142 | - return false;  | 
                                                        |
| 143 | - }  | 
                                                        |
| 144 | -  | 
                                                        |
| 145 | -	if (!is_array($known_hosts)) { | 
                                                        |
| 146 | - $known_hosts = array($known_hosts);  | 
                                                        |
| 147 | - }  | 
                                                        |
| 148 | - $known_hosts[] = $GLOBALS['meta']['adresse_site'];  | 
                                                        |
| 149 | - $known_hosts[] = url_de_base();  | 
                                                        |
| 150 | -	$known_hosts = pipeline('declarer_hosts_distants', $known_hosts); | 
                                                        |
| 151 | -  | 
                                                        |
| 152 | - $is_known_host = false;  | 
                                                        |
| 153 | -	foreach ($known_hosts as $known_host) { | 
                                                        |
| 154 | - $parse_known = parse_url($known_host);  | 
                                                        |
| 155 | - if ($parse_known  | 
                                                        |
| 156 | -		  and strtolower($parse_known['host']) === strtolower($parsed_url['host'])) { | 
                                                        |
| 157 | - $is_known_host = true;  | 
                                                        |
| 158 | - break;  | 
                                                        |
| 159 | - }  | 
                                                        |
| 160 | - }  | 
                                                        |
| 161 | -  | 
                                                        |
| 162 | -	if (!$is_known_host) { | 
                                                        |
| 163 | - $host = trim($parsed_url['host'], '.');  | 
                                                        |
| 164 | -		if (preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host)) { | 
                                                        |
| 165 | - $ip = $host;  | 
                                                        |
| 166 | -		} else { | 
                                                        |
| 167 | - $ip = gethostbyname($host);  | 
                                                        |
| 168 | -			if ($ip === $host) { | 
                                                        |
| 169 | - // Error condition for gethostbyname()  | 
                                                        |
| 170 | - $ip = false;  | 
                                                        |
| 171 | - }  | 
                                                        |
| 172 | - }  | 
                                                        |
| 173 | -		if ($ip) { | 
                                                        |
| 174 | -			$parts = array_map('intval', explode( '.', $ip )); | 
                                                        |
| 175 | - if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0]  | 
                                                        |
| 176 | - or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] )  | 
                                                        |
| 177 | - or ( 192 === $parts[0] && 168 === $parts[1] )  | 
                                                        |
| 178 | -			) { | 
                                                        |
| 179 | - return false;  | 
                                                        |
| 180 | - }  | 
                                                        |
| 181 | - }  | 
                                                        |
| 182 | - }  | 
                                                        |
| 183 | -  | 
                                                        |
| 184 | -	if (empty($parsed_url['port'])) { | 
                                                        |
| 185 | - return $url;  | 
                                                        |
| 186 | - }  | 
                                                        |
| 187 | -  | 
                                                        |
| 188 | - $port = $parsed_url['port'];  | 
                                                        |
| 189 | -	if ($port === 80  or $port === 443  or $port === 8080) { | 
                                                        |
| 190 | - return $url;  | 
                                                        |
| 191 | - }  | 
                                                        |
| 192 | -  | 
                                                        |
| 193 | -	if ($is_known_host) { | 
                                                        |
| 194 | -		foreach ($known_hosts as $known_host) { | 
                                                        |
| 195 | - $parse_known = parse_url($known_host);  | 
                                                        |
| 196 | - if ($parse_known  | 
                                                        |
| 197 | - and !empty($parse_known['port'])  | 
                                                        |
| 198 | - and strtolower($parse_known['host']) === strtolower($parsed_url['host'])  | 
                                                        |
| 199 | -			  and $parse_known['port'] == $port) { | 
                                                        |
| 200 | - return $url;  | 
                                                        |
| 201 | - }  | 
                                                        |
| 202 | - }  | 
                                                        |
| 203 | - }  | 
                                                        |
| 204 | -  | 
                                                        |
| 205 | - return false;  | 
                                                        |
| 132 | + $parsed_url = parse_url($url);  | 
                                                        |
| 133 | +    if (!$parsed_url or empty($parsed_url['host']) ) { | 
                                                        |
| 134 | + return false;  | 
                                                        |
| 135 | + }  | 
                                                        |
| 136 | +  | 
                                                        |
| 137 | +    if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { | 
                                                        |
| 138 | + return false;  | 
                                                        |
| 139 | + }  | 
                                                        |
| 140 | +  | 
                                                        |
| 141 | +    if (false !== strpbrk($parsed_url['host'], ':#?[]')) { | 
                                                        |
| 142 | + return false;  | 
                                                        |
| 143 | + }  | 
                                                        |
| 144 | +  | 
                                                        |
| 145 | +    if (!is_array($known_hosts)) { | 
                                                        |
| 146 | + $known_hosts = array($known_hosts);  | 
                                                        |
| 147 | + }  | 
                                                        |
| 148 | + $known_hosts[] = $GLOBALS['meta']['adresse_site'];  | 
                                                        |
| 149 | + $known_hosts[] = url_de_base();  | 
                                                        |
| 150 | +    $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); | 
                                                        |
| 151 | +  | 
                                                        |
| 152 | + $is_known_host = false;  | 
                                                        |
| 153 | +    foreach ($known_hosts as $known_host) { | 
                                                        |
| 154 | + $parse_known = parse_url($known_host);  | 
                                                        |
| 155 | + if ($parse_known  | 
                                                        |
| 156 | +          and strtolower($parse_known['host']) === strtolower($parsed_url['host'])) { | 
                                                        |
| 157 | + $is_known_host = true;  | 
                                                        |
| 158 | + break;  | 
                                                        |
| 159 | + }  | 
                                                        |
| 160 | + }  | 
                                                        |
| 161 | +  | 
                                                        |
| 162 | +    if (!$is_known_host) { | 
                                                        |
| 163 | + $host = trim($parsed_url['host'], '.');  | 
                                                        |
| 164 | +        if (preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host)) { | 
                                                        |
| 165 | + $ip = $host;  | 
                                                        |
| 166 | +        } else { | 
                                                        |
| 167 | + $ip = gethostbyname($host);  | 
                                                        |
| 168 | +            if ($ip === $host) { | 
                                                        |
| 169 | + // Error condition for gethostbyname()  | 
                                                        |
| 170 | + $ip = false;  | 
                                                        |
| 171 | + }  | 
                                                        |
| 172 | + }  | 
                                                        |
| 173 | +        if ($ip) { | 
                                                        |
| 174 | +            $parts = array_map('intval', explode( '.', $ip )); | 
                                                        |
| 175 | + if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0]  | 
                                                        |
| 176 | + or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] )  | 
                                                        |
| 177 | + or ( 192 === $parts[0] && 168 === $parts[1] )  | 
                                                        |
| 178 | +            ) { | 
                                                        |
| 179 | + return false;  | 
                                                        |
| 180 | + }  | 
                                                        |
| 181 | + }  | 
                                                        |
| 182 | + }  | 
                                                        |
| 183 | +  | 
                                                        |
| 184 | +    if (empty($parsed_url['port'])) { | 
                                                        |
| 185 | + return $url;  | 
                                                        |
| 186 | + }  | 
                                                        |
| 187 | +  | 
                                                        |
| 188 | + $port = $parsed_url['port'];  | 
                                                        |
| 189 | +    if ($port === 80  or $port === 443  or $port === 8080) { | 
                                                        |
| 190 | + return $url;  | 
                                                        |
| 191 | + }  | 
                                                        |
| 192 | +  | 
                                                        |
| 193 | +    if ($is_known_host) { | 
                                                        |
| 194 | +        foreach ($known_hosts as $known_host) { | 
                                                        |
| 195 | + $parse_known = parse_url($known_host);  | 
                                                        |
| 196 | + if ($parse_known  | 
                                                        |
| 197 | + and !empty($parse_known['port'])  | 
                                                        |
| 198 | + and strtolower($parse_known['host']) === strtolower($parsed_url['host'])  | 
                                                        |
| 199 | +              and $parse_known['port'] == $port) { | 
                                                        |
| 200 | + return $url;  | 
                                                        |
| 201 | + }  | 
                                                        |
| 202 | + }  | 
                                                        |
| 203 | + }  | 
                                                        |
| 204 | +  | 
                                                        |
| 205 | + return false;  | 
                                                        |
| 206 | 206 | }  | 
                                                        
| 207 | 207 | |
| 208 | 208 | // http://doc.spip.org/@prepare_donnees_post  | 
                                                        
| 209 | 209 |  function prepare_donnees_post($donnees, $boundary = '') { | 
                                                        
| 210 | 210 | |
| 211 | - // permettre a la fonction qui a demande le post de formater elle meme ses donnees  | 
                                                        |
| 212 | - // pour un appel soap par exemple  | 
                                                        |
| 213 | - // l'entete est separe des donnees par un double retour a la ligne  | 
                                                        |
| 214 | - // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n  | 
                                                        |
| 215 | -	if (is_string($donnees) && strlen($donnees)){ | 
                                                        |
| 216 | - $entete = "";  | 
                                                        |
| 217 | - // on repasse tous les \r\n et \r en simples \n  | 
                                                        |
| 218 | -		$donnees = str_replace("\r\n","\n",$donnees); | 
                                                        |
| 219 | -		$donnees = str_replace("\r","\n",$donnees); | 
                                                        |
| 220 | - // un double retour a la ligne signifie la fin de l'entete et le debut des donnees  | 
                                                        |
| 221 | - $p = strpos($donnees, "\n\n");  | 
                                                        |
| 222 | -		if ($p!==FALSE){ | 
                                                        |
| 223 | -			$entete = str_replace("\n", "\r\n", substr($donnees, 0, $p+1)); | 
                                                        |
| 224 | - $donnees = substr($donnees, $p+2);  | 
                                                        |
| 225 | - }  | 
                                                        |
| 226 | -		$chaine = str_replace("\n", "\r\n", $donnees); | 
                                                        |
| 227 | - }  | 
                                                        |
| 228 | -	else { | 
                                                        |
| 229 | - /* boundary automatique */  | 
                                                        |
| 230 | - // Si on a plus de 500 octects de donnees, on "boundarise"  | 
                                                        |
| 231 | -		if ($boundary===''){ | 
                                                        |
| 232 | - $taille = 0;  | 
                                                        |
| 233 | -			foreach ($donnees as $cle => $valeur){ | 
                                                        |
| 234 | -				if (is_array($valeur)){ | 
                                                        |
| 235 | -					foreach ($valeur as $val2){ | 
                                                        |
| 236 | - $taille += strlen($val2);  | 
                                                        |
| 237 | - }  | 
                                                        |
| 238 | -				} else { | 
                                                        |
| 239 | - // faut-il utiliser spip_strlen() dans inc/charsets ?  | 
                                                        |
| 240 | - $taille += strlen($valeur);  | 
                                                        |
| 241 | - }  | 
                                                        |
| 242 | - }  | 
                                                        |
| 243 | -			if ($taille>500){ | 
                                                        |
| 244 | - $boundary = substr(md5(rand() . 'spip'), 0, 8);  | 
                                                        |
| 245 | - }  | 
                                                        |
| 246 | - }  | 
                                                        |
| 247 | -  | 
                                                        |
| 248 | -		if (is_string($boundary) and strlen($boundary)){ | 
                                                        |
| 249 | - // fabrique une chaine HTTP pour un POST avec boundary  | 
                                                        |
| 250 | - $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n";  | 
                                                        |
| 251 | - $chaine = '';  | 
                                                        |
| 252 | -			if (is_array($donnees)) { | 
                                                        |
| 253 | -				foreach ($donnees as $cle => $valeur) { | 
                                                        |
| 254 | -					if (is_array($valeur)) { | 
                                                        |
| 255 | -						foreach ($valeur as $val2) { | 
                                                        |
| 256 | - $chaine .= "\r\n--$boundary\r\n";  | 
                                                        |
| 257 | -							$chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; | 
                                                        |
| 258 | - $chaine .= "\r\n";  | 
                                                        |
| 259 | - $chaine .= $val2;  | 
                                                        |
| 260 | - }  | 
                                                        |
| 261 | -					} else { | 
                                                        |
| 262 | - $chaine .= "\r\n--$boundary\r\n";  | 
                                                        |
| 263 | - $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n";  | 
                                                        |
| 264 | - $chaine .= "\r\n";  | 
                                                        |
| 265 | - $chaine .= $valeur;  | 
                                                        |
| 266 | - }  | 
                                                        |
| 267 | - }  | 
                                                        |
| 268 | - $chaine .= "\r\n--$boundary\r\n";  | 
                                                        |
| 269 | - }  | 
                                                        |
| 270 | -		} else { | 
                                                        |
| 271 | - // fabrique une chaine HTTP simple pour un POST  | 
                                                        |
| 272 | - $entete = 'Content-Type: application/x-www-form-urlencoded'."\r\n";  | 
                                                        |
| 273 | - $chaine = array();  | 
                                                        |
| 274 | -			if (is_array($donnees)) { | 
                                                        |
| 275 | -				foreach ($donnees as $cle => $valeur) { | 
                                                        |
| 276 | -					if (is_array($valeur)) { | 
                                                        |
| 277 | -						foreach ($valeur as $val2) { | 
                                                        |
| 278 | - $chaine[] = rawurlencode($cle).'[]='.rawurlencode($val2);  | 
                                                        |
| 279 | - }  | 
                                                        |
| 280 | -					} else { | 
                                                        |
| 281 | - $chaine[] = rawurlencode($cle).'='.rawurlencode($valeur);  | 
                                                        |
| 282 | - }  | 
                                                        |
| 283 | - }  | 
                                                        |
| 284 | -				$chaine = implode('&', $chaine); | 
                                                        |
| 285 | -			} else { | 
                                                        |
| 286 | - $chaine = $donnees;  | 
                                                        |
| 287 | - }  | 
                                                        |
| 288 | - }  | 
                                                        |
| 289 | - }  | 
                                                        |
| 290 | - return array($entete, $chaine);  | 
                                                        |
| 211 | + // permettre a la fonction qui a demande le post de formater elle meme ses donnees  | 
                                                        |
| 212 | + // pour un appel soap par exemple  | 
                                                        |
| 213 | + // l'entete est separe des donnees par un double retour a la ligne  | 
                                                        |
| 214 | + // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n  | 
                                                        |
| 215 | +    if (is_string($donnees) && strlen($donnees)){ | 
                                                        |
| 216 | + $entete = "";  | 
                                                        |
| 217 | + // on repasse tous les \r\n et \r en simples \n  | 
                                                        |
| 218 | +        $donnees = str_replace("\r\n","\n",$donnees); | 
                                                        |
| 219 | +        $donnees = str_replace("\r","\n",$donnees); | 
                                                        |
| 220 | + // un double retour a la ligne signifie la fin de l'entete et le debut des donnees  | 
                                                        |
| 221 | + $p = strpos($donnees, "\n\n");  | 
                                                        |
| 222 | +        if ($p!==FALSE){ | 
                                                        |
| 223 | +            $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p+1)); | 
                                                        |
| 224 | + $donnees = substr($donnees, $p+2);  | 
                                                        |
| 225 | + }  | 
                                                        |
| 226 | +        $chaine = str_replace("\n", "\r\n", $donnees); | 
                                                        |
| 227 | + }  | 
                                                        |
| 228 | +    else { | 
                                                        |
| 229 | + /* boundary automatique */  | 
                                                        |
| 230 | + // Si on a plus de 500 octects de donnees, on "boundarise"  | 
                                                        |
| 231 | +        if ($boundary===''){ | 
                                                        |
| 232 | + $taille = 0;  | 
                                                        |
| 233 | +            foreach ($donnees as $cle => $valeur){ | 
                                                        |
| 234 | +                if (is_array($valeur)){ | 
                                                        |
| 235 | +                    foreach ($valeur as $val2){ | 
                                                        |
| 236 | + $taille += strlen($val2);  | 
                                                        |
| 237 | + }  | 
                                                        |
| 238 | +                } else { | 
                                                        |
| 239 | + // faut-il utiliser spip_strlen() dans inc/charsets ?  | 
                                                        |
| 240 | + $taille += strlen($valeur);  | 
                                                        |
| 241 | + }  | 
                                                        |
| 242 | + }  | 
                                                        |
| 243 | +            if ($taille>500){ | 
                                                        |
| 244 | + $boundary = substr(md5(rand() . 'spip'), 0, 8);  | 
                                                        |
| 245 | + }  | 
                                                        |
| 246 | + }  | 
                                                        |
| 247 | +  | 
                                                        |
| 248 | +        if (is_string($boundary) and strlen($boundary)){ | 
                                                        |
| 249 | + // fabrique une chaine HTTP pour un POST avec boundary  | 
                                                        |
| 250 | + $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n";  | 
                                                        |
| 251 | + $chaine = '';  | 
                                                        |
| 252 | +            if (is_array($donnees)) { | 
                                                        |
| 253 | +                foreach ($donnees as $cle => $valeur) { | 
                                                        |
| 254 | +                    if (is_array($valeur)) { | 
                                                        |
| 255 | +                        foreach ($valeur as $val2) { | 
                                                        |
| 256 | + $chaine .= "\r\n--$boundary\r\n";  | 
                                                        |
| 257 | +                            $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; | 
                                                        |
| 258 | + $chaine .= "\r\n";  | 
                                                        |
| 259 | + $chaine .= $val2;  | 
                                                        |
| 260 | + }  | 
                                                        |
| 261 | +                    } else { | 
                                                        |
| 262 | + $chaine .= "\r\n--$boundary\r\n";  | 
                                                        |
| 263 | + $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n";  | 
                                                        |
| 264 | + $chaine .= "\r\n";  | 
                                                        |
| 265 | + $chaine .= $valeur;  | 
                                                        |
| 266 | + }  | 
                                                        |
| 267 | + }  | 
                                                        |
| 268 | + $chaine .= "\r\n--$boundary\r\n";  | 
                                                        |
| 269 | + }  | 
                                                        |
| 270 | +        } else { | 
                                                        |
| 271 | + // fabrique une chaine HTTP simple pour un POST  | 
                                                        |
| 272 | + $entete = 'Content-Type: application/x-www-form-urlencoded'."\r\n";  | 
                                                        |
| 273 | + $chaine = array();  | 
                                                        |
| 274 | +            if (is_array($donnees)) { | 
                                                        |
| 275 | +                foreach ($donnees as $cle => $valeur) { | 
                                                        |
| 276 | +                    if (is_array($valeur)) { | 
                                                        |
| 277 | +                        foreach ($valeur as $val2) { | 
                                                        |
| 278 | + $chaine[] = rawurlencode($cle).'[]='.rawurlencode($val2);  | 
                                                        |
| 279 | + }  | 
                                                        |
| 280 | +                    } else { | 
                                                        |
| 281 | + $chaine[] = rawurlencode($cle).'='.rawurlencode($valeur);  | 
                                                        |
| 282 | + }  | 
                                                        |
| 283 | + }  | 
                                                        |
| 284 | +                $chaine = implode('&', $chaine); | 
                                                        |
| 285 | +            } else { | 
                                                        |
| 286 | + $chaine = $donnees;  | 
                                                        |
| 287 | + }  | 
                                                        |
| 288 | + }  | 
                                                        |
| 289 | + }  | 
                                                        |
| 290 | + return array($entete, $chaine);  | 
                                                        |
| 291 | 291 | }  | 
                                                        
| 292 | 292 | |
| 293 | 293 | /**  | 
                                                        
@@ -301,18 +301,18 @@ discard block  | 
                                                    ||
| 301 | 301 | */  | 
                                                        
| 302 | 302 |  function url_to_ascii($url_idn) { | 
                                                        
| 303 | 303 | |
| 304 | -	if ($parts = parse_url($url_idn)) { | 
                                                        |
| 305 | - $host = $parts['host'];  | 
                                                        |
| 306 | -		if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { | 
                                                        |
| 307 | -			include_spip('inc/idna_convert.class'); | 
                                                        |
| 308 | - $IDN = new idna_convert();  | 
                                                        |
| 309 | - $host_ascii = $IDN->encode($host);  | 
                                                        |
| 310 | - $url_idn = explode($host, $url_idn, 2);  | 
                                                        |
| 311 | - $url_idn = implode($host_ascii, $url_idn);  | 
                                                        |
| 312 | - }  | 
                                                        |
| 313 | - }  | 
                                                        |
| 314 | -  | 
                                                        |
| 315 | - return $url_idn;  | 
                                                        |
| 304 | +    if ($parts = parse_url($url_idn)) { | 
                                                        |
| 305 | + $host = $parts['host'];  | 
                                                        |
| 306 | +        if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { | 
                                                        |
| 307 | +            include_spip('inc/idna_convert.class'); | 
                                                        |
| 308 | + $IDN = new idna_convert();  | 
                                                        |
| 309 | + $host_ascii = $IDN->encode($host);  | 
                                                        |
| 310 | + $url_idn = explode($host, $url_idn, 2);  | 
                                                        |
| 311 | + $url_idn = implode($host_ascii, $url_idn);  | 
                                                        |
| 312 | + }  | 
                                                        |
| 313 | + }  | 
                                                        |
| 314 | +  | 
                                                        |
| 315 | + return $url_idn;  | 
                                                        |
| 316 | 316 | }  | 
                                                        
| 317 | 317 | |
| 318 | 318 | //  | 
                                                        
@@ -335,39 +335,39 @@ discard block  | 
                                                    ||
| 335 | 335 | function recuperer_page($url, $trans = false, $get_headers = false,  | 
                                                        
| 336 | 336 | $taille_max = null, $datas = '', $boundary = '', $refuser_gz = false,  | 
                                                        
| 337 | 337 |                          $date_verif = '', $uri_referer = ''){ | 
                                                        
| 338 | - $gz = false;  | 
                                                        |
| 339 | -  | 
                                                        |
| 340 | - // $copy = copier le fichier ?  | 
                                                        |
| 341 | - $copy = (is_string($trans) AND strlen($trans)>5); // eviter "false" :-)  | 
                                                        |
| 342 | -  | 
                                                        |
| 343 | - if (is_null($taille_max))  | 
                                                        |
| 344 | - $taille_max = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;  | 
                                                        |
| 345 | -  | 
                                                        |
| 346 | - // Accepter les URLs au format feed:// ou qui ont oublie le http://  | 
                                                        |
| 347 | -	$url = preg_replace(',^feed://,i', 'http://', $url); | 
                                                        |
| 348 | -	if (!preg_match(',^[a-z]+://,i', $url)) $url = 'http://' . $url; | 
                                                        |
| 349 | - $url = url_to_ascii($url);  | 
                                                        |
| 350 | -  | 
                                                        |
| 351 | - if ($taille_max==0)  | 
                                                        |
| 352 | - $get = 'HEAD';  | 
                                                        |
| 353 | - else  | 
                                                        |
| 354 | - $get = 'GET';  | 
                                                        |
| 355 | -  | 
                                                        |
| 356 | -	if (!empty($datas)) { | 
                                                        |
| 357 | - $get = 'POST';  | 
                                                        |
| 358 | - list($type, $postdata) = prepare_donnees_post($datas, $boundary);  | 
                                                        |
| 359 | - $datas = $type . 'Content-Length: ' . strlen($postdata) . "\r\n\r\n" . $postdata;  | 
                                                        |
| 360 | - }  | 
                                                        |
| 361 | -  | 
                                                        |
| 362 | - // dix tentatives maximum en cas d'entetes 301...  | 
                                                        |
| 363 | -	for ($i = 0; $i<10; $i++){ | 
                                                        |
| 364 | - $url = recuperer_lapage($url, $trans, $get, $taille_max, $datas, $refuser_gz, $date_verif, $uri_referer);  | 
                                                        |
| 365 | - if (!$url) return false;  | 
                                                        |
| 366 | -		if (is_array($url)){ | 
                                                        |
| 367 | - list($headers, $result) = $url;  | 
                                                        |
| 368 | - return ($get_headers ? $headers . "\n" : '') . $result;  | 
                                                        |
| 369 | -		} else spip_log("recuperer page recommence sur $url"); | 
                                                        |
| 370 | - }  | 
                                                        |
| 338 | + $gz = false;  | 
                                                        |
| 339 | +  | 
                                                        |
| 340 | + // $copy = copier le fichier ?  | 
                                                        |
| 341 | + $copy = (is_string($trans) AND strlen($trans)>5); // eviter "false" :-)  | 
                                                        |
| 342 | +  | 
                                                        |
| 343 | + if (is_null($taille_max))  | 
                                                        |
| 344 | + $taille_max = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;  | 
                                                        |
| 345 | +  | 
                                                        |
| 346 | + // Accepter les URLs au format feed:// ou qui ont oublie le http://  | 
                                                        |
| 347 | +    $url = preg_replace(',^feed://,i', 'http://', $url); | 
                                                        |
| 348 | +    if (!preg_match(',^[a-z]+://,i', $url)) $url = 'http://' . $url; | 
                                                        |
| 349 | + $url = url_to_ascii($url);  | 
                                                        |
| 350 | +  | 
                                                        |
| 351 | + if ($taille_max==0)  | 
                                                        |
| 352 | + $get = 'HEAD';  | 
                                                        |
| 353 | + else  | 
                                                        |
| 354 | + $get = 'GET';  | 
                                                        |
| 355 | +  | 
                                                        |
| 356 | +    if (!empty($datas)) { | 
                                                        |
| 357 | + $get = 'POST';  | 
                                                        |
| 358 | + list($type, $postdata) = prepare_donnees_post($datas, $boundary);  | 
                                                        |
| 359 | + $datas = $type . 'Content-Length: ' . strlen($postdata) . "\r\n\r\n" . $postdata;  | 
                                                        |
| 360 | + }  | 
                                                        |
| 361 | +  | 
                                                        |
| 362 | + // dix tentatives maximum en cas d'entetes 301...  | 
                                                        |
| 363 | +    for ($i = 0; $i<10; $i++){ | 
                                                        |
| 364 | + $url = recuperer_lapage($url, $trans, $get, $taille_max, $datas, $refuser_gz, $date_verif, $uri_referer);  | 
                                                        |
| 365 | + if (!$url) return false;  | 
                                                        |
| 366 | +        if (is_array($url)){ | 
                                                        |
| 367 | + list($headers, $result) = $url;  | 
                                                        |
| 368 | + return ($get_headers ? $headers . "\n" : '') . $result;  | 
                                                        |
| 369 | +        } else spip_log("recuperer page recommence sur $url"); | 
                                                        |
| 370 | + }  | 
                                                        |
| 371 | 371 | }  | 
                                                        
| 372 | 372 | |
| 373 | 373 | // args comme ci-dessus (presque)  | 
                                                        
@@ -376,118 +376,118 @@ discard block  | 
                                                    ||
| 376 | 376 | // si $trans est une chaine, c'est un nom de fichier pour ecrire directement dedans  | 
                                                        
| 377 | 377 | // http://doc.spip.org/@recuperer_lapage  | 
                                                        
| 378 | 378 |  function recuperer_lapage($url, $trans = false, $get = 'GET', $taille_max = _INC_DISTANT_MAX_SIZE, $datas = '', $refuser_gz = false, $date_verif = '', $uri_referer = ''){ | 
                                                        
| 379 | - // $copy = copier le fichier ?  | 
                                                        |
| 380 | - $copy = (is_string($trans) AND strlen($trans)>5); // eviter "false" :-)  | 
                                                        |
| 381 | -  | 
                                                        |
| 382 | - // si on ecrit directement dans un fichier, pour ne pas manipuler  | 
                                                        |
| 383 | - // en memoire refuser gz  | 
                                                        |
| 384 | - if ($copy)  | 
                                                        |
| 385 | - $refuser_gz = true;  | 
                                                        |
| 386 | -  | 
                                                        |
| 387 | - // ouvrir la connexion et envoyer la requete et ses en-tetes  | 
                                                        |
| 388 | - list($f, $fopen) = init_http($get, $url, $refuser_gz, $uri_referer, $datas, _INC_DISTANT_VERSION_HTTP, $date_verif);  | 
                                                        |
| 389 | -	if (!$f){ | 
                                                        |
| 390 | -		spip_log("ECHEC init_http $url"); | 
                                                        |
| 391 | - return false;  | 
                                                        |
| 392 | - }  | 
                                                        |
| 393 | -  | 
                                                        |
| 394 | - $result = '';  | 
                                                        |
| 395 | - // Sauf en fopen, envoyer le flux d'entree  | 
                                                        |
| 396 | - // et recuperer les en-tetes de reponses  | 
                                                        |
| 397 | - if ($fopen)  | 
                                                        |
| 398 | - $headers = '';  | 
                                                        |
| 399 | -	else { | 
                                                        |
| 400 | - $headers = recuperer_entetes($f, $date_verif);  | 
                                                        |
| 401 | -		if (is_numeric($headers)){ | 
                                                        |
| 402 | - fclose($f);  | 
                                                        |
| 403 | - // Chinoisierie inexplicable pour contrer  | 
                                                        |
| 404 | - // les actions liberticides de l'empire du milieu  | 
                                                        |
| 405 | -			if ($headers){ | 
                                                        |
| 406 | -				spip_log("HTTP status $headers pour $url"); | 
                                                        |
| 407 | - return false;  | 
                                                        |
| 408 | - }  | 
                                                        |
| 409 | - $t = @parse_url($url);  | 
                                                        |
| 410 | - $host = $t['host'];  | 
                                                        |
| 411 | - if (!need_proxy($host)  | 
                                                        |
| 412 | -				AND $result = @file_get_contents($url)){ | 
                                                        |
| 413 | - // on peuple les headers de vide et on continue  | 
                                                        |
| 414 | -				$headers = array(''); | 
                                                        |
| 415 | - }  | 
                                                        |
| 416 | - else  | 
                                                        |
| 417 | - return false;  | 
                                                        |
| 418 | - }  | 
                                                        |
| 419 | -		if (!is_array($headers)){ // cas Location | 
                                                        |
| 420 | - fclose($f);  | 
                                                        |
| 421 | -			include_spip('inc/filtres'); | 
                                                        |
| 422 | - return suivre_lien($url, $headers);  | 
                                                        |
| 423 | - }  | 
                                                        |
| 424 | -		$headers = join('', $headers); | 
                                                        |
| 425 | - }  | 
                                                        |
| 426 | -  | 
                                                        |
| 427 | - if ($trans===NULL) return array($headers, '');  | 
                                                        |
| 428 | -  | 
                                                        |
| 429 | - // s'il faut deballer, le faire via un fichier temporaire  | 
                                                        |
| 430 | - // sinon la memoire explose pour les gros flux  | 
                                                        |
| 431 | -  | 
                                                        |
| 432 | -	$gz = preg_match(",\bContent-Encoding: .*gzip,is", $headers) ? | 
                                                        |
| 433 | - (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz') : '';  | 
                                                        |
| 379 | + // $copy = copier le fichier ?  | 
                                                        |
| 380 | + $copy = (is_string($trans) AND strlen($trans)>5); // eviter "false" :-)  | 
                                                        |
| 381 | +  | 
                                                        |
| 382 | + // si on ecrit directement dans un fichier, pour ne pas manipuler  | 
                                                        |
| 383 | + // en memoire refuser gz  | 
                                                        |
| 384 | + if ($copy)  | 
                                                        |
| 385 | + $refuser_gz = true;  | 
                                                        |
| 386 | +  | 
                                                        |
| 387 | + // ouvrir la connexion et envoyer la requete et ses en-tetes  | 
                                                        |
| 388 | + list($f, $fopen) = init_http($get, $url, $refuser_gz, $uri_referer, $datas, _INC_DISTANT_VERSION_HTTP, $date_verif);  | 
                                                        |
| 389 | +    if (!$f){ | 
                                                        |
| 390 | +        spip_log("ECHEC init_http $url"); | 
                                                        |
| 391 | + return false;  | 
                                                        |
| 392 | + }  | 
                                                        |
| 393 | +  | 
                                                        |
| 394 | + $result = '';  | 
                                                        |
| 395 | + // Sauf en fopen, envoyer le flux d'entree  | 
                                                        |
| 396 | + // et recuperer les en-tetes de reponses  | 
                                                        |
| 397 | + if ($fopen)  | 
                                                        |
| 398 | + $headers = '';  | 
                                                        |
| 399 | +    else { | 
                                                        |
| 400 | + $headers = recuperer_entetes($f, $date_verif);  | 
                                                        |
| 401 | +        if (is_numeric($headers)){ | 
                                                        |
| 402 | + fclose($f);  | 
                                                        |
| 403 | + // Chinoisierie inexplicable pour contrer  | 
                                                        |
| 404 | + // les actions liberticides de l'empire du milieu  | 
                                                        |
| 405 | +            if ($headers){ | 
                                                        |
| 406 | +                spip_log("HTTP status $headers pour $url"); | 
                                                        |
| 407 | + return false;  | 
                                                        |
| 408 | + }  | 
                                                        |
| 409 | + $t = @parse_url($url);  | 
                                                        |
| 410 | + $host = $t['host'];  | 
                                                        |
| 411 | + if (!need_proxy($host)  | 
                                                        |
| 412 | +                AND $result = @file_get_contents($url)){ | 
                                                        |
| 413 | + // on peuple les headers de vide et on continue  | 
                                                        |
| 414 | +                $headers = array(''); | 
                                                        |
| 415 | + }  | 
                                                        |
| 416 | + else  | 
                                                        |
| 417 | + return false;  | 
                                                        |
| 418 | + }  | 
                                                        |
| 419 | +        if (!is_array($headers)){ // cas Location | 
                                                        |
| 420 | + fclose($f);  | 
                                                        |
| 421 | +            include_spip('inc/filtres'); | 
                                                        |
| 422 | + return suivre_lien($url, $headers);  | 
                                                        |
| 423 | + }  | 
                                                        |
| 424 | +        $headers = join('', $headers); | 
                                                        |
| 425 | + }  | 
                                                        |
| 426 | +  | 
                                                        |
| 427 | + if ($trans===NULL) return array($headers, '');  | 
                                                        |
| 428 | +  | 
                                                        |
| 429 | + // s'il faut deballer, le faire via un fichier temporaire  | 
                                                        |
| 430 | + // sinon la memoire explose pour les gros flux  | 
                                                        |
| 431 | +  | 
                                                        |
| 432 | +    $gz = preg_match(",\bContent-Encoding: .*gzip,is", $headers) ? | 
                                                        |
| 433 | + (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz') : '';  | 
                                                        |
| 434 | 434 | |
| 435 | 435 |  #	spip_log("entete ($trans $copy $gz)\n$headers"); | 
                                                        
| 436 | -	if (!$result){ | 
                                                        |
| 437 | - $result = recuperer_body($f, $taille_max, $gz ? $gz : ($copy ? $trans : ''));  | 
                                                        |
| 438 | - fclose($f);  | 
                                                        |
| 439 | - }  | 
                                                        |
| 440 | - if (!$result)  | 
                                                        |
| 441 | - return array($headers, $result);  | 
                                                        |
| 442 | -  | 
                                                        |
| 443 | - // Decompresser au besoin  | 
                                                        |
| 444 | -	if ($gz){ | 
                                                        |
| 445 | -		$result = join('', gzfile($gz)); | 
                                                        |
| 446 | - supprimer_fichier($gz);  | 
                                                        |
| 447 | - }  | 
                                                        |
| 448 | - // Faut-il l'importer dans notre charset local ?  | 
                                                        |
| 449 | -	if ($trans===true){ | 
                                                        |
| 450 | -		include_spip('inc/charsets'); | 
                                                        |
| 451 | - $result = transcoder_page($result, $headers);  | 
                                                        |
| 452 | - }  | 
                                                        |
| 453 | -  | 
                                                        |
| 454 | - return array($headers, $result);  | 
                                                        |
| 436 | +    if (!$result){ | 
                                                        |
| 437 | + $result = recuperer_body($f, $taille_max, $gz ? $gz : ($copy ? $trans : ''));  | 
                                                        |
| 438 | + fclose($f);  | 
                                                        |
| 439 | + }  | 
                                                        |
| 440 | + if (!$result)  | 
                                                        |
| 441 | + return array($headers, $result);  | 
                                                        |
| 442 | +  | 
                                                        |
| 443 | + // Decompresser au besoin  | 
                                                        |
| 444 | +    if ($gz){ | 
                                                        |
| 445 | +        $result = join('', gzfile($gz)); | 
                                                        |
| 446 | + supprimer_fichier($gz);  | 
                                                        |
| 447 | + }  | 
                                                        |
| 448 | + // Faut-il l'importer dans notre charset local ?  | 
                                                        |
| 449 | +    if ($trans===true){ | 
                                                        |
| 450 | +        include_spip('inc/charsets'); | 
                                                        |
| 451 | + $result = transcoder_page($result, $headers);  | 
                                                        |
| 452 | + }  | 
                                                        |
| 453 | +  | 
                                                        |
| 454 | + return array($headers, $result);  | 
                                                        |
| 455 | 455 | }  | 
                                                        
| 456 | 456 | |
| 457 | 457 | // http://doc.spip.org/@recuperer_body  | 
                                                        
| 458 | 458 |  function recuperer_body($f, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = ''){ | 
                                                        
| 459 | - $taille = 0;  | 
                                                        |
| 460 | - $result = '';  | 
                                                        |
| 461 | - $fp = false;  | 
                                                        |
| 462 | -	if ($fichier){ | 
                                                        |
| 463 | -		include_spip("inc/acces"); | 
                                                        |
| 464 | - $tmpfile = "$fichier.".creer_uniqid().".tmp";  | 
                                                        |
| 465 | - $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX);  | 
                                                        |
| 466 | -		if (!$fp AND file_exists($fichier)){ | 
                                                        |
| 467 | - return filesize($fichier);  | 
                                                        |
| 468 | - }  | 
                                                        |
| 469 | - if (!$fp)  | 
                                                        |
| 470 | - return false;  | 
                                                        |
| 471 | - $result = 0; // on renvoie la taille du fichier  | 
                                                        |
| 472 | - }  | 
                                                        |
| 473 | -	while (!feof($f) AND $taille<$taille_max){ | 
                                                        |
| 474 | - $res = fread($f, 16384);  | 
                                                        |
| 475 | - $taille += strlen($res);  | 
                                                        |
| 476 | -		if ($fp){ | 
                                                        |
| 477 | - fwrite($fp, $res);  | 
                                                        |
| 478 | - $result = $taille;  | 
                                                        |
| 479 | - }  | 
                                                        |
| 480 | - else  | 
                                                        |
| 481 | - $result .= $res;  | 
                                                        |
| 482 | - }  | 
                                                        |
| 483 | -	if ($fp){ | 
                                                        |
| 484 | - spip_fclose_unlock($fp);  | 
                                                        |
| 485 | - spip_unlink($fichier);  | 
                                                        |
| 486 | - @rename($tmpfile, $fichier);  | 
                                                        |
| 487 | - if (!file_exists($fichier))  | 
                                                        |
| 488 | - return false;  | 
                                                        |
| 489 | - }  | 
                                                        |
| 490 | - return $result;  | 
                                                        |
| 459 | + $taille = 0;  | 
                                                        |
| 460 | + $result = '';  | 
                                                        |
| 461 | + $fp = false;  | 
                                                        |
| 462 | +    if ($fichier){ | 
                                                        |
| 463 | +        include_spip("inc/acces"); | 
                                                        |
| 464 | + $tmpfile = "$fichier.".creer_uniqid().".tmp";  | 
                                                        |
| 465 | + $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX);  | 
                                                        |
| 466 | +        if (!$fp AND file_exists($fichier)){ | 
                                                        |
| 467 | + return filesize($fichier);  | 
                                                        |
| 468 | + }  | 
                                                        |
| 469 | + if (!$fp)  | 
                                                        |
| 470 | + return false;  | 
                                                        |
| 471 | + $result = 0; // on renvoie la taille du fichier  | 
                                                        |
| 472 | + }  | 
                                                        |
| 473 | +    while (!feof($f) AND $taille<$taille_max){ | 
                                                        |
| 474 | + $res = fread($f, 16384);  | 
                                                        |
| 475 | + $taille += strlen($res);  | 
                                                        |
| 476 | +        if ($fp){ | 
                                                        |
| 477 | + fwrite($fp, $res);  | 
                                                        |
| 478 | + $result = $taille;  | 
                                                        |
| 479 | + }  | 
                                                        |
| 480 | + else  | 
                                                        |
| 481 | + $result .= $res;  | 
                                                        |
| 482 | + }  | 
                                                        |
| 483 | +    if ($fp){ | 
                                                        |
| 484 | + spip_fclose_unlock($fp);  | 
                                                        |
| 485 | + spip_unlink($fichier);  | 
                                                        |
| 486 | + @rename($tmpfile, $fichier);  | 
                                                        |
| 487 | + if (!file_exists($fichier))  | 
                                                        |
| 488 | + return false;  | 
                                                        |
| 489 | + }  | 
                                                        |
| 490 | + return $result;  | 
                                                        |
| 491 | 491 | }  | 
                                                        
| 492 | 492 | |
| 493 | 493 | // Lit les entetes de reponse HTTP sur la socket $f et retourne:  | 
                                                        
@@ -497,35 +497,35 @@ discard block  | 
                                                    ||
| 497 | 497 | |
| 498 | 498 | // http://doc.spip.org/@recuperer_entetes  | 
                                                        
| 499 | 499 |  function recuperer_entetes($f, $date_verif = ''){ | 
                                                        
| 500 | - $s = @trim(fgets($f, 16384));  | 
                                                        |
| 501 | -  | 
                                                        |
| 502 | -	if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)){ | 
                                                        |
| 503 | - return 0;  | 
                                                        |
| 504 | - }  | 
                                                        |
| 505 | - $status = intval($r[1]);  | 
                                                        |
| 506 | - $headers = array();  | 
                                                        |
| 507 | - $not_modif = $location = false;  | 
                                                        |
| 508 | -	while ($s = trim(fgets($f, 16384))){ | 
                                                        |
| 509 | - $headers[] = $s . "\n";  | 
                                                        |
| 510 | -		preg_match(',^([^:]*): *(.*)$,i', $s, $r); | 
                                                        |
| 511 | - list(, $d, $v) = $r;  | 
                                                        |
| 512 | -		if (strtolower(trim($d))=='location' AND $status>=300 AND $status<400){ | 
                                                        |
| 513 | - $location = $v;  | 
                                                        |
| 514 | - }  | 
                                                        |
| 515 | -		elseif ($date_verif AND ($d=='Last-Modified')) { | 
                                                        |
| 516 | -			if ($date_verif>=strtotime($v)){ | 
                                                        |
| 517 | - //Cas ou la page distante n'a pas bouge depuis  | 
                                                        |
| 518 | - //la derniere visite  | 
                                                        |
| 519 | - $not_modif = true;  | 
                                                        |
| 520 | - }  | 
                                                        |
| 521 | - }  | 
                                                        |
| 522 | - }  | 
                                                        |
| 523 | -  | 
                                                        |
| 524 | - if ($location)  | 
                                                        |
| 525 | - return $location;  | 
                                                        |
| 526 | - if ($status!=200 or $not_modif)  | 
                                                        |
| 527 | - return $status;  | 
                                                        |
| 528 | - return $headers;  | 
                                                        |
| 500 | + $s = @trim(fgets($f, 16384));  | 
                                                        |
| 501 | +  | 
                                                        |
| 502 | +    if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)){ | 
                                                        |
| 503 | + return 0;  | 
                                                        |
| 504 | + }  | 
                                                        |
| 505 | + $status = intval($r[1]);  | 
                                                        |
| 506 | + $headers = array();  | 
                                                        |
| 507 | + $not_modif = $location = false;  | 
                                                        |
| 508 | +    while ($s = trim(fgets($f, 16384))){ | 
                                                        |
| 509 | + $headers[] = $s . "\n";  | 
                                                        |
| 510 | +        preg_match(',^([^:]*): *(.*)$,i', $s, $r); | 
                                                        |
| 511 | + list(, $d, $v) = $r;  | 
                                                        |
| 512 | +        if (strtolower(trim($d))=='location' AND $status>=300 AND $status<400){ | 
                                                        |
| 513 | + $location = $v;  | 
                                                        |
| 514 | + }  | 
                                                        |
| 515 | +        elseif ($date_verif AND ($d=='Last-Modified')) { | 
                                                        |
| 516 | +            if ($date_verif>=strtotime($v)){ | 
                                                        |
| 517 | + //Cas ou la page distante n'a pas bouge depuis  | 
                                                        |
| 518 | + //la derniere visite  | 
                                                        |
| 519 | + $not_modif = true;  | 
                                                        |
| 520 | + }  | 
                                                        |
| 521 | + }  | 
                                                        |
| 522 | + }  | 
                                                        |
| 523 | +  | 
                                                        |
| 524 | + if ($location)  | 
                                                        |
| 525 | + return $location;  | 
                                                        |
| 526 | + if ($status!=200 or $not_modif)  | 
                                                        |
| 527 | + return $status;  | 
                                                        |
| 528 | + return $headers;  | 
                                                        |
| 529 | 529 | }  | 
                                                        
| 530 | 530 | |
| 531 | 531 | // Si on doit conserver une copie locale des fichiers distants, autant que ca  | 
                                                        
@@ -534,21 +534,21 @@ discard block  | 
                                                    ||
| 534 | 534 | // des filesystems  | 
                                                        
| 535 | 535 | // http://doc.spip.org/@nom_fichier_copie_locale  | 
                                                        
| 536 | 536 |  function nom_fichier_copie_locale($source, $extension){ | 
                                                        
| 537 | -	include_spip('inc/documents'); | 
                                                        |
| 537 | +    include_spip('inc/documents'); | 
                                                        |
| 538 | 538 | |
| 539 | -	$d = creer_repertoire_documents('distant'); # IMG/distant/ | 
                                                        |
| 540 | - $d = sous_repertoire($d, $extension); # IMG/distant/pdf/  | 
                                                        |
| 539 | +    $d = creer_repertoire_documents('distant'); # IMG/distant/ | 
                                                        |
| 540 | + $d = sous_repertoire($d, $extension); # IMG/distant/pdf/  | 
                                                        |
| 541 | 541 | |
| 542 | - // on se place tout le temps comme si on etait a la racine  | 
                                                        |
| 543 | - if (_DIR_RACINE)  | 
                                                        |
| 544 | -		$d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); | 
                                                        |
| 542 | + // on se place tout le temps comme si on etait a la racine  | 
                                                        |
| 543 | + if (_DIR_RACINE)  | 
                                                        |
| 544 | +        $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); | 
                                                        |
| 545 | 545 | |
| 546 | - $m = md5($source);  | 
                                                        |
| 546 | + $m = md5($source);  | 
                                                        |
| 547 | 547 | |
| 548 | - return $d  | 
                                                        |
| 549 | -		. substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) | 
                                                        |
| 550 | - . substr($m, 0, 4)  | 
                                                        |
| 551 | - . ".$extension";  | 
                                                        |
| 548 | + return $d  | 
                                                        |
| 549 | +        . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) | 
                                                        |
| 550 | + . substr($m, 0, 4)  | 
                                                        |
| 551 | + . ".$extension";  | 
                                                        |
| 552 | 552 | }  | 
                                                        
| 553 | 553 | |
| 554 | 554 | //  | 
                                                        
@@ -556,61 +556,61 @@ discard block  | 
                                                    ||
| 556 | 556 | //  | 
                                                        
| 557 | 557 | // http://doc.spip.org/@fichier_copie_locale  | 
                                                        
| 558 | 558 |  function fichier_copie_locale($source){ | 
                                                        
| 559 | - // Si c'est deja local pas de souci  | 
                                                        |
| 560 | -	if (!preg_match(',^\w+://,', $source)){ | 
                                                        |
| 561 | - if (_DIR_RACINE)  | 
                                                        |
| 562 | -			$source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); | 
                                                        |
| 563 | - return $source;  | 
                                                        |
| 564 | - }  | 
                                                        |
| 565 | -  | 
                                                        |
| 566 | - // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier  | 
                                                        |
| 567 | - // a deja ete copie en local avec cette extension  | 
                                                        |
| 568 | - // dans ce cas elle est fiable, pas la peine de requeter en base  | 
                                                        |
| 569 | - $path_parts = pathinfo($source);  | 
                                                        |
| 570 | - $ext = $path_parts ? $path_parts['extension'] : '';  | 
                                                        |
| 571 | - if ($ext  | 
                                                        |
| 572 | -	AND preg_match(',^\w+$,', $ext) // pas de php?truc=1&... | 
                                                        |
| 573 | - AND $f = nom_fichier_copie_locale($source, $ext)  | 
                                                        |
| 574 | - AND file_exists(_DIR_RACINE . $f)  | 
                                                        |
| 575 | - )  | 
                                                        |
| 576 | - return $f;  | 
                                                        |
| 577 | -  | 
                                                        |
| 578 | -  | 
                                                        |
| 579 | - // Si c'est deja dans la table des documents,  | 
                                                        |
| 580 | - // ramener le nom de sa copie potentielle  | 
                                                        |
| 581 | -  | 
                                                        |
| 582 | -	$ext = sql_getfetsel("extension", "spip_documents", "fichier=" . sql_quote($source) . " AND distant='oui' AND extension <> ''"); | 
                                                        |
| 583 | -  | 
                                                        |
| 584 | -  | 
                                                        |
| 585 | - if ($ext) return nom_fichier_copie_locale($source, $ext);  | 
                                                        |
| 586 | -  | 
                                                        |
| 587 | - // voir si l'extension indiquee dans le nom du fichier est ok  | 
                                                        |
| 588 | - // et si il n'aurait pas deja ete rapatrie  | 
                                                        |
| 589 | -  | 
                                                        |
| 590 | - $ext = $path_parts ? $path_parts['extension'] : '';  | 
                                                        |
| 591 | -  | 
                                                        |
| 592 | -	if ($ext AND sql_getfetsel("extension", "spip_types_documents", "extension=" . sql_quote($ext))){ | 
                                                        |
| 593 | - $f = nom_fichier_copie_locale($source, $ext);  | 
                                                        |
| 594 | - if (file_exists(_DIR_RACINE . $f))  | 
                                                        |
| 595 | - return $f;  | 
                                                        |
| 596 | - }  | 
                                                        |
| 597 | -  | 
                                                        |
| 598 | - // Ping pour voir si son extension est connue et autorisee  | 
                                                        |
| 599 | - // avec mise en cache du resultat du ping  | 
                                                        |
| 600 | -  | 
                                                        |
| 601 | - $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source);  | 
                                                        |
| 602 | - if (!@file_exists($cache)  | 
                                                        |
| 603 | - OR !$path_parts = @unserialize(spip_file_get_contents($cache))  | 
                                                        |
| 604 | -		OR _request('var_mode')=='recalcul' | 
                                                        |
| 605 | -	){ | 
                                                        |
| 606 | - $path_parts = recuperer_infos_distantes($source, 0, false);  | 
                                                        |
| 607 | - ecrire_fichier($cache, serialize($path_parts));  | 
                                                        |
| 608 | - }  | 
                                                        |
| 609 | - $ext = $path_parts ? $path_parts['extension'] : '';  | 
                                                        |
| 610 | -	if ($ext AND sql_getfetsel("extension", "spip_types_documents", "extension=" . sql_quote($ext))){ | 
                                                        |
| 611 | - return nom_fichier_copie_locale($source, $ext);  | 
                                                        |
| 612 | - }  | 
                                                        |
| 613 | -	spip_log("pas de copie locale pour $source"); | 
                                                        |
| 559 | + // Si c'est deja local pas de souci  | 
                                                        |
| 560 | +    if (!preg_match(',^\w+://,', $source)){ | 
                                                        |
| 561 | + if (_DIR_RACINE)  | 
                                                        |
| 562 | +            $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); | 
                                                        |
| 563 | + return $source;  | 
                                                        |
| 564 | + }  | 
                                                        |
| 565 | +  | 
                                                        |
| 566 | + // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier  | 
                                                        |
| 567 | + // a deja ete copie en local avec cette extension  | 
                                                        |
| 568 | + // dans ce cas elle est fiable, pas la peine de requeter en base  | 
                                                        |
| 569 | + $path_parts = pathinfo($source);  | 
                                                        |
| 570 | + $ext = $path_parts ? $path_parts['extension'] : '';  | 
                                                        |
| 571 | + if ($ext  | 
                                                        |
| 572 | +    AND preg_match(',^\w+$,', $ext) // pas de php?truc=1&... | 
                                                        |
| 573 | + AND $f = nom_fichier_copie_locale($source, $ext)  | 
                                                        |
| 574 | + AND file_exists(_DIR_RACINE . $f)  | 
                                                        |
| 575 | + )  | 
                                                        |
| 576 | + return $f;  | 
                                                        |
| 577 | +  | 
                                                        |
| 578 | +  | 
                                                        |
| 579 | + // Si c'est deja dans la table des documents,  | 
                                                        |
| 580 | + // ramener le nom de sa copie potentielle  | 
                                                        |
| 581 | +  | 
                                                        |
| 582 | +    $ext = sql_getfetsel("extension", "spip_documents", "fichier=" . sql_quote($source) . " AND distant='oui' AND extension <> ''"); | 
                                                        |
| 583 | +  | 
                                                        |
| 584 | +  | 
                                                        |
| 585 | + if ($ext) return nom_fichier_copie_locale($source, $ext);  | 
                                                        |
| 586 | +  | 
                                                        |
| 587 | + // voir si l'extension indiquee dans le nom du fichier est ok  | 
                                                        |
| 588 | + // et si il n'aurait pas deja ete rapatrie  | 
                                                        |
| 589 | +  | 
                                                        |
| 590 | + $ext = $path_parts ? $path_parts['extension'] : '';  | 
                                                        |
| 591 | +  | 
                                                        |
| 592 | +    if ($ext AND sql_getfetsel("extension", "spip_types_documents", "extension=" . sql_quote($ext))){ | 
                                                        |
| 593 | + $f = nom_fichier_copie_locale($source, $ext);  | 
                                                        |
| 594 | + if (file_exists(_DIR_RACINE . $f))  | 
                                                        |
| 595 | + return $f;  | 
                                                        |
| 596 | + }  | 
                                                        |
| 597 | +  | 
                                                        |
| 598 | + // Ping pour voir si son extension est connue et autorisee  | 
                                                        |
| 599 | + // avec mise en cache du resultat du ping  | 
                                                        |
| 600 | +  | 
                                                        |
| 601 | + $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source);  | 
                                                        |
| 602 | + if (!@file_exists($cache)  | 
                                                        |
| 603 | + OR !$path_parts = @unserialize(spip_file_get_contents($cache))  | 
                                                        |
| 604 | +        OR _request('var_mode')=='recalcul' | 
                                                        |
| 605 | +    ){ | 
                                                        |
| 606 | + $path_parts = recuperer_infos_distantes($source, 0, false);  | 
                                                        |
| 607 | + ecrire_fichier($cache, serialize($path_parts));  | 
                                                        |
| 608 | + }  | 
                                                        |
| 609 | + $ext = $path_parts ? $path_parts['extension'] : '';  | 
                                                        |
| 610 | +    if ($ext AND sql_getfetsel("extension", "spip_types_documents", "extension=" . sql_quote($ext))){ | 
                                                        |
| 611 | + return nom_fichier_copie_locale($source, $ext);  | 
                                                        |
| 612 | + }  | 
                                                        |
| 613 | +    spip_log("pas de copie locale pour $source"); | 
                                                        |
| 614 | 614 | }  | 
                                                        
| 615 | 615 | |
| 616 | 616 | |
@@ -628,133 +628,133 @@ discard block  | 
                                                    ||
| 628 | 628 | // http://doc.spip.org/@recuperer_infos_distantes  | 
                                                        
| 629 | 629 |  function recuperer_infos_distantes($source, $max = 0, $charger_si_petite_image = true){ | 
                                                        
| 630 | 630 | |
| 631 | - // pas la peine de perdre son temps  | 
                                                        |
| 632 | -	if (!tester_url_absolue($source)) { | 
                                                        |
| 633 | - return false;  | 
                                                        |
| 634 | - }  | 
                                                        |
| 631 | + // pas la peine de perdre son temps  | 
                                                        |
| 632 | +    if (!tester_url_absolue($source)) { | 
                                                        |
| 633 | + return false;  | 
                                                        |
| 634 | + }  | 
                                                        |
| 635 | 635 | |
| 636 | - # charger les alias des types mime  | 
                                                        |
| 637 | -	include_spip('base/typedoc'); | 
                                                        |
| 638 | - global $mime_alias;  | 
                                                        |
| 639 | -  | 
                                                        |
| 640 | - $a = array();  | 
                                                        |
| 641 | - $mime_type = '';  | 
                                                        |
| 642 | - // On va directement charger le debut des images et des fichiers html,  | 
                                                        |
| 643 | - // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si  | 
                                                        |
| 644 | - // ca echoue l'utilisateur devra les entrer...  | 
                                                        |
| 645 | -	if ($headers = recuperer_page($source, false, true, $max, '', '', true)){ | 
                                                        |
| 646 | -		list($headers, $a['body']) = preg_split(',\n\n,', $headers, 2); | 
                                                        |
| 647 | -  | 
                                                        |
| 648 | -		if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) | 
                                                        |
| 649 | - $mime_type = (trim($regs[1]));  | 
                                                        |
| 650 | - else  | 
                                                        |
| 651 | - $mime_type = ''; // inconnu  | 
                                                        |
| 652 | -  | 
                                                        |
| 653 | - // Appliquer les alias  | 
                                                        |
| 654 | - while (isset($mime_alias[$mime_type]))  | 
                                                        |
| 655 | - $mime_type = $mime_alias[$mime_type];  | 
                                                        |
| 656 | -  | 
                                                        |
| 657 | - // Si on a un mime-type insignifiant  | 
                                                        |
| 658 | - // text/plain,application/octet-stream ou vide  | 
                                                        |
| 659 | - // c'est peut-etre que le serveur ne sait pas  | 
                                                        |
| 660 | - // ce qu'il sert ; on va tenter de detecter via l'extension de l'url  | 
                                                        |
| 661 | - // ou le Content-Disposition: attachment; filename=...  | 
                                                        |
| 662 | - $t = null;  | 
                                                        |
| 663 | -		if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))){ | 
                                                        |
| 664 | - if (!$t  | 
                                                        |
| 665 | -				AND preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) | 
                                                        |
| 666 | -			){ | 
                                                        |
| 667 | -				$t = sql_fetsel("extension", "spip_types_documents", "extension=" . sql_quote($rext[1],'','text')); | 
                                                        |
| 668 | - }  | 
                                                        |
| 669 | - if (!$t  | 
                                                        |
| 670 | -				AND preg_match(",^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims", $headers, $m) | 
                                                        |
| 671 | -				AND preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) | 
                                                        |
| 672 | -			){ | 
                                                        |
| 673 | -				$t = sql_fetsel("extension", "spip_types_documents", "extension=" . sql_quote($rext[1],'','text')); | 
                                                        |
| 674 | - }  | 
                                                        |
| 675 | - }  | 
                                                        |
| 676 | -  | 
                                                        |
| 677 | - // Autre mime/type (ou text/plain avec fichier d'extension inconnue)  | 
                                                        |
| 678 | - if (!$t)  | 
                                                        |
| 679 | -			$t = sql_fetsel("extension", "spip_types_documents", "mime_type=" . sql_quote($mime_type)); | 
                                                        |
| 680 | -  | 
                                                        |
| 681 | - // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)  | 
                                                        |
| 682 | - // On essaie de nouveau avec l'extension  | 
                                                        |
| 683 | - if (!$t  | 
                                                        |
| 684 | - AND $mime_type!='text/plain'  | 
                                                        |
| 685 | -			AND preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) | 
                                                        |
| 686 | -		){ | 
                                                        |
| 687 | -			$t = sql_fetsel("extension", "spip_types_documents", "extension=" . sql_quote($rext[1],'','text')); # eviter xxx.3 => 3gp (> SPIP 3) | 
                                                        |
| 688 | - }  | 
                                                        |
| 689 | -  | 
                                                        |
| 690 | -  | 
                                                        |
| 691 | -		if ($t){ | 
                                                        |
| 692 | -			spip_log("mime-type $mime_type ok, extension " . $t['extension']); | 
                                                        |
| 693 | - $a['extension'] = $t['extension'];  | 
                                                        |
| 694 | -		} else { | 
                                                        |
| 695 | - # par defaut on retombe sur '.bin' si c'est autorise  | 
                                                        |
| 696 | -			spip_log("mime-type $mime_type inconnu"); | 
                                                        |
| 697 | -			$t = sql_fetsel("extension", "spip_types_documents", "extension='bin'"); | 
                                                        |
| 698 | - if (!$t) return false;  | 
                                                        |
| 699 | - $a['extension'] = $t['extension'];  | 
                                                        |
| 700 | - }  | 
                                                        |
| 701 | -  | 
                                                        |
| 702 | -		if (preg_match(",\nContent-Length: *([^[:space:]]*),i", | 
                                                        |
| 703 | - "\n$headers", $regs)  | 
                                                        |
| 704 | - )  | 
                                                        |
| 705 | - $a['taille'] = intval($regs[1]);  | 
                                                        |
| 706 | - }  | 
                                                        |
| 707 | -  | 
                                                        |
| 708 | - // Echec avec HEAD, on tente avec GET  | 
                                                        |
| 709 | -	if (!$a AND !$max){ | 
                                                        |
| 710 | -		spip_log("tenter GET $source"); | 
                                                        |
| 711 | - $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);  | 
                                                        |
| 712 | - }  | 
                                                        |
| 713 | -  | 
                                                        |
| 714 | - // si on a rien trouve pas la peine d'insister  | 
                                                        |
| 715 | -	if (!$a) { | 
                                                        |
| 716 | - return false;  | 
                                                        |
| 717 | - }  | 
                                                        |
| 718 | -  | 
                                                        |
| 719 | - // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller  | 
                                                        |
| 720 | - // recharger le document en GET et recuperer des donnees supplementaires...  | 
                                                        |
| 721 | -	if (preg_match(',^image/(jpeg|gif|png|swf),', $mime_type)){ | 
                                                        |
| 722 | - if ($max==0  | 
                                                        |
| 723 | - AND $a['taille']<_INC_DISTANT_MAX_SIZE  | 
                                                        |
| 724 | - AND (strpos($GLOBALS['meta']['formats_graphiques'], $a['extension'])!==false)  | 
                                                        |
| 725 | - AND $charger_si_petite_image  | 
                                                        |
| 726 | -		){ | 
                                                        |
| 727 | - $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);  | 
                                                        |
| 728 | - }  | 
                                                        |
| 729 | -		else if ($a['body']){ | 
                                                        |
| 730 | - $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $a['extension']);  | 
                                                        |
| 731 | - ecrire_fichier($a['fichier'], $a['body']);  | 
                                                        |
| 732 | - $size_image = @getimagesize($a['fichier']);  | 
                                                        |
| 733 | - $a['largeur'] = intval($size_image[0]);  | 
                                                        |
| 734 | - $a['hauteur'] = intval($size_image[1]);  | 
                                                        |
| 735 | - $a['type_image'] = true;  | 
                                                        |
| 736 | - }  | 
                                                        |
| 737 | - }  | 
                                                        |
| 738 | -  | 
                                                        |
| 739 | - // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut  | 
                                                        |
| 740 | - // ce sera mieux que 0x0  | 
                                                        |
| 741 | - if ($a AND $a['extension']=='swf'  | 
                                                        |
| 742 | - AND !$a['largeur']  | 
                                                        |
| 743 | -	){ | 
                                                        |
| 744 | - $a['largeur'] = 425;  | 
                                                        |
| 745 | - $a['hauteur'] = 350;  | 
                                                        |
| 746 | - }  | 
                                                        |
| 747 | -  | 
                                                        |
| 748 | -	if ($mime_type=='text/html'){ | 
                                                        |
| 749 | -		include_spip('inc/filtres'); | 
                                                        |
| 750 | - $page = recuperer_page($source, true, false, _INC_DISTANT_MAX_SIZE);  | 
                                                        |
| 751 | -		if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) | 
                                                        |
| 752 | - $a['titre'] = corriger_caracteres(trim($regs[1]));  | 
                                                        |
| 753 | - if (!$a['taille']) $a['taille'] = strlen($page); # a peu pres  | 
                                                        |
| 754 | - }  | 
                                                        |
| 755 | - $a['mime_type']=$mime_type;  | 
                                                        |
| 756 | -  | 
                                                        |
| 757 | - return $a;  | 
                                                        |
| 636 | + # charger les alias des types mime  | 
                                                        |
| 637 | +    include_spip('base/typedoc'); | 
                                                        |
| 638 | + global $mime_alias;  | 
                                                        |
| 639 | +  | 
                                                        |
| 640 | + $a = array();  | 
                                                        |
| 641 | + $mime_type = '';  | 
                                                        |
| 642 | + // On va directement charger le debut des images et des fichiers html,  | 
                                                        |
| 643 | + // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si  | 
                                                        |
| 644 | + // ca echoue l'utilisateur devra les entrer...  | 
                                                        |
| 645 | +    if ($headers = recuperer_page($source, false, true, $max, '', '', true)){ | 
                                                        |
| 646 | +        list($headers, $a['body']) = preg_split(',\n\n,', $headers, 2); | 
                                                        |
| 647 | +  | 
                                                        |
| 648 | +        if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) | 
                                                        |
| 649 | + $mime_type = (trim($regs[1]));  | 
                                                        |
| 650 | + else  | 
                                                        |
| 651 | + $mime_type = ''; // inconnu  | 
                                                        |
| 652 | +  | 
                                                        |
| 653 | + // Appliquer les alias  | 
                                                        |
| 654 | + while (isset($mime_alias[$mime_type]))  | 
                                                        |
| 655 | + $mime_type = $mime_alias[$mime_type];  | 
                                                        |
| 656 | +  | 
                                                        |
| 657 | + // Si on a un mime-type insignifiant  | 
                                                        |
| 658 | + // text/plain,application/octet-stream ou vide  | 
                                                        |
| 659 | + // c'est peut-etre que le serveur ne sait pas  | 
                                                        |
| 660 | + // ce qu'il sert ; on va tenter de detecter via l'extension de l'url  | 
                                                        |
| 661 | + // ou le Content-Disposition: attachment; filename=...  | 
                                                        |
| 662 | + $t = null;  | 
                                                        |
| 663 | +        if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))){ | 
                                                        |
| 664 | + if (!$t  | 
                                                        |
| 665 | +                AND preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) | 
                                                        |
| 666 | +            ){ | 
                                                        |
| 667 | +                $t = sql_fetsel("extension", "spip_types_documents", "extension=" . sql_quote($rext[1],'','text')); | 
                                                        |
| 668 | + }  | 
                                                        |
| 669 | + if (!$t  | 
                                                        |
| 670 | +                AND preg_match(",^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims", $headers, $m) | 
                                                        |
| 671 | +                AND preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) | 
                                                        |
| 672 | +            ){ | 
                                                        |
| 673 | +                $t = sql_fetsel("extension", "spip_types_documents", "extension=" . sql_quote($rext[1],'','text')); | 
                                                        |
| 674 | + }  | 
                                                        |
| 675 | + }  | 
                                                        |
| 676 | +  | 
                                                        |
| 677 | + // Autre mime/type (ou text/plain avec fichier d'extension inconnue)  | 
                                                        |
| 678 | + if (!$t)  | 
                                                        |
| 679 | +            $t = sql_fetsel("extension", "spip_types_documents", "mime_type=" . sql_quote($mime_type)); | 
                                                        |
| 680 | +  | 
                                                        |
| 681 | + // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)  | 
                                                        |
| 682 | + // On essaie de nouveau avec l'extension  | 
                                                        |
| 683 | + if (!$t  | 
                                                        |
| 684 | + AND $mime_type!='text/plain'  | 
                                                        |
| 685 | +            AND preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) | 
                                                        |
| 686 | +        ){ | 
                                                        |
| 687 | +            $t = sql_fetsel("extension", "spip_types_documents", "extension=" . sql_quote($rext[1],'','text')); # eviter xxx.3 => 3gp (> SPIP 3) | 
                                                        |
| 688 | + }  | 
                                                        |
| 689 | +  | 
                                                        |
| 690 | +  | 
                                                        |
| 691 | +        if ($t){ | 
                                                        |
| 692 | +            spip_log("mime-type $mime_type ok, extension " . $t['extension']); | 
                                                        |
| 693 | + $a['extension'] = $t['extension'];  | 
                                                        |
| 694 | +        } else { | 
                                                        |
| 695 | + # par defaut on retombe sur '.bin' si c'est autorise  | 
                                                        |
| 696 | +            spip_log("mime-type $mime_type inconnu"); | 
                                                        |
| 697 | +            $t = sql_fetsel("extension", "spip_types_documents", "extension='bin'"); | 
                                                        |
| 698 | + if (!$t) return false;  | 
                                                        |
| 699 | + $a['extension'] = $t['extension'];  | 
                                                        |
| 700 | + }  | 
                                                        |
| 701 | +  | 
                                                        |
| 702 | +        if (preg_match(",\nContent-Length: *([^[:space:]]*),i", | 
                                                        |
| 703 | + "\n$headers", $regs)  | 
                                                        |
| 704 | + )  | 
                                                        |
| 705 | + $a['taille'] = intval($regs[1]);  | 
                                                        |
| 706 | + }  | 
                                                        |
| 707 | +  | 
                                                        |
| 708 | + // Echec avec HEAD, on tente avec GET  | 
                                                        |
| 709 | +    if (!$a AND !$max){ | 
                                                        |
| 710 | +        spip_log("tenter GET $source"); | 
                                                        |
| 711 | + $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);  | 
                                                        |
| 712 | + }  | 
                                                        |
| 713 | +  | 
                                                        |
| 714 | + // si on a rien trouve pas la peine d'insister  | 
                                                        |
| 715 | +    if (!$a) { | 
                                                        |
| 716 | + return false;  | 
                                                        |
| 717 | + }  | 
                                                        |
| 718 | +  | 
                                                        |
| 719 | + // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller  | 
                                                        |
| 720 | + // recharger le document en GET et recuperer des donnees supplementaires...  | 
                                                        |
| 721 | +    if (preg_match(',^image/(jpeg|gif|png|swf),', $mime_type)){ | 
                                                        |
| 722 | + if ($max==0  | 
                                                        |
| 723 | + AND $a['taille']<_INC_DISTANT_MAX_SIZE  | 
                                                        |
| 724 | + AND (strpos($GLOBALS['meta']['formats_graphiques'], $a['extension'])!==false)  | 
                                                        |
| 725 | + AND $charger_si_petite_image  | 
                                                        |
| 726 | +        ){ | 
                                                        |
| 727 | + $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);  | 
                                                        |
| 728 | + }  | 
                                                        |
| 729 | +        else if ($a['body']){ | 
                                                        |
| 730 | + $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $a['extension']);  | 
                                                        |
| 731 | + ecrire_fichier($a['fichier'], $a['body']);  | 
                                                        |
| 732 | + $size_image = @getimagesize($a['fichier']);  | 
                                                        |
| 733 | + $a['largeur'] = intval($size_image[0]);  | 
                                                        |
| 734 | + $a['hauteur'] = intval($size_image[1]);  | 
                                                        |
| 735 | + $a['type_image'] = true;  | 
                                                        |
| 736 | + }  | 
                                                        |
| 737 | + }  | 
                                                        |
| 738 | +  | 
                                                        |
| 739 | + // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut  | 
                                                        |
| 740 | + // ce sera mieux que 0x0  | 
                                                        |
| 741 | + if ($a AND $a['extension']=='swf'  | 
                                                        |
| 742 | + AND !$a['largeur']  | 
                                                        |
| 743 | +    ){ | 
                                                        |
| 744 | + $a['largeur'] = 425;  | 
                                                        |
| 745 | + $a['hauteur'] = 350;  | 
                                                        |
| 746 | + }  | 
                                                        |
| 747 | +  | 
                                                        |
| 748 | +    if ($mime_type=='text/html'){ | 
                                                        |
| 749 | +        include_spip('inc/filtres'); | 
                                                        |
| 750 | + $page = recuperer_page($source, true, false, _INC_DISTANT_MAX_SIZE);  | 
                                                        |
| 751 | +        if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) | 
                                                        |
| 752 | + $a['titre'] = corriger_caracteres(trim($regs[1]));  | 
                                                        |
| 753 | + if (!$a['taille']) $a['taille'] = strlen($page); # a peu pres  | 
                                                        |
| 754 | + }  | 
                                                        |
| 755 | + $a['mime_type']=$mime_type;  | 
                                                        |
| 756 | +  | 
                                                        |
| 757 | + return $a;  | 
                                                        |
| 758 | 758 | }  | 
                                                        
| 759 | 759 | |
| 760 | 760 | |
@@ -769,17 +769,17 @@ discard block  | 
                                                    ||
| 769 | 769 | * @return string  | 
                                                        
| 770 | 770 | */  | 
                                                        
| 771 | 771 |  function need_proxy($host, $http_proxy = null, $http_noproxy = null){ | 
                                                        
| 772 | - if (is_null($http_proxy))  | 
                                                        |
| 773 | - $http_proxy = @$GLOBALS['meta']["http_proxy"];  | 
                                                        |
| 774 | - if (is_null($http_noproxy))  | 
                                                        |
| 775 | - $http_noproxy = @$GLOBALS['meta']["http_noproxy"];  | 
                                                        |
| 772 | + if (is_null($http_proxy))  | 
                                                        |
| 773 | + $http_proxy = @$GLOBALS['meta']["http_proxy"];  | 
                                                        |
| 774 | + if (is_null($http_noproxy))  | 
                                                        |
| 775 | + $http_noproxy = @$GLOBALS['meta']["http_noproxy"];  | 
                                                        |
| 776 | 776 | |
| 777 | - $domain = substr($host, strpos($host, '.'));  | 
                                                        |
| 777 | + $domain = substr($host, strpos($host, '.'));  | 
                                                        |
| 778 | 778 | |
| 779 | - return ($http_proxy  | 
                                                        |
| 780 | -		AND (strpos(" $http_noproxy ", " $host ")===false | 
                                                        |
| 781 | -		AND (strpos(" $http_noproxy ", " $domain ")===false))) | 
                                                        |
| 782 | - ? $http_proxy : '';  | 
                                                        |
| 779 | + return ($http_proxy  | 
                                                        |
| 780 | +        AND (strpos(" $http_noproxy ", " $host ")===false | 
                                                        |
| 781 | +        AND (strpos(" $http_noproxy ", " $domain ")===false))) | 
                                                        |
| 782 | + ? $http_proxy : '';  | 
                                                        |
| 783 | 783 | }  | 
                                                        
| 784 | 784 | |
| 785 | 785 | //  | 
                                                        
@@ -788,124 +788,124 @@ discard block  | 
                                                    ||
| 788 | 788 | //  | 
                                                        
| 789 | 789 | // http://doc.spip.org/@init_http  | 
                                                        
| 790 | 790 |  function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = "", $vers = "HTTP/1.0", $date = ''){ | 
                                                        
| 791 | - $user = $via_proxy = $proxy_user = '';  | 
                                                        |
| 792 | - $fopen = false;  | 
                                                        |
| 793 | -  | 
                                                        |
| 794 | - $t = @parse_url($url);  | 
                                                        |
| 795 | - $host = $t['host'];  | 
                                                        |
| 796 | -	if ($t['scheme']=='http'){ | 
                                                        |
| 797 | - $scheme = 'http';  | 
                                                        |
| 798 | - $noproxy = '';  | 
                                                        |
| 799 | -	} elseif ($t['scheme']=='https') { | 
                                                        |
| 800 | - $scheme = 'ssl';  | 
                                                        |
| 801 | - $noproxy = 'ssl://';  | 
                                                        |
| 802 | - if (!isset($t['port']) || !($port = $t['port'])) $t['port'] = 443;  | 
                                                        |
| 803 | - }  | 
                                                        |
| 804 | -	else { | 
                                                        |
| 805 | - $scheme = $t['scheme'];  | 
                                                        |
| 806 | - $noproxy = $scheme . '://';  | 
                                                        |
| 807 | - }  | 
                                                        |
| 808 | - if (isset($t['user']))  | 
                                                        |
| 809 | - $user = array($t['user'], $t['pass']);  | 
                                                        |
| 810 | -  | 
                                                        |
| 811 | - if (!isset($t['port']) || !($port = $t['port'])) $port = 80;  | 
                                                        |
| 812 | - if (!isset($t['path']) || !($path = $t['path'])) $path = "/";  | 
                                                        |
| 813 | - if (@$t['query']) $path .= "?" . $t['query'];  | 
                                                        |
| 814 | -  | 
                                                        |
| 815 | - $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);  | 
                                                        |
| 816 | -	if (!$f){ | 
                                                        |
| 817 | - // fallback : fopen  | 
                                                        |
| 818 | - if (!need_proxy($host)  | 
                                                        |
| 819 | -		  AND !_request('tester_proxy') | 
                                                        |
| 820 | -		  AND (!isset($GLOBALS['inc_distant_allow_fopen']) OR $GLOBALS['inc_distant_allow_fopen'])){ | 
                                                        |
| 821 | - $f = @fopen($url, "rb");  | 
                                                        |
| 822 | -			spip_log("connexion vers $url par simple fopen"); | 
                                                        |
| 823 | - $fopen = true;  | 
                                                        |
| 824 | - }  | 
                                                        |
| 825 | - else  | 
                                                        |
| 826 | - $f = false;  | 
                                                        |
| 827 | - // echec total  | 
                                                        |
| 828 | - }  | 
                                                        |
| 829 | -  | 
                                                        |
| 830 | - return array($f, $fopen);  | 
                                                        |
| 791 | + $user = $via_proxy = $proxy_user = '';  | 
                                                        |
| 792 | + $fopen = false;  | 
                                                        |
| 793 | +  | 
                                                        |
| 794 | + $t = @parse_url($url);  | 
                                                        |
| 795 | + $host = $t['host'];  | 
                                                        |
| 796 | +    if ($t['scheme']=='http'){ | 
                                                        |
| 797 | + $scheme = 'http';  | 
                                                        |
| 798 | + $noproxy = '';  | 
                                                        |
| 799 | +    } elseif ($t['scheme']=='https') { | 
                                                        |
| 800 | + $scheme = 'ssl';  | 
                                                        |
| 801 | + $noproxy = 'ssl://';  | 
                                                        |
| 802 | + if (!isset($t['port']) || !($port = $t['port'])) $t['port'] = 443;  | 
                                                        |
| 803 | + }  | 
                                                        |
| 804 | +    else { | 
                                                        |
| 805 | + $scheme = $t['scheme'];  | 
                                                        |
| 806 | + $noproxy = $scheme . '://';  | 
                                                        |
| 807 | + }  | 
                                                        |
| 808 | + if (isset($t['user']))  | 
                                                        |
| 809 | + $user = array($t['user'], $t['pass']);  | 
                                                        |
| 810 | +  | 
                                                        |
| 811 | + if (!isset($t['port']) || !($port = $t['port'])) $port = 80;  | 
                                                        |
| 812 | + if (!isset($t['path']) || !($path = $t['path'])) $path = "/";  | 
                                                        |
| 813 | + if (@$t['query']) $path .= "?" . $t['query'];  | 
                                                        |
| 814 | +  | 
                                                        |
| 815 | + $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);  | 
                                                        |
| 816 | +    if (!$f){ | 
                                                        |
| 817 | + // fallback : fopen  | 
                                                        |
| 818 | + if (!need_proxy($host)  | 
                                                        |
| 819 | +          AND !_request('tester_proxy') | 
                                                        |
| 820 | +          AND (!isset($GLOBALS['inc_distant_allow_fopen']) OR $GLOBALS['inc_distant_allow_fopen'])){ | 
                                                        |
| 821 | + $f = @fopen($url, "rb");  | 
                                                        |
| 822 | +            spip_log("connexion vers $url par simple fopen"); | 
                                                        |
| 823 | + $fopen = true;  | 
                                                        |
| 824 | + }  | 
                                                        |
| 825 | + else  | 
                                                        |
| 826 | + $f = false;  | 
                                                        |
| 827 | + // echec total  | 
                                                        |
| 828 | + }  | 
                                                        |
| 829 | +  | 
                                                        |
| 830 | + return array($f, $fopen);  | 
                                                        |
| 831 | 831 | }  | 
                                                        
| 832 | 832 | |
| 833 | 833 | // http://doc.spip.org/@lance_requete  | 
                                                        
| 834 | 834 |  function lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz = false, $referer = '', $datas = "", $vers = "HTTP/1.0", $date = ''){ | 
                                                        
| 835 | 835 | |
| 836 | - $proxy_user = '';  | 
                                                        |
| 837 | - $http_proxy = need_proxy($host);  | 
                                                        |
| 838 | - if ($user) $user = urlencode($user[0]) . ":" . urlencode($user[1]);  | 
                                                        |
| 839 | -  | 
                                                        |
| 840 | - $connect = '';  | 
                                                        |
| 841 | -	if ($http_proxy) { | 
                                                        |
| 842 | -		if (defined('_PROXY_HTTPS_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) { | 
                                                        |
| 843 | - $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : '');  | 
                                                        |
| 844 | - $connect = 'CONNECT ' . $path_host . " $vers\r\n"  | 
                                                        |
| 845 | - . "Host: $path_host\r\n"  | 
                                                        |
| 846 | - . "Proxy-Connection: Keep-Alive\r\n";  | 
                                                        |
| 847 | -		} else { | 
                                                        |
| 848 | -			$path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://") | 
                                                        |
| 849 | - . (!$user ? '' : "$user@")  | 
                                                        |
| 850 | - . "$host" . (($port!=80) ? ":$port" : "") . $path;  | 
                                                        |
| 851 | - }  | 
                                                        |
| 852 | - $t2 = @parse_url($http_proxy);  | 
                                                        |
| 853 | - $first_host = $t2['host'];  | 
                                                        |
| 854 | - if (!($port = $t2['port'])) $port = 80;  | 
                                                        |
| 855 | - if ($t2['user'])  | 
                                                        |
| 856 | - $proxy_user = base64_encode($t2['user'] . ":" . $t2['pass']);  | 
                                                        |
| 857 | - }  | 
                                                        |
| 858 | - else  | 
                                                        |
| 859 | - $first_host = $noproxy . $host;  | 
                                                        |
| 860 | -  | 
                                                        |
| 861 | -	if ($connect){ | 
                                                        |
| 862 | -		$streamContext = stream_context_create(array('ssl' => array('verify_peer' => false, 'allow_self_signed' => true))); | 
                                                        |
| 863 | -		$f = @stream_socket_client("tcp://$first_host:$port", $nError, $sError, _INC_DISTANT_CONNECT_TIMEOUT, STREAM_CLIENT_CONNECT, $streamContext); | 
                                                        |
| 864 | -		spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)","connect"); | 
                                                        |
| 865 | - if (!$f) return false;  | 
                                                        |
| 866 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        |
| 867 | -  | 
                                                        |
| 868 | - fputs($f, $connect);  | 
                                                        |
| 869 | - fputs($f, "\r\n");  | 
                                                        |
| 870 | - $res = fread($f, 1024);  | 
                                                        |
| 871 | - if (!$res  | 
                                                        |
| 872 | -		  OR !count($res = explode(' ',$res)) | 
                                                        |
| 873 | -		  OR $res[1]!=='200'){ | 
                                                        |
| 874 | -			spip_log("Echec CONNECT sur $first_host:$port","connect"._LOG_INFO_IMPORTANTE); | 
                                                        |
| 875 | - fclose($f);  | 
                                                        |
| 876 | - return false;  | 
                                                        |
| 877 | - }  | 
                                                        |
| 878 | - // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo  | 
                                                        |
| 879 | - stream_set_blocking($f, true);  | 
                                                        |
| 880 | - // envoyer le handshake  | 
                                                        |
| 881 | - stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);  | 
                                                        |
| 882 | -		spip_log("OK CONNECT sur $first_host:$port","connect"); | 
                                                        |
| 883 | - }  | 
                                                        |
| 884 | -	else { | 
                                                        |
| 885 | - $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        |
| 886 | -		spip_log("Recuperer $path sur $first_host:$port par $f"); | 
                                                        |
| 887 | -		if (!$f) { | 
                                                        |
| 888 | -			spip_log("Erreur connexion $errno $errstr",_LOG_ERREUR); | 
                                                        |
| 889 | - return false;  | 
                                                        |
| 890 | - }  | 
                                                        |
| 891 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        |
| 892 | - }  | 
                                                        |
| 893 | -  | 
                                                        |
| 894 | - $site = $GLOBALS['meta']["adresse_site"];  | 
                                                        |
| 895 | -  | 
                                                        |
| 896 | - $req = "$method $path $vers\r\n"  | 
                                                        |
| 897 | - . "Host: $host\r\n"  | 
                                                        |
| 898 | - . "User-Agent: " . _INC_DISTANT_USER_AGENT . "\r\n"  | 
                                                        |
| 899 | -		. ($refuse_gz ? '' : ("Accept-Encoding: " . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) | 
                                                        |
| 900 | - . (!$site ? '' : "Referer: $site/$referer\r\n")  | 
                                                        |
| 901 | -		. (!$date ? '' : "If-Modified-Since: " . (gmdate("D, d M Y H:i:s", $date) . " GMT\r\n")) | 
                                                        |
| 902 | -		. (!$user ? '' : ("Authorization: Basic " . base64_encode($user) . "\r\n")) | 
                                                        |
| 903 | - . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n")  | 
                                                        |
| 904 | - . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n");  | 
                                                        |
| 836 | + $proxy_user = '';  | 
                                                        |
| 837 | + $http_proxy = need_proxy($host);  | 
                                                        |
| 838 | + if ($user) $user = urlencode($user[0]) . ":" . urlencode($user[1]);  | 
                                                        |
| 839 | +  | 
                                                        |
| 840 | + $connect = '';  | 
                                                        |
| 841 | +    if ($http_proxy) { | 
                                                        |
| 842 | +        if (defined('_PROXY_HTTPS_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) { | 
                                                        |
| 843 | + $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : '');  | 
                                                        |
| 844 | + $connect = 'CONNECT ' . $path_host . " $vers\r\n"  | 
                                                        |
| 845 | + . "Host: $path_host\r\n"  | 
                                                        |
| 846 | + . "Proxy-Connection: Keep-Alive\r\n";  | 
                                                        |
| 847 | +        } else { | 
                                                        |
| 848 | +            $path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://") | 
                                                        |
| 849 | + . (!$user ? '' : "$user@")  | 
                                                        |
| 850 | + . "$host" . (($port!=80) ? ":$port" : "") . $path;  | 
                                                        |
| 851 | + }  | 
                                                        |
| 852 | + $t2 = @parse_url($http_proxy);  | 
                                                        |
| 853 | + $first_host = $t2['host'];  | 
                                                        |
| 854 | + if (!($port = $t2['port'])) $port = 80;  | 
                                                        |
| 855 | + if ($t2['user'])  | 
                                                        |
| 856 | + $proxy_user = base64_encode($t2['user'] . ":" . $t2['pass']);  | 
                                                        |
| 857 | + }  | 
                                                        |
| 858 | + else  | 
                                                        |
| 859 | + $first_host = $noproxy . $host;  | 
                                                        |
| 860 | +  | 
                                                        |
| 861 | +    if ($connect){ | 
                                                        |
| 862 | +        $streamContext = stream_context_create(array('ssl' => array('verify_peer' => false, 'allow_self_signed' => true))); | 
                                                        |
| 863 | +        $f = @stream_socket_client("tcp://$first_host:$port", $nError, $sError, _INC_DISTANT_CONNECT_TIMEOUT, STREAM_CLIENT_CONNECT, $streamContext); | 
                                                        |
| 864 | +        spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)","connect"); | 
                                                        |
| 865 | + if (!$f) return false;  | 
                                                        |
| 866 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        |
| 867 | +  | 
                                                        |
| 868 | + fputs($f, $connect);  | 
                                                        |
| 869 | + fputs($f, "\r\n");  | 
                                                        |
| 870 | + $res = fread($f, 1024);  | 
                                                        |
| 871 | + if (!$res  | 
                                                        |
| 872 | +          OR !count($res = explode(' ',$res)) | 
                                                        |
| 873 | +          OR $res[1]!=='200'){ | 
                                                        |
| 874 | +            spip_log("Echec CONNECT sur $first_host:$port","connect"._LOG_INFO_IMPORTANTE); | 
                                                        |
| 875 | + fclose($f);  | 
                                                        |
| 876 | + return false;  | 
                                                        |
| 877 | + }  | 
                                                        |
| 878 | + // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo  | 
                                                        |
| 879 | + stream_set_blocking($f, true);  | 
                                                        |
| 880 | + // envoyer le handshake  | 
                                                        |
| 881 | + stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);  | 
                                                        |
| 882 | +        spip_log("OK CONNECT sur $first_host:$port","connect"); | 
                                                        |
| 883 | + }  | 
                                                        |
| 884 | +    else { | 
                                                        |
| 885 | + $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        |
| 886 | +        spip_log("Recuperer $path sur $first_host:$port par $f"); | 
                                                        |
| 887 | +        if (!$f) { | 
                                                        |
| 888 | +            spip_log("Erreur connexion $errno $errstr",_LOG_ERREUR); | 
                                                        |
| 889 | + return false;  | 
                                                        |
| 890 | + }  | 
                                                        |
| 891 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        |
| 892 | + }  | 
                                                        |
| 893 | +  | 
                                                        |
| 894 | + $site = $GLOBALS['meta']["adresse_site"];  | 
                                                        |
| 895 | +  | 
                                                        |
| 896 | + $req = "$method $path $vers\r\n"  | 
                                                        |
| 897 | + . "Host: $host\r\n"  | 
                                                        |
| 898 | + . "User-Agent: " . _INC_DISTANT_USER_AGENT . "\r\n"  | 
                                                        |
| 899 | +        . ($refuse_gz ? '' : ("Accept-Encoding: " . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) | 
                                                        |
| 900 | + . (!$site ? '' : "Referer: $site/$referer\r\n")  | 
                                                        |
| 901 | +        . (!$date ? '' : "If-Modified-Since: " . (gmdate("D, d M Y H:i:s", $date) . " GMT\r\n")) | 
                                                        |
| 902 | +        . (!$user ? '' : ("Authorization: Basic " . base64_encode($user) . "\r\n")) | 
                                                        |
| 903 | + . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n")  | 
                                                        |
| 904 | + . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n");  | 
                                                        |
| 905 | 905 | |
| 906 | 906 |  #	spip_log("Requete\n$req"); | 
                                                        
| 907 | - fputs($f, $req);  | 
                                                        |
| 908 | - fputs($f, $datas ? $datas : "\r\n");  | 
                                                        |
| 909 | - return $f;  | 
                                                        |
| 907 | + fputs($f, $req);  | 
                                                        |
| 908 | + fputs($f, $datas ? $datas : "\r\n");  | 
                                                        |
| 909 | + return $f;  | 
                                                        |
| 910 | 910 | }  | 
                                                        
| 911 | 911 | |
@@ -19,11 +19,11 @@ discard block  | 
                                                    ||
| 19 | 19 | |
| 20 | 20 |  if (!defined('_INC_DISTANT_VERSION_HTTP')) define('_INC_DISTANT_VERSION_HTTP', "HTTP/1.0"); | 
                                                        
| 21 | 21 |  if (!defined('_INC_DISTANT_CONTENT_ENCODING')) define('_INC_DISTANT_CONTENT_ENCODING', "gzip"); | 
                                                        
| 22 | -if (!defined('_INC_DISTANT_USER_AGENT')) define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . " (" . $GLOBALS['home_server'] . ")"); | 
                                                        |
| 23 | -if (!defined('_INC_DISTANT_MAX_SIZE')) define('_INC_DISTANT_MAX_SIZE',2097152); | 
                                                        |
| 24 | -if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) define('_INC_DISTANT_CONNECT_TIMEOUT',10); | 
                                                        |
| 22 | +if (!defined('_INC_DISTANT_USER_AGENT')) define('_INC_DISTANT_USER_AGENT', 'SPIP-'.$GLOBALS['spip_version_affichee']." (".$GLOBALS['home_server'].")"); | 
                                                        |
| 23 | +if (!defined('_INC_DISTANT_MAX_SIZE')) define('_INC_DISTANT_MAX_SIZE', 2097152); | 
                                                        |
| 24 | +if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) define('_INC_DISTANT_CONNECT_TIMEOUT', 10); | 
                                                        |
| 25 | 25 | |
| 26 | -define('_REGEXP_COPIE_LOCALE', ',' . | 
                                                        |
| 26 | +define('_REGEXP_COPIE_LOCALE', ','. | 
                                                        |
| 27 | 27 |         preg_replace('@^https?:@', 'https?:', $GLOBALS['meta']['adresse_site']) | 
                                                        
| 28 | 28 | . "/?spip.php[?]action=acceder_document.*file=(.*)$,");  | 
                                                        
| 29 | 29 | |
@@ -49,18 +49,18 @@ discard block  | 
                                                    ||
| 49 | 49 | * taille maxi de la copie local, par defaut _COPIE_LOCALE_MAX_SIZE  | 
                                                        
| 50 | 50 | * @return bool|string  | 
                                                        
| 51 | 51 | */  | 
                                                        
| 52 | -function copie_locale($source, $mode='auto', $local=null, $taille_max=null){ | 
                                                        |
| 52 | +function copie_locale($source, $mode = 'auto', $local = null, $taille_max = null) { | 
                                                        |
| 53 | 53 | |
| 54 | 54 | // si c'est la protection de soi-meme, retourner le path  | 
                                                        
| 55 | 55 |  	if ($mode !== 'force' AND preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { | 
                                                        
| 56 | - $source = substr(_DIR_IMG,strlen(_DIR_RACINE)) . urldecode($match[1]);  | 
                                                        |
| 56 | + $source = substr(_DIR_IMG, strlen(_DIR_RACINE)).urldecode($match[1]);  | 
                                                        |
| 57 | 57 | return @file_exists($source) ? $source : false;  | 
                                                        
| 58 | 58 | }  | 
                                                        
| 59 | 59 | |
| 60 | 60 | if (is_null($local))  | 
                                                        
| 61 | 61 | $local = fichier_copie_locale($source);  | 
                                                        
| 62 | 62 |  	else { | 
                                                        
| 63 | -		if (_DIR_RACINE AND strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE))==0) { | 
                                                        |
| 63 | +		if (_DIR_RACINE AND strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) { | 
                                                        |
| 64 | 64 | $local = substr($local, strlen(_DIR_RACINE));  | 
                                                        
| 65 | 65 | }  | 
                                                        
| 66 | 66 | }  | 
                                                        
@@ -71,16 +71,16 @@ discard block  | 
                                                    ||
| 71 | 71 | if (!$local) return false;  | 
                                                        
| 72 | 72 | |
| 73 | 73 | $localrac = _DIR_RACINE.$local;  | 
                                                        
| 74 | - $t = ($mode=='force') ? false : @file_exists($localrac);  | 
                                                        |
| 74 | + $t = ($mode == 'force') ? false : @file_exists($localrac);  | 
                                                        |
| 75 | 75 | |
| 76 | 76 | // test d'existence du fichier  | 
                                                        
| 77 | - if ($mode=='test') return $t ? $local : '';  | 
                                                        |
| 77 | + if ($mode == 'test') return $t ? $local : '';  | 
                                                        |
| 78 | 78 | |
| 79 | 79 | // sinon voir si on doit/peut le telecharger  | 
                                                        
| 80 | -	if ($local==$source OR !preg_match(',^\w+://,', $source)) | 
                                                        |
| 80 | +	if ($local == $source OR !preg_match(',^\w+://,', $source)) | 
                                                        |
| 81 | 81 | return $local;  | 
                                                        
| 82 | 82 | |
| 83 | -	if ($mode=='modif' OR !$t){ | 
                                                        |
| 83 | +	if ($mode == 'modif' OR !$t) { | 
                                                        |
| 84 | 84 | // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation  | 
                                                        
| 85 | 85 | // et des eventuelles recuperations concurantes  | 
                                                        
| 86 | 86 |  		include_spip("inc/acces"); | 
                                                        
@@ -88,7 +88,7 @@ discard block  | 
                                                    ||
| 88 | 88 | $res = recuperer_page($source, $localrac, false, $taille_max, '', '', false, $t ? filemtime($localrac) : '');  | 
                                                        
| 89 | 89 |  		if (!$res) { | 
                                                        
| 90 | 90 | if (!$t) // si $t c'est sans doute juste un not-modified-since qui fait renvoyer false  | 
                                                        
| 91 | -				spip_log("copie_locale : Echec recuperation $source sur $localrac",_LOG_INFO_IMPORTANTE); | 
                                                        |
| 91 | +				spip_log("copie_locale : Echec recuperation $source sur $localrac", _LOG_INFO_IMPORTANTE); | 
                                                        |
| 92 | 92 | return $t ? $local : false;  | 
                                                        
| 93 | 93 | }  | 
                                                        
| 94 | 94 |  		spip_log("copie_locale : recuperation $source sur $localrac taille $res OK"); | 
                                                        
@@ -121,7 +121,7 @@ discard block  | 
                                                    ||
| 121 | 121 | * url ou false en cas d'echec  | 
                                                        
| 122 | 122 | */  | 
                                                        
| 123 | 123 |  function valider_url_distante($url, $known_hosts = array()) { | 
                                                        
| 124 | -	if (!function_exists('protocole_verifier')){ | 
                                                        |
| 124 | +	if (!function_exists('protocole_verifier')) { | 
                                                        |
| 125 | 125 |  		include_spip('inc/filtres_mini'); | 
                                                        
| 126 | 126 | }  | 
                                                        
| 127 | 127 | |
@@ -130,7 +130,7 @@ discard block  | 
                                                    ||
| 130 | 130 | }  | 
                                                        
| 131 | 131 | |
| 132 | 132 | $parsed_url = parse_url($url);  | 
                                                        
| 133 | -	if (!$parsed_url or empty($parsed_url['host']) ) { | 
                                                        |
| 133 | +	if (!$parsed_url or empty($parsed_url['host'])) { | 
                                                        |
| 134 | 134 | return false;  | 
                                                        
| 135 | 135 | }  | 
                                                        
| 136 | 136 | |
@@ -171,10 +171,10 @@ discard block  | 
                                                    ||
| 171 | 171 | }  | 
                                                        
| 172 | 172 | }  | 
                                                        
| 173 | 173 |  		if ($ip) { | 
                                                        
| 174 | -			$parts = array_map('intval', explode( '.', $ip )); | 
                                                        |
| 174 | +			$parts = array_map('intval', explode('.', $ip)); | 
                                                        |
| 175 | 175 | if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0]  | 
                                                        
| 176 | - or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] )  | 
                                                        |
| 177 | - or ( 192 === $parts[0] && 168 === $parts[1] )  | 
                                                        |
| 176 | + or (172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1])  | 
                                                        |
| 177 | + or (192 === $parts[0] && 168 === $parts[1])  | 
                                                        |
| 178 | 178 |  			) { | 
                                                        
| 179 | 179 | return false;  | 
                                                        
| 180 | 180 | }  | 
                                                        
@@ -186,7 +186,7 @@ discard block  | 
                                                    ||
| 186 | 186 | }  | 
                                                        
| 187 | 187 | |
| 188 | 188 | $port = $parsed_url['port'];  | 
                                                        
| 189 | -	if ($port === 80  or $port === 443  or $port === 8080) { | 
                                                        |
| 189 | +	if ($port === 80 or $port === 443 or $port === 8080) { | 
                                                        |
| 190 | 190 | return $url;  | 
                                                        
| 191 | 191 | }  | 
                                                        
| 192 | 192 | |
@@ -212,27 +212,27 @@ discard block  | 
                                                    ||
| 212 | 212 | // pour un appel soap par exemple  | 
                                                        
| 213 | 213 | // l'entete est separe des donnees par un double retour a la ligne  | 
                                                        
| 214 | 214 | // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n  | 
                                                        
| 215 | -	if (is_string($donnees) && strlen($donnees)){ | 
                                                        |
| 215 | +	if (is_string($donnees) && strlen($donnees)) { | 
                                                        |
| 216 | 216 | $entete = "";  | 
                                                        
| 217 | 217 | // on repasse tous les \r\n et \r en simples \n  | 
                                                        
| 218 | -		$donnees = str_replace("\r\n","\n",$donnees); | 
                                                        |
| 219 | -		$donnees = str_replace("\r","\n",$donnees); | 
                                                        |
| 218 | +		$donnees = str_replace("\r\n", "\n", $donnees); | 
                                                        |
| 219 | +		$donnees = str_replace("\r", "\n", $donnees); | 
                                                        |
| 220 | 220 | // un double retour a la ligne signifie la fin de l'entete et le debut des donnees  | 
                                                        
| 221 | 221 | $p = strpos($donnees, "\n\n");  | 
                                                        
| 222 | -		if ($p!==FALSE){ | 
                                                        |
| 223 | -			$entete = str_replace("\n", "\r\n", substr($donnees, 0, $p+1)); | 
                                                        |
| 224 | - $donnees = substr($donnees, $p+2);  | 
                                                        |
| 222 | +		if ($p !== FALSE) { | 
                                                        |
| 223 | +			$entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); | 
                                                        |
| 224 | + $donnees = substr($donnees, $p + 2);  | 
                                                        |
| 225 | 225 | }  | 
                                                        
| 226 | 226 |  		$chaine = str_replace("\n", "\r\n", $donnees); | 
                                                        
| 227 | 227 | }  | 
                                                        
| 228 | 228 |  	else { | 
                                                        
| 229 | 229 | /* boundary automatique */  | 
                                                        
| 230 | 230 | // Si on a plus de 500 octects de donnees, on "boundarise"  | 
                                                        
| 231 | -		if ($boundary===''){ | 
                                                        |
| 231 | +		if ($boundary === '') { | 
                                                        |
| 232 | 232 | $taille = 0;  | 
                                                        
| 233 | -			foreach ($donnees as $cle => $valeur){ | 
                                                        |
| 234 | -				if (is_array($valeur)){ | 
                                                        |
| 235 | -					foreach ($valeur as $val2){ | 
                                                        |
| 233 | +			foreach ($donnees as $cle => $valeur) { | 
                                                        |
| 234 | +				if (is_array($valeur)) { | 
                                                        |
| 235 | +					foreach ($valeur as $val2) { | 
                                                        |
| 236 | 236 | $taille += strlen($val2);  | 
                                                        
| 237 | 237 | }  | 
                                                        
| 238 | 238 |  				} else { | 
                                                        
@@ -240,12 +240,12 @@ discard block  | 
                                                    ||
| 240 | 240 | $taille += strlen($valeur);  | 
                                                        
| 241 | 241 | }  | 
                                                        
| 242 | 242 | }  | 
                                                        
| 243 | -			if ($taille>500){ | 
                                                        |
| 244 | - $boundary = substr(md5(rand() . 'spip'), 0, 8);  | 
                                                        |
| 243 | +			if ($taille > 500) { | 
                                                        |
| 244 | + $boundary = substr(md5(rand().'spip'), 0, 8);  | 
                                                        |
| 245 | 245 | }  | 
                                                        
| 246 | 246 | }  | 
                                                        
| 247 | 247 | |
| 248 | -		if (is_string($boundary) and strlen($boundary)){ | 
                                                        |
| 248 | +		if (is_string($boundary) and strlen($boundary)) { | 
                                                        |
| 249 | 249 | // fabrique une chaine HTTP pour un POST avec boundary  | 
                                                        
| 250 | 250 | $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n";  | 
                                                        
| 251 | 251 | $chaine = '';  | 
                                                        
@@ -334,21 +334,21 @@ discard block  | 
                                                    ||
| 334 | 334 | // http://doc.spip.org/@recuperer_page  | 
                                                        
| 335 | 335 | function recuperer_page($url, $trans = false, $get_headers = false,  | 
                                                        
| 336 | 336 | $taille_max = null, $datas = '', $boundary = '', $refuser_gz = false,  | 
                                                        
| 337 | -                        $date_verif = '', $uri_referer = ''){ | 
                                                        |
| 337 | +                        $date_verif = '', $uri_referer = '') { | 
                                                        |
| 338 | 338 | $gz = false;  | 
                                                        
| 339 | 339 | |
| 340 | 340 | // $copy = copier le fichier ?  | 
                                                        
| 341 | - $copy = (is_string($trans) AND strlen($trans)>5); // eviter "false" :-)  | 
                                                        |
| 341 | + $copy = (is_string($trans) AND strlen($trans) > 5); // eviter "false" :-)  | 
                                                        |
| 342 | 342 | |
| 343 | 343 | if (is_null($taille_max))  | 
                                                        
| 344 | 344 | $taille_max = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;  | 
                                                        
| 345 | 345 | |
| 346 | 346 | // Accepter les URLs au format feed:// ou qui ont oublie le http://  | 
                                                        
| 347 | 347 |  	$url = preg_replace(',^feed://,i', 'http://', $url); | 
                                                        
| 348 | -	if (!preg_match(',^[a-z]+://,i', $url)) $url = 'http://' . $url; | 
                                                        |
| 348 | +	if (!preg_match(',^[a-z]+://,i', $url)) $url = 'http://'.$url; | 
                                                        |
| 349 | 349 | $url = url_to_ascii($url);  | 
                                                        
| 350 | 350 | |
| 351 | - if ($taille_max==0)  | 
                                                        |
| 351 | + if ($taille_max == 0)  | 
                                                        |
| 352 | 352 | $get = 'HEAD';  | 
                                                        
| 353 | 353 | else  | 
                                                        
| 354 | 354 | $get = 'GET';  | 
                                                        
@@ -356,16 +356,16 @@ discard block  | 
                                                    ||
| 356 | 356 |  	if (!empty($datas)) { | 
                                                        
| 357 | 357 | $get = 'POST';  | 
                                                        
| 358 | 358 | list($type, $postdata) = prepare_donnees_post($datas, $boundary);  | 
                                                        
| 359 | - $datas = $type . 'Content-Length: ' . strlen($postdata) . "\r\n\r\n" . $postdata;  | 
                                                        |
| 359 | + $datas = $type.'Content-Length: '.strlen($postdata)."\r\n\r\n".$postdata;  | 
                                                        |
| 360 | 360 | }  | 
                                                        
| 361 | 361 | |
| 362 | 362 | // dix tentatives maximum en cas d'entetes 301...  | 
                                                        
| 363 | -	for ($i = 0; $i<10; $i++){ | 
                                                        |
| 363 | +	for ($i = 0; $i < 10; $i++) { | 
                                                        |
| 364 | 364 | $url = recuperer_lapage($url, $trans, $get, $taille_max, $datas, $refuser_gz, $date_verif, $uri_referer);  | 
                                                        
| 365 | 365 | if (!$url) return false;  | 
                                                        
| 366 | -		if (is_array($url)){ | 
                                                        |
| 366 | +		if (is_array($url)) { | 
                                                        |
| 367 | 367 | list($headers, $result) = $url;  | 
                                                        
| 368 | - return ($get_headers ? $headers . "\n" : '') . $result;  | 
                                                        |
| 368 | + return ($get_headers ? $headers."\n" : '').$result;  | 
                                                        |
| 369 | 369 |  		} else spip_log("recuperer page recommence sur $url"); | 
                                                        
| 370 | 370 | }  | 
                                                        
| 371 | 371 | }  | 
                                                        
@@ -375,9 +375,9 @@ discard block  | 
                                                    ||
| 375 | 375 | // si $trans est null -> on ne veut que les headers  | 
                                                        
| 376 | 376 | // si $trans est une chaine, c'est un nom de fichier pour ecrire directement dedans  | 
                                                        
| 377 | 377 | // http://doc.spip.org/@recuperer_lapage  | 
                                                        
| 378 | -function recuperer_lapage($url, $trans = false, $get = 'GET', $taille_max = _INC_DISTANT_MAX_SIZE, $datas = '', $refuser_gz = false, $date_verif = '', $uri_referer = ''){ | 
                                                        |
| 378 | +function recuperer_lapage($url, $trans = false, $get = 'GET', $taille_max = _INC_DISTANT_MAX_SIZE, $datas = '', $refuser_gz = false, $date_verif = '', $uri_referer = '') { | 
                                                        |
| 379 | 379 | // $copy = copier le fichier ?  | 
                                                        
| 380 | - $copy = (is_string($trans) AND strlen($trans)>5); // eviter "false" :-)  | 
                                                        |
| 380 | + $copy = (is_string($trans) AND strlen($trans) > 5); // eviter "false" :-)  | 
                                                        |
| 381 | 381 | |
| 382 | 382 | // si on ecrit directement dans un fichier, pour ne pas manipuler  | 
                                                        
| 383 | 383 | // en memoire refuser gz  | 
                                                        
@@ -386,7 +386,7 @@ discard block  | 
                                                    ||
| 386 | 386 | |
| 387 | 387 | // ouvrir la connexion et envoyer la requete et ses en-tetes  | 
                                                        
| 388 | 388 | list($f, $fopen) = init_http($get, $url, $refuser_gz, $uri_referer, $datas, _INC_DISTANT_VERSION_HTTP, $date_verif);  | 
                                                        
| 389 | -	if (!$f){ | 
                                                        |
| 389 | +	if (!$f) { | 
                                                        |
| 390 | 390 |  		spip_log("ECHEC init_http $url"); | 
                                                        
| 391 | 391 | return false;  | 
                                                        
| 392 | 392 | }  | 
                                                        
@@ -398,25 +398,25 @@ discard block  | 
                                                    ||
| 398 | 398 | $headers = '';  | 
                                                        
| 399 | 399 |  	else { | 
                                                        
| 400 | 400 | $headers = recuperer_entetes($f, $date_verif);  | 
                                                        
| 401 | -		if (is_numeric($headers)){ | 
                                                        |
| 401 | +		if (is_numeric($headers)) { | 
                                                        |
| 402 | 402 | fclose($f);  | 
                                                        
| 403 | 403 | // Chinoisierie inexplicable pour contrer  | 
                                                        
| 404 | 404 | // les actions liberticides de l'empire du milieu  | 
                                                        
| 405 | -			if ($headers){ | 
                                                        |
| 405 | +			if ($headers) { | 
                                                        |
| 406 | 406 |  				spip_log("HTTP status $headers pour $url"); | 
                                                        
| 407 | 407 | return false;  | 
                                                        
| 408 | 408 | }  | 
                                                        
| 409 | 409 | $t = @parse_url($url);  | 
                                                        
| 410 | 410 | $host = $t['host'];  | 
                                                        
| 411 | 411 | if (!need_proxy($host)  | 
                                                        
| 412 | -				AND $result = @file_get_contents($url)){ | 
                                                        |
| 412 | +				AND $result = @file_get_contents($url)) { | 
                                                        |
| 413 | 413 | // on peuple les headers de vide et on continue  | 
                                                        
| 414 | 414 |  				$headers = array(''); | 
                                                        
| 415 | 415 | }  | 
                                                        
| 416 | 416 | else  | 
                                                        
| 417 | 417 | return false;  | 
                                                        
| 418 | 418 | }  | 
                                                        
| 419 | -		if (!is_array($headers)){ // cas Location | 
                                                        |
| 419 | +		if (!is_array($headers)) { // cas Location | 
                                                        |
| 420 | 420 | fclose($f);  | 
                                                        
| 421 | 421 |  			include_spip('inc/filtres'); | 
                                                        
| 422 | 422 | return suivre_lien($url, $headers);  | 
                                                        
@@ -424,16 +424,16 @@ discard block  | 
                                                    ||
| 424 | 424 |  		$headers = join('', $headers); | 
                                                        
| 425 | 425 | }  | 
                                                        
| 426 | 426 | |
| 427 | - if ($trans===NULL) return array($headers, '');  | 
                                                        |
| 427 | + if ($trans === NULL) return array($headers, '');  | 
                                                        |
| 428 | 428 | |
| 429 | 429 | // s'il faut deballer, le faire via un fichier temporaire  | 
                                                        
| 430 | 430 | // sinon la memoire explose pour les gros flux  | 
                                                        
| 431 | 431 | |
| 432 | 432 |  	$gz = preg_match(",\bContent-Encoding: .*gzip,is", $headers) ? | 
                                                        
| 433 | - (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz') : '';  | 
                                                        |
| 433 | + (_DIR_TMP.md5(uniqid(mt_rand())).'.tmp.gz') : '';  | 
                                                        |
| 434 | 434 | |
| 435 | 435 |  #	spip_log("entete ($trans $copy $gz)\n$headers"); | 
                                                        
| 436 | -	if (!$result){ | 
                                                        |
| 436 | +	if (!$result) { | 
                                                        |
| 437 | 437 | $result = recuperer_body($f, $taille_max, $gz ? $gz : ($copy ? $trans : ''));  | 
                                                        
| 438 | 438 | fclose($f);  | 
                                                        
| 439 | 439 | }  | 
                                                        
@@ -441,12 +441,12 @@ discard block  | 
                                                    ||
| 441 | 441 | return array($headers, $result);  | 
                                                        
| 442 | 442 | |
| 443 | 443 | // Decompresser au besoin  | 
                                                        
| 444 | -	if ($gz){ | 
                                                        |
| 444 | +	if ($gz) { | 
                                                        |
| 445 | 445 |  		$result = join('', gzfile($gz)); | 
                                                        
| 446 | 446 | supprimer_fichier($gz);  | 
                                                        
| 447 | 447 | }  | 
                                                        
| 448 | 448 | // Faut-il l'importer dans notre charset local ?  | 
                                                        
| 449 | -	if ($trans===true){ | 
                                                        |
| 449 | +	if ($trans === true) { | 
                                                        |
| 450 | 450 |  		include_spip('inc/charsets'); | 
                                                        
| 451 | 451 | $result = transcoder_page($result, $headers);  | 
                                                        
| 452 | 452 | }  | 
                                                        
@@ -455,32 +455,32 @@ discard block  | 
                                                    ||
| 455 | 455 | }  | 
                                                        
| 456 | 456 | |
| 457 | 457 | // http://doc.spip.org/@recuperer_body  | 
                                                        
| 458 | -function recuperer_body($f, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = ''){ | 
                                                        |
| 458 | +function recuperer_body($f, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = '') { | 
                                                        |
| 459 | 459 | $taille = 0;  | 
                                                        
| 460 | 460 | $result = '';  | 
                                                        
| 461 | 461 | $fp = false;  | 
                                                        
| 462 | -	if ($fichier){ | 
                                                        |
| 462 | +	if ($fichier) { | 
                                                        |
| 463 | 463 |  		include_spip("inc/acces"); | 
                                                        
| 464 | 464 | $tmpfile = "$fichier.".creer_uniqid().".tmp";  | 
                                                        
| 465 | 465 | $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX);  | 
                                                        
| 466 | -		if (!$fp AND file_exists($fichier)){ | 
                                                        |
| 466 | +		if (!$fp AND file_exists($fichier)) { | 
                                                        |
| 467 | 467 | return filesize($fichier);  | 
                                                        
| 468 | 468 | }  | 
                                                        
| 469 | 469 | if (!$fp)  | 
                                                        
| 470 | 470 | return false;  | 
                                                        
| 471 | 471 | $result = 0; // on renvoie la taille du fichier  | 
                                                        
| 472 | 472 | }  | 
                                                        
| 473 | -	while (!feof($f) AND $taille<$taille_max){ | 
                                                        |
| 473 | +	while (!feof($f) AND $taille < $taille_max) { | 
                                                        |
| 474 | 474 | $res = fread($f, 16384);  | 
                                                        
| 475 | 475 | $taille += strlen($res);  | 
                                                        
| 476 | -		if ($fp){ | 
                                                        |
| 476 | +		if ($fp) { | 
                                                        |
| 477 | 477 | fwrite($fp, $res);  | 
                                                        
| 478 | 478 | $result = $taille;  | 
                                                        
| 479 | 479 | }  | 
                                                        
| 480 | 480 | else  | 
                                                        
| 481 | 481 | $result .= $res;  | 
                                                        
| 482 | 482 | }  | 
                                                        
| 483 | -	if ($fp){ | 
                                                        |
| 483 | +	if ($fp) { | 
                                                        |
| 484 | 484 | spip_fclose_unlock($fp);  | 
                                                        
| 485 | 485 | spip_unlink($fichier);  | 
                                                        
| 486 | 486 | @rename($tmpfile, $fichier);  | 
                                                        
@@ -496,24 +496,24 @@ discard block  | 
                                                    ||
| 496 | 496 | // le tableau des entetes dans tous les autres cas  | 
                                                        
| 497 | 497 | |
| 498 | 498 | // http://doc.spip.org/@recuperer_entetes  | 
                                                        
| 499 | -function recuperer_entetes($f, $date_verif = ''){ | 
                                                        |
| 499 | +function recuperer_entetes($f, $date_verif = '') { | 
                                                        |
| 500 | 500 | $s = @trim(fgets($f, 16384));  | 
                                                        
| 501 | 501 | |
| 502 | -	if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)){ | 
                                                        |
| 502 | +	if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) { | 
                                                        |
| 503 | 503 | return 0;  | 
                                                        
| 504 | 504 | }  | 
                                                        
| 505 | 505 | $status = intval($r[1]);  | 
                                                        
| 506 | 506 | $headers = array();  | 
                                                        
| 507 | 507 | $not_modif = $location = false;  | 
                                                        
| 508 | -	while ($s = trim(fgets($f, 16384))){ | 
                                                        |
| 509 | - $headers[] = $s . "\n";  | 
                                                        |
| 508 | +	while ($s = trim(fgets($f, 16384))) { | 
                                                        |
| 509 | + $headers[] = $s."\n";  | 
                                                        |
| 510 | 510 |  		preg_match(',^([^:]*): *(.*)$,i', $s, $r); | 
                                                        
| 511 | 511 | list(, $d, $v) = $r;  | 
                                                        
| 512 | -		if (strtolower(trim($d))=='location' AND $status>=300 AND $status<400){ | 
                                                        |
| 512 | +		if (strtolower(trim($d)) == 'location' AND $status >= 300 AND $status < 400) { | 
                                                        |
| 513 | 513 | $location = $v;  | 
                                                        
| 514 | 514 | }  | 
                                                        
| 515 | -		elseif ($date_verif AND ($d=='Last-Modified')) { | 
                                                        |
| 516 | -			if ($date_verif>=strtotime($v)){ | 
                                                        |
| 515 | +		elseif ($date_verif AND ($d == 'Last-Modified')) { | 
                                                        |
| 516 | +			if ($date_verif >= strtotime($v)) { | 
                                                        |
| 517 | 517 | //Cas ou la page distante n'a pas bouge depuis  | 
                                                        
| 518 | 518 | //la derniere visite  | 
                                                        
| 519 | 519 | $not_modif = true;  | 
                                                        
@@ -523,7 +523,7 @@ discard block  | 
                                                    ||
| 523 | 523 | |
| 524 | 524 | if ($location)  | 
                                                        
| 525 | 525 | return $location;  | 
                                                        
| 526 | - if ($status!=200 or $not_modif)  | 
                                                        |
| 526 | + if ($status != 200 or $not_modif)  | 
                                                        |
| 527 | 527 | return $status;  | 
                                                        
| 528 | 528 | return $headers;  | 
                                                        
| 529 | 529 | }  | 
                                                        
@@ -533,7 +533,7 @@ discard block  | 
                                                    ||
| 533 | 533 | // mais la tout de suite je ne trouve pas l'idee, etant donne les limitations  | 
                                                        
| 534 | 534 | // des filesystems  | 
                                                        
| 535 | 535 | // http://doc.spip.org/@nom_fichier_copie_locale  | 
                                                        
| 536 | -function nom_fichier_copie_locale($source, $extension){ | 
                                                        |
| 536 | +function nom_fichier_copie_locale($source, $extension) { | 
                                                        |
| 537 | 537 |  	include_spip('inc/documents'); | 
                                                        
| 538 | 538 | |
| 539 | 539 |  	$d = creer_repertoire_documents('distant'); # IMG/distant/ | 
                                                        
@@ -541,12 +541,12 @@ discard block  | 
                                                    ||
| 541 | 541 | |
| 542 | 542 | // on se place tout le temps comme si on etait a la racine  | 
                                                        
| 543 | 543 | if (_DIR_RACINE)  | 
                                                        
| 544 | -		$d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); | 
                                                        |
| 544 | +		$d = preg_replace(',^'.preg_quote(_DIR_RACINE).',', '', $d); | 
                                                        |
| 545 | 545 | |
| 546 | 546 | $m = md5($source);  | 
                                                        
| 547 | 547 | |
| 548 | 548 | return $d  | 
                                                        
| 549 | -		. substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) | 
                                                        |
| 549 | +		. substr(preg_replace(',[^\w-],', '', basename($source)).'-'.$m, 0, 12) | 
                                                        |
| 550 | 550 | . substr($m, 0, 4)  | 
                                                        
| 551 | 551 | . ".$extension";  | 
                                                        
| 552 | 552 | }  | 
                                                        
@@ -555,11 +555,11 @@ discard block  | 
                                                    ||
| 555 | 555 | // Donne le nom de la copie locale de la source  | 
                                                        
| 556 | 556 | //  | 
                                                        
| 557 | 557 | // http://doc.spip.org/@fichier_copie_locale  | 
                                                        
| 558 | -function fichier_copie_locale($source){ | 
                                                        |
| 558 | +function fichier_copie_locale($source) { | 
                                                        |
| 559 | 559 | // Si c'est deja local pas de souci  | 
                                                        
| 560 | -	if (!preg_match(',^\w+://,', $source)){ | 
                                                        |
| 560 | +	if (!preg_match(',^\w+://,', $source)) { | 
                                                        |
| 561 | 561 | if (_DIR_RACINE)  | 
                                                        
| 562 | -			$source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); | 
                                                        |
| 562 | +			$source = preg_replace(',^'.preg_quote(_DIR_RACINE).',', '', $source); | 
                                                        |
| 563 | 563 | return $source;  | 
                                                        
| 564 | 564 | }  | 
                                                        
| 565 | 565 | |
@@ -571,7 +571,7 @@ discard block  | 
                                                    ||
| 571 | 571 | if ($ext  | 
                                                        
| 572 | 572 |  	AND preg_match(',^\w+$,', $ext) // pas de php?truc=1&... | 
                                                        
| 573 | 573 | AND $f = nom_fichier_copie_locale($source, $ext)  | 
                                                        
| 574 | - AND file_exists(_DIR_RACINE . $f)  | 
                                                        |
| 574 | + AND file_exists(_DIR_RACINE.$f)  | 
                                                        |
| 575 | 575 | )  | 
                                                        
| 576 | 576 | return $f;  | 
                                                        
| 577 | 577 | |
@@ -579,7 +579,7 @@ discard block  | 
                                                    ||
| 579 | 579 | // Si c'est deja dans la table des documents,  | 
                                                        
| 580 | 580 | // ramener le nom de sa copie potentielle  | 
                                                        
| 581 | 581 | |
| 582 | -	$ext = sql_getfetsel("extension", "spip_documents", "fichier=" . sql_quote($source) . " AND distant='oui' AND extension <> ''"); | 
                                                        |
| 582 | +	$ext = sql_getfetsel("extension", "spip_documents", "fichier=".sql_quote($source)." AND distant='oui' AND extension <> ''"); | 
                                                        |
| 583 | 583 | |
| 584 | 584 | |
| 585 | 585 | if ($ext) return nom_fichier_copie_locale($source, $ext);  | 
                                                        
@@ -589,25 +589,25 @@ discard block  | 
                                                    ||
| 589 | 589 | |
| 590 | 590 | $ext = $path_parts ? $path_parts['extension'] : '';  | 
                                                        
| 591 | 591 | |
| 592 | -	if ($ext AND sql_getfetsel("extension", "spip_types_documents", "extension=" . sql_quote($ext))){ | 
                                                        |
| 592 | +	if ($ext AND sql_getfetsel("extension", "spip_types_documents", "extension=".sql_quote($ext))) { | 
                                                        |
| 593 | 593 | $f = nom_fichier_copie_locale($source, $ext);  | 
                                                        
| 594 | - if (file_exists(_DIR_RACINE . $f))  | 
                                                        |
| 594 | + if (file_exists(_DIR_RACINE.$f))  | 
                                                        |
| 595 | 595 | return $f;  | 
                                                        
| 596 | 596 | }  | 
                                                        
| 597 | 597 | |
| 598 | 598 | // Ping pour voir si son extension est connue et autorisee  | 
                                                        
| 599 | 599 | // avec mise en cache du resultat du ping  | 
                                                        
| 600 | 600 | |
| 601 | - $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source);  | 
                                                        |
| 601 | + $cache = sous_repertoire(_DIR_CACHE, 'rid').md5($source);  | 
                                                        |
| 602 | 602 | if (!@file_exists($cache)  | 
                                                        
| 603 | 603 | OR !$path_parts = @unserialize(spip_file_get_contents($cache))  | 
                                                        
| 604 | -		OR _request('var_mode')=='recalcul' | 
                                                        |
| 605 | -	){ | 
                                                        |
| 604 | +		OR _request('var_mode') == 'recalcul' | 
                                                        |
| 605 | +	) { | 
                                                        |
| 606 | 606 | $path_parts = recuperer_infos_distantes($source, 0, false);  | 
                                                        
| 607 | 607 | ecrire_fichier($cache, serialize($path_parts));  | 
                                                        
| 608 | 608 | }  | 
                                                        
| 609 | 609 | $ext = $path_parts ? $path_parts['extension'] : '';  | 
                                                        
| 610 | -	if ($ext AND sql_getfetsel("extension", "spip_types_documents", "extension=" . sql_quote($ext))){ | 
                                                        |
| 610 | +	if ($ext AND sql_getfetsel("extension", "spip_types_documents", "extension=".sql_quote($ext))) { | 
                                                        |
| 611 | 611 | return nom_fichier_copie_locale($source, $ext);  | 
                                                        
| 612 | 612 | }  | 
                                                        
| 613 | 613 |  	spip_log("pas de copie locale pour $source"); | 
                                                        
@@ -626,7 +626,7 @@ discard block  | 
                                                    ||
| 626 | 626 | #$a['mime_type'] = chaine  | 
                                                        
| 627 | 627 | |
| 628 | 628 | // http://doc.spip.org/@recuperer_infos_distantes  | 
                                                        
| 629 | -function recuperer_infos_distantes($source, $max = 0, $charger_si_petite_image = true){ | 
                                                        |
| 629 | +function recuperer_infos_distantes($source, $max = 0, $charger_si_petite_image = true) { | 
                                                        |
| 630 | 630 | |
| 631 | 631 | // pas la peine de perdre son temps  | 
                                                        
| 632 | 632 |  	if (!tester_url_absolue($source)) { | 
                                                        
@@ -642,7 +642,7 @@ discard block  | 
                                                    ||
| 642 | 642 | // On va directement charger le debut des images et des fichiers html,  | 
                                                        
| 643 | 643 | // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si  | 
                                                        
| 644 | 644 | // ca echoue l'utilisateur devra les entrer...  | 
                                                        
| 645 | -	if ($headers = recuperer_page($source, false, true, $max, '', '', true)){ | 
                                                        |
| 645 | +	if ($headers = recuperer_page($source, false, true, $max, '', '', true)) { | 
                                                        |
| 646 | 646 |  		list($headers, $a['body']) = preg_split(',\n\n,', $headers, 2); | 
                                                        
| 647 | 647 | |
| 648 | 648 |  		if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) | 
                                                        
@@ -660,36 +660,36 @@ discard block  | 
                                                    ||
| 660 | 660 | // ce qu'il sert ; on va tenter de detecter via l'extension de l'url  | 
                                                        
| 661 | 661 | // ou le Content-Disposition: attachment; filename=...  | 
                                                        
| 662 | 662 | $t = null;  | 
                                                        
| 663 | -		if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))){ | 
                                                        |
| 663 | +		if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))) { | 
                                                        |
| 664 | 664 | if (!$t  | 
                                                        
| 665 | 665 |  				AND preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) | 
                                                        
| 666 | -			){ | 
                                                        |
| 667 | -				$t = sql_fetsel("extension", "spip_types_documents", "extension=" . sql_quote($rext[1],'','text')); | 
                                                        |
| 666 | +			) { | 
                                                        |
| 667 | +				$t = sql_fetsel("extension", "spip_types_documents", "extension=".sql_quote($rext[1], '', 'text')); | 
                                                        |
| 668 | 668 | }  | 
                                                        
| 669 | 669 | if (!$t  | 
                                                        
| 670 | 670 |  				AND preg_match(",^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims", $headers, $m) | 
                                                        
| 671 | 671 |  				AND preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) | 
                                                        
| 672 | -			){ | 
                                                        |
| 673 | -				$t = sql_fetsel("extension", "spip_types_documents", "extension=" . sql_quote($rext[1],'','text')); | 
                                                        |
| 672 | +			) { | 
                                                        |
| 673 | +				$t = sql_fetsel("extension", "spip_types_documents", "extension=".sql_quote($rext[1], '', 'text')); | 
                                                        |
| 674 | 674 | }  | 
                                                        
| 675 | 675 | }  | 
                                                        
| 676 | 676 | |
| 677 | 677 | // Autre mime/type (ou text/plain avec fichier d'extension inconnue)  | 
                                                        
| 678 | 678 | if (!$t)  | 
                                                        
| 679 | -			$t = sql_fetsel("extension", "spip_types_documents", "mime_type=" . sql_quote($mime_type)); | 
                                                        |
| 679 | +			$t = sql_fetsel("extension", "spip_types_documents", "mime_type=".sql_quote($mime_type)); | 
                                                        |
| 680 | 680 | |
| 681 | 681 | // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)  | 
                                                        
| 682 | 682 | // On essaie de nouveau avec l'extension  | 
                                                        
| 683 | 683 | if (!$t  | 
                                                        
| 684 | - AND $mime_type!='text/plain'  | 
                                                        |
| 684 | + AND $mime_type != 'text/plain'  | 
                                                        |
| 685 | 685 |  			AND preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) | 
                                                        
| 686 | -		){ | 
                                                        |
| 687 | -			$t = sql_fetsel("extension", "spip_types_documents", "extension=" . sql_quote($rext[1],'','text')); # eviter xxx.3 => 3gp (> SPIP 3) | 
                                                        |
| 686 | +		) { | 
                                                        |
| 687 | +			$t = sql_fetsel("extension", "spip_types_documents", "extension=".sql_quote($rext[1], '', 'text')); # eviter xxx.3 => 3gp (> SPIP 3) | 
                                                        |
| 688 | 688 | }  | 
                                                        
| 689 | 689 | |
| 690 | 690 | |
| 691 | -		if ($t){ | 
                                                        |
| 692 | -			spip_log("mime-type $mime_type ok, extension " . $t['extension']); | 
                                                        |
| 691 | +		if ($t) { | 
                                                        |
| 692 | +			spip_log("mime-type $mime_type ok, extension ".$t['extension']); | 
                                                        |
| 693 | 693 | $a['extension'] = $t['extension'];  | 
                                                        
| 694 | 694 |  		} else { | 
                                                        
| 695 | 695 | # par defaut on retombe sur '.bin' si c'est autorise  | 
                                                        
@@ -706,7 +706,7 @@ discard block  | 
                                                    ||
| 706 | 706 | }  | 
                                                        
| 707 | 707 | |
| 708 | 708 | // Echec avec HEAD, on tente avec GET  | 
                                                        
| 709 | -	if (!$a AND !$max){ | 
                                                        |
| 709 | +	if (!$a AND !$max) { | 
                                                        |
| 710 | 710 |  		spip_log("tenter GET $source"); | 
                                                        
| 711 | 711 | $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);  | 
                                                        
| 712 | 712 | }  | 
                                                        
@@ -718,16 +718,16 @@ discard block  | 
                                                    ||
| 718 | 718 | |
| 719 | 719 | // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller  | 
                                                        
| 720 | 720 | // recharger le document en GET et recuperer des donnees supplementaires...  | 
                                                        
| 721 | -	if (preg_match(',^image/(jpeg|gif|png|swf),', $mime_type)){ | 
                                                        |
| 722 | - if ($max==0  | 
                                                        |
| 723 | - AND $a['taille']<_INC_DISTANT_MAX_SIZE  | 
                                                        |
| 724 | - AND (strpos($GLOBALS['meta']['formats_graphiques'], $a['extension'])!==false)  | 
                                                        |
| 721 | +	if (preg_match(',^image/(jpeg|gif|png|swf),', $mime_type)) { | 
                                                        |
| 722 | + if ($max == 0  | 
                                                        |
| 723 | + AND $a['taille'] < _INC_DISTANT_MAX_SIZE  | 
                                                        |
| 724 | + AND (strpos($GLOBALS['meta']['formats_graphiques'], $a['extension']) !== false)  | 
                                                        |
| 725 | 725 | AND $charger_si_petite_image  | 
                                                        
| 726 | -		){ | 
                                                        |
| 726 | +		) { | 
                                                        |
| 727 | 727 | $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);  | 
                                                        
| 728 | 728 | }  | 
                                                        
| 729 | -		else if ($a['body']){ | 
                                                        |
| 730 | - $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $a['extension']);  | 
                                                        |
| 729 | +		else if ($a['body']) { | 
                                                        |
| 730 | + $a['fichier'] = _DIR_RACINE.nom_fichier_copie_locale($source, $a['extension']);  | 
                                                        |
| 731 | 731 | ecrire_fichier($a['fichier'], $a['body']);  | 
                                                        
| 732 | 732 | $size_image = @getimagesize($a['fichier']);  | 
                                                        
| 733 | 733 | $a['largeur'] = intval($size_image[0]);  | 
                                                        
@@ -738,21 +738,21 @@ discard block  | 
                                                    ||
| 738 | 738 | |
| 739 | 739 | // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut  | 
                                                        
| 740 | 740 | // ce sera mieux que 0x0  | 
                                                        
| 741 | - if ($a AND $a['extension']=='swf'  | 
                                                        |
| 741 | + if ($a AND $a['extension'] == 'swf'  | 
                                                        |
| 742 | 742 | AND !$a['largeur']  | 
                                                        
| 743 | -	){ | 
                                                        |
| 743 | +	) { | 
                                                        |
| 744 | 744 | $a['largeur'] = 425;  | 
                                                        
| 745 | 745 | $a['hauteur'] = 350;  | 
                                                        
| 746 | 746 | }  | 
                                                        
| 747 | 747 | |
| 748 | -	if ($mime_type=='text/html'){ | 
                                                        |
| 748 | +	if ($mime_type == 'text/html') { | 
                                                        |
| 749 | 749 |  		include_spip('inc/filtres'); | 
                                                        
| 750 | 750 | $page = recuperer_page($source, true, false, _INC_DISTANT_MAX_SIZE);  | 
                                                        
| 751 | 751 |  		if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) | 
                                                        
| 752 | 752 | $a['titre'] = corriger_caracteres(trim($regs[1]));  | 
                                                        
| 753 | 753 | if (!$a['taille']) $a['taille'] = strlen($page); # a peu pres  | 
                                                        
| 754 | 754 | }  | 
                                                        
| 755 | - $a['mime_type']=$mime_type;  | 
                                                        |
| 755 | + $a['mime_type'] = $mime_type;  | 
                                                        |
| 756 | 756 | |
| 757 | 757 | return $a;  | 
                                                        
| 758 | 758 | }  | 
                                                        
@@ -768,7 +768,7 @@ discard block  | 
                                                    ||
| 768 | 768 | * @param string $http_noproxy  | 
                                                        
| 769 | 769 | * @return string  | 
                                                        
| 770 | 770 | */  | 
                                                        
| 771 | -function need_proxy($host, $http_proxy = null, $http_noproxy = null){ | 
                                                        |
| 771 | +function need_proxy($host, $http_proxy = null, $http_noproxy = null) { | 
                                                        |
| 772 | 772 | if (is_null($http_proxy))  | 
                                                        
| 773 | 773 | $http_proxy = @$GLOBALS['meta']["http_proxy"];  | 
                                                        
| 774 | 774 | if (is_null($http_noproxy))  | 
                                                        
@@ -777,8 +777,8 @@ discard block  | 
                                                    ||
| 777 | 777 | $domain = substr($host, strpos($host, '.'));  | 
                                                        
| 778 | 778 | |
| 779 | 779 | return ($http_proxy  | 
                                                        
| 780 | -		AND (strpos(" $http_noproxy ", " $host ")===false | 
                                                        |
| 781 | -		AND (strpos(" $http_noproxy ", " $domain ")===false))) | 
                                                        |
| 780 | +		AND (strpos(" $http_noproxy ", " $host ") === false | 
                                                        |
| 781 | +		AND (strpos(" $http_noproxy ", " $domain ") === false))) | 
                                                        |
| 782 | 782 | ? $http_proxy : '';  | 
                                                        
| 783 | 783 | }  | 
                                                        
| 784 | 784 | |
@@ -787,37 +787,37 @@ discard block  | 
                                                    ||
| 787 | 787 | // retourne le descripteur sur lequel lire la reponse  | 
                                                        
| 788 | 788 | //  | 
                                                        
| 789 | 789 | // http://doc.spip.org/@init_http  | 
                                                        
| 790 | -function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = "", $vers = "HTTP/1.0", $date = ''){ | 
                                                        |
| 790 | +function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = "", $vers = "HTTP/1.0", $date = '') { | 
                                                        |
| 791 | 791 | $user = $via_proxy = $proxy_user = '';  | 
                                                        
| 792 | 792 | $fopen = false;  | 
                                                        
| 793 | 793 | |
| 794 | 794 | $t = @parse_url($url);  | 
                                                        
| 795 | 795 | $host = $t['host'];  | 
                                                        
| 796 | -	if ($t['scheme']=='http'){ | 
                                                        |
| 796 | +	if ($t['scheme'] == 'http') { | 
                                                        |
| 797 | 797 | $scheme = 'http';  | 
                                                        
| 798 | 798 | $noproxy = '';  | 
                                                        
| 799 | -	} elseif ($t['scheme']=='https') { | 
                                                        |
| 799 | +	} elseif ($t['scheme'] == 'https') { | 
                                                        |
| 800 | 800 | $scheme = 'ssl';  | 
                                                        
| 801 | 801 | $noproxy = 'ssl://';  | 
                                                        
| 802 | 802 | if (!isset($t['port']) || !($port = $t['port'])) $t['port'] = 443;  | 
                                                        
| 803 | 803 | }  | 
                                                        
| 804 | 804 |  	else { | 
                                                        
| 805 | 805 | $scheme = $t['scheme'];  | 
                                                        
| 806 | - $noproxy = $scheme . '://';  | 
                                                        |
| 806 | + $noproxy = $scheme.'://';  | 
                                                        |
| 807 | 807 | }  | 
                                                        
| 808 | 808 | if (isset($t['user']))  | 
                                                        
| 809 | 809 | $user = array($t['user'], $t['pass']);  | 
                                                        
| 810 | 810 | |
| 811 | 811 | if (!isset($t['port']) || !($port = $t['port'])) $port = 80;  | 
                                                        
| 812 | 812 | if (!isset($t['path']) || !($path = $t['path'])) $path = "/";  | 
                                                        
| 813 | - if (@$t['query']) $path .= "?" . $t['query'];  | 
                                                        |
| 813 | + if (@$t['query']) $path .= "?".$t['query'];  | 
                                                        |
| 814 | 814 | |
| 815 | 815 | $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);  | 
                                                        
| 816 | -	if (!$f){ | 
                                                        |
| 816 | +	if (!$f) { | 
                                                        |
| 817 | 817 | // fallback : fopen  | 
                                                        
| 818 | 818 | if (!need_proxy($host)  | 
                                                        
| 819 | 819 |  		  AND !_request('tester_proxy') | 
                                                        
| 820 | -		  AND (!isset($GLOBALS['inc_distant_allow_fopen']) OR $GLOBALS['inc_distant_allow_fopen'])){ | 
                                                        |
| 820 | +		  AND (!isset($GLOBALS['inc_distant_allow_fopen']) OR $GLOBALS['inc_distant_allow_fopen'])) { | 
                                                        |
| 821 | 821 | $f = @fopen($url, "rb");  | 
                                                        
| 822 | 822 |  			spip_log("connexion vers $url par simple fopen"); | 
                                                        
| 823 | 823 | $fopen = true;  | 
                                                        
@@ -831,37 +831,37 @@ discard block  | 
                                                    ||
| 831 | 831 | }  | 
                                                        
| 832 | 832 | |
| 833 | 833 | // http://doc.spip.org/@lance_requete  | 
                                                        
| 834 | -function lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz = false, $referer = '', $datas = "", $vers = "HTTP/1.0", $date = ''){ | 
                                                        |
| 834 | +function lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz = false, $referer = '', $datas = "", $vers = "HTTP/1.0", $date = '') { | 
                                                        |
| 835 | 835 | |
| 836 | 836 | $proxy_user = '';  | 
                                                        
| 837 | 837 | $http_proxy = need_proxy($host);  | 
                                                        
| 838 | - if ($user) $user = urlencode($user[0]) . ":" . urlencode($user[1]);  | 
                                                        |
| 838 | + if ($user) $user = urlencode($user[0]).":".urlencode($user[1]);  | 
                                                        |
| 839 | 839 | |
| 840 | 840 | $connect = '';  | 
                                                        
| 841 | 841 |  	if ($http_proxy) { | 
                                                        
| 842 | -		if (defined('_PROXY_HTTPS_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) { | 
                                                        |
| 843 | - $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : '');  | 
                                                        |
| 844 | - $connect = 'CONNECT ' . $path_host . " $vers\r\n"  | 
                                                        |
| 842 | +		if (defined('_PROXY_HTTPS_VIA_CONNECT') and in_array($scheme, array('tls', 'ssl'))) { | 
                                                        |
| 843 | + $path_host = (!$user ? '' : "$user@").$host.(($port != 80) ? ":$port" : '');  | 
                                                        |
| 844 | + $connect = 'CONNECT '.$path_host." $vers\r\n"  | 
                                                        |
| 845 | 845 | . "Host: $path_host\r\n"  | 
                                                        
| 846 | 846 | . "Proxy-Connection: Keep-Alive\r\n";  | 
                                                        
| 847 | 847 |  		} else { | 
                                                        
| 848 | -			$path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://") | 
                                                        |
| 848 | +			$path = (in_array($scheme, array('tls', 'ssl')) ? 'https://' : "$scheme://") | 
                                                        |
| 849 | 849 | . (!$user ? '' : "$user@")  | 
                                                        
| 850 | - . "$host" . (($port!=80) ? ":$port" : "") . $path;  | 
                                                        |
| 850 | + . "$host".(($port != 80) ? ":$port" : "").$path;  | 
                                                        |
| 851 | 851 | }  | 
                                                        
| 852 | 852 | $t2 = @parse_url($http_proxy);  | 
                                                        
| 853 | 853 | $first_host = $t2['host'];  | 
                                                        
| 854 | 854 | if (!($port = $t2['port'])) $port = 80;  | 
                                                        
| 855 | 855 | if ($t2['user'])  | 
                                                        
| 856 | - $proxy_user = base64_encode($t2['user'] . ":" . $t2['pass']);  | 
                                                        |
| 856 | + $proxy_user = base64_encode($t2['user'].":".$t2['pass']);  | 
                                                        |
| 857 | 857 | }  | 
                                                        
| 858 | 858 | else  | 
                                                        
| 859 | - $first_host = $noproxy . $host;  | 
                                                        |
| 859 | + $first_host = $noproxy.$host;  | 
                                                        |
| 860 | 860 | |
| 861 | -	if ($connect){ | 
                                                        |
| 861 | +	if ($connect) { | 
                                                        |
| 862 | 862 |  		$streamContext = stream_context_create(array('ssl' => array('verify_peer' => false, 'allow_self_signed' => true))); | 
                                                        
| 863 | 863 |  		$f = @stream_socket_client("tcp://$first_host:$port", $nError, $sError, _INC_DISTANT_CONNECT_TIMEOUT, STREAM_CLIENT_CONNECT, $streamContext); | 
                                                        
| 864 | -		spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)","connect"); | 
                                                        |
| 864 | +		spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", "connect"); | 
                                                        |
| 865 | 865 | if (!$f) return false;  | 
                                                        
| 866 | 866 | stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        
| 867 | 867 | |
@@ -869,23 +869,23 @@ discard block  | 
                                                    ||
| 869 | 869 | fputs($f, "\r\n");  | 
                                                        
| 870 | 870 | $res = fread($f, 1024);  | 
                                                        
| 871 | 871 | if (!$res  | 
                                                        
| 872 | -		  OR !count($res = explode(' ',$res)) | 
                                                        |
| 873 | -		  OR $res[1]!=='200'){ | 
                                                        |
| 874 | -			spip_log("Echec CONNECT sur $first_host:$port","connect"._LOG_INFO_IMPORTANTE); | 
                                                        |
| 872 | +		  OR !count($res = explode(' ', $res)) | 
                                                        |
| 873 | +		  OR $res[1] !== '200') { | 
                                                        |
| 874 | +			spip_log("Echec CONNECT sur $first_host:$port", "connect"._LOG_INFO_IMPORTANTE); | 
                                                        |
| 875 | 875 | fclose($f);  | 
                                                        
| 876 | 876 | return false;  | 
                                                        
| 877 | 877 | }  | 
                                                        
| 878 | 878 | // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo  | 
                                                        
| 879 | 879 | stream_set_blocking($f, true);  | 
                                                        
| 880 | 880 | // envoyer le handshake  | 
                                                        
| 881 | - stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);  | 
                                                        |
| 882 | -		spip_log("OK CONNECT sur $first_host:$port","connect"); | 
                                                        |
| 881 | + stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);  | 
                                                        |
| 882 | +		spip_log("OK CONNECT sur $first_host:$port", "connect"); | 
                                                        |
| 883 | 883 | }  | 
                                                        
| 884 | 884 |  	else { | 
                                                        
| 885 | 885 | $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        
| 886 | 886 |  		spip_log("Recuperer $path sur $first_host:$port par $f"); | 
                                                        
| 887 | 887 |  		if (!$f) { | 
                                                        
| 888 | -			spip_log("Erreur connexion $errno $errstr",_LOG_ERREUR); | 
                                                        |
| 888 | +			spip_log("Erreur connexion $errno $errstr", _LOG_ERREUR); | 
                                                        |
| 889 | 889 | return false;  | 
                                                        
| 890 | 890 | }  | 
                                                        
| 891 | 891 | stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        
@@ -895,11 +895,11 @@ discard block  | 
                                                    ||
| 895 | 895 | |
| 896 | 896 | $req = "$method $path $vers\r\n"  | 
                                                        
| 897 | 897 | . "Host: $host\r\n"  | 
                                                        
| 898 | - . "User-Agent: " . _INC_DISTANT_USER_AGENT . "\r\n"  | 
                                                        |
| 899 | -		. ($refuse_gz ? '' : ("Accept-Encoding: " . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) | 
                                                        |
| 898 | + . "User-Agent: "._INC_DISTANT_USER_AGENT."\r\n"  | 
                                                        |
| 899 | +		. ($refuse_gz ? '' : ("Accept-Encoding: "._INC_DISTANT_CONTENT_ENCODING."\r\n")) | 
                                                        |
| 900 | 900 | . (!$site ? '' : "Referer: $site/$referer\r\n")  | 
                                                        
| 901 | -		. (!$date ? '' : "If-Modified-Since: " . (gmdate("D, d M Y H:i:s", $date) . " GMT\r\n")) | 
                                                        |
| 902 | -		. (!$user ? '' : ("Authorization: Basic " . base64_encode($user) . "\r\n")) | 
                                                        |
| 901 | +		. (!$date ? '' : "If-Modified-Since: ".(gmdate("D, d M Y H:i:s", $date)." GMT\r\n")) | 
                                                        |
| 902 | +		. (!$user ? '' : ("Authorization: Basic ".base64_encode($user)."\r\n")) | 
                                                        |
| 903 | 903 | . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n")  | 
                                                        
| 904 | 904 | . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n");  | 
                                                        
| 905 | 905 | |
@@ -15,13 +15,25 @@ discard block  | 
                                                    ||
| 15 | 15 | *  | 
                                                        
| 16 | 16 | * @package SPIP\Core\Distant  | 
                                                        
| 17 | 17 | **/  | 
                                                        
| 18 | -if (!defined('_ECRIRE_INC_VERSION')) return; | 
                                                        |
| 18 | +if (!defined('_ECRIRE_INC_VERSION')) { | 
                                                        |
| 19 | + return;  | 
                                                        |
| 20 | +}  | 
                                                        |
| 19 | 21 | |
| 20 | -if (!defined('_INC_DISTANT_VERSION_HTTP')) define('_INC_DISTANT_VERSION_HTTP', "HTTP/1.0"); | 
                                                        |
| 21 | -if (!defined('_INC_DISTANT_CONTENT_ENCODING')) define('_INC_DISTANT_CONTENT_ENCODING', "gzip"); | 
                                                        |
| 22 | -if (!defined('_INC_DISTANT_USER_AGENT')) define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . " (" . $GLOBALS['home_server'] . ")"); | 
                                                        |
| 23 | -if (!defined('_INC_DISTANT_MAX_SIZE')) define('_INC_DISTANT_MAX_SIZE',2097152); | 
                                                        |
| 24 | -if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) define('_INC_DISTANT_CONNECT_TIMEOUT',10); | 
                                                        |
| 22 | +if (!defined('_INC_DISTANT_VERSION_HTTP')) { | 
                                                        |
| 23 | +    define('_INC_DISTANT_VERSION_HTTP', "HTTP/1.0"); | 
                                                        |
| 24 | +}  | 
                                                        |
| 25 | +if (!defined('_INC_DISTANT_CONTENT_ENCODING')) { | 
                                                        |
| 26 | +    define('_INC_DISTANT_CONTENT_ENCODING', "gzip"); | 
                                                        |
| 27 | +}  | 
                                                        |
| 28 | +if (!defined('_INC_DISTANT_USER_AGENT')) { | 
                                                        |
| 29 | +    define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . " (" . $GLOBALS['home_server'] . ")"); | 
                                                        |
| 30 | +}  | 
                                                        |
| 31 | +if (!defined('_INC_DISTANT_MAX_SIZE')) { | 
                                                        |
| 32 | +    define('_INC_DISTANT_MAX_SIZE',2097152); | 
                                                        |
| 33 | +}  | 
                                                        |
| 34 | +if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) { | 
                                                        |
| 35 | +    define('_INC_DISTANT_CONNECT_TIMEOUT',10); | 
                                                        |
| 36 | +}  | 
                                                        |
| 25 | 37 | |
| 26 | 38 |  define('_REGEXP_COPIE_LOCALE', ',' . | 
                                                        
| 27 | 39 |         preg_replace('@^https?:@', 'https?:', $GLOBALS['meta']['adresse_site']) | 
                                                        
@@ -57,9 +69,9 @@ discard block  | 
                                                    ||
| 57 | 69 | return @file_exists($source) ? $source : false;  | 
                                                        
| 58 | 70 | }  | 
                                                        
| 59 | 71 | |
| 60 | - if (is_null($local))  | 
                                                        |
| 61 | - $local = fichier_copie_locale($source);  | 
                                                        |
| 62 | -	else { | 
                                                        |
| 72 | +	if (is_null($local)) { | 
                                                        |
| 73 | + $local = fichier_copie_locale($source);  | 
                                                        |
| 74 | +	} else { | 
                                                        |
| 63 | 75 |  		if (_DIR_RACINE AND strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE))==0) { | 
                                                        
| 64 | 76 | $local = substr($local, strlen(_DIR_RACINE));  | 
                                                        
| 65 | 77 | }  | 
                                                        
@@ -68,27 +80,36 @@ discard block  | 
                                                    ||
| 68 | 80 | // si $local = '' c'est un fichier refuse par fichier_copie_locale(),  | 
                                                        
| 69 | 81 | // par exemple un fichier qui ne figure pas dans nos documents ;  | 
                                                        
| 70 | 82 | // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer  | 
                                                        
| 71 | - if (!$local) return false;  | 
                                                        |
| 83 | +	if (!$local) { | 
                                                        |
| 84 | + return false;  | 
                                                        |
| 85 | + }  | 
                                                        |
| 72 | 86 | |
| 73 | 87 | $localrac = _DIR_RACINE.$local;  | 
                                                        
| 74 | 88 | $t = ($mode=='force') ? false : @file_exists($localrac);  | 
                                                        
| 75 | 89 | |
| 76 | 90 | // test d'existence du fichier  | 
                                                        
| 77 | - if ($mode=='test') return $t ? $local : '';  | 
                                                        |
| 91 | +	if ($mode=='test') { | 
                                                        |
| 92 | + return $t ? $local : '';  | 
                                                        |
| 93 | + }  | 
                                                        |
| 78 | 94 | |
| 79 | 95 | // sinon voir si on doit/peut le telecharger  | 
                                                        
| 80 | -	if ($local==$source OR !preg_match(',^\w+://,', $source)) | 
                                                        |
| 81 | - return $local;  | 
                                                        |
| 96 | +	if ($local==$source OR !preg_match(',^\w+://,', $source)) { | 
                                                        |
| 97 | + return $local;  | 
                                                        |
| 98 | + }  | 
                                                        |
| 82 | 99 | |
| 83 | 100 |  	if ($mode=='modif' OR !$t){ | 
                                                        
| 84 | 101 | // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation  | 
                                                        
| 85 | 102 | // et des eventuelles recuperations concurantes  | 
                                                        
| 86 | 103 |  		include_spip("inc/acces"); | 
                                                        
| 87 | - if (!$taille_max) $taille_max = _COPIE_LOCALE_MAX_SIZE;  | 
                                                        |
| 104 | +		if (!$taille_max) { | 
                                                        |
| 105 | + $taille_max = _COPIE_LOCALE_MAX_SIZE;  | 
                                                        |
| 106 | + }  | 
                                                        |
| 88 | 107 | $res = recuperer_page($source, $localrac, false, $taille_max, '', '', false, $t ? filemtime($localrac) : '');  | 
                                                        
| 89 | 108 |  		if (!$res) { | 
                                                        
| 90 | - if (!$t) // si $t c'est sans doute juste un not-modified-since qui fait renvoyer false  | 
                                                        |
| 109 | +			if (!$t) { | 
                                                        |
| 110 | + // si $t c'est sans doute juste un not-modified-since qui fait renvoyer false  | 
                                                        |
| 91 | 111 |  				spip_log("copie_locale : Echec recuperation $source sur $localrac",_LOG_INFO_IMPORTANTE); | 
                                                        
| 112 | + }  | 
                                                        |
| 92 | 113 | return $t ? $local : false;  | 
                                                        
| 93 | 114 | }  | 
                                                        
| 94 | 115 |  		spip_log("copie_locale : recuperation $source sur $localrac taille $res OK"); | 
                                                        
@@ -224,8 +245,7 @@ discard block  | 
                                                    ||
| 224 | 245 | $donnees = substr($donnees, $p+2);  | 
                                                        
| 225 | 246 | }  | 
                                                        
| 226 | 247 |  		$chaine = str_replace("\n", "\r\n", $donnees); | 
                                                        
| 227 | - }  | 
                                                        |
| 228 | -	else { | 
                                                        |
| 248 | +	} else { | 
                                                        |
| 229 | 249 | /* boundary automatique */  | 
                                                        
| 230 | 250 | // Si on a plus de 500 octects de donnees, on "boundarise"  | 
                                                        
| 231 | 251 |  		if ($boundary===''){ | 
                                                        
@@ -340,18 +360,22 @@ discard block  | 
                                                    ||
| 340 | 360 | // $copy = copier le fichier ?  | 
                                                        
| 341 | 361 | $copy = (is_string($trans) AND strlen($trans)>5); // eviter "false" :-)  | 
                                                        
| 342 | 362 | |
| 343 | - if (is_null($taille_max))  | 
                                                        |
| 344 | - $taille_max = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;  | 
                                                        |
| 363 | +	if (is_null($taille_max)) { | 
                                                        |
| 364 | + $taille_max = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;  | 
                                                        |
| 365 | + }  | 
                                                        |
| 345 | 366 | |
| 346 | 367 | // Accepter les URLs au format feed:// ou qui ont oublie le http://  | 
                                                        
| 347 | 368 |  	$url = preg_replace(',^feed://,i', 'http://', $url); | 
                                                        
| 348 | -	if (!preg_match(',^[a-z]+://,i', $url)) $url = 'http://' . $url; | 
                                                        |
| 369 | +	if (!preg_match(',^[a-z]+://,i', $url)) { | 
                                                        |
| 370 | + $url = 'http://' . $url;  | 
                                                        |
| 371 | + }  | 
                                                        |
| 349 | 372 | $url = url_to_ascii($url);  | 
                                                        
| 350 | 373 | |
| 351 | - if ($taille_max==0)  | 
                                                        |
| 352 | - $get = 'HEAD';  | 
                                                        |
| 353 | - else  | 
                                                        |
| 354 | - $get = 'GET';  | 
                                                        |
| 374 | +	if ($taille_max==0) { | 
                                                        |
| 375 | + $get = 'HEAD';  | 
                                                        |
| 376 | +	} else { | 
                                                        |
| 377 | + $get = 'GET';  | 
                                                        |
| 378 | + }  | 
                                                        |
| 355 | 379 | |
| 356 | 380 |  	if (!empty($datas)) { | 
                                                        
| 357 | 381 | $get = 'POST';  | 
                                                        
@@ -362,11 +386,15 @@ discard block  | 
                                                    ||
| 362 | 386 | // dix tentatives maximum en cas d'entetes 301...  | 
                                                        
| 363 | 387 |  	for ($i = 0; $i<10; $i++){ | 
                                                        
| 364 | 388 | $url = recuperer_lapage($url, $trans, $get, $taille_max, $datas, $refuser_gz, $date_verif, $uri_referer);  | 
                                                        
| 365 | - if (!$url) return false;  | 
                                                        |
| 389 | +		if (!$url) { | 
                                                        |
| 390 | + return false;  | 
                                                        |
| 391 | + }  | 
                                                        |
| 366 | 392 |  		if (is_array($url)){ | 
                                                        
| 367 | 393 | list($headers, $result) = $url;  | 
                                                        
| 368 | 394 | return ($get_headers ? $headers . "\n" : '') . $result;  | 
                                                        
| 369 | -		} else spip_log("recuperer page recommence sur $url"); | 
                                                        |
| 395 | +		} else { | 
                                                        |
| 396 | +		    spip_log("recuperer page recommence sur $url"); | 
                                                        |
| 397 | + }  | 
                                                        |
| 370 | 398 | }  | 
                                                        
| 371 | 399 | }  | 
                                                        
| 372 | 400 | |
@@ -381,8 +409,9 @@ discard block  | 
                                                    ||
| 381 | 409 | |
| 382 | 410 | // si on ecrit directement dans un fichier, pour ne pas manipuler  | 
                                                        
| 383 | 411 | // en memoire refuser gz  | 
                                                        
| 384 | - if ($copy)  | 
                                                        |
| 385 | - $refuser_gz = true;  | 
                                                        |
| 412 | +	if ($copy) { | 
                                                        |
| 413 | + $refuser_gz = true;  | 
                                                        |
| 414 | + }  | 
                                                        |
| 386 | 415 | |
| 387 | 416 | // ouvrir la connexion et envoyer la requete et ses en-tetes  | 
                                                        
| 388 | 417 | list($f, $fopen) = init_http($get, $url, $refuser_gz, $uri_referer, $datas, _INC_DISTANT_VERSION_HTTP, $date_verif);  | 
                                                        
@@ -394,9 +423,9 @@ discard block  | 
                                                    ||
| 394 | 423 | $result = '';  | 
                                                        
| 395 | 424 | // Sauf en fopen, envoyer le flux d'entree  | 
                                                        
| 396 | 425 | // et recuperer les en-tetes de reponses  | 
                                                        
| 397 | - if ($fopen)  | 
                                                        |
| 398 | - $headers = '';  | 
                                                        |
| 399 | -	else { | 
                                                        |
| 426 | +	if ($fopen) { | 
                                                        |
| 427 | + $headers = '';  | 
                                                        |
| 428 | +	} else { | 
                                                        |
| 400 | 429 | $headers = recuperer_entetes($f, $date_verif);  | 
                                                        
| 401 | 430 |  		if (is_numeric($headers)){ | 
                                                        
| 402 | 431 | fclose($f);  | 
                                                        
@@ -412,9 +441,9 @@ discard block  | 
                                                    ||
| 412 | 441 |  				AND $result = @file_get_contents($url)){ | 
                                                        
| 413 | 442 | // on peuple les headers de vide et on continue  | 
                                                        
| 414 | 443 |  				$headers = array(''); | 
                                                        
| 444 | +			} else { | 
                                                        |
| 445 | + return false;  | 
                                                        |
| 415 | 446 | }  | 
                                                        
| 416 | - else  | 
                                                        |
| 417 | - return false;  | 
                                                        |
| 418 | 447 | }  | 
                                                        
| 419 | 448 |  		if (!is_array($headers)){ // cas Location | 
                                                        
| 420 | 449 | fclose($f);  | 
                                                        
@@ -424,7 +453,9 @@ discard block  | 
                                                    ||
| 424 | 453 |  		$headers = join('', $headers); | 
                                                        
| 425 | 454 | }  | 
                                                        
| 426 | 455 | |
| 427 | - if ($trans===NULL) return array($headers, '');  | 
                                                        |
| 456 | +	if ($trans===NULL) { | 
                                                        |
| 457 | + return array($headers, '');  | 
                                                        |
| 458 | + }  | 
                                                        |
| 428 | 459 | |
| 429 | 460 | // s'il faut deballer, le faire via un fichier temporaire  | 
                                                        
| 430 | 461 | // sinon la memoire explose pour les gros flux  | 
                                                        
@@ -437,8 +468,9 @@ discard block  | 
                                                    ||
| 437 | 468 | $result = recuperer_body($f, $taille_max, $gz ? $gz : ($copy ? $trans : ''));  | 
                                                        
| 438 | 469 | fclose($f);  | 
                                                        
| 439 | 470 | }  | 
                                                        
| 440 | - if (!$result)  | 
                                                        |
| 441 | - return array($headers, $result);  | 
                                                        |
| 471 | +	if (!$result) { | 
                                                        |
| 472 | + return array($headers, $result);  | 
                                                        |
| 473 | + }  | 
                                                        |
| 442 | 474 | |
| 443 | 475 | // Decompresser au besoin  | 
                                                        
| 444 | 476 |  	if ($gz){ | 
                                                        
@@ -466,8 +498,9 @@ discard block  | 
                                                    ||
| 466 | 498 |  		if (!$fp AND file_exists($fichier)){ | 
                                                        
| 467 | 499 | return filesize($fichier);  | 
                                                        
| 468 | 500 | }  | 
                                                        
| 469 | - if (!$fp)  | 
                                                        |
| 470 | - return false;  | 
                                                        |
| 501 | +		if (!$fp) { | 
                                                        |
| 502 | + return false;  | 
                                                        |
| 503 | + }  | 
                                                        |
| 471 | 504 | $result = 0; // on renvoie la taille du fichier  | 
                                                        
| 472 | 505 | }  | 
                                                        
| 473 | 506 |  	while (!feof($f) AND $taille<$taille_max){ | 
                                                        
@@ -476,16 +509,17 @@ discard block  | 
                                                    ||
| 476 | 509 |  		if ($fp){ | 
                                                        
| 477 | 510 | fwrite($fp, $res);  | 
                                                        
| 478 | 511 | $result = $taille;  | 
                                                        
| 512 | +		} else { | 
                                                        |
| 513 | + $result .= $res;  | 
                                                        |
| 479 | 514 | }  | 
                                                        
| 480 | - else  | 
                                                        |
| 481 | - $result .= $res;  | 
                                                        |
| 482 | 515 | }  | 
                                                        
| 483 | 516 |  	if ($fp){ | 
                                                        
| 484 | 517 | spip_fclose_unlock($fp);  | 
                                                        
| 485 | 518 | spip_unlink($fichier);  | 
                                                        
| 486 | 519 | @rename($tmpfile, $fichier);  | 
                                                        
| 487 | - if (!file_exists($fichier))  | 
                                                        |
| 488 | - return false;  | 
                                                        |
| 520 | +		if (!file_exists($fichier)) { | 
                                                        |
| 521 | + return false;  | 
                                                        |
| 522 | + }  | 
                                                        |
| 489 | 523 | }  | 
                                                        
| 490 | 524 | return $result;  | 
                                                        
| 491 | 525 | }  | 
                                                        
@@ -511,8 +545,7 @@ discard block  | 
                                                    ||
| 511 | 545 | list(, $d, $v) = $r;  | 
                                                        
| 512 | 546 |  		if (strtolower(trim($d))=='location' AND $status>=300 AND $status<400){ | 
                                                        
| 513 | 547 | $location = $v;  | 
                                                        
| 514 | - }  | 
                                                        |
| 515 | -		elseif ($date_verif AND ($d=='Last-Modified')) { | 
                                                        |
| 548 | +		} elseif ($date_verif AND ($d=='Last-Modified')) { | 
                                                        |
| 516 | 549 |  			if ($date_verif>=strtotime($v)){ | 
                                                        
| 517 | 550 | //Cas ou la page distante n'a pas bouge depuis  | 
                                                        
| 518 | 551 | //la derniere visite  | 
                                                        
@@ -521,10 +554,12 @@ discard block  | 
                                                    ||
| 521 | 554 | }  | 
                                                        
| 522 | 555 | }  | 
                                                        
| 523 | 556 | |
| 524 | - if ($location)  | 
                                                        |
| 525 | - return $location;  | 
                                                        |
| 526 | - if ($status!=200 or $not_modif)  | 
                                                        |
| 527 | - return $status;  | 
                                                        |
| 557 | +	if ($location) { | 
                                                        |
| 558 | + return $location;  | 
                                                        |
| 559 | + }  | 
                                                        |
| 560 | +	if ($status!=200 or $not_modif) { | 
                                                        |
| 561 | + return $status;  | 
                                                        |
| 562 | + }  | 
                                                        |
| 528 | 563 | return $headers;  | 
                                                        
| 529 | 564 | }  | 
                                                        
| 530 | 565 | |
@@ -540,8 +575,9 @@ discard block  | 
                                                    ||
| 540 | 575 | $d = sous_repertoire($d, $extension); # IMG/distant/pdf/  | 
                                                        
| 541 | 576 | |
| 542 | 577 | // on se place tout le temps comme si on etait a la racine  | 
                                                        
| 543 | - if (_DIR_RACINE)  | 
                                                        |
| 544 | -		$d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); | 
                                                        |
| 578 | +	if (_DIR_RACINE) { | 
                                                        |
| 579 | +			$d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); | 
                                                        |
| 580 | + }  | 
                                                        |
| 545 | 581 | |
| 546 | 582 | $m = md5($source);  | 
                                                        
| 547 | 583 | |
@@ -558,8 +594,9 @@ discard block  | 
                                                    ||
| 558 | 594 |  function fichier_copie_locale($source){ | 
                                                        
| 559 | 595 | // Si c'est deja local pas de souci  | 
                                                        
| 560 | 596 |  	if (!preg_match(',^\w+://,', $source)){ | 
                                                        
| 561 | - if (_DIR_RACINE)  | 
                                                        |
| 562 | -			$source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); | 
                                                        |
| 597 | +		if (_DIR_RACINE) { | 
                                                        |
| 598 | +					$source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); | 
                                                        |
| 599 | + }  | 
                                                        |
| 563 | 600 | return $source;  | 
                                                        
| 564 | 601 | }  | 
                                                        
| 565 | 602 | |
@@ -572,8 +609,9 @@ discard block  | 
                                                    ||
| 572 | 609 |  	AND preg_match(',^\w+$,', $ext) // pas de php?truc=1&... | 
                                                        
| 573 | 610 | AND $f = nom_fichier_copie_locale($source, $ext)  | 
                                                        
| 574 | 611 | AND file_exists(_DIR_RACINE . $f)  | 
                                                        
| 575 | - )  | 
                                                        |
| 576 | - return $f;  | 
                                                        |
| 612 | +	) { | 
                                                        |
| 613 | + return $f;  | 
                                                        |
| 614 | + }  | 
                                                        |
| 577 | 615 | |
| 578 | 616 | |
| 579 | 617 | // Si c'est deja dans la table des documents,  | 
                                                        
@@ -582,7 +620,9 @@ discard block  | 
                                                    ||
| 582 | 620 |  	$ext = sql_getfetsel("extension", "spip_documents", "fichier=" . sql_quote($source) . " AND distant='oui' AND extension <> ''"); | 
                                                        
| 583 | 621 | |
| 584 | 622 | |
| 585 | - if ($ext) return nom_fichier_copie_locale($source, $ext);  | 
                                                        |
| 623 | +	if ($ext) { | 
                                                        |
| 624 | + return nom_fichier_copie_locale($source, $ext);  | 
                                                        |
| 625 | + }  | 
                                                        |
| 586 | 626 | |
| 587 | 627 | // voir si l'extension indiquee dans le nom du fichier est ok  | 
                                                        
| 588 | 628 | // et si il n'aurait pas deja ete rapatrie  | 
                                                        
@@ -591,8 +631,9 @@ discard block  | 
                                                    ||
| 591 | 631 | |
| 592 | 632 |  	if ($ext AND sql_getfetsel("extension", "spip_types_documents", "extension=" . sql_quote($ext))){ | 
                                                        
| 593 | 633 | $f = nom_fichier_copie_locale($source, $ext);  | 
                                                        
| 594 | - if (file_exists(_DIR_RACINE . $f))  | 
                                                        |
| 595 | - return $f;  | 
                                                        |
| 634 | +		if (file_exists(_DIR_RACINE . $f)) { | 
                                                        |
| 635 | + return $f;  | 
                                                        |
| 636 | + }  | 
                                                        |
| 596 | 637 | }  | 
                                                        
| 597 | 638 | |
| 598 | 639 | // Ping pour voir si son extension est connue et autorisee  | 
                                                        
@@ -645,14 +686,17 @@ discard block  | 
                                                    ||
| 645 | 686 |  	if ($headers = recuperer_page($source, false, true, $max, '', '', true)){ | 
                                                        
| 646 | 687 |  		list($headers, $a['body']) = preg_split(',\n\n,', $headers, 2); | 
                                                        
| 647 | 688 | |
| 648 | -		if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) | 
                                                        |
| 649 | - $mime_type = (trim($regs[1]));  | 
                                                        |
| 650 | - else  | 
                                                        |
| 651 | - $mime_type = ''; // inconnu  | 
                                                        |
| 689 | +		if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) { | 
                                                        |
| 690 | + $mime_type = (trim($regs[1]));  | 
                                                        |
| 691 | +		} else { | 
                                                        |
| 692 | + $mime_type = '';  | 
                                                        |
| 693 | + }  | 
                                                        |
| 694 | + // inconnu  | 
                                                        |
| 652 | 695 | |
| 653 | 696 | // Appliquer les alias  | 
                                                        
| 654 | - while (isset($mime_alias[$mime_type]))  | 
                                                        |
| 655 | - $mime_type = $mime_alias[$mime_type];  | 
                                                        |
| 697 | +		while (isset($mime_alias[$mime_type])) { | 
                                                        |
| 698 | + $mime_type = $mime_alias[$mime_type];  | 
                                                        |
| 699 | + }  | 
                                                        |
| 656 | 700 | |
| 657 | 701 | // Si on a un mime-type insignifiant  | 
                                                        
| 658 | 702 | // text/plain,application/octet-stream ou vide  | 
                                                        
@@ -675,8 +719,9 @@ discard block  | 
                                                    ||
| 675 | 719 | }  | 
                                                        
| 676 | 720 | |
| 677 | 721 | // Autre mime/type (ou text/plain avec fichier d'extension inconnue)  | 
                                                        
| 678 | - if (!$t)  | 
                                                        |
| 679 | -			$t = sql_fetsel("extension", "spip_types_documents", "mime_type=" . sql_quote($mime_type)); | 
                                                        |
| 722 | +		if (!$t) { | 
                                                        |
| 723 | +					$t = sql_fetsel("extension", "spip_types_documents", "mime_type=" . sql_quote($mime_type)); | 
                                                        |
| 724 | + }  | 
                                                        |
| 680 | 725 | |
| 681 | 726 | // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)  | 
                                                        
| 682 | 727 | // On essaie de nouveau avec l'extension  | 
                                                        
@@ -695,14 +740,17 @@ discard block  | 
                                                    ||
| 695 | 740 | # par defaut on retombe sur '.bin' si c'est autorise  | 
                                                        
| 696 | 741 |  			spip_log("mime-type $mime_type inconnu"); | 
                                                        
| 697 | 742 |  			$t = sql_fetsel("extension", "spip_types_documents", "extension='bin'"); | 
                                                        
| 698 | - if (!$t) return false;  | 
                                                        |
| 743 | +			if (!$t) { | 
                                                        |
| 744 | + return false;  | 
                                                        |
| 745 | + }  | 
                                                        |
| 699 | 746 | $a['extension'] = $t['extension'];  | 
                                                        
| 700 | 747 | }  | 
                                                        
| 701 | 748 | |
| 702 | 749 |  		if (preg_match(",\nContent-Length: *([^[:space:]]*),i", | 
                                                        
| 703 | 750 | "\n$headers", $regs)  | 
                                                        
| 704 | - )  | 
                                                        |
| 705 | - $a['taille'] = intval($regs[1]);  | 
                                                        |
| 751 | +		) { | 
                                                        |
| 752 | + $a['taille'] = intval($regs[1]);  | 
                                                        |
| 753 | + }  | 
                                                        |
| 706 | 754 | }  | 
                                                        
| 707 | 755 | |
| 708 | 756 | // Echec avec HEAD, on tente avec GET  | 
                                                        
@@ -725,8 +773,7 @@ discard block  | 
                                                    ||
| 725 | 773 | AND $charger_si_petite_image  | 
                                                        
| 726 | 774 |  		){ | 
                                                        
| 727 | 775 | $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);  | 
                                                        
| 728 | - }  | 
                                                        |
| 729 | -		else if ($a['body']){ | 
                                                        |
| 776 | +		} else if ($a['body']){ | 
                                                        |
| 730 | 777 | $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $a['extension']);  | 
                                                        
| 731 | 778 | ecrire_fichier($a['fichier'], $a['body']);  | 
                                                        
| 732 | 779 | $size_image = @getimagesize($a['fichier']);  | 
                                                        
@@ -748,9 +795,13 @@ discard block  | 
                                                    ||
| 748 | 795 |  	if ($mime_type=='text/html'){ | 
                                                        
| 749 | 796 |  		include_spip('inc/filtres'); | 
                                                        
| 750 | 797 | $page = recuperer_page($source, true, false, _INC_DISTANT_MAX_SIZE);  | 
                                                        
| 751 | -		if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) | 
                                                        |
| 752 | - $a['titre'] = corriger_caracteres(trim($regs[1]));  | 
                                                        |
| 753 | - if (!$a['taille']) $a['taille'] = strlen($page); # a peu pres  | 
                                                        |
| 798 | +		if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) { | 
                                                        |
| 799 | + $a['titre'] = corriger_caracteres(trim($regs[1]));  | 
                                                        |
| 800 | + }  | 
                                                        |
| 801 | +		if (!$a['taille']) { | 
                                                        |
| 802 | + $a['taille'] = strlen($page);  | 
                                                        |
| 803 | + }  | 
                                                        |
| 804 | + # a peu pres  | 
                                                        |
| 754 | 805 | }  | 
                                                        
| 755 | 806 | $a['mime_type']=$mime_type;  | 
                                                        
| 756 | 807 | |
@@ -769,10 +820,12 @@ discard block  | 
                                                    ||
| 769 | 820 | * @return string  | 
                                                        
| 770 | 821 | */  | 
                                                        
| 771 | 822 |  function need_proxy($host, $http_proxy = null, $http_noproxy = null){ | 
                                                        
| 772 | - if (is_null($http_proxy))  | 
                                                        |
| 773 | - $http_proxy = @$GLOBALS['meta']["http_proxy"];  | 
                                                        |
| 774 | - if (is_null($http_noproxy))  | 
                                                        |
| 775 | - $http_noproxy = @$GLOBALS['meta']["http_noproxy"];  | 
                                                        |
| 823 | +	if (is_null($http_proxy)) { | 
                                                        |
| 824 | + $http_proxy = @$GLOBALS['meta']["http_proxy"];  | 
                                                        |
| 825 | + }  | 
                                                        |
| 826 | +	if (is_null($http_noproxy)) { | 
                                                        |
| 827 | + $http_noproxy = @$GLOBALS['meta']["http_noproxy"];  | 
                                                        |
| 828 | + }  | 
                                                        |
| 776 | 829 | |
| 777 | 830 | $domain = substr($host, strpos($host, '.'));  | 
                                                        
| 778 | 831 | |
@@ -799,18 +852,26 @@ discard block  | 
                                                    ||
| 799 | 852 |  	} elseif ($t['scheme']=='https') { | 
                                                        
| 800 | 853 | $scheme = 'ssl';  | 
                                                        
| 801 | 854 | $noproxy = 'ssl://';  | 
                                                        
| 802 | - if (!isset($t['port']) || !($port = $t['port'])) $t['port'] = 443;  | 
                                                        |
| 803 | - }  | 
                                                        |
| 804 | -	else { | 
                                                        |
| 855 | +		if (!isset($t['port']) || !($port = $t['port'])) { | 
                                                        |
| 856 | + $t['port'] = 443;  | 
                                                        |
| 857 | + }  | 
                                                        |
| 858 | +	} else { | 
                                                        |
| 805 | 859 | $scheme = $t['scheme'];  | 
                                                        
| 806 | 860 | $noproxy = $scheme . '://';  | 
                                                        
| 807 | 861 | }  | 
                                                        
| 808 | - if (isset($t['user']))  | 
                                                        |
| 809 | - $user = array($t['user'], $t['pass']);  | 
                                                        |
| 862 | +	if (isset($t['user'])) { | 
                                                        |
| 863 | + $user = array($t['user'], $t['pass']);  | 
                                                        |
| 864 | + }  | 
                                                        |
| 810 | 865 | |
| 811 | - if (!isset($t['port']) || !($port = $t['port'])) $port = 80;  | 
                                                        |
| 812 | - if (!isset($t['path']) || !($path = $t['path'])) $path = "/";  | 
                                                        |
| 813 | - if (@$t['query']) $path .= "?" . $t['query'];  | 
                                                        |
| 866 | +	if (!isset($t['port']) || !($port = $t['port'])) { | 
                                                        |
| 867 | + $port = 80;  | 
                                                        |
| 868 | + }  | 
                                                        |
| 869 | +	if (!isset($t['path']) || !($path = $t['path'])) { | 
                                                        |
| 870 | + $path = "/";  | 
                                                        |
| 871 | + }  | 
                                                        |
| 872 | +	if (@$t['query']) { | 
                                                        |
| 873 | + $path .= "?" . $t['query'];  | 
                                                        |
| 874 | + }  | 
                                                        |
| 814 | 875 | |
| 815 | 876 | $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);  | 
                                                        
| 816 | 877 |  	if (!$f){ | 
                                                        
@@ -821,9 +882,9 @@ discard block  | 
                                                    ||
| 821 | 882 | $f = @fopen($url, "rb");  | 
                                                        
| 822 | 883 |  			spip_log("connexion vers $url par simple fopen"); | 
                                                        
| 823 | 884 | $fopen = true;  | 
                                                        
| 885 | +		} else { | 
                                                        |
| 886 | + $f = false;  | 
                                                        |
| 824 | 887 | }  | 
                                                        
| 825 | - else  | 
                                                        |
| 826 | - $f = false;  | 
                                                        |
| 827 | 888 | // echec total  | 
                                                        
| 828 | 889 | }  | 
                                                        
| 829 | 890 | |
@@ -835,7 +896,9 @@ discard block  | 
                                                    ||
| 835 | 896 | |
| 836 | 897 | $proxy_user = '';  | 
                                                        
| 837 | 898 | $http_proxy = need_proxy($host);  | 
                                                        
| 838 | - if ($user) $user = urlencode($user[0]) . ":" . urlencode($user[1]);  | 
                                                        |
| 899 | +	if ($user) { | 
                                                        |
| 900 | + $user = urlencode($user[0]) . ":" . urlencode($user[1]);  | 
                                                        |
| 901 | + }  | 
                                                        |
| 839 | 902 | |
| 840 | 903 | $connect = '';  | 
                                                        
| 841 | 904 |  	if ($http_proxy) { | 
                                                        
@@ -851,18 +914,23 @@ discard block  | 
                                                    ||
| 851 | 914 | }  | 
                                                        
| 852 | 915 | $t2 = @parse_url($http_proxy);  | 
                                                        
| 853 | 916 | $first_host = $t2['host'];  | 
                                                        
| 854 | - if (!($port = $t2['port'])) $port = 80;  | 
                                                        |
| 855 | - if ($t2['user'])  | 
                                                        |
| 856 | - $proxy_user = base64_encode($t2['user'] . ":" . $t2['pass']);  | 
                                                        |
| 917 | +		if (!($port = $t2['port'])) { | 
                                                        |
| 918 | + $port = 80;  | 
                                                        |
| 919 | + }  | 
                                                        |
| 920 | +		if ($t2['user']) { | 
                                                        |
| 921 | + $proxy_user = base64_encode($t2['user'] . ":" . $t2['pass']);  | 
                                                        |
| 922 | + }  | 
                                                        |
| 923 | +	} else { | 
                                                        |
| 924 | + $first_host = $noproxy . $host;  | 
                                                        |
| 857 | 925 | }  | 
                                                        
| 858 | - else  | 
                                                        |
| 859 | - $first_host = $noproxy . $host;  | 
                                                        |
| 860 | 926 | |
| 861 | 927 |  	if ($connect){ | 
                                                        
| 862 | 928 |  		$streamContext = stream_context_create(array('ssl' => array('verify_peer' => false, 'allow_self_signed' => true))); | 
                                                        
| 863 | 929 |  		$f = @stream_socket_client("tcp://$first_host:$port", $nError, $sError, _INC_DISTANT_CONNECT_TIMEOUT, STREAM_CLIENT_CONNECT, $streamContext); | 
                                                        
| 864 | 930 |  		spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)","connect"); | 
                                                        
| 865 | - if (!$f) return false;  | 
                                                        |
| 931 | +		if (!$f) { | 
                                                        |
| 932 | + return false;  | 
                                                        |
| 933 | + }  | 
                                                        |
| 866 | 934 | stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        
| 867 | 935 | |
| 868 | 936 | fputs($f, $connect);  | 
                                                        
@@ -880,8 +948,7 @@ discard block  | 
                                                    ||
| 880 | 948 | // envoyer le handshake  | 
                                                        
| 881 | 949 | stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);  | 
                                                        
| 882 | 950 |  		spip_log("OK CONNECT sur $first_host:$port","connect"); | 
                                                        
| 883 | - }  | 
                                                        |
| 884 | -	else { | 
                                                        |
| 951 | +	} else { | 
                                                        |
| 885 | 952 | $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT);  | 
                                                        
| 886 | 953 |  		spip_log("Recuperer $path sur $first_host:$port par $f"); | 
                                                        
| 887 | 954 |  		if (!$f) { |