[PLUGINS] ~maj Crayons
[lhc/web/www.git] / www / plugins / crayons / crayons_fonctions.php
1 <?php
2 /**
3 * Crayons
4 * plugin for spip
5 * (c) Fil, toggg 2006-2014
6 * licence GPL
7 *
8 * @package SPIP\Crayons\Fonctions
9 */
10
11 if (!defined('_ECRIRE_INC_VERSION')) {
12 return;
13 }
14
15 if (!defined('_DEBUG_CRAYONS')) {
16 /**
17 * Débuguer les crayons
18 *
19 * Mettre a true dans mes_options pour avoir les crayons non compresses
20 */
21 define('_DEBUG_CRAYONS', false);
22 }
23
24 /**
25 * Dire rapidement si ca vaut le coup de chercher des droits
26 *
27 * @return bool
28 **/
29 function analyse_droits_rapide_dist() {
30 return isset($GLOBALS['auteur_session']['statut']);
31 }
32
33 /**
34 * Vérifier si un exec du privé est crayonnable
35 *
36 * @param string $exec
37 *
38 * @return bool
39 **/
40 function test_exec_crayonnable($exec) {
41 if ($exec_autorise = lire_config('crayons/exec_autorise')) {
42 $execs = explode(',', $exec_autorise);
43 foreach ($execs as $key => $value) {
44 $execs[$key] = trim($value);
45 }
46 if ($exec_autorise == '*' || in_array($exec, $execs)) {
47 return true;
48 }
49 }
50
51 return false;
52 }
53
54 /**
55 * Ajouter la gestion des crayons dans l'espace privé
56 *
57 * @pipeline header_prive
58 * @uses Crayons_preparer_page()
59 *
60 * @param string $head
61 * Contenu du header
62 * @return string
63 * Contenu du header
64 **/
65 function Crayons_insert_head($head) {
66 // verifie la presence d'une meta crayons, si c'est vide
67 // on ne cherche meme pas a traiter l'espace prive
68 $config_espace_prive = @unserialize($GLOBALS['meta']['crayons']);
69 if (empty($config_espace_prive)) {
70 return $head;
71 }
72
73 // verifie que l'edition de l'espace prive est autorisee
74 if (isset($config_espace_prive['espaceprive'])
75 and $config_espace_prive['espaceprive'] == 'on') {
76 // determine les pages (exec) crayonnables
77 if (test_exec_crayonnable(_request('exec'))) {
78 // Calcul des droits
79 include_spip('inc/crayons');
80 $head = Crayons_preparer_page($head, '*', wdgcfg(), 'head');
81 }
82 }
83
84 // retourne l'entete modifiee
85 return $head;
86 }
87
88 /**
89 * Ajouter la gestion des crayons dans l'espace public
90 *
91 * @pipeline affichage_final
92 * @uses analyse_droits_rapide_dist()
93 * @uses Crayons_preparer_page()
94 * @note
95 * Le pipeline affichage_final est executé à chaque hit sur toute la page
96 *
97 * @param string $page
98 * Contenu de la page à envoyer au navigateur
99 * @return string
100 * Contenu de la page à envoyer au navigateur
101 **/
102 function &Crayons_affichage_final(&$page) {
103
104 // ne pas se fatiguer si le visiteur n'a aucun droit
105 if (!(function_exists('analyse_droits_rapide')?analyse_droits_rapide():analyse_droits_rapide_dist())) {
106 return $page;
107 }
108
109 // sinon regarder rapidement si la page a des classes crayon
110 if (strpos($page, 'crayon')===false) {
111 return $page;
112 }
113
114 // voir un peu plus precisement lesquelles
115 include_spip('inc/crayons');
116 if (!preg_match_all(_PREG_CRAYON, $page, $regs, PREG_SET_ORDER)) {
117 return $page;
118 }
119
120 $wdgcfg = wdgcfg();
121
122 // calculer les droits sur ces crayons
123 include_spip('inc/autoriser');
124 $droits = array();
125 $droits_accordes = 0;
126 foreach ($regs as $reg) {
127 list(,$crayon,$type,$champ,$id) = $reg;
128 if (_DEBUG_CRAYONS) {
129 spip_log("autoriser('modifier', $type, $id, NULL, array('champ'=>$champ))", 'crayons_distant');
130 }
131 if (autoriser('modifier', $type, $id, null, array('champ'=>$champ))) {
132 if (!isset($droits['.' . $crayon])) {
133 $droits['.' . $crayon] = 0;
134 }
135 $droits['.' . $crayon]++;
136 $droits_accordes ++;
137 }
138 }
139
140 // et les signaler dans la page
141 if ($droits_accordes == count($regs)) { // tous les droits
142 $page = Crayons_preparer_page($page, '*', $wdgcfg);
143 } elseif ($droits) { // seulement certains droits, preciser lesquels
144 $page = Crayons_preparer_page($page, join(',', array_keys($droits)), $wdgcfg);
145 }
146
147 return $page;
148 }
149
150 /**
151 * Ajoute les scripts css et js nécessaires aux crayons dans le code HTML
152 *
153 * @uses crayons_var2js()
154 *
155 * @param string $page
156 * Code HTML de la page complète ou du header seulement
157 * @param string $droits
158 * - Liste de css définissant les champs crayonnables
159 * (séparés par virgule) dont l'édition est autorisée
160 * - "*" si tous sont autorisés
161 * @param array $wdgcfg
162 * Description de la configuration des crayons (attribut => valeur)
163 * @param string $mode
164 * - page : toute la page est présente dans `$page`
165 * - head : seul le header est présent dans `$page`
166 * @return
167 **/
168 function &Crayons_preparer_page(&$page, $droits, $wdgcfg = array(), $mode = 'page') {
169 /**
170 * Si pas forcer_lang, on charge le contrôleur dans la langue que l'utilisateur a dans le privé
171 */
172 if (!isset($GLOBALS['forcer_lang']) or !$GLOBALS['forcer_lang'] or ($GLOBALS['forcer_lang'] === 'non')) {
173 lang_select($GLOBALS['auteur_session']['lang']);
174 }
175
176 $jsFile = generer_url_public('crayons.js');
177 if (_DEBUG_CRAYONS) {
178 $jsFile = parametre_url($jsFile, 'debug_crayons', 1, '&');
179 }
180 include_spip('inc/filtres'); // rien que pour direction_css() :(
181 $cssFile = direction_css(find_in_path('crayons.css'));
182
183 $config = crayons_var2js(array(
184 'imgPath' => dirname(find_in_path('images/crayon.png')),
185 'droits' => $droits,
186 'dir_racine' => _DIR_RACINE,
187 'self' => self('&'),
188 'txt' => array(
189 'error' => _U('crayons:svp_copier_coller'),
190 'sauvegarder' => $wdgcfg['msgAbandon'] ? _U('crayons:sauvegarder') : ''
191 ),
192 'img' => array(
193 'searching' => array(
194 'txt' => _U('crayons:veuillez_patienter')
195 ),
196 'crayon' => array(
197 'txt' => _U('crayons:editer')
198 ),
199 'edit' => array(
200 'txt' => _U('crayons:editer_tout')
201 ),
202 'img-changed' => array(
203 'txt' => _U('crayons:deja_modifie')
204 )
205 ),
206 'cfg' => $wdgcfg
207 ));
208
209
210 // Est-ce que PortePlume est la ?
211 $meta_crayon = isset($GLOBALS['meta']['crayons']) ? unserialize($GLOBALS['meta']['crayons']): array();
212 $pp = '';
213 if (isset($meta_crayon['barretypo']) && $meta_crayon['barretypo']) {
214 if (function_exists('chercher_filtre')
215 and $f = chercher_filtre('info_plugin')
216 and $f('PORTE_PLUME','est_actif')) {
217 $pp = <<<EOF
218 cQuery(function() {
219 if (typeof onAjaxLoad == 'function') {
220 function barrebouilles_crayons() {
221 $('.formulaire_crayon textarea.crayon-active')
222 .barre_outils('edition');
223 }
224 onAjaxLoad(barrebouilles_crayons);
225 }
226 });
227 EOF;
228 }
229 }
230
231
232 $incCSS = "<link rel=\"stylesheet\" href=\"{$cssFile}\" type=\"text/css\" media=\"all\" />";
233 $incJS = <<<EOH
234 <script type="text/javascript">/* <![CDATA[ */
235 var configCrayons;
236 function startCrayons() {
237 configCrayons = new cQuery.prototype.cfgCrayons({$config});
238 cQuery.fn.crayonsstart();
239 {$pp}
240 }
241 var cr = document.createElement('script');
242 cr.type = 'text/javascript'; cr.async = true;
243 cr.src = '{$jsFile}\x26callback=startCrayons';
244 var s = document.getElementsByTagName('script')[0];
245 s.parentNode.insertBefore(cr, s);
246 /* ]]> */</script>
247
248 EOH;
249
250 if ($mode == 'head') {
251 //js inline avant les css, sinon ca bloque le chargement
252 $page = $page . $incJS . $incCSS;
253 return $page;
254 }
255
256 $pos_head = strpos($page, '</head>');
257 if ($pos_head === false) {
258 return $page;
259 }
260
261 // js inline avant la premiere css, ou sinon avant la fin du head
262 $pos_link = strpos($page, '<link ');
263 if (!$pos_link) {
264 $pos_link = $pos_head;
265 }
266 $page = substr_replace($page, $incJS, $pos_link, 0);
267
268 // css avant la fin du head
269 $pos_head = strpos($page, '</head>');
270 $page = substr_replace($page, $incCSS, $pos_head, 0);
271
272 return $page;
273 }
274
275 /**
276 * Balise indiquant un champ SQL crayonnable
277 *
278 * @note
279 * Si cette fonction est absente, `balise_EDIT_dist()` déclarée par SPIP
280 * ne retourne rien
281 *
282 * @example
283 * ```
284 * <div class="#EDIT{texte}">#TEXTE</div>
285 * <div class="#EDIT{ps}">#PS</div>
286 * ```
287 *
288 * @param Champ $p
289 * Pile au niveau de la balise
290 * @return Champ
291 * Pile complétée par le code à générer
292 **/
293 function balise_EDIT($p) {
294
295 // le code compile de ce qui se trouve entre les {} de la balise
296 $label = interprete_argument_balise(1, $p);
297
298 // Verification si l'on est dans le cas d'une meta
299 // #EDIT{meta-descriptif_site} ou #EDIT{meta-demo/truc}
300 if (preg_match('/meta-(.*)\'/', $label, $meta)) {
301 $type = 'meta';
302 $label= 'valeur';
303 $primary = $meta[1];
304 $p->code = "classe_boucle_crayon('"
305 . $type
306 ."','"
307 .$label
308 ."',"
309 . "str_replace('/', '__', '$primary')" # chaque / doit être remplacé pour CSS.
310 .").' '";
311 $p->interdire_scripts = false;
312 return $p;
313 }
314
315 $i_boucle = $p->nom_boucle ? $p->nom_boucle : $p->id_boucle;
316 // #EDIT hors boucle? ne rien faire
317 if (!isset($p->boucles[$i_boucle]) or !$type = ($p->boucles[$i_boucle]->type_requete)) {
318 $p->code = "''";
319 $p->interdire_scripts = false;
320 return $p;
321 }
322
323 // crayon sur une base distante 'nua__article-intro-5'
324 if ($distant = $p->boucles[$i_boucle]->sql_serveur) {
325 $type = $distant.'__'.$type;
326 }
327
328 // le compilateur 1.9.2 ne calcule pas primary pour les tables secondaires
329 // il peut aussi arriver une table sans primary (par ex: une vue)
330 if (!($primary = $p->boucles[$i_boucle]->primary)) {
331 include_spip('inc/vieilles_defs'); # 1.9.2 pour trouver_def_table
332 if (function_exists('trouver_def_table')) {
333 list($nom, $desc) = trouver_def_table(
334 $p->boucles[$i_boucle]->type_requete,
335 $p->boucles[$i_boucle]
336 );
337 $primary = $desc['key']['PRIMARY KEY'];
338 }
339 }
340 // On rajoute ici un debug dans le cas où aucune clé primaire n'est trouvée.
341 // Cela peut se présenter par exemple si on utilise #EDIT{monchamp} directement
342 // dans une boucle CONDITION sans faire référence au nom de la boucle d'au dessus.
343 if (!$primary) {
344 erreur_squelette(_T('crayons:absence_cle_primaire'), $p);
345 }
346
347 $primary = explode(',', $primary);
348 $id = array();
349 foreach ($primary as $key) {
350 $id[] = champ_sql(trim($key), $p);
351 }
352 $primary = implode(".'-'.", $id);
353
354 $p->code = "classe_boucle_crayon('"
355 . $type
356 ."',"
357 .sinon($label, "''")
358 .','
359 . $primary
360 .").' '";
361 $p->interdire_scripts = false;
362 return $p;
363 }
364
365 /**
366 * Balise indiquant une configuration crayonnable
367 *
368 * @example
369 * ```
370 * <div class="#EDIT_CONFIG{descriptif_site}">#DESCRIPTIF_SITE_SPIP</div>
371 * <div class="#EDIT_CONFIG{demo/truc}">#CONFIG{demo/truc}</div>
372 * ```
373 *
374 * @param Champ $p
375 * Pile au niveau de la balise
376 * @return Champ
377 * Pile complétée par le code à générer
378 **/
379 function balise_EDIT_CONFIG_dist($p) {
380
381 // le code compile de ce qui se trouve entre les {} de la balise
382 $config = interprete_argument_balise(1, $p);
383 if (!$config) {
384 return $p;
385 }
386
387 // chaque / du nom de config doit être transformé pour css.
388 // nous utiliserons '__' à la place.
389
390 $type = 'meta';
391 $label= 'valeur';
392
393 $p->code = "classe_boucle_crayon('"
394 . $type
395 . "','"
396 . $label
397 . "',"
398 . "str_replace('/', '__', $config)"
399 . ").' '";
400 $p->interdire_scripts = false;
401 return $p;
402 }
403
404 /**
405 * Crée le controleur du crayon indiqué par la classe CSS
406 *
407 * @param string $class
408 * Class CSS de crayon tel que créé par #EDIT
409 * @return string
410 * HTML du crayon, sinon texte d'erreur
411 **/
412 function creer_le_crayon($class) {
413 include_spip('inc/crayons');
414 include_spip('action/crayons_html');
415 $a = affiche_controleur($class, array('w' => 485, 'h' => 300, 'wh' => 500));
416 return $a['$erreur'] ? $a['$erreur'] : $a['$html'];
417 }
418
419 /**
420 * Balise `#CRAYON` affichant un formulaire de crayon
421 *
422 * SI `?edit=1;`
423 *
424 * @example
425 * ```
426 * #CRAYON{ps}
427 * ```
428 *
429 * @param Champ $p
430 * Pile au niveau de la balise
431 * @return Champ
432 * Pile complétée par le code à générer
433 **/
434 function balise_CRAYON($p) {
435 $p = balise_EDIT($p);
436 $p->code = 'creer_le_crayon('.$p->code.')';
437 return $p;
438 }
439
440
441 /**
442 * Donne la classe CSS crayon
443 *
444 * En fonction :
445 * - du type de la boucle
446 * (attention aux exceptions pour `#EDIT` dans les boucles HIERARCHIE et SITES)
447 * - du champ demande (vide, + ou se terminant par + : (+)classe type--id)
448 * - de l'id courant
449 *
450 * @param string $type
451 * Type d'objet, ou "meta" pour un champ de configuration
452 * @param string $champ
453 * Champ SQL concerné
454 * @param int|string $id
455 * Identifiant de la ligne sql
456 * @return string
457 * Classes CSS (à ajouter dans le HTML à destination du javascript de Crayons)
458 **/
459 function classe_boucle_crayon($type, $champ, $id) {
460 // $type = objet_type($type);
461 $type = $type[strlen($type) - 1] == 's' ?
462 substr($type, 0, -1) :
463 str_replace(
464 array('hierarchie','syndication'),
465 array('rubrique','site'),
466 $type
467 );
468
469 $plus = (substr($champ, -1) == '+' and $champ = substr($champ, 0, -1))
470 ? " $type--$id"
471 : '';
472
473 // test rapide pour verifier que l'id est valide (a-zA-Z0-9)
474 if (false !== strpos($id, ' ')) {
475 spip_log("L'identifiant ($id) ne pourra être géré ($type | $champ)", 'crayons');
476 return 'crayon_id_ingerable';
477 }
478
479 return 'crayon ' . $type . '-' . $champ . '-' . $id . $plus;
480 }