-
+ EDE281F799366D95F41D73BC66791D9486CE22ED6A4E2938814378904F54C39B8FCCF46247AE8D7236FC33C57CCC8A29D593E33D31C062667812A4274FD912CA
mp-wp/wp-includes/atomlib.php
(0 . 0)(1 . 354)
70860 <?php
70861 /**
70862 * Atom Syndication Format PHP Library
70863 *
70864 * @package AtomLib
70865 * @link http://code.google.com/p/phpatomlib/
70866 *
70867 * @author Elias Torres <elias@torrez.us>
70868 * @version 0.4
70869 * @since 2.3
70870 */
70871
70872 /**
70873 * Structure that store common Atom Feed Properties
70874 *
70875 * @package AtomLib
70876 */
70877 class AtomFeed {
70878 /**
70879 * Stores Links
70880 * @var array
70881 * @access public
70882 */
70883 var $links = array();
70884 /**
70885 * Stores Categories
70886 * @var array
70887 * @access public
70888 */
70889 var $categories = array();
70890 /**
70891 * Stores Entries
70892 *
70893 * @var array
70894 * @access public
70895 */
70896 var $entries = array();
70897 }
70898
70899 /**
70900 * Structure that store Atom Entry Properties
70901 *
70902 * @package AtomLib
70903 */
70904 class AtomEntry {
70905 /**
70906 * Stores Links
70907 * @var array
70908 * @access public
70909 */
70910 var $links = array();
70911 /**
70912 * Stores Categories
70913 * @var array
70914 * @access public
70915 */
70916 var $categories = array();
70917 }
70918
70919 /**
70920 * AtomLib Atom Parser API
70921 *
70922 * @package AtomLib
70923 */
70924 class AtomParser {
70925
70926 var $NS = 'http://www.w3.org/2005/Atom';
70927 var $ATOM_CONTENT_ELEMENTS = array('content','summary','title','subtitle','rights');
70928 var $ATOM_SIMPLE_ELEMENTS = array('id','updated','published','draft');
70929
70930 var $debug = false;
70931
70932 var $depth = 0;
70933 var $indent = 2;
70934 var $in_content;
70935 var $ns_contexts = array();
70936 var $ns_decls = array();
70937 var $content_ns_decls = array();
70938 var $content_ns_contexts = array();
70939 var $is_xhtml = false;
70940 var $is_html = false;
70941 var $is_text = true;
70942 var $skipped_div = false;
70943
70944 var $FILE = "php://input";
70945
70946 var $feed;
70947 var $current;
70948
70949 function AtomParser() {
70950
70951 $this->feed = new AtomFeed();
70952 $this->current = null;
70953 $this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";');
70954 $this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";');
70955 }
70956
70957 function _p($msg) {
70958 if($this->debug) {
70959 print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";
70960 }
70961 }
70962
70963 function error_handler($log_level, $log_text, $error_file, $error_line) {
70964 $this->error = $log_text;
70965 }
70966
70967 function parse() {
70968
70969 set_error_handler(array(&$this, 'error_handler'));
70970
70971 array_unshift($this->ns_contexts, array());
70972
70973 $parser = xml_parser_create_ns();
70974 xml_set_object($parser, $this);
70975 xml_set_element_handler($parser, "start_element", "end_element");
70976 xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
70977 xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
70978 xml_set_character_data_handler($parser, "cdata");
70979 xml_set_default_handler($parser, "_default");
70980 xml_set_start_namespace_decl_handler($parser, "start_ns");
70981 xml_set_end_namespace_decl_handler($parser, "end_ns");
70982
70983 $this->content = '';
70984
70985 $ret = true;
70986
70987 $fp = fopen($this->FILE, "r");
70988 while ($data = fread($fp, 4096)) {
70989 if($this->debug) $this->content .= $data;
70990
70991 if(!xml_parse($parser, $data, feof($fp))) {
70992 trigger_error(sprintf(__('XML error: %s at line %d')."\n",
70993 xml_error_string(xml_get_error_code($xml_parser)),
70994 xml_get_current_line_number($xml_parser)));
70995 $ret = false;
70996 break;
70997 }
70998 }
70999 fclose($fp);
71000
71001 xml_parser_free($parser);
71002
71003 restore_error_handler();
71004
71005 return $ret;
71006 }
71007
71008 function start_element($parser, $name, $attrs) {
71009
71010 $tag = array_pop(split(":", $name));
71011
71012 switch($name) {
71013 case $this->NS . ':feed':
71014 $this->current = $this->feed;
71015 break;
71016 case $this->NS . ':entry':
71017 $this->current = new AtomEntry();
71018 break;
71019 };
71020
71021 $this->_p("start_element('$name')");
71022 #$this->_p(print_r($this->ns_contexts,true));
71023 #$this->_p('current(' . $this->current . ')');
71024
71025 array_unshift($this->ns_contexts, $this->ns_decls);
71026
71027 $this->depth++;
71028
71029 if(!empty($this->in_content)) {
71030
71031 $this->content_ns_decls = array();
71032
71033 if($this->is_html || $this->is_text)
71034 trigger_error("Invalid content in element found. Content must not be of type text or html if it contains markup.");
71035
71036 $attrs_prefix = array();
71037
71038 // resolve prefixes for attributes
71039 foreach($attrs as $key => $value) {
71040 $with_prefix = $this->ns_to_prefix($key, true);
71041 $attrs_prefix[$with_prefix[1]] = $this->xml_escape($value);
71042 }
71043
71044 $attrs_str = join(' ', array_map($this->map_attrs_func, array_keys($attrs_prefix), array_values($attrs_prefix)));
71045 if(strlen($attrs_str) > 0) {
71046 $attrs_str = " " . $attrs_str;
71047 }
71048
71049 $with_prefix = $this->ns_to_prefix($name);
71050
71051 if(!$this->is_declared_content_ns($with_prefix[0])) {
71052 array_push($this->content_ns_decls, $with_prefix[0]);
71053 }
71054
71055 $xmlns_str = '';
71056 if(count($this->content_ns_decls) > 0) {
71057 array_unshift($this->content_ns_contexts, $this->content_ns_decls);
71058 $xmlns_str .= join(' ', array_map($this->map_xmlns_func, array_keys($this->content_ns_contexts[0]), array_values($this->content_ns_contexts[0])));
71059 if(strlen($xmlns_str) > 0) {
71060 $xmlns_str = " " . $xmlns_str;
71061 }
71062 }
71063
71064 array_push($this->in_content, array($tag, $this->depth, "<". $with_prefix[1] ."{$xmlns_str}{$attrs_str}" . ">"));
71065
71066 } else if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS) || in_array($tag, $this->ATOM_SIMPLE_ELEMENTS)) {
71067 $this->in_content = array();
71068 $this->is_xhtml = $attrs['type'] == 'xhtml';
71069 $this->is_html = $attrs['type'] == 'html' || $attrs['type'] == 'text/html';
71070 $this->is_text = !in_array('type',array_keys($attrs)) || $attrs['type'] == 'text';
71071 $type = $this->is_xhtml ? 'XHTML' : ($this->is_html ? 'HTML' : ($this->is_text ? 'TEXT' : $attrs['type']));
71072
71073 if(in_array('src',array_keys($attrs))) {
71074 $this->current->$tag = $attrs;
71075 } else {
71076 array_push($this->in_content, array($tag,$this->depth, $type));
71077 }
71078 } else if($tag == 'link') {
71079 array_push($this->current->links, $attrs);
71080 } else if($tag == 'category') {
71081 array_push($this->current->categories, $attrs);
71082 }
71083
71084 $this->ns_decls = array();
71085 }
71086
71087 function end_element($parser, $name) {
71088
71089 $tag = array_pop(split(":", $name));
71090
71091 $ccount = count($this->in_content);
71092
71093 # if we are *in* content, then let's proceed to serialize it
71094 if(!empty($this->in_content)) {
71095 # if we are ending the original content element
71096 # then let's finalize the content
71097 if($this->in_content[0][0] == $tag &&
71098 $this->in_content[0][1] == $this->depth) {
71099 $origtype = $this->in_content[0][2];
71100 array_shift($this->in_content);
71101 $newcontent = array();
71102 foreach($this->in_content as $c) {
71103 if(count($c) == 3) {
71104 array_push($newcontent, $c[2]);
71105 } else {
71106 if($this->is_xhtml || $this->is_text) {
71107 array_push($newcontent, $this->xml_escape($c));
71108 } else {
71109 array_push($newcontent, $c);
71110 }
71111 }
71112 }
71113 if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS)) {
71114 $this->current->$tag = array($origtype, join('',$newcontent));
71115 } else {
71116 $this->current->$tag = join('',$newcontent);
71117 }
71118 $this->in_content = array();
71119 } else if($this->in_content[$ccount-1][0] == $tag &&
71120 $this->in_content[$ccount-1][1] == $this->depth) {
71121 $this->in_content[$ccount-1][2] = substr($this->in_content[$ccount-1][2],0,-1) . "/>";
71122 } else {
71123 # else, just finalize the current element's content
71124 $endtag = $this->ns_to_prefix($name);
71125 array_push($this->in_content, array($tag, $this->depth, "</$endtag[1]>"));
71126 }
71127 }
71128
71129 array_shift($this->ns_contexts);
71130
71131 $this->depth--;
71132
71133 if($name == ($this->NS . ':entry')) {
71134 array_push($this->feed->entries, $this->current);
71135 $this->current = null;
71136 }
71137
71138 $this->_p("end_element('$name')");
71139 }
71140
71141 function start_ns($parser, $prefix, $uri) {
71142 $this->_p("starting: " . $prefix . ":" . $uri);
71143 array_push($this->ns_decls, array($prefix,$uri));
71144 }
71145
71146 function end_ns($parser, $prefix) {
71147 $this->_p("ending: #" . $prefix . "#");
71148 }
71149
71150 function cdata($parser, $data) {
71151 $this->_p("data: #" . str_replace(array("\n"), array("\\n"), trim($data)) . "#");
71152 if(!empty($this->in_content)) {
71153 array_push($this->in_content, $data);
71154 }
71155 }
71156
71157 function _default($parser, $data) {
71158 # when does this gets called?
71159 }
71160
71161
71162 function ns_to_prefix($qname, $attr=false) {
71163 # split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div')
71164 $components = split(":", $qname);
71165
71166 # grab the last one (e.g 'div')
71167 $name = array_pop($components);
71168
71169 if(!empty($components)) {
71170 # re-join back the namespace component
71171 $ns = join(":",$components);
71172 foreach($this->ns_contexts as $context) {
71173 foreach($context as $mapping) {
71174 if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
71175 return array($mapping, "$mapping[0]:$name");
71176 }
71177 }
71178 }
71179 }
71180
71181 if($attr) {
71182 return array(null, $name);
71183 } else {
71184 foreach($this->ns_contexts as $context) {
71185 foreach($context as $mapping) {
71186 if(strlen($mapping[0]) == 0) {
71187 return array($mapping, $name);
71188 }
71189 }
71190 }
71191 }
71192 }
71193
71194 function is_declared_content_ns($new_mapping) {
71195 foreach($this->content_ns_contexts as $context) {
71196 foreach($context as $mapping) {
71197 if($new_mapping == $mapping) {
71198 return true;
71199 }
71200 }
71201 }
71202 return false;
71203 }
71204
71205 function xml_escape($string)
71206 {
71207 return str_replace(array('&','"',"'",'<','>'),
71208 array('&','"',''','<','>'),
71209 $string );
71210 }
71211 }
71212
71213 ?>