Completed
Push — master ( 76bd8f...267cf1 )
by cam
01:50
created
ecrire/inc/distant.php 2 patches
Indentation   +1142 added lines, -1142 removed lines patch added patch discarded remove patch
@@ -17,32 +17,32 @@  discard block
 block discarded – undo
17 17
  * @package SPIP\Core\Distant
18 18
  **/
19 19
 if (!defined('_ECRIRE_INC_VERSION')) {
20
-	return;
20
+    return;
21 21
 }
22 22
 
23 23
 if (!defined('_INC_DISTANT_VERSION_HTTP')) {
24
-	define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0');
24
+    define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0');
25 25
 }
26 26
 if (!defined('_INC_DISTANT_CONTENT_ENCODING')) {
27
-	define('_INC_DISTANT_CONTENT_ENCODING', 'gzip');
27
+    define('_INC_DISTANT_CONTENT_ENCODING', 'gzip');
28 28
 }
29 29
 if (!defined('_INC_DISTANT_USER_AGENT')) {
30
-	define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')');
30
+    define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')');
31 31
 }
32 32
 if (!defined('_INC_DISTANT_MAX_SIZE')) {
33
-	define('_INC_DISTANT_MAX_SIZE', 2_097_152);
33
+    define('_INC_DISTANT_MAX_SIZE', 2_097_152);
34 34
 }
35 35
 if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) {
36
-	define('_INC_DISTANT_CONNECT_TIMEOUT', 10);
36
+    define('_INC_DISTANT_CONNECT_TIMEOUT', 10);
37 37
 }
38 38
 
39 39
 define('_REGEXP_COPIE_LOCALE', ',' 	.
40
-	preg_replace(
41
-		'@^https?:@',
42
-		'https?:',
43
-		($GLOBALS['meta']['adresse_site'] ?? '')
44
-	)
45
-	. '/?spip.php[?]action=acceder_document.*file=(.*)$,');
40
+    preg_replace(
41
+        '@^https?:@',
42
+        'https?:',
43
+        ($GLOBALS['meta']['adresse_site'] ?? '')
44
+    )
45
+    . '/?spip.php[?]action=acceder_document.*file=(.*)$,');
46 46
 
47 47
 //@define('_COPIE_LOCALE_MAX_SIZE',2097152); // poids (inc/utils l'a fait)
48 48
 
@@ -71,105 +71,105 @@  discard block
 block discarded – undo
71 71
  */
72 72
 function copie_locale($source, $mode = 'auto', $local = null, $taille_max = null, $callback_valider_url = null) {
73 73
 
74
-	// si c'est la protection de soi-meme, retourner le path
75
-	if ($mode !== 'force' && preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) {
76
-		$source = substr((string) _DIR_IMG, strlen((string) _DIR_RACINE)) . urldecode($match[1]);
77
-
78
-		return @file_exists($source) ? $source : false;
79
-	}
80
-
81
-	if (is_null($local)) {
82
-		$local = fichier_copie_locale($source);
83
-	} else {
84
-		if (_DIR_RACINE && strncmp((string) _DIR_RACINE, $local, strlen((string) _DIR_RACINE)) == 0) {
85
-			$local = substr($local, strlen((string) _DIR_RACINE));
86
-		}
87
-	}
88
-
89
-	// si $local = '' c'est un fichier refuse par fichier_copie_locale(),
90
-	// par exemple un fichier qui ne figure pas dans nos documents ;
91
-	// dans ce cas on n'essaie pas de le telecharger pour ensuite echouer
92
-	if (!$local) {
93
-		return false;
94
-	}
95
-
96
-	$localrac = _DIR_RACINE . $local;
97
-	$t = ($mode === 'force') ? false : @file_exists($localrac);
98
-
99
-	// test d'existence du fichier
100
-	if ($mode === 'test') {
101
-		return $t ? $local : '';
102
-	}
103
-
104
-	// sinon voir si on doit/peut le telecharger
105
-	if ($local === $source || !tester_url_absolue($source)) {
106
-		return $t ? $local : '';
107
-	}
108
-
109
-	if ($mode === 'modif' || !$t) {
110
-		// passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation
111
-		// et des eventuelles recuperations concurantes
112
-		include_spip('inc/acces');
113
-		if (!$taille_max) {
114
-			$taille_max = _COPIE_LOCALE_MAX_SIZE;
115
-		}
116
-		$localrac_tmp = $localrac . '.tmp';
117
-		$res = recuperer_url(
118
-			$source,
119
-			['file' => $localrac_tmp, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '']
120
-		);
121
-
122
-		if (!$res || !$res['length'] && $res['status'] != 304) {
123
-			spip_log("copie_locale : Echec recuperation $source sur $localrac_tmp status : " . ($res ? $res['status'] : '-'), 'distant' . _LOG_INFO_IMPORTANTE);
124
-			@unlink($localrac_tmp);
125
-		} else {
126
-			spip_log("copie_locale : recuperation $source sur $localrac_tmp OK | taille " . $res['length'] . ' status ' . $res['status'], 'distant');
127
-		}
128
-		if (!$res || !$res['length']) {
129
-			// si $t c'est sans doute juste un not-modified-since
130
-			return $t ? $local : false;
131
-		}
132
-
133
-		// si option valider url, verifions que l'URL finale est acceptable
134
-		if (
135
-			$callback_valider_url
136
-			&& is_callable($callback_valider_url)
137
-			&& !$callback_valider_url($res['url'])
138
-		) {
139
-			spip_log('copie_locale : url finale ' . $res['url'] . " non valide, on refuse le fichier $localrac_tmp", 'distant' . _LOG_INFO_IMPORTANTE);
140
-			@unlink($localrac_tmp);
141
-			return $t ? $local : false;
142
-		}
143
-
144
-		// on peut renommer le fichier tmp
145
-		@rename($localrac_tmp, $localrac);
146
-
147
-		// si on retrouve l'extension
148
-		if (
149
-			!empty($res['headers'])
150
-			&& ($extension = distant_trouver_extension_selon_headers($source, $res['headers']))
151
-			&& ($sanitizer = charger_fonction($extension, 'sanitizer', true))
152
-		) {
153
-			$sanitizer($localrac);
154
-		}
155
-
156
-		// pour une eventuelle indexation
157
-		pipeline(
158
-			'post_edition',
159
-			[
160
-				'args' => [
161
-					'operation' => 'copie_locale',
162
-					'source' => $source,
163
-					'fichier' => $local,
164
-					'http_res' => $res['length'],
165
-					'url' => $res['url'],
166
-				],
167
-				'data' => null
168
-			]
169
-		);
170
-	}
171
-
172
-	return $local;
74
+    // si c'est la protection de soi-meme, retourner le path
75
+    if ($mode !== 'force' && preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) {
76
+        $source = substr((string) _DIR_IMG, strlen((string) _DIR_RACINE)) . urldecode($match[1]);
77
+
78
+        return @file_exists($source) ? $source : false;
79
+    }
80
+
81
+    if (is_null($local)) {
82
+        $local = fichier_copie_locale($source);
83
+    } else {
84
+        if (_DIR_RACINE && strncmp((string) _DIR_RACINE, $local, strlen((string) _DIR_RACINE)) == 0) {
85
+            $local = substr($local, strlen((string) _DIR_RACINE));
86
+        }
87
+    }
88
+
89
+    // si $local = '' c'est un fichier refuse par fichier_copie_locale(),
90
+    // par exemple un fichier qui ne figure pas dans nos documents ;
91
+    // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer
92
+    if (!$local) {
93
+        return false;
94
+    }
95
+
96
+    $localrac = _DIR_RACINE . $local;
97
+    $t = ($mode === 'force') ? false : @file_exists($localrac);
98
+
99
+    // test d'existence du fichier
100
+    if ($mode === 'test') {
101
+        return $t ? $local : '';
102
+    }
103
+
104
+    // sinon voir si on doit/peut le telecharger
105
+    if ($local === $source || !tester_url_absolue($source)) {
106
+        return $t ? $local : '';
107
+    }
108
+
109
+    if ($mode === 'modif' || !$t) {
110
+        // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation
111
+        // et des eventuelles recuperations concurantes
112
+        include_spip('inc/acces');
113
+        if (!$taille_max) {
114
+            $taille_max = _COPIE_LOCALE_MAX_SIZE;
115
+        }
116
+        $localrac_tmp = $localrac . '.tmp';
117
+        $res = recuperer_url(
118
+            $source,
119
+            ['file' => $localrac_tmp, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '']
120
+        );
121
+
122
+        if (!$res || !$res['length'] && $res['status'] != 304) {
123
+            spip_log("copie_locale : Echec recuperation $source sur $localrac_tmp status : " . ($res ? $res['status'] : '-'), 'distant' . _LOG_INFO_IMPORTANTE);
124
+            @unlink($localrac_tmp);
125
+        } else {
126
+            spip_log("copie_locale : recuperation $source sur $localrac_tmp OK | taille " . $res['length'] . ' status ' . $res['status'], 'distant');
127
+        }
128
+        if (!$res || !$res['length']) {
129
+            // si $t c'est sans doute juste un not-modified-since
130
+            return $t ? $local : false;
131
+        }
132
+
133
+        // si option valider url, verifions que l'URL finale est acceptable
134
+        if (
135
+            $callback_valider_url
136
+            && is_callable($callback_valider_url)
137
+            && !$callback_valider_url($res['url'])
138
+        ) {
139
+            spip_log('copie_locale : url finale ' . $res['url'] . " non valide, on refuse le fichier $localrac_tmp", 'distant' . _LOG_INFO_IMPORTANTE);
140
+            @unlink($localrac_tmp);
141
+            return $t ? $local : false;
142
+        }
143
+
144
+        // on peut renommer le fichier tmp
145
+        @rename($localrac_tmp, $localrac);
146
+
147
+        // si on retrouve l'extension
148
+        if (
149
+            !empty($res['headers'])
150
+            && ($extension = distant_trouver_extension_selon_headers($source, $res['headers']))
151
+            && ($sanitizer = charger_fonction($extension, 'sanitizer', true))
152
+        ) {
153
+            $sanitizer($localrac);
154
+        }
155
+
156
+        // pour une eventuelle indexation
157
+        pipeline(
158
+            'post_edition',
159
+            [
160
+                'args' => [
161
+                    'operation' => 'copie_locale',
162
+                    'source' => $source,
163
+                    'fichier' => $local,
164
+                    'http_res' => $res['length'],
165
+                    'url' => $res['url'],
166
+                ],
167
+                'data' => null
168
+            ]
169
+        );
170
+    }
171
+
172
+    return $local;
173 173
 }
174 174
 
175 175
 /**
@@ -184,97 +184,97 @@  discard block
 block discarded – undo
184 184
  *   url ou false en cas d'echec
185 185
  */
186 186
 function valider_url_distante($url, $known_hosts = []) {
187
-	if (!function_exists('protocole_verifier')) {
188
-		include_spip('inc/filtres_mini');
189
-	}
190
-
191
-	if (!protocole_verifier($url, ['http', 'https'])) {
192
-		return false;
193
-	}
194
-
195
-	$parsed_url = parse_url($url);
196
-	if (!$parsed_url || empty($parsed_url['host'])) {
197
-		return false;
198
-	}
199
-
200
-	if (isset($parsed_url['user']) || isset($parsed_url['pass'])) {
201
-		return false;
202
-	}
203
-
204
-	if (false !== strpbrk($parsed_url['host'], ':#?[]')) {
205
-		return false;
206
-	}
207
-
208
-	if (!is_array($known_hosts)) {
209
-		$known_hosts = [$known_hosts];
210
-	}
211
-	$known_hosts[] = $GLOBALS['meta']['adresse_site'];
212
-	$known_hosts[] = url_de_base();
213
-	$known_hosts = pipeline('declarer_hosts_distants', $known_hosts);
214
-
215
-	$is_known_host = false;
216
-	foreach ($known_hosts as $known_host) {
217
-		$parse_known = parse_url((string) $known_host);
218
-		if (
219
-			$parse_known
220
-			&& strtolower($parse_known['host']) === strtolower($parsed_url['host'])
221
-		) {
222
-			$is_known_host = true;
223
-			break;
224
-		}
225
-	}
226
-
227
-	if (!$is_known_host) {
228
-		$host = trim($parsed_url['host'], '.');
229
-		if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) {
230
-			$ip = gethostbyname($host);
231
-			if ($ip === $host) {
232
-				// Error condition for gethostbyname()
233
-				$ip = false;
234
-			}
235
-			if ($records = dns_get_record($host)) {
236
-				foreach ($records as $record) {
237
-					// il faut que le TTL soit suffisant afin d'etre certain que le copie_locale eventuel qui suit
238
-					// se fasse sur la meme IP
239
-					if ($record['ttl'] < 10) {
240
-						$ip = false;
241
-						break;
242
-					}
243
-				}
244
-			}
245
-			else {
246
-				$ip = false;
247
-			}
248
-		}
249
-		if ($ip && ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
250
-			return false;
251
-		}
252
-	}
253
-
254
-	if (empty($parsed_url['port'])) {
255
-		return $url;
256
-	}
257
-
258
-	$port = $parsed_url['port'];
259
-	if ($port === 80 || $port === 443 || $port === 8080) {
260
-		return $url;
261
-	}
262
-
263
-	if ($is_known_host) {
264
-		foreach ($known_hosts as $known_host) {
265
-			$parse_known = parse_url((string) $known_host);
266
-			if (
267
-				$parse_known
268
-				&& !empty($parse_known['port'])
269
-				&& strtolower($parse_known['host']) === strtolower($parsed_url['host'])
270
-				&& $parse_known['port'] == $port
271
-			) {
272
-				return $url;
273
-			}
274
-		}
275
-	}
276
-
277
-	return false;
187
+    if (!function_exists('protocole_verifier')) {
188
+        include_spip('inc/filtres_mini');
189
+    }
190
+
191
+    if (!protocole_verifier($url, ['http', 'https'])) {
192
+        return false;
193
+    }
194
+
195
+    $parsed_url = parse_url($url);
196
+    if (!$parsed_url || empty($parsed_url['host'])) {
197
+        return false;
198
+    }
199
+
200
+    if (isset($parsed_url['user']) || isset($parsed_url['pass'])) {
201
+        return false;
202
+    }
203
+
204
+    if (false !== strpbrk($parsed_url['host'], ':#?[]')) {
205
+        return false;
206
+    }
207
+
208
+    if (!is_array($known_hosts)) {
209
+        $known_hosts = [$known_hosts];
210
+    }
211
+    $known_hosts[] = $GLOBALS['meta']['adresse_site'];
212
+    $known_hosts[] = url_de_base();
213
+    $known_hosts = pipeline('declarer_hosts_distants', $known_hosts);
214
+
215
+    $is_known_host = false;
216
+    foreach ($known_hosts as $known_host) {
217
+        $parse_known = parse_url((string) $known_host);
218
+        if (
219
+            $parse_known
220
+            && strtolower($parse_known['host']) === strtolower($parsed_url['host'])
221
+        ) {
222
+            $is_known_host = true;
223
+            break;
224
+        }
225
+    }
226
+
227
+    if (!$is_known_host) {
228
+        $host = trim($parsed_url['host'], '.');
229
+        if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) {
230
+            $ip = gethostbyname($host);
231
+            if ($ip === $host) {
232
+                // Error condition for gethostbyname()
233
+                $ip = false;
234
+            }
235
+            if ($records = dns_get_record($host)) {
236
+                foreach ($records as $record) {
237
+                    // il faut que le TTL soit suffisant afin d'etre certain que le copie_locale eventuel qui suit
238
+                    // se fasse sur la meme IP
239
+                    if ($record['ttl'] < 10) {
240
+                        $ip = false;
241
+                        break;
242
+                    }
243
+                }
244
+            }
245
+            else {
246
+                $ip = false;
247
+            }
248
+        }
249
+        if ($ip && ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
250
+            return false;
251
+        }
252
+    }
253
+
254
+    if (empty($parsed_url['port'])) {
255
+        return $url;
256
+    }
257
+
258
+    $port = $parsed_url['port'];
259
+    if ($port === 80 || $port === 443 || $port === 8080) {
260
+        return $url;
261
+    }
262
+
263
+    if ($is_known_host) {
264
+        foreach ($known_hosts as $known_host) {
265
+            $parse_known = parse_url((string) $known_host);
266
+            if (
267
+                $parse_known
268
+                && !empty($parse_known['port'])
269
+                && strtolower($parse_known['host']) === strtolower($parsed_url['host'])
270
+                && $parse_known['port'] == $port
271
+            ) {
272
+                return $url;
273
+            }
274
+        }
275
+    }
276
+
277
+    return false;
278 278
 }
279 279
 
280 280
 /**
@@ -294,87 +294,87 @@  discard block
 block discarded – undo
294 294
  */
295 295
 function prepare_donnees_post($donnees, $boundary = '') {
296 296
 
297
-	// permettre a la fonction qui a demande le post de formater elle meme ses donnees
298
-	// pour un appel soap par exemple
299
-	// l'entete est separe des donnees par un double retour a la ligne
300
-	// on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n
301
-	$chaine = '';
302
-	if (is_string($donnees) && strlen($donnees)) {
303
-		$entete = '';
304
-		// on repasse tous les \r\n et \r en simples \n
305
-		$donnees = str_replace("\r\n", "\n", $donnees);
306
-		$donnees = str_replace("\r", "\n", $donnees);
307
-		// un double retour a la ligne signifie la fin de l'entete et le debut des donnees
308
-		$p = strpos($donnees, "\n\n");
309
-		if ($p !== false) {
310
-			$entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1));
311
-			$donnees = substr($donnees, $p + 2);
312
-		}
313
-		$chaine = str_replace("\n", "\r\n", $donnees);
314
-	} else {
315
-		/* boundary automatique */
316
-		// Si on a plus de 500 octects de donnees, on "boundarise"
317
-		if ($boundary === '') {
318
-			$taille = 0;
319
-			foreach ($donnees as $cle => $valeur) {
320
-				if (is_array($valeur)) {
321
-					foreach ($valeur as $val2) {
322
-						$taille += strlen((string) $val2);
323
-					}
324
-				} else {
325
-					// faut-il utiliser spip_strlen() dans inc/charsets ?
326
-					$taille += strlen((string) $valeur);
327
-				}
328
-			}
329
-			if ($taille > 500) {
330
-				$boundary = substr(md5(random_int(0, mt_getrandmax()) . 'spip'), 0, 8);
331
-			}
332
-		}
333
-
334
-		if (is_string($boundary) && strlen($boundary)) {
335
-			// fabrique une chaine HTTP pour un POST avec boundary
336
-			$entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n";
337
-			if (is_array($donnees)) {
338
-				foreach ($donnees as $cle => $valeur) {
339
-					if (is_array($valeur)) {
340
-						foreach ($valeur as $val2) {
341
-							$chaine .= "\r\n--$boundary\r\n";
342
-							$chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n";
343
-							$chaine .= "\r\n";
344
-							$chaine .= $val2;
345
-						}
346
-					} else {
347
-						$chaine .= "\r\n--$boundary\r\n";
348
-						$chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n";
349
-						$chaine .= "\r\n";
350
-						$chaine .= $valeur;
351
-					}
352
-				}
353
-				$chaine .= "\r\n--$boundary\r\n";
354
-			}
355
-		} else {
356
-			// fabrique une chaine HTTP simple pour un POST
357
-			$entete = "Content-Type: application/x-www-form-urlencoded\r\n";
358
-			if (is_array($donnees)) {
359
-				$chaines = [];
360
-				foreach ($donnees as $cle => $valeur) {
361
-					if (is_array($valeur)) {
362
-						foreach ($valeur as $val2) {
363
-							$chaines[] = rawurlencode($cle) . '[]=' . rawurlencode((string) $val2);
364
-						}
365
-					} else {
366
-						$chaines[] = rawurlencode($cle) . '=' . rawurlencode((string) $valeur);
367
-					}
368
-				}
369
-				$chaine = implode('&', $chaines);
370
-				unset($chaines);
371
-			} else {
372
-				$chaine = $donnees;
373
-			}
374
-		}
375
-	}
376
-
377
-	return [$entete, $chaine];
297
+    // permettre a la fonction qui a demande le post de formater elle meme ses donnees
298
+    // pour un appel soap par exemple
299
+    // l'entete est separe des donnees par un double retour a la ligne
300
+    // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n
301
+    $chaine = '';
302
+    if (is_string($donnees) && strlen($donnees)) {
303
+        $entete = '';
304
+        // on repasse tous les \r\n et \r en simples \n
305
+        $donnees = str_replace("\r\n", "\n", $donnees);
306
+        $donnees = str_replace("\r", "\n", $donnees);
307
+        // un double retour a la ligne signifie la fin de l'entete et le debut des donnees
308
+        $p = strpos($donnees, "\n\n");
309
+        if ($p !== false) {
310
+            $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1));
311
+            $donnees = substr($donnees, $p + 2);
312
+        }
313
+        $chaine = str_replace("\n", "\r\n", $donnees);
314
+    } else {
315
+        /* boundary automatique */
316
+        // Si on a plus de 500 octects de donnees, on "boundarise"
317
+        if ($boundary === '') {
318
+            $taille = 0;
319
+            foreach ($donnees as $cle => $valeur) {
320
+                if (is_array($valeur)) {
321
+                    foreach ($valeur as $val2) {
322
+                        $taille += strlen((string) $val2);
323
+                    }
324
+                } else {
325
+                    // faut-il utiliser spip_strlen() dans inc/charsets ?
326
+                    $taille += strlen((string) $valeur);
327
+                }
328
+            }
329
+            if ($taille > 500) {
330
+                $boundary = substr(md5(random_int(0, mt_getrandmax()) . 'spip'), 0, 8);
331
+            }
332
+        }
333
+
334
+        if (is_string($boundary) && strlen($boundary)) {
335
+            // fabrique une chaine HTTP pour un POST avec boundary
336
+            $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n";
337
+            if (is_array($donnees)) {
338
+                foreach ($donnees as $cle => $valeur) {
339
+                    if (is_array($valeur)) {
340
+                        foreach ($valeur as $val2) {
341
+                            $chaine .= "\r\n--$boundary\r\n";
342
+                            $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n";
343
+                            $chaine .= "\r\n";
344
+                            $chaine .= $val2;
345
+                        }
346
+                    } else {
347
+                        $chaine .= "\r\n--$boundary\r\n";
348
+                        $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n";
349
+                        $chaine .= "\r\n";
350
+                        $chaine .= $valeur;
351
+                    }
352
+                }
353
+                $chaine .= "\r\n--$boundary\r\n";
354
+            }
355
+        } else {
356
+            // fabrique une chaine HTTP simple pour un POST
357
+            $entete = "Content-Type: application/x-www-form-urlencoded\r\n";
358
+            if (is_array($donnees)) {
359
+                $chaines = [];
360
+                foreach ($donnees as $cle => $valeur) {
361
+                    if (is_array($valeur)) {
362
+                        foreach ($valeur as $val2) {
363
+                            $chaines[] = rawurlencode($cle) . '[]=' . rawurlencode((string) $val2);
364
+                        }
365
+                    } else {
366
+                        $chaines[] = rawurlencode($cle) . '=' . rawurlencode((string) $valeur);
367
+                    }
368
+                }
369
+                $chaine = implode('&', $chaines);
370
+                unset($chaines);
371
+            } else {
372
+                $chaine = $donnees;
373
+            }
374
+        }
375
+    }
376
+
377
+    return [$entete, $chaine];
378 378
 }
379 379
 
380 380
 /**
@@ -385,19 +385,19 @@  discard block
 block discarded – undo
385 385
  */
386 386
 function url_to_ascii($url_idn) {
387 387
 
388
-	if ($parts = parse_url($url_idn)) {
389
-		$host = $parts['host'];
390
-		if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) {
391
-			$converter = new ToIdn();
392
-			$host_ascii = $converter->convert($host);
393
-			$url_idn = explode($host, $url_idn, 2);
394
-			$url_idn = implode($host_ascii, $url_idn);
395
-		}
396
-		// et on urlencode les char utf si besoin dans le path
397
-		$url_idn = preg_replace_callback('/[^\x20-\x7f]/', fn($match) => urlencode((string) $match[0]), $url_idn);
398
-	}
399
-
400
-	return $url_idn;
388
+    if ($parts = parse_url($url_idn)) {
389
+        $host = $parts['host'];
390
+        if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) {
391
+            $converter = new ToIdn();
392
+            $host_ascii = $converter->convert($host);
393
+            $url_idn = explode($host, $url_idn, 2);
394
+            $url_idn = implode($host_ascii, $url_idn);
395
+        }
396
+        // et on urlencode les char utf si besoin dans le path
397
+        $url_idn = preg_replace_callback('/[^\x20-\x7f]/', fn($match) => urlencode((string) $match[0]), $url_idn);
398
+    }
399
+
400
+    return $url_idn;
401 401
 }
402 402
 
403 403
 /**
@@ -439,223 +439,223 @@  discard block
 block discarded – undo
439 439
  *     string file : nom du fichier si enregistre dans un fichier
440 440
  */
441 441
 function recuperer_url($url, $options = []) {
442
-	// Conserve la mémoire de la méthode fournit éventuellement
443
-	$methode_demandee = $options['methode'] ?? '';
444
-	$default = [
445
-		'transcoder' => false,
446
-		'methode' => 'GET',
447
-		'taille_max' => null,
448
-		'headers' => [],
449
-		'datas' => '',
450
-		'boundary' => '',
451
-		'refuser_gz' => false,
452
-		'if_modified_since' => '',
453
-		'uri_referer' => '',
454
-		'file' => '',
455
-		'follow_location' => 10,
456
-		'version_http' => _INC_DISTANT_VERSION_HTTP,
457
-	];
458
-	$options = array_merge($default, $options);
459
-	// copier directement dans un fichier ?
460
-	$copy = $options['file'];
461
-
462
-	if ($options['methode'] == 'HEAD') {
463
-		$options['taille_max'] = 0;
464
-	}
465
-	if (is_null($options['taille_max'])) {
466
-		$options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;
467
-	}
468
-
469
-	spip_log('recuperer_url ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG);
470
-
471
-	// Ajout des en-têtes spécifiques si besoin
472
-	$formatted_data = '';
473
-	if (!empty($options['headers'])) {
474
-		foreach ($options['headers'] as $champ => $valeur) {
475
-			$formatted_data .= $champ . ': ' . $valeur . "\r\n";
476
-		}
477
-	}
478
-
479
-	if (!empty($options['datas'])) {
480
-		[$head, $postdata] = prepare_donnees_post($options['datas'], $options['boundary']);
481
-		$head .= $formatted_data;
482
-		if (stripos($head, 'Content-Length:') === false) {
483
-			$head .= 'Content-Length: ' . strlen((string) $postdata) . "\r\n";
484
-		}
485
-		$formatted_data = $head . "\r\n" . $postdata;
486
-		if (
487
-			strlen((string) $postdata) && !$methode_demandee
488
-		) {
489
-			$options['methode'] = 'POST';
490
-		}
491
-	} elseif ($formatted_data) {
492
-		$formatted_data .= "\r\n";
493
-	}
494
-
495
-	// Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole
496
-	$url = preg_replace(',^feed://,i', 'http://', $url);
497
-	if (!tester_url_absolue($url)) {
498
-		$url = 'http://' . $url;
499
-	} elseif (str_starts_with($url, '//')) {
500
-		$url = 'http:' . $url;
501
-	}
502
-
503
-	$url = url_to_ascii($url);
504
-
505
-	$result = [
506
-		'status' => 0,
507
-		'headers' => '',
508
-		'page' => '',
509
-		'length' => 0,
510
-		'last_modified' => '',
511
-		'location' => '',
512
-		'url' => $url
513
-	];
514
-
515
-	// si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz
516
-	$refuser_gz = ($options['refuser_gz'] || $copy);
517
-
518
-	// ouvrir la connexion et envoyer la requete et ses en-tetes
519
-	[$handle, $fopen] = init_http(
520
-		$options['methode'],
521
-		$url,
522
-		$refuser_gz,
523
-		$options['uri_referer'],
524
-		$formatted_data,
525
-		$options['version_http'],
526
-		$options['if_modified_since']
527
-	);
528
-	if (!$handle) {
529
-		spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR);
530
-
531
-		return false;
532
-	}
533
-
534
-	// Sauf en fopen, envoyer le flux d'entree
535
-	// et recuperer les en-tetes de reponses
536
-	if (!$fopen) {
537
-		$res = recuperer_entetes_complets($handle, $options['if_modified_since']);
538
-		if (!$res) {
539
-			fclose($handle);
540
-			$t = @parse_url($url);
541
-			$host = $t['host'];
542
-			// Chinoisierie inexplicable pour contrer
543
-			// les actions liberticides de l'empire du milieu
544
-			if (
545
-				!need_proxy($host)
546
-				&& ($res = @file_get_contents($url))
547
-			) {
548
-				$result['length'] = strlen($res);
549
-				if ($copy) {
550
-					ecrire_fichier($copy, $res);
551
-					$result['file'] = $copy;
552
-				} else {
553
-					$result['page'] = $res;
554
-				}
555
-				$res = [
556
-					'status' => 200,
557
-				];
558
-			} else {
559
-				spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR);
560
-				return false;
561
-			}
562
-		} elseif ($res['location'] && $options['follow_location']) {
563
-			$options['follow_location']--;
564
-			fclose($handle);
565
-			include_spip('inc/filtres');
566
-			$url = suivre_lien($url, $res['location']);
567
-
568
-			// une redirection doit se faire en GET, sauf status explicite 307 ou 308 qui indique de garder la meme methode
569
-			if (
570
-				$options['methode'] !== 'GET'
571
-				&& (empty($res['status']) || !in_array($res['status'], [307, 308]))
572
-			) {
573
-				$options['methode'] = 'GET';
574
-				$options['datas'] = '';
575
-			}
576
-			spip_log('recuperer_url recommence ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG);
577
-
578
-			return recuperer_url($url, $options);
579
-		} elseif ($res['status'] !== 200) {
580
-			spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant');
581
-		}
582
-		$result['status'] = $res['status'];
583
-		if (isset($res['headers'])) {
584
-			$result['headers'] = $res['headers'];
585
-		}
586
-		if (isset($res['last_modified'])) {
587
-			$result['last_modified'] = $res['last_modified'];
588
-		}
589
-		if (isset($res['location'])) {
590
-			$result['location'] = $res['location'];
591
-		}
592
-		if (isset($res['content_length'])) {
593
-			$result['content_length'] = $res['content_length'];
594
-		}
595
-	}
596
-
597
-	// on ne veut que les entetes
598
-	if (!$options['taille_max'] || $options['methode'] == 'HEAD' || $result['status'] == '304') {
599
-		spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($result, JSON_THROW_ON_ERROR), 'distant' . _LOG_DEBUG);
600
-		return $result;
601
-	}
602
-
603
-
604
-	// s'il faut deballer, le faire via un fichier temporaire
605
-	// sinon la memoire explose pour les gros flux
606
-
607
-	$gz = false;
608
-	if (preg_match(",\bContent-Encoding: .*gzip,is", (string) $result['headers'])) {
609
-		$gz = (_DIR_TMP . md5(uniqid(random_int(0, mt_getrandmax()))) . '.tmp.gz');
610
-	}
611
-
612
-	// si on n'a pas deja récupéré le contenu par une methode detournée
613
-	if (!$result['length']) {
614
-		$taille_max = $options['taille_max'];
615
-		if (isset($result['content_length'])
616
-		  && !empty($result['content_length'])
617
-		  && ($result['content_length'] < $taille_max)) {
618
-			$taille_max = $result['content_length'];
619
-		}
620
-		$res = recuperer_body($handle, $taille_max, $gz ?: $copy);
621
-		fclose($handle);
622
-		if ($copy) {
623
-			$result['length'] = $res;
624
-			$result['file'] = $copy;
625
-		} elseif ($res) {
626
-			$result['page'] = &$res;
627
-			$result['length'] = strlen($result['page']);
628
-		}
629
-		if (!$result['status']) {
630
-			$result['status'] = 200; // on a reussi, donc !
631
-		}
632
-	}
633
-	if (!$result['page']) {
634
-		return $result;
635
-	}
636
-
637
-	// Decompresser au besoin
638
-	if ($gz) {
639
-		$result['page'] = implode('', gzfile($gz));
640
-		supprimer_fichier($gz);
641
-	}
642
-
643
-	// Faut-il l'importer dans notre charset local ?
644
-	if ($options['transcoder']) {
645
-		include_spip('inc/charsets');
646
-		$result['page'] = transcoder_page($result['page'], $result['headers']);
647
-	}
648
-
649
-	try {
650
-		$trace = json_decode(json_encode($result, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR);
651
-	} catch (JsonException $e) {
652
-		$trace = [];
653
-		spip_log('Failed to parse Json data : ' . $e->getMessage(), _LOG_ERREUR);
654
-	}
655
-	$trace['page'] = '...';
656
-	spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($trace, JSON_THROW_ON_ERROR), 'distant' . _LOG_DEBUG);
657
-
658
-	return $result;
442
+    // Conserve la mémoire de la méthode fournit éventuellement
443
+    $methode_demandee = $options['methode'] ?? '';
444
+    $default = [
445
+        'transcoder' => false,
446
+        'methode' => 'GET',
447
+        'taille_max' => null,
448
+        'headers' => [],
449
+        'datas' => '',
450
+        'boundary' => '',
451
+        'refuser_gz' => false,
452
+        'if_modified_since' => '',
453
+        'uri_referer' => '',
454
+        'file' => '',
455
+        'follow_location' => 10,
456
+        'version_http' => _INC_DISTANT_VERSION_HTTP,
457
+    ];
458
+    $options = array_merge($default, $options);
459
+    // copier directement dans un fichier ?
460
+    $copy = $options['file'];
461
+
462
+    if ($options['methode'] == 'HEAD') {
463
+        $options['taille_max'] = 0;
464
+    }
465
+    if (is_null($options['taille_max'])) {
466
+        $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;
467
+    }
468
+
469
+    spip_log('recuperer_url ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG);
470
+
471
+    // Ajout des en-têtes spécifiques si besoin
472
+    $formatted_data = '';
473
+    if (!empty($options['headers'])) {
474
+        foreach ($options['headers'] as $champ => $valeur) {
475
+            $formatted_data .= $champ . ': ' . $valeur . "\r\n";
476
+        }
477
+    }
478
+
479
+    if (!empty($options['datas'])) {
480
+        [$head, $postdata] = prepare_donnees_post($options['datas'], $options['boundary']);
481
+        $head .= $formatted_data;
482
+        if (stripos($head, 'Content-Length:') === false) {
483
+            $head .= 'Content-Length: ' . strlen((string) $postdata) . "\r\n";
484
+        }
485
+        $formatted_data = $head . "\r\n" . $postdata;
486
+        if (
487
+            strlen((string) $postdata) && !$methode_demandee
488
+        ) {
489
+            $options['methode'] = 'POST';
490
+        }
491
+    } elseif ($formatted_data) {
492
+        $formatted_data .= "\r\n";
493
+    }
494
+
495
+    // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole
496
+    $url = preg_replace(',^feed://,i', 'http://', $url);
497
+    if (!tester_url_absolue($url)) {
498
+        $url = 'http://' . $url;
499
+    } elseif (str_starts_with($url, '//')) {
500
+        $url = 'http:' . $url;
501
+    }
502
+
503
+    $url = url_to_ascii($url);
504
+
505
+    $result = [
506
+        'status' => 0,
507
+        'headers' => '',
508
+        'page' => '',
509
+        'length' => 0,
510
+        'last_modified' => '',
511
+        'location' => '',
512
+        'url' => $url
513
+    ];
514
+
515
+    // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz
516
+    $refuser_gz = ($options['refuser_gz'] || $copy);
517
+
518
+    // ouvrir la connexion et envoyer la requete et ses en-tetes
519
+    [$handle, $fopen] = init_http(
520
+        $options['methode'],
521
+        $url,
522
+        $refuser_gz,
523
+        $options['uri_referer'],
524
+        $formatted_data,
525
+        $options['version_http'],
526
+        $options['if_modified_since']
527
+    );
528
+    if (!$handle) {
529
+        spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR);
530
+
531
+        return false;
532
+    }
533
+
534
+    // Sauf en fopen, envoyer le flux d'entree
535
+    // et recuperer les en-tetes de reponses
536
+    if (!$fopen) {
537
+        $res = recuperer_entetes_complets($handle, $options['if_modified_since']);
538
+        if (!$res) {
539
+            fclose($handle);
540
+            $t = @parse_url($url);
541
+            $host = $t['host'];
542
+            // Chinoisierie inexplicable pour contrer
543
+            // les actions liberticides de l'empire du milieu
544
+            if (
545
+                !need_proxy($host)
546
+                && ($res = @file_get_contents($url))
547
+            ) {
548
+                $result['length'] = strlen($res);
549
+                if ($copy) {
550
+                    ecrire_fichier($copy, $res);
551
+                    $result['file'] = $copy;
552
+                } else {
553
+                    $result['page'] = $res;
554
+                }
555
+                $res = [
556
+                    'status' => 200,
557
+                ];
558
+            } else {
559
+                spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR);
560
+                return false;
561
+            }
562
+        } elseif ($res['location'] && $options['follow_location']) {
563
+            $options['follow_location']--;
564
+            fclose($handle);
565
+            include_spip('inc/filtres');
566
+            $url = suivre_lien($url, $res['location']);
567
+
568
+            // une redirection doit se faire en GET, sauf status explicite 307 ou 308 qui indique de garder la meme methode
569
+            if (
570
+                $options['methode'] !== 'GET'
571
+                && (empty($res['status']) || !in_array($res['status'], [307, 308]))
572
+            ) {
573
+                $options['methode'] = 'GET';
574
+                $options['datas'] = '';
575
+            }
576
+            spip_log('recuperer_url recommence ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG);
577
+
578
+            return recuperer_url($url, $options);
579
+        } elseif ($res['status'] !== 200) {
580
+            spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant');
581
+        }
582
+        $result['status'] = $res['status'];
583
+        if (isset($res['headers'])) {
584
+            $result['headers'] = $res['headers'];
585
+        }
586
+        if (isset($res['last_modified'])) {
587
+            $result['last_modified'] = $res['last_modified'];
588
+        }
589
+        if (isset($res['location'])) {
590
+            $result['location'] = $res['location'];
591
+        }
592
+        if (isset($res['content_length'])) {
593
+            $result['content_length'] = $res['content_length'];
594
+        }
595
+    }
596
+
597
+    // on ne veut que les entetes
598
+    if (!$options['taille_max'] || $options['methode'] == 'HEAD' || $result['status'] == '304') {
599
+        spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($result, JSON_THROW_ON_ERROR), 'distant' . _LOG_DEBUG);
600
+        return $result;
601
+    }
602
+
603
+
604
+    // s'il faut deballer, le faire via un fichier temporaire
605
+    // sinon la memoire explose pour les gros flux
606
+
607
+    $gz = false;
608
+    if (preg_match(",\bContent-Encoding: .*gzip,is", (string) $result['headers'])) {
609
+        $gz = (_DIR_TMP . md5(uniqid(random_int(0, mt_getrandmax()))) . '.tmp.gz');
610
+    }
611
+
612
+    // si on n'a pas deja récupéré le contenu par une methode detournée
613
+    if (!$result['length']) {
614
+        $taille_max = $options['taille_max'];
615
+        if (isset($result['content_length'])
616
+          && !empty($result['content_length'])
617
+          && ($result['content_length'] < $taille_max)) {
618
+            $taille_max = $result['content_length'];
619
+        }
620
+        $res = recuperer_body($handle, $taille_max, $gz ?: $copy);
621
+        fclose($handle);
622
+        if ($copy) {
623
+            $result['length'] = $res;
624
+            $result['file'] = $copy;
625
+        } elseif ($res) {
626
+            $result['page'] = &$res;
627
+            $result['length'] = strlen($result['page']);
628
+        }
629
+        if (!$result['status']) {
630
+            $result['status'] = 200; // on a reussi, donc !
631
+        }
632
+    }
633
+    if (!$result['page']) {
634
+        return $result;
635
+    }
636
+
637
+    // Decompresser au besoin
638
+    if ($gz) {
639
+        $result['page'] = implode('', gzfile($gz));
640
+        supprimer_fichier($gz);
641
+    }
642
+
643
+    // Faut-il l'importer dans notre charset local ?
644
+    if ($options['transcoder']) {
645
+        include_spip('inc/charsets');
646
+        $result['page'] = transcoder_page($result['page'], $result['headers']);
647
+    }
648
+
649
+    try {
650
+        $trace = json_decode(json_encode($result, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR);
651
+    } catch (JsonException $e) {
652
+        $trace = [];
653
+        spip_log('Failed to parse Json data : ' . $e->getMessage(), _LOG_ERREUR);
654
+    }
655
+    $trace['page'] = '...';
656
+    spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($trace, JSON_THROW_ON_ERROR), 'distant' . _LOG_DEBUG);
657
+
658
+    return $result;
659 659
 }
660 660
 
661 661
 /**
@@ -671,73 +671,73 @@  discard block
 block discarded – undo
671 671
  * @return array|bool|mixed
672 672
  */
673 673
 function recuperer_url_cache($url, $options = []) {
674
-	if (!defined('_DELAI_RECUPERER_URL_CACHE')) {
675
-		define('_DELAI_RECUPERER_URL_CACHE', 3600);
676
-	}
677
-	$default = [
678
-		'transcoder' => false,
679
-		'methode' => 'GET',
680
-		'taille_max' => null,
681
-		'datas' => '',
682
-		'boundary' => '',
683
-		'refuser_gz' => false,
684
-		'if_modified_since' => '',
685
-		'uri_referer' => '',
686
-		'file' => '',
687
-		'follow_location' => 10,
688
-		'version_http' => _INC_DISTANT_VERSION_HTTP,
689
-		'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE,
690
-	];
691
-	$options = array_merge($default, $options);
692
-
693
-	// cas ou il n'est pas possible de cacher
694
-	if (!empty($options['data']) || $options['methode'] == 'POST') {
695
-		return recuperer_url($url, $options);
696
-	}
697
-
698
-	// ne pas tenter plusieurs fois la meme url en erreur (non cachee donc)
699
-	static $errors = [];
700
-	if (isset($errors[$url])) {
701
-		return $errors[$url];
702
-	}
703
-
704
-	$sig = $options;
705
-	unset($sig['if_modified_since']);
706
-	unset($sig['delai_cache']);
707
-	$sig['url'] = $url;
708
-
709
-	$dir = sous_repertoire(_DIR_CACHE, 'curl');
710
-	$cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80);
711
-	$sub = sous_repertoire($dir, substr($cache, 0, 2));
712
-	$cache = "$sub$cache";
713
-
714
-	$res = false;
715
-	$is_cached = file_exists($cache);
716
-	if (
717
-		$is_cached
718
-		&& filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']
719
-	) {
720
-		lire_fichier($cache, $res);
721
-		if ($res = unserialize($res)) {
722
-			// mettre le last_modified et le status=304 ?
723
-		}
724
-	}
725
-	if (!$res) {
726
-		$res = recuperer_url($url, $options);
727
-		// ne pas recharger cette url non cachee dans le meme hit puisque non disponible
728
-		if (!$res) {
729
-			if ($is_cached) {
730
-				// on a pas reussi a recuperer mais on avait un cache : l'utiliser
731
-				lire_fichier($cache, $res);
732
-				$res = unserialize($res);
733
-			}
734
-
735
-			return $errors[$url] = $res;
736
-		}
737
-		ecrire_fichier($cache, serialize($res));
738
-	}
739
-
740
-	return $res;
674
+    if (!defined('_DELAI_RECUPERER_URL_CACHE')) {
675
+        define('_DELAI_RECUPERER_URL_CACHE', 3600);
676
+    }
677
+    $default = [
678
+        'transcoder' => false,
679
+        'methode' => 'GET',
680
+        'taille_max' => null,
681
+        'datas' => '',
682
+        'boundary' => '',
683
+        'refuser_gz' => false,
684
+        'if_modified_since' => '',
685
+        'uri_referer' => '',
686
+        'file' => '',
687
+        'follow_location' => 10,
688
+        'version_http' => _INC_DISTANT_VERSION_HTTP,
689
+        'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE,
690
+    ];
691
+    $options = array_merge($default, $options);
692
+
693
+    // cas ou il n'est pas possible de cacher
694
+    if (!empty($options['data']) || $options['methode'] == 'POST') {
695
+        return recuperer_url($url, $options);
696
+    }
697
+
698
+    // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc)
699
+    static $errors = [];
700
+    if (isset($errors[$url])) {
701
+        return $errors[$url];
702
+    }
703
+
704
+    $sig = $options;
705
+    unset($sig['if_modified_since']);
706
+    unset($sig['delai_cache']);
707
+    $sig['url'] = $url;
708
+
709
+    $dir = sous_repertoire(_DIR_CACHE, 'curl');
710
+    $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80);
711
+    $sub = sous_repertoire($dir, substr($cache, 0, 2));
712
+    $cache = "$sub$cache";
713
+
714
+    $res = false;
715
+    $is_cached = file_exists($cache);
716
+    if (
717
+        $is_cached
718
+        && filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']
719
+    ) {
720
+        lire_fichier($cache, $res);
721
+        if ($res = unserialize($res)) {
722
+            // mettre le last_modified et le status=304 ?
723
+        }
724
+    }
725
+    if (!$res) {
726
+        $res = recuperer_url($url, $options);
727
+        // ne pas recharger cette url non cachee dans le meme hit puisque non disponible
728
+        if (!$res) {
729
+            if ($is_cached) {
730
+                // on a pas reussi a recuperer mais on avait un cache : l'utiliser
731
+                lire_fichier($cache, $res);
732
+                $res = unserialize($res);
733
+            }
734
+
735
+            return $errors[$url] = $res;
736
+        }
737
+        ecrire_fichier($cache, serialize($res));
738
+    }
739
+
740
+    return $res;
741 741
 }
742 742
 
743 743
 /**
@@ -755,72 +755,72 @@  discard block
 block discarded – undo
755 755
  *   string contenu de la resource
756 756
  */
757 757
 function recuperer_body($handle, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = '') {
758
-	$tmpfile = null;
759
-	$taille = 0;
760
-	$result = '';
761
-	$fp = false;
762
-	if ($fichier) {
763
-		include_spip('inc/acces');
764
-		$tmpfile = "$fichier." . creer_uniqid() . '.tmp';
765
-		$fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX);
766
-		if (!$fp && file_exists($fichier)) {
767
-			return filesize($fichier);
768
-		}
769
-		if (!$fp) {
770
-			return false;
771
-		}
772
-		$result = 0; // on renvoie la taille du fichier
773
-	}
774
-
775
-	$max_longueur_morceaux = 16384;
776
-	while (!feof($handle) && $taille < $taille_max) {
777
-		// ne pas lire plus que ce qu'on a besoin (ou que la longueur annoncée du document)
778
-		$max_longueur_morceaux = min($max_longueur_morceaux, $taille_max - $taille);
779
-		$res = fread($handle, $max_longueur_morceaux);
780
-
781
-		// si feof ne trig pas mais on est à la fin, fread retourne false
782
-		if ($res === false) {
783
-			break;
784
-		}
785
-
786
-		$taille_morceau = strlen($res);
787
-		$taille += $taille_morceau;
788
-
789
-		if ($fp) {
790
-			fwrite($fp, $res);
791
-			$result = $taille;
792
-		} else {
793
-			$result .= $res;
794
-		}
795
-
796
-		// si on a un morceau plus court que taille maxi mais que feof ne trig pas
797
-		// reduire le timeout par précaution, car on a sûrement atteint la fin et le prochain fread va sinon durer 10s
798
-		if (($taille_morceau < $max_longueur_morceaux) && !feof($handle)) {
799
-
800
-			// si les informations du stream nous indiquent qu'il n'y a plus rien à lire, on sort
801
-			$metadata = @stream_get_meta_data($handle);
802
-			if ($metadata && isset($metadata['unread_bytes']) && ($metadata['unread_bytes'] === 0)) {
803
-				break;
804
-			}
805
-
806
-			// sinon on réduit le timeout pour éviter de rester bloqué à attendre un serveur qui
807
-			// n'implémente pas le connection:close correctement
808
-			if (_INC_DISTANT_CONNECT_TIMEOUT > 1) {
809
-				@stream_set_timeout($handle, 1);
810
-			}
811
-		}
812
-	}
813
-
814
-	if ($fp) {
815
-		spip_fclose_unlock($fp);
816
-		spip_unlink($fichier);
817
-		@rename($tmpfile, $fichier);
818
-		if (!file_exists($fichier)) {
819
-			return false;
820
-		}
821
-	}
822
-
823
-	return $result;
758
+    $tmpfile = null;
759
+    $taille = 0;
760
+    $result = '';
761
+    $fp = false;
762
+    if ($fichier) {
763
+        include_spip('inc/acces');
764
+        $tmpfile = "$fichier." . creer_uniqid() . '.tmp';
765
+        $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX);
766
+        if (!$fp && file_exists($fichier)) {
767
+            return filesize($fichier);
768
+        }
769
+        if (!$fp) {
770
+            return false;
771
+        }
772
+        $result = 0; // on renvoie la taille du fichier
773
+    }
774
+
775
+    $max_longueur_morceaux = 16384;
776
+    while (!feof($handle) && $taille < $taille_max) {
777
+        // ne pas lire plus que ce qu'on a besoin (ou que la longueur annoncée du document)
778
+        $max_longueur_morceaux = min($max_longueur_morceaux, $taille_max - $taille);
779
+        $res = fread($handle, $max_longueur_morceaux);
780
+
781
+        // si feof ne trig pas mais on est à la fin, fread retourne false
782
+        if ($res === false) {
783
+            break;
784
+        }
785
+
786
+        $taille_morceau = strlen($res);
787
+        $taille += $taille_morceau;
788
+
789
+        if ($fp) {
790
+            fwrite($fp, $res);
791
+            $result = $taille;
792
+        } else {
793
+            $result .= $res;
794
+        }
795
+
796
+        // si on a un morceau plus court que taille maxi mais que feof ne trig pas
797
+        // reduire le timeout par précaution, car on a sûrement atteint la fin et le prochain fread va sinon durer 10s
798
+        if (($taille_morceau < $max_longueur_morceaux) && !feof($handle)) {
799
+
800
+            // si les informations du stream nous indiquent qu'il n'y a plus rien à lire, on sort
801
+            $metadata = @stream_get_meta_data($handle);
802
+            if ($metadata && isset($metadata['unread_bytes']) && ($metadata['unread_bytes'] === 0)) {
803
+                break;
804
+            }
805
+
806
+            // sinon on réduit le timeout pour éviter de rester bloqué à attendre un serveur qui
807
+            // n'implémente pas le connection:close correctement
808
+            if (_INC_DISTANT_CONNECT_TIMEOUT > 1) {
809
+                @stream_set_timeout($handle, 1);
810
+            }
811
+        }
812
+    }
813
+
814
+    if ($fp) {
815
+        spip_fclose_unlock($fp);
816
+        spip_unlink($fichier);
817
+        @rename($tmpfile, $fichier);
818
+        if (!file_exists($fichier)) {
819
+            return false;
820
+        }
821
+    }
822
+
823
+    return $result;
824 824
 }
825 825
 
826 826
 /**
@@ -842,38 +842,38 @@  discard block
 block discarded – undo
842 842
  *   string location
843 843
  */
844 844
 function recuperer_entetes_complets($handle, $if_modified_since = false) {
845
-	$result = ['status' => 0, 'headers' => [], 'last_modified' => 0, 'location' => ''];
846
-
847
-	$s = @trim(fgets($handle, 16384));
848
-	if (!preg_match(',^HTTP/\d+\.\d+ (\d+),', $s, $r)) {
849
-		return false;
850
-	}
851
-	$result['status'] = (int) $r[1];
852
-	while ($s = trim(fgets($handle, 16384))) {
853
-		$result['headers'][] = $s . "\n";
854
-		preg_match(',^([^:]*): *(.*)$,i', $s, $r);
855
-		[, $d, $v] = $r;
856
-		$d = strtolower(trim($d));
857
-		if ( $d === 'location' && $result['status'] >= 300 && $result['status'] < 400) {
858
-			$result['location'] = $v;
859
-		} elseif ($d === 'last-modified') {
860
-			$result['last_modified'] = strtotime($v);
861
-		} elseif ($d === 'content-length' and strlen(trim($v))) {
862
-			$result['content_length'] = intval($v);
863
-		}
864
-	}
865
-	if (
866
-		$if_modified_since
867
-		&& $result['last_modified']
868
-		&& $if_modified_since > $result['last_modified']
869
-		&& $result['status'] == 200
870
-	) {
871
-		$result['status'] = 304;
872
-	}
873
-
874
-	$result['headers'] = implode('', $result['headers']);
875
-
876
-	return $result;
845
+    $result = ['status' => 0, 'headers' => [], 'last_modified' => 0, 'location' => ''];
846
+
847
+    $s = @trim(fgets($handle, 16384));
848
+    if (!preg_match(',^HTTP/\d+\.\d+ (\d+),', $s, $r)) {
849
+        return false;
850
+    }
851
+    $result['status'] = (int) $r[1];
852
+    while ($s = trim(fgets($handle, 16384))) {
853
+        $result['headers'][] = $s . "\n";
854
+        preg_match(',^([^:]*): *(.*)$,i', $s, $r);
855
+        [, $d, $v] = $r;
856
+        $d = strtolower(trim($d));
857
+        if ( $d === 'location' && $result['status'] >= 300 && $result['status'] < 400) {
858
+            $result['location'] = $v;
859
+        } elseif ($d === 'last-modified') {
860
+            $result['last_modified'] = strtotime($v);
861
+        } elseif ($d === 'content-length' and strlen(trim($v))) {
862
+            $result['content_length'] = intval($v);
863
+        }
864
+    }
865
+    if (
866
+        $if_modified_since
867
+        && $result['last_modified']
868
+        && $if_modified_since > $result['last_modified']
869
+        && $result['status'] == 200
870
+    ) {
871
+        $result['status'] = 304;
872
+    }
873
+
874
+    $result['headers'] = implode('', $result['headers']);
875
+
876
+    return $result;
877 877
 }
878 878
 
879 879
 /**
@@ -895,36 +895,36 @@  discard block
 block discarded – undo
895 895
  *     Nom du fichier pour copie locale
896 896
  **/
897 897
 function nom_fichier_copie_locale($source, $extension) {
898
-	include_spip('inc/documents');
898
+    include_spip('inc/documents');
899 899
 
900
-	$d = creer_repertoire_documents('distant'); # IMG/distant/
901
-	$d = sous_repertoire($d, $extension); # IMG/distant/pdf/
900
+    $d = creer_repertoire_documents('distant'); # IMG/distant/
901
+    $d = sous_repertoire($d, $extension); # IMG/distant/pdf/
902 902
 
903
-	// on se place tout le temps comme si on était a la racine
904
-	if (_DIR_RACINE) {
905
-		$d = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', (string) $d);
906
-	}
903
+    // on se place tout le temps comme si on était a la racine
904
+    if (_DIR_RACINE) {
905
+        $d = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', (string) $d);
906
+    }
907 907
 
908
-	$m = md5($source);
908
+    $m = md5($source);
909 909
 
910
-	$filename =
911
-		$d
912
-		. substr(preg_replace(',[^\w-],', '', basename($source, $extension)), 0, 16)
913
-		. '-' . substr($m, 0, 8)
914
-		. ".$extension";
910
+    $filename =
911
+        $d
912
+        . substr(preg_replace(',[^\w-],', '', basename($source, $extension)), 0, 16)
913
+        . '-' . substr($m, 0, 8)
914
+        . ".$extension";
915 915
 
916
-	// ancien nommage des fichiers distants : renommer le fichier a la volee si besoin pour eviter de dupliquer les caches
917
-	$legacy_filename =
918
-		$d
919
-		. substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12)
920
-		. substr($m, 0, 4)
921
-		. ".$extension";
916
+    // ancien nommage des fichiers distants : renommer le fichier a la volee si besoin pour eviter de dupliquer les caches
917
+    $legacy_filename =
918
+        $d
919
+        . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12)
920
+        . substr($m, 0, 4)
921
+        . ".$extension";
922 922
 
923
-	if (file_exists(_DIR_RACINE . $legacy_filename)) {
924
-		@rename(_DIR_RACINE . $legacy_filename, $filename);
925
-	}
923
+    if (file_exists(_DIR_RACINE . $legacy_filename)) {
924
+        @rename(_DIR_RACINE . $legacy_filename, $filename);
925
+    }
926 926
 
927
-	return $filename;
927
+    return $filename;
928 928
 }
929 929
 
930 930
 /**
@@ -943,72 +943,72 @@  discard block
 block discarded – undo
943 943
  *      - null: Copie locale impossible
944 944
  **/
945 945
 function fichier_copie_locale($source) {
946
-	// Si c'est deja local pas de souci
947
-	if (!tester_url_absolue($source)) {
948
-		if (_DIR_RACINE) {
949
-			$source = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', $source);
950
-		}
951
-
952
-		return $source;
953
-	}
954
-
955
-	// optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier
956
-	// a deja ete copie en local avec cette extension
957
-	// dans ce cas elle est fiable, pas la peine de requeter en base
958
-	$path_parts = pathinfo($source);
959
-	if (!isset($path_parts['extension'])) {
960
-		$path_parts['extension'] = '';
961
-	}
962
-	$ext = $path_parts ? $path_parts['extension'] : '';
963
-	if (
964
-		$ext
965
-		&& preg_match(',^\w+$,', $ext)
966
-		&& ($f = nom_fichier_copie_locale($source, $ext))
967
-		&& file_exists(_DIR_RACINE . $f)
968
-	) {
969
-		return $f;
970
-	}
971
-
972
-
973
-	// Si c'est deja dans la table des documents,
974
-	// ramener le nom de sa copie potentielle
975
-	$ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''");
976
-
977
-	if ($ext) {
978
-		return nom_fichier_copie_locale($source, $ext);
979
-	}
980
-
981
-	// voir si l'extension indiquee dans le nom du fichier est ok
982
-	// et si il n'aurait pas deja ete rapatrie
983
-
984
-	$ext = $path_parts ? $path_parts['extension'] : '';
985
-
986
-	if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
987
-		$f = nom_fichier_copie_locale($source, $ext);
988
-		if (file_exists(_DIR_RACINE . $f)) {
989
-			return $f;
990
-		}
991
-	}
992
-
993
-	// Ping  pour voir si son extension est connue et autorisee
994
-	// avec mise en cache du resultat du ping
995
-
996
-	$cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source);
997
-	if (
998
-		!@file_exists($cache)
999
-		|| !($path_parts = @unserialize(spip_file_get_contents($cache)))
1000
-		|| _request('var_mode') === 'recalcul'
1001
-	) {
1002
-		$path_parts = recuperer_infos_distantes($source, ['charger_si_petite_image' => false]);
1003
-		ecrire_fichier($cache, serialize($path_parts));
1004
-	}
1005
-	$ext = empty($path_parts['extension']) ? '' : $path_parts['extension'];
1006
-	if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
1007
-		return nom_fichier_copie_locale($source, $ext);
1008
-	}
1009
-
1010
-	spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR);
1011
-	return null;
946
+    // Si c'est deja local pas de souci
947
+    if (!tester_url_absolue($source)) {
948
+        if (_DIR_RACINE) {
949
+            $source = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', $source);
950
+        }
951
+
952
+        return $source;
953
+    }
954
+
955
+    // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier
956
+    // a deja ete copie en local avec cette extension
957
+    // dans ce cas elle est fiable, pas la peine de requeter en base
958
+    $path_parts = pathinfo($source);
959
+    if (!isset($path_parts['extension'])) {
960
+        $path_parts['extension'] = '';
961
+    }
962
+    $ext = $path_parts ? $path_parts['extension'] : '';
963
+    if (
964
+        $ext
965
+        && preg_match(',^\w+$,', $ext)
966
+        && ($f = nom_fichier_copie_locale($source, $ext))
967
+        && file_exists(_DIR_RACINE . $f)
968
+    ) {
969
+        return $f;
970
+    }
971
+
972
+
973
+    // Si c'est deja dans la table des documents,
974
+    // ramener le nom de sa copie potentielle
975
+    $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''");
976
+
977
+    if ($ext) {
978
+        return nom_fichier_copie_locale($source, $ext);
979
+    }
980
+
981
+    // voir si l'extension indiquee dans le nom du fichier est ok
982
+    // et si il n'aurait pas deja ete rapatrie
983
+
984
+    $ext = $path_parts ? $path_parts['extension'] : '';
985
+
986
+    if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
987
+        $f = nom_fichier_copie_locale($source, $ext);
988
+        if (file_exists(_DIR_RACINE . $f)) {
989
+            return $f;
990
+        }
991
+    }
992
+
993
+    // Ping  pour voir si son extension est connue et autorisee
994
+    // avec mise en cache du resultat du ping
995
+
996
+    $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source);
997
+    if (
998
+        !@file_exists($cache)
999
+        || !($path_parts = @unserialize(spip_file_get_contents($cache)))
1000
+        || _request('var_mode') === 'recalcul'
1001
+    ) {
1002
+        $path_parts = recuperer_infos_distantes($source, ['charger_si_petite_image' => false]);
1003
+        ecrire_fichier($cache, serialize($path_parts));
1004
+    }
1005
+    $ext = empty($path_parts['extension']) ? '' : $path_parts['extension'];
1006
+    if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
1007
+        return nom_fichier_copie_locale($source, $ext);
1008
+    }
1009
+
1010
+    spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR);
1011
+    return null;
1012 1012
 }
1013 1013
 
1014 1014
 
@@ -1037,129 +1037,129 @@  discard block
 block discarded – undo
1037 1037
  **/
1038 1038
 function recuperer_infos_distantes($source, $options = []) {
1039 1039
 
1040
-	// pas la peine de perdre son temps
1041
-	if (!tester_url_absolue($source)) {
1042
-		return false;
1043
-	}
1044
-
1045
-	$taille_max = $options['taille_max'] ?? 0;
1046
-	$charger_si_petite_image = (bool) ($options['charger_si_petite_image'] ?? true);
1047
-	$callback_valider_url = $options['callback_valider_url'] ?? null;
1048
-
1049
-	# charger les alias des types mime
1050
-	include_spip('base/typedoc');
1051
-
1052
-	$a = [];
1053
-	$mime_type = '';
1054
-	// On va directement charger le debut des images et des fichiers html,
1055
-	// de maniere a attrapper le maximum d'infos (titre, taille, etc). Si
1056
-	// ca echoue l'utilisateur devra les entrer...
1057
-	$reponse = recuperer_url($source, ['taille_max' => $taille_max, 'refuser_gz' => true]);
1058
-	if (
1059
-		$callback_valider_url
1060
-		&& is_callable($callback_valider_url)
1061
-		&& !$callback_valider_url($reponse['url'])
1062
-	) {
1063
-		return false;
1064
-	}
1065
-	$headers = $reponse['headers'] ?? '';
1066
-	$a['body'] = $reponse['page'] ?? '';
1067
-	if ($headers) {
1068
-		$mime_type = distant_trouver_mime_type_selon_headers($source, $headers);
1069
-
1070
-		if (!$extension = distant_trouver_extension_selon_headers($source, $headers)) {
1071
-			return false;
1072
-		}
1073
-
1074
-		$a['extension'] = $extension;
1075
-
1076
-		if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) {
1077
-			$a['taille'] = (int) $regs[1];
1078
-		}
1079
-	}
1080
-
1081
-	// Echec avec HEAD, on tente avec GET
1082
-	if (!$a && !$taille_max) {
1083
-		spip_log("tenter GET $source", 'distant');
1084
-		$options['taille_max'] = _INC_DISTANT_MAX_SIZE;
1085
-		$a = recuperer_infos_distantes($source, $options);
1086
-	}
1087
-
1088
-	// si on a rien trouve pas la peine d'insister
1089
-	if (!$a) {
1090
-		return false;
1091
-	}
1092
-
1093
-	// S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller
1094
-	// recharger le document en GET et recuperer des donnees supplementaires...
1095
-	include_spip('inc/filtres_images_lib_mini');
1096
-	include_spip('inc/documents');
1097
-	if (
1098
-		str_starts_with($mime_type, 'image/')
1099
-		&& ($extension = _image_trouver_extension_depuis_mime($mime_type))
1100
-	) {
1101
-		if (
1102
-			$taille_max == 0
1103
-			&& (empty($a['taille']) || $a['taille'] < _INC_DISTANT_MAX_SIZE)
1104
-			&& in_array($extension, formats_image_acceptables())
1105
-			&& $charger_si_petite_image
1106
-		) {
1107
-			$options['taille_max'] = _INC_DISTANT_MAX_SIZE;
1108
-			$a = recuperer_infos_distantes($source, $options);
1109
-		} else {
1110
-			if ($a['body']) {
1111
-				$a['extension'] = corriger_extension($extension);
1112
-				$a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension);
1113
-				ecrire_fichier($a['fichier'], $a['body']);
1114
-				$size_image = @spip_getimagesize($a['fichier']);
1115
-				$a['largeur'] = (int) $size_image[0];
1116
-				$a['hauteur'] = (int) $size_image[1];
1117
-				$a['type_image'] = true;
1118
-			}
1119
-		}
1120
-	}
1121
-
1122
-	// Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut
1123
-	// ce sera mieux que 0x0
1124
-	// Flash is dead!
1125
-	if (
1126
-		$a
1127
-		&& isset($a['extension'])
1128
-		&& $a['extension'] == 'swf'
1129
-		&& empty($a['largeur'])
1130
-	) {
1131
-		$a['largeur'] = 425;
1132
-		$a['hauteur'] = 350;
1133
-	}
1134
-
1135
-	if ($mime_type == 'text/html') {
1136
-		include_spip('inc/filtres');
1137
-		$page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]);
1138
-		$page = $page['page'] ?? '';
1139
-		if (preg_match(',<title>(.*?)</title>,ims', (string) $page, $regs)) {
1140
-			$a['titre'] = corriger_caracteres(trim($regs[1]));
1141
-		}
1142
-		if (!isset($a['taille']) || !$a['taille']) {
1143
-			$a['taille'] = strlen((string) $page); # a peu pres
1144
-		}
1145
-	}
1146
-	$a['mime_type'] = $mime_type;
1147
-
1148
-	return $a;
1040
+    // pas la peine de perdre son temps
1041
+    if (!tester_url_absolue($source)) {
1042
+        return false;
1043
+    }
1044
+
1045
+    $taille_max = $options['taille_max'] ?? 0;
1046
+    $charger_si_petite_image = (bool) ($options['charger_si_petite_image'] ?? true);
1047
+    $callback_valider_url = $options['callback_valider_url'] ?? null;
1048
+
1049
+    # charger les alias des types mime
1050
+    include_spip('base/typedoc');
1051
+
1052
+    $a = [];
1053
+    $mime_type = '';
1054
+    // On va directement charger le debut des images et des fichiers html,
1055
+    // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si
1056
+    // ca echoue l'utilisateur devra les entrer...
1057
+    $reponse = recuperer_url($source, ['taille_max' => $taille_max, 'refuser_gz' => true]);
1058
+    if (
1059
+        $callback_valider_url
1060
+        && is_callable($callback_valider_url)
1061
+        && !$callback_valider_url($reponse['url'])
1062
+    ) {
1063
+        return false;
1064
+    }
1065
+    $headers = $reponse['headers'] ?? '';
1066
+    $a['body'] = $reponse['page'] ?? '';
1067
+    if ($headers) {
1068
+        $mime_type = distant_trouver_mime_type_selon_headers($source, $headers);
1069
+
1070
+        if (!$extension = distant_trouver_extension_selon_headers($source, $headers)) {
1071
+            return false;
1072
+        }
1073
+
1074
+        $a['extension'] = $extension;
1075
+
1076
+        if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) {
1077
+            $a['taille'] = (int) $regs[1];
1078
+        }
1079
+    }
1080
+
1081
+    // Echec avec HEAD, on tente avec GET
1082
+    if (!$a && !$taille_max) {
1083
+        spip_log("tenter GET $source", 'distant');
1084
+        $options['taille_max'] = _INC_DISTANT_MAX_SIZE;
1085
+        $a = recuperer_infos_distantes($source, $options);
1086
+    }
1087
+
1088
+    // si on a rien trouve pas la peine d'insister
1089
+    if (!$a) {
1090
+        return false;
1091
+    }
1092
+
1093
+    // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller
1094
+    // recharger le document en GET et recuperer des donnees supplementaires...
1095
+    include_spip('inc/filtres_images_lib_mini');
1096
+    include_spip('inc/documents');
1097
+    if (
1098
+        str_starts_with($mime_type, 'image/')
1099
+        && ($extension = _image_trouver_extension_depuis_mime($mime_type))
1100
+    ) {
1101
+        if (
1102
+            $taille_max == 0
1103
+            && (empty($a['taille']) || $a['taille'] < _INC_DISTANT_MAX_SIZE)
1104
+            && in_array($extension, formats_image_acceptables())
1105
+            && $charger_si_petite_image
1106
+        ) {
1107
+            $options['taille_max'] = _INC_DISTANT_MAX_SIZE;
1108
+            $a = recuperer_infos_distantes($source, $options);
1109
+        } else {
1110
+            if ($a['body']) {
1111
+                $a['extension'] = corriger_extension($extension);
1112
+                $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension);
1113
+                ecrire_fichier($a['fichier'], $a['body']);
1114
+                $size_image = @spip_getimagesize($a['fichier']);
1115
+                $a['largeur'] = (int) $size_image[0];
1116
+                $a['hauteur'] = (int) $size_image[1];
1117
+                $a['type_image'] = true;
1118
+            }
1119
+        }
1120
+    }
1121
+
1122
+    // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut
1123
+    // ce sera mieux que 0x0
1124
+    // Flash is dead!
1125
+    if (
1126
+        $a
1127
+        && isset($a['extension'])
1128
+        && $a['extension'] == 'swf'
1129
+        && empty($a['largeur'])
1130
+    ) {
1131
+        $a['largeur'] = 425;
1132
+        $a['hauteur'] = 350;
1133
+    }
1134
+
1135
+    if ($mime_type == 'text/html') {
1136
+        include_spip('inc/filtres');
1137
+        $page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]);
1138
+        $page = $page['page'] ?? '';
1139
+        if (preg_match(',<title>(.*?)</title>,ims', (string) $page, $regs)) {
1140
+            $a['titre'] = corriger_caracteres(trim($regs[1]));
1141
+        }
1142
+        if (!isset($a['taille']) || !$a['taille']) {
1143
+            $a['taille'] = strlen((string) $page); # a peu pres
1144
+        }
1145
+    }
1146
+    $a['mime_type'] = $mime_type;
1147
+
1148
+    return $a;
1149 1149
 }
1150 1150
 
1151 1151
 /**
1152 1152
  * Retrouver un mime type depuis les headers
1153 1153
  */
1154 1154
 function distant_trouver_mime_type_selon_headers(string $source, string $headers): string {
1155
-	$mime_type = preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs) ? trim($regs[1]) : ''; // inconnu
1155
+    $mime_type = preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs) ? trim($regs[1]) : ''; // inconnu
1156 1156
 
1157
-	// Appliquer les alias
1158
-	while (isset($GLOBALS['mime_alias'][$mime_type])) {
1159
-		$mime_type = $GLOBALS['mime_alias'][$mime_type];
1160
-	}
1157
+    // Appliquer les alias
1158
+    while (isset($GLOBALS['mime_alias'][$mime_type])) {
1159
+        $mime_type = $GLOBALS['mime_alias'][$mime_type];
1160
+    }
1161 1161
 
1162
-	return $mime_type;
1162
+    return $mime_type;
1163 1163
 }
1164 1164
 
1165 1165
 /**
@@ -1168,58 +1168,58 @@  discard block
 block discarded – undo
1168 1168
  * @return false|string
1169 1169
  */
1170 1170
 function distant_trouver_extension_selon_headers(string $source, string $headers) {
1171
-	$mime_type = distant_trouver_mime_type_selon_headers($source, $headers);
1172
-
1173
-	// pour corriger_extension()
1174
-	include_spip('inc/documents');
1175
-
1176
-	// Si on a un mime-type insignifiant
1177
-	// text/plain,application/octet-stream ou vide
1178
-	// c'est peut-etre que le serveur ne sait pas
1179
-	// ce qu'il sert ; on va tenter de detecter via l'extension de l'url
1180
-	// ou le Content-Disposition: attachment; filename=...
1181
-	$t = null;
1182
-	if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) {
1183
-		if (!$t && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)) {
1184
-			$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text'));
1185
-		}
1186
-		if (
1187
-			!$t
1188
-			&& preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m)
1189
-			&& preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext)
1190
-		) {
1191
-			$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text'));
1192
-		}
1193
-	}
1194
-
1195
-	// Autre mime/type (ou text/plain avec fichier d'extension inconnue)
1196
-	if (!$t) {
1197
-		$t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type));
1198
-	}
1199
-
1200
-	// Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)
1201
-	// On essaie de nouveau avec l'extension
1202
-	if (
1203
-		!$t
1204
-		&& $mime_type != 'text/plain'
1205
-		&& preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)
1206
-	) {
1207
-		# eviter xxx.3 => 3gp (> SPIP 3)
1208
-		$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text'));
1209
-	}
1210
-
1211
-	if ($t) {
1212
-		spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant');
1213
-		return $t['extension'];
1214
-	} else {
1215
-		# par defaut on retombe sur '.bin' si c'est autorise
1216
-		spip_log("mime-type $mime_type inconnu", 'distant');
1217
-		$t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'");
1218
-		if (!$t) {
1219
-			return false;
1220
-		}
1221
-		return $t['extension'];
1222
-	}
1171
+    $mime_type = distant_trouver_mime_type_selon_headers($source, $headers);
1172
+
1173
+    // pour corriger_extension()
1174
+    include_spip('inc/documents');
1175
+
1176
+    // Si on a un mime-type insignifiant
1177
+    // text/plain,application/octet-stream ou vide
1178
+    // c'est peut-etre que le serveur ne sait pas
1179
+    // ce qu'il sert ; on va tenter de detecter via l'extension de l'url
1180
+    // ou le Content-Disposition: attachment; filename=...
1181
+    $t = null;
1182
+    if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) {
1183
+        if (!$t && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)) {
1184
+            $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text'));
1185
+        }
1186
+        if (
1187
+            !$t
1188
+            && preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m)
1189
+            && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext)
1190
+        ) {
1191
+            $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text'));
1192
+        }
1193
+    }
1194
+
1195
+    // Autre mime/type (ou text/plain avec fichier d'extension inconnue)
1196
+    if (!$t) {
1197
+        $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type));
1198
+    }
1199
+
1200
+    // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)
1201
+    // On essaie de nouveau avec l'extension
1202
+    if (
1203
+        !$t
1204
+        && $mime_type != 'text/plain'
1205
+        && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)
1206
+    ) {
1207
+        # eviter xxx.3 => 3gp (> SPIP 3)
1208
+        $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text'));
1209
+    }
1210
+
1211
+    if ($t) {
1212
+        spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant');
1213
+        return $t['extension'];
1214
+    } else {
1215
+        # par defaut on retombe sur '.bin' si c'est autorise
1216
+        spip_log("mime-type $mime_type inconnu", 'distant');
1217
+        $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'");
1218
+        if (!$t) {
1219
+            return false;
1220
+        }
1221
+        return $t['extension'];
1222
+    }
1223 1223
 }
1224 1224
 
1225 1225
 /**
@@ -1235,45 +1235,45 @@  discard block
 block discarded – undo
1235 1235
  */
1236 1236
 function need_proxy($host, $http_proxy = null, $http_noproxy = null) {
1237 1237
 
1238
-	$http_proxy ??= $GLOBALS['meta']['http_proxy'] ?? null;
1239
-
1240
-	// rien a faire si pas de proxy :)
1241
-	if (is_null($http_proxy) || !$http_proxy = trim((string) $http_proxy)) {
1242
-		return '';
1243
-	}
1244
-
1245
-	if (is_null($http_noproxy)) {
1246
-		$http_noproxy = $GLOBALS['meta']['http_noproxy'] ?? null;
1247
-	}
1248
-	// si pas d'exception, on retourne le proxy
1249
-	if (is_null($http_noproxy) || !$http_noproxy = trim((string) $http_noproxy)) {
1250
-		return $http_proxy;
1251
-	}
1252
-
1253
-	// si le host ou l'un des domaines parents est dans $http_noproxy on fait exception
1254
-	// $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne
1255
-	$http_noproxy = str_replace("\n", ' ', $http_noproxy);
1256
-	$http_noproxy = str_replace("\r", ' ', $http_noproxy);
1257
-	$http_noproxy = " $http_noproxy ";
1258
-	$domain = $host;
1259
-	// si le domaine exact www.example.org est dans les exceptions
1260
-	if (str_contains($http_noproxy, (string) " $domain ")) {
1261
-		return '';
1262
-	}
1263
-
1264
-	while (str_contains($domain, '.')) {
1265
-		$domain = explode('.', $domain);
1266
-		array_shift($domain);
1267
-		$domain = implode('.', $domain);
1268
-
1269
-		// ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines)
1270
-		if (str_contains($http_noproxy, (string) " .$domain ")) {
1271
-			return '';
1272
-		}
1273
-	}
1274
-
1275
-	// ok c'est pas une exception
1276
-	return $http_proxy;
1238
+    $http_proxy ??= $GLOBALS['meta']['http_proxy'] ?? null;
1239
+
1240
+    // rien a faire si pas de proxy :)
1241
+    if (is_null($http_proxy) || !$http_proxy = trim((string) $http_proxy)) {
1242
+        return '';
1243
+    }
1244
+
1245
+    if (is_null($http_noproxy)) {
1246
+        $http_noproxy = $GLOBALS['meta']['http_noproxy'] ?? null;
1247
+    }
1248
+    // si pas d'exception, on retourne le proxy
1249
+    if (is_null($http_noproxy) || !$http_noproxy = trim((string) $http_noproxy)) {
1250
+        return $http_proxy;
1251
+    }
1252
+
1253
+    // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception
1254
+    // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne
1255
+    $http_noproxy = str_replace("\n", ' ', $http_noproxy);
1256
+    $http_noproxy = str_replace("\r", ' ', $http_noproxy);
1257
+    $http_noproxy = " $http_noproxy ";
1258
+    $domain = $host;
1259
+    // si le domaine exact www.example.org est dans les exceptions
1260
+    if (str_contains($http_noproxy, (string) " $domain ")) {
1261
+        return '';
1262
+    }
1263
+
1264
+    while (str_contains($domain, '.')) {
1265
+        $domain = explode('.', $domain);
1266
+        array_shift($domain);
1267
+        $domain = implode('.', $domain);
1268
+
1269
+        // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines)
1270
+        if (str_contains($http_noproxy, (string) " .$domain ")) {
1271
+            return '';
1272
+        }
1273
+    }
1274
+
1275
+    // ok c'est pas une exception
1276
+    return $http_proxy;
1277 1277
 }
1278 1278
 
1279 1279
 
@@ -1296,60 +1296,60 @@  discard block
 block discarded – undo
1296 1296
  * @return array
1297 1297
  */
1298 1298
 function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = '', $vers = 'HTTP/1.0', $date = '') {
1299
-	$user = $via_proxy = $proxy_user = '';
1300
-	$fopen = false;
1301
-
1302
-	$t = @parse_url($url);
1303
-	$host = $t['host'];
1304
-	if ($t['scheme'] == 'http') {
1305
-		$scheme = 'http';
1306
-		$noproxy = '';
1307
-	} elseif ($t['scheme'] == 'https') {
1308
-		$scheme = 'ssl';
1309
-		$noproxy = 'ssl://';
1310
-		if (!isset($t['port']) || !($port = $t['port'])) {
1311
-			$t['port'] = 443;
1312
-		}
1313
-	} else {
1314
-		$scheme = $t['scheme'];
1315
-		$noproxy = $scheme . '://';
1316
-	}
1317
-	if (isset($t['user'])) {
1318
-		// user et pass doivent être passés en urlencodé dans l'URL, on redecode ici
1319
-		$user = [urldecode($t['user']), urldecode($t['pass'])];
1320
-	}
1321
-
1322
-	if (!isset($t['port']) || !($port = $t['port'])) {
1323
-		$port = 80;
1324
-	}
1325
-	if (!isset($t['path']) || !($path = $t['path'])) {
1326
-		$path = '/';
1327
-	}
1328
-
1329
-	if (!empty($t['query'])) {
1330
-		$path .= '?' . $t['query'];
1331
-	}
1332
-
1333
-	$f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);
1334
-	if (!$f || !is_resource($f)) {
1335
-		// fallback : fopen si on a pas fait timeout dans lance_requete
1336
-		// ce qui correspond a $f===110
1337
-		if (
1338
-			$f !== 110
1339
-			&& !need_proxy($host)
1340
-			&& !_request('tester_proxy')
1341
-			&& (!isset($GLOBALS['inc_distant_allow_fopen']) || $GLOBALS['inc_distant_allow_fopen'])
1342
-		) {
1343
-			$f = @fopen($url, 'rb');
1344
-			spip_log("connexion vers $url par simple fopen", 'distant');
1345
-			$fopen = true;
1346
-		} else {
1347
-			// echec total
1348
-			$f = false;
1349
-		}
1350
-	}
1351
-
1352
-	return [$f, $fopen];
1299
+    $user = $via_proxy = $proxy_user = '';
1300
+    $fopen = false;
1301
+
1302
+    $t = @parse_url($url);
1303
+    $host = $t['host'];
1304
+    if ($t['scheme'] == 'http') {
1305
+        $scheme = 'http';
1306
+        $noproxy = '';
1307
+    } elseif ($t['scheme'] == 'https') {
1308
+        $scheme = 'ssl';
1309
+        $noproxy = 'ssl://';
1310
+        if (!isset($t['port']) || !($port = $t['port'])) {
1311
+            $t['port'] = 443;
1312
+        }
1313
+    } else {
1314
+        $scheme = $t['scheme'];
1315
+        $noproxy = $scheme . '://';
1316
+    }
1317
+    if (isset($t['user'])) {
1318
+        // user et pass doivent être passés en urlencodé dans l'URL, on redecode ici
1319
+        $user = [urldecode($t['user']), urldecode($t['pass'])];
1320
+    }
1321
+
1322
+    if (!isset($t['port']) || !($port = $t['port'])) {
1323
+        $port = 80;
1324
+    }
1325
+    if (!isset($t['path']) || !($path = $t['path'])) {
1326
+        $path = '/';
1327
+    }
1328
+
1329
+    if (!empty($t['query'])) {
1330
+        $path .= '?' . $t['query'];
1331
+    }
1332
+
1333
+    $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);
1334
+    if (!$f || !is_resource($f)) {
1335
+        // fallback : fopen si on a pas fait timeout dans lance_requete
1336
+        // ce qui correspond a $f===110
1337
+        if (
1338
+            $f !== 110
1339
+            && !need_proxy($host)
1340
+            && !_request('tester_proxy')
1341
+            && (!isset($GLOBALS['inc_distant_allow_fopen']) || $GLOBALS['inc_distant_allow_fopen'])
1342
+        ) {
1343
+            $f = @fopen($url, 'rb');
1344
+            spip_log("connexion vers $url par simple fopen", 'distant');
1345
+            $fopen = true;
1346
+        } else {
1347
+            // echec total
1348
+            $f = false;
1349
+        }
1350
+    }
1351
+
1352
+    return [$f, $fopen];
1353 1353
 }
1354 1354
 
1355 1355
 /**
@@ -1384,124 +1384,124 @@  discard block
 block discarded – undo
1384 1384
  *   resource socket vers l'url demandee
1385 1385
  */
1386 1386
 function lance_requete(
1387
-	$method,
1388
-	$scheme,
1389
-	$user,
1390
-	$host,
1391
-	$path,
1392
-	$port,
1393
-	$noproxy,
1394
-	$refuse_gz = false,
1395
-	$referer = '',
1396
-	$datas = '',
1397
-	$vers = 'HTTP/1.0',
1398
-	$date = ''
1387
+    $method,
1388
+    $scheme,
1389
+    $user,
1390
+    $host,
1391
+    $path,
1392
+    $port,
1393
+    $noproxy,
1394
+    $refuse_gz = false,
1395
+    $referer = '',
1396
+    $datas = '',
1397
+    $vers = 'HTTP/1.0',
1398
+    $date = ''
1399 1399
 ) {
1400 1400
 
1401
-	$proxy_user = '';
1402
-	$http_proxy = need_proxy($host);
1403
-	if ($user) {
1404
-		$user = urlencode((string) $user[0]) . ':' . urlencode((string) $user[1]);
1405
-	}
1406
-
1407
-	$connect = '';
1408
-	if ($http_proxy) {
1409
-		if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') && in_array($scheme, ['tls','ssl'])) {
1410
-			$path_host = ($user ? "$user@" : '') . $host . (($port != 80) ? ":$port" : '');
1411
-			$connect = 'CONNECT ' . $path_host . " $vers\r\n"
1412
-				. "Host: $path_host\r\n"
1413
-				. "Proxy-Connection: Keep-Alive\r\n";
1414
-		} else {
1415
-			$path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://")
1416
-				. ($user ? "$user@" : '')
1417
-				. "$host" . (($port != 80) ? ":$port" : '') . $path;
1418
-		}
1419
-		$t2 = @parse_url($http_proxy);
1420
-		$first_host = $t2['host'];
1421
-		$first_port = ($t2['port'] ?? null) ?: 80;
1422
-		if ($t2['user'] ?? null) {
1423
-			$proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']);
1424
-		}
1425
-	} else {
1426
-		$first_host = $noproxy . $host;
1427
-		$first_port = $port;
1428
-	}
1429
-
1430
-	if ($connect) {
1431
-		$streamContext = stream_context_create([
1432
-			'ssl' => [
1433
-				'verify_peer' => false,
1434
-				'allow_self_signed' => true,
1435
-				'SNI_enabled' => true,
1436
-				'peer_name' => $host,
1437
-			]
1438
-		]);
1439
-		$f = @stream_socket_client(
1440
-			"tcp://$first_host:$first_port",
1441
-			$errno,
1442
-			$errstr,
1443
-			_INC_DISTANT_CONNECT_TIMEOUT,
1444
-			STREAM_CLIENT_CONNECT,
1445
-			$streamContext
1446
-		);
1447
-		spip_log("Recuperer $path sur $first_host:$first_port par $f (via CONNECT)", 'connect');
1448
-		if (!$f) {
1449
-			spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1450
-			return $errno;
1451
-		}
1452
-		stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
1453
-
1454
-		fwrite($f, $connect);
1455
-		fwrite($f, "\r\n");
1456
-		$res = fread($f, 1024);
1457
-		if (
1458
-			!$res
1459
-			|| ($res = explode(' ', $res)) === []
1460
-			|| $res[1] !== '200'
1461
-		) {
1462
-			spip_log("Echec CONNECT sur $first_host:$first_port", 'connect' . _LOG_INFO_IMPORTANTE);
1463
-			fclose($f);
1464
-
1465
-			return false;
1466
-		}
1467
-		// important, car sinon on lit trop vite et les donnees ne sont pas encore dispo
1468
-		stream_set_blocking($f, true);
1469
-		// envoyer le handshake
1470
-		stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
1471
-		spip_log("OK CONNECT sur $first_host:$first_port", 'connect');
1472
-	} else {
1473
-		$ntry = 3;
1474
-		do {
1475
-			$f = @fsockopen($first_host, $first_port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT);
1476
-		} while (!$f && $ntry-- && $errno !== 110 && sleep(1));
1477
-		spip_log("Recuperer $path sur $first_host:$first_port par $f");
1478
-		if (!$f) {
1479
-			spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1480
-
1481
-			return $errno;
1482
-		}
1483
-		stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
1484
-	}
1485
-
1486
-	$site = $GLOBALS['meta']['adresse_site'] ?? '';
1487
-
1488
-	$host_port = $host;
1489
-	if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) {
1490
-		$host_port .= ":$port";
1491
-	}
1492
-	$req = "$method $path $vers\r\n"
1493
-		. "Host: $host_port\r\n"
1494
-		. 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n"
1495
-		. ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n"))
1496
-		. ($site ? "Referer: $site/$referer\r\n" : '')
1497
-		. ($date ? 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n") : '')
1498
-		. ($user ? 'Authorization: Basic ' . base64_encode(urldecode($user)) . "\r\n" : '')
1499
-		. ($proxy_user ? "Proxy-Authorization: Basic $proxy_user\r\n" : '')
1500
-		. (strpos($vers, '1.1') ? "Keep-Alive: 300\r\nConnection: keep-alive\r\n" : '');
1401
+    $proxy_user = '';
1402
+    $http_proxy = need_proxy($host);
1403
+    if ($user) {
1404
+        $user = urlencode((string) $user[0]) . ':' . urlencode((string) $user[1]);
1405
+    }
1406
+
1407
+    $connect = '';
1408
+    if ($http_proxy) {
1409
+        if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') && in_array($scheme, ['tls','ssl'])) {
1410
+            $path_host = ($user ? "$user@" : '') . $host . (($port != 80) ? ":$port" : '');
1411
+            $connect = 'CONNECT ' . $path_host . " $vers\r\n"
1412
+                . "Host: $path_host\r\n"
1413
+                . "Proxy-Connection: Keep-Alive\r\n";
1414
+        } else {
1415
+            $path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://")
1416
+                . ($user ? "$user@" : '')
1417
+                . "$host" . (($port != 80) ? ":$port" : '') . $path;
1418
+        }
1419
+        $t2 = @parse_url($http_proxy);
1420
+        $first_host = $t2['host'];
1421
+        $first_port = ($t2['port'] ?? null) ?: 80;
1422
+        if ($t2['user'] ?? null) {
1423
+            $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']);
1424
+        }
1425
+    } else {
1426
+        $first_host = $noproxy . $host;
1427
+        $first_port = $port;
1428
+    }
1429
+
1430
+    if ($connect) {
1431
+        $streamContext = stream_context_create([
1432
+            'ssl' => [
1433
+                'verify_peer' => false,
1434
+                'allow_self_signed' => true,
1435
+                'SNI_enabled' => true,
1436
+                'peer_name' => $host,
1437
+            ]
1438
+        ]);
1439
+        $f = @stream_socket_client(
1440
+            "tcp://$first_host:$first_port",
1441
+            $errno,
1442
+            $errstr,
1443
+            _INC_DISTANT_CONNECT_TIMEOUT,
1444
+            STREAM_CLIENT_CONNECT,
1445
+            $streamContext
1446
+        );
1447
+        spip_log("Recuperer $path sur $first_host:$first_port par $f (via CONNECT)", 'connect');
1448
+        if (!$f) {
1449
+            spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1450
+            return $errno;
1451
+        }
1452
+        stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
1453
+
1454
+        fwrite($f, $connect);
1455
+        fwrite($f, "\r\n");
1456
+        $res = fread($f, 1024);
1457
+        if (
1458
+            !$res
1459
+            || ($res = explode(' ', $res)) === []
1460
+            || $res[1] !== '200'
1461
+        ) {
1462
+            spip_log("Echec CONNECT sur $first_host:$first_port", 'connect' . _LOG_INFO_IMPORTANTE);
1463
+            fclose($f);
1464
+
1465
+            return false;
1466
+        }
1467
+        // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo
1468
+        stream_set_blocking($f, true);
1469
+        // envoyer le handshake
1470
+        stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
1471
+        spip_log("OK CONNECT sur $first_host:$first_port", 'connect');
1472
+    } else {
1473
+        $ntry = 3;
1474
+        do {
1475
+            $f = @fsockopen($first_host, $first_port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT);
1476
+        } while (!$f && $ntry-- && $errno !== 110 && sleep(1));
1477
+        spip_log("Recuperer $path sur $first_host:$first_port par $f");
1478
+        if (!$f) {
1479
+            spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1480
+
1481
+            return $errno;
1482
+        }
1483
+        stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
1484
+    }
1485
+
1486
+    $site = $GLOBALS['meta']['adresse_site'] ?? '';
1487
+
1488
+    $host_port = $host;
1489
+    if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) {
1490
+        $host_port .= ":$port";
1491
+    }
1492
+    $req = "$method $path $vers\r\n"
1493
+        . "Host: $host_port\r\n"
1494
+        . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n"
1495
+        . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n"))
1496
+        . ($site ? "Referer: $site/$referer\r\n" : '')
1497
+        . ($date ? 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n") : '')
1498
+        . ($user ? 'Authorization: Basic ' . base64_encode(urldecode($user)) . "\r\n" : '')
1499
+        . ($proxy_user ? "Proxy-Authorization: Basic $proxy_user\r\n" : '')
1500
+        . (strpos($vers, '1.1') ? "Keep-Alive: 300\r\nConnection: keep-alive\r\n" : '');
1501 1501
 
1502 1502
 #	spip_log("Requete\n$req", 'distant');
1503
-	fwrite($f, $req);
1504
-	fwrite($f, $datas ?: "\r\n");
1503
+    fwrite($f, $req);
1504
+    fwrite($f, $datas ?: "\r\n");
1505 1505
 
1506
-	return $f;
1506
+    return $f;
1507 1507
 }
Please login to merge, or discard this patch.
Spacing   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
27 27
 	define('_INC_DISTANT_CONTENT_ENCODING', 'gzip');
28 28
 }
29 29
 if (!defined('_INC_DISTANT_USER_AGENT')) {
30
-	define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')');
30
+	define('_INC_DISTANT_USER_AGENT', 'SPIP-'.$GLOBALS['spip_version_affichee'].' ('.$GLOBALS['home_server'].')');
31 31
 }
32 32
 if (!defined('_INC_DISTANT_MAX_SIZE')) {
33 33
 	define('_INC_DISTANT_MAX_SIZE', 2_097_152);
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
 	define('_INC_DISTANT_CONNECT_TIMEOUT', 10);
37 37
 }
38 38
 
39
-define('_REGEXP_COPIE_LOCALE', ',' 	.
39
+define('_REGEXP_COPIE_LOCALE', ','.
40 40
 	preg_replace(
41 41
 		'@^https?:@',
42 42
 		'https?:',
@@ -73,7 +73,7 @@  discard block
 block discarded – undo
73 73
 
74 74
 	// si c'est la protection de soi-meme, retourner le path
75 75
 	if ($mode !== 'force' && preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) {
76
-		$source = substr((string) _DIR_IMG, strlen((string) _DIR_RACINE)) . urldecode($match[1]);
76
+		$source = substr((string) _DIR_IMG, strlen((string) _DIR_RACINE)).urldecode($match[1]);
77 77
 
78 78
 		return @file_exists($source) ? $source : false;
79 79
 	}
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
 		return false;
94 94
 	}
95 95
 
96
-	$localrac = _DIR_RACINE . $local;
96
+	$localrac = _DIR_RACINE.$local;
97 97
 	$t = ($mode === 'force') ? false : @file_exists($localrac);
98 98
 
99 99
 	// test d'existence du fichier
@@ -113,17 +113,17 @@  discard block
 block discarded – undo
113 113
 		if (!$taille_max) {
114 114
 			$taille_max = _COPIE_LOCALE_MAX_SIZE;
115 115
 		}
116
-		$localrac_tmp = $localrac . '.tmp';
116
+		$localrac_tmp = $localrac.'.tmp';
117 117
 		$res = recuperer_url(
118 118
 			$source,
119 119
 			['file' => $localrac_tmp, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '']
120 120
 		);
121 121
 
122 122
 		if (!$res || !$res['length'] && $res['status'] != 304) {
123
-			spip_log("copie_locale : Echec recuperation $source sur $localrac_tmp status : " . ($res ? $res['status'] : '-'), 'distant' . _LOG_INFO_IMPORTANTE);
123
+			spip_log("copie_locale : Echec recuperation $source sur $localrac_tmp status : ".($res ? $res['status'] : '-'), 'distant'._LOG_INFO_IMPORTANTE);
124 124
 			@unlink($localrac_tmp);
125 125
 		} else {
126
-			spip_log("copie_locale : recuperation $source sur $localrac_tmp OK | taille " . $res['length'] . ' status ' . $res['status'], 'distant');
126
+			spip_log("copie_locale : recuperation $source sur $localrac_tmp OK | taille ".$res['length'].' status '.$res['status'], 'distant');
127 127
 		}
128 128
 		if (!$res || !$res['length']) {
129 129
 			// si $t c'est sans doute juste un not-modified-since
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 			&& is_callable($callback_valider_url)
137 137
 			&& !$callback_valider_url($res['url'])
138 138
 		) {
139
-			spip_log('copie_locale : url finale ' . $res['url'] . " non valide, on refuse le fichier $localrac_tmp", 'distant' . _LOG_INFO_IMPORTANTE);
139
+			spip_log('copie_locale : url finale '.$res['url']." non valide, on refuse le fichier $localrac_tmp", 'distant'._LOG_INFO_IMPORTANTE);
140 140
 			@unlink($localrac_tmp);
141 141
 			return $t ? $local : false;
142 142
 		}
@@ -226,7 +226,7 @@  discard block
 block discarded – undo
226 226
 
227 227
 	if (!$is_known_host) {
228 228
 		$host = trim($parsed_url['host'], '.');
229
-		if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) {
229
+		if (!$ip = filter_var($host, FILTER_VALIDATE_IP)) {
230 230
 			$ip = gethostbyname($host);
231 231
 			if ($ip === $host) {
232 232
 				// Error condition for gethostbyname()
@@ -246,7 +246,7 @@  discard block
 block discarded – undo
246 246
 				$ip = false;
247 247
 			}
248 248
 		}
249
-		if ($ip && ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
249
+		if ($ip && !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
250 250
 			return false;
251 251
 		}
252 252
 	}
@@ -327,7 +327,7 @@  discard block
 block discarded – undo
327 327
 				}
328 328
 			}
329 329
 			if ($taille > 500) {
330
-				$boundary = substr(md5(random_int(0, mt_getrandmax()) . 'spip'), 0, 8);
330
+				$boundary = substr(md5(random_int(0, mt_getrandmax()).'spip'), 0, 8);
331 331
 			}
332 332
 		}
333 333
 
@@ -360,10 +360,10 @@  discard block
 block discarded – undo
360 360
 				foreach ($donnees as $cle => $valeur) {
361 361
 					if (is_array($valeur)) {
362 362
 						foreach ($valeur as $val2) {
363
-							$chaines[] = rawurlencode($cle) . '[]=' . rawurlencode((string) $val2);
363
+							$chaines[] = rawurlencode($cle).'[]='.rawurlencode((string) $val2);
364 364
 						}
365 365
 					} else {
366
-						$chaines[] = rawurlencode($cle) . '=' . rawurlencode((string) $valeur);
366
+						$chaines[] = rawurlencode($cle).'='.rawurlencode((string) $valeur);
367 367
 					}
368 368
 				}
369 369
 				$chaine = implode('&', $chaines);
@@ -466,13 +466,13 @@  discard block
 block discarded – undo
466 466
 		$options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE;
467 467
 	}
468 468
 
469
-	spip_log('recuperer_url ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG);
469
+	spip_log('recuperer_url '.$options['methode']." sur $url", 'distant'._LOG_DEBUG);
470 470
 
471 471
 	// Ajout des en-têtes spécifiques si besoin
472 472
 	$formatted_data = '';
473 473
 	if (!empty($options['headers'])) {
474 474
 		foreach ($options['headers'] as $champ => $valeur) {
475
-			$formatted_data .= $champ . ': ' . $valeur . "\r\n";
475
+			$formatted_data .= $champ.': '.$valeur."\r\n";
476 476
 		}
477 477
 	}
478 478
 
@@ -480,9 +480,9 @@  discard block
 block discarded – undo
480 480
 		[$head, $postdata] = prepare_donnees_post($options['datas'], $options['boundary']);
481 481
 		$head .= $formatted_data;
482 482
 		if (stripos($head, 'Content-Length:') === false) {
483
-			$head .= 'Content-Length: ' . strlen((string) $postdata) . "\r\n";
483
+			$head .= 'Content-Length: '.strlen((string) $postdata)."\r\n";
484 484
 		}
485
-		$formatted_data = $head . "\r\n" . $postdata;
485
+		$formatted_data = $head."\r\n".$postdata;
486 486
 		if (
487 487
 			strlen((string) $postdata) && !$methode_demandee
488 488
 		) {
@@ -495,9 +495,9 @@  discard block
 block discarded – undo
495 495
 	// Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole
496 496
 	$url = preg_replace(',^feed://,i', 'http://', $url);
497 497
 	if (!tester_url_absolue($url)) {
498
-		$url = 'http://' . $url;
498
+		$url = 'http://'.$url;
499 499
 	} elseif (str_starts_with($url, '//')) {
500
-		$url = 'http:' . $url;
500
+		$url = 'http:'.$url;
501 501
 	}
502 502
 
503 503
 	$url = url_to_ascii($url);
@@ -526,7 +526,7 @@  discard block
 block discarded – undo
526 526
 		$options['if_modified_since']
527 527
 	);
528 528
 	if (!$handle) {
529
-		spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR);
529
+		spip_log("ECHEC init_http $url", 'distant'._LOG_ERREUR);
530 530
 
531 531
 		return false;
532 532
 	}
@@ -556,7 +556,7 @@  discard block
 block discarded – undo
556 556
 					'status' => 200,
557 557
 				];
558 558
 			} else {
559
-				spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR);
559
+				spip_log("ECHEC chinoiserie $url", 'distant'._LOG_ERREUR);
560 560
 				return false;
561 561
 			}
562 562
 		} elseif ($res['location'] && $options['follow_location']) {
@@ -573,11 +573,11 @@  discard block
 block discarded – undo
573 573
 				$options['methode'] = 'GET';
574 574
 				$options['datas'] = '';
575 575
 			}
576
-			spip_log('recuperer_url recommence ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG);
576
+			spip_log('recuperer_url recommence '.$options['methode']." sur $url", 'distant'._LOG_DEBUG);
577 577
 
578 578
 			return recuperer_url($url, $options);
579 579
 		} elseif ($res['status'] !== 200) {
580
-			spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant');
580
+			spip_log('HTTP status '.$res['status']." pour $url", 'distant');
581 581
 		}
582 582
 		$result['status'] = $res['status'];
583 583
 		if (isset($res['headers'])) {
@@ -596,7 +596,7 @@  discard block
 block discarded – undo
596 596
 
597 597
 	// on ne veut que les entetes
598 598
 	if (!$options['taille_max'] || $options['methode'] == 'HEAD' || $result['status'] == '304') {
599
-		spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($result, JSON_THROW_ON_ERROR), 'distant' . _LOG_DEBUG);
599
+		spip_log('RESULTAT recuperer_url '.$options['methode']." sur $url : ".json_encode($result, JSON_THROW_ON_ERROR), 'distant'._LOG_DEBUG);
600 600
 		return $result;
601 601
 	}
602 602
 
@@ -606,7 +606,7 @@  discard block
 block discarded – undo
606 606
 
607 607
 	$gz = false;
608 608
 	if (preg_match(",\bContent-Encoding: .*gzip,is", (string) $result['headers'])) {
609
-		$gz = (_DIR_TMP . md5(uniqid(random_int(0, mt_getrandmax()))) . '.tmp.gz');
609
+		$gz = (_DIR_TMP.md5(uniqid(random_int(0, mt_getrandmax()))).'.tmp.gz');
610 610
 	}
611 611
 
612 612
 	// si on n'a pas deja récupéré le contenu par une methode detournée
@@ -650,10 +650,10 @@  discard block
 block discarded – undo
650 650
 		$trace = json_decode(json_encode($result, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR);
651 651
 	} catch (JsonException $e) {
652 652
 		$trace = [];
653
-		spip_log('Failed to parse Json data : ' . $e->getMessage(), _LOG_ERREUR);
653
+		spip_log('Failed to parse Json data : '.$e->getMessage(), _LOG_ERREUR);
654 654
 	}
655 655
 	$trace['page'] = '...';
656
-	spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($trace, JSON_THROW_ON_ERROR), 'distant' . _LOG_DEBUG);
656
+	spip_log('RESULTAT recuperer_url '.$options['methode']." sur $url : ".json_encode($trace, JSON_THROW_ON_ERROR), 'distant'._LOG_DEBUG);
657 657
 
658 658
 	return $result;
659 659
 }
@@ -707,7 +707,7 @@  discard block
 block discarded – undo
707 707
 	$sig['url'] = $url;
708 708
 
709 709
 	$dir = sous_repertoire(_DIR_CACHE, 'curl');
710
-	$cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80);
710
+	$cache = md5(serialize($sig)).'-'.substr(preg_replace(',\W+,', '_', $url), 0, 80);
711 711
 	$sub = sous_repertoire($dir, substr($cache, 0, 2));
712 712
 	$cache = "$sub$cache";
713 713
 
@@ -761,7 +761,7 @@  discard block
 block discarded – undo
761 761
 	$fp = false;
762 762
 	if ($fichier) {
763 763
 		include_spip('inc/acces');
764
-		$tmpfile = "$fichier." . creer_uniqid() . '.tmp';
764
+		$tmpfile = "$fichier.".creer_uniqid().'.tmp';
765 765
 		$fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX);
766 766
 		if (!$fp && file_exists($fichier)) {
767 767
 			return filesize($fichier);
@@ -850,11 +850,11 @@  discard block
 block discarded – undo
850 850
 	}
851 851
 	$result['status'] = (int) $r[1];
852 852
 	while ($s = trim(fgets($handle, 16384))) {
853
-		$result['headers'][] = $s . "\n";
853
+		$result['headers'][] = $s."\n";
854 854
 		preg_match(',^([^:]*): *(.*)$,i', $s, $r);
855 855
 		[, $d, $v] = $r;
856 856
 		$d = strtolower(trim($d));
857
-		if ( $d === 'location' && $result['status'] >= 300 && $result['status'] < 400) {
857
+		if ($d === 'location' && $result['status'] >= 300 && $result['status'] < 400) {
858 858
 			$result['location'] = $v;
859 859
 		} elseif ($d === 'last-modified') {
860 860
 			$result['last_modified'] = strtotime($v);
@@ -902,7 +902,7 @@  discard block
 block discarded – undo
902 902
 
903 903
 	// on se place tout le temps comme si on était a la racine
904 904
 	if (_DIR_RACINE) {
905
-		$d = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', (string) $d);
905
+		$d = preg_replace(',^'.preg_quote((string) _DIR_RACINE, ',').',', '', (string) $d);
906 906
 	}
907 907
 
908 908
 	$m = md5($source);
@@ -910,18 +910,18 @@  discard block
 block discarded – undo
910 910
 	$filename =
911 911
 		$d
912 912
 		. substr(preg_replace(',[^\w-],', '', basename($source, $extension)), 0, 16)
913
-		. '-' . substr($m, 0, 8)
913
+		. '-'.substr($m, 0, 8)
914 914
 		. ".$extension";
915 915
 
916 916
 	// ancien nommage des fichiers distants : renommer le fichier a la volee si besoin pour eviter de dupliquer les caches
917 917
 	$legacy_filename =
918 918
 		$d
919
-		. substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12)
919
+		. substr(preg_replace(',[^\w-],', '', basename($source)).'-'.$m, 0, 12)
920 920
 		. substr($m, 0, 4)
921 921
 		. ".$extension";
922 922
 
923
-	if (file_exists(_DIR_RACINE . $legacy_filename)) {
924
-		@rename(_DIR_RACINE . $legacy_filename, $filename);
923
+	if (file_exists(_DIR_RACINE.$legacy_filename)) {
924
+		@rename(_DIR_RACINE.$legacy_filename, $filename);
925 925
 	}
926 926
 
927 927
 	return $filename;
@@ -946,7 +946,7 @@  discard block
 block discarded – undo
946 946
 	// Si c'est deja local pas de souci
947 947
 	if (!tester_url_absolue($source)) {
948 948
 		if (_DIR_RACINE) {
949
-			$source = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', $source);
949
+			$source = preg_replace(',^'.preg_quote((string) _DIR_RACINE, ',').',', '', $source);
950 950
 		}
951 951
 
952 952
 		return $source;
@@ -964,7 +964,7 @@  discard block
 block discarded – undo
964 964
 		$ext
965 965
 		&& preg_match(',^\w+$,', $ext)
966 966
 		&& ($f = nom_fichier_copie_locale($source, $ext))
967
-		&& file_exists(_DIR_RACINE . $f)
967
+		&& file_exists(_DIR_RACINE.$f)
968 968
 	) {
969 969
 		return $f;
970 970
 	}
@@ -972,7 +972,7 @@  discard block
 block discarded – undo
972 972
 
973 973
 	// Si c'est deja dans la table des documents,
974 974
 	// ramener le nom de sa copie potentielle
975
-	$ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''");
975
+	$ext = sql_getfetsel('extension', 'spip_documents', 'fichier='.sql_quote($source)." AND distant='oui' AND extension <> ''");
976 976
 
977 977
 	if ($ext) {
978 978
 		return nom_fichier_copie_locale($source, $ext);
@@ -983,9 +983,9 @@  discard block
 block discarded – undo
983 983
 
984 984
 	$ext = $path_parts ? $path_parts['extension'] : '';
985 985
 
986
-	if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
986
+	if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension='.sql_quote($ext))) {
987 987
 		$f = nom_fichier_copie_locale($source, $ext);
988
-		if (file_exists(_DIR_RACINE . $f)) {
988
+		if (file_exists(_DIR_RACINE.$f)) {
989 989
 			return $f;
990 990
 		}
991 991
 	}
@@ -993,7 +993,7 @@  discard block
 block discarded – undo
993 993
 	// Ping  pour voir si son extension est connue et autorisee
994 994
 	// avec mise en cache du resultat du ping
995 995
 
996
-	$cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source);
996
+	$cache = sous_repertoire(_DIR_CACHE, 'rid').md5($source);
997 997
 	if (
998 998
 		!@file_exists($cache)
999 999
 		|| !($path_parts = @unserialize(spip_file_get_contents($cache)))
@@ -1003,11 +1003,11 @@  discard block
 block discarded – undo
1003 1003
 		ecrire_fichier($cache, serialize($path_parts));
1004 1004
 	}
1005 1005
 	$ext = empty($path_parts['extension']) ? '' : $path_parts['extension'];
1006
-	if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) {
1006
+	if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension='.sql_quote($ext))) {
1007 1007
 		return nom_fichier_copie_locale($source, $ext);
1008 1008
 	}
1009 1009
 
1010
-	spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR);
1010
+	spip_log("pas de copie locale pour $source", 'distant'._LOG_ERREUR);
1011 1011
 	return null;
1012 1012
 }
1013 1013
 
@@ -1109,7 +1109,7 @@  discard block
 block discarded – undo
1109 1109
 		} else {
1110 1110
 			if ($a['body']) {
1111 1111
 				$a['extension'] = corriger_extension($extension);
1112
-				$a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension);
1112
+				$a['fichier'] = _DIR_RACINE.nom_fichier_copie_locale($source, $extension);
1113 1113
 				ecrire_fichier($a['fichier'], $a['body']);
1114 1114
 				$size_image = @spip_getimagesize($a['fichier']);
1115 1115
 				$a['largeur'] = (int) $size_image[0];
@@ -1181,20 +1181,20 @@  discard block
 block discarded – undo
1181 1181
 	$t = null;
1182 1182
 	if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) {
1183 1183
 		if (!$t && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)) {
1184
-			$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text'));
1184
+			$t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote(corriger_extension($rext[1]), '', 'text'));
1185 1185
 		}
1186 1186
 		if (
1187 1187
 			!$t
1188 1188
 			&& preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m)
1189 1189
 			&& preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext)
1190 1190
 		) {
1191
-			$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text'));
1191
+			$t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote(corriger_extension($rext[1]), '', 'text'));
1192 1192
 		}
1193 1193
 	}
1194 1194
 
1195 1195
 	// Autre mime/type (ou text/plain avec fichier d'extension inconnue)
1196 1196
 	if (!$t) {
1197
-		$t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type));
1197
+		$t = sql_fetsel('extension', 'spip_types_documents', 'mime_type='.sql_quote($mime_type));
1198 1198
 	}
1199 1199
 
1200 1200
 	// Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg)
@@ -1205,11 +1205,11 @@  discard block
 block discarded – undo
1205 1205
 		&& preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)
1206 1206
 	) {
1207 1207
 		# eviter xxx.3 => 3gp (> SPIP 3)
1208
-		$t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text'));
1208
+		$t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote(corriger_extension($rext[1]), '', 'text'));
1209 1209
 	}
1210 1210
 
1211 1211
 	if ($t) {
1212
-		spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant');
1212
+		spip_log("mime-type $mime_type ok, extension ".$t['extension'], 'distant');
1213 1213
 		return $t['extension'];
1214 1214
 	} else {
1215 1215
 		# par defaut on retombe sur '.bin' si c'est autorise
@@ -1312,7 +1312,7 @@  discard block
 block discarded – undo
1312 1312
 		}
1313 1313
 	} else {
1314 1314
 		$scheme = $t['scheme'];
1315
-		$noproxy = $scheme . '://';
1315
+		$noproxy = $scheme.'://';
1316 1316
 	}
1317 1317
 	if (isset($t['user'])) {
1318 1318
 		// user et pass doivent être passés en urlencodé dans l'URL, on redecode ici
@@ -1327,7 +1327,7 @@  discard block
 block discarded – undo
1327 1327
 	}
1328 1328
 
1329 1329
 	if (!empty($t['query'])) {
1330
-		$path .= '?' . $t['query'];
1330
+		$path .= '?'.$t['query'];
1331 1331
 	}
1332 1332
 
1333 1333
 	$f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date);
@@ -1401,29 +1401,29 @@  discard block
 block discarded – undo
1401 1401
 	$proxy_user = '';
1402 1402
 	$http_proxy = need_proxy($host);
1403 1403
 	if ($user) {
1404
-		$user = urlencode((string) $user[0]) . ':' . urlencode((string) $user[1]);
1404
+		$user = urlencode((string) $user[0]).':'.urlencode((string) $user[1]);
1405 1405
 	}
1406 1406
 
1407 1407
 	$connect = '';
1408 1408
 	if ($http_proxy) {
1409
-		if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') && in_array($scheme, ['tls','ssl'])) {
1410
-			$path_host = ($user ? "$user@" : '') . $host . (($port != 80) ? ":$port" : '');
1411
-			$connect = 'CONNECT ' . $path_host . " $vers\r\n"
1409
+		if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') && in_array($scheme, ['tls', 'ssl'])) {
1410
+			$path_host = ($user ? "$user@" : '').$host.(($port != 80) ? ":$port" : '');
1411
+			$connect = 'CONNECT '.$path_host." $vers\r\n"
1412 1412
 				. "Host: $path_host\r\n"
1413 1413
 				. "Proxy-Connection: Keep-Alive\r\n";
1414 1414
 		} else {
1415
-			$path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://")
1415
+			$path = (in_array($scheme, ['tls', 'ssl']) ? 'https://' : "$scheme://")
1416 1416
 				. ($user ? "$user@" : '')
1417
-				. "$host" . (($port != 80) ? ":$port" : '') . $path;
1417
+				. "$host".(($port != 80) ? ":$port" : '').$path;
1418 1418
 		}
1419 1419
 		$t2 = @parse_url($http_proxy);
1420 1420
 		$first_host = $t2['host'];
1421 1421
 		$first_port = ($t2['port'] ?? null) ?: 80;
1422 1422
 		if ($t2['user'] ?? null) {
1423
-			$proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']);
1423
+			$proxy_user = base64_encode($t2['user'].':'.$t2['pass']);
1424 1424
 		}
1425 1425
 	} else {
1426
-		$first_host = $noproxy . $host;
1426
+		$first_host = $noproxy.$host;
1427 1427
 		$first_port = $port;
1428 1428
 	}
1429 1429
 
@@ -1446,7 +1446,7 @@  discard block
 block discarded – undo
1446 1446
 		);
1447 1447
 		spip_log("Recuperer $path sur $first_host:$first_port par $f (via CONNECT)", 'connect');
1448 1448
 		if (!$f) {
1449
-			spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1449
+			spip_log("Erreur connexion $errno $errstr", 'distant'._LOG_ERREUR);
1450 1450
 			return $errno;
1451 1451
 		}
1452 1452
 		stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT);
@@ -1459,7 +1459,7 @@  discard block
 block discarded – undo
1459 1459
 			|| ($res = explode(' ', $res)) === []
1460 1460
 			|| $res[1] !== '200'
1461 1461
 		) {
1462
-			spip_log("Echec CONNECT sur $first_host:$first_port", 'connect' . _LOG_INFO_IMPORTANTE);
1462
+			spip_log("Echec CONNECT sur $first_host:$first_port", 'connect'._LOG_INFO_IMPORTANTE);
1463 1463
 			fclose($f);
1464 1464
 
1465 1465
 			return false;
@@ -1476,7 +1476,7 @@  discard block
 block discarded – undo
1476 1476
 		} while (!$f && $ntry-- && $errno !== 110 && sleep(1));
1477 1477
 		spip_log("Recuperer $path sur $first_host:$first_port par $f");
1478 1478
 		if (!$f) {
1479
-			spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR);
1479
+			spip_log("Erreur connexion $errno $errstr", 'distant'._LOG_ERREUR);
1480 1480
 
1481 1481
 			return $errno;
1482 1482
 		}
@@ -1486,16 +1486,16 @@  discard block
 block discarded – undo
1486 1486
 	$site = $GLOBALS['meta']['adresse_site'] ?? '';
1487 1487
 
1488 1488
 	$host_port = $host;
1489
-	if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) {
1489
+	if ($port != (in_array($scheme, ['tls', 'ssl']) ? 443 : 80)) {
1490 1490
 		$host_port .= ":$port";
1491 1491
 	}
1492 1492
 	$req = "$method $path $vers\r\n"
1493 1493
 		. "Host: $host_port\r\n"
1494
-		. 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n"
1495
-		. ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n"))
1494
+		. 'User-Agent: '._INC_DISTANT_USER_AGENT."\r\n"
1495
+		. ($refuse_gz ? '' : ('Accept-Encoding: '._INC_DISTANT_CONTENT_ENCODING."\r\n"))
1496 1496
 		. ($site ? "Referer: $site/$referer\r\n" : '')
1497
-		. ($date ? 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n") : '')
1498
-		. ($user ? 'Authorization: Basic ' . base64_encode(urldecode($user)) . "\r\n" : '')
1497
+		. ($date ? 'If-Modified-Since: '.(gmdate('D, d M Y H:i:s', $date)." GMT\r\n") : '')
1498
+		. ($user ? 'Authorization: Basic '.base64_encode(urldecode($user))."\r\n" : '')
1499 1499
 		. ($proxy_user ? "Proxy-Authorization: Basic $proxy_user\r\n" : '')
1500 1500
 		. (strpos($vers, '1.1') ? "Keep-Alive: 300\r\nConnection: keep-alive\r\n" : '');
1501 1501
 
Please login to merge, or discard this patch.