Completed
Push — master ( d9d770...cd354f )
by cam
07:26 queued 02:40
created
ecrire/inc/distant.php 2 patches
Indentation   +1082 added lines, -1082 removed lines patch added patch discarded remove patch
@@ -16,32 +16,32 @@  discard block
 block discarded – undo
16 16
  * @package SPIP\Core\Distant
17 17
  **/
18 18
 if (!defined('_ECRIRE_INC_VERSION')) {
19
-	return;
19
+    return;
20 20
 }
21 21
 
22 22
 if (!defined('_INC_DISTANT_VERSION_HTTP')) {
23
-	define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0');
23
+    define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0');
24 24
 }
25 25
 if (!defined('_INC_DISTANT_CONTENT_ENCODING')) {
26
-	define('_INC_DISTANT_CONTENT_ENCODING', 'gzip');
26
+    define('_INC_DISTANT_CONTENT_ENCODING', 'gzip');
27 27
 }
28 28
 if (!defined('_INC_DISTANT_USER_AGENT')) {
29
-	define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')');
29
+    define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')');
30 30
 }
31 31
 if (!defined('_INC_DISTANT_MAX_SIZE')) {
32
-	define('_INC_DISTANT_MAX_SIZE', 2097152);
32
+    define('_INC_DISTANT_MAX_SIZE', 2097152);
33 33
 }
34 34
 if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) {
35
-	define('_INC_DISTANT_CONNECT_TIMEOUT', 10);
35
+    define('_INC_DISTANT_CONNECT_TIMEOUT', 10);
36 36
 }
37 37
 
38 38
 define('_REGEXP_COPIE_LOCALE', ',' 	.
39
-	preg_replace(
40
-		'@^https?:@',
41
-		'https?:',
42
-		(isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '')
43
-	)
44
-	. '/?spip.php[?]action=acceder_document.*file=(.*)$,');
39
+    preg_replace(
40
+        '@^https?:@',
41
+        'https?:',
42
+        (isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '')
43
+    )
44
+    . '/?spip.php[?]action=acceder_document.*file=(.*)$,');
45 45
 
46 46
 //@define('_COPIE_LOCALE_MAX_SIZE',2097152); // poids (inc/utils l'a fait)
47 47
 
@@ -68,77 +68,77 @@  discard block
 block discarded – undo
68 68
  */
69 69
 function copie_locale($source, $mode = 'auto', $local = null, $taille_max = null) {
70 70
 
71
-	// si c'est la protection de soi-meme, retourner le path
72
-	if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) {
73
-		$source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]);
74
-
75
-		return @file_exists($source) ? $source : false;
76
-	}
77
-
78
-	if (is_null($local)) {
79
-		$local = fichier_copie_locale($source);
80
-	} else {
81
-		if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) {
82
-			$local = substr($local, strlen(_DIR_RACINE));
83
-		}
84
-	}
85
-
86
-	// si $local = '' c'est un fichier refuse par fichier_copie_locale(),
87
-	// par exemple un fichier qui ne figure pas dans nos documents ;
88
-	// dans ce cas on n'essaie pas de le telecharger pour ensuite echouer
89
-	if (!$local) {
90
-		return false;
91
-	}
92
-
93
-	$localrac = _DIR_RACINE . $local;
94
-	$t = ($mode == 'force') ? false : @file_exists($localrac);
95
-
96
-	// test d'existence du fichier
97
-	if ($mode == 'test') {
98
-		return $t ? $local : '';
99
-	}
100
-
101
-	// sinon voir si on doit/peut le telecharger
102
-	if ($local == $source or !tester_url_absolue($source)) {
103
-		return $local;
104
-	}
105
-
106
-	if ($mode == 'modif' or !$t) {
107
-		// passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation
108
-		// et des eventuelles recuperations concurantes
109
-		include_spip('inc/acces');
110
-		if (!$taille_max) {
111
-			$taille_max = _COPIE_LOCALE_MAX_SIZE;
112
-		}
113
-		$res = recuperer_url(
114
-			$source,
115
-			array('file' => $localrac, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '')
116
-		);
117
-		if (!$res or (!$res['length'] and $res['status'] != 304)) {
118
-			spip_log("copie_locale : Echec recuperation $source sur $localrac status : " . $res['status'], 'distant' . _LOG_INFO_IMPORTANTE);
119
-		}
120
-		if (!$res['length']) {
121
-			// si $t c'est sans doute juste un not-modified-since
122
-			return $t ? $local : false;
123
-		}
124
-		spip_log("copie_locale : recuperation $source sur $localrac taille " . $res['length'] . ' OK', 'distant');
125
-
126
-		// pour une eventuelle indexation
127
-		pipeline(
128
-			'post_edition',
129
-			array(
130
-				'args' => array(
131
-					'operation' => 'copie_locale',
132
-					'source' => $source,
133
-					'fichier' => $local,
134
-					'http_res' => $res['length'],
135
-				),
136
-				'data' => null
137
-			)
138
-		);
139
-	}
140
-
141
-	return $local;
71
+    // si c'est la protection de soi-meme, retourner le path
72
+    if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) {
73
+        $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]);
74
+
75
+        return @file_exists($source) ? $source : false;
76
+    }
77
+
78
+    if (is_null($local)) {
79
+        $local = fichier_copie_locale($source);
80
+    } else {
81
+        if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) {
82
+            $local = substr($local, strlen(_DIR_RACINE));
83
+        }
84
+    }
85
+
86
+    // si $local = '' c'est un fichier refuse par fichier_copie_locale(),
87
+    // par exemple un fichier qui ne figure pas dans nos documents ;
88
+    // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer
89
+    if (!$local) {
90
+        return false;
91
+    }
92
+
93
+    $localrac = _DIR_RACINE . $local;
94
+    $t = ($mode == 'force') ? false : @file_exists($localrac);
95
+
96
+    // test d'existence du fichier
97
+    if ($mode == 'test') {
98
+        return $t ? $local : '';
99
+    }
100
+
101
+    // sinon voir si on doit/peut le telecharger
102
+    if ($local == $source or !tester_url_absolue($source)) {
103
+        return $local;
104
+    }
105
+
106
+    if ($mode == 'modif' or !$t) {
107
+        // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation
108
+        // et des eventuelles recuperations concurantes
109
+        include_spip('inc/acces');
110
+        if (!$taille_max) {
111
+            $taille_max = _COPIE_LOCALE_MAX_SIZE;
112
+        }
113
+        $res = recuperer_url(
114
+            $source,
115
+            array('file' => $localrac, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '')
116
+        );
117
+        if (!$res or (!$res['length'] and $res['status'] != 304)) {
118
+            spip_log("copie_locale : Echec recuperation $source sur $localrac status : " . $res['status'], 'distant' . _LOG_INFO_IMPORTANTE);
119
+        }
120
+        if (!$res['length']) {
121
+            // si $t c'est sans doute juste un not-modified-since
122
+            return $t ? $local : false;
123
+        }
124
+        spip_log("copie_locale : recuperation $source sur $localrac taille " . $res['length'] . ' OK', 'distant');
125
+
126
+        // pour une eventuelle indexation
127
+        pipeline(
128
+            'post_edition',
129
+            array(
130
+                'args' => array(
131
+                    'operation' => 'copie_locale',
132
+                    'source' => $source,
133
+                    'fichier' => $local,
134
+                    'http_res' => $res['length'],
135
+                ),
136
+                'data' => null
137
+            )
138
+        );
139
+    }
140
+
141
+    return $local;
142 142
 }
143 143
 
144 144
 /**
@@ -153,88 +153,88 @@  discard block
 block discarded – undo
153 153
  *   url ou false en cas d'echec
154 154
  */
155 155
 function valider_url_distante($url, $known_hosts = array()) {
156
-	if (!function_exists('protocole_verifier')){
157
-		include_spip('inc/filtres_mini');
158
-	}
156
+    if (!function_exists('protocole_verifier')){
157
+        include_spip('inc/filtres_mini');
158
+    }
159 159
 
160
-	if (!protocole_verifier($url, array('http', 'https'))) {
161
-		return false;
162
-	}
160
+    if (!protocole_verifier($url, array('http', 'https'))) {
161
+        return false;
162
+    }
163 163
 	
164
-	$parsed_url = parse_url($url);
165
-	if (!$parsed_url or empty($parsed_url['host']) ) {
166
-		return false;
167
-	}
168
-
169
-	if (isset($parsed_url['user']) or isset($parsed_url['pass'])) {
170
-		return false;
171
-	}
172
-
173
-	if (false !== strpbrk($parsed_url['host'], ':#?[]')) {
174
-		return false;
175
-	}
176
-
177
-	if (!is_array($known_hosts)) {
178
-		$known_hosts = array($known_hosts);
179
-	}
180
-	$known_hosts[] = $GLOBALS['meta']['adresse_site'];
181
-	$known_hosts[] = url_de_base();
182
-	$known_hosts = pipeline('declarer_hosts_distants', $known_hosts);
183
-
184
-	$is_known_host = false;
185
-	foreach ($known_hosts as $known_host) {
186
-		$parse_known = parse_url($known_host);
187
-		if ($parse_known
188
-		  and strtolower($parse_known['host']) === strtolower($parsed_url['host'])) {
189
-			$is_known_host = true;
190
-			break;
191
-		}
192
-	}
193
-
194
-	if (!$is_known_host) {
195
-		$host = trim($parsed_url['host'], '.');
196
-		if (preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host)) {
197
-			$ip = $host;
198
-		} else {
199
-			$ip = gethostbyname($host);
200
-			if ($ip === $host) {
201
-				// Error condition for gethostbyname()
202
-				$ip = false;
203
-			}
204
-		}
205
-		if ($ip) {
206
-			$parts = array_map('intval', explode( '.', $ip ));
207
-			if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0]
208
-			  or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] )
209
-			  or ( 192 === $parts[0] && 168 === $parts[1] )
210
-			) {
211
-				return false;
212
-			}
213
-		}
214
-	}
215
-
216
-	if (empty($parsed_url['port'])) {
217
-		return $url;
218
-	}
219
-
220
-	$port = $parsed_url['port'];
221
-	if ($port === 80  or $port === 443  or $port === 8080) {
222
-		return $url;
223
-	}
224
-
225
-	if ($is_known_host) {
226
-		foreach ($known_hosts as $known_host) {
227
-			$parse_known = parse_url($known_host);
228
-			if ($parse_known
229
-				and !empty($parse_known['port'])
230
-			  and strtolower($parse_known['host']) === strtolower($parsed_url['host'])
231
-			  and $parse_known['port'] == $port) {
232
-				return $url;
233
-			}
234
-		}
235
-	}
236
-
237
-	return false;
164
+    $parsed_url = parse_url($url);
165
+    if (!$parsed_url or empty($parsed_url['host']) ) {
166
+        return false;
167
+    }
168
+
169
+    if (isset($parsed_url['user']) or isset($parsed_url['pass'])) {
170
+        return false;
171
+    }
172
+
173
+    if (false !== strpbrk($parsed_url['host'], ':#?[]')) {
174
+        return false;
175
+    }
176
+
177
+    if (!is_array($known_hosts)) {
178
+        $known_hosts = array($known_hosts);
179
+    }
180
+    $known_hosts[] = $GLOBALS['meta']['adresse_site'];
181
+    $known_hosts[] = url_de_base();
182
+    $known_hosts = pipeline('declarer_hosts_distants', $known_hosts);
183
+
184
+    $is_known_host = false;
185
+    foreach ($known_hosts as $known_host) {
186
+        $parse_known = parse_url($known_host);
187
+        if ($parse_known
188
+          and strtolower($parse_known['host']) === strtolower($parsed_url['host'])) {
189
+            $is_known_host = true;
190
+            break;
191
+        }
192
+    }
193
+
194
+    if (!$is_known_host) {
195
+        $host = trim($parsed_url['host'], '.');
196
+        if (preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host)) {
197
+            $ip = $host;
198
+        } else {
199
+            $ip = gethostbyname($host);
200
+            if ($ip === $host) {
201
+                // Error condition for gethostbyname()
202
+                $ip = false;
203
+            }
204
+        }
205
+        if ($ip) {
206
+            $parts = array_map('intval', explode( '.', $ip ));
207
+            if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0]
208
+              or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] )
209
+              or ( 192 === $parts[0] && 168 === $parts[1] )
210
+            ) {
211
+                return false;
212
+            }
213
+        }
214
+    }
215
+
216
+    if (empty($parsed_url['port'])) {
217
+        return $url;
218
+    }
219
+
220
+    $port = $parsed_url['port'];
221
+    if ($port === 80  or $port === 443  or $port === 8080) {
222
+        return $url;
223
+    }
224
+
225
+    if ($is_known_host) {
226
+        foreach ($known_hosts as $known_host) {
227
+            $parse_known = parse_url($known_host);
228
+            if ($parse_known
229
+                and !empty($parse_known['port'])
230
+              and strtolower($parse_known['host']) === strtolower($parsed_url['host'])
231
+              and $parse_known['port'] == $port) {
232
+                return $url;
233
+            }
234
+        }
235
+    }
236
+
237
+    return false;
238 238
 }
239 239
 
240 240
 /**
@@ -254,86 +254,86 @@  discard block
 block discarded – undo
254 254
  */
255 255
 function prepare_donnees_post($donnees, $boundary = '') {
256 256
 
257
-	// permettre a la fonction qui a demande le post de formater elle meme ses donnees
258
-	// pour un appel soap par exemple
259
-	// l'entete est separe des donnees par un double retour a la ligne
260
-	// on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n
261
-	if (is_string($donnees) && strlen($donnees)) {
262
-		$entete = '';
263
-		// on repasse tous les \r\n et \r en simples \n
264
-		$donnees = str_replace("\r\n", "\n", $donnees);
265
-		$donnees = str_replace("\r", "\n", $donnees);
266
-		// un double retour a la ligne signifie la fin de l'entete et le debut des donnees
267
-		$p = strpos($donnees, "\n\n");
268
-		if ($p !== false) {
269
-			$entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1));
270
-			$donnees = substr($donnees, $p + 2);
271
-		}
272
-		$chaine = str_replace("\n", "\r\n", $donnees);
273
-	} else {
274
-		/* boundary automatique */
275
-		// Si on a plus de 500 octects de donnees, on "boundarise"
276
-		if ($boundary === '') {
277
-			$taille = 0;
278
-			foreach ($donnees as $cle => $valeur) {
279
-				if (is_array($valeur)) {
280
-					foreach ($valeur as $val2) {
281
-						$taille += strlen($val2);
282
-					}
283
-				} else {
284
-					// faut-il utiliser spip_strlen() dans inc/charsets ?
285
-					$taille += strlen($valeur);
286
-				}
287
-			}
288
-			if ($taille > 500) {
289
-				$boundary = substr(md5(rand() . 'spip'), 0, 8);
290
-			}
291
-		}
292
-
293
-		if (is_string($boundary) and strlen($boundary)) {
294
-			// fabrique une chaine HTTP pour un POST avec boundary
295
-			$entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n";
296
-			$chaine = '';
297
-			if (is_array($donnees)) {
298
-				foreach ($donnees as $cle => $valeur) {
299
-					if (is_array($valeur)) {
300
-						foreach ($valeur as $val2) {
301
-							$chaine .= "\r\n--$boundary\r\n";
302
-							$chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n";
303
-							$chaine .= "\r\n";
304
-							$chaine .= $val2;
305
-						}
306
-					} else {
307
-						$chaine .= "\r\n--$boundary\r\n";
308
-						$chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n";
309
-						$chaine .= "\r\n";
310
-						$chaine .= $valeur;
311
-					}
312
-				}
313
-				$chaine .= "\r\n--$boundary\r\n";
314
-			}
315
-		} else {
316
-			// fabrique une chaine HTTP simple pour un POST
317
-			$entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
318
-			$chaine = array();
319
-			if (is_array($donnees)) {
320
-				foreach ($donnees as $cle => $valeur) {
321
-					if (is_array($valeur)) {
322
-						foreach ($valeur as $val2) {
323
-							$chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2);
324
-						}
325
-					} else {
326
-						$chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur);
327
-					}
328
-				}
329
-				$chaine = implode('&', $chaine);
330
-			} else {
331
-				$chaine = $donnees;
332
-			}
333
-		}
334
-	}
335
-
336
-	return array($entete, $chaine);
257
+    // permettre a la fonction qui a demande le post de formater elle meme ses donnees
258
+    // pour un appel soap par exemple
259
+    // l'entete est separe des donnees par un double retour a la ligne
260
+    // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n
261
+    if (is_string($donnees) && strlen($donnees)) {
262
+        $entete = '';
263
+        // on repasse tous les \r\n et \r en simples \n
264
+        $donnees = str_replace("\r\n", "\n", $donnees);
265
+        $donnees = str_replace("\r", "\n", $donnees);
266
+        // un double retour a la ligne signifie la fin de l'entete et le debut des donnees
267
+        $p = strpos($donnees, "\n\n");
268
+        if ($p !== false) {
269
+            $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1));
270
+            $donnees = substr($donnees, $p + 2);
271
+        }
272
+        $chaine = str_replace("\n", "\r\n", $donnees);
273
+    } else {
274
+        /* boundary automatique */
275
+        // Si on a plus de 500 octects de donnees, on "boundarise"
276
+        if ($boundary === '') {
277
+            $taille = 0;
278
+            foreach ($donnees as $cle => $valeur) {
279
+                if (is_array($valeur)) {
280
+                    foreach ($valeur as $val2) {
281
+                        $taille += strlen($val2);
282
+                    }
283
+                } else {
284
+                    // faut-il utiliser spip_strlen() dans inc/charsets ?
285
+                    $taille += strlen($valeur);
286
+                }
287
+            }
288
+            if ($taille > 500) {
289
+                $boundary = substr(md5(rand() . 'spip'), 0, 8);
290
+            }
291
+        }
292
+
293
+        if (is_string($boundary) and strlen($boundary)) {
294
+            // fabrique une chaine HTTP pour un POST avec boundary
295
+            $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n";
296
+            $chaine = '';
297
+            if (is_array($donnees)) {
298
+                foreach ($donnees as $cle => $valeur) {
299
+                    if (is_array($valeur)) {
300
+                        foreach ($valeur as $val2) {
301
+                            $chaine .= "\r\n--$boundary\r\n";
302
+                            $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n";
303
+                            $chaine .= "\r\n";
304
+                            $chaine .= $val2;
305
+                        }
306
+                    } else {
307
+                        $chaine .= "\r\n--$boundary\r\n";
308
+                        $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n";
309
+                        $chaine .= "\r\n";
310
+                        $chaine .= $valeur;
311
+                    }
312
+                }
313
+                $chaine .= "\r\n--$boundary\r\n";
314
+            }
315
+        } else {
316
+            // fabrique une chaine HTTP simple pour un POST
317
+            $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
318
+            $chaine = array();
319
+            if (is_array($donnees)) {
320
+                foreach ($donnees as $cle => $valeur) {
321
+                    if (is_array($valeur)) {
322
+                        foreach ($valeur as $val2) {
323
+                            $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2);
324
+                        }
325
+                    } else {
326
+                        $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur);
327
+                    }
328
+                }
329
+                $chaine = implode('&', $chaine);
330
+            } else {
331
+                $chaine = $donnees;
332
+            }
333
+        }
334
+    }
335
+
336
+    return array($entete, $chaine);
337 337
 }
338 338
 
339 339
 /**
@@ -347,20 +347,20 @@  discard block
 block discarded – undo
347 347
  */
348 348
 function url_to_ascii($url_idn) {
349 349
 
350
-	if ($parts = parse_url($url_idn)) {
351
-		$host = $parts['host'];
352
-		if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) {
353
-			include_spip('inc/idna_convert.class');
354
-			$IDN = new idna_convert();
355
-			$host_ascii = $IDN->encode($host);
356
-			$url_idn = explode($host, $url_idn, 2);
357
-			$url_idn = implode($host_ascii, $url_idn);
358
-		}
359
-		// et on urlencode les char utf si besoin dans le path
360
-		$url_idn = preg_replace_callback('/[^\x20-\x7f]/', function($match) { return urlencode($match[0]); }, $url_idn);
361
-	}
362
-
363
-	return $url_idn;
350
+    if ($parts = parse_url($url_idn)) {
351
+        $host = $parts['host'];
352
+        if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) {
353
+            include_spip('inc/idna_convert.class');
354
+            $IDN = new idna_convert();
355
+            $host_ascii = $IDN->encode($host);
356
+            $url_idn = explode($host, $url_idn, 2);
357
+            $url_idn = implode($host_ascii, $url_idn);
358
+        }
359
+        // et on urlencode les char utf si besoin dans le path
360
+        $url_idn = preg_replace_callback('/[^\x20-\x7f]/', function($match) { return urlencode($match[0]); }, $url_idn);
361
+    }
362
+
363
+    return $url_idn;
364 364
 }
365 365
 
366 366
 /**
@@ -401,196 +401,196 @@  discard block
 block discarded – undo
401 401
  *     string file : nom du fichier si enregistre dans un fichier
402 402
  */
403 403
 function recuperer_url($url, $options = array()) {
404
-	// Conserve la mémoire de la méthode fournit éventuellement
405
-	$methode_demandee = $options['methode'] ?? '';
406
-	$default = array(
407
-		'transcoder' => false,
408
-		'methode' => 'GET',
409
-		'taille_max' => null,
410
-		'headers' => [],
411
-		'datas' => '',
412
-		'boundary' => '',
413
-		'refuser_gz' => false,
414
-		'if_modified_since' => '',
415
-		'uri_referer' => '',
416
-		'file' => '',
417
-		'follow_location' => 10,
418
-		'version_http' => _INC_DISTANT_VERSION_HTTP,
419
-	);
420
-	$options = array_merge($default, $options);
421
-	// copier directement dans un fichier ?
422
-	$copy = $options['file'];
423
-
424
-	if ($options['methode'] == 'HEAD') {
425
-		$options['taille_max'] = 0;
426
-	}
427
-	if (is_null($options['taille_max'])) {
428
-		$options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;
429
-	}
430
-
431
-
432
-	// Ajout des en-têtes spécifiques si besoin
433
-	$head_add = '';
434
-	if (!empty($options['headers'])) {
435
-		foreach ($options['headers'] as $champ => $valeur) {
436
-			$head_add .= $champ . ': ' . $valeur . "\r\n";
437
-		}
438
-		// ne pas le repasser a recuperer_url si on follow un location, car ils seront dans datas
439
-		unset($options['entetes']);
440
-	}
441
-
442
-	if (!empty($options['datas'])) {
443
-		list($head, $postdata) = prepare_donnees_post($options['datas'], $options['boundary']);
444
-		$head .= $head_add;
445
-		if (stripos($head, 'Content-Length:') === false) {
446
-			$head .= 'Content-Length: ' . strlen($postdata) . "\r\n";
447
-		}
448
-		$options['datas'] = $head . "\r\n" . $postdata;
449
-		if (
450
-			strlen($postdata)
451
-			and !$methode_demandee
452
-		) {
453
-			$options['methode'] = 'POST';
454
-		}
455
-	} elseif ($head_add) {
456
-		$options['datas'] = $head_add . "\r\n";
457
-	}
458
-
459
-	// Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole
460
-	$url = preg_replace(',^feed://,i', 'http://', $url);
461
-	if (!tester_url_absolue($url)) {
462
-		$url = 'http://' . $url;
463
-	} elseif (strncmp($url, '//', 2) == 0) {
464
-		$url = 'http:' . $url;
465
-	}
466
-
467
-	$url = url_to_ascii($url);
468
-
469
-	$result = array(
470
-		'status' => 0,
471
-		'headers' => '',
472
-		'page' => '',
473
-		'length' => 0,
474
-		'last_modified' => '',
475
-		'location' => '',
476
-		'url' => $url
477
-	);
478
-
479
-	// si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz
480
-	$refuser_gz = (($options['refuser_gz'] or $copy) ? true : false);
481
-
482
-	// ouvrir la connexion et envoyer la requete et ses en-tetes
483
-	list($handle, $fopen) = init_http(
484
-		$options['methode'],
485
-		$url,
486
-		$refuser_gz,
487
-		$options['uri_referer'],
488
-		$options['datas'],
489
-		$options['version_http'],
490
-		$options['if_modified_since']
491
-	);
492
-	if (!$handle) {
493
-		spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR);
494
-
495
-		return false;
496
-	}
497
-
498
-	// Sauf en fopen, envoyer le flux d'entree
499
-	// et recuperer les en-tetes de reponses
500
-	if (!$fopen) {
501
-		$res = recuperer_entetes_complets($handle, $options['if_modified_since']);
502
-		if (!$res) {
503
-			fclose($handle);
504
-			$t = @parse_url($url);
505
-			$host = $t['host'];
506
-			// Chinoisierie inexplicable pour contrer
507
-			// les actions liberticides de l'empire du milieu
508
-			if (!need_proxy($host)
509
-				and $res = @file_get_contents($url)
510
-			) {
511
-				$result['length'] = strlen($res);
512
-				if ($copy) {
513
-					ecrire_fichier($copy, $res);
514
-					$result['file'] = $copy;
515
-				} else {
516
-					$result['page'] = $res;
517
-				}
518
-				$res = array(
519
-					'status' => 200,
520
-				);
521
-			} else {
522
-				spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR);
523
-				return false;
524
-			}
525
-		} elseif ($res['location'] and $options['follow_location']) {
526
-			$options['follow_location']--;
527
-			fclose($handle);
528
-			include_spip('inc/filtres');
529
-			$url = suivre_lien($url, $res['location']);
530
-			spip_log("recuperer_url recommence sur $url", 'distant');
531
-
532
-			return recuperer_url($url, $options);
533
-		} elseif ($res['status'] !== 200) {
534
-			spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant');
535
-		}
536
-		$result['status'] = $res['status'];
537
-		if (isset($res['headers'])) {
538
-			$result['headers'] = $res['headers'];
539
-		}
540
-		if (isset($res['last_modified'])) {
541
-			$result['last_modified'] = $res['last_modified'];
542
-		}
543
-		if (isset($res['location'])) {
544
-			$result['location'] = $res['location'];
545
-		}
546
-	}
547
-
548
-	// on ne veut que les entetes
549
-	if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') {
550
-		return $result;
551
-	}
552
-
553
-
554
-	// s'il faut deballer, le faire via un fichier temporaire
555
-	// sinon la memoire explose pour les gros flux
556
-
557
-	$gz = false;
558
-	if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) {
559
-		$gz = (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz');
560
-	}
561
-
562
-	// si on a pas deja recuperer le contenu par une methode detournee
563
-	if (!$result['length']) {
564
-		$res = recuperer_body($handle, $options['taille_max'], $gz ? $gz : $copy);
565
-		fclose($handle);
566
-		if ($copy) {
567
-			$result['length'] = $res;
568
-			$result['file'] = $copy;
569
-		} elseif ($res) {
570
-			$result['page'] = &$res;
571
-			$result['length'] = strlen($result['page']);
572
-		}
573
-		if (!$result['status']) {
574
-			$result['status'] = 200; // on a reussi, donc !
575
-		}
576
-	}
577
-	if (!$result['page']) {
578
-		return $result;
579
-	}
580
-
581
-	// Decompresser au besoin
582
-	if ($gz) {
583
-		$result['page'] = implode('', gzfile($gz));
584
-		supprimer_fichier($gz);
585
-	}
586
-
587
-	// Faut-il l'importer dans notre charset local ?
588
-	if ($options['transcoder']) {
589
-		include_spip('inc/charsets');
590
-		$result['page'] = transcoder_page($result['page'], $result['headers']);
591
-	}
592
-
593
-	return $result;
404
+    // Conserve la mémoire de la méthode fournit éventuellement
405
+    $methode_demandee = $options['methode'] ?? '';
406
+    $default = array(
407
+        'transcoder' => false,
408
+        'methode' => 'GET',
409
+        'taille_max' => null,
410
+        'headers' => [],
411
+        'datas' => '',
412
+        'boundary' => '',
413
+        'refuser_gz' => false,
414
+        'if_modified_since' => '',
415
+        'uri_referer' => '',
416
+        'file' => '',
417
+        'follow_location' => 10,
418
+        'version_http' => _INC_DISTANT_VERSION_HTTP,
419
+    );
420
+    $options = array_merge($default, $options);
421
+    // copier directement dans un fichier ?
422
+    $copy = $options['file'];
423
+
424
+    if ($options['methode'] == 'HEAD') {
425
+        $options['taille_max'] = 0;
426
+    }
427
+    if (is_null($options['taille_max'])) {
428
+        $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;
429
+    }
430
+
431
+
432
+    // Ajout des en-têtes spécifiques si besoin
433
+    $head_add = '';
434
+    if (!empty($options['headers'])) {
435
+        foreach ($options['headers'] as $champ => $valeur) {
436
+            $head_add .= $champ . ': ' . $valeur . "\r\n";
437
+        }
438
+        // ne pas le repasser a recuperer_url si on follow un location, car ils seront dans datas
439
+        unset($options['entetes']);
440
+    }
441
+
442
+    if (!empty($options['datas'])) {
443
+        list($head, $postdata) = prepare_donnees_post($options['datas'], $options['boundary']);
444
+        $head .= $head_add;
445
+        if (stripos($head, 'Content-Length:') === false) {
446
+            $head .= 'Content-Length: ' . strlen($postdata) . "\r\n";
447
+        }
448
+        $options['datas'] = $head . "\r\n" . $postdata;
449
+        if (
450
+            strlen($postdata)
451
+            and !$methode_demandee
452
+        ) {
453
+            $options['methode'] = 'POST';
454
+        }
455
+    } elseif ($head_add) {
456
+        $options['datas'] = $head_add . "\r\n";
457
+    }
458
+
459
+    // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole
460
+    $url = preg_replace(',^feed://,i', 'http://', $url);
461
+    if (!tester_url_absolue($url)) {
462
+        $url = 'http://' . $url;
463
+    } elseif (strncmp($url, '//', 2) == 0) {
464
+        $url = 'http:' . $url;
465
+    }
466
+
467
+    $url = url_to_ascii($url);
468
+
469
+    $result = array(
470
+        'status' => 0,
471
+        'headers' => '',
472
+        'page' => '',
473
+        'length' => 0,
474
+        'last_modified' => '',
475
+        'location' => '',
476
+        'url' => $url
477
+    );
478
+
479
+    // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz
480
+    $refuser_gz = (($options['refuser_gz'] or $copy) ? true : false);
481
+
482
+    // ouvrir la connexion et envoyer la requete et ses en-tetes
483
+    list($handle, $fopen) = init_http(
484
+        $options['methode'],
485
+        $url,
486
+        $refuser_gz,
487
+        $options['uri_referer'],
488
+        $options['datas'],
489
+        $options['version_http'],
490
+        $options['if_modified_since']
491
+    );
492
+    if (!$handle) {
493
+        spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR);
494
+
495
+        return false;
496
+    }
497
+
498
+    // Sauf en fopen, envoyer le flux d'entree
499
+    // et recuperer les en-tetes de reponses
500
+    if (!$fopen) {
501
+        $res = recuperer_entetes_complets($handle, $options['if_modified_since']);
502
+        if (!$res) {
503
+            fclose($handle);
504
+            $t = @parse_url($url);
505
+            $host = $t['host'];
506
+            // Chinoisierie inexplicable pour contrer
507
+            // les actions liberticides de l'empire du milieu
508
+            if (!need_proxy($host)
509
+                and $res = @file_get_contents($url)
510
+            ) {
511
+                $result['length'] = strlen($res);
512
+                if ($copy) {
513
+                    ecrire_fichier($copy, $res);
514
+                    $result['file'] = $copy;
515
+                } else {
516
+                    $result['page'] = $res;
517
+                }
518
+                $res = array(
519
+                    'status' => 200,
520
+                );
521
+            } else {
522
+                spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR);
523
+                return false;
524
+            }
525
+        } elseif ($res['location'] and $options['follow_location']) {
526
+            $options['follow_location']--;
527
+            fclose($handle);
528
+            include_spip('inc/filtres');
529
+            $url = suivre_lien($url, $res['location']);
530
+            spip_log("recuperer_url recommence sur $url", 'distant');
531
+
532
+            return recuperer_url($url, $options);
533
+        } elseif ($res['status'] !== 200) {
534
+            spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant');
535
+        }
536
+        $result['status'] = $res['status'];
537
+        if (isset($res['headers'])) {
538
+            $result['headers'] = $res['headers'];
539
+        }
540
+        if (isset($res['last_modified'])) {
541
+            $result['last_modified'] = $res['last_modified'];
542
+        }
543
+        if (isset($res['location'])) {
544
+            $result['location'] = $res['location'];
545
+        }
546
+    }
547
+
548
+    // on ne veut que les entetes
549
+    if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') {
550
+        return $result;
551
+    }
552
+
553
+
554
+    // s'il faut deballer, le faire via un fichier temporaire
555
+    // sinon la memoire explose pour les gros flux
556
+
557
+    $gz = false;
558
+    if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) {
559
+        $gz = (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz');
560
+    }
561
+
562
+    // si on a pas deja recuperer le contenu par une methode detournee
563
+    if (!$result['length']) {
564
+        $res = recuperer_body($handle, $options['taille_max'], $gz ? $gz : $copy);
565
+        fclose($handle);
566
+        if ($copy) {
567
+            $result['length'] = $res;
568
+            $result['file'] = $copy;
569
+        } elseif ($res) {
570
+            $result['page'] = &$res;
571
+            $result['length'] = strlen($result['page']);
572
+        }
573
+        if (!$result['status']) {
574
+            $result['status'] = 200; // on a reussi, donc !
575
+        }
576
+    }
577
+    if (!$result['page']) {
578
+        return $result;
579
+    }
580
+
581
+    // Decompresser au besoin
582
+    if ($gz) {
583
+        $result['page'] = implode('', gzfile($gz));
584
+        supprimer_fichier($gz);
585
+    }
586
+
587
+    // Faut-il l'importer dans notre charset local ?
588
+    if ($options['transcoder']) {
589
+        include_spip('inc/charsets');
590
+        $result['page'] = transcoder_page($result['page'], $result['headers']);
591
+    }
592
+
593
+    return $result;
594 594
 }
595 595
 
596 596
 /**
@@ -606,72 +606,72 @@  discard block
 block discarded – undo
606 606
  * @return array|bool|mixed
607 607
  */
608 608
 function recuperer_url_cache($url, $options = array()) {
609
-	if (!defined('_DELAI_RECUPERER_URL_CACHE')) {
610
-		define('_DELAI_RECUPERER_URL_CACHE', 3600);
611
-	}
612
-	$default = array(
613
-		'transcoder' => false,
614
-		'methode' => 'GET',
615
-		'taille_max' => null,
616
-		'datas' => '',
617
-		'boundary' => '',
618
-		'refuser_gz' => false,
619
-		'if_modified_since' => '',
620
-		'uri_referer' => '',
621
-		'file' => '',
622
-		'follow_location' => 10,
623
-		'version_http' => _INC_DISTANT_VERSION_HTTP,
624
-		'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE,
625
-	);
626
-	$options = array_merge($default, $options);
627
-
628
-	// cas ou il n'est pas possible de cacher
629
-	if (!empty($options['data']) or $options['methode'] == 'POST') {
630
-		return recuperer_url($url, $options);
631
-	}
632
-
633
-	// ne pas tenter plusieurs fois la meme url en erreur (non cachee donc)
634
-	static $errors = array();
635
-	if (isset($errors[$url])) {
636
-		return $errors[$url];
637
-	}
638
-
639
-	$sig = $options;
640
-	unset($sig['if_modified_since']);
641
-	unset($sig['delai_cache']);
642
-	$sig['url'] = $url;
643
-
644
-	$dir = sous_repertoire(_DIR_CACHE, 'curl');
645
-	$cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80);
646
-	$sub = sous_repertoire($dir, substr($cache, 0, 2));
647
-	$cache = "$sub$cache";
648
-
649
-	$res = false;
650
-	$is_cached = file_exists($cache);
651
-	if ($is_cached
652
-		and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache'])
653
-	) {
654
-		lire_fichier($cache, $res);
655
-		if ($res = unserialize($res)) {
656
-			// mettre le last_modified et le status=304 ?
657
-		}
658
-	}
659
-	if (!$res) {
660
-		$res = recuperer_url($url, $options);
661
-		// ne pas recharger cette url non cachee dans le meme hit puisque non disponible
662
-		if (!$res) {
663
-			if ($is_cached) {
664
-				// on a pas reussi a recuperer mais on avait un cache : l'utiliser
665
-				lire_fichier($cache, $res);
666
-				$res = unserialize($res);
667
-			}
668
-
669
-			return $errors[$url] = $res;
670
-		}
671
-		ecrire_fichier($cache, serialize($res));
672
-	}
673
-
674
-	return $res;
609
+    if (!defined('_DELAI_RECUPERER_URL_CACHE')) {
610
+        define('_DELAI_RECUPERER_URL_CACHE', 3600);
611
+    }
612
+    $default = array(
613
+        'transcoder' => false,
614
+        'methode' => 'GET',
615
+        'taille_max' => null,
616
+        'datas' => '',
617
+        'boundary' => '',
618
+        'refuser_gz' => false,
619
+        'if_modified_since' => '',
620
+        'uri_referer' => '',
621
+        'file' => '',
622
+        'follow_location' => 10,
623
+        'version_http' => _INC_DISTANT_VERSION_HTTP,
624
+        'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE,
625
+    );
626
+    $options = array_merge($default, $options);
627
+
628
+    // cas ou il n'est pas possible de cacher
629
+    if (!empty($options['data']) or $options['methode'] == 'POST') {
630
+        return recuperer_url($url, $options);
631
+    }
632
+
633
+    // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc)
634
+    static $errors = array();
635
+    if (isset($errors[$url])) {
636
+        return $errors[$url];
637
+    }
638
+
639
+    $sig = $options;
640
+    unset($sig['if_modified_since']);
641
+    unset($sig['delai_cache']);
642
+    $sig['url'] = $url;
643
+
644
+    $dir = sous_repertoire(_DIR_CACHE, 'curl');
645
+    $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80);
646
+    $sub = sous_repertoire($dir, substr($cache, 0, 2));
647
+    $cache = "$sub$cache";
648
+
649
+    $res = false;
650
+    $is_cached = file_exists($cache);
651
+    if ($is_cached
652
+        and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache'])
653
+    ) {
654
+        lire_fichier($cache, $res);
655
+        if ($res = unserialize($res)) {
656
+            // mettre le last_modified et le status=304 ?
657
+        }
658
+    }
659
+    if (!$res) {
660
+        $res = recuperer_url($url, $options);
661
+        // ne pas recharger cette url non cachee dans le meme hit puisque non disponible
662
+        if (!$res) {
663
+            if ($is_cached) {
664
+                // on a pas reussi a recuperer mais on avait un cache : l'utiliser
665
+                lire_fichier($cache, $res);
666
+                $res = unserialize($res);
667
+            }
668
+
669
+            return $errors[$url] = $res;
670
+        }
671
+        ecrire_fichier($cache, serialize($res));
672
+    }
673
+
674
+    return $res;
675 675
 }
676 676
 
677 677
 /**
@@ -710,52 +710,52 @@  discard block
 block discarded – undo
710 710
  *     - false si la page n'a pu être récupérée (status different de 200)
711 711
  **/
712 712
 function recuperer_page(
713
-	$url,
714
-	$trans = false,
715
-	$get_headers = false,
716
-	$taille_max = null,
717
-	$datas = '',
718
-	$boundary = '',
719
-	$refuser_gz = false,
720
-	$date_verif = '',
721
-	$uri_referer = ''
713
+    $url,
714
+    $trans = false,
715
+    $get_headers = false,
716
+    $taille_max = null,
717
+    $datas = '',
718
+    $boundary = '',
719
+    $refuser_gz = false,
720
+    $date_verif = '',
721
+    $uri_referer = ''
722 722
 ) {
723
-	// $copy = copier le fichier ?
724
-	$copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-)
725
-
726
-	if (!is_null($taille_max) and ($taille_max == 0)) {
727
-		$get = 'HEAD';
728
-	} else {
729
-		$get = 'GET';
730
-	}
731
-
732
-	$options = array(
733
-		'transcoder' => $trans === true,
734
-		'methode' => $get,
735
-		'datas' => $datas,
736
-		'boundary' => $boundary,
737
-		'refuser_gz' => $refuser_gz,
738
-		'if_modified_since' => $date_verif,
739
-		'uri_referer' => $uri_referer,
740
-		'file' => $copy ? $trans : '',
741
-		'follow_location' => 10,
742
-	);
743
-	if (!is_null($taille_max)) {
744
-		$options['taille_max'] = $taille_max;
745
-	}
746
-	// dix tentatives maximum en cas d'entetes 301...
747
-	$res = recuperer_url($url, $options);
748
-	if (!$res) {
749
-		return false;
750
-	}
751
-	if ($res['status'] !== 200) {
752
-		return false;
753
-	}
754
-	if ($get_headers) {
755
-		return $res['headers'] . "\n" . $res['page'];
756
-	}
757
-
758
-	return $res['page'];
723
+    // $copy = copier le fichier ?
724
+    $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-)
725
+
726
+    if (!is_null($taille_max) and ($taille_max == 0)) {
727
+        $get = 'HEAD';
728
+    } else {
729
+        $get = 'GET';
730
+    }
731
+
732
+    $options = array(
733
+        'transcoder' => $trans === true,
734
+        'methode' => $get,
735
+        'datas' => $datas,
736
+        'boundary' => $boundary,
737
+        'refuser_gz' => $refuser_gz,
738
+        'if_modified_since' => $date_verif,
739
+        'uri_referer' => $uri_referer,
740
+        'file' => $copy ? $trans : '',
741
+        'follow_location' => 10,
742
+    );
743
+    if (!is_null($taille_max)) {
744
+        $options['taille_max'] = $taille_max;
745
+    }
746
+    // dix tentatives maximum en cas d'entetes 301...
747
+    $res = recuperer_url($url, $options);
748
+    if (!$res) {
749
+        return false;
750
+    }
751
+    if ($res['status'] !== 200) {
752
+        return false;
753
+    }
754
+    if ($get_headers) {
755
+        return $res['headers'] . "\n" . $res['page'];
756
+    }
757
+
758
+    return $res['page'];
759 759
 }
760 760
 
761 761
 
@@ -792,48 +792,48 @@  discard block
 block discarded – undo
792 792
  *     - false sinon
793 793
  **/
794 794
 function recuperer_lapage(
795
-	$url,
796
-	$trans = false,
797
-	$get = 'GET',
798
-	$taille_max = 1048576,
799
-	$datas = '',
800
-	$refuser_gz = false,
801
-	$date_verif = '',
802
-	$uri_referer = ''
795
+    $url,
796
+    $trans = false,
797
+    $get = 'GET',
798
+    $taille_max = 1048576,
799
+    $datas = '',
800
+    $refuser_gz = false,
801
+    $date_verif = '',
802
+    $uri_referer = ''
803 803
 ) {
804
-	// $copy = copier le fichier ?
805
-	$copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-)
806
-
807
-	// si on ecrit directement dans un fichier, pour ne pas manipuler
808
-	// en memoire refuser gz
809
-	if ($copy) {
810
-		$refuser_gz = true;
811
-	}
812
-
813
-	$options = array(
814
-		'transcoder' => $trans === true,
815
-		'methode' => $get,
816
-		'datas' => $datas,
817
-		'refuser_gz' => $refuser_gz,
818
-		'if_modified_since' => $date_verif,
819
-		'uri_referer' => $uri_referer,
820
-		'file' => $copy ? $trans : '',
821
-		'follow_location' => false,
822
-	);
823
-	if (!is_null($taille_max)) {
824
-		$options['taille_max'] = $taille_max;
825
-	}
826
-	// dix tentatives maximum en cas d'entetes 301...
827
-	$res = recuperer_url($url, $options);
828
-
829
-	if (!$res) {
830
-		return false;
831
-	}
832
-	if ($res['status'] !== 200) {
833
-		return false;
834
-	}
835
-
836
-	return array($res['headers'], $res['page']);
804
+    // $copy = copier le fichier ?
805
+    $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-)
806
+
807
+    // si on ecrit directement dans un fichier, pour ne pas manipuler
808
+    // en memoire refuser gz
809
+    if ($copy) {
810
+        $refuser_gz = true;
811
+    }
812
+
813
+    $options = array(
814
+        'transcoder' => $trans === true,
815
+        'methode' => $get,
816
+        'datas' => $datas,
817
+        'refuser_gz' => $refuser_gz,
818
+        'if_modified_since' => $date_verif,
819
+        'uri_referer' => $uri_referer,
820
+        'file' => $copy ? $trans : '',
821
+        'follow_location' => false,
822
+    );
823
+    if (!is_null($taille_max)) {
824
+        $options['taille_max'] = $taille_max;
825
+    }
826
+    // dix tentatives maximum en cas d'entetes 301...
827
+    $res = recuperer_url($url, $options);
828
+
829
+    if (!$res) {
830
+        return false;
831
+    }
832
+    if ($res['status'] !== 200) {
833
+        return false;
834
+    }
835
+
836
+    return array($res['headers'], $res['page']);
837 837
 }
838 838
 
839 839
 /**
@@ -851,41 +851,41 @@  discard block
 block discarded – undo
851 851
  *   string contenu de la resource
852 852
  */
853 853
 function recuperer_body($handle, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = '') {
854
-	$taille = 0;
855
-	$result = '';
856
-	$fp = false;
857
-	if ($fichier) {
858
-		include_spip('inc/acces');
859
-		$tmpfile = "$fichier." . creer_uniqid() . '.tmp';
860
-		$fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX);
861
-		if (!$fp and file_exists($fichier)) {
862
-			return filesize($fichier);
863
-		}
864
-		if (!$fp) {
865
-			return false;
866
-		}
867
-		$result = 0; // on renvoie la taille du fichier
868
-	}
869
-	while (!feof($handle) and $taille < $taille_max) {
870
-		$res = fread($handle, 16384);
871
-		$taille += strlen($res);
872
-		if ($fp) {
873
-			fwrite($fp, $res);
874
-			$result = $taille;
875
-		} else {
876
-			$result .= $res;
877
-		}
878
-	}
879
-	if ($fp) {
880
-		spip_fclose_unlock($fp);
881
-		spip_unlink($fichier);
882
-		@rename($tmpfile, $fichier);
883
-		if (!file_exists($fichier)) {
884
-			return false;
885
-		}
886
-	}
887
-
888
-	return $result;
854
+    $taille = 0;
855
+    $result = '';
856
+    $fp = false;
857
+    if ($fichier) {
858
+        include_spip('inc/acces');
859
+        $tmpfile = "$fichier." . creer_uniqid() . '.tmp';
860
+        $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX);
861
+        if (!$fp and file_exists($fichier)) {
862
+            return filesize($fichier);
863
+        }
864
+        if (!$fp) {
865
+            return false;
866
+        }
867
+        $result = 0; // on renvoie la taille du fichier
868
+    }
869
+    while (!feof($handle) and $taille < $taille_max) {
870
+        $res = fread($handle, 16384);
871
+        $taille += strlen($res);
872
+        if ($fp) {
873
+            fwrite($fp, $res);
874
+            $result = $taille;
875
+        } else {
876
+            $result .= $res;
877
+        }
878
+    }
879
+    if ($fp) {
880
+        spip_fclose_unlock($fp);
881
+        spip_unlink($fichier);
882
+        @rename($tmpfile, $fichier);
883
+        if (!file_exists($fichier)) {
884
+            return false;
885
+        }
886
+    }
887
+
888
+    return $result;
889 889
 }
890 890
 
891 891
 /**
@@ -907,34 +907,34 @@  discard block
 block discarded – undo
907 907
  *   string location
908 908
  */
909 909
 function recuperer_entetes_complets($handle, $if_modified_since = false) {
910
-	$result = array('status' => 0, 'headers' => array(), 'last_modified' => 0, 'location' => '');
911
-
912
-	$s = @trim(fgets($handle, 16384));
913
-	if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) {
914
-		return false;
915
-	}
916
-	$result['status'] = intval($r[1]);
917
-	while ($s = trim(fgets($handle, 16384))) {
918
-		$result['headers'][] = $s . "\n";
919
-		preg_match(',^([^:]*): *(.*)$,i', $s, $r);
920
-		list(, $d, $v) = $r;
921
-		if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) {
922
-			$result['location'] = $v;
923
-		} elseif ($d == 'Last-Modified') {
924
-			$result['last_modified'] = strtotime($v);
925
-		}
926
-	}
927
-	if ($if_modified_since
928
-		and $result['last_modified']
929
-		and $if_modified_since > $result['last_modified']
930
-		and $result['status'] == 200
931
-	) {
932
-		$result['status'] = 304;
933
-	}
934
-
935
-	$result['headers'] = implode('', $result['headers']);
936
-
937
-	return $result;
910
+    $result = array('status' => 0, 'headers' => array(), 'last_modified' => 0, 'location' => '');
911
+
912
+    $s = @trim(fgets($handle, 16384));
913
+    if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) {
914
+        return false;
915
+    }
916
+    $result['status'] = intval($r[1]);
917
+    while ($s = trim(fgets($handle, 16384))) {
918
+        $result['headers'][] = $s . "\n";
919
+        preg_match(',^([^:]*): *(.*)$,i', $s, $r);
920
+        list(, $d, $v) = $r;
921
+        if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) {
922
+            $result['location'] = $v;
923
+        } elseif ($d == 'Last-Modified') {
924
+            $result['last_modified'] = strtotime($v);
925
+        }
926
+    }
927
+    if ($if_modified_since
928
+        and $result['last_modified']
929
+        and $if_modified_since > $result['last_modified']
930
+        and $result['status'] == 200
931
+    ) {
932
+        $result['status'] = 304;
933
+    }
934
+
935
+    $result['headers'] = implode('', $result['headers']);
936
+
937
+    return $result;
938 938
 }
939 939
 
940 940
 /**
@@ -956,22 +956,22 @@  discard block
 block discarded – undo
956 956
  *     Nom du fichier pour copie locale
957 957
  **/
958 958
 function nom_fichier_copie_locale($source, $extension) {
959
-	include_spip('inc/documents');
959
+    include_spip('inc/documents');
960 960
 
961
-	$d = creer_repertoire_documents('distant'); # IMG/distant/
962
-	$d = sous_repertoire($d, $extension); # IMG/distant/pdf/
961
+    $d = creer_repertoire_documents('distant'); # IMG/distant/
962
+    $d = sous_repertoire($d, $extension); # IMG/distant/pdf/
963 963
 
964
-	// on se place tout le temps comme si on etait a la racine
965
-	if (_DIR_RACINE) {
966
-		$d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d);
967
-	}
964
+    // on se place tout le temps comme si on etait a la racine
965
+    if (_DIR_RACINE) {
966
+        $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d);
967
+    }
968 968
 
969
-	$m = md5($source);
969
+    $m = md5($source);
970 970
 
971
-	return $d
972
-	. substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12)
973
-	. substr($m, 0, 4)
974
-	. ".$extension";
971
+    return $d
972
+    . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12)
973
+    . substr($m, 0, 4)
974
+    . ".$extension";
975 975
 }
976 976
 
977 977
 /**
@@ -989,68 +989,68 @@  discard block
 block discarded – undo
989 989
  *      Nom du fichier calculé
990 990
  **/
991 991
 function fichier_copie_locale($source) {
992
-	// Si c'est deja local pas de souci
993
-	if (!tester_url_absolue($source)) {
994
-		if (_DIR_RACINE) {
995
-			$source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source);
996
-		}
997
-
998
-		return $source;
999
-	}
1000
-
1001
-	// optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier
1002
-	// a deja ete copie en local avec cette extension
1003
-	// dans ce cas elle est fiable, pas la peine de requeter en base
1004
-	$path_parts = pathinfo($source);
1005
-	if (!isset($path_parts['extension'])) {
1006
-		$path_parts['extension'] = '';
1007
-	}
1008
-	$ext = $path_parts ? $path_parts['extension'] : '';
1009
-	if ($ext
1010
-		and preg_match(',^\w+$,', $ext) // pas de php?truc=1&...
1011
-		and $f = nom_fichier_copie_locale($source, $ext)
1012
-		and file_exists(_DIR_RACINE . $f)
1013
-	) {
1014
-		return $f;
1015
-	}
1016
-
1017
-
1018
-	// Si c'est deja dans la table des documents,
1019
-	// ramener le nom de sa copie potentielle
1020
-	$ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''");
1021
-
1022
-	if ($ext) {
1023
-		return nom_fichier_copie_locale($source, $ext);
1024
-	}
1025
-
1026
-	// voir si l'extension indiquee dans le nom du fichier est ok
1027
-	// et si il n'aurait pas deja ete rapatrie
1028
-
1029
-	$ext = $path_parts ? $path_parts['extension'] : '';
1030
-
1031
-	if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
1032
-		$f = nom_fichier_copie_locale($source, $ext);
1033
-		if (file_exists(_DIR_RACINE . $f)) {
1034
-			return $f;
1035
-		}
1036
-	}
1037
-
1038
-	// Ping  pour voir si son extension est connue et autorisee
1039
-	// avec mise en cache du resultat du ping
1040
-
1041
-	$cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source);
1042
-	if (!@file_exists($cache)
1043
-		or !$path_parts = @unserialize(spip_file_get_contents($cache))
1044
-		or _request('var_mode') == 'recalcul'
1045
-	) {
1046
-		$path_parts = recuperer_infos_distantes($source, 0, false);
1047
-		ecrire_fichier($cache, serialize($path_parts));
1048
-	}
1049
-	$ext = !empty($path_parts['extension']) ? $path_parts['extension'] : '';
1050
-	if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
1051
-		return nom_fichier_copie_locale($source, $ext);
1052
-	}
1053
-	spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR);
992
+    // Si c'est deja local pas de souci
993
+    if (!tester_url_absolue($source)) {
994
+        if (_DIR_RACINE) {
995
+            $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source);
996
+        }
997
+
998
+        return $source;
999
+    }
1000
+
1001
+    // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier
1002
+    // a deja ete copie en local avec cette extension
1003
+    // dans ce cas elle est fiable, pas la peine de requeter en base
1004
+    $path_parts = pathinfo($source);
1005
+    if (!isset($path_parts['extension'])) {
1006
+        $path_parts['extension'] = '';
1007
+    }
1008
+    $ext = $path_parts ? $path_parts['extension'] : '';
1009
+    if ($ext
1010
+        and preg_match(',^\w+$,', $ext) // pas de php?truc=1&...
1011
+        and $f = nom_fichier_copie_locale($source, $ext)
1012
+        and file_exists(_DIR_RACINE . $f)
1013
+    ) {
1014
+        return $f;
1015
+    }
1016
+
1017
+
1018
+    // Si c'est deja dans la table des documents,
1019
+    // ramener le nom de sa copie potentielle
1020
+    $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''");
1021
+
1022
+    if ($ext) {
1023
+        return nom_fichier_copie_locale($source, $ext);
1024
+    }
1025
+
1026
+    // voir si l'extension indiquee dans le nom du fichier est ok
1027
+    // et si il n'aurait pas deja ete rapatrie
1028
+
1029
+    $ext = $path_parts ? $path_parts['extension'] : '';
1030
+
1031
+    if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
1032
+        $f = nom_fichier_copie_locale($source, $ext);
1033
+        if (file_exists(_DIR_RACINE . $f)) {
1034
+            return $f;
1035
+        }
1036
+    }
1037
+
1038
+    // Ping  pour voir si son extension est connue et autorisee
1039
+    // avec mise en cache du resultat du ping
1040
+
1041
+    $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source);
1042
+    if (!@file_exists($cache)
1043
+        or !$path_parts = @unserialize(spip_file_get_contents($cache))
1044
+        or _request('var_mode') == 'recalcul'
1045
+    ) {
1046
+        $path_parts = recuperer_infos_distantes($source, 0, false);
1047
+        ecrire_fichier($cache, serialize($path_parts));
1048
+    }
1049
+    $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : '';
1050
+    if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
1051
+        return nom_fichier_copie_locale($source, $ext);
1052
+    }
1053
+    spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR);
1054 1054
 }
1055 1055
 
1056 1056
 
@@ -1078,146 +1078,146 @@  discard block
 block discarded – undo
1078 1078
  **/
1079 1079
 function recuperer_infos_distantes($source, $max = 0, $charger_si_petite_image = true) {
1080 1080
 
1081
-	// pas la peine de perdre son temps
1082
-	if (!tester_url_absolue($source)) {
1083
-		return false;
1084
-	}
1085
-
1086
-	# charger les alias des types mime
1087
-	include_spip('base/typedoc');
1088
-
1089
-	$a = array();
1090
-	$mime_type = '';
1091
-	// On va directement charger le debut des images et des fichiers html,
1092
-	// de maniere a attrapper le maximum d'infos (titre, taille, etc). Si
1093
-	// ca echoue l'utilisateur devra les entrer...
1094
-	$reponse = recuperer_url($source, ['taille_max' => $max, 'refuser_gz' => true]);
1095
-	$headers = $reponse['headers'] ?? '';
1096
-	$a['body'] = $reponse['page'] ?? '';
1097
-	if ($headers) {
1098
-		if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) {
1099
-			$mime_type = (trim($regs[1]));
1100
-		} else {
1101
-			$mime_type = '';
1102
-		} // inconnu
1103
-
1104
-		// Appliquer les alias
1105
-		while (isset($GLOBALS['mime_alias'][$mime_type])) {
1106
-			$mime_type = $GLOBALS['mime_alias'][$mime_type];
1107
-		}
1108
-
1109
-		// Si on a un mime-type insignifiant
1110
-		// text/plain,application/octet-stream ou vide
1111
-		// c'est peut-etre que le serveur ne sait pas
1112
-		// ce qu'il sert ; on va tenter de detecter via l'extension de l'url
1113
-		// ou le Content-Disposition: attachment; filename=...
1114
-		$t = null;
1115
-		if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))) {
1116
-			if (!$t
1117
-				and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)
1118
-			) {
1119
-				$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text'));
1120
-			}
1121
-			if (!$t
1122
-				and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m)
1123
-				and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext)
1124
-			) {
1125
-				$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text'));
1126
-			}
1127
-		}
1128
-
1129
-		// Autre mime/type (ou text/plain avec fichier d'extension inconnue)
1130
-		if (!$t) {
1131
-			$t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type));
1132
-		}
1133
-
1134
-		// Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)
1135
-		// On essaie de nouveau avec l'extension
1136
-		if (!$t
1137
-			and $mime_type != 'text/plain'
1138
-			and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)
1139
-		) {
1140
-			# eviter xxx.3 => 3gp (> SPIP 3)
1141
-			$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text'));
1142
-		}
1143
-
1144
-		if ($t) {
1145
-			spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant');
1146
-			$a['extension'] = $t['extension'];
1147
-		} else {
1148
-			# par defaut on retombe sur '.bin' si c'est autorise
1149
-			spip_log("mime-type $mime_type inconnu", 'distant');
1150
-			$t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'");
1151
-			if (!$t) {
1152
-				return false;
1153
-			}
1154
-			$a['extension'] = $t['extension'];
1155
-		}
1156
-
1157
-		if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) {
1158
-			$a['taille'] = intval($regs[1]);
1159
-		}
1160
-	}
1161
-
1162
-	// Echec avec HEAD, on tente avec GET
1163
-	if (!$a and !$max) {
1164
-		spip_log("tenter GET $source", 'distant');
1165
-		$a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);
1166
-	}
1167
-
1168
-	// si on a rien trouve pas la peine d'insister
1169
-	if (!$a) {
1170
-		return false;
1171
-	}
1172
-
1173
-	// S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller
1174
-	// recharger le document en GET et recuperer des donnees supplementaires...
1175
-	include_spip('inc/filtres_images_lib_mini');
1176
-	if (strpos($mime_type, "image/") === 0
1177
-	  and $extension = _image_trouver_extension_depuis_mime($mime_type)) {
1178
-		if ($max == 0
1179
-			and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE)
1180
-			and in_array($extension, formats_image_acceptables())
1181
-			and $charger_si_petite_image
1182
-		) {
1183
-			$a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);
1184
-		} else {
1185
-			if ($a['body']) {
1186
-				$a['extension'] = $extension;
1187
-				$a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension);
1188
-				ecrire_fichier($a['fichier'], $a['body']);
1189
-				$size_image = @spip_getimagesize($a['fichier']);
1190
-				$a['largeur'] = intval($size_image[0]);
1191
-				$a['hauteur'] = intval($size_image[1]);
1192
-				$a['type_image'] = true;
1193
-			}
1194
-		}
1195
-	}
1196
-
1197
-	// Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut
1198
-	// ce sera mieux que 0x0
1199
-	// Flash is dead!
1200
-	if ($a and isset($a['extension']) and $a['extension'] == 'swf'
1201
-		and empty($a['largeur'])
1202
-	) {
1203
-		$a['largeur'] = 425;
1204
-		$a['hauteur'] = 350;
1205
-	}
1206
-
1207
-	if ($mime_type == 'text/html') {
1208
-		include_spip('inc/filtres');
1209
-		$page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]);
1210
-		$page = $page['page'] ?? '';
1211
-		if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) {
1212
-			$a['titre'] = corriger_caracteres(trim($regs[1]));
1213
-		}
1214
-		if (!isset($a['taille']) or !$a['taille']) {
1215
-			$a['taille'] = strlen($page); # a peu pres
1216
-		}
1217
-	}
1218
-	$a['mime_type'] = $mime_type;
1219
-
1220
-	return $a;
1081
+    // pas la peine de perdre son temps
1082
+    if (!tester_url_absolue($source)) {
1083
+        return false;
1084
+    }
1085
+
1086
+    # charger les alias des types mime
1087
+    include_spip('base/typedoc');
1088
+
1089
+    $a = array();
1090
+    $mime_type = '';
1091
+    // On va directement charger le debut des images et des fichiers html,
1092
+    // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si
1093
+    // ca echoue l'utilisateur devra les entrer...
1094
+    $reponse = recuperer_url($source, ['taille_max' => $max, 'refuser_gz' => true]);
1095
+    $headers = $reponse['headers'] ?? '';
1096
+    $a['body'] = $reponse['page'] ?? '';
1097
+    if ($headers) {
1098
+        if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) {
1099
+            $mime_type = (trim($regs[1]));
1100
+        } else {
1101
+            $mime_type = '';
1102
+        } // inconnu
1103
+
1104
+        // Appliquer les alias
1105
+        while (isset($GLOBALS['mime_alias'][$mime_type])) {
1106
+            $mime_type = $GLOBALS['mime_alias'][$mime_type];
1107
+        }
1108
+
1109
+        // Si on a un mime-type insignifiant
1110
+        // text/plain,application/octet-stream ou vide
1111
+        // c'est peut-etre que le serveur ne sait pas
1112
+        // ce qu'il sert ; on va tenter de detecter via l'extension de l'url
1113
+        // ou le Content-Disposition: attachment; filename=...
1114
+        $t = null;
1115
+        if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))) {
1116
+            if (!$t
1117
+                and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)
1118
+            ) {
1119
+                $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text'));
1120
+            }
1121
+            if (!$t
1122
+                and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m)
1123
+                and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext)
1124
+            ) {
1125
+                $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text'));
1126
+            }
1127
+        }
1128
+
1129
+        // Autre mime/type (ou text/plain avec fichier d'extension inconnue)
1130
+        if (!$t) {
1131
+            $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type));
1132
+        }
1133
+
1134
+        // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)
1135
+        // On essaie de nouveau avec l'extension
1136
+        if (!$t
1137
+            and $mime_type != 'text/plain'
1138
+            and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)
1139
+        ) {
1140
+            # eviter xxx.3 => 3gp (> SPIP 3)
1141
+            $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text'));
1142
+        }
1143
+
1144
+        if ($t) {
1145
+            spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant');
1146
+            $a['extension'] = $t['extension'];
1147
+        } else {
1148
+            # par defaut on retombe sur '.bin' si c'est autorise
1149
+            spip_log("mime-type $mime_type inconnu", 'distant');
1150
+            $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'");
1151
+            if (!$t) {
1152
+                return false;
1153
+            }
1154
+            $a['extension'] = $t['extension'];
1155
+        }
1156
+
1157
+        if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) {
1158
+            $a['taille'] = intval($regs[1]);
1159
+        }
1160
+    }
1161
+
1162
+    // Echec avec HEAD, on tente avec GET
1163
+    if (!$a and !$max) {
1164
+        spip_log("tenter GET $source", 'distant');
1165
+        $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);
1166
+    }
1167
+
1168
+    // si on a rien trouve pas la peine d'insister
1169
+    if (!$a) {
1170
+        return false;
1171
+    }
1172
+
1173
+    // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller
1174
+    // recharger le document en GET et recuperer des donnees supplementaires...
1175
+    include_spip('inc/filtres_images_lib_mini');
1176
+    if (strpos($mime_type, "image/") === 0
1177
+      and $extension = _image_trouver_extension_depuis_mime($mime_type)) {
1178
+        if ($max == 0
1179
+            and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE)
1180
+            and in_array($extension, formats_image_acceptables())
1181
+            and $charger_si_petite_image
1182
+        ) {
1183
+            $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);
1184
+        } else {
1185
+            if ($a['body']) {
1186
+                $a['extension'] = $extension;
1187
+                $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension);
1188
+                ecrire_fichier($a['fichier'], $a['body']);
1189
+                $size_image = @spip_getimagesize($a['fichier']);
1190
+                $a['largeur'] = intval($size_image[0]);
1191
+                $a['hauteur'] = intval($size_image[1]);
1192
+                $a['type_image'] = true;
1193
+            }
1194
+        }
1195
+    }
1196
+
1197
+    // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut
1198
+    // ce sera mieux que 0x0
1199
+    // Flash is dead!
1200
+    if ($a and isset($a['extension']) and $a['extension'] == 'swf'
1201
+        and empty($a['largeur'])
1202
+    ) {
1203
+        $a['largeur'] = 425;
1204
+        $a['hauteur'] = 350;
1205
+    }
1206
+
1207
+    if ($mime_type == 'text/html') {
1208
+        include_spip('inc/filtres');
1209
+        $page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]);
1210
+        $page = $page['page'] ?? '';
1211
+        if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) {
1212
+            $a['titre'] = corriger_caracteres(trim($regs[1]));
1213
+        }
1214
+        if (!isset($a['taille']) or !$a['taille']) {
1215
+            $a['taille'] = strlen($page); # a peu pres
1216
+        }
1217
+    }
1218
+    $a['mime_type'] = $mime_type;
1219
+
1220
+    return $a;
1221 1221
 }
1222 1222
 
1223 1223
 
@@ -1233,45 +1233,45 @@  discard block
 block discarded – undo
1233 1233
  * @return string
1234 1234
  */
1235 1235
 function need_proxy($host, $http_proxy = null, $http_noproxy = null) {
1236
-	if (is_null($http_proxy)) {
1237
-		$http_proxy = isset($GLOBALS['meta']['http_proxy']) ? $GLOBALS['meta']['http_proxy'] : null;
1238
-	}
1239
-	// rien a faire si pas de proxy :)
1240
-	if (is_null($http_proxy) or !$http_proxy = trim($http_proxy)) {
1241
-		return '';
1242
-	}
1243
-
1244
-	if (is_null($http_noproxy)) {
1245
-		$http_noproxy = isset($GLOBALS['meta']['http_noproxy']) ? $GLOBALS['meta']['http_noproxy'] : null;
1246
-	}
1247
-	// si pas d'exception, on retourne le proxy
1248
-	if (is_null($http_noproxy) or !$http_noproxy = trim($http_noproxy)) {
1249
-		return $http_proxy;
1250
-	}
1251
-
1252
-	// si le host ou l'un des domaines parents est dans $http_noproxy on fait exception
1253
-	// $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne
1254
-	$http_noproxy = str_replace("\n", " ", $http_noproxy);
1255
-	$http_noproxy = str_replace("\r", " ", $http_noproxy);
1256
-	$http_noproxy = " $http_noproxy ";
1257
-	$domain = $host;
1258
-	// si le domaine exact www.example.org est dans les exceptions
1259
-	if (strpos($http_noproxy, " $domain ") !== false)
1260
-		return '';
1261
-
1262
-	while (strpos($domain, '.') !== false) {
1263
-		$domain = explode('.', $domain);
1264
-		array_shift($domain);
1265
-		$domain = implode('.', $domain);
1266
-
1267
-		// ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines)
1268
-		if (strpos($http_noproxy, " .$domain ") !== false) {
1269
-			return '';
1270
-		}
1271
-	}
1272
-
1273
-	// ok c'est pas une exception
1274
-	return $http_proxy;
1236
+    if (is_null($http_proxy)) {
1237
+        $http_proxy = isset($GLOBALS['meta']['http_proxy']) ? $GLOBALS['meta']['http_proxy'] : null;
1238
+    }
1239
+    // rien a faire si pas de proxy :)
1240
+    if (is_null($http_proxy) or !$http_proxy = trim($http_proxy)) {
1241
+        return '';
1242
+    }
1243
+
1244
+    if (is_null($http_noproxy)) {
1245
+        $http_noproxy = isset($GLOBALS['meta']['http_noproxy']) ? $GLOBALS['meta']['http_noproxy'] : null;
1246
+    }
1247
+    // si pas d'exception, on retourne le proxy
1248
+    if (is_null($http_noproxy) or !$http_noproxy = trim($http_noproxy)) {
1249
+        return $http_proxy;
1250
+    }
1251
+
1252
+    // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception
1253
+    // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne
1254
+    $http_noproxy = str_replace("\n", " ", $http_noproxy);
1255
+    $http_noproxy = str_replace("\r", " ", $http_noproxy);
1256
+    $http_noproxy = " $http_noproxy ";
1257
+    $domain = $host;
1258
+    // si le domaine exact www.example.org est dans les exceptions
1259
+    if (strpos($http_noproxy, " $domain ") !== false)
1260
+        return '';
1261
+
1262
+    while (strpos($domain, '.') !== false) {
1263
+        $domain = explode('.', $domain);
1264
+        array_shift($domain);
1265
+        $domain = implode('.', $domain);
1266
+
1267
+        // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines)
1268
+        if (strpos($http_noproxy, " .$domain ") !== false) {
1269
+            return '';
1270
+        }
1271
+    }
1272
+
1273
+    // ok c'est pas une exception
1274
+    return $http_proxy;
1275 1275
 }
1276 1276
 
1277 1277
 
@@ -1294,58 +1294,58 @@  discard block
 block discarded – undo
1294 1294
  * @return array
1295 1295
  */
1296 1296
 function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = '', $vers = 'HTTP/1.0', $date = '') {
1297
-	$user = $via_proxy = $proxy_user = '';
1298
-	$fopen = false;
1299
-
1300
-	$t = @parse_url($url);
1301
-	$host = $t['host'];
1302
-	if ($t['scheme'] == 'http') {
1303
-		$scheme = 'http';
1304
-		$noproxy = '';
1305
-	} elseif ($t['scheme'] == 'https') {
1306
-		$scheme = 'ssl';
1307
-		$noproxy = 'ssl://';
1308
-		if (!isset($t['port']) || !($port = $t['port'])) {
1309
-			$t['port'] = 443;
1310
-		}
1311
-	} else {
1312
-		$scheme = $t['scheme'];
1313
-		$noproxy = $scheme . '://';
1314
-	}
1315
-	if (isset($t['user'])) {
1316
-		$user = array($t['user'], $t['pass']);
1317
-	}
1318
-
1319
-	if (!isset($t['port']) || !($port = $t['port'])) {
1320
-		$port = 80;
1321
-	}
1322
-	if (!isset($t['path']) || !($path = $t['path'])) {
1323
-		$path = '/';
1324
-	}
1325
-
1326
-	if (!empty($t['query'])) {
1327
-		$path .= '?' . $t['query'];
1328
-	}
1329
-
1330
-	$f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);
1331
-	if (!$f or !is_resource($f)) {
1332
-		// fallback : fopen si on a pas fait timeout dans lance_requete
1333
-		// ce qui correspond a $f===110
1334
-		if ($f !== 110
1335
-			and !need_proxy($host)
1336
-			and !_request('tester_proxy')
1337
-			and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen'])
1338
-		) {
1339
-			$f = @fopen($url, 'rb');
1340
-			spip_log("connexion vers $url par simple fopen", 'distant');
1341
-			$fopen = true;
1342
-		} else {
1343
-			// echec total
1344
-			$f = false;
1345
-		}
1346
-	}
1347
-
1348
-	return array($f, $fopen);
1297
+    $user = $via_proxy = $proxy_user = '';
1298
+    $fopen = false;
1299
+
1300
+    $t = @parse_url($url);
1301
+    $host = $t['host'];
1302
+    if ($t['scheme'] == 'http') {
1303
+        $scheme = 'http';
1304
+        $noproxy = '';
1305
+    } elseif ($t['scheme'] == 'https') {
1306
+        $scheme = 'ssl';
1307
+        $noproxy = 'ssl://';
1308
+        if (!isset($t['port']) || !($port = $t['port'])) {
1309
+            $t['port'] = 443;
1310
+        }
1311
+    } else {
1312
+        $scheme = $t['scheme'];
1313
+        $noproxy = $scheme . '://';
1314
+    }
1315
+    if (isset($t['user'])) {
1316
+        $user = array($t['user'], $t['pass']);
1317
+    }
1318
+
1319
+    if (!isset($t['port']) || !($port = $t['port'])) {
1320
+        $port = 80;
1321
+    }
1322
+    if (!isset($t['path']) || !($path = $t['path'])) {
1323
+        $path = '/';
1324
+    }
1325
+
1326
+    if (!empty($t['query'])) {
1327
+        $path .= '?' . $t['query'];
1328
+    }
1329
+
1330
+    $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);
1331
+    if (!$f or !is_resource($f)) {
1332
+        // fallback : fopen si on a pas fait timeout dans lance_requete
1333
+        // ce qui correspond a $f===110
1334
+        if ($f !== 110
1335
+            and !need_proxy($host)
1336
+            and !_request('tester_proxy')
1337
+            and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen'])
1338
+        ) {
1339
+            $f = @fopen($url, 'rb');
1340
+            spip_log("connexion vers $url par simple fopen", 'distant');
1341
+            $fopen = true;
1342
+        } else {
1343
+            // echec total
1344
+            $f = false;
1345
+        }
1346
+    }
1347
+
1348
+    return array($f, $fopen);
1349 1349
 }
1350 1350
 
1351 1351
 /**
@@ -1380,124 +1380,124 @@  discard block
 block discarded – undo
1380 1380
  *   resource socket vers l'url demandee
1381 1381
  */
1382 1382
 function lance_requete(
1383
-	$method,
1384
-	$scheme,
1385
-	$user,
1386
-	$host,
1387
-	$path,
1388
-	$port,
1389
-	$noproxy,
1390
-	$refuse_gz = false,
1391
-	$referer = '',
1392
-	$datas = '',
1393
-	$vers = 'HTTP/1.0',
1394
-	$date = ''
1383
+    $method,
1384
+    $scheme,
1385
+    $user,
1386
+    $host,
1387
+    $path,
1388
+    $port,
1389
+    $noproxy,
1390
+    $refuse_gz = false,
1391
+    $referer = '',
1392
+    $datas = '',
1393
+    $vers = 'HTTP/1.0',
1394
+    $date = ''
1395 1395
 ) {
1396 1396
 
1397
-	$proxy_user = '';
1398
-	$http_proxy = need_proxy($host);
1399
-	if ($user) {
1400
-		$user = urlencode($user[0]) . ':' . urlencode($user[1]);
1401
-	}
1402
-
1403
-	$connect = '';
1404
-	if ($http_proxy) {
1405
-		if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) {
1406
-			$path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : '');
1407
-			$connect = 'CONNECT ' . $path_host . " $vers\r\n"
1408
-				. "Host: $path_host\r\n"
1409
-				. "Proxy-Connection: Keep-Alive\r\n";
1410
-		} else {
1411
-			$path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://")
1412
-				. (!$user ? '' : "$user@")
1413
-				. "$host" . (($port != 80) ? ":$port" : '') . $path;
1414
-		}
1415
-		$t2 = @parse_url($http_proxy);
1416
-		$first_host = $t2['host'];
1417
-		if (!($port = $t2['port'])) {
1418
-			$port = 80;
1419
-		}
1420
-		if ($t2['user']) {
1421
-			$proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']);
1422
-		}
1423
-	} else {
1424
-		$first_host = $noproxy . $host;
1425
-	}
1426
-
1427
-	if ($connect) {
1428
-		$streamContext = stream_context_create(array(
1429
-			'ssl' => array(
1430
-				'verify_peer' => false,
1431
-				'allow_self_signed' => true,
1432
-				'SNI_enabled' => true,
1433
-				'peer_name' => $host,
1434
-			)
1435
-		));
1436
-		$f = @stream_socket_client(
1437
-			"tcp://$first_host:$port",
1438
-			$errno,
1439
-			$errstr,
1440
-			_INC_DISTANT_CONNECT_TIMEOUT,
1441
-			STREAM_CLIENT_CONNECT,
1442
-			$streamContext
1443
-		);
1444
-		spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect');
1445
-		if (!$f) {
1446
-			spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1447
-			return $errno;
1448
-		}
1449
-		stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
1450
-
1451
-		fputs($f, $connect);
1452
-		fputs($f, "\r\n");
1453
-		$res = fread($f, 1024);
1454
-		if (!$res
1455
-			or !count($res = explode(' ', $res))
1456
-			or $res[1] !== '200'
1457
-		) {
1458
-			spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE);
1459
-			fclose($f);
1460
-
1461
-			return false;
1462
-		}
1463
-		// important, car sinon on lit trop vite et les donnees ne sont pas encore dispo
1464
-		stream_set_blocking($f, true);
1465
-		// envoyer le handshake
1466
-		stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
1467
-		spip_log("OK CONNECT sur $first_host:$port", 'connect');
1468
-	} else {
1469
-		$ntry = 3;
1470
-		do {
1471
-			$f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT);
1472
-		} while (!$f and $ntry-- and $errno !== 110 and sleep(1));
1473
-		spip_log("Recuperer $path sur $first_host:$port par $f");
1474
-		if (!$f) {
1475
-			spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1476
-
1477
-			return $errno;
1478
-		}
1479
-		stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
1480
-	}
1481
-
1482
-	$site = isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '';
1483
-
1484
-	$host_port = $host;
1485
-	if ($port != (in_array($scheme , array('tls','ssl')) ? 443 : 80)) {
1486
-		$host_port .= ":$port";
1487
-	}
1488
-	$req = "$method $path $vers\r\n"
1489
-		. "Host: $host_port\r\n"
1490
-		. 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n"
1491
-		. ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n"))
1492
-		. (!$site ? '' : "Referer: $site/$referer\r\n")
1493
-		. (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n"))
1494
-		. (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n"))
1495
-		. (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n")
1496
-		. (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n");
1397
+    $proxy_user = '';
1398
+    $http_proxy = need_proxy($host);
1399
+    if ($user) {
1400
+        $user = urlencode($user[0]) . ':' . urlencode($user[1]);
1401
+    }
1402
+
1403
+    $connect = '';
1404
+    if ($http_proxy) {
1405
+        if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) {
1406
+            $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : '');
1407
+            $connect = 'CONNECT ' . $path_host . " $vers\r\n"
1408
+                . "Host: $path_host\r\n"
1409
+                . "Proxy-Connection: Keep-Alive\r\n";
1410
+        } else {
1411
+            $path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://")
1412
+                . (!$user ? '' : "$user@")
1413
+                . "$host" . (($port != 80) ? ":$port" : '') . $path;
1414
+        }
1415
+        $t2 = @parse_url($http_proxy);
1416
+        $first_host = $t2['host'];
1417
+        if (!($port = $t2['port'])) {
1418
+            $port = 80;
1419
+        }
1420
+        if ($t2['user']) {
1421
+            $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']);
1422
+        }
1423
+    } else {
1424
+        $first_host = $noproxy . $host;
1425
+    }
1426
+
1427
+    if ($connect) {
1428
+        $streamContext = stream_context_create(array(
1429
+            'ssl' => array(
1430
+                'verify_peer' => false,
1431
+                'allow_self_signed' => true,
1432
+                'SNI_enabled' => true,
1433
+                'peer_name' => $host,
1434
+            )
1435
+        ));
1436
+        $f = @stream_socket_client(
1437
+            "tcp://$first_host:$port",
1438
+            $errno,
1439
+            $errstr,
1440
+            _INC_DISTANT_CONNECT_TIMEOUT,
1441
+            STREAM_CLIENT_CONNECT,
1442
+            $streamContext
1443
+        );
1444
+        spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect');
1445
+        if (!$f) {
1446
+            spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1447
+            return $errno;
1448
+        }
1449
+        stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
1450
+
1451
+        fputs($f, $connect);
1452
+        fputs($f, "\r\n");
1453
+        $res = fread($f, 1024);
1454
+        if (!$res
1455
+            or !count($res = explode(' ', $res))
1456
+            or $res[1] !== '200'
1457
+        ) {
1458
+            spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE);
1459
+            fclose($f);
1460
+
1461
+            return false;
1462
+        }
1463
+        // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo
1464
+        stream_set_blocking($f, true);
1465
+        // envoyer le handshake
1466
+        stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
1467
+        spip_log("OK CONNECT sur $first_host:$port", 'connect');
1468
+    } else {
1469
+        $ntry = 3;
1470
+        do {
1471
+            $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT);
1472
+        } while (!$f and $ntry-- and $errno !== 110 and sleep(1));
1473
+        spip_log("Recuperer $path sur $first_host:$port par $f");
1474
+        if (!$f) {
1475
+            spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1476
+
1477
+            return $errno;
1478
+        }
1479
+        stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
1480
+    }
1481
+
1482
+    $site = isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '';
1483
+
1484
+    $host_port = $host;
1485
+    if ($port != (in_array($scheme , array('tls','ssl')) ? 443 : 80)) {
1486
+        $host_port .= ":$port";
1487
+    }
1488
+    $req = "$method $path $vers\r\n"
1489
+        . "Host: $host_port\r\n"
1490
+        . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n"
1491
+        . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n"))
1492
+        . (!$site ? '' : "Referer: $site/$referer\r\n")
1493
+        . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n"))
1494
+        . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n"))
1495
+        . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n")
1496
+        . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n");
1497 1497
 
1498 1498
 #	spip_log("Requete\n$req", 'distant');
1499
-	fputs($f, $req);
1500
-	fputs($f, $datas ? $datas : "\r\n");
1499
+    fputs($f, $req);
1500
+    fputs($f, $datas ? $datas : "\r\n");
1501 1501
 
1502
-	return $f;
1502
+    return $f;
1503 1503
 }
Please login to merge, or discard this patch.
Spacing   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
 	define('_INC_DISTANT_CONTENT_ENCODING', 'gzip');
27 27
 }
28 28
 if (!defined('_INC_DISTANT_USER_AGENT')) {
29
-	define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')');
29
+	define('_INC_DISTANT_USER_AGENT', 'SPIP-'.$GLOBALS['spip_version_affichee'].' ('.$GLOBALS['home_server'].')');
30 30
 }
31 31
 if (!defined('_INC_DISTANT_MAX_SIZE')) {
32 32
 	define('_INC_DISTANT_MAX_SIZE', 2097152);
@@ -35,7 +35,7 @@  discard block
 block discarded – undo
35 35
 	define('_INC_DISTANT_CONNECT_TIMEOUT', 10);
36 36
 }
37 37
 
38
-define('_REGEXP_COPIE_LOCALE', ',' 	.
38
+define('_REGEXP_COPIE_LOCALE', ','.
39 39
 	preg_replace(
40 40
 		'@^https?:@',
41 41
 		'https?:',
@@ -70,7 +70,7 @@  discard block
 block discarded – undo
70 70
 
71 71
 	// si c'est la protection de soi-meme, retourner le path
72 72
 	if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) {
73
-		$source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]);
73
+		$source = substr(_DIR_IMG, strlen(_DIR_RACINE)).urldecode($match[1]);
74 74
 
75 75
 		return @file_exists($source) ? $source : false;
76 76
 	}
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
 		return false;
91 91
 	}
92 92
 
93
-	$localrac = _DIR_RACINE . $local;
93
+	$localrac = _DIR_RACINE.$local;
94 94
 	$t = ($mode == 'force') ? false : @file_exists($localrac);
95 95
 
96 96
 	// test d'existence du fichier
@@ -115,13 +115,13 @@  discard block
 block discarded – undo
115 115
 			array('file' => $localrac, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '')
116 116
 		);
117 117
 		if (!$res or (!$res['length'] and $res['status'] != 304)) {
118
-			spip_log("copie_locale : Echec recuperation $source sur $localrac status : " . $res['status'], 'distant' . _LOG_INFO_IMPORTANTE);
118
+			spip_log("copie_locale : Echec recuperation $source sur $localrac status : ".$res['status'], 'distant'._LOG_INFO_IMPORTANTE);
119 119
 		}
120 120
 		if (!$res['length']) {
121 121
 			// si $t c'est sans doute juste un not-modified-since
122 122
 			return $t ? $local : false;
123 123
 		}
124
-		spip_log("copie_locale : recuperation $source sur $localrac taille " . $res['length'] . ' OK', 'distant');
124
+		spip_log("copie_locale : recuperation $source sur $localrac taille ".$res['length'].' OK', 'distant');
125 125
 
126 126
 		// pour une eventuelle indexation
127 127
 		pipeline(
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
  *   url ou false en cas d'echec
154 154
  */
155 155
 function valider_url_distante($url, $known_hosts = array()) {
156
-	if (!function_exists('protocole_verifier')){
156
+	if (!function_exists('protocole_verifier')) {
157 157
 		include_spip('inc/filtres_mini');
158 158
 	}
159 159
 
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 	}
163 163
 	
164 164
 	$parsed_url = parse_url($url);
165
-	if (!$parsed_url or empty($parsed_url['host']) ) {
165
+	if (!$parsed_url or empty($parsed_url['host'])) {
166 166
 		return false;
167 167
 	}
168 168
 
@@ -203,10 +203,10 @@  discard block
 block discarded – undo
203 203
 			}
204 204
 		}
205 205
 		if ($ip) {
206
-			$parts = array_map('intval', explode( '.', $ip ));
206
+			$parts = array_map('intval', explode('.', $ip));
207 207
 			if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0]
208
-			  or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] )
209
-			  or ( 192 === $parts[0] && 168 === $parts[1] )
208
+			  or (172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1])
209
+			  or (192 === $parts[0] && 168 === $parts[1])
210 210
 			) {
211 211
 				return false;
212 212
 			}
@@ -218,7 +218,7 @@  discard block
 block discarded – undo
218 218
 	}
219 219
 
220 220
 	$port = $parsed_url['port'];
221
-	if ($port === 80  or $port === 443  or $port === 8080) {
221
+	if ($port === 80 or $port === 443 or $port === 8080) {
222 222
 		return $url;
223 223
 	}
224 224
 
@@ -286,7 +286,7 @@  discard block
 block discarded – undo
286 286
 				}
287 287
 			}
288 288
 			if ($taille > 500) {
289
-				$boundary = substr(md5(rand() . 'spip'), 0, 8);
289
+				$boundary = substr(md5(rand().'spip'), 0, 8);
290 290
 			}
291 291
 		}
292 292
 
@@ -314,16 +314,16 @@  discard block
 block discarded – undo
314 314
 			}
315 315
 		} else {
316 316
 			// fabrique une chaine HTTP simple pour un POST
317
-			$entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
317
+			$entete = 'Content-Type: application/x-www-form-urlencoded'."\r\n";
318 318
 			$chaine = array();
319 319
 			if (is_array($donnees)) {
320 320
 				foreach ($donnees as $cle => $valeur) {
321 321
 					if (is_array($valeur)) {
322 322
 						foreach ($valeur as $val2) {
323
-							$chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2);
323
+							$chaine[] = rawurlencode($cle).'[]='.rawurlencode($val2);
324 324
 						}
325 325
 					} else {
326
-						$chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur);
326
+						$chaine[] = rawurlencode($cle).'='.rawurlencode($valeur);
327 327
 					}
328 328
 				}
329 329
 				$chaine = implode('&', $chaine);
@@ -433,7 +433,7 @@  discard block
 block discarded – undo
433 433
 	$head_add = '';
434 434
 	if (!empty($options['headers'])) {
435 435
 		foreach ($options['headers'] as $champ => $valeur) {
436
-			$head_add .= $champ . ': ' . $valeur . "\r\n";
436
+			$head_add .= $champ.': '.$valeur."\r\n";
437 437
 		}
438 438
 		// ne pas le repasser a recuperer_url si on follow un location, car ils seront dans datas
439 439
 		unset($options['entetes']);
@@ -443,9 +443,9 @@  discard block
 block discarded – undo
443 443
 		list($head, $postdata) = prepare_donnees_post($options['datas'], $options['boundary']);
444 444
 		$head .= $head_add;
445 445
 		if (stripos($head, 'Content-Length:') === false) {
446
-			$head .= 'Content-Length: ' . strlen($postdata) . "\r\n";
446
+			$head .= 'Content-Length: '.strlen($postdata)."\r\n";
447 447
 		}
448
-		$options['datas'] = $head . "\r\n" . $postdata;
448
+		$options['datas'] = $head."\r\n".$postdata;
449 449
 		if (
450 450
 			strlen($postdata)
451 451
 			and !$methode_demandee
@@ -453,15 +453,15 @@  discard block
 block discarded – undo
453 453
 			$options['methode'] = 'POST';
454 454
 		}
455 455
 	} elseif ($head_add) {
456
-		$options['datas'] = $head_add . "\r\n";
456
+		$options['datas'] = $head_add."\r\n";
457 457
 	}
458 458
 
459 459
 	// Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole
460 460
 	$url = preg_replace(',^feed://,i', 'http://', $url);
461 461
 	if (!tester_url_absolue($url)) {
462
-		$url = 'http://' . $url;
462
+		$url = 'http://'.$url;
463 463
 	} elseif (strncmp($url, '//', 2) == 0) {
464
-		$url = 'http:' . $url;
464
+		$url = 'http:'.$url;
465 465
 	}
466 466
 
467 467
 	$url = url_to_ascii($url);
@@ -490,7 +490,7 @@  discard block
 block discarded – undo
490 490
 		$options['if_modified_since']
491 491
 	);
492 492
 	if (!$handle) {
493
-		spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR);
493
+		spip_log("ECHEC init_http $url", 'distant'._LOG_ERREUR);
494 494
 
495 495
 		return false;
496 496
 	}
@@ -519,7 +519,7 @@  discard block
 block discarded – undo
519 519
 					'status' => 200,
520 520
 				);
521 521
 			} else {
522
-				spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR);
522
+				spip_log("ECHEC chinoiserie $url", 'distant'._LOG_ERREUR);
523 523
 				return false;
524 524
 			}
525 525
 		} elseif ($res['location'] and $options['follow_location']) {
@@ -531,7 +531,7 @@  discard block
 block discarded – undo
531 531
 
532 532
 			return recuperer_url($url, $options);
533 533
 		} elseif ($res['status'] !== 200) {
534
-			spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant');
534
+			spip_log('HTTP status '.$res['status']." pour $url", 'distant');
535 535
 		}
536 536
 		$result['status'] = $res['status'];
537 537
 		if (isset($res['headers'])) {
@@ -556,7 +556,7 @@  discard block
 block discarded – undo
556 556
 
557 557
 	$gz = false;
558 558
 	if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) {
559
-		$gz = (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz');
559
+		$gz = (_DIR_TMP.md5(uniqid(mt_rand())).'.tmp.gz');
560 560
 	}
561 561
 
562 562
 	// si on a pas deja recuperer le contenu par une methode detournee
@@ -642,7 +642,7 @@  discard block
 block discarded – undo
642 642
 	$sig['url'] = $url;
643 643
 
644 644
 	$dir = sous_repertoire(_DIR_CACHE, 'curl');
645
-	$cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80);
645
+	$cache = md5(serialize($sig)).'-'.substr(preg_replace(',\W+,', '_', $url), 0, 80);
646 646
 	$sub = sous_repertoire($dir, substr($cache, 0, 2));
647 647
 	$cache = "$sub$cache";
648 648
 
@@ -752,7 +752,7 @@  discard block
 block discarded – undo
752 752
 		return false;
753 753
 	}
754 754
 	if ($get_headers) {
755
-		return $res['headers'] . "\n" . $res['page'];
755
+		return $res['headers']."\n".$res['page'];
756 756
 	}
757 757
 
758 758
 	return $res['page'];
@@ -856,7 +856,7 @@  discard block
 block discarded – undo
856 856
 	$fp = false;
857 857
 	if ($fichier) {
858 858
 		include_spip('inc/acces');
859
-		$tmpfile = "$fichier." . creer_uniqid() . '.tmp';
859
+		$tmpfile = "$fichier.".creer_uniqid().'.tmp';
860 860
 		$fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX);
861 861
 		if (!$fp and file_exists($fichier)) {
862 862
 			return filesize($fichier);
@@ -915,7 +915,7 @@  discard block
 block discarded – undo
915 915
 	}
916 916
 	$result['status'] = intval($r[1]);
917 917
 	while ($s = trim(fgets($handle, 16384))) {
918
-		$result['headers'][] = $s . "\n";
918
+		$result['headers'][] = $s."\n";
919 919
 		preg_match(',^([^:]*): *(.*)$,i', $s, $r);
920 920
 		list(, $d, $v) = $r;
921 921
 		if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) {
@@ -963,13 +963,13 @@  discard block
 block discarded – undo
963 963
 
964 964
 	// on se place tout le temps comme si on etait a la racine
965 965
 	if (_DIR_RACINE) {
966
-		$d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d);
966
+		$d = preg_replace(',^'.preg_quote(_DIR_RACINE).',', '', $d);
967 967
 	}
968 968
 
969 969
 	$m = md5($source);
970 970
 
971 971
 	return $d
972
-	. substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12)
972
+	. substr(preg_replace(',[^\w-],', '', basename($source)).'-'.$m, 0, 12)
973 973
 	. substr($m, 0, 4)
974 974
 	. ".$extension";
975 975
 }
@@ -992,7 +992,7 @@  discard block
 block discarded – undo
992 992
 	// Si c'est deja local pas de souci
993 993
 	if (!tester_url_absolue($source)) {
994 994
 		if (_DIR_RACINE) {
995
-			$source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source);
995
+			$source = preg_replace(',^'.preg_quote(_DIR_RACINE).',', '', $source);
996 996
 		}
997 997
 
998 998
 		return $source;
@@ -1009,7 +1009,7 @@  discard block
 block discarded – undo
1009 1009
 	if ($ext
1010 1010
 		and preg_match(',^\w+$,', $ext) // pas de php?truc=1&...
1011 1011
 		and $f = nom_fichier_copie_locale($source, $ext)
1012
-		and file_exists(_DIR_RACINE . $f)
1012
+		and file_exists(_DIR_RACINE.$f)
1013 1013
 	) {
1014 1014
 		return $f;
1015 1015
 	}
@@ -1017,7 +1017,7 @@  discard block
 block discarded – undo
1017 1017
 
1018 1018
 	// Si c'est deja dans la table des documents,
1019 1019
 	// ramener le nom de sa copie potentielle
1020
-	$ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''");
1020
+	$ext = sql_getfetsel('extension', 'spip_documents', 'fichier='.sql_quote($source)." AND distant='oui' AND extension <> ''");
1021 1021
 
1022 1022
 	if ($ext) {
1023 1023
 		return nom_fichier_copie_locale($source, $ext);
@@ -1028,9 +1028,9 @@  discard block
 block discarded – undo
1028 1028
 
1029 1029
 	$ext = $path_parts ? $path_parts['extension'] : '';
1030 1030
 
1031
-	if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
1031
+	if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension='.sql_quote($ext))) {
1032 1032
 		$f = nom_fichier_copie_locale($source, $ext);
1033
-		if (file_exists(_DIR_RACINE . $f)) {
1033
+		if (file_exists(_DIR_RACINE.$f)) {
1034 1034
 			return $f;
1035 1035
 		}
1036 1036
 	}
@@ -1038,7 +1038,7 @@  discard block
 block discarded – undo
1038 1038
 	// Ping  pour voir si son extension est connue et autorisee
1039 1039
 	// avec mise en cache du resultat du ping
1040 1040
 
1041
-	$cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source);
1041
+	$cache = sous_repertoire(_DIR_CACHE, 'rid').md5($source);
1042 1042
 	if (!@file_exists($cache)
1043 1043
 		or !$path_parts = @unserialize(spip_file_get_contents($cache))
1044 1044
 		or _request('var_mode') == 'recalcul'
@@ -1047,10 +1047,10 @@  discard block
 block discarded – undo
1047 1047
 		ecrire_fichier($cache, serialize($path_parts));
1048 1048
 	}
1049 1049
 	$ext = !empty($path_parts['extension']) ? $path_parts['extension'] : '';
1050
-	if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
1050
+	if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension='.sql_quote($ext))) {
1051 1051
 		return nom_fichier_copie_locale($source, $ext);
1052 1052
 	}
1053
-	spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR);
1053
+	spip_log("pas de copie locale pour $source", 'distant'._LOG_ERREUR);
1054 1054
 }
1055 1055
 
1056 1056
 
@@ -1116,19 +1116,19 @@  discard block
 block discarded – undo
1116 1116
 			if (!$t
1117 1117
 				and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)
1118 1118
 			) {
1119
-				$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text'));
1119
+				$t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote($rext[1], '', 'text'));
1120 1120
 			}
1121 1121
 			if (!$t
1122 1122
 				and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m)
1123 1123
 				and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext)
1124 1124
 			) {
1125
-				$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text'));
1125
+				$t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote($rext[1], '', 'text'));
1126 1126
 			}
1127 1127
 		}
1128 1128
 
1129 1129
 		// Autre mime/type (ou text/plain avec fichier d'extension inconnue)
1130 1130
 		if (!$t) {
1131
-			$t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type));
1131
+			$t = sql_fetsel('extension', 'spip_types_documents', 'mime_type='.sql_quote($mime_type));
1132 1132
 		}
1133 1133
 
1134 1134
 		// Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)
@@ -1138,11 +1138,11 @@  discard block
 block discarded – undo
1138 1138
 			and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)
1139 1139
 		) {
1140 1140
 			# eviter xxx.3 => 3gp (> SPIP 3)
1141
-			$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text'));
1141
+			$t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote($rext[1], '', 'text'));
1142 1142
 		}
1143 1143
 
1144 1144
 		if ($t) {
1145
-			spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant');
1145
+			spip_log("mime-type $mime_type ok, extension ".$t['extension'], 'distant');
1146 1146
 			$a['extension'] = $t['extension'];
1147 1147
 		} else {
1148 1148
 			# par defaut on retombe sur '.bin' si c'est autorise
@@ -1184,7 +1184,7 @@  discard block
 block discarded – undo
1184 1184
 		} else {
1185 1185
 			if ($a['body']) {
1186 1186
 				$a['extension'] = $extension;
1187
-				$a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension);
1187
+				$a['fichier'] = _DIR_RACINE.nom_fichier_copie_locale($source, $extension);
1188 1188
 				ecrire_fichier($a['fichier'], $a['body']);
1189 1189
 				$size_image = @spip_getimagesize($a['fichier']);
1190 1190
 				$a['largeur'] = intval($size_image[0]);
@@ -1310,7 +1310,7 @@  discard block
 block discarded – undo
1310 1310
 		}
1311 1311
 	} else {
1312 1312
 		$scheme = $t['scheme'];
1313
-		$noproxy = $scheme . '://';
1313
+		$noproxy = $scheme.'://';
1314 1314
 	}
1315 1315
 	if (isset($t['user'])) {
1316 1316
 		$user = array($t['user'], $t['pass']);
@@ -1324,7 +1324,7 @@  discard block
 block discarded – undo
1324 1324
 	}
1325 1325
 
1326 1326
 	if (!empty($t['query'])) {
1327
-		$path .= '?' . $t['query'];
1327
+		$path .= '?'.$t['query'];
1328 1328
 	}
1329 1329
 
1330 1330
 	$f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);
@@ -1397,20 +1397,20 @@  discard block
 block discarded – undo
1397 1397
 	$proxy_user = '';
1398 1398
 	$http_proxy = need_proxy($host);
1399 1399
 	if ($user) {
1400
-		$user = urlencode($user[0]) . ':' . urlencode($user[1]);
1400
+		$user = urlencode($user[0]).':'.urlencode($user[1]);
1401 1401
 	}
1402 1402
 
1403 1403
 	$connect = '';
1404 1404
 	if ($http_proxy) {
1405
-		if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) {
1406
-			$path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : '');
1407
-			$connect = 'CONNECT ' . $path_host . " $vers\r\n"
1405
+		if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme, array('tls', 'ssl'))) {
1406
+			$path_host = (!$user ? '' : "$user@").$host.(($port != 80) ? ":$port" : '');
1407
+			$connect = 'CONNECT '.$path_host." $vers\r\n"
1408 1408
 				. "Host: $path_host\r\n"
1409 1409
 				. "Proxy-Connection: Keep-Alive\r\n";
1410 1410
 		} else {
1411
-			$path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://")
1411
+			$path = (in_array($scheme, array('tls', 'ssl')) ? 'https://' : "$scheme://")
1412 1412
 				. (!$user ? '' : "$user@")
1413
-				. "$host" . (($port != 80) ? ":$port" : '') . $path;
1413
+				. "$host".(($port != 80) ? ":$port" : '').$path;
1414 1414
 		}
1415 1415
 		$t2 = @parse_url($http_proxy);
1416 1416
 		$first_host = $t2['host'];
@@ -1418,10 +1418,10 @@  discard block
 block discarded – undo
1418 1418
 			$port = 80;
1419 1419
 		}
1420 1420
 		if ($t2['user']) {
1421
-			$proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']);
1421
+			$proxy_user = base64_encode($t2['user'].':'.$t2['pass']);
1422 1422
 		}
1423 1423
 	} else {
1424
-		$first_host = $noproxy . $host;
1424
+		$first_host = $noproxy.$host;
1425 1425
 	}
1426 1426
 
1427 1427
 	if ($connect) {
@@ -1443,7 +1443,7 @@  discard block
 block discarded – undo
1443 1443
 		);
1444 1444
 		spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect');
1445 1445
 		if (!$f) {
1446
-			spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1446
+			spip_log("Erreur connexion $errno $errstr", 'distant'._LOG_ERREUR);
1447 1447
 			return $errno;
1448 1448
 		}
1449 1449
 		stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
@@ -1455,7 +1455,7 @@  discard block
 block discarded – undo
1455 1455
 			or !count($res = explode(' ', $res))
1456 1456
 			or $res[1] !== '200'
1457 1457
 		) {
1458
-			spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE);
1458
+			spip_log("Echec CONNECT sur $first_host:$port", 'connect'._LOG_INFO_IMPORTANTE);
1459 1459
 			fclose($f);
1460 1460
 
1461 1461
 			return false;
@@ -1472,7 +1472,7 @@  discard block
 block discarded – undo
1472 1472
 		} while (!$f and $ntry-- and $errno !== 110 and sleep(1));
1473 1473
 		spip_log("Recuperer $path sur $first_host:$port par $f");
1474 1474
 		if (!$f) {
1475
-			spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1475
+			spip_log("Erreur connexion $errno $errstr", 'distant'._LOG_ERREUR);
1476 1476
 
1477 1477
 			return $errno;
1478 1478
 		}
@@ -1482,16 +1482,16 @@  discard block
 block discarded – undo
1482 1482
 	$site = isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '';
1483 1483
 
1484 1484
 	$host_port = $host;
1485
-	if ($port != (in_array($scheme , array('tls','ssl')) ? 443 : 80)) {
1485
+	if ($port != (in_array($scheme, array('tls', 'ssl')) ? 443 : 80)) {
1486 1486
 		$host_port .= ":$port";
1487 1487
 	}
1488 1488
 	$req = "$method $path $vers\r\n"
1489 1489
 		. "Host: $host_port\r\n"
1490
-		. 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n"
1491
-		. ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n"))
1490
+		. 'User-Agent: '._INC_DISTANT_USER_AGENT."\r\n"
1491
+		. ($refuse_gz ? '' : ('Accept-Encoding: '._INC_DISTANT_CONTENT_ENCODING."\r\n"))
1492 1492
 		. (!$site ? '' : "Referer: $site/$referer\r\n")
1493
-		. (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n"))
1494
-		. (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n"))
1493
+		. (!$date ? '' : 'If-Modified-Since: '.(gmdate('D, d M Y H:i:s', $date)." GMT\r\n"))
1494
+		. (!$user ? '' : ('Authorization: Basic '.base64_encode($user)."\r\n"))
1495 1495
 		. (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n")
1496 1496
 		. (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n");
1497 1497
 
Please login to merge, or discard this patch.