85 var $FILE = "php://input"; |
85 var $FILE = "php://input"; |
86 |
86 |
87 var $feed; |
87 var $feed; |
88 var $current; |
88 var $current; |
89 |
89 |
90 function AtomParser() { |
90 /** |
|
91 * PHP5 constructor. |
|
92 */ |
|
93 function __construct() { |
91 |
94 |
92 $this->feed = new AtomFeed(); |
95 $this->feed = new AtomFeed(); |
93 $this->current = null; |
96 $this->current = null; |
94 $this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";'); |
97 $this->map_attrs_func = array( __CLASS__, 'map_attrs' ); |
95 $this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";'); |
98 $this->map_xmlns_func = array( __CLASS__, 'map_xmlns' ); |
96 } |
99 } |
|
100 |
|
101 /** |
|
102 * PHP4 constructor. |
|
103 */ |
|
104 public function AtomParser() { |
|
105 self::__construct(); |
|
106 } |
|
107 |
|
108 /** |
|
109 * Map attributes to key="val" |
|
110 * |
|
111 * @param string $k Key |
|
112 * @param string $v Value |
|
113 * @return string |
|
114 */ |
|
115 public static function map_attrs($k, $v) { |
|
116 return "$k=\"$v\""; |
|
117 } |
|
118 |
|
119 /** |
|
120 * Map XML namespace to string. |
|
121 * |
|
122 * @param indexish $p XML Namespace element index |
|
123 * @param array $n Two-element array pair. [ 0 => {namespace}, 1 => {url} ] |
|
124 * @return string 'xmlns="{url}"' or 'xmlns:{namespace}="{url}"' |
|
125 */ |
|
126 public static function map_xmlns($p, $n) { |
|
127 $xd = "xmlns"; |
|
128 if( 0 < strlen($n[0]) ) { |
|
129 $xd .= ":{$n[0]}"; |
|
130 } |
|
131 return "{$xd}=\"{$n[1]}\""; |
|
132 } |
97 |
133 |
98 function _p($msg) { |
134 function _p($msg) { |
99 if($this->debug) { |
135 if($this->debug) { |
100 print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n"; |
136 print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n"; |
101 } |
137 } |
108 function parse() { |
144 function parse() { |
109 |
145 |
110 set_error_handler(array(&$this, 'error_handler')); |
146 set_error_handler(array(&$this, 'error_handler')); |
111 |
147 |
112 array_unshift($this->ns_contexts, array()); |
148 array_unshift($this->ns_contexts, array()); |
|
149 |
|
150 if ( ! function_exists( 'xml_parser_create_ns' ) ) { |
|
151 trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) ); |
|
152 return false; |
|
153 } |
113 |
154 |
114 $parser = xml_parser_create_ns(); |
155 $parser = xml_parser_create_ns(); |
115 xml_set_object($parser, $this); |
156 xml_set_object($parser, $this); |
116 xml_set_element_handler($parser, "start_element", "end_element"); |
157 xml_set_element_handler($parser, "start_element", "end_element"); |
117 xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0); |
158 xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0); |
128 $fp = fopen($this->FILE, "r"); |
169 $fp = fopen($this->FILE, "r"); |
129 while ($data = fread($fp, 4096)) { |
170 while ($data = fread($fp, 4096)) { |
130 if($this->debug) $this->content .= $data; |
171 if($this->debug) $this->content .= $data; |
131 |
172 |
132 if(!xml_parse($parser, $data, feof($fp))) { |
173 if(!xml_parse($parser, $data, feof($fp))) { |
133 trigger_error(sprintf(__('XML error: %s at line %d')."\n", |
174 /* translators: 1: error message, 2: line number */ |
|
175 trigger_error(sprintf(__('XML Error: %1$s at line %2$s')."\n", |
134 xml_error_string(xml_get_error_code($parser)), |
176 xml_error_string(xml_get_error_code($parser)), |
135 xml_get_current_line_number($parser))); |
177 xml_get_current_line_number($parser))); |
136 $ret = false; |
178 $ret = false; |
137 break; |
179 break; |
138 } |
180 } |
146 return $ret; |
188 return $ret; |
147 } |
189 } |
148 |
190 |
149 function start_element($parser, $name, $attrs) { |
191 function start_element($parser, $name, $attrs) { |
150 |
192 |
151 $tag = array_pop(split(":", $name)); |
193 $tag = array_pop(explode(":", $name)); |
152 |
194 |
153 switch($name) { |
195 switch($name) { |
154 case $this->NS . ':feed': |
196 case $this->NS . ':feed': |
155 $this->current = $this->feed; |
197 $this->current = $this->feed; |
156 break; |
198 break; |
225 $this->ns_decls = array(); |
267 $this->ns_decls = array(); |
226 } |
268 } |
227 |
269 |
228 function end_element($parser, $name) { |
270 function end_element($parser, $name) { |
229 |
271 |
230 $tag = array_pop(split(":", $name)); |
272 $tag = array_pop(explode(":", $name)); |
231 |
273 |
232 $ccount = count($this->in_content); |
274 $ccount = count($this->in_content); |
233 |
275 |
234 # if we are *in* content, then let's proceed to serialize it |
276 # if we are *in* content, then let's proceed to serialize it |
235 if(!empty($this->in_content)) { |
277 if(!empty($this->in_content)) { |
300 } |
342 } |
301 |
343 |
302 |
344 |
303 function ns_to_prefix($qname, $attr=false) { |
345 function ns_to_prefix($qname, $attr=false) { |
304 # split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div') |
346 # split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div') |
305 $components = split(":", $qname); |
347 $components = explode(":", $qname); |
306 |
348 |
307 # grab the last one (e.g 'div') |
349 # grab the last one (e.g 'div') |
308 $name = array_pop($components); |
350 $name = array_pop($components); |
309 |
351 |
310 if(!empty($components)) { |
352 if(!empty($components)) { |