0eead0a6adabf090ed3bd1033106b864b409e9b3
[lhc/web/www.git] / www / ecrire / public / styliser_par_z.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 * Gestion de la sélection d'un squelette depuis son nom parmi les
15 * chemins connus de SPIP, dans un contexte de type Z
16 *
17 * Recherche par exemple `contenu\xx` et en absence utilisera `contenu\dist`
18 *
19 * @package SPIP\Core\Public\Styliser
20 **/
21 if (!defined("_ECRIRE_INC_VERSION")) {
22 return;
23 }
24
25 /**
26 * Recherche automatique d'un squelette Page à partir de `contenu/xx`
27 *
28 * @param array $flux Données du pipeline
29 * @return array Données modifiées du pipeline
30 */
31 function public_styliser_par_z_dist($flux) {
32 static $prefix_path = null;
33 static $prefix_length;
34 static $z_blocs;
35 static $apl_constant;
36 static $page;
37 static $disponible = array();
38 static $echafauder;
39 static $prepend = "";
40
41 if (!isset($prefix_path)) {
42 $z_blocs = z_blocs(test_espace_prive());
43 if (test_espace_prive()) {
44 $prefix_path = "prive/squelettes/";
45 $prefix_length = strlen($prefix_path);
46 $apl_constant = '_ECRIRE_AJAX_PARALLEL_LOAD';
47 $page = 'exec';
48 $echafauder = charger_fonction('echafauder', 'prive', true);
49 define('_ZCORE_EXCLURE_PATH', '');
50 } else {
51 $prefix_path = "";
52 $prefix_length = 0;
53 $apl_constant = '_Z_AJAX_PARALLEL_LOAD';
54 $page = _SPIP_PAGE;
55 $echafauder = charger_fonction('echafauder', 'public', true);
56 define('_ZCORE_EXCLURE_PATH', '\bprive|\bsquelettes-dist' . (defined('_DIR_PLUGIN_DIST') ? '|\b' . rtrim(_DIR_PLUGIN_DIST,
57 '/') : ''));
58 }
59 $prepend = (defined('_Z_PREPEND_PATH') ? _Z_PREPEND_PATH : "");
60 }
61 $z_contenu = reset($z_blocs); // contenu par defaut
62
63 $fond = $flux['args']['fond'];
64
65 if ($prepend or strncmp($fond, $prefix_path, $prefix_length) == 0) {
66 $fond = substr($fond, $prefix_length);
67 $squelette = $flux['data'];
68 $ext = $flux['args']['ext'];
69 // Ajax Parallel loading : ne pas calculer le bloc, mais renvoyer un js qui le loadera en ajax
70 if (defined('_Z_AJAX_PARALLEL_LOAD_OK')
71 and $dir = explode('/', $fond)
72 and count($dir) == 2 // pas un sous repertoire
73 and $dir = reset($dir)
74 and in_array($dir, $z_blocs) // verifier deja qu'on est dans un bloc Z
75 and defined($apl_constant)
76 and in_array($dir, explode(',', constant($apl_constant))) // et dans un demande en APL
77 and $pipe = z_trouver_bloc($prefix_path . $prepend, $dir, 'z_apl', $ext) // et qui contient le squelette APL
78 ) {
79 $flux['data'] = $pipe;
80
81 return $flux;
82 }
83
84 // surcharger aussi les squelettes venant de squelettes-dist/
85 if ($squelette and !z_fond_valide($squelette)) {
86 $squelette = "";
87 $echafauder = "";
88 }
89 if ($prepend) {
90 $squelette = substr(find_in_path($prefix_path . $prepend . "$fond.$ext"), 0, -strlen(".$ext"));
91 if ($squelette) {
92 $flux['data'] = $squelette;
93 }
94 }
95
96 // gerer les squelettes non trouves
97 // -> router vers les /dist.html
98 // ou scaffolding ou page automatique les contenus
99 if (!$squelette) {
100
101 // si on est sur un ?page=XX non trouve
102 if ((isset($flux['args']['contexte'][$page])
103 and $flux['args']['contexte'][$page] == $fond)
104 or (isset($flux['args']['contexte']['type-page'])
105 and $flux['args']['contexte']['type-page'] == $fond)
106 or ($fond == 'sommaire'
107 and (!isset($flux['args']['contexte'][$page]) or !$flux['args']['contexte'][$page]))
108 ) {
109
110 // si on est sur un ?page=XX non trouve
111 // se brancher sur contenu/xx si il existe
112 // ou si c'est un objet spip, associe a une table, utiliser le fond homonyme
113 if (!isset($disponible[$fond])) {
114 $disponible[$fond] = z_contenu_disponible($prefix_path . $prepend, $z_contenu, $fond, $ext, $echafauder);
115 }
116
117 if ($disponible[$fond]) {
118 $flux['data'] = substr(find_in_path($prefix_path . "page.$ext"), 0, -strlen(".$ext"));
119 }
120 }
121
122 // echafaudage :
123 // si c'est un fond de contenu d'un objet en base
124 // generer un fond automatique a la volee pour les webmestres
125 elseif (strncmp($fond, "$z_contenu/", strlen($z_contenu) + 1) == 0) {
126 $type = substr($fond, strlen($z_contenu) + 1);
127 if (($type == 'page') and isset($flux['args']['contexte'][$page])) {
128 $type = $flux['args']['contexte'][$page];
129 }
130 if (!isset($disponible[$type])) {
131 $disponible[$type] = z_contenu_disponible($prefix_path . $prepend, $z_contenu, $type, $ext, $echafauder);
132 }
133 if (is_string($disponible[$type])) {
134 $flux['data'] = $disponible[$type];
135 } elseif ($echafauder
136 and include_spip('inc/autoriser')
137 and isset($GLOBALS['visiteur_session']['statut']) // performance
138 and autoriser('echafauder', $type)
139 and $is = $disponible[$type]
140 and is_array($is)
141 ) {
142 $flux['data'] = $echafauder($type, $is[0], $is[1], $is[2], $ext);
143 } else {
144 $flux['data'] = ($disponible['404'] = z_contenu_disponible($prefix_path . $prepend, $z_contenu, '404', $ext,
145 $echafauder));
146 }
147 }
148
149 // sinon, si on demande un fond non trouve dans un des autres blocs
150 // et si il y a bien un contenu correspondant ou echafaudable
151 // se rabbatre sur le dist.html du bloc concerne
152 else {
153 if ($dir = explode('/', $fond)
154 and $dir = reset($dir)
155 and $dir !== $z_contenu
156 and in_array($dir, $z_blocs)
157 ) {
158 $type = substr($fond, strlen("$dir/"));
159 if (($type == 'page') and isset($flux['args']['contexte'][$page])) {
160 $type = $flux['args']['contexte'][$page];
161 }
162 if ($type !== 'page' and !isset($disponible[$type])) {
163 $disponible[$type] = z_contenu_disponible($prefix_path . $prepend, $z_contenu, $type, $ext, $echafauder);
164 }
165 if ($type == 'page' or $disponible[$type]) {
166 $flux['data'] = z_trouver_bloc($prefix_path . $prepend, $dir, 'dist', $ext);
167 }
168 }
169 }
170 $squelette = $flux['data'];
171 }
172 // layout specifiques par type et compositions :
173 // body-article.html
174 // body-sommaire.html
175 // pour des raisons de perfo, les declinaisons doivent etre dans le
176 // meme dossier que body.html
177 if ($fond == 'body' and substr($squelette, -strlen($fond)) == $fond) {
178 if (isset($flux['args']['contexte']['type-page'])
179 and (
180 (isset($flux['args']['contexte']['composition'])
181 and file_exists(($f = $squelette . "-" . $flux['args']['contexte']['type-page'] . "-" . $flux['args']['contexte']['composition']) . ".$ext"))
182 or
183 file_exists(($f = $squelette . "-" . $flux['args']['contexte']['type-page']) . ".$ext")
184 )
185 ) {
186 $flux['data'] = $f;
187 }
188 } elseif ($fond == 'structure'
189 and z_sanitize_var_zajax()
190 and $f = find_in_path($prefix_path . $prepend . 'ajax' . ".$ext")
191 ) {
192 $flux['data'] = substr($f, 0, -strlen(".$ext"));
193 } // chercher le fond correspondant a la composition
194 elseif (isset($flux['args']['contexte']['composition'])
195 and (basename($fond) == 'page' or ($squelette and substr($squelette, -strlen($fond)) == $fond))
196 and $dir = substr($fond, $prefix_length)
197 and $dir = explode('/', $dir)
198 and $dir = reset($dir)
199 and in_array($dir, $z_blocs)
200 and $f = find_in_path($prefix_path . $prepend . $fond . "-" . $flux['args']['contexte']['composition'] . ".$ext")
201 ) {
202 $flux['data'] = substr($f, 0, -strlen(".$ext"));
203 }
204 }
205
206 return $flux;
207 }
208
209 /**
210 * Lister les blocs de la page selon le contexte prive/public
211 *
212 * @param bool $espace_prive
213 * @return array
214 */
215 function z_blocs($espace_prive = false) {
216 if ($espace_prive) {
217 return (isset($GLOBALS['z_blocs_ecrire']) ? $GLOBALS['z_blocs_ecrire'] : array(
218 'contenu',
219 'navigation',
220 'extra',
221 'head',
222 'hierarchie',
223 'top'
224 ));
225 }
226
227 return (isset($GLOBALS['z_blocs']) ? $GLOBALS['z_blocs'] : array('contenu'));
228 }
229
230 /**
231 * Vérifie qu'un type à un contenu disponible,
232 * soit parcequ'il a un fond, soit parce qu'il est echafaudable
233 *
234 * @param string $prefix_path
235 * @param string $z_contenu
236 * @param string $type
237 * @param string $ext
238 * @param bool $echafauder
239 * @return mixed
240 */
241 function z_contenu_disponible($prefix_path, $z_contenu, $type, $ext, $echafauder = true) {
242 if ($d = z_trouver_bloc($prefix_path, $z_contenu, $type, $ext)) {
243 return $d;
244 }
245
246 return $echafauder ? z_echafaudable($type) : false;
247 }
248
249 /**
250 * Teste si le fond de squelette trouvé est autorisé
251 *
252 * Compare le chemin du squelette trouvé avec les chemins exclus connus.
253 *
254 * @param string $squelette
255 * Un chemin de squelette
256 * @return bool
257 * `true` si on peut l'utiliser, `false` sinon.
258 **/
259 function z_fond_valide($squelette) {
260 if (!_ZCORE_EXCLURE_PATH
261 or !preg_match(',(' . _ZCORE_EXCLURE_PATH . ')/,', $squelette)
262 ) {
263 return true;
264 }
265
266 return false;
267 }
268
269 /**
270 * Trouve un bloc qui peut être sous le nom
271 * `contenu/article.html` ou `contenu/contenu.article.html`
272 *
273 * @param string $prefix_path
274 * chemin de base qui prefixe la recherche
275 * @param string $bloc
276 * nom du bloc cherche
277 * @param string $fond
278 * nom de la page (ou 'dist' pour le bloc par defaut)
279 * @param string $ext
280 * extension du squelette
281 * @return string
282 */
283 function z_trouver_bloc($prefix_path, $bloc, $fond, $ext) {
284 if (
285 (defined('_ZCORE_BLOC_PREFIX_SKEL') and $f = find_in_path("$prefix_path$bloc/$bloc.$fond.$ext") and z_fond_valide($f))
286 or ($f = find_in_path("$prefix_path$bloc/$fond.$ext") and z_fond_valide($f))
287 ) {
288 return substr($f, 0, -strlen(".$ext"));
289 }
290
291 return "";
292 }
293
294 /**
295 * Tester si un type est echafaudable
296 * c'est à dire s'il correspond bien à un objet en base
297 *
298 * @staticvar array $echafaudable
299 * @param string $type
300 * @return bool
301 */
302 function z_echafaudable($type) {
303 static $pages = null;
304 static $echafaudable = array();
305 if (isset($echafaudable[$type])) {
306 return $echafaudable[$type];
307 }
308 if (preg_match(',[^\w],', $type)) {
309 return $echafaudable[$type] = false;
310 }
311
312 if (test_espace_prive()) {
313 if (!function_exists('trouver_objet_exec')) {
314 include_spip('inc/pipelines_ecrire');
315 }
316 if ($e = trouver_objet_exec($type)) {
317 return $echafaudable[$type] = array($e['table'], $e['table_objet_sql'], $e);
318 } else {
319 // peut etre c'est un exec=types qui liste tous les objets "type"
320 if (($t = objet_type($type, false)) !== $type
321 and $e = trouver_objet_exec($t)
322 ) {
323 return $echafaudable[$type] = array($e['table'], $e['table_objet_sql'], $t);
324 }
325 }
326 } else {
327 if (is_null($pages)) {
328 $pages = array();
329 $liste = lister_tables_objets_sql();
330 foreach ($liste as $t => $d) {
331 if ($d['page']) {
332 $pages[$d['page']] = array($d['table_objet'], $t);
333 }
334 }
335 }
336 if (!isset($pages[$type])) {
337 return $echafaudable[$type] = false;
338 }
339 if (count($pages[$type]) == 2) {
340 $trouver_table = charger_fonction('trouver_table', 'base');
341 $pages[$type][] = $trouver_table(reset($pages[$type]));
342 }
343
344 return $echafaudable[$type] = $pages[$type];
345 }
346
347 return $echafaudable[$type] = false;
348 }
349
350
351 /**
352 * Generer a la volee un fond a partir d'un contenu connu
353 * tous les squelettes d'echafaudage du prive sont en fait explicites dans prive/echafaudage
354 * on ne fait qu'un mini squelette d'inclusion pour reecrire les variables d'env
355 *
356 * @param string $exec
357 * @param string $table
358 * @param string $table_sql
359 * @param array $desc_exec
360 * @param string $ext
361 * @return string
362 */
363 function prive_echafauder_dist($exec, $table, $table_sql, $desc_exec, $ext) {
364 $scaffold = "";
365
366 // page objet ou objet_edit
367 if (is_array($desc_exec)) {
368 $type = $desc_exec['type'];
369 $primary = $desc_exec['id_table_objet'];
370
371 if ($desc_exec['edition'] === false) {
372 $fond = "objet";
373 } else {
374 $trouver_table = charger_fonction('trouver_table', 'base');
375 $desc = $trouver_table($table_sql);
376 if (isset($desc['field']['id_rubrique'])) {
377 $fond = 'objet_edit';
378 } else {
379 $fond = 'objet_edit.sans_rubrique';
380 }
381 }
382 $dir = z_blocs(test_espace_prive());
383 $dir = reset($dir);
384 $scaffold = "<INCLURE{fond=prive/echafaudage/$dir/" . $fond . ",objet=" . $type . ",id_objet=#" . strtoupper($primary) . ",env}>";
385 } // page objets
386 elseif ($type = $desc_exec and strpos($type, "/") === false) {
387 $dir = z_blocs(test_espace_prive());
388 $dir = reset($dir);
389 $scaffold = "<INCLURE{fond=prive/echafaudage/$dir/objets,objet=" . $type . ",env} />";
390 }
391 // morceau d'objet : on fournit le fond de sibstitution dans $desc_exec
392 // et objet et tire de $table
393 elseif ($fond = $desc_exec) {
394 $dir = md5(dirname($fond));
395 $scaffold = "<INCLURE{fond=$fond,objet=" . objet_type($table) . ",env} />";
396 }
397
398 $base_dir = sous_repertoire(_DIR_CACHE, "scaffold", false);
399 $base_dir = sous_repertoire($base_dir, $dir, false);
400 $f = $base_dir . "$exec";
401 ecrire_fichier("$f.$ext", $scaffold);
402
403 return $f;
404 }
405
406 /**
407 * Recuperer et verifier var_zajax si demande dans l'url
408 *
409 * @return bool|string
410 */
411 function z_sanitize_var_zajax() {
412 $z_ajax = _request('var_zajax');
413 if (!$z_ajax) {
414 return false;
415 }
416 if (!$z_blocs = z_blocs(test_espace_prive())
417 or !in_array($z_ajax, $z_blocs)
418 ) {
419 set_request('var_zajax'); // enlever cette demande incongrue
420 $z_ajax = false;
421 }
422
423 return $z_ajax;
424 }