[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / ecrire / inc / xml.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
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 if (!defined('_ECRIRE_INC_VERSION')) return;
14
15 // http://doc.spip.org/@spip_xml_load
16 function spip_xml_load($fichier, $strict=true, $clean=true, $taille_max = 1048576, $datas='', $profondeur = -1){
17 $contenu = "";
18 if (preg_match(",^(http|ftp)://,",$fichier)){
19 include_spip('inc/distant');
20 $contenu = recuperer_page($fichier,false,false,$taille_max, $datas);
21 }
22 else lire_fichier ($fichier, $contenu);
23 $arbre = array();
24 if ($contenu)
25 $arbre = spip_xml_parse($contenu, $strict, $clean, $profondeur);
26
27 return count($arbre)?$arbre:false;
28 }
29
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){
33 $out = array();
34 // enlever les commentaires
35 $charset = 'AUTO';
36 if ($clean===true){
37 if (preg_match(",<\?xml\s(.*?)encoding=['\"]?(.*?)['\"]?(\s(.*))?\?>,im",$texte,$regs))
38 $charset = $regs[2];
39 $texte = preg_replace(',<!--(.*?)-->,is','',$texte);
40 $texte = preg_replace(',<\?(.*?)\?>,is','',$texte);
41 include_spip('inc/charsets');
42 $clean = $charset;
43 //$texte = importer_charset($texte,$charset);
44 }
45 if (is_string($clean)) $charset = $clean;
46 $txt = $texte;
47
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){
51 // tag ouvrant
52 //$chars = preg_split("{<([^>]*?)>}s",$txt,2,PREG_SPLIT_DELIM_CAPTURE);
53
54 // $before doit etre vide ou des espaces uniquements!
55 $before = trim($chars[0]);
56
57 if (strlen($before)>0)
58 return importer_charset($texte,$charset);//$texte; // before non vide, donc on est dans du texte
59
60 $tag = rtrim($chars[1]);
61 $txt = $chars[2];
62
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));
66 $out[$tag][]="";
67 }
68 else{
69 $closing_tag = preg_split(",\s|\t|\n|\r,",trim($tag));
70 $closing_tag=reset($closing_tag);
71 // tag fermant
72 $ncclos = strlen("</$closing_tag>");
73 $p = strpos($txt,"</$closing_tag>");
74 if ($p!==FALSE AND (strpos($txt,"<")<$p)){
75 $nclose =0; $nopen = 0;
76 $d = 0;
77 while (
78 $p!==FALSE
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)
81 ){
82 $nclose++;
83 $d=$p+$ncclos;
84 $p = strpos($txt,"</$closing_tag>",$d);
85 }
86 }
87 if ($p===FALSE){
88 if ($strict){
89 $out[$tag][]="erreur : tag fermant $tag manquant::$txt";
90 return $out;
91 }
92 else return importer_charset($texte,$charset);//$texte // un tag qui constitue du texte a reporter dans $before
93 }
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;
98 else
99 $out[$tag][]=spip_xml_parse($content, $strict, $clean, $profondeur-1);
100 }
101 $chars = preg_split(_SPIP_XML_TAG_SPLIT,$txt,2,PREG_SPLIT_DELIM_CAPTURE);
102 }
103 if (count($out)&&(strlen(trim($txt))==0))
104 return $out;
105 else
106 return importer_charset($texte,$charset);//$texte;
107 }
108
109 // http://doc.spip.org/@spip_xml_aplatit
110 function spip_xml_aplatit($arbre,$separateur = " "){
111 $s = "";
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);
117 if (strlen($f)) {
118 $tagf = explode(" ",$tag);
119 $tagf = $tagf[0];
120 $s.="<$tag>$f</$tagf>";
121 }
122 else $s.="<$tag />";
123 }
124 else
125 $s.=spip_xml_aplatit($feuille);
126 $s .= $separateur;
127 }
128 else
129 $s.="$feuille$separateur";
130 }
131 return strlen($separateur) ? substr($s, 0, -strlen($separateur)) : $s;
132 }
133
134 // http://doc.spip.org/@spip_xml_tagname
135 function spip_xml_tagname($tag){
136 if (preg_match(',^([a-z][\w:]*),i',$tag,$reg))
137 return $reg[1];
138 return "";
139 }
140 // http://doc.spip.org/@spip_xml_decompose_tag
141 function spip_xml_decompose_tag($tag){
142 $tagname = spip_xml_tagname($tag);
143 $liste = array();
144 $p=strpos($tag,' ');
145 $tag = substr($tag,$p);
146 $p=strpos($tag,'=');
147 while($p!==false){
148 $attr = trim(substr($tag,0,$p));
149 $tag = ltrim(substr($tag,$p+1));
150 $quote = $tag{0};
151 $p=strpos($tag,$quote,1);
152 $cont = substr($tag,1,$p-1);
153 $liste[$attr] = $cont;
154 $tag = substr($tag,$p+1);
155 $p=strpos($tag,'=');
156 }
157 return array($tagname,$liste);
158 }
159
160 // http://doc.spip.org/@spip_xml_match_nodes
161 function spip_xml_match_nodes($regexp,&$arbre,&$matches,$init=true){
162 if ($init)
163 $matches = array();
164 if(is_array($arbre) && count($arbre))
165 foreach(array_keys($arbre) as $tag){
166 if (preg_match($regexp,$tag))
167 $matches[$tag] = &$arbre[$tag];
168 if (is_array($arbre[$tag]))
169 foreach(array_keys($arbre[$tag]) as $occurences)
170 spip_xml_match_nodes($regexp,$arbre[$tag][$occurences],$matches,false);
171 }
172 return (count($matches));
173 }
174
175
176 ?>