PageRenderTime 56ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/acs/branches/v2/inc/composant/page_source.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 291 lines | 229 code | 32 blank | 30 comment | 47 complexity | 9030a00a8a0a2dcd4f43ba884b60fb1c MD5 | raw file
  1. <?php
  2. # ACS
  3. # (Plugin Spip)
  4. # http://acs.geomaticien.org
  5. #
  6. # Copyright Daniel FAIVRE, 2007-2012
  7. # Copyleft: licence GPL - Cf. LICENCES.txt
  8. include_spip('inc/composant/composants_variables');
  9. include_spip('inc/composant/pages_liste');
  10. /**
  11. * Analyse une page
  12. * Retourne un tableau des variables ACS, des balises et des inclusions
  13. */
  14. function analyse_page($page, $mode_source) {
  15. $modeles = pages_liste();
  16. $modeles = $modeles['modeles'];
  17. if (is_array($modeles) && (count($modeles) > 0)) {
  18. foreach ($modeles as $m=>$src) {
  19. $modeles_regexp .= '#'.strtoupper($m).'|';
  20. }
  21. $modeles_regexp = substr($modeles_regexp, 0, -1);
  22. }
  23. $balises = explode(', ', liste_balises());
  24. usort($balises, "compare_taille");
  25. $balises = '#'.implode('|#', $balises);
  26. // Quelques définitions de regexp :
  27. // capture tout entre accolades jusqu'à trois niveaux d'imbrication
  28. $reg_entre_accolades = '(?:\{(?:(?:[^\}]*\{(?:[^\}]*\{[^\}]*\})[\s]*\})*[\s]*|(?:[^\}]*\{[^\}]*\})*[\s]*|[^}]*)*\})';
  29. // filtre spip
  30. $reg_filtre_spip = '(?:\|(?:==|!=|\?)*[\w]*(?:'.$reg_entre_accolades.')*)';
  31. // Les types de tags spip repérés en mode schéma
  32. $reg[] = array('REM', 2, '(\[\(#REM\)([^\]]*)\])');
  33. if ($modeles_regexp)
  34. $reg[] = array('MODELE', 3, '(('.$modeles_regexp.')('.$reg_entre_accolades.'|'.$reg_filtre_spip.')*)');
  35. $reg[] = array('INCLURE', 3, '((?:<|#)INCLU[R|D]E[\s]*\{fond=([^\}]*)\}((?:[\s]*'.$reg_entre_accolades.')*)>?)');
  36. $reg[] = array('BOUCLE', 4, '(<BOUCLE[_]?([^(]*)\(([^)]*)\)((?:>=|[^\>])*)>)');
  37. $reg[] = array('FIN_BOUCLE', 2, '(<\/BOUCLE[_]?([^>]*)>)');
  38. if ($mode_source) {
  39. $reg[] = array('B', 1, '(<B_[^>]*>)');
  40. $reg[] = array('FIN_B', 1, '(<\/B_[^>]*>)');
  41. $reg[] = array('TRAD', 1, '(<:[\w]*:>)');
  42. }
  43. $reg[] = array('BALISE', 3, '(('.$balises.')('.$reg_entre_accolades.'|'.$reg_filtre_spip.')*)');
  44. $def = array();
  45. foreach ($reg as $rx) {
  46. $def[] = array('spip_tag' => $rx[0], 'nb' => $rx[1]);
  47. $regexp .= $rx[2].'|';
  48. }
  49. // La structure $analyse détermine l'analyse recursive avec regexp
  50. $analyse = array(
  51. 'regexp' => '/'.substr($regexp, 0, -1).'/s',
  52. 'regdef' => $def,
  53. 'mode_source' => $mode_source
  54. );
  55. return page_includes($page, $analyse);
  56. }
  57. // Analyse séquentiellement la page, et retourne un tableau constitué
  58. // d'un tableau des variables ACS trouveés
  59. // et d'un tableau des contenus avec leurs positions.de début et de fin
  60. // (fonction récursive)
  61. function page_includes(
  62. $texte, // Texte à analyser
  63. &$analyse, // Structure d'analyse
  64. &$includes=array(
  65. 'tags' => array(),
  66. 'vars' => array()
  67. ), // tableau des tages et des variables trouvés
  68. $offset=0, // offset depuis le début de $pg
  69. $tagoffset=0 // offset depuis le début du tag
  70. ) {
  71. static $k;
  72. if ($k > 9999) return $includes;
  73. $k++;
  74. if (preg_match($analyse['regexp'], $texte, $matches, PREG_OFFSET_CAPTURE, $offset)) {
  75. //print_r($matches);
  76. $indice = 1;
  77. foreach($analyse['regdef'] as $capture) {
  78. if ($matches[$indice][0]) {
  79. $args = array();
  80. $contenu = '';
  81. //echo "<br>".$capture['f']."<br>";
  82. $matched = $matches[$indice][0];
  83. $debut = $matches[$indice][1];
  84. $fin = $debut + strlen($matched);
  85. for ($i = $indice; $i < $indice + $capture['nb']; $i++) {
  86. //echo " [".($i)."][0]=".htmlspecialchars($matches[$i][0])."<br>";
  87. $args[] = $matches[$i][0];
  88. }
  89. if (is_callable('pi_'.$capture['spip_tag'])) {
  90. $contenu = call_user_func('pi_'.$capture['spip_tag'], array($args, &$analyse, &$includes));
  91. if (is_array($contenu)) {
  92. if ($contenu[1])
  93. $includes['vars'][] = $contenu[1];
  94. $contenu = $contenu[0];
  95. }
  96. }
  97. if ($contenu)
  98. $includes['tags'][$tagoffset + $debut] = array(
  99. 'fin' => $tagoffset + $fin,
  100. 'contenu' => $contenu,
  101. 'type' => $capture['spip_tag']
  102. );
  103. page_includes($matched, $analyse, $includes, 1, $tagoffset + $debut); // Recherche des imbrications
  104. }
  105. $indice = $indice + $capture['nb'];
  106. }
  107. page_includes($texte, $analyse, $includes, $fin, $tagoffset); // Va au tag suivant
  108. }
  109. return $includes;
  110. }
  111. function pi_REM($args) {
  112. if ($args[1]['mode_source']) return $args[0][0];
  113. return '<div class="spip_params onlinehelp pliable">'.nl2br($args[0][1]).'</div>';
  114. }
  115. function pi_MODELE($args) {
  116. if ($args[1]['mode_source']) return $args[0][0];
  117. $m = $args[0][1];
  118. $p = $args[0][2];
  119. $r = indent($args[1]['indentation']).'<span class="col_MODELE" title="'._T('acs:modele').' '.strtolower(substr($m, 1)).($p ? ' '.htmlspecialchars($p) : '').'" style="color:#4f00af">'.$m;
  120. if ($p)
  121. $r .= ' <span class="spip_params pliable">'.indent($args[1]['indentation']).str_replace('}{', '} {', htmlspecialchars($p)).'</span>';
  122. $r .= '</span>';
  123. return $r;
  124. }
  125. function pi_INCLURE($args) {
  126. if ($args[1]['mode_source']) return $args[0][0];
  127. $include = $args[0][1];
  128. $param = $args[0][2];
  129. if (preg_match('/{nic=(\d*)\}/', $args[0][0], $matches)) {
  130. $nic = $matches[1];
  131. }
  132. // Inclusion d'un composant
  133. if (substr($include, 0, 11) == 'composants/') {
  134. $r = affiche_widgy($include, $param, $args[1]['indentation'], $nic);
  135. }
  136. else {
  137. // inclusion classique
  138. if (find_in_path($include.'.html'))
  139. $r = indent($args[1]['indentation']).'<a class="'.get_widget_class($include, $param['on'], 'widgy').' lien_page" style="background: none" href="?exec=acs&onglet=page&pg='.$include.'" onclick=\'$("#page_infos").empty();
  140. AjaxSqueeze("?exec=acs_page_get_infos&pg=" + this.text, "page_infos");
  141. return false;\' title="'.$param.'">'.$include.'</a>';
  142. else
  143. $r = indent($args[1]['indentation']).'<a class="'.get_widget_class($include, $param['on'], 'widgy').' lien_page" style="background: #efefef; border-style: solid; color: red; text-decoration: blink; " title="'._T('acs:err_fichier_absent', array('file' => $include)).'">'.$include.'</a>';
  144. }
  145. $r = $r;
  146. if ($param)
  147. $r .= '<div class="spip_params pliable">'.indent($args[1]['indentation']).str_replace('}{', '} {', $param).'</div>';
  148. return $r;
  149. }
  150. function pi_BOUCLE($args) {
  151. if ($args[1]['mode_source']) return $args[0][0];
  152. $boucle = $args[0][1];
  153. $type = $args[0][2];
  154. $param = $args[0][3];
  155. $r = '<div class="col_BOUCLE" title="'._T('acs:boucle').' '.strtolower($type).' '.$param.'">'.indent($args[1]['indentation']).$boucle.' ('.$type.')';
  156. if ($param)
  157. $r .= indent($args[1]['indentation']).' <span class="spip_params pliable">'.indent($args[1]['indentation']).str_replace('}{', '} {', $param).'</span>';
  158. $r .= '</div>';
  159. $args[1]['indentation']++;
  160. return $r;
  161. }
  162. function pi_FIN_BOUCLE($args) {
  163. if ($args[1]['mode_source']) return $args[0][0];
  164. $args[1]['indentation']--;
  165. return '<div class="col_FIN_BOUCLE">'.indent($args[1]['indentation']).'/'.$args[0][1].'</div>';
  166. }
  167. function pi_BALISE($args) {
  168. $balise = substr($args[0][1], 1);
  169. // on a trouve une variable ACS presumee
  170. if ($balise == 'VAR') {
  171. // On lit le contenu entre accolades apres #VAR
  172. if (preg_match('/{acs(\w*)\}/', $args[0][2], $matches)) {
  173. $var = $matches[1];
  174. // on verifie que c'est bien une variable ACS :
  175. $lv = liste_variables();
  176. if (in_array($var, array_keys($lv))) {
  177. $c = $lv[$var]['c'];
  178. $html = '<a class="col_VAR" href="?exec=acs&amp;onglet=composants&amp;composant='.$c.($lv['c']['nic'] ? '&amp;nic='.$lv['c']['nic']: '').'" title="'._T('acs:composant').' '.ucfirst(str_replace('_', ' ', $c)).'">acs'.$var.'</a>';
  179. }
  180. }
  181. }
  182. return array(($args[1]['mode_source'] ? $args[0][0] : false), $html);
  183. }
  184. function affiche_widgy($include, $param, $indentation, $nic) {
  185. $include = substr($include, 11);
  186. if ($pos = strpos($include, '/')) {
  187. if (substr($include, 0, $pos) == substr($include, $pos + 1)) {
  188. $label = substr($include, 0, $pos);
  189. }
  190. else {
  191. $label = $include;
  192. }
  193. $composant = substr($include, 0, $pos);
  194. }
  195. return widgy($composant, $param, $label, $indentation, $nic);
  196. }
  197. function widgy($composant, $param, $label='', $indentation=0, $nic = '', $recursive_indent = 0, $in_horizontal = false) {
  198. $label = ucfirst(str_replace('_', ' ', $label));
  199. $horizontal = ($GLOBALS['meta']['acs'.ucfirst($composant).$nic.'Orientation'] == "horizontal") ? true : false;
  200. $content .= $horizontal ? '<tr>' : '';
  201. // On recherche ce que contient le widgy, recursivement
  202. $cv = composants_variables();
  203. if (is_array($cv) && is_array($cv[$composant]) && is_array($cv[$composant]['vars'])) {
  204. foreach($cv[$composant]['vars'] as $varname=>$v) {
  205. if ($v['type'] != 'widget')
  206. continue;
  207. $var = 'acs'.ucfirst($composant).$nic.ucfirst($varname);
  208. if (isset($GLOBALS['meta'][$var]) && $GLOBALS['meta'][$var]) {
  209. $ci = explode('-', $GLOBALS['meta'][$var]);
  210. $cinom = $ci[0];
  211. $cinic = $ci[1];
  212. $cilabel = $cinom;
  213. $content .= (!$horizontal ? '<tr>' : '');
  214. $content .= '<td class="widgy_included_label"><a class="nompage" href="?exec=acs&onglet=composants&composant='.$composant.($nic ? '&nic='.$nic : '').'" title="acs'.$varname.'">'.substr($varname, strlen($composant.$nic)).'</a>'.widgy($cinom, '', $cilabel, $indentation, $cinic, 1, $horizontal).'</td>';
  215. $content .= (!$horizontal ? '</tr>' : '');
  216. }
  217. }
  218. }
  219. $content .= $horizontal ? '</tr>' : '';
  220. // On recupere le Nom du composant
  221. $cvn = 'acs'.ucfirst($composant).$nic.'Nom';
  222. if (isset($GLOBALS['meta'][$cvn])) {
  223. $lbl = $GLOBALS['meta'][$cvn];
  224. $title = $label.($nic ? ' '.$nic : ''). ' ('.$lbl.')';
  225. $lbl = str_replace(' ', '&nbsp;', couper(typo($lbl), 18));
  226. }
  227. else {
  228. $lbl = $label.($nic ? '&nbsp;'.$nic : '');
  229. $title = _T('acs:composant').' '.$label.' '.$nic;
  230. }
  231. $titleHTML = ' title="'.$title.'"';
  232. // affichage du contenu du widgy avec l'indentation voulue
  233. $indentationHTML = $in_horizontal ? '' : '<td>'.indent($indentation + $recursive_indent).'</td>';
  234. $over = $cv[$composant]['over'];
  235. $on = ($GLOBALS['meta']['acs'.ucfirst($composant).$nic.'Use'] == "oui") ? true : false;;
  236. $r = '<table><tr>'.$indentationHTML.'<td'.($recursive_indent ? ' class="widgy_included"' : '').'>';
  237. $r .= '<table><tr><th><a class="'.get_widget_class($over, $on, 'widgy').'" href="?exec=acs&onglet=composants&composant='.$composant.($nic ? '&nic='.$nic : '').'"'.$titleHTML.'>'.widget_icon($composant, $nic, 10).'&nbsp;'.$lbl.'</a></th></tr>';
  238. $r .= $content;
  239. $r .= '</table></td></tr></table>';
  240. return $r;
  241. }
  242. function indent($l) {
  243. for($i = 0; $i < $l; $i++) {
  244. $r .= '&nbsp;&nbsp;&nbsp;';
  245. }
  246. return $r;
  247. }
  248. function compare_taille($a, $b) {
  249. if (strlen($a) == strlen($b)) {
  250. return 0;
  251. }
  252. return (strlen($a) > strlen($b)) ? -1 : 1;
  253. }
  254. function liste_balises() {
  255. $balises_acs = 'INTRO, ACS_CHEMIN, ACS_DERNIERE_MODIF, HEADER_COMPOSANTS, PINCEAU, VAR, ACS_VERSION, ACS_RELEASE';
  256. $balises_spip = 'ANCRE_PAGINATION, ARRAY, ANCRE_PAGINATION, ARRAY, BIO, CACHE, CHAPO, CHARSET, CHEMIN, COMPTEUR_BOUCLE, CONFIG, DATE, DATE_MODIF, DATE_NOUVEAUTES, DATE_REDAC, DEBUT_SURLIGNE, DESCRIPTIF, DESCRIPTIF_SITE_SPIP, DISTANT, DOSSIER_SQUELETTE, EDIT, EMAIL, EMAIL_WEBMASTER, EMBED_DOCUMENT, ENV, EVAL, EXPOSE, EXPOSER, FICHIER, FIN_SURLIGNE, FORMULAIRE_ADMIN, FORMULAIRE_ECRIRE_AUTEUR, FORMULAIRE_FORUM, FORMULAIRE_INSCRIPTION, FORMULAIRE_RECHERCHE, FORMULAIRE_SIGNATURE, FORMULAIRE_SITE, GET, GRAND_TOTAL, HAUTEUR, HTTP_HEADER, ID_ARTICLE, ID_AUTEUR, ID_BREVE, ID_DOCUMENT, ID_FORUM, ID_GROUPE, ID_MOT, ID_PARENT, ID_RUBRIQUE, ID_SECTEUR, ID_SIGNATURE, ID_SYNDIC, ID_SYNDIC_ARTICLE, ID_THREAD, INCLURE, INSERT_HEAD, INTRODUCTION, IP, LANG, LANG_DIR, LANG_LEFT, LANG_RIGHT, LARGEUR, LESAUTEURS, LOGIN_PRIVE, LOGIN_PUBLIC, LOGO_ARTICLE, LOGO_ARTICLE_NORMAL, LOGO_ARTICLE_RUBRIQUE, LOGO_ARTICLE_SURVOL, LOGO_AUTEUR, LOGO_AUTEUR_NORMAL, LOGO_AUTEUR_SURVOL, LOGO_BREVE, LOGO_BREVE_RUBRIQUE, LOGO_DOCUMENT, LOGO_MOT, LOGO_RUBRIQUE, LOGO_RUBRIQUE_NORMAL, LOGO_RUBRIQUE_SURVOL, LOGO_SITE, LOGO_SITE_SPIP, MENU_LANG, MENU_LANG_ECRIRE, MESSAGE, MIME_TYPE, MODELE, NOM, NOM_SITE, NOM_SITE_SPIP, NOTES, PAGINATION, PARAMETRES_FORUM, PETITION, PGP, PIPELINE, POINTS, POPULARITE, POPULARITE_ABSOLUE, POPULARITE_MAX, POPULARITE_SITE, PS, PUCE, RECHERCHE, SELF, SET, SOURCE, SOUSTITRE, SPIP_CRON, SPIP_VERSION, SQUELETTE, SURTITRE, TAGS, TAILLE, TEXTE, TITRE, TOTAL_BOUCLE, TOTAL_UNIQUE, TYPE, TYPE_DOCUMENT, URL_ACTION_AUTEUR, URL_ARTICLE, URL_AUTEUR, URL_BREVE, URL_DOCUMENT, URL_FORUM, URL_LOGOUT, URL_MOT, URL_PAGE, URL_RUBRIQUE, URL_SITE, URL_SITE_SPIP, URL_SOURCE, URL_SYNDIC, VISITES';
  257. return $balises_acs.', '.$balises_spip;
  258. }
  259. ?>