0eead0a6adabf090ed3bd1033106b864b409e9b3
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2017 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
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 \***************************************************************************/
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
17 * Recherche par exemple `contenu\xx` et en absence utilisera `contenu\dist`
19 * @package SPIP\Core\Public\Styliser
21 if (!defined("_ECRIRE_INC_VERSION")) {
26 * Recherche automatique d'un squelette Page à partir de `contenu/xx`
28 * @param array $flux Données du pipeline
29 * @return array Données modifiées du pipeline
31 function public_styliser_par_z_dist($flux) {
32 static $prefix_path = null;
33 static $prefix_length;
37 static $disponible = array();
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';
48 $echafauder = charger_fonction('echafauder', 'prive', true);
49 define('_ZCORE_EXCLURE_PATH', '');
53 $apl_constant = '_Z_AJAX_PARALLEL_LOAD';
55 $echafauder = charger_fonction('echafauder', 'public', true);
56 define('_ZCORE_EXCLURE_PATH', '\bprive|\bsquelettes-dist' . (defined('_DIR_PLUGIN_DIST') ?
'|\b' . rtrim(_DIR_PLUGIN_DIST
,
59 $prepend = (defined('_Z_PREPEND_PATH') ? _Z_PREPEND_PATH
: "");
61 $z_contenu = reset($z_blocs); // contenu par defaut
63 $fond = $flux['args']['fond'];
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
79 $flux['data'] = $pipe;
84 // surcharger aussi les squelettes venant de squelettes-dist/
85 if ($squelette and !z_fond_valide($squelette)) {
90 $squelette = substr(find_in_path($prefix_path . $prepend . "$fond.$ext"), 0, -strlen(".$ext"));
92 $flux['data'] = $squelette;
96 // gerer les squelettes non trouves
97 // -> router vers les /dist.html
98 // ou scaffolding ou page automatique les contenus
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]))
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);
117 if ($disponible[$fond]) {
118 $flux['data'] = substr(find_in_path($prefix_path . "page.$ext"), 0, -strlen(".$ext"));
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];
130 if (!isset($disponible[$type])) {
131 $disponible[$type] = z_contenu_disponible($prefix_path . $prepend, $z_contenu, $type, $ext, $echafauder);
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]
142 $flux['data'] = $echafauder($type, $is[0], $is[1], $is[2], $ext);
144 $flux['data'] = ($disponible['404'] = z_contenu_disponible($prefix_path . $prepend, $z_contenu, '404', $ext,
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
153 if ($dir = explode('/', $fond)
154 and $dir = reset($dir)
155 and $dir !== $z_contenu
156 and in_array($dir, $z_blocs)
158 $type = substr($fond, strlen("$dir/"));
159 if (($type == 'page') and isset($flux['args']['contexte'][$page])) {
160 $type = $flux['args']['contexte'][$page];
162 if ($type !== 'page' and !isset($disponible[$type])) {
163 $disponible[$type] = z_contenu_disponible($prefix_path . $prepend, $z_contenu, $type, $ext, $echafauder);
165 if ($type == 'page' or $disponible[$type]) {
166 $flux['data'] = z_trouver_bloc($prefix_path . $prepend, $dir, 'dist', $ext);
170 $squelette = $flux['data'];
172 // layout specifiques par type et compositions :
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'])
180 (isset($flux['args']['contexte']['composition'])
181 and file_exists(($f = $squelette . "-" . $flux['args']['contexte']['type-page'] . "-" . $flux['args']['contexte']['composition']) . ".$ext"))
183 file_exists(($f = $squelette . "-" . $flux['args']['contexte']['type-page']) . ".$ext")
188 } elseif ($fond == 'structure'
189 and z_sanitize_var_zajax()
190 and $f = find_in_path($prefix_path . $prepend . 'ajax' . ".$ext")
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")
202 $flux['data'] = substr($f, 0, -strlen(".$ext"));
210 * Lister les blocs de la page selon le contexte prive/public
212 * @param bool $espace_prive
215 function z_blocs($espace_prive = false) {
217 return (isset($GLOBALS['z_blocs_ecrire']) ?
$GLOBALS['z_blocs_ecrire'] : array(
227 return (isset($GLOBALS['z_blocs']) ?
$GLOBALS['z_blocs'] : array('contenu'));
231 * Vérifie qu'un type à un contenu disponible,
232 * soit parcequ'il a un fond, soit parce qu'il est echafaudable
234 * @param string $prefix_path
235 * @param string $z_contenu
236 * @param string $type
238 * @param bool $echafauder
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)) {
246 return $echafauder ?
z_echafaudable($type) : false;
250 * Teste si le fond de squelette trouvé est autorisé
252 * Compare le chemin du squelette trouvé avec les chemins exclus connus.
254 * @param string $squelette
255 * Un chemin de squelette
257 * `true` si on peut l'utiliser, `false` sinon.
259 function z_fond_valide($squelette) {
260 if (!_ZCORE_EXCLURE_PATH
261 or !preg_match(',(' . _ZCORE_EXCLURE_PATH
. ')/,', $squelette)
270 * Trouve un bloc qui peut être sous le nom
271 * `contenu/article.html` ou `contenu/contenu.article.html`
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)
280 * extension du squelette
283 function z_trouver_bloc($prefix_path, $bloc, $fond, $ext) {
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))
288 return substr($f, 0, -strlen(".$ext"));
295 * Tester si un type est echafaudable
296 * c'est à dire s'il correspond bien à un objet en base
298 * @staticvar array $echafaudable
299 * @param string $type
302 function z_echafaudable($type) {
303 static $pages = null;
304 static $echafaudable = array();
305 if (isset($echafaudable[$type])) {
306 return $echafaudable[$type];
308 if (preg_match(',[^\w],', $type)) {
309 return $echafaudable[$type] = false;
312 if (test_espace_prive()) {
313 if (!function_exists('trouver_objet_exec')) {
314 include_spip('inc/pipelines_ecrire');
316 if ($e = trouver_objet_exec($type)) {
317 return $echafaudable[$type] = array($e['table'], $e['table_objet_sql'], $e);
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)
323 return $echafaudable[$type] = array($e['table'], $e['table_objet_sql'], $t);
327 if (is_null($pages)) {
329 $liste = lister_tables_objets_sql();
330 foreach ($liste as $t => $d) {
332 $pages[$d['page']] = array($d['table_objet'], $t);
336 if (!isset($pages[$type])) {
337 return $echafaudable[$type] = false;
339 if (count($pages[$type]) == 2) {
340 $trouver_table = charger_fonction('trouver_table', 'base');
341 $pages[$type][] = $trouver_table(reset($pages[$type]));
344 return $echafaudable[$type] = $pages[$type];
347 return $echafaudable[$type] = false;
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
356 * @param string $exec
357 * @param string $table
358 * @param string $table_sql
359 * @param array $desc_exec
363 function prive_echafauder_dist($exec, $table, $table_sql, $desc_exec, $ext) {
366 // page objet ou objet_edit
367 if (is_array($desc_exec)) {
368 $type = $desc_exec['type'];
369 $primary = $desc_exec['id_table_objet'];
371 if ($desc_exec['edition'] === false) {
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';
379 $fond = 'objet_edit.sans_rubrique';
382 $dir = z_blocs(test_espace_prive());
384 $scaffold = "<INCLURE{fond=prive/echafaudage/$dir/" . $fond . ",objet=" . $type . ",id_objet=#" . strtoupper($primary) . ",env}>";
386 elseif ($type = $desc_exec and strpos($type, "/") === false) {
387 $dir = z_blocs(test_espace_prive());
389 $scaffold = "<INCLURE{fond=prive/echafaudage/$dir/objets,objet=" . $type . ",env} />";
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} />";
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);
407 * Recuperer et verifier var_zajax si demande dans l'url
409 * @return bool|string
411 function z_sanitize_var_zajax() {
412 $z_ajax = _request('var_zajax');
416 if (!$z_blocs = z_blocs(test_espace_prive())
417 or !in_array($z_ajax, $z_blocs)
419 set_request('var_zajax'); // enlever cette demande incongrue