PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/spip/ecrire/public/decompiler.php

https://github.com/eyeswebcrea/espace-couture-sittler.fr
PHP | 196 lines | 155 code | 22 blank | 19 comment | 30 complexity | a1aab8ca865727447fcbfb6d9247f6ec MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. /***************************************************************************\
  3. * SPIP, Systeme de publication pour l'internet *
  4. * *
  5. * Copyright (c) 2001-2011 *
  6. * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
  7. * *
  8. * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
  9. * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
  10. \***************************************************************************/
  11. if (!defined('_ECRIRE_INC_VERSION')) return;
  12. // Decompilation de l'arbre de syntaxe abstraite d'un squelette SPIP
  13. function decompiler_boucle($struct, $fmt='', $prof=0)
  14. {
  15. $nom = $struct->id_boucle;
  16. $avant = public_decompiler($struct->avant, $fmt, $prof);
  17. $apres = public_decompiler($struct->apres, $fmt, $prof);
  18. $altern = public_decompiler($struct->altern, $fmt, $prof);
  19. $milieu = public_decompiler($struct->milieu, $fmt, $prof);
  20. $type = $struct->sql_serveur ? "$struct->sql_serveur:" : '';
  21. $type .= ($struct->type_requete ? $struct->type_requete :
  22. $struct->table_optionnelle);
  23. if ($struct->jointures_explicites)
  24. $type .= " " . $struct->jointures_explicites;
  25. if ($struct->table_optionnelle)
  26. $type .= "?";
  27. // Revoir le cas de la boucle recursive
  28. $crit = $struct->param;
  29. if ($crit AND !is_array($crit[0])) {
  30. $type = strtolower($type) . array_shift($crit);
  31. }
  32. $crit = decompiler_criteres($crit, $struct->criteres, $fmt, $prof) ;
  33. $f = 'format_boucle_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES);
  34. return $f($avant, $nom, $type, $crit, $milieu, $apres, $altern, $prof);
  35. }
  36. function decompiler_include($struct, $fmt='', $prof=0)
  37. {
  38. $res = array();
  39. foreach($struct->param ? $struct->param : array() as $couple) {
  40. array_shift($couple);
  41. foreach($couple as $v) {
  42. $res[]= public_decompiler($v, $fmt, $prof);
  43. }
  44. }
  45. $file = is_string($struct->texte) ? $struct->texte :
  46. public_decompiler($struct->texte, $fmt, $prof);
  47. $f = 'format_inclure_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES);
  48. return $f($file, $res, $prof);
  49. }
  50. function decompiler_texte($struct, $fmt='', $prof=0)
  51. {
  52. $f = 'format_texte_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES);
  53. return strlen($struct->texte) ? $f($struct->texte, $prof) : '';
  54. }
  55. function decompiler_polyglotte($struct, $fmt='', $prof=0)
  56. {
  57. $f = 'format_polyglotte_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES);
  58. return $f($struct->traductions, $prof);
  59. }
  60. function decompiler_idiome($struct, $fmt='', $prof=0)
  61. {
  62. $module = ($struct->module == MODULES_IDIOMES)? ''
  63. : $struct->module;
  64. $args = array();
  65. foreach ($struct->arg as $k => $v) {
  66. if ($k) $args[$k]= public_decompiler($v, $fmt, $prof);
  67. }
  68. $filtres = decompiler_liste($struct->param, $fmt, $prof);
  69. $f = 'format_idiome_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES);
  70. return $f($struct->nom_champ, $module, $args, $filtres, $prof);
  71. }
  72. function decompiler_champ($struct, $fmt='', $prof=0)
  73. {
  74. $avant = public_decompiler($struct->avant, $fmt, $prof);
  75. $apres = public_decompiler($struct->apres, $fmt, $prof);
  76. $args = $filtres = '';
  77. if ($p = $struct->param) {
  78. if ($p[0][0]==='')
  79. $args = decompiler_liste(array(array_shift($p)), $fmt, $prof);
  80. $filtres = decompiler_liste($p, $fmt, $prof);
  81. }
  82. $f = 'format_champ_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES);
  83. return $f($struct->nom_champ, $struct->nom_boucle, $struct->etoile, $avant, $apres, $args, $filtres, $prof);
  84. }
  85. function decompiler_liste($sources, $fmt='', $prof=0) {
  86. if (!is_array($sources)) return '';
  87. $f = 'format_liste_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES);
  88. $res = '';
  89. foreach($sources as $arg) {
  90. if (!is_array($arg)) {
  91. continue; // ne devrait pas arriver.
  92. } else {$r = array_shift($arg);}
  93. $args = array();
  94. foreach($arg as $v) {
  95. // cas des arguments entoures de ' ou "
  96. if ((count($v) == 1)
  97. AND $v[0]->type=='texte'
  98. AND (strlen($v[0]->apres) == 1)
  99. AND $v[0]->apres == $v[0]->avant)
  100. $args[]= $v[0]->avant . $v[0]->texte . $v[0]->apres;
  101. else $args[]= public_decompiler($v, $fmt, 0-$prof);
  102. }
  103. if (($r!=='') OR $args) $res .= $f($r, $args, $prof);
  104. }
  105. return $res;
  106. }
  107. // Decompilation des criteres: on triche et on deroge:
  108. // - le phraseur fournit un bout du source en plus de la compil
  109. // - le champ apres signale le critere {"separateur"} ou {'separateur'}
  110. // - les champs sont implicitement etendus (crochets implicites mais interdits)
  111. function decompiler_criteres($sources, $comp, $fmt='', $prof=0) {
  112. if (!is_array($sources)) return '';
  113. $res = '';
  114. $f = 'format_critere_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES);
  115. include_spip('public/format_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES));
  116. foreach($sources as $crit) {
  117. if (!is_array($crit)) continue; // boucle recursive
  118. array_shift($crit);
  119. $args = array();
  120. foreach($crit as $i => $v) {
  121. if ((count($v) == 1)
  122. AND $v[0]->type=='texte'
  123. AND $v[0]->apres)
  124. $args[]= array(array('texte', ( $v[0]->apres . $v[0]->texte . $v[0]->apres)));
  125. else {
  126. $res2 = array();
  127. foreach($v as $k => $p) {
  128. if (isset($p->type)
  129. AND function_exists($d = 'decompiler_' . $p->type)) {
  130. $r = $d($p, $fmt, (0-$prof), @$v[$k+1]);
  131. $res2[]= array($p->type, $r);
  132. } else spip_log("critere $i / $k mal forme");
  133. }
  134. $args[]= $res2;
  135. }
  136. }
  137. $res .= $f($args);
  138. }
  139. return $res;
  140. }
  141. function public_decompiler($liste, $fmt='', $prof=0)
  142. {
  143. if (!is_array($liste)) return '';
  144. include_spip('public/format_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES));
  145. $prof2 = ($prof < 0) ? ($prof-1) : ($prof+1);
  146. $contenu = array();
  147. foreach($liste as $k => $p) {
  148. if (!isset($p->type)) continue; #??????
  149. $d = 'decompiler_' . $p->type;
  150. $next = isset($liste[$k+1]) ? $liste[$k+1] : false;
  151. // Forcer le champ etendu si son source (pas les reecritures)
  152. // contenait des args et s'il est suivi d'espaces,
  153. // le champ simple les eliminant est un bug helas perenne.
  154. if ($next
  155. AND ($next->type == 'texte')
  156. AND $p->type == 'champ'
  157. AND !$p->apres
  158. AND !$p->avant
  159. AND $p->fonctions) {
  160. $n = strlen($next->texte) - strlen(ltrim($next->texte));
  161. if ($n) {
  162. $champ = new Texte;
  163. $champ->texte = substr($next->texte, 0, $n);
  164. $champ->ligne = $p->ligne;
  165. $p->apres = array($champ);
  166. $next->texte = substr($next->texte, $n);
  167. }
  168. }
  169. $contenu[] = array($d($p, $fmt, $prof2), $p->type);
  170. }
  171. $f = 'format_suite_' . ($fmt ? $fmt : _EXTENSION_SQUELETTES);
  172. return $f($contenu);
  173. }
  174. ?>