1 | <?php |
---|
2 | |
---|
3 | /***************************************************************************\ |
---|
4 | * SPIP, Systeme de publication pour l'internet * |
---|
5 | * * |
---|
6 | * Copyright (c) 2001-2011 * |
---|
7 | * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James * |
---|
8 | * * |
---|
9 | * Ce programme est un logiciel libre distribue sous licence GNU/GPL. * |
---|
10 | * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * |
---|
11 | \***************************************************************************/ |
---|
12 | |
---|
13 | if (!defined('_ECRIRE_INC_VERSION')) return; |
---|
14 | |
---|
15 | include_spip('inc/charsets'); |
---|
16 | include_spip('inc/filtres_mini'); |
---|
17 | |
---|
18 | /** |
---|
19 | * Charger un filtre depuis le php : |
---|
20 | * - on inclue tous les fichiers fonctions des plugins et du skel |
---|
21 | * - on appelle chercher_filtre |
---|
22 | */ |
---|
23 | function charger_filtre($fonc, $default=NULL) { |
---|
24 | include_spip('public/parametrer'); // inclure les fichiers fonctions |
---|
25 | return chercher_filtre($fonc, $default); |
---|
26 | } |
---|
27 | |
---|
28 | // http://adoc.spip.org/@chercher_filtre |
---|
29 | function chercher_filtre($fonc, $default=NULL) { |
---|
30 | // Cas MIME = type/subtype |
---|
31 | // sans confondre avec les appels de fonction de classe Foo::Bar |
---|
32 | // qui peuvent etre avec un namespace : space\Foo::Bar |
---|
33 | if (strpos($fonc, '/')){ |
---|
34 | $f = preg_replace(',\W,','_', $fonc); |
---|
35 | if ($f = chercher_filtre($f)) return $f; |
---|
36 | // cas du sous-type MIME sans filtre associe, passer au type: |
---|
37 | // si filtre_text_plain pas defini, passe a filtre_text |
---|
38 | $fonc = preg_replace(',\W.*$,','', $fonc); |
---|
39 | } |
---|
40 | if (!$fonc) return $default; |
---|
41 | foreach ( |
---|
42 | array('filtre_'.$fonc, 'filtre_'.$fonc.'_dist', $fonc) as $f){ |
---|
43 | if (is_string($g = $GLOBALS['spip_matrice'][$f])) |
---|
44 | find_in_path($g,'', true); |
---|
45 | if (function_exists($f) |
---|
46 | OR (preg_match("/^(\w*)::(\w*)$/", $f, $regs) |
---|
47 | AND is_callable(array($regs[1], $regs[2])) |
---|
48 | )) { |
---|
49 | return $f; |
---|
50 | } |
---|
51 | } |
---|
52 | return $default; |
---|
53 | } |
---|
54 | |
---|
55 | // https://code.spip.net/@appliquer_filtre |
---|
56 | function appliquer_filtre($arg, $filtre) { |
---|
57 | $f = chercher_filtre($filtre); |
---|
58 | if (!$f) |
---|
59 | return ''; // ou faut-il retourner $arg inchange == filtre_identite? |
---|
60 | |
---|
61 | $args = func_get_args(); |
---|
62 | array_shift($args); // enlever $arg |
---|
63 | array_shift($args); // enlever $filtre |
---|
64 | array_unshift($args, $arg); // remettre $arg |
---|
65 | return call_user_func_array($f,$args); |
---|
66 | } |
---|
67 | |
---|
68 | // https://code.spip.net/@spip_version |
---|
69 | function spip_version() { |
---|
70 | $version = $GLOBALS['spip_version_affichee']; |
---|
71 | if ($svn_revision = version_svn_courante(_DIR_RACINE)) |
---|
72 | $version .= ($svn_revision<0 ? ' SVN':'').' ['.abs($svn_revision).']'; |
---|
73 | return $version; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | // |
---|
78 | // Mention de la revision SVN courante de l'espace restreint standard |
---|
79 | // (numero non garanti pour l'espace public et en cas de mutualisation) |
---|
80 | // on est negatif si on est sur .svn, et positif si on utilise svn.revision |
---|
81 | // https://code.spip.net/@version_svn_courante |
---|
82 | function version_svn_courante($dir) { |
---|
83 | if (!$dir) $dir = '.'; |
---|
84 | |
---|
85 | // version installee par paquet ZIP |
---|
86 | if (lire_fichier($dir.'/svn.revision', $c) |
---|
87 | AND preg_match(',Revision: (\d+),', $c, $d)) |
---|
88 | return intval($d[1]); |
---|
89 | |
---|
90 | // version installee par SVN |
---|
91 | if (lire_fichier($dir . '/.svn/entries', $c) |
---|
92 | AND ( |
---|
93 | (preg_match_all( |
---|
94 | ',committed-rev="([0-9]+)",', $c, $r1, PREG_PATTERN_ORDER) |
---|
95 | AND $v = max($r1[1]) |
---|
96 | ) |
---|
97 | OR |
---|
98 | (preg_match(',^\d.*dir[\r\n]+(\d+),ms', $c, $r1) # svn >= 1.4 |
---|
99 | AND $v = $r1[1] |
---|
100 | ))) |
---|
101 | return -$v; |
---|
102 | |
---|
103 | // Bug ou paquet fait main |
---|
104 | return 0; |
---|
105 | } |
---|
106 | |
---|
107 | // La matrice est necessaire pour ne filtrer _que_ des fonctions definies dans filtres_images |
---|
108 | // et laisser passer les fonctions personnelles baptisees image_... |
---|
109 | $GLOBALS['spip_matrice']['image_graver'] = 'inc/filtres_images_mini.php'; |
---|
110 | $GLOBALS['spip_matrice']['image_select'] = 'inc/filtres_images_mini.php'; |
---|
111 | $GLOBALS['spip_matrice']['image_reduire'] = 'inc/filtres_images_mini.php'; |
---|
112 | $GLOBALS['spip_matrice']['image_reduire_par'] = 'inc/filtres_images_mini.php'; |
---|
113 | $GLOBALS['spip_matrice']['image_passe_partout'] = 'inc/filtres_images_mini.php'; |
---|
114 | |
---|
115 | $GLOBALS['spip_matrice']['couleur_html_to_hex'] = 'inc/filtres_images_mini.php'; |
---|
116 | $GLOBALS['spip_matrice']['couleur_foncer'] = 'inc/filtres_images_mini.php'; |
---|
117 | $GLOBALS['spip_matrice']['couleur_eclaircir'] = 'inc/filtres_images_mini.php'; |
---|
118 | |
---|
119 | // ou pour inclure un script au moment ou l'on cherche le filtre |
---|
120 | $GLOBALS['spip_matrice']['filtre_image_dist'] = 'inc/filtres_mime.php'; |
---|
121 | $GLOBALS['spip_matrice']['filtre_audio_dist'] = 'inc/filtres_mime.php'; |
---|
122 | $GLOBALS['spip_matrice']['filtre_video_dist'] = 'inc/filtres_mime.php'; |
---|
123 | $GLOBALS['spip_matrice']['filtre_application_dist'] = 'inc/filtres_mime.php'; |
---|
124 | $GLOBALS['spip_matrice']['filtre_message_dist'] = 'inc/filtres_mime.php'; |
---|
125 | $GLOBALS['spip_matrice']['filtre_multipart_dist'] = 'inc/filtres_mime.php'; |
---|
126 | $GLOBALS['spip_matrice']['filtre_text_dist'] = 'inc/filtres_mime.php'; |
---|
127 | $GLOBALS['spip_matrice']['filtre_text_csv_dist'] = 'inc/filtres_mime.php'; |
---|
128 | $GLOBALS['spip_matrice']['filtre_text_html_dist'] = 'inc/filtres_mime.php'; |
---|
129 | $GLOBALS['spip_matrice']['filtre_audio_x_pn_realaudio'] = 'inc/filtres_mime.php'; |
---|
130 | |
---|
131 | |
---|
132 | // charge les fonctions graphiques et applique celle demandee |
---|
133 | // https://code.spip.net/@filtrer |
---|
134 | function filtrer($filtre) { |
---|
135 | include_spip('public/parametrer'); // charger les fichiers fonctions |
---|
136 | if (is_string($f = $GLOBALS['spip_matrice'][$filtre])) |
---|
137 | find_in_path($f,'', true); |
---|
138 | $tous = func_get_args(); |
---|
139 | if (substr($filtre,0,6)=='image_' && $GLOBALS['spip_matrice'][$filtre]) |
---|
140 | return image_filtrer($tous); |
---|
141 | elseif($f = chercher_filtre($filtre)) { |
---|
142 | array_shift($tous); |
---|
143 | return call_user_func_array($f, $tous); |
---|
144 | } |
---|
145 | else { |
---|
146 | // le filtre n'existe pas, on provoque une erreur |
---|
147 | $msg = array('zbug_erreur_filtre', array('filtre'=>texte_script($filtre))); |
---|
148 | erreur_squelette($msg); |
---|
149 | return ''; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | // fonction generique d'entree des filtres images |
---|
154 | // accepte en entree un texte complet, un img-log (produit par #LOGO_XX), |
---|
155 | // un tag <img ...> complet, ou encore un nom de fichier *local* (passer |
---|
156 | // le filtre |copie_locale si on veut l'appliquer a un document) |
---|
157 | // applique le filtre demande a chacune des occurrences |
---|
158 | |
---|
159 | // https://code.spip.net/@image_filtrer |
---|
160 | function image_filtrer($args){ |
---|
161 | $filtre = array_shift($args); # enlever $filtre |
---|
162 | $texte = array_shift($args); |
---|
163 | if (!strlen($texte)) return; |
---|
164 | find_in_path('filtres_images_mini.php','inc/', true); |
---|
165 | statut_effacer_images_temporaires(true); // activer la suppression des images temporaires car le compilo finit la chaine par un image_graver |
---|
166 | // Cas du nom de fichier local |
---|
167 | if ( strpos(substr($texte,strlen(_DIR_RACINE)),'..')===FALSE |
---|
168 | AND !preg_match(',^/|[<>]|\s,S', $texte) |
---|
169 | AND ( |
---|
170 | file_exists(preg_replace(',[?].*$,','',$texte)) |
---|
171 | OR preg_match(';^(\w{3,7}://);', $texte) |
---|
172 | )) { |
---|
173 | array_unshift($args,"<img src='$texte' />"); |
---|
174 | $res = call_user_func_array($filtre, $args); |
---|
175 | statut_effacer_images_temporaires(false); // desactiver pour les appels hors compilo |
---|
176 | return $res; |
---|
177 | } |
---|
178 | |
---|
179 | // Cas general : trier toutes les images, avec eventuellement leur <span> |
---|
180 | if (preg_match_all( |
---|
181 | ',(<([a-z]+) [^<>]*spip_documents[^<>]*>)?\s*(<img\s.*>),UimsS', |
---|
182 | $texte, $tags, PREG_SET_ORDER)) { |
---|
183 | foreach ($tags as $tag) { |
---|
184 | $class = extraire_attribut($tag[3],'class'); |
---|
185 | if (!$class || (strpos($class,'no_image_filtrer')===FALSE)){ |
---|
186 | array_unshift($args,$tag[3]); |
---|
187 | if ($reduit = call_user_func_array($filtre, $args)) { |
---|
188 | // En cas de span spip_documents, modifier le style=...width: |
---|
189 | if($tag[1]){ |
---|
190 | $w = extraire_attribut($reduit, 'width'); |
---|
191 | if (!$w AND preg_match(",width:\s*(\d+)px,S",extraire_attribut($reduit,'style'),$regs)) |
---|
192 | $w = $regs[1]; |
---|
193 | if ($w AND ($style = extraire_attribut($tag[1], 'style'))){ |
---|
194 | $style = preg_replace(",width:\s*\d+px,S", "width:${w}px", $style); |
---|
195 | $replace = inserer_attribut($tag[1], 'style', $style); |
---|
196 | $texte = str_replace($tag[1], $replace, $texte); |
---|
197 | } |
---|
198 | } |
---|
199 | // traiter aussi un eventuel mouseover |
---|
200 | if ($mouseover = extraire_attribut($reduit,'onmouseover')){ |
---|
201 | if (preg_match(",this[.]src=['\"]([^'\"]+)['\"],ims", $mouseover, $match)){ |
---|
202 | $srcover = $match[1]; |
---|
203 | array_shift($args); |
---|
204 | array_unshift($args,"<img src='".$match[1]."' />"); |
---|
205 | $srcover_filter = call_user_func_array($filtre, $args); |
---|
206 | $srcover_filter = extraire_attribut($srcover_filter,'src'); |
---|
207 | $reduit = str_replace($srcover,$srcover_filter,$reduit); |
---|
208 | } |
---|
209 | } |
---|
210 | $texte = str_replace($tag[3], $reduit, $texte); |
---|
211 | } |
---|
212 | array_shift($args); |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | statut_effacer_images_temporaires(false); // desactiver pour les appels hors compilo |
---|
217 | return $texte; |
---|
218 | } |
---|
219 | |
---|
220 | // pour les feuilles de style CSS du prive |
---|
221 | function filtre_background_image_dist ($img, $couleur, $pos="") { |
---|
222 | if (!function_exists("imagecreatetruecolor") |
---|
223 | OR !include_spip('filtres/images_transforme') |
---|
224 | OR !function_exists('image_sepia') |
---|
225 | OR !function_exists('image_aplatir') |
---|
226 | ) |
---|
227 | return "background-color: #$couleur;"; |
---|
228 | return "background: url(".url_absolue(extraire_attribut(image_aplatir(image_sepia($img, $couleur),"gif","cccccc", 64, true), "src")).") $pos;"; |
---|
229 | } |
---|
230 | |
---|
231 | // |
---|
232 | // Retourner taille d'une image |
---|
233 | // pour les filtres |largeur et |hauteur |
---|
234 | // |
---|
235 | // https://code.spip.net/@taille_image |
---|
236 | function taille_image($img) { |
---|
237 | |
---|
238 | static $largeur_img =array(), $hauteur_img= array(); |
---|
239 | $srcWidth = 0; |
---|
240 | $srcHeight = 0; |
---|
241 | |
---|
242 | $logo = extraire_attribut($img,'src'); |
---|
243 | |
---|
244 | if (!$logo) $logo = $img; |
---|
245 | else { |
---|
246 | $srcWidth = extraire_attribut($img,'width'); |
---|
247 | $srcHeight = extraire_attribut($img,'height'); |
---|
248 | } |
---|
249 | |
---|
250 | // ne jamais operer directement sur une image distante pour des raisons de perfo |
---|
251 | // la copie locale a toutes les chances d'etre la ou de resservir |
---|
252 | if (preg_match(';^(\w{3,7}://);', $logo)){ |
---|
253 | include_spip('inc/distant'); |
---|
254 | $fichier = copie_locale($logo); |
---|
255 | $logo = $fichier ? _DIR_RACINE . $fichier : $logo; |
---|
256 | } |
---|
257 | if (($p=strpos($logo,'?'))!==FALSE) |
---|
258 | $logo=substr($logo,0,$p); |
---|
259 | |
---|
260 | $srcsize = false; |
---|
261 | if (isset($largeur_img[$logo])) |
---|
262 | $srcWidth = $largeur_img[$logo]; |
---|
263 | if (isset($hauteur_img[$logo])) |
---|
264 | $srcHeight = $hauteur_img[$logo]; |
---|
265 | if (!$srcWidth OR !$srcHeight){ |
---|
266 | if ($srcsize = @getimagesize($logo)){ |
---|
267 | if (!$srcWidth) $largeur_img[$logo] = $srcWidth = $srcsize[0]; |
---|
268 | if (!$srcHeight) $hauteur_img[$logo] = $srcHeight = $srcsize[1]; |
---|
269 | } |
---|
270 | // $logo peut etre une reference a une image temporaire dont a n'a que le log .src |
---|
271 | // on s'y refere, l'image sera reconstruite en temps utile si necessaire |
---|
272 | elseif(@file_exists($f = "$logo.src") |
---|
273 | AND lire_fichier($f,$valeurs) |
---|
274 | AND $valeurs=unserialize($valeurs)) { |
---|
275 | if (!$srcWidth) $largeur_img[$mem] = $srcWidth = $valeurs["largeur_dest"]; |
---|
276 | if (!$srcHeight) $hauteur_img[$mem] = $srcHeight = $valeurs["hauteur_dest"]; |
---|
277 | } |
---|
278 | } |
---|
279 | return array($srcHeight, $srcWidth); |
---|
280 | } |
---|
281 | // https://code.spip.net/@largeur |
---|
282 | function largeur($img) { |
---|
283 | if (!$img) return; |
---|
284 | list ($h,$l) = taille_image($img); |
---|
285 | return $l; |
---|
286 | } |
---|
287 | // https://code.spip.net/@hauteur |
---|
288 | function hauteur($img) { |
---|
289 | if (!$img) return; |
---|
290 | list ($h,$l) = taille_image($img); |
---|
291 | return $h; |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | // Echappement des entites HTML avec correction des entites "brutes" |
---|
296 | // (generees par les butineurs lorsqu'on rentre des caracteres n'appartenant |
---|
297 | // pas au charset de la page [iso-8859-1 par defaut]) |
---|
298 | // |
---|
299 | // Attention on limite cette correction aux caracteres "hauts" (en fait > 99 |
---|
300 | // pour aller plus vite que le > 127 qui serait logique), de maniere a |
---|
301 | // preserver des echappements de caracteres "bas" (par exemple [ ou ") |
---|
302 | // et au cas particulier de & qui devient &amp; dans les url |
---|
303 | // https://code.spip.net/@corriger_entites_html |
---|
304 | function corriger_entites_html($texte) { |
---|
305 | if (strpos($texte,'&') === false) return $texte; |
---|
306 | return preg_replace(',&(#[0-9][0-9][0-9]+;|amp;),iS', '&\1', $texte); |
---|
307 | } |
---|
308 | // idem mais corriger aussi les &eacute; en é |
---|
309 | // https://code.spip.net/@corriger_toutes_entites_html |
---|
310 | function corriger_toutes_entites_html($texte) { |
---|
311 | if (strpos($texte,'&') === false) return $texte; |
---|
312 | return preg_replace(',&(#?[a-z0-9]+;),iS', '&\1', $texte); |
---|
313 | } |
---|
314 | |
---|
315 | // https://code.spip.net/@proteger_amp |
---|
316 | function proteger_amp($texte){ |
---|
317 | return str_replace('&','&',$texte); |
---|
318 | } |
---|
319 | // https://code.spip.net/@entites_html |
---|
320 | function entites_html($texte, $tout=false) { |
---|
321 | if (!is_string($texte) OR !$texte |
---|
322 | OR !preg_match(",[&\"'<>],S", $texte) # strpbrk($texte, "&\"'<>")!==false |
---|
323 | ) return $texte; |
---|
324 | include_spip('inc/texte'); |
---|
325 | $texte = htmlspecialchars(echappe_retour(echappe_html($texte,'',true),'','proteger_amp')); |
---|
326 | if ($tout) |
---|
327 | return corriger_toutes_entites_html($texte); |
---|
328 | else |
---|
329 | return corriger_entites_html($texte); |
---|
330 | } |
---|
331 | |
---|
332 | // Transformer les é dans le charset local |
---|
333 | // https://code.spip.net/@filtrer_entites |
---|
334 | function filtrer_entites($texte) { |
---|
335 | if (strpos($texte,'&') === false) return $texte; |
---|
336 | // filtrer |
---|
337 | $texte = html2unicode($texte); |
---|
338 | // remettre le tout dans le charset cible |
---|
339 | return unicode2charset($texte); |
---|
340 | } |
---|
341 | |
---|
342 | // caracteres de controle - http://www.w3.org/TR/REC-xml/#charsets |
---|
343 | // https://code.spip.net/@supprimer_caracteres_illegaux |
---|
344 | function supprimer_caracteres_illegaux($texte) { |
---|
345 | static $from = "\x0\x1\x2\x3\x4\x5\x6\x7\x8\xB\xC\xE\xF\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"; |
---|
346 | static $to = null; |
---|
347 | if (!$to) $to = str_repeat('-', strlen($from)); |
---|
348 | return strtr($texte, $from, $to); |
---|
349 | } |
---|
350 | |
---|
351 | // Supprimer caracteres windows et les caracteres de controle ILLEGAUX |
---|
352 | // https://code.spip.net/@corriger_caracteres |
---|
353 | function corriger_caracteres ($texte) { |
---|
354 | $texte = corriger_caracteres_windows($texte); |
---|
355 | $texte = supprimer_caracteres_illegaux($texte); |
---|
356 | return $texte; |
---|
357 | } |
---|
358 | |
---|
359 | // Encode du HTML pour transmission XML |
---|
360 | // https://code.spip.net/@texte_backend |
---|
361 | function texte_backend($texte) { |
---|
362 | |
---|
363 | static $apostrophe = array("’", "'"); # n'allouer qu'une fois |
---|
364 | |
---|
365 | // si on a des liens ou des images, les passer en absolu |
---|
366 | $texte = liens_absolus($texte); |
---|
367 | |
---|
368 | // echapper les tags > < |
---|
369 | $texte = preg_replace(',&(gt|lt);,S', '&\1;', $texte); |
---|
370 | |
---|
371 | // importer les é |
---|
372 | $texte = filtrer_entites($texte); |
---|
373 | |
---|
374 | // " -> " et tout ce genre de choses |
---|
375 | $u = $GLOBALS['meta']['pcre_u']; |
---|
376 | $texte = str_replace(" ", " ", $texte); |
---|
377 | $texte = preg_replace('/\s{2,}/S'.$u, " ", $texte); |
---|
378 | $texte = entites_html($texte); |
---|
379 | |
---|
380 | // verifier le charset |
---|
381 | $texte = charset2unicode($texte); |
---|
382 | |
---|
383 | // Caracteres problematiques en iso-latin 1 |
---|
384 | if ($GLOBALS['meta']['charset'] == 'iso-8859-1') { |
---|
385 | $texte = str_replace(chr(156), 'œ', $texte); |
---|
386 | $texte = str_replace(chr(140), 'Œ', $texte); |
---|
387 | $texte = str_replace(chr(159), 'Ÿ', $texte); |
---|
388 | } |
---|
389 | |
---|
390 | // l'apostrophe curly pose probleme a certains lecteure de RSS |
---|
391 | // et le caractere apostrophe alourdit les squelettes avec PHP |
---|
392 | // ==> on les remplace par l'entite HTML |
---|
393 | return str_replace($apostrophe, "'", $texte); |
---|
394 | } |
---|
395 | |
---|
396 | // Comme ci-dessus, mais avec addslashes final pour squelettes avec PHP (rss) |
---|
397 | |
---|
398 | function texte_backendq($texte) { |
---|
399 | return addslashes(texte_backend($texte)); |
---|
400 | } |
---|
401 | |
---|
402 | // Enleve le numero des titres numerotes ("1. Titre" -> "Titre") |
---|
403 | // https://code.spip.net/@supprimer_numero |
---|
404 | function supprimer_numero($texte) { |
---|
405 | return preg_replace( |
---|
406 | ",^[[:space:]]*([0-9]+)([.)]|".chr(194).'?'.chr(176).")[[:space:]]+,S", |
---|
407 | "", $texte); |
---|
408 | } |
---|
409 | |
---|
410 | // et la fonction inverse |
---|
411 | // https://code.spip.net/@recuperer_numero |
---|
412 | function recuperer_numero($texte) { |
---|
413 | if (preg_match( |
---|
414 | ",^[[:space:]]*([0-9]+)([.)]|".chr(194).'?'.chr(176).")[[:space:]]+,S", |
---|
415 | $texte, $regs)) |
---|
416 | return intval($regs[1]); |
---|
417 | else |
---|
418 | return ''; |
---|
419 | } |
---|
420 | |
---|
421 | // Suppression basique et brutale de tous les <...> |
---|
422 | // https://code.spip.net/@supprimer_tags |
---|
423 | function supprimer_tags($texte, $rempl = "") { |
---|
424 | $texte = preg_replace(",<[^>]*>,US", $rempl, $texte); |
---|
425 | // ne pas oublier un < final non ferme |
---|
426 | // mais qui peut aussi etre un simple signe plus petit que |
---|
427 | $texte = str_replace('<', ' ', $texte); |
---|
428 | return $texte; |
---|
429 | } |
---|
430 | |
---|
431 | // Convertit les <...> en la version lisible en HTML |
---|
432 | // https://code.spip.net/@echapper_tags |
---|
433 | function echapper_tags($texte, $rempl = "") { |
---|
434 | $texte = preg_replace("/<([^>]*)>/", "<\\1>", $texte); |
---|
435 | return $texte; |
---|
436 | } |
---|
437 | |
---|
438 | // Convertit un texte HTML en texte brut |
---|
439 | // https://code.spip.net/@textebrut |
---|
440 | function textebrut($texte) { |
---|
441 | $u = $GLOBALS['meta']['pcre_u']; |
---|
442 | $texte = preg_replace('/\s+/S'.$u, " ", $texte); |
---|
443 | $texte = preg_replace("/<(p|br)( [^>]*)?".">/iS", "\n\n", $texte); |
---|
444 | $texte = preg_replace("/^\n+/", "", $texte); |
---|
445 | $texte = preg_replace("/\n+$/", "", $texte); |
---|
446 | $texte = preg_replace("/\n +/", "\n", $texte); |
---|
447 | $texte = supprimer_tags($texte); |
---|
448 | $texte = preg_replace("/( | )+/S", " ", $texte); |
---|
449 | // nettoyer l'apostrophe curly qui pose probleme a certains rss-readers, lecteurs de mail... |
---|
450 | $texte = str_replace("’","'",$texte); |
---|
451 | return $texte; |
---|
452 | } |
---|
453 | |
---|
454 | // Remplace les liens SPIP en liens ouvrant dans une nouvelle fenetre (target=blank) |
---|
455 | // https://code.spip.net/@liens_ouvrants |
---|
456 | function liens_ouvrants ($texte) { |
---|
457 | return preg_replace(",<a ([^>]*https?://[^>]*class=[\"']spip_(out|url)\b[^>]+)>,", |
---|
458 | "<a \\1 target=\"_blank\">", $texte); |
---|
459 | } |
---|
460 | |
---|
461 | // Transformer les sauts de paragraphe en simples passages a la ligne |
---|
462 | // https://code.spip.net/@PtoBR |
---|
463 | function PtoBR($texte){ |
---|
464 | $u = $GLOBALS['meta']['pcre_u']; |
---|
465 | $texte = preg_replace("@</p>@iS", "\n", $texte); |
---|
466 | $texte = preg_replace("@<p\b.*>@UiS", "<br />", $texte); |
---|
467 | $texte = preg_replace("@^\s*<br />@S".$u, "", $texte); |
---|
468 | return $texte; |
---|
469 | } |
---|
470 | |
---|
471 | // Couper les "mots" de plus de $l caracteres (souvent des URLs) |
---|
472 | // https://code.spip.net/@lignes_longues |
---|
473 | function lignes_longues($texte, $l = 70) { |
---|
474 | if ($l<1) return $texte; |
---|
475 | if (!preg_match("/[\w,\/.]{".$l."}/UmsS", $texte)) |
---|
476 | return $texte; |
---|
477 | // Passer en utf-8 pour ne pas avoir de coupes trop courtes avec les &#xxxx; |
---|
478 | // qui prennent 7 caracteres |
---|
479 | #include_spip('inc/charsets'); |
---|
480 | $texte = str_replace(" ","< >",$texte); |
---|
481 | $texte = html2unicode($texte, true); |
---|
482 | $texte = str_replace("< >"," ",$texte); |
---|
483 | $texte = unicode_to_utf_8(charset2unicode( |
---|
484 | $texte, $GLOBALS['meta']['charset'], true)); |
---|
485 | |
---|
486 | // echapper les tags (on ne veut pas casser les a href=...) |
---|
487 | $tags = array(); |
---|
488 | if (preg_match_all('/<.+>|&(?:amp;)?#x?[0-9]+;|&(?:amp;)?[a-zA-Z1-4]{2,6};/UumsS', $texte, $t, PREG_SET_ORDER)) { |
---|
489 | foreach ($t as $n => $tag) { |
---|
490 | $tags[$n] = $tag[0]; |
---|
491 | $texte = str_replace($tag[0], "<---$n--->", $texte); |
---|
492 | } |
---|
493 | } |
---|
494 | // casser les mots longs qui restent |
---|
495 | // note : on pourrait preferer couper sur les / , etc. |
---|
496 | if (preg_match_all("/[\w,\/.]{".$l."}/UmsS", $texte, $longs, PREG_SET_ORDER)) { |
---|
497 | foreach ($longs as $long) { |
---|
498 | $texte = str_replace($long[0], $long[0].' ', $texte); |
---|
499 | } |
---|
500 | } |
---|
501 | |
---|
502 | // retablir les tags |
---|
503 | if (preg_match_all('/<---[\s0-9]+--->/UumsS', $texte, $t, PREG_SET_ORDER)) { |
---|
504 | foreach ($t as $tag) { |
---|
505 | $n = intval(preg_replace(',[^0-9]+,U','',$tag[0])); |
---|
506 | $texte = str_replace($tag[0], $tags[$n], $texte); |
---|
507 | } |
---|
508 | } |
---|
509 | |
---|
510 | return importer_charset($texte, 'utf-8'); |
---|
511 | } |
---|
512 | |
---|
513 | // Majuscules y compris accents, en HTML |
---|
514 | // https://code.spip.net/@majuscules |
---|
515 | function majuscules($texte) { |
---|
516 | if (!strlen($texte)) return ''; |
---|
517 | |
---|
518 | // Cas du turc |
---|
519 | if ($GLOBALS['spip_lang'] == 'tr') { |
---|
520 | # remplacer hors des tags et des entites |
---|
521 | if (preg_match_all(',<[^<>]+>|&[^;]+;,S', $texte, $regs, PREG_SET_ORDER)) |
---|
522 | foreach ($regs as $n => $match) |
---|
523 | $texte = str_replace($match[0], "@@SPIP_TURC$n@@", $texte); |
---|
524 | |
---|
525 | $texte = str_replace('i', 'İ', $texte); |
---|
526 | |
---|
527 | if ($regs) |
---|
528 | foreach ($regs as $n => $match) |
---|
529 | $texte = str_replace("@@SPIP_TURC$n@@", $match[0], $texte); |
---|
530 | } |
---|
531 | |
---|
532 | // Cas general |
---|
533 | return "<span style='text-transform: uppercase;'>$texte</span>"; |
---|
534 | } |
---|
535 | |
---|
536 | // "127.4 ko" ou "3.1 Mo" |
---|
537 | // https://code.spip.net/@taille_en_octets |
---|
538 | function taille_en_octets ($taille) { |
---|
539 | if ($taille < 1024) {$taille = _T('taille_octets', array('taille' => $taille));} |
---|
540 | else if ($taille < 1024*1024) { |
---|
541 | $taille = _T('taille_ko', array('taille' => ((floor($taille / 102.4))/10))); |
---|
542 | } else { |
---|
543 | $taille = _T('taille_mo', array('taille' => ((floor(($taille / 1024) / 102.4))/10))); |
---|
544 | } |
---|
545 | return $taille; |
---|
546 | } |
---|
547 | |
---|
548 | |
---|
549 | // Rend une chaine utilisable sans dommage comme attribut HTML |
---|
550 | // https://code.spip.net/@attribut_html |
---|
551 | function attribut_html($texte,$textebrut = true) { |
---|
552 | $u = $GLOBALS['meta']['pcre_u']; |
---|
553 | if ($textebrut) |
---|
554 | $texte = preg_replace(array(",\n,",",\s(?=\s),msS".$u),array(" ",""),textebrut($texte)); |
---|
555 | $texte = texte_backend($texte); |
---|
556 | $texte = str_replace(array("'",'"'),array(''', '"'), $texte); |
---|
557 | |
---|
558 | return preg_replace(array("/&(amp;|#38;)/","/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,5};)/"),array("&","&") , $texte); |
---|
559 | } |
---|
560 | |
---|
561 | // Vider les url nulles comme 'http://' ou 'mailto:' |
---|
562 | // et leur appliquer un htmlspecialchars() + gerer les & |
---|
563 | // https://code.spip.net/@vider_url |
---|
564 | function vider_url($url, $entites = true) { |
---|
565 | # un message pour abs_url |
---|
566 | $GLOBALS['mode_abs_url'] = 'url'; |
---|
567 | |
---|
568 | $url = trim($url); |
---|
569 | if (preg_match(",^(http:?/?/?|mailto:?)$,iS", $url)) |
---|
570 | return ''; |
---|
571 | |
---|
572 | if ($entites) $url = entites_html($url); |
---|
573 | |
---|
574 | return $url; |
---|
575 | } |
---|
576 | |
---|
577 | // Extraire une date de n'importe quel champ (a completer...) |
---|
578 | // https://code.spip.net/@extraire_date |
---|
579 | function extraire_date($texte) { |
---|
580 | // format = 2001-08 |
---|
581 | if (preg_match(",([1-2][0-9]{3})[^0-9]*(1[0-2]|0?[1-9]),",$texte,$regs)) |
---|
582 | return $regs[1]."-".sprintf("%02d", $regs[2])."-01"; |
---|
583 | } |
---|
584 | |
---|
585 | // Maquiller une adresse e-mail |
---|
586 | // https://code.spip.net/@antispam |
---|
587 | function antispam($texte) { |
---|
588 | include_spip('inc/acces'); |
---|
589 | $masque = creer_pass_aleatoire(3); |
---|
590 | return preg_replace("/@/", " $masque ", $texte); |
---|
591 | } |
---|
592 | |
---|
593 | // https://code.spip.net/@securiser_acces |
---|
594 | function securiser_acces($id_auteur, $cle, $dir, $op='', $args='') |
---|
595 | { |
---|
596 | include_spip('inc/acces'); |
---|
597 | if ($op) $dir .= " $op $args"; |
---|
598 | return verifier_low_sec($id_auteur, $cle, $dir); |
---|
599 | } |
---|
600 | |
---|
601 | // sinon{texte, rien} : affiche "rien" si la chaine est vide, |
---|
602 | // affiche la chaine si non vide ; |
---|
603 | // attention c'est compile directement dans inc/references |
---|
604 | // https://code.spip.net/@sinon |
---|
605 | function sinon ($texte, $sinon='') { |
---|
606 | if ($texte OR (!is_array($texte) AND strlen($texte))) |
---|
607 | return $texte; |
---|
608 | else |
---|
609 | return $sinon; |
---|
610 | } |
---|
611 | |
---|
612 | // |choixsivide{vide,pasvide} affiche pasvide si la chaine n'est pas vide... |
---|
613 | // https://code.spip.net/@choixsivide |
---|
614 | function choixsivide($a, $vide, $pasvide) { |
---|
615 | return $a ? $pasvide : $vide; |
---|
616 | } |
---|
617 | |
---|
618 | // |choixsiegal{aquoi,oui,non} affiche oui si la chaine est egal a aquoi ... |
---|
619 | // https://code.spip.net/@choixsiegal |
---|
620 | function choixsiegal($a1,$a2,$v,$f) { |
---|
621 | return ($a1 == $a2) ? $v : $f; |
---|
622 | } |
---|
623 | |
---|
624 | |
---|
625 | // |
---|
626 | // Date, heure, saisons |
---|
627 | // |
---|
628 | |
---|
629 | // on normalise la date, si elle vient du contexte (public/parametrer.php), on force le jour |
---|
630 | // https://code.spip.net/@normaliser_date |
---|
631 | function normaliser_date($date, $forcer_jour = false) { |
---|
632 | $date = vider_date($date); |
---|
633 | if ($date) { |
---|
634 | if (preg_match("/^[0-9]{8,10}$/", $date)) |
---|
635 | $date = date("Y-m-d H:i:s", $date); |
---|
636 | if (preg_match("#^([12][0-9]{3})([-/]00)?( [-0-9:]+)?$#", $date, $regs)) |
---|
637 | $date = $regs[1]."-00-00".$regs[3]; |
---|
638 | else if (preg_match("#^([12][0-9]{3}[-/][01]?[0-9])([-/]00)?( [-0-9:]+)?$#", $date, $regs)) |
---|
639 | $date = preg_replace("@/@","-",$regs[1])."-00".$regs[3]; |
---|
640 | else |
---|
641 | $date = date("Y-m-d H:i:s", strtotime($date)); |
---|
642 | |
---|
643 | if ($forcer_jour) |
---|
644 | $date = str_replace('-00', '-01', $date); |
---|
645 | } |
---|
646 | return $date; |
---|
647 | } |
---|
648 | |
---|
649 | // https://code.spip.net/@vider_date |
---|
650 | function vider_date($letexte) { |
---|
651 | if (strncmp("0000-00-00", $letexte,10)==0) return ''; |
---|
652 | if (strncmp("0001-01-01", $letexte,10)==0) return ''; |
---|
653 | if (strncmp("1970-01-01", $letexte,10)==0) return ''; // eviter le bug GMT-1 |
---|
654 | return $letexte; |
---|
655 | } |
---|
656 | |
---|
657 | // https://code.spip.net/@recup_heure |
---|
658 | function recup_heure($date){ |
---|
659 | |
---|
660 | static $d = array(0,0,0); |
---|
661 | if (!preg_match('#([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $date, $r)) |
---|
662 | return $d; |
---|
663 | |
---|
664 | array_shift($r); |
---|
665 | return $r; |
---|
666 | } |
---|
667 | |
---|
668 | // https://code.spip.net/@heures |
---|
669 | function heures($numdate) { |
---|
670 | $date_array = recup_heure($numdate); |
---|
671 | if ($date_array) |
---|
672 | list($heures, $minutes, $secondes) = $date_array; |
---|
673 | return $heures; |
---|
674 | } |
---|
675 | |
---|
676 | // https://code.spip.net/@minutes |
---|
677 | function minutes($numdate) { |
---|
678 | $date_array = recup_heure($numdate); |
---|
679 | if ($date_array) |
---|
680 | list($heures, $minutes, $secondes) = $date_array; |
---|
681 | return $minutes; |
---|
682 | } |
---|
683 | |
---|
684 | // https://code.spip.net/@secondes |
---|
685 | function secondes($numdate) { |
---|
686 | $date_array = recup_heure($numdate); |
---|
687 | if ($date_array) |
---|
688 | list($heures,$minutes,$secondes) = $date_array; |
---|
689 | return $secondes; |
---|
690 | } |
---|
691 | |
---|
692 | // https://code.spip.net/@heures_minutes |
---|
693 | function heures_minutes($numdate) { |
---|
694 | return _T('date_fmt_heures_minutes', array('h'=> heures($numdate), 'm'=> minutes($numdate))); |
---|
695 | } |
---|
696 | |
---|
697 | // https://code.spip.net/@recup_date |
---|
698 | function recup_date($numdate, $forcer_jour = true){ |
---|
699 | if (!$numdate) return ''; |
---|
700 | $heures = $minutes = $secondes = 0; |
---|
701 | if (preg_match('#([0-9]{1,2})/([0-9]{1,2})/([0-9]{4}|[0-9]{1,2})#', $numdate, $regs)) { |
---|
702 | $jour = $regs[1]; |
---|
703 | $mois = $regs[2]; |
---|
704 | $annee = $regs[3]; |
---|
705 | if ($annee < 90){ |
---|
706 | $annee = 2000 + $annee; |
---|
707 | } elseif ($annee<100) { |
---|
708 | $annee = 1900 + $annee ; |
---|
709 | } |
---|
710 | list($heures, $minutes, $secondes) = recup_heure($numdate); |
---|
711 | |
---|
712 | } |
---|
713 | elseif (preg_match('#([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})#',$numdate, $regs)) { |
---|
714 | $annee = $regs[1]; |
---|
715 | $mois = $regs[2]; |
---|
716 | $jour = $regs[3]; |
---|
717 | list($heures, $minutes, $secondes) = recup_heure($numdate); |
---|
718 | } |
---|
719 | elseif (preg_match('#([0-9]{4})-([0-9]{2})#', $numdate, $regs)){ |
---|
720 | $annee = $regs[1]; |
---|
721 | $mois = $regs[2]; |
---|
722 | $jour =''; |
---|
723 | list($heures, $minutes, $secondes) = recup_heure($numdate); |
---|
724 | } |
---|
725 | elseif (preg_match('#^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$#', $numdate, $regs)){ |
---|
726 | $annee = $regs[1]; |
---|
727 | $mois = $regs[2]; |
---|
728 | $jour = $regs[3]; |
---|
729 | $heures = $regs[4]; |
---|
730 | $minutes = $regs[5]; |
---|
731 | $secondes = $regs[6]; |
---|
732 | } else $annee = $mois = $jour =''; |
---|
733 | if ($annee > 4000) $annee -= 9000; |
---|
734 | if (substr($jour, 0, 1) == '0') $jour = substr($jour, 1); |
---|
735 | |
---|
736 | if ($forcer_jour AND $jour == '0') $jour = '1'; |
---|
737 | if ($forcer_jour AND $mois == '0') $mois = '1'; |
---|
738 | if ($annee OR $mois OR $jour OR $heures OR $minutes OR $secondes) |
---|
739 | return array($annee, $mois, $jour, $heures, $minutes, $secondes); |
---|
740 | } |
---|
741 | |
---|
742 | // une date pour l'interface : utilise date_relative si le decalage |
---|
743 | // avec time() est de moins de douze heures, sinon la date complete |
---|
744 | // https://code.spip.net/@date_interface |
---|
745 | function date_interface($date, $decalage_maxi = 43200/* 12*3600 */) { |
---|
746 | return sinon( |
---|
747 | date_relative($date, $decalage_maxi), |
---|
748 | affdate_heure($date) |
---|
749 | ); |
---|
750 | } |
---|
751 | |
---|
752 | // https://code.spip.net/@date_relative |
---|
753 | function date_relative($date, $decalage_maxi=0,$ref_date=null) { |
---|
754 | |
---|
755 | if (is_null($ref_date)) |
---|
756 | $ref_time = time(); |
---|
757 | else |
---|
758 | $ref_time = strtotime($ref_date); |
---|
759 | |
---|
760 | if (!$date) return; |
---|
761 | $decal = date("U",$ref_time) - date("U", strtotime($date)); |
---|
762 | |
---|
763 | if ($decalage_maxi AND ($decal > $decalage_maxi OR $decal < 0)) |
---|
764 | return ''; |
---|
765 | |
---|
766 | if ($decal < 0) { |
---|
767 | $il_y_a = "date_dans"; |
---|
768 | $decal = -1 * $decal; |
---|
769 | } else { |
---|
770 | $il_y_a = "date_il_y_a"; |
---|
771 | } |
---|
772 | |
---|
773 | if ($decal > 3600 * 24 * 30 * 6) |
---|
774 | return affdate_court($date); |
---|
775 | |
---|
776 | if ($decal > 3600 * 24 * 30) { |
---|
777 | $mois = floor ($decal / (3600 * 24 * 30)); |
---|
778 | if ($mois < 2) |
---|
779 | $delai = "$mois "._T("date_un_mois"); |
---|
780 | else |
---|
781 | $delai = "$mois "._T("date_mois"); |
---|
782 | } |
---|
783 | else if ($decal > 3600 * 24 * 7) { |
---|
784 | $semaines = floor ($decal / (3600 * 24 * 7)); |
---|
785 | if ($semaines < 2) |
---|
786 | $delai = "$semaines "._T("date_une_semaine"); |
---|
787 | else |
---|
788 | $delai = "$semaines "._T("date_semaines"); |
---|
789 | } |
---|
790 | else if ($decal > 3600 * 24) { |
---|
791 | $jours = floor ($decal / (3600 * 24)); |
---|
792 | if ($jours < 2) |
---|
793 | return $il_y_a=="date_dans"?_T("date_demain"):_T("date_hier"); |
---|
794 | else |
---|
795 | $delai = "$jours "._T("date_jours"); |
---|
796 | } |
---|
797 | else if ($decal >= 3600) { |
---|
798 | $heures = floor ($decal / 3600); |
---|
799 | if ($heures < 2) |
---|
800 | $delai = "$heures "._T("date_une_heure"); |
---|
801 | else |
---|
802 | $delai = "$heures "._T("date_heures"); |
---|
803 | } |
---|
804 | else if ($decal >= 60) { |
---|
805 | $minutes = floor($decal / 60); |
---|
806 | if ($minutes < 2) |
---|
807 | $delai = "$minutes "._T("date_une_minute"); |
---|
808 | else |
---|
809 | $delai = "$minutes "._T("date_minutes"); |
---|
810 | } else { |
---|
811 | $secondes = ceil($decal); |
---|
812 | if ($secondes < 2) |
---|
813 | $delai = "$secondes "._T("date_une_seconde"); |
---|
814 | else |
---|
815 | $delai = "$secondes "._T("date_secondes"); |
---|
816 | } |
---|
817 | |
---|
818 | return _T($il_y_a, array("delai"=> $delai)); |
---|
819 | } |
---|
820 | |
---|
821 | |
---|
822 | // https://code.spip.net/@date_relativecourt |
---|
823 | function date_relativecourt($date, $decalage_maxi=0) { |
---|
824 | |
---|
825 | if (!$date) return; |
---|
826 | $decal = date("U",strtotime(date('Y-m-d'))-strtotime(date('Y-m-d',strtotime($date)))); |
---|
827 | |
---|
828 | if ($decalage_maxi AND ($decal > $decalage_maxi OR $decal < 0)) |
---|
829 | return ''; |
---|
830 | |
---|
831 | if ($decal < -24*3600) { |
---|
832 | $retour = date_relative($date, $decalage_maxi); |
---|
833 | } |
---|
834 | elseif ($decal < 0) { |
---|
835 | $retour = _T("date_demain"); |
---|
836 | } |
---|
837 | else if ($decal < (3600 * 24) ) { |
---|
838 | $retour = _T("date_aujourdhui"); |
---|
839 | } |
---|
840 | else if ($decal < (3600 * 24 *2) ) { |
---|
841 | $retour = _T("date_hier"); |
---|
842 | } |
---|
843 | else { |
---|
844 | $retour = date_relative($date, $decalage_maxi); |
---|
845 | } |
---|
846 | |
---|
847 | |
---|
848 | |
---|
849 | return $retour; |
---|
850 | } |
---|
851 | |
---|
852 | // https://code.spip.net/@affdate_base |
---|
853 | function affdate_base($numdate, $vue, $param = '') { |
---|
854 | global $spip_lang; |
---|
855 | $date_array = recup_date($numdate, false); |
---|
856 | if (!$date_array) return; |
---|
857 | list($annee, $mois, $jour, $heures, $minutes, $secondes)= $date_array; |
---|
858 | |
---|
859 | // 1er, 21st, etc. |
---|
860 | $journum = $jour; |
---|
861 | |
---|
862 | if ($jour == 0) { |
---|
863 | $jour = ''; |
---|
864 | } else { |
---|
865 | $njour = intval($jour); |
---|
866 | if ($jourth = _T('date_jnum'.$jour)) |
---|
867 | $jour = $jourth; |
---|
868 | } |
---|
869 | |
---|
870 | $mois = intval($mois); |
---|
871 | if ($mois > 0 AND $mois < 13) { |
---|
872 | $nommois = _T('date_mois_'.$mois); |
---|
873 | if ($jour) |
---|
874 | $jourmois = _T('date_de_mois_'.$mois, array('j'=>$jour, 'nommois'=>$nommois)); |
---|
875 | else |
---|
876 | $jourmois = $nommois; |
---|
877 | } else $nommois = ''; |
---|
878 | |
---|
879 | if ($annee < 0) { |
---|
880 | $annee = -$annee." "._T('date_avant_jc'); |
---|
881 | $avjc = true; |
---|
882 | } |
---|
883 | else $avjc = false; |
---|
884 | |
---|
885 | switch ($vue) { |
---|
886 | case 'saison': |
---|
887 | if ($mois > 0){ |
---|
888 | $saison = 1; |
---|
889 | if (($mois == 3 AND $jour >= 21) OR $mois > 3) $saison = 2; |
---|
890 | if (($mois == 6 AND $jour >= 21) OR $mois > 6) $saison = 3; |
---|
891 | if (($mois == 9 AND $jour >= 21) OR $mois > 9) $saison = 4; |
---|
892 | if (($mois == 12 AND $jour >= 21) OR $mois > 12) $saison = 1; |
---|
893 | } |
---|
894 | return _T('date_saison_'.$saison); |
---|
895 | |
---|
896 | case 'court': |
---|
897 | if ($avjc) return $annee; |
---|
898 | $a = date('Y'); |
---|
899 | if ($annee < ($a - 100) OR $annee > ($a + 100)) return $annee; |
---|
900 | if ($annee != $a) return _T('date_fmt_mois_annee', array ('mois'=>$mois, 'nommois'=>ucfirst($nommois), 'annee'=>$annee)); |
---|
901 | return _T('date_fmt_jour_mois', array ('jourmois'=>$jourmois, 'jour'=>$jour, 'mois'=>$mois, 'nommois'=>$nommois, 'annee'=>$annee)); |
---|
902 | |
---|
903 | case 'jourcourt': |
---|
904 | if ($avjc) return $annee; |
---|
905 | $a = date('Y'); |
---|
906 | if ($annee < ($a - 100) OR $annee > ($a + 100)) return $annee; |
---|
907 | if ($annee != $a) return _T('date_fmt_jour_mois_annee', array ('jourmois'=>$jourmois, 'jour'=>$jour, 'mois'=>$mois, 'nommois'=>$nommois, 'annee'=>$annee)); |
---|
908 | return _T('date_fmt_jour_mois', array ('jourmois'=>$jourmois, 'jour'=>$jour, 'mois'=>$mois, 'nommois'=>$nommois, 'annee'=>$annee)); |
---|
909 | |
---|
910 | case 'entier': |
---|
911 | if ($avjc) return $annee; |
---|
912 | if ($jour) |
---|
913 | return _T('date_fmt_jour_mois_annee', array ('jourmois'=>$jourmois, 'jour'=>$jour, 'mois'=>$mois, 'nommois'=>$nommois, 'annee'=>$annee)); |
---|
914 | elseif ($mois) |
---|
915 | return trim(_T('date_fmt_mois_annee', array ('mois'=>$mois, 'nommois'=>$nommois, 'annee'=>$annee))); |
---|
916 | else |
---|
917 | return $annee; |
---|
918 | |
---|
919 | case 'nom_mois': |
---|
920 | return $nommois; |
---|
921 | |
---|
922 | case 'mois': |
---|
923 | return sprintf("%02s",$mois); |
---|
924 | |
---|
925 | case 'jour': |
---|
926 | return $jour; |
---|
927 | |
---|
928 | case 'journum': |
---|
929 | return $journum; |
---|
930 | |
---|
931 | case 'nom_jour': |
---|
932 | if (!$mois OR !$njour) |
---|
933 | return ''; |
---|
934 | $nom = mktime(1,1,1,$mois,$njour,$annee); |
---|
935 | $nom = 1+date('w',$nom); |
---|
936 | $param = $param ? '_'.$param : ''; |
---|
937 | return _T('date_jour_'.$nom.$param); |
---|
938 | |
---|
939 | case 'mois_annee': |
---|
940 | if ($avjc) return $annee; |
---|
941 | return trim(_T('date_fmt_mois_annee', array('mois'=>$mois, 'nommois'=>$nommois, 'annee'=>$annee))); |
---|
942 | |
---|
943 | case 'annee': |
---|
944 | return $annee; |
---|
945 | |
---|
946 | // Cas d'une vue non definie : retomber sur le format |
---|
947 | // de date propose par http://www.php.net/date |
---|
948 | default: |
---|
949 | return date($vue, strtotime($numdate)); |
---|
950 | } |
---|
951 | } |
---|
952 | |
---|
953 | // https://code.spip.net/@nom_jour |
---|
954 | function nom_jour($numdate, $forme = '') { |
---|
955 | if(!($forme == 'abbr' OR $forme == 'initiale')) $forme = ''; |
---|
956 | return affdate_base($numdate, 'nom_jour', $forme); |
---|
957 | } |
---|
958 | |
---|
959 | // https://code.spip.net/@jour |
---|
960 | function jour($numdate) { |
---|
961 | return affdate_base($numdate, 'jour'); |
---|
962 | } |
---|
963 | |
---|
964 | // https://code.spip.net/@journum |
---|
965 | function journum($numdate) { |
---|
966 | return affdate_base($numdate, 'journum'); |
---|
967 | } |
---|
968 | |
---|
969 | // https://code.spip.net/@mois |
---|
970 | function mois($numdate) { |
---|
971 | return affdate_base($numdate, 'mois'); |
---|
972 | } |
---|
973 | |
---|
974 | // https://code.spip.net/@nom_mois |
---|
975 | function nom_mois($numdate) { |
---|
976 | return affdate_base($numdate, 'nom_mois'); |
---|
977 | } |
---|
978 | |
---|
979 | // https://code.spip.net/@annee |
---|
980 | function annee($numdate) { |
---|
981 | return affdate_base($numdate, 'annee'); |
---|
982 | } |
---|
983 | |
---|
984 | // https://code.spip.net/@saison |
---|
985 | function saison($numdate) { |
---|
986 | return affdate_base($numdate, 'saison'); |
---|
987 | } |
---|
988 | |
---|
989 | // https://code.spip.net/@affdate |
---|
990 | function affdate($numdate, $format='entier') { |
---|
991 | return affdate_base($numdate, $format); |
---|
992 | } |
---|
993 | |
---|
994 | // https://code.spip.net/@affdate_court |
---|
995 | function affdate_court($numdate) { |
---|
996 | return affdate_base($numdate, 'court'); |
---|
997 | } |
---|
998 | |
---|
999 | // https://code.spip.net/@affdate_jourcourt |
---|
1000 | function affdate_jourcourt($numdate) { |
---|
1001 | return affdate_base($numdate, 'jourcourt'); |
---|
1002 | } |
---|
1003 | |
---|
1004 | // https://code.spip.net/@affdate_mois_annee |
---|
1005 | function affdate_mois_annee($numdate) { |
---|
1006 | return affdate_base($numdate, 'mois_annee'); |
---|
1007 | } |
---|
1008 | |
---|
1009 | // https://code.spip.net/@affdate_heure |
---|
1010 | function affdate_heure($numdate) { |
---|
1011 | $date_array = recup_date($numdate); |
---|
1012 | if (!$date_array) return; |
---|
1013 | list($annee, $mois, $jour, $heures, $minutes, $sec)= $date_array; |
---|
1014 | return _T('date_fmt_jour_heure', array('jour' => affdate($numdate), 'heure' => _T('date_fmt_heures_minutes', array('h'=> $heures, 'm'=> $minutes)))); |
---|
1015 | } |
---|
1016 | |
---|
1017 | |
---|
1018 | // |
---|
1019 | // Alignements en HTML (Old-style, preferer CSS) |
---|
1020 | // |
---|
1021 | |
---|
1022 | // Cette fonction cree le paragraphe s'il n'existe pas (texte sur un seul para) |
---|
1023 | // https://code.spip.net/@aligner |
---|
1024 | function aligner($letexte, $justif='') { |
---|
1025 | $letexte = trim($letexte); |
---|
1026 | if (!strlen($letexte)) return ''; |
---|
1027 | |
---|
1028 | // Paragrapher proprement |
---|
1029 | $letexte = paragrapher($letexte, true); |
---|
1030 | |
---|
1031 | // Inserer les alignements |
---|
1032 | if ($justif) |
---|
1033 | $letexte = str_replace( |
---|
1034 | '<p class="spip">', '<p class="spip" align="'.$justif.'">', |
---|
1035 | $letexte); |
---|
1036 | |
---|
1037 | return $letexte; |
---|
1038 | } |
---|
1039 | |
---|
1040 | // https://code.spip.net/@justifier |
---|
1041 | function justifier($letexte) { |
---|
1042 | return aligner($letexte,'justify'); |
---|
1043 | } |
---|
1044 | |
---|
1045 | // https://code.spip.net/@aligner_droite |
---|
1046 | function aligner_droite($letexte) { |
---|
1047 | return aligner($letexte,'right'); |
---|
1048 | } |
---|
1049 | |
---|
1050 | // https://code.spip.net/@aligner_gauche |
---|
1051 | function aligner_gauche($letexte) { |
---|
1052 | return aligner($letexte,'left'); |
---|
1053 | } |
---|
1054 | |
---|
1055 | // https://code.spip.net/@centrer |
---|
1056 | function centrer($letexte) { |
---|
1057 | return aligner($letexte,'center'); |
---|
1058 | } |
---|
1059 | |
---|
1060 | // https://code.spip.net/@style_align |
---|
1061 | function style_align($bof) { |
---|
1062 | global $spip_lang_left; |
---|
1063 | return "text-align: $spip_lang_left"; |
---|
1064 | } |
---|
1065 | |
---|
1066 | // |
---|
1067 | // Export iCal |
---|
1068 | // |
---|
1069 | |
---|
1070 | // https://code.spip.net/@filtrer_ical |
---|
1071 | function filtrer_ical($texte) { |
---|
1072 | #include_spip('inc/charsets'); |
---|
1073 | $texte = html2unicode($texte); |
---|
1074 | $texte = unicode2charset(charset2unicode($texte, $GLOBALS['meta']['charset'], 1), 'utf-8'); |
---|
1075 | $texte = preg_replace("/\n/", " ", $texte); |
---|
1076 | $texte = preg_replace("/,/", "\,", $texte); |
---|
1077 | |
---|
1078 | return $texte; |
---|
1079 | } |
---|
1080 | |
---|
1081 | // https://code.spip.net/@date_ical |
---|
1082 | function date_ical($date, $addminutes = 0) { |
---|
1083 | list($heures, $minutes, $secondes) = recup_heure($date); |
---|
1084 | list($annee, $mois, $jour) = recup_date($date); |
---|
1085 | return date("Ymd\THis", |
---|
1086 | mktime($heures, $minutes+$addminutes,$secondes,$mois,$jour,$annee)); |
---|
1087 | } |
---|
1088 | |
---|
1089 | // date_iso retourne la date au format "RFC 3339" / "ISO 8601" |
---|
1090 | // voir http://www.php.net/manual/fr/ref.datetime.php#datetime.constants |
---|
1091 | // https://code.spip.net/@date_iso |
---|
1092 | function date_iso($date_heure) { |
---|
1093 | list($annee, $mois, $jour) = recup_date($date_heure); |
---|
1094 | list($heures, $minutes, $secondes) = recup_heure($date_heure); |
---|
1095 | $time = @mktime($heures, $minutes, $secondes, $mois, $jour, $annee); |
---|
1096 | return gmdate('Y-m-d\TH:i:s\Z', $time); |
---|
1097 | } |
---|
1098 | |
---|
1099 | // date_822 retourne la date au format "RFC 822" |
---|
1100 | // utilise pour <pubdate> dans certains feeds RSS |
---|
1101 | // https://code.spip.net/@date_822 |
---|
1102 | function date_822($date_heure) { |
---|
1103 | list($annee, $mois, $jour) = recup_date($date_heure); |
---|
1104 | list($heures, $minutes, $secondes) = recup_heure($date_heure); |
---|
1105 | $time = mktime($heures, $minutes, $secondes, $mois, $jour, $annee); |
---|
1106 | return date('r', $time); |
---|
1107 | } |
---|
1108 | |
---|
1109 | // https://code.spip.net/@date_anneemoisjour |
---|
1110 | function date_anneemoisjour($d) { |
---|
1111 | if (!$d) $d = date("Y-m-d"); |
---|
1112 | return substr($d, 0, 4) . substr($d, 5, 2) .substr($d, 8, 2); |
---|
1113 | } |
---|
1114 | |
---|
1115 | // https://code.spip.net/@date_anneemois |
---|
1116 | function date_anneemois($d) { |
---|
1117 | if (!$d) $d = date("Y-m-d"); |
---|
1118 | return substr($d, 0, 4) . substr($d, 5, 2); |
---|
1119 | } |
---|
1120 | |
---|
1121 | // https://code.spip.net/@date_debut_semaine |
---|
1122 | function date_debut_semaine($annee, $mois, $jour) { |
---|
1123 | $w_day = date("w", mktime(0,0,0,$mois, $jour, $annee)); |
---|
1124 | if ($w_day == 0) $w_day = 7; // Gaffe: le dimanche est zero |
---|
1125 | $debut = $jour-$w_day+1; |
---|
1126 | return date("Ymd", mktime(0,0,0,$mois,$debut,$annee)); |
---|
1127 | } |
---|
1128 | |
---|
1129 | // https://code.spip.net/@date_fin_semaine |
---|
1130 | function date_fin_semaine($annee, $mois, $jour) { |
---|
1131 | $w_day = date("w", mktime(0,0,0,$mois, $jour, $annee)); |
---|
1132 | if ($w_day == 0) $w_day = 7; // Gaffe: le dimanche est zero |
---|
1133 | $debut = $jour-$w_day+1; |
---|
1134 | return date("Ymd", mktime(0,0,0,$mois,$debut+6,$annee)); |
---|
1135 | } |
---|
1136 | |
---|
1137 | // https://code.spip.net/@agenda_connu |
---|
1138 | function agenda_connu($type) |
---|
1139 | { |
---|
1140 | return in_array($type, array('jour','mois','semaine','periode')) ? ' ' : ''; |
---|
1141 | } |
---|
1142 | |
---|
1143 | |
---|
1144 | // Cette fonction memorise dans un tableau indexe par son 5e arg |
---|
1145 | // un evenement decrit par les 4 autres (date, descriptif, titre, URL). |
---|
1146 | // Appellee avec une date nulle, elle renvoie le tableau construit. |
---|
1147 | // l'indexation par le 5e arg autorise plusieurs calendriers dans une page |
---|
1148 | |
---|
1149 | // https://code.spip.net/@agenda_memo |
---|
1150 | function agenda_memo($date=0 , $descriptif='', $titre='', $url='', $cal='') |
---|
1151 | { |
---|
1152 | static $agenda = array(); |
---|
1153 | if (!$date) return $agenda; |
---|
1154 | $idate = date_ical($date); |
---|
1155 | $cal = trim($cal); // func_get_args (filtre alterner) rajoute \n !!!! |
---|
1156 | $agenda[$cal][(date_anneemoisjour($date))][] = array( |
---|
1157 | 'CATEGORIES' => $cal, |
---|
1158 | 'DTSTART' => $idate, |
---|
1159 | 'DTEND' => $idate, |
---|
1160 | 'DESCRIPTION' => texte_script($descriptif), |
---|
1161 | 'SUMMARY' => texte_script($titre), |
---|
1162 | 'URL' => $url); |
---|
1163 | // toujours retourner vide pour qu'il ne se passe rien |
---|
1164 | return ""; |
---|
1165 | } |
---|
1166 | |
---|
1167 | // Cette fonction recoit: |
---|
1168 | // - un nombre d'evenements, |
---|
1169 | // - une chaine a afficher si ce nombre est nul, |
---|
1170 | // - un type de calendrier |
---|
1171 | // -- et une suite de noms N. |
---|
1172 | // Elle demande a la fonction precedente son tableau |
---|
1173 | // et affiche selon le type les elements indexes par N dans ce tableau. |
---|
1174 | // Si le suite de noms est vide, tout le tableau est pris |
---|
1175 | // Ces noms N sont aussi des classes CSS utilisees par http_calendrier_init |
---|
1176 | // Cette fonction recupere aussi par _request les parametres |
---|
1177 | // jour, mois, annee, echelle, partie_cal (a ameliorer) |
---|
1178 | |
---|
1179 | // https://code.spip.net/@agenda_affiche |
---|
1180 | function agenda_affiche($i) |
---|
1181 | { |
---|
1182 | $args = func_get_args(); |
---|
1183 | // date (ou nombre d'evenements qu'on pourrait alors afficher) |
---|
1184 | $nb = array_shift($args); |
---|
1185 | $evt = array_shift($args); |
---|
1186 | $type = array_shift($args); |
---|
1187 | if ($nb) { |
---|
1188 | $agenda = agenda_memo(0); |
---|
1189 | $evt = array(); |
---|
1190 | foreach (($args ? $args : array_keys($agenda)) as $k) { |
---|
1191 | if (is_array($agenda[$k])) |
---|
1192 | foreach($agenda[$k] as $d => $v) { |
---|
1193 | $evt[$d] = $evt[$d] ? (array_merge($evt[$d], $v)) : $v; |
---|
1194 | } |
---|
1195 | } |
---|
1196 | } |
---|
1197 | return agenda_periode($type, $nb, $evt); |
---|
1198 | } |
---|
1199 | |
---|
1200 | function agenda_periode($type, $nb, $avec, $sans='') |
---|
1201 | { |
---|
1202 | include_spip('inc/agenda'); |
---|
1203 | $start = agenda_controle(); |
---|
1204 | if ($start<0) $start = time(); |
---|
1205 | $mindate = date("Ymd", $start); |
---|
1206 | |
---|
1207 | if ($type != 'periode') |
---|
1208 | $evt = array($sans, $avec); |
---|
1209 | else { |
---|
1210 | $min = substr($mindate,6,2); |
---|
1211 | $max = !(is_array($avec) AND $avec) ? time() : strtotime(max(array_keys($avec))); |
---|
1212 | $max = $min + (($max - $start) / (3600 * 24)); |
---|
1213 | if ($max < 31) $max = 0; |
---|
1214 | $evt = array($sans, $avec, $min, $max); |
---|
1215 | $type = 'mois'; |
---|
1216 | } |
---|
1217 | return http_calendrier_init($start, $type, _request('echelle'), _request('partie_cal'), self('&'), $evt); |
---|
1218 | } |
---|
1219 | |
---|
1220 | |
---|
1221 | // Controle la coherence des 3 nombres d'une date et retourne le temps Unix, |
---|
1222 | // sinon retourne un code d'erreur, toujours negatif |
---|
1223 | function agenda_controle($date='date', $jour='jour', $mois='mois', $annee='annee') |
---|
1224 | { |
---|
1225 | $jour = _request($jour); |
---|
1226 | $mois = _request($mois); |
---|
1227 | $annee = _request($annee); |
---|
1228 | |
---|
1229 | if (!($jour||$mois||$anne)) { |
---|
1230 | if ($date = recup_date(_request($date))) { |
---|
1231 | list($annee, $mois, $jour ) = $date; |
---|
1232 | } else return -1; |
---|
1233 | } |
---|
1234 | if (!$d = mktime(0,0,0, $mois, $jour, $annee)) return -2; |
---|
1235 | if ($jour != date("d", $d)) return -3; |
---|
1236 | if ($mois != date("m", $d)) return -4; |
---|
1237 | if ($annee != date("Y", $d)) return -5; |
---|
1238 | return $d; |
---|
1239 | } |
---|
1240 | |
---|
1241 | // |
---|
1242 | // Recuperation de donnees dans le champ extra |
---|
1243 | // Ce filtre n'a de sens qu'avec la balise #EXTRA |
---|
1244 | // |
---|
1245 | // https://code.spip.net/@extra |
---|
1246 | function extra($letexte, $champ) { |
---|
1247 | $champs = unserialize($letexte); |
---|
1248 | return $champs[$champ]; |
---|
1249 | } |
---|
1250 | |
---|
1251 | // postautobr : transforme les sauts de ligne en _ |
---|
1252 | // https://code.spip.net/@post_autobr |
---|
1253 | function post_autobr($texte, $delim="\n_ ") { |
---|
1254 | $texte = str_replace("\r\n", "\r", $texte); |
---|
1255 | $texte = str_replace("\r", "\n", $texte); |
---|
1256 | |
---|
1257 | if (preg_match(",\n+$,", $texte, $fin)) |
---|
1258 | $texte = substr($texte, 0, -strlen($fin = $fin[0])); |
---|
1259 | else |
---|
1260 | $fin = ''; |
---|
1261 | |
---|
1262 | $texte = echappe_html($texte, '', true); |
---|
1263 | |
---|
1264 | |
---|
1265 | $debut = ''; |
---|
1266 | $suite = $texte; |
---|
1267 | while ($t = strpos('-'.$suite, "\n", 1)) { |
---|
1268 | $debut .= substr($suite, 0, $t-1); |
---|
1269 | $suite = substr($suite, $t); |
---|
1270 | $car = substr($suite, 0, 1); |
---|
1271 | if (($car<>'-') AND ($car<>'_') AND ($car<>"\n") AND ($car<>"|") AND ($car<>"}") |
---|
1272 | AND !preg_match(',^\s*(\n|</?(quote|div)|$),S',($suite)) |
---|
1273 | AND !preg_match(',</?(quote|div)> *$,iS', $debut)) { |
---|
1274 | $debut .= $delim; |
---|
1275 | } else |
---|
1276 | $debut .= "\n"; |
---|
1277 | if (preg_match(",^\n+,", $suite, $regs)) { |
---|
1278 | $debut.=$regs[0]; |
---|
1279 | $suite = substr($suite, strlen($regs[0])); |
---|
1280 | } |
---|
1281 | } |
---|
1282 | $texte = $debut.$suite; |
---|
1283 | |
---|
1284 | $texte = echappe_retour($texte); |
---|
1285 | return $texte.$fin; |
---|
1286 | } |
---|
1287 | |
---|
1288 | |
---|
1289 | // |
---|
1290 | // Gestion des blocs multilingues |
---|
1291 | // |
---|
1292 | |
---|
1293 | define('_EXTRAIRE_MULTI', "@<multi>(.*?)</multi>@sS"); |
---|
1294 | // Extraire et transformer les blocs multi ; on indique la langue courante |
---|
1295 | // pour ne pas mettre de span@lang=fr si on est deja en fr |
---|
1296 | // https://code.spip.net/@extraire_multi |
---|
1297 | function extraire_multi($letexte, $lang=null, $echappe_span=false) { |
---|
1298 | if (strpos($letexte, '<multi>') === false) return $letexte; // perf |
---|
1299 | if (preg_match_all(_EXTRAIRE_MULTI, $letexte, $regs, PREG_SET_ORDER)) { |
---|
1300 | if (!$lang) $lang = $GLOBALS['spip_lang']; |
---|
1301 | |
---|
1302 | foreach ($regs as $reg) { |
---|
1303 | // chercher la version de la langue courante |
---|
1304 | $trads = extraire_trads($reg[1]); |
---|
1305 | if ($l = approcher_langue($trads, $lang)) { |
---|
1306 | $trad = $trads[$l]; |
---|
1307 | } else { |
---|
1308 | include_spip('inc/texte'); |
---|
1309 | // langue absente, prendre la premiere dispo |
---|
1310 | // mais typographier le texte selon les regles de celle-ci |
---|
1311 | // Attention aux blocs multi sur plusieurs lignes |
---|
1312 | $l = key($trads); |
---|
1313 | $trad = $trads[$l]; |
---|
1314 | $typographie = charger_fonction(lang_typo($l), 'typographie'); |
---|
1315 | $trad = $typographie($trad); |
---|
1316 | //backport du commit http://core.spip.org/projects/spip/repository/revisions/17424 pour Spip 3 |
---|
1317 | include_spip('inc/texte'); |
---|
1318 | $trad_propre = propre($trad); |
---|
1319 | // Tester si on echappe en span ou en div |
---|
1320 | $mode = preg_match(',</?('._BALISES_BLOCS.')[>[:space:]],iS', $trad_propre) ? 'div' : 'span'; |
---|
1321 | $trad = code_echappement($trad, 'multi', false, $mode); |
---|
1322 | $trad = str_replace("'", '"', inserer_attribut($trad, 'lang', $l)); |
---|
1323 | if (lang_dir($l) !== lang_dir($lang)) |
---|
1324 | $trad = str_replace("'", '"', inserer_attribut($trad, 'dir', lang_dir($l))); |
---|
1325 | //fin backport |
---|
1326 | if (!$echappe_span) |
---|
1327 | $trad = echappe_retour($trad, 'multi'); |
---|
1328 | } |
---|
1329 | $letexte = str_replace($reg[0], $trad, $letexte); |
---|
1330 | } |
---|
1331 | } |
---|
1332 | |
---|
1333 | return $letexte; |
---|
1334 | } |
---|
1335 | |
---|
1336 | |
---|
1337 | |
---|
1338 | // |
---|
1339 | // Selection dans un tableau dont les index sont des noms de langues |
---|
1340 | // de la valeur associee a la langue en cours |
---|
1341 | // si absente, retourne le premier |
---|
1342 | // remarque : on pourrait aussi appeler un service de traduction externe |
---|
1343 | // ou permettre de choisir une langue "plus proche", |
---|
1344 | // par exemple le francais pour l'espagnol, l'anglais pour l'allemand, etc. |
---|
1345 | |
---|
1346 | |
---|
1347 | function multi_trad ($trads, $lang='') { |
---|
1348 | $k = multi_trads($trads, $lang); |
---|
1349 | return $k ? $trads[$k] : array_shift($trads); |
---|
1350 | } |
---|
1351 | |
---|
1352 | // idem, mais retourne l'index |
---|
1353 | |
---|
1354 | function multi_trads ($trads, $lang='') { |
---|
1355 | |
---|
1356 | if (!$lang) $lang = $GLOBALS['spip_lang']; |
---|
1357 | |
---|
1358 | if (isset($trads[$lang])) { |
---|
1359 | return $lang; |
---|
1360 | |
---|
1361 | } // cas des langues xx_yy |
---|
1362 | else if (preg_match(',^([a-z]+)_,', $lang, $regs) AND isset($trads[$regs[1]])) { |
---|
1363 | return $regs[1]; |
---|
1364 | } |
---|
1365 | else return ''; |
---|
1366 | } |
---|
1367 | |
---|
1368 | // convertit le contenu d'une balise multi en un tableau |
---|
1369 | // https://code.spip.net/@extraire_trad |
---|
1370 | function extraire_trads($bloc) { |
---|
1371 | $lang = ''; |
---|
1372 | // ce reg fait planter l'analyse multi s'il y a de l'{italique} dans le champ |
---|
1373 | // while (preg_match("/^(.*?)[{\[]([a-z_]+)[}\]]/siS", $bloc, $regs)) { |
---|
1374 | while (preg_match("/^(.*?)[\[]([a-z_]+)[\]]/siS", $bloc, $regs)) { |
---|
1375 | $texte = trim($regs[1]); |
---|
1376 | if ($texte OR $lang) |
---|
1377 | $trads[$lang] = $texte; |
---|
1378 | $bloc = substr($bloc, strlen($regs[0])); |
---|
1379 | $lang = $regs[2]; |
---|
1380 | } |
---|
1381 | $trads[$lang] = $bloc; |
---|
1382 | |
---|
1383 | return $trads; |
---|
1384 | } |
---|
1385 | |
---|
1386 | // |
---|
1387 | // Ce filtre retourne la donnee si c'est la premiere fois qu'il la voit ; |
---|
1388 | // possibilite de gerer differentes "familles" de donnees |unique{famille} |
---|
1389 | # |unique{famille,1} affiche le nombre d'elements affiches (preferer toutefois #TOTAL_UNIQUE) |
---|
1390 | # ameliorations possibles : |
---|
1391 | # 1) si la donnee est grosse, mettre son md5 comme cle |
---|
1392 | # 2) purger $mem quand on change de squelette (sinon bug inclusions) |
---|
1393 | // |
---|
1394 | // https://www.spip.net/@unique |
---|
1395 | // https://code.spip.net/@unique |
---|
1396 | function unique($donnee, $famille='', $cpt = false) { |
---|
1397 | static $mem; |
---|
1398 | // permettre de vider la pile et de la restaurer |
---|
1399 | // pour le calcul de introduction... |
---|
1400 | if ($famille=='_spip_raz_'){ |
---|
1401 | $tmp = $mem; |
---|
1402 | $mem = array(); |
---|
1403 | return $tmp; |
---|
1404 | } elseif ($famille=='_spip_set_'){ |
---|
1405 | $mem = $donnee; |
---|
1406 | return; |
---|
1407 | } |
---|
1408 | |
---|
1409 | if ($cpt) |
---|
1410 | return count($mem[$famille]); |
---|
1411 | if (!($mem[$famille][$donnee]++)) |
---|
1412 | return $donnee; |
---|
1413 | } |
---|
1414 | |
---|
1415 | // |
---|
1416 | // Filtre |alterner |
---|
1417 | // |
---|
1418 | // Exemple [(#COMPTEUR_BOUCLE|alterner{'bleu','vert','rouge'})] |
---|
1419 | // |
---|
1420 | // https://code.spip.net/@alterner |
---|
1421 | function alterner($i) { |
---|
1422 | // recuperer les arguments (attention fonctions un peu space) |
---|
1423 | $num = func_num_args(); |
---|
1424 | $args = func_get_args(); |
---|
1425 | |
---|
1426 | if($num == 2 && is_array($args[1])) { |
---|
1427 | $args = $args[1]; |
---|
1428 | array_unshift($args,''); |
---|
1429 | $num = count($args); |
---|
1430 | } |
---|
1431 | |
---|
1432 | // renvoyer le i-ieme argument, modulo le nombre d'arguments |
---|
1433 | return $args[(intval($i)-1)%($num-1)+1]; |
---|
1434 | } |
---|
1435 | |
---|
1436 | // recuperer un attribut d'une balise html |
---|
1437 | // ($complet demande de retourner $r) |
---|
1438 | // la regexp est mortelle : cf. tests/filtres/extraire_attribut.php |
---|
1439 | // Si on a passe un tableau de balises, renvoyer un tableau de resultats |
---|
1440 | // (dans ce cas l'option $complet n'est pas disponible) |
---|
1441 | // https://code.spip.net/@extraire_attribut |
---|
1442 | function extraire_attribut($balise, $attribut, $complet = false) { |
---|
1443 | if (is_array($balise)) { |
---|
1444 | array_walk($balise, |
---|
1445 | create_function('&$a,$key,$t', |
---|
1446 | '$a = extraire_attribut($a,$t);' |
---|
1447 | ), |
---|
1448 | $attribut); |
---|
1449 | return $balise; |
---|
1450 | } |
---|
1451 | if (preg_match( |
---|
1452 | ',(^.*?<(?:(?>\s*)(?>[\w:.-]+)(?>(?:=(?:"[^"]*"|\'[^\']*\'|[^\'"]\S*))?))*?)(\s+' |
---|
1453 | .$attribut |
---|
1454 | .'(?:=\s*("[^"]*"|\'[^\']*\'|[^\'"]\S*))?)()([^>]*>.*),isS', |
---|
1455 | |
---|
1456 | $balise, $r)) { |
---|
1457 | if ($r[3][0] == '"' || $r[3][0] == "'") { |
---|
1458 | $r[4] = substr($r[3], 1, -1); |
---|
1459 | $r[3] = $r[3][0]; |
---|
1460 | } elseif ($r[3]!=='') { |
---|
1461 | $r[4] = $r[3]; |
---|
1462 | $r[3] = ''; |
---|
1463 | } else { |
---|
1464 | $r[4] = trim($r[2]); |
---|
1465 | } |
---|
1466 | $att = filtrer_entites(str_replace("'", "'", $r[4])); |
---|
1467 | } |
---|
1468 | else |
---|
1469 | $att = NULL; |
---|
1470 | |
---|
1471 | if ($complet) |
---|
1472 | return array($att, $r); |
---|
1473 | else |
---|
1474 | return $att; |
---|
1475 | } |
---|
1476 | |
---|
1477 | // modifier (ou inserer) un attribut html dans une balise |
---|
1478 | // https://code.spip.net/@inserer_attribut |
---|
1479 | function inserer_attribut($balise, $attribut, $val, $proteger=true, $vider=false) { |
---|
1480 | // preparer l'attribut |
---|
1481 | // supprimer les etc mais pas les balises html |
---|
1482 | // qui ont un sens dans un attribut value d'un input |
---|
1483 | if ($proteger) $val = attribut_html($val,false); |
---|
1484 | |
---|
1485 | // echapper les ' pour eviter tout bug |
---|
1486 | $val = str_replace("'", "'", $val); |
---|
1487 | if ($vider AND strlen($val)==0) |
---|
1488 | $insert = ''; |
---|
1489 | else |
---|
1490 | $insert = " $attribut='$val'"; |
---|
1491 | |
---|
1492 | list($old, $r) = extraire_attribut($balise, $attribut, true); |
---|
1493 | |
---|
1494 | if ($old !== NULL) { |
---|
1495 | // Remplacer l'ancien attribut du meme nom |
---|
1496 | $balise = $r[1].$insert.$r[5]; |
---|
1497 | } |
---|
1498 | else { |
---|
1499 | // preferer une balise " />" (comme <img />) |
---|
1500 | if (preg_match(',/>,', $balise)) |
---|
1501 | $balise = preg_replace(",\s?/>,S", $insert." />", $balise, 1); |
---|
1502 | // sinon une balise <a ...> ... </a> |
---|
1503 | else |
---|
1504 | $balise = preg_replace(",\s?>,S", $insert.">", $balise, 1); |
---|
1505 | } |
---|
1506 | |
---|
1507 | return $balise; |
---|
1508 | } |
---|
1509 | |
---|
1510 | // https://code.spip.net/@vider_attribut |
---|
1511 | function vider_attribut ($balise, $attribut) { |
---|
1512 | return inserer_attribut($balise, $attribut, '', false, true); |
---|
1513 | } |
---|
1514 | |
---|
1515 | |
---|
1516 | // Un filtre pour determiner le nom du mode des librement inscrits, |
---|
1517 | // a l'aide de la liste globale des statuts (tableau mode => nom du mode) |
---|
1518 | // Utile pour le formulaire d'inscription. |
---|
1519 | // Si un mode est fourni, verifier que la configuration l'accepte. |
---|
1520 | // Si mode inconnu laisser faire, c'est une extension non std |
---|
1521 | // mais verifier que la syntaxe est compatible avec SQL |
---|
1522 | |
---|
1523 | // https://code.spip.net/@tester_config |
---|
1524 | function tester_config($id, $mode='') { |
---|
1525 | |
---|
1526 | $s = array_search($mode, $GLOBALS['liste_des_statuts']); |
---|
1527 | switch ($s) { |
---|
1528 | |
---|
1529 | case 'info_redacteurs' : |
---|
1530 | return (($GLOBALS['meta']['accepter_inscriptions'] == 'oui') ? $mode : ''); |
---|
1531 | |
---|
1532 | case 'info_visiteurs' : |
---|
1533 | return (($GLOBALS['meta']['accepter_visiteurs'] == 'oui' OR $GLOBALS['meta']['forums_publics'] == 'abo') ? $mode : ''); |
---|
1534 | |
---|
1535 | default: |
---|
1536 | if ($mode AND $mode == addslashes($mode)) |
---|
1537 | return $mode; |
---|
1538 | if ($GLOBALS['meta']["accepter_inscriptions"] == "oui") |
---|
1539 | return $GLOBALS['liste_des_statuts']['info_redacteurs']; |
---|
1540 | if ($GLOBALS['meta']["accepter_visiteurs"] == "oui") |
---|
1541 | return $GLOBALS['liste_des_statuts']['info_visiteurs']; |
---|
1542 | return ''; |
---|
1543 | } |
---|
1544 | } |
---|
1545 | |
---|
1546 | // |
---|
1547 | // Un filtre qui, etant donne un #PARAMETRES_FORUM, retourne un URL de suivi rss |
---|
1548 | // dudit forum |
---|
1549 | // Attention applique a un #PARAMETRES_FORUM complexe (id_article=x&id_forum=y) |
---|
1550 | // ca retourne un url de suivi du thread y (que le thread existe ou non) |
---|
1551 | // https://code.spip.net/@url_rss_forum |
---|
1552 | function url_rss_forum($param) { |
---|
1553 | if (!preg_match(',.*(id_(\w*?))=([0-9]+),S', $param, $regs)) return ''; |
---|
1554 | list(,$k,$t,$v) = $regs; |
---|
1555 | if ($t == 'forum') $k = 'id_' . ($t = 'thread'); |
---|
1556 | return generer_url_public("rss_forum_$t", array($k => $v)); |
---|
1557 | } |
---|
1558 | |
---|
1559 | // |
---|
1560 | // Un filtre applique a #PARAMETRES_FORUM, qui donne l'adresse de la page |
---|
1561 | // de reponse |
---|
1562 | // |
---|
1563 | // https://code.spip.net/@url_reponse_forum |
---|
1564 | function url_reponse_forum($parametres) { |
---|
1565 | if (!$parametres) return ''; |
---|
1566 | return generer_url_public('forum', $parametres); |
---|
1567 | } |
---|
1568 | |
---|
1569 | // |
---|
1570 | // Quelques fonctions de calcul arithmetique |
---|
1571 | // |
---|
1572 | // https://code.spip.net/@plus |
---|
1573 | function plus($a,$b) { |
---|
1574 | return $a+$b; |
---|
1575 | } |
---|
1576 | // https://code.spip.net/@moins |
---|
1577 | function moins($a,$b) { |
---|
1578 | return $a-$b; |
---|
1579 | } |
---|
1580 | // https://code.spip.net/@mult |
---|
1581 | function mult($a,$b) { |
---|
1582 | return $a*$b; |
---|
1583 | } |
---|
1584 | // https://code.spip.net/@div |
---|
1585 | function div($a,$b) { |
---|
1586 | return $b?$a/$b:0; |
---|
1587 | } |
---|
1588 | // https://code.spip.net/@modulo |
---|
1589 | function modulo($nb, $mod, $add=0) { |
---|
1590 | return ($mod?$nb%$mod:0)+$add; |
---|
1591 | } |
---|
1592 | |
---|
1593 | |
---|
1594 | // Verifier la conformite d'une ou plusieurs adresses email |
---|
1595 | // retourne false ou la normalisation de la derniere adresse donnee |
---|
1596 | // https://code.spip.net/@email_valide |
---|
1597 | function email_valide($adresses) { |
---|
1598 | // Si c'est un spammeur autant arreter tout de suite |
---|
1599 | if (preg_match(",[\n\r].*(MIME|multipart|Content-),i", $adresses)) { |
---|
1600 | spip_log("Tentative d'injection de mail : $adresses"); |
---|
1601 | return false; |
---|
1602 | } |
---|
1603 | |
---|
1604 | foreach (explode(',', $adresses) as $v) { |
---|
1605 | // nettoyer certains formats |
---|
1606 | // "Marie Toto <Marie@toto.com>" |
---|
1607 | $adresse = trim(preg_replace(",^[^<>\"]*<([^<>\"]+)>$,i", "\\1", $v)); |
---|
1608 | // RFC 822 |
---|
1609 | if (!preg_match('#^[^()<>@,;:\\"/[:space:]]+(@([-_0-9a-z]+\.)*[-_0-9a-z]+)$#i', $adresse)) |
---|
1610 | return false; |
---|
1611 | } |
---|
1612 | return $adresse; |
---|
1613 | } |
---|
1614 | |
---|
1615 | // https://code.spip.net/@afficher_enclosures |
---|
1616 | function afficher_enclosures($tags) { |
---|
1617 | $s = array(); |
---|
1618 | foreach (extraire_balises($tags, 'a') as $tag) { |
---|
1619 | if (extraire_attribut($tag, 'rel') == 'enclosure' |
---|
1620 | AND $t = extraire_attribut($tag, 'href')) { |
---|
1621 | $s[] = preg_replace(',>[^<]+</a>,S', |
---|
1622 | '>' |
---|
1623 | .http_img_pack('attachment.gif', $t, |
---|
1624 | 'height="15" width="15" title="'.attribut_html($t).'"') |
---|
1625 | .'</a>', $tag); |
---|
1626 | } |
---|
1627 | } |
---|
1628 | return join(' ', $s); |
---|
1629 | } |
---|
1630 | // https://code.spip.net/@afficher_tags |
---|
1631 | function afficher_tags($tags, $rels='tag,directory') { |
---|
1632 | $s = array(); |
---|
1633 | foreach (extraire_balises($tags, 'a') as $tag) { |
---|
1634 | $rel = extraire_attribut($tag, 'rel'); |
---|
1635 | if (strstr(",$rels,", ",$rel,")) |
---|
1636 | $s[] = $tag; |
---|
1637 | } |
---|
1638 | return join(', ', $s); |
---|
1639 | } |
---|
1640 | |
---|
1641 | // Passe un <enclosure url="fichier" length="5588242" type="audio/mpeg"/> |
---|
1642 | // au format microformat <a rel="enclosure" href="fichier" ...>fichier</a> |
---|
1643 | // attention length="zz" devient title="zz", pour rester conforme |
---|
1644 | // https://code.spip.net/@enclosure2microformat |
---|
1645 | function enclosure2microformat($e) { |
---|
1646 | if (!$url = filtrer_entites(extraire_attribut($e, 'url'))) |
---|
1647 | $url = filtrer_entites(extraire_attribut($e, 'href')); |
---|
1648 | $type = extraire_attribut($e, 'type'); |
---|
1649 | $length = extraire_attribut($e, 'length'); |
---|
1650 | $fichier = basename($url); |
---|
1651 | return '<a rel="enclosure"' |
---|
1652 | . ($url? ' href="'.htmlspecialchars($url).'"' : '') |
---|
1653 | . ($type? ' type="'.htmlspecialchars($type).'"' : '') |
---|
1654 | . ($length? ' title="'.htmlspecialchars($length).'"' : '') |
---|
1655 | . '>'.$fichier.'</a>'; |
---|
1656 | } |
---|
1657 | // La fonction inverse |
---|
1658 | // https://code.spip.net/@microformat2enclosure |
---|
1659 | function microformat2enclosure($tags) { |
---|
1660 | $enclosures = array(); |
---|
1661 | foreach (extraire_balises($tags, 'a') as $e) |
---|
1662 | if (extraire_attribut($e, 'rel') == 'enclosure') { |
---|
1663 | $url = filtrer_entites(extraire_attribut($e, 'href')); |
---|
1664 | $type = extraire_attribut($e, 'type'); |
---|
1665 | if (!$length = intval(extraire_attribut($e, 'title'))) |
---|
1666 | $length = intval(extraire_attribut($e, 'length')); # vieux data |
---|
1667 | $fichier = basename($url); |
---|
1668 | $enclosures[] = '<enclosure' |
---|
1669 | . ($url? ' url="'.htmlspecialchars($url).'"' : '') |
---|
1670 | . ($type? ' type="'.htmlspecialchars($type).'"' : '') |
---|
1671 | . ($length? ' length="'.$length.'"' : '') |
---|
1672 | . ' />'; |
---|
1673 | } |
---|
1674 | return join("\n", $enclosures); |
---|
1675 | } |
---|
1676 | // Creer les elements ATOM <dc:subject> a partir des tags |
---|
1677 | // https://code.spip.net/@tags2dcsubject |
---|
1678 | function tags2dcsubject($tags) { |
---|
1679 | $subjects = ''; |
---|
1680 | foreach (extraire_balises($tags, 'a') as $e) { |
---|
1681 | if (extraire_attribut($e, rel) == 'tag') { |
---|
1682 | $subjects .= '<dc:subject>' |
---|
1683 | . texte_backend(textebrut($e)) |
---|
1684 | . '</dc:subject>'."\n"; |
---|
1685 | } |
---|
1686 | } |
---|
1687 | return $subjects; |
---|
1688 | } |
---|
1689 | // fabrique un bouton de type $t de Name $n, de Value $v et autres attributs $a |
---|
1690 | // https://code.spip.net/@boutonne |
---|
1691 | function boutonne($t, $n, $v, $a='') { |
---|
1692 | return "\n<input type='$t'" |
---|
1693 | . (!$n ? '' : " name='$n'") |
---|
1694 | . " value=\"$v\" $a />"; |
---|
1695 | } |
---|
1696 | |
---|
1697 | // retourne la premiere balise du type demande |
---|
1698 | // ex: [(#DESCRIPTIF|extraire_balise{img})] |
---|
1699 | // Si on a passe un tableau de textes, renvoyer un tableau de resultats |
---|
1700 | // https://code.spip.net/@extraire_balise |
---|
1701 | function extraire_balise($texte, $tag='a') { |
---|
1702 | if (is_array($texte)) { |
---|
1703 | array_walk($texte, |
---|
1704 | create_function('&$a,$key,$t', '$a = extraire_balise($a,$t);'), |
---|
1705 | $tag); |
---|
1706 | return $texte; |
---|
1707 | } |
---|
1708 | |
---|
1709 | if (preg_match( |
---|
1710 | ",<$tag\b[^>]*(/>|>.*</$tag\b[^>]*>|>),UimsS", |
---|
1711 | $texte, $regs)) |
---|
1712 | return $regs[0]; |
---|
1713 | } |
---|
1714 | |
---|
1715 | // extraire toutes les balises du type demande, sous forme de tableau |
---|
1716 | // Si on a passe un tableau de textes, renvoyer un tableau de resultats |
---|
1717 | // https://code.spip.net/@extraire_balises |
---|
1718 | function extraire_balises($texte, $tag='a') { |
---|
1719 | if (is_array($texte)) { |
---|
1720 | array_walk($texte, |
---|
1721 | create_function('&$a,$key,$t', '$a = extraire_balises($a,$t);'), |
---|
1722 | $tag); |
---|
1723 | return $texte; |
---|
1724 | } |
---|
1725 | |
---|
1726 | if (preg_match_all( |
---|
1727 | ",<${tag}\b[^>]*(/>|>.*</${tag}\b[^>]*>|>),UimsS", |
---|
1728 | $texte, $regs, PREG_PATTERN_ORDER)) |
---|
1729 | return $regs[0]; |
---|
1730 | else |
---|
1731 | return array(); |
---|
1732 | } |
---|
1733 | |
---|
1734 | // comme in_array mais renvoie son 3e arg si le 2er arg n'est pas un tableau |
---|
1735 | // prend ' ' comme representant de vrai et '' de faux |
---|
1736 | |
---|
1737 | // https://code.spip.net/@in_any |
---|
1738 | function in_any($val, $vals, $def='') { |
---|
1739 | return (!is_array($vals) ? $def : (in_array($val, $vals) ? ' ' : '')); |
---|
1740 | } |
---|
1741 | |
---|
1742 | // valeur_numerique("3*2") => 6 |
---|
1743 | // n'accepte que les *, + et - (a ameliorer si on l'utilise vraiment) |
---|
1744 | // https://code.spip.net/@valeur_numerique |
---|
1745 | function valeur_numerique($expr) { |
---|
1746 | if (preg_match(',^[0-9]+(\s*[+*-]\s*[0-9]+)*$,S', trim($expr))) |
---|
1747 | eval("\$a = $expr;"); |
---|
1748 | return intval($a); |
---|
1749 | } |
---|
1750 | |
---|
1751 | // https://code.spip.net/@regledetrois |
---|
1752 | function regledetrois($a,$b,$c) |
---|
1753 | { |
---|
1754 | return round($a*$b/$c); |
---|
1755 | } |
---|
1756 | // Fournit la suite de Input-Hidden correspondant aux parametres de |
---|
1757 | // l'URL donnee en argument, compatible avec les types_urls depuis [14447]. |
---|
1758 | // cf. tests/filtres/form_hidden.html |
---|
1759 | // https://code.spip.net/@form_hidden |
---|
1760 | function form_hidden($action) { |
---|
1761 | |
---|
1762 | $fond = ''; // inutilise mais necessaire |
---|
1763 | $contexte = array(); |
---|
1764 | if ( |
---|
1765 | ($renommer = generer_url_entite() OR $renommer = charger_fonction('page','urls')) |
---|
1766 | AND $p = $renommer($action, $fond, $contexte) |
---|
1767 | AND $p[3]) { |
---|
1768 | $contexte = $p[0]; |
---|
1769 | $contexte['page'] = $p[3]; |
---|
1770 | $action = preg_replace('/([?]'.$p[3].'[^&=]*[0-9]+)(&|$)/', '?&', $action); |
---|
1771 | } |
---|
1772 | |
---|
1773 | // on va remplir un tableau de valeurs en prenant bien soin de ne pas |
---|
1774 | // ecraser les elements de la forme mots[]=1&mots[]=2 |
---|
1775 | $values = array(); |
---|
1776 | |
---|
1777 | // d'abord avec celles de l'url |
---|
1778 | if (false !== ($p = strpos($action, '?'))) { |
---|
1779 | foreach(preg_split('/&(amp;)?/S',substr($action,$p+1)) as $c){ |
---|
1780 | list($var,$val) = explode('=', $c, 2); |
---|
1781 | if ($var) { |
---|
1782 | $val = rawurldecode($val); |
---|
1783 | $var = rawurldecode($var); // decoder les [] eventuels |
---|
1784 | if (preg_match(',\[\]$,S', $var)) |
---|
1785 | $values[] = array($var, $val); |
---|
1786 | else if (!isset($values[$var])) |
---|
1787 | $values[$var] = array($var, $val); |
---|
1788 | } |
---|
1789 | } |
---|
1790 | } |
---|
1791 | |
---|
1792 | // ensuite avec celles du contexte, sans doublonner ! |
---|
1793 | foreach($contexte as $var=>$val) |
---|
1794 | if (preg_match(',\[\]$,S', $var)) |
---|
1795 | $values[] = array($var, $val); |
---|
1796 | else if (!isset($values[$var])) |
---|
1797 | $values[$var] = array($var, $val); |
---|
1798 | |
---|
1799 | // puis on rassemble le tout |
---|
1800 | $hidden = array(); |
---|
1801 | foreach($values as $value) { |
---|
1802 | list($var,$val) = $value; |
---|
1803 | $hidden[] = '<input name="' |
---|
1804 | . entites_html($var) |
---|
1805 | .'"' |
---|
1806 | . (is_null($val) |
---|
1807 | ? '' |
---|
1808 | : ' value="'.entites_html($val).'"' |
---|
1809 | ) |
---|
1810 | . ' type="hidden" />'; |
---|
1811 | } |
---|
1812 | return join("\n", $hidden); |
---|
1813 | } |
---|
1814 | |
---|
1815 | |
---|
1816 | // https://code.spip.net/@filtre_bornes_pagination_dist |
---|
1817 | function filtre_bornes_pagination_dist($courante, $nombre, $max = 10) { |
---|
1818 | if($max<=0 OR $max>=$nombre) |
---|
1819 | return array(1, $nombre); |
---|
1820 | |
---|
1821 | $premiere = max(1, $courante-floor(($max-1)/2)); |
---|
1822 | $derniere = min($nombre, $premiere+$max-2); |
---|
1823 | $premiere = $derniere == $nombre ? $derniere-$max+1 : $premiere; |
---|
1824 | return array($premiere, $derniere); |
---|
1825 | } |
---|
1826 | |
---|
1827 | |
---|
1828 | // Ces trois fonctions permettent de simuler les filtres |reset et |end |
---|
1829 | // pour extraire la premiere ou la derniere valeur d'un tableau ; utile |
---|
1830 | // pour la pagination (mais peut-etre a refaire plus simplement) |
---|
1831 | // https://code.spip.net/@filtre_valeur_tableau |
---|
1832 | function filtre_valeur_tableau($array, $index) { |
---|
1833 | if (!is_array($array) |
---|
1834 | OR !isset($array[$index])) |
---|
1835 | return null; |
---|
1836 | return $array[$index]; |
---|
1837 | } |
---|
1838 | // https://code.spip.net/@filtre_reset |
---|
1839 | function filtre_reset($array) { |
---|
1840 | return !is_array($array) ? null : reset($array); |
---|
1841 | } |
---|
1842 | // https://code.spip.net/@filtre_end |
---|
1843 | function filtre_end($array) { |
---|
1844 | return !is_array($array) ? null : end($array); |
---|
1845 | } |
---|
1846 | |
---|
1847 | // https://code.spip.net/@filtre_push |
---|
1848 | function filtre_push($array, $val) { |
---|
1849 | if($array == '' OR !array_push($array, $val)) return ''; |
---|
1850 | return $array; |
---|
1851 | } |
---|
1852 | |
---|
1853 | // https://code.spip.net/@filtre_find |
---|
1854 | function filtre_find($array, $val) { |
---|
1855 | return (is_array($array) AND in_array($val, $array)); |
---|
1856 | } |
---|
1857 | |
---|
1858 | |
---|
1859 | // |
---|
1860 | // fonction standard de calcul de la balise #PAGINATION |
---|
1861 | // on peut la surcharger en definissant filtre_pagination dans mes_fonctions |
---|
1862 | // |
---|
1863 | |
---|
1864 | // https://code.spip.net/@filtre_pagination_dist |
---|
1865 | function filtre_pagination_dist($total, $nom, $position, $pas, $liste = true, $modele='', $connect='', $env=array()) { |
---|
1866 | static $ancres = array(); |
---|
1867 | if ($pas<1) return ''; |
---|
1868 | $ancre = 'pagination'.$nom; // #pagination_articles |
---|
1869 | $debut = 'debut'.$nom; // 'debut_articles' |
---|
1870 | |
---|
1871 | // n'afficher l'ancre qu'une fois |
---|
1872 | if (!isset($ancres[$ancre])) |
---|
1873 | $bloc_ancre = $ancres[$ancre] = "<a name='$ancre' id='$ancre'></a>"; |
---|
1874 | else $bloc_ancre = ''; |
---|
1875 | // liste = false : on ne veut que l'ancre |
---|
1876 | if (!$liste) |
---|
1877 | return $ancres[$ancre]; |
---|
1878 | |
---|
1879 | $position = min($position,$total); |
---|
1880 | $pagination = array( |
---|
1881 | 'debut' => $debut, |
---|
1882 | 'url' => parametre_url(self(),'fragment',''), // nettoyer l'id ahah eventuel |
---|
1883 | 'total' => $total, |
---|
1884 | 'position' => intval($position), |
---|
1885 | 'pas' => $pas, |
---|
1886 | 'nombre_pages' => floor(($total-1)/$pas)+1, |
---|
1887 | 'page_courante' => floor(intval($position)/$pas)+1, |
---|
1888 | 'ancre' => $ancre, |
---|
1889 | 'bloc_ancre' => $bloc_ancre |
---|
1890 | ); |
---|
1891 | if (is_array($env)) |
---|
1892 | $pagination = array_merge($env,$pagination); |
---|
1893 | |
---|
1894 | // Pas de pagination |
---|
1895 | if ($pagination['nombre_pages']<=1) |
---|
1896 | return ''; |
---|
1897 | |
---|
1898 | if ($modele) $modele = '_'.$modele; |
---|
1899 | |
---|
1900 | return recuperer_fond("modeles/pagination$modele", $pagination, array('trim'=>true), $connect); |
---|
1901 | } |
---|
1902 | |
---|
1903 | // passer les url relatives a la css d'origine en url absolues |
---|
1904 | // https://code.spip.net/@urls_absolues_css |
---|
1905 | function urls_absolues_css($contenu, $source) { |
---|
1906 | $path = suivre_lien(url_absolue($source),'./'); |
---|
1907 | |
---|
1908 | $contenu = preg_replace_callback( |
---|
1909 | ",url\s*\(\s*['\"]?([^'\"/][^:]*)['\"]?\s*\),Uims", |
---|
1910 | create_function('$x', |
---|
1911 | 'return "url(\"".suivre_lien("'.$path.'",$x[1])."\")";' |
---|
1912 | ), $contenu); |
---|
1913 | |
---|
1914 | // les directives filter ... progid:DXImageTransform.Microsoft.AlphaImageLoader(src=...,..) |
---|
1915 | // de IEx prennent des urls relatives a la page, et non a la css |
---|
1916 | // ne pas y toucher. |
---|
1917 | /* |
---|
1918 | $contenu = preg_replace_callback( |
---|
1919 | ";\(\s*src=['\"]?([^'\"/][^:]*)['\"]?\s*(,|\));Uims", |
---|
1920 | create_function('$x', |
---|
1921 | 'return "(src=\"".suivre_lien("'.$path.'",$x[1])."\"".$x[2];' |
---|
1922 | ), $contenu); |
---|
1923 | */ |
---|
1924 | return $contenu; |
---|
1925 | } |
---|
1926 | |
---|
1927 | // recuperere le chemin d'une css existante et : |
---|
1928 | // 1. regarde si une css inversee droite-gauche existe dans le meme repertoire |
---|
1929 | // 2. sinon la cree (ou la recree) dans _DIR_VAR/cache_css/ |
---|
1930 | // SI on lui donne a manger une feuille nommee _rtl.css il va faire l'inverse |
---|
1931 | // https://code.spip.net/@direction_css |
---|
1932 | function direction_css ($css, $voulue='') { |
---|
1933 | if (!preg_match(',(_rtl)?\.css$,i', $css, $r)) return $css; |
---|
1934 | |
---|
1935 | // si on a precise le sens voulu en argument, le prendre en compte |
---|
1936 | if ($voulue = strtolower($voulue)) { |
---|
1937 | if ($voulue != 'rtl' AND $voulue != 'ltr') |
---|
1938 | $voulue = lang_dir($voulue); |
---|
1939 | } |
---|
1940 | else |
---|
1941 | $voulue = lang_dir(); |
---|
1942 | |
---|
1943 | $r = count($r) > 1; |
---|
1944 | $right = $r ? 'left' : 'right'; // 'right' de la css lue en entree |
---|
1945 | $dir = $r ? 'rtl' : 'ltr'; |
---|
1946 | $ndir = $r ? 'ltr' : 'rtl'; |
---|
1947 | |
---|
1948 | if ($voulue == $dir) |
---|
1949 | return $css; |
---|
1950 | |
---|
1951 | if ( |
---|
1952 | // url absolue |
---|
1953 | preg_match(",^http:,i",$css) |
---|
1954 | // ou qui contient un ? |
---|
1955 | OR (($p=strpos($css,'?'))!==FALSE)) { |
---|
1956 | $distant = true; |
---|
1957 | $cssf = parse_url($css); |
---|
1958 | $cssf = $cssf['path'].($cssf['query']?"?".$cssf['query']:""); |
---|
1959 | $cssf = preg_replace(',[\W],', "_", $cssf); |
---|
1960 | } |
---|
1961 | else { |
---|
1962 | $distant = false; |
---|
1963 | $cssf = $css; |
---|
1964 | // 1. regarder d'abord si un fichier avec la bonne direction n'est pas aussi |
---|
1965 | //propose (rien a faire dans ce cas) |
---|
1966 | $f = preg_replace(',(_rtl)?\.css$,i', '_'.$ndir.'.css', $css); |
---|
1967 | if (@file_exists($f)) |
---|
1968 | return $f; |
---|
1969 | } |
---|
1970 | |
---|
1971 | // 2. |
---|
1972 | $dir_var = sous_repertoire (_DIR_VAR, 'cache-css'); |
---|
1973 | $f = $dir_var |
---|
1974 | . preg_replace(',.*/(.*?)(_rtl)?\.css,', '\1', $cssf) |
---|
1975 | . '.' . substr(md5($cssf), 0,4) . '_' . $ndir . '.css'; |
---|
1976 | |
---|
1977 | // la css peut etre distante (url absolue !) |
---|
1978 | if ($distant){ |
---|
1979 | include_spip('inc/distant'); |
---|
1980 | $contenu = recuperer_page($css); |
---|
1981 | if (!$contenu) return $css; |
---|
1982 | } |
---|
1983 | else { |
---|
1984 | if ((@filemtime($f) > @filemtime($css)) |
---|
1985 | AND ($GLOBALS['var_mode'] != 'recalcul')) |
---|
1986 | return $f; |
---|
1987 | if (!lire_fichier($css, $contenu)) |
---|
1988 | return $css; |
---|
1989 | } |
---|
1990 | |
---|
1991 | $contenu = str_replace( |
---|
1992 | array('right', 'left', '@@@@L E F T@@@@'), |
---|
1993 | array('@@@@L E F T@@@@', 'right', 'left'), |
---|
1994 | $contenu); |
---|
1995 | |
---|
1996 | // reperer les @import auxquels il faut propager le direction_css |
---|
1997 | preg_match_all(",\@import\s*url\s*\(\s*['\"]?([^'\"/][^:]*)['\"]?\s*\),Uims",$contenu,$regs); |
---|
1998 | $src = array();$src_direction_css = array();$src_faux_abs=array(); |
---|
1999 | $d = dirname($css); |
---|
2000 | foreach($regs[1] as $k=>$import_css){ |
---|
2001 | $css_direction = direction_css("$d/$import_css",$voulue); |
---|
2002 | // si la css_direction est dans le meme path que la css d'origine, on tronque le path, elle sera passee en absolue |
---|
2003 | if (substr($css_direction,0,strlen($d)+1)=="$d/") $css_direction = substr($css_direction,strlen($d)+1); |
---|
2004 | // si la css_direction commence par $dir_var on la fait passer pour une absolue |
---|
2005 | elseif (substr($css_direction,0,strlen($dir_var))==$dir_var) { |
---|
2006 | $css_direction = substr($css_direction,strlen($dir_var)); |
---|
2007 | $src_faux_abs["/@@@@@@/".$css_direction] = $css_direction; |
---|
2008 | $css_direction = "/@@@@@@/".$css_direction; |
---|
2009 | } |
---|
2010 | $src[] = $regs[0][$k]; |
---|
2011 | $src_direction_css[] = str_replace($import_css,$css_direction,$regs[0][$k]); |
---|
2012 | } |
---|
2013 | $contenu = str_replace($src,$src_direction_css,$contenu); |
---|
2014 | |
---|
2015 | $contenu = urls_absolues_css($contenu, $css); |
---|
2016 | |
---|
2017 | // virer les fausses url absolues que l'on a mis dans les import |
---|
2018 | if (count($src_faux_abs)) |
---|
2019 | $contenu = str_replace(array_keys($src_faux_abs),$src_faux_abs,$contenu); |
---|
2020 | |
---|
2021 | if (!ecrire_fichier($f, $contenu)) |
---|
2022 | return $css; |
---|
2023 | |
---|
2024 | return $f; |
---|
2025 | } |
---|
2026 | |
---|
2027 | // recuperere le chemin d'une css existante et : |
---|
2028 | // cree (ou recree) dans _DIR_VAR/cache_css/ une css dont les url relatives sont passees en url absolues |
---|
2029 | // https://code.spip.net/@url_absolue_css |
---|
2030 | function url_absolue_css ($css) { |
---|
2031 | if (!preg_match(',\.css$,i', $css, $r)) return $css; |
---|
2032 | |
---|
2033 | $url_absolue_css = url_absolue($css); |
---|
2034 | |
---|
2035 | $f = basename($css,'.css'); |
---|
2036 | $f = sous_repertoire (_DIR_VAR, 'cache-css') |
---|
2037 | . preg_replace(",(.*?)(_rtl|_ltr)?$,","\\1-urlabs-" . substr(md5("$css-urlabs"), 0,4) . "\\2",$f) |
---|
2038 | . '.css'; |
---|
2039 | |
---|
2040 | if ((@filemtime($f) > @filemtime($css)) |
---|
2041 | AND ($GLOBALS['var_mode'] != 'recalcul')) |
---|
2042 | return $f; |
---|
2043 | |
---|
2044 | if ($url_absolue_css==$css){ |
---|
2045 | if (strncmp($GLOBALS['meta']['adresse_site'],$css,$l=strlen($GLOBALS['meta']['adresse_site']))!=0 |
---|
2046 | OR !lire_fichier(_DIR_RACINE . substr($css,$l), $contenu)){ |
---|
2047 | include_spip('inc/distant'); |
---|
2048 | if (!$contenu = recuperer_page($css)) |
---|
2049 | return $css; |
---|
2050 | } |
---|
2051 | } |
---|
2052 | elseif (!lire_fichier($css, $contenu)) |
---|
2053 | return $css; |
---|
2054 | |
---|
2055 | // passer les url relatives a la css d'origine en url absolues |
---|
2056 | $contenu = urls_absolues_css($contenu, $css); |
---|
2057 | |
---|
2058 | // ecrire la css |
---|
2059 | if (!ecrire_fichier($f, $contenu)) |
---|
2060 | return $css; |
---|
2061 | |
---|
2062 | return $f; |
---|
2063 | } |
---|
2064 | |
---|
2065 | |
---|
2066 | // filtre table_valeur |
---|
2067 | // permet de recuperer la valeur d'un tableau pour une cle donnee |
---|
2068 | // prend en entree un tableau serialise ou non (ce qui permet d'enchainer le filtre) |
---|
2069 | // https://code.spip.net/@table_valeur |
---|
2070 | function table_valeur($table,$cle,$defaut=''){ |
---|
2071 | $table= is_string($table)?unserialize($table):$table; |
---|
2072 | $table= is_array($table)?$table:array(); |
---|
2073 | return isset($table[$cle])?$table[$cle]:$defaut; |
---|
2074 | } |
---|
2075 | |
---|
2076 | // filtre match pour faire des tests avec expression reguliere |
---|
2077 | // [(#TEXTE|match{^ceci$,Uims})] |
---|
2078 | // retourne le fragment de chaine qui "matche" |
---|
2079 | // il est possible de passer en 3eme argument optionnel le numero de parenthese capturante |
---|
2080 | // accepte egalement la syntaxe #TRUC|match{truc(...)$,1} ou le modificateur n'est pas passe en second argument |
---|
2081 | // https://code.spip.net/@match |
---|
2082 | function match($texte, $expression, $modif="UimsS",$capte=0) { |
---|
2083 | if (intval($modif) AND $capte==0){ |
---|
2084 | $capte = $modif; |
---|
2085 | $modif = "UimsS"; |
---|
2086 | } |
---|
2087 | $expression=str_replace("\/","/",$expression); |
---|
2088 | $expression=str_replace("/","\/",$expression); |
---|
2089 | |
---|
2090 | if (preg_match('/' . $expression . '/' . $modif,$texte, $r)) { |
---|
2091 | if (isset($r[$capte])) |
---|
2092 | return $r[$capte]; |
---|
2093 | else |
---|
2094 | return true; |
---|
2095 | } |
---|
2096 | return false; |
---|
2097 | } |
---|
2098 | |
---|
2099 | // filtre replace pour faire des operations avec expression reguliere |
---|
2100 | // [(#TEXTE|replace{^ceci$,cela,UimsS})] |
---|
2101 | // https://code.spip.net/@replace |
---|
2102 | function replace($texte, $expression, $replace='', $modif="UimsS") { |
---|
2103 | $expression=str_replace("\/","/", $expression); |
---|
2104 | $expression=str_replace("/","\/",$expression); |
---|
2105 | return preg_replace('/' . $expression . '/' . $modif, $replace, $texte); |
---|
2106 | } |
---|
2107 | |
---|
2108 | |
---|
2109 | // cherche les documents numerotes dans un texte traite par propre() |
---|
2110 | // et affecte les doublons['documents'] |
---|
2111 | // https://code.spip.net/@traiter_doublons_documents |
---|
2112 | // https://code.spip.net/@traiter_doublons_documents |
---|
2113 | function traiter_doublons_documents(&$doublons, $letexte) { |
---|
2114 | |
---|
2115 | // Verifier dans le texte & les notes (pas beau, helas) |
---|
2116 | $t = $letexte.$GLOBALS['les_notes']; |
---|
2117 | |
---|
2118 | if (strstr($t, 'spip_document_') // evite le preg_match_all si inutile |
---|
2119 | AND preg_match_all( |
---|
2120 | ',<[^>]+\sclass=["\']spip_document_([0-9]+)[\s"\'],imsS', |
---|
2121 | $t, $matches, PREG_PATTERN_ORDER)) |
---|
2122 | $doublons['documents'] .= "," . join(',', $matches[1]); |
---|
2123 | |
---|
2124 | return $letexte; |
---|
2125 | } |
---|
2126 | |
---|
2127 | // filtre vide qui ne renvoie rien |
---|
2128 | // https://code.spip.net/@vide |
---|
2129 | function vide($texte){ |
---|
2130 | return ""; |
---|
2131 | } |
---|
2132 | |
---|
2133 | // |
---|
2134 | // Filtres pour le modele/emb (embed document) |
---|
2135 | // |
---|
2136 | |
---|
2137 | // A partir d'un #ENV, retourne des <param ...> |
---|
2138 | // https://code.spip.net/@env_to_params |
---|
2139 | function env_to_params ($texte, $ignore_params=array()) { |
---|
2140 | $ignore_params = array_merge ( |
---|
2141 | array('id', 'lang', 'id_document', 'date', 'date_redac', 'align', 'fond', '', 'recurs', 'emb', 'dir_racine'), |
---|
2142 | $ignore_params); |
---|
2143 | $tableau = unserialize($texte); |
---|
2144 | $texte = ""; |
---|
2145 | foreach ($tableau as $i => $j) |
---|
2146 | if (is_string($j) AND !in_array($i,$ignore_params)) |
---|
2147 | $texte .= "<param name='".$i."'\n\tvalue='".$j."' />"; |
---|
2148 | return $texte; |
---|
2149 | } |
---|
2150 | // A partir d'un #ENV, retourne des attributs |
---|
2151 | // https://code.spip.net/@env_to_attributs |
---|
2152 | function env_to_attributs ($texte, $ignore_params=array()) { |
---|
2153 | $ignore_params = array_merge ( |
---|
2154 | array('id', 'lang', 'id_document', 'date', 'date_redac', 'align', 'fond', '', 'recurs', 'emb', 'dir_racine'), |
---|
2155 | $ignore_params); |
---|
2156 | $tableau = unserialize($texte); |
---|
2157 | $texte = ""; |
---|
2158 | foreach ($tableau as $i => $j) |
---|
2159 | if (is_string($j) AND !in_array($i,$ignore_params)) |
---|
2160 | $texte .= $i."='".$j."' "; |
---|
2161 | return $texte; |
---|
2162 | } |
---|
2163 | |
---|
2164 | // Inserer jQuery |
---|
2165 | // et au passage verifier qu'on ne doublonne pas #INSERT_HEAD |
---|
2166 | // https://code.spip.net/@f_jQuery |
---|
2167 | function f_jQuery ($texte) { |
---|
2168 | static $doublon=0; |
---|
2169 | if ($doublon++) { |
---|
2170 | erreur_squelette(array('double_occurrence', array('balise' => "INSERT_HEAD"))); |
---|
2171 | } else { |
---|
2172 | $x = ''; |
---|
2173 | foreach (array_unique(pipeline('jquery_plugins', |
---|
2174 | array( |
---|
2175 | 'javascript/jquery.js', |
---|
2176 | 'javascript/jquery.form.js', |
---|
2177 | 'javascript/ajaxCallback.js', |
---|
2178 | 'javascript/jquery.cookie.js' |
---|
2179 | ))) as $script) |
---|
2180 | if ($script = find_in_path($script)) |
---|
2181 | $x .= "\n<script src=\"$script\" type=\"text/javascript\"></script>\n"; |
---|
2182 | $texte = $x.$texte; |
---|
2183 | } |
---|
2184 | return $texte; |
---|
2185 | } |
---|
2186 | |
---|
2187 | /** |
---|
2188 | * fonction appelee par #INSERT_HEAD_CSS et #INSERT_HEAD pour la compatibilite |
---|
2189 | * @staticvar <type> $done |
---|
2190 | * @return <type> |
---|
2191 | */ |
---|
2192 | function insert_head_css(){ |
---|
2193 | static $done = false; |
---|
2194 | if ($done) return ''; |
---|
2195 | $done = true; |
---|
2196 | return pipeline('insert_head_css',''); |
---|
2197 | } |
---|
2198 | |
---|
2199 | |
---|
2200 | // Concatener des chaines |
---|
2201 | // #TEXTE|concat{texte1,texte2,...} |
---|
2202 | // https://code.spip.net/@concat |
---|
2203 | function concat(){ |
---|
2204 | $args = func_get_args(); |
---|
2205 | return join('', $args); |
---|
2206 | } |
---|
2207 | |
---|
2208 | |
---|
2209 | // https://code.spip.net/@charge_scripts |
---|
2210 | function charge_scripts($scripts) { |
---|
2211 | $flux = ""; |
---|
2212 | $args = is_array($scripts)?$scripts:explode("|",$scripts); |
---|
2213 | foreach($args as $script) { |
---|
2214 | if(preg_match(",^\w+$,",$script)) { |
---|
2215 | $path = find_in_path("javascript/$script.js"); |
---|
2216 | if($path) $flux .= spip_file_get_contents($path); |
---|
2217 | } |
---|
2218 | } |
---|
2219 | return $flux; |
---|
2220 | } |
---|
2221 | |
---|
2222 | |
---|
2223 | |
---|
2224 | |
---|
2225 | // produit une balise img avec un champ alt d'office si vide |
---|
2226 | // attention le htmlentities et la traduction doivent etre appliques avant. |
---|
2227 | |
---|
2228 | // https://code.spip.net/@http_wrapper |
---|
2229 | function http_wrapper($img){ |
---|
2230 | if (strpos($img,'/')===FALSE) // on ne prefixe par _NOM_IMG_PACK que si c'est un nom de fichier sans chemin |
---|
2231 | $f = chemin_image($img); |
---|
2232 | else { // sinon, le path a ete fourni |
---|
2233 | $f = $img; |
---|
2234 | } |
---|
2235 | return $f; |
---|
2236 | } |
---|
2237 | |
---|
2238 | // https://code.spip.net/@http_img_pack |
---|
2239 | function http_img_pack($img, $alt, $atts='', $title='') { |
---|
2240 | |
---|
2241 | $img = http_wrapper($img); |
---|
2242 | if (strpos($atts, 'width')===FALSE){ |
---|
2243 | // utiliser directement l'info de taille presente dans le nom |
---|
2244 | if (preg_match(',-([0-9]+)[.](png|gif)$,',$img,$regs)){ |
---|
2245 | $size = array(intval($regs[1]),intval($regs[1])); |
---|
2246 | } |
---|
2247 | else |
---|
2248 | $size = @getimagesize($img); |
---|
2249 | $atts.=" width='".$size[0]."' height='".$size[1]."'"; |
---|
2250 | } |
---|
2251 | return "<img src='" . $img |
---|
2252 | . ("'\nalt=\"" . |
---|
2253 | str_replace('"','', textebrut($alt ? $alt : ($title ? $title : ''))) |
---|
2254 | . '" ') |
---|
2255 | . ($title ? "title=\"$title\" " : '') |
---|
2256 | . $atts |
---|
2257 | . " />"; |
---|
2258 | } |
---|
2259 | |
---|
2260 | // https://code.spip.net/@http_style_background |
---|
2261 | function http_style_background($img, $att='') |
---|
2262 | { |
---|
2263 | return " style='background: url(\"".http_wrapper($img)."\")" . |
---|
2264 | ($att ? (' ' . $att) : '') . ";'"; |
---|
2265 | } |
---|
2266 | |
---|
2267 | //[(#ENV*|unserialize|foreach)] |
---|
2268 | // https://code.spip.net/@filtre_foreach_dist |
---|
2269 | function filtre_foreach_dist($balise_deserializee, $modele = 'foreach') { |
---|
2270 | $texte = ''; |
---|
2271 | if(is_array($balise_deserializee)) |
---|
2272 | foreach($balise_deserializee as $k => $v) { |
---|
2273 | $res = recuperer_fond('modeles/'.$modele, |
---|
2274 | array_merge(array('cle' => $k), (is_array($v) ? $v : array('valeur' => $v))) |
---|
2275 | ); |
---|
2276 | $texte .= $res; |
---|
2277 | } |
---|
2278 | return $texte; |
---|
2279 | } |
---|
2280 | |
---|
2281 | // renvoie la liste des plugins actifs du site |
---|
2282 | // si le premier parametre est un prefix de cette liste, renvoie vrai, faux sinon |
---|
2283 | // la valeur du second parametre si celui-ci renvoie a une information connue |
---|
2284 | // cf liste_plugin_actifs() pour connaitre les informations affichables |
---|
2285 | // appelee par la balise #PLUGIN |
---|
2286 | // https://code.spip.net/@filtre_info_plugin_dist |
---|
2287 | function filtre_info_plugin_dist($plugin, $type_info) { |
---|
2288 | include_spip('inc/plugin'); |
---|
2289 | $plugin = strtoupper($plugin); |
---|
2290 | $plugins_actifs = liste_plugin_actifs(); |
---|
2291 | |
---|
2292 | if (!$plugin) |
---|
2293 | return serialize(array_keys($plugins_actifs)); |
---|
2294 | elseif (empty($plugins_actifs[$plugin])) |
---|
2295 | return ''; |
---|
2296 | elseif ($type_info == 'est_actif') |
---|
2297 | return $plugins_actifs[$plugin] ? 1 : 0; |
---|
2298 | elseif (isset($plugins_actifs[$plugin][$type_info])) |
---|
2299 | return $plugins_actifs[$plugin][$type_info]; |
---|
2300 | else { |
---|
2301 | $get_infos = charger_fonction('get_infos','plugins'); |
---|
2302 | if (!$infos = $get_infos($plugins_actifs[$plugin]['dir'])) |
---|
2303 | return ''; |
---|
2304 | if ($type_info == 'tout') |
---|
2305 | return $infos; |
---|
2306 | else |
---|
2307 | return strval($infos[$type_info]); |
---|
2308 | } |
---|
2309 | } |
---|
2310 | |
---|
2311 | |
---|
2312 | // https://code.spip.net/@chercher_rubrique |
---|
2313 | function chercher_rubrique($msg,$id, $id_parent, $type, $id_secteur, $restreint,$actionable = false, $retour_sans_cadre=false){ |
---|
2314 | global $spip_lang_right; |
---|
2315 | include_spip('inc/autoriser'); |
---|
2316 | if (intval($id) && !autoriser('modifier', $type, $id)) |
---|
2317 | return ""; |
---|
2318 | if (!sql_countsel('spip_rubriques')) |
---|
2319 | return ""; |
---|
2320 | $chercher_rubrique = charger_fonction('chercher_rubrique', 'inc'); |
---|
2321 | $form = $chercher_rubrique($id_parent, $type, $restreint, ($type=='rubrique')?$id:0); |
---|
2322 | |
---|
2323 | if ($id_parent == 0) $logo = "racine-site-24.gif"; |
---|
2324 | elseif ($id_secteur == $id_parent) $logo = "secteur-24.gif"; |
---|
2325 | else $logo = "rubrique-24.gif"; |
---|
2326 | |
---|
2327 | $confirm = ""; |
---|
2328 | if ($type=='rubrique') { |
---|
2329 | // si c'est une rubrique-secteur contenant des breves, demander la |
---|
2330 | // confirmation du deplacement |
---|
2331 | $contient_breves = sql_countsel('spip_breves', "id_rubrique=$id"); |
---|
2332 | |
---|
2333 | if ($contient_breves > 0) { |
---|
2334 | $scb = ($contient_breves>1? 's':''); |
---|
2335 | $scb = _T('avis_deplacement_rubrique', |
---|
2336 | array('contient_breves' => $contient_breves, |
---|
2337 | 'scb' => $scb)); |
---|
2338 | $confirm .= "\n<div class='confirmer_deplacement verdana2'><div class='choix'><input type='checkbox' name='confirme_deplace' value='oui' id='confirme-deplace' /><label for='confirme-deplace'>" . $scb . "</label></div></div>\n"; |
---|
2339 | } else |
---|
2340 | $confirm .= "<input type='hidden' name='confirme_deplace' value='oui' />\n"; |
---|
2341 | } |
---|
2342 | $form .= $confirm; |
---|
2343 | if ($actionable){ |
---|
2344 | if (strpos($form,'<select')!==false) { |
---|
2345 | $form .= "<div style='text-align: $spip_lang_right;'>" |
---|
2346 | . '<input type="submit" value="'._T('bouton_choisir').'"/>' |
---|
2347 | . "</div>"; |
---|
2348 | } |
---|
2349 | $form = "<input type='hidden' name='editer_$type' value='oui' />\n" . $form; |
---|
2350 | $form = generer_action_auteur("editer_$type", $id, self(), $form, " method='post' class='submit_plongeur'"); |
---|
2351 | } |
---|
2352 | |
---|
2353 | if ($retour_sans_cadre) |
---|
2354 | return $form; |
---|
2355 | |
---|
2356 | include_spip('inc/presentation'); |
---|
2357 | return debut_cadre_couleur($logo, true, "", $msg) . $form .fin_cadre_couleur(true); |
---|
2358 | |
---|
2359 | } |
---|
2360 | |
---|
2361 | |
---|
2362 | // https://code.spip.net/@puce_changement_statut |
---|
2363 | function puce_changement_statut($id_objet, $statut, $id_rubrique, $type, $ajax=false){ |
---|
2364 | $puce_statut = charger_fonction('puce_statut','inc'); |
---|
2365 | return $puce_statut($id_objet, $statut, $id_rubrique, $type, $ajax=false); |
---|
2366 | } |
---|
2367 | |
---|
2368 | // Encoder un contexte pour l'ajax, le signer avec une cle, le crypter |
---|
2369 | // avec le secret du site, le gziper si possible... |
---|
2370 | // l'entree peut etre serialisee (le #ENV** des fonds ajax et ajax_stat) |
---|
2371 | // https://code.spip.net/@encoder_contexte_ajax |
---|
2372 | function encoder_contexte_ajax($c,$form='', $emboite=NULL) { |
---|
2373 | if (is_string($c) |
---|
2374 | AND !is_null(@unserialize($c))) |
---|
2375 | $c = unserialize($c); |
---|
2376 | |
---|
2377 | // supprimer les parametres debut_x |
---|
2378 | // pour que la pagination ajax ne soit pas plantee |
---|
2379 | // si on charge la page &debut_x=1 : car alors en cliquant sur l'item 0, |
---|
2380 | // le debut_x=0 n'existe pas, et on resterait sur 1 |
---|
2381 | foreach ($c as $k => $v) |
---|
2382 | if (strpos($k,'debut_') === 0) |
---|
2383 | unset($c[$k]); |
---|
2384 | |
---|
2385 | include_spip("inc/securiser_action"); |
---|
2386 | $cle = calculer_cle_action($form.(is_array($c)?serialize($c):$c)); |
---|
2387 | $c = serialize(array($c,$cle)); |
---|
2388 | |
---|
2389 | if ((defined('_CACHE_CONTEXTES_AJAX') AND _CACHE_CONTEXTES_AJAX) |
---|
2390 | AND $dir = sous_repertoire(_DIR_CACHE, 'contextes')) { |
---|
2391 | // stocker les contextes sur disque et ne passer qu'un hash dans l'url |
---|
2392 | $md5 = md5($c); |
---|
2393 | ecrire_fichier("$dir/c$md5",$c); |
---|
2394 | $c = $md5; |
---|
2395 | } else { |
---|
2396 | if (function_exists('gzdeflate') && function_exists('gzinflate')) |
---|
2397 | $c = gzdeflate($c); |
---|
2398 | $c = _xor($c); |
---|
2399 | $c = base64_encode($c); |
---|
2400 | } |
---|
2401 | |
---|
2402 | if ($emboite === NULL) return $c; |
---|
2403 | return !trim($emboite) ? '' : |
---|
2404 | "<div class='ajaxbloc env-$c'>\n$emboite</div><!-- ajaxbloc -->\n"; |
---|
2405 | } |
---|
2406 | |
---|
2407 | // la procedure inverse de encoder_contexte_ajax() |
---|
2408 | // https://code.spip.net/@decoder_contexte_ajax |
---|
2409 | function decoder_contexte_ajax($c,$form='') { |
---|
2410 | include_spip("inc/securiser_action"); |
---|
2411 | if (( (defined('_CACHE_CONTEXTES_AJAX') AND _CACHE_CONTEXTES_AJAX) OR strlen($c)==32) |
---|
2412 | AND $dir = sous_repertoire(_DIR_CACHE, 'contextes') |
---|
2413 | AND lire_fichier("$dir/c$c",$contexte)) { |
---|
2414 | $c = $contexte; |
---|
2415 | } else { |
---|
2416 | $c = @base64_decode($c); |
---|
2417 | $c = _xor($c); |
---|
2418 | if (function_exists('gzdeflate') && function_exists('gzinflate')) |
---|
2419 | $c = @gzinflate($c); |
---|
2420 | } |
---|
2421 | list($env, $cle) = @unserialize($c); |
---|
2422 | |
---|
2423 | if ($cle == calculer_cle_action($form.(is_array($env)?serialize($env):$env))) |
---|
2424 | return $env; |
---|
2425 | return false; |
---|
2426 | } |
---|
2427 | |
---|
2428 | // encrypter/decrypter un message |
---|
2429 | // http://www.php.net/manual/fr/language.operators.bitwise.php#81358 |
---|
2430 | // https://code.spip.net/@_xor |
---|
2431 | function _xor($message, $key=null){ |
---|
2432 | if (is_null($key)) { |
---|
2433 | include_spip("inc/securiser_action"); |
---|
2434 | $key = pack("H*", calculer_cle_action('_xor')); |
---|
2435 | } |
---|
2436 | |
---|
2437 | $keylen = strlen($key); |
---|
2438 | $messagelen = strlen($message); |
---|
2439 | for($i=0; $i<$messagelen; $i++) |
---|
2440 | $message[$i] = ~($message[$i]^$key[$i%$keylen]); |
---|
2441 | |
---|
2442 | return $message; |
---|
2443 | } |
---|
2444 | |
---|
2445 | |
---|
2446 | |
---|
2447 | /** |
---|
2448 | * une fonction pour generer des menus avec liens |
---|
2449 | * ou un <strong class='on'> non clicable lorsque l'item est selectionne |
---|
2450 | * |
---|
2451 | * @param string $url |
---|
2452 | * @param string $libelle |
---|
2453 | * le texte du lien |
---|
2454 | * @param bool $on |
---|
2455 | * etat expose (genere un strong) ou non (genere un lien) |
---|
2456 | * @param string $class |
---|
2457 | * @param string $title |
---|
2458 | * @param string $rel |
---|
2459 | * @param string $evt |
---|
2460 | * complement a la balise a pour gerer un evenement javascript, de la forme " onclick='...'" |
---|
2461 | * @return string |
---|
2462 | */ |
---|
2463 | function lien_ou_expose($url,$libelle,$on=false,$class="",$title="",$rel="", $evt=''){ |
---|
2464 | if ($on) { |
---|
2465 | $bal = "strong"; |
---|
2466 | $att = "class='on'"; |
---|
2467 | } else { |
---|
2468 | $bal = 'a'; |
---|
2469 | $att = "href='$url'" |
---|
2470 | .($title?" title='".attribut_html($title)."'":'') |
---|
2471 | .($class?" class='".attribut_html($class)."'":'') |
---|
2472 | .($rel?" rel='".attribut_html($rel)."'":'') |
---|
2473 | .$evt; |
---|
2474 | } |
---|
2475 | |
---|
2476 | return "<$bal $att>$libelle</$bal>"; |
---|
2477 | } |
---|
2478 | |
---|
2479 | |
---|
2480 | /** |
---|
2481 | * une fonction pour generer une balise img a partir d'un nom de fichier |
---|
2482 | * |
---|
2483 | * @param string $img |
---|
2484 | * @param string $alt |
---|
2485 | * @param string $class |
---|
2486 | * @return string |
---|
2487 | */ |
---|
2488 | function filtre_balise_img_dist($img,$alt="",$class=""){ |
---|
2489 | $taille = taille_image($img); |
---|
2490 | list($hauteur,$largeur) = $taille; |
---|
2491 | if (!$hauteur OR !$largeur) |
---|
2492 | return ""; |
---|
2493 | return |
---|
2494 | "<img src='$img' width='$largeur' height='$hauteur'" |
---|
2495 | ." alt='".attribut_html($alt)."'" |
---|
2496 | .($class?" class='".attribut_html($class)."'":'') |
---|
2497 | .' />'; |
---|
2498 | } |
---|
2499 | |
---|
2500 | |
---|
2501 | /** |
---|
2502 | * Afficher un message "un truc"/"N trucs" |
---|
2503 | * |
---|
2504 | * @param int $nb |
---|
2505 | * @return string |
---|
2506 | */ |
---|
2507 | function singulier_ou_pluriel($nb,$chaine_un,$chaine_plusieurs,$var='nb'){ |
---|
2508 | if (!$nb=intval($nb)) return ""; |
---|
2509 | if ($nb>1) return _T($chaine_plusieurs, array($var => $nb)); |
---|
2510 | else return _T($chaine_un); |
---|
2511 | } |
---|
2512 | |
---|
2513 | |
---|
2514 | /** |
---|
2515 | * un filtre icone mappe sur icone_inline, qui cree une icone a gauche par defaut |
---|
2516 | * le code de icone_inline est grandement reproduit ici car les liens ajax portent simplement une class ajax |
---|
2517 | * lorsque les interfaces sont en squelette, alors que l'implementation d'ajax de des scripts php |
---|
2518 | * est plus complexe |
---|
2519 | * |
---|
2520 | * @param string $lien |
---|
2521 | * @param string $texte |
---|
2522 | * @param string $fond |
---|
2523 | * @param string $align |
---|
2524 | * @param string $fonction |
---|
2525 | * @return string |
---|
2526 | */ |
---|
2527 | function filtre_icone_dist($lien, $texte, $fond, $align="", $fonction="", $class="",$javascript=""){ |
---|
2528 | if ($icone_renommer = charger_fonction('icone_renommer','inc',true)) |
---|
2529 | list($fond,$fonction) = $icone_renommer($fond,$fonction); |
---|
2530 | |
---|
2531 | $align = $align?$align:$GLOBALS['spip_lang_left']; |
---|
2532 | global $spip_display; |
---|
2533 | |
---|
2534 | if ($fonction == "del") { |
---|
2535 | $style = 'icone36 danger'; |
---|
2536 | } else { |
---|
2537 | $style = 'icone36'; |
---|
2538 | if (strlen($fonction) < 3) $fonction = "rien.gif"; |
---|
2539 | } |
---|
2540 | $style .= " " . substr(basename($fond),0,-4); |
---|
2541 | |
---|
2542 | if ($spip_display == 1){ |
---|
2543 | $hauteur = 20; |
---|
2544 | $largeur = 100; |
---|
2545 | $title = $alt = ""; |
---|
2546 | } |
---|
2547 | else if ($spip_display == 3){ |
---|
2548 | $hauteur = 30; |
---|
2549 | $largeur = 30; |
---|
2550 | $title = "\ntitle=\"$texte\""; |
---|
2551 | $alt = $texte; |
---|
2552 | } |
---|
2553 | else { |
---|
2554 | $hauteur = 70; |
---|
2555 | $largeur = 100; |
---|
2556 | $title = ''; |
---|
2557 | $alt = $texte; |
---|
2558 | } |
---|
2559 | |
---|
2560 | $size = 24; |
---|
2561 | if (preg_match("/-([0-9]{1,3})[.](gif|png)$/i",$fond,$match)) |
---|
2562 | $size = $match[1]; |
---|
2563 | if ($spip_display != 1 AND $spip_display != 4){ |
---|
2564 | if ($fonction != "rien.gif"){ |
---|
2565 | $icone = http_img_pack($fonction, $alt, "$title width='$size' height='$size'\n" . |
---|
2566 | http_style_background($fond, "no-repeat center center")); |
---|
2567 | } |
---|
2568 | else { |
---|
2569 | $icone = http_img_pack($fond, $alt, "$title width='$size' height='$size'"); |
---|
2570 | } |
---|
2571 | } else $icone = ''; |
---|
2572 | |
---|
2573 | // cas d'ajax_action_auteur: faut defaire le boulot |
---|
2574 | // (il faudrait fusionner avec le cas $javascript) |
---|
2575 | if (preg_match(",^<a\shref='([^']*)'([^>]*)>(.*)</a>$,i",$lien,$r)) |
---|
2576 | list($x,$lien,$atts,$texte)= $r; |
---|
2577 | else $atts = ''; |
---|
2578 | |
---|
2579 | if ($align && $align!='center') $align = "float: $align; "; |
---|
2580 | |
---|
2581 | $icone = "<a style='$align' class='$style $class'" |
---|
2582 | . $atts |
---|
2583 | . $javascript |
---|
2584 | . "\nhref='" |
---|
2585 | . $lien |
---|
2586 | . "'>" |
---|
2587 | . $icone |
---|
2588 | . (($spip_display == 3) ? '' : "<span>$texte</span>") |
---|
2589 | . "</a>\n"; |
---|
2590 | |
---|
2591 | if ($align <> 'center') return $icone; |
---|
2592 | $style = " style='text-align:center;'"; |
---|
2593 | return "<div$style>$icone</div>"; |
---|
2594 | } |
---|
2595 | |
---|
2596 | |
---|
2597 | /** |
---|
2598 | * filtre explode pour les squelettes permettant d'ecrire |
---|
2599 | * #GET{truc}|explode{-} |
---|
2600 | * |
---|
2601 | * @param strong $a |
---|
2602 | * @param string $b |
---|
2603 | * @return array |
---|
2604 | */ |
---|
2605 | function filtre_explode_dist($a,$b){return explode($b,$a);} |
---|
2606 | |
---|
2607 | /** |
---|
2608 | * filtre implode pour les squelettes permettant d'ecrire |
---|
2609 | * #GET{truc}|implode{-} |
---|
2610 | * |
---|
2611 | * @param array $a |
---|
2612 | * @param string $b |
---|
2613 | * @return string |
---|
2614 | */ |
---|
2615 | function filtre_implode_dist($a,$b){return is_array($a)?implode($b,$a):$a;} |
---|
2616 | |
---|
2617 | /* |
---|
2618 | * Deux verrues pour que le pipeline de revisions soit correct |
---|
2619 | * elles vont sauter quand ca passera en plugin |
---|
2620 | */ |
---|
2621 | function premiere_revision($x) { |
---|
2622 | include_spip('inc/revisions'); |
---|
2623 | return enregistrer_premiere_revision($x); |
---|
2624 | } |
---|
2625 | function nouvelle_revision($x) { |
---|
2626 | include_spip('inc/revisions'); |
---|
2627 | return enregistrer_nouvelle_revision($x); |
---|
2628 | } |
---|
2629 | |
---|
2630 | /** |
---|
2631 | * Generer un bouton_action |
---|
2632 | * utilise par #BOUTON_ACTION |
---|
2633 | * |
---|
2634 | * @param string $libelle |
---|
2635 | * @param string $url |
---|
2636 | * @param string $class |
---|
2637 | * @param string $confirm |
---|
2638 | * @param string $title |
---|
2639 | * @return string |
---|
2640 | */ |
---|
2641 | function bouton_action($libelle, $url, $class="", $confirm="", $title=""){ |
---|
2642 | $onclick = $confirm?" onclick='return confirm(\"" . attribut_html($confirm) . "\");'":""; |
---|
2643 | $title = $title ? " title='$title'" : ""; |
---|
2644 | |
---|
2645 | return "<form class='bouton_action_post $class' method='post' action='$url'><div>".form_hidden($url) |
---|
2646 | ."<button type='submit' class='submit'$title$onclick>$libelle</button></div></form>"; |
---|
2647 | } |
---|
2648 | |
---|
2649 | |
---|
2650 | ?> |
---|