[ Index ]
 

Code source de SPIP 1.9.2c

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/ecrire/inc/ -> valider_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  include_spip('inc/sax');
  16  
  17  // http://doc.spip.org/@validerElement
  18  function validerElement($phraseur, $name, $attrs)
  19  {
  20      global $phraseur_xml;
  21  
  22      if (!isset($phraseur_xml->dtc->elements[$name]))
  23  
  24          $phraseur_xml->err[]= " <b>$name</b> "
  25          . _T('zxml_inconnu_balise')
  26          . ' '
  27          .  coordonnees_erreur($phraseur);
  28      else {
  29      // controler les filles illegitimes, ca suffit 
  30        $depth = $phraseur_xml->depth;
  31        $ouvrant = $phraseur_xml->ouvrant;
  32        if (isset($ouvrant[$depth])) {
  33          if (preg_match('/^\s*(\w+)/', $ouvrant[$depth], $r)) {
  34            $pere = $r[1];
  35            if (isset($phraseur_xml->dtc->elements[$pere]))
  36          if (!@in_array($name, $phraseur_xml->dtc->elements[$pere])) {
  37                $bons_peres = @join ('</b>, <b>', $phraseur_xml->dtc->peres[$name]);
  38                $phraseur_xml->err[]= " <b>$name</b> "
  39                  . _T('zxml_non_fils')
  40                  . ' <b>'
  41                  .  $pere
  42                  . '</b>'
  43                  . (!$bons_peres ? ''
  44                     : ('<p style="font-size: 80%"> '._T('zxml_mais_de').' <b>'. $bons_peres . '</b></p>'))
  45              .  coordonnees_erreur($phraseur);
  46          } else if ($phraseur_xml->dtc->regles[$pere][0]=='/') {
  47            $phraseur_xml->fratrie[substr($depth,2)].= "$name ";
  48          }
  49          }
  50        }
  51        // Init de la suite des balises a memoriser si regle difficile
  52        if ($phraseur_xml->dtc->regles[$name][0]=='/')
  53          $phraseur_xml->fratrie[$depth]='';
  54        if (isset($phraseur_xml->dtc->attributs[$name])) {
  55            foreach ($phraseur_xml->dtc->attributs[$name] as $n => $v)
  56              { if (($v[1] == '#REQUIRED') AND (!isset($attrs[$n])))
  57              $phraseur_xml->err[]= " <b>$n</b>"
  58                . '&nbsp;:&nbsp;'
  59                . _T('zxml_obligatoire_attribut')
  60                . " <b>$name</b>"
  61                .  coordonnees_erreur($phraseur);
  62              }
  63        }
  64      }
  65  }
  66  
  67  
  68  // http://doc.spip.org/@validerAttribut
  69  function validerAttribut($phraseur, $name, $val, $bal)
  70  {
  71      global $phraseur_xml;
  72  
  73      // Si la balise est inconnue, eviter d'insister
  74      if (!isset($phraseur_xml->dtc->attributs[$bal]))
  75          return ;
  76          
  77      $a = $phraseur_xml->dtc->attributs[$bal];
  78      if (!isset($a[$name])) {
  79          $bons = join(', ',array_keys($a));
  80          if ($bons)
  81            $bons = " title=' " .
  82              _T('zxml_connus_attributs') .
  83              '&nbsp;: ' .
  84              $bons .
  85              "'";
  86          $bons .= " style='font-weight: bold'";
  87  
  88          $phraseur_xml->err[]= " <b>$name</b> "
  89          . _T('zxml_inconnu_attribut').' '._T('zxml_de')
  90          . " <a$bons>$bal</a> ("
  91          . _T('zxml_survoler')
  92          . ")"
  93          .  coordonnees_erreur($phraseur);
  94      } else{
  95          $type =  $a[$name][0];
  96          if (!preg_match('/^\w+$/', $type))
  97              valider_motif($phraseur, $name, $val, $bal, $type);
  98          else if (function_exists($f = 'validerAttribut_' . $type))
  99              $f($phraseur, $name, $val, $bal);
 100      }
 101  }
 102  
 103  // http://doc.spip.org/@validerAttribut_ID
 104  function validerAttribut_ID($phraseur, $name, $val, $bal)
 105  {
 106      global $phraseur_xml;
 107  
 108      if (isset($phraseur_xml->ids[$val])) {
 109          list($l,$c) = $phraseur_xml->ids[$val];
 110          $phraseur_xml->err[]= " <p><b>$val</b> "
 111                . _T('zxml_valeur_attribut')
 112                . " <b>$name</b> "
 113                . _T('zxml_de')
 114                . " <b>$bal</b> "
 115                . _T('zxml_vu')
 116                . " (L$l,C$c)"
 117                .  coordonnees_erreur($phraseur);
 118      } else {
 119          valider_motif($phraseur, $name, $val, $bal, _REGEXP_ID);
 120          $phraseur_xml->ids[$val] = array(xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur));
 121      }
 122  }
 123  
 124  // http://doc.spip.org/@validerAttribut_IDREF
 125  function validerAttribut_IDREF($phraseur, $name, $val, $bal)
 126  {
 127      global $phraseur_xml;
 128      $phraseur_xml->idrefs[] = array($val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur));
 129  }
 130  
 131  // http://doc.spip.org/@validerAttribut_IDREFS
 132  function validerAttribut_IDREFS($phraseur, $name, $val, $bal)
 133  {
 134      global $phraseur_xml;
 135  
 136      $phraseur_xml->idrefss[] = array($val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur));
 137  }
 138  
 139  // http://doc.spip.org/@valider_motif
 140  function valider_motif($phraseur, $name, $val, $bal, $motif)
 141  {
 142      global $phraseur_xml;
 143  
 144      if (!preg_match($motif, $val)) {
 145          $phraseur_xml->err[]= " <p><b>$val</b> "
 146          . _T('zxml_valeur_attribut')
 147          . " <b>$name</b> "
 148          . _T('zxml_de')
 149          . " <b>$bal</b> "
 150          . _T('zxml_non_conforme')
 151          . "</p><p>"
 152          . "<b>" . $motif . "</b></p>"
 153          .  coordonnees_erreur($phraseur);
 154      }
 155  }
 156  
 157  // http://doc.spip.org/@valider_idref
 158  function valider_idref(&$own, $nom, $ligne, $col)
 159  {
 160      if (!isset($own->ids[$nom]))
 161          $own->err[]= " <p><b>$nom</b> "
 162          . _T('zxml_inconnu_id')
 163          . " "
 164          . $ligne
 165          . " "
 166          . $col;
 167  }
 168  
 169  // http://doc.spip.org/@inc_valider_passe2_dist
 170  function inc_valider_passe2_dist(&$own)
 171  {
 172      if (!$own->err) {
 173          foreach ($own->idrefs as $idref) {
 174              list($nom, $ligne, $col) = $idref;
 175              valider_idref($own, $nom, $ligne, $col);
 176          }
 177          foreach ($own->idrefss as $idref) {
 178              list($noms, $ligne, $col) = $idref;
 179              foreach(preg_split('/\s+/', $noms) as $nom)
 180                  valider_idref($own, $nom, $ligne, $col);
 181          }
 182      }
 183  }
 184  
 185  class ValidateurXML {
 186  
 187  // http://doc.spip.org/@debutElement
 188  function debutElement($phraseur, $name, $attrs)
 189  { 
 190      global $phraseur_xml;
 191  
 192      if ($phraseur_xml->dtc->elements)
 193          validerElement($phraseur, $name, $attrs);
 194  
 195      xml_debutElement($phraseur, $name, $attrs);
 196      $depth = &$phraseur_xml->depth;
 197      $phraseur_xml->debuts[$depth] =  strlen($phraseur_xml->res);
 198      foreach ($attrs as $k => $v) {
 199          validerAttribut($phraseur, $k, $v, $name);
 200      }
 201  }
 202  
 203  // http://doc.spip.org/@finElement
 204  function finElement($phraseur, $name)
 205  {
 206      global $phraseur_xml;
 207  
 208      $depth = &$phraseur_xml->depth;
 209      $contenu = &$phraseur_xml->contenu;
 210  
 211      $n = strlen($phraseur_xml->res);
 212      $c = strlen(trim($contenu[$depth]));
 213      $k = $phraseur_xml->debuts[$depth];
 214  
 215      $regle = $phraseur_xml->dtc->regles[$name];
 216      $vide = ($regle  == 'EMPTY');
 217      // controler que les balises devant etre vides le sont 
 218      if ($vide) {
 219        if ($n <> ($k + $c))
 220              $phraseur_xml->err[]= " <p><b>$name</b> "
 221              . _T('zxml_nonvide_balise')
 222              .  coordonnees_erreur($phraseur);
 223      // pour les regles PCDATA ou iteration de disjonction, tout est fait
 224      } elseif ($regle AND ($regle != '*')) {
 225          if ($regle == '+') {
 226              // iteration de disjonction non vide: 1 balise au -
 227              if ($n == $k) {
 228                  $phraseur_xml->err[]= " <p>\n<b>$name</b> "
 229                    . _T('zxml_vide_balise')
 230                    .  coordonnees_erreur($phraseur);
 231              }
 232          } else {
 233              $f = $phraseur_xml->fratrie[substr($depth,2)];
 234              if (!preg_match($regle, $f))
 235                  $phraseur_xml->err[]= " <p>\n<b>$name</b> "
 236                    .  _T('zxml_succession_fils_incorrecte')
 237                    . '&nbsp;: <b>'
 238                    . $f
 239                    . '</b>'
 240                    .  coordonnees_erreur($phraseur);
 241          }
 242  
 243      }
 244      xml_finElement($phraseur, $name, $vide);
 245  }
 246  
 247  // http://doc.spip.org/@textElement
 248  function textElement($phraseur, $data)
 249  {    xml_textElement($phraseur, $data);}
 250  
 251  // http://doc.spip.org/@PiElement
 252  function PiElement($phraseur, $target, $data)
 253  {    xml_PiElement($phraseur, $target, $data);}
 254  
 255  // Denonciation des entitees XML inconnues
 256  // Pour contourner le bug de conception de SAX qui ne signale pas si elles
 257  // sont dans un attribut, les  entites les plus frequentes ont ete
 258  // transcodees au prealable  (sauf & < > " que SAX traite correctement).
 259  // On ne les verra donc pas passer a cette etape, contrairement a ce que 
 260  // le source de la page laisse legitimement supposer. 
 261  
 262  // http://doc.spip.org/@defautElement
 263  function defautElement($phraseur, $data)
 264  {    
 265      global $phraseur_xml;
 266  
 267      if (!preg_match('/^<!--/', $data)
 268      AND (preg_match_all('/&([^;]*)?/', $data, $r, PREG_SET_ORDER)))
 269          foreach ($r as $m) {
 270              list($t,$e) = $m;
 271              if (!isset($phraseur_xml->dtc->entites[$e]))
 272                  $phraseur_xml->err[]= " <b>$e</b> "
 273                    . _T('zxml_inconnu_entite')
 274                    . ' '
 275                    .  coordonnees_erreur($phraseur);
 276          }
 277  
 278      xml_defautElement($phraseur, $data);
 279  }
 280  
 281  // http://doc.spip.org/@phraserTout
 282  function phraserTout($phraseur, $data)
 283  { 
 284      xml_parsestring($phraseur, $data);
 285  
 286      if (!$this->dtc OR preg_match(',^' . _MESSAGE_DOCTYPE . ',', $data)) {
 287          $GLOBALS['xhtml_error'] .= 'DOCTYPE ? 0 0<br />';
 288          $this->err[]= ('DOCTYPE ? 0 0<br />');
 289      } else {
 290          $valider_passe2 = charger_fonction('valider_passe2', 'inc');
 291          $valider_passe2($this);
 292      }
 293      return !$this->err ?  $this->res : join('<br />', $this->err) . '<br />';
 294  }
 295  
 296   var $depth = "";
 297   var $res = "";
 298   var $contenu = array();
 299   var $ouvrant = array();
 300   var $reperes = array();
 301  
 302   var $dtc = NULL;
 303   var $err = array();
 304   var $ids = array();
 305   var $idrefs = array();
 306   var $idrefss = array();
 307   var $debuts = array();
 308   var $fratrie = array();
 309  }
 310  
 311  // http://doc.spip.org/@inc_valider_xml_dist
 312  function inc_valider_xml_dist($page, $apply=false)
 313  {
 314      spip_timer('valider');
 315      $sax = charger_fonction('sax', 'inc');
 316      $sax = $sax($page, $apply, $GLOBALS['phraseur_xml'] = new ValidateurXML());
 317      spip_log("validation : " . spip_timer('valider'));
 318      return $sax;
 319  }
 320  ?>


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