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