Completed
Push — spip-3.0 ( 0bbd73...b24c06 )
by cam
06:45
created
ecrire/inc/filtres_mini.php 1 patch
Indentation   +97 added lines, -97 removed lines patch added patch discarded remove patch
@@ -24,16 +24,16 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 ?>
Please login to merge, or discard this patch.
ecrire/inc/distant.php 1 patch
Indentation   +711 added lines, -711 removed lines patch added patch discarded remove patch
@@ -24,8 +24,8 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 
Please login to merge, or discard this patch.