3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2011 *
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 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) return;
15 // http://doc.spip.org/@spip_xml_load
16 function spip_xml_load($fichier, $strict=true, $clean=true, $taille_max = 1048576, $datas='', $profondeur = -1){
18 if (preg_match(",^(http|ftp)://,",$fichier)){
19 include_spip('inc/distant');
20 $contenu = recuperer_page($fichier,false,false,$taille_max, $datas);
22 else lire_fichier ($fichier, $contenu);
25 $arbre = spip_xml_parse($contenu, $strict, $clean, $profondeur);
27 return count($arbre)?
$arbre:false;
30 if (!defined('_SPIP_XML_TAG_SPLIT')) define ('_SPIP_XML_TAG_SPLIT', "{<([^:>][^>]*?)>}sS");
31 // http://doc.spip.org/@spip_xml_parse
32 function spip_xml_parse(&$texte, $strict=true, $clean=true, $profondeur = -1){
34 // enlever les commentaires
37 if (preg_match(",<\?xml\s(.*?)encoding=['\"]?(.*?)['\"]?(\s(.*))?\?>,im",$texte,$regs))
39 $texte = preg_replace(',<!--(.*?)-->,is','',$texte);
40 $texte = preg_replace(',<\?(.*?)\?>,is','',$texte);
41 include_spip('inc/charsets');
43 //$texte = importer_charset($texte,$charset);
45 if (is_string($clean)) $charset = $clean;
48 // tant qu'il y a des tags
49 $chars = preg_split(_SPIP_XML_TAG_SPLIT
,$txt,2,PREG_SPLIT_DELIM_CAPTURE
);
50 while(count($chars)>=2){
52 //$chars = preg_split("{<([^>]*?)>}s",$txt,2,PREG_SPLIT_DELIM_CAPTURE);
54 // $before doit etre vide ou des espaces uniquements!
55 $before = trim($chars[0]);
57 if (strlen($before)>0)
58 return importer_charset($texte,$charset);//$texte; // before non vide, donc on est dans du texte
60 $tag = rtrim($chars[1]);
63 if (strncmp($tag,'![CDATA[',8)==0) return importer_charset($texte,$charset);//$texte;
64 if(substr($tag,-1)=='/'){ // self closing tag
65 $tag = rtrim(substr($tag,0,strlen($tag)-1));
69 $closing_tag = preg_split(",\s|\t|\n|\r,",trim($tag));
70 $closing_tag=reset($closing_tag);
72 $ncclos = strlen("</$closing_tag>");
73 $p = strpos($txt,"</$closing_tag>");
74 if ($p!==FALSE AND (strpos($txt,"<")<$p)){
75 $nclose =0; $nopen = 0;
79 AND ($morceau = substr($txt,$d,$p-$d))
80 AND (($nopen+
=preg_match_all("{<".preg_quote($closing_tag)."(\s*>|\s[^>]*[^/>]>)}is",$morceau,$matches,PREG_SET_ORDER
))>$nclose)
84 $p = strpos($txt,"</$closing_tag>",$d);
89 $out[$tag][]="erreur : tag fermant $tag manquant::$txt";
92 else return importer_charset($texte,$charset);//$texte // un tag qui constitue du texte a reporter dans $before
94 $content = substr($txt,0,$p);
95 $txt = substr($txt,$p+
$ncclos);
96 if ($profondeur==0 OR strpos($content,"<")===FALSE) // eviter une recursion si pas utile
97 $out[$tag][] = importer_charset($content,$charset);//$content;
99 $out[$tag][]=spip_xml_parse($content, $strict, $clean, $profondeur-1);
101 $chars = preg_split(_SPIP_XML_TAG_SPLIT
,$txt,2,PREG_SPLIT_DELIM_CAPTURE
);
103 if (count($out)&&(strlen(trim($txt))==0))
106 return importer_charset($texte,$charset);//$texte;
109 // http://doc.spip.org/@spip_xml_aplatit
110 function spip_xml_aplatit($arbre,$separateur = " "){
112 if (is_array($arbre))
113 foreach($arbre as $tag=>$feuille){
114 if (is_array($feuille)){
115 if ($tag!==intval($tag)){
116 $f = spip_xml_aplatit($feuille, $separateur);
118 $tagf = explode(" ",$tag);
120 $s.="<$tag>$f</$tagf>";
125 $s.=spip_xml_aplatit($feuille);
129 $s.="$feuille$separateur";
131 return strlen($separateur) ?
substr($s, 0, -strlen($separateur)) : $s;
134 // http://doc.spip.org/@spip_xml_tagname
135 function spip_xml_tagname($tag){
136 if (preg_match(',^([a-z][\w:]*),i',$tag,$reg))
140 // http://doc.spip.org/@spip_xml_decompose_tag
141 function spip_xml_decompose_tag($tag){
142 $tagname = spip_xml_tagname($tag);
145 $tag = substr($tag,$p);
148 $attr = trim(substr($tag,0,$p));
149 $tag = ltrim(substr($tag,$p+
1));
151 $p=strpos($tag,$quote,1);
152 $cont = substr($tag,1,$p-1);
153 $liste[$attr] = $cont;
154 $tag = substr($tag,$p+
1);
157 return array($tagname,$liste);
160 // http://doc.spip.org/@spip_xml_match_nodes
161 function spip_xml_match_nodes($regexp,&$arbre,&$matches){
162 if(is_array($arbre) && count($arbre))
163 foreach(array_keys($arbre) as $tag){
164 if (preg_match($regexp,$tag))
165 $matches[$tag] = &$arbre[$tag];
166 if (is_array($arbre[$tag]))
167 foreach(array_keys($arbre[$tag]) as $occurences)
168 spip_xml_match_nodes($regexp,$arbre[$tag][$occurences],$matches);
170 return (count($matches));