@@ -16,32 +16,32 @@ discard block |
||
| 16 | 16 | * @package SPIP\Core\Distant |
| 17 | 17 | **/ |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | if (!defined('_INC_DISTANT_VERSION_HTTP')) { |
| 23 | - define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 23 | + define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 24 | 24 | } |
| 25 | 25 | if (!defined('_INC_DISTANT_CONTENT_ENCODING')) { |
| 26 | - define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 26 | + define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 27 | 27 | } |
| 28 | 28 | if (!defined('_INC_DISTANT_USER_AGENT')) { |
| 29 | - define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 29 | + define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 30 | 30 | } |
| 31 | 31 | if (!defined('_INC_DISTANT_MAX_SIZE')) { |
| 32 | - define('_INC_DISTANT_MAX_SIZE', 2097152); |
|
| 32 | + define('_INC_DISTANT_MAX_SIZE', 2097152); |
|
| 33 | 33 | } |
| 34 | 34 | if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) { |
| 35 | - define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 35 | + define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | define('_REGEXP_COPIE_LOCALE', ',' . |
| 39 | - preg_replace( |
|
| 40 | - '@^https?:@', |
|
| 41 | - 'https?:', |
|
| 42 | - (isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '') |
|
| 43 | - ) |
|
| 44 | - . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 39 | + preg_replace( |
|
| 40 | + '@^https?:@', |
|
| 41 | + 'https?:', |
|
| 42 | + (isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '') |
|
| 43 | + ) |
|
| 44 | + . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 45 | 45 | |
| 46 | 46 | //@define('_COPIE_LOCALE_MAX_SIZE',2097152); // poids (inc/utils l'a fait) |
| 47 | 47 | |
@@ -68,89 +68,89 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | function copie_locale($source, $mode = 'auto', $local = null, $taille_max = null) { |
| 70 | 70 | |
| 71 | - // si c'est la protection de soi-meme, retourner le path |
|
| 72 | - if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 73 | - $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]); |
|
| 74 | - |
|
| 75 | - return @file_exists($source) ? $source : false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - if (is_null($local)) { |
|
| 79 | - $local = fichier_copie_locale($source); |
|
| 80 | - } else { |
|
| 81 | - if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) { |
|
| 82 | - $local = substr($local, strlen(_DIR_RACINE)); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 87 | - // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 88 | - // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 89 | - if (!$local) { |
|
| 90 | - return false; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $localrac = _DIR_RACINE . $local; |
|
| 94 | - $t = ($mode === 'force') ? false : @file_exists($localrac); |
|
| 95 | - |
|
| 96 | - // test d'existence du fichier |
|
| 97 | - if ($mode === 'test') { |
|
| 98 | - return $t ? $local : ''; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // sinon voir si on doit/peut le telecharger |
|
| 102 | - if ($local === $source or !tester_url_absolue($source)) { |
|
| 103 | - return $local; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if ($mode === 'modif' or !$t) { |
|
| 107 | - // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 108 | - // et des eventuelles recuperations concurantes |
|
| 109 | - include_spip('inc/acces'); |
|
| 110 | - if (!$taille_max) { |
|
| 111 | - $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 112 | - } |
|
| 113 | - $res = recuperer_url( |
|
| 114 | - $source, |
|
| 115 | - ['file' => $localrac, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : ''] |
|
| 116 | - ); |
|
| 117 | - if (!$res or (!$res['length'] and $res['status'] != 304)) { |
|
| 118 | - spip_log("copie_locale : Echec recuperation $source sur $localrac status : " . ($res ? $res['status'] : '-'), 'distant' . _LOG_INFO_IMPORTANTE); |
|
| 119 | - } |
|
| 120 | - else { |
|
| 121 | - spip_log("copie_locale : recuperation $source sur $localrac OK | taille " . $res['length'] . ' status ' . $res['status'], 'distant'); |
|
| 122 | - } |
|
| 123 | - if (!$res or !$res['length']) { |
|
| 124 | - // si $t c'est sans doute juste un not-modified-since |
|
| 125 | - return $t ? $local : false; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // si on retrouve l'extension |
|
| 129 | - if ( |
|
| 130 | - !empty($res['headers']) |
|
| 131 | - and $extension = distant_trouver_extension_selon_headers($source, $res['headers']) |
|
| 132 | - ) { |
|
| 133 | - if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) { |
|
| 134 | - $sanitizer($localrac); |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // pour une eventuelle indexation |
|
| 139 | - pipeline( |
|
| 140 | - 'post_edition', |
|
| 141 | - [ |
|
| 142 | - 'args' => [ |
|
| 143 | - 'operation' => 'copie_locale', |
|
| 144 | - 'source' => $source, |
|
| 145 | - 'fichier' => $local, |
|
| 146 | - 'http_res' => $res['length'], |
|
| 147 | - ], |
|
| 148 | - 'data' => null |
|
| 149 | - ] |
|
| 150 | - ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $local; |
|
| 71 | + // si c'est la protection de soi-meme, retourner le path |
|
| 72 | + if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 73 | + $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]); |
|
| 74 | + |
|
| 75 | + return @file_exists($source) ? $source : false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + if (is_null($local)) { |
|
| 79 | + $local = fichier_copie_locale($source); |
|
| 80 | + } else { |
|
| 81 | + if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) { |
|
| 82 | + $local = substr($local, strlen(_DIR_RACINE)); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 87 | + // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 88 | + // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 89 | + if (!$local) { |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $localrac = _DIR_RACINE . $local; |
|
| 94 | + $t = ($mode === 'force') ? false : @file_exists($localrac); |
|
| 95 | + |
|
| 96 | + // test d'existence du fichier |
|
| 97 | + if ($mode === 'test') { |
|
| 98 | + return $t ? $local : ''; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // sinon voir si on doit/peut le telecharger |
|
| 102 | + if ($local === $source or !tester_url_absolue($source)) { |
|
| 103 | + return $local; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if ($mode === 'modif' or !$t) { |
|
| 107 | + // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 108 | + // et des eventuelles recuperations concurantes |
|
| 109 | + include_spip('inc/acces'); |
|
| 110 | + if (!$taille_max) { |
|
| 111 | + $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 112 | + } |
|
| 113 | + $res = recuperer_url( |
|
| 114 | + $source, |
|
| 115 | + ['file' => $localrac, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : ''] |
|
| 116 | + ); |
|
| 117 | + if (!$res or (!$res['length'] and $res['status'] != 304)) { |
|
| 118 | + spip_log("copie_locale : Echec recuperation $source sur $localrac status : " . ($res ? $res['status'] : '-'), 'distant' . _LOG_INFO_IMPORTANTE); |
|
| 119 | + } |
|
| 120 | + else { |
|
| 121 | + spip_log("copie_locale : recuperation $source sur $localrac OK | taille " . $res['length'] . ' status ' . $res['status'], 'distant'); |
|
| 122 | + } |
|
| 123 | + if (!$res or !$res['length']) { |
|
| 124 | + // si $t c'est sans doute juste un not-modified-since |
|
| 125 | + return $t ? $local : false; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // si on retrouve l'extension |
|
| 129 | + if ( |
|
| 130 | + !empty($res['headers']) |
|
| 131 | + and $extension = distant_trouver_extension_selon_headers($source, $res['headers']) |
|
| 132 | + ) { |
|
| 133 | + if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) { |
|
| 134 | + $sanitizer($localrac); |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // pour une eventuelle indexation |
|
| 139 | + pipeline( |
|
| 140 | + 'post_edition', |
|
| 141 | + [ |
|
| 142 | + 'args' => [ |
|
| 143 | + 'operation' => 'copie_locale', |
|
| 144 | + 'source' => $source, |
|
| 145 | + 'fichier' => $local, |
|
| 146 | + 'http_res' => $res['length'], |
|
| 147 | + ], |
|
| 148 | + 'data' => null |
|
| 149 | + ] |
|
| 150 | + ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $local; |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** |
@@ -165,99 +165,99 @@ discard block |
||
| 165 | 165 | * url ou false en cas d'echec |
| 166 | 166 | */ |
| 167 | 167 | function valider_url_distante($url, $known_hosts = []) { |
| 168 | - if (!function_exists('protocole_verifier')) { |
|
| 169 | - include_spip('inc/filtres_mini'); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - if (!protocole_verifier($url, ['http', 'https'])) { |
|
| 173 | - return false; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - $parsed_url = parse_url($url); |
|
| 177 | - if (!$parsed_url or empty($parsed_url['host'])) { |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { |
|
| 182 | - return false; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 186 | - return false; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - if (!is_array($known_hosts)) { |
|
| 190 | - $known_hosts = [$known_hosts]; |
|
| 191 | - } |
|
| 192 | - $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 193 | - $known_hosts[] = url_de_base(); |
|
| 194 | - $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 195 | - |
|
| 196 | - $is_known_host = false; |
|
| 197 | - foreach ($known_hosts as $known_host) { |
|
| 198 | - $parse_known = parse_url($known_host); |
|
| 199 | - if ( |
|
| 200 | - $parse_known |
|
| 201 | - and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 202 | - ) { |
|
| 203 | - $is_known_host = true; |
|
| 204 | - break; |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - if (!$is_known_host) { |
|
| 209 | - $host = trim($parsed_url['host'], '.'); |
|
| 210 | - if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 211 | - $ip = gethostbyname($host); |
|
| 212 | - if ($ip === $host) { |
|
| 213 | - // Error condition for gethostbyname() |
|
| 214 | - $ip = false; |
|
| 215 | - } |
|
| 216 | - if ($records = dns_get_record($host)) { |
|
| 217 | - foreach ($records as $record) { |
|
| 218 | - // il faut que le TTL soit suffisant afin d'etre certain que le copie_locale eventuel qui suit |
|
| 219 | - // se fasse sur la meme IP |
|
| 220 | - if ($record['ttl']<10) { |
|
| 221 | - $ip = false; |
|
| 222 | - break; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - else { |
|
| 227 | - $ip = false; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - if ($ip) { |
|
| 231 | - if (! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 232 | - return false; |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - if (empty($parsed_url['port'])) { |
|
| 238 | - return $url; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $port = $parsed_url['port']; |
|
| 242 | - if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 243 | - return $url; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - if ($is_known_host) { |
|
| 247 | - foreach ($known_hosts as $known_host) { |
|
| 248 | - $parse_known = parse_url($known_host); |
|
| 249 | - if ( |
|
| 250 | - $parse_known |
|
| 251 | - and !empty($parse_known['port']) |
|
| 252 | - and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 253 | - and $parse_known['port'] == $port |
|
| 254 | - ) { |
|
| 255 | - return $url; |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return false; |
|
| 168 | + if (!function_exists('protocole_verifier')) { |
|
| 169 | + include_spip('inc/filtres_mini'); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + if (!protocole_verifier($url, ['http', 'https'])) { |
|
| 173 | + return false; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + $parsed_url = parse_url($url); |
|
| 177 | + if (!$parsed_url or empty($parsed_url['host'])) { |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { |
|
| 182 | + return false; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 186 | + return false; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + if (!is_array($known_hosts)) { |
|
| 190 | + $known_hosts = [$known_hosts]; |
|
| 191 | + } |
|
| 192 | + $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 193 | + $known_hosts[] = url_de_base(); |
|
| 194 | + $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 195 | + |
|
| 196 | + $is_known_host = false; |
|
| 197 | + foreach ($known_hosts as $known_host) { |
|
| 198 | + $parse_known = parse_url($known_host); |
|
| 199 | + if ( |
|
| 200 | + $parse_known |
|
| 201 | + and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 202 | + ) { |
|
| 203 | + $is_known_host = true; |
|
| 204 | + break; |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + if (!$is_known_host) { |
|
| 209 | + $host = trim($parsed_url['host'], '.'); |
|
| 210 | + if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 211 | + $ip = gethostbyname($host); |
|
| 212 | + if ($ip === $host) { |
|
| 213 | + // Error condition for gethostbyname() |
|
| 214 | + $ip = false; |
|
| 215 | + } |
|
| 216 | + if ($records = dns_get_record($host)) { |
|
| 217 | + foreach ($records as $record) { |
|
| 218 | + // il faut que le TTL soit suffisant afin d'etre certain que le copie_locale eventuel qui suit |
|
| 219 | + // se fasse sur la meme IP |
|
| 220 | + if ($record['ttl']<10) { |
|
| 221 | + $ip = false; |
|
| 222 | + break; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + else { |
|
| 227 | + $ip = false; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + if ($ip) { |
|
| 231 | + if (! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 232 | + return false; |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + if (empty($parsed_url['port'])) { |
|
| 238 | + return $url; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $port = $parsed_url['port']; |
|
| 242 | + if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 243 | + return $url; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + if ($is_known_host) { |
|
| 247 | + foreach ($known_hosts as $known_host) { |
|
| 248 | + $parse_known = parse_url($known_host); |
|
| 249 | + if ( |
|
| 250 | + $parse_known |
|
| 251 | + and !empty($parse_known['port']) |
|
| 252 | + and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 253 | + and $parse_known['port'] == $port |
|
| 254 | + ) { |
|
| 255 | + return $url; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return false; |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | /** |
@@ -277,86 +277,86 @@ discard block |
||
| 277 | 277 | */ |
| 278 | 278 | function prepare_donnees_post($donnees, $boundary = '') { |
| 279 | 279 | |
| 280 | - // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 281 | - // pour un appel soap par exemple |
|
| 282 | - // l'entete est separe des donnees par un double retour a la ligne |
|
| 283 | - // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 284 | - if (is_string($donnees) && strlen($donnees)) { |
|
| 285 | - $entete = ''; |
|
| 286 | - // on repasse tous les \r\n et \r en simples \n |
|
| 287 | - $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 288 | - $donnees = str_replace("\r", "\n", $donnees); |
|
| 289 | - // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 290 | - $p = strpos($donnees, "\n\n"); |
|
| 291 | - if ($p !== false) { |
|
| 292 | - $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 293 | - $donnees = substr($donnees, $p + 2); |
|
| 294 | - } |
|
| 295 | - $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 296 | - } else { |
|
| 297 | - /* boundary automatique */ |
|
| 298 | - // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 299 | - if ($boundary === '') { |
|
| 300 | - $taille = 0; |
|
| 301 | - foreach ($donnees as $cle => $valeur) { |
|
| 302 | - if (is_array($valeur)) { |
|
| 303 | - foreach ($valeur as $val2) { |
|
| 304 | - $taille += strlen($val2); |
|
| 305 | - } |
|
| 306 | - } else { |
|
| 307 | - // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 308 | - $taille += strlen($valeur); |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - if ($taille > 500) { |
|
| 312 | - $boundary = substr(md5(rand() . 'spip'), 0, 8); |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - if (is_string($boundary) and strlen($boundary)) { |
|
| 317 | - // fabrique une chaine HTTP pour un POST avec boundary |
|
| 318 | - $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 319 | - $chaine = ''; |
|
| 320 | - if (is_array($donnees)) { |
|
| 321 | - foreach ($donnees as $cle => $valeur) { |
|
| 322 | - if (is_array($valeur)) { |
|
| 323 | - foreach ($valeur as $val2) { |
|
| 324 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 325 | - $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 326 | - $chaine .= "\r\n"; |
|
| 327 | - $chaine .= $val2; |
|
| 328 | - } |
|
| 329 | - } else { |
|
| 330 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 331 | - $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 332 | - $chaine .= "\r\n"; |
|
| 333 | - $chaine .= $valeur; |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 337 | - } |
|
| 338 | - } else { |
|
| 339 | - // fabrique une chaine HTTP simple pour un POST |
|
| 340 | - $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; |
|
| 341 | - $chaine = []; |
|
| 342 | - if (is_array($donnees)) { |
|
| 343 | - foreach ($donnees as $cle => $valeur) { |
|
| 344 | - if (is_array($valeur)) { |
|
| 345 | - foreach ($valeur as $val2) { |
|
| 346 | - $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2); |
|
| 347 | - } |
|
| 348 | - } else { |
|
| 349 | - $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur); |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - $chaine = implode('&', $chaine); |
|
| 353 | - } else { |
|
| 354 | - $chaine = $donnees; |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - return [$entete, $chaine]; |
|
| 280 | + // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 281 | + // pour un appel soap par exemple |
|
| 282 | + // l'entete est separe des donnees par un double retour a la ligne |
|
| 283 | + // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 284 | + if (is_string($donnees) && strlen($donnees)) { |
|
| 285 | + $entete = ''; |
|
| 286 | + // on repasse tous les \r\n et \r en simples \n |
|
| 287 | + $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 288 | + $donnees = str_replace("\r", "\n", $donnees); |
|
| 289 | + // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 290 | + $p = strpos($donnees, "\n\n"); |
|
| 291 | + if ($p !== false) { |
|
| 292 | + $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 293 | + $donnees = substr($donnees, $p + 2); |
|
| 294 | + } |
|
| 295 | + $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 296 | + } else { |
|
| 297 | + /* boundary automatique */ |
|
| 298 | + // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 299 | + if ($boundary === '') { |
|
| 300 | + $taille = 0; |
|
| 301 | + foreach ($donnees as $cle => $valeur) { |
|
| 302 | + if (is_array($valeur)) { |
|
| 303 | + foreach ($valeur as $val2) { |
|
| 304 | + $taille += strlen($val2); |
|
| 305 | + } |
|
| 306 | + } else { |
|
| 307 | + // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 308 | + $taille += strlen($valeur); |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + if ($taille > 500) { |
|
| 312 | + $boundary = substr(md5(rand() . 'spip'), 0, 8); |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + if (is_string($boundary) and strlen($boundary)) { |
|
| 317 | + // fabrique une chaine HTTP pour un POST avec boundary |
|
| 318 | + $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 319 | + $chaine = ''; |
|
| 320 | + if (is_array($donnees)) { |
|
| 321 | + foreach ($donnees as $cle => $valeur) { |
|
| 322 | + if (is_array($valeur)) { |
|
| 323 | + foreach ($valeur as $val2) { |
|
| 324 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 325 | + $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 326 | + $chaine .= "\r\n"; |
|
| 327 | + $chaine .= $val2; |
|
| 328 | + } |
|
| 329 | + } else { |
|
| 330 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 331 | + $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 332 | + $chaine .= "\r\n"; |
|
| 333 | + $chaine .= $valeur; |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 337 | + } |
|
| 338 | + } else { |
|
| 339 | + // fabrique une chaine HTTP simple pour un POST |
|
| 340 | + $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; |
|
| 341 | + $chaine = []; |
|
| 342 | + if (is_array($donnees)) { |
|
| 343 | + foreach ($donnees as $cle => $valeur) { |
|
| 344 | + if (is_array($valeur)) { |
|
| 345 | + foreach ($valeur as $val2) { |
|
| 346 | + $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2); |
|
| 347 | + } |
|
| 348 | + } else { |
|
| 349 | + $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur); |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + $chaine = implode('&', $chaine); |
|
| 353 | + } else { |
|
| 354 | + $chaine = $donnees; |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + return [$entete, $chaine]; |
|
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | /** |
@@ -370,22 +370,22 @@ discard block |
||
| 370 | 370 | */ |
| 371 | 371 | function url_to_ascii($url_idn) { |
| 372 | 372 | |
| 373 | - if ($parts = parse_url($url_idn)) { |
|
| 374 | - $host = $parts['host']; |
|
| 375 | - if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 376 | - include_spip('inc/idna_convert.class'); |
|
| 377 | - $IDN = new idna_convert(); |
|
| 378 | - $host_ascii = $IDN->encode($host); |
|
| 379 | - $url_idn = explode($host, $url_idn, 2); |
|
| 380 | - $url_idn = implode($host_ascii, $url_idn); |
|
| 381 | - } |
|
| 382 | - // et on urlencode les char utf si besoin dans le path |
|
| 383 | - $url_idn = preg_replace_callback('/[^\x20-\x7f]/', function ($match) { |
|
| 384 | - return urlencode($match[0]); |
|
| 385 | - }, $url_idn); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - return $url_idn; |
|
| 373 | + if ($parts = parse_url($url_idn)) { |
|
| 374 | + $host = $parts['host']; |
|
| 375 | + if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 376 | + include_spip('inc/idna_convert.class'); |
|
| 377 | + $IDN = new idna_convert(); |
|
| 378 | + $host_ascii = $IDN->encode($host); |
|
| 379 | + $url_idn = explode($host, $url_idn, 2); |
|
| 380 | + $url_idn = implode($host_ascii, $url_idn); |
|
| 381 | + } |
|
| 382 | + // et on urlencode les char utf si besoin dans le path |
|
| 383 | + $url_idn = preg_replace_callback('/[^\x20-\x7f]/', function ($match) { |
|
| 384 | + return urlencode($match[0]); |
|
| 385 | + }, $url_idn); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + return $url_idn; |
|
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | /** |
@@ -426,197 +426,197 @@ discard block |
||
| 426 | 426 | * string file : nom du fichier si enregistre dans un fichier |
| 427 | 427 | */ |
| 428 | 428 | function recuperer_url($url, $options = []) { |
| 429 | - // Conserve la mémoire de la méthode fournit éventuellement |
|
| 430 | - $methode_demandee = $options['methode'] ?? ''; |
|
| 431 | - $default = [ |
|
| 432 | - 'transcoder' => false, |
|
| 433 | - 'methode' => 'GET', |
|
| 434 | - 'taille_max' => null, |
|
| 435 | - 'headers' => [], |
|
| 436 | - 'datas' => '', |
|
| 437 | - 'boundary' => '', |
|
| 438 | - 'refuser_gz' => false, |
|
| 439 | - 'if_modified_since' => '', |
|
| 440 | - 'uri_referer' => '', |
|
| 441 | - 'file' => '', |
|
| 442 | - 'follow_location' => 10, |
|
| 443 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 444 | - ]; |
|
| 445 | - $options = array_merge($default, $options); |
|
| 446 | - // copier directement dans un fichier ? |
|
| 447 | - $copy = $options['file']; |
|
| 448 | - |
|
| 449 | - if ($options['methode'] == 'HEAD') { |
|
| 450 | - $options['taille_max'] = 0; |
|
| 451 | - } |
|
| 452 | - if (is_null($options['taille_max'])) { |
|
| 453 | - $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - |
|
| 457 | - // Ajout des en-têtes spécifiques si besoin |
|
| 458 | - $head_add = ''; |
|
| 459 | - if (!empty($options['headers'])) { |
|
| 460 | - foreach ($options['headers'] as $champ => $valeur) { |
|
| 461 | - $head_add .= $champ . ': ' . $valeur . "\r\n"; |
|
| 462 | - } |
|
| 463 | - // ne pas le repasser a recuperer_url si on follow un location, car ils seront dans datas |
|
| 464 | - unset($options['headers']); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - if (!empty($options['datas'])) { |
|
| 468 | - list($head, $postdata) = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 469 | - $head .= $head_add; |
|
| 470 | - if (stripos($head, 'Content-Length:') === false) { |
|
| 471 | - $head .= 'Content-Length: ' . strlen($postdata) . "\r\n"; |
|
| 472 | - } |
|
| 473 | - $options['datas'] = $head . "\r\n" . $postdata; |
|
| 474 | - if ( |
|
| 475 | - strlen($postdata) |
|
| 476 | - and !$methode_demandee |
|
| 477 | - ) { |
|
| 478 | - $options['methode'] = 'POST'; |
|
| 479 | - } |
|
| 480 | - } elseif ($head_add) { |
|
| 481 | - $options['datas'] = $head_add . "\r\n"; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 485 | - $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 486 | - if (!tester_url_absolue($url)) { |
|
| 487 | - $url = 'http://' . $url; |
|
| 488 | - } elseif (strncmp($url, '//', 2) == 0) { |
|
| 489 | - $url = 'http:' . $url; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - $url = url_to_ascii($url); |
|
| 493 | - |
|
| 494 | - $result = [ |
|
| 495 | - 'status' => 0, |
|
| 496 | - 'headers' => '', |
|
| 497 | - 'page' => '', |
|
| 498 | - 'length' => 0, |
|
| 499 | - 'last_modified' => '', |
|
| 500 | - 'location' => '', |
|
| 501 | - 'url' => $url |
|
| 502 | - ]; |
|
| 503 | - |
|
| 504 | - // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 505 | - $refuser_gz = (($options['refuser_gz'] or $copy) ? true : false); |
|
| 506 | - |
|
| 507 | - // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 508 | - list($handle, $fopen) = init_http( |
|
| 509 | - $options['methode'], |
|
| 510 | - $url, |
|
| 511 | - $refuser_gz, |
|
| 512 | - $options['uri_referer'], |
|
| 513 | - $options['datas'], |
|
| 514 | - $options['version_http'], |
|
| 515 | - $options['if_modified_since'] |
|
| 516 | - ); |
|
| 517 | - if (!$handle) { |
|
| 518 | - spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR); |
|
| 519 | - |
|
| 520 | - return false; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - // Sauf en fopen, envoyer le flux d'entree |
|
| 524 | - // et recuperer les en-tetes de reponses |
|
| 525 | - if (!$fopen) { |
|
| 526 | - $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 527 | - if (!$res) { |
|
| 528 | - fclose($handle); |
|
| 529 | - $t = @parse_url($url); |
|
| 530 | - $host = $t['host']; |
|
| 531 | - // Chinoisierie inexplicable pour contrer |
|
| 532 | - // les actions liberticides de l'empire du milieu |
|
| 533 | - if ( |
|
| 534 | - !need_proxy($host) |
|
| 535 | - and $res = @file_get_contents($url) |
|
| 536 | - ) { |
|
| 537 | - $result['length'] = strlen($res); |
|
| 538 | - if ($copy) { |
|
| 539 | - ecrire_fichier($copy, $res); |
|
| 540 | - $result['file'] = $copy; |
|
| 541 | - } else { |
|
| 542 | - $result['page'] = $res; |
|
| 543 | - } |
|
| 544 | - $res = [ |
|
| 545 | - 'status' => 200, |
|
| 546 | - ]; |
|
| 547 | - } else { |
|
| 548 | - spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR); |
|
| 549 | - return false; |
|
| 550 | - } |
|
| 551 | - } elseif ($res['location'] and $options['follow_location']) { |
|
| 552 | - $options['follow_location']--; |
|
| 553 | - fclose($handle); |
|
| 554 | - include_spip('inc/filtres'); |
|
| 555 | - $url = suivre_lien($url, $res['location']); |
|
| 556 | - spip_log("recuperer_url recommence sur $url", 'distant'); |
|
| 557 | - |
|
| 558 | - return recuperer_url($url, $options); |
|
| 559 | - } elseif ($res['status'] !== 200) { |
|
| 560 | - spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant'); |
|
| 561 | - } |
|
| 562 | - $result['status'] = $res['status']; |
|
| 563 | - if (isset($res['headers'])) { |
|
| 564 | - $result['headers'] = $res['headers']; |
|
| 565 | - } |
|
| 566 | - if (isset($res['last_modified'])) { |
|
| 567 | - $result['last_modified'] = $res['last_modified']; |
|
| 568 | - } |
|
| 569 | - if (isset($res['location'])) { |
|
| 570 | - $result['location'] = $res['location']; |
|
| 571 | - } |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - // on ne veut que les entetes |
|
| 575 | - if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') { |
|
| 576 | - return $result; |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - |
|
| 580 | - // s'il faut deballer, le faire via un fichier temporaire |
|
| 581 | - // sinon la memoire explose pour les gros flux |
|
| 582 | - |
|
| 583 | - $gz = false; |
|
| 584 | - if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) { |
|
| 585 | - $gz = (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz'); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - // si on a pas deja recuperer le contenu par une methode detournee |
|
| 589 | - if (!$result['length']) { |
|
| 590 | - $res = recuperer_body($handle, $options['taille_max'], $gz ? $gz : $copy); |
|
| 591 | - fclose($handle); |
|
| 592 | - if ($copy) { |
|
| 593 | - $result['length'] = $res; |
|
| 594 | - $result['file'] = $copy; |
|
| 595 | - } elseif ($res) { |
|
| 596 | - $result['page'] = &$res; |
|
| 597 | - $result['length'] = strlen($result['page']); |
|
| 598 | - } |
|
| 599 | - if (!$result['status']) { |
|
| 600 | - $result['status'] = 200; // on a reussi, donc ! |
|
| 601 | - } |
|
| 602 | - } |
|
| 603 | - if (!$result['page']) { |
|
| 604 | - return $result; |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - // Decompresser au besoin |
|
| 608 | - if ($gz) { |
|
| 609 | - $result['page'] = implode('', gzfile($gz)); |
|
| 610 | - supprimer_fichier($gz); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - // Faut-il l'importer dans notre charset local ? |
|
| 614 | - if ($options['transcoder']) { |
|
| 615 | - include_spip('inc/charsets'); |
|
| 616 | - $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - return $result; |
|
| 429 | + // Conserve la mémoire de la méthode fournit éventuellement |
|
| 430 | + $methode_demandee = $options['methode'] ?? ''; |
|
| 431 | + $default = [ |
|
| 432 | + 'transcoder' => false, |
|
| 433 | + 'methode' => 'GET', |
|
| 434 | + 'taille_max' => null, |
|
| 435 | + 'headers' => [], |
|
| 436 | + 'datas' => '', |
|
| 437 | + 'boundary' => '', |
|
| 438 | + 'refuser_gz' => false, |
|
| 439 | + 'if_modified_since' => '', |
|
| 440 | + 'uri_referer' => '', |
|
| 441 | + 'file' => '', |
|
| 442 | + 'follow_location' => 10, |
|
| 443 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 444 | + ]; |
|
| 445 | + $options = array_merge($default, $options); |
|
| 446 | + // copier directement dans un fichier ? |
|
| 447 | + $copy = $options['file']; |
|
| 448 | + |
|
| 449 | + if ($options['methode'] == 'HEAD') { |
|
| 450 | + $options['taille_max'] = 0; |
|
| 451 | + } |
|
| 452 | + if (is_null($options['taille_max'])) { |
|
| 453 | + $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + |
|
| 457 | + // Ajout des en-têtes spécifiques si besoin |
|
| 458 | + $head_add = ''; |
|
| 459 | + if (!empty($options['headers'])) { |
|
| 460 | + foreach ($options['headers'] as $champ => $valeur) { |
|
| 461 | + $head_add .= $champ . ': ' . $valeur . "\r\n"; |
|
| 462 | + } |
|
| 463 | + // ne pas le repasser a recuperer_url si on follow un location, car ils seront dans datas |
|
| 464 | + unset($options['headers']); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + if (!empty($options['datas'])) { |
|
| 468 | + list($head, $postdata) = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 469 | + $head .= $head_add; |
|
| 470 | + if (stripos($head, 'Content-Length:') === false) { |
|
| 471 | + $head .= 'Content-Length: ' . strlen($postdata) . "\r\n"; |
|
| 472 | + } |
|
| 473 | + $options['datas'] = $head . "\r\n" . $postdata; |
|
| 474 | + if ( |
|
| 475 | + strlen($postdata) |
|
| 476 | + and !$methode_demandee |
|
| 477 | + ) { |
|
| 478 | + $options['methode'] = 'POST'; |
|
| 479 | + } |
|
| 480 | + } elseif ($head_add) { |
|
| 481 | + $options['datas'] = $head_add . "\r\n"; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 485 | + $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 486 | + if (!tester_url_absolue($url)) { |
|
| 487 | + $url = 'http://' . $url; |
|
| 488 | + } elseif (strncmp($url, '//', 2) == 0) { |
|
| 489 | + $url = 'http:' . $url; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + $url = url_to_ascii($url); |
|
| 493 | + |
|
| 494 | + $result = [ |
|
| 495 | + 'status' => 0, |
|
| 496 | + 'headers' => '', |
|
| 497 | + 'page' => '', |
|
| 498 | + 'length' => 0, |
|
| 499 | + 'last_modified' => '', |
|
| 500 | + 'location' => '', |
|
| 501 | + 'url' => $url |
|
| 502 | + ]; |
|
| 503 | + |
|
| 504 | + // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 505 | + $refuser_gz = (($options['refuser_gz'] or $copy) ? true : false); |
|
| 506 | + |
|
| 507 | + // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 508 | + list($handle, $fopen) = init_http( |
|
| 509 | + $options['methode'], |
|
| 510 | + $url, |
|
| 511 | + $refuser_gz, |
|
| 512 | + $options['uri_referer'], |
|
| 513 | + $options['datas'], |
|
| 514 | + $options['version_http'], |
|
| 515 | + $options['if_modified_since'] |
|
| 516 | + ); |
|
| 517 | + if (!$handle) { |
|
| 518 | + spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR); |
|
| 519 | + |
|
| 520 | + return false; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + // Sauf en fopen, envoyer le flux d'entree |
|
| 524 | + // et recuperer les en-tetes de reponses |
|
| 525 | + if (!$fopen) { |
|
| 526 | + $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 527 | + if (!$res) { |
|
| 528 | + fclose($handle); |
|
| 529 | + $t = @parse_url($url); |
|
| 530 | + $host = $t['host']; |
|
| 531 | + // Chinoisierie inexplicable pour contrer |
|
| 532 | + // les actions liberticides de l'empire du milieu |
|
| 533 | + if ( |
|
| 534 | + !need_proxy($host) |
|
| 535 | + and $res = @file_get_contents($url) |
|
| 536 | + ) { |
|
| 537 | + $result['length'] = strlen($res); |
|
| 538 | + if ($copy) { |
|
| 539 | + ecrire_fichier($copy, $res); |
|
| 540 | + $result['file'] = $copy; |
|
| 541 | + } else { |
|
| 542 | + $result['page'] = $res; |
|
| 543 | + } |
|
| 544 | + $res = [ |
|
| 545 | + 'status' => 200, |
|
| 546 | + ]; |
|
| 547 | + } else { |
|
| 548 | + spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR); |
|
| 549 | + return false; |
|
| 550 | + } |
|
| 551 | + } elseif ($res['location'] and $options['follow_location']) { |
|
| 552 | + $options['follow_location']--; |
|
| 553 | + fclose($handle); |
|
| 554 | + include_spip('inc/filtres'); |
|
| 555 | + $url = suivre_lien($url, $res['location']); |
|
| 556 | + spip_log("recuperer_url recommence sur $url", 'distant'); |
|
| 557 | + |
|
| 558 | + return recuperer_url($url, $options); |
|
| 559 | + } elseif ($res['status'] !== 200) { |
|
| 560 | + spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant'); |
|
| 561 | + } |
|
| 562 | + $result['status'] = $res['status']; |
|
| 563 | + if (isset($res['headers'])) { |
|
| 564 | + $result['headers'] = $res['headers']; |
|
| 565 | + } |
|
| 566 | + if (isset($res['last_modified'])) { |
|
| 567 | + $result['last_modified'] = $res['last_modified']; |
|
| 568 | + } |
|
| 569 | + if (isset($res['location'])) { |
|
| 570 | + $result['location'] = $res['location']; |
|
| 571 | + } |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + // on ne veut que les entetes |
|
| 575 | + if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') { |
|
| 576 | + return $result; |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + |
|
| 580 | + // s'il faut deballer, le faire via un fichier temporaire |
|
| 581 | + // sinon la memoire explose pour les gros flux |
|
| 582 | + |
|
| 583 | + $gz = false; |
|
| 584 | + if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) { |
|
| 585 | + $gz = (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz'); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + // si on a pas deja recuperer le contenu par une methode detournee |
|
| 589 | + if (!$result['length']) { |
|
| 590 | + $res = recuperer_body($handle, $options['taille_max'], $gz ? $gz : $copy); |
|
| 591 | + fclose($handle); |
|
| 592 | + if ($copy) { |
|
| 593 | + $result['length'] = $res; |
|
| 594 | + $result['file'] = $copy; |
|
| 595 | + } elseif ($res) { |
|
| 596 | + $result['page'] = &$res; |
|
| 597 | + $result['length'] = strlen($result['page']); |
|
| 598 | + } |
|
| 599 | + if (!$result['status']) { |
|
| 600 | + $result['status'] = 200; // on a reussi, donc ! |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + if (!$result['page']) { |
|
| 604 | + return $result; |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + // Decompresser au besoin |
|
| 608 | + if ($gz) { |
|
| 609 | + $result['page'] = implode('', gzfile($gz)); |
|
| 610 | + supprimer_fichier($gz); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + // Faut-il l'importer dans notre charset local ? |
|
| 614 | + if ($options['transcoder']) { |
|
| 615 | + include_spip('inc/charsets'); |
|
| 616 | + $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + return $result; |
|
| 620 | 620 | } |
| 621 | 621 | |
| 622 | 622 | /** |
@@ -632,73 +632,73 @@ discard block |
||
| 632 | 632 | * @return array|bool|mixed |
| 633 | 633 | */ |
| 634 | 634 | function recuperer_url_cache($url, $options = []) { |
| 635 | - if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 636 | - define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 637 | - } |
|
| 638 | - $default = [ |
|
| 639 | - 'transcoder' => false, |
|
| 640 | - 'methode' => 'GET', |
|
| 641 | - 'taille_max' => null, |
|
| 642 | - 'datas' => '', |
|
| 643 | - 'boundary' => '', |
|
| 644 | - 'refuser_gz' => false, |
|
| 645 | - 'if_modified_since' => '', |
|
| 646 | - 'uri_referer' => '', |
|
| 647 | - 'file' => '', |
|
| 648 | - 'follow_location' => 10, |
|
| 649 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 650 | - 'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE, |
|
| 651 | - ]; |
|
| 652 | - $options = array_merge($default, $options); |
|
| 653 | - |
|
| 654 | - // cas ou il n'est pas possible de cacher |
|
| 655 | - if (!empty($options['data']) or $options['methode'] == 'POST') { |
|
| 656 | - return recuperer_url($url, $options); |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 660 | - static $errors = []; |
|
| 661 | - if (isset($errors[$url])) { |
|
| 662 | - return $errors[$url]; |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - $sig = $options; |
|
| 666 | - unset($sig['if_modified_since']); |
|
| 667 | - unset($sig['delai_cache']); |
|
| 668 | - $sig['url'] = $url; |
|
| 669 | - |
|
| 670 | - $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 671 | - $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 672 | - $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 673 | - $cache = "$sub$cache"; |
|
| 674 | - |
|
| 675 | - $res = false; |
|
| 676 | - $is_cached = file_exists($cache); |
|
| 677 | - if ( |
|
| 678 | - $is_cached |
|
| 679 | - and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']) |
|
| 680 | - ) { |
|
| 681 | - lire_fichier($cache, $res); |
|
| 682 | - if ($res = unserialize($res)) { |
|
| 683 | - // mettre le last_modified et le status=304 ? |
|
| 684 | - } |
|
| 685 | - } |
|
| 686 | - if (!$res) { |
|
| 687 | - $res = recuperer_url($url, $options); |
|
| 688 | - // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 689 | - if (!$res) { |
|
| 690 | - if ($is_cached) { |
|
| 691 | - // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 692 | - lire_fichier($cache, $res); |
|
| 693 | - $res = unserialize($res); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - return $errors[$url] = $res; |
|
| 697 | - } |
|
| 698 | - ecrire_fichier($cache, serialize($res)); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - return $res; |
|
| 635 | + if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 636 | + define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 637 | + } |
|
| 638 | + $default = [ |
|
| 639 | + 'transcoder' => false, |
|
| 640 | + 'methode' => 'GET', |
|
| 641 | + 'taille_max' => null, |
|
| 642 | + 'datas' => '', |
|
| 643 | + 'boundary' => '', |
|
| 644 | + 'refuser_gz' => false, |
|
| 645 | + 'if_modified_since' => '', |
|
| 646 | + 'uri_referer' => '', |
|
| 647 | + 'file' => '', |
|
| 648 | + 'follow_location' => 10, |
|
| 649 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 650 | + 'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE, |
|
| 651 | + ]; |
|
| 652 | + $options = array_merge($default, $options); |
|
| 653 | + |
|
| 654 | + // cas ou il n'est pas possible de cacher |
|
| 655 | + if (!empty($options['data']) or $options['methode'] == 'POST') { |
|
| 656 | + return recuperer_url($url, $options); |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 660 | + static $errors = []; |
|
| 661 | + if (isset($errors[$url])) { |
|
| 662 | + return $errors[$url]; |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + $sig = $options; |
|
| 666 | + unset($sig['if_modified_since']); |
|
| 667 | + unset($sig['delai_cache']); |
|
| 668 | + $sig['url'] = $url; |
|
| 669 | + |
|
| 670 | + $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 671 | + $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 672 | + $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 673 | + $cache = "$sub$cache"; |
|
| 674 | + |
|
| 675 | + $res = false; |
|
| 676 | + $is_cached = file_exists($cache); |
|
| 677 | + if ( |
|
| 678 | + $is_cached |
|
| 679 | + and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']) |
|
| 680 | + ) { |
|
| 681 | + lire_fichier($cache, $res); |
|
| 682 | + if ($res = unserialize($res)) { |
|
| 683 | + // mettre le last_modified et le status=304 ? |
|
| 684 | + } |
|
| 685 | + } |
|
| 686 | + if (!$res) { |
|
| 687 | + $res = recuperer_url($url, $options); |
|
| 688 | + // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 689 | + if (!$res) { |
|
| 690 | + if ($is_cached) { |
|
| 691 | + // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 692 | + lire_fichier($cache, $res); |
|
| 693 | + $res = unserialize($res); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + return $errors[$url] = $res; |
|
| 697 | + } |
|
| 698 | + ecrire_fichier($cache, serialize($res)); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + return $res; |
|
| 702 | 702 | } |
| 703 | 703 | |
| 704 | 704 | /** |
@@ -716,41 +716,41 @@ discard block |
||
| 716 | 716 | * string contenu de la resource |
| 717 | 717 | */ |
| 718 | 718 | function recuperer_body($handle, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = '') { |
| 719 | - $taille = 0; |
|
| 720 | - $result = ''; |
|
| 721 | - $fp = false; |
|
| 722 | - if ($fichier) { |
|
| 723 | - include_spip('inc/acces'); |
|
| 724 | - $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 725 | - $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 726 | - if (!$fp and file_exists($fichier)) { |
|
| 727 | - return filesize($fichier); |
|
| 728 | - } |
|
| 729 | - if (!$fp) { |
|
| 730 | - return false; |
|
| 731 | - } |
|
| 732 | - $result = 0; // on renvoie la taille du fichier |
|
| 733 | - } |
|
| 734 | - while (!feof($handle) and $taille < $taille_max) { |
|
| 735 | - $res = fread($handle, 16384); |
|
| 736 | - $taille += strlen($res); |
|
| 737 | - if ($fp) { |
|
| 738 | - fwrite($fp, $res); |
|
| 739 | - $result = $taille; |
|
| 740 | - } else { |
|
| 741 | - $result .= $res; |
|
| 742 | - } |
|
| 743 | - } |
|
| 744 | - if ($fp) { |
|
| 745 | - spip_fclose_unlock($fp); |
|
| 746 | - spip_unlink($fichier); |
|
| 747 | - @rename($tmpfile, $fichier); |
|
| 748 | - if (!file_exists($fichier)) { |
|
| 749 | - return false; |
|
| 750 | - } |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - return $result; |
|
| 719 | + $taille = 0; |
|
| 720 | + $result = ''; |
|
| 721 | + $fp = false; |
|
| 722 | + if ($fichier) { |
|
| 723 | + include_spip('inc/acces'); |
|
| 724 | + $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 725 | + $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 726 | + if (!$fp and file_exists($fichier)) { |
|
| 727 | + return filesize($fichier); |
|
| 728 | + } |
|
| 729 | + if (!$fp) { |
|
| 730 | + return false; |
|
| 731 | + } |
|
| 732 | + $result = 0; // on renvoie la taille du fichier |
|
| 733 | + } |
|
| 734 | + while (!feof($handle) and $taille < $taille_max) { |
|
| 735 | + $res = fread($handle, 16384); |
|
| 736 | + $taille += strlen($res); |
|
| 737 | + if ($fp) { |
|
| 738 | + fwrite($fp, $res); |
|
| 739 | + $result = $taille; |
|
| 740 | + } else { |
|
| 741 | + $result .= $res; |
|
| 742 | + } |
|
| 743 | + } |
|
| 744 | + if ($fp) { |
|
| 745 | + spip_fclose_unlock($fp); |
|
| 746 | + spip_unlink($fichier); |
|
| 747 | + @rename($tmpfile, $fichier); |
|
| 748 | + if (!file_exists($fichier)) { |
|
| 749 | + return false; |
|
| 750 | + } |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + return $result; |
|
| 754 | 754 | } |
| 755 | 755 | |
| 756 | 756 | /** |
@@ -772,35 +772,35 @@ discard block |
||
| 772 | 772 | * string location |
| 773 | 773 | */ |
| 774 | 774 | function recuperer_entetes_complets($handle, $if_modified_since = false) { |
| 775 | - $result = ['status' => 0, 'headers' => [], 'last_modified' => 0, 'location' => '']; |
|
| 776 | - |
|
| 777 | - $s = @trim(fgets($handle, 16384)); |
|
| 778 | - if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) { |
|
| 779 | - return false; |
|
| 780 | - } |
|
| 781 | - $result['status'] = intval($r[1]); |
|
| 782 | - while ($s = trim(fgets($handle, 16384))) { |
|
| 783 | - $result['headers'][] = $s . "\n"; |
|
| 784 | - preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 785 | - list(, $d, $v) = $r; |
|
| 786 | - if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) { |
|
| 787 | - $result['location'] = $v; |
|
| 788 | - } elseif ($d == 'Last-Modified') { |
|
| 789 | - $result['last_modified'] = strtotime($v); |
|
| 790 | - } |
|
| 791 | - } |
|
| 792 | - if ( |
|
| 793 | - $if_modified_since |
|
| 794 | - and $result['last_modified'] |
|
| 795 | - and $if_modified_since > $result['last_modified'] |
|
| 796 | - and $result['status'] == 200 |
|
| 797 | - ) { |
|
| 798 | - $result['status'] = 304; |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - $result['headers'] = implode('', $result['headers']); |
|
| 802 | - |
|
| 803 | - return $result; |
|
| 775 | + $result = ['status' => 0, 'headers' => [], 'last_modified' => 0, 'location' => '']; |
|
| 776 | + |
|
| 777 | + $s = @trim(fgets($handle, 16384)); |
|
| 778 | + if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) { |
|
| 779 | + return false; |
|
| 780 | + } |
|
| 781 | + $result['status'] = intval($r[1]); |
|
| 782 | + while ($s = trim(fgets($handle, 16384))) { |
|
| 783 | + $result['headers'][] = $s . "\n"; |
|
| 784 | + preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 785 | + list(, $d, $v) = $r; |
|
| 786 | + if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) { |
|
| 787 | + $result['location'] = $v; |
|
| 788 | + } elseif ($d == 'Last-Modified') { |
|
| 789 | + $result['last_modified'] = strtotime($v); |
|
| 790 | + } |
|
| 791 | + } |
|
| 792 | + if ( |
|
| 793 | + $if_modified_since |
|
| 794 | + and $result['last_modified'] |
|
| 795 | + and $if_modified_since > $result['last_modified'] |
|
| 796 | + and $result['status'] == 200 |
|
| 797 | + ) { |
|
| 798 | + $result['status'] = 304; |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + $result['headers'] = implode('', $result['headers']); |
|
| 802 | + |
|
| 803 | + return $result; |
|
| 804 | 804 | } |
| 805 | 805 | |
| 806 | 806 | /** |
@@ -822,22 +822,22 @@ discard block |
||
| 822 | 822 | * Nom du fichier pour copie locale |
| 823 | 823 | **/ |
| 824 | 824 | function nom_fichier_copie_locale($source, $extension) { |
| 825 | - include_spip('inc/documents'); |
|
| 825 | + include_spip('inc/documents'); |
|
| 826 | 826 | |
| 827 | - $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 828 | - $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 827 | + $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 828 | + $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 829 | 829 | |
| 830 | - // on se place tout le temps comme si on etait a la racine |
|
| 831 | - if (_DIR_RACINE) { |
|
| 832 | - $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); |
|
| 833 | - } |
|
| 830 | + // on se place tout le temps comme si on etait a la racine |
|
| 831 | + if (_DIR_RACINE) { |
|
| 832 | + $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); |
|
| 833 | + } |
|
| 834 | 834 | |
| 835 | - $m = md5($source); |
|
| 835 | + $m = md5($source); |
|
| 836 | 836 | |
| 837 | - return $d |
|
| 838 | - . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 839 | - . substr($m, 0, 4) |
|
| 840 | - . ".$extension"; |
|
| 837 | + return $d |
|
| 838 | + . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 839 | + . substr($m, 0, 4) |
|
| 840 | + . ".$extension"; |
|
| 841 | 841 | } |
| 842 | 842 | |
| 843 | 843 | /** |
@@ -855,70 +855,70 @@ discard block |
||
| 855 | 855 | * Nom du fichier calculé |
| 856 | 856 | **/ |
| 857 | 857 | function fichier_copie_locale($source) { |
| 858 | - // Si c'est deja local pas de souci |
|
| 859 | - if (!tester_url_absolue($source)) { |
|
| 860 | - if (_DIR_RACINE) { |
|
| 861 | - $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - return $source; |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 868 | - // a deja ete copie en local avec cette extension |
|
| 869 | - // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 870 | - $path_parts = pathinfo($source); |
|
| 871 | - if (!isset($path_parts['extension'])) { |
|
| 872 | - $path_parts['extension'] = ''; |
|
| 873 | - } |
|
| 874 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 875 | - if ( |
|
| 876 | - $ext |
|
| 877 | - and preg_match(',^\w+$,', $ext) // pas de php?truc=1&... |
|
| 878 | - and $f = nom_fichier_copie_locale($source, $ext) |
|
| 879 | - and file_exists(_DIR_RACINE . $f) |
|
| 880 | - ) { |
|
| 881 | - return $f; |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - |
|
| 885 | - // Si c'est deja dans la table des documents, |
|
| 886 | - // ramener le nom de sa copie potentielle |
|
| 887 | - $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 888 | - |
|
| 889 | - if ($ext) { |
|
| 890 | - return nom_fichier_copie_locale($source, $ext); |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 894 | - // et si il n'aurait pas deja ete rapatrie |
|
| 895 | - |
|
| 896 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 897 | - |
|
| 898 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 899 | - $f = nom_fichier_copie_locale($source, $ext); |
|
| 900 | - if (file_exists(_DIR_RACINE . $f)) { |
|
| 901 | - return $f; |
|
| 902 | - } |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - // Ping pour voir si son extension est connue et autorisee |
|
| 906 | - // avec mise en cache du resultat du ping |
|
| 907 | - |
|
| 908 | - $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 909 | - if ( |
|
| 910 | - !@file_exists($cache) |
|
| 911 | - or !$path_parts = @unserialize(spip_file_get_contents($cache)) |
|
| 912 | - or _request('var_mode') === 'recalcul' |
|
| 913 | - ) { |
|
| 914 | - $path_parts = recuperer_infos_distantes($source, 0, false); |
|
| 915 | - ecrire_fichier($cache, serialize($path_parts)); |
|
| 916 | - } |
|
| 917 | - $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : ''; |
|
| 918 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 919 | - return nom_fichier_copie_locale($source, $ext); |
|
| 920 | - } |
|
| 921 | - spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR); |
|
| 858 | + // Si c'est deja local pas de souci |
|
| 859 | + if (!tester_url_absolue($source)) { |
|
| 860 | + if (_DIR_RACINE) { |
|
| 861 | + $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + return $source; |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 868 | + // a deja ete copie en local avec cette extension |
|
| 869 | + // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 870 | + $path_parts = pathinfo($source); |
|
| 871 | + if (!isset($path_parts['extension'])) { |
|
| 872 | + $path_parts['extension'] = ''; |
|
| 873 | + } |
|
| 874 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 875 | + if ( |
|
| 876 | + $ext |
|
| 877 | + and preg_match(',^\w+$,', $ext) // pas de php?truc=1&... |
|
| 878 | + and $f = nom_fichier_copie_locale($source, $ext) |
|
| 879 | + and file_exists(_DIR_RACINE . $f) |
|
| 880 | + ) { |
|
| 881 | + return $f; |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + |
|
| 885 | + // Si c'est deja dans la table des documents, |
|
| 886 | + // ramener le nom de sa copie potentielle |
|
| 887 | + $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 888 | + |
|
| 889 | + if ($ext) { |
|
| 890 | + return nom_fichier_copie_locale($source, $ext); |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 894 | + // et si il n'aurait pas deja ete rapatrie |
|
| 895 | + |
|
| 896 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 897 | + |
|
| 898 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 899 | + $f = nom_fichier_copie_locale($source, $ext); |
|
| 900 | + if (file_exists(_DIR_RACINE . $f)) { |
|
| 901 | + return $f; |
|
| 902 | + } |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + // Ping pour voir si son extension est connue et autorisee |
|
| 906 | + // avec mise en cache du resultat du ping |
|
| 907 | + |
|
| 908 | + $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 909 | + if ( |
|
| 910 | + !@file_exists($cache) |
|
| 911 | + or !$path_parts = @unserialize(spip_file_get_contents($cache)) |
|
| 912 | + or _request('var_mode') === 'recalcul' |
|
| 913 | + ) { |
|
| 914 | + $path_parts = recuperer_infos_distantes($source, 0, false); |
|
| 915 | + ecrire_fichier($cache, serialize($path_parts)); |
|
| 916 | + } |
|
| 917 | + $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : ''; |
|
| 918 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 919 | + return nom_fichier_copie_locale($source, $ext); |
|
| 920 | + } |
|
| 921 | + spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR); |
|
| 922 | 922 | } |
| 923 | 923 | |
| 924 | 924 | |
@@ -946,97 +946,97 @@ discard block |
||
| 946 | 946 | **/ |
| 947 | 947 | function recuperer_infos_distantes($source, $max = 0, $charger_si_petite_image = true) { |
| 948 | 948 | |
| 949 | - // pas la peine de perdre son temps |
|
| 950 | - if (!tester_url_absolue($source)) { |
|
| 951 | - return false; |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - # charger les alias des types mime |
|
| 955 | - include_spip('base/typedoc'); |
|
| 956 | - |
|
| 957 | - $a = []; |
|
| 958 | - $mime_type = ''; |
|
| 959 | - // On va directement charger le debut des images et des fichiers html, |
|
| 960 | - // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 961 | - // ca echoue l'utilisateur devra les entrer... |
|
| 962 | - $reponse = recuperer_url($source, ['taille_max' => $max, 'refuser_gz' => true]); |
|
| 963 | - $headers = $reponse['headers'] ?? ''; |
|
| 964 | - $a['body'] = $reponse['page'] ?? ''; |
|
| 965 | - if ($headers) { |
|
| 966 | - if (!$extension = distant_trouver_extension_selon_headers($source, $headers)) { |
|
| 967 | - return false; |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - $a['extension'] = $extension; |
|
| 971 | - |
|
| 972 | - if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 973 | - $a['taille'] = intval($regs[1]); |
|
| 974 | - } |
|
| 975 | - } |
|
| 976 | - |
|
| 977 | - // Echec avec HEAD, on tente avec GET |
|
| 978 | - if (!$a and !$max) { |
|
| 979 | - spip_log("tenter GET $source", 'distant'); |
|
| 980 | - $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 981 | - } |
|
| 982 | - |
|
| 983 | - // si on a rien trouve pas la peine d'insister |
|
| 984 | - if (!$a) { |
|
| 985 | - return false; |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 989 | - // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 990 | - include_spip('inc/filtres_images_lib_mini'); |
|
| 991 | - if ( |
|
| 992 | - strpos($mime_type, 'image/') === 0 |
|
| 993 | - and $extension = _image_trouver_extension_depuis_mime($mime_type) |
|
| 994 | - ) { |
|
| 995 | - if ( |
|
| 996 | - $max == 0 |
|
| 997 | - and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 998 | - and in_array($extension, formats_image_acceptables()) |
|
| 999 | - and $charger_si_petite_image |
|
| 1000 | - ) { |
|
| 1001 | - $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1002 | - } else { |
|
| 1003 | - if ($a['body']) { |
|
| 1004 | - $a['extension'] = $extension; |
|
| 1005 | - $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension); |
|
| 1006 | - ecrire_fichier($a['fichier'], $a['body']); |
|
| 1007 | - $size_image = @spip_getimagesize($a['fichier']); |
|
| 1008 | - $a['largeur'] = intval($size_image[0]); |
|
| 1009 | - $a['hauteur'] = intval($size_image[1]); |
|
| 1010 | - $a['type_image'] = true; |
|
| 1011 | - } |
|
| 1012 | - } |
|
| 1013 | - } |
|
| 1014 | - |
|
| 1015 | - // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1016 | - // ce sera mieux que 0x0 |
|
| 1017 | - // Flash is dead! |
|
| 1018 | - if ( |
|
| 1019 | - $a and isset($a['extension']) and $a['extension'] == 'swf' |
|
| 1020 | - and empty($a['largeur']) |
|
| 1021 | - ) { |
|
| 1022 | - $a['largeur'] = 425; |
|
| 1023 | - $a['hauteur'] = 350; |
|
| 1024 | - } |
|
| 1025 | - |
|
| 1026 | - if ($mime_type == 'text/html') { |
|
| 1027 | - include_spip('inc/filtres'); |
|
| 1028 | - $page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]); |
|
| 1029 | - $page = $page['page'] ?? ''; |
|
| 1030 | - if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) { |
|
| 1031 | - $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1032 | - } |
|
| 1033 | - if (!isset($a['taille']) or !$a['taille']) { |
|
| 1034 | - $a['taille'] = strlen($page); # a peu pres |
|
| 1035 | - } |
|
| 1036 | - } |
|
| 1037 | - $a['mime_type'] = $mime_type; |
|
| 1038 | - |
|
| 1039 | - return $a; |
|
| 949 | + // pas la peine de perdre son temps |
|
| 950 | + if (!tester_url_absolue($source)) { |
|
| 951 | + return false; |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + # charger les alias des types mime |
|
| 955 | + include_spip('base/typedoc'); |
|
| 956 | + |
|
| 957 | + $a = []; |
|
| 958 | + $mime_type = ''; |
|
| 959 | + // On va directement charger le debut des images et des fichiers html, |
|
| 960 | + // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 961 | + // ca echoue l'utilisateur devra les entrer... |
|
| 962 | + $reponse = recuperer_url($source, ['taille_max' => $max, 'refuser_gz' => true]); |
|
| 963 | + $headers = $reponse['headers'] ?? ''; |
|
| 964 | + $a['body'] = $reponse['page'] ?? ''; |
|
| 965 | + if ($headers) { |
|
| 966 | + if (!$extension = distant_trouver_extension_selon_headers($source, $headers)) { |
|
| 967 | + return false; |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + $a['extension'] = $extension; |
|
| 971 | + |
|
| 972 | + if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 973 | + $a['taille'] = intval($regs[1]); |
|
| 974 | + } |
|
| 975 | + } |
|
| 976 | + |
|
| 977 | + // Echec avec HEAD, on tente avec GET |
|
| 978 | + if (!$a and !$max) { |
|
| 979 | + spip_log("tenter GET $source", 'distant'); |
|
| 980 | + $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 981 | + } |
|
| 982 | + |
|
| 983 | + // si on a rien trouve pas la peine d'insister |
|
| 984 | + if (!$a) { |
|
| 985 | + return false; |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 989 | + // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 990 | + include_spip('inc/filtres_images_lib_mini'); |
|
| 991 | + if ( |
|
| 992 | + strpos($mime_type, 'image/') === 0 |
|
| 993 | + and $extension = _image_trouver_extension_depuis_mime($mime_type) |
|
| 994 | + ) { |
|
| 995 | + if ( |
|
| 996 | + $max == 0 |
|
| 997 | + and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 998 | + and in_array($extension, formats_image_acceptables()) |
|
| 999 | + and $charger_si_petite_image |
|
| 1000 | + ) { |
|
| 1001 | + $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1002 | + } else { |
|
| 1003 | + if ($a['body']) { |
|
| 1004 | + $a['extension'] = $extension; |
|
| 1005 | + $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension); |
|
| 1006 | + ecrire_fichier($a['fichier'], $a['body']); |
|
| 1007 | + $size_image = @spip_getimagesize($a['fichier']); |
|
| 1008 | + $a['largeur'] = intval($size_image[0]); |
|
| 1009 | + $a['hauteur'] = intval($size_image[1]); |
|
| 1010 | + $a['type_image'] = true; |
|
| 1011 | + } |
|
| 1012 | + } |
|
| 1013 | + } |
|
| 1014 | + |
|
| 1015 | + // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1016 | + // ce sera mieux que 0x0 |
|
| 1017 | + // Flash is dead! |
|
| 1018 | + if ( |
|
| 1019 | + $a and isset($a['extension']) and $a['extension'] == 'swf' |
|
| 1020 | + and empty($a['largeur']) |
|
| 1021 | + ) { |
|
| 1022 | + $a['largeur'] = 425; |
|
| 1023 | + $a['hauteur'] = 350; |
|
| 1024 | + } |
|
| 1025 | + |
|
| 1026 | + if ($mime_type == 'text/html') { |
|
| 1027 | + include_spip('inc/filtres'); |
|
| 1028 | + $page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]); |
|
| 1029 | + $page = $page['page'] ?? ''; |
|
| 1030 | + if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) { |
|
| 1031 | + $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1032 | + } |
|
| 1033 | + if (!isset($a['taille']) or !$a['taille']) { |
|
| 1034 | + $a['taille'] = strlen($page); # a peu pres |
|
| 1035 | + } |
|
| 1036 | + } |
|
| 1037 | + $a['mime_type'] = $mime_type; |
|
| 1038 | + |
|
| 1039 | + return $a; |
|
| 1040 | 1040 | } |
| 1041 | 1041 | |
| 1042 | 1042 | /** |
@@ -1045,70 +1045,70 @@ discard block |
||
| 1045 | 1045 | * @return false|mixed |
| 1046 | 1046 | */ |
| 1047 | 1047 | function distant_trouver_extension_selon_headers($source, $headers) { |
| 1048 | - if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) { |
|
| 1049 | - $mime_type = (trim($regs[1])); |
|
| 1050 | - } else { |
|
| 1051 | - $mime_type = ''; |
|
| 1052 | - } // inconnu |
|
| 1053 | - |
|
| 1054 | - // Appliquer les alias |
|
| 1055 | - while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1056 | - $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1057 | - } |
|
| 1058 | - |
|
| 1059 | - // pour corriger_extension() |
|
| 1060 | - include_spip('inc/documents'); |
|
| 1061 | - |
|
| 1062 | - // Si on a un mime-type insignifiant |
|
| 1063 | - // text/plain,application/octet-stream ou vide |
|
| 1064 | - // c'est peut-etre que le serveur ne sait pas |
|
| 1065 | - // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1066 | - // ou le Content-Disposition: attachment; filename=... |
|
| 1067 | - $t = null; |
|
| 1068 | - if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) { |
|
| 1069 | - if ( |
|
| 1070 | - !$t |
|
| 1071 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1072 | - ) { |
|
| 1073 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1074 | - } |
|
| 1075 | - if ( |
|
| 1076 | - !$t |
|
| 1077 | - and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1078 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1079 | - ) { |
|
| 1080 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1081 | - } |
|
| 1082 | - } |
|
| 1083 | - |
|
| 1084 | - // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1085 | - if (!$t) { |
|
| 1086 | - $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1087 | - } |
|
| 1088 | - |
|
| 1089 | - // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1090 | - // On essaie de nouveau avec l'extension |
|
| 1091 | - if ( |
|
| 1092 | - !$t |
|
| 1093 | - and $mime_type != 'text/plain' |
|
| 1094 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1095 | - ) { |
|
| 1096 | - # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1097 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1098 | - } |
|
| 1099 | - |
|
| 1100 | - if ($t) { |
|
| 1101 | - spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant'); |
|
| 1102 | - return $t['extension']; |
|
| 1103 | - } else { |
|
| 1104 | - # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1105 | - spip_log("mime-type $mime_type inconnu", 'distant'); |
|
| 1106 | - $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1107 | - if (!$t) { |
|
| 1108 | - return false; |
|
| 1109 | - } |
|
| 1110 | - return $t['extension']; |
|
| 1111 | - } |
|
| 1048 | + if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) { |
|
| 1049 | + $mime_type = (trim($regs[1])); |
|
| 1050 | + } else { |
|
| 1051 | + $mime_type = ''; |
|
| 1052 | + } // inconnu |
|
| 1053 | + |
|
| 1054 | + // Appliquer les alias |
|
| 1055 | + while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1056 | + $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1057 | + } |
|
| 1058 | + |
|
| 1059 | + // pour corriger_extension() |
|
| 1060 | + include_spip('inc/documents'); |
|
| 1061 | + |
|
| 1062 | + // Si on a un mime-type insignifiant |
|
| 1063 | + // text/plain,application/octet-stream ou vide |
|
| 1064 | + // c'est peut-etre que le serveur ne sait pas |
|
| 1065 | + // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1066 | + // ou le Content-Disposition: attachment; filename=... |
|
| 1067 | + $t = null; |
|
| 1068 | + if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) { |
|
| 1069 | + if ( |
|
| 1070 | + !$t |
|
| 1071 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1072 | + ) { |
|
| 1073 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1074 | + } |
|
| 1075 | + if ( |
|
| 1076 | + !$t |
|
| 1077 | + and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1078 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1079 | + ) { |
|
| 1080 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1081 | + } |
|
| 1082 | + } |
|
| 1083 | + |
|
| 1084 | + // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1085 | + if (!$t) { |
|
| 1086 | + $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1087 | + } |
|
| 1088 | + |
|
| 1089 | + // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1090 | + // On essaie de nouveau avec l'extension |
|
| 1091 | + if ( |
|
| 1092 | + !$t |
|
| 1093 | + and $mime_type != 'text/plain' |
|
| 1094 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1095 | + ) { |
|
| 1096 | + # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1097 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1098 | + } |
|
| 1099 | + |
|
| 1100 | + if ($t) { |
|
| 1101 | + spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant'); |
|
| 1102 | + return $t['extension']; |
|
| 1103 | + } else { |
|
| 1104 | + # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1105 | + spip_log("mime-type $mime_type inconnu", 'distant'); |
|
| 1106 | + $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1107 | + if (!$t) { |
|
| 1108 | + return false; |
|
| 1109 | + } |
|
| 1110 | + return $t['extension']; |
|
| 1111 | + } |
|
| 1112 | 1112 | } |
| 1113 | 1113 | |
| 1114 | 1114 | /** |
@@ -1123,46 +1123,46 @@ discard block |
||
| 1123 | 1123 | * @return string |
| 1124 | 1124 | */ |
| 1125 | 1125 | function need_proxy($host, $http_proxy = null, $http_noproxy = null) { |
| 1126 | - if (is_null($http_proxy)) { |
|
| 1127 | - $http_proxy = isset($GLOBALS['meta']['http_proxy']) ? $GLOBALS['meta']['http_proxy'] : null; |
|
| 1128 | - } |
|
| 1129 | - // rien a faire si pas de proxy :) |
|
| 1130 | - if (is_null($http_proxy) or !$http_proxy = trim($http_proxy)) { |
|
| 1131 | - return ''; |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - if (is_null($http_noproxy)) { |
|
| 1135 | - $http_noproxy = isset($GLOBALS['meta']['http_noproxy']) ? $GLOBALS['meta']['http_noproxy'] : null; |
|
| 1136 | - } |
|
| 1137 | - // si pas d'exception, on retourne le proxy |
|
| 1138 | - if (is_null($http_noproxy) or !$http_noproxy = trim($http_noproxy)) { |
|
| 1139 | - return $http_proxy; |
|
| 1140 | - } |
|
| 1141 | - |
|
| 1142 | - // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception |
|
| 1143 | - // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne |
|
| 1144 | - $http_noproxy = str_replace("\n", ' ', $http_noproxy); |
|
| 1145 | - $http_noproxy = str_replace("\r", ' ', $http_noproxy); |
|
| 1146 | - $http_noproxy = " $http_noproxy "; |
|
| 1147 | - $domain = $host; |
|
| 1148 | - // si le domaine exact www.example.org est dans les exceptions |
|
| 1149 | - if (strpos($http_noproxy, " $domain ") !== false) { |
|
| 1150 | - return ''; |
|
| 1151 | - } |
|
| 1152 | - |
|
| 1153 | - while (strpos($domain, '.') !== false) { |
|
| 1154 | - $domain = explode('.', $domain); |
|
| 1155 | - array_shift($domain); |
|
| 1156 | - $domain = implode('.', $domain); |
|
| 1157 | - |
|
| 1158 | - // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines) |
|
| 1159 | - if (strpos($http_noproxy, " .$domain ") !== false) { |
|
| 1160 | - return ''; |
|
| 1161 | - } |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - // ok c'est pas une exception |
|
| 1165 | - return $http_proxy; |
|
| 1126 | + if (is_null($http_proxy)) { |
|
| 1127 | + $http_proxy = isset($GLOBALS['meta']['http_proxy']) ? $GLOBALS['meta']['http_proxy'] : null; |
|
| 1128 | + } |
|
| 1129 | + // rien a faire si pas de proxy :) |
|
| 1130 | + if (is_null($http_proxy) or !$http_proxy = trim($http_proxy)) { |
|
| 1131 | + return ''; |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + if (is_null($http_noproxy)) { |
|
| 1135 | + $http_noproxy = isset($GLOBALS['meta']['http_noproxy']) ? $GLOBALS['meta']['http_noproxy'] : null; |
|
| 1136 | + } |
|
| 1137 | + // si pas d'exception, on retourne le proxy |
|
| 1138 | + if (is_null($http_noproxy) or !$http_noproxy = trim($http_noproxy)) { |
|
| 1139 | + return $http_proxy; |
|
| 1140 | + } |
|
| 1141 | + |
|
| 1142 | + // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception |
|
| 1143 | + // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne |
|
| 1144 | + $http_noproxy = str_replace("\n", ' ', $http_noproxy); |
|
| 1145 | + $http_noproxy = str_replace("\r", ' ', $http_noproxy); |
|
| 1146 | + $http_noproxy = " $http_noproxy "; |
|
| 1147 | + $domain = $host; |
|
| 1148 | + // si le domaine exact www.example.org est dans les exceptions |
|
| 1149 | + if (strpos($http_noproxy, " $domain ") !== false) { |
|
| 1150 | + return ''; |
|
| 1151 | + } |
|
| 1152 | + |
|
| 1153 | + while (strpos($domain, '.') !== false) { |
|
| 1154 | + $domain = explode('.', $domain); |
|
| 1155 | + array_shift($domain); |
|
| 1156 | + $domain = implode('.', $domain); |
|
| 1157 | + |
|
| 1158 | + // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines) |
|
| 1159 | + if (strpos($http_noproxy, " .$domain ") !== false) { |
|
| 1160 | + return ''; |
|
| 1161 | + } |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + // ok c'est pas une exception |
|
| 1165 | + return $http_proxy; |
|
| 1166 | 1166 | } |
| 1167 | 1167 | |
| 1168 | 1168 | |
@@ -1185,59 +1185,59 @@ discard block |
||
| 1185 | 1185 | * @return array |
| 1186 | 1186 | */ |
| 1187 | 1187 | function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = '', $vers = 'HTTP/1.0', $date = '') { |
| 1188 | - $user = $via_proxy = $proxy_user = ''; |
|
| 1189 | - $fopen = false; |
|
| 1190 | - |
|
| 1191 | - $t = @parse_url($url); |
|
| 1192 | - $host = $t['host']; |
|
| 1193 | - if ($t['scheme'] == 'http') { |
|
| 1194 | - $scheme = 'http'; |
|
| 1195 | - $noproxy = ''; |
|
| 1196 | - } elseif ($t['scheme'] == 'https') { |
|
| 1197 | - $scheme = 'ssl'; |
|
| 1198 | - $noproxy = 'ssl://'; |
|
| 1199 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1200 | - $t['port'] = 443; |
|
| 1201 | - } |
|
| 1202 | - } else { |
|
| 1203 | - $scheme = $t['scheme']; |
|
| 1204 | - $noproxy = $scheme . '://'; |
|
| 1205 | - } |
|
| 1206 | - if (isset($t['user'])) { |
|
| 1207 | - $user = [$t['user'], $t['pass']]; |
|
| 1208 | - } |
|
| 1209 | - |
|
| 1210 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1211 | - $port = 80; |
|
| 1212 | - } |
|
| 1213 | - if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1214 | - $path = '/'; |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - if (!empty($t['query'])) { |
|
| 1218 | - $path .= '?' . $t['query']; |
|
| 1219 | - } |
|
| 1220 | - |
|
| 1221 | - $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1222 | - if (!$f or !is_resource($f)) { |
|
| 1223 | - // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1224 | - // ce qui correspond a $f===110 |
|
| 1225 | - if ( |
|
| 1226 | - $f !== 110 |
|
| 1227 | - and !need_proxy($host) |
|
| 1228 | - and !_request('tester_proxy') |
|
| 1229 | - and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen']) |
|
| 1230 | - ) { |
|
| 1231 | - $f = @fopen($url, 'rb'); |
|
| 1232 | - spip_log("connexion vers $url par simple fopen", 'distant'); |
|
| 1233 | - $fopen = true; |
|
| 1234 | - } else { |
|
| 1235 | - // echec total |
|
| 1236 | - $f = false; |
|
| 1237 | - } |
|
| 1238 | - } |
|
| 1239 | - |
|
| 1240 | - return [$f, $fopen]; |
|
| 1188 | + $user = $via_proxy = $proxy_user = ''; |
|
| 1189 | + $fopen = false; |
|
| 1190 | + |
|
| 1191 | + $t = @parse_url($url); |
|
| 1192 | + $host = $t['host']; |
|
| 1193 | + if ($t['scheme'] == 'http') { |
|
| 1194 | + $scheme = 'http'; |
|
| 1195 | + $noproxy = ''; |
|
| 1196 | + } elseif ($t['scheme'] == 'https') { |
|
| 1197 | + $scheme = 'ssl'; |
|
| 1198 | + $noproxy = 'ssl://'; |
|
| 1199 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1200 | + $t['port'] = 443; |
|
| 1201 | + } |
|
| 1202 | + } else { |
|
| 1203 | + $scheme = $t['scheme']; |
|
| 1204 | + $noproxy = $scheme . '://'; |
|
| 1205 | + } |
|
| 1206 | + if (isset($t['user'])) { |
|
| 1207 | + $user = [$t['user'], $t['pass']]; |
|
| 1208 | + } |
|
| 1209 | + |
|
| 1210 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1211 | + $port = 80; |
|
| 1212 | + } |
|
| 1213 | + if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1214 | + $path = '/'; |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + if (!empty($t['query'])) { |
|
| 1218 | + $path .= '?' . $t['query']; |
|
| 1219 | + } |
|
| 1220 | + |
|
| 1221 | + $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1222 | + if (!$f or !is_resource($f)) { |
|
| 1223 | + // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1224 | + // ce qui correspond a $f===110 |
|
| 1225 | + if ( |
|
| 1226 | + $f !== 110 |
|
| 1227 | + and !need_proxy($host) |
|
| 1228 | + and !_request('tester_proxy') |
|
| 1229 | + and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen']) |
|
| 1230 | + ) { |
|
| 1231 | + $f = @fopen($url, 'rb'); |
|
| 1232 | + spip_log("connexion vers $url par simple fopen", 'distant'); |
|
| 1233 | + $fopen = true; |
|
| 1234 | + } else { |
|
| 1235 | + // echec total |
|
| 1236 | + $f = false; |
|
| 1237 | + } |
|
| 1238 | + } |
|
| 1239 | + |
|
| 1240 | + return [$f, $fopen]; |
|
| 1241 | 1241 | } |
| 1242 | 1242 | |
| 1243 | 1243 | /** |
@@ -1272,125 +1272,125 @@ discard block |
||
| 1272 | 1272 | * resource socket vers l'url demandee |
| 1273 | 1273 | */ |
| 1274 | 1274 | function lance_requete( |
| 1275 | - $method, |
|
| 1276 | - $scheme, |
|
| 1277 | - $user, |
|
| 1278 | - $host, |
|
| 1279 | - $path, |
|
| 1280 | - $port, |
|
| 1281 | - $noproxy, |
|
| 1282 | - $refuse_gz = false, |
|
| 1283 | - $referer = '', |
|
| 1284 | - $datas = '', |
|
| 1285 | - $vers = 'HTTP/1.0', |
|
| 1286 | - $date = '' |
|
| 1275 | + $method, |
|
| 1276 | + $scheme, |
|
| 1277 | + $user, |
|
| 1278 | + $host, |
|
| 1279 | + $path, |
|
| 1280 | + $port, |
|
| 1281 | + $noproxy, |
|
| 1282 | + $refuse_gz = false, |
|
| 1283 | + $referer = '', |
|
| 1284 | + $datas = '', |
|
| 1285 | + $vers = 'HTTP/1.0', |
|
| 1286 | + $date = '' |
|
| 1287 | 1287 | ) { |
| 1288 | 1288 | |
| 1289 | - $proxy_user = ''; |
|
| 1290 | - $http_proxy = need_proxy($host); |
|
| 1291 | - if ($user) { |
|
| 1292 | - $user = urlencode($user[0]) . ':' . urlencode($user[1]); |
|
| 1293 | - } |
|
| 1294 | - |
|
| 1295 | - $connect = ''; |
|
| 1296 | - if ($http_proxy) { |
|
| 1297 | - if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme, ['tls','ssl'])) { |
|
| 1298 | - $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : ''); |
|
| 1299 | - $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1300 | - . "Host: $path_host\r\n" |
|
| 1301 | - . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1302 | - } else { |
|
| 1303 | - $path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://") |
|
| 1304 | - . (!$user ? '' : "$user@") |
|
| 1305 | - . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1306 | - } |
|
| 1307 | - $t2 = @parse_url($http_proxy); |
|
| 1308 | - $first_host = $t2['host']; |
|
| 1309 | - if (!($port = $t2['port'])) { |
|
| 1310 | - $port = 80; |
|
| 1311 | - } |
|
| 1312 | - if ($t2['user']) { |
|
| 1313 | - $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1314 | - } |
|
| 1315 | - } else { |
|
| 1316 | - $first_host = $noproxy . $host; |
|
| 1317 | - } |
|
| 1318 | - |
|
| 1319 | - if ($connect) { |
|
| 1320 | - $streamContext = stream_context_create([ |
|
| 1321 | - 'ssl' => [ |
|
| 1322 | - 'verify_peer' => false, |
|
| 1323 | - 'allow_self_signed' => true, |
|
| 1324 | - 'SNI_enabled' => true, |
|
| 1325 | - 'peer_name' => $host, |
|
| 1326 | - ] |
|
| 1327 | - ]); |
|
| 1328 | - $f = @stream_socket_client( |
|
| 1329 | - "tcp://$first_host:$port", |
|
| 1330 | - $errno, |
|
| 1331 | - $errstr, |
|
| 1332 | - _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1333 | - STREAM_CLIENT_CONNECT, |
|
| 1334 | - $streamContext |
|
| 1335 | - ); |
|
| 1336 | - spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect'); |
|
| 1337 | - if (!$f) { |
|
| 1338 | - spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1339 | - return $errno; |
|
| 1340 | - } |
|
| 1341 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1342 | - |
|
| 1343 | - fputs($f, $connect); |
|
| 1344 | - fputs($f, "\r\n"); |
|
| 1345 | - $res = fread($f, 1024); |
|
| 1346 | - if ( |
|
| 1347 | - !$res |
|
| 1348 | - or !count($res = explode(' ', $res)) |
|
| 1349 | - or $res[1] !== '200' |
|
| 1350 | - ) { |
|
| 1351 | - spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE); |
|
| 1352 | - fclose($f); |
|
| 1353 | - |
|
| 1354 | - return false; |
|
| 1355 | - } |
|
| 1356 | - // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1357 | - stream_set_blocking($f, true); |
|
| 1358 | - // envoyer le handshake |
|
| 1359 | - stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1360 | - spip_log("OK CONNECT sur $first_host:$port", 'connect'); |
|
| 1361 | - } else { |
|
| 1362 | - $ntry = 3; |
|
| 1363 | - do { |
|
| 1364 | - $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1365 | - } while (!$f and $ntry-- and $errno !== 110 and sleep(1)); |
|
| 1366 | - spip_log("Recuperer $path sur $first_host:$port par $f"); |
|
| 1367 | - if (!$f) { |
|
| 1368 | - spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1369 | - |
|
| 1370 | - return $errno; |
|
| 1371 | - } |
|
| 1372 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1373 | - } |
|
| 1374 | - |
|
| 1375 | - $site = isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : ''; |
|
| 1376 | - |
|
| 1377 | - $host_port = $host; |
|
| 1378 | - if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) { |
|
| 1379 | - $host_port .= ":$port"; |
|
| 1380 | - } |
|
| 1381 | - $req = "$method $path $vers\r\n" |
|
| 1382 | - . "Host: $host_port\r\n" |
|
| 1383 | - . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1384 | - . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1385 | - . (!$site ? '' : "Referer: $site/$referer\r\n") |
|
| 1386 | - . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n")) |
|
| 1387 | - . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n")) |
|
| 1388 | - . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n") |
|
| 1389 | - . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n"); |
|
| 1289 | + $proxy_user = ''; |
|
| 1290 | + $http_proxy = need_proxy($host); |
|
| 1291 | + if ($user) { |
|
| 1292 | + $user = urlencode($user[0]) . ':' . urlencode($user[1]); |
|
| 1293 | + } |
|
| 1294 | + |
|
| 1295 | + $connect = ''; |
|
| 1296 | + if ($http_proxy) { |
|
| 1297 | + if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme, ['tls','ssl'])) { |
|
| 1298 | + $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : ''); |
|
| 1299 | + $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1300 | + . "Host: $path_host\r\n" |
|
| 1301 | + . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1302 | + } else { |
|
| 1303 | + $path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://") |
|
| 1304 | + . (!$user ? '' : "$user@") |
|
| 1305 | + . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1306 | + } |
|
| 1307 | + $t2 = @parse_url($http_proxy); |
|
| 1308 | + $first_host = $t2['host']; |
|
| 1309 | + if (!($port = $t2['port'])) { |
|
| 1310 | + $port = 80; |
|
| 1311 | + } |
|
| 1312 | + if ($t2['user']) { |
|
| 1313 | + $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1314 | + } |
|
| 1315 | + } else { |
|
| 1316 | + $first_host = $noproxy . $host; |
|
| 1317 | + } |
|
| 1318 | + |
|
| 1319 | + if ($connect) { |
|
| 1320 | + $streamContext = stream_context_create([ |
|
| 1321 | + 'ssl' => [ |
|
| 1322 | + 'verify_peer' => false, |
|
| 1323 | + 'allow_self_signed' => true, |
|
| 1324 | + 'SNI_enabled' => true, |
|
| 1325 | + 'peer_name' => $host, |
|
| 1326 | + ] |
|
| 1327 | + ]); |
|
| 1328 | + $f = @stream_socket_client( |
|
| 1329 | + "tcp://$first_host:$port", |
|
| 1330 | + $errno, |
|
| 1331 | + $errstr, |
|
| 1332 | + _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1333 | + STREAM_CLIENT_CONNECT, |
|
| 1334 | + $streamContext |
|
| 1335 | + ); |
|
| 1336 | + spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect'); |
|
| 1337 | + if (!$f) { |
|
| 1338 | + spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1339 | + return $errno; |
|
| 1340 | + } |
|
| 1341 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1342 | + |
|
| 1343 | + fputs($f, $connect); |
|
| 1344 | + fputs($f, "\r\n"); |
|
| 1345 | + $res = fread($f, 1024); |
|
| 1346 | + if ( |
|
| 1347 | + !$res |
|
| 1348 | + or !count($res = explode(' ', $res)) |
|
| 1349 | + or $res[1] !== '200' |
|
| 1350 | + ) { |
|
| 1351 | + spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE); |
|
| 1352 | + fclose($f); |
|
| 1353 | + |
|
| 1354 | + return false; |
|
| 1355 | + } |
|
| 1356 | + // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1357 | + stream_set_blocking($f, true); |
|
| 1358 | + // envoyer le handshake |
|
| 1359 | + stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1360 | + spip_log("OK CONNECT sur $first_host:$port", 'connect'); |
|
| 1361 | + } else { |
|
| 1362 | + $ntry = 3; |
|
| 1363 | + do { |
|
| 1364 | + $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1365 | + } while (!$f and $ntry-- and $errno !== 110 and sleep(1)); |
|
| 1366 | + spip_log("Recuperer $path sur $first_host:$port par $f"); |
|
| 1367 | + if (!$f) { |
|
| 1368 | + spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1369 | + |
|
| 1370 | + return $errno; |
|
| 1371 | + } |
|
| 1372 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1373 | + } |
|
| 1374 | + |
|
| 1375 | + $site = isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : ''; |
|
| 1376 | + |
|
| 1377 | + $host_port = $host; |
|
| 1378 | + if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) { |
|
| 1379 | + $host_port .= ":$port"; |
|
| 1380 | + } |
|
| 1381 | + $req = "$method $path $vers\r\n" |
|
| 1382 | + . "Host: $host_port\r\n" |
|
| 1383 | + . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1384 | + . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1385 | + . (!$site ? '' : "Referer: $site/$referer\r\n") |
|
| 1386 | + . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n")) |
|
| 1387 | + . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n")) |
|
| 1388 | + . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n") |
|
| 1389 | + . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n"); |
|
| 1390 | 1390 | |
| 1391 | 1391 | # spip_log("Requete\n$req", 'distant'); |
| 1392 | - fputs($f, $req); |
|
| 1393 | - fputs($f, $datas ? $datas : "\r\n"); |
|
| 1392 | + fputs($f, $req); |
|
| 1393 | + fputs($f, $datas ? $datas : "\r\n"); |
|
| 1394 | 1394 | |
| 1395 | - return $f; |
|
| 1395 | + return $f; |
|
| 1396 | 1396 | } |