@@ -17,13 +17,13 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | if (!defined('IMG_SVG')) { |
| 24 | - // complete IMG_BMP | IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM | IMG_WEBP |
|
| 25 | - define('IMG_SVG', 128); |
|
| 26 | - define('IMAGETYPE_SVG', 19); |
|
| 24 | + // complete IMG_BMP | IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM | IMG_WEBP |
|
| 25 | + define('IMG_SVG', 128); |
|
| 26 | + define('IMAGETYPE_SVG', 19); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
@@ -39,39 +39,39 @@ discard block |
||
| 39 | 39 | * false si on a pas pu charger l'image |
| 40 | 40 | */ |
| 41 | 41 | function svg_charger($fichier, $maxlen = null) { |
| 42 | - if (strpos($fichier, 'data:image/svg+xml') === 0) { |
|
| 43 | - $image = explode(';', $fichier, 2); |
|
| 44 | - $image = end($image); |
|
| 45 | - if (strpos($image, 'base64,') === 0) { |
|
| 46 | - $image = base64_decode(substr($image, 7)); |
|
| 47 | - } |
|
| 48 | - if (strpos($image, '<svg') !== false) { |
|
| 49 | - return $image; |
|
| 50 | - } |
|
| 51 | - // encodage inconnu ou autre format d'image ? |
|
| 52 | - return false; |
|
| 53 | - } |
|
| 54 | - // c'est peut etre deja une image svg ? |
|
| 55 | - if (strpos($fichier, '<svg') !== false) { |
|
| 56 | - return $fichier; |
|
| 57 | - } |
|
| 58 | - if (!file_exists($fichier)) { |
|
| 59 | - $fichier = supprimer_timestamp($fichier); |
|
| 60 | - if (!file_exists($fichier)) { |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - if (is_null($maxlen)) { |
|
| 65 | - $image = file_get_contents($fichier); |
|
| 66 | - } |
|
| 67 | - else { |
|
| 68 | - $image = file_get_contents($fichier, false, null, 0, $maxlen); |
|
| 69 | - } |
|
| 70 | - // est-ce bien une image svg ? |
|
| 71 | - if (strpos($image, '<svg') !== false) { |
|
| 72 | - return $image; |
|
| 73 | - } |
|
| 74 | - return false; |
|
| 42 | + if (strpos($fichier, 'data:image/svg+xml') === 0) { |
|
| 43 | + $image = explode(';', $fichier, 2); |
|
| 44 | + $image = end($image); |
|
| 45 | + if (strpos($image, 'base64,') === 0) { |
|
| 46 | + $image = base64_decode(substr($image, 7)); |
|
| 47 | + } |
|
| 48 | + if (strpos($image, '<svg') !== false) { |
|
| 49 | + return $image; |
|
| 50 | + } |
|
| 51 | + // encodage inconnu ou autre format d'image ? |
|
| 52 | + return false; |
|
| 53 | + } |
|
| 54 | + // c'est peut etre deja une image svg ? |
|
| 55 | + if (strpos($fichier, '<svg') !== false) { |
|
| 56 | + return $fichier; |
|
| 57 | + } |
|
| 58 | + if (!file_exists($fichier)) { |
|
| 59 | + $fichier = supprimer_timestamp($fichier); |
|
| 60 | + if (!file_exists($fichier)) { |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + if (is_null($maxlen)) { |
|
| 65 | + $image = file_get_contents($fichier); |
|
| 66 | + } |
|
| 67 | + else { |
|
| 68 | + $image = file_get_contents($fichier, false, null, 0, $maxlen); |
|
| 69 | + } |
|
| 70 | + // est-ce bien une image svg ? |
|
| 71 | + if (strpos($image, '<svg') !== false) { |
|
| 72 | + return $image; |
|
| 73 | + } |
|
| 74 | + return false; |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
@@ -80,28 +80,28 @@ discard block |
||
| 80 | 80 | * @return array|bool |
| 81 | 81 | */ |
| 82 | 82 | function svg_lire_balise_svg($fichier) { |
| 83 | - if (!$debut_fichier = svg_charger($fichier, 4096)) { |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - if (($ps = stripos($debut_fichier, '<svg')) !== false) { |
|
| 88 | - $pe = stripos($debut_fichier, '>', $ps); |
|
| 89 | - $balise_svg = substr($debut_fichier, $ps, $pe - $ps + 1); |
|
| 90 | - |
|
| 91 | - if (preg_match_all(',([\w:\-]+)=,Uims', $balise_svg, $matches)) { |
|
| 92 | - if (!function_exists('extraire_attribut')) { |
|
| 93 | - include_spip('inc/filtres'); |
|
| 94 | - } |
|
| 95 | - $attributs = []; |
|
| 96 | - foreach ($matches[1] as $att) { |
|
| 97 | - $attributs[$att] = extraire_attribut($balise_svg, $att); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return [$balise_svg, $attributs]; |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return false; |
|
| 83 | + if (!$debut_fichier = svg_charger($fichier, 4096)) { |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + if (($ps = stripos($debut_fichier, '<svg')) !== false) { |
|
| 88 | + $pe = stripos($debut_fichier, '>', $ps); |
|
| 89 | + $balise_svg = substr($debut_fichier, $ps, $pe - $ps + 1); |
|
| 90 | + |
|
| 91 | + if (preg_match_all(',([\w:\-]+)=,Uims', $balise_svg, $matches)) { |
|
| 92 | + if (!function_exists('extraire_attribut')) { |
|
| 93 | + include_spip('inc/filtres'); |
|
| 94 | + } |
|
| 95 | + $attributs = []; |
|
| 96 | + foreach ($matches[1] as $att) { |
|
| 97 | + $attributs[$att] = extraire_attribut($balise_svg, $att); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return [$balise_svg, $attributs]; |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return false; |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -111,12 +111,12 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | function svg_lire_attributs($img) { |
| 113 | 113 | |
| 114 | - if ($svg_infos = svg_lire_balise_svg($img)) { |
|
| 115 | - [$balise_svg, $attributs] = $svg_infos; |
|
| 116 | - return $attributs; |
|
| 117 | - } |
|
| 114 | + if ($svg_infos = svg_lire_balise_svg($img)) { |
|
| 115 | + [$balise_svg, $attributs] = $svg_infos; |
|
| 116 | + return $attributs; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - return false; |
|
| 119 | + return false; |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
@@ -126,38 +126,38 @@ discard block |
||
| 126 | 126 | * @return bool|float|int |
| 127 | 127 | */ |
| 128 | 128 | function svg_dimension_to_pixels($dimension, $precision = 2) { |
| 129 | - if (preg_match(',^(-?\d+(\.\d+)?)([^\d]*),i', trim($dimension), $m)) { |
|
| 130 | - switch (strtolower($m[2])) { |
|
| 131 | - case '%': |
|
| 132 | - // on ne sait pas faire :( |
|
| 133 | - return false; |
|
| 134 | - break; |
|
| 135 | - case 'em': |
|
| 136 | - return round($m[1] * 16, $precision); // 16px font-size par defaut |
|
| 137 | - break; |
|
| 138 | - case 'ex': |
|
| 139 | - return round($m[1] * 16, $precision); // 16px font-size par defaut |
|
| 140 | - break; |
|
| 141 | - case 'pc': |
|
| 142 | - return round($m[1] * 16, $precision); // 1/6 inch = 96px/6 in CSS |
|
| 143 | - break; |
|
| 144 | - case 'cm': |
|
| 145 | - return round($m[1] * 96 / 2.54, $precision); // 96px / 2.54cm; |
|
| 146 | - break; |
|
| 147 | - case 'mm': |
|
| 148 | - return round($m[1] * 96 / 25.4, $precision); // 96px / 25.4mm; |
|
| 149 | - break; |
|
| 150 | - case 'in': |
|
| 151 | - return round($m[1] * 96, $precision); // 1 inch = 96px in CSS |
|
| 152 | - break; |
|
| 153 | - case 'px': |
|
| 154 | - case 'pt': |
|
| 155 | - default: |
|
| 156 | - return $m[1]; |
|
| 157 | - break; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - return false; |
|
| 129 | + if (preg_match(',^(-?\d+(\.\d+)?)([^\d]*),i', trim($dimension), $m)) { |
|
| 130 | + switch (strtolower($m[2])) { |
|
| 131 | + case '%': |
|
| 132 | + // on ne sait pas faire :( |
|
| 133 | + return false; |
|
| 134 | + break; |
|
| 135 | + case 'em': |
|
| 136 | + return round($m[1] * 16, $precision); // 16px font-size par defaut |
|
| 137 | + break; |
|
| 138 | + case 'ex': |
|
| 139 | + return round($m[1] * 16, $precision); // 16px font-size par defaut |
|
| 140 | + break; |
|
| 141 | + case 'pc': |
|
| 142 | + return round($m[1] * 16, $precision); // 1/6 inch = 96px/6 in CSS |
|
| 143 | + break; |
|
| 144 | + case 'cm': |
|
| 145 | + return round($m[1] * 96 / 2.54, $precision); // 96px / 2.54cm; |
|
| 146 | + break; |
|
| 147 | + case 'mm': |
|
| 148 | + return round($m[1] * 96 / 25.4, $precision); // 96px / 25.4mm; |
|
| 149 | + break; |
|
| 150 | + case 'in': |
|
| 151 | + return round($m[1] * 96, $precision); // 1 inch = 96px in CSS |
|
| 152 | + break; |
|
| 153 | + case 'px': |
|
| 154 | + case 'pt': |
|
| 155 | + default: |
|
| 156 | + return $m[1]; |
|
| 157 | + break; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + return false; |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -168,15 +168,15 @@ discard block |
||
| 168 | 168 | * @return string |
| 169 | 169 | */ |
| 170 | 170 | function svg_change_balise_svg($svg, $old_balise_svg, $attributs) { |
| 171 | - $new_balise_svg = '<svg'; |
|
| 172 | - foreach ($attributs as $k => $v) { |
|
| 173 | - $new_balise_svg .= " $k=\"" . entites_html($v) . '"'; |
|
| 174 | - } |
|
| 175 | - $new_balise_svg .= '>'; |
|
| 176 | - |
|
| 177 | - $p = strpos($svg, $old_balise_svg); |
|
| 178 | - $svg = substr_replace($svg, $new_balise_svg, $p, strlen($old_balise_svg)); |
|
| 179 | - return $svg; |
|
| 171 | + $new_balise_svg = '<svg'; |
|
| 172 | + foreach ($attributs as $k => $v) { |
|
| 173 | + $new_balise_svg .= " $k=\"" . entites_html($v) . '"'; |
|
| 174 | + } |
|
| 175 | + $new_balise_svg .= '>'; |
|
| 176 | + |
|
| 177 | + $p = strpos($svg, $old_balise_svg); |
|
| 178 | + $svg = substr_replace($svg, $new_balise_svg, $p, strlen($old_balise_svg)); |
|
| 179 | + return $svg; |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
@@ -188,15 +188,15 @@ discard block |
||
| 188 | 188 | */ |
| 189 | 189 | function svg_insert_shapes($svg, $shapes, $start = true) { |
| 190 | 190 | |
| 191 | - if ($start === false or $start === 'end') { |
|
| 192 | - $svg = str_replace('</svg>', $shapes . '</svg>', $svg); |
|
| 193 | - } |
|
| 194 | - else { |
|
| 195 | - $p = stripos($svg, '<svg'); |
|
| 196 | - $p = strpos($svg, '>', $p); |
|
| 197 | - $svg = substr_replace($svg, $shapes, $p + 1, 0); |
|
| 198 | - } |
|
| 199 | - return $svg; |
|
| 191 | + if ($start === false or $start === 'end') { |
|
| 192 | + $svg = str_replace('</svg>', $shapes . '</svg>', $svg); |
|
| 193 | + } |
|
| 194 | + else { |
|
| 195 | + $p = stripos($svg, '<svg'); |
|
| 196 | + $p = strpos($svg, '>', $p); |
|
| 197 | + $svg = substr_replace($svg, $shapes, $p + 1, 0); |
|
| 198 | + } |
|
| 199 | + return $svg; |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | /** |
@@ -209,13 +209,13 @@ discard block |
||
| 209 | 209 | * @return string |
| 210 | 210 | */ |
| 211 | 211 | function svg_clip_in_box($svg, $x, $y, $width, $height) { |
| 212 | - $rect = "<rect x=\"$x\" y=\"$y\" width=\"$width\" height=\"$height\" />"; |
|
| 213 | - $id = 'clip-' . substr(md5($rect . strlen($svg)), 0, 8); |
|
| 214 | - $clippath = "<clipPath id=\"$id\">$rect</clipPath>"; |
|
| 215 | - $g = "<g clip-path=\"url(#$id)\">"; |
|
| 216 | - $svg = svg_insert_shapes($svg, $clippath . $g); |
|
| 217 | - $svg = svg_insert_shapes($svg, '</g>', false); |
|
| 218 | - return $svg; |
|
| 212 | + $rect = "<rect x=\"$x\" y=\"$y\" width=\"$width\" height=\"$height\" />"; |
|
| 213 | + $id = 'clip-' . substr(md5($rect . strlen($svg)), 0, 8); |
|
| 214 | + $clippath = "<clipPath id=\"$id\">$rect</clipPath>"; |
|
| 215 | + $g = "<g clip-path=\"url(#$id)\">"; |
|
| 216 | + $svg = svg_insert_shapes($svg, $clippath . $g); |
|
| 217 | + $svg = svg_insert_shapes($svg, '</g>', false); |
|
| 218 | + return $svg; |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
@@ -226,22 +226,22 @@ discard block |
||
| 226 | 226 | * @return bool|string |
| 227 | 227 | */ |
| 228 | 228 | function svg_redimensionner($img, $new_width, $new_height) { |
| 229 | - if ( |
|
| 230 | - $svg = svg_charger($img) |
|
| 231 | - and $svg_infos = svg_lire_balise_svg($svg) |
|
| 232 | - ) { |
|
| 233 | - [$balise_svg, $attributs] = $svg_infos; |
|
| 234 | - if (!isset($attributs['viewBox'])) { |
|
| 235 | - $attributs['viewBox'] = '0 0 ' . $attributs['width'] . ' ' . $attributs['height']; |
|
| 236 | - } |
|
| 237 | - $attributs['width'] = strval($new_width); |
|
| 238 | - $attributs['height'] = strval($new_height); |
|
| 239 | - |
|
| 240 | - $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 241 | - return $svg; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - return $img; |
|
| 229 | + if ( |
|
| 230 | + $svg = svg_charger($img) |
|
| 231 | + and $svg_infos = svg_lire_balise_svg($svg) |
|
| 232 | + ) { |
|
| 233 | + [$balise_svg, $attributs] = $svg_infos; |
|
| 234 | + if (!isset($attributs['viewBox'])) { |
|
| 235 | + $attributs['viewBox'] = '0 0 ' . $attributs['width'] . ' ' . $attributs['height']; |
|
| 236 | + } |
|
| 237 | + $attributs['width'] = strval($new_width); |
|
| 238 | + $attributs['height'] = strval($new_height); |
|
| 239 | + |
|
| 240 | + $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 241 | + return $svg; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + return $img; |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
@@ -250,15 +250,15 @@ discard block |
||
| 250 | 250 | * @return string |
| 251 | 251 | */ |
| 252 | 252 | function svg_couleur_to_hexa($couleur) { |
| 253 | - if (strpos($couleur, 'rgb(') === 0) { |
|
| 254 | - $c = explode(',', substr($couleur, 4)); |
|
| 255 | - $couleur = _couleur_dec_to_hex(intval($c[0]), intval($c[1]), intval($c[2])); |
|
| 256 | - } |
|
| 257 | - else { |
|
| 258 | - $couleur = couleur_html_to_hex($couleur); |
|
| 259 | - } |
|
| 260 | - $couleur = '#' . ltrim($couleur, '#'); |
|
| 261 | - return $couleur; |
|
| 253 | + if (strpos($couleur, 'rgb(') === 0) { |
|
| 254 | + $c = explode(',', substr($couleur, 4)); |
|
| 255 | + $couleur = _couleur_dec_to_hex(intval($c[0]), intval($c[1]), intval($c[2])); |
|
| 256 | + } |
|
| 257 | + else { |
|
| 258 | + $couleur = couleur_html_to_hex($couleur); |
|
| 259 | + } |
|
| 260 | + $couleur = '#' . ltrim($couleur, '#'); |
|
| 261 | + return $couleur; |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
@@ -267,11 +267,11 @@ discard block |
||
| 267 | 267 | * @return array |
| 268 | 268 | */ |
| 269 | 269 | function svg_couleur_to_rgb($couleur) { |
| 270 | - if (strpos($couleur, 'rgb(') === 0) { |
|
| 271 | - $c = explode(',', substr($couleur, 4)); |
|
| 272 | - return ['red' => intval($c[0]),'green' => intval($c[1]),'blue' => intval($c[2])]; |
|
| 273 | - } |
|
| 274 | - return _couleur_hex_to_dec($couleur); |
|
| 270 | + if (strpos($couleur, 'rgb(') === 0) { |
|
| 271 | + $c = explode(',', substr($couleur, 4)); |
|
| 272 | + return ['red' => intval($c[0]),'green' => intval($c[1]),'blue' => intval($c[2])]; |
|
| 273 | + } |
|
| 274 | + return _couleur_hex_to_dec($couleur); |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | |
@@ -281,70 +281,70 @@ discard block |
||
| 281 | 281 | * @return array |
| 282 | 282 | */ |
| 283 | 283 | function svg_getimagesize_from_attr($attributs) { |
| 284 | - $width = 350; // default width |
|
| 285 | - $height = 150; // default height |
|
| 286 | - |
|
| 287 | - $viewBox = "0 0 $width $height"; |
|
| 288 | - if (isset($attributs['viewBox'])) { |
|
| 289 | - $viewBox = $attributs['viewBox']; |
|
| 290 | - $viewBox = preg_replace(',\s+,', ' ', $viewBox); |
|
| 291 | - } |
|
| 292 | - // et on la convertit en px |
|
| 293 | - $viewBox = explode(' ', $viewBox); |
|
| 294 | - $viewBox = array_map('svg_dimension_to_pixels', $viewBox); |
|
| 295 | - if (!$viewBox[2]) { |
|
| 296 | - $viewBox[2] = $width; |
|
| 297 | - } |
|
| 298 | - if (!$viewBox[3]) { |
|
| 299 | - $viewBox[3] = $height; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - $coeff = 1; |
|
| 303 | - if ( |
|
| 304 | - isset($attributs['width']) |
|
| 305 | - and $w = svg_dimension_to_pixels($attributs['width']) |
|
| 306 | - ) { |
|
| 307 | - $width = $w; |
|
| 308 | - } |
|
| 309 | - else { |
|
| 310 | - // si on recupere la taille de la viewbox mais si la viewbox est petite on met un multiplicateur pour la taille finale |
|
| 311 | - $width = $viewBox[2]; |
|
| 312 | - if ($width < 1) { |
|
| 313 | - $coeff = max($coeff, 1000); |
|
| 314 | - } |
|
| 315 | - elseif ($width < 10) { |
|
| 316 | - $coeff = max($coeff, 100); |
|
| 317 | - } |
|
| 318 | - elseif ($width < 100) { |
|
| 319 | - $coeff = max($coeff, 10); |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - if ( |
|
| 323 | - isset($attributs['height']) |
|
| 324 | - and $h = svg_dimension_to_pixels($attributs['height']) |
|
| 325 | - ) { |
|
| 326 | - $height = $h; |
|
| 327 | - } |
|
| 328 | - else { |
|
| 329 | - $height = $viewBox[3]; |
|
| 330 | - if ($height < 1) { |
|
| 331 | - $coeff = max($coeff, 1000); |
|
| 332 | - } |
|
| 333 | - elseif ($height < 10) { |
|
| 334 | - $coeff = max($coeff, 100); |
|
| 335 | - } |
|
| 336 | - elseif ($height < 100) { |
|
| 337 | - $coeff = max($coeff, 10); |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - // arrondir le width et height en pixel in fine |
|
| 342 | - $width = round($coeff * $width); |
|
| 343 | - $height = round($coeff * $height); |
|
| 344 | - |
|
| 345 | - $viewBox = implode(' ', $viewBox); |
|
| 346 | - |
|
| 347 | - return [$width, $height, $viewBox]; |
|
| 284 | + $width = 350; // default width |
|
| 285 | + $height = 150; // default height |
|
| 286 | + |
|
| 287 | + $viewBox = "0 0 $width $height"; |
|
| 288 | + if (isset($attributs['viewBox'])) { |
|
| 289 | + $viewBox = $attributs['viewBox']; |
|
| 290 | + $viewBox = preg_replace(',\s+,', ' ', $viewBox); |
|
| 291 | + } |
|
| 292 | + // et on la convertit en px |
|
| 293 | + $viewBox = explode(' ', $viewBox); |
|
| 294 | + $viewBox = array_map('svg_dimension_to_pixels', $viewBox); |
|
| 295 | + if (!$viewBox[2]) { |
|
| 296 | + $viewBox[2] = $width; |
|
| 297 | + } |
|
| 298 | + if (!$viewBox[3]) { |
|
| 299 | + $viewBox[3] = $height; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + $coeff = 1; |
|
| 303 | + if ( |
|
| 304 | + isset($attributs['width']) |
|
| 305 | + and $w = svg_dimension_to_pixels($attributs['width']) |
|
| 306 | + ) { |
|
| 307 | + $width = $w; |
|
| 308 | + } |
|
| 309 | + else { |
|
| 310 | + // si on recupere la taille de la viewbox mais si la viewbox est petite on met un multiplicateur pour la taille finale |
|
| 311 | + $width = $viewBox[2]; |
|
| 312 | + if ($width < 1) { |
|
| 313 | + $coeff = max($coeff, 1000); |
|
| 314 | + } |
|
| 315 | + elseif ($width < 10) { |
|
| 316 | + $coeff = max($coeff, 100); |
|
| 317 | + } |
|
| 318 | + elseif ($width < 100) { |
|
| 319 | + $coeff = max($coeff, 10); |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + if ( |
|
| 323 | + isset($attributs['height']) |
|
| 324 | + and $h = svg_dimension_to_pixels($attributs['height']) |
|
| 325 | + ) { |
|
| 326 | + $height = $h; |
|
| 327 | + } |
|
| 328 | + else { |
|
| 329 | + $height = $viewBox[3]; |
|
| 330 | + if ($height < 1) { |
|
| 331 | + $coeff = max($coeff, 1000); |
|
| 332 | + } |
|
| 333 | + elseif ($height < 10) { |
|
| 334 | + $coeff = max($coeff, 100); |
|
| 335 | + } |
|
| 336 | + elseif ($height < 100) { |
|
| 337 | + $coeff = max($coeff, 10); |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + // arrondir le width et height en pixel in fine |
|
| 342 | + $width = round($coeff * $width); |
|
| 343 | + $height = round($coeff * $height); |
|
| 344 | + |
|
| 345 | + $viewBox = implode(' ', $viewBox); |
|
| 346 | + |
|
| 347 | + return [$width, $height, $viewBox]; |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | 350 | /** |
@@ -360,25 +360,25 @@ discard block |
||
| 360 | 360 | * @return string |
| 361 | 361 | */ |
| 362 | 362 | function svg_force_viewBox_px($img, $force_width_and_height = false) { |
| 363 | - if ( |
|
| 364 | - $svg = svg_charger($img) |
|
| 365 | - and $svg_infos = svg_lire_balise_svg($svg) |
|
| 366 | - ) { |
|
| 367 | - [$balise_svg, $attributs] = $svg_infos; |
|
| 363 | + if ( |
|
| 364 | + $svg = svg_charger($img) |
|
| 365 | + and $svg_infos = svg_lire_balise_svg($svg) |
|
| 366 | + ) { |
|
| 367 | + [$balise_svg, $attributs] = $svg_infos; |
|
| 368 | 368 | |
| 369 | - [$width, $height, $viewBox] = svg_getimagesize_from_attr($attributs); |
|
| 369 | + [$width, $height, $viewBox] = svg_getimagesize_from_attr($attributs); |
|
| 370 | 370 | |
| 371 | - if ($force_width_and_height) { |
|
| 372 | - $attributs['width'] = $width; |
|
| 373 | - $attributs['height'] = $height; |
|
| 374 | - } |
|
| 371 | + if ($force_width_and_height) { |
|
| 372 | + $attributs['width'] = $width; |
|
| 373 | + $attributs['height'] = $height; |
|
| 374 | + } |
|
| 375 | 375 | |
| 376 | - $attributs['viewBox'] = $viewBox; |
|
| 376 | + $attributs['viewBox'] = $viewBox; |
|
| 377 | 377 | |
| 378 | - $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 379 | - return $svg; |
|
| 380 | - } |
|
| 381 | - return $img; |
|
| 378 | + $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 379 | + return $svg; |
|
| 380 | + } |
|
| 381 | + return $img; |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -387,12 +387,12 @@ discard block |
||
| 387 | 387 | * @return array|mixed |
| 388 | 388 | */ |
| 389 | 389 | function svg_extract_couleurs($img) { |
| 390 | - if ($svg = svg_charger($img)) { |
|
| 391 | - if (preg_match_all('/(#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])|(rgb\([\s\d]+,[\s\d]+,[\s\d]+\))|(#[0-9a-f][0-9a-f][0-9a-f])/imS', $svg, $matches)) { |
|
| 392 | - return $matches[0]; |
|
| 393 | - } |
|
| 394 | - } |
|
| 395 | - return []; |
|
| 390 | + if ($svg = svg_charger($img)) { |
|
| 391 | + if (preg_match_all('/(#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])|(rgb\([\s\d]+,[\s\d]+,[\s\d]+\))|(#[0-9a-f][0-9a-f][0-9a-f])/imS', $svg, $matches)) { |
|
| 392 | + return $matches[0]; |
|
| 393 | + } |
|
| 394 | + } |
|
| 395 | + return []; |
|
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 398 | /** |
@@ -403,58 +403,58 @@ discard block |
||
| 403 | 403 | * @return bool|string |
| 404 | 404 | */ |
| 405 | 405 | function svg_recadrer($img, $new_width, $new_height, $offset_width, $offset_height, $background_color = '') { |
| 406 | - if ( |
|
| 407 | - $svg = svg_force_viewBox_px($img) |
|
| 408 | - and $svg_infos = svg_lire_balise_svg($svg) |
|
| 409 | - ) { |
|
| 410 | - [$balise_svg, $attributs] = $svg_infos; |
|
| 411 | - $viewBox = explode(' ', $attributs['viewBox']); |
|
| 412 | - |
|
| 413 | - $viewport_w = $new_width; |
|
| 414 | - $viewport_h = $new_height; |
|
| 415 | - $viewport_ox = $offset_width; |
|
| 416 | - $viewport_oy = $offset_height; |
|
| 417 | - |
|
| 418 | - // si on a un width/height qui rescale, il faut rescaler |
|
| 419 | - if ( |
|
| 420 | - isset($attributs['width']) |
|
| 421 | - and $w = svg_dimension_to_pixels($attributs['width']) |
|
| 422 | - and isset($attributs['height']) |
|
| 423 | - and $h = svg_dimension_to_pixels($attributs['height']) |
|
| 424 | - ) { |
|
| 425 | - $xscale = $viewBox[2] / $w; |
|
| 426 | - $viewport_w = round($viewport_w * $xscale, 2); |
|
| 427 | - $viewport_ox = round($viewport_ox * $xscale, 2); |
|
| 428 | - $yscale = $viewBox[3] / $h; |
|
| 429 | - $viewport_h = round($viewport_h * $yscale, 2); |
|
| 430 | - $viewport_oy = round($viewport_oy * $yscale, 2); |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - if ($viewport_w > $viewBox[2] or $viewport_h > $viewBox[3]) { |
|
| 434 | - $svg = svg_clip_in_box($svg, $viewBox[0], $viewBox[1], $viewBox[2], $viewBox[3]); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - // maintenant on redefinit la viewBox |
|
| 438 | - $viewBox[0] += $viewport_ox; |
|
| 439 | - $viewBox[1] += $viewport_oy; |
|
| 440 | - $viewBox[2] = $viewport_w; |
|
| 441 | - $viewBox[3] = $viewport_h; |
|
| 442 | - |
|
| 443 | - $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 444 | - $attributs['width'] = strval($new_width); |
|
| 445 | - $attributs['height'] = strval($new_height); |
|
| 446 | - |
|
| 447 | - $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 448 | - |
|
| 449 | - // ajouter un background |
|
| 450 | - if ($background_color and $background_color !== 'transparent') { |
|
| 451 | - $svg = svg_ajouter_background($svg, $background_color); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - return $svg; |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - return $img; |
|
| 406 | + if ( |
|
| 407 | + $svg = svg_force_viewBox_px($img) |
|
| 408 | + and $svg_infos = svg_lire_balise_svg($svg) |
|
| 409 | + ) { |
|
| 410 | + [$balise_svg, $attributs] = $svg_infos; |
|
| 411 | + $viewBox = explode(' ', $attributs['viewBox']); |
|
| 412 | + |
|
| 413 | + $viewport_w = $new_width; |
|
| 414 | + $viewport_h = $new_height; |
|
| 415 | + $viewport_ox = $offset_width; |
|
| 416 | + $viewport_oy = $offset_height; |
|
| 417 | + |
|
| 418 | + // si on a un width/height qui rescale, il faut rescaler |
|
| 419 | + if ( |
|
| 420 | + isset($attributs['width']) |
|
| 421 | + and $w = svg_dimension_to_pixels($attributs['width']) |
|
| 422 | + and isset($attributs['height']) |
|
| 423 | + and $h = svg_dimension_to_pixels($attributs['height']) |
|
| 424 | + ) { |
|
| 425 | + $xscale = $viewBox[2] / $w; |
|
| 426 | + $viewport_w = round($viewport_w * $xscale, 2); |
|
| 427 | + $viewport_ox = round($viewport_ox * $xscale, 2); |
|
| 428 | + $yscale = $viewBox[3] / $h; |
|
| 429 | + $viewport_h = round($viewport_h * $yscale, 2); |
|
| 430 | + $viewport_oy = round($viewport_oy * $yscale, 2); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + if ($viewport_w > $viewBox[2] or $viewport_h > $viewBox[3]) { |
|
| 434 | + $svg = svg_clip_in_box($svg, $viewBox[0], $viewBox[1], $viewBox[2], $viewBox[3]); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + // maintenant on redefinit la viewBox |
|
| 438 | + $viewBox[0] += $viewport_ox; |
|
| 439 | + $viewBox[1] += $viewport_oy; |
|
| 440 | + $viewBox[2] = $viewport_w; |
|
| 441 | + $viewBox[3] = $viewport_h; |
|
| 442 | + |
|
| 443 | + $attributs['viewBox'] = implode(' ', $viewBox); |
|
| 444 | + $attributs['width'] = strval($new_width); |
|
| 445 | + $attributs['height'] = strval($new_height); |
|
| 446 | + |
|
| 447 | + $svg = svg_change_balise_svg($svg, $balise_svg, $attributs); |
|
| 448 | + |
|
| 449 | + // ajouter un background |
|
| 450 | + if ($background_color and $background_color !== 'transparent') { |
|
| 451 | + $svg = svg_ajouter_background($svg, $background_color); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + return $svg; |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + return $img; |
|
| 458 | 458 | } |
| 459 | 459 | |
| 460 | 460 | /** |
@@ -464,26 +464,26 @@ discard block |
||
| 464 | 464 | * @return bool|string |
| 465 | 465 | */ |
| 466 | 466 | function svg_ajouter_background($img, $background_color) { |
| 467 | - if ( |
|
| 468 | - $svg = svg_charger($img) |
|
| 469 | - and $svg_infos = svg_lire_balise_svg($svg) |
|
| 470 | - ) { |
|
| 471 | - if ($background_color and $background_color !== 'transparent') { |
|
| 472 | - [$balise_svg, $attributs] = $svg_infos; |
|
| 473 | - |
|
| 474 | - $background_color = svg_couleur_to_hexa($background_color); |
|
| 475 | - if (isset($attributs['viewBox'])) { |
|
| 476 | - $viewBox = explode(' ', $attributs['viewBox']); |
|
| 477 | - $rect = '<rect x="' . $viewBox[0] . '" y="' . $viewBox[1] . '" width="' . $viewBox[2] . '" height="' . $viewBox[3] . "\" fill=\"$background_color\"/>"; |
|
| 478 | - } |
|
| 479 | - else { |
|
| 480 | - $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 481 | - } |
|
| 482 | - $svg = svg_insert_shapes($svg, $rect); |
|
| 483 | - } |
|
| 484 | - return $svg; |
|
| 485 | - } |
|
| 486 | - return $img; |
|
| 467 | + if ( |
|
| 468 | + $svg = svg_charger($img) |
|
| 469 | + and $svg_infos = svg_lire_balise_svg($svg) |
|
| 470 | + ) { |
|
| 471 | + if ($background_color and $background_color !== 'transparent') { |
|
| 472 | + [$balise_svg, $attributs] = $svg_infos; |
|
| 473 | + |
|
| 474 | + $background_color = svg_couleur_to_hexa($background_color); |
|
| 475 | + if (isset($attributs['viewBox'])) { |
|
| 476 | + $viewBox = explode(' ', $attributs['viewBox']); |
|
| 477 | + $rect = '<rect x="' . $viewBox[0] . '" y="' . $viewBox[1] . '" width="' . $viewBox[2] . '" height="' . $viewBox[3] . "\" fill=\"$background_color\"/>"; |
|
| 478 | + } |
|
| 479 | + else { |
|
| 480 | + $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 481 | + } |
|
| 482 | + $svg = svg_insert_shapes($svg, $rect); |
|
| 483 | + } |
|
| 484 | + return $svg; |
|
| 485 | + } |
|
| 486 | + return $img; |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | |
@@ -494,26 +494,26 @@ discard block |
||
| 494 | 494 | * @return bool|string |
| 495 | 495 | */ |
| 496 | 496 | function svg_ajouter_voile($img, $background_color, $opacity) { |
| 497 | - if ( |
|
| 498 | - $svg = svg_charger($img) |
|
| 499 | - and $svg_infos = svg_lire_balise_svg($svg) |
|
| 500 | - ) { |
|
| 501 | - if ($background_color and $background_color !== 'transparent') { |
|
| 502 | - [$balise_svg, $attributs] = $svg_infos; |
|
| 503 | - |
|
| 504 | - $background_color = svg_couleur_to_hexa($background_color); |
|
| 505 | - if (isset($attributs['viewBox'])) { |
|
| 506 | - $viewBox = explode(' ', $attributs['viewBox']); |
|
| 507 | - $rect = '<rect x="' . $viewBox[0] . '" y="' . $viewBox[1] . '" width="' . $viewBox[2] . '" height="' . $viewBox[3] . "\" fill=\"$background_color\" opacity=\"$opacity\"/>"; |
|
| 508 | - } |
|
| 509 | - else { |
|
| 510 | - $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 511 | - } |
|
| 512 | - $svg = svg_insert_shapes($svg, $rect, false); |
|
| 513 | - } |
|
| 514 | - return $svg; |
|
| 515 | - } |
|
| 516 | - return $img; |
|
| 497 | + if ( |
|
| 498 | + $svg = svg_charger($img) |
|
| 499 | + and $svg_infos = svg_lire_balise_svg($svg) |
|
| 500 | + ) { |
|
| 501 | + if ($background_color and $background_color !== 'transparent') { |
|
| 502 | + [$balise_svg, $attributs] = $svg_infos; |
|
| 503 | + |
|
| 504 | + $background_color = svg_couleur_to_hexa($background_color); |
|
| 505 | + if (isset($attributs['viewBox'])) { |
|
| 506 | + $viewBox = explode(' ', $attributs['viewBox']); |
|
| 507 | + $rect = '<rect x="' . $viewBox[0] . '" y="' . $viewBox[1] . '" width="' . $viewBox[2] . '" height="' . $viewBox[3] . "\" fill=\"$background_color\" opacity=\"$opacity\"/>"; |
|
| 508 | + } |
|
| 509 | + else { |
|
| 510 | + $rect = "<rect width=\"100%\" height=\"100%\" fill=\"$background_color\"/>"; |
|
| 511 | + } |
|
| 512 | + $svg = svg_insert_shapes($svg, $rect, false); |
|
| 513 | + } |
|
| 514 | + return $svg; |
|
| 515 | + } |
|
| 516 | + return $img; |
|
| 517 | 517 | } |
| 518 | 518 | |
| 519 | 519 | |
@@ -524,27 +524,27 @@ discard block |
||
| 524 | 524 | * @return bool|string |
| 525 | 525 | */ |
| 526 | 526 | function svg_transformer($img, $attributs) { |
| 527 | - if ( |
|
| 528 | - $svg = svg_charger($img) |
|
| 529 | - and $svg_infos = svg_lire_balise_svg($svg) |
|
| 530 | - ) { |
|
| 531 | - if ($attributs) { |
|
| 532 | - [$balise_svg, ] = $svg_infos; |
|
| 533 | - $g = '<g'; |
|
| 534 | - foreach ($attributs as $k => $v) { |
|
| 535 | - if (strlen($v)) { |
|
| 536 | - $g .= " $k=\"" . attribut_html($v) . '"'; |
|
| 537 | - } |
|
| 538 | - } |
|
| 539 | - if (strlen($g) > 2) { |
|
| 540 | - $g .= '>'; |
|
| 541 | - $svg = svg_insert_shapes($svg, $g); |
|
| 542 | - $svg = svg_insert_shapes($svg, '</g>', false); |
|
| 543 | - } |
|
| 544 | - } |
|
| 545 | - return $svg; |
|
| 546 | - } |
|
| 547 | - return $img; |
|
| 527 | + if ( |
|
| 528 | + $svg = svg_charger($img) |
|
| 529 | + and $svg_infos = svg_lire_balise_svg($svg) |
|
| 530 | + ) { |
|
| 531 | + if ($attributs) { |
|
| 532 | + [$balise_svg, ] = $svg_infos; |
|
| 533 | + $g = '<g'; |
|
| 534 | + foreach ($attributs as $k => $v) { |
|
| 535 | + if (strlen($v)) { |
|
| 536 | + $g .= " $k=\"" . attribut_html($v) . '"'; |
|
| 537 | + } |
|
| 538 | + } |
|
| 539 | + if (strlen($g) > 2) { |
|
| 540 | + $g .= '>'; |
|
| 541 | + $svg = svg_insert_shapes($svg, $g); |
|
| 542 | + $svg = svg_insert_shapes($svg, '</g>', false); |
|
| 543 | + } |
|
| 544 | + } |
|
| 545 | + return $svg; |
|
| 546 | + } |
|
| 547 | + return $img; |
|
| 548 | 548 | } |
| 549 | 549 | |
| 550 | 550 | /** |
@@ -555,21 +555,21 @@ discard block |
||
| 555 | 555 | * @return bool|string |
| 556 | 556 | */ |
| 557 | 557 | function svg_apply_filter($img, $filter_def) { |
| 558 | - if ( |
|
| 559 | - $svg = svg_charger($img) |
|
| 560 | - and $svg_infos = svg_lire_balise_svg($svg) |
|
| 561 | - ) { |
|
| 562 | - if ($filter_def) { |
|
| 563 | - [$balise_svg, ] = $svg_infos; |
|
| 564 | - $filter_id = 'filter-' . substr(md5($filter_def . strlen($svg)), 0, 8); |
|
| 565 | - $filter = "<defs><filter id=\"$filter_id\">$filter_def</filter></defs>"; |
|
| 566 | - $g = "<g filter=\"url(#$filter_id)\">"; |
|
| 567 | - $svg = svg_insert_shapes($svg, $filter . $g); |
|
| 568 | - $svg = svg_insert_shapes($svg, '</g>', false); |
|
| 569 | - } |
|
| 570 | - return $svg; |
|
| 571 | - } |
|
| 572 | - return $img; |
|
| 558 | + if ( |
|
| 559 | + $svg = svg_charger($img) |
|
| 560 | + and $svg_infos = svg_lire_balise_svg($svg) |
|
| 561 | + ) { |
|
| 562 | + if ($filter_def) { |
|
| 563 | + [$balise_svg, ] = $svg_infos; |
|
| 564 | + $filter_id = 'filter-' . substr(md5($filter_def . strlen($svg)), 0, 8); |
|
| 565 | + $filter = "<defs><filter id=\"$filter_id\">$filter_def</filter></defs>"; |
|
| 566 | + $g = "<g filter=\"url(#$filter_id)\">"; |
|
| 567 | + $svg = svg_insert_shapes($svg, $filter . $g); |
|
| 568 | + $svg = svg_insert_shapes($svg, '</g>', false); |
|
| 569 | + } |
|
| 570 | + return $svg; |
|
| 571 | + } |
|
| 572 | + return $img; |
|
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | /** |
@@ -579,8 +579,8 @@ discard block |
||
| 579 | 579 | * @return string |
| 580 | 580 | */ |
| 581 | 581 | function svg_filter_blur($img, $blur_width) { |
| 582 | - $blur_width = intval($blur_width); |
|
| 583 | - return svg_apply_filter($img, "<feGaussianBlur stdDeviation=\"$blur_width\"/>"); |
|
| 582 | + $blur_width = intval($blur_width); |
|
| 583 | + return svg_apply_filter($img, "<feGaussianBlur stdDeviation=\"$blur_width\"/>"); |
|
| 584 | 584 | } |
| 585 | 585 | |
| 586 | 586 | /** |
@@ -590,10 +590,10 @@ discard block |
||
| 590 | 590 | * @return bool|string |
| 591 | 591 | */ |
| 592 | 592 | function svg_filter_grayscale($img, $intensity) { |
| 593 | - $value = round(1.0 - $intensity, 2); |
|
| 594 | - //$filter = "<feColorMatrix type=\"matrix\" values=\"0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\"/>"; |
|
| 595 | - $filter = "<feColorMatrix type=\"saturate\" values=\"$value\"/>"; |
|
| 596 | - return svg_apply_filter($img, $filter); |
|
| 593 | + $value = round(1.0 - $intensity, 2); |
|
| 594 | + //$filter = "<feColorMatrix type=\"matrix\" values=\"0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\"/>"; |
|
| 595 | + $filter = "<feColorMatrix type=\"saturate\" values=\"$value\"/>"; |
|
| 596 | + return svg_apply_filter($img, $filter); |
|
| 597 | 597 | } |
| 598 | 598 | |
| 599 | 599 | /** |
@@ -603,8 +603,8 @@ discard block |
||
| 603 | 603 | * @return bool|string |
| 604 | 604 | */ |
| 605 | 605 | function svg_filter_sepia($img, $intensity) { |
| 606 | - $filter = '<feColorMatrix type="matrix" values="0.30 0.30 0.30 0.0 0 0.25 0.25 0.25 0.0 0 0.20 0.20 0.20 0.0 0 0.00 0.00 0.00 1 0"/>'; |
|
| 607 | - return svg_apply_filter($img, $filter); |
|
| 606 | + $filter = '<feColorMatrix type="matrix" values="0.30 0.30 0.30 0.0 0 0.25 0.25 0.25 0.0 0 0.20 0.20 0.20 0.0 0 0.00 0.00 0.00 1 0"/>'; |
|
| 607 | + return svg_apply_filter($img, $filter); |
|
| 608 | 608 | } |
| 609 | 609 | |
| 610 | 610 | /** |
@@ -614,31 +614,31 @@ discard block |
||
| 614 | 614 | * @return bool|string |
| 615 | 615 | */ |
| 616 | 616 | function svg_flip($img, $HorV) { |
| 617 | - if ( |
|
| 618 | - $svg = svg_force_viewBox_px($img) |
|
| 619 | - and $svg_infos = svg_lire_balise_svg($svg) |
|
| 620 | - ) { |
|
| 621 | - [$balise_svg, $atts] = $svg_infos; |
|
| 622 | - $viewBox = explode(' ', $atts['viewBox']); |
|
| 623 | - |
|
| 624 | - if (!in_array($HorV, ['h', 'H'])) { |
|
| 625 | - $transform = 'scale(-1,1)'; |
|
| 626 | - |
|
| 627 | - $x = intval($viewBox[0]) + intval($viewBox[2] / 2); |
|
| 628 | - $mx = -$x; |
|
| 629 | - $transform = "translate($x, 0) $transform translate($mx, 0)"; |
|
| 630 | - } |
|
| 631 | - else { |
|
| 632 | - $transform = 'scale(1,-1)'; |
|
| 633 | - |
|
| 634 | - $y = intval($viewBox[1]) + intval($viewBox[3] / 2); |
|
| 635 | - $my = -$y; |
|
| 636 | - $transform = "translate(0, $y) $transform translate(0, $my)"; |
|
| 637 | - } |
|
| 638 | - $svg = svg_transformer($svg, ['transform' => $transform]); |
|
| 639 | - return $svg; |
|
| 640 | - } |
|
| 641 | - return $img; |
|
| 617 | + if ( |
|
| 618 | + $svg = svg_force_viewBox_px($img) |
|
| 619 | + and $svg_infos = svg_lire_balise_svg($svg) |
|
| 620 | + ) { |
|
| 621 | + [$balise_svg, $atts] = $svg_infos; |
|
| 622 | + $viewBox = explode(' ', $atts['viewBox']); |
|
| 623 | + |
|
| 624 | + if (!in_array($HorV, ['h', 'H'])) { |
|
| 625 | + $transform = 'scale(-1,1)'; |
|
| 626 | + |
|
| 627 | + $x = intval($viewBox[0]) + intval($viewBox[2] / 2); |
|
| 628 | + $mx = -$x; |
|
| 629 | + $transform = "translate($x, 0) $transform translate($mx, 0)"; |
|
| 630 | + } |
|
| 631 | + else { |
|
| 632 | + $transform = 'scale(1,-1)'; |
|
| 633 | + |
|
| 634 | + $y = intval($viewBox[1]) + intval($viewBox[3] / 2); |
|
| 635 | + $my = -$y; |
|
| 636 | + $transform = "translate(0, $y) $transform translate(0, $my)"; |
|
| 637 | + } |
|
| 638 | + $svg = svg_transformer($svg, ['transform' => $transform]); |
|
| 639 | + return $svg; |
|
| 640 | + } |
|
| 641 | + return $img; |
|
| 642 | 642 | } |
| 643 | 643 | |
| 644 | 644 | /** |
@@ -652,20 +652,20 @@ discard block |
||
| 652 | 652 | * @return bool|string |
| 653 | 653 | */ |
| 654 | 654 | function svg_rotate($img, $angle, $center_x, $center_y) { |
| 655 | - if ( |
|
| 656 | - $svg = svg_force_viewBox_px($img) |
|
| 657 | - and $svg_infos = svg_lire_balise_svg($svg) |
|
| 658 | - ) { |
|
| 659 | - [$balise_svg, $atts] = $svg_infos; |
|
| 660 | - $viewBox = explode(' ', $atts['viewBox']); |
|
| 661 | - |
|
| 662 | - $center_x = round($viewBox[0] + $center_x * $viewBox[2]); |
|
| 663 | - $center_y = round($viewBox[1] + $center_y * $viewBox[3]); |
|
| 664 | - $svg = svg_transformer($svg, ['transform' => "rotate($angle $center_x $center_y)"]); |
|
| 665 | - |
|
| 666 | - return $svg; |
|
| 667 | - } |
|
| 668 | - return $img; |
|
| 655 | + if ( |
|
| 656 | + $svg = svg_force_viewBox_px($img) |
|
| 657 | + and $svg_infos = svg_lire_balise_svg($svg) |
|
| 658 | + ) { |
|
| 659 | + [$balise_svg, $atts] = $svg_infos; |
|
| 660 | + $viewBox = explode(' ', $atts['viewBox']); |
|
| 661 | + |
|
| 662 | + $center_x = round($viewBox[0] + $center_x * $viewBox[2]); |
|
| 663 | + $center_y = round($viewBox[1] + $center_y * $viewBox[3]); |
|
| 664 | + $svg = svg_transformer($svg, ['transform' => "rotate($angle $center_x $center_y)"]); |
|
| 665 | + |
|
| 666 | + return $svg; |
|
| 667 | + } |
|
| 668 | + return $img; |
|
| 669 | 669 | } |
| 670 | 670 | |
| 671 | 671 | /** |
@@ -677,41 +677,41 @@ discard block |
||
| 677 | 677 | * @return bool|mixed|string |
| 678 | 678 | */ |
| 679 | 679 | function svg_filtrer_couleurs($img, $callback_filter) { |
| 680 | - if ( |
|
| 681 | - $svg = svg_force_viewBox_px($img) |
|
| 682 | - and $colors = svg_extract_couleurs($svg) |
|
| 683 | - ) { |
|
| 684 | - $colors = array_unique($colors); |
|
| 685 | - |
|
| 686 | - $short = []; |
|
| 687 | - $long = []; |
|
| 688 | - while (count($colors)) { |
|
| 689 | - $c = array_shift($colors); |
|
| 690 | - if (strlen($c) == 4) { |
|
| 691 | - $short[] = $c; |
|
| 692 | - } |
|
| 693 | - else { |
|
| 694 | - $long[] = $c; |
|
| 695 | - } |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - $colors = [...$long, ...$short]; |
|
| 699 | - $new_colors = []; |
|
| 700 | - $colors = array_flip($colors); |
|
| 701 | - foreach ($colors as $c => $k) { |
|
| 702 | - $colors[$c] = "@@@COLOR$$k$@@@"; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - |
|
| 706 | - foreach ($colors as $original => $replace) { |
|
| 707 | - $new = svg_couleur_to_hexa($original); |
|
| 708 | - $new_colors[$replace] = $callback_filter($new); |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - $svg = str_replace(array_keys($colors), array_values($colors), $svg); |
|
| 712 | - $svg = str_replace(array_keys($new_colors), array_values($new_colors), $svg); |
|
| 713 | - |
|
| 714 | - return $svg; |
|
| 715 | - } |
|
| 716 | - return $img; |
|
| 680 | + if ( |
|
| 681 | + $svg = svg_force_viewBox_px($img) |
|
| 682 | + and $colors = svg_extract_couleurs($svg) |
|
| 683 | + ) { |
|
| 684 | + $colors = array_unique($colors); |
|
| 685 | + |
|
| 686 | + $short = []; |
|
| 687 | + $long = []; |
|
| 688 | + while (count($colors)) { |
|
| 689 | + $c = array_shift($colors); |
|
| 690 | + if (strlen($c) == 4) { |
|
| 691 | + $short[] = $c; |
|
| 692 | + } |
|
| 693 | + else { |
|
| 694 | + $long[] = $c; |
|
| 695 | + } |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + $colors = [...$long, ...$short]; |
|
| 699 | + $new_colors = []; |
|
| 700 | + $colors = array_flip($colors); |
|
| 701 | + foreach ($colors as $c => $k) { |
|
| 702 | + $colors[$c] = "@@@COLOR$$k$@@@"; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + |
|
| 706 | + foreach ($colors as $original => $replace) { |
|
| 707 | + $new = svg_couleur_to_hexa($original); |
|
| 708 | + $new_colors[$replace] = $callback_filter($new); |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + $svg = str_replace(array_keys($colors), array_values($colors), $svg); |
|
| 712 | + $svg = str_replace(array_keys($new_colors), array_values($new_colors), $svg); |
|
| 713 | + |
|
| 714 | + return $svg; |
|
| 715 | + } |
|
| 716 | + return $img; |
|
| 717 | 717 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | **/ |
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | |
@@ -44,18 +44,18 @@ discard block |
||
| 44 | 44 | * Pile complétée par le code à générer |
| 45 | 45 | */ |
| 46 | 46 | function balise_BOITE_OUVRIR_dist($p) { |
| 47 | - $_titre = interprete_argument_balise(1, $p); |
|
| 48 | - $_class = interprete_argument_balise(2, $p); |
|
| 49 | - $_head_class = interprete_argument_balise(3, $p); |
|
| 50 | - $_titre = ($_titre ?: "''"); |
|
| 51 | - $_class = ($_class ? ", $_class" : ", 'simple'"); |
|
| 52 | - $_head_class = ($_head_class ? ", $_head_class" : ''); |
|
| 53 | - |
|
| 54 | - $f = chercher_filtre('boite_ouvrir'); |
|
| 55 | - $p->code = "$f($_titre$_class$_head_class)"; |
|
| 56 | - $p->interdire_scripts = false; |
|
| 57 | - |
|
| 58 | - return $p; |
|
| 47 | + $_titre = interprete_argument_balise(1, $p); |
|
| 48 | + $_class = interprete_argument_balise(2, $p); |
|
| 49 | + $_head_class = interprete_argument_balise(3, $p); |
|
| 50 | + $_titre = ($_titre ?: "''"); |
|
| 51 | + $_class = ($_class ? ", $_class" : ", 'simple'"); |
|
| 52 | + $_head_class = ($_head_class ? ", $_head_class" : ''); |
|
| 53 | + |
|
| 54 | + $f = chercher_filtre('boite_ouvrir'); |
|
| 55 | + $p->code = "$f($_titre$_class$_head_class)"; |
|
| 56 | + $p->interdire_scripts = false; |
|
| 57 | + |
|
| 58 | + return $p; |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -75,14 +75,14 @@ discard block |
||
| 75 | 75 | * Pile complétée par le code à générer |
| 76 | 76 | */ |
| 77 | 77 | function balise_BOITE_PIED_dist($p) { |
| 78 | - $_class = interprete_argument_balise(1, $p); |
|
| 79 | - $_class = ($_class ? "$_class" : ''); |
|
| 78 | + $_class = interprete_argument_balise(1, $p); |
|
| 79 | + $_class = ($_class ? "$_class" : ''); |
|
| 80 | 80 | |
| 81 | - $f = chercher_filtre('boite_pied'); |
|
| 82 | - $p->code = "$f($_class)"; |
|
| 83 | - $p->interdire_scripts = false; |
|
| 81 | + $f = chercher_filtre('boite_pied'); |
|
| 82 | + $p->code = "$f($_class)"; |
|
| 83 | + $p->interdire_scripts = false; |
|
| 84 | 84 | |
| 85 | - return $p; |
|
| 85 | + return $p; |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | |
@@ -102,11 +102,11 @@ discard block |
||
| 102 | 102 | * Pile complétée par le code à générer |
| 103 | 103 | */ |
| 104 | 104 | function balise_BOITE_FERMER_dist($p) { |
| 105 | - $f = chercher_filtre('boite_fermer'); |
|
| 106 | - $p->code = "$f()"; |
|
| 107 | - $p->interdire_scripts = false; |
|
| 105 | + $f = chercher_filtre('boite_fermer'); |
|
| 106 | + $p->code = "$f()"; |
|
| 107 | + $p->interdire_scripts = false; |
|
| 108 | 108 | |
| 109 | - return $p; |
|
| 109 | + return $p; |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -128,16 +128,16 @@ discard block |
||
| 128 | 128 | * HTML du début de la boîte |
| 129 | 129 | */ |
| 130 | 130 | function boite_ouvrir($titre, $class = '', $head_class = '', $id = '') { |
| 131 | - $class = "box $class"; |
|
| 132 | - $head_class = "box__header $head_class clearfix"; |
|
| 133 | - // dans l'espace prive, titrer en h3 si pas de balise <hn> |
|
| 134 | - if (test_espace_prive() and strlen($titre) and strpos($titre, '<h') === false) { |
|
| 135 | - $titre = "<h3>$titre</h3>"; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - return '<div class="' . $class . ($id ? "\" id=\"$id" : '') . '">' |
|
| 139 | - . ($titre ? "<div class=\"$head_class\">$titre<!--/hd--></div>" : '') |
|
| 140 | - . '<div class="box__body clearfix">'; |
|
| 131 | + $class = "box $class"; |
|
| 132 | + $head_class = "box__header $head_class clearfix"; |
|
| 133 | + // dans l'espace prive, titrer en h3 si pas de balise <hn> |
|
| 134 | + if (test_espace_prive() and strlen($titre) and strpos($titre, '<h') === false) { |
|
| 135 | + $titre = "<h3>$titre</h3>"; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + return '<div class="' . $class . ($id ? "\" id=\"$id" : '') . '">' |
|
| 139 | + . ($titre ? "<div class=\"$head_class\">$titre<!--/hd--></div>" : '') |
|
| 140 | + . '<div class="box__body clearfix">'; |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | |
@@ -154,10 +154,10 @@ discard block |
||
| 154 | 154 | * HTML de transition vers le pied de la boîte |
| 155 | 155 | */ |
| 156 | 156 | function boite_pied($class = 'act') { |
| 157 | - $class = "box__footer $class"; |
|
| 157 | + $class = "box__footer $class"; |
|
| 158 | 158 | |
| 159 | - return '</div>' |
|
| 160 | - . "<div class=\"$class clearfix\">"; |
|
| 159 | + return '</div>' |
|
| 160 | + . "<div class=\"$class clearfix\">"; |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | |
@@ -172,6 +172,6 @@ discard block |
||
| 172 | 172 | * HTML de fin de la boîte |
| 173 | 173 | */ |
| 174 | 174 | function boite_fermer() { |
| 175 | - return '</div>' |
|
| 176 | - . '</div>'; |
|
| 175 | + return '</div>' |
|
| 176 | + . '</div>'; |
|
| 177 | 177 | } |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -25,51 +25,51 @@ discard block |
||
| 25 | 25 | * privée ou dans un de ses sous menus |
| 26 | 26 | */ |
| 27 | 27 | class Bouton { |
| 28 | - /** @var string L'icone à mettre dans le bouton */ |
|
| 29 | - public $icone; |
|
| 30 | - |
|
| 31 | - /** @var string Le nom de l'entrée d'i18n associé */ |
|
| 32 | - public $libelle; |
|
| 33 | - |
|
| 34 | - /** @var null|string L'URL de la page (null => ?exec=nom) */ |
|
| 35 | - public $url = null; |
|
| 36 | - |
|
| 37 | - /** @var null|string|array Arguments supplementaires de l'URL */ |
|
| 38 | - public $urlArg = null; |
|
| 39 | - |
|
| 40 | - /** @var null|string URL du javascript */ |
|
| 41 | - public $url2 = null; |
|
| 42 | - |
|
| 43 | - /** @var null|string Pour ouvrir dans une fenetre a part */ |
|
| 44 | - public $target = null; |
|
| 45 | - |
|
| 46 | - /** @var null|mixed Sous-barre de boutons / onglets */ |
|
| 47 | - public $sousmenu = null; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Définit un bouton |
|
| 51 | - * |
|
| 52 | - * @param string $icone |
|
| 53 | - * L'icone à mettre dans le bouton |
|
| 54 | - * @param string $libelle |
|
| 55 | - * Le nom de l'entrée i18n associé |
|
| 56 | - * @param null|string $url |
|
| 57 | - * L'URL de la page |
|
| 58 | - * @param null|string|array $urlArg |
|
| 59 | - * Arguments supplémentaires de l'URL |
|
| 60 | - * @param null|string $url2 |
|
| 61 | - * URL du javascript |
|
| 62 | - * @param null|mixed $target |
|
| 63 | - * Pour ouvrir une fenêtre à part |
|
| 64 | - */ |
|
| 65 | - public function __construct($icone, $libelle, $url = null, $urlArg = null, $url2 = null, $target = null) { |
|
| 66 | - $this->icone = $icone; |
|
| 67 | - $this->libelle = $libelle; |
|
| 68 | - $this->url = $url; |
|
| 69 | - $this->urlArg = $urlArg; |
|
| 70 | - $this->url2 = $url2; |
|
| 71 | - $this->target = $target; |
|
| 72 | - } |
|
| 28 | + /** @var string L'icone à mettre dans le bouton */ |
|
| 29 | + public $icone; |
|
| 30 | + |
|
| 31 | + /** @var string Le nom de l'entrée d'i18n associé */ |
|
| 32 | + public $libelle; |
|
| 33 | + |
|
| 34 | + /** @var null|string L'URL de la page (null => ?exec=nom) */ |
|
| 35 | + public $url = null; |
|
| 36 | + |
|
| 37 | + /** @var null|string|array Arguments supplementaires de l'URL */ |
|
| 38 | + public $urlArg = null; |
|
| 39 | + |
|
| 40 | + /** @var null|string URL du javascript */ |
|
| 41 | + public $url2 = null; |
|
| 42 | + |
|
| 43 | + /** @var null|string Pour ouvrir dans une fenetre a part */ |
|
| 44 | + public $target = null; |
|
| 45 | + |
|
| 46 | + /** @var null|mixed Sous-barre de boutons / onglets */ |
|
| 47 | + public $sousmenu = null; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Définit un bouton |
|
| 51 | + * |
|
| 52 | + * @param string $icone |
|
| 53 | + * L'icone à mettre dans le bouton |
|
| 54 | + * @param string $libelle |
|
| 55 | + * Le nom de l'entrée i18n associé |
|
| 56 | + * @param null|string $url |
|
| 57 | + * L'URL de la page |
|
| 58 | + * @param null|string|array $urlArg |
|
| 59 | + * Arguments supplémentaires de l'URL |
|
| 60 | + * @param null|string $url2 |
|
| 61 | + * URL du javascript |
|
| 62 | + * @param null|mixed $target |
|
| 63 | + * Pour ouvrir une fenêtre à part |
|
| 64 | + */ |
|
| 65 | + public function __construct($icone, $libelle, $url = null, $urlArg = null, $url2 = null, $target = null) { |
|
| 66 | + $this->icone = $icone; |
|
| 67 | + $this->libelle = $libelle; |
|
| 68 | + $this->url = $url; |
|
| 69 | + $this->urlArg = $urlArg; |
|
| 70 | + $this->url2 = $url2; |
|
| 71 | + $this->target = $target; |
|
| 72 | + } |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | |
@@ -86,35 +86,35 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | function definir_barre_onglets($script) { |
| 88 | 88 | |
| 89 | - $onglets = []; |
|
| 90 | - $liste_onglets = []; |
|
| 91 | - |
|
| 92 | - // ajouter les onglets issus des plugin via paquet.xml |
|
| 93 | - if (function_exists('onglets_plugins')) { |
|
| 94 | - $liste_onglets = onglets_plugins(); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - foreach ($liste_onglets as $id => $infos) { |
|
| 99 | - if ( |
|
| 100 | - ($parent = $infos['parent']) |
|
| 101 | - && $parent == $script |
|
| 102 | - && autoriser('onglet', "_$id") |
|
| 103 | - ) { |
|
| 104 | - $onglets[$id] = new Bouton( |
|
| 105 | - isset($infos['icone']) ? find_in_theme($infos['icone']) : '', // icone |
|
| 106 | - $infos['titre'], // titre |
|
| 107 | - (isset($infos['action']) and $infos['action']) |
|
| 108 | - ? generer_url_ecrire( |
|
| 109 | - $infos['action'], |
|
| 110 | - (isset($infos['parametres']) and $infos['parametres']) ? $infos['parametres'] : '' |
|
| 111 | - ) |
|
| 112 | - : null |
|
| 113 | - ); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return pipeline('ajouter_onglets', ['data' => $onglets, 'args' => $script]); |
|
| 89 | + $onglets = []; |
|
| 90 | + $liste_onglets = []; |
|
| 91 | + |
|
| 92 | + // ajouter les onglets issus des plugin via paquet.xml |
|
| 93 | + if (function_exists('onglets_plugins')) { |
|
| 94 | + $liste_onglets = onglets_plugins(); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + foreach ($liste_onglets as $id => $infos) { |
|
| 99 | + if ( |
|
| 100 | + ($parent = $infos['parent']) |
|
| 101 | + && $parent == $script |
|
| 102 | + && autoriser('onglet', "_$id") |
|
| 103 | + ) { |
|
| 104 | + $onglets[$id] = new Bouton( |
|
| 105 | + isset($infos['icone']) ? find_in_theme($infos['icone']) : '', // icone |
|
| 106 | + $infos['titre'], // titre |
|
| 107 | + (isset($infos['action']) and $infos['action']) |
|
| 108 | + ? generer_url_ecrire( |
|
| 109 | + $infos['action'], |
|
| 110 | + (isset($infos['parametres']) and $infos['parametres']) ? $infos['parametres'] : '' |
|
| 111 | + ) |
|
| 112 | + : null |
|
| 113 | + ); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return pipeline('ajouter_onglets', ['data' => $onglets, 'args' => $script]); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | |
@@ -133,14 +133,14 @@ discard block |
||
| 133 | 133 | * @return string |
| 134 | 134 | */ |
| 135 | 135 | function barre_onglets($rubrique, $ongletCourant, $class = 'barre_onglet') { |
| 136 | - include_spip('inc/presentation'); |
|
| 136 | + include_spip('inc/presentation'); |
|
| 137 | 137 | |
| 138 | - $res = ''; |
|
| 138 | + $res = ''; |
|
| 139 | 139 | |
| 140 | - foreach (definir_barre_onglets($rubrique) as $exec => $onglet) { |
|
| 141 | - $url = $onglet->url ?: generer_url_ecrire($exec); |
|
| 142 | - $res .= onglet(_T($onglet->libelle), $url, $exec, $ongletCourant, $onglet->icone); |
|
| 143 | - } |
|
| 140 | + foreach (definir_barre_onglets($rubrique) as $exec => $onglet) { |
|
| 141 | + $url = $onglet->url ?: generer_url_ecrire($exec); |
|
| 142 | + $res .= onglet(_T($onglet->libelle), $url, $exec, $ongletCourant, $onglet->icone); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - return !$res ? '' : (debut_onglet($class) . $res . fin_onglet()); |
|
| 145 | + return !$res ? '' : (debut_onglet($class) . $res . fin_onglet()); |
|
| 146 | 146 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | // securité |
| 24 | 24 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 25 | - return; |
|
| 25 | + return; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | // se faciliter la lecture du charset |
@@ -42,45 +42,45 @@ discard block |
||
| 42 | 42 | * - false si le charset n'est pas décrit dans le répertoire charsets/ |
| 43 | 43 | **/ |
| 44 | 44 | function load_charset($charset = 'AUTO') { |
| 45 | - if ($charset == 'AUTO') { |
|
| 46 | - $charset = $GLOBALS['meta']['charset']; |
|
| 47 | - } |
|
| 48 | - $charset = trim(strtolower($charset)); |
|
| 49 | - if (isset($GLOBALS['CHARSET'][$charset])) { |
|
| 50 | - return $charset; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - if ($charset == 'utf-8') { |
|
| 54 | - $GLOBALS['CHARSET'][$charset] = []; |
|
| 55 | - |
|
| 56 | - return $charset; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - // Quelques synonymes |
|
| 60 | - if ($charset == '') { |
|
| 61 | - $charset = 'iso-8859-1'; |
|
| 62 | - } else { |
|
| 63 | - if ($charset == 'windows-1250') { |
|
| 64 | - $charset = 'cp1250'; |
|
| 65 | - } else { |
|
| 66 | - if ($charset == 'windows-1251') { |
|
| 67 | - $charset = 'cp1251'; |
|
| 68 | - } else { |
|
| 69 | - if ($charset == 'windows-1256') { |
|
| 70 | - $charset = 'cp1256'; |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 77 | - return $charset; |
|
| 78 | - } else { |
|
| 79 | - spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
|
| 80 | - $GLOBALS['CHARSET'][$charset] = []; |
|
| 81 | - |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 45 | + if ($charset == 'AUTO') { |
|
| 46 | + $charset = $GLOBALS['meta']['charset']; |
|
| 47 | + } |
|
| 48 | + $charset = trim(strtolower($charset)); |
|
| 49 | + if (isset($GLOBALS['CHARSET'][$charset])) { |
|
| 50 | + return $charset; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + if ($charset == 'utf-8') { |
|
| 54 | + $GLOBALS['CHARSET'][$charset] = []; |
|
| 55 | + |
|
| 56 | + return $charset; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + // Quelques synonymes |
|
| 60 | + if ($charset == '') { |
|
| 61 | + $charset = 'iso-8859-1'; |
|
| 62 | + } else { |
|
| 63 | + if ($charset == 'windows-1250') { |
|
| 64 | + $charset = 'cp1250'; |
|
| 65 | + } else { |
|
| 66 | + if ($charset == 'windows-1251') { |
|
| 67 | + $charset = 'cp1251'; |
|
| 68 | + } else { |
|
| 69 | + if ($charset == 'windows-1256') { |
|
| 70 | + $charset = 'cp1256'; |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 77 | + return $charset; |
|
| 78 | + } else { |
|
| 79 | + spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
|
| 80 | + $GLOBALS['CHARSET'][$charset] = []; |
|
| 81 | + |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | |
@@ -91,31 +91,31 @@ discard block |
||
| 91 | 91 | * true si toutes les fonctions mb nécessaires sont présentes |
| 92 | 92 | **/ |
| 93 | 93 | function init_mb_string() { |
| 94 | - static $mb; |
|
| 95 | - |
|
| 96 | - // verifier que tout est present (fonctions mb_string pour php >= 4.0.6) |
|
| 97 | - // et que le charset interne est connu de mb_string |
|
| 98 | - if (!$mb) { |
|
| 99 | - if ( |
|
| 100 | - function_exists('mb_internal_encoding') |
|
| 101 | - and function_exists('mb_detect_order') |
|
| 102 | - and function_exists('mb_substr') |
|
| 103 | - and function_exists('mb_strlen') |
|
| 104 | - and function_exists('mb_strtolower') |
|
| 105 | - and function_exists('mb_strtoupper') |
|
| 106 | - and function_exists('mb_encode_mimeheader') |
|
| 107 | - and function_exists('mb_encode_numericentity') |
|
| 108 | - and function_exists('mb_decode_numericentity') |
|
| 109 | - and mb_detect_order(lire_config('charset', _DEFAULT_CHARSET)) |
|
| 110 | - ) { |
|
| 111 | - mb_internal_encoding('utf-8'); |
|
| 112 | - $mb = 1; |
|
| 113 | - } else { |
|
| 114 | - $mb = -1; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return ($mb == 1); |
|
| 94 | + static $mb; |
|
| 95 | + |
|
| 96 | + // verifier que tout est present (fonctions mb_string pour php >= 4.0.6) |
|
| 97 | + // et que le charset interne est connu de mb_string |
|
| 98 | + if (!$mb) { |
|
| 99 | + if ( |
|
| 100 | + function_exists('mb_internal_encoding') |
|
| 101 | + and function_exists('mb_detect_order') |
|
| 102 | + and function_exists('mb_substr') |
|
| 103 | + and function_exists('mb_strlen') |
|
| 104 | + and function_exists('mb_strtolower') |
|
| 105 | + and function_exists('mb_strtoupper') |
|
| 106 | + and function_exists('mb_encode_mimeheader') |
|
| 107 | + and function_exists('mb_encode_numericentity') |
|
| 108 | + and function_exists('mb_decode_numericentity') |
|
| 109 | + and mb_detect_order(lire_config('charset', _DEFAULT_CHARSET)) |
|
| 110 | + ) { |
|
| 111 | + mb_internal_encoding('utf-8'); |
|
| 112 | + $mb = 1; |
|
| 113 | + } else { |
|
| 114 | + $mb = -1; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return ($mb == 1); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | /** |
@@ -130,21 +130,21 @@ discard block |
||
| 130 | 130 | * true si iconv fonctionne correctement |
| 131 | 131 | **/ |
| 132 | 132 | function test_iconv() { |
| 133 | - static $iconv_ok; |
|
| 134 | - |
|
| 135 | - if (!$iconv_ok) { |
|
| 136 | - if (!function_exists('iconv')) { |
|
| 137 | - $iconv_ok = -1; |
|
| 138 | - } else { |
|
| 139 | - if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) == 'chaine de test') { |
|
| 140 | - $iconv_ok = 1; |
|
| 141 | - } else { |
|
| 142 | - $iconv_ok = -1; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return ($iconv_ok == 1); |
|
| 133 | + static $iconv_ok; |
|
| 134 | + |
|
| 135 | + if (!$iconv_ok) { |
|
| 136 | + if (!function_exists('iconv')) { |
|
| 137 | + $iconv_ok = -1; |
|
| 138 | + } else { |
|
| 139 | + if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) == 'chaine de test') { |
|
| 140 | + $iconv_ok = 1; |
|
| 141 | + } else { |
|
| 142 | + $iconv_ok = -1; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return ($iconv_ok == 1); |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | |
@@ -157,18 +157,18 @@ discard block |
||
| 157 | 157 | * true si PCRE supporte l'UTF-8 correctement |
| 158 | 158 | **/ |
| 159 | 159 | function test_pcre_unicode() { |
| 160 | - static $pcre_ok = 0; |
|
| 161 | - |
|
| 162 | - if (!$pcre_ok) { |
|
| 163 | - $s = ' ' . chr(195) . chr(169) . 't' . chr(195) . chr(169) . ' '; |
|
| 164 | - if (preg_match(',\W...\W,u', $s)) { |
|
| 165 | - $pcre_ok = 1; |
|
| 166 | - } else { |
|
| 167 | - $pcre_ok = -1; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return $pcre_ok == 1; |
|
| 160 | + static $pcre_ok = 0; |
|
| 161 | + |
|
| 162 | + if (!$pcre_ok) { |
|
| 163 | + $s = ' ' . chr(195) . chr(169) . 't' . chr(195) . chr(169) . ' '; |
|
| 164 | + if (preg_match(',\W...\W,u', $s)) { |
|
| 165 | + $pcre_ok = 1; |
|
| 166 | + } else { |
|
| 167 | + $pcre_ok = -1; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return $pcre_ok == 1; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -184,22 +184,22 @@ discard block |
||
| 184 | 184 | * Plage de caractères |
| 185 | 185 | **/ |
| 186 | 186 | function pcre_lettres_unicode() { |
| 187 | - static $plage_unicode; |
|
| 188 | - |
|
| 189 | - if (!$plage_unicode) { |
|
| 190 | - if (test_pcre_unicode()) { |
|
| 191 | - // cf. http://www.unicode.org/charts/ |
|
| 192 | - $plage_unicode = '\w' // iso-latin |
|
| 193 | - . '\x{100}-\x{24f}' // europeen etendu |
|
| 194 | - . '\x{300}-\x{1cff}' // des tas de trucs |
|
| 195 | - ; |
|
| 196 | - } else { |
|
| 197 | - // fallback a trois sous |
|
| 198 | - $plage_unicode = '\w'; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return $plage_unicode; |
|
| 187 | + static $plage_unicode; |
|
| 188 | + |
|
| 189 | + if (!$plage_unicode) { |
|
| 190 | + if (test_pcre_unicode()) { |
|
| 191 | + // cf. http://www.unicode.org/charts/ |
|
| 192 | + $plage_unicode = '\w' // iso-latin |
|
| 193 | + . '\x{100}-\x{24f}' // europeen etendu |
|
| 194 | + . '\x{300}-\x{1cff}' // des tas de trucs |
|
| 195 | + ; |
|
| 196 | + } else { |
|
| 197 | + // fallback a trois sous |
|
| 198 | + $plage_unicode = '\w'; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return $plage_unicode; |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | * Plage de caractères |
| 218 | 218 | **/ |
| 219 | 219 | function plage_punct_unicode() { |
| 220 | - return '\xE2(\x80[\x80-\xBF]|\x81[\x80-\xAF])'; |
|
| 220 | + return '\xE2(\x80[\x80-\xBF]|\x81[\x80-\xAF])'; |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | /** |
@@ -237,75 +237,75 @@ discard block |
||
| 237 | 237 | * Texte corrigé |
| 238 | 238 | **/ |
| 239 | 239 | function corriger_caracteres_windows($texte, $charset = 'AUTO', $charset_cible = 'unicode') { |
| 240 | - static $trans; |
|
| 241 | - |
|
| 242 | - if (is_array($texte)) { |
|
| 243 | - return array_map('corriger_caracteres_windows', $texte); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - if ($charset == 'AUTO') { |
|
| 247 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 248 | - } |
|
| 249 | - if ($charset == 'utf-8') { |
|
| 250 | - $p = chr(194); |
|
| 251 | - if (strpos($texte, $p) == false) { |
|
| 252 | - return $texte; |
|
| 253 | - } |
|
| 254 | - } else { |
|
| 255 | - if ($charset == 'iso-8859-1') { |
|
| 256 | - $p = ''; |
|
| 257 | - } else { |
|
| 258 | - return $texte; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - if (!isset($trans[$charset][$charset_cible])) { |
|
| 263 | - $trans[$charset][$charset_cible] = [ |
|
| 264 | - $p . chr(128) => '€', |
|
| 265 | - $p . chr(129) => ' ', # pas affecte |
|
| 266 | - $p . chr(130) => '‚', |
|
| 267 | - $p . chr(131) => 'ƒ', |
|
| 268 | - $p . chr(132) => '„', |
|
| 269 | - $p . chr(133) => '…', |
|
| 270 | - $p . chr(134) => '†', |
|
| 271 | - $p . chr(135) => '‡', |
|
| 272 | - $p . chr(136) => 'ˆ', |
|
| 273 | - $p . chr(137) => '‰', |
|
| 274 | - $p . chr(138) => 'Š', |
|
| 275 | - $p . chr(139) => '‹', |
|
| 276 | - $p . chr(140) => 'Œ', |
|
| 277 | - $p . chr(141) => ' ', # pas affecte |
|
| 278 | - $p . chr(142) => 'Ž', |
|
| 279 | - $p . chr(143) => ' ', # pas affecte |
|
| 280 | - $p . chr(144) => ' ', # pas affecte |
|
| 281 | - $p . chr(145) => '‘', |
|
| 282 | - $p . chr(146) => '’', |
|
| 283 | - $p . chr(147) => '“', |
|
| 284 | - $p . chr(148) => '”', |
|
| 285 | - $p . chr(149) => '•', |
|
| 286 | - $p . chr(150) => '–', |
|
| 287 | - $p . chr(151) => '—', |
|
| 288 | - $p . chr(152) => '˜', |
|
| 289 | - $p . chr(153) => '™', |
|
| 290 | - $p . chr(154) => 'š', |
|
| 291 | - $p . chr(155) => '›', |
|
| 292 | - $p . chr(156) => 'œ', |
|
| 293 | - $p . chr(157) => ' ', # pas affecte |
|
| 294 | - $p . chr(158) => 'ž', |
|
| 295 | - $p . chr(159) => 'Ÿ', |
|
| 296 | - ]; |
|
| 297 | - if ($charset_cible != 'unicode') { |
|
| 298 | - foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 299 | - $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - return @str_replace( |
|
| 305 | - array_keys($trans[$charset][$charset_cible]), |
|
| 306 | - array_values($trans[$charset][$charset_cible]), |
|
| 307 | - $texte |
|
| 308 | - ); |
|
| 240 | + static $trans; |
|
| 241 | + |
|
| 242 | + if (is_array($texte)) { |
|
| 243 | + return array_map('corriger_caracteres_windows', $texte); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + if ($charset == 'AUTO') { |
|
| 247 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 248 | + } |
|
| 249 | + if ($charset == 'utf-8') { |
|
| 250 | + $p = chr(194); |
|
| 251 | + if (strpos($texte, $p) == false) { |
|
| 252 | + return $texte; |
|
| 253 | + } |
|
| 254 | + } else { |
|
| 255 | + if ($charset == 'iso-8859-1') { |
|
| 256 | + $p = ''; |
|
| 257 | + } else { |
|
| 258 | + return $texte; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + if (!isset($trans[$charset][$charset_cible])) { |
|
| 263 | + $trans[$charset][$charset_cible] = [ |
|
| 264 | + $p . chr(128) => '€', |
|
| 265 | + $p . chr(129) => ' ', # pas affecte |
|
| 266 | + $p . chr(130) => '‚', |
|
| 267 | + $p . chr(131) => 'ƒ', |
|
| 268 | + $p . chr(132) => '„', |
|
| 269 | + $p . chr(133) => '…', |
|
| 270 | + $p . chr(134) => '†', |
|
| 271 | + $p . chr(135) => '‡', |
|
| 272 | + $p . chr(136) => 'ˆ', |
|
| 273 | + $p . chr(137) => '‰', |
|
| 274 | + $p . chr(138) => 'Š', |
|
| 275 | + $p . chr(139) => '‹', |
|
| 276 | + $p . chr(140) => 'Œ', |
|
| 277 | + $p . chr(141) => ' ', # pas affecte |
|
| 278 | + $p . chr(142) => 'Ž', |
|
| 279 | + $p . chr(143) => ' ', # pas affecte |
|
| 280 | + $p . chr(144) => ' ', # pas affecte |
|
| 281 | + $p . chr(145) => '‘', |
|
| 282 | + $p . chr(146) => '’', |
|
| 283 | + $p . chr(147) => '“', |
|
| 284 | + $p . chr(148) => '”', |
|
| 285 | + $p . chr(149) => '•', |
|
| 286 | + $p . chr(150) => '–', |
|
| 287 | + $p . chr(151) => '—', |
|
| 288 | + $p . chr(152) => '˜', |
|
| 289 | + $p . chr(153) => '™', |
|
| 290 | + $p . chr(154) => 'š', |
|
| 291 | + $p . chr(155) => '›', |
|
| 292 | + $p . chr(156) => 'œ', |
|
| 293 | + $p . chr(157) => ' ', # pas affecte |
|
| 294 | + $p . chr(158) => 'ž', |
|
| 295 | + $p . chr(159) => 'Ÿ', |
|
| 296 | + ]; |
|
| 297 | + if ($charset_cible != 'unicode') { |
|
| 298 | + foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 299 | + $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + return @str_replace( |
|
| 305 | + array_keys($trans[$charset][$charset_cible]), |
|
| 306 | + array_values($trans[$charset][$charset_cible]), |
|
| 307 | + $texte |
|
| 308 | + ); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | |
@@ -322,26 +322,26 @@ discard block |
||
| 322 | 322 | * Texte converti |
| 323 | 323 | **/ |
| 324 | 324 | function html2unicode($texte, $secure = false) { |
| 325 | - if (strpos($texte, '&') === false) { |
|
| 326 | - return $texte; |
|
| 327 | - } |
|
| 328 | - static $trans = []; |
|
| 329 | - if (!$trans) { |
|
| 330 | - load_charset('html'); |
|
| 331 | - foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 332 | - $trans["&$key;"] = $val; |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - if ($secure) { |
|
| 337 | - return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 338 | - } else { |
|
| 339 | - return str_replace( |
|
| 340 | - ['&', '"', '<', '>'], |
|
| 341 | - ['&', '"', '<', '>'], |
|
| 342 | - str_replace(array_keys($trans), array_values($trans), $texte) |
|
| 343 | - ); |
|
| 344 | - } |
|
| 325 | + if (strpos($texte, '&') === false) { |
|
| 326 | + return $texte; |
|
| 327 | + } |
|
| 328 | + static $trans = []; |
|
| 329 | + if (!$trans) { |
|
| 330 | + load_charset('html'); |
|
| 331 | + foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 332 | + $trans["&$key;"] = $val; |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + if ($secure) { |
|
| 337 | + return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 338 | + } else { |
|
| 339 | + return str_replace( |
|
| 340 | + ['&', '"', '<', '>'], |
|
| 341 | + ['&', '"', '<', '>'], |
|
| 342 | + str_replace(array_keys($trans), array_values($trans), $texte) |
|
| 343 | + ); |
|
| 344 | + } |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | |
@@ -356,16 +356,16 @@ discard block |
||
| 356 | 356 | * Texte converti |
| 357 | 357 | **/ |
| 358 | 358 | function mathml2unicode($texte) { |
| 359 | - static $trans; |
|
| 360 | - if (!$trans) { |
|
| 361 | - load_charset('mathml'); |
|
| 359 | + static $trans; |
|
| 360 | + if (!$trans) { |
|
| 361 | + load_charset('mathml'); |
|
| 362 | 362 | |
| 363 | - foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 364 | - $trans["&$key;"] = $val; |
|
| 365 | - } |
|
| 366 | - } |
|
| 363 | + foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 364 | + $trans["&$key;"] = $val; |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | 367 | |
| 368 | - return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 368 | + return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | |
@@ -387,75 +387,75 @@ discard block |
||
| 387 | 387 | * Texte converti en unicode |
| 388 | 388 | **/ |
| 389 | 389 | function charset2unicode($texte, $charset = 'AUTO' /* $forcer: obsolete*/) { |
| 390 | - static $trans; |
|
| 391 | - |
|
| 392 | - if ($charset == 'AUTO') { |
|
| 393 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - if ($charset == '') { |
|
| 397 | - $charset = 'iso-8859-1'; |
|
| 398 | - } |
|
| 399 | - $charset = strtolower($charset); |
|
| 400 | - |
|
| 401 | - switch ($charset) { |
|
| 402 | - case 'utf-8': |
|
| 403 | - case 'utf8': |
|
| 404 | - return utf_8_to_unicode($texte); |
|
| 405 | - |
|
| 406 | - case 'iso-8859-1': |
|
| 407 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 408 | - // pas de break; ici, on suit sur default: |
|
| 409 | - |
|
| 410 | - default: |
|
| 411 | - // mbstring presente ? |
|
| 412 | - if (init_mb_string()) { |
|
| 413 | - $order = mb_detect_order(); |
|
| 414 | - try { |
|
| 415 | - # mb_string connait-il $charset? |
|
| 416 | - if ($order and mb_detect_order($charset)) { |
|
| 417 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 418 | - if ($s && $s != $texte) { |
|
| 419 | - return utf_8_to_unicode($s); |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - } catch (\Error $e) { |
|
| 423 | - // Le charset n'existe probablement pas |
|
| 424 | - } finally { |
|
| 425 | - mb_detect_order($order); # remettre comme precedemment |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 430 | - if (!isset($trans[$charset])) { |
|
| 431 | - if ( |
|
| 432 | - $cset = load_charset($charset) |
|
| 433 | - and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 434 | - ) { |
|
| 435 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 436 | - $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - if (isset($trans[$charset]) and is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 441 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 445 | - // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 446 | - // surtout en utf-8, gere ci-dessus) |
|
| 447 | - if (test_iconv()) { |
|
| 448 | - $s = iconv($charset, 'utf-32le', $texte); |
|
| 449 | - if ($s) { |
|
| 450 | - return utf_32_to_unicode($s); |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - // Au pire ne rien faire |
|
| 455 | - spip_log("erreur charset '$charset' non supporte"); |
|
| 456 | - |
|
| 457 | - return $texte; |
|
| 458 | - } |
|
| 390 | + static $trans; |
|
| 391 | + |
|
| 392 | + if ($charset == 'AUTO') { |
|
| 393 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + if ($charset == '') { |
|
| 397 | + $charset = 'iso-8859-1'; |
|
| 398 | + } |
|
| 399 | + $charset = strtolower($charset); |
|
| 400 | + |
|
| 401 | + switch ($charset) { |
|
| 402 | + case 'utf-8': |
|
| 403 | + case 'utf8': |
|
| 404 | + return utf_8_to_unicode($texte); |
|
| 405 | + |
|
| 406 | + case 'iso-8859-1': |
|
| 407 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 408 | + // pas de break; ici, on suit sur default: |
|
| 409 | + |
|
| 410 | + default: |
|
| 411 | + // mbstring presente ? |
|
| 412 | + if (init_mb_string()) { |
|
| 413 | + $order = mb_detect_order(); |
|
| 414 | + try { |
|
| 415 | + # mb_string connait-il $charset? |
|
| 416 | + if ($order and mb_detect_order($charset)) { |
|
| 417 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 418 | + if ($s && $s != $texte) { |
|
| 419 | + return utf_8_to_unicode($s); |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + } catch (\Error $e) { |
|
| 423 | + // Le charset n'existe probablement pas |
|
| 424 | + } finally { |
|
| 425 | + mb_detect_order($order); # remettre comme precedemment |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 430 | + if (!isset($trans[$charset])) { |
|
| 431 | + if ( |
|
| 432 | + $cset = load_charset($charset) |
|
| 433 | + and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 434 | + ) { |
|
| 435 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 436 | + $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + if (isset($trans[$charset]) and is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 441 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 445 | + // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 446 | + // surtout en utf-8, gere ci-dessus) |
|
| 447 | + if (test_iconv()) { |
|
| 448 | + $s = iconv($charset, 'utf-32le', $texte); |
|
| 449 | + if ($s) { |
|
| 450 | + return utf_32_to_unicode($s); |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + // Au pire ne rien faire |
|
| 455 | + spip_log("erreur charset '$charset' non supporte"); |
|
| 456 | + |
|
| 457 | + return $texte; |
|
| 458 | + } |
|
| 459 | 459 | } |
| 460 | 460 | |
| 461 | 461 | |
@@ -474,44 +474,44 @@ discard block |
||
| 474 | 474 | * Texte transformé dans le charset souhaité |
| 475 | 475 | **/ |
| 476 | 476 | function unicode2charset($texte, $charset = 'AUTO') { |
| 477 | - static $CHARSET_REVERSE = []; |
|
| 478 | - static $trans = []; |
|
| 479 | - |
|
| 480 | - if ($charset == 'AUTO') { |
|
| 481 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - switch ($charset) { |
|
| 485 | - case 'utf-8': |
|
| 486 | - return unicode_to_utf_8($texte); |
|
| 487 | - break; |
|
| 488 | - |
|
| 489 | - default: |
|
| 490 | - $charset = load_charset($charset); |
|
| 491 | - |
|
| 492 | - if (empty($CHARSET_REVERSE[$charset])) { |
|
| 493 | - $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - if (!isset($trans[$charset])) { |
|
| 497 | - $trans[$charset] = []; |
|
| 498 | - $t = &$trans[$charset]; |
|
| 499 | - for ($e = 128; $e < 255; $e++) { |
|
| 500 | - $h = dechex($e); |
|
| 501 | - if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 502 | - $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 503 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 504 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 505 | - } else { |
|
| 506 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 507 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 508 | - } |
|
| 509 | - } |
|
| 510 | - } |
|
| 511 | - $texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 512 | - |
|
| 513 | - return $texte; |
|
| 514 | - } |
|
| 477 | + static $CHARSET_REVERSE = []; |
|
| 478 | + static $trans = []; |
|
| 479 | + |
|
| 480 | + if ($charset == 'AUTO') { |
|
| 481 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + switch ($charset) { |
|
| 485 | + case 'utf-8': |
|
| 486 | + return unicode_to_utf_8($texte); |
|
| 487 | + break; |
|
| 488 | + |
|
| 489 | + default: |
|
| 490 | + $charset = load_charset($charset); |
|
| 491 | + |
|
| 492 | + if (empty($CHARSET_REVERSE[$charset])) { |
|
| 493 | + $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + if (!isset($trans[$charset])) { |
|
| 497 | + $trans[$charset] = []; |
|
| 498 | + $t = &$trans[$charset]; |
|
| 499 | + for ($e = 128; $e < 255; $e++) { |
|
| 500 | + $h = dechex($e); |
|
| 501 | + if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 502 | + $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 503 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 504 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 505 | + } else { |
|
| 506 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 507 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 508 | + } |
|
| 509 | + } |
|
| 510 | + } |
|
| 511 | + $texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 512 | + |
|
| 513 | + return $texte; |
|
| 514 | + } |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | |
@@ -529,40 +529,40 @@ discard block |
||
| 529 | 529 | * Texte transformé dans le charset site |
| 530 | 530 | **/ |
| 531 | 531 | function importer_charset($texte, $charset = 'AUTO') { |
| 532 | - $s = null; |
|
| 533 | - static $trans = []; |
|
| 534 | - // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 535 | - if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 536 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 537 | - if (init_mb_string()) { |
|
| 538 | - if ( |
|
| 539 | - $order = mb_detect_order() # mb_string connait-il $charset? |
|
| 540 | - and mb_detect_order($charset) |
|
| 541 | - ) { |
|
| 542 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 543 | - } |
|
| 544 | - mb_detect_order($order); # remettre comme precedemment |
|
| 545 | - return $s; |
|
| 546 | - } |
|
| 547 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 548 | - if (!isset($trans[$charset])) { |
|
| 549 | - if ( |
|
| 550 | - $cset = load_charset($charset) |
|
| 551 | - and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 552 | - ) { |
|
| 553 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 554 | - $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 555 | - } |
|
| 556 | - } |
|
| 557 | - } |
|
| 558 | - if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 559 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - return $texte; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - return unicode2charset(charset2unicode($texte, $charset)); |
|
| 532 | + $s = null; |
|
| 533 | + static $trans = []; |
|
| 534 | + // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 535 | + if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 536 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 537 | + if (init_mb_string()) { |
|
| 538 | + if ( |
|
| 539 | + $order = mb_detect_order() # mb_string connait-il $charset? |
|
| 540 | + and mb_detect_order($charset) |
|
| 541 | + ) { |
|
| 542 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 543 | + } |
|
| 544 | + mb_detect_order($order); # remettre comme precedemment |
|
| 545 | + return $s; |
|
| 546 | + } |
|
| 547 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 548 | + if (!isset($trans[$charset])) { |
|
| 549 | + if ( |
|
| 550 | + $cset = load_charset($charset) |
|
| 551 | + and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 552 | + ) { |
|
| 553 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 554 | + $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 555 | + } |
|
| 556 | + } |
|
| 557 | + } |
|
| 558 | + if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 559 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + return $texte; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + return unicode2charset(charset2unicode($texte, $charset)); |
|
| 566 | 566 | } |
| 567 | 567 | |
| 568 | 568 | |
@@ -578,92 +578,92 @@ discard block |
||
| 578 | 578 | **/ |
| 579 | 579 | function utf_8_to_unicode($source) { |
| 580 | 580 | |
| 581 | - // mb_string : methode rapide |
|
| 582 | - if (init_mb_string()) { |
|
| 583 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 584 | - |
|
| 585 | - return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - // Sinon methode pas a pas |
|
| 589 | - static $decrement; |
|
| 590 | - static $shift; |
|
| 591 | - |
|
| 592 | - // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 593 | - if (!is_array($decrement)) { |
|
| 594 | - // array used to figure what number to decrement from character order value |
|
| 595 | - // according to number of characters used to map unicode to ascii by utf-8 |
|
| 596 | - $decrement[4] = 240; |
|
| 597 | - $decrement[3] = 224; |
|
| 598 | - $decrement[2] = 192; |
|
| 599 | - $decrement[1] = 0; |
|
| 600 | - // the number of bits to shift each charNum by |
|
| 601 | - $shift[1][0] = 0; |
|
| 602 | - $shift[2][0] = 6; |
|
| 603 | - $shift[2][1] = 0; |
|
| 604 | - $shift[3][0] = 12; |
|
| 605 | - $shift[3][1] = 6; |
|
| 606 | - $shift[3][2] = 0; |
|
| 607 | - $shift[4][0] = 18; |
|
| 608 | - $shift[4][1] = 12; |
|
| 609 | - $shift[4][2] = 6; |
|
| 610 | - $shift[4][3] = 0; |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - $pos = 0; |
|
| 614 | - $len = strlen($source); |
|
| 615 | - $encodedString = ''; |
|
| 616 | - while ($pos < $len) { |
|
| 617 | - $char = ''; |
|
| 618 | - $ischar = false; |
|
| 619 | - $asciiPos = ord(substr($source, $pos, 1)); |
|
| 620 | - if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 621 | - // 4 chars representing one unicode character |
|
| 622 | - $thisLetter = substr($source, $pos, 4); |
|
| 623 | - $pos += 4; |
|
| 624 | - } else { |
|
| 625 | - if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 626 | - // 3 chars representing one unicode character |
|
| 627 | - $thisLetter = substr($source, $pos, 3); |
|
| 628 | - $pos += 3; |
|
| 629 | - } else { |
|
| 630 | - if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 631 | - // 2 chars representing one unicode character |
|
| 632 | - $thisLetter = substr($source, $pos, 2); |
|
| 633 | - $pos += 2; |
|
| 634 | - } else { |
|
| 635 | - // 1 char (lower ascii) |
|
| 636 | - $thisLetter = substr($source, $pos, 1); |
|
| 637 | - $pos += 1; |
|
| 638 | - $char = $thisLetter; |
|
| 639 | - $ischar = true; |
|
| 640 | - } |
|
| 641 | - } |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - if ($ischar) { |
|
| 645 | - $encodedString .= $char; |
|
| 646 | - } else { // process the string representing the letter to a unicode entity |
|
| 647 | - $thisLen = strlen($thisLetter); |
|
| 648 | - $thisPos = 0; |
|
| 649 | - $decimalCode = 0; |
|
| 650 | - while ($thisPos < $thisLen) { |
|
| 651 | - $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 652 | - if ($thisPos == 0) { |
|
| 653 | - $charNum = intval($thisCharOrd - $decrement[$thisLen]); |
|
| 654 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 655 | - } else { |
|
| 656 | - $charNum = intval($thisCharOrd - 128); |
|
| 657 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 658 | - } |
|
| 659 | - $thisPos++; |
|
| 660 | - } |
|
| 661 | - $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 662 | - $encodedString .= $encodedLetter; |
|
| 663 | - } |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - return $encodedString; |
|
| 581 | + // mb_string : methode rapide |
|
| 582 | + if (init_mb_string()) { |
|
| 583 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 584 | + |
|
| 585 | + return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + // Sinon methode pas a pas |
|
| 589 | + static $decrement; |
|
| 590 | + static $shift; |
|
| 591 | + |
|
| 592 | + // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 593 | + if (!is_array($decrement)) { |
|
| 594 | + // array used to figure what number to decrement from character order value |
|
| 595 | + // according to number of characters used to map unicode to ascii by utf-8 |
|
| 596 | + $decrement[4] = 240; |
|
| 597 | + $decrement[3] = 224; |
|
| 598 | + $decrement[2] = 192; |
|
| 599 | + $decrement[1] = 0; |
|
| 600 | + // the number of bits to shift each charNum by |
|
| 601 | + $shift[1][0] = 0; |
|
| 602 | + $shift[2][0] = 6; |
|
| 603 | + $shift[2][1] = 0; |
|
| 604 | + $shift[3][0] = 12; |
|
| 605 | + $shift[3][1] = 6; |
|
| 606 | + $shift[3][2] = 0; |
|
| 607 | + $shift[4][0] = 18; |
|
| 608 | + $shift[4][1] = 12; |
|
| 609 | + $shift[4][2] = 6; |
|
| 610 | + $shift[4][3] = 0; |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + $pos = 0; |
|
| 614 | + $len = strlen($source); |
|
| 615 | + $encodedString = ''; |
|
| 616 | + while ($pos < $len) { |
|
| 617 | + $char = ''; |
|
| 618 | + $ischar = false; |
|
| 619 | + $asciiPos = ord(substr($source, $pos, 1)); |
|
| 620 | + if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 621 | + // 4 chars representing one unicode character |
|
| 622 | + $thisLetter = substr($source, $pos, 4); |
|
| 623 | + $pos += 4; |
|
| 624 | + } else { |
|
| 625 | + if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 626 | + // 3 chars representing one unicode character |
|
| 627 | + $thisLetter = substr($source, $pos, 3); |
|
| 628 | + $pos += 3; |
|
| 629 | + } else { |
|
| 630 | + if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 631 | + // 2 chars representing one unicode character |
|
| 632 | + $thisLetter = substr($source, $pos, 2); |
|
| 633 | + $pos += 2; |
|
| 634 | + } else { |
|
| 635 | + // 1 char (lower ascii) |
|
| 636 | + $thisLetter = substr($source, $pos, 1); |
|
| 637 | + $pos += 1; |
|
| 638 | + $char = $thisLetter; |
|
| 639 | + $ischar = true; |
|
| 640 | + } |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + if ($ischar) { |
|
| 645 | + $encodedString .= $char; |
|
| 646 | + } else { // process the string representing the letter to a unicode entity |
|
| 647 | + $thisLen = strlen($thisLetter); |
|
| 648 | + $thisPos = 0; |
|
| 649 | + $decimalCode = 0; |
|
| 650 | + while ($thisPos < $thisLen) { |
|
| 651 | + $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 652 | + if ($thisPos == 0) { |
|
| 653 | + $charNum = intval($thisCharOrd - $decrement[$thisLen]); |
|
| 654 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 655 | + } else { |
|
| 656 | + $charNum = intval($thisCharOrd - 128); |
|
| 657 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 658 | + } |
|
| 659 | + $thisPos++; |
|
| 660 | + } |
|
| 661 | + $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 662 | + $encodedString .= $encodedLetter; |
|
| 663 | + } |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + return $encodedString; |
|
| 667 | 667 | } |
| 668 | 668 | |
| 669 | 669 | /** |
@@ -682,32 +682,32 @@ discard block |
||
| 682 | 682 | **/ |
| 683 | 683 | function utf_32_to_unicode($source) { |
| 684 | 684 | |
| 685 | - // mb_string : methode rapide |
|
| 686 | - if (init_mb_string()) { |
|
| 687 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 688 | - $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 689 | - |
|
| 690 | - return str_replace(chr(0), '', $source); |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - // Sinon methode lente |
|
| 694 | - $texte = ''; |
|
| 695 | - while ($source) { |
|
| 696 | - $words = unpack('V*', substr($source, 0, 1024)); |
|
| 697 | - $source = substr($source, 1024); |
|
| 698 | - foreach ($words as $word) { |
|
| 699 | - if ($word < 128) { |
|
| 700 | - $texte .= chr($word); |
|
| 701 | - } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 702 | - else { |
|
| 703 | - if ($word != 65279) { |
|
| 704 | - $texte .= '&#' . $word . ';'; |
|
| 705 | - } |
|
| 706 | - } |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - return $texte; |
|
| 685 | + // mb_string : methode rapide |
|
| 686 | + if (init_mb_string()) { |
|
| 687 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 688 | + $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 689 | + |
|
| 690 | + return str_replace(chr(0), '', $source); |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + // Sinon methode lente |
|
| 694 | + $texte = ''; |
|
| 695 | + while ($source) { |
|
| 696 | + $words = unpack('V*', substr($source, 0, 1024)); |
|
| 697 | + $source = substr($source, 1024); |
|
| 698 | + foreach ($words as $word) { |
|
| 699 | + if ($word < 128) { |
|
| 700 | + $texte .= chr($word); |
|
| 701 | + } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 702 | + else { |
|
| 703 | + if ($word != 65279) { |
|
| 704 | + $texte .= '&#' . $word . ';'; |
|
| 705 | + } |
|
| 706 | + } |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + return $texte; |
|
| 711 | 711 | } |
| 712 | 712 | |
| 713 | 713 | |
@@ -724,21 +724,21 @@ discard block |
||
| 724 | 724 | * Caractère utf8 si trouvé, '' sinon |
| 725 | 725 | **/ |
| 726 | 726 | function caractere_utf_8($num) { |
| 727 | - $num = intval($num); |
|
| 728 | - if ($num < 128) { |
|
| 729 | - return chr($num); |
|
| 730 | - } |
|
| 731 | - if ($num < 2048) { |
|
| 732 | - return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 733 | - } |
|
| 734 | - if ($num < 65536) { |
|
| 735 | - return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 736 | - } |
|
| 737 | - if ($num < 1_114_112) { |
|
| 738 | - return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - return ''; |
|
| 727 | + $num = intval($num); |
|
| 728 | + if ($num < 128) { |
|
| 729 | + return chr($num); |
|
| 730 | + } |
|
| 731 | + if ($num < 2048) { |
|
| 732 | + return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 733 | + } |
|
| 734 | + if ($num < 65536) { |
|
| 735 | + return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 736 | + } |
|
| 737 | + if ($num < 1_114_112) { |
|
| 738 | + return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + return ''; |
|
| 742 | 742 | } |
| 743 | 743 | |
| 744 | 744 | /** |
@@ -751,42 +751,42 @@ discard block |
||
| 751 | 751 | **/ |
| 752 | 752 | function unicode_to_utf_8($texte) { |
| 753 | 753 | |
| 754 | - // 1. Entites € et suivantes |
|
| 755 | - $vu = []; |
|
| 756 | - if ( |
|
| 757 | - preg_match_all( |
|
| 758 | - ',�*([1-9][0-9][0-9]+);,S', |
|
| 759 | - $texte, |
|
| 760 | - $regs, |
|
| 761 | - PREG_SET_ORDER |
|
| 762 | - ) |
|
| 763 | - ) { |
|
| 764 | - foreach ($regs as $reg) { |
|
| 765 | - if ($reg[1] > 127 and !isset($vu[$reg[0]])) { |
|
| 766 | - $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 767 | - } |
|
| 768 | - } |
|
| 769 | - } |
|
| 770 | - //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 771 | - |
|
| 772 | - // 2. Entites > ÿ |
|
| 773 | - //$vu = array(); |
|
| 774 | - if ( |
|
| 775 | - preg_match_all( |
|
| 776 | - ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 777 | - $texte, |
|
| 778 | - $regs, |
|
| 779 | - PREG_SET_ORDER |
|
| 780 | - ) |
|
| 781 | - ) { |
|
| 782 | - foreach ($regs as $reg) { |
|
| 783 | - if (!isset($vu[$reg[0]])) { |
|
| 784 | - $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 785 | - } |
|
| 786 | - } |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 754 | + // 1. Entites € et suivantes |
|
| 755 | + $vu = []; |
|
| 756 | + if ( |
|
| 757 | + preg_match_all( |
|
| 758 | + ',�*([1-9][0-9][0-9]+);,S', |
|
| 759 | + $texte, |
|
| 760 | + $regs, |
|
| 761 | + PREG_SET_ORDER |
|
| 762 | + ) |
|
| 763 | + ) { |
|
| 764 | + foreach ($regs as $reg) { |
|
| 765 | + if ($reg[1] > 127 and !isset($vu[$reg[0]])) { |
|
| 766 | + $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 767 | + } |
|
| 768 | + } |
|
| 769 | + } |
|
| 770 | + //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 771 | + |
|
| 772 | + // 2. Entites > ÿ |
|
| 773 | + //$vu = array(); |
|
| 774 | + if ( |
|
| 775 | + preg_match_all( |
|
| 776 | + ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 777 | + $texte, |
|
| 778 | + $regs, |
|
| 779 | + PREG_SET_ORDER |
|
| 780 | + ) |
|
| 781 | + ) { |
|
| 782 | + foreach ($regs as $reg) { |
|
| 783 | + if (!isset($vu[$reg[0]])) { |
|
| 784 | + $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 785 | + } |
|
| 786 | + } |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 790 | 790 | } |
| 791 | 791 | |
| 792 | 792 | /** |
@@ -798,15 +798,15 @@ discard block |
||
| 798 | 798 | * Texte converti |
| 799 | 799 | **/ |
| 800 | 800 | function unicode_to_javascript($texte) { |
| 801 | - $vu = []; |
|
| 802 | - while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
|
| 803 | - $num = $regs[1]; |
|
| 804 | - $vu[$num] = true; |
|
| 805 | - $s = '\u' . sprintf('%04x', $num); |
|
| 806 | - $texte = str_replace($regs[0], $s, $texte); |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - return $texte; |
|
| 801 | + $vu = []; |
|
| 802 | + while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
|
| 803 | + $num = $regs[1]; |
|
| 804 | + $vu[$num] = true; |
|
| 805 | + $s = '\u' . sprintf('%04x', $num); |
|
| 806 | + $texte = str_replace($regs[0], $s, $texte); |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + return $texte; |
|
| 810 | 810 | } |
| 811 | 811 | |
| 812 | 812 | /** |
@@ -818,11 +818,11 @@ discard block |
||
| 818 | 818 | * Texte converti |
| 819 | 819 | **/ |
| 820 | 820 | function javascript_to_unicode($texte) { |
| 821 | - while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 822 | - $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 823 | - } |
|
| 821 | + while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 822 | + $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 823 | + } |
|
| 824 | 824 | |
| 825 | - return $texte; |
|
| 825 | + return $texte; |
|
| 826 | 826 | } |
| 827 | 827 | |
| 828 | 828 | /** |
@@ -834,11 +834,11 @@ discard block |
||
| 834 | 834 | * Texte converti |
| 835 | 835 | **/ |
| 836 | 836 | function javascript_to_binary($texte) { |
| 837 | - while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 838 | - $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 839 | - } |
|
| 837 | + while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 838 | + $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 839 | + } |
|
| 840 | 840 | |
| 841 | - return $texte; |
|
| 841 | + return $texte; |
|
| 842 | 842 | } |
| 843 | 843 | |
| 844 | 844 | |
@@ -856,26 +856,26 @@ discard block |
||
| 856 | 856 | * @return string |
| 857 | 857 | */ |
| 858 | 858 | function translitteration_rapide($texte, $charset = 'AUTO', $complexe = '') { |
| 859 | - static $trans = []; |
|
| 860 | - if ($charset == 'AUTO') { |
|
| 861 | - $charset = $GLOBALS['meta']['charset']; |
|
| 862 | - } |
|
| 863 | - if (!strlen($texte)) { |
|
| 864 | - return $texte; |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - $table_translit = 'translit' . $complexe; |
|
| 868 | - |
|
| 869 | - // 2. Translitterer grace a la table predefinie |
|
| 870 | - if (!isset($trans[$complexe])) { |
|
| 871 | - $trans[$complexe] = []; |
|
| 872 | - load_charset($table_translit); |
|
| 873 | - foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 874 | - $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 875 | - } |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 859 | + static $trans = []; |
|
| 860 | + if ($charset == 'AUTO') { |
|
| 861 | + $charset = $GLOBALS['meta']['charset']; |
|
| 862 | + } |
|
| 863 | + if (!strlen($texte)) { |
|
| 864 | + return $texte; |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + $table_translit = 'translit' . $complexe; |
|
| 868 | + |
|
| 869 | + // 2. Translitterer grace a la table predefinie |
|
| 870 | + if (!isset($trans[$complexe])) { |
|
| 871 | + $trans[$complexe] = []; |
|
| 872 | + load_charset($table_translit); |
|
| 873 | + foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 874 | + $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 875 | + } |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 879 | 879 | } |
| 880 | 880 | |
| 881 | 881 | /** |
@@ -898,14 +898,14 @@ discard block |
||
| 898 | 898 | * @return string |
| 899 | 899 | */ |
| 900 | 900 | function translitteration($texte, $charset = 'AUTO', $complexe = '') { |
| 901 | - // 0. Supprimer les caracteres illegaux |
|
| 902 | - include_spip('inc/filtres'); |
|
| 903 | - $texte = corriger_caracteres($texte); |
|
| 901 | + // 0. Supprimer les caracteres illegaux |
|
| 902 | + include_spip('inc/filtres'); |
|
| 903 | + $texte = corriger_caracteres($texte); |
|
| 904 | 904 | |
| 905 | - // 1. Passer le charset et les é en utf-8 |
|
| 906 | - $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 905 | + // 1. Passer le charset et les é en utf-8 |
|
| 906 | + $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 907 | 907 | |
| 908 | - return translitteration_rapide($texte, $charset, $complexe); |
|
| 908 | + return translitteration_rapide($texte, $charset, $complexe); |
|
| 909 | 909 | } |
| 910 | 910 | |
| 911 | 911 | /** |
@@ -920,17 +920,17 @@ discard block |
||
| 920 | 920 | * @return string |
| 921 | 921 | */ |
| 922 | 922 | function translitteration_complexe($texte, $chiffres = false) { |
| 923 | - $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 923 | + $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 924 | 924 | |
| 925 | - if ($chiffres) { |
|
| 926 | - $texte = preg_replace_callback( |
|
| 927 | - "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 928 | - fn($m) => translitteration_chiffree($m[0]), |
|
| 929 | - $texte |
|
| 930 | - ); |
|
| 931 | - } |
|
| 925 | + if ($chiffres) { |
|
| 926 | + $texte = preg_replace_callback( |
|
| 927 | + "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 928 | + fn($m) => translitteration_chiffree($m[0]), |
|
| 929 | + $texte |
|
| 930 | + ); |
|
| 931 | + } |
|
| 932 | 932 | |
| 933 | - return $texte; |
|
| 933 | + return $texte; |
|
| 934 | 934 | } |
| 935 | 935 | |
| 936 | 936 | /** |
@@ -942,7 +942,7 @@ discard block |
||
| 942 | 942 | * @return string |
| 943 | 943 | */ |
| 944 | 944 | function translitteration_chiffree($car) { |
| 945 | - return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 945 | + return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 946 | 946 | } |
| 947 | 947 | |
| 948 | 948 | |
@@ -955,7 +955,7 @@ discard block |
||
| 955 | 955 | * true s'il a un BOM |
| 956 | 956 | **/ |
| 957 | 957 | function bom_utf8($texte) { |
| 958 | - return (substr($texte, 0, 3) == chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 958 | + return (substr($texte, 0, 3) == chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 959 | 959 | } |
| 960 | 960 | |
| 961 | 961 | /** |
@@ -972,21 +972,21 @@ discard block |
||
| 972 | 972 | * true si c'est le cas |
| 973 | 973 | **/ |
| 974 | 974 | function is_utf8($string) { |
| 975 | - return !strlen( |
|
| 976 | - preg_replace( |
|
| 977 | - ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 978 | - . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 979 | - . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 980 | - . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 981 | - . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 982 | - . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 983 | - . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 984 | - . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 985 | - . ',sS', |
|
| 986 | - '', |
|
| 987 | - $string |
|
| 988 | - ) |
|
| 989 | - ); |
|
| 975 | + return !strlen( |
|
| 976 | + preg_replace( |
|
| 977 | + ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 978 | + . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 979 | + . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 980 | + . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 981 | + . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 982 | + . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 983 | + . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 984 | + . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 985 | + . ',sS', |
|
| 986 | + '', |
|
| 987 | + $string |
|
| 988 | + ) |
|
| 989 | + ); |
|
| 990 | 990 | } |
| 991 | 991 | |
| 992 | 992 | /** |
@@ -998,13 +998,13 @@ discard block |
||
| 998 | 998 | * true si c'est le cas |
| 999 | 999 | **/ |
| 1000 | 1000 | function is_ascii($string) { |
| 1001 | - return !strlen( |
|
| 1002 | - preg_replace( |
|
| 1003 | - ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 1004 | - '', |
|
| 1005 | - $string |
|
| 1006 | - ) |
|
| 1007 | - ); |
|
| 1001 | + return !strlen( |
|
| 1002 | + preg_replace( |
|
| 1003 | + ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 1004 | + '', |
|
| 1005 | + $string |
|
| 1006 | + ) |
|
| 1007 | + ); |
|
| 1008 | 1008 | } |
| 1009 | 1009 | |
| 1010 | 1010 | /** |
@@ -1023,53 +1023,53 @@ discard block |
||
| 1023 | 1023 | **/ |
| 1024 | 1024 | function transcoder_page($texte, $headers = '') { |
| 1025 | 1025 | |
| 1026 | - // Si tout est < 128 pas la peine d'aller plus loin |
|
| 1027 | - if (is_ascii($texte)) { |
|
| 1028 | - #spip_log('charset: ascii'); |
|
| 1029 | - return $texte; |
|
| 1030 | - } |
|
| 1031 | - |
|
| 1032 | - if (bom_utf8($texte)) { |
|
| 1033 | - // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 1034 | - $charset = 'utf-8'; |
|
| 1035 | - $texte = substr($texte, 3); |
|
| 1036 | - } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 1037 | - // charset precise par le contenu (xml) |
|
| 1038 | - $charset = trim(strtolower($regs[1])); |
|
| 1039 | - } elseif ( |
|
| 1040 | - // charset precise par le contenu (html) |
|
| 1041 | - preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 1042 | - # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 1043 | - and false === strpos($regs[2], '#') |
|
| 1044 | - and $tmp = trim(strtolower($regs[2])) |
|
| 1045 | - ) { |
|
| 1046 | - $charset = $tmp; |
|
| 1047 | - } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 1048 | - // charset de la reponse http |
|
| 1049 | - $charset = trim(strtolower($regs[1])); |
|
| 1050 | - } else { |
|
| 1051 | - $charset = ''; |
|
| 1052 | - } |
|
| 1053 | - |
|
| 1054 | - |
|
| 1055 | - // normaliser les noms du shif-jis japonais |
|
| 1056 | - if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 1057 | - $charset = 'shift-jis'; |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - if ($charset) { |
|
| 1061 | - spip_log("charset: $charset"); |
|
| 1062 | - } else { |
|
| 1063 | - // valeur par defaut |
|
| 1064 | - if (is_utf8($texte)) { |
|
| 1065 | - $charset = 'utf-8'; |
|
| 1066 | - } else { |
|
| 1067 | - $charset = 'iso-8859-1'; |
|
| 1068 | - } |
|
| 1069 | - spip_log("charset probable: $charset"); |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - return importer_charset($texte, $charset); |
|
| 1026 | + // Si tout est < 128 pas la peine d'aller plus loin |
|
| 1027 | + if (is_ascii($texte)) { |
|
| 1028 | + #spip_log('charset: ascii'); |
|
| 1029 | + return $texte; |
|
| 1030 | + } |
|
| 1031 | + |
|
| 1032 | + if (bom_utf8($texte)) { |
|
| 1033 | + // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 1034 | + $charset = 'utf-8'; |
|
| 1035 | + $texte = substr($texte, 3); |
|
| 1036 | + } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 1037 | + // charset precise par le contenu (xml) |
|
| 1038 | + $charset = trim(strtolower($regs[1])); |
|
| 1039 | + } elseif ( |
|
| 1040 | + // charset precise par le contenu (html) |
|
| 1041 | + preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 1042 | + # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 1043 | + and false === strpos($regs[2], '#') |
|
| 1044 | + and $tmp = trim(strtolower($regs[2])) |
|
| 1045 | + ) { |
|
| 1046 | + $charset = $tmp; |
|
| 1047 | + } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 1048 | + // charset de la reponse http |
|
| 1049 | + $charset = trim(strtolower($regs[1])); |
|
| 1050 | + } else { |
|
| 1051 | + $charset = ''; |
|
| 1052 | + } |
|
| 1053 | + |
|
| 1054 | + |
|
| 1055 | + // normaliser les noms du shif-jis japonais |
|
| 1056 | + if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 1057 | + $charset = 'shift-jis'; |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + if ($charset) { |
|
| 1061 | + spip_log("charset: $charset"); |
|
| 1062 | + } else { |
|
| 1063 | + // valeur par defaut |
|
| 1064 | + if (is_utf8($texte)) { |
|
| 1065 | + $charset = 'utf-8'; |
|
| 1066 | + } else { |
|
| 1067 | + $charset = 'iso-8859-1'; |
|
| 1068 | + } |
|
| 1069 | + spip_log("charset probable: $charset"); |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + return importer_charset($texte, $charset); |
|
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | 1075 | |
@@ -1093,26 +1093,26 @@ discard block |
||
| 1093 | 1093 | * Le texte coupé |
| 1094 | 1094 | **/ |
| 1095 | 1095 | function spip_substr($c, $start = 0, $length = null) { |
| 1096 | - // Si ce n'est pas utf-8, utiliser substr |
|
| 1097 | - if ($GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1098 | - if ($length) { |
|
| 1099 | - return substr($c, $start, $length); |
|
| 1100 | - } else { |
|
| 1101 | - substr($c, $start); |
|
| 1102 | - } |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - // Si utf-8, voir si on dispose de mb_string |
|
| 1106 | - if (init_mb_string()) { |
|
| 1107 | - if ($length) { |
|
| 1108 | - return mb_substr($c, $start, $length); |
|
| 1109 | - } else { |
|
| 1110 | - return mb_substr($c, $start); |
|
| 1111 | - } |
|
| 1112 | - } |
|
| 1113 | - |
|
| 1114 | - // Version manuelle (cf. ci-dessous) |
|
| 1115 | - return spip_substr_manuelle($c, $start, $length); |
|
| 1096 | + // Si ce n'est pas utf-8, utiliser substr |
|
| 1097 | + if ($GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1098 | + if ($length) { |
|
| 1099 | + return substr($c, $start, $length); |
|
| 1100 | + } else { |
|
| 1101 | + substr($c, $start); |
|
| 1102 | + } |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + // Si utf-8, voir si on dispose de mb_string |
|
| 1106 | + if (init_mb_string()) { |
|
| 1107 | + if ($length) { |
|
| 1108 | + return mb_substr($c, $start, $length); |
|
| 1109 | + } else { |
|
| 1110 | + return mb_substr($c, $start); |
|
| 1111 | + } |
|
| 1112 | + } |
|
| 1113 | + |
|
| 1114 | + // Version manuelle (cf. ci-dessous) |
|
| 1115 | + return spip_substr_manuelle($c, $start, $length); |
|
| 1116 | 1116 | } |
| 1117 | 1117 | |
| 1118 | 1118 | |
@@ -1131,40 +1131,40 @@ discard block |
||
| 1131 | 1131 | **/ |
| 1132 | 1132 | function spip_substr_manuelle($c, $start, $length = null) { |
| 1133 | 1133 | |
| 1134 | - // Cas pathologique |
|
| 1135 | - if ($length === 0) { |
|
| 1136 | - return ''; |
|
| 1137 | - } |
|
| 1138 | - |
|
| 1139 | - // S'il y a un demarrage, on se positionne |
|
| 1140 | - if ($start > 0) { |
|
| 1141 | - $c = substr($c, strlen(spip_substr_manuelle($c, 0, $start))); |
|
| 1142 | - } elseif ($start < 0) { |
|
| 1143 | - return spip_substr_manuelle($c, spip_strlen($c) + $start, $length); |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - if (!$length) { |
|
| 1147 | - return $c; |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - if ($length > 0) { |
|
| 1151 | - // on prend n fois la longueur desiree, pour etre surs d'avoir tout |
|
| 1152 | - // (un caractere utf-8 prenant au maximum n bytes) |
|
| 1153 | - $n = 0; |
|
| 1154 | - while (preg_match(',[\x80-\xBF]{' . (++$n) . '},', $c)) { |
|
| 1155 | - ; |
|
| 1156 | - } |
|
| 1157 | - $c = substr($c, 0, $n * $length); |
|
| 1158 | - // puis, tant qu'on est trop long, on coupe... |
|
| 1159 | - while (($l = spip_strlen($c)) > $length) { |
|
| 1160 | - $c = substr($c, 0, $length - $l); |
|
| 1161 | - } |
|
| 1162 | - |
|
| 1163 | - return $c; |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - // $length < 0 |
|
| 1167 | - return spip_substr_manuelle($c, 0, spip_strlen($c) + $length); |
|
| 1134 | + // Cas pathologique |
|
| 1135 | + if ($length === 0) { |
|
| 1136 | + return ''; |
|
| 1137 | + } |
|
| 1138 | + |
|
| 1139 | + // S'il y a un demarrage, on se positionne |
|
| 1140 | + if ($start > 0) { |
|
| 1141 | + $c = substr($c, strlen(spip_substr_manuelle($c, 0, $start))); |
|
| 1142 | + } elseif ($start < 0) { |
|
| 1143 | + return spip_substr_manuelle($c, spip_strlen($c) + $start, $length); |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + if (!$length) { |
|
| 1147 | + return $c; |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + if ($length > 0) { |
|
| 1151 | + // on prend n fois la longueur desiree, pour etre surs d'avoir tout |
|
| 1152 | + // (un caractere utf-8 prenant au maximum n bytes) |
|
| 1153 | + $n = 0; |
|
| 1154 | + while (preg_match(',[\x80-\xBF]{' . (++$n) . '},', $c)) { |
|
| 1155 | + ; |
|
| 1156 | + } |
|
| 1157 | + $c = substr($c, 0, $n * $length); |
|
| 1158 | + // puis, tant qu'on est trop long, on coupe... |
|
| 1159 | + while (($l = spip_strlen($c)) > $length) { |
|
| 1160 | + $c = substr($c, 0, $length - $l); |
|
| 1161 | + } |
|
| 1162 | + |
|
| 1163 | + return $c; |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + // $length < 0 |
|
| 1167 | + return spip_substr_manuelle($c, 0, spip_strlen($c) + $length); |
|
| 1168 | 1168 | } |
| 1169 | 1169 | |
| 1170 | 1170 | /** |
@@ -1178,14 +1178,14 @@ discard block |
||
| 1178 | 1178 | * La chaîne avec une majuscule sur le premier mot |
| 1179 | 1179 | */ |
| 1180 | 1180 | function spip_ucfirst($c) { |
| 1181 | - // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser ucfirst |
|
| 1182 | - if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1183 | - return ucfirst($c); |
|
| 1184 | - } |
|
| 1181 | + // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser ucfirst |
|
| 1182 | + if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1183 | + return ucfirst($c); |
|
| 1184 | + } |
|
| 1185 | 1185 | |
| 1186 | - $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1186 | + $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1187 | 1187 | |
| 1188 | - return $lettre1 . spip_substr($c, 1); |
|
| 1188 | + return $lettre1 . spip_substr($c, 1); |
|
| 1189 | 1189 | } |
| 1190 | 1190 | |
| 1191 | 1191 | /** |
@@ -1199,12 +1199,12 @@ discard block |
||
| 1199 | 1199 | * La chaîne en minuscules |
| 1200 | 1200 | */ |
| 1201 | 1201 | function spip_strtolower($c) { |
| 1202 | - // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser strtolower |
|
| 1203 | - if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1204 | - return strtolower($c); |
|
| 1205 | - } |
|
| 1202 | + // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser strtolower |
|
| 1203 | + if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1204 | + return strtolower($c); |
|
| 1205 | + } |
|
| 1206 | 1206 | |
| 1207 | - return mb_strtolower($c); |
|
| 1207 | + return mb_strtolower($c); |
|
| 1208 | 1208 | } |
| 1209 | 1209 | |
| 1210 | 1210 | /** |
@@ -1218,23 +1218,23 @@ discard block |
||
| 1218 | 1218 | * Longueur de la chaîne |
| 1219 | 1219 | */ |
| 1220 | 1220 | function spip_strlen($c) { |
| 1221 | - // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1222 | - $c = str_replace("\r\n", "\n", $c); |
|
| 1223 | - |
|
| 1224 | - // Si ce n'est pas utf-8, utiliser strlen |
|
| 1225 | - if ($GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1226 | - return strlen($c); |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - // Sinon, utiliser mb_strlen() si disponible |
|
| 1230 | - if (init_mb_string()) { |
|
| 1231 | - return mb_strlen($c); |
|
| 1232 | - } |
|
| 1233 | - |
|
| 1234 | - // Methode manuelle : on supprime les bytes 10......, |
|
| 1235 | - // on compte donc les ascii (0.......) et les demarrages |
|
| 1236 | - // de caracteres utf-8 (11......) |
|
| 1237 | - return strlen(preg_replace(',[\x80-\xBF],S', '', $c)); |
|
| 1221 | + // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1222 | + $c = str_replace("\r\n", "\n", $c); |
|
| 1223 | + |
|
| 1224 | + // Si ce n'est pas utf-8, utiliser strlen |
|
| 1225 | + if ($GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1226 | + return strlen($c); |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + // Sinon, utiliser mb_strlen() si disponible |
|
| 1230 | + if (init_mb_string()) { |
|
| 1231 | + return mb_strlen($c); |
|
| 1232 | + } |
|
| 1233 | + |
|
| 1234 | + // Methode manuelle : on supprime les bytes 10......, |
|
| 1235 | + // on compte donc les ascii (0.......) et les demarrages |
|
| 1236 | + // de caracteres utf-8 (11......) |
|
| 1237 | + return strlen(preg_replace(',[\x80-\xBF],S', '', $c)); |
|
| 1238 | 1238 | } |
| 1239 | 1239 | |
| 1240 | 1240 | // Initialisation |
@@ -1244,16 +1244,16 @@ discard block |
||
| 1244 | 1244 | // dans les preg_replace pour ne pas casser certaines lettres accentuees : |
| 1245 | 1245 | // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp |
| 1246 | 1246 | if ( |
| 1247 | - !isset($GLOBALS['meta']['pcre_u']) |
|
| 1248 | - or (isset($_GET['var_mode']) and !isset($_GET['var_profile'])) |
|
| 1247 | + !isset($GLOBALS['meta']['pcre_u']) |
|
| 1248 | + or (isset($_GET['var_mode']) and !isset($_GET['var_profile'])) |
|
| 1249 | 1249 | ) { |
| 1250 | - include_spip('inc/meta'); |
|
| 1251 | - ecrire_meta( |
|
| 1252 | - 'pcre_u', |
|
| 1253 | - $u = (lire_config('charset', _DEFAULT_CHARSET) == 'utf-8' |
|
| 1254 | - and test_pcre_unicode()) |
|
| 1255 | - ? 'u' : '' |
|
| 1256 | - ); |
|
| 1250 | + include_spip('inc/meta'); |
|
| 1251 | + ecrire_meta( |
|
| 1252 | + 'pcre_u', |
|
| 1253 | + $u = (lire_config('charset', _DEFAULT_CHARSET) == 'utf-8' |
|
| 1254 | + and test_pcre_unicode()) |
|
| 1255 | + ? 'u' : '' |
|
| 1256 | + ); |
|
| 1257 | 1257 | } |
| 1258 | 1258 | |
| 1259 | 1259 | |
@@ -1269,17 +1269,17 @@ discard block |
||
| 1269 | 1269 | * en unicode : 💩 |
| 1270 | 1270 | */ |
| 1271 | 1271 | function utf8_noplanes($x) { |
| 1272 | - $regexp_utf8_4bytes = '/( |
|
| 1272 | + $regexp_utf8_4bytes = '/( |
|
| 1273 | 1273 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 1274 | 1274 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 1275 | 1275 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 1276 | 1276 | )/xS'; |
| 1277 | - if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1278 | - foreach ($z[0] as $k) { |
|
| 1279 | - $ku = utf_8_to_unicode($k); |
|
| 1280 | - $x = str_replace($k, $ku, $x); |
|
| 1281 | - } |
|
| 1282 | - } |
|
| 1283 | - |
|
| 1284 | - return $x; |
|
| 1277 | + if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1278 | + foreach ($z[0] as $k) { |
|
| 1279 | + $ku = utf_8_to_unicode($k); |
|
| 1280 | + $x = str_replace($k, $ku, $x); |
|
| 1281 | + } |
|
| 1282 | + } |
|
| 1283 | + |
|
| 1284 | + return $x; |
|
| 1285 | 1285 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | |
| 30 | 30 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 31 | - return; |
|
| 31 | + return; |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | |
@@ -48,55 +48,55 @@ discard block |
||
| 48 | 48 | * array : description des roles applicables dans 3 index : colonne, titres, roles |
| 49 | 49 | **/ |
| 50 | 50 | function roles_presents($objet, $objet_destination = '') { |
| 51 | - $desc = lister_tables_objets_sql(table_objet_sql($objet)); |
|
| 52 | - |
|
| 53 | - // pas de liste de roles, on sort |
|
| 54 | - if (!isset($desc['roles_titres']) or !($titres = $desc['roles_titres'])) { |
|
| 55 | - return false; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - // on vérifie que la table de liaison existe |
|
| 59 | - include_spip('action/editer_liens'); |
|
| 60 | - if (!$lien = objet_associable($objet)) { |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // on cherche ensuite si la colonne existe bien dans la table de liaison (par défaut 'role') |
|
| 65 | - $colonne = $desc['roles_colonne'] ?? 'role'; |
|
| 66 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 67 | - [, $table_lien] = $lien; |
|
| 68 | - $desc_lien = $trouver_table($table_lien); |
|
| 69 | - if (!isset($desc_lien['field'][$colonne])) { |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - // sur quoi peuvent s'appliquer nos rôles |
|
| 74 | - if (!$application = $desc['roles_objets']) { |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // destination presente, on restreint si possible |
|
| 79 | - if ($objet_destination) { |
|
| 80 | - $objet_destination = table_objet($objet_destination); |
|
| 81 | - |
|
| 82 | - // pour l'objet |
|
| 83 | - if (isset($application[$objet_destination])) { |
|
| 84 | - $application = $application[$objet_destination]; |
|
| 85 | - // sinon pour tous les objets |
|
| 86 | - } elseif (isset($application['*'])) { |
|
| 87 | - $application = $application['*']; |
|
| 88 | - } // sinon tant pis |
|
| 89 | - else { |
|
| 90 | - return false; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - // tout est ok |
|
| 95 | - return [ |
|
| 96 | - 'titres' => $titres, |
|
| 97 | - 'roles' => $application, |
|
| 98 | - 'colonne' => $colonne |
|
| 99 | - ]; |
|
| 51 | + $desc = lister_tables_objets_sql(table_objet_sql($objet)); |
|
| 52 | + |
|
| 53 | + // pas de liste de roles, on sort |
|
| 54 | + if (!isset($desc['roles_titres']) or !($titres = $desc['roles_titres'])) { |
|
| 55 | + return false; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + // on vérifie que la table de liaison existe |
|
| 59 | + include_spip('action/editer_liens'); |
|
| 60 | + if (!$lien = objet_associable($objet)) { |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // on cherche ensuite si la colonne existe bien dans la table de liaison (par défaut 'role') |
|
| 65 | + $colonne = $desc['roles_colonne'] ?? 'role'; |
|
| 66 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 67 | + [, $table_lien] = $lien; |
|
| 68 | + $desc_lien = $trouver_table($table_lien); |
|
| 69 | + if (!isset($desc_lien['field'][$colonne])) { |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + // sur quoi peuvent s'appliquer nos rôles |
|
| 74 | + if (!$application = $desc['roles_objets']) { |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // destination presente, on restreint si possible |
|
| 79 | + if ($objet_destination) { |
|
| 80 | + $objet_destination = table_objet($objet_destination); |
|
| 81 | + |
|
| 82 | + // pour l'objet |
|
| 83 | + if (isset($application[$objet_destination])) { |
|
| 84 | + $application = $application[$objet_destination]; |
|
| 85 | + // sinon pour tous les objets |
|
| 86 | + } elseif (isset($application['*'])) { |
|
| 87 | + $application = $application['*']; |
|
| 88 | + } // sinon tant pis |
|
| 89 | + else { |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + // tout est ok |
|
| 95 | + return [ |
|
| 96 | + 'titres' => $titres, |
|
| 97 | + 'roles' => $application, |
|
| 98 | + 'colonne' => $colonne |
|
| 99 | + ]; |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
@@ -110,11 +110,11 @@ discard block |
||
| 110 | 110 | * Nom de la colonne, sinon vide |
| 111 | 111 | **/ |
| 112 | 112 | function roles_colonne($objet, $objet_destination) { |
| 113 | - if ($roles = roles_presents($objet, $objet_destination)) { |
|
| 114 | - return $roles['colonne']; |
|
| 115 | - } |
|
| 113 | + if ($roles = roles_presents($objet, $objet_destination)) { |
|
| 114 | + return $roles['colonne']; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - return ''; |
|
| 117 | + return ''; |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | |
@@ -136,24 +136,24 @@ discard block |
||
| 136 | 136 | * Liste ('', '', array()) sinon. |
| 137 | 137 | **/ |
| 138 | 138 | function roles_trouver_dans_qualif($objet, $objet_destination, $qualif = []) { |
| 139 | - // si des rôles sont possibles, on les utilise |
|
| 140 | - $role = $colonne_role = ''; # role défini |
|
| 141 | - // condition du where par defaut |
|
| 142 | - $cond = []; |
|
| 143 | - if ($roles = roles_presents($objet, $objet_destination)) { |
|
| 144 | - $colonne_role = $roles['colonne']; |
|
| 145 | - // qu'il n'est pas défini |
|
| 146 | - if ( |
|
| 147 | - !isset($qualif[$colonne_role]) |
|
| 148 | - or !($role = $qualif[$colonne_role]) |
|
| 149 | - ) { |
|
| 150 | - $role = $roles['roles']['defaut']; |
|
| 151 | - } |
|
| 152 | - // where |
|
| 153 | - $cond = ["$colonne_role=" . sql_quote($role)]; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return [$role, $colonne_role, $cond]; |
|
| 139 | + // si des rôles sont possibles, on les utilise |
|
| 140 | + $role = $colonne_role = ''; # role défini |
|
| 141 | + // condition du where par defaut |
|
| 142 | + $cond = []; |
|
| 143 | + if ($roles = roles_presents($objet, $objet_destination)) { |
|
| 144 | + $colonne_role = $roles['colonne']; |
|
| 145 | + // qu'il n'est pas défini |
|
| 146 | + if ( |
|
| 147 | + !isset($qualif[$colonne_role]) |
|
| 148 | + or !($role = $qualif[$colonne_role]) |
|
| 149 | + ) { |
|
| 150 | + $role = $roles['roles']['defaut']; |
|
| 151 | + } |
|
| 152 | + // where |
|
| 153 | + $cond = ["$colonne_role=" . sql_quote($role)]; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return [$role, $colonne_role, $cond]; |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
@@ -175,21 +175,21 @@ discard block |
||
| 175 | 175 | * Liste (Tableau de conditions where complété du role, Colonne du role, role utilisé) |
| 176 | 176 | **/ |
| 177 | 177 | function roles_creer_condition_role($objet_source, $objet, $cond, $tous_si_absent = false) { |
| 178 | - // role par défaut, colonne |
|
| 179 | - [$role_defaut, $colonne_role] = roles_trouver_dans_qualif($objet_source, $objet); |
|
| 178 | + // role par défaut, colonne |
|
| 179 | + [$role_defaut, $colonne_role] = roles_trouver_dans_qualif($objet_source, $objet); |
|
| 180 | 180 | |
| 181 | - // chercher d'eventuels rôles transmis |
|
| 182 | - $role = ($cond['role'] ?? ($tous_si_absent ? '*' : $role_defaut)); |
|
| 183 | - unset($cond['role']); // cette condition est particuliere... |
|
| 181 | + // chercher d'eventuels rôles transmis |
|
| 182 | + $role = ($cond['role'] ?? ($tous_si_absent ? '*' : $role_defaut)); |
|
| 183 | + unset($cond['role']); // cette condition est particuliere... |
|
| 184 | 184 | |
| 185 | - if ($colonne_role) { |
|
| 186 | - // on ajoute la condition du role aux autres conditions. |
|
| 187 | - if ($role != '*') { |
|
| 188 | - $cond[] = "$colonne_role=" . sql_quote($role); |
|
| 189 | - } |
|
| 190 | - } |
|
| 185 | + if ($colonne_role) { |
|
| 186 | + // on ajoute la condition du role aux autres conditions. |
|
| 187 | + if ($role != '*') { |
|
| 188 | + $cond[] = "$colonne_role=" . sql_quote($role); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - return [$cond, $colonne_role, $role]; |
|
| 192 | + return [$cond, $colonne_role, $role]; |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | /** |
@@ -211,28 +211,28 @@ discard block |
||
| 211 | 211 | */ |
| 212 | 212 | function roles_complets($objet_source, $objet, $id_objet, $objet_lien) { |
| 213 | 213 | |
| 214 | - $presents = roles_presents_liaisons($objet_source, $objet, $id_objet, $objet_lien); |
|
| 215 | - // pas de roles sur ces objets => la liste par defaut, comme sans role |
|
| 216 | - if ($presents === false) { |
|
| 217 | - return lister_objets_lies($objet_source, $objet, $id_objet, $objet_lien); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - // types de roles possibles |
|
| 221 | - $roles_possibles = $presents['roles']['roles']['choix']; |
|
| 222 | - // couples id / roles |
|
| 223 | - $ids = $presents['ids']; |
|
| 224 | - |
|
| 225 | - // pour chaque groupe, on fait le diff entre tous les roles possibles |
|
| 226 | - // et les roles attribués à l'élément : s'il en reste, c'est que l'élément |
|
| 227 | - // n'est pas complet |
|
| 228 | - $complets = []; |
|
| 229 | - foreach ($ids as $id => $roles_presents) { |
|
| 230 | - if (!array_diff($roles_possibles, $roles_presents)) { |
|
| 231 | - $complets[] = $id; |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - return $complets; |
|
| 214 | + $presents = roles_presents_liaisons($objet_source, $objet, $id_objet, $objet_lien); |
|
| 215 | + // pas de roles sur ces objets => la liste par defaut, comme sans role |
|
| 216 | + if ($presents === false) { |
|
| 217 | + return lister_objets_lies($objet_source, $objet, $id_objet, $objet_lien); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + // types de roles possibles |
|
| 221 | + $roles_possibles = $presents['roles']['roles']['choix']; |
|
| 222 | + // couples id / roles |
|
| 223 | + $ids = $presents['ids']; |
|
| 224 | + |
|
| 225 | + // pour chaque groupe, on fait le diff entre tous les roles possibles |
|
| 226 | + // et les roles attribués à l'élément : s'il en reste, c'est que l'élément |
|
| 227 | + // n'est pas complet |
|
| 228 | + $complets = []; |
|
| 229 | + foreach ($ids as $id => $roles_presents) { |
|
| 230 | + if (!array_diff($roles_possibles, $roles_presents)) { |
|
| 231 | + $complets[] = $id; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + return $complets; |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | |
@@ -249,17 +249,17 @@ discard block |
||
| 249 | 249 | */ |
| 250 | 250 | function roles_presents_sur_id($id_objet_source, $objet_source, $objet, $id_objet, $objet_lien) { |
| 251 | 251 | |
| 252 | - $presents = roles_presents_liaisons($objet_source, $objet, $id_objet, $objet_lien); |
|
| 253 | - // pas de roles sur ces objets => la liste par defaut, comme sans role |
|
| 254 | - if ($presents === false) { |
|
| 255 | - return []; |
|
| 256 | - } |
|
| 252 | + $presents = roles_presents_liaisons($objet_source, $objet, $id_objet, $objet_lien); |
|
| 253 | + // pas de roles sur ces objets => la liste par defaut, comme sans role |
|
| 254 | + if ($presents === false) { |
|
| 255 | + return []; |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - if (!isset($presents['ids'][$id_objet_source])) { |
|
| 259 | - return []; |
|
| 260 | - } |
|
| 258 | + if (!isset($presents['ids'][$id_objet_source])) { |
|
| 259 | + return []; |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | - return $presents['ids'][$id_objet_source]; |
|
| 262 | + return $presents['ids'][$id_objet_source]; |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | |
@@ -287,47 +287,47 @@ discard block |
||
| 287 | 287 | * - False si pas de role déclarés |
| 288 | 288 | */ |
| 289 | 289 | function roles_presents_liaisons($objet_source, $objet, $id_objet, $objet_lien) { |
| 290 | - static $done = []; |
|
| 291 | - |
|
| 292 | - // stocker le résultat |
|
| 293 | - $hash = "$objet_source-$objet-$id_objet-$objet_lien"; |
|
| 294 | - if (isset($done[$hash])) { |
|
| 295 | - return $done[$hash]; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // pas de roles sur ces objets, on sort |
|
| 299 | - $roles = roles_presents($objet_lien, ($objet_lien == $objet) ? $objet_source : $objet); |
|
| 300 | - if (!$roles) { |
|
| 301 | - return $done[$hash] = false; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // inspiré de lister_objets_lies() |
|
| 305 | - if ($objet_lien == $objet) { |
|
| 306 | - $res = objet_trouver_liens([$objet => $id_objet], [$objet_source => '*']); |
|
| 307 | - } else { |
|
| 308 | - $res = objet_trouver_liens([$objet_source => '*'], [$objet => $id_objet]); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - // types de roles possibles |
|
| 312 | - $roles_possibles = $roles['roles']['choix']; |
|
| 313 | - // colonne du role |
|
| 314 | - $colonne = $roles['colonne']; |
|
| 315 | - |
|
| 316 | - // on recupere par id, et role existant |
|
| 317 | - $ids = []; |
|
| 318 | - while ($row = array_shift($res)) { |
|
| 319 | - $id = $row[$objet_source]; |
|
| 320 | - if (!isset($ids[$id])) { |
|
| 321 | - $ids[$id] = []; |
|
| 322 | - } |
|
| 323 | - // tableau des roles présents |
|
| 324 | - $ids[$id][] = $row[$colonne]; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return $done[$hash] = [ |
|
| 328 | - 'roles' => $roles, |
|
| 329 | - 'ids' => $ids |
|
| 330 | - ]; |
|
| 290 | + static $done = []; |
|
| 291 | + |
|
| 292 | + // stocker le résultat |
|
| 293 | + $hash = "$objet_source-$objet-$id_objet-$objet_lien"; |
|
| 294 | + if (isset($done[$hash])) { |
|
| 295 | + return $done[$hash]; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // pas de roles sur ces objets, on sort |
|
| 299 | + $roles = roles_presents($objet_lien, ($objet_lien == $objet) ? $objet_source : $objet); |
|
| 300 | + if (!$roles) { |
|
| 301 | + return $done[$hash] = false; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // inspiré de lister_objets_lies() |
|
| 305 | + if ($objet_lien == $objet) { |
|
| 306 | + $res = objet_trouver_liens([$objet => $id_objet], [$objet_source => '*']); |
|
| 307 | + } else { |
|
| 308 | + $res = objet_trouver_liens([$objet_source => '*'], [$objet => $id_objet]); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + // types de roles possibles |
|
| 312 | + $roles_possibles = $roles['roles']['choix']; |
|
| 313 | + // colonne du role |
|
| 314 | + $colonne = $roles['colonne']; |
|
| 315 | + |
|
| 316 | + // on recupere par id, et role existant |
|
| 317 | + $ids = []; |
|
| 318 | + while ($row = array_shift($res)) { |
|
| 319 | + $id = $row[$objet_source]; |
|
| 320 | + if (!isset($ids[$id])) { |
|
| 321 | + $ids[$id] = []; |
|
| 322 | + } |
|
| 323 | + // tableau des roles présents |
|
| 324 | + $ids[$id][] = $row[$colonne]; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return $done[$hash] = [ |
|
| 328 | + 'roles' => $roles, |
|
| 329 | + 'ids' => $ids |
|
| 330 | + ]; |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | |
@@ -345,33 +345,33 @@ discard block |
||
| 345 | 345 | * - false si pas de role déclarés |
| 346 | 346 | */ |
| 347 | 347 | function roles_connus_en_base($objet_source, $objet, $objet_lien) { |
| 348 | - static $done = []; |
|
| 349 | - |
|
| 350 | - // stocker le résultat |
|
| 351 | - $hash = "$objet_source-$objet-$objet_lien"; |
|
| 352 | - if (isset($done[$hash])) { |
|
| 353 | - return $done[$hash]; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - if (!$lien = objet_associable($objet_lien)) { |
|
| 357 | - return $done[$hash] = false; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - // pas de roles sur ces objets, on sort |
|
| 361 | - $roles = roles_presents($objet_lien, ($objet_lien == $objet) ? $objet_source : $objet); |
|
| 362 | - if (!$roles) { |
|
| 363 | - return $done[$hash] = false; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - [$primary, $l] = $lien; |
|
| 367 | - $colone_role = $roles['colonne']; |
|
| 368 | - |
|
| 369 | - $all = sql_allfetsel( |
|
| 370 | - "DISTINCT $colone_role", |
|
| 371 | - $l, |
|
| 372 | - 'objet=' . sql_quote(($objet_source == $objet_lien) ? $objet : $objet_source) |
|
| 373 | - ); |
|
| 374 | - $done[$hash] = array_map('reset', $all); |
|
| 375 | - |
|
| 376 | - return $done[$hash]; |
|
| 348 | + static $done = []; |
|
| 349 | + |
|
| 350 | + // stocker le résultat |
|
| 351 | + $hash = "$objet_source-$objet-$objet_lien"; |
|
| 352 | + if (isset($done[$hash])) { |
|
| 353 | + return $done[$hash]; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + if (!$lien = objet_associable($objet_lien)) { |
|
| 357 | + return $done[$hash] = false; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + // pas de roles sur ces objets, on sort |
|
| 361 | + $roles = roles_presents($objet_lien, ($objet_lien == $objet) ? $objet_source : $objet); |
|
| 362 | + if (!$roles) { |
|
| 363 | + return $done[$hash] = false; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + [$primary, $l] = $lien; |
|
| 367 | + $colone_role = $roles['colonne']; |
|
| 368 | + |
|
| 369 | + $all = sql_allfetsel( |
|
| 370 | + "DISTINCT $colone_role", |
|
| 371 | + $l, |
|
| 372 | + 'objet=' . sql_quote(($objet_source == $objet_lien) ? $objet : $objet_source) |
|
| 373 | + ); |
|
| 374 | + $done[$hash] = array_map('reset', $all); |
|
| 375 | + |
|
| 376 | + return $done[$hash]; |
|
| 377 | 377 | } |
@@ -11,298 +11,298 @@ |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | |
| 18 | 18 | // methodes sql |
| 19 | 19 | function inc_recherche_to_array_dist($recherche, $options = []) { |
| 20 | 20 | |
| 21 | - // options par defaut |
|
| 22 | - $options = array_merge( |
|
| 23 | - [ |
|
| 24 | - 'score' => true, |
|
| 25 | - 'champs' => false, |
|
| 26 | - 'toutvoir' => false, |
|
| 27 | - 'matches' => false, |
|
| 28 | - 'jointures' => false |
|
| 29 | - ], |
|
| 30 | - $options |
|
| 31 | - ); |
|
| 21 | + // options par defaut |
|
| 22 | + $options = array_merge( |
|
| 23 | + [ |
|
| 24 | + 'score' => true, |
|
| 25 | + 'champs' => false, |
|
| 26 | + 'toutvoir' => false, |
|
| 27 | + 'matches' => false, |
|
| 28 | + 'jointures' => false |
|
| 29 | + ], |
|
| 30 | + $options |
|
| 31 | + ); |
|
| 32 | 32 | |
| 33 | - include_spip('inc/rechercher'); |
|
| 34 | - include_spip('inc/autoriser'); |
|
| 33 | + include_spip('inc/rechercher'); |
|
| 34 | + include_spip('inc/autoriser'); |
|
| 35 | 35 | |
| 36 | - $requete = [ |
|
| 37 | - 'SELECT' => [], |
|
| 38 | - 'FROM' => [], |
|
| 39 | - 'WHERE' => [], |
|
| 40 | - 'GROUPBY' => [], |
|
| 41 | - 'ORDERBY' => [], |
|
| 42 | - 'LIMIT' => '', |
|
| 43 | - 'HAVING' => [] |
|
| 44 | - ]; |
|
| 36 | + $requete = [ |
|
| 37 | + 'SELECT' => [], |
|
| 38 | + 'FROM' => [], |
|
| 39 | + 'WHERE' => [], |
|
| 40 | + 'GROUPBY' => [], |
|
| 41 | + 'ORDERBY' => [], |
|
| 42 | + 'LIMIT' => '', |
|
| 43 | + 'HAVING' => [] |
|
| 44 | + ]; |
|
| 45 | 45 | |
| 46 | - $table = sinon($options['table'], 'article'); |
|
| 47 | - if ($options['champs']) { |
|
| 48 | - $champs = $options['champs']; |
|
| 49 | - } else { |
|
| 50 | - $l = liste_des_champs(); |
|
| 51 | - $champs = $l['article']; |
|
| 52 | - } |
|
| 53 | - $serveur = $options['serveur']; |
|
| 46 | + $table = sinon($options['table'], 'article'); |
|
| 47 | + if ($options['champs']) { |
|
| 48 | + $champs = $options['champs']; |
|
| 49 | + } else { |
|
| 50 | + $l = liste_des_champs(); |
|
| 51 | + $champs = $l['article']; |
|
| 52 | + } |
|
| 53 | + $serveur = $options['serveur']; |
|
| 54 | 54 | |
| 55 | - [$methode, $q, $preg] = expression_recherche($recherche, $options); |
|
| 55 | + [$methode, $q, $preg] = expression_recherche($recherche, $options); |
|
| 56 | 56 | |
| 57 | - $jointures = $options['jointures'] |
|
| 58 | - ? liste_des_jointures() |
|
| 59 | - : []; |
|
| 57 | + $jointures = $options['jointures'] |
|
| 58 | + ? liste_des_jointures() |
|
| 59 | + : []; |
|
| 60 | 60 | |
| 61 | - $_id_table = id_table_objet($table); |
|
| 61 | + $_id_table = id_table_objet($table); |
|
| 62 | 62 | |
| 63 | - // c'est un pis-aller : ca a peu de chance de marcher, mais mieux quand meme que en conservant la ',' |
|
| 64 | - // (aka ca marche au moins dans certains cas comme avec spip_formulaires_reponses_champs) |
|
| 65 | - if (strpos($_id_table, ',') !== false) { |
|
| 66 | - $_id_table = explode(',', $_id_table); |
|
| 67 | - $_id_table = reset($_id_table); |
|
| 68 | - } |
|
| 63 | + // c'est un pis-aller : ca a peu de chance de marcher, mais mieux quand meme que en conservant la ',' |
|
| 64 | + // (aka ca marche au moins dans certains cas comme avec spip_formulaires_reponses_champs) |
|
| 65 | + if (strpos($_id_table, ',') !== false) { |
|
| 66 | + $_id_table = explode(',', $_id_table); |
|
| 67 | + $_id_table = reset($_id_table); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - $requete['SELECT'][] = 't.' . $_id_table; |
|
| 71 | - $a = []; |
|
| 72 | - // Recherche fulltext |
|
| 73 | - foreach ($champs as $champ => $poids) { |
|
| 74 | - if (is_array($champ)) { |
|
| 75 | - spip_log('requetes imbriquees interdites'); |
|
| 76 | - } else { |
|
| 77 | - if (strpos($champ, '.') === false) { |
|
| 78 | - $champ = "t.$champ"; |
|
| 79 | - } |
|
| 80 | - $requete['SELECT'][] = $champ; |
|
| 81 | - $a[] = $champ . ' ' . $methode . ' ' . $q; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - if ($a) { |
|
| 85 | - $requete['WHERE'][] = join(' OR ', $a); |
|
| 86 | - } |
|
| 87 | - $requete['FROM'][] = table_objet_sql($table) . ' AS t'; |
|
| 70 | + $requete['SELECT'][] = 't.' . $_id_table; |
|
| 71 | + $a = []; |
|
| 72 | + // Recherche fulltext |
|
| 73 | + foreach ($champs as $champ => $poids) { |
|
| 74 | + if (is_array($champ)) { |
|
| 75 | + spip_log('requetes imbriquees interdites'); |
|
| 76 | + } else { |
|
| 77 | + if (strpos($champ, '.') === false) { |
|
| 78 | + $champ = "t.$champ"; |
|
| 79 | + } |
|
| 80 | + $requete['SELECT'][] = $champ; |
|
| 81 | + $a[] = $champ . ' ' . $methode . ' ' . $q; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + if ($a) { |
|
| 85 | + $requete['WHERE'][] = join(' OR ', $a); |
|
| 86 | + } |
|
| 87 | + $requete['FROM'][] = table_objet_sql($table) . ' AS t'; |
|
| 88 | 88 | |
| 89 | - $results = []; |
|
| 89 | + $results = []; |
|
| 90 | 90 | |
| 91 | - $s = sql_select( |
|
| 92 | - $requete['SELECT'], |
|
| 93 | - $requete['FROM'], |
|
| 94 | - $requete['WHERE'], |
|
| 95 | - implode(' ', $requete['GROUPBY']), |
|
| 96 | - $requete['ORDERBY'], |
|
| 97 | - $requete['LIMIT'], |
|
| 98 | - $requete['HAVING'], |
|
| 99 | - $serveur |
|
| 100 | - ); |
|
| 91 | + $s = sql_select( |
|
| 92 | + $requete['SELECT'], |
|
| 93 | + $requete['FROM'], |
|
| 94 | + $requete['WHERE'], |
|
| 95 | + implode(' ', $requete['GROUPBY']), |
|
| 96 | + $requete['ORDERBY'], |
|
| 97 | + $requete['LIMIT'], |
|
| 98 | + $requete['HAVING'], |
|
| 99 | + $serveur |
|
| 100 | + ); |
|
| 101 | 101 | |
| 102 | - while ( |
|
| 103 | - $t = sql_fetch($s, $serveur) |
|
| 104 | - and (!isset($t['score']) or $t['score'] > 0) |
|
| 105 | - ) { |
|
| 106 | - $id = intval($t[$_id_table]); |
|
| 102 | + while ( |
|
| 103 | + $t = sql_fetch($s, $serveur) |
|
| 104 | + and (!isset($t['score']) or $t['score'] > 0) |
|
| 105 | + ) { |
|
| 106 | + $id = intval($t[$_id_table]); |
|
| 107 | 107 | |
| 108 | - if ( |
|
| 109 | - $options['toutvoir'] |
|
| 110 | - or autoriser('voir', $table, $id) |
|
| 111 | - ) { |
|
| 112 | - // indiquer les champs concernes |
|
| 113 | - $champs_vus = []; |
|
| 114 | - $score = 0; |
|
| 115 | - $matches = []; |
|
| 108 | + if ( |
|
| 109 | + $options['toutvoir'] |
|
| 110 | + or autoriser('voir', $table, $id) |
|
| 111 | + ) { |
|
| 112 | + // indiquer les champs concernes |
|
| 113 | + $champs_vus = []; |
|
| 114 | + $score = 0; |
|
| 115 | + $matches = []; |
|
| 116 | 116 | |
| 117 | - $vu = false; |
|
| 118 | - foreach ($champs as $champ => $poids) { |
|
| 119 | - $champ = explode('.', $champ); |
|
| 120 | - $champ = end($champ); |
|
| 121 | - // translitteration_rapide uniquement si on est deja en utf-8 |
|
| 122 | - $value = ($GLOBALS['meta']['charset'] == 'utf-8' ? translitteration_rapide($t[$champ]) : translitteration($t[$champ])); |
|
| 123 | - if ( |
|
| 124 | - $n = |
|
| 125 | - ($options['score'] || $options['matches']) |
|
| 126 | - ? preg_match_all($preg, $value, $regs, PREG_SET_ORDER) |
|
| 127 | - : preg_match($preg, $value) |
|
| 128 | - ) { |
|
| 129 | - $vu = true; |
|
| 117 | + $vu = false; |
|
| 118 | + foreach ($champs as $champ => $poids) { |
|
| 119 | + $champ = explode('.', $champ); |
|
| 120 | + $champ = end($champ); |
|
| 121 | + // translitteration_rapide uniquement si on est deja en utf-8 |
|
| 122 | + $value = ($GLOBALS['meta']['charset'] == 'utf-8' ? translitteration_rapide($t[$champ]) : translitteration($t[$champ])); |
|
| 123 | + if ( |
|
| 124 | + $n = |
|
| 125 | + ($options['score'] || $options['matches']) |
|
| 126 | + ? preg_match_all($preg, $value, $regs, PREG_SET_ORDER) |
|
| 127 | + : preg_match($preg, $value) |
|
| 128 | + ) { |
|
| 129 | + $vu = true; |
|
| 130 | 130 | |
| 131 | - if ($options['champs']) { |
|
| 132 | - $champs_vus[$champ] = $t[$champ]; |
|
| 133 | - } |
|
| 134 | - if ($options['score']) { |
|
| 135 | - // compter les points avec un peu de discernement : on pondere par la longueur du match compte en chars |
|
| 136 | - $score += $poids * strlen(implode('', array_column($regs, 0))); |
|
| 137 | - } |
|
| 131 | + if ($options['champs']) { |
|
| 132 | + $champs_vus[$champ] = $t[$champ]; |
|
| 133 | + } |
|
| 134 | + if ($options['score']) { |
|
| 135 | + // compter les points avec un peu de discernement : on pondere par la longueur du match compte en chars |
|
| 136 | + $score += $poids * strlen(implode('', array_column($regs, 0))); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - if ($options['matches']) { |
|
| 140 | - $matches[$champ] = $regs; |
|
| 141 | - } |
|
| 139 | + if ($options['matches']) { |
|
| 140 | + $matches[$champ] = $regs; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - if ( |
|
| 144 | - !$options['champs'] |
|
| 145 | - and !$options['score'] |
|
| 146 | - and !$options['matches'] |
|
| 147 | - ) { |
|
| 148 | - break; |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - } |
|
| 143 | + if ( |
|
| 144 | + !$options['champs'] |
|
| 145 | + and !$options['score'] |
|
| 146 | + and !$options['matches'] |
|
| 147 | + ) { |
|
| 148 | + break; |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - if ($vu) { |
|
| 154 | - if (!isset($results)) { |
|
| 155 | - $results = []; |
|
| 156 | - } |
|
| 157 | - $results[$id] = []; |
|
| 158 | - if ($champs_vus) { |
|
| 159 | - $results[$id]['champs'] = $champs_vus; |
|
| 160 | - } |
|
| 161 | - if ($score) { |
|
| 162 | - $results[$id]['score'] = $score; |
|
| 163 | - } |
|
| 164 | - if ($matches) { |
|
| 165 | - $results[$id]['matches'] = $matches; |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - } |
|
| 153 | + if ($vu) { |
|
| 154 | + if (!isset($results)) { |
|
| 155 | + $results = []; |
|
| 156 | + } |
|
| 157 | + $results[$id] = []; |
|
| 158 | + if ($champs_vus) { |
|
| 159 | + $results[$id]['champs'] = $champs_vus; |
|
| 160 | + } |
|
| 161 | + if ($score) { |
|
| 162 | + $results[$id]['score'] = $score; |
|
| 163 | + } |
|
| 164 | + if ($matches) { |
|
| 165 | + $results[$id]['matches'] = $matches; |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | 171 | |
| 172 | - // Gerer les donnees associees |
|
| 173 | - // ici on est un peu naze : pas capables de reconstruire une jointure complexe |
|
| 174 | - // on ne sait passer que par table de laison en 1 coup |
|
| 175 | - if ( |
|
| 176 | - isset($jointures[$table]) |
|
| 177 | - and $joints = recherche_en_base( |
|
| 178 | - $recherche, |
|
| 179 | - $jointures[$table], |
|
| 180 | - array_merge($options, ['jointures' => false]) |
|
| 181 | - ) |
|
| 182 | - ) { |
|
| 183 | - include_spip('action/editer_liens'); |
|
| 184 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 185 | - $cle_depart = id_table_objet($table); |
|
| 186 | - $table_depart = table_objet($table, $serveur); |
|
| 187 | - $desc_depart = $trouver_table($table_depart, $serveur); |
|
| 188 | - $depart_associable = objet_associable($table); |
|
| 189 | - foreach ($joints as $table_liee => $ids_trouves) { |
|
| 190 | - // on peut definir une fonction de recherche jointe pour regler les cas particuliers |
|
| 191 | - if ( |
|
| 192 | - !( |
|
| 193 | - $rechercher_joints = charger_fonction("rechercher_joints_${table}_${table_liee}", 'inc', true) |
|
| 194 | - or $rechercher_joints = charger_fonction("rechercher_joints_objet_${table_liee}", 'inc', true) |
|
| 195 | - or $rechercher_joints = charger_fonction("rechercher_joints_${table}_objet_lie", 'inc', true) |
|
| 196 | - ) |
|
| 197 | - ) { |
|
| 198 | - $cle_arrivee = id_table_objet($table_liee); |
|
| 199 | - $table_arrivee = table_objet($table_liee, $serveur); |
|
| 200 | - $desc_arrivee = $trouver_table($table_arrivee, $serveur); |
|
| 201 | - // cas simple : $cle_depart dans la table_liee |
|
| 202 | - if (isset($desc_arrivee['field'][$cle_depart])) { |
|
| 203 | - $s = sql_select( |
|
| 204 | - "$cle_depart, $cle_arrivee", |
|
| 205 | - $desc_arrivee['table_sql'], |
|
| 206 | - sql_in($cle_arrivee, array_keys($ids_trouves)), |
|
| 207 | - '', |
|
| 208 | - '', |
|
| 209 | - '', |
|
| 210 | - '', |
|
| 211 | - $serveur |
|
| 212 | - ); |
|
| 213 | - } // cas simple : $cle_arrivee dans la table |
|
| 214 | - elseif (isset($desc_depart['field'][$cle_arrivee])) { |
|
| 215 | - $s = sql_select( |
|
| 216 | - "$cle_depart, $cle_arrivee", |
|
| 217 | - $desc_depart['table_sql'], |
|
| 218 | - sql_in($cle_arrivee, array_keys($ids_trouves)), |
|
| 219 | - '', |
|
| 220 | - '', |
|
| 221 | - '', |
|
| 222 | - '', |
|
| 223 | - $serveur |
|
| 224 | - ); |
|
| 225 | - } |
|
| 226 | - // sinon cherchons une table de liaison |
|
| 227 | - // cas recherche principale article, objet lie document : passer par spip_documents_liens |
|
| 228 | - elseif ($l = objet_associable($table_liee)) { |
|
| 229 | - [$primary, $table_liens] = $l; |
|
| 230 | - $s = sql_select( |
|
| 231 | - "id_objet as $cle_depart, $primary as $cle_arrivee", |
|
| 232 | - $table_liens, |
|
| 233 | - ["objet='$table'", sql_in($primary, array_keys($ids_trouves))], |
|
| 234 | - '', |
|
| 235 | - '', |
|
| 236 | - '', |
|
| 237 | - '', |
|
| 238 | - $serveur |
|
| 239 | - ); |
|
| 240 | - } // cas recherche principale auteur, objet lie article: passer par spip_auteurs_liens |
|
| 241 | - elseif ($l = $depart_associable) { |
|
| 242 | - [$primary, $table_liens] = $l; |
|
| 243 | - $s = sql_select( |
|
| 244 | - "$primary as $cle_depart, id_objet as $cle_arrivee", |
|
| 245 | - $table_liens, |
|
| 246 | - ["objet='$table_liee'", sql_in('id_objet', array_keys($ids_trouves))], |
|
| 247 | - '', |
|
| 248 | - '', |
|
| 249 | - '', |
|
| 250 | - '', |
|
| 251 | - $serveur |
|
| 252 | - ); |
|
| 253 | - } // cas table de liaison generique spip_xxx_yyy |
|
| 254 | - elseif ( |
|
| 255 | - $t = $trouver_table($table_arrivee . '_' . $table_depart, $serveur) |
|
| 256 | - or $t = $trouver_table($table_depart . '_' . $table_arrivee, $serveur) |
|
| 257 | - ) { |
|
| 258 | - $s = sql_select( |
|
| 259 | - "$cle_depart,$cle_arrivee", |
|
| 260 | - $t['table_sql'], |
|
| 261 | - sql_in($cle_arrivee, array_keys($ids_trouves)), |
|
| 262 | - '', |
|
| 263 | - '', |
|
| 264 | - '', |
|
| 265 | - '', |
|
| 266 | - $serveur |
|
| 267 | - ); |
|
| 268 | - } |
|
| 269 | - } else { |
|
| 270 | - [$cle_depart, $cle_arrivee, $s] = $rechercher_joints( |
|
| 271 | - $table, |
|
| 272 | - $table_liee, |
|
| 273 | - array_keys($ids_trouves), |
|
| 274 | - $serveur |
|
| 275 | - ); |
|
| 276 | - } |
|
| 172 | + // Gerer les donnees associees |
|
| 173 | + // ici on est un peu naze : pas capables de reconstruire une jointure complexe |
|
| 174 | + // on ne sait passer que par table de laison en 1 coup |
|
| 175 | + if ( |
|
| 176 | + isset($jointures[$table]) |
|
| 177 | + and $joints = recherche_en_base( |
|
| 178 | + $recherche, |
|
| 179 | + $jointures[$table], |
|
| 180 | + array_merge($options, ['jointures' => false]) |
|
| 181 | + ) |
|
| 182 | + ) { |
|
| 183 | + include_spip('action/editer_liens'); |
|
| 184 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 185 | + $cle_depart = id_table_objet($table); |
|
| 186 | + $table_depart = table_objet($table, $serveur); |
|
| 187 | + $desc_depart = $trouver_table($table_depart, $serveur); |
|
| 188 | + $depart_associable = objet_associable($table); |
|
| 189 | + foreach ($joints as $table_liee => $ids_trouves) { |
|
| 190 | + // on peut definir une fonction de recherche jointe pour regler les cas particuliers |
|
| 191 | + if ( |
|
| 192 | + !( |
|
| 193 | + $rechercher_joints = charger_fonction("rechercher_joints_${table}_${table_liee}", 'inc', true) |
|
| 194 | + or $rechercher_joints = charger_fonction("rechercher_joints_objet_${table_liee}", 'inc', true) |
|
| 195 | + or $rechercher_joints = charger_fonction("rechercher_joints_${table}_objet_lie", 'inc', true) |
|
| 196 | + ) |
|
| 197 | + ) { |
|
| 198 | + $cle_arrivee = id_table_objet($table_liee); |
|
| 199 | + $table_arrivee = table_objet($table_liee, $serveur); |
|
| 200 | + $desc_arrivee = $trouver_table($table_arrivee, $serveur); |
|
| 201 | + // cas simple : $cle_depart dans la table_liee |
|
| 202 | + if (isset($desc_arrivee['field'][$cle_depart])) { |
|
| 203 | + $s = sql_select( |
|
| 204 | + "$cle_depart, $cle_arrivee", |
|
| 205 | + $desc_arrivee['table_sql'], |
|
| 206 | + sql_in($cle_arrivee, array_keys($ids_trouves)), |
|
| 207 | + '', |
|
| 208 | + '', |
|
| 209 | + '', |
|
| 210 | + '', |
|
| 211 | + $serveur |
|
| 212 | + ); |
|
| 213 | + } // cas simple : $cle_arrivee dans la table |
|
| 214 | + elseif (isset($desc_depart['field'][$cle_arrivee])) { |
|
| 215 | + $s = sql_select( |
|
| 216 | + "$cle_depart, $cle_arrivee", |
|
| 217 | + $desc_depart['table_sql'], |
|
| 218 | + sql_in($cle_arrivee, array_keys($ids_trouves)), |
|
| 219 | + '', |
|
| 220 | + '', |
|
| 221 | + '', |
|
| 222 | + '', |
|
| 223 | + $serveur |
|
| 224 | + ); |
|
| 225 | + } |
|
| 226 | + // sinon cherchons une table de liaison |
|
| 227 | + // cas recherche principale article, objet lie document : passer par spip_documents_liens |
|
| 228 | + elseif ($l = objet_associable($table_liee)) { |
|
| 229 | + [$primary, $table_liens] = $l; |
|
| 230 | + $s = sql_select( |
|
| 231 | + "id_objet as $cle_depart, $primary as $cle_arrivee", |
|
| 232 | + $table_liens, |
|
| 233 | + ["objet='$table'", sql_in($primary, array_keys($ids_trouves))], |
|
| 234 | + '', |
|
| 235 | + '', |
|
| 236 | + '', |
|
| 237 | + '', |
|
| 238 | + $serveur |
|
| 239 | + ); |
|
| 240 | + } // cas recherche principale auteur, objet lie article: passer par spip_auteurs_liens |
|
| 241 | + elseif ($l = $depart_associable) { |
|
| 242 | + [$primary, $table_liens] = $l; |
|
| 243 | + $s = sql_select( |
|
| 244 | + "$primary as $cle_depart, id_objet as $cle_arrivee", |
|
| 245 | + $table_liens, |
|
| 246 | + ["objet='$table_liee'", sql_in('id_objet', array_keys($ids_trouves))], |
|
| 247 | + '', |
|
| 248 | + '', |
|
| 249 | + '', |
|
| 250 | + '', |
|
| 251 | + $serveur |
|
| 252 | + ); |
|
| 253 | + } // cas table de liaison generique spip_xxx_yyy |
|
| 254 | + elseif ( |
|
| 255 | + $t = $trouver_table($table_arrivee . '_' . $table_depart, $serveur) |
|
| 256 | + or $t = $trouver_table($table_depart . '_' . $table_arrivee, $serveur) |
|
| 257 | + ) { |
|
| 258 | + $s = sql_select( |
|
| 259 | + "$cle_depart,$cle_arrivee", |
|
| 260 | + $t['table_sql'], |
|
| 261 | + sql_in($cle_arrivee, array_keys($ids_trouves)), |
|
| 262 | + '', |
|
| 263 | + '', |
|
| 264 | + '', |
|
| 265 | + '', |
|
| 266 | + $serveur |
|
| 267 | + ); |
|
| 268 | + } |
|
| 269 | + } else { |
|
| 270 | + [$cle_depart, $cle_arrivee, $s] = $rechercher_joints( |
|
| 271 | + $table, |
|
| 272 | + $table_liee, |
|
| 273 | + array_keys($ids_trouves), |
|
| 274 | + $serveur |
|
| 275 | + ); |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | - while ($t = is_array($s) ? array_shift($s) : sql_fetch($s)) { |
|
| 279 | - $id = $t[$cle_depart]; |
|
| 280 | - $joint = $ids_trouves[$t[$cle_arrivee]]; |
|
| 281 | - if (!isset($results)) { |
|
| 282 | - $results = []; |
|
| 283 | - } |
|
| 284 | - if (!isset($results[$id])) { |
|
| 285 | - $results[$id] = []; |
|
| 286 | - } |
|
| 287 | - if (isset($joint['score']) and $joint['score']) { |
|
| 288 | - if (!isset($results[$id]['score'])) { |
|
| 289 | - $results[$id]['score'] = 0; |
|
| 290 | - } |
|
| 291 | - $results[$id]['score'] += $joint['score']; |
|
| 292 | - } |
|
| 293 | - if (isset($joint['champs']) and $joint['champs']) { |
|
| 294 | - foreach ($joint['champs'] as $c => $val) { |
|
| 295 | - $results[$id]['champs'][$table_liee . '.' . $c] = $val; |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - if (isset($joint['matches']) and $joint['matches']) { |
|
| 299 | - foreach ($joint['matches'] as $c => $val) { |
|
| 300 | - $results[$id]['matches'][$table_liee . '.' . $c] = $val; |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - } |
|
| 278 | + while ($t = is_array($s) ? array_shift($s) : sql_fetch($s)) { |
|
| 279 | + $id = $t[$cle_depart]; |
|
| 280 | + $joint = $ids_trouves[$t[$cle_arrivee]]; |
|
| 281 | + if (!isset($results)) { |
|
| 282 | + $results = []; |
|
| 283 | + } |
|
| 284 | + if (!isset($results[$id])) { |
|
| 285 | + $results[$id] = []; |
|
| 286 | + } |
|
| 287 | + if (isset($joint['score']) and $joint['score']) { |
|
| 288 | + if (!isset($results[$id]['score'])) { |
|
| 289 | + $results[$id]['score'] = 0; |
|
| 290 | + } |
|
| 291 | + $results[$id]['score'] += $joint['score']; |
|
| 292 | + } |
|
| 293 | + if (isset($joint['champs']) and $joint['champs']) { |
|
| 294 | + foreach ($joint['champs'] as $c => $val) { |
|
| 295 | + $results[$id]['champs'][$table_liee . '.' . $c] = $val; |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + if (isset($joint['matches']) and $joint['matches']) { |
|
| 299 | + foreach ($joint['matches'] as $c => $val) { |
|
| 300 | + $results[$id]['matches'][$table_liee . '.' . $c] = $val; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | 306 | |
| 307 | - return $results; |
|
| 307 | + return $results; |
|
| 308 | 308 | } |
@@ -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 ['', '']; |
|
| 37 | + return ['', '']; |
|
| 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,22 +102,22 @@ discard block |
||
| 102 | 102 | * @return string |
| 103 | 103 | */ |
| 104 | 104 | function echappe_js($t, $class = ' class = "echappe-js"') { |
| 105 | - foreach (['script', 'iframe'] as $tag) { |
|
| 106 | - if ( |
|
| 107 | - stripos($t, (string) "<$tag") !== false |
|
| 108 | - and preg_match_all(',<' . $tag . '.*?($|</' . $tag . '.),isS', $t, $r, PREG_SET_ORDER) |
|
| 109 | - ) { |
|
| 110 | - foreach ($r as $regs) { |
|
| 111 | - $t = str_replace( |
|
| 112 | - $regs[0], |
|
| 113 | - "<code$class>" . nl2br(spip_htmlspecialchars($regs[0])) . '</code>', |
|
| 114 | - $t |
|
| 115 | - ); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $t; |
|
| 105 | + foreach (['script', 'iframe'] as $tag) { |
|
| 106 | + if ( |
|
| 107 | + stripos($t, (string) "<$tag") !== false |
|
| 108 | + and preg_match_all(',<' . $tag . '.*?($|</' . $tag . '.),isS', $t, $r, PREG_SET_ORDER) |
|
| 109 | + ) { |
|
| 110 | + foreach ($r as $regs) { |
|
| 111 | + $t = str_replace( |
|
| 112 | + $regs[0], |
|
| 113 | + "<code$class>" . nl2br(spip_htmlspecialchars($regs[0])) . '</code>', |
|
| 114 | + $t |
|
| 115 | + ); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $t; |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | |
@@ -146,55 +146,55 @@ discard block |
||
| 146 | 146 | * Code protégé |
| 147 | 147 | **/ |
| 148 | 148 | function interdire_scripts($arg, $mode_filtre = null) { |
| 149 | - // on memorise le resultat sur les arguments non triviaux |
|
| 150 | - static $dejavu = []; |
|
| 151 | - |
|
| 152 | - // Attention, si ce n'est pas une chaine, laisser intact |
|
| 153 | - if (!$arg or !is_string($arg) or !strstr($arg, '<')) { |
|
| 154 | - return $arg; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if (is_null($mode_filtre) or !in_array($mode_filtre, [-1, 0, 1])) { |
|
| 158 | - $mode_filtre = $GLOBALS['filtrer_javascript']; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if (isset($dejavu[$mode_filtre][$arg])) { |
|
| 162 | - return $dejavu[$mode_filtre][$arg]; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // echapper les tags asp/php |
|
| 166 | - $t = str_replace('<' . '%', '<%', $arg); |
|
| 167 | - |
|
| 168 | - // echapper le php |
|
| 169 | - $t = str_replace('<' . '?', '<?', $t); |
|
| 170 | - |
|
| 171 | - // echapper le < script language=php > |
|
| 172 | - $t = preg_replace(',<(script\b[^>]+\blanguage\b[^\w>]+php\b),UimsS', '<\1', $t); |
|
| 173 | - |
|
| 174 | - // Pour le js, trois modes : parano (-1), prive (0), ok (1) |
|
| 175 | - switch ($mode_filtre) { |
|
| 176 | - case 0: |
|
| 177 | - if (!_DIR_RESTREINT) { |
|
| 178 | - $t = echappe_js($t); |
|
| 179 | - } |
|
| 180 | - break; |
|
| 181 | - case -1: |
|
| 182 | - $t = echappe_js($t); |
|
| 183 | - break; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - // pas de <base href /> svp ! |
|
| 187 | - $t = preg_replace(',<(base\b),iS', '<\1', $t); |
|
| 188 | - |
|
| 189 | - // Reinserer les echappements des modeles |
|
| 190 | - if (defined('_PROTEGE_JS_MODELES')) { |
|
| 191 | - $t = echappe_retour($t, 'javascript' . _PROTEGE_JS_MODELES); |
|
| 192 | - } |
|
| 193 | - if (defined('_PROTEGE_PHP_MODELES')) { |
|
| 194 | - $t = echappe_retour($t, 'php' . _PROTEGE_PHP_MODELES); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return $dejavu[$mode_filtre][$arg] = $t; |
|
| 149 | + // on memorise le resultat sur les arguments non triviaux |
|
| 150 | + static $dejavu = []; |
|
| 151 | + |
|
| 152 | + // Attention, si ce n'est pas une chaine, laisser intact |
|
| 153 | + if (!$arg or !is_string($arg) or !strstr($arg, '<')) { |
|
| 154 | + return $arg; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if (is_null($mode_filtre) or !in_array($mode_filtre, [-1, 0, 1])) { |
|
| 158 | + $mode_filtre = $GLOBALS['filtrer_javascript']; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if (isset($dejavu[$mode_filtre][$arg])) { |
|
| 162 | + return $dejavu[$mode_filtre][$arg]; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // echapper les tags asp/php |
|
| 166 | + $t = str_replace('<' . '%', '<%', $arg); |
|
| 167 | + |
|
| 168 | + // echapper le php |
|
| 169 | + $t = str_replace('<' . '?', '<?', $t); |
|
| 170 | + |
|
| 171 | + // echapper le < script language=php > |
|
| 172 | + $t = preg_replace(',<(script\b[^>]+\blanguage\b[^\w>]+php\b),UimsS', '<\1', $t); |
|
| 173 | + |
|
| 174 | + // Pour le js, trois modes : parano (-1), prive (0), ok (1) |
|
| 175 | + switch ($mode_filtre) { |
|
| 176 | + case 0: |
|
| 177 | + if (!_DIR_RESTREINT) { |
|
| 178 | + $t = echappe_js($t); |
|
| 179 | + } |
|
| 180 | + break; |
|
| 181 | + case -1: |
|
| 182 | + $t = echappe_js($t); |
|
| 183 | + break; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + // pas de <base href /> svp ! |
|
| 187 | + $t = preg_replace(',<(base\b),iS', '<\1', $t); |
|
| 188 | + |
|
| 189 | + // Reinserer les echappements des modeles |
|
| 190 | + if (defined('_PROTEGE_JS_MODELES')) { |
|
| 191 | + $t = echappe_retour($t, 'javascript' . _PROTEGE_JS_MODELES); |
|
| 192 | + } |
|
| 193 | + if (defined('_PROTEGE_PHP_MODELES')) { |
|
| 194 | + $t = echappe_retour($t, 'php' . _PROTEGE_PHP_MODELES); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return $dejavu[$mode_filtre][$arg] = $t; |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | |
@@ -223,66 +223,66 @@ discard block |
||
| 223 | 223 | * Texte transformé |
| 224 | 224 | **/ |
| 225 | 225 | function typo($letexte, $echapper = true, $connect = null, $env = []) { |
| 226 | - // Plus vite ! |
|
| 227 | - if (!$letexte) { |
|
| 228 | - return $letexte; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // les appels directs a cette fonction depuis le php de l'espace |
|
| 232 | - // prive etant historiquement ecrit sans argment $connect |
|
| 233 | - // on utilise la presence de celui-ci pour distinguer les cas |
|
| 234 | - // ou il faut passer interdire_script explicitement |
|
| 235 | - // les appels dans les squelettes (de l'espace prive) fournissant un $connect |
|
| 236 | - // ne seront pas perturbes |
|
| 237 | - $interdire_script = false; |
|
| 238 | - if (is_null($connect)) { |
|
| 239 | - $connect = ''; |
|
| 240 | - $interdire_script = true; |
|
| 241 | - $env['espace_prive'] = test_espace_prive(); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - // Echapper les codes <html> etc |
|
| 245 | - if ($echapper) { |
|
| 246 | - $letexte = echappe_html($letexte, 'TYPO'); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - // |
|
| 250 | - // Installer les modeles, notamment images et documents ; |
|
| 251 | - // |
|
| 252 | - // NOTE : propre() ne passe pas par ici mais directement par corriger_typo |
|
| 253 | - // cf. inc/lien |
|
| 254 | - |
|
| 255 | - $letexte = traiter_modeles($mem = $letexte, false, $echapper ? 'TYPO' : '', $connect, null, $env); |
|
| 256 | - if ($letexte != $mem) { |
|
| 257 | - $echapper = true; |
|
| 258 | - } |
|
| 259 | - unset($mem); |
|
| 260 | - |
|
| 261 | - $letexte = corriger_typo($letexte); |
|
| 262 | - $letexte = echapper_faux_tags($letexte); |
|
| 263 | - |
|
| 264 | - // reintegrer les echappements |
|
| 265 | - if ($echapper) { |
|
| 266 | - $letexte = echappe_retour($letexte, 'TYPO'); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // Dans les appels directs hors squelette, securiser ici aussi |
|
| 270 | - if ($interdire_script) { |
|
| 271 | - $letexte = interdire_scripts($letexte); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - // Dans l'espace prive on se mefie de tout contenu dangereux |
|
| 275 | - // https://core.spip.net/issues/3371 |
|
| 276 | - // et aussi dans l'espace public si la globale filtrer_javascript = -1 |
|
| 277 | - // https://core.spip.net/issues/4166 |
|
| 278 | - if ( |
|
| 279 | - $GLOBALS['filtrer_javascript'] == -1 |
|
| 280 | - or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript'] <= 0) |
|
| 281 | - ) { |
|
| 282 | - $letexte = echapper_html_suspect($letexte); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - return $letexte; |
|
| 226 | + // Plus vite ! |
|
| 227 | + if (!$letexte) { |
|
| 228 | + return $letexte; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // les appels directs a cette fonction depuis le php de l'espace |
|
| 232 | + // prive etant historiquement ecrit sans argment $connect |
|
| 233 | + // on utilise la presence de celui-ci pour distinguer les cas |
|
| 234 | + // ou il faut passer interdire_script explicitement |
|
| 235 | + // les appels dans les squelettes (de l'espace prive) fournissant un $connect |
|
| 236 | + // ne seront pas perturbes |
|
| 237 | + $interdire_script = false; |
|
| 238 | + if (is_null($connect)) { |
|
| 239 | + $connect = ''; |
|
| 240 | + $interdire_script = true; |
|
| 241 | + $env['espace_prive'] = test_espace_prive(); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + // Echapper les codes <html> etc |
|
| 245 | + if ($echapper) { |
|
| 246 | + $letexte = echappe_html($letexte, 'TYPO'); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + // |
|
| 250 | + // Installer les modeles, notamment images et documents ; |
|
| 251 | + // |
|
| 252 | + // NOTE : propre() ne passe pas par ici mais directement par corriger_typo |
|
| 253 | + // cf. inc/lien |
|
| 254 | + |
|
| 255 | + $letexte = traiter_modeles($mem = $letexte, false, $echapper ? 'TYPO' : '', $connect, null, $env); |
|
| 256 | + if ($letexte != $mem) { |
|
| 257 | + $echapper = true; |
|
| 258 | + } |
|
| 259 | + unset($mem); |
|
| 260 | + |
|
| 261 | + $letexte = corriger_typo($letexte); |
|
| 262 | + $letexte = echapper_faux_tags($letexte); |
|
| 263 | + |
|
| 264 | + // reintegrer les echappements |
|
| 265 | + if ($echapper) { |
|
| 266 | + $letexte = echappe_retour($letexte, 'TYPO'); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // Dans les appels directs hors squelette, securiser ici aussi |
|
| 270 | + if ($interdire_script) { |
|
| 271 | + $letexte = interdire_scripts($letexte); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + // Dans l'espace prive on se mefie de tout contenu dangereux |
|
| 275 | + // https://core.spip.net/issues/3371 |
|
| 276 | + // et aussi dans l'espace public si la globale filtrer_javascript = -1 |
|
| 277 | + // https://core.spip.net/issues/4166 |
|
| 278 | + if ( |
|
| 279 | + $GLOBALS['filtrer_javascript'] == -1 |
|
| 280 | + or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript'] <= 0) |
|
| 281 | + ) { |
|
| 282 | + $letexte = echapper_html_suspect($letexte); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + return $letexte; |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | // Correcteur typographique |
@@ -307,57 +307,57 @@ discard block |
||
| 307 | 307 | */ |
| 308 | 308 | function corriger_typo($letexte, $lang = '') { |
| 309 | 309 | |
| 310 | - // Plus vite ! |
|
| 311 | - if (!$letexte) { |
|
| 312 | - return $letexte; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - $letexte = pipeline('pre_typo', $letexte); |
|
| 316 | - |
|
| 317 | - // Caracteres de controle "illegaux" |
|
| 318 | - $letexte = corriger_caracteres($letexte); |
|
| 319 | - |
|
| 320 | - // Proteger les caracteres typographiques a l'interieur des tags html |
|
| 321 | - if (preg_match_all(_TYPO_BALISE, $letexte, $regs, PREG_SET_ORDER)) { |
|
| 322 | - foreach ($regs as $reg) { |
|
| 323 | - $insert = $reg[0]; |
|
| 324 | - // hack: on transforme les caracteres a proteger en les remplacant |
|
| 325 | - // par des caracteres "illegaux". (cf corriger_caracteres()) |
|
| 326 | - $insert = strtr($insert, _TYPO_PROTEGER, _TYPO_PROTECTEUR); |
|
| 327 | - $letexte = str_replace($reg[0], $insert, $letexte); |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - // trouver les blocs idiomes et les traiter à part |
|
| 332 | - $letexte = extraire_idiome($ei = $letexte, $lang, true); |
|
| 333 | - $ei = ($ei !== $letexte); |
|
| 334 | - |
|
| 335 | - // trouver les blocs multi et les traiter a part |
|
| 336 | - $letexte = extraire_multi($em = $letexte, $lang, true); |
|
| 337 | - $em = ($em !== $letexte); |
|
| 338 | - |
|
| 339 | - // Charger & appliquer les fonctions de typographie |
|
| 340 | - $typographie = charger_fonction(lang_typo($lang), 'typographie'); |
|
| 341 | - $letexte = $typographie($letexte); |
|
| 342 | - |
|
| 343 | - // Les citations en une autre langue, s'il y a lieu |
|
| 344 | - if ($em) { |
|
| 345 | - $letexte = echappe_retour($letexte, 'multi'); |
|
| 346 | - } |
|
| 347 | - if ($ei) { |
|
| 348 | - $letexte = echappe_retour($letexte, 'idiome'); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - // Retablir les caracteres proteges |
|
| 352 | - $letexte = strtr($letexte, _TYPO_PROTECTEUR, _TYPO_PROTEGER); |
|
| 353 | - |
|
| 354 | - // pipeline |
|
| 355 | - $letexte = pipeline('post_typo', $letexte); |
|
| 356 | - |
|
| 357 | - # un message pour abs_url - on est passe en mode texte |
|
| 358 | - $GLOBALS['mode_abs_url'] = 'texte'; |
|
| 359 | - |
|
| 360 | - return $letexte; |
|
| 310 | + // Plus vite ! |
|
| 311 | + if (!$letexte) { |
|
| 312 | + return $letexte; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + $letexte = pipeline('pre_typo', $letexte); |
|
| 316 | + |
|
| 317 | + // Caracteres de controle "illegaux" |
|
| 318 | + $letexte = corriger_caracteres($letexte); |
|
| 319 | + |
|
| 320 | + // Proteger les caracteres typographiques a l'interieur des tags html |
|
| 321 | + if (preg_match_all(_TYPO_BALISE, $letexte, $regs, PREG_SET_ORDER)) { |
|
| 322 | + foreach ($regs as $reg) { |
|
| 323 | + $insert = $reg[0]; |
|
| 324 | + // hack: on transforme les caracteres a proteger en les remplacant |
|
| 325 | + // par des caracteres "illegaux". (cf corriger_caracteres()) |
|
| 326 | + $insert = strtr($insert, _TYPO_PROTEGER, _TYPO_PROTECTEUR); |
|
| 327 | + $letexte = str_replace($reg[0], $insert, $letexte); |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + // trouver les blocs idiomes et les traiter à part |
|
| 332 | + $letexte = extraire_idiome($ei = $letexte, $lang, true); |
|
| 333 | + $ei = ($ei !== $letexte); |
|
| 334 | + |
|
| 335 | + // trouver les blocs multi et les traiter a part |
|
| 336 | + $letexte = extraire_multi($em = $letexte, $lang, true); |
|
| 337 | + $em = ($em !== $letexte); |
|
| 338 | + |
|
| 339 | + // Charger & appliquer les fonctions de typographie |
|
| 340 | + $typographie = charger_fonction(lang_typo($lang), 'typographie'); |
|
| 341 | + $letexte = $typographie($letexte); |
|
| 342 | + |
|
| 343 | + // Les citations en une autre langue, s'il y a lieu |
|
| 344 | + if ($em) { |
|
| 345 | + $letexte = echappe_retour($letexte, 'multi'); |
|
| 346 | + } |
|
| 347 | + if ($ei) { |
|
| 348 | + $letexte = echappe_retour($letexte, 'idiome'); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + // Retablir les caracteres proteges |
|
| 352 | + $letexte = strtr($letexte, _TYPO_PROTECTEUR, _TYPO_PROTEGER); |
|
| 353 | + |
|
| 354 | + // pipeline |
|
| 355 | + $letexte = pipeline('post_typo', $letexte); |
|
| 356 | + |
|
| 357 | + # un message pour abs_url - on est passe en mode texte |
|
| 358 | + $GLOBALS['mode_abs_url'] = 'texte'; |
|
| 359 | + |
|
| 360 | + return $letexte; |
|
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | |
@@ -373,7 +373,7 @@ discard block |
||
| 373 | 373 | * @return string |
| 374 | 374 | */ |
| 375 | 375 | function paragrapher($letexte, $forcer = true) { |
| 376 | - return $letexte; |
|
| 376 | + return $letexte; |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | /** |
@@ -385,11 +385,11 @@ discard block |
||
| 385 | 385 | * @return string Texte |
| 386 | 386 | **/ |
| 387 | 387 | function traiter_retours_chariots($letexte) { |
| 388 | - $letexte = preg_replace(",\r\n?,S", "\n", $letexte); |
|
| 389 | - $letexte = preg_replace(',<p[>[:space:]],iS', "\n\n\\0", $letexte); |
|
| 390 | - $letexte = preg_replace(',</p[>[:space:]],iS', "\\0\n\n", $letexte); |
|
| 388 | + $letexte = preg_replace(",\r\n?,S", "\n", $letexte); |
|
| 389 | + $letexte = preg_replace(',<p[>[:space:]],iS', "\n\n\\0", $letexte); |
|
| 390 | + $letexte = preg_replace(',</p[>[:space:]],iS', "\\0\n\n", $letexte); |
|
| 391 | 391 | |
| 392 | - return $letexte; |
|
| 392 | + return $letexte; |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | |
@@ -415,39 +415,39 @@ discard block |
||
| 415 | 415 | * Texte transformé |
| 416 | 416 | **/ |
| 417 | 417 | function propre($t, $connect = null, $env = []) { |
| 418 | - // les appels directs a cette fonction depuis le php de l'espace |
|
| 419 | - // prive etant historiquement ecrits sans argment $connect |
|
| 420 | - // on utilise la presence de celui-ci pour distinguer les cas |
|
| 421 | - // ou il faut passer interdire_script explicitement |
|
| 422 | - // les appels dans les squelettes (de l'espace prive) fournissant un $connect |
|
| 423 | - // ne seront pas perturbes |
|
| 424 | - $interdire_script = false; |
|
| 425 | - if (is_null($connect)) { |
|
| 426 | - $connect = ''; |
|
| 427 | - $interdire_script = true; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - if (!$t) { |
|
| 431 | - return strval($t); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - // Dans l'espace prive on se mefie de tout contenu dangereux |
|
| 435 | - // avant echappement des balises <html> |
|
| 436 | - // https://core.spip.net/issues/3371 |
|
| 437 | - // et aussi dans l'espace public si la globale filtrer_javascript = -1 |
|
| 438 | - // https://core.spip.net/issues/4166 |
|
| 439 | - if ( |
|
| 440 | - $interdire_script |
|
| 441 | - or $GLOBALS['filtrer_javascript'] == -1 |
|
| 442 | - or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript'] <= 0) |
|
| 443 | - or (isset($env['wysiwyg']) and $env['wysiwyg'] and $GLOBALS['filtrer_javascript'] <= 0) |
|
| 444 | - ) { |
|
| 445 | - $t = echapper_html_suspect($t, false); |
|
| 446 | - } |
|
| 447 | - $t = echappe_html($t); |
|
| 448 | - $t = expanser_liens($t, $connect, $env); |
|
| 449 | - $t = traiter_raccourcis($t); |
|
| 450 | - $t = echappe_retour_modeles($t, $interdire_script); |
|
| 451 | - |
|
| 452 | - return $t; |
|
| 418 | + // les appels directs a cette fonction depuis le php de l'espace |
|
| 419 | + // prive etant historiquement ecrits sans argment $connect |
|
| 420 | + // on utilise la presence de celui-ci pour distinguer les cas |
|
| 421 | + // ou il faut passer interdire_script explicitement |
|
| 422 | + // les appels dans les squelettes (de l'espace prive) fournissant un $connect |
|
| 423 | + // ne seront pas perturbes |
|
| 424 | + $interdire_script = false; |
|
| 425 | + if (is_null($connect)) { |
|
| 426 | + $connect = ''; |
|
| 427 | + $interdire_script = true; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + if (!$t) { |
|
| 431 | + return strval($t); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + // Dans l'espace prive on se mefie de tout contenu dangereux |
|
| 435 | + // avant echappement des balises <html> |
|
| 436 | + // https://core.spip.net/issues/3371 |
|
| 437 | + // et aussi dans l'espace public si la globale filtrer_javascript = -1 |
|
| 438 | + // https://core.spip.net/issues/4166 |
|
| 439 | + if ( |
|
| 440 | + $interdire_script |
|
| 441 | + or $GLOBALS['filtrer_javascript'] == -1 |
|
| 442 | + or (isset($env['espace_prive']) and $env['espace_prive'] and $GLOBALS['filtrer_javascript'] <= 0) |
|
| 443 | + or (isset($env['wysiwyg']) and $env['wysiwyg'] and $GLOBALS['filtrer_javascript'] <= 0) |
|
| 444 | + ) { |
|
| 445 | + $t = echapper_html_suspect($t, false); |
|
| 446 | + } |
|
| 447 | + $t = echappe_html($t); |
|
| 448 | + $t = expanser_liens($t, $connect, $env); |
|
| 449 | + $t = traiter_raccourcis($t); |
|
| 450 | + $t = echappe_retour_modeles($t, $interdire_script); |
|
| 451 | + |
|
| 452 | + return $t; |
|
| 453 | 453 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | **/ |
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
@@ -43,22 +43,22 @@ discard block |
||
| 43 | 43 | * Pile complétée par le code à générer |
| 44 | 44 | */ |
| 45 | 45 | function balise_ALERTE_MESSAGE_dist($p) { |
| 46 | - $_texte = interprete_argument_balise(1, $p); |
|
| 47 | - $_titre = interprete_argument_balise(2, $p); |
|
| 48 | - $_class = interprete_argument_balise(3, $p); |
|
| 49 | - $_role = interprete_argument_balise(4, $p); |
|
| 50 | - $_id = interprete_argument_balise(5, $p); |
|
| 51 | - $_texte = ($_texte ?: "''"); |
|
| 52 | - $_titre = ($_titre ? ", $_titre" : ', null'); |
|
| 53 | - $_class = ($_class ? ", $_class" : ', null'); |
|
| 54 | - $_role = ($_role ? ", $_role" : ', null'); |
|
| 55 | - $_id = ($_id ? ", $_id" : ', null'); |
|
| 46 | + $_texte = interprete_argument_balise(1, $p); |
|
| 47 | + $_titre = interprete_argument_balise(2, $p); |
|
| 48 | + $_class = interprete_argument_balise(3, $p); |
|
| 49 | + $_role = interprete_argument_balise(4, $p); |
|
| 50 | + $_id = interprete_argument_balise(5, $p); |
|
| 51 | + $_texte = ($_texte ?: "''"); |
|
| 52 | + $_titre = ($_titre ? ", $_titre" : ', null'); |
|
| 53 | + $_class = ($_class ? ", $_class" : ', null'); |
|
| 54 | + $_role = ($_role ? ", $_role" : ', null'); |
|
| 55 | + $_id = ($_id ? ", $_id" : ', null'); |
|
| 56 | 56 | |
| 57 | - $f = chercher_filtre('message_alerte'); |
|
| 58 | - $p->code = "$f($_texte$_titre$_class$_role$_id)"; |
|
| 59 | - $p->interdire_scripts = false; |
|
| 57 | + $f = chercher_filtre('message_alerte'); |
|
| 58 | + $p->code = "$f($_texte$_titre$_class$_role$_id)"; |
|
| 59 | + $p->interdire_scripts = false; |
|
| 60 | 60 | |
| 61 | - return $p; |
|
| 61 | + return $p; |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
@@ -86,20 +86,20 @@ discard block |
||
| 86 | 86 | * Pile complétée par le code à générer |
| 87 | 87 | */ |
| 88 | 88 | function balise_ALERTE_OUVRIR_dist($p) { |
| 89 | - $_titre = interprete_argument_balise(1, $p); |
|
| 90 | - $_class = interprete_argument_balise(2, $p); |
|
| 91 | - $_role = interprete_argument_balise(3, $p); |
|
| 92 | - $_id = interprete_argument_balise(4, $p); |
|
| 93 | - $_titre = ($_titre ? "$_titre" : 'null'); |
|
| 94 | - $_class = ($_class ? ", $_class" : ', null'); |
|
| 95 | - $_role = ($_role ? ", $_role" : ', null'); |
|
| 96 | - $_id = ($_id ? ", $_id" : ', null'); |
|
| 89 | + $_titre = interprete_argument_balise(1, $p); |
|
| 90 | + $_class = interprete_argument_balise(2, $p); |
|
| 91 | + $_role = interprete_argument_balise(3, $p); |
|
| 92 | + $_id = interprete_argument_balise(4, $p); |
|
| 93 | + $_titre = ($_titre ? "$_titre" : 'null'); |
|
| 94 | + $_class = ($_class ? ", $_class" : ', null'); |
|
| 95 | + $_role = ($_role ? ", $_role" : ', null'); |
|
| 96 | + $_id = ($_id ? ", $_id" : ', null'); |
|
| 97 | 97 | |
| 98 | - $f = chercher_filtre('message_alerte_ouvrir'); |
|
| 99 | - $p->code = "$f($_titre$_class$_role$_id)"; |
|
| 100 | - $p->interdire_scripts = false; |
|
| 98 | + $f = chercher_filtre('message_alerte_ouvrir'); |
|
| 99 | + $p->code = "$f($_titre$_class$_role$_id)"; |
|
| 100 | + $p->interdire_scripts = false; |
|
| 101 | 101 | |
| 102 | - return $p; |
|
| 102 | + return $p; |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | /** |
@@ -121,11 +121,11 @@ discard block |
||
| 121 | 121 | * Pile complétée par le code à générer |
| 122 | 122 | */ |
| 123 | 123 | function balise_ALERTE_FERMER_dist($p) { |
| 124 | - $f = chercher_filtre('message_alerte_fermer'); |
|
| 125 | - $p->code = "$f()"; |
|
| 126 | - $p->interdire_scripts = false; |
|
| 124 | + $f = chercher_filtre('message_alerte_fermer'); |
|
| 125 | + $p->code = "$f()"; |
|
| 126 | + $p->interdire_scripts = false; |
|
| 127 | 127 | |
| 128 | - return $p; |
|
| 128 | + return $p; |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
@@ -160,14 +160,14 @@ discard block |
||
| 160 | 160 | */ |
| 161 | 161 | function message_alerte(string $texte, ?string $titre = null, ?string $class = null, ?string $role = null, ?string $id = null): string { |
| 162 | 162 | |
| 163 | - $message_alerte_ouvrir = chercher_filtre('message_alerte_ouvrir'); |
|
| 164 | - $message_alerte_fermer = chercher_filtre('message_alerte_fermer'); |
|
| 165 | - $message = |
|
| 166 | - $message_alerte_ouvrir($titre, $class, $role, $id) . |
|
| 167 | - $texte . |
|
| 168 | - $message_alerte_fermer(); |
|
| 163 | + $message_alerte_ouvrir = chercher_filtre('message_alerte_ouvrir'); |
|
| 164 | + $message_alerte_fermer = chercher_filtre('message_alerte_fermer'); |
|
| 165 | + $message = |
|
| 166 | + $message_alerte_ouvrir($titre, $class, $role, $id) . |
|
| 167 | + $texte . |
|
| 168 | + $message_alerte_fermer(); |
|
| 169 | 169 | |
| 170 | - return $message; |
|
| 170 | + return $message; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -198,55 +198,55 @@ discard block |
||
| 198 | 198 | */ |
| 199 | 199 | function message_alerte_ouvrir(?string $titre = null, ?string $class = null, ?string $role = null, ?string $id = null): string { |
| 200 | 200 | |
| 201 | - $prive = test_espace_prive(); |
|
| 201 | + $prive = test_espace_prive(); |
|
| 202 | 202 | |
| 203 | - // Valeurs par défaut |
|
| 204 | - $role ??= 'alert'; // fallback uniquement si null |
|
| 205 | - $class ??= 'notice'; // fallback uniquement si null |
|
| 203 | + // Valeurs par défaut |
|
| 204 | + $role ??= 'alert'; // fallback uniquement si null |
|
| 205 | + $class ??= 'notice'; // fallback uniquement si null |
|
| 206 | 206 | |
| 207 | - // Type d'alerte : le chercher dans les classes, nettoyer celles-ci, puis le réinjecter |
|
| 208 | - $types = [ |
|
| 209 | - 'notice', |
|
| 210 | - 'error', |
|
| 211 | - 'success', |
|
| 212 | - 'info', |
|
| 213 | - ]; |
|
| 214 | - $type = array_intersect(explode(' ', $class), $types); |
|
| 215 | - $type = reset($type); |
|
| 216 | - $class = trim(str_replace($types, '', $class) . " $type"); |
|
| 207 | + // Type d'alerte : le chercher dans les classes, nettoyer celles-ci, puis le réinjecter |
|
| 208 | + $types = [ |
|
| 209 | + 'notice', |
|
| 210 | + 'error', |
|
| 211 | + 'success', |
|
| 212 | + 'info', |
|
| 213 | + ]; |
|
| 214 | + $type = array_intersect(explode(' ', $class), $types); |
|
| 215 | + $type = reset($type); |
|
| 216 | + $class = trim(str_replace($types, '', $class) . " $type"); |
|
| 217 | 217 | |
| 218 | - // Classes |
|
| 219 | - $class_racine = 'msg-alert'; |
|
| 220 | - $clearfix = ($prive ? 'clearfix' : ''); |
|
| 221 | - $class_alerte = "$class_racine $class"; |
|
| 222 | - $class_texte = "${class_racine}__text $clearfix"; |
|
| 223 | - $class_titre = "${class_racine}__heading"; |
|
| 218 | + // Classes |
|
| 219 | + $class_racine = 'msg-alert'; |
|
| 220 | + $clearfix = ($prive ? 'clearfix' : ''); |
|
| 221 | + $class_alerte = "$class_racine $class"; |
|
| 222 | + $class_texte = "${class_racine}__text $clearfix"; |
|
| 223 | + $class_titre = "${class_racine}__heading"; |
|
| 224 | 224 | |
| 225 | - // Titre : markup |
|
| 226 | - $titre = trim($titre); |
|
| 227 | - if (strlen($titre)) { |
|
| 228 | - include_spip('inc/filtres'); |
|
| 229 | - // Si besoin on encapsule le titre : un h3 dans le privé, un simple div sinon. |
|
| 230 | - $cherche_tag = ($prive ? '<h' : '<'); |
|
| 231 | - $wrap_tag = ($prive ? '<h3>' : '<div>'); |
|
| 232 | - if (strpos($titre, $cherche_tag) !== 0) { |
|
| 233 | - $titre = wrap($titre, $wrap_tag); |
|
| 234 | - } |
|
| 235 | - // puis on ajoute la classe |
|
| 236 | - $titre = ajouter_class($titre, $class_titre); |
|
| 237 | - } |
|
| 225 | + // Titre : markup |
|
| 226 | + $titre = trim($titre); |
|
| 227 | + if (strlen($titre)) { |
|
| 228 | + include_spip('inc/filtres'); |
|
| 229 | + // Si besoin on encapsule le titre : un h3 dans le privé, un simple div sinon. |
|
| 230 | + $cherche_tag = ($prive ? '<h' : '<'); |
|
| 231 | + $wrap_tag = ($prive ? '<h3>' : '<div>'); |
|
| 232 | + if (strpos($titre, $cherche_tag) !== 0) { |
|
| 233 | + $titre = wrap($titre, $wrap_tag); |
|
| 234 | + } |
|
| 235 | + // puis on ajoute la classe |
|
| 236 | + $titre = ajouter_class($titre, $class_titre); |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - // Attributs |
|
| 240 | - $attr_role = ($role ? "role=\"$role\"" : ''); |
|
| 241 | - $attr_id = ($id ? "id=\"$id\"" : ''); |
|
| 242 | - $attr_data = ($type ? "data-alert=\"$type\"" : ''); |
|
| 239 | + // Attributs |
|
| 240 | + $attr_role = ($role ? "role=\"$role\"" : ''); |
|
| 241 | + $attr_id = ($id ? "id=\"$id\"" : ''); |
|
| 242 | + $attr_data = ($type ? "data-alert=\"$type\"" : ''); |
|
| 243 | 243 | |
| 244 | - $message = |
|
| 245 | - "<div class=\"$class_alerte\" $attr_role $attr_id $attr_data>" |
|
| 246 | - . $titre |
|
| 247 | - . "<div class=\"$class_texte\">"; |
|
| 244 | + $message = |
|
| 245 | + "<div class=\"$class_alerte\" $attr_role $attr_id $attr_data>" |
|
| 246 | + . $titre |
|
| 247 | + . "<div class=\"$class_texte\">"; |
|
| 248 | 248 | |
| 249 | - return $message; |
|
| 249 | + return $message; |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | /** |
@@ -260,5 +260,5 @@ discard block |
||
| 260 | 260 | * HTML de fin de l'alerte |
| 261 | 261 | */ |
| 262 | 262 | function message_alerte_fermer(): string { |
| 263 | - return '</div></div>'; |
|
| 263 | + return '</div></div>'; |
|
| 264 | 264 | } |
@@ -17,12 +17,12 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | if (!defined('_TEST_FILE_EXISTS')) { |
| 24 | - /** Permettre d'éviter des tests file_exists sur certains hébergeurs */ |
|
| 25 | - define('_TEST_FILE_EXISTS', preg_match(',(online|free)[.]fr$,', $_ENV['HTTP_HOST'] ?? '')); |
|
| 24 | + /** Permettre d'éviter des tests file_exists sur certains hébergeurs */ |
|
| 25 | + define('_TEST_FILE_EXISTS', preg_match(',(online|free)[.]fr$,', $_ENV['HTTP_HOST'] ?? '')); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | #define('_SPIP_LOCK_MODE',0); // ne pas utiliser de lock (deconseille) |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | #define('_SPIP_LOCK_MODE',2); // utiliser le nfslock de spip |
| 31 | 31 | |
| 32 | 32 | if (_SPIP_LOCK_MODE == 2) { |
| 33 | - include_spip('inc/nfslock'); |
|
| 33 | + include_spip('inc/nfslock'); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | $GLOBALS['liste_verrous'] = []; |
@@ -53,24 +53,24 @@ discard block |
||
| 53 | 53 | * Ressource sur le fichier ouvert, sinon false. |
| 54 | 54 | **/ |
| 55 | 55 | function spip_fopen_lock($fichier, $mode, $verrou) { |
| 56 | - if (_SPIP_LOCK_MODE == 1) { |
|
| 57 | - if ($fl = @fopen($fichier, $mode)) { |
|
| 58 | - // verrou |
|
| 59 | - @flock($fl, $verrou); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - return $fl; |
|
| 63 | - } elseif (_SPIP_LOCK_MODE == 2) { |
|
| 64 | - if (($verrou = spip_nfslock($fichier)) && ($fl = @fopen($fichier, $mode))) { |
|
| 65 | - $GLOBALS['liste_verrous'][$fl] = [$fichier, $verrou]; |
|
| 66 | - |
|
| 67 | - return $fl; |
|
| 68 | - } else { |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - return @fopen($fichier, $mode); |
|
| 56 | + if (_SPIP_LOCK_MODE == 1) { |
|
| 57 | + if ($fl = @fopen($fichier, $mode)) { |
|
| 58 | + // verrou |
|
| 59 | + @flock($fl, $verrou); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + return $fl; |
|
| 63 | + } elseif (_SPIP_LOCK_MODE == 2) { |
|
| 64 | + if (($verrou = spip_nfslock($fichier)) && ($fl = @fopen($fichier, $mode))) { |
|
| 65 | + $GLOBALS['liste_verrous'][$fl] = [$fichier, $verrou]; |
|
| 66 | + |
|
| 67 | + return $fl; |
|
| 68 | + } else { |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + return @fopen($fichier, $mode); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
@@ -85,14 +85,14 @@ discard block |
||
| 85 | 85 | * true si succès, false sinon. |
| 86 | 86 | **/ |
| 87 | 87 | function spip_fclose_unlock($handle) { |
| 88 | - if (_SPIP_LOCK_MODE == 1) { |
|
| 89 | - @flock($handle, LOCK_UN); |
|
| 90 | - } elseif (_SPIP_LOCK_MODE == 2) { |
|
| 91 | - spip_nfsunlock(reset($GLOBALS['liste_verrous'][$handle]), end($GLOBALS['liste_verrous'][$handle])); |
|
| 92 | - unset($GLOBALS['liste_verrous'][$handle]); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - return @fclose($handle); |
|
| 88 | + if (_SPIP_LOCK_MODE == 1) { |
|
| 89 | + @flock($handle, LOCK_UN); |
|
| 90 | + } elseif (_SPIP_LOCK_MODE == 2) { |
|
| 91 | + spip_nfsunlock(reset($GLOBALS['liste_verrous'][$handle]), end($GLOBALS['liste_verrous'][$handle])); |
|
| 92 | + unset($GLOBALS['liste_verrous'][$handle]); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + return @fclose($handle); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | |
@@ -106,23 +106,23 @@ discard block |
||
| 106 | 106 | * Contenu du fichier |
| 107 | 107 | **/ |
| 108 | 108 | function spip_file_get_contents($fichier) { |
| 109 | - if (substr($fichier, -3) != '.gz') { |
|
| 110 | - if (function_exists('file_get_contents')) { |
|
| 111 | - // quand on est sous windows on ne sait pas si file_get_contents marche |
|
| 112 | - // on essaye : si ca retourne du contenu alors c'est bon |
|
| 113 | - // sinon on fait un file() pour avoir le coeur net |
|
| 114 | - $contenu = @file_get_contents($fichier); |
|
| 115 | - if (!$contenu and _OS_SERVEUR == 'windows') { |
|
| 116 | - $contenu = @file($fichier); |
|
| 117 | - } |
|
| 118 | - } else { |
|
| 119 | - $contenu = @file($fichier); |
|
| 120 | - } |
|
| 121 | - } else { |
|
| 122 | - $contenu = @gzfile($fichier); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - return is_array($contenu) ? join('', $contenu) : (string)$contenu; |
|
| 109 | + if (substr($fichier, -3) != '.gz') { |
|
| 110 | + if (function_exists('file_get_contents')) { |
|
| 111 | + // quand on est sous windows on ne sait pas si file_get_contents marche |
|
| 112 | + // on essaye : si ca retourne du contenu alors c'est bon |
|
| 113 | + // sinon on fait un file() pour avoir le coeur net |
|
| 114 | + $contenu = @file_get_contents($fichier); |
|
| 115 | + if (!$contenu and _OS_SERVEUR == 'windows') { |
|
| 116 | + $contenu = @file($fichier); |
|
| 117 | + } |
|
| 118 | + } else { |
|
| 119 | + $contenu = @file($fichier); |
|
| 120 | + } |
|
| 121 | + } else { |
|
| 122 | + $contenu = @gzfile($fichier); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + return is_array($contenu) ? join('', $contenu) : (string)$contenu; |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | |
@@ -147,48 +147,48 @@ discard block |
||
| 147 | 147 | * true si l'opération a réussie, false sinon. |
| 148 | 148 | **/ |
| 149 | 149 | function lire_fichier($fichier, &$contenu, $options = []) { |
| 150 | - $contenu = ''; |
|
| 151 | - // inutile car si le fichier n'existe pas, le lock va renvoyer false juste apres |
|
| 152 | - // economisons donc les acces disque, sauf chez free qui rale pour un rien |
|
| 153 | - if (_TEST_FILE_EXISTS and !@file_exists($fichier)) { |
|
| 154 | - return false; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - #spip_timer('lire_fichier'); |
|
| 158 | - |
|
| 159 | - // pas de @ sur spip_fopen_lock qui est silencieux de toute facon |
|
| 160 | - if ($fl = spip_fopen_lock($fichier, 'r', LOCK_SH)) { |
|
| 161 | - // lire le fichier avant tout |
|
| 162 | - $contenu = spip_file_get_contents($fichier); |
|
| 163 | - |
|
| 164 | - // le fichier a-t-il ete supprime par le locker ? |
|
| 165 | - // on ne verifie que si la tentative de lecture a echoue |
|
| 166 | - // pour discriminer un contenu vide d'un fichier absent |
|
| 167 | - // et eviter un acces disque |
|
| 168 | - if (!$contenu and !@file_exists($fichier)) { |
|
| 169 | - spip_fclose_unlock($fl); |
|
| 170 | - |
|
| 171 | - return false; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - // liberer le verrou |
|
| 175 | - spip_fclose_unlock($fl); |
|
| 176 | - |
|
| 177 | - // Verifications |
|
| 178 | - $ok = true; |
|
| 179 | - if (isset($options['phpcheck']) and $options['phpcheck'] == 'oui') { |
|
| 180 | - $ok &= (preg_match(",[?]>\n?$,", $contenu)); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - #spip_log("$fread $fichier ".spip_timer('lire_fichier')); |
|
| 184 | - if (!$ok) { |
|
| 185 | - spip_log("echec lecture $fichier"); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return $ok; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return false; |
|
| 150 | + $contenu = ''; |
|
| 151 | + // inutile car si le fichier n'existe pas, le lock va renvoyer false juste apres |
|
| 152 | + // economisons donc les acces disque, sauf chez free qui rale pour un rien |
|
| 153 | + if (_TEST_FILE_EXISTS and !@file_exists($fichier)) { |
|
| 154 | + return false; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + #spip_timer('lire_fichier'); |
|
| 158 | + |
|
| 159 | + // pas de @ sur spip_fopen_lock qui est silencieux de toute facon |
|
| 160 | + if ($fl = spip_fopen_lock($fichier, 'r', LOCK_SH)) { |
|
| 161 | + // lire le fichier avant tout |
|
| 162 | + $contenu = spip_file_get_contents($fichier); |
|
| 163 | + |
|
| 164 | + // le fichier a-t-il ete supprime par le locker ? |
|
| 165 | + // on ne verifie que si la tentative de lecture a echoue |
|
| 166 | + // pour discriminer un contenu vide d'un fichier absent |
|
| 167 | + // et eviter un acces disque |
|
| 168 | + if (!$contenu and !@file_exists($fichier)) { |
|
| 169 | + spip_fclose_unlock($fl); |
|
| 170 | + |
|
| 171 | + return false; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + // liberer le verrou |
|
| 175 | + spip_fclose_unlock($fl); |
|
| 176 | + |
|
| 177 | + // Verifications |
|
| 178 | + $ok = true; |
|
| 179 | + if (isset($options['phpcheck']) and $options['phpcheck'] == 'oui') { |
|
| 180 | + $ok &= (preg_match(",[?]>\n?$,", $contenu)); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + #spip_log("$fread $fichier ".spip_timer('lire_fichier')); |
|
| 184 | + if (!$ok) { |
|
| 185 | + spip_log("echec lecture $fichier"); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return $ok; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return false; |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | |
@@ -216,85 +216,85 @@ discard block |
||
| 216 | 216 | **/ |
| 217 | 217 | function ecrire_fichier($fichier, $contenu, $ignorer_echec = false, $truncate = true) { |
| 218 | 218 | |
| 219 | - #spip_timer('ecrire_fichier'); |
|
| 220 | - |
|
| 221 | - // verrouiller le fichier destination |
|
| 222 | - if ($fp = spip_fopen_lock($fichier, 'a', LOCK_EX)) { |
|
| 223 | - // ecrire les donnees, compressees le cas echeant |
|
| 224 | - // (on ouvre un nouveau pointeur sur le fichier, ce qui a l'avantage |
|
| 225 | - // de le recreer si le locker qui nous precede l'avait supprime...) |
|
| 226 | - if (substr($fichier, -3) == '.gz') { |
|
| 227 | - $contenu = gzencode($contenu); |
|
| 228 | - } |
|
| 229 | - // si c'est une ecriture avec troncation , on fait plutot une ecriture complete a cote suivie unlink+rename |
|
| 230 | - // pour etre sur d'avoir une operation atomique |
|
| 231 | - // y compris en NFS : http://www.ietf.org/rfc/rfc1094.txt |
|
| 232 | - // sauf sous wintruc ou ca ne marche pas |
|
| 233 | - $ok = false; |
|
| 234 | - if ($truncate and _OS_SERVEUR != 'windows') { |
|
| 235 | - if (!function_exists('creer_uniqid')) { |
|
| 236 | - include_spip('inc/acces'); |
|
| 237 | - } |
|
| 238 | - $id = creer_uniqid(); |
|
| 239 | - // on ouvre un pointeur sur un fichier temporaire en ecriture +raz |
|
| 240 | - if ($fp2 = spip_fopen_lock("$fichier.$id", 'w', LOCK_EX)) { |
|
| 241 | - $s = @fputs($fp2, $contenu, $a = strlen($contenu)); |
|
| 242 | - $ok = ($s == $a); |
|
| 243 | - spip_fclose_unlock($fp2); |
|
| 244 | - spip_fclose_unlock($fp); |
|
| 245 | - // unlink direct et pas spip_unlink car on avait deja le verrou |
|
| 246 | - // a priori pas besoin car rename ecrase la cible |
|
| 247 | - // @unlink($fichier); |
|
| 248 | - // le rename aussitot, atomique quand on est pas sous windows |
|
| 249 | - // au pire on arrive en second en cas de concourance, et le rename echoue |
|
| 250 | - // --> on a la version de l'autre process qui doit etre identique |
|
| 251 | - @rename("$fichier.$id", $fichier); |
|
| 252 | - // precaution en cas d'echec du rename |
|
| 253 | - if (!_TEST_FILE_EXISTS or @file_exists("$fichier.$id")) { |
|
| 254 | - @unlink("$fichier.$id"); |
|
| 255 | - } |
|
| 256 | - if ($ok) { |
|
| 257 | - $ok = file_exists($fichier); |
|
| 258 | - } |
|
| 259 | - } else // echec mais penser a fermer .. |
|
| 260 | - { |
|
| 261 | - spip_fclose_unlock($fp); |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - // sinon ou si methode precedente a echoueee |
|
| 265 | - // on se rabat sur la methode ancienne |
|
| 266 | - if (!$ok) { |
|
| 267 | - // ici on est en ajout ou sous windows, cas desespere |
|
| 268 | - if ($truncate) { |
|
| 269 | - @ftruncate($fp, 0); |
|
| 270 | - } |
|
| 271 | - $s = @fputs($fp, $contenu, $a = strlen($contenu)); |
|
| 272 | - |
|
| 273 | - $ok = ($s == $a); |
|
| 274 | - spip_fclose_unlock($fp); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - // liberer le verrou et fermer le fichier |
|
| 278 | - @chmod($fichier, _SPIP_CHMOD & 0666); |
|
| 279 | - if ($ok) { |
|
| 280 | - if (strpos($fichier, '.php') !== false) { |
|
| 281 | - spip_clear_opcode_cache(realpath($fichier)); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - return $ok; |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - if (!$ignorer_echec) { |
|
| 289 | - include_spip('inc/autoriser'); |
|
| 290 | - if (autoriser('chargerftp')) { |
|
| 291 | - raler_fichier($fichier); |
|
| 292 | - } |
|
| 293 | - spip_unlink($fichier); |
|
| 294 | - } |
|
| 295 | - spip_log("Ecriture fichier $fichier impossible", _LOG_INFO_IMPORTANTE); |
|
| 296 | - |
|
| 297 | - return false; |
|
| 219 | + #spip_timer('ecrire_fichier'); |
|
| 220 | + |
|
| 221 | + // verrouiller le fichier destination |
|
| 222 | + if ($fp = spip_fopen_lock($fichier, 'a', LOCK_EX)) { |
|
| 223 | + // ecrire les donnees, compressees le cas echeant |
|
| 224 | + // (on ouvre un nouveau pointeur sur le fichier, ce qui a l'avantage |
|
| 225 | + // de le recreer si le locker qui nous precede l'avait supprime...) |
|
| 226 | + if (substr($fichier, -3) == '.gz') { |
|
| 227 | + $contenu = gzencode($contenu); |
|
| 228 | + } |
|
| 229 | + // si c'est une ecriture avec troncation , on fait plutot une ecriture complete a cote suivie unlink+rename |
|
| 230 | + // pour etre sur d'avoir une operation atomique |
|
| 231 | + // y compris en NFS : http://www.ietf.org/rfc/rfc1094.txt |
|
| 232 | + // sauf sous wintruc ou ca ne marche pas |
|
| 233 | + $ok = false; |
|
| 234 | + if ($truncate and _OS_SERVEUR != 'windows') { |
|
| 235 | + if (!function_exists('creer_uniqid')) { |
|
| 236 | + include_spip('inc/acces'); |
|
| 237 | + } |
|
| 238 | + $id = creer_uniqid(); |
|
| 239 | + // on ouvre un pointeur sur un fichier temporaire en ecriture +raz |
|
| 240 | + if ($fp2 = spip_fopen_lock("$fichier.$id", 'w', LOCK_EX)) { |
|
| 241 | + $s = @fputs($fp2, $contenu, $a = strlen($contenu)); |
|
| 242 | + $ok = ($s == $a); |
|
| 243 | + spip_fclose_unlock($fp2); |
|
| 244 | + spip_fclose_unlock($fp); |
|
| 245 | + // unlink direct et pas spip_unlink car on avait deja le verrou |
|
| 246 | + // a priori pas besoin car rename ecrase la cible |
|
| 247 | + // @unlink($fichier); |
|
| 248 | + // le rename aussitot, atomique quand on est pas sous windows |
|
| 249 | + // au pire on arrive en second en cas de concourance, et le rename echoue |
|
| 250 | + // --> on a la version de l'autre process qui doit etre identique |
|
| 251 | + @rename("$fichier.$id", $fichier); |
|
| 252 | + // precaution en cas d'echec du rename |
|
| 253 | + if (!_TEST_FILE_EXISTS or @file_exists("$fichier.$id")) { |
|
| 254 | + @unlink("$fichier.$id"); |
|
| 255 | + } |
|
| 256 | + if ($ok) { |
|
| 257 | + $ok = file_exists($fichier); |
|
| 258 | + } |
|
| 259 | + } else // echec mais penser a fermer .. |
|
| 260 | + { |
|
| 261 | + spip_fclose_unlock($fp); |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + // sinon ou si methode precedente a echoueee |
|
| 265 | + // on se rabat sur la methode ancienne |
|
| 266 | + if (!$ok) { |
|
| 267 | + // ici on est en ajout ou sous windows, cas desespere |
|
| 268 | + if ($truncate) { |
|
| 269 | + @ftruncate($fp, 0); |
|
| 270 | + } |
|
| 271 | + $s = @fputs($fp, $contenu, $a = strlen($contenu)); |
|
| 272 | + |
|
| 273 | + $ok = ($s == $a); |
|
| 274 | + spip_fclose_unlock($fp); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // liberer le verrou et fermer le fichier |
|
| 278 | + @chmod($fichier, _SPIP_CHMOD & 0666); |
|
| 279 | + if ($ok) { |
|
| 280 | + if (strpos($fichier, '.php') !== false) { |
|
| 281 | + spip_clear_opcode_cache(realpath($fichier)); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + return $ok; |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + if (!$ignorer_echec) { |
|
| 289 | + include_spip('inc/autoriser'); |
|
| 290 | + if (autoriser('chargerftp')) { |
|
| 291 | + raler_fichier($fichier); |
|
| 292 | + } |
|
| 293 | + spip_unlink($fichier); |
|
| 294 | + } |
|
| 295 | + spip_log("Ecriture fichier $fichier impossible", _LOG_INFO_IMPORTANTE); |
|
| 296 | + |
|
| 297 | + return false; |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | /** |
@@ -314,12 +314,12 @@ discard block |
||
| 314 | 314 | * Écriture avec troncation ? |
| 315 | 315 | */ |
| 316 | 316 | function ecrire_fichier_securise($fichier, $contenu, $ecrire_quand_meme = false, $truncate = true) { |
| 317 | - if (substr($fichier, -4) !== '.php') { |
|
| 318 | - spip_log('Erreur de programmation: ' . $fichier . ' doit finir par .php'); |
|
| 319 | - } |
|
| 320 | - $contenu = '<' . "?php die ('Acces interdit'); ?" . ">\n" . $contenu; |
|
| 317 | + if (substr($fichier, -4) !== '.php') { |
|
| 318 | + spip_log('Erreur de programmation: ' . $fichier . ' doit finir par .php'); |
|
| 319 | + } |
|
| 320 | + $contenu = '<' . "?php die ('Acces interdit'); ?" . ">\n" . $contenu; |
|
| 321 | 321 | |
| 322 | - return ecrire_fichier($fichier, $contenu, $ecrire_quand_meme, $truncate); |
|
| 322 | + return ecrire_fichier($fichier, $contenu, $ecrire_quand_meme, $truncate); |
|
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | |
@@ -330,25 +330,25 @@ discard block |
||
| 330 | 330 | * @return bool |
| 331 | 331 | */ |
| 332 | 332 | function ecrire_fichier_calcule_si_modifie($fichier, $contenu, $force = false, $use_copy = false) { |
| 333 | - $fichier_tmp = $fichier . '.last'; |
|
| 334 | - if (!ecrire_fichier($fichier_tmp, $contenu, true)) { |
|
| 335 | - return false; |
|
| 336 | - } |
|
| 337 | - if ( |
|
| 338 | - $force |
|
| 339 | - or !file_exists($fichier) |
|
| 340 | - or md5_file($fichier) != md5_file($fichier_tmp) |
|
| 341 | - ) { |
|
| 342 | - if ($use_copy) { |
|
| 343 | - @copy($fichier_tmp, $fichier); |
|
| 344 | - } |
|
| 345 | - else { |
|
| 346 | - @rename($fichier_tmp, $fichier); |
|
| 347 | - } |
|
| 348 | - // eviter que PHP ne reserve le vieux timestamp |
|
| 349 | - clearstatcache(true, $fichier); |
|
| 350 | - } |
|
| 351 | - return true; |
|
| 333 | + $fichier_tmp = $fichier . '.last'; |
|
| 334 | + if (!ecrire_fichier($fichier_tmp, $contenu, true)) { |
|
| 335 | + return false; |
|
| 336 | + } |
|
| 337 | + if ( |
|
| 338 | + $force |
|
| 339 | + or !file_exists($fichier) |
|
| 340 | + or md5_file($fichier) != md5_file($fichier_tmp) |
|
| 341 | + ) { |
|
| 342 | + if ($use_copy) { |
|
| 343 | + @copy($fichier_tmp, $fichier); |
|
| 344 | + } |
|
| 345 | + else { |
|
| 346 | + @rename($fichier_tmp, $fichier); |
|
| 347 | + } |
|
| 348 | + // eviter que PHP ne reserve le vieux timestamp |
|
| 349 | + clearstatcache(true, $fichier); |
|
| 350 | + } |
|
| 351 | + return true; |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | |
@@ -369,11 +369,11 @@ discard block |
||
| 369 | 369 | * true si l'opération a réussie, false sinon. |
| 370 | 370 | */ |
| 371 | 371 | function lire_fichier_securise($fichier, &$contenu, $options = []) { |
| 372 | - if ($res = lire_fichier($fichier, $contenu, $options)) { |
|
| 373 | - $contenu = substr($contenu, strlen('<' . "?php die ('Acces interdit'); ?" . ">\n")); |
|
| 374 | - } |
|
| 372 | + if ($res = lire_fichier($fichier, $contenu, $options)) { |
|
| 373 | + $contenu = substr($contenu, strlen('<' . "?php die ('Acces interdit'); ?" . ">\n")); |
|
| 374 | + } |
|
| 375 | 375 | |
| 376 | - return $res; |
|
| 376 | + return $res; |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | /** |
@@ -388,25 +388,25 @@ discard block |
||
| 388 | 388 | * Chemin du fichier |
| 389 | 389 | **/ |
| 390 | 390 | function raler_fichier($fichier) { |
| 391 | - if (!defined('_SPIP_ECRIRE_SCRIPT')) { |
|
| 392 | - spip_initialisation_suite(); |
|
| 393 | - } |
|
| 394 | - include_spip('inc/minipres'); |
|
| 395 | - $dir = dirname($fichier); |
|
| 396 | - http_response_code(401); |
|
| 397 | - echo minipres(_T('texte_inc_meta_2'), "<h4 style='color: red'>" |
|
| 398 | - . _T('texte_inc_meta_1', ['fichier' => $fichier]) |
|
| 399 | - . " <a href='" |
|
| 400 | - . generer_url_ecrire('install', "etape=chmod&test_dir=$dir") |
|
| 401 | - . "'>" |
|
| 402 | - . _T('texte_inc_meta_2') |
|
| 403 | - . '</a> ' |
|
| 404 | - . _T( |
|
| 405 | - 'texte_inc_meta_3', |
|
| 406 | - ['repertoire' => joli_repertoire($dir)] |
|
| 407 | - ) |
|
| 408 | - . "</h4>\n"); |
|
| 409 | - exit; |
|
| 391 | + if (!defined('_SPIP_ECRIRE_SCRIPT')) { |
|
| 392 | + spip_initialisation_suite(); |
|
| 393 | + } |
|
| 394 | + include_spip('inc/minipres'); |
|
| 395 | + $dir = dirname($fichier); |
|
| 396 | + http_response_code(401); |
|
| 397 | + echo minipres(_T('texte_inc_meta_2'), "<h4 style='color: red'>" |
|
| 398 | + . _T('texte_inc_meta_1', ['fichier' => $fichier]) |
|
| 399 | + . " <a href='" |
|
| 400 | + . generer_url_ecrire('install', "etape=chmod&test_dir=$dir") |
|
| 401 | + . "'>" |
|
| 402 | + . _T('texte_inc_meta_2') |
|
| 403 | + . '</a> ' |
|
| 404 | + . _T( |
|
| 405 | + 'texte_inc_meta_3', |
|
| 406 | + ['repertoire' => joli_repertoire($dir)] |
|
| 407 | + ) |
|
| 408 | + . "</h4>\n"); |
|
| 409 | + exit; |
|
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | |
@@ -421,14 +421,14 @@ discard block |
||
| 421 | 421 | * - true si récent, false sinon |
| 422 | 422 | */ |
| 423 | 423 | function jeune_fichier($fichier, $n) { |
| 424 | - if (!file_exists($fichier)) { |
|
| 425 | - return false; |
|
| 426 | - } |
|
| 427 | - if (!$c = @filemtime($fichier)) { |
|
| 428 | - return false; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - return (time() - $n <= $c); |
|
| 424 | + if (!file_exists($fichier)) { |
|
| 425 | + return false; |
|
| 426 | + } |
|
| 427 | + if (!$c = @filemtime($fichier)) { |
|
| 428 | + return false; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + return (time() - $n <= $c); |
|
| 432 | 432 | } |
| 433 | 433 | |
| 434 | 434 | /** |
@@ -443,22 +443,22 @@ discard block |
||
| 443 | 443 | * - false si on n'arrive pas poser le verrou ou si la suppression échoue |
| 444 | 444 | */ |
| 445 | 445 | function supprimer_fichier($fichier, $lock = true) { |
| 446 | - if (!@file_exists($fichier)) { |
|
| 447 | - return true; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - if ($lock) { |
|
| 451 | - // verrouiller le fichier destination |
|
| 452 | - if (!$fp = spip_fopen_lock($fichier, 'a', LOCK_EX)) { |
|
| 453 | - return false; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - // liberer le verrou |
|
| 457 | - spip_fclose_unlock($fp); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - // supprimer |
|
| 461 | - return @unlink($fichier); |
|
| 446 | + if (!@file_exists($fichier)) { |
|
| 447 | + return true; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + if ($lock) { |
|
| 451 | + // verrouiller le fichier destination |
|
| 452 | + if (!$fp = spip_fopen_lock($fichier, 'a', LOCK_EX)) { |
|
| 453 | + return false; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + // liberer le verrou |
|
| 457 | + spip_fclose_unlock($fp); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + // supprimer |
|
| 461 | + return @unlink($fichier); |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | /** |
@@ -468,12 +468,12 @@ discard block |
||
| 468 | 468 | * Chemin du fichier |
| 469 | 469 | */ |
| 470 | 470 | function spip_unlink($f) { |
| 471 | - if (!is_dir($f)) { |
|
| 472 | - supprimer_fichier($f, false); |
|
| 473 | - } else { |
|
| 474 | - @unlink("$f/.ok"); |
|
| 475 | - @rmdir($f); |
|
| 476 | - } |
|
| 471 | + if (!is_dir($f)) { |
|
| 472 | + supprimer_fichier($f, false); |
|
| 473 | + } else { |
|
| 474 | + @unlink("$f/.ok"); |
|
| 475 | + @rmdir($f); |
|
| 476 | + } |
|
| 477 | 477 | } |
| 478 | 478 | |
| 479 | 479 | /** |
@@ -487,26 +487,26 @@ discard block |
||
| 487 | 487 | * The absolute path of the PHP file to invalidate. |
| 488 | 488 | */ |
| 489 | 489 | function spip_clear_opcode_cache($filepath) { |
| 490 | - clearstatcache(true, $filepath); |
|
| 491 | - |
|
| 492 | - // Zend OPcache |
|
| 493 | - if (function_exists('opcache_invalidate')) { |
|
| 494 | - $invalidate = @opcache_invalidate($filepath, true); |
|
| 495 | - // si l'invalidation a echoue lever un flag |
|
| 496 | - if (!$invalidate and !defined('_spip_attend_invalidation_opcode_cache')) { |
|
| 497 | - define('_spip_attend_invalidation_opcode_cache', true); |
|
| 498 | - } |
|
| 499 | - } elseif (!defined('_spip_attend_invalidation_opcode_cache')) { |
|
| 500 | - // n'agira que si opcache est effectivement actif (il semble qu'on a pas toujours la fonction opcache_invalidate) |
|
| 501 | - define('_spip_attend_invalidation_opcode_cache', true); |
|
| 502 | - } |
|
| 503 | - // APC. |
|
| 504 | - if (function_exists('apc_delete_file')) { |
|
| 505 | - // apc_delete_file() throws a PHP warning in case the specified file was |
|
| 506 | - // not compiled yet. |
|
| 507 | - // @see http://php.net/apc-delete-file |
|
| 508 | - @apc_delete_file($filepath); |
|
| 509 | - } |
|
| 490 | + clearstatcache(true, $filepath); |
|
| 491 | + |
|
| 492 | + // Zend OPcache |
|
| 493 | + if (function_exists('opcache_invalidate')) { |
|
| 494 | + $invalidate = @opcache_invalidate($filepath, true); |
|
| 495 | + // si l'invalidation a echoue lever un flag |
|
| 496 | + if (!$invalidate and !defined('_spip_attend_invalidation_opcode_cache')) { |
|
| 497 | + define('_spip_attend_invalidation_opcode_cache', true); |
|
| 498 | + } |
|
| 499 | + } elseif (!defined('_spip_attend_invalidation_opcode_cache')) { |
|
| 500 | + // n'agira que si opcache est effectivement actif (il semble qu'on a pas toujours la fonction opcache_invalidate) |
|
| 501 | + define('_spip_attend_invalidation_opcode_cache', true); |
|
| 502 | + } |
|
| 503 | + // APC. |
|
| 504 | + if (function_exists('apc_delete_file')) { |
|
| 505 | + // apc_delete_file() throws a PHP warning in case the specified file was |
|
| 506 | + // not compiled yet. |
|
| 507 | + // @see http://php.net/apc-delete-file |
|
| 508 | + @apc_delete_file($filepath); |
|
| 509 | + } |
|
| 510 | 510 | } |
| 511 | 511 | |
| 512 | 512 | /** |
@@ -529,25 +529,25 @@ discard block |
||
| 529 | 529 | * |
| 530 | 530 | */ |
| 531 | 531 | function spip_attend_invalidation_opcode_cache($timestamp = null) { |
| 532 | - if ( |
|
| 533 | - function_exists('opcache_get_configuration') |
|
| 534 | - and @ini_get('opcache.enable') |
|
| 535 | - and @ini_get('opcache.validate_timestamps') |
|
| 536 | - and ($duree = intval(@ini_get('opcache.revalidate_freq')) or $duree = 2) |
|
| 537 | - and defined('_spip_attend_invalidation_opcode_cache') // des invalidations ont echouees |
|
| 538 | - ) { |
|
| 539 | - $wait = $duree + 1; |
|
| 540 | - if ($timestamp) { |
|
| 541 | - $wait -= (time() - $timestamp); |
|
| 542 | - if ($wait < 0) { |
|
| 543 | - $wait = 0; |
|
| 544 | - } |
|
| 545 | - } |
|
| 546 | - spip_log('Probleme de configuration opcache.revalidate_freq ' . $duree . 's : on attend ' . $wait . 's', _LOG_INFO_IMPORTANTE); |
|
| 547 | - if ($wait) { |
|
| 548 | - sleep($duree + 1); |
|
| 549 | - } |
|
| 550 | - } |
|
| 532 | + if ( |
|
| 533 | + function_exists('opcache_get_configuration') |
|
| 534 | + and @ini_get('opcache.enable') |
|
| 535 | + and @ini_get('opcache.validate_timestamps') |
|
| 536 | + and ($duree = intval(@ini_get('opcache.revalidate_freq')) or $duree = 2) |
|
| 537 | + and defined('_spip_attend_invalidation_opcode_cache') // des invalidations ont echouees |
|
| 538 | + ) { |
|
| 539 | + $wait = $duree + 1; |
|
| 540 | + if ($timestamp) { |
|
| 541 | + $wait -= (time() - $timestamp); |
|
| 542 | + if ($wait < 0) { |
|
| 543 | + $wait = 0; |
|
| 544 | + } |
|
| 545 | + } |
|
| 546 | + spip_log('Probleme de configuration opcache.revalidate_freq ' . $duree . 's : on attend ' . $wait . 's', _LOG_INFO_IMPORTANTE); |
|
| 547 | + if ($wait) { |
|
| 548 | + sleep($duree + 1); |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | 551 | } |
| 552 | 552 | |
| 553 | 553 | |
@@ -560,26 +560,26 @@ discard block |
||
| 560 | 560 | * @return bool Suppression reussie. |
| 561 | 561 | */ |
| 562 | 562 | function supprimer_repertoire($dir) { |
| 563 | - if (!file_exists($dir)) { |
|
| 564 | - return true; |
|
| 565 | - } |
|
| 566 | - if (!is_dir($dir) || is_link($dir)) { |
|
| 567 | - return @unlink($dir); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - foreach (scandir($dir) as $item) { |
|
| 571 | - if ($item == '.' || $item == '..') { |
|
| 572 | - continue; |
|
| 573 | - } |
|
| 574 | - if (!supprimer_repertoire($dir . '/' . $item)) { |
|
| 575 | - @chmod($dir . '/' . $item, 0777); |
|
| 576 | - if (!supprimer_repertoire($dir . '/' . $item)) { |
|
| 577 | - return false; |
|
| 578 | - } |
|
| 579 | - }; |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - return @rmdir($dir); |
|
| 563 | + if (!file_exists($dir)) { |
|
| 564 | + return true; |
|
| 565 | + } |
|
| 566 | + if (!is_dir($dir) || is_link($dir)) { |
|
| 567 | + return @unlink($dir); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + foreach (scandir($dir) as $item) { |
|
| 571 | + if ($item == '.' || $item == '..') { |
|
| 572 | + continue; |
|
| 573 | + } |
|
| 574 | + if (!supprimer_repertoire($dir . '/' . $item)) { |
|
| 575 | + @chmod($dir . '/' . $item, 0777); |
|
| 576 | + if (!supprimer_repertoire($dir . '/' . $item)) { |
|
| 577 | + return false; |
|
| 578 | + } |
|
| 579 | + }; |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + return @rmdir($dir); |
|
| 583 | 583 | } |
| 584 | 584 | |
| 585 | 585 | |
@@ -608,57 +608,57 @@ discard block |
||
| 608 | 608 | * Chemin du répertoire créé. |
| 609 | 609 | **/ |
| 610 | 610 | function sous_repertoire($base, $subdir = '', $nobase = false, $tantpis = false) { |
| 611 | - static $dirs = []; |
|
| 612 | - |
|
| 613 | - $base = str_replace('//', '/', $base); |
|
| 614 | - |
|
| 615 | - # suppr le dernier caractere si c'est un / |
|
| 616 | - $base = rtrim($base, '/'); |
|
| 617 | - |
|
| 618 | - if (!strlen($subdir)) { |
|
| 619 | - $n = strrpos($base, '/'); |
|
| 620 | - if ($n === false) { |
|
| 621 | - return $nobase ? '' : ($base . '/'); |
|
| 622 | - } |
|
| 623 | - $subdir = substr($base, $n + 1); |
|
| 624 | - $base = substr($base, 0, $n + 1); |
|
| 625 | - } else { |
|
| 626 | - $base .= '/'; |
|
| 627 | - $subdir = str_replace('/', '', $subdir); |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - $baseaff = $nobase ? '' : $base; |
|
| 631 | - if (isset($dirs[$base . $subdir])) { |
|
| 632 | - return $baseaff . $dirs[$base . $subdir]; |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - $path = $base . $subdir; # $path = 'IMG/distant/pdf' ou 'IMG/distant_pdf' |
|
| 636 | - |
|
| 637 | - if (file_exists("$path/.ok")) { |
|
| 638 | - return $baseaff . ($dirs[$base . $subdir] = "$subdir/"); |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - @mkdir($path, _SPIP_CHMOD); |
|
| 642 | - @chmod($path, _SPIP_CHMOD); |
|
| 643 | - |
|
| 644 | - if (is_dir($path) && is_writable($path)) { |
|
| 645 | - @touch("$path/.ok"); |
|
| 646 | - spip_log("creation $base$subdir/"); |
|
| 647 | - |
|
| 648 | - return $baseaff . ($dirs[$base . $subdir] = "$subdir/"); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - // en cas d'echec c'est peut etre tout simplement que le disque est plein : |
|
| 652 | - // l'inode du fichier dir_test existe, mais impossible d'y mettre du contenu |
|
| 653 | - spip_log("echec creation $base${subdir}"); |
|
| 654 | - if ($tantpis) { |
|
| 655 | - return ''; |
|
| 656 | - } |
|
| 657 | - if (!_DIR_RESTREINT) { |
|
| 658 | - $base = preg_replace(',^' . _DIR_RACINE . ',', '', $base); |
|
| 659 | - } |
|
| 660 | - $base .= $subdir; |
|
| 661 | - raler_fichier($base . '/.ok'); |
|
| 611 | + static $dirs = []; |
|
| 612 | + |
|
| 613 | + $base = str_replace('//', '/', $base); |
|
| 614 | + |
|
| 615 | + # suppr le dernier caractere si c'est un / |
|
| 616 | + $base = rtrim($base, '/'); |
|
| 617 | + |
|
| 618 | + if (!strlen($subdir)) { |
|
| 619 | + $n = strrpos($base, '/'); |
|
| 620 | + if ($n === false) { |
|
| 621 | + return $nobase ? '' : ($base . '/'); |
|
| 622 | + } |
|
| 623 | + $subdir = substr($base, $n + 1); |
|
| 624 | + $base = substr($base, 0, $n + 1); |
|
| 625 | + } else { |
|
| 626 | + $base .= '/'; |
|
| 627 | + $subdir = str_replace('/', '', $subdir); |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + $baseaff = $nobase ? '' : $base; |
|
| 631 | + if (isset($dirs[$base . $subdir])) { |
|
| 632 | + return $baseaff . $dirs[$base . $subdir]; |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + $path = $base . $subdir; # $path = 'IMG/distant/pdf' ou 'IMG/distant_pdf' |
|
| 636 | + |
|
| 637 | + if (file_exists("$path/.ok")) { |
|
| 638 | + return $baseaff . ($dirs[$base . $subdir] = "$subdir/"); |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + @mkdir($path, _SPIP_CHMOD); |
|
| 642 | + @chmod($path, _SPIP_CHMOD); |
|
| 643 | + |
|
| 644 | + if (is_dir($path) && is_writable($path)) { |
|
| 645 | + @touch("$path/.ok"); |
|
| 646 | + spip_log("creation $base$subdir/"); |
|
| 647 | + |
|
| 648 | + return $baseaff . ($dirs[$base . $subdir] = "$subdir/"); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + // en cas d'echec c'est peut etre tout simplement que le disque est plein : |
|
| 652 | + // l'inode du fichier dir_test existe, mais impossible d'y mettre du contenu |
|
| 653 | + spip_log("echec creation $base${subdir}"); |
|
| 654 | + if ($tantpis) { |
|
| 655 | + return ''; |
|
| 656 | + } |
|
| 657 | + if (!_DIR_RESTREINT) { |
|
| 658 | + $base = preg_replace(',^' . _DIR_RACINE . ',', '', $base); |
|
| 659 | + } |
|
| 660 | + $base .= $subdir; |
|
| 661 | + raler_fichier($base . '/.ok'); |
|
| 662 | 662 | } |
| 663 | 663 | |
| 664 | 664 | |
@@ -691,56 +691,56 @@ discard block |
||
| 691 | 691 | * Chemins des fichiers trouvés. |
| 692 | 692 | **/ |
| 693 | 693 | function preg_files($dir, $pattern = -1 /* AUTO */, $maxfiles = 10000, $recurs = []) { |
| 694 | - $nbfiles = 0; |
|
| 695 | - if ($pattern == -1) { |
|
| 696 | - $pattern = "^$dir"; |
|
| 697 | - } |
|
| 698 | - $fichiers = []; |
|
| 699 | - // revenir au repertoire racine si on a recu dossier/truc |
|
| 700 | - // pour regarder dossier/truc/ ne pas oublier le / final |
|
| 701 | - $dir = preg_replace(',/[^/]*$,', '', $dir); |
|
| 702 | - if ($dir == '') { |
|
| 703 | - $dir = '.'; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - if (@is_dir($dir) and is_readable($dir) and $d = opendir($dir)) { |
|
| 707 | - while (($f = readdir($d)) !== false && ($nbfiles < $maxfiles)) { |
|
| 708 | - if ( |
|
| 709 | - $f[0] != '.' # ignorer . .. .svn etc |
|
| 710 | - and $f != 'CVS' |
|
| 711 | - and $f != 'remove.txt' |
|
| 712 | - and is_readable($f = "$dir/$f") |
|
| 713 | - ) { |
|
| 714 | - if (is_file($f)) { |
|
| 715 | - if (preg_match(";$pattern;iS", $f)) { |
|
| 716 | - $fichiers[] = $f; |
|
| 717 | - $nbfiles++; |
|
| 718 | - } |
|
| 719 | - } else { |
|
| 720 | - if (is_dir($f) and is_array($recurs)) { |
|
| 721 | - $rp = @realpath($f); |
|
| 722 | - if (!is_string($rp) or !strlen($rp)) { |
|
| 723 | - $rp = $f; |
|
| 724 | - } # realpath n'est peut etre pas autorise |
|
| 725 | - if (!isset($recurs[$rp])) { |
|
| 726 | - $recurs[$rp] = true; |
|
| 727 | - $beginning = $fichiers; |
|
| 728 | - $end = preg_files( |
|
| 729 | - "$f/", |
|
| 730 | - $pattern, |
|
| 731 | - $maxfiles - $nbfiles, |
|
| 732 | - $recurs |
|
| 733 | - ); |
|
| 734 | - $fichiers = array_merge((array)$beginning, (array)$end); |
|
| 735 | - $nbfiles = count($fichiers); |
|
| 736 | - } |
|
| 737 | - } |
|
| 738 | - } |
|
| 739 | - } |
|
| 740 | - } |
|
| 741 | - closedir($d); |
|
| 742 | - } |
|
| 743 | - sort($fichiers); |
|
| 744 | - |
|
| 745 | - return $fichiers; |
|
| 694 | + $nbfiles = 0; |
|
| 695 | + if ($pattern == -1) { |
|
| 696 | + $pattern = "^$dir"; |
|
| 697 | + } |
|
| 698 | + $fichiers = []; |
|
| 699 | + // revenir au repertoire racine si on a recu dossier/truc |
|
| 700 | + // pour regarder dossier/truc/ ne pas oublier le / final |
|
| 701 | + $dir = preg_replace(',/[^/]*$,', '', $dir); |
|
| 702 | + if ($dir == '') { |
|
| 703 | + $dir = '.'; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + if (@is_dir($dir) and is_readable($dir) and $d = opendir($dir)) { |
|
| 707 | + while (($f = readdir($d)) !== false && ($nbfiles < $maxfiles)) { |
|
| 708 | + if ( |
|
| 709 | + $f[0] != '.' # ignorer . .. .svn etc |
|
| 710 | + and $f != 'CVS' |
|
| 711 | + and $f != 'remove.txt' |
|
| 712 | + and is_readable($f = "$dir/$f") |
|
| 713 | + ) { |
|
| 714 | + if (is_file($f)) { |
|
| 715 | + if (preg_match(";$pattern;iS", $f)) { |
|
| 716 | + $fichiers[] = $f; |
|
| 717 | + $nbfiles++; |
|
| 718 | + } |
|
| 719 | + } else { |
|
| 720 | + if (is_dir($f) and is_array($recurs)) { |
|
| 721 | + $rp = @realpath($f); |
|
| 722 | + if (!is_string($rp) or !strlen($rp)) { |
|
| 723 | + $rp = $f; |
|
| 724 | + } # realpath n'est peut etre pas autorise |
|
| 725 | + if (!isset($recurs[$rp])) { |
|
| 726 | + $recurs[$rp] = true; |
|
| 727 | + $beginning = $fichiers; |
|
| 728 | + $end = preg_files( |
|
| 729 | + "$f/", |
|
| 730 | + $pattern, |
|
| 731 | + $maxfiles - $nbfiles, |
|
| 732 | + $recurs |
|
| 733 | + ); |
|
| 734 | + $fichiers = array_merge((array)$beginning, (array)$end); |
|
| 735 | + $nbfiles = count($fichiers); |
|
| 736 | + } |
|
| 737 | + } |
|
| 738 | + } |
|
| 739 | + } |
|
| 740 | + } |
|
| 741 | + closedir($d); |
|
| 742 | + } |
|
| 743 | + sort($fichiers); |
|
| 744 | + |
|
| 745 | + return $fichiers; |
|
| 746 | 746 | } |