~maj plugins
[ptitvelo/web/www.git] / www / plugins / spip-bonux-3 / spip_bonux_options.php
1 <?php
2 /**
3 * Plugin Spip-Bonux
4 * Le plugin qui lave plus SPIP que SPIP
5 * (c) 2008 Mathieu Marcillaud, Cedric Morin, Tetue
6 * Licence GPL
7 *
8 */
9
10 // On inclu une bonne fois pour toutes array_column
11 include_spip('lib/array_column/array_column');
12
13 if (_request('var_mode')=='preview'
14 AND $cle = _request('var_relecture')){
15 include_spip("spip_bonux_fonctions");
16 if (previsu_verifier_cle_temporaire($cle)){
17 include_spip("inc/autoriser");
18 autoriser_exception('previsualiser','',0);
19 define('_VAR_PREVIEW_EXCEPTION',true);
20 }
21 }
22
23 function spip_bonux_affichage_final($flux){
24 if (defined('_VAR_PREVIEW') AND _VAR_PREVIEW){
25 $p = stripos($flux,"</body>");
26 $url_relecture = parametre_url(self(),'var_mode','preview','&');
27 $js = "";
28 if (!defined('_VAR_PREVIEW_EXCEPTION')){
29 $url_relecture = parametre_url($url_relecture,'var_relecture',previsu_cle_temporaire(),'&');
30 $label = "Relecture temporaire";
31 }
32 else {
33 $label = _T('previsualisation');
34 $js = "jQuery('.spip-previsu').html('Relecture temporaire');";
35 }
36 $js .= "jQuery('#spip-admin').append('<a class=\"spip-admin-boutons review_link\" href=\"$url_relecture\">$label</a>');";
37 $js = "jQuery(function(){ $js });";
38 $js = "<script>$js</script>";
39 $flux = substr_replace($flux,$js,$p,0);
40 }
41 return $flux;
42 }
43
44 if (!defined('_ECRIRE_INC_VERSION')) return;
45
46 /**
47 * une fonction qui regarde si $texte est une chaine de langue
48 * de la forme <:qqch:>
49 * si oui applique _T()
50 * si non applique typo() suivant le mode choisi
51 *
52 * @param unknown_type $valeur Une valeur à tester. Si c'est un tableau, la fonction s'appliquera récursivement dessus.
53 * @param string $mode_typo Le mode d'application de la fonction typo(), avec trois valeurs possibles "toujours", "jamais" ou "multi".
54 * @return unknown_type Retourne la valeur éventuellement modifiée.
55 */
56 if (!function_exists('_T_ou_typo')) {
57 function _T_ou_typo($valeur, $mode_typo='toujours') {
58
59 // Si la valeur est bien une chaine (et pas non plus un entier déguisé)
60 if (is_string($valeur) and !intval($valeur)){
61 // Si la chaine est du type <:truc:> on passe à _T()
62 if (preg_match('/^\<:(.*?):\>$/', $valeur, $match))
63 $valeur = _T($match[1]);
64 // Sinon on la passe a typo()
65 else {
66 if (!in_array($mode_typo, array('toujours', 'multi', 'jamais')))
67 $mode_typo = 'toujours';
68
69 if ($mode_typo == 'toujours' or ($mode_typo == 'multi' and strpos($valeur, '<multi>') !== false)){
70 include_spip('inc/texte');
71 $valeur = typo($valeur);
72 }
73 }
74 }
75 // Si c'est un tableau, on reapplique la fonction récursivement
76 elseif (is_array($valeur)){
77 foreach ($valeur as $cle => $valeur2){
78 $valeur[$cle] = _T_ou_typo($valeur2, $mode_typo);
79 }
80 }
81
82 return $valeur;
83
84 }
85 }
86
87 /*
88 * Insère toutes les valeurs du tableau $arr2 après (ou avant) $cle dans le tableau $arr1.
89 * Si $cle n'est pas trouvé, les valeurs de $arr2 seront ajoutés à la fin de $arr1.
90 *
91 * La fonction garde autant que possible les associations entre les clés. Elle fonctionnera donc aussi bien
92 * avec des tableaux à index numérique que des tableaux associatifs.
93 * Attention tout de même, elle utilise array_merge() donc les valeurs de clés étant en conflits seront écrasées.
94 *
95 * @param array $arr1 Tableau dans lequel se fera l'insertion
96 * @param unknown_type $cle Clé de $arr1 après (ou avant) laquelle se fera l'insertion
97 * @param array $arr2 Tableau contenant les valeurs à insérer
98 * @param bool $avant Indique si l'insertion se fait avant la clé (par défaut c'est après)
99 * @return array Retourne le tableau avec l'insertion
100 */
101 if (!function_exists('spip_array_insert')) {
102 function spip_array_insert($arr1, $cle, $arr2, $avant=false){
103 $index = array_search($cle, array_keys($arr1));
104 if($index === false){
105 $index = count($arr1); // insert @ end of array if $key not found
106 }
107 else {
108 if(!$avant){
109 $index++;
110 }
111 }
112 $fin = array_splice($arr1, $index);
113 return array_merge($arr1, $arr2, $fin);
114 }
115 }
116
117 /*
118 * Une fonction extrêmement pratique, mais qui n'est disponible qu'à partir de PHP 5.3 !
119 * cf. http://www.php.net/manual/fr/function.array-replace-recursive.php
120 */
121 if (!function_exists('array_replace_recursive')){
122 function array_replace_recursive($array, $array1){
123 function recurse($array, $array1){
124 foreach ($array1 as $key => $value){
125 // create new key in $array, if it is empty or not an array
126 if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key])))
127 $array[$key] = array();
128 // overwrite the value in the base array
129 if (is_array($value))
130 $value = recurse($array[$key], $value);
131 $array[$key] = $value;
132 }
133 return $array;
134 }
135
136 // handle the arguments, merge one by one
137 $args = func_get_args();
138 $array = $args[0];
139 if (!is_array($array))
140 return $array;
141
142 for ($i = 1; $i < count($args); $i++)
143 if (is_array($args[$i]))
144 $array = recurse($array, $args[$i]);
145
146 return $array;
147 }
148 }
149
150 if (!function_exists('text_truncate')) {
151 /**
152 * Truncates text.
153 *
154 * Cuts a string to the length of $length and replaces the last characters
155 * with the ending if the text is longer than length.
156 *
157 * ### Options:
158 *
159 * - `ending` Will be used as Ending and appended to the trimmed string
160 * - `exact` If false, $text will not be cut mid-word
161 * - `html` If true, HTML tags would be handled correctly
162 *
163 * @param string $text String to truncate.
164 * @param integer $length Length of returned string, including ellipsis.
165 * @param array $options An array of html attributes and options.
166 * @return string Trimmed string.
167 * @access public
168 * @link http://book.cakephp.org/view/1469/Text#truncate-1625
169 */
170 function text_truncate($text, $length = 100, $options = array()) {
171 $default = array(
172 'ending' => '...', 'exact' => true, 'html' => false
173 );
174 $options = array_merge($default, $options);
175 extract($options);
176
177 if ($html) {
178 if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
179 return $text;
180 }
181 $totalLength = mb_strlen(strip_tags($ending));
182 $openTags = array();
183 $truncate = '';
184
185 preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
186 foreach ($tags as $tag) {
187 if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
188 if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
189 array_unshift($openTags, $tag[2]);
190 } else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
191 $pos = array_search($closeTag[1], $openTags);
192 if ($pos !== false) {
193 array_splice($openTags, $pos, 1);
194 }
195 }
196 }
197 $truncate .= $tag[1];
198
199 $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
200 if ($contentLength + $totalLength > $length) {
201 $left = $length - $totalLength;
202 $entitiesLength = 0;
203 if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
204 foreach ($entities[0] as $entity) {
205 if ($entity[1] + 1 - $entitiesLength <= $left) {
206 $left--;
207 $entitiesLength += mb_strlen($entity[0]);
208 } else {
209 break;
210 }
211 }
212 }
213
214 $truncate .= mb_substr($tag[3], 0 , $left + $entitiesLength);
215 break;
216 } else {
217 $truncate .= $tag[3];
218 $totalLength += $contentLength;
219 }
220 if ($totalLength >= $length) {
221 break;
222 }
223 }
224 } else {
225 if (mb_strlen($text) <= $length) {
226 return $text;
227 } else {
228 $truncate = mb_substr($text, 0, $length - mb_strlen($ending));
229 }
230 }
231 if (!$exact) {
232 $spacepos = mb_strrpos($truncate, ' ');
233 if (isset($spacepos)) {
234 if ($html) {
235 $bits = mb_substr($truncate, $spacepos);
236 preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
237 if (!empty($droppedTags)) {
238 foreach ($droppedTags as $closingTag) {
239 if (!in_array($closingTag[1], $openTags)) {
240 array_unshift($openTags, $closingTag[1]);
241 }
242 }
243 }
244 }
245 $truncate = mb_substr($truncate, 0, $spacepos);
246 }
247 }
248 $truncate .= $ending;
249
250 if ($html) {
251 foreach ($openTags as $tag) {
252 $truncate .= '</'.$tag.'>';
253 }
254 }
255
256 return $truncate;
257 }
258 }