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