3 * atomlib.php - Atom Syndication Format PHP Library
5 * Project: http://code.google.com/p/phpatomlib/
7 * Author: Elias Torres <elias@torrez.us>
14 var $categories = array();
16 var $entries = array();
21 var $categories = array();
26 var $NS = 'http://www.w3.org/2005/Atom';
27 var $ATOM_CONTENT_ELEMENTS = array('content','summary','title','subtitle','rights');
28 var $ATOM_SIMPLE_ELEMENTS = array('id','updated','published','draft');
35 var $ns_contexts = array();
36 var $ns_decls = array();
37 var $content_ns_decls = array();
38 var $content_ns_contexts = array();
39 var $is_xhtml = false;
42 var $skipped_div = false;
44 var $FILE = "php://input";
49 function AtomParser() {
51 $this->feed = new AtomFeed();
52 $this->current = null;
53 $this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";');
54 $this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";');
59 print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";
63 function error_handler($log_level, $log_text, $error_file, $error_line) {
64 $this->error = $log_text;
69 set_error_handler(array(&$this, 'error_handler'));
71 array_unshift($this->ns_contexts, array());
73 $parser = xml_parser_create_ns();
74 xml_set_object($parser, $this);
75 xml_set_element_handler($parser, "start_element", "end_element");
76 xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
77 xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
78 xml_set_character_data_handler($parser, "cdata");
79 xml_set_default_handler($parser, "_default");
80 xml_set_start_namespace_decl_handler($parser, "start_ns");
81 xml_set_end_namespace_decl_handler($parser, "end_ns");
87 $fp = fopen($this->FILE, "r");
88 while ($data = fread($fp, 4096)) {
89 if($this->debug) $this->content .= $data;
91 if(!xml_parse($parser, $data, feof($fp))) {
92 trigger_error(sprintf(__('XML error: %s at line %d')."\n",
93 xml_error_string(xml_get_error_code($xml_parser)),
94 xml_get_current_line_number($xml_parser)));
101 xml_parser_free($parser);
103 restore_error_handler();
108 function start_element($parser, $name, $attrs) {
110 $tag = array_pop(split(":", $name));
113 case $this->NS . ':feed':
114 $this->current = $this->feed;
116 case $this->NS . ':entry':
117 $this->current = new AtomEntry();
121 $this->_p("start_element('$name')");
122 #$this->_p(print_r($this->ns_contexts,true));
123 #$this->_p('current(' . $this->current . ')');
125 array_unshift($this->ns_contexts, $this->ns_decls);
129 if(!empty($this->in_content)) {
131 $this->content_ns_decls = array();
133 if($this->is_html || $this->is_text)
134 trigger_error("Invalid content in element found. Content must not be of type text or html if it contains markup.");
136 $attrs_prefix = array();
138 // resolve prefixes for attributes
139 foreach($attrs as $key => $value) {
140 $with_prefix = $this->ns_to_prefix($key, true);
141 $attrs_prefix[$with_prefix[1]] = $this->xml_escape($value);
144 $attrs_str = join(' ', array_map($this->map_attrs_func, array_keys($attrs_prefix), array_values($attrs_prefix)));
145 if(strlen($attrs_str) > 0) {
146 $attrs_str = " " . $attrs_str;
149 $with_prefix = $this->ns_to_prefix($name);
151 if(!$this->is_declared_content_ns($with_prefix[0])) {
152 array_push($this->content_ns_decls, $with_prefix[0]);
156 if(count($this->content_ns_decls) > 0) {
157 array_unshift($this->content_ns_contexts, $this->content_ns_decls);
158 $xmlns_str .= join(' ', array_map($this->map_xmlns_func, array_keys($this->content_ns_contexts[0]), array_values($this->content_ns_contexts[0])));
159 if(strlen($xmlns_str) > 0) {
160 $xmlns_str = " " . $xmlns_str;
164 array_push($this->in_content, array($tag, $this->depth, "<". $with_prefix[1] ."{$xmlns_str}{$attrs_str}" . ">"));
166 } else if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS) || in_array($tag, $this->ATOM_SIMPLE_ELEMENTS)) {
167 $this->in_content = array();
168 $this->is_xhtml = $attrs['type'] == 'xhtml';
169 $this->is_html = $attrs['type'] == 'html' || $attrs['type'] == 'text/html';
170 $this->is_text = !in_array('type',array_keys($attrs)) || $attrs['type'] == 'text';
171 $type = $this->is_xhtml ? 'XHTML' : ($this->is_html ? 'HTML' : ($this->is_text ? 'TEXT' : $attrs['type']));
173 if(in_array('src',array_keys($attrs))) {
174 $this->current->$tag = $attrs;
176 array_push($this->in_content, array($tag,$this->depth, $type));
178 } else if($tag == 'link') {
179 array_push($this->current->links, $attrs);
180 } else if($tag == 'category') {
181 array_push($this->current->categories, $attrs);
184 $this->ns_decls = array();
187 function end_element($parser, $name) {
189 $tag = array_pop(split(":", $name));
191 $ccount = count($this->in_content);
193 # if we are *in* content, then let's proceed to serialize it
194 if(!empty($this->in_content)) {
195 # if we are ending the original content element
196 # then let's finalize the content
197 if($this->in_content[0][0] == $tag &&
198 $this->in_content[0][1] == $this->depth) {
199 $origtype = $this->in_content[0][2];
200 array_shift($this->in_content);
201 $newcontent = array();
202 foreach($this->in_content as $c) {
204 array_push($newcontent, $c[2]);
206 if($this->is_xhtml || $this->is_text) {
207 array_push($newcontent, $this->xml_escape($c));
209 array_push($newcontent, $c);
213 if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS)) {
214 $this->current->$tag = array($origtype, join('',$newcontent));
216 $this->current->$tag = join('',$newcontent);
218 $this->in_content = array();
219 } else if($this->in_content[$ccount-1][0] == $tag &&
220 $this->in_content[$ccount-1][1] == $this->depth) {
221 $this->in_content[$ccount-1][2] = substr($this->in_content[$ccount-1][2],0,-1) . "/>";
223 # else, just finalize the current element's content
224 $endtag = $this->ns_to_prefix($name);
225 array_push($this->in_content, array($tag, $this->depth, "</$endtag[1]>"));
229 array_shift($this->ns_contexts);
233 if($name == ($this->NS . ':entry')) {
234 array_push($this->feed->entries, $this->current);
235 $this->current = null;
238 $this->_p("end_element('$name')");
241 function start_ns($parser, $prefix, $uri) {
242 $this->_p("starting: " . $prefix . ":" . $uri);
243 array_push($this->ns_decls, array($prefix,$uri));
246 function end_ns($parser, $prefix) {
247 $this->_p("ending: #" . $prefix . "#");
250 function cdata($parser, $data) {
251 $this->_p("data: #" . str_replace(array("\n"), array("\\n"), trim($data)) . "#");
252 if(!empty($this->in_content)) {
253 array_push($this->in_content, $data);
257 function _default($parser, $data) {
258 # when does this gets called?
262 function ns_to_prefix($qname, $attr=false) {
263 # split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div')
264 $components = split(":", $qname);
266 # grab the last one (e.g 'div')
267 $name = array_pop($components);
269 if(!empty($components)) {
270 # re-join back the namespace component
271 $ns = join(":",$components);
272 foreach($this->ns_contexts as $context) {
273 foreach($context as $mapping) {
274 if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
275 return array($mapping, "$mapping[0]:$name");
282 return array(null, $name);
284 foreach($this->ns_contexts as $context) {
285 foreach($context as $mapping) {
286 if(strlen($mapping[0]) == 0) {
287 return array($mapping, $name);
294 function is_declared_content_ns($new_mapping) {
295 foreach($this->content_ns_contexts as $context) {
296 foreach($context as $mapping) {
297 if($new_mapping == $mapping) {
305 function xml_escape($string)
307 return str_replace(array('&','"',"'",'<','>'),
308 array('&','"',''','<','>'),