[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / ecrire / inc / filtres_mini.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
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
14 if (!defined('_ECRIRE_INC_VERSION')) return;
15
16 //
17 // Filtres d'URLs
18 //
19
20 // Nettoyer une URL contenant des ../
21 //
22 // resolve_url('/.././/truc/chose/machin/./.././.././hopla/..');
23 // inspire (de loin) par PEAR:NetURL:resolvePath
24 //
25 // http://doc.spip.org/@resolve_path
26 function resolve_path($url) {
27 list($url, $query) = array_pad(explode('?', $url, 2), 2, null);
28 while (preg_match(',/\.?/,', $url, $regs) # supprime // et /./
29 OR preg_match(',/[^/]*/\.\./,S', $url, $regs) # supprime /toto/../
30 OR preg_match(',^/\.\./,S', $url, $regs)) # supprime les /../ du haut
31 $url = str_replace($regs[0], '/', $url);
32
33 if ($query)
34 $url .= '?'.$query;
35
36 return '/'.preg_replace(',^/,S', '', $url);
37 }
38
39 //
40 // Suivre un lien depuis une adresse donnee -> nouvelle adresse
41 //
42 // suivre_lien('http://rezo.net/sous/dir/../ect/ory/fi.html..s#toto',
43 // 'a/../../titi.coco.html/tata#titi');
44 // http://doc.spip.org/@suivre_lien
45 function suivre_lien($url, $lien) {
46
47 if (preg_match(',^(mailto|javascript|data):,iS', $lien))
48 return $lien;
49 if (preg_match(';^((?:[a-z]{3,7}:)?//.*?)(/.*)?$;iS', $lien, $r))
50 return $r[1].resolve_path($r[2]);
51
52 # L'url site spip est un lien absolu aussi
53 if ($lien == $GLOBALS['meta']['adresse_site']){
54 return $lien;
55 }
56
57 # lien relatif, il faut verifier l'url de base
58 # commencer par virer la chaine de get de l'url de base
59 if (preg_match(';^((?:[a-z]{3,7}:)?//[^/]+)(/.*?/?)?([^/#?]*)([?][^#]*)?(#.*)?$;S', $url, $regs)) {
60 $debut = $regs[1];
61 $dir = !strlen($regs[2]) ? '/' : $regs[2];
62 $mot = $regs[3];
63 $get = isset($regs[4])?$regs[4]:"";
64 $hash = isset($regs[5])?$regs[5]:"";
65 }
66 switch (substr($lien,0,1)) {
67 case '/':
68 return $debut . resolve_path($lien);
69 case '#':
70 return $debut . resolve_path($dir.$mot.$get.$lien);
71 case '':
72 return $debut . resolve_path($dir.$mot.$get.$hash);
73 default:
74 return $debut . resolve_path($dir.$lien);
75 }
76 }
77
78 // un filtre pour transformer les URLs relatives en URLs absolues ;
79 // ne s'applique qu'aux #URL_XXXX
80 // http://doc.spip.org/@url_absolue
81 function url_absolue($url, $base='') {
82 if (strlen($url = trim($url)) == 0)
83 return '';
84 if (!$base)
85 $base = url_de_base() . (_DIR_RACINE ? _DIR_RESTREINT_ABS : '');
86 return suivre_lien($base, $url);
87 }
88
89 /**
90 * Supprimer le protocole d'une url absolue
91 * pour le rendre implicite (URL commencant par "//")
92 * @param string $url_absolue
93 * @return string
94 */
95 function protocole_implicite($url_absolue){
96 return preg_replace(";^[a-z]{3,7}://;i","//",$url_absolue);
97 }
98
99 // un filtre pour transformer les URLs relatives en URLs absolues ;
100 // ne s'applique qu'aux textes contenant des liens
101 // http://doc.spip.org/@liens_absolus
102 function liens_absolus($texte, $base='') {
103 if (preg_match_all(',(<(a|link|image|img|script)\s[^<>]*(href|src)=[^<>]*>),imsS',
104 $texte, $liens, PREG_SET_ORDER)) {
105 if (!function_exists('extraire_attribut')) {
106 include_spip('inc/filtres');
107 }
108 foreach ($liens as $lien) {
109 foreach(array('href', 'src') as $attr) {
110 $href = extraire_attribut($lien[0], $attr);
111 if (strlen($href)>0) {
112 $abs = url_absolue($href, $base);
113 if ($href != $abs and !preg_match('/^#/',$href)) {
114 $texte_lien = inserer_attribut($lien[0], $attr, $abs);
115 $texte = str_replace($lien[0],$texte_lien,$texte);
116 }
117 }
118 }
119 }
120 }
121
122 return $texte;
123 }
124
125 //
126 // Ce filtre public va traiter les URL ou les <a href>
127 //
128 // http://doc.spip.org/@abs_url
129 function abs_url($texte, $base='') {
130 if ($GLOBALS['mode_abs_url'] == 'url')
131 return url_absolue($texte, $base);
132 else
133 return liens_absolus($texte, $base);
134 }
135
136 /**
137 * htmlspecialchars wrapper (PHP >= 5.4 compat issue)
138 *
139 * @param string $string
140 * @param int $flags
141 * @param string $encoding
142 * @param bool $double_encode
143 * @return string
144 */
145 function spip_htmlspecialchars($string, $flags=null, $encoding='ISO-8859-1', $double_encode = true){
146 if (is_null($flags)) {
147 if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50400)
148 $flags = ENT_COMPAT;
149 else
150 $flags = ENT_COMPAT|ENT_HTML401;
151 }
152
153 if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50203)
154 return htmlspecialchars($string,$flags,$encoding);
155 else
156 return htmlspecialchars($string,$flags,$encoding,$double_encode);
157 }
158
159 /**
160 * htmlentities wrapper (PHP >= 5.4 compat issue)
161 *
162 * @param string $string
163 * @param int $flags
164 * @param string $encoding
165 * @param bool $double_encode
166 * @return string
167 */
168 function spip_htmlentities($string,$flags=null,$encoding = 'ISO-8859-1',$double_encode = true){
169 if (is_null($flags)) {
170 if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50400)
171 $flags = ENT_COMPAT;
172 else
173 $flags = ENT_COMPAT|ENT_HTML401;
174 }
175
176 if (!defined('PHP_VERSION_ID') OR PHP_VERSION_ID < 50203)
177 return htmlentities($string,$flags,$encoding);
178 else
179 return htmlentities($string,$flags,$encoding,$double_encode);
180 }
181 ?>