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