cvs: pear /XML_DTD DTD_Parser.php DTD_Validator.php package.xml /XML_DTD/tests non.xml package.dtd test.php
| From: | Thomas V.V.Cox | Date: | Sat, 23 Feb 2002 21:15:03 +0000 |
| Subject: | cvs: pear /XML_DTD DTD_Parser.php DTD_Validator.php package.xml /XML_DTD/tests non.xml package.dtd test.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-2577@lists.php.net to get a copy of this message | ||
cox Sat Feb 23 16:15:03 2002 EDT
Added files:
/pear/XML_DTD DTD_Parser.php DTD_Validator.php package.xml
/pear/XML_DTD/tests non.xml package.dtd test.php
Log:
Initial release of a DTD parser and validation class
# very limited and perhaps buggy yet, more work on it in progress
Index: pear/XML_DTD/DTD_Parser.php +++ pear/XML_DTD/DTD_Parser.php <?php /* DTD tree format: [elements] => array( <tag name> => array( [children] => array( <allowed children tag> => <requirement> ), [attributes] => array( <att name> => array( [opts] => array(), [prop] => <property> ) ) ) ) */ class XML_DTD_Parser { var $dtd = array(); function parse($file) { $cont = join('', file($file)); if (preg_match_all('|<!([^>]+)>|s', $cont, $m)) { foreach ($m[1] as $tag) { // XXX Will fail with this format: <!ELEMENT p (a | ul | b )*> // because of the spaces inside the allowed list $t = preg_split('|\s+|s', trim($tag)); switch ($t[0]) { case 'ELEMENT': $this->ELEMENT($t); break; case 'ATTLIST': $this->ATTLIST($t); break; } } } return $this->dtd; } function ELEMENT($data) { $childs_str = $data[2]; if (substr($childs_str, -1) != ')') { $list_prop = substr($childs_str, -1); $childs_str = substr($childs_str, 1, -2); } else { $list_prop = null; $childs_str = substr($childs_str, 1, -1); } $childs = explode('|', preg_replace('|\s+|', '', $childs_str)); $props = array('+', '*', '?'); $c = array(); foreach ($childs as $child) { $prop = null; if (in_array(substr($child, -1), $props)) { $prop = substr($child, -1); $child = substr($child, 0, -1); } if (!$prop && $list_prop) { $prop = $list_prop; } $c[$child] = $prop; } $this->dtd['elements'][$data[1]]['children'] = $c; } /* Array ( [0] => ATTLIST [1] => dep [2] => type [3] => (pkg|ext|php|prog|ldlib|ltlib|os|websrv|sapi) [4] => #REQUIRED [5] => rel [6] => (has|eq|lt|le|gt|ge) [7] => #IMPLIED [8] => version [9] => CDATA [10] => #IMPLIED ) */ function ATTLIST($data) { $elem = $data[1]; array_shift($data); array_shift($data); // XXX Wish: array_shift(array, num_of_elems) // XXX #FIXED not supported yet for ($i=0; $i < count($data) ; $i = $i + 3) { $att = $data[$i]; $opts = $data[$i+1]; if ($opts{0} == '(' && $opts{strlen($opts)-1} == ')') { $a['opts'] = explode('|', preg_replace('|\s+|', '', substr($opts, 1, -1) ) ); } else { $a['opts'] = $opts; } $a['prop'] = $data[$i+2]; $this->dtd['elements'][$elem]['attributes'][$att] = $a; } } } ?> Index: pear/XML_DTD/DTD_Validator.php +++ pear/XML_DTD/DTD_Validator.php <?php require_once 'XML/DTD_Parser.php'; require_once 'XML/Tree.php'; class DTD_Validator { var $dtd = array(); var $errors = false; function isValid($dtd_file, $xml_file) { $xml_tree =& new XML_Tree($xml_file); $nodes = $xml_tree->getTreeFromFile(); if (PEAR::isError($nodes)) { $this->errors = $nodes->getMessage(); return false; } $dtd_parser =& new XML_DTD_Parser; $this->dtd = $dtd_parser->parse($dtd_file); $this->runTree($nodes); return ($this->errors) ? false : true; } function runTree(&$node) { $name = $node->name; if (!isset($this->dtd['elements'][$name])) { $this->errors("tag <$name> not defined in DTD\n"); return; } $dtd_childs = $this->dtd['elements'][$name]['children']; $allowed = array_keys($dtd_childs); // Validate allowed childs $count = array(); foreach ($node->children as $child) { $chname = $child->name; if (!in_array($chname, $allowed)) { // XXX put #PCDATA elem off here $this->errors("<$chname> not allowed under <$name>"); } else { $count[$chname] = (isset($count[$chname])) ? $count[$chname] + 1 : 1; } } // Validate the number of present childs foreach ($dtd_childs as $chname => $req) { if ($allowed[0] == '#PCDATA') { // For mixed contents continue; } $num = (isset($count[$chname])) ? $count[$chname] : 0; switch ($req) { case null: if ($num < 1) { $this->errors("missing elem <$chname> under <$name>"); } elseif ($num > 1) { $this->errors("only one <$chname> tag allowed under <$name>"); } break; case '+': if ($num < 1) { $this->errors("elem <$name> must have at least one <$chname>"); } break; case '?': if ($num != 0 || $num != 1) { $this->errors("elem <$name> must have one or zero <$chname>"); } break; } } // Validation allowed attributes $node_atts = $node->attributes; if (isset($this->dtd['elements'][$name]['attributes'])) { $atts = $this->dtd['elements'][$name]['attributes']; foreach ($atts as $attname => $attvalue) { $opts = $attvalue['opts']; $prop = $attvalue['prop']; if ($prop == '#REQUIRED' && !isset($node_atts[$attname])) { $this->errors("missing required '$attname' attribute in <$name>"); } if (isset($node_atts[$attname])) { $node_val = $node_atts[$attname]; if (is_array($opts)) { if (!in_array($node_val, $opts)) { $this->errors("'$node_val' value for attribute '$attname' under <$name> ". "can only be: '". implode(', ', $opts) . "'"); } } unset($node_atts[$attname]); // Remove the element } } } // If there are still elements those are not defined if (count($node_atts) > 0) { $this->errors("the attributes: '" . implode(', ', array_keys($node_atts)) . "' are not defined in the DTD for tag <$name>"); } // Continue through the tree foreach ($node->children as $child) { $this->runTree($child); } } function errors($str) { $this->errors .= "$str\n"; } function getError() { return $this->errors; } } ?> Index: pear/XML_DTD/package.xml +++ pear/XML_DTD/package.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE package SYSTEM "../package.dtd"> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <package version="1.0"> <name>XML_DTD</name> <summary>Parsing of DTD files and DTD validation for XML files</summary> <description>Parsing of DTD files and DTD validation for XML files</description> <maintainers> <maintainer> <user>cox</user> <name>Tomas V.V.Cox</name> <email>cox@idecnet.com</email> <role>lead</role> </maintainer> </maintainers> <release> <license>LGPL</license> <version>0.2</version> <date>2002-02-23</date> <state>alpha</state> <notes>Initial release</notes> <filelist> <dir name="/" baseinstalldir="XML"> <file role="php">DTD_Parser.php</file> <file role="php">DTD_Validator.php</file> </dir> </filelist> </release> </package> Index: pear/XML_DTD/tests/non.xml +++ pear/XML_DTD/tests/non.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE package SYSTEM "../package.dtd"> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <package version="1.0"> <name>HTTP_Upload</name> <summary>Easy and secure managment of files submitted via HTML Forms</summary> <!-- <description> This class provides an advanced file uploader system for file uploads made from html forms. Features: * Can handle from one file to multiple files. * Safe file copying from tmp dir. * Easy detecting mechanism of valid upload, missing upload or error. * Gives extensive information about the uploaded file. * Rename uploaded files in different ways: as it is, safe or unique * Validate allowed file extensions * Multiple languages error messages support (es, en, de, fr, nl) </description> --> <maintainers> <!--<maintainer> <user>cox</user> <name>Tomas V.V.Cox</name> <email>cox@idecnet.com</email> <role>lead</role> </maintainer>--> </maintainers> <release> <license>LGPL</license> <version>0.8</version> <date>2002-1-25</date> <state>stable</state> <notes> - Fixed compatibility issues with PHP > 4.1 - Added isMissing() to the upload main object. Use this first to check if the user has submitted a file (for PHP > 4.1 users) - Depreciate the use of errorMsg() in the file object. Use getMessage() instead. - Added NL and FR translations (thanks Hans and Luc!) (full backawards compatibility preserved) </notes> <filelist> <dir name="/" baseinstalldir="HTTP"> <file role="php">Upload.php</file> <dir name="docs" role="doc"> <file>upload_example.php</file> </dir> </dir> </filelist> </release> <changelog> <release> <version>0.7</version> <date>2001-12-27</date> <state>stable</state> <notes> - Added file extension checking support. Now by default rejects this extensions: 'php', 'phtm', 'phtml', 'php3', 'inc' - More in-line documentation and error code tranlations in spanish, english and german - Other minor fixes </notes> </release> <release> <version>0.6.4</version> <date>2001-08-15</date> <notes> - more safe checks - the default file name mode is now 'safe' - phpdoc update Please use this version if you are using the class </notes> </release> </changelog> </package> Index: pear/XML_DTD/tests/package.dtd +++ pear/XML_DTD/tests/package.dtd <!-- $Id: package.dtd,v 1.1 2002/02/23 21:15:02 cox Exp $ This is the PEAR package description, version 1.0b3. It should be used with the informal public identifier: "-//PHP Group//DTD PEAR Package 1.0b3//EN//XML" Copyright (c) 1997-2002 The PHP Group This source file is subject to version 2.02 of the PHP license, that is bundled with this package in the file LICENSE, and is available at through the world-wide-web at http://www.php.net/license/2_02.txt. If you did not receive a copy of the PHP license and are unable to obtain it through the world-wide-web, please send a note to license@php.net so we can mail you a copy immediately. Authors: Stig S. Bakken <ssb@fast.no> --> <!ELEMENT package (name|summary|description|license|maintainers|release|changelog)> <!ATTLIST package type (source|binary|empty) "empty" version CDATA #REQUIRED> <!ELEMENT name (#PCDATA)> <!ELEMENT summary (#PCDATA)> <!ELEMENT description (#PCDATA)> <!ELEMENT maintainers (maintainer)+> <!ELEMENT maintainer (user|role|name|email)*> <!ELEMENT user (#PCDATA)> <!ELEMENT role (#PCDATA)> <!ELEMENT email (#PCDATA)> <!ELEMENT changelog (release)*> <!ELEMENT release (version|license|state|date|notes|filelist|deps)*> <!ELEMENT version (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT date (#PCDATA)> <!ELEMENT notes (#PCDATA)> <!ELEMENT filelist (dir|file|libfile)*> <!ELEMENT dir (dir|file|libfile)*> <!ATTLIST dir name CDATA #REQUIRED baseinstalldir CDATA #IMPLIED> <!ELEMENT file (#PCDATA)> <!ATTLIST file role (php|ext|test|doc|data) 'php' debug (na|on|off) 'na' threaded (na|on|off) 'na' format CDATA #IMPLIED baseinstalldir CDATA #IMPLIED> <!ELEMENT libfile (libname|sources|includes|libadd)*> <!ELEMENT libname (#PCDATA)> <!ELEMENT sources (#PCDATA)> <!ELEMENT libadd (#PCDATA)> <!ELEMENT deps (dep)*> <!ELEMENT dep (#PCDATA)> <!ATTLIST dep type (pkg|ext|php|prog|ldlib|ltlib|os|websrv|sapi) #REQUIRED rel (has|eq|lt|le|gt|ge) #IMPLIED version CDATA #IMPLIED> Index: pear/XML_DTD/tests/test.php +++ pear/XML_DTD/tests/test.php #!/usr/bin/php -Cq <?php require 'XML/DTD_Validator.php'; $dtd = './package.dtd'; $xml = $argv[1]; $a = new DTD_Validator; if (!$a->isValid($dtd, $xml)) { echo $a->getError(); } ?>
Index: pear/XML_DTD/DTD_Parser.php +++ pear/XML_DTD/DTD_Parser.php <?php /* DTD tree format: [elements] => array( <tag name> => array( [children] => array( <allowed children tag> => <requirement> ), [attributes] => array( <att name> => array( [opts] => array(), [prop] => <property> ) ) ) ) */ class XML_DTD_Parser { var $dtd = array(); function parse($file) { $cont = join('', file($file)); if (preg_match_all('|<!([^>]+)>|s', $cont, $m)) { foreach ($m[1] as $tag) { // XXX Will fail with this format: <!ELEMENT p (a | ul | b )*> // because of the spaces inside the allowed list $t = preg_split('|\s+|s', trim($tag)); switch ($t[0]) { case 'ELEMENT': $this->ELEMENT($t); break; case 'ATTLIST': $this->ATTLIST($t); break; } } } return $this->dtd; } function ELEMENT($data) { $childs_str = $data[2]; if (substr($childs_str, -1) != ')') { $list_prop = substr($childs_str, -1); $childs_str = substr($childs_str, 1, -2); } else { $list_prop = null; $childs_str = substr($childs_str, 1, -1); } $childs = explode('|', preg_replace('|\s+|', '', $childs_str)); $props = array('+', '*', '?'); $c = array(); foreach ($childs as $child) { $prop = null; if (in_array(substr($child, -1), $props)) { $prop = substr($child, -1); $child = substr($child, 0, -1); } if (!$prop && $list_prop) { $prop = $list_prop; } $c[$child] = $prop; } $this->dtd['elements'][$data[1]]['children'] = $c; } /* Array ( [0] => ATTLIST [1] => dep [2] => type [3] => (pkg|ext|php|prog|ldlib|ltlib|os|websrv|sapi) [4] => #REQUIRED [5] => rel [6] => (has|eq|lt|le|gt|ge) [7] => #IMPLIED [8] => version [9] => CDATA [10] => #IMPLIED ) */ function ATTLIST($data) { $elem = $data[1]; array_shift($data); array_shift($data); // XXX Wish: array_shift(array, num_of_elems) // XXX #FIXED not supported yet for ($i=0; $i < count($data) ; $i = $i + 3) { $att = $data[$i]; $opts = $data[$i+1]; if ($opts{0} == '(' && $opts{strlen($opts)-1} == ')') { $a['opts'] = explode('|', preg_replace('|\s+|', '', substr($opts, 1, -1) ) ); } else { $a['opts'] = $opts; } $a['prop'] = $data[$i+2]; $this->dtd['elements'][$elem]['attributes'][$att] = $a; } } } ?> Index: pear/XML_DTD/DTD_Validator.php +++ pear/XML_DTD/DTD_Validator.php <?php require_once 'XML/DTD_Parser.php'; require_once 'XML/Tree.php'; class DTD_Validator { var $dtd = array(); var $errors = false; function isValid($dtd_file, $xml_file) { $xml_tree =& new XML_Tree($xml_file); $nodes = $xml_tree->getTreeFromFile(); if (PEAR::isError($nodes)) { $this->errors = $nodes->getMessage(); return false; } $dtd_parser =& new XML_DTD_Parser; $this->dtd = $dtd_parser->parse($dtd_file); $this->runTree($nodes); return ($this->errors) ? false : true; } function runTree(&$node) { $name = $node->name; if (!isset($this->dtd['elements'][$name])) { $this->errors("tag <$name> not defined in DTD\n"); return; } $dtd_childs = $this->dtd['elements'][$name]['children']; $allowed = array_keys($dtd_childs); // Validate allowed childs $count = array(); foreach ($node->children as $child) { $chname = $child->name; if (!in_array($chname, $allowed)) { // XXX put #PCDATA elem off here $this->errors("<$chname> not allowed under <$name>"); } else { $count[$chname] = (isset($count[$chname])) ? $count[$chname] + 1 : 1; } } // Validate the number of present childs foreach ($dtd_childs as $chname => $req) { if ($allowed[0] == '#PCDATA') { // For mixed contents continue; } $num = (isset($count[$chname])) ? $count[$chname] : 0; switch ($req) { case null: if ($num < 1) { $this->errors("missing elem <$chname> under <$name>"); } elseif ($num > 1) { $this->errors("only one <$chname> tag allowed under <$name>"); } break; case '+': if ($num < 1) { $this->errors("elem <$name> must have at least one <$chname>"); } break; case '?': if ($num != 0 || $num != 1) { $this->errors("elem <$name> must have one or zero <$chname>"); } break; } } // Validation allowed attributes $node_atts = $node->attributes; if (isset($this->dtd['elements'][$name]['attributes'])) { $atts = $this->dtd['elements'][$name]['attributes']; foreach ($atts as $attname => $attvalue) { $opts = $attvalue['opts']; $prop = $attvalue['prop']; if ($prop == '#REQUIRED' && !isset($node_atts[$attname])) { $this->errors("missing required '$attname' attribute in <$name>"); } if (isset($node_atts[$attname])) { $node_val = $node_atts[$attname]; if (is_array($opts)) { if (!in_array($node_val, $opts)) { $this->errors("'$node_val' value for attribute '$attname' under <$name> ". "can only be: '". implode(', ', $opts) . "'"); } } unset($node_atts[$attname]); // Remove the element } } } // If there are still elements those are not defined if (count($node_atts) > 0) { $this->errors("the attributes: '" . implode(', ', array_keys($node_atts)) . "' are not defined in the DTD for tag <$name>"); } // Continue through the tree foreach ($node->children as $child) { $this->runTree($child); } } function errors($str) { $this->errors .= "$str\n"; } function getError() { return $this->errors; } } ?> Index: pear/XML_DTD/package.xml +++ pear/XML_DTD/package.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE package SYSTEM "../package.dtd"> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <package version="1.0"> <name>XML_DTD</name> <summary>Parsing of DTD files and DTD validation for XML files</summary> <description>Parsing of DTD files and DTD validation for XML files</description> <maintainers> <maintainer> <user>cox</user> <name>Tomas V.V.Cox</name> <email>cox@idecnet.com</email> <role>lead</role> </maintainer> </maintainers> <release> <license>LGPL</license> <version>0.2</version> <date>2002-02-23</date> <state>alpha</state> <notes>Initial release</notes> <filelist> <dir name="/" baseinstalldir="XML"> <file role="php">DTD_Parser.php</file> <file role="php">DTD_Validator.php</file> </dir> </filelist> </release> </package> Index: pear/XML_DTD/tests/non.xml +++ pear/XML_DTD/tests/non.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE package SYSTEM "../package.dtd"> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <package version="1.0"> <name>HTTP_Upload</name> <summary>Easy and secure managment of files submitted via HTML Forms</summary> <!-- <description> This class provides an advanced file uploader system for file uploads made from html forms. Features: * Can handle from one file to multiple files. * Safe file copying from tmp dir. * Easy detecting mechanism of valid upload, missing upload or error. * Gives extensive information about the uploaded file. * Rename uploaded files in different ways: as it is, safe or unique * Validate allowed file extensions * Multiple languages error messages support (es, en, de, fr, nl) </description> --> <maintainers> <!--<maintainer> <user>cox</user> <name>Tomas V.V.Cox</name> <email>cox@idecnet.com</email> <role>lead</role> </maintainer>--> </maintainers> <release> <license>LGPL</license> <version>0.8</version> <date>2002-1-25</date> <state>stable</state> <notes> - Fixed compatibility issues with PHP > 4.1 - Added isMissing() to the upload main object. Use this first to check if the user has submitted a file (for PHP > 4.1 users) - Depreciate the use of errorMsg() in the file object. Use getMessage() instead. - Added NL and FR translations (thanks Hans and Luc!) (full backawards compatibility preserved) </notes> <filelist> <dir name="/" baseinstalldir="HTTP"> <file role="php">Upload.php</file> <dir name="docs" role="doc"> <file>upload_example.php</file> </dir> </dir> </filelist> </release> <changelog> <release> <version>0.7</version> <date>2001-12-27</date> <state>stable</state> <notes> - Added file extension checking support. Now by default rejects this extensions: 'php', 'phtm', 'phtml', 'php3', 'inc' - More in-line documentation and error code tranlations in spanish, english and german - Other minor fixes </notes> </release> <release> <version>0.6.4</version> <date>2001-08-15</date> <notes> - more safe checks - the default file name mode is now 'safe' - phpdoc update Please use this version if you are using the class </notes> </release> </changelog> </package> Index: pear/XML_DTD/tests/package.dtd +++ pear/XML_DTD/tests/package.dtd <!-- $Id: package.dtd,v 1.1 2002/02/23 21:15:02 cox Exp $ This is the PEAR package description, version 1.0b3. It should be used with the informal public identifier: "-//PHP Group//DTD PEAR Package 1.0b3//EN//XML" Copyright (c) 1997-2002 The PHP Group This source file is subject to version 2.02 of the PHP license, that is bundled with this package in the file LICENSE, and is available at through the world-wide-web at http://www.php.net/license/2_02.txt. If you did not receive a copy of the PHP license and are unable to obtain it through the world-wide-web, please send a note to license@php.net so we can mail you a copy immediately. Authors: Stig S. Bakken <ssb@fast.no> --> <!ELEMENT package (name|summary|description|license|maintainers|release|changelog)> <!ATTLIST package type (source|binary|empty) "empty" version CDATA #REQUIRED> <!ELEMENT name (#PCDATA)> <!ELEMENT summary (#PCDATA)> <!ELEMENT description (#PCDATA)> <!ELEMENT maintainers (maintainer)+> <!ELEMENT maintainer (user|role|name|email)*> <!ELEMENT user (#PCDATA)> <!ELEMENT role (#PCDATA)> <!ELEMENT email (#PCDATA)> <!ELEMENT changelog (release)*> <!ELEMENT release (version|license|state|date|notes|filelist|deps)*> <!ELEMENT version (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT date (#PCDATA)> <!ELEMENT notes (#PCDATA)> <!ELEMENT filelist (dir|file|libfile)*> <!ELEMENT dir (dir|file|libfile)*> <!ATTLIST dir name CDATA #REQUIRED baseinstalldir CDATA #IMPLIED> <!ELEMENT file (#PCDATA)> <!ATTLIST file role (php|ext|test|doc|data) 'php' debug (na|on|off) 'na' threaded (na|on|off) 'na' format CDATA #IMPLIED baseinstalldir CDATA #IMPLIED> <!ELEMENT libfile (libname|sources|includes|libadd)*> <!ELEMENT libname (#PCDATA)> <!ELEMENT sources (#PCDATA)> <!ELEMENT libadd (#PCDATA)> <!ELEMENT deps (dep)*> <!ELEMENT dep (#PCDATA)> <!ATTLIST dep type (pkg|ext|php|prog|ldlib|ltlib|os|websrv|sapi) #REQUIRED rel (has|eq|lt|le|gt|ge) #IMPLIED version CDATA #IMPLIED> Index: pear/XML_DTD/tests/test.php +++ pear/XML_DTD/tests/test.php #!/usr/bin/php -Cq <?php require 'XML/DTD_Validator.php'; $dtd = './package.dtd'; $xml = $argv[1]; $a = new DTD_Validator; if (!$a->isValid($dtd, $xml)) { echo $a->getError(); } ?>