@@ -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 | |
@@ -35,19 +35,19 @@ discard block |
||
| 35 | 35 | * @return string URL nettoyée |
| 36 | 36 | **/ |
| 37 | 37 | function resolve_path($url) { |
| 38 | - list($url, $query) = array_pad(explode('?', $url, 2), 2, null); |
|
| 39 | - while (preg_match(',/\.?/,', $url, $regs) # supprime // et /./ |
|
| 40 | - or preg_match(',/[^/]*/\.\./,S', $url, $regs) # supprime /toto/../ |
|
| 41 | - or preg_match(',^/\.\./,S', $url, $regs) # supprime les /../ du haut |
|
| 42 | - ) { |
|
| 43 | - $url = str_replace($regs[0], '/', $url); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - if ($query) { |
|
| 47 | - $url .= '?' . $query; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - return '/' . preg_replace(',^/,S', '', $url); |
|
| 38 | + list($url, $query) = array_pad(explode('?', $url, 2), 2, null); |
|
| 39 | + while (preg_match(',/\.?/,', $url, $regs) # supprime // et /./ |
|
| 40 | + or preg_match(',/[^/]*/\.\./,S', $url, $regs) # supprime /toto/../ |
|
| 41 | + or preg_match(',^/\.\./,S', $url, $regs) # supprime les /../ du haut |
|
| 42 | + ) { |
|
| 43 | + $url = str_replace($regs[0], '/', $url); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + if ($query) { |
|
| 47 | + $url .= '?' . $query; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + return '/' . preg_replace(',^/,S', '', $url); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | |
@@ -68,39 +68,39 @@ discard block |
||
| 68 | 68 | **/ |
| 69 | 69 | function suivre_lien($url, $lien) { |
| 70 | 70 | |
| 71 | - if (preg_match(',^(mailto|javascript|data|tel|callto|file|ftp):,iS', $lien)) { |
|
| 72 | - return $lien; |
|
| 73 | - } |
|
| 74 | - if (preg_match(';^((?:[a-z]{3,33}:)?//.*?)(/.*)?$;iS', $lien, $r)) { |
|
| 75 | - $r = array_pad($r, 3, null); |
|
| 76 | - |
|
| 77 | - return $r[1] . resolve_path($r[2]); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - # L'url site spip est un lien absolu aussi |
|
| 81 | - if (isset($GLOBALS['meta']['adresse_site']) and $lien == $GLOBALS['meta']['adresse_site']) { |
|
| 82 | - return $lien; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - # lien relatif, il faut verifier l'url de base |
|
| 86 | - # commencer par virer la chaine de get de l'url de base |
|
| 87 | - if (preg_match(';^((?:[a-z]{3,7}:)?//[^/]+)(/.*?/?)?([^/#?]*)([?][^#]*)?(#.*)?$;S', $url, $regs)) { |
|
| 88 | - $debut = $regs[1]; |
|
| 89 | - $dir = !strlen($regs[2]) ? '/' : $regs[2]; |
|
| 90 | - $mot = $regs[3]; |
|
| 91 | - $get = isset($regs[4]) ? $regs[4] : ''; |
|
| 92 | - $hash = isset($regs[5]) ? $regs[5] : ''; |
|
| 93 | - } |
|
| 94 | - switch (substr($lien, 0, 1)) { |
|
| 95 | - case '/': |
|
| 96 | - return $debut . resolve_path($lien); |
|
| 97 | - case '#': |
|
| 98 | - return $debut . resolve_path($dir . $mot . $get . $lien); |
|
| 99 | - case '': |
|
| 100 | - return $debut . resolve_path($dir . $mot . $get . $hash); |
|
| 101 | - default: |
|
| 102 | - return $debut . resolve_path($dir . $lien); |
|
| 103 | - } |
|
| 71 | + if (preg_match(',^(mailto|javascript|data|tel|callto|file|ftp):,iS', $lien)) { |
|
| 72 | + return $lien; |
|
| 73 | + } |
|
| 74 | + if (preg_match(';^((?:[a-z]{3,33}:)?//.*?)(/.*)?$;iS', $lien, $r)) { |
|
| 75 | + $r = array_pad($r, 3, null); |
|
| 76 | + |
|
| 77 | + return $r[1] . resolve_path($r[2]); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + # L'url site spip est un lien absolu aussi |
|
| 81 | + if (isset($GLOBALS['meta']['adresse_site']) and $lien == $GLOBALS['meta']['adresse_site']) { |
|
| 82 | + return $lien; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + # lien relatif, il faut verifier l'url de base |
|
| 86 | + # commencer par virer la chaine de get de l'url de base |
|
| 87 | + if (preg_match(';^((?:[a-z]{3,7}:)?//[^/]+)(/.*?/?)?([^/#?]*)([?][^#]*)?(#.*)?$;S', $url, $regs)) { |
|
| 88 | + $debut = $regs[1]; |
|
| 89 | + $dir = !strlen($regs[2]) ? '/' : $regs[2]; |
|
| 90 | + $mot = $regs[3]; |
|
| 91 | + $get = isset($regs[4]) ? $regs[4] : ''; |
|
| 92 | + $hash = isset($regs[5]) ? $regs[5] : ''; |
|
| 93 | + } |
|
| 94 | + switch (substr($lien, 0, 1)) { |
|
| 95 | + case '/': |
|
| 96 | + return $debut . resolve_path($lien); |
|
| 97 | + case '#': |
|
| 98 | + return $debut . resolve_path($dir . $mot . $get . $lien); |
|
| 99 | + case '': |
|
| 100 | + return $debut . resolve_path($dir . $mot . $get . $hash); |
|
| 101 | + default: |
|
| 102 | + return $debut . resolve_path($dir . $lien); |
|
| 103 | + } |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | |
@@ -123,14 +123,14 @@ discard block |
||
| 123 | 123 | * @return string Texte ou URL (en absolus) |
| 124 | 124 | **/ |
| 125 | 125 | function url_absolue($url, $base = '') { |
| 126 | - if (strlen($url = trim($url)) == 0) { |
|
| 127 | - return ''; |
|
| 128 | - } |
|
| 129 | - if (!$base) { |
|
| 130 | - $base = url_de_base() . (_DIR_RACINE ? _DIR_RESTREINT_ABS : ''); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return suivre_lien($base, $url); |
|
| 126 | + if (strlen($url = trim($url)) == 0) { |
|
| 127 | + return ''; |
|
| 128 | + } |
|
| 129 | + if (!$base) { |
|
| 130 | + $base = url_de_base() . (_DIR_RACINE ? _DIR_RESTREINT_ABS : ''); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return suivre_lien($base, $url); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | * @return string |
| 142 | 142 | */ |
| 143 | 143 | function protocole_implicite($url_absolue) { |
| 144 | - return preg_replace(';^[a-z]{3,7}://;i', '//', $url_absolue); |
|
| 144 | + return preg_replace(';^[a-z]{3,7}://;i', '//', $url_absolue); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -152,14 +152,14 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | function protocole_verifier($url_absolue, $protocoles_autorises = array('http','https')) { |
| 154 | 154 | |
| 155 | - if (preg_match(';^([a-z]{3,7})://;i', $url_absolue, $m)) { |
|
| 156 | - $protocole = $m[1]; |
|
| 157 | - if (in_array($protocole, $protocoles_autorises) |
|
| 158 | - or in_array(strtolower($protocole), array_map('strtolower', $protocoles_autorises))) { |
|
| 159 | - return true; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - return false; |
|
| 155 | + if (preg_match(';^([a-z]{3,7})://;i', $url_absolue, $m)) { |
|
| 156 | + $protocole = $m[1]; |
|
| 157 | + if (in_array($protocole, $protocoles_autorises) |
|
| 158 | + or in_array(strtolower($protocole), array_map('strtolower', $protocoles_autorises))) { |
|
| 159 | + return true; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + return false; |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
@@ -176,27 +176,27 @@ discard block |
||
| 176 | 176 | * @return string Texte avec des URLs absolues |
| 177 | 177 | **/ |
| 178 | 178 | function liens_absolus($texte, $base = '') { |
| 179 | - if (preg_match_all(',(<(a|link|image|img|script)\s[^<>]*(href|src)=[^<>]*>),imsS', $texte, $liens, PREG_SET_ORDER)) { |
|
| 180 | - if (!function_exists('extraire_attribut')) { |
|
| 181 | - include_spip('inc/filtres'); |
|
| 182 | - } |
|
| 183 | - foreach ($liens as $lien) { |
|
| 184 | - foreach (array('href', 'src') as $attr) { |
|
| 185 | - $href = extraire_attribut($lien[0], $attr); |
|
| 186 | - if (strlen($href) > 0) { |
|
| 187 | - if (!preg_match(';^((?:[a-z]{3,7}:)?//);iS', $href)) { |
|
| 188 | - $abs = url_absolue($href, $base); |
|
| 189 | - if (rtrim($href, '/') !== rtrim($abs, '/') and !preg_match('/^#/', $href)) { |
|
| 190 | - $texte_lien = inserer_attribut($lien[0], $attr, $abs); |
|
| 191 | - $texte = str_replace($lien[0], $texte_lien, $texte); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $texte; |
|
| 179 | + if (preg_match_all(',(<(a|link|image|img|script)\s[^<>]*(href|src)=[^<>]*>),imsS', $texte, $liens, PREG_SET_ORDER)) { |
|
| 180 | + if (!function_exists('extraire_attribut')) { |
|
| 181 | + include_spip('inc/filtres'); |
|
| 182 | + } |
|
| 183 | + foreach ($liens as $lien) { |
|
| 184 | + foreach (array('href', 'src') as $attr) { |
|
| 185 | + $href = extraire_attribut($lien[0], $attr); |
|
| 186 | + if (strlen($href) > 0) { |
|
| 187 | + if (!preg_match(';^((?:[a-z]{3,7}:)?//);iS', $href)) { |
|
| 188 | + $abs = url_absolue($href, $base); |
|
| 189 | + if (rtrim($href, '/') !== rtrim($abs, '/') and !preg_match('/^#/', $href)) { |
|
| 190 | + $texte_lien = inserer_attribut($lien[0], $attr, $abs); |
|
| 191 | + $texte = str_replace($lien[0], $texte_lien, $texte); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $texte; |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | |
@@ -212,11 +212,11 @@ discard block |
||
| 212 | 212 | * @return string Texte ou URL (en absolus) |
| 213 | 213 | **/ |
| 214 | 214 | function abs_url($texte, $base = '') { |
| 215 | - if ($GLOBALS['mode_abs_url'] == 'url') { |
|
| 216 | - return url_absolue($texte, $base); |
|
| 217 | - } else { |
|
| 218 | - return liens_absolus($texte, $base); |
|
| 219 | - } |
|
| 215 | + if ($GLOBALS['mode_abs_url'] == 'url') { |
|
| 216 | + return url_absolue($texte, $base); |
|
| 217 | + } else { |
|
| 218 | + return liens_absolus($texte, $base); |
|
| 219 | + } |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | /** |
@@ -229,11 +229,11 @@ discard block |
||
| 229 | 229 | * @return string |
| 230 | 230 | */ |
| 231 | 231 | function spip_htmlspecialchars($string, $flags = null, $encoding = 'ISO-8859-1', $double_encode = true) { |
| 232 | - if (is_null($flags)) { |
|
| 233 | - $flags = ENT_COMPAT|ENT_HTML401; |
|
| 234 | - } |
|
| 232 | + if (is_null($flags)) { |
|
| 233 | + $flags = ENT_COMPAT|ENT_HTML401; |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - return htmlspecialchars($string, $flags, $encoding, $double_encode); |
|
| 236 | + return htmlspecialchars($string, $flags, $encoding, $double_encode); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | /** |
@@ -246,9 +246,9 @@ discard block |
||
| 246 | 246 | * @return string |
| 247 | 247 | */ |
| 248 | 248 | function spip_htmlentities($string, $flags = null, $encoding = 'ISO-8859-1', $double_encode = true) { |
| 249 | - if (is_null($flags)) { |
|
| 250 | - $flags = ENT_COMPAT|ENT_HTML401; |
|
| 251 | - } |
|
| 249 | + if (is_null($flags)) { |
|
| 250 | + $flags = ENT_COMPAT|ENT_HTML401; |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - return htmlentities($string, $flags, $encoding, $double_encode); |
|
| 253 | + return htmlentities($string, $flags, $encoding, $double_encode); |
|
| 254 | 254 | } |
@@ -44,10 +44,10 @@ discard block |
||
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | if ($query) { |
| 47 | - $url .= '?' . $query; |
|
| 47 | + $url .= '?'.$query; |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - return '/' . preg_replace(',^/,S', '', $url); |
|
| 50 | + return '/'.preg_replace(',^/,S', '', $url); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | if (preg_match(';^((?:[a-z]{3,33}:)?//.*?)(/.*)?$;iS', $lien, $r)) { |
| 75 | 75 | $r = array_pad($r, 3, null); |
| 76 | 76 | |
| 77 | - return $r[1] . resolve_path($r[2]); |
|
| 77 | + return $r[1].resolve_path($r[2]); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | # L'url site spip est un lien absolu aussi |
@@ -93,13 +93,13 @@ discard block |
||
| 93 | 93 | } |
| 94 | 94 | switch (substr($lien, 0, 1)) { |
| 95 | 95 | case '/': |
| 96 | - return $debut . resolve_path($lien); |
|
| 96 | + return $debut.resolve_path($lien); |
|
| 97 | 97 | case '#': |
| 98 | - return $debut . resolve_path($dir . $mot . $get . $lien); |
|
| 98 | + return $debut.resolve_path($dir.$mot.$get.$lien); |
|
| 99 | 99 | case '': |
| 100 | - return $debut . resolve_path($dir . $mot . $get . $hash); |
|
| 100 | + return $debut.resolve_path($dir.$mot.$get.$hash); |
|
| 101 | 101 | default: |
| 102 | - return $debut . resolve_path($dir . $lien); |
|
| 102 | + return $debut.resolve_path($dir.$lien); |
|
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | return ''; |
| 128 | 128 | } |
| 129 | 129 | if (!$base) { |
| 130 | - $base = url_de_base() . (_DIR_RACINE ? _DIR_RESTREINT_ABS : ''); |
|
| 130 | + $base = url_de_base().(_DIR_RACINE ? _DIR_RESTREINT_ABS : ''); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | return suivre_lien($base, $url); |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | * @param array $protocoles_autorises |
| 151 | 151 | * @return bool |
| 152 | 152 | */ |
| 153 | -function protocole_verifier($url_absolue, $protocoles_autorises = array('http','https')) { |
|
| 153 | +function protocole_verifier($url_absolue, $protocoles_autorises = array('http', 'https')) { |
|
| 154 | 154 | |
| 155 | 155 | if (preg_match(';^([a-z]{3,7})://;i', $url_absolue, $m)) { |
| 156 | 156 | $protocole = $m[1]; |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | */ |
| 231 | 231 | function spip_htmlspecialchars($string, $flags = null, $encoding = 'ISO-8859-1', $double_encode = true) { |
| 232 | 232 | if (is_null($flags)) { |
| 233 | - $flags = ENT_COMPAT|ENT_HTML401; |
|
| 233 | + $flags = ENT_COMPAT | ENT_HTML401; |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | return htmlspecialchars($string, $flags, $encoding, $double_encode); |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | */ |
| 248 | 248 | function spip_htmlentities($string, $flags = null, $encoding = 'ISO-8859-1', $double_encode = true) { |
| 249 | 249 | if (is_null($flags)) { |
| 250 | - $flags = ENT_COMPAT|ENT_HTML401; |
|
| 250 | + $flags = ENT_COMPAT | ENT_HTML401; |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | return htmlentities($string, $flags, $encoding, $double_encode); |
@@ -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 | include_spip('inc/texte_mini'); |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * @return array Tablea ('','') |
| 35 | 35 | */ |
| 36 | 36 | function definir_raccourcis_alineas() { |
| 37 | - return array('', ''); |
|
| 37 | + return array('', ''); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | * @return string |
| 48 | 48 | */ |
| 49 | 49 | function traiter_tableau($bloc) { |
| 50 | - return $bloc; |
|
| 50 | + return $bloc; |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * @return string |
| 62 | 62 | */ |
| 63 | 63 | function traiter_listes($texte) { |
| 64 | - return $texte; |
|
| 64 | + return $texte; |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
@@ -77,16 +77,16 @@ discard block |
||
| 77 | 77 | */ |
| 78 | 78 | function traiter_raccourcis($letexte) { |
| 79 | 79 | |
| 80 | - // Appeler les fonctions de pre_traitement |
|
| 81 | - $letexte = pipeline('pre_propre', $letexte); |
|
| 80 | + // Appeler les fonctions de pre_traitement |
|
| 81 | + $letexte = pipeline('pre_propre', $letexte); |
|
| 82 | 82 | |
| 83 | - // APPELER ICI UN PIPELINE traiter_raccourcis ? |
|
| 84 | - // $letexte = pipeline('traiter_raccourcis', $letexte); |
|
| 83 | + // APPELER ICI UN PIPELINE traiter_raccourcis ? |
|
| 84 | + // $letexte = pipeline('traiter_raccourcis', $letexte); |
|
| 85 | 85 | |
| 86 | - // Appeler les fonctions de post-traitement |
|
| 87 | - $letexte = pipeline('post_propre', $letexte); |
|
| 86 | + // Appeler les fonctions de post-traitement |
|
| 87 | + $letexte = pipeline('post_propre', $letexte); |
|
| 88 | 88 | |
| 89 | - return $letexte; |
|
| 89 | + return $letexte; |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /************************************************************************************************************************* |
@@ -102,19 +102,19 @@ discard block |
||
| 102 | 102 | * @return string |
| 103 | 103 | */ |
| 104 | 104 | function echappe_js($t, $class = ' class = "echappe-js"') { |
| 105 | - foreach (array('script', 'iframe') as $tag) { |
|
| 106 | - if (stripos($t, "<$tag") !== false |
|
| 107 | - and preg_match_all(',<' . $tag . '.*?($|</' . $tag . '.),isS', $t, $r, PREG_SET_ORDER) |
|
| 108 | - ) { |
|
| 109 | - foreach ($r as $regs) { |
|
| 110 | - $t = str_replace($regs[0], |
|
| 111 | - "<code$class>" . nl2br(spip_htmlspecialchars($regs[0])) . '</code>', |
|
| 112 | - $t); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return $t; |
|
| 105 | + foreach (array('script', 'iframe') as $tag) { |
|
| 106 | + if (stripos($t, "<$tag") !== false |
|
| 107 | + and preg_match_all(',<' . $tag . '.*?($|</' . $tag . '.),isS', $t, $r, PREG_SET_ORDER) |
|
| 108 | + ) { |
|
| 109 | + foreach ($r as $regs) { |
|
| 110 | + $t = str_replace($regs[0], |
|
| 111 | + "<code$class>" . nl2br(spip_htmlspecialchars($regs[0])) . '</code>', |
|
| 112 | + $t); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return $t; |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | |
@@ -143,55 +143,55 @@ discard block |
||
| 143 | 143 | * Code protégé |
| 144 | 144 | **/ |
| 145 | 145 | function interdire_scripts($arg, $mode_filtre=null) { |
| 146 | - // on memorise le resultat sur les arguments non triviaux |
|
| 147 | - static $dejavu = array(); |
|
| 148 | - |
|
| 149 | - // Attention, si ce n'est pas une chaine, laisser intact |
|
| 150 | - if (!$arg or !is_string($arg) or !strstr($arg, '<')) { |
|
| 151 | - return $arg; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - if (is_null($mode_filtre) or !in_array($mode_filtre, array(-1, 0, 1))) { |
|
| 155 | - $mode_filtre = $GLOBALS['filtrer_javascript']; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - if (isset($dejavu[$mode_filtre][$arg])) { |
|
| 159 | - return $dejavu[$mode_filtre][$arg]; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - // echapper les tags asp/php |
|
| 163 | - $t = str_replace('<' . '%', '<%', $arg); |
|
| 164 | - |
|
| 165 | - // echapper le php |
|
| 166 | - $t = str_replace('<' . '?', '<?', $t); |
|
| 167 | - |
|
| 168 | - // echapper le < script language=php > |
|
| 169 | - $t = preg_replace(',<(script\b[^>]+\blanguage\b[^\w>]+php\b),UimsS', '<\1', $t); |
|
| 170 | - |
|
| 171 | - // Pour le js, trois modes : parano (-1), prive (0), ok (1) |
|
| 172 | - switch ($mode_filtre) { |
|
| 173 | - case 0: |
|
| 174 | - if (!_DIR_RESTREINT) { |
|
| 175 | - $t = echappe_js($t); |
|
| 176 | - } |
|
| 177 | - break; |
|
| 178 | - case -1: |
|
| 179 | - $t = echappe_js($t); |
|
| 180 | - break; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - // pas de <base href /> svp ! |
|
| 184 | - $t = preg_replace(',<(base\b),iS', '<\1', $t); |
|
| 185 | - |
|
| 186 | - // Reinserer les echappements des modeles |
|
| 187 | - if (defined('_PROTEGE_JS_MODELES')) { |
|
| 188 | - $t = echappe_retour($t, "javascript" . _PROTEGE_JS_MODELES); |
|
| 189 | - } |
|
| 190 | - if (defined('_PROTEGE_PHP_MODELES')) { |
|
| 191 | - $t = echappe_retour($t, "php" . _PROTEGE_PHP_MODELES); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - return $dejavu[$mode_filtre][$arg] = $t; |
|
| 146 | + // on memorise le resultat sur les arguments non triviaux |
|
| 147 | + static $dejavu = array(); |
|
| 148 | + |
|
| 149 | + // Attention, si ce n'est pas une chaine, laisser intact |
|
| 150 | + if (!$arg or !is_string($arg) or !strstr($arg, '<')) { |
|
| 151 | + return $arg; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + if (is_null($mode_filtre) or !in_array($mode_filtre, array(-1, 0, 1))) { |
|
| 155 | + $mode_filtre = $GLOBALS['filtrer_javascript']; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + if (isset($dejavu[$mode_filtre][$arg])) { |
|
| 159 | + return $dejavu[$mode_filtre][$arg]; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + // echapper les tags asp/php |
|
| 163 | + $t = str_replace('<' . '%', '<%', $arg); |
|
| 164 | + |
|
| 165 | + // echapper le php |
|
| 166 | + $t = str_replace('<' . '?', '<?', $t); |
|
| 167 | + |
|
| 168 | + // echapper le < script language=php > |
|
| 169 | + $t = preg_replace(',<(script\b[^>]+\blanguage\b[^\w>]+php\b),UimsS', '<\1', $t); |
|
| 170 | + |
|
| 171 | + // Pour le js, trois modes : parano (-1), prive (0), ok (1) |
|
| 172 | + switch ($mode_filtre) { |
|
| 173 | + case 0: |
|
| 174 | + if (!_DIR_RESTREINT) { |
|
| 175 | + $t = echappe_js($t); |
|
| 176 | + } |
|
| 177 | + break; |
|
| 178 | + case -1: |
|
| 179 | + $t = echappe_js($t); |
|
| 180 | + break; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + // pas de <base href /> svp ! |
|
| 184 | + $t = preg_replace(',<(base\b),iS', '<\1', $t); |
|
| 185 | + |
|
| 186 | + // Reinserer les echappements des modeles |
|
| 187 | + if (defined('_PROTEGE_JS_MODELES')) { |
|
| 188 | + $t = echappe_retour($t, "javascript" . _PROTEGE_JS_MODELES); |
|
| 189 | + } |
|
| 190 | + if (defined('_PROTEGE_PHP_MODELES')) { |
|
| 191 | + $t = echappe_retour($t, "php" . _PROTEGE_PHP_MODELES); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + return $dejavu[$mode_filtre][$arg] = $t; |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | |
@@ -220,64 +220,64 @@ discard block |
||
| 220 | 220 | * Texte transformé |
| 221 | 221 | **/ |
| 222 | 222 | function typo($letexte, $echapper = true, $connect = null, $env = array()) { |
| 223 | - // Plus vite ! |
|
| 224 | - if (!$letexte) { |
|
| 225 | - return $letexte; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - // les appels directs a cette fonction depuis le php de l'espace |
|
| 229 | - // prive etant historiquement ecrit sans argment $connect |
|
| 230 | - // on utilise la presence de celui-ci pour distinguer les cas |
|
| 231 | - // ou il faut passer interdire_script explicitement |
|
| 232 | - // les appels dans les squelettes (de l'espace prive) fournissant un $connect |
|
| 233 | - // ne seront pas perturbes |
|
| 234 | - $interdire_script = false; |
|
| 235 | - if (is_null($connect)) { |
|
| 236 | - $connect = ''; |
|
| 237 | - $interdire_script = true; |
|
| 238 | - $env['espace_prive'] = test_espace_prive(); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - // Echapper les codes <html> etc |
|
| 242 | - if ($echapper) { |
|
| 243 | - $letexte = echappe_html($letexte, 'TYPO'); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - // |
|
| 247 | - // Installer les modeles, notamment images et documents ; |
|
| 248 | - // |
|
| 249 | - // NOTE : propre() ne passe pas par ici mais directement par corriger_typo |
|
| 250 | - // cf. inc/lien |
|
| 251 | - |
|
| 252 | - $letexte = traiter_modeles($mem = $letexte, false, $echapper ? 'TYPO' : '', $connect, null, $env); |
|
| 253 | - if ($letexte != $mem) { |
|
| 254 | - $echapper = true; |
|
| 255 | - } |
|
| 256 | - unset($mem); |
|
| 257 | - |
|
| 258 | - $letexte = corriger_typo($letexte); |
|
| 259 | - $letexte = echapper_faux_tags($letexte); |
|
| 260 | - |
|
| 261 | - // reintegrer les echappements |
|
| 262 | - if ($echapper) { |
|
| 263 | - $letexte = echappe_retour($letexte, 'TYPO'); |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - // Dans les appels directs hors squelette, securiser ici aussi |
|
| 267 | - if ($interdire_script) { |
|
| 268 | - $letexte = interdire_scripts($letexte); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // Dans l'espace prive on se mefie de tout contenu dangereux |
|
| 272 | - // https://core.spip.net/issues/3371 |
|
| 273 | - // et aussi dans l'espace public si la globale filtrer_javascript = -1 |
|
| 274 | - // https://core.spip.net/issues/4166 |
|
| 275 | - if ($GLOBALS['filtrer_javascript'] == -1 |
|
| 276 | - or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript']<=0)) { |
|
| 277 | - $letexte = echapper_html_suspect($letexte); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - return $letexte; |
|
| 223 | + // Plus vite ! |
|
| 224 | + if (!$letexte) { |
|
| 225 | + return $letexte; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + // les appels directs a cette fonction depuis le php de l'espace |
|
| 229 | + // prive etant historiquement ecrit sans argment $connect |
|
| 230 | + // on utilise la presence de celui-ci pour distinguer les cas |
|
| 231 | + // ou il faut passer interdire_script explicitement |
|
| 232 | + // les appels dans les squelettes (de l'espace prive) fournissant un $connect |
|
| 233 | + // ne seront pas perturbes |
|
| 234 | + $interdire_script = false; |
|
| 235 | + if (is_null($connect)) { |
|
| 236 | + $connect = ''; |
|
| 237 | + $interdire_script = true; |
|
| 238 | + $env['espace_prive'] = test_espace_prive(); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + // Echapper les codes <html> etc |
|
| 242 | + if ($echapper) { |
|
| 243 | + $letexte = echappe_html($letexte, 'TYPO'); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + // |
|
| 247 | + // Installer les modeles, notamment images et documents ; |
|
| 248 | + // |
|
| 249 | + // NOTE : propre() ne passe pas par ici mais directement par corriger_typo |
|
| 250 | + // cf. inc/lien |
|
| 251 | + |
|
| 252 | + $letexte = traiter_modeles($mem = $letexte, false, $echapper ? 'TYPO' : '', $connect, null, $env); |
|
| 253 | + if ($letexte != $mem) { |
|
| 254 | + $echapper = true; |
|
| 255 | + } |
|
| 256 | + unset($mem); |
|
| 257 | + |
|
| 258 | + $letexte = corriger_typo($letexte); |
|
| 259 | + $letexte = echapper_faux_tags($letexte); |
|
| 260 | + |
|
| 261 | + // reintegrer les echappements |
|
| 262 | + if ($echapper) { |
|
| 263 | + $letexte = echappe_retour($letexte, 'TYPO'); |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + // Dans les appels directs hors squelette, securiser ici aussi |
|
| 267 | + if ($interdire_script) { |
|
| 268 | + $letexte = interdire_scripts($letexte); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // Dans l'espace prive on se mefie de tout contenu dangereux |
|
| 272 | + // https://core.spip.net/issues/3371 |
|
| 273 | + // et aussi dans l'espace public si la globale filtrer_javascript = -1 |
|
| 274 | + // https://core.spip.net/issues/4166 |
|
| 275 | + if ($GLOBALS['filtrer_javascript'] == -1 |
|
| 276 | + or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript']<=0)) { |
|
| 277 | + $letexte = echapper_html_suspect($letexte); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + return $letexte; |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | 283 | // Correcteur typographique |
@@ -302,57 +302,57 @@ discard block |
||
| 302 | 302 | */ |
| 303 | 303 | function corriger_typo($letexte, $lang = '') { |
| 304 | 304 | |
| 305 | - // Plus vite ! |
|
| 306 | - if (!$letexte) { |
|
| 307 | - return $letexte; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - $letexte = pipeline('pre_typo', $letexte); |
|
| 311 | - |
|
| 312 | - // Caracteres de controle "illegaux" |
|
| 313 | - $letexte = corriger_caracteres($letexte); |
|
| 314 | - |
|
| 315 | - // Proteger les caracteres typographiques a l'interieur des tags html |
|
| 316 | - if (preg_match_all(_TYPO_BALISE, $letexte, $regs, PREG_SET_ORDER)) { |
|
| 317 | - foreach ($regs as $reg) { |
|
| 318 | - $insert = $reg[0]; |
|
| 319 | - // hack: on transforme les caracteres a proteger en les remplacant |
|
| 320 | - // par des caracteres "illegaux". (cf corriger_caracteres()) |
|
| 321 | - $insert = strtr($insert, _TYPO_PROTEGER, _TYPO_PROTECTEUR); |
|
| 322 | - $letexte = str_replace($reg[0], $insert, $letexte); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - // trouver les blocs idiomes et les traiter à part |
|
| 327 | - $letexte = extraire_idiome($ei = $letexte, $lang, true); |
|
| 328 | - $ei = ($ei !== $letexte); |
|
| 329 | - |
|
| 330 | - // trouver les blocs multi et les traiter a part |
|
| 331 | - $letexte = extraire_multi($em = $letexte, $lang, true); |
|
| 332 | - $em = ($em !== $letexte); |
|
| 333 | - |
|
| 334 | - // Charger & appliquer les fonctions de typographie |
|
| 335 | - $typographie = charger_fonction(lang_typo($lang), 'typographie'); |
|
| 336 | - $letexte = $typographie($letexte); |
|
| 337 | - |
|
| 338 | - // Les citations en une autre langue, s'il y a lieu |
|
| 339 | - if ($em) { |
|
| 340 | - $letexte = echappe_retour($letexte, 'multi'); |
|
| 341 | - } |
|
| 342 | - if ($ei) { |
|
| 343 | - $letexte = echappe_retour($letexte, 'idiome'); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - // Retablir les caracteres proteges |
|
| 347 | - $letexte = strtr($letexte, _TYPO_PROTECTEUR, _TYPO_PROTEGER); |
|
| 348 | - |
|
| 349 | - // pipeline |
|
| 350 | - $letexte = pipeline('post_typo', $letexte); |
|
| 351 | - |
|
| 352 | - # un message pour abs_url - on est passe en mode texte |
|
| 353 | - $GLOBALS['mode_abs_url'] = 'texte'; |
|
| 354 | - |
|
| 355 | - return $letexte; |
|
| 305 | + // Plus vite ! |
|
| 306 | + if (!$letexte) { |
|
| 307 | + return $letexte; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + $letexte = pipeline('pre_typo', $letexte); |
|
| 311 | + |
|
| 312 | + // Caracteres de controle "illegaux" |
|
| 313 | + $letexte = corriger_caracteres($letexte); |
|
| 314 | + |
|
| 315 | + // Proteger les caracteres typographiques a l'interieur des tags html |
|
| 316 | + if (preg_match_all(_TYPO_BALISE, $letexte, $regs, PREG_SET_ORDER)) { |
|
| 317 | + foreach ($regs as $reg) { |
|
| 318 | + $insert = $reg[0]; |
|
| 319 | + // hack: on transforme les caracteres a proteger en les remplacant |
|
| 320 | + // par des caracteres "illegaux". (cf corriger_caracteres()) |
|
| 321 | + $insert = strtr($insert, _TYPO_PROTEGER, _TYPO_PROTECTEUR); |
|
| 322 | + $letexte = str_replace($reg[0], $insert, $letexte); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + // trouver les blocs idiomes et les traiter à part |
|
| 327 | + $letexte = extraire_idiome($ei = $letexte, $lang, true); |
|
| 328 | + $ei = ($ei !== $letexte); |
|
| 329 | + |
|
| 330 | + // trouver les blocs multi et les traiter a part |
|
| 331 | + $letexte = extraire_multi($em = $letexte, $lang, true); |
|
| 332 | + $em = ($em !== $letexte); |
|
| 333 | + |
|
| 334 | + // Charger & appliquer les fonctions de typographie |
|
| 335 | + $typographie = charger_fonction(lang_typo($lang), 'typographie'); |
|
| 336 | + $letexte = $typographie($letexte); |
|
| 337 | + |
|
| 338 | + // Les citations en une autre langue, s'il y a lieu |
|
| 339 | + if ($em) { |
|
| 340 | + $letexte = echappe_retour($letexte, 'multi'); |
|
| 341 | + } |
|
| 342 | + if ($ei) { |
|
| 343 | + $letexte = echappe_retour($letexte, 'idiome'); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + // Retablir les caracteres proteges |
|
| 347 | + $letexte = strtr($letexte, _TYPO_PROTECTEUR, _TYPO_PROTEGER); |
|
| 348 | + |
|
| 349 | + // pipeline |
|
| 350 | + $letexte = pipeline('post_typo', $letexte); |
|
| 351 | + |
|
| 352 | + # un message pour abs_url - on est passe en mode texte |
|
| 353 | + $GLOBALS['mode_abs_url'] = 'texte'; |
|
| 354 | + |
|
| 355 | + return $letexte; |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | * @return string |
| 369 | 369 | */ |
| 370 | 370 | function paragrapher($letexte, $forcer = true) { |
| 371 | - return $letexte; |
|
| 371 | + return $letexte; |
|
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | /** |
@@ -380,11 +380,11 @@ discard block |
||
| 380 | 380 | * @return string Texte |
| 381 | 381 | **/ |
| 382 | 382 | function traiter_retours_chariots($letexte) { |
| 383 | - $letexte = preg_replace(",\r\n?,S", "\n", $letexte); |
|
| 384 | - $letexte = preg_replace(",<p[>[:space:]],iS", "\n\n\\0", $letexte); |
|
| 385 | - $letexte = preg_replace(",</p[>[:space:]],iS", "\\0\n\n", $letexte); |
|
| 383 | + $letexte = preg_replace(",\r\n?,S", "\n", $letexte); |
|
| 384 | + $letexte = preg_replace(",<p[>[:space:]],iS", "\n\n\\0", $letexte); |
|
| 385 | + $letexte = preg_replace(",</p[>[:space:]],iS", "\\0\n\n", $letexte); |
|
| 386 | 386 | |
| 387 | - return $letexte; |
|
| 387 | + return $letexte; |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | |
@@ -410,37 +410,37 @@ discard block |
||
| 410 | 410 | * Texte transformé |
| 411 | 411 | **/ |
| 412 | 412 | function propre($t, $connect = null, $env = array()) { |
| 413 | - // les appels directs a cette fonction depuis le php de l'espace |
|
| 414 | - // prive etant historiquement ecrits sans argment $connect |
|
| 415 | - // on utilise la presence de celui-ci pour distinguer les cas |
|
| 416 | - // ou il faut passer interdire_script explicitement |
|
| 417 | - // les appels dans les squelettes (de l'espace prive) fournissant un $connect |
|
| 418 | - // ne seront pas perturbes |
|
| 419 | - $interdire_script = false; |
|
| 420 | - if (is_null($connect)) { |
|
| 421 | - $connect = ''; |
|
| 422 | - $interdire_script = true; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - if (!$t) { |
|
| 426 | - return strval($t); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - // Dans l'espace prive on se mefie de tout contenu dangereux |
|
| 430 | - // avant echappement des balises <html> |
|
| 431 | - // https://core.spip.net/issues/3371 |
|
| 432 | - // et aussi dans l'espace public si la globale filtrer_javascript = -1 |
|
| 433 | - // https://core.spip.net/issues/4166 |
|
| 434 | - if ($interdire_script |
|
| 435 | - or $GLOBALS['filtrer_javascript'] == -1 |
|
| 436 | - or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript']<=0) |
|
| 437 | - or (isset($env['wysiwyg']) and $env['wysiwyg'] and $GLOBALS['filtrer_javascript']<=0)) { |
|
| 438 | - $t = echapper_html_suspect($t, false); |
|
| 439 | - } |
|
| 440 | - $t = echappe_html($t); |
|
| 441 | - $t = expanser_liens($t, $connect, $env); |
|
| 442 | - $t = traiter_raccourcis($t); |
|
| 443 | - $t = echappe_retour_modeles($t, $interdire_script); |
|
| 444 | - |
|
| 445 | - return $t; |
|
| 413 | + // les appels directs a cette fonction depuis le php de l'espace |
|
| 414 | + // prive etant historiquement ecrits sans argment $connect |
|
| 415 | + // on utilise la presence de celui-ci pour distinguer les cas |
|
| 416 | + // ou il faut passer interdire_script explicitement |
|
| 417 | + // les appels dans les squelettes (de l'espace prive) fournissant un $connect |
|
| 418 | + // ne seront pas perturbes |
|
| 419 | + $interdire_script = false; |
|
| 420 | + if (is_null($connect)) { |
|
| 421 | + $connect = ''; |
|
| 422 | + $interdire_script = true; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + if (!$t) { |
|
| 426 | + return strval($t); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + // Dans l'espace prive on se mefie de tout contenu dangereux |
|
| 430 | + // avant echappement des balises <html> |
|
| 431 | + // https://core.spip.net/issues/3371 |
|
| 432 | + // et aussi dans l'espace public si la globale filtrer_javascript = -1 |
|
| 433 | + // https://core.spip.net/issues/4166 |
|
| 434 | + if ($interdire_script |
|
| 435 | + or $GLOBALS['filtrer_javascript'] == -1 |
|
| 436 | + or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript']<=0) |
|
| 437 | + or (isset($env['wysiwyg']) and $env['wysiwyg'] and $GLOBALS['filtrer_javascript']<=0)) { |
|
| 438 | + $t = echapper_html_suspect($t, false); |
|
| 439 | + } |
|
| 440 | + $t = echappe_html($t); |
|
| 441 | + $t = expanser_liens($t, $connect, $env); |
|
| 442 | + $t = traiter_raccourcis($t); |
|
| 443 | + $t = echappe_retour_modeles($t, $interdire_script); |
|
| 444 | + |
|
| 445 | + return $t; |
|
| 446 | 446 | } |
@@ -104,11 +104,11 @@ discard block |
||
| 104 | 104 | function echappe_js($t, $class = ' class = "echappe-js"') { |
| 105 | 105 | foreach (array('script', 'iframe') as $tag) { |
| 106 | 106 | if (stripos($t, "<$tag") !== false |
| 107 | - and preg_match_all(',<' . $tag . '.*?($|</' . $tag . '.),isS', $t, $r, PREG_SET_ORDER) |
|
| 107 | + and preg_match_all(',<'.$tag.'.*?($|</'.$tag.'.),isS', $t, $r, PREG_SET_ORDER) |
|
| 108 | 108 | ) { |
| 109 | 109 | foreach ($r as $regs) { |
| 110 | 110 | $t = str_replace($regs[0], |
| 111 | - "<code$class>" . nl2br(spip_htmlspecialchars($regs[0])) . '</code>', |
|
| 111 | + "<code$class>".nl2br(spip_htmlspecialchars($regs[0])).'</code>', |
|
| 112 | 112 | $t); |
| 113 | 113 | } |
| 114 | 114 | } |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | * @return string |
| 143 | 143 | * Code protégé |
| 144 | 144 | **/ |
| 145 | -function interdire_scripts($arg, $mode_filtre=null) { |
|
| 145 | +function interdire_scripts($arg, $mode_filtre = null) { |
|
| 146 | 146 | // on memorise le resultat sur les arguments non triviaux |
| 147 | 147 | static $dejavu = array(); |
| 148 | 148 | |
@@ -160,10 +160,10 @@ discard block |
||
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | // echapper les tags asp/php |
| 163 | - $t = str_replace('<' . '%', '<%', $arg); |
|
| 163 | + $t = str_replace('<'.'%', '<%', $arg); |
|
| 164 | 164 | |
| 165 | 165 | // echapper le php |
| 166 | - $t = str_replace('<' . '?', '<?', $t); |
|
| 166 | + $t = str_replace('<'.'?', '<?', $t); |
|
| 167 | 167 | |
| 168 | 168 | // echapper le < script language=php > |
| 169 | 169 | $t = preg_replace(',<(script\b[^>]+\blanguage\b[^\w>]+php\b),UimsS', '<\1', $t); |
@@ -185,10 +185,10 @@ discard block |
||
| 185 | 185 | |
| 186 | 186 | // Reinserer les echappements des modeles |
| 187 | 187 | if (defined('_PROTEGE_JS_MODELES')) { |
| 188 | - $t = echappe_retour($t, "javascript" . _PROTEGE_JS_MODELES); |
|
| 188 | + $t = echappe_retour($t, "javascript"._PROTEGE_JS_MODELES); |
|
| 189 | 189 | } |
| 190 | 190 | if (defined('_PROTEGE_PHP_MODELES')) { |
| 191 | - $t = echappe_retour($t, "php" . _PROTEGE_PHP_MODELES); |
|
| 191 | + $t = echappe_retour($t, "php"._PROTEGE_PHP_MODELES); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | return $dejavu[$mode_filtre][$arg] = $t; |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | // et aussi dans l'espace public si la globale filtrer_javascript = -1 |
| 274 | 274 | // https://core.spip.net/issues/4166 |
| 275 | 275 | if ($GLOBALS['filtrer_javascript'] == -1 |
| 276 | - or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript']<=0)) { |
|
| 276 | + or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript'] <= 0)) { |
|
| 277 | 277 | $letexte = echapper_html_suspect($letexte); |
| 278 | 278 | } |
| 279 | 279 | |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | define('_TYPO_PROTEGER', "!':;?~%-"); |
| 285 | 285 | define('_TYPO_PROTECTEUR', "\x1\x2\x3\x4\x5\x6\x7\x8"); |
| 286 | 286 | |
| 287 | -define('_TYPO_BALISE', ",</?[a-z!][^<>]*[" . preg_quote(_TYPO_PROTEGER) . "][^<>]*>,imsS"); |
|
| 287 | +define('_TYPO_BALISE', ",</?[a-z!][^<>]*[".preg_quote(_TYPO_PROTEGER)."][^<>]*>,imsS"); |
|
| 288 | 288 | |
| 289 | 289 | /** |
| 290 | 290 | * Corrige la typographie |
@@ -433,8 +433,8 @@ discard block |
||
| 433 | 433 | // https://core.spip.net/issues/4166 |
| 434 | 434 | if ($interdire_script |
| 435 | 435 | or $GLOBALS['filtrer_javascript'] == -1 |
| 436 | - or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript']<=0) |
|
| 437 | - or (isset($env['wysiwyg']) and $env['wysiwyg'] and $GLOBALS['filtrer_javascript']<=0)) { |
|
| 436 | + or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript'] <= 0) |
|
| 437 | + or (isset($env['wysiwyg']) and $env['wysiwyg'] and $GLOBALS['filtrer_javascript'] <= 0)) { |
|
| 438 | 438 | $t = echapper_html_suspect($t, false); |
| 439 | 439 | } |
| 440 | 440 | $t = echappe_html($t); |
@@ -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 | include_spip('inc/filtres'); |
| 23 | 23 | include_spip('inc/lang'); |
@@ -39,23 +39,23 @@ discard block |
||
| 39 | 39 | **/ |
| 40 | 40 | function definir_puce() { |
| 41 | 41 | |
| 42 | - // Attention au sens, qui n'est pas defini de la meme facon dans |
|
| 43 | - // l'espace prive (spip_lang est la langue de l'interface, lang_dir |
|
| 44 | - // celle du texte) et public (spip_lang est la langue du texte) |
|
| 45 | - $dir = _DIR_RESTREINT ? lang_dir() : lang_dir($GLOBALS['spip_lang']); |
|
| 42 | + // Attention au sens, qui n'est pas defini de la meme facon dans |
|
| 43 | + // l'espace prive (spip_lang est la langue de l'interface, lang_dir |
|
| 44 | + // celle du texte) et public (spip_lang est la langue du texte) |
|
| 45 | + $dir = _DIR_RESTREINT ? lang_dir() : lang_dir($GLOBALS['spip_lang']); |
|
| 46 | 46 | |
| 47 | - $p = 'puce' . (test_espace_prive() ? '_prive' : ''); |
|
| 48 | - if ($dir == 'rtl') { |
|
| 49 | - $p .= '_rtl'; |
|
| 50 | - } |
|
| 47 | + $p = 'puce' . (test_espace_prive() ? '_prive' : ''); |
|
| 48 | + if ($dir == 'rtl') { |
|
| 49 | + $p .= '_rtl'; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - if (!isset($GLOBALS[$p])) { |
|
| 53 | - $img = find_in_path($p . '.gif'); |
|
| 54 | - list(, , , $size) = @getimagesize($img); |
|
| 55 | - $GLOBALS[$p] = '<img src="' . $img . '" ' . $size . ' class="puce" alt="-" />'; |
|
| 56 | - } |
|
| 52 | + if (!isset($GLOBALS[$p])) { |
|
| 53 | + $img = find_in_path($p . '.gif'); |
|
| 54 | + list(, , , $size) = @getimagesize($img); |
|
| 55 | + $GLOBALS[$p] = '<img src="' . $img . '" ' . $size . ' class="puce" alt="-" />'; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - return $GLOBALS[$p]; |
|
| 58 | + return $GLOBALS[$p]; |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | |
@@ -63,13 +63,13 @@ discard block |
||
| 63 | 63 | // dont on souhaite qu'ils provoquent un saut de paragraphe |
| 64 | 64 | |
| 65 | 65 | if (!defined('_BALISES_BLOCS')) { |
| 66 | - define('_BALISES_BLOCS', |
|
| 67 | - 'address|applet|article|aside|blockquote|button|center|d[ltd]|div|fieldset|fig(ure|caption)|footer|form|h[1-6r]|hgroup|head|header|iframe|li|map|marquee|nav|noscript|object|ol|pre|section|t(able|[rdh]|body|foot|extarea)|ul|script|style' |
|
| 68 | - ); |
|
| 66 | + define('_BALISES_BLOCS', |
|
| 67 | + 'address|applet|article|aside|blockquote|button|center|d[ltd]|div|fieldset|fig(ure|caption)|footer|form|h[1-6r]|hgroup|head|header|iframe|li|map|marquee|nav|noscript|object|ol|pre|section|t(able|[rdh]|body|foot|extarea)|ul|script|style' |
|
| 68 | + ); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | if (!defined('_BALISES_BLOCS_REGEXP')) { |
| 72 | - define('_BALISES_BLOCS_REGEXP', ',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS'); |
|
| 72 | + define('_BALISES_BLOCS_REGEXP', ',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS'); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // |
@@ -81,26 +81,26 @@ discard block |
||
| 81 | 81 | // echappe est un div ou un span |
| 82 | 82 | // http://code.spip.net/@code_echappement |
| 83 | 83 | function code_echappement($rempl, $source = '', $no_transform = false, $mode = null) { |
| 84 | - if (!strlen($rempl)) { |
|
| 85 | - return ''; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - // Tester si on echappe en span ou en div |
|
| 89 | - if (is_null($mode) or !in_array($mode, array('div', 'span'))) { |
|
| 90 | - $mode = preg_match(',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS', $rempl) ? 'div' : 'span'; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - // Decouper en morceaux, base64 a des probleme selon la taille de la pile |
|
| 94 | - $taille = 30000; |
|
| 95 | - $return = ""; |
|
| 96 | - for ($i = 0; $i < strlen($rempl); $i += $taille) { |
|
| 97 | - // Convertir en base64 et cacher dans un attribut |
|
| 98 | - // utiliser les " pour eviter le re-encodage de ' et ’ |
|
| 99 | - $base64 = base64_encode(substr($rempl, $i, $taille)); |
|
| 100 | - $return .= "<$mode class=\"base64$source\" title=\"$base64\"></$mode>"; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return $return; |
|
| 84 | + if (!strlen($rempl)) { |
|
| 85 | + return ''; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + // Tester si on echappe en span ou en div |
|
| 89 | + if (is_null($mode) or !in_array($mode, array('div', 'span'))) { |
|
| 90 | + $mode = preg_match(',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS', $rempl) ? 'div' : 'span'; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + // Decouper en morceaux, base64 a des probleme selon la taille de la pile |
|
| 94 | + $taille = 30000; |
|
| 95 | + $return = ""; |
|
| 96 | + for ($i = 0; $i < strlen($rempl); $i += $taille) { |
|
| 97 | + // Convertir en base64 et cacher dans un attribut |
|
| 98 | + // utiliser les " pour eviter le re-encodage de ' et ’ |
|
| 99 | + $base64 = base64_encode(substr($rempl, $i, $taille)); |
|
| 100 | + $return .= "<$mode class=\"base64$source\" title=\"$base64\"></$mode>"; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return $return; |
|
| 104 | 104 | |
| 105 | 105 | } |
| 106 | 106 | |
@@ -108,64 +108,64 @@ discard block |
||
| 108 | 108 | // Echapper les <html>...</ html> |
| 109 | 109 | // http://code.spip.net/@traiter_echap_html_dist |
| 110 | 110 | function traiter_echap_html_dist($regs) { |
| 111 | - return $regs[3]; |
|
| 111 | + return $regs[3]; |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | // Echapper les <code>...</ code> |
| 115 | 115 | // http://code.spip.net/@traiter_echap_code_dist |
| 116 | 116 | function traiter_echap_code_dist($regs) { |
| 117 | - list(, , $att, $corps) = $regs; |
|
| 118 | - $echap = spip_htmlspecialchars($corps); // il ne faut pas passer dans entites_html, ne pas transformer les &#xxx; du code ! |
|
| 119 | - |
|
| 120 | - // ne pas mettre le <div...> s'il n'y a qu'une ligne |
|
| 121 | - if (is_int(strpos($echap, "\n"))) { |
|
| 122 | - // supprimer les sauts de ligne debut/fin |
|
| 123 | - // (mais pas les espaces => ascii art). |
|
| 124 | - $echap = preg_replace("/^[\n\r]+|[\n\r]+$/s", "", $echap); |
|
| 125 | - $echap = nl2br($echap); |
|
| 126 | - $echap = "<div style='text-align: left;' " |
|
| 127 | - . "class='spip_code' dir='ltr'><code$att>" |
|
| 128 | - . $echap . "</code></div>"; |
|
| 129 | - } else { |
|
| 130 | - $echap = "<code$att class='spip_code' dir='ltr'>" . $echap . "</code>"; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $echap = str_replace("\t", " ", $echap); |
|
| 134 | - $echap = str_replace(" ", " ", $echap); |
|
| 135 | - |
|
| 136 | - return $echap; |
|
| 117 | + list(, , $att, $corps) = $regs; |
|
| 118 | + $echap = spip_htmlspecialchars($corps); // il ne faut pas passer dans entites_html, ne pas transformer les &#xxx; du code ! |
|
| 119 | + |
|
| 120 | + // ne pas mettre le <div...> s'il n'y a qu'une ligne |
|
| 121 | + if (is_int(strpos($echap, "\n"))) { |
|
| 122 | + // supprimer les sauts de ligne debut/fin |
|
| 123 | + // (mais pas les espaces => ascii art). |
|
| 124 | + $echap = preg_replace("/^[\n\r]+|[\n\r]+$/s", "", $echap); |
|
| 125 | + $echap = nl2br($echap); |
|
| 126 | + $echap = "<div style='text-align: left;' " |
|
| 127 | + . "class='spip_code' dir='ltr'><code$att>" |
|
| 128 | + . $echap . "</code></div>"; |
|
| 129 | + } else { |
|
| 130 | + $echap = "<code$att class='spip_code' dir='ltr'>" . $echap . "</code>"; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $echap = str_replace("\t", " ", $echap); |
|
| 134 | + $echap = str_replace(" ", " ", $echap); |
|
| 135 | + |
|
| 136 | + return $echap; |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | // Echapper les <cadre>...</ cadre> aka <frame>...</ frame> |
| 140 | 140 | // http://code.spip.net/@traiter_echap_cadre_dist |
| 141 | 141 | function traiter_echap_cadre_dist($regs) { |
| 142 | - $echap = trim(entites_html($regs[3])); |
|
| 143 | - // compter les lignes un peu plus finement qu'avec les \n |
|
| 144 | - $lignes = explode("\n", trim($echap)); |
|
| 145 | - $n = 0; |
|
| 146 | - foreach ($lignes as $l) { |
|
| 147 | - $n += floor(strlen($l) / 60) + 1; |
|
| 148 | - } |
|
| 149 | - $n = max($n, 2); |
|
| 150 | - $echap = "\n<textarea readonly='readonly' cols='40' rows='$n' class='spip_cadre' dir='ltr'>$echap</textarea>"; |
|
| 151 | - |
|
| 152 | - return $echap; |
|
| 142 | + $echap = trim(entites_html($regs[3])); |
|
| 143 | + // compter les lignes un peu plus finement qu'avec les \n |
|
| 144 | + $lignes = explode("\n", trim($echap)); |
|
| 145 | + $n = 0; |
|
| 146 | + foreach ($lignes as $l) { |
|
| 147 | + $n += floor(strlen($l) / 60) + 1; |
|
| 148 | + } |
|
| 149 | + $n = max($n, 2); |
|
| 150 | + $echap = "\n<textarea readonly='readonly' cols='40' rows='$n' class='spip_cadre' dir='ltr'>$echap</textarea>"; |
|
| 151 | + |
|
| 152 | + return $echap; |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | // http://code.spip.net/@traiter_echap_frame_dist |
| 156 | 156 | function traiter_echap_frame_dist($regs) { |
| 157 | - return traiter_echap_cadre_dist($regs); |
|
| 157 | + return traiter_echap_cadre_dist($regs); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // http://code.spip.net/@traiter_echap_script_dist |
| 161 | 161 | function traiter_echap_script_dist($regs) { |
| 162 | - // rendre joli (et inactif) si c'est un script language=php |
|
| 163 | - if (preg_match(',<script\b[^>]+php,ims', $regs[0])) { |
|
| 164 | - return highlight_string($regs[0], true); |
|
| 165 | - } |
|
| 162 | + // rendre joli (et inactif) si c'est un script language=php |
|
| 163 | + if (preg_match(',<script\b[^>]+php,ims', $regs[0])) { |
|
| 164 | + return highlight_string($regs[0], true); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - // Cas normal : le script passe tel quel |
|
| 168 | - return $regs[0]; |
|
| 167 | + // Cas normal : le script passe tel quel |
|
| 168 | + return $regs[0]; |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | define('_PROTEGE_BLOCS', ',<(html|code|cadre|frame|script|style)(\s[^>]*)?>(.*)</\1>,UimsS'); |
@@ -174,71 +174,71 @@ discard block |
||
| 174 | 174 | // - pour $no_transform voir le filtre post_autobr dans inc/filtres |
| 175 | 175 | // http://code.spip.net/@echappe_html |
| 176 | 176 | function echappe_html( |
| 177 | - $letexte, |
|
| 178 | - $source = '', |
|
| 179 | - $no_transform = false, |
|
| 180 | - $preg = '' |
|
| 177 | + $letexte, |
|
| 178 | + $source = '', |
|
| 179 | + $no_transform = false, |
|
| 180 | + $preg = '' |
|
| 181 | 181 | ) { |
| 182 | - if (!is_string($letexte) or !strlen($letexte)) { |
|
| 183 | - return $letexte; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - // si le texte recu est long PCRE risque d'exploser, on |
|
| 187 | - // fait donc un mic-mac pour augmenter pcre.backtrack_limit |
|
| 188 | - if (($len = strlen($letexte)) > 100000) { |
|
| 189 | - if (!$old = @ini_get('pcre.backtrack_limit')) { |
|
| 190 | - $old = 100000; |
|
| 191 | - } |
|
| 192 | - if ($len > $old) { |
|
| 193 | - $a = @ini_set('pcre.backtrack_limit', $len); |
|
| 194 | - spip_log("ini_set pcre.backtrack_limit=$len ($old)"); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - if (($preg or strpos($letexte, "<") !== false) |
|
| 199 | - and preg_match_all($preg ? $preg : _PROTEGE_BLOCS, $letexte, $matches, PREG_SET_ORDER) |
|
| 200 | - ) { |
|
| 201 | - foreach ($matches as $regs) { |
|
| 202 | - // echappements tels quels ? |
|
| 203 | - if ($no_transform) { |
|
| 204 | - $echap = $regs[0]; |
|
| 205 | - } // sinon les traiter selon le cas |
|
| 206 | - else { |
|
| 207 | - if (function_exists($f = 'traiter_echap_' . strtolower($regs[1]))) { |
|
| 208 | - $echap = $f($regs); |
|
| 209 | - } else { |
|
| 210 | - if (function_exists($f = $f . '_dist')) { |
|
| 211 | - $echap = $f($regs); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $p = strpos($letexte, $regs[0]); |
|
| 217 | - $letexte = substr_replace($letexte, code_echappement($echap, $source, $no_transform), $p, strlen($regs[0])); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - if ($no_transform) { |
|
| 222 | - return $letexte; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - // Echapper le php pour faire joli (ici, c'est pas pour la securite) |
|
| 226 | - // seulement si on a echappe les <script> |
|
| 227 | - // (derogatoire car on ne peut pas faire passer < ? ... ? > |
|
| 228 | - // dans une callback autonommee |
|
| 229 | - if (strpos($preg ? $preg : _PROTEGE_BLOCS, 'script') !== false) { |
|
| 230 | - if (strpos($letexte, "<" . "?") !== false and preg_match_all(',<[?].*($|[?]>),UisS', |
|
| 231 | - $letexte, $matches, PREG_SET_ORDER) |
|
| 232 | - ) { |
|
| 233 | - foreach ($matches as $regs) { |
|
| 234 | - $letexte = str_replace($regs[0], |
|
| 235 | - code_echappement(highlight_string($regs[0], true), $source), |
|
| 236 | - $letexte); |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - return $letexte; |
|
| 182 | + if (!is_string($letexte) or !strlen($letexte)) { |
|
| 183 | + return $letexte; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + // si le texte recu est long PCRE risque d'exploser, on |
|
| 187 | + // fait donc un mic-mac pour augmenter pcre.backtrack_limit |
|
| 188 | + if (($len = strlen($letexte)) > 100000) { |
|
| 189 | + if (!$old = @ini_get('pcre.backtrack_limit')) { |
|
| 190 | + $old = 100000; |
|
| 191 | + } |
|
| 192 | + if ($len > $old) { |
|
| 193 | + $a = @ini_set('pcre.backtrack_limit', $len); |
|
| 194 | + spip_log("ini_set pcre.backtrack_limit=$len ($old)"); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + if (($preg or strpos($letexte, "<") !== false) |
|
| 199 | + and preg_match_all($preg ? $preg : _PROTEGE_BLOCS, $letexte, $matches, PREG_SET_ORDER) |
|
| 200 | + ) { |
|
| 201 | + foreach ($matches as $regs) { |
|
| 202 | + // echappements tels quels ? |
|
| 203 | + if ($no_transform) { |
|
| 204 | + $echap = $regs[0]; |
|
| 205 | + } // sinon les traiter selon le cas |
|
| 206 | + else { |
|
| 207 | + if (function_exists($f = 'traiter_echap_' . strtolower($regs[1]))) { |
|
| 208 | + $echap = $f($regs); |
|
| 209 | + } else { |
|
| 210 | + if (function_exists($f = $f . '_dist')) { |
|
| 211 | + $echap = $f($regs); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $p = strpos($letexte, $regs[0]); |
|
| 217 | + $letexte = substr_replace($letexte, code_echappement($echap, $source, $no_transform), $p, strlen($regs[0])); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + if ($no_transform) { |
|
| 222 | + return $letexte; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + // Echapper le php pour faire joli (ici, c'est pas pour la securite) |
|
| 226 | + // seulement si on a echappe les <script> |
|
| 227 | + // (derogatoire car on ne peut pas faire passer < ? ... ? > |
|
| 228 | + // dans une callback autonommee |
|
| 229 | + if (strpos($preg ? $preg : _PROTEGE_BLOCS, 'script') !== false) { |
|
| 230 | + if (strpos($letexte, "<" . "?") !== false and preg_match_all(',<[?].*($|[?]>),UisS', |
|
| 231 | + $letexte, $matches, PREG_SET_ORDER) |
|
| 232 | + ) { |
|
| 233 | + foreach ($matches as $regs) { |
|
| 234 | + $letexte = str_replace($regs[0], |
|
| 235 | + code_echappement(highlight_string($regs[0], true), $source), |
|
| 236 | + $letexte); |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + return $letexte; |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | // |
@@ -247,52 +247,52 @@ discard block |
||
| 247 | 247 | // par propre() : exemple dans multi et dans typo() |
| 248 | 248 | // http://code.spip.net/@echappe_retour |
| 249 | 249 | function echappe_retour($letexte, $source = '', $filtre = "") { |
| 250 | - if (strpos($letexte, "base64$source")) { |
|
| 251 | - # spip_log(spip_htmlspecialchars($letexte)); ## pour les curieux |
|
| 252 | - $max_prof = 5; |
|
| 253 | - while (strpos($letexte, "<") !== false |
|
| 254 | - and |
|
| 255 | - preg_match_all(',<(span|div)\sclass=[\'"]base64' . $source . '[\'"]\s(.*)>\s*</\1>,UmsS', |
|
| 256 | - $letexte, $regs, PREG_SET_ORDER) |
|
| 257 | - and $max_prof--) { |
|
| 258 | - foreach ($regs as $reg) { |
|
| 259 | - $rempl = base64_decode(extraire_attribut($reg[0], 'title')); |
|
| 260 | - // recherche d'attributs supplementaires |
|
| 261 | - $at = array(); |
|
| 262 | - foreach (array('lang', 'dir') as $attr) { |
|
| 263 | - if ($a = extraire_attribut($reg[0], $attr)) { |
|
| 264 | - $at[$attr] = $a; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - if ($at) { |
|
| 268 | - $rempl = '<' . $reg[1] . '>' . $rempl . '</' . $reg[1] . '>'; |
|
| 269 | - foreach ($at as $attr => $a) { |
|
| 270 | - $rempl = inserer_attribut($rempl, $attr, $a); |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - if ($filtre) { |
|
| 274 | - $rempl = $filtre($rempl); |
|
| 275 | - } |
|
| 276 | - $letexte = str_replace($reg[0], $rempl, $letexte); |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - return $letexte; |
|
| 250 | + if (strpos($letexte, "base64$source")) { |
|
| 251 | + # spip_log(spip_htmlspecialchars($letexte)); ## pour les curieux |
|
| 252 | + $max_prof = 5; |
|
| 253 | + while (strpos($letexte, "<") !== false |
|
| 254 | + and |
|
| 255 | + preg_match_all(',<(span|div)\sclass=[\'"]base64' . $source . '[\'"]\s(.*)>\s*</\1>,UmsS', |
|
| 256 | + $letexte, $regs, PREG_SET_ORDER) |
|
| 257 | + and $max_prof--) { |
|
| 258 | + foreach ($regs as $reg) { |
|
| 259 | + $rempl = base64_decode(extraire_attribut($reg[0], 'title')); |
|
| 260 | + // recherche d'attributs supplementaires |
|
| 261 | + $at = array(); |
|
| 262 | + foreach (array('lang', 'dir') as $attr) { |
|
| 263 | + if ($a = extraire_attribut($reg[0], $attr)) { |
|
| 264 | + $at[$attr] = $a; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + if ($at) { |
|
| 268 | + $rempl = '<' . $reg[1] . '>' . $rempl . '</' . $reg[1] . '>'; |
|
| 269 | + foreach ($at as $attr => $a) { |
|
| 270 | + $rempl = inserer_attribut($rempl, $attr, $a); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + if ($filtre) { |
|
| 274 | + $rempl = $filtre($rempl); |
|
| 275 | + } |
|
| 276 | + $letexte = str_replace($reg[0], $rempl, $letexte); |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + return $letexte; |
|
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | // Reinserer le javascript de confiance (venant des modeles) |
| 285 | 285 | |
| 286 | 286 | // http://code.spip.net/@echappe_retour_modeles |
| 287 | 287 | function echappe_retour_modeles($letexte, $interdire_scripts = false) { |
| 288 | - $letexte = echappe_retour($letexte); |
|
| 288 | + $letexte = echappe_retour($letexte); |
|
| 289 | 289 | |
| 290 | - // Dans les appels directs hors squelette, securiser aussi ici |
|
| 291 | - if ($interdire_scripts) { |
|
| 292 | - $letexte = interdire_scripts($letexte); |
|
| 293 | - } |
|
| 290 | + // Dans les appels directs hors squelette, securiser aussi ici |
|
| 291 | + if ($interdire_scripts) { |
|
| 292 | + $letexte = interdire_scripts($letexte); |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - return trim($letexte); |
|
| 295 | + return trim($letexte); |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | |
@@ -320,127 +320,127 @@ discard block |
||
| 320 | 320 | * Texte coupé |
| 321 | 321 | **/ |
| 322 | 322 | function couper($texte, $taille = 50, $suite = ' (...)') { |
| 323 | - if (!($length = strlen($texte)) or $taille <= 0) { |
|
| 324 | - return ''; |
|
| 325 | - } |
|
| 326 | - $offset = 400 + 2 * $taille; |
|
| 327 | - while ($offset < $length |
|
| 328 | - and strlen(preg_replace(",<(!--|\w|/)[^>]+>,Uims", "", substr($texte, 0, $offset))) < $taille) { |
|
| 329 | - $offset = 2 * $offset; |
|
| 330 | - } |
|
| 331 | - if ($offset < $length |
|
| 332 | - && ($p_tag_ouvrant = strpos($texte, '<', $offset)) !== null |
|
| 333 | - ) { |
|
| 334 | - $p_tag_fermant = strpos($texte, '>', $offset); |
|
| 335 | - if ($p_tag_fermant && ($p_tag_fermant < $p_tag_ouvrant)) { |
|
| 336 | - $offset = $p_tag_fermant + 1; |
|
| 337 | - } // prolonger la coupe jusqu'au tag fermant suivant eventuel |
|
| 338 | - } |
|
| 339 | - $texte = substr($texte, 0, $offset); /* eviter de travailler sur 10ko pour extraire 150 caracteres */ |
|
| 340 | - |
|
| 341 | - if (!function_exists('nettoyer_raccourcis_typo')) { |
|
| 342 | - include_spip('inc/lien'); |
|
| 343 | - } |
|
| 344 | - $texte = nettoyer_raccourcis_typo($texte); |
|
| 345 | - |
|
| 346 | - // balises de sauts de ligne et paragraphe |
|
| 347 | - $texte = preg_replace("/<p( [^>]*)?" . ">/", "\r", $texte); |
|
| 348 | - $texte = preg_replace("/<br( [^>]*)?" . ">/", "\n", $texte); |
|
| 349 | - |
|
| 350 | - // on repasse les doubles \n en \r que nettoyer_raccourcis_typo() a pu modifier |
|
| 351 | - $texte = str_replace("\n\n", "\r", $texte); |
|
| 352 | - |
|
| 353 | - // supprimer les tags |
|
| 354 | - $texte = supprimer_tags($texte); |
|
| 355 | - $texte = trim(str_replace("\n", " ", $texte)); |
|
| 356 | - $texte .= "\n"; // marquer la fin |
|
| 357 | - |
|
| 358 | - // corriger la longueur de coupe |
|
| 359 | - // en fonction de la presence de caracteres utf |
|
| 360 | - if ($GLOBALS['meta']['charset'] == 'utf-8') { |
|
| 361 | - $long = charset2unicode($texte); |
|
| 362 | - $long = spip_substr($long, 0, max($taille, 1)); |
|
| 363 | - $nbcharutf = preg_match_all('/(&#[0-9]{3,6};)/S', $long, $matches); |
|
| 364 | - $taille += $nbcharutf; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - // couper au mot precedent |
|
| 369 | - $long = spip_substr($texte, 0, max($taille - 4, 1)); |
|
| 370 | - $u = $GLOBALS['meta']['pcre_u']; |
|
| 371 | - $court = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long); |
|
| 372 | - $points = $suite; |
|
| 373 | - |
|
| 374 | - // trop court ? ne pas faire de (...) |
|
| 375 | - if (spip_strlen($court) < max(0.75 * $taille, 2)) { |
|
| 376 | - $points = ''; |
|
| 377 | - $long = spip_substr($texte, 0, $taille); |
|
| 378 | - $texte = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long); |
|
| 379 | - // encore trop court ? couper au caractere |
|
| 380 | - if (spip_strlen($texte) < 0.75 * $taille) { |
|
| 381 | - $texte = $long; |
|
| 382 | - } |
|
| 383 | - } else { |
|
| 384 | - $texte = $court; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - if (strpos($texte, "\n")) // la fin est encore la : c'est qu'on n'a pas de texte de suite |
|
| 388 | - { |
|
| 389 | - $points = ''; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - // remettre les paragraphes |
|
| 393 | - $texte = preg_replace("/\r+/", "\n\n", $texte); |
|
| 394 | - |
|
| 395 | - // supprimer l'eventuelle entite finale mal coupee |
|
| 396 | - $texte = preg_replace('/&#?[a-z0-9]*$/S', '', $texte); |
|
| 397 | - |
|
| 398 | - return quote_amp(trim($texte)) . $points; |
|
| 323 | + if (!($length = strlen($texte)) or $taille <= 0) { |
|
| 324 | + return ''; |
|
| 325 | + } |
|
| 326 | + $offset = 400 + 2 * $taille; |
|
| 327 | + while ($offset < $length |
|
| 328 | + and strlen(preg_replace(",<(!--|\w|/)[^>]+>,Uims", "", substr($texte, 0, $offset))) < $taille) { |
|
| 329 | + $offset = 2 * $offset; |
|
| 330 | + } |
|
| 331 | + if ($offset < $length |
|
| 332 | + && ($p_tag_ouvrant = strpos($texte, '<', $offset)) !== null |
|
| 333 | + ) { |
|
| 334 | + $p_tag_fermant = strpos($texte, '>', $offset); |
|
| 335 | + if ($p_tag_fermant && ($p_tag_fermant < $p_tag_ouvrant)) { |
|
| 336 | + $offset = $p_tag_fermant + 1; |
|
| 337 | + } // prolonger la coupe jusqu'au tag fermant suivant eventuel |
|
| 338 | + } |
|
| 339 | + $texte = substr($texte, 0, $offset); /* eviter de travailler sur 10ko pour extraire 150 caracteres */ |
|
| 340 | + |
|
| 341 | + if (!function_exists('nettoyer_raccourcis_typo')) { |
|
| 342 | + include_spip('inc/lien'); |
|
| 343 | + } |
|
| 344 | + $texte = nettoyer_raccourcis_typo($texte); |
|
| 345 | + |
|
| 346 | + // balises de sauts de ligne et paragraphe |
|
| 347 | + $texte = preg_replace("/<p( [^>]*)?" . ">/", "\r", $texte); |
|
| 348 | + $texte = preg_replace("/<br( [^>]*)?" . ">/", "\n", $texte); |
|
| 349 | + |
|
| 350 | + // on repasse les doubles \n en \r que nettoyer_raccourcis_typo() a pu modifier |
|
| 351 | + $texte = str_replace("\n\n", "\r", $texte); |
|
| 352 | + |
|
| 353 | + // supprimer les tags |
|
| 354 | + $texte = supprimer_tags($texte); |
|
| 355 | + $texte = trim(str_replace("\n", " ", $texte)); |
|
| 356 | + $texte .= "\n"; // marquer la fin |
|
| 357 | + |
|
| 358 | + // corriger la longueur de coupe |
|
| 359 | + // en fonction de la presence de caracteres utf |
|
| 360 | + if ($GLOBALS['meta']['charset'] == 'utf-8') { |
|
| 361 | + $long = charset2unicode($texte); |
|
| 362 | + $long = spip_substr($long, 0, max($taille, 1)); |
|
| 363 | + $nbcharutf = preg_match_all('/(&#[0-9]{3,6};)/S', $long, $matches); |
|
| 364 | + $taille += $nbcharutf; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + // couper au mot precedent |
|
| 369 | + $long = spip_substr($texte, 0, max($taille - 4, 1)); |
|
| 370 | + $u = $GLOBALS['meta']['pcre_u']; |
|
| 371 | + $court = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long); |
|
| 372 | + $points = $suite; |
|
| 373 | + |
|
| 374 | + // trop court ? ne pas faire de (...) |
|
| 375 | + if (spip_strlen($court) < max(0.75 * $taille, 2)) { |
|
| 376 | + $points = ''; |
|
| 377 | + $long = spip_substr($texte, 0, $taille); |
|
| 378 | + $texte = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long); |
|
| 379 | + // encore trop court ? couper au caractere |
|
| 380 | + if (spip_strlen($texte) < 0.75 * $taille) { |
|
| 381 | + $texte = $long; |
|
| 382 | + } |
|
| 383 | + } else { |
|
| 384 | + $texte = $court; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + if (strpos($texte, "\n")) // la fin est encore la : c'est qu'on n'a pas de texte de suite |
|
| 388 | + { |
|
| 389 | + $points = ''; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + // remettre les paragraphes |
|
| 393 | + $texte = preg_replace("/\r+/", "\n\n", $texte); |
|
| 394 | + |
|
| 395 | + // supprimer l'eventuelle entite finale mal coupee |
|
| 396 | + $texte = preg_replace('/&#?[a-z0-9]*$/S', '', $texte); |
|
| 397 | + |
|
| 398 | + return quote_amp(trim($texte)) . $points; |
|
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | |
| 402 | 402 | // http://code.spip.net/@protege_js_modeles |
| 403 | 403 | function protege_js_modeles($t) { |
| 404 | - if (isset($GLOBALS['visiteur_session'])) { |
|
| 405 | - if (preg_match_all(',<script.*?($|</script.),isS', $t, $r, PREG_SET_ORDER)) { |
|
| 406 | - if (!defined('_PROTEGE_JS_MODELES')) { |
|
| 407 | - include_spip('inc/acces'); |
|
| 408 | - define('_PROTEGE_JS_MODELES', creer_uniqid()); |
|
| 409 | - } |
|
| 410 | - foreach ($r as $regs) { |
|
| 411 | - $t = str_replace($regs[0], code_echappement($regs[0], 'javascript' . _PROTEGE_JS_MODELES), $t); |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - if (preg_match_all(',<\?php.*?($|\?' . '>),isS', $t, $r, PREG_SET_ORDER)) { |
|
| 415 | - if (!defined('_PROTEGE_PHP_MODELES')) { |
|
| 416 | - include_spip('inc/acces'); |
|
| 417 | - define('_PROTEGE_PHP_MODELES', creer_uniqid()); |
|
| 418 | - } |
|
| 419 | - foreach ($r as $regs) { |
|
| 420 | - $t = str_replace($regs[0], code_echappement($regs[0], 'php' . _PROTEGE_PHP_MODELES), $t); |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - return $t; |
|
| 404 | + if (isset($GLOBALS['visiteur_session'])) { |
|
| 405 | + if (preg_match_all(',<script.*?($|</script.),isS', $t, $r, PREG_SET_ORDER)) { |
|
| 406 | + if (!defined('_PROTEGE_JS_MODELES')) { |
|
| 407 | + include_spip('inc/acces'); |
|
| 408 | + define('_PROTEGE_JS_MODELES', creer_uniqid()); |
|
| 409 | + } |
|
| 410 | + foreach ($r as $regs) { |
|
| 411 | + $t = str_replace($regs[0], code_echappement($regs[0], 'javascript' . _PROTEGE_JS_MODELES), $t); |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + if (preg_match_all(',<\?php.*?($|\?' . '>),isS', $t, $r, PREG_SET_ORDER)) { |
|
| 415 | + if (!defined('_PROTEGE_PHP_MODELES')) { |
|
| 416 | + include_spip('inc/acces'); |
|
| 417 | + define('_PROTEGE_PHP_MODELES', creer_uniqid()); |
|
| 418 | + } |
|
| 419 | + foreach ($r as $regs) { |
|
| 420 | + $t = str_replace($regs[0], code_echappement($regs[0], 'php' . _PROTEGE_PHP_MODELES), $t); |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + return $t; |
|
| 426 | 426 | } |
| 427 | 427 | |
| 428 | 428 | |
| 429 | 429 | function echapper_faux_tags($letexte) { |
| 430 | - if (strpos($letexte, '<') === false) { |
|
| 431 | - return $letexte; |
|
| 432 | - } |
|
| 433 | - $textMatches = preg_split(',(</?[a-z!][^<>]*>),', $letexte, null, PREG_SPLIT_DELIM_CAPTURE); |
|
| 434 | - |
|
| 435 | - $letexte = ""; |
|
| 436 | - while (count($textMatches)) { |
|
| 437 | - // un texte a echapper |
|
| 438 | - $letexte .= str_replace("<", '<', array_shift($textMatches)); |
|
| 439 | - // un tag html qui a servit a faite le split |
|
| 440 | - $letexte .= array_shift($textMatches); |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - return $letexte; |
|
| 430 | + if (strpos($letexte, '<') === false) { |
|
| 431 | + return $letexte; |
|
| 432 | + } |
|
| 433 | + $textMatches = preg_split(',(</?[a-z!][^<>]*>),', $letexte, null, PREG_SPLIT_DELIM_CAPTURE); |
|
| 434 | + |
|
| 435 | + $letexte = ""; |
|
| 436 | + while (count($textMatches)) { |
|
| 437 | + // un texte a echapper |
|
| 438 | + $letexte .= str_replace("<", '<', array_shift($textMatches)); |
|
| 439 | + // un tag html qui a servit a faite le split |
|
| 440 | + $letexte .= array_shift($textMatches); |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + return $letexte; |
|
| 444 | 444 | } |
| 445 | 445 | |
| 446 | 446 | /** |
@@ -453,30 +453,30 @@ discard block |
||
| 453 | 453 | * @return string |
| 454 | 454 | */ |
| 455 | 455 | function echapper_html_suspect($texte, $strict=true) { |
| 456 | - if (!$texte |
|
| 457 | - or strpos($texte, '<') === false or strpos($texte, '=') === false) { |
|
| 458 | - return $texte; |
|
| 459 | - } |
|
| 460 | - // quand c'est du texte qui passe par propre on est plus coulant tant qu'il y a pas d'attribut du type onxxx= |
|
| 461 | - // car sinon on declenche sur les modeles ou ressources |
|
| 462 | - if (!$strict and |
|
| 463 | - (strpos($texte,'on') === false or !preg_match(",<\w+.*\bon\w+\s*=,UimsS", $texte)) |
|
| 464 | - ){ |
|
| 465 | - return $texte; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - // on teste sur strlen car safehtml supprime le contenu dangereux |
|
| 469 | - // mais il peut aussi changer des ' en " sur les attributs html, |
|
| 470 | - // donc un test d'egalite est trop strict |
|
| 471 | - if (strlen(safehtml($texte)) !== strlen($texte)) { |
|
| 472 | - $texte = str_replace("<", "<", $texte); |
|
| 473 | - if (!function_exists('attribut_html')) { |
|
| 474 | - include_spip('inc/filtres'); |
|
| 475 | - } |
|
| 476 | - $texte = "<mark class='danger-js' title='".attribut_html(_T('erreur_contenu_suspect'))."'>⚠️</mark> ".$texte; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - return $texte; |
|
| 456 | + if (!$texte |
|
| 457 | + or strpos($texte, '<') === false or strpos($texte, '=') === false) { |
|
| 458 | + return $texte; |
|
| 459 | + } |
|
| 460 | + // quand c'est du texte qui passe par propre on est plus coulant tant qu'il y a pas d'attribut du type onxxx= |
|
| 461 | + // car sinon on declenche sur les modeles ou ressources |
|
| 462 | + if (!$strict and |
|
| 463 | + (strpos($texte,'on') === false or !preg_match(",<\w+.*\bon\w+\s*=,UimsS", $texte)) |
|
| 464 | + ){ |
|
| 465 | + return $texte; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + // on teste sur strlen car safehtml supprime le contenu dangereux |
|
| 469 | + // mais il peut aussi changer des ' en " sur les attributs html, |
|
| 470 | + // donc un test d'egalite est trop strict |
|
| 471 | + if (strlen(safehtml($texte)) !== strlen($texte)) { |
|
| 472 | + $texte = str_replace("<", "<", $texte); |
|
| 473 | + if (!function_exists('attribut_html')) { |
|
| 474 | + include_spip('inc/filtres'); |
|
| 475 | + } |
|
| 476 | + $texte = "<mark class='danger-js' title='".attribut_html(_T('erreur_contenu_suspect'))."'>⚠️</mark> ".$texte; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + return $texte; |
|
| 480 | 480 | } |
| 481 | 481 | |
| 482 | 482 | |
@@ -497,27 +497,27 @@ discard block |
||
| 497 | 497 | * Texte sécurisé |
| 498 | 498 | **/ |
| 499 | 499 | function safehtml($t) { |
| 500 | - static $safehtml; |
|
| 501 | - |
|
| 502 | - if (!$t or !is_string($t)) { |
|
| 503 | - return $t; |
|
| 504 | - } |
|
| 505 | - # attention safehtml nettoie deux ou trois caracteres de plus. A voir |
|
| 506 | - if (strpos($t, '<') === false) { |
|
| 507 | - return str_replace("\x00", '', $t); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - $t = interdire_scripts($t); // jolifier le php |
|
| 511 | - $t = echappe_js($t); |
|
| 512 | - |
|
| 513 | - if (!isset($safehtml)) { |
|
| 514 | - $safehtml = charger_fonction('safehtml', 'inc', true); |
|
| 515 | - } |
|
| 516 | - if ($safehtml) { |
|
| 517 | - $t = $safehtml($t); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - return interdire_scripts($t); // interdire le php (2 precautions) |
|
| 500 | + static $safehtml; |
|
| 501 | + |
|
| 502 | + if (!$t or !is_string($t)) { |
|
| 503 | + return $t; |
|
| 504 | + } |
|
| 505 | + # attention safehtml nettoie deux ou trois caracteres de plus. A voir |
|
| 506 | + if (strpos($t, '<') === false) { |
|
| 507 | + return str_replace("\x00", '', $t); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + $t = interdire_scripts($t); // jolifier le php |
|
| 511 | + $t = echappe_js($t); |
|
| 512 | + |
|
| 513 | + if (!isset($safehtml)) { |
|
| 514 | + $safehtml = charger_fonction('safehtml', 'inc', true); |
|
| 515 | + } |
|
| 516 | + if ($safehtml) { |
|
| 517 | + $t = $safehtml($t); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + return interdire_scripts($t); // interdire le php (2 precautions) |
|
| 521 | 521 | } |
| 522 | 522 | |
| 523 | 523 | |
@@ -539,10 +539,10 @@ discard block |
||
| 539 | 539 | * Texte sans les modèles d'image |
| 540 | 540 | **/ |
| 541 | 541 | function supprime_img($letexte, $message = null) { |
| 542 | - if ($message === null) { |
|
| 543 | - $message = '(' . _T('img_indisponible') . ')'; |
|
| 544 | - } |
|
| 542 | + if ($message === null) { |
|
| 543 | + $message = '(' . _T('img_indisponible') . ')'; |
|
| 544 | + } |
|
| 545 | 545 | |
| 546 | - return preg_replace(',<(img|doc|emb)([0-9]+)(\|([^>]*))?' . '\s*/?' . '>,i', |
|
| 547 | - $message, $letexte); |
|
| 546 | + return preg_replace(',<(img|doc|emb)([0-9]+)(\|([^>]*))?' . '\s*/?' . '>,i', |
|
| 547 | + $message, $letexte); |
|
| 548 | 548 | } |
@@ -16,32 +16,32 @@ discard block |
||
| 16 | 16 | * @package SPIP\Core\Distant |
| 17 | 17 | **/ |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | if (!defined('_INC_DISTANT_VERSION_HTTP')) { |
| 23 | - define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 23 | + define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 24 | 24 | } |
| 25 | 25 | if (!defined('_INC_DISTANT_CONTENT_ENCODING')) { |
| 26 | - define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 26 | + define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 27 | 27 | } |
| 28 | 28 | if (!defined('_INC_DISTANT_USER_AGENT')) { |
| 29 | - define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 29 | + define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 30 | 30 | } |
| 31 | 31 | if (!defined('_INC_DISTANT_MAX_SIZE')) { |
| 32 | - define('_INC_DISTANT_MAX_SIZE', 2097152); |
|
| 32 | + define('_INC_DISTANT_MAX_SIZE', 2097152); |
|
| 33 | 33 | } |
| 34 | 34 | if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) { |
| 35 | - define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 35 | + define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | define('_REGEXP_COPIE_LOCALE', ',' . |
| 39 | - preg_replace( |
|
| 40 | - '@^https?:@', |
|
| 41 | - 'https?:', |
|
| 42 | - (isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '') |
|
| 43 | - ) |
|
| 44 | - . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 39 | + preg_replace( |
|
| 40 | + '@^https?:@', |
|
| 41 | + 'https?:', |
|
| 42 | + (isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '') |
|
| 43 | + ) |
|
| 44 | + . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 45 | 45 | |
| 46 | 46 | //@define('_COPIE_LOCALE_MAX_SIZE',2097152); // poids (inc/utils l'a fait) |
| 47 | 47 | |
@@ -68,77 +68,77 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | function copie_locale($source, $mode = 'auto', $local = null, $taille_max = null) { |
| 70 | 70 | |
| 71 | - // si c'est la protection de soi-meme, retourner le path |
|
| 72 | - if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 73 | - $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]); |
|
| 74 | - |
|
| 75 | - return @file_exists($source) ? $source : false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - if (is_null($local)) { |
|
| 79 | - $local = fichier_copie_locale($source); |
|
| 80 | - } else { |
|
| 81 | - if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) { |
|
| 82 | - $local = substr($local, strlen(_DIR_RACINE)); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 87 | - // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 88 | - // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 89 | - if (!$local) { |
|
| 90 | - return false; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $localrac = _DIR_RACINE . $local; |
|
| 94 | - $t = ($mode == 'force') ? false : @file_exists($localrac); |
|
| 95 | - |
|
| 96 | - // test d'existence du fichier |
|
| 97 | - if ($mode == 'test') { |
|
| 98 | - return $t ? $local : ''; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // sinon voir si on doit/peut le telecharger |
|
| 102 | - if ($local == $source or !tester_url_absolue($source)) { |
|
| 103 | - return $local; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if ($mode == 'modif' or !$t) { |
|
| 107 | - // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 108 | - // et des eventuelles recuperations concurantes |
|
| 109 | - include_spip('inc/acces'); |
|
| 110 | - if (!$taille_max) { |
|
| 111 | - $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 112 | - } |
|
| 113 | - $res = recuperer_url( |
|
| 114 | - $source, |
|
| 115 | - array('file' => $localrac, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '') |
|
| 116 | - ); |
|
| 117 | - if (!$res or (!$res['length'] and $res['status'] != 304)) { |
|
| 118 | - spip_log("copie_locale : Echec recuperation $source sur $localrac status : " . $res['status'], _LOG_INFO_IMPORTANTE); |
|
| 119 | - } |
|
| 120 | - if (!$res['length']) { |
|
| 121 | - // si $t c'est sans doute juste un not-modified-since |
|
| 122 | - return $t ? $local : false; |
|
| 123 | - } |
|
| 124 | - spip_log("copie_locale : recuperation $source sur $localrac taille " . $res['length'] . ' OK'); |
|
| 125 | - |
|
| 126 | - // pour une eventuelle indexation |
|
| 127 | - pipeline( |
|
| 128 | - 'post_edition', |
|
| 129 | - array( |
|
| 130 | - 'args' => array( |
|
| 131 | - 'operation' => 'copie_locale', |
|
| 132 | - 'source' => $source, |
|
| 133 | - 'fichier' => $local, |
|
| 134 | - 'http_res' => $res['length'], |
|
| 135 | - ), |
|
| 136 | - 'data' => null |
|
| 137 | - ) |
|
| 138 | - ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - return $local; |
|
| 71 | + // si c'est la protection de soi-meme, retourner le path |
|
| 72 | + if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 73 | + $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]); |
|
| 74 | + |
|
| 75 | + return @file_exists($source) ? $source : false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + if (is_null($local)) { |
|
| 79 | + $local = fichier_copie_locale($source); |
|
| 80 | + } else { |
|
| 81 | + if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) { |
|
| 82 | + $local = substr($local, strlen(_DIR_RACINE)); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 87 | + // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 88 | + // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 89 | + if (!$local) { |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $localrac = _DIR_RACINE . $local; |
|
| 94 | + $t = ($mode == 'force') ? false : @file_exists($localrac); |
|
| 95 | + |
|
| 96 | + // test d'existence du fichier |
|
| 97 | + if ($mode == 'test') { |
|
| 98 | + return $t ? $local : ''; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // sinon voir si on doit/peut le telecharger |
|
| 102 | + if ($local == $source or !tester_url_absolue($source)) { |
|
| 103 | + return $local; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if ($mode == 'modif' or !$t) { |
|
| 107 | + // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 108 | + // et des eventuelles recuperations concurantes |
|
| 109 | + include_spip('inc/acces'); |
|
| 110 | + if (!$taille_max) { |
|
| 111 | + $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 112 | + } |
|
| 113 | + $res = recuperer_url( |
|
| 114 | + $source, |
|
| 115 | + array('file' => $localrac, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '') |
|
| 116 | + ); |
|
| 117 | + if (!$res or (!$res['length'] and $res['status'] != 304)) { |
|
| 118 | + spip_log("copie_locale : Echec recuperation $source sur $localrac status : " . $res['status'], _LOG_INFO_IMPORTANTE); |
|
| 119 | + } |
|
| 120 | + if (!$res['length']) { |
|
| 121 | + // si $t c'est sans doute juste un not-modified-since |
|
| 122 | + return $t ? $local : false; |
|
| 123 | + } |
|
| 124 | + spip_log("copie_locale : recuperation $source sur $localrac taille " . $res['length'] . ' OK'); |
|
| 125 | + |
|
| 126 | + // pour une eventuelle indexation |
|
| 127 | + pipeline( |
|
| 128 | + 'post_edition', |
|
| 129 | + array( |
|
| 130 | + 'args' => array( |
|
| 131 | + 'operation' => 'copie_locale', |
|
| 132 | + 'source' => $source, |
|
| 133 | + 'fichier' => $local, |
|
| 134 | + 'http_res' => $res['length'], |
|
| 135 | + ), |
|
| 136 | + 'data' => null |
|
| 137 | + ) |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + return $local; |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | /** |
@@ -153,88 +153,88 @@ discard block |
||
| 153 | 153 | * url ou false en cas d'echec |
| 154 | 154 | */ |
| 155 | 155 | function valider_url_distante($url, $known_hosts = array()) { |
| 156 | - if (!function_exists('protocole_verifier')){ |
|
| 157 | - include_spip('inc/filtres_mini'); |
|
| 158 | - } |
|
| 156 | + if (!function_exists('protocole_verifier')){ |
|
| 157 | + include_spip('inc/filtres_mini'); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - if (!protocole_verifier($url, array('http', 'https'))) { |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 160 | + if (!protocole_verifier($url, array('http', 'https'))) { |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - $parsed_url = parse_url($url); |
|
| 165 | - if (!$parsed_url or empty($parsed_url['host']) ) { |
|
| 166 | - return false; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { |
|
| 170 | - return false; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if (!is_array($known_hosts)) { |
|
| 178 | - $known_hosts = array($known_hosts); |
|
| 179 | - } |
|
| 180 | - $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 181 | - $known_hosts[] = url_de_base(); |
|
| 182 | - $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 183 | - |
|
| 184 | - $is_known_host = false; |
|
| 185 | - foreach ($known_hosts as $known_host) { |
|
| 186 | - $parse_known = parse_url($known_host); |
|
| 187 | - if ($parse_known |
|
| 188 | - and strtolower($parse_known['host']) === strtolower($parsed_url['host'])) { |
|
| 189 | - $is_known_host = true; |
|
| 190 | - break; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - if (!$is_known_host) { |
|
| 195 | - $host = trim($parsed_url['host'], '.'); |
|
| 196 | - if (preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host)) { |
|
| 197 | - $ip = $host; |
|
| 198 | - } else { |
|
| 199 | - $ip = gethostbyname($host); |
|
| 200 | - if ($ip === $host) { |
|
| 201 | - // Error condition for gethostbyname() |
|
| 202 | - $ip = false; |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - if ($ip) { |
|
| 206 | - $parts = array_map('intval', explode( '.', $ip )); |
|
| 207 | - if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0] |
|
| 208 | - or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] ) |
|
| 209 | - or ( 192 === $parts[0] && 168 === $parts[1] ) |
|
| 210 | - ) { |
|
| 211 | - return false; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - if (empty($parsed_url['port'])) { |
|
| 217 | - return $url; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - $port = $parsed_url['port']; |
|
| 221 | - if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 222 | - return $url; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - if ($is_known_host) { |
|
| 226 | - foreach ($known_hosts as $known_host) { |
|
| 227 | - $parse_known = parse_url($known_host); |
|
| 228 | - if ($parse_known |
|
| 229 | - and !empty($parse_known['port']) |
|
| 230 | - and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 231 | - and $parse_known['port'] == $port) { |
|
| 232 | - return $url; |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return false; |
|
| 164 | + $parsed_url = parse_url($url); |
|
| 165 | + if (!$parsed_url or empty($parsed_url['host']) ) { |
|
| 166 | + return false; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { |
|
| 170 | + return false; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if (!is_array($known_hosts)) { |
|
| 178 | + $known_hosts = array($known_hosts); |
|
| 179 | + } |
|
| 180 | + $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 181 | + $known_hosts[] = url_de_base(); |
|
| 182 | + $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 183 | + |
|
| 184 | + $is_known_host = false; |
|
| 185 | + foreach ($known_hosts as $known_host) { |
|
| 186 | + $parse_known = parse_url($known_host); |
|
| 187 | + if ($parse_known |
|
| 188 | + and strtolower($parse_known['host']) === strtolower($parsed_url['host'])) { |
|
| 189 | + $is_known_host = true; |
|
| 190 | + break; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + if (!$is_known_host) { |
|
| 195 | + $host = trim($parsed_url['host'], '.'); |
|
| 196 | + if (preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host)) { |
|
| 197 | + $ip = $host; |
|
| 198 | + } else { |
|
| 199 | + $ip = gethostbyname($host); |
|
| 200 | + if ($ip === $host) { |
|
| 201 | + // Error condition for gethostbyname() |
|
| 202 | + $ip = false; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + if ($ip) { |
|
| 206 | + $parts = array_map('intval', explode( '.', $ip )); |
|
| 207 | + if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0] |
|
| 208 | + or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] ) |
|
| 209 | + or ( 192 === $parts[0] && 168 === $parts[1] ) |
|
| 210 | + ) { |
|
| 211 | + return false; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + if (empty($parsed_url['port'])) { |
|
| 217 | + return $url; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + $port = $parsed_url['port']; |
|
| 221 | + if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 222 | + return $url; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + if ($is_known_host) { |
|
| 226 | + foreach ($known_hosts as $known_host) { |
|
| 227 | + $parse_known = parse_url($known_host); |
|
| 228 | + if ($parse_known |
|
| 229 | + and !empty($parse_known['port']) |
|
| 230 | + and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 231 | + and $parse_known['port'] == $port) { |
|
| 232 | + return $url; |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return false; |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | /** |
@@ -253,86 +253,86 @@ discard block |
||
| 253 | 253 | */ |
| 254 | 254 | function prepare_donnees_post($donnees, $boundary = '') { |
| 255 | 255 | |
| 256 | - // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 257 | - // pour un appel soap par exemple |
|
| 258 | - // l'entete est separe des donnees par un double retour a la ligne |
|
| 259 | - // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 260 | - if (is_string($donnees) && strlen($donnees)) { |
|
| 261 | - $entete = ''; |
|
| 262 | - // on repasse tous les \r\n et \r en simples \n |
|
| 263 | - $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 264 | - $donnees = str_replace("\r", "\n", $donnees); |
|
| 265 | - // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 266 | - $p = strpos($donnees, "\n\n"); |
|
| 267 | - if ($p !== false) { |
|
| 268 | - $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 269 | - $donnees = substr($donnees, $p + 2); |
|
| 270 | - } |
|
| 271 | - $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 272 | - } else { |
|
| 273 | - /* boundary automatique */ |
|
| 274 | - // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 275 | - if ($boundary === '') { |
|
| 276 | - $taille = 0; |
|
| 277 | - foreach ($donnees as $cle => $valeur) { |
|
| 278 | - if (is_array($valeur)) { |
|
| 279 | - foreach ($valeur as $val2) { |
|
| 280 | - $taille += strlen($val2); |
|
| 281 | - } |
|
| 282 | - } else { |
|
| 283 | - // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 284 | - $taille += strlen($valeur); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - if ($taille > 500) { |
|
| 288 | - $boundary = substr(md5(rand() . 'spip'), 0, 8); |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if (is_string($boundary) and strlen($boundary)) { |
|
| 293 | - // fabrique une chaine HTTP pour un POST avec boundary |
|
| 294 | - $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 295 | - $chaine = ''; |
|
| 296 | - if (is_array($donnees)) { |
|
| 297 | - foreach ($donnees as $cle => $valeur) { |
|
| 298 | - if (is_array($valeur)) { |
|
| 299 | - foreach ($valeur as $val2) { |
|
| 300 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 301 | - $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 302 | - $chaine .= "\r\n"; |
|
| 303 | - $chaine .= $val2; |
|
| 304 | - } |
|
| 305 | - } else { |
|
| 306 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 307 | - $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 308 | - $chaine .= "\r\n"; |
|
| 309 | - $chaine .= $valeur; |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 313 | - } |
|
| 314 | - } else { |
|
| 315 | - // fabrique une chaine HTTP simple pour un POST |
|
| 316 | - $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; |
|
| 317 | - $chaine = array(); |
|
| 318 | - if (is_array($donnees)) { |
|
| 319 | - foreach ($donnees as $cle => $valeur) { |
|
| 320 | - if (is_array($valeur)) { |
|
| 321 | - foreach ($valeur as $val2) { |
|
| 322 | - $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2); |
|
| 323 | - } |
|
| 324 | - } else { |
|
| 325 | - $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur); |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - $chaine = implode('&', $chaine); |
|
| 329 | - } else { |
|
| 330 | - $chaine = $donnees; |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - return array($entete, $chaine); |
|
| 256 | + // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 257 | + // pour un appel soap par exemple |
|
| 258 | + // l'entete est separe des donnees par un double retour a la ligne |
|
| 259 | + // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 260 | + if (is_string($donnees) && strlen($donnees)) { |
|
| 261 | + $entete = ''; |
|
| 262 | + // on repasse tous les \r\n et \r en simples \n |
|
| 263 | + $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 264 | + $donnees = str_replace("\r", "\n", $donnees); |
|
| 265 | + // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 266 | + $p = strpos($donnees, "\n\n"); |
|
| 267 | + if ($p !== false) { |
|
| 268 | + $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 269 | + $donnees = substr($donnees, $p + 2); |
|
| 270 | + } |
|
| 271 | + $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 272 | + } else { |
|
| 273 | + /* boundary automatique */ |
|
| 274 | + // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 275 | + if ($boundary === '') { |
|
| 276 | + $taille = 0; |
|
| 277 | + foreach ($donnees as $cle => $valeur) { |
|
| 278 | + if (is_array($valeur)) { |
|
| 279 | + foreach ($valeur as $val2) { |
|
| 280 | + $taille += strlen($val2); |
|
| 281 | + } |
|
| 282 | + } else { |
|
| 283 | + // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 284 | + $taille += strlen($valeur); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + if ($taille > 500) { |
|
| 288 | + $boundary = substr(md5(rand() . 'spip'), 0, 8); |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if (is_string($boundary) and strlen($boundary)) { |
|
| 293 | + // fabrique une chaine HTTP pour un POST avec boundary |
|
| 294 | + $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 295 | + $chaine = ''; |
|
| 296 | + if (is_array($donnees)) { |
|
| 297 | + foreach ($donnees as $cle => $valeur) { |
|
| 298 | + if (is_array($valeur)) { |
|
| 299 | + foreach ($valeur as $val2) { |
|
| 300 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 301 | + $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 302 | + $chaine .= "\r\n"; |
|
| 303 | + $chaine .= $val2; |
|
| 304 | + } |
|
| 305 | + } else { |
|
| 306 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 307 | + $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 308 | + $chaine .= "\r\n"; |
|
| 309 | + $chaine .= $valeur; |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 313 | + } |
|
| 314 | + } else { |
|
| 315 | + // fabrique une chaine HTTP simple pour un POST |
|
| 316 | + $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; |
|
| 317 | + $chaine = array(); |
|
| 318 | + if (is_array($donnees)) { |
|
| 319 | + foreach ($donnees as $cle => $valeur) { |
|
| 320 | + if (is_array($valeur)) { |
|
| 321 | + foreach ($valeur as $val2) { |
|
| 322 | + $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2); |
|
| 323 | + } |
|
| 324 | + } else { |
|
| 325 | + $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur); |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + $chaine = implode('&', $chaine); |
|
| 329 | + } else { |
|
| 330 | + $chaine = $donnees; |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + return array($entete, $chaine); |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | /** |
@@ -346,18 +346,18 @@ discard block |
||
| 346 | 346 | */ |
| 347 | 347 | function url_to_ascii($url_idn) { |
| 348 | 348 | |
| 349 | - if ($parts = parse_url($url_idn)) { |
|
| 350 | - $host = $parts['host']; |
|
| 351 | - if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 352 | - include_spip('inc/idna_convert.class'); |
|
| 353 | - $IDN = new idna_convert(); |
|
| 354 | - $host_ascii = $IDN->encode($host); |
|
| 355 | - $url_idn = explode($host, $url_idn, 2); |
|
| 356 | - $url_idn = implode($host_ascii, $url_idn); |
|
| 357 | - } |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - return $url_idn; |
|
| 349 | + if ($parts = parse_url($url_idn)) { |
|
| 350 | + $host = $parts['host']; |
|
| 351 | + if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 352 | + include_spip('inc/idna_convert.class'); |
|
| 353 | + $IDN = new idna_convert(); |
|
| 354 | + $host_ascii = $IDN->encode($host); |
|
| 355 | + $url_idn = explode($host, $url_idn, 2); |
|
| 356 | + $url_idn = implode($host_ascii, $url_idn); |
|
| 357 | + } |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + return $url_idn; |
|
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | /** |
@@ -396,172 +396,172 @@ discard block |
||
| 396 | 396 | * string file : nom du fichier si enregistre dans un fichier |
| 397 | 397 | */ |
| 398 | 398 | function recuperer_url($url, $options = array()) { |
| 399 | - $default = array( |
|
| 400 | - 'transcoder' => false, |
|
| 401 | - 'methode' => 'GET', |
|
| 402 | - 'taille_max' => null, |
|
| 403 | - 'datas' => '', |
|
| 404 | - 'boundary' => '', |
|
| 405 | - 'refuser_gz' => false, |
|
| 406 | - 'if_modified_since' => '', |
|
| 407 | - 'uri_referer' => '', |
|
| 408 | - 'file' => '', |
|
| 409 | - 'follow_location' => 10, |
|
| 410 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 411 | - ); |
|
| 412 | - $options = array_merge($default, $options); |
|
| 413 | - // copier directement dans un fichier ? |
|
| 414 | - $copy = $options['file']; |
|
| 415 | - |
|
| 416 | - if ($options['methode'] == 'HEAD') { |
|
| 417 | - $options['taille_max'] = 0; |
|
| 418 | - } |
|
| 419 | - if (is_null($options['taille_max'])) { |
|
| 420 | - $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - if (!empty($options['datas'])) { |
|
| 424 | - list($head, $postdata) = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 425 | - if (stripos($head, 'Content-Length:') === false) { |
|
| 426 | - $head .= 'Content-Length: ' . strlen($postdata); |
|
| 427 | - } |
|
| 428 | - $options['datas'] = $head . "\r\n\r\n" . $postdata; |
|
| 429 | - if (strlen($postdata)) { |
|
| 430 | - $options['methode'] = 'POST'; |
|
| 431 | - } |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 435 | - $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 436 | - if (!tester_url_absolue($url)) { |
|
| 437 | - $url = 'http://' . $url; |
|
| 438 | - } elseif (strncmp($url, '//', 2) == 0) { |
|
| 439 | - $url = 'http:' . $url; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - $url = url_to_ascii($url); |
|
| 443 | - |
|
| 444 | - $result = array( |
|
| 445 | - 'status' => 0, |
|
| 446 | - 'headers' => '', |
|
| 447 | - 'page' => '', |
|
| 448 | - 'length' => 0, |
|
| 449 | - 'last_modified' => '', |
|
| 450 | - 'location' => '', |
|
| 451 | - 'url' => $url |
|
| 452 | - ); |
|
| 453 | - |
|
| 454 | - // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 455 | - $refuser_gz = (($options['refuser_gz'] or $copy) ? true : false); |
|
| 456 | - |
|
| 457 | - // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 458 | - list($handle, $fopen) = init_http( |
|
| 459 | - $options['methode'], |
|
| 460 | - $url, |
|
| 461 | - $refuser_gz, |
|
| 462 | - $options['uri_referer'], |
|
| 463 | - $options['datas'], |
|
| 464 | - $options['version_http'], |
|
| 465 | - $options['if_modified_since'] |
|
| 466 | - ); |
|
| 467 | - if (!$handle) { |
|
| 468 | - spip_log("ECHEC init_http $url"); |
|
| 469 | - |
|
| 470 | - return false; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - // Sauf en fopen, envoyer le flux d'entree |
|
| 474 | - // et recuperer les en-tetes de reponses |
|
| 475 | - if (!$fopen) { |
|
| 476 | - $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 477 | - if (!$res) { |
|
| 478 | - fclose($handle); |
|
| 479 | - $t = @parse_url($url); |
|
| 480 | - $host = $t['host']; |
|
| 481 | - // Chinoisierie inexplicable pour contrer |
|
| 482 | - // les actions liberticides de l'empire du milieu |
|
| 483 | - if (!need_proxy($host) |
|
| 484 | - and $res = @file_get_contents($url) |
|
| 485 | - ) { |
|
| 486 | - $result['length'] = strlen($res); |
|
| 487 | - if ($copy) { |
|
| 488 | - ecrire_fichier($copy, $res); |
|
| 489 | - $result['file'] = $copy; |
|
| 490 | - } else { |
|
| 491 | - $result['page'] = $res; |
|
| 492 | - } |
|
| 493 | - $res = array( |
|
| 494 | - 'status' => 200, |
|
| 495 | - ); |
|
| 496 | - } else { |
|
| 497 | - return false; |
|
| 498 | - } |
|
| 499 | - } elseif ($res['location'] and $options['follow_location']) { |
|
| 500 | - $options['follow_location']--; |
|
| 501 | - fclose($handle); |
|
| 502 | - include_spip('inc/filtres'); |
|
| 503 | - $url = suivre_lien($url, $res['location']); |
|
| 504 | - spip_log("recuperer_url recommence sur $url"); |
|
| 505 | - |
|
| 506 | - return recuperer_url($url, $options); |
|
| 507 | - } elseif ($res['status'] !== 200) { |
|
| 508 | - spip_log('HTTP status ' . $res['status'] . " pour $url"); |
|
| 509 | - } |
|
| 510 | - $result['status'] = $res['status']; |
|
| 511 | - if (isset($res['headers'])) { |
|
| 512 | - $result['headers'] = $res['headers']; |
|
| 513 | - } |
|
| 514 | - if (isset($res['last_modified'])) { |
|
| 515 | - $result['last_modified'] = $res['last_modified']; |
|
| 516 | - } |
|
| 517 | - if (isset($res['location'])) { |
|
| 518 | - $result['location'] = $res['location']; |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - // on ne veut que les entetes |
|
| 523 | - if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') { |
|
| 524 | - return $result; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - |
|
| 528 | - // s'il faut deballer, le faire via un fichier temporaire |
|
| 529 | - // sinon la memoire explose pour les gros flux |
|
| 530 | - |
|
| 531 | - $gz = false; |
|
| 532 | - if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) { |
|
| 533 | - $gz = (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz'); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - // si on a pas deja recuperer le contenu par une methode detournee |
|
| 537 | - if (!$result['length']) { |
|
| 538 | - $res = recuperer_body($handle, $options['taille_max'], $gz ? $gz : $copy); |
|
| 539 | - fclose($handle); |
|
| 540 | - if ($copy) { |
|
| 541 | - $result['length'] = $res; |
|
| 542 | - $result['file'] = $copy; |
|
| 543 | - } elseif ($res) { |
|
| 544 | - $result['page'] = &$res; |
|
| 545 | - $result['length'] = strlen($result['page']); |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - if (!$result['page']) { |
|
| 549 | - return $result; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - // Decompresser au besoin |
|
| 553 | - if ($gz) { |
|
| 554 | - $result['page'] = implode('', gzfile($gz)); |
|
| 555 | - supprimer_fichier($gz); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - // Faut-il l'importer dans notre charset local ? |
|
| 559 | - if ($options['transcoder']) { |
|
| 560 | - include_spip('inc/charsets'); |
|
| 561 | - $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - return $result; |
|
| 399 | + $default = array( |
|
| 400 | + 'transcoder' => false, |
|
| 401 | + 'methode' => 'GET', |
|
| 402 | + 'taille_max' => null, |
|
| 403 | + 'datas' => '', |
|
| 404 | + 'boundary' => '', |
|
| 405 | + 'refuser_gz' => false, |
|
| 406 | + 'if_modified_since' => '', |
|
| 407 | + 'uri_referer' => '', |
|
| 408 | + 'file' => '', |
|
| 409 | + 'follow_location' => 10, |
|
| 410 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 411 | + ); |
|
| 412 | + $options = array_merge($default, $options); |
|
| 413 | + // copier directement dans un fichier ? |
|
| 414 | + $copy = $options['file']; |
|
| 415 | + |
|
| 416 | + if ($options['methode'] == 'HEAD') { |
|
| 417 | + $options['taille_max'] = 0; |
|
| 418 | + } |
|
| 419 | + if (is_null($options['taille_max'])) { |
|
| 420 | + $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + if (!empty($options['datas'])) { |
|
| 424 | + list($head, $postdata) = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 425 | + if (stripos($head, 'Content-Length:') === false) { |
|
| 426 | + $head .= 'Content-Length: ' . strlen($postdata); |
|
| 427 | + } |
|
| 428 | + $options['datas'] = $head . "\r\n\r\n" . $postdata; |
|
| 429 | + if (strlen($postdata)) { |
|
| 430 | + $options['methode'] = 'POST'; |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 435 | + $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 436 | + if (!tester_url_absolue($url)) { |
|
| 437 | + $url = 'http://' . $url; |
|
| 438 | + } elseif (strncmp($url, '//', 2) == 0) { |
|
| 439 | + $url = 'http:' . $url; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + $url = url_to_ascii($url); |
|
| 443 | + |
|
| 444 | + $result = array( |
|
| 445 | + 'status' => 0, |
|
| 446 | + 'headers' => '', |
|
| 447 | + 'page' => '', |
|
| 448 | + 'length' => 0, |
|
| 449 | + 'last_modified' => '', |
|
| 450 | + 'location' => '', |
|
| 451 | + 'url' => $url |
|
| 452 | + ); |
|
| 453 | + |
|
| 454 | + // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 455 | + $refuser_gz = (($options['refuser_gz'] or $copy) ? true : false); |
|
| 456 | + |
|
| 457 | + // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 458 | + list($handle, $fopen) = init_http( |
|
| 459 | + $options['methode'], |
|
| 460 | + $url, |
|
| 461 | + $refuser_gz, |
|
| 462 | + $options['uri_referer'], |
|
| 463 | + $options['datas'], |
|
| 464 | + $options['version_http'], |
|
| 465 | + $options['if_modified_since'] |
|
| 466 | + ); |
|
| 467 | + if (!$handle) { |
|
| 468 | + spip_log("ECHEC init_http $url"); |
|
| 469 | + |
|
| 470 | + return false; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + // Sauf en fopen, envoyer le flux d'entree |
|
| 474 | + // et recuperer les en-tetes de reponses |
|
| 475 | + if (!$fopen) { |
|
| 476 | + $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 477 | + if (!$res) { |
|
| 478 | + fclose($handle); |
|
| 479 | + $t = @parse_url($url); |
|
| 480 | + $host = $t['host']; |
|
| 481 | + // Chinoisierie inexplicable pour contrer |
|
| 482 | + // les actions liberticides de l'empire du milieu |
|
| 483 | + if (!need_proxy($host) |
|
| 484 | + and $res = @file_get_contents($url) |
|
| 485 | + ) { |
|
| 486 | + $result['length'] = strlen($res); |
|
| 487 | + if ($copy) { |
|
| 488 | + ecrire_fichier($copy, $res); |
|
| 489 | + $result['file'] = $copy; |
|
| 490 | + } else { |
|
| 491 | + $result['page'] = $res; |
|
| 492 | + } |
|
| 493 | + $res = array( |
|
| 494 | + 'status' => 200, |
|
| 495 | + ); |
|
| 496 | + } else { |
|
| 497 | + return false; |
|
| 498 | + } |
|
| 499 | + } elseif ($res['location'] and $options['follow_location']) { |
|
| 500 | + $options['follow_location']--; |
|
| 501 | + fclose($handle); |
|
| 502 | + include_spip('inc/filtres'); |
|
| 503 | + $url = suivre_lien($url, $res['location']); |
|
| 504 | + spip_log("recuperer_url recommence sur $url"); |
|
| 505 | + |
|
| 506 | + return recuperer_url($url, $options); |
|
| 507 | + } elseif ($res['status'] !== 200) { |
|
| 508 | + spip_log('HTTP status ' . $res['status'] . " pour $url"); |
|
| 509 | + } |
|
| 510 | + $result['status'] = $res['status']; |
|
| 511 | + if (isset($res['headers'])) { |
|
| 512 | + $result['headers'] = $res['headers']; |
|
| 513 | + } |
|
| 514 | + if (isset($res['last_modified'])) { |
|
| 515 | + $result['last_modified'] = $res['last_modified']; |
|
| 516 | + } |
|
| 517 | + if (isset($res['location'])) { |
|
| 518 | + $result['location'] = $res['location']; |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + // on ne veut que les entetes |
|
| 523 | + if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') { |
|
| 524 | + return $result; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + |
|
| 528 | + // s'il faut deballer, le faire via un fichier temporaire |
|
| 529 | + // sinon la memoire explose pour les gros flux |
|
| 530 | + |
|
| 531 | + $gz = false; |
|
| 532 | + if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) { |
|
| 533 | + $gz = (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz'); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + // si on a pas deja recuperer le contenu par une methode detournee |
|
| 537 | + if (!$result['length']) { |
|
| 538 | + $res = recuperer_body($handle, $options['taille_max'], $gz ? $gz : $copy); |
|
| 539 | + fclose($handle); |
|
| 540 | + if ($copy) { |
|
| 541 | + $result['length'] = $res; |
|
| 542 | + $result['file'] = $copy; |
|
| 543 | + } elseif ($res) { |
|
| 544 | + $result['page'] = &$res; |
|
| 545 | + $result['length'] = strlen($result['page']); |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + if (!$result['page']) { |
|
| 549 | + return $result; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + // Decompresser au besoin |
|
| 553 | + if ($gz) { |
|
| 554 | + $result['page'] = implode('', gzfile($gz)); |
|
| 555 | + supprimer_fichier($gz); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + // Faut-il l'importer dans notre charset local ? |
|
| 559 | + if ($options['transcoder']) { |
|
| 560 | + include_spip('inc/charsets'); |
|
| 561 | + $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + return $result; |
|
| 565 | 565 | } |
| 566 | 566 | |
| 567 | 567 | /** |
@@ -577,72 +577,72 @@ discard block |
||
| 577 | 577 | * @return array|bool|mixed |
| 578 | 578 | */ |
| 579 | 579 | function recuperer_url_cache($url, $options = array()) { |
| 580 | - if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 581 | - define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 582 | - } |
|
| 583 | - $default = array( |
|
| 584 | - 'transcoder' => false, |
|
| 585 | - 'methode' => 'GET', |
|
| 586 | - 'taille_max' => null, |
|
| 587 | - 'datas' => '', |
|
| 588 | - 'boundary' => '', |
|
| 589 | - 'refuser_gz' => false, |
|
| 590 | - 'if_modified_since' => '', |
|
| 591 | - 'uri_referer' => '', |
|
| 592 | - 'file' => '', |
|
| 593 | - 'follow_location' => 10, |
|
| 594 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 595 | - 'delai_cache' => _DELAI_RECUPERER_URL_CACHE, |
|
| 596 | - ); |
|
| 597 | - $options = array_merge($default, $options); |
|
| 598 | - |
|
| 599 | - // cas ou il n'est pas possible de cacher |
|
| 600 | - if (!empty($options['data']) or $options['methode'] == 'POST') { |
|
| 601 | - return recuperer_url($url, $options); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 605 | - static $errors = array(); |
|
| 606 | - if (isset($errors[$url])) { |
|
| 607 | - return $errors[$url]; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - $sig = $options; |
|
| 611 | - unset($sig['if_modified_since']); |
|
| 612 | - unset($sig['delai_cache']); |
|
| 613 | - $sig['url'] = $url; |
|
| 614 | - |
|
| 615 | - $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 616 | - $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 617 | - $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 618 | - $cache = "$sub$cache"; |
|
| 619 | - |
|
| 620 | - $res = false; |
|
| 621 | - $is_cached = file_exists($cache); |
|
| 622 | - if ($is_cached |
|
| 623 | - and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']) |
|
| 624 | - ) { |
|
| 625 | - lire_fichier($cache, $res); |
|
| 626 | - if ($res = unserialize($res)) { |
|
| 627 | - // mettre le last_modified et le status=304 ? |
|
| 628 | - } |
|
| 629 | - } |
|
| 630 | - if (!$res) { |
|
| 631 | - $res = recuperer_url($url, $options); |
|
| 632 | - // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 633 | - if (!$res) { |
|
| 634 | - if ($is_cached) { |
|
| 635 | - // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 636 | - lire_fichier($cache, $res); |
|
| 637 | - $res = unserialize($res); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - return $errors[$url] = $res; |
|
| 641 | - } |
|
| 642 | - ecrire_fichier($cache, serialize($res)); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - return $res; |
|
| 580 | + if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 581 | + define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 582 | + } |
|
| 583 | + $default = array( |
|
| 584 | + 'transcoder' => false, |
|
| 585 | + 'methode' => 'GET', |
|
| 586 | + 'taille_max' => null, |
|
| 587 | + 'datas' => '', |
|
| 588 | + 'boundary' => '', |
|
| 589 | + 'refuser_gz' => false, |
|
| 590 | + 'if_modified_since' => '', |
|
| 591 | + 'uri_referer' => '', |
|
| 592 | + 'file' => '', |
|
| 593 | + 'follow_location' => 10, |
|
| 594 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 595 | + 'delai_cache' => _DELAI_RECUPERER_URL_CACHE, |
|
| 596 | + ); |
|
| 597 | + $options = array_merge($default, $options); |
|
| 598 | + |
|
| 599 | + // cas ou il n'est pas possible de cacher |
|
| 600 | + if (!empty($options['data']) or $options['methode'] == 'POST') { |
|
| 601 | + return recuperer_url($url, $options); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 605 | + static $errors = array(); |
|
| 606 | + if (isset($errors[$url])) { |
|
| 607 | + return $errors[$url]; |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + $sig = $options; |
|
| 611 | + unset($sig['if_modified_since']); |
|
| 612 | + unset($sig['delai_cache']); |
|
| 613 | + $sig['url'] = $url; |
|
| 614 | + |
|
| 615 | + $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 616 | + $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 617 | + $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 618 | + $cache = "$sub$cache"; |
|
| 619 | + |
|
| 620 | + $res = false; |
|
| 621 | + $is_cached = file_exists($cache); |
|
| 622 | + if ($is_cached |
|
| 623 | + and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']) |
|
| 624 | + ) { |
|
| 625 | + lire_fichier($cache, $res); |
|
| 626 | + if ($res = unserialize($res)) { |
|
| 627 | + // mettre le last_modified et le status=304 ? |
|
| 628 | + } |
|
| 629 | + } |
|
| 630 | + if (!$res) { |
|
| 631 | + $res = recuperer_url($url, $options); |
|
| 632 | + // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 633 | + if (!$res) { |
|
| 634 | + if ($is_cached) { |
|
| 635 | + // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 636 | + lire_fichier($cache, $res); |
|
| 637 | + $res = unserialize($res); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + return $errors[$url] = $res; |
|
| 641 | + } |
|
| 642 | + ecrire_fichier($cache, serialize($res)); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + return $res; |
|
| 646 | 646 | } |
| 647 | 647 | |
| 648 | 648 | /** |
@@ -680,52 +680,52 @@ discard block |
||
| 680 | 680 | * - false si la page n'a pu être récupérée (status different de 200) |
| 681 | 681 | **/ |
| 682 | 682 | function recuperer_page( |
| 683 | - $url, |
|
| 684 | - $trans = false, |
|
| 685 | - $get_headers = false, |
|
| 686 | - $taille_max = null, |
|
| 687 | - $datas = '', |
|
| 688 | - $boundary = '', |
|
| 689 | - $refuser_gz = false, |
|
| 690 | - $date_verif = '', |
|
| 691 | - $uri_referer = '' |
|
| 683 | + $url, |
|
| 684 | + $trans = false, |
|
| 685 | + $get_headers = false, |
|
| 686 | + $taille_max = null, |
|
| 687 | + $datas = '', |
|
| 688 | + $boundary = '', |
|
| 689 | + $refuser_gz = false, |
|
| 690 | + $date_verif = '', |
|
| 691 | + $uri_referer = '' |
|
| 692 | 692 | ) { |
| 693 | - // $copy = copier le fichier ? |
|
| 694 | - $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-) |
|
| 695 | - |
|
| 696 | - if (!is_null($taille_max) and ($taille_max == 0)) { |
|
| 697 | - $get = 'HEAD'; |
|
| 698 | - } else { |
|
| 699 | - $get = 'GET'; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - $options = array( |
|
| 703 | - 'transcoder' => $trans === true, |
|
| 704 | - 'methode' => $get, |
|
| 705 | - 'datas' => $datas, |
|
| 706 | - 'boundary' => $boundary, |
|
| 707 | - 'refuser_gz' => $refuser_gz, |
|
| 708 | - 'if_modified_since' => $date_verif, |
|
| 709 | - 'uri_referer' => $uri_referer, |
|
| 710 | - 'file' => $copy ? $trans : '', |
|
| 711 | - 'follow_location' => 10, |
|
| 712 | - ); |
|
| 713 | - if (!is_null($taille_max)) { |
|
| 714 | - $options['taille_max'] = $taille_max; |
|
| 715 | - } |
|
| 716 | - // dix tentatives maximum en cas d'entetes 301... |
|
| 717 | - $res = recuperer_url($url, $options); |
|
| 718 | - if (!$res) { |
|
| 719 | - return false; |
|
| 720 | - } |
|
| 721 | - if ($res['status'] !== 200) { |
|
| 722 | - return false; |
|
| 723 | - } |
|
| 724 | - if ($get_headers) { |
|
| 725 | - return $res['headers'] . "\n" . $res['page']; |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - return $res['page']; |
|
| 693 | + // $copy = copier le fichier ? |
|
| 694 | + $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-) |
|
| 695 | + |
|
| 696 | + if (!is_null($taille_max) and ($taille_max == 0)) { |
|
| 697 | + $get = 'HEAD'; |
|
| 698 | + } else { |
|
| 699 | + $get = 'GET'; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + $options = array( |
|
| 703 | + 'transcoder' => $trans === true, |
|
| 704 | + 'methode' => $get, |
|
| 705 | + 'datas' => $datas, |
|
| 706 | + 'boundary' => $boundary, |
|
| 707 | + 'refuser_gz' => $refuser_gz, |
|
| 708 | + 'if_modified_since' => $date_verif, |
|
| 709 | + 'uri_referer' => $uri_referer, |
|
| 710 | + 'file' => $copy ? $trans : '', |
|
| 711 | + 'follow_location' => 10, |
|
| 712 | + ); |
|
| 713 | + if (!is_null($taille_max)) { |
|
| 714 | + $options['taille_max'] = $taille_max; |
|
| 715 | + } |
|
| 716 | + // dix tentatives maximum en cas d'entetes 301... |
|
| 717 | + $res = recuperer_url($url, $options); |
|
| 718 | + if (!$res) { |
|
| 719 | + return false; |
|
| 720 | + } |
|
| 721 | + if ($res['status'] !== 200) { |
|
| 722 | + return false; |
|
| 723 | + } |
|
| 724 | + if ($get_headers) { |
|
| 725 | + return $res['headers'] . "\n" . $res['page']; |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + return $res['page']; |
|
| 729 | 729 | } |
| 730 | 730 | |
| 731 | 731 | |
@@ -762,48 +762,48 @@ discard block |
||
| 762 | 762 | * - false sinon |
| 763 | 763 | **/ |
| 764 | 764 | function recuperer_lapage( |
| 765 | - $url, |
|
| 766 | - $trans = false, |
|
| 767 | - $get = 'GET', |
|
| 768 | - $taille_max = 1048576, |
|
| 769 | - $datas = '', |
|
| 770 | - $refuser_gz = false, |
|
| 771 | - $date_verif = '', |
|
| 772 | - $uri_referer = '' |
|
| 765 | + $url, |
|
| 766 | + $trans = false, |
|
| 767 | + $get = 'GET', |
|
| 768 | + $taille_max = 1048576, |
|
| 769 | + $datas = '', |
|
| 770 | + $refuser_gz = false, |
|
| 771 | + $date_verif = '', |
|
| 772 | + $uri_referer = '' |
|
| 773 | 773 | ) { |
| 774 | - // $copy = copier le fichier ? |
|
| 775 | - $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-) |
|
| 776 | - |
|
| 777 | - // si on ecrit directement dans un fichier, pour ne pas manipuler |
|
| 778 | - // en memoire refuser gz |
|
| 779 | - if ($copy) { |
|
| 780 | - $refuser_gz = true; |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - $options = array( |
|
| 784 | - 'transcoder' => $trans === true, |
|
| 785 | - 'methode' => $get, |
|
| 786 | - 'datas' => $datas, |
|
| 787 | - 'refuser_gz' => $refuser_gz, |
|
| 788 | - 'if_modified_since' => $date_verif, |
|
| 789 | - 'uri_referer' => $uri_referer, |
|
| 790 | - 'file' => $copy ? $trans : '', |
|
| 791 | - 'follow_location' => false, |
|
| 792 | - ); |
|
| 793 | - if (!is_null($taille_max)) { |
|
| 794 | - $options['taille_max'] = $taille_max; |
|
| 795 | - } |
|
| 796 | - // dix tentatives maximum en cas d'entetes 301... |
|
| 797 | - $res = recuperer_url($url, $options); |
|
| 798 | - |
|
| 799 | - if (!$res) { |
|
| 800 | - return false; |
|
| 801 | - } |
|
| 802 | - if ($res['status'] !== 200) { |
|
| 803 | - return false; |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - return array($res['headers'], $res['page']); |
|
| 774 | + // $copy = copier le fichier ? |
|
| 775 | + $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-) |
|
| 776 | + |
|
| 777 | + // si on ecrit directement dans un fichier, pour ne pas manipuler |
|
| 778 | + // en memoire refuser gz |
|
| 779 | + if ($copy) { |
|
| 780 | + $refuser_gz = true; |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + $options = array( |
|
| 784 | + 'transcoder' => $trans === true, |
|
| 785 | + 'methode' => $get, |
|
| 786 | + 'datas' => $datas, |
|
| 787 | + 'refuser_gz' => $refuser_gz, |
|
| 788 | + 'if_modified_since' => $date_verif, |
|
| 789 | + 'uri_referer' => $uri_referer, |
|
| 790 | + 'file' => $copy ? $trans : '', |
|
| 791 | + 'follow_location' => false, |
|
| 792 | + ); |
|
| 793 | + if (!is_null($taille_max)) { |
|
| 794 | + $options['taille_max'] = $taille_max; |
|
| 795 | + } |
|
| 796 | + // dix tentatives maximum en cas d'entetes 301... |
|
| 797 | + $res = recuperer_url($url, $options); |
|
| 798 | + |
|
| 799 | + if (!$res) { |
|
| 800 | + return false; |
|
| 801 | + } |
|
| 802 | + if ($res['status'] !== 200) { |
|
| 803 | + return false; |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + return array($res['headers'], $res['page']); |
|
| 807 | 807 | } |
| 808 | 808 | |
| 809 | 809 | /** |
@@ -821,41 +821,41 @@ discard block |
||
| 821 | 821 | * string contenu de la resource |
| 822 | 822 | */ |
| 823 | 823 | function recuperer_body($handle, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = '') { |
| 824 | - $taille = 0; |
|
| 825 | - $result = ''; |
|
| 826 | - $fp = false; |
|
| 827 | - if ($fichier) { |
|
| 828 | - include_spip('inc/acces'); |
|
| 829 | - $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 830 | - $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 831 | - if (!$fp and file_exists($fichier)) { |
|
| 832 | - return filesize($fichier); |
|
| 833 | - } |
|
| 834 | - if (!$fp) { |
|
| 835 | - return false; |
|
| 836 | - } |
|
| 837 | - $result = 0; // on renvoie la taille du fichier |
|
| 838 | - } |
|
| 839 | - while (!feof($handle) and $taille < $taille_max) { |
|
| 840 | - $res = fread($handle, 16384); |
|
| 841 | - $taille += strlen($res); |
|
| 842 | - if ($fp) { |
|
| 843 | - fwrite($fp, $res); |
|
| 844 | - $result = $taille; |
|
| 845 | - } else { |
|
| 846 | - $result .= $res; |
|
| 847 | - } |
|
| 848 | - } |
|
| 849 | - if ($fp) { |
|
| 850 | - spip_fclose_unlock($fp); |
|
| 851 | - spip_unlink($fichier); |
|
| 852 | - @rename($tmpfile, $fichier); |
|
| 853 | - if (!file_exists($fichier)) { |
|
| 854 | - return false; |
|
| 855 | - } |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - return $result; |
|
| 824 | + $taille = 0; |
|
| 825 | + $result = ''; |
|
| 826 | + $fp = false; |
|
| 827 | + if ($fichier) { |
|
| 828 | + include_spip('inc/acces'); |
|
| 829 | + $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 830 | + $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 831 | + if (!$fp and file_exists($fichier)) { |
|
| 832 | + return filesize($fichier); |
|
| 833 | + } |
|
| 834 | + if (!$fp) { |
|
| 835 | + return false; |
|
| 836 | + } |
|
| 837 | + $result = 0; // on renvoie la taille du fichier |
|
| 838 | + } |
|
| 839 | + while (!feof($handle) and $taille < $taille_max) { |
|
| 840 | + $res = fread($handle, 16384); |
|
| 841 | + $taille += strlen($res); |
|
| 842 | + if ($fp) { |
|
| 843 | + fwrite($fp, $res); |
|
| 844 | + $result = $taille; |
|
| 845 | + } else { |
|
| 846 | + $result .= $res; |
|
| 847 | + } |
|
| 848 | + } |
|
| 849 | + if ($fp) { |
|
| 850 | + spip_fclose_unlock($fp); |
|
| 851 | + spip_unlink($fichier); |
|
| 852 | + @rename($tmpfile, $fichier); |
|
| 853 | + if (!file_exists($fichier)) { |
|
| 854 | + return false; |
|
| 855 | + } |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + return $result; |
|
| 859 | 859 | } |
| 860 | 860 | |
| 861 | 861 | /** |
@@ -877,34 +877,34 @@ discard block |
||
| 877 | 877 | * string location |
| 878 | 878 | */ |
| 879 | 879 | function recuperer_entetes_complets($handle, $if_modified_since = false) { |
| 880 | - $result = array('status' => 0, 'headers' => array(), 'last_modified' => 0, 'location' => ''); |
|
| 881 | - |
|
| 882 | - $s = @trim(fgets($handle, 16384)); |
|
| 883 | - if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) { |
|
| 884 | - return false; |
|
| 885 | - } |
|
| 886 | - $result['status'] = intval($r[1]); |
|
| 887 | - while ($s = trim(fgets($handle, 16384))) { |
|
| 888 | - $result['headers'][] = $s . "\n"; |
|
| 889 | - preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 890 | - list(, $d, $v) = $r; |
|
| 891 | - if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) { |
|
| 892 | - $result['location'] = $v; |
|
| 893 | - } elseif ($d == 'Last-Modified') { |
|
| 894 | - $result['last_modified'] = strtotime($v); |
|
| 895 | - } |
|
| 896 | - } |
|
| 897 | - if ($if_modified_since |
|
| 898 | - and $result['last_modified'] |
|
| 899 | - and $if_modified_since > $result['last_modified'] |
|
| 900 | - and $result['status'] == 200 |
|
| 901 | - ) { |
|
| 902 | - $result['status'] = 304; |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - $result['headers'] = implode('', $result['headers']); |
|
| 906 | - |
|
| 907 | - return $result; |
|
| 880 | + $result = array('status' => 0, 'headers' => array(), 'last_modified' => 0, 'location' => ''); |
|
| 881 | + |
|
| 882 | + $s = @trim(fgets($handle, 16384)); |
|
| 883 | + if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) { |
|
| 884 | + return false; |
|
| 885 | + } |
|
| 886 | + $result['status'] = intval($r[1]); |
|
| 887 | + while ($s = trim(fgets($handle, 16384))) { |
|
| 888 | + $result['headers'][] = $s . "\n"; |
|
| 889 | + preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 890 | + list(, $d, $v) = $r; |
|
| 891 | + if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) { |
|
| 892 | + $result['location'] = $v; |
|
| 893 | + } elseif ($d == 'Last-Modified') { |
|
| 894 | + $result['last_modified'] = strtotime($v); |
|
| 895 | + } |
|
| 896 | + } |
|
| 897 | + if ($if_modified_since |
|
| 898 | + and $result['last_modified'] |
|
| 899 | + and $if_modified_since > $result['last_modified'] |
|
| 900 | + and $result['status'] == 200 |
|
| 901 | + ) { |
|
| 902 | + $result['status'] = 304; |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + $result['headers'] = implode('', $result['headers']); |
|
| 906 | + |
|
| 907 | + return $result; |
|
| 908 | 908 | } |
| 909 | 909 | |
| 910 | 910 | /** |
@@ -926,20 +926,20 @@ discard block |
||
| 926 | 926 | * - le tableau des entetes dans tous les autres cas |
| 927 | 927 | **/ |
| 928 | 928 | function recuperer_entetes($f, $date_verif = '') { |
| 929 | - //Cas ou la page distante n'a pas bouge depuis |
|
| 930 | - //la derniere visite |
|
| 931 | - $res = recuperer_entetes_complets($f, $date_verif); |
|
| 932 | - if (!$res) { |
|
| 933 | - return false; |
|
| 934 | - } |
|
| 935 | - if ($res['location']) { |
|
| 936 | - return $res['location']; |
|
| 937 | - } |
|
| 938 | - if ($res['status'] != 200) { |
|
| 939 | - return $res['status']; |
|
| 940 | - } |
|
| 941 | - |
|
| 942 | - return explode("\n", $res['headers']); |
|
| 929 | + //Cas ou la page distante n'a pas bouge depuis |
|
| 930 | + //la derniere visite |
|
| 931 | + $res = recuperer_entetes_complets($f, $date_verif); |
|
| 932 | + if (!$res) { |
|
| 933 | + return false; |
|
| 934 | + } |
|
| 935 | + if ($res['location']) { |
|
| 936 | + return $res['location']; |
|
| 937 | + } |
|
| 938 | + if ($res['status'] != 200) { |
|
| 939 | + return $res['status']; |
|
| 940 | + } |
|
| 941 | + |
|
| 942 | + return explode("\n", $res['headers']); |
|
| 943 | 943 | } |
| 944 | 944 | |
| 945 | 945 | /** |
@@ -961,22 +961,22 @@ discard block |
||
| 961 | 961 | * Nom du fichier pour copie locale |
| 962 | 962 | **/ |
| 963 | 963 | function nom_fichier_copie_locale($source, $extension) { |
| 964 | - include_spip('inc/documents'); |
|
| 964 | + include_spip('inc/documents'); |
|
| 965 | 965 | |
| 966 | - $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 967 | - $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 966 | + $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 967 | + $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 968 | 968 | |
| 969 | - // on se place tout le temps comme si on etait a la racine |
|
| 970 | - if (_DIR_RACINE) { |
|
| 971 | - $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); |
|
| 972 | - } |
|
| 969 | + // on se place tout le temps comme si on etait a la racine |
|
| 970 | + if (_DIR_RACINE) { |
|
| 971 | + $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); |
|
| 972 | + } |
|
| 973 | 973 | |
| 974 | - $m = md5($source); |
|
| 974 | + $m = md5($source); |
|
| 975 | 975 | |
| 976 | - return $d |
|
| 977 | - . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 978 | - . substr($m, 0, 4) |
|
| 979 | - . ".$extension"; |
|
| 976 | + return $d |
|
| 977 | + . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 978 | + . substr($m, 0, 4) |
|
| 979 | + . ".$extension"; |
|
| 980 | 980 | } |
| 981 | 981 | |
| 982 | 982 | /** |
@@ -994,68 +994,68 @@ discard block |
||
| 994 | 994 | * Nom du fichier calculé |
| 995 | 995 | **/ |
| 996 | 996 | function fichier_copie_locale($source) { |
| 997 | - // Si c'est deja local pas de souci |
|
| 998 | - if (!tester_url_absolue($source)) { |
|
| 999 | - if (_DIR_RACINE) { |
|
| 1000 | - $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - return $source; |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 1007 | - // a deja ete copie en local avec cette extension |
|
| 1008 | - // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 1009 | - $path_parts = pathinfo($source); |
|
| 1010 | - if (!isset($path_parts['extension'])) { |
|
| 1011 | - $path_parts['extension'] = ''; |
|
| 1012 | - } |
|
| 1013 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 1014 | - if ($ext |
|
| 1015 | - and preg_match(',^\w+$,', $ext) // pas de php?truc=1&... |
|
| 1016 | - and $f = nom_fichier_copie_locale($source, $ext) |
|
| 1017 | - and file_exists(_DIR_RACINE . $f) |
|
| 1018 | - ) { |
|
| 1019 | - return $f; |
|
| 1020 | - } |
|
| 1021 | - |
|
| 1022 | - |
|
| 1023 | - // Si c'est deja dans la table des documents, |
|
| 1024 | - // ramener le nom de sa copie potentielle |
|
| 1025 | - $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 1026 | - |
|
| 1027 | - if ($ext) { |
|
| 1028 | - return nom_fichier_copie_locale($source, $ext); |
|
| 1029 | - } |
|
| 1030 | - |
|
| 1031 | - // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 1032 | - // et si il n'aurait pas deja ete rapatrie |
|
| 1033 | - |
|
| 1034 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 1035 | - |
|
| 1036 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 1037 | - $f = nom_fichier_copie_locale($source, $ext); |
|
| 1038 | - if (file_exists(_DIR_RACINE . $f)) { |
|
| 1039 | - return $f; |
|
| 1040 | - } |
|
| 1041 | - } |
|
| 1042 | - |
|
| 1043 | - // Ping pour voir si son extension est connue et autorisee |
|
| 1044 | - // avec mise en cache du resultat du ping |
|
| 1045 | - |
|
| 1046 | - $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 1047 | - if (!@file_exists($cache) |
|
| 1048 | - or !$path_parts = @unserialize(spip_file_get_contents($cache)) |
|
| 1049 | - or _request('var_mode') == 'recalcul' |
|
| 1050 | - ) { |
|
| 1051 | - $path_parts = recuperer_infos_distantes($source, 0, false); |
|
| 1052 | - ecrire_fichier($cache, serialize($path_parts)); |
|
| 1053 | - } |
|
| 1054 | - $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : ''; |
|
| 1055 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 1056 | - return nom_fichier_copie_locale($source, $ext); |
|
| 1057 | - } |
|
| 1058 | - spip_log("pas de copie locale pour $source"); |
|
| 997 | + // Si c'est deja local pas de souci |
|
| 998 | + if (!tester_url_absolue($source)) { |
|
| 999 | + if (_DIR_RACINE) { |
|
| 1000 | + $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + return $source; |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 1007 | + // a deja ete copie en local avec cette extension |
|
| 1008 | + // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 1009 | + $path_parts = pathinfo($source); |
|
| 1010 | + if (!isset($path_parts['extension'])) { |
|
| 1011 | + $path_parts['extension'] = ''; |
|
| 1012 | + } |
|
| 1013 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 1014 | + if ($ext |
|
| 1015 | + and preg_match(',^\w+$,', $ext) // pas de php?truc=1&... |
|
| 1016 | + and $f = nom_fichier_copie_locale($source, $ext) |
|
| 1017 | + and file_exists(_DIR_RACINE . $f) |
|
| 1018 | + ) { |
|
| 1019 | + return $f; |
|
| 1020 | + } |
|
| 1021 | + |
|
| 1022 | + |
|
| 1023 | + // Si c'est deja dans la table des documents, |
|
| 1024 | + // ramener le nom de sa copie potentielle |
|
| 1025 | + $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 1026 | + |
|
| 1027 | + if ($ext) { |
|
| 1028 | + return nom_fichier_copie_locale($source, $ext); |
|
| 1029 | + } |
|
| 1030 | + |
|
| 1031 | + // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 1032 | + // et si il n'aurait pas deja ete rapatrie |
|
| 1033 | + |
|
| 1034 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 1035 | + |
|
| 1036 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 1037 | + $f = nom_fichier_copie_locale($source, $ext); |
|
| 1038 | + if (file_exists(_DIR_RACINE . $f)) { |
|
| 1039 | + return $f; |
|
| 1040 | + } |
|
| 1041 | + } |
|
| 1042 | + |
|
| 1043 | + // Ping pour voir si son extension est connue et autorisee |
|
| 1044 | + // avec mise en cache du resultat du ping |
|
| 1045 | + |
|
| 1046 | + $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 1047 | + if (!@file_exists($cache) |
|
| 1048 | + or !$path_parts = @unserialize(spip_file_get_contents($cache)) |
|
| 1049 | + or _request('var_mode') == 'recalcul' |
|
| 1050 | + ) { |
|
| 1051 | + $path_parts = recuperer_infos_distantes($source, 0, false); |
|
| 1052 | + ecrire_fichier($cache, serialize($path_parts)); |
|
| 1053 | + } |
|
| 1054 | + $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : ''; |
|
| 1055 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 1056 | + return nom_fichier_copie_locale($source, $ext); |
|
| 1057 | + } |
|
| 1058 | + spip_log("pas de copie locale pour $source"); |
|
| 1059 | 1059 | } |
| 1060 | 1060 | |
| 1061 | 1061 | |
@@ -1083,141 +1083,141 @@ discard block |
||
| 1083 | 1083 | **/ |
| 1084 | 1084 | function recuperer_infos_distantes($source, $max = 0, $charger_si_petite_image = true) { |
| 1085 | 1085 | |
| 1086 | - // pas la peine de perdre son temps |
|
| 1087 | - if (!tester_url_absolue($source)) { |
|
| 1088 | - return false; |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - # charger les alias des types mime |
|
| 1092 | - include_spip('base/typedoc'); |
|
| 1093 | - |
|
| 1094 | - $a = array(); |
|
| 1095 | - $mime_type = ''; |
|
| 1096 | - // On va directement charger le debut des images et des fichiers html, |
|
| 1097 | - // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 1098 | - // ca echoue l'utilisateur devra les entrer... |
|
| 1099 | - if ($headers = recuperer_page($source, false, true, $max, '', '', true)) { |
|
| 1100 | - list($headers, $a['body']) = preg_split(',\n\n,', $headers, 2); |
|
| 1101 | - |
|
| 1102 | - if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) { |
|
| 1103 | - $mime_type = (trim($regs[1])); |
|
| 1104 | - } else { |
|
| 1105 | - $mime_type = ''; |
|
| 1106 | - } // inconnu |
|
| 1107 | - |
|
| 1108 | - // Appliquer les alias |
|
| 1109 | - while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1110 | - $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1111 | - } |
|
| 1112 | - |
|
| 1113 | - // Si on a un mime-type insignifiant |
|
| 1114 | - // text/plain,application/octet-stream ou vide |
|
| 1115 | - // c'est peut-etre que le serveur ne sait pas |
|
| 1116 | - // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1117 | - // ou le Content-Disposition: attachment; filename=... |
|
| 1118 | - $t = null; |
|
| 1119 | - if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))) { |
|
| 1120 | - if (!$t |
|
| 1121 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1122 | - ) { |
|
| 1123 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1124 | - } |
|
| 1125 | - if (!$t |
|
| 1126 | - and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1127 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1128 | - ) { |
|
| 1129 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1130 | - } |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1134 | - if (!$t) { |
|
| 1135 | - $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1139 | - // On essaie de nouveau avec l'extension |
|
| 1140 | - if (!$t |
|
| 1141 | - and $mime_type != 'text/plain' |
|
| 1142 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1143 | - ) { |
|
| 1144 | - # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1145 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1146 | - } |
|
| 1147 | - |
|
| 1148 | - if ($t) { |
|
| 1149 | - spip_log("mime-type $mime_type ok, extension " . $t['extension']); |
|
| 1150 | - $a['extension'] = $t['extension']; |
|
| 1151 | - } else { |
|
| 1152 | - # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1153 | - spip_log("mime-type $mime_type inconnu"); |
|
| 1154 | - $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1155 | - if (!$t) { |
|
| 1156 | - return false; |
|
| 1157 | - } |
|
| 1158 | - $a['extension'] = $t['extension']; |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 1162 | - $a['taille'] = intval($regs[1]); |
|
| 1163 | - } |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - // Echec avec HEAD, on tente avec GET |
|
| 1167 | - if (!$a and !$max) { |
|
| 1168 | - spip_log("tenter GET $source"); |
|
| 1169 | - $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1170 | - } |
|
| 1171 | - |
|
| 1172 | - // si on a rien trouve pas la peine d'insister |
|
| 1173 | - if (!$a) { |
|
| 1174 | - return false; |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 1178 | - // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 1179 | - if (preg_match(',^image/(jpeg|gif|png|swf),', $mime_type)) { |
|
| 1180 | - if ($max == 0 |
|
| 1181 | - and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 1182 | - and isset($GLOBALS['meta']['formats_graphiques']) |
|
| 1183 | - and (strpos($GLOBALS['meta']['formats_graphiques'], $a['extension']) !== false) |
|
| 1184 | - and $charger_si_petite_image |
|
| 1185 | - ) { |
|
| 1186 | - $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1187 | - } else { |
|
| 1188 | - if ($a['body']) { |
|
| 1189 | - $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $a['extension']); |
|
| 1190 | - ecrire_fichier($a['fichier'], $a['body']); |
|
| 1191 | - $size_image = @getimagesize($a['fichier']); |
|
| 1192 | - $a['largeur'] = intval($size_image[0]); |
|
| 1193 | - $a['hauteur'] = intval($size_image[1]); |
|
| 1194 | - $a['type_image'] = true; |
|
| 1195 | - } |
|
| 1196 | - } |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1200 | - // ce sera mieux que 0x0 |
|
| 1201 | - if ($a and isset($a['extension']) and $a['extension'] == 'swf' |
|
| 1202 | - and empty($a['largeur']) |
|
| 1203 | - ) { |
|
| 1204 | - $a['largeur'] = 425; |
|
| 1205 | - $a['hauteur'] = 350; |
|
| 1206 | - } |
|
| 1207 | - |
|
| 1208 | - if ($mime_type == 'text/html') { |
|
| 1209 | - include_spip('inc/filtres'); |
|
| 1210 | - $page = recuperer_page($source, true, false, _INC_DISTANT_MAX_SIZE); |
|
| 1211 | - if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) { |
|
| 1212 | - $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1213 | - } |
|
| 1214 | - if (!isset($a['taille']) or !$a['taille']) { |
|
| 1215 | - $a['taille'] = strlen($page); # a peu pres |
|
| 1216 | - } |
|
| 1217 | - } |
|
| 1218 | - $a['mime_type'] = $mime_type; |
|
| 1219 | - |
|
| 1220 | - return $a; |
|
| 1086 | + // pas la peine de perdre son temps |
|
| 1087 | + if (!tester_url_absolue($source)) { |
|
| 1088 | + return false; |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + # charger les alias des types mime |
|
| 1092 | + include_spip('base/typedoc'); |
|
| 1093 | + |
|
| 1094 | + $a = array(); |
|
| 1095 | + $mime_type = ''; |
|
| 1096 | + // On va directement charger le debut des images et des fichiers html, |
|
| 1097 | + // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 1098 | + // ca echoue l'utilisateur devra les entrer... |
|
| 1099 | + if ($headers = recuperer_page($source, false, true, $max, '', '', true)) { |
|
| 1100 | + list($headers, $a['body']) = preg_split(',\n\n,', $headers, 2); |
|
| 1101 | + |
|
| 1102 | + if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) { |
|
| 1103 | + $mime_type = (trim($regs[1])); |
|
| 1104 | + } else { |
|
| 1105 | + $mime_type = ''; |
|
| 1106 | + } // inconnu |
|
| 1107 | + |
|
| 1108 | + // Appliquer les alias |
|
| 1109 | + while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1110 | + $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1111 | + } |
|
| 1112 | + |
|
| 1113 | + // Si on a un mime-type insignifiant |
|
| 1114 | + // text/plain,application/octet-stream ou vide |
|
| 1115 | + // c'est peut-etre que le serveur ne sait pas |
|
| 1116 | + // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1117 | + // ou le Content-Disposition: attachment; filename=... |
|
| 1118 | + $t = null; |
|
| 1119 | + if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))) { |
|
| 1120 | + if (!$t |
|
| 1121 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1122 | + ) { |
|
| 1123 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1124 | + } |
|
| 1125 | + if (!$t |
|
| 1126 | + and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1127 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1128 | + ) { |
|
| 1129 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1130 | + } |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1134 | + if (!$t) { |
|
| 1135 | + $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1139 | + // On essaie de nouveau avec l'extension |
|
| 1140 | + if (!$t |
|
| 1141 | + and $mime_type != 'text/plain' |
|
| 1142 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1143 | + ) { |
|
| 1144 | + # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1145 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1146 | + } |
|
| 1147 | + |
|
| 1148 | + if ($t) { |
|
| 1149 | + spip_log("mime-type $mime_type ok, extension " . $t['extension']); |
|
| 1150 | + $a['extension'] = $t['extension']; |
|
| 1151 | + } else { |
|
| 1152 | + # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1153 | + spip_log("mime-type $mime_type inconnu"); |
|
| 1154 | + $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1155 | + if (!$t) { |
|
| 1156 | + return false; |
|
| 1157 | + } |
|
| 1158 | + $a['extension'] = $t['extension']; |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 1162 | + $a['taille'] = intval($regs[1]); |
|
| 1163 | + } |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + // Echec avec HEAD, on tente avec GET |
|
| 1167 | + if (!$a and !$max) { |
|
| 1168 | + spip_log("tenter GET $source"); |
|
| 1169 | + $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1170 | + } |
|
| 1171 | + |
|
| 1172 | + // si on a rien trouve pas la peine d'insister |
|
| 1173 | + if (!$a) { |
|
| 1174 | + return false; |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 1178 | + // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 1179 | + if (preg_match(',^image/(jpeg|gif|png|swf),', $mime_type)) { |
|
| 1180 | + if ($max == 0 |
|
| 1181 | + and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 1182 | + and isset($GLOBALS['meta']['formats_graphiques']) |
|
| 1183 | + and (strpos($GLOBALS['meta']['formats_graphiques'], $a['extension']) !== false) |
|
| 1184 | + and $charger_si_petite_image |
|
| 1185 | + ) { |
|
| 1186 | + $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1187 | + } else { |
|
| 1188 | + if ($a['body']) { |
|
| 1189 | + $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $a['extension']); |
|
| 1190 | + ecrire_fichier($a['fichier'], $a['body']); |
|
| 1191 | + $size_image = @getimagesize($a['fichier']); |
|
| 1192 | + $a['largeur'] = intval($size_image[0]); |
|
| 1193 | + $a['hauteur'] = intval($size_image[1]); |
|
| 1194 | + $a['type_image'] = true; |
|
| 1195 | + } |
|
| 1196 | + } |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1200 | + // ce sera mieux que 0x0 |
|
| 1201 | + if ($a and isset($a['extension']) and $a['extension'] == 'swf' |
|
| 1202 | + and empty($a['largeur']) |
|
| 1203 | + ) { |
|
| 1204 | + $a['largeur'] = 425; |
|
| 1205 | + $a['hauteur'] = 350; |
|
| 1206 | + } |
|
| 1207 | + |
|
| 1208 | + if ($mime_type == 'text/html') { |
|
| 1209 | + include_spip('inc/filtres'); |
|
| 1210 | + $page = recuperer_page($source, true, false, _INC_DISTANT_MAX_SIZE); |
|
| 1211 | + if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) { |
|
| 1212 | + $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1213 | + } |
|
| 1214 | + if (!isset($a['taille']) or !$a['taille']) { |
|
| 1215 | + $a['taille'] = strlen($page); # a peu pres |
|
| 1216 | + } |
|
| 1217 | + } |
|
| 1218 | + $a['mime_type'] = $mime_type; |
|
| 1219 | + |
|
| 1220 | + return $a; |
|
| 1221 | 1221 | } |
| 1222 | 1222 | |
| 1223 | 1223 | |
@@ -1233,19 +1233,19 @@ discard block |
||
| 1233 | 1233 | * @return string |
| 1234 | 1234 | */ |
| 1235 | 1235 | function need_proxy($host, $http_proxy = null, $http_noproxy = null) { |
| 1236 | - if (is_null($http_proxy)) { |
|
| 1237 | - $http_proxy = isset($GLOBALS['meta']['http_proxy']) ? $GLOBALS['meta']['http_proxy'] : null; |
|
| 1238 | - } |
|
| 1239 | - if (is_null($http_noproxy)) { |
|
| 1240 | - $http_noproxy = isset($GLOBALS['meta']['http_noproxy']) ? $GLOBALS['meta']['http_noproxy'] : null; |
|
| 1241 | - } |
|
| 1242 | - |
|
| 1243 | - $domain = substr($host, strpos($host, '.')); |
|
| 1244 | - |
|
| 1245 | - return ($http_proxy |
|
| 1246 | - and (strpos(" $http_noproxy ", " $host ") === false |
|
| 1247 | - and (strpos(" $http_noproxy ", " $domain ") === false))) |
|
| 1248 | - ? $http_proxy : ''; |
|
| 1236 | + if (is_null($http_proxy)) { |
|
| 1237 | + $http_proxy = isset($GLOBALS['meta']['http_proxy']) ? $GLOBALS['meta']['http_proxy'] : null; |
|
| 1238 | + } |
|
| 1239 | + if (is_null($http_noproxy)) { |
|
| 1240 | + $http_noproxy = isset($GLOBALS['meta']['http_noproxy']) ? $GLOBALS['meta']['http_noproxy'] : null; |
|
| 1241 | + } |
|
| 1242 | + |
|
| 1243 | + $domain = substr($host, strpos($host, '.')); |
|
| 1244 | + |
|
| 1245 | + return ($http_proxy |
|
| 1246 | + and (strpos(" $http_noproxy ", " $host ") === false |
|
| 1247 | + and (strpos(" $http_noproxy ", " $domain ") === false))) |
|
| 1248 | + ? $http_proxy : ''; |
|
| 1249 | 1249 | } |
| 1250 | 1250 | |
| 1251 | 1251 | |
@@ -1268,58 +1268,58 @@ discard block |
||
| 1268 | 1268 | * @return array |
| 1269 | 1269 | */ |
| 1270 | 1270 | function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = '', $vers = 'HTTP/1.0', $date = '') { |
| 1271 | - $user = $via_proxy = $proxy_user = ''; |
|
| 1272 | - $fopen = false; |
|
| 1273 | - |
|
| 1274 | - $t = @parse_url($url); |
|
| 1275 | - $host = $t['host']; |
|
| 1276 | - if ($t['scheme'] == 'http') { |
|
| 1277 | - $scheme = 'http'; |
|
| 1278 | - $noproxy = ''; |
|
| 1279 | - } elseif ($t['scheme'] == 'https') { |
|
| 1280 | - $scheme = 'ssl'; |
|
| 1281 | - $noproxy = 'ssl://'; |
|
| 1282 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1283 | - $t['port'] = 443; |
|
| 1284 | - } |
|
| 1285 | - } else { |
|
| 1286 | - $scheme = $t['scheme']; |
|
| 1287 | - $noproxy = $scheme . '://'; |
|
| 1288 | - } |
|
| 1289 | - if (isset($t['user'])) { |
|
| 1290 | - $user = array($t['user'], $t['pass']); |
|
| 1291 | - } |
|
| 1292 | - |
|
| 1293 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1294 | - $port = 80; |
|
| 1295 | - } |
|
| 1296 | - if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1297 | - $path = '/'; |
|
| 1298 | - } |
|
| 1299 | - |
|
| 1300 | - if (!empty($t['query'])) { |
|
| 1301 | - $path .= '?' . $t['query']; |
|
| 1302 | - } |
|
| 1303 | - |
|
| 1304 | - $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1305 | - if (!$f or !is_resource($f)) { |
|
| 1306 | - // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1307 | - // ce qui correspond a $f===110 |
|
| 1308 | - if ($f !== 110 |
|
| 1309 | - and !need_proxy($host) |
|
| 1310 | - and !_request('tester_proxy') |
|
| 1311 | - and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen']) |
|
| 1312 | - ) { |
|
| 1313 | - $f = @fopen($url, 'rb'); |
|
| 1314 | - spip_log("connexion vers $url par simple fopen"); |
|
| 1315 | - $fopen = true; |
|
| 1316 | - } else { |
|
| 1317 | - // echec total |
|
| 1318 | - $f = false; |
|
| 1319 | - } |
|
| 1320 | - } |
|
| 1321 | - |
|
| 1322 | - return array($f, $fopen); |
|
| 1271 | + $user = $via_proxy = $proxy_user = ''; |
|
| 1272 | + $fopen = false; |
|
| 1273 | + |
|
| 1274 | + $t = @parse_url($url); |
|
| 1275 | + $host = $t['host']; |
|
| 1276 | + if ($t['scheme'] == 'http') { |
|
| 1277 | + $scheme = 'http'; |
|
| 1278 | + $noproxy = ''; |
|
| 1279 | + } elseif ($t['scheme'] == 'https') { |
|
| 1280 | + $scheme = 'ssl'; |
|
| 1281 | + $noproxy = 'ssl://'; |
|
| 1282 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1283 | + $t['port'] = 443; |
|
| 1284 | + } |
|
| 1285 | + } else { |
|
| 1286 | + $scheme = $t['scheme']; |
|
| 1287 | + $noproxy = $scheme . '://'; |
|
| 1288 | + } |
|
| 1289 | + if (isset($t['user'])) { |
|
| 1290 | + $user = array($t['user'], $t['pass']); |
|
| 1291 | + } |
|
| 1292 | + |
|
| 1293 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1294 | + $port = 80; |
|
| 1295 | + } |
|
| 1296 | + if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1297 | + $path = '/'; |
|
| 1298 | + } |
|
| 1299 | + |
|
| 1300 | + if (!empty($t['query'])) { |
|
| 1301 | + $path .= '?' . $t['query']; |
|
| 1302 | + } |
|
| 1303 | + |
|
| 1304 | + $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1305 | + if (!$f or !is_resource($f)) { |
|
| 1306 | + // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1307 | + // ce qui correspond a $f===110 |
|
| 1308 | + if ($f !== 110 |
|
| 1309 | + and !need_proxy($host) |
|
| 1310 | + and !_request('tester_proxy') |
|
| 1311 | + and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen']) |
|
| 1312 | + ) { |
|
| 1313 | + $f = @fopen($url, 'rb'); |
|
| 1314 | + spip_log("connexion vers $url par simple fopen"); |
|
| 1315 | + $fopen = true; |
|
| 1316 | + } else { |
|
| 1317 | + // echec total |
|
| 1318 | + $f = false; |
|
| 1319 | + } |
|
| 1320 | + } |
|
| 1321 | + |
|
| 1322 | + return array($f, $fopen); |
|
| 1323 | 1323 | } |
| 1324 | 1324 | |
| 1325 | 1325 | /** |
@@ -1354,123 +1354,123 @@ discard block |
||
| 1354 | 1354 | * resource socket vers l'url demandee |
| 1355 | 1355 | */ |
| 1356 | 1356 | function lance_requete( |
| 1357 | - $method, |
|
| 1358 | - $scheme, |
|
| 1359 | - $user, |
|
| 1360 | - $host, |
|
| 1361 | - $path, |
|
| 1362 | - $port, |
|
| 1363 | - $noproxy, |
|
| 1364 | - $refuse_gz = false, |
|
| 1365 | - $referer = '', |
|
| 1366 | - $datas = '', |
|
| 1367 | - $vers = 'HTTP/1.0', |
|
| 1368 | - $date = '' |
|
| 1357 | + $method, |
|
| 1358 | + $scheme, |
|
| 1359 | + $user, |
|
| 1360 | + $host, |
|
| 1361 | + $path, |
|
| 1362 | + $port, |
|
| 1363 | + $noproxy, |
|
| 1364 | + $refuse_gz = false, |
|
| 1365 | + $referer = '', |
|
| 1366 | + $datas = '', |
|
| 1367 | + $vers = 'HTTP/1.0', |
|
| 1368 | + $date = '' |
|
| 1369 | 1369 | ) { |
| 1370 | 1370 | |
| 1371 | - $proxy_user = ''; |
|
| 1372 | - $http_proxy = need_proxy($host); |
|
| 1373 | - if ($user) { |
|
| 1374 | - $user = urlencode($user[0]) . ':' . urlencode($user[1]); |
|
| 1375 | - } |
|
| 1376 | - |
|
| 1377 | - $connect = ''; |
|
| 1378 | - if ($http_proxy) { |
|
| 1379 | - if (defined('_PROXY_HTTPS_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) { |
|
| 1380 | - $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : ''); |
|
| 1381 | - $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1382 | - . "Host: $path_host\r\n" |
|
| 1383 | - . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1384 | - } else { |
|
| 1385 | - $path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://") |
|
| 1386 | - . (!$user ? '' : "$user@") |
|
| 1387 | - . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1388 | - } |
|
| 1389 | - $t2 = @parse_url($http_proxy); |
|
| 1390 | - $first_host = $t2['host']; |
|
| 1391 | - if (!($port = $t2['port'])) { |
|
| 1392 | - $port = 80; |
|
| 1393 | - } |
|
| 1394 | - if ($t2['user']) { |
|
| 1395 | - $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1396 | - } |
|
| 1397 | - } else { |
|
| 1398 | - $first_host = $noproxy . $host; |
|
| 1399 | - } |
|
| 1400 | - |
|
| 1401 | - if ($connect) { |
|
| 1402 | - $streamContext = stream_context_create(array( |
|
| 1403 | - 'ssl' => array( |
|
| 1404 | - 'verify_peer' => false, |
|
| 1405 | - 'allow_self_signed' => true, |
|
| 1406 | - 'SNI_enabled' => true, |
|
| 1407 | - 'peer_name' => $host, |
|
| 1408 | - ) |
|
| 1409 | - )); |
|
| 1410 | - if (version_compare(phpversion(), '5.6', '<')) { |
|
| 1411 | - stream_context_set_option($streamContext, 'ssl', 'SNI_server_name', $host); |
|
| 1412 | - } |
|
| 1413 | - $f = @stream_socket_client( |
|
| 1414 | - "tcp://$first_host:$port", |
|
| 1415 | - $errno, |
|
| 1416 | - $errstr, |
|
| 1417 | - _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1418 | - STREAM_CLIENT_CONNECT, |
|
| 1419 | - $streamContext |
|
| 1420 | - ); |
|
| 1421 | - spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect'); |
|
| 1422 | - if (!$f) { |
|
| 1423 | - spip_log("Erreur connexion $errno $errstr", _LOG_ERREUR); |
|
| 1424 | - return $errno; |
|
| 1425 | - } |
|
| 1426 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1427 | - |
|
| 1428 | - fputs($f, $connect); |
|
| 1429 | - fputs($f, "\r\n"); |
|
| 1430 | - $res = fread($f, 1024); |
|
| 1431 | - if (!$res |
|
| 1432 | - or !count($res = explode(' ', $res)) |
|
| 1433 | - or $res[1] !== '200' |
|
| 1434 | - ) { |
|
| 1435 | - spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE); |
|
| 1436 | - fclose($f); |
|
| 1437 | - |
|
| 1438 | - return false; |
|
| 1439 | - } |
|
| 1440 | - // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1441 | - stream_set_blocking($f, true); |
|
| 1442 | - // envoyer le handshake |
|
| 1443 | - stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1444 | - spip_log("OK CONNECT sur $first_host:$port", 'connect'); |
|
| 1445 | - } else { |
|
| 1446 | - $ntry = 3; |
|
| 1447 | - do { |
|
| 1448 | - $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1449 | - } while (!$f and $ntry-- and $errno !== 110 and sleep(1)); |
|
| 1450 | - spip_log("Recuperer $path sur $first_host:$port par $f"); |
|
| 1451 | - if (!$f) { |
|
| 1452 | - spip_log("Erreur connexion $errno $errstr", _LOG_ERREUR); |
|
| 1453 | - |
|
| 1454 | - return $errno; |
|
| 1455 | - } |
|
| 1456 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1457 | - } |
|
| 1458 | - |
|
| 1459 | - $site = isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : ''; |
|
| 1460 | - |
|
| 1461 | - $req = "$method $path $vers\r\n" |
|
| 1462 | - . "Host: $host\r\n" |
|
| 1463 | - . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1464 | - . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1465 | - . (!$site ? '' : "Referer: $site/$referer\r\n") |
|
| 1466 | - . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n")) |
|
| 1467 | - . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n")) |
|
| 1468 | - . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n") |
|
| 1469 | - . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n"); |
|
| 1371 | + $proxy_user = ''; |
|
| 1372 | + $http_proxy = need_proxy($host); |
|
| 1373 | + if ($user) { |
|
| 1374 | + $user = urlencode($user[0]) . ':' . urlencode($user[1]); |
|
| 1375 | + } |
|
| 1376 | + |
|
| 1377 | + $connect = ''; |
|
| 1378 | + if ($http_proxy) { |
|
| 1379 | + if (defined('_PROXY_HTTPS_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) { |
|
| 1380 | + $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : ''); |
|
| 1381 | + $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1382 | + . "Host: $path_host\r\n" |
|
| 1383 | + . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1384 | + } else { |
|
| 1385 | + $path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://") |
|
| 1386 | + . (!$user ? '' : "$user@") |
|
| 1387 | + . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1388 | + } |
|
| 1389 | + $t2 = @parse_url($http_proxy); |
|
| 1390 | + $first_host = $t2['host']; |
|
| 1391 | + if (!($port = $t2['port'])) { |
|
| 1392 | + $port = 80; |
|
| 1393 | + } |
|
| 1394 | + if ($t2['user']) { |
|
| 1395 | + $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1396 | + } |
|
| 1397 | + } else { |
|
| 1398 | + $first_host = $noproxy . $host; |
|
| 1399 | + } |
|
| 1400 | + |
|
| 1401 | + if ($connect) { |
|
| 1402 | + $streamContext = stream_context_create(array( |
|
| 1403 | + 'ssl' => array( |
|
| 1404 | + 'verify_peer' => false, |
|
| 1405 | + 'allow_self_signed' => true, |
|
| 1406 | + 'SNI_enabled' => true, |
|
| 1407 | + 'peer_name' => $host, |
|
| 1408 | + ) |
|
| 1409 | + )); |
|
| 1410 | + if (version_compare(phpversion(), '5.6', '<')) { |
|
| 1411 | + stream_context_set_option($streamContext, 'ssl', 'SNI_server_name', $host); |
|
| 1412 | + } |
|
| 1413 | + $f = @stream_socket_client( |
|
| 1414 | + "tcp://$first_host:$port", |
|
| 1415 | + $errno, |
|
| 1416 | + $errstr, |
|
| 1417 | + _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1418 | + STREAM_CLIENT_CONNECT, |
|
| 1419 | + $streamContext |
|
| 1420 | + ); |
|
| 1421 | + spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect'); |
|
| 1422 | + if (!$f) { |
|
| 1423 | + spip_log("Erreur connexion $errno $errstr", _LOG_ERREUR); |
|
| 1424 | + return $errno; |
|
| 1425 | + } |
|
| 1426 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1427 | + |
|
| 1428 | + fputs($f, $connect); |
|
| 1429 | + fputs($f, "\r\n"); |
|
| 1430 | + $res = fread($f, 1024); |
|
| 1431 | + if (!$res |
|
| 1432 | + or !count($res = explode(' ', $res)) |
|
| 1433 | + or $res[1] !== '200' |
|
| 1434 | + ) { |
|
| 1435 | + spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE); |
|
| 1436 | + fclose($f); |
|
| 1437 | + |
|
| 1438 | + return false; |
|
| 1439 | + } |
|
| 1440 | + // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1441 | + stream_set_blocking($f, true); |
|
| 1442 | + // envoyer le handshake |
|
| 1443 | + stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1444 | + spip_log("OK CONNECT sur $first_host:$port", 'connect'); |
|
| 1445 | + } else { |
|
| 1446 | + $ntry = 3; |
|
| 1447 | + do { |
|
| 1448 | + $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1449 | + } while (!$f and $ntry-- and $errno !== 110 and sleep(1)); |
|
| 1450 | + spip_log("Recuperer $path sur $first_host:$port par $f"); |
|
| 1451 | + if (!$f) { |
|
| 1452 | + spip_log("Erreur connexion $errno $errstr", _LOG_ERREUR); |
|
| 1453 | + |
|
| 1454 | + return $errno; |
|
| 1455 | + } |
|
| 1456 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1457 | + } |
|
| 1458 | + |
|
| 1459 | + $site = isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : ''; |
|
| 1460 | + |
|
| 1461 | + $req = "$method $path $vers\r\n" |
|
| 1462 | + . "Host: $host\r\n" |
|
| 1463 | + . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1464 | + . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1465 | + . (!$site ? '' : "Referer: $site/$referer\r\n") |
|
| 1466 | + . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n")) |
|
| 1467 | + . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n")) |
|
| 1468 | + . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n") |
|
| 1469 | + . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n"); |
|
| 1470 | 1470 | |
| 1471 | 1471 | # spip_log("Requete\n$req"); |
| 1472 | - fputs($f, $req); |
|
| 1473 | - fputs($f, $datas ? $datas : "\r\n"); |
|
| 1472 | + fputs($f, $req); |
|
| 1473 | + fputs($f, $datas ? $datas : "\r\n"); |
|
| 1474 | 1474 | |
| 1475 | - return $f; |
|
| 1475 | + return $f; |
|
| 1476 | 1476 | } |