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