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