Add accessor to $rootElement, rather than accessing it directly.
[lhc/web/wiklou.git] / includes / XmlTypeCheck.php
1 <?php
2
3 class XmlTypeCheck {
4 /**
5 * Will be set to true or false to indicate whether the file is
6 * well-formed XML. Note that this doesn't check schema validity.
7 */
8 public $wellFormed = false;
9
10 /**
11 * Name of the document's root element, including any namespace
12 * as an expanded URL.
13 */
14 public $rootElement = '';
15
16 private $softNamespaces;
17 private $namespaces = array();
18
19 /**
20 * @param $file string filename
21 * @param $softNamespaces bool
22 * If set to true, use of undeclared XML namespaces will be ignored.
23 * This matches the behavior of rsvg, but more compliant consumers
24 * such as Firefox will reject such files.
25 * Leave off for the default, stricter checks.
26 */
27 function __construct( $file, $softNamespaces=false ) {
28 $this->softNamespaces = $softNamespaces;
29 $this->run( $file );
30 }
31
32 /**
33 * Get the root element. Simple accessor to $rootElement
34 */
35 public function getRootElement() {
36 return $this->rootElement;
37 }
38
39 private function run( $fname ) {
40 if( $this->softNamespaces ) {
41 $parser = xml_parser_create( 'UTF-8' );
42 } else {
43 $parser = xml_parser_create_ns( 'UTF-8' );
44 }
45
46 // case folding violates XML standard, turn it off
47 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
48
49 xml_set_element_handler( $parser, array( $this, 'elementOpen' ), false );
50
51 $file = fopen( $fname, "rb" );
52 do {
53 $chunk = fread( $file, 32768 );
54 $ret = xml_parse( $parser, $chunk, feof( $file ) );
55 if( $ret == 0 ) {
56 // XML isn't well-formed!
57 fclose( $file );
58 xml_parser_free( $parser );
59 return;
60 }
61 } while( !feof( $file ) );
62
63 $this->wellFormed = true;
64
65 fclose( $file );
66 xml_parser_free( $parser );
67 }
68
69 private function elementOpen( $parser, $name, $attribs ) {
70 if( $this->softNamespaces ) {
71 // Check namespaces manually, so expat doesn't throw
72 // errors on use of undeclared namespaces.
73 foreach( $attribs as $attrib => $val ) {
74 if( $attrib == 'xmlns' ) {
75 $this->namespaces[''] = $val;
76 } elseif( substr( $attrib, 0, strlen( 'xmlns:' ) ) == 'xmlns:' ) {
77 $this->namespaces[substr( $attrib, strlen( 'xmlns:' ) )] = $val;
78 }
79 }
80
81 if( strpos( $name, ':' ) === false ) {
82 $ns = '';
83 $subname = $name;
84 } else {
85 list( $ns, $subname ) = explode( ':', $name, 2 );
86 }
87
88 if( isset( $this->namespaces[$ns] ) ) {
89 $name = $this->namespaces[$ns] . ':' . $subname;
90 } else {
91 // Technically this is invalid for XML with Namespaces.
92 // But..... we'll just let it slide in soft mode.
93 }
94 }
95
96 // We only need the first open element
97 $this->rootElement = $name;
98 xml_set_element_handler( $parser, false, false );
99 }
100 }