3 * Crayons plugin for spip (c) Fil, toggg 2006-2013 -- licence GPL
5 * @package SPIP\Crayons\Fonctions
7 if (!defined("_ECRIRE_INC_VERSION")) return;
10 * Débuguer les crayons
11 * mettre a true dans mes_options pour avoir les crayons non compresses
13 if(!defined('_DEBUG_CRAYONS'))
14 define('_DEBUG_CRAYONS', false);
16 // Dire rapidement si ca vaut le coup de chercher des droits
17 function analyse_droits_rapide_dist() {
18 return isset($GLOBALS['auteur_session']['statut']);
21 // Le pipeline header_prive (pour y tester les crayons)
22 function Crayons_insert_head($head) {
23 // verifie la presence d'une meta crayons, si c'est vide
24 // on ne cherche meme pas a traiter l'espace prive
25 $config_espace_prive = @unserialize
($GLOBALS['meta']['crayons']);
26 if (empty($config_espace_prive)) {
30 // verifie que l'edition de l'espace prive est autorisee
31 if ($config_espace_prive['espaceprive'] == 'on') {
32 // determine les pages (exec) crayonnables
33 if (($config_espace_prive['exec_autorise'] == '*') ||
34 in_array(_request('exec'),explode(',',$config_espace_prive['exec_autorise']))) {
36 include_spip('inc/crayons');
37 $head = Crayons_preparer_page($head, '*', wdgcfg(), 'head');
41 // retourne l'entete modifiee
45 // Le pipeline affichage_final, execute a chaque hit sur toute la page
46 function &Crayons_affichage_final(&$page) {
48 // ne pas se fatiguer si le visiteur n'a aucun droit
49 if (!(function_exists('analyse_droits_rapide')?
analyse_droits_rapide():analyse_droits_rapide_dist()))
52 // sinon regarder rapidement si la page a des classes crayon
53 if (strpos($page, 'crayon')===FALSE)
56 // voir un peu plus precisement lesquelles
57 include_spip('inc/crayons');
58 if (!preg_match_all(_PREG_CRAYON
, $page, $regs, PREG_SET_ORDER
))
62 // calculer les droits sur ces crayons
63 include_spip('inc/autoriser');
66 foreach ($regs as $reg) {
67 list(,$crayon,$type,$champ,$id) = $reg;
68 if (_DEBUG_CRAYONS
) spip_log("autoriser('modifier', $type, $id, NULL, array('champ'=>$champ))","crayons_distant");
69 if (autoriser('modifier', $type, $id, NULL, array('champ'=>$champ))) {
70 if(!isset($droits['.' . $crayon]))
71 $droits['.' . $crayon] = 0;
72 $droits['.' . $crayon]++
;
76 // et les signaler dans la page
77 if ($droits_accordes == count($regs)) // tous les droits
78 $page = Crayons_preparer_page($page, '*', $wdgcfg);
79 else if ($droits) // seulement certains droits, preciser lesquels
80 $page = Crayons_preparer_page($page, join(',',array_keys($droits)), $wdgcfg);
85 function &Crayons_preparer_page(&$page, $droits, $wdgcfg = array(), $mode='page') {
87 * Si pas forcer_lang, on charge le contrôleur dans la langue que l'utilisateur a dans le privé
89 if(!isset($GLOBALS['forcer_lang']) OR !$GLOBALS['forcer_lang'] OR ($GLOBALS['forcer_lang'] === 'non'))
90 lang_select($GLOBALS['auteur_session']['lang']);
92 $jsFile = generer_url_public('crayons.js');
94 $jsFile = parametre_url($jsFile,'debug_crayons',1,'&');
95 include_spip('inc/filtres'); // rien que pour direction_css() :(
96 $cssFile = direction_css(find_in_path('crayons.css'));
98 $config = crayons_var2js(array(
99 'imgPath' => dirname(find_in_path('images/crayon.png')),
101 'dir_racine' => _DIR_RACINE
,
104 'error' => _U('crayons:svp_copier_coller'),
105 'sauvegarder' => $wdgcfg['msgAbandon'] ?
_U('crayons:sauvegarder') : ''
108 'searching' => array(
109 'txt' => _U('crayons:veuillez_patienter')
112 'txt' => _U('crayons:editer')
115 'txt' => _U('crayons:editer_tout')
117 'img-changed' => array(
118 'txt' => _U('crayons:deja_modifie')
125 // Est-ce que PortePlume est la ?
126 $meta_crayon = isset($GLOBALS['meta']['crayons']) ?
unserialize($GLOBALS['meta']['crayons']): array();
128 if (isset($meta_crayon['barretypo']) && $meta_crayon['barretypo']) {
129 if (function_exists('chercher_filtre')
130 AND $f = chercher_filtre('info_plugin')
131 AND $f('PORTE_PLUME','est_actif')) {
135 if (typeof onAjaxLoad == 'function') {
136 function barrebouilles_crayons() {
137 $('.formulaire_crayon textarea.crayon-active')
138 .barre_outils('edition');
140 onAjaxLoad(barrebouilles_crayons);
149 $incCSS = "<link rel=\"stylesheet\" href=\"{$cssFile}\" type=\"text/css\" media=\"all\" />";
151 <script type="text/javascript">/* <![CDATA[ */
153 function startCrayons() {
154 configCrayons = new cQuery.prototype.cfgCrayons({$config});
155 cQuery.fn.crayonsstart();
158 var cr = document.createElement('script');
159 cr.type = 'text/javascript'; cr.async = true;
160 cr.src = '{$jsFile}\x26callback=startCrayons';
161 var s = document.getElementsByTagName('script')[0];
162 s.parentNode.insertBefore(cr, s);
168 return $page . $incJS . $incCSS; //js inline avant les css, sinon ca bloque le chargement
170 $pos_head = strpos($page, '</head>');
171 if ($pos_head === false)
174 // js inline avant la premiere css, ou sinon avant la fin du head
175 $pos_link = strpos($page, '<link ');
177 $pos_link = $pos_head;
178 $page = substr_replace($page, $incJS, $pos_link, 0);
180 // css avant la fin du head
181 $pos_head = strpos($page, '</head>');
182 $page = substr_replace($page, $incCSS, $pos_head, 0);
189 * Balise indiquant un champ SQL crayonnable
192 * Si cette fonction est absente, balise_EDIT_dist() déclarée par SPIP
196 * <div class="#EDIT{texte}">#TEXTE</div>
197 * <div class="#EDIT{ps}">#PS</div>
200 * Pile au niveau de la balise
202 * Pile complétée par le code à générer
204 function balise_EDIT($p) {
206 // le code compile de ce qui se trouve entre les {} de la balise
207 $label = interprete_argument_balise(1,$p);
209 // Verification si l'on est dans le cas d'une meta
210 // #EDIT{meta-descriptif_site} ou #EDIT{meta-demo/truc}
211 if (preg_match('/meta-(.*)\'/',$label,$meta)) {
215 $p->code
= "classe_boucle_crayon('"
220 . "str_replace('/', '__', '$primary')" # chaque / doit être remplacé pour CSS.
222 $p->interdire_scripts
= false;
226 $i_boucle = $p->nom_boucle ?
$p->nom_boucle
: $p->id_boucle
;
227 // #EDIT hors boucle? ne rien faire
228 if (!$type = ($p->boucles
[$i_boucle]->type_requete
)) {
230 $p->interdire_scripts
= false;
234 // crayon sur une base distante 'nua:article-intro-5'
235 if ($distant = $p->boucles
[$i_boucle]->sql_serveur
)
236 $type = $distant.'__'.$type;
238 // le compilateur 1.9.2 ne calcule pas primary pour les tables secondaires
239 // il peut aussi arriver une table sans primary (par ex: une vue)
240 if(!($primary = $p->boucles
[$i_boucle]->primary
)){
241 include_spip('inc/vieilles_defs'); # 1.9.2 pour trouver_def_table
242 list($nom, $desc) = trouver_def_table(
243 $p->boucles
[$i_boucle]->type_requete
, $p->boucles
[$i_boucle]);
244 $primary = $desc['key']['PRIMARY KEY'];
247 $primary = explode(',',$primary);
249 foreach($primary as $key) {
250 $id[] = champ_sql(trim($key),$p);
252 $primary = implode(".'-'.",$id);
254 $p->code
= "classe_boucle_crayon('"
261 $p->interdire_scripts
= false;
267 * Balise indiquant une configuration crayonnable
270 * <div class="#EDIT_CONFIG{descriptif_site}">#DESCRIPTIF_SITE_SPIP</div>
271 * <div class="#EDIT_CONFIG{demo/truc}">#CONFIG{demo/truc}</div>
274 * Pile au niveau de la balise
276 * Pile complétée par le code à générer
278 function balise_EDIT_CONFIG_dist($p) {
280 // le code compile de ce qui se trouve entre les {} de la balise
281 $config = interprete_argument_balise(1,$p);
282 if (!$config) return $p;
284 // chaque / du nom de config doit être transformé pour css.
285 // nous utiliserons '__' à la place.
290 $p->code
= "classe_boucle_crayon('"
295 . "str_replace('/', '__', $config)"
297 $p->interdire_scripts
= false;
302 * Crée le controleur du crayon indiqué par la classe CSS
304 * @param string $class
305 * Class CSS de crayon tel que créé par #EDIT
307 * HTML du crayon, sinon texte d'erreur
309 function creer_le_crayon($class) {
310 include_spip('inc/crayons');
311 include_spip('action/crayons_html');
312 $a = affiche_controleur($class, array('w' => 485, 'h' => 300, 'wh' => 500));
313 return $a['$erreur'] ?
$a['$erreur'] : $a['$html'];
317 * Balise #CRAYON affichant un formulaire de crayon
324 * Pile au niveau de la balise
326 * Pile complétée par le code à générer
328 function balise_CRAYON($p) {
329 $p = balise_EDIT($p);
330 $p->code
= 'creer_le_crayon('.$p->code
.')';
336 * Donne la classe CSS crayon en fonction
337 * - du type de la boucle
338 * (attention aux exceptions pour #EDIT dans les boucles HIERARCHIE et SITES)
339 * - du champ demande (vide, + ou se terminant par + : (+)classe type--id)
342 * @param string $type
343 * Type d'objet, ou "meta" pour un champ de configuration
344 * @param string $champ
346 * @param int|string $id
347 * Identifiant de la ligne sql
349 * Classes CSS (à ajouter dans le HTML à destination du javascript de Crayons)
351 function classe_boucle_crayon($type, $champ, $id) {
352 // $type = objet_type($type);
353 $type = $type[strlen($type) - 1] == 's' ?
354 substr($type, 0, -1) :
356 array('hierarchie', 'syndication'),
357 array('rubrique', 'site'),
360 $plus = (substr($champ, -1) == '+' AND $champ = substr($champ, 0, -1))
364 // test rapide pour verifier que l'id est valide (a-zA-Z0-9)
365 if (false !== strpos($id, ' ')) {
366 spip_log("L'identifiant ($id) ne pourra être géré ($type | $champ)", 'crayons');
367 return 'crayon_id_ingerable';
370 return 'crayon ' . $type . '-' . $champ . '-' . $id . $plus;