Merge "API: Handle "special" options in action=options"
[lhc/web/wiklou.git] / includes / libs / XmlTypeCheck.php
1 <?php
2 /**
3 * XML syntax and type checker.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 class XmlTypeCheck {
24 /**
25 * Will be set to true or false to indicate whether the file is
26 * well-formed XML. Note that this doesn't check schema validity.
27 */
28 public $wellFormed = false;
29
30 /**
31 * Will be set to true if the optional element filter returned
32 * a match at some point.
33 */
34 public $filterMatch = false;
35
36 /**
37 * Name of the document's root element, including any namespace
38 * as an expanded URL.
39 */
40 public $rootElement = '';
41
42 /**
43 * Additional parsing options
44 */
45 private $parserOptions = array(
46 'processing_instruction_handler' => '',
47 );
48
49 /**
50 * @param string $input a filename or string containing the XML element
51 * @param callable $filterCallback (optional)
52 * Function to call to do additional custom validity checks from the
53 * SAX element handler event. This gives you access to the element
54 * namespace, name, and attributes, but not to text contents.
55 * Filter should return 'true' to toggle on $this->filterMatch
56 * @param boolean $isFile (optional) indicates if the first parameter is a
57 * filename (default, true) or if it is a string (false)
58 * @param array $options list of additional parsing options:
59 * processing_instruction_handler: Callback for xml_set_processing_instruction_handler
60 */
61 function __construct( $input, $filterCallback = null, $isFile = true, $options = array() ) {
62 $this->filterCallback = $filterCallback;
63 $this->parserOptions = array_merge( $this->parserOptions, $options );
64
65 if ( $isFile ) {
66 $this->validateFromFile( $input );
67 } else {
68 $this->validateFromString( $input );
69 }
70 }
71
72 /**
73 * Alternative constructor: from filename
74 *
75 * @param string $fname the filename of an XML document
76 * @param callable $filterCallback (optional)
77 * Function to call to do additional custom validity checks from the
78 * SAX element handler event. This gives you access to the element
79 * namespace, name, and attributes, but not to text contents.
80 * Filter should return 'true' to toggle on $this->filterMatch
81 * @return XmlTypeCheck
82 */
83 public static function newFromFilename( $fname, $filterCallback = null ) {
84 return new self( $fname, $filterCallback, true );
85 }
86
87 /**
88 * Alternative constructor: from string
89 *
90 * @param string $string a string containing an XML element
91 * @param callable $filterCallback (optional)
92 * Function to call to do additional custom validity checks from the
93 * SAX element handler event. This gives you access to the element
94 * namespace, name, and attributes, but not to text contents.
95 * Filter should return 'true' to toggle on $this->filterMatch
96 * @return XmlTypeCheck
97 */
98 public static function newFromString( $string, $filterCallback = null ) {
99 return new self( $string, $filterCallback, false );
100 }
101
102 /**
103 * Get the root element. Simple accessor to $rootElement
104 *
105 * @return string
106 */
107 public function getRootElement() {
108 return $this->rootElement;
109 }
110
111 /**
112 * Get an XML parser with the root element handler.
113 * @see XmlTypeCheck::rootElementOpen()
114 * @return resource a resource handle for the XML parser
115 */
116 private function getParser() {
117 $parser = xml_parser_create_ns( 'UTF-8' );
118 // case folding violates XML standard, turn it off
119 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
120 xml_set_element_handler( $parser, array( $this, 'rootElementOpen' ), false );
121 if ( $this->parserOptions['processing_instruction_handler'] ) {
122 xml_set_processing_instruction_handler(
123 $parser,
124 array( $this, 'processingInstructionHandler' )
125 );
126 }
127 return $parser;
128 }
129
130 /**
131 * @param string $fname the filename
132 */
133 private function validateFromFile( $fname ) {
134 $parser = $this->getParser();
135
136 if ( file_exists( $fname ) ) {
137 $file = fopen( $fname, "rb" );
138 if ( $file ) {
139 do {
140 $chunk = fread( $file, 32768 );
141 $ret = xml_parse( $parser, $chunk, feof( $file ) );
142 if ( $ret == 0 ) {
143 $this->wellFormed = false;
144 fclose( $file );
145 xml_parser_free( $parser );
146 return;
147 }
148 } while ( !feof( $file ) );
149
150 fclose( $file );
151 }
152 }
153 $this->wellFormed = true;
154
155 xml_parser_free( $parser );
156 }
157
158 /**
159 *
160 * @param string $string the XML-input-string to be checked.
161 */
162 private function validateFromString( $string ) {
163 $parser = $this->getParser();
164 $ret = xml_parse( $parser, $string, true );
165 xml_parser_free( $parser );
166 if ( $ret == 0 ) {
167 $this->wellFormed = false;
168 return;
169 }
170 $this->wellFormed = true;
171 }
172
173 /**
174 * @param $parser
175 * @param $name
176 * @param $attribs
177 */
178 private function rootElementOpen( $parser, $name, $attribs ) {
179 $this->rootElement = $name;
180
181 if ( is_callable( $this->filterCallback ) ) {
182 xml_set_element_handler( $parser, array( $this, 'elementOpen' ), false );
183 $this->elementOpen( $parser, $name, $attribs );
184 } else {
185 // We only need the first open element
186 xml_set_element_handler( $parser, false, false );
187 }
188 }
189
190 /**
191 * @param $parser
192 * @param $name
193 * @param $attribs
194 */
195 private function elementOpen( $parser, $name, $attribs ) {
196 if ( call_user_func( $this->filterCallback, $name, $attribs ) ) {
197 // Filter hit!
198 $this->filterMatch = true;
199 }
200 }
201
202 /**
203 * @param $parser
204 * @param $target
205 * @param $data
206 */
207 private function processingInstructionHandler( $parser, $target, $data ) {
208 if ( call_user_func( $this->parserOptions['processing_instruction_handler'], $target, $data ) ) {
209 // Filter hit!
210 $this->filterMatch = true;
211 }
212 }
213 }