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