[ Index ]
 

Code source de SPIP 1.9.2c

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/ecrire/inc/ -> xml.php (source)

   1  <?php
   2  
   3  /***************************************************************************\
   4   *  SPIP, Systeme de publication pour l'internet                           *
   5   *                                                                         *
   6   *  Copyright (c) 2001-2007                                                *
   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=''){
  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);
  26          
  27      return count($arbre)?$arbre:false;
  28  }
  29  
  30  // http://doc.spip.org/@spip_xml_parse
  31  function spip_xml_parse($texte, $strict=true, $clean=true){
  32      $out = array();
  33    // enlever les commentaires
  34    if ($clean){
  35        $charset = 'AUTO';
  36        if (preg_match(",<\?xml\s(.*?)encoding=['\"]?(.*?)['\"]?(\s(.*))?\?>,im",$texte,$regs))
  37            $charset = $regs[2];
  38        $texte = preg_replace(',<!--(.*?)-->,is','',$texte);
  39        $texte = preg_replace(',<\?(.*?)\?>,is','',$texte);
  40          include_spip('inc/charsets');
  41          $texte = importer_charset($texte,$charset);
  42    }
  43    $txt = $texte;
  44  
  45      // tant qu'il y a des tags
  46      $chars = preg_split("{<([^>]*?)>}s",$txt,2,PREG_SPLIT_DELIM_CAPTURE);
  47      while(count($chars)>=2){
  48          // tag ouvrant
  49          //$chars = preg_split("{<([^>]*?)>}s",$txt,2,PREG_SPLIT_DELIM_CAPTURE);
  50      
  51          // $before doit etre vide ou des espaces uniquements!
  52          $before = trim($chars[0]);
  53  
  54          if (strlen($before)>0)
  55              return $texte; // before non vide, donc on est dans du texte
  56      
  57          $tag = rtrim($chars[1]);
  58          $closing_tag = explode(" ",trim($tag));$closing_tag=reset($closing_tag);
  59          $txt = $chars[2];
  60      
  61          if(substr($tag,-1)=='/'){ // self closing tag
  62              $tag = rtrim(substr($tag,0,strlen($tag)-1));
  63              $out[$tag][]="";
  64          }
  65          else{
  66              // tag fermant
  67              $chars = preg_split("{(</".preg_quote($closing_tag).">)}is",$txt,-1,PREG_SPLIT_DELIM_CAPTURE);
  68              $content = "";
  69              if (count($chars)>3){ // plusieurs tags fermant -> verifier les tags ouvrants/fermants
  70                  $nclose =0; $nopen = 0;
  71                  preg_match_all("{<".preg_quote($closing_tag)."(\s*>|\s[^>]*[^/>]>)}is",$chars[0],$matches,PREG_SET_ORDER);
  72                  $nopen += count($matches);
  73                  while ($nopen>$nclose && (count($chars)>3)){
  74                      $content.=array_shift($chars);
  75                      $content.=array_shift($chars);
  76                      $nclose++;
  77                      preg_match_all("{<".preg_quote($closing_tag)."(>|[^>]*[^/>]>)}is",$chars[0],$matches,PREG_SET_ORDER);
  78                      $nopen += count($matches);
  79                  }
  80              }
  81              if (!isset($chars[1])) { // tag fermant manquant
  82                  if ($strict){
  83                      $out[$tag][]="erreur : tag fermant $tag manquant::$txt"; 
  84                      return $out;
  85                  }
  86                  else return $texte; // un tag qui constitue du texte a reporter dans $before
  87              }
  88              $content .= array_shift($chars);
  89              array_shift($chars); // enlever le separateur
  90              $txt = implode("",$chars);
  91              if (strpos($content,"<")===FALSE) // eviter une recursion si pas utile
  92                  $out[$tag][] = $content;
  93              else
  94                  $out[$tag][]=spip_xml_parse($content, $strict, false);
  95          }
  96          $chars = preg_split("{<([^>]*?)>}s",$txt,2,PREG_SPLIT_DELIM_CAPTURE);
  97      }
  98      if (count($out)&&(strlen(trim($txt))==0))
  99          return $out;
 100      else
 101          return $texte;
 102  }
 103  
 104  // http://doc.spip.org/@spip_xml_aplatit
 105  function spip_xml_aplatit($arbre,$separateur = " "){
 106      $s = "";
 107      if (is_array($arbre))
 108          foreach($arbre as $tag=>$feuille){
 109              if (is_array($feuille)){
 110                  if ($tag!==intval($tag)){
 111                      $f = spip_xml_aplatit($feuille);
 112                      if (strlen($f)) {
 113                          $tagf = explode(" ",$tag);
 114                          $tagf = $tagf[0];
 115                          $s.="<$tag>$f</$tagf>";
 116                      }
 117                      else $s.="<$tag />";
 118                  }
 119                  else
 120                      $s.=spip_xml_aplatit($feuille);
 121                  $s .= $separateur;
 122              }                
 123              else
 124                  $s.="$feuille$separateur";
 125          }
 126      return substr($s,0,strlen($s)-strlen($separateur));
 127  }
 128  ?>


Généré le : Wed Nov 21 10:20:27 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics