Newer
Older
cerdic
a validé
<?php
/***************************************************************************\
* SPIP, Systeme de publication pour l'internet *
* *
cerdic
a validé
* Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
* *
* Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
* Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
\***************************************************************************/
if (!defined("_ECRIRE_INC_VERSION")) return;
// http://doc.spip.org/@spip_xml_load
cerdic
a validé
function spip_xml_load($fichier, $strict=true, $clean=true, $taille_max = 1048576, $datas=''){
cerdic
a validé
$contenu = "";
if (preg_match(",^(http|ftp)://,",$fichier)){
cerdic
a validé
$contenu = recuperer_page($fichier,false,false,$taille_max, $datas);
cerdic
a validé
}
else lire_fichier ($fichier, $contenu);
$arbre = array();
if ($contenu)
$arbre = spip_xml_parse($contenu, $strict, $clean);
cerdic
a validé
return count($arbre)?$arbre:false;
}
// http://doc.spip.org/@spip_xml_parse
function spip_xml_parse($texte, $strict=true, $clean=true){
cerdic
a validé
$out = array();
// enlever les commentaires
if ($clean){
$charset = 'AUTO';
if (preg_match(",<\?xml\s(.*?)encoding=['\"]?(.*?)['\"]?(\s(.*))?\?>,im",$texte,$regs))
$charset = $regs[2];
cerdic
a validé
$texte = preg_replace(',<!--(.*?)-->,is','',$texte);
$texte = preg_replace(',<\?(.*?)\?>,is','',$texte);
include_spip('inc/charsets');
$texte = importer_charset($texte,$charset);
cerdic
a validé
}
$txt = $texte;
// tant qu'il y a des tags
$chars = preg_split("{<([^>]*?)>}s",$txt,2,PREG_SPLIT_DELIM_CAPTURE);
while(count($chars)>=2){
// tag ouvrant
//$chars = preg_split("{<([^>]*?)>}s",$txt,2,PREG_SPLIT_DELIM_CAPTURE);
// $before doit etre vide ou des espaces uniquements!
$before = trim($chars[0]);
if (strlen($before)>0)
return $texte; // before non vide, donc on est dans du texte
cerdic
a validé
$tag = rtrim($chars[1]);
cerdic
a validé
$closing_tag = explode(" ",trim($tag));$closing_tag=reset($closing_tag);
$txt = $chars[2];
if(substr($tag,-1)=='/'){ // self closing tag
cerdic
a validé
$tag = rtrim(substr($tag,0,strlen($tag)-1));
cerdic
a validé
$out[$tag][]="";
}
else{
// tag fermant
cerdic
a validé
$chars = preg_split("{(</".preg_quote($closing_tag).">)}is",$txt,-1,PREG_SPLIT_DELIM_CAPTURE);
$content = "";
if (count($chars)>3){ // plusieurs tags fermant -> verifier les tags ouvrants/fermants
$nclose =0; $nopen = 0;
preg_match_all("{<".preg_quote($closing_tag)."(\s*>|\s[^>]*[^/>]>)}is",$chars[0],$matches,PREG_SET_ORDER);
$nopen += count($matches);
while ($nopen>$nclose && (count($chars)>3)){
$content.=array_shift($chars);
$content.=array_shift($chars);
$nclose++;
preg_match_all("{<".preg_quote($closing_tag)."(>|[^>]*[^/>]>)}is",$chars[0],$matches,PREG_SET_ORDER);
$nopen += count($matches);
}
}
cerdic
a validé
if (!isset($chars[1])) { // tag fermant manquant
if ($strict){
$out[$tag][]="erreur : tag fermant $tag manquant::$txt";
return $out;
}
else return $texte; // un tag qui constitue du texte a reporter dans $before
cerdic
a validé
}
cerdic
a validé
$content .= array_shift($chars);
array_shift($chars); // enlever le separateur
$txt = implode("",$chars);
cerdic
a validé
if (strpos($content,"<")===FALSE) // eviter une recursion si pas utile
$out[$tag][] = $content;
else
$out[$tag][]=spip_xml_parse($content, $strict, false);
cerdic
a validé
}
$chars = preg_split("{<([^>]*?)>}s",$txt,2,PREG_SPLIT_DELIM_CAPTURE);
cerdic
a validé
}
if (count($out)&&(strlen(trim($txt))==0))
cerdic
a validé
return $out;
else
return $texte;
}
// http://doc.spip.org/@spip_xml_aplatit
cerdic
a validé
function spip_xml_aplatit($arbre,$separateur = " "){
$s = "";
if (is_array($arbre))
foreach($arbre as $tag=>$feuille){
if (is_array($feuille)){
if ($tag!==intval($tag)){
$f = spip_xml_aplatit($feuille);
cerdic
a validé
if (strlen($f)) {
$tagf = explode(" ",$tag);
$tagf = $tagf[0];
cerdic
a validé
$s.="<$tag>$f</$tagf>";
}
cerdic
a validé
else $s.="<$tag />";
cerdic
a validé
}
else
$s.=spip_xml_aplatit($feuille);
$s .= $separateur;
}
else
$s.="$feuille$separateur";
}
return substr($s,0,strlen($s)-strlen($separateur));
}
?>