3 * Atom Syndication Format PHP Library
6 * @link http://code.google.com/p/phpatomlib/
8 * @author Elias Torres <elias@torrez.us>
14 * Structure that store common Atom Feed Properties
30 var $categories = array();
37 var $entries = array();
41 * Structure that store Atom Entry Properties
57 var $categories = array();
61 * AtomLib Atom Parser API
67 var $NS = 'http://www.w3.org/2005/Atom';
68 var $ATOM_CONTENT_ELEMENTS = array('content','summary','title','subtitle','rights');
69 var $ATOM_SIMPLE_ELEMENTS = array('id','updated','published','draft');
76 var $ns_contexts = array();
77 var $ns_decls = array();
78 var $content_ns_decls = array();
79 var $content_ns_contexts = array();
80 var $is_xhtml = false;
83 var $skipped_div = false;
85 var $FILE = "php://input";
93 function __construct() {
95 $this->feed = new AtomFeed();
96 $this->current = null;
97 $this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";');
98 $this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";');
104 public function AtomParser() {
110 print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";
114 function error_handler($log_level, $log_text, $error_file, $error_line) {
115 $this->error = $log_text;
120 set_error_handler(array(&$this, 'error_handler'));
122 array_unshift($this->ns_contexts, array());
124 if ( ! function_exists( 'xml_parser_create_ns' ) ) {
125 trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
129 $parser = xml_parser_create_ns();
130 xml_set_object($parser, $this);
131 xml_set_element_handler($parser, "start_element", "end_element");
132 xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
133 xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
134 xml_set_character_data_handler($parser, "cdata");
135 xml_set_default_handler($parser, "_default");
136 xml_set_start_namespace_decl_handler($parser, "start_ns");
137 xml_set_end_namespace_decl_handler($parser, "end_ns");
143 $fp = fopen($this->FILE, "r");
144 while ($data = fread($fp, 4096)) {
145 if($this->debug) $this->content .= $data;
147 if(!xml_parse($parser, $data, feof($fp))) {
148 /* translators: 1: error message, 2: line number */
149 trigger_error(sprintf(__('XML Error: %1$s at line %2$s')."\n",
150 xml_error_string(xml_get_error_code($parser)),
151 xml_get_current_line_number($parser)));
158 xml_parser_free($parser);
160 restore_error_handler();
165 function start_element($parser, $name, $attrs) {
167 $tag = array_pop(explode(":", $name));
170 case $this->NS . ':feed':
171 $this->current = $this->feed;
173 case $this->NS . ':entry':
174 $this->current = new AtomEntry();
178 $this->_p("start_element('$name')");
179 #$this->_p(print_r($this->ns_contexts,true));
180 #$this->_p('current(' . $this->current . ')');
182 array_unshift($this->ns_contexts, $this->ns_decls);
186 if(!empty($this->in_content)) {
188 $this->content_ns_decls = array();
190 if($this->is_html || $this->is_text)
191 trigger_error("Invalid content in element found. Content must not be of type text or html if it contains markup.");
193 $attrs_prefix = array();
195 // resolve prefixes for attributes
196 foreach($attrs as $key => $value) {
197 $with_prefix = $this->ns_to_prefix($key, true);
198 $attrs_prefix[$with_prefix[1]] = $this->xml_escape($value);
201 $attrs_str = join(' ', array_map($this->map_attrs_func, array_keys($attrs_prefix), array_values($attrs_prefix)));
202 if(strlen($attrs_str) > 0) {
203 $attrs_str = " " . $attrs_str;
206 $with_prefix = $this->ns_to_prefix($name);
208 if(!$this->is_declared_content_ns($with_prefix[0])) {
209 array_push($this->content_ns_decls, $with_prefix[0]);
213 if(count($this->content_ns_decls) > 0) {
214 array_unshift($this->content_ns_contexts, $this->content_ns_decls);
215 $xmlns_str .= join(' ', array_map($this->map_xmlns_func, array_keys($this->content_ns_contexts[0]), array_values($this->content_ns_contexts[0])));
216 if(strlen($xmlns_str) > 0) {
217 $xmlns_str = " " . $xmlns_str;
221 array_push($this->in_content, array($tag, $this->depth, "<". $with_prefix[1] ."{$xmlns_str}{$attrs_str}" . ">"));
223 } else if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS) || in_array($tag, $this->ATOM_SIMPLE_ELEMENTS)) {
224 $this->in_content = array();
225 $this->is_xhtml = $attrs['type'] == 'xhtml';
226 $this->is_html = $attrs['type'] == 'html' || $attrs['type'] == 'text/html';
227 $this->is_text = !in_array('type',array_keys($attrs)) || $attrs['type'] == 'text';
228 $type = $this->is_xhtml ? 'XHTML' : ($this->is_html ? 'HTML' : ($this->is_text ? 'TEXT' : $attrs['type']));
230 if(in_array('src',array_keys($attrs))) {
231 $this->current->$tag = $attrs;
233 array_push($this->in_content, array($tag,$this->depth, $type));
235 } else if($tag == 'link') {
236 array_push($this->current->links, $attrs);
237 } else if($tag == 'category') {
238 array_push($this->current->categories, $attrs);
241 $this->ns_decls = array();
244 function end_element($parser, $name) {
246 $tag = array_pop(explode(":", $name));
248 $ccount = count($this->in_content);
250 # if we are *in* content, then let's proceed to serialize it
251 if(!empty($this->in_content)) {
252 # if we are ending the original content element
253 # then let's finalize the content
254 if($this->in_content[0][0] == $tag &&
255 $this->in_content[0][1] == $this->depth) {
256 $origtype = $this->in_content[0][2];
257 array_shift($this->in_content);
258 $newcontent = array();
259 foreach($this->in_content as $c) {
261 array_push($newcontent, $c[2]);
263 if($this->is_xhtml || $this->is_text) {
264 array_push($newcontent, $this->xml_escape($c));
266 array_push($newcontent, $c);
270 if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS)) {
271 $this->current->$tag = array($origtype, join('',$newcontent));
273 $this->current->$tag = join('',$newcontent);
275 $this->in_content = array();
276 } else if($this->in_content[$ccount-1][0] == $tag &&
277 $this->in_content[$ccount-1][1] == $this->depth) {
278 $this->in_content[$ccount-1][2] = substr($this->in_content[$ccount-1][2],0,-1) . "/>";
280 # else, just finalize the current element's content
281 $endtag = $this->ns_to_prefix($name);
282 array_push($this->in_content, array($tag, $this->depth, "</$endtag[1]>"));
286 array_shift($this->ns_contexts);
290 if($name == ($this->NS . ':entry')) {
291 array_push($this->feed->entries, $this->current);
292 $this->current = null;
295 $this->_p("end_element('$name')");
298 function start_ns($parser, $prefix, $uri) {
299 $this->_p("starting: " . $prefix . ":" . $uri);
300 array_push($this->ns_decls, array($prefix,$uri));
303 function end_ns($parser, $prefix) {
304 $this->_p("ending: #" . $prefix . "#");
307 function cdata($parser, $data) {
308 $this->_p("data: #" . str_replace(array("\n"), array("\\n"), trim($data)) . "#");
309 if(!empty($this->in_content)) {
310 array_push($this->in_content, $data);
314 function _default($parser, $data) {
315 # when does this gets called?
319 function ns_to_prefix($qname, $attr=false) {
320 # split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div')
321 $components = explode(":", $qname);
323 # grab the last one (e.g 'div')
324 $name = array_pop($components);
326 if(!empty($components)) {
327 # re-join back the namespace component
328 $ns = join(":",$components);
329 foreach($this->ns_contexts as $context) {
330 foreach($context as $mapping) {
331 if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
332 return array($mapping, "$mapping[0]:$name");
339 return array(null, $name);
341 foreach($this->ns_contexts as $context) {
342 foreach($context as $mapping) {
343 if(strlen($mapping[0]) == 0) {
344 return array($mapping, $name);
351 function is_declared_content_ns($new_mapping) {
352 foreach($this->content_ns_contexts as $context) {
353 foreach($context as $mapping) {
354 if($new_mapping == $mapping) {
362 function xml_escape($string)
364 return str_replace(array('&','"',"'",'<','>'),
365 array('&','"',''','<','>'),