c0750b7817882270394f3d7d909d5b112ab9b828
[lhc/web/www.git] / www / ecrire / inc / pipelines_ecrire.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2017 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 /**
14 * Fonctions déclarées dans des pipelines (espace privé)
15 *
16 * @package SPIP\Core\Pipelines
17 **/
18 if (!defined('_ECRIRE_INC_VERSION')) {
19 return;
20 }
21
22
23 /**
24 * Inserer jQuery et ses plugins pour l'espace privé
25 *
26 * La fonction ajoute les balises scripts dans le texte qui appelent
27 * les scripts jQuery ainsi que certains de ses plugins. La liste
28 * des js chargée peut être complété par le pipeline 'jquery_plugins'
29 *
30 * Cette fonction est appelée par le pipeline header_prive
31 *
32 * @see f_jQuery()
33 * @link http://code.spip.net/@f_jQuery
34 *
35 * @param string $texte Contenu qui sera inséré dans le head HTML
36 * @return string Contenu complété des scripts javascripts, dont jQuery
37 **/
38 function f_jQuery_prive($texte) {
39 $x = '';
40 $jquery_plugins = pipeline('jquery_plugins',
41 array(
42 'prive/javascript/jquery.js',
43 'prive/javascript/jquery-migrate-3.0.1.js',
44 'prive/javascript/jquery.form.js',
45 'prive/javascript/jquery.autosave.js',
46 'prive/javascript/jquery.placeholder-label.js',
47 'prive/javascript/ajaxCallback.js',
48 'prive/javascript/js.cookie.js',
49 'prive/javascript/jquery.cookie.js',
50 'prive/javascript/spip_barre.js',
51 ));
52 foreach (array_unique($jquery_plugins) as $script) {
53 if ($script = find_in_path(supprimer_timestamp($script))) {
54 $script = timestamp($script);
55 $x .= "\n<script src=\"$script\" type=\"text/javascript\"></script>\n";
56 }
57 }
58 // inserer avant le premier script externe ou a la fin
59 if (preg_match(",<script[^><]*src=,", $texte, $match)
60 and $p = strpos($texte, $match[0])
61 ) {
62 $texte = substr_replace($texte, $x, $p, 0);
63 } else {
64 $texte .= $x;
65 }
66
67 return $texte;
68 }
69
70
71 /**
72 * Ajout automatique du title dans les pages du privé en squelette
73 *
74 * Appellé dans le pipeline affichage_final_prive
75 *
76 * @param string $texte
77 * @return string
78 */
79 function affichage_final_prive_title_auto($texte) {
80 if (strpos($texte, '<title>') === false
81 and
82 (preg_match(",<h1[^>]*>(.+)</h1>,Uims", $texte, $match)
83 or preg_match(",<h[23][^>]*>(.+)</h[23]>,Uims", $texte, $match))
84 and $match = textebrut(trim($match[1]))
85 and ($p = strpos($texte, '<head>')) !== false
86 ) {
87 if (!$nom_site_spip = textebrut(typo($GLOBALS['meta']["nom_site"]))) {
88 $nom_site_spip = _T('info_mon_site_spip');
89 }
90
91 $titre = "<title>["
92 . $nom_site_spip
93 . "] " . $match
94 . "</title>";
95
96 $texte = substr_replace($texte, $titre, $p + 6, 0);
97 }
98
99 return $texte;
100 }
101
102
103 // Fonction standard pour le pipeline 'boite_infos'
104 // http://code.spip.net/@f_boite_infos
105 function f_boite_infos($flux) {
106 $args = $flux['args'];
107 $type = $args['type'];
108 unset($args['row']);
109 if (!trouver_fond($type, "prive/objets/infos/")) {
110 $type = 'objet';
111 }
112 $args['espace_prive'] = 1;
113 $flux['data'] .= recuperer_fond("prive/objets/infos/$type", $args);
114
115 return $flux;
116 }
117
118
119 /**
120 * Utilisation du pipeline recuperer_fond dans le prive
121 *
122 * Branchement automatise de affiche_gauche, affiche_droite, affiche_milieu
123 * pour assurer la compat avec les versions precedentes des exec en php
124 * Branche de affiche_objet
125 *
126 * Les pipelines ne recevront plus exactement le meme contenu en entree,
127 * mais la compat multi vertions pourra etre assuree
128 * par une insertion au bon endroit quand le contenu de depart n'est pas vide
129 *
130 * @param array $flux Données du pipeline
131 * @return array Données du pipeline
132 */
133 function f_afficher_blocs_ecrire($flux) {
134 static $o = array();
135 if (is_string($fond = $flux['args']['fond'])) {
136 $exec = isset($flux['args']['contexte']['exec']) ? $flux['args']['contexte']['exec'] : _request('exec');
137 if (!isset($o[$exec])) {
138 $o[$exec] = trouver_objet_exec($exec);
139 }
140 // cas particulier
141 if ($exec == "infos_perso") {
142 $flux['args']['contexte']['id_auteur'] = $GLOBALS['visiteur_session']['id_auteur'];
143 }
144 $typepage = (isset($flux['args']['contexte']['type-page']) ? $flux['args']['contexte']['type-page'] : $exec);
145 if ($fond == "prive/squelettes/navigation/$typepage") {
146 $flux['data']['texte'] = pipeline('affiche_gauche',
147 array('args' => $flux['args']['contexte'], 'data' => $flux['data']['texte']));
148 } elseif ($fond == "prive/squelettes/extra/$typepage") {
149 include_spip('inc/presentation_mini');
150 $flux['data']['texte'] = pipeline('affiche_droite',
151 array('args' => $flux['args']['contexte'], 'data' => $flux['data']['texte'])) . liste_objets_bloques($exec,
152 $flux['args']['contexte']);
153 } elseif ($fond == "prive/squelettes/hierarchie/$typepage" and $o[$exec]) {
154 // id non defini sur les formulaire de nouveaux objets
155 $id = isset($flux['args']['contexte'][$o[$exec]['id_table_objet']]) ? intval($flux['args']['contexte'][$o[$exec]['id_table_objet']]) : 0;
156 $flux['data']['texte'] = pipeline('affiche_hierarchie',
157 array('args' => array('objet' => $o[$exec]['type'], 'id_objet' => $id), 'data' => $flux['data']['texte']));
158 } elseif ($fond == "prive/squelettes/contenu/$typepage") {
159 if (!strpos($flux['data']['texte'], "<!--affiche_milieu-->")) {
160 $flux['data']['texte'] = preg_replace(',<div id=["\']wysiwyg,', "<!--affiche_milieu-->\\0",
161 $flux['data']['texte']);
162 }
163 if ($o[$exec]
164 and $objet = $o[$exec]['type']
165 and $o[$exec]['edition'] == false
166 and isset($flux['args']['contexte'][$o[$exec]['id_table_objet']])
167 and $id = intval($flux['args']['contexte'][$o[$exec]['id_table_objet']])
168 ) {
169 // inserer le formulaire de traduction
170 $flux['data']['texte'] = str_replace("<!--affiche_milieu-->", recuperer_fond('prive/objets/editer/traductions',
171 array('objet' => $objet, 'id_objet' => $id, 'espace_prive' => 1)) . "<!--affiche_milieu-->", $flux['data']['texte']);
172 $flux['data']['texte'] = pipeline('afficher_fiche_objet', array(
173 'args' => array(
174 'contexte' => $flux['args']['contexte'],
175 'type' => $objet,
176 'id' => $id
177 ),
178 'data' => $flux['data']['texte']
179 ));
180 }
181 $flux['data']['texte'] = pipeline('affiche_milieu',
182 array('args' => $flux['args']['contexte'], 'data' => $flux['data']['texte']));
183 } elseif ($fond == "prive/squelettes/inclure/pied") {
184 $flux['data']['texte'] = pipeline('affiche_pied',
185 array('args' => $flux['args']['contexte'], 'data' => $flux['data']['texte']));
186 } elseif (strncmp($fond, "prive/objets/contenu/", 21) == 0
187 and $objet = basename($fond)
188 and $objet == substr($fond, 21)
189 and isset($o[$objet])
190 and $o[$objet]
191 ) {
192 $id = intval($flux['args']['contexte'][$o[$exec]['id_table_objet']]);
193 $flux['data']['texte'] = pipeline('afficher_contenu_objet', array(
194 'args' => array('type' => $objet, 'id_objet' => $id, 'contexte' => $flux['args']['contexte']),
195 'data' => $flux['data']['texte']
196 ));
197 }
198 }
199
200 return $flux;
201 }
202
203 /**
204 * Afficher les taches en attente liees a un objet
205 *
206 * @pipeline affiche_milieu
207 * @param string $flux
208 * @return string
209 */
210 function f_queue_affiche_milieu($flux) {
211 $args = $flux['args'];
212 $res = "";
213 foreach ($args as $key => $arg) {
214 if (preg_match(",^id_,", $key) and is_numeric($arg) and $arg = intval($arg)) {
215 $objet = preg_replace(',^id_,', '', $key);
216 $res .= recuperer_fond('modeles/object_jobs_list', array('id_objet' => $arg, 'objet' => $objet, 'espace_prive' => 1),
217 array('ajax' => true));
218 }
219 }
220 if ($res) {
221 $flux['data'] = $res . $flux['data'];
222 }
223
224 return $flux;
225 }
226
227 /**
228 * Trouver l'objet qui correspond à l'exec de l'espace privé passé en argument
229 *
230 * renvoie false si pas d'objet en cours, ou un tableau associatif
231 * contenant les informations table_objet_sql,table,type,id_table_objet,edition
232 *
233 * @param string $exec
234 * nom de la page testee
235 * @return array|bool
236 */
237 function trouver_objet_exec($exec) {
238 static $objet_exec = array();
239 if (!$exec) {
240 return false;
241 }
242 // cas particulier
243 if ($exec == "infos_perso") {
244 $exec = "auteur";
245 set_request('id_auteur', $GLOBALS['visiteur_session']['id_auteur']);
246 }
247 if (!isset($objet_exec[$exec])) {
248 $objet_exec[$exec] = false;
249 $infos = lister_tables_objets_sql();
250 foreach ($infos as $t => $info) {
251 if ($exec == $info['url_edit'] and $info['editable']) {
252 return $objet_exec[$exec] = array(
253 'edition' => $exec == $info['url_voir'] ? '' : true,
254 'table_objet_sql' => $t,
255 'table' => $info['table_objet'],
256 'type' => $info['type'],
257 'id_table_objet' => id_table_objet($info['type'])
258 );
259 }
260 if ($exec == $info['url_voir']) {
261 return $objet_exec[$exec] = array(
262 'edition' => false,
263 'table_objet_sql' => $t,
264 'table' => $info['table_objet'],
265 'type' => $info['type'],
266 'id_table_objet' => id_table_objet($info['type'])
267 );
268 }
269 }
270 }
271
272 return $objet_exec[$exec];
273 }