PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/atomlib.php

http://github.com/wordpress/wordpress
PHP | 394 lines | 246 code | 65 blank | 83 comment | 61 complexity | 3e422f1490ddaf417a29bb9e41114dab MD5 | raw file
Possible License(s): 0BSD
  1. <?php
  2. /**
  3. * Atom Syndication Format PHP Library
  4. *
  5. * @package AtomLib
  6. * @link http://code.google.com/p/phpatomlib/
  7. *
  8. * @author Elias Torres <elias@torrez.us>
  9. * @version 0.4
  10. * @since 2.3.0
  11. */
  12. /**
  13. * Structure that store common Atom Feed Properties
  14. *
  15. * @package AtomLib
  16. */
  17. class AtomFeed {
  18. /**
  19. * Stores Links
  20. * @var array
  21. * @access public
  22. */
  23. var $links = array();
  24. /**
  25. * Stores Categories
  26. * @var array
  27. * @access public
  28. */
  29. var $categories = array();
  30. /**
  31. * Stores Entries
  32. *
  33. * @var array
  34. * @access public
  35. */
  36. var $entries = array();
  37. }
  38. /**
  39. * Structure that store Atom Entry Properties
  40. *
  41. * @package AtomLib
  42. */
  43. class AtomEntry {
  44. /**
  45. * Stores Links
  46. * @var array
  47. * @access public
  48. */
  49. var $links = array();
  50. /**
  51. * Stores Categories
  52. * @var array
  53. * @access public
  54. */
  55. var $categories = array();
  56. }
  57. /**
  58. * AtomLib Atom Parser API
  59. *
  60. * @package AtomLib
  61. */
  62. class AtomParser {
  63. var $NS = 'http://www.w3.org/2005/Atom';
  64. var $ATOM_CONTENT_ELEMENTS = array('content','summary','title','subtitle','rights');
  65. var $ATOM_SIMPLE_ELEMENTS = array('id','updated','published','draft');
  66. var $debug = false;
  67. var $depth = 0;
  68. var $indent = 2;
  69. var $in_content;
  70. var $ns_contexts = array();
  71. var $ns_decls = array();
  72. var $content_ns_decls = array();
  73. var $content_ns_contexts = array();
  74. var $is_xhtml = false;
  75. var $is_html = false;
  76. var $is_text = true;
  77. var $skipped_div = false;
  78. var $FILE = "php://input";
  79. var $feed;
  80. var $current;
  81. /**
  82. * PHP5 constructor.
  83. */
  84. function __construct() {
  85. $this->feed = new AtomFeed();
  86. $this->current = null;
  87. $this->map_attrs_func = array( __CLASS__, 'map_attrs' );
  88. $this->map_xmlns_func = array( __CLASS__, 'map_xmlns' );
  89. }
  90. /**
  91. * PHP4 constructor.
  92. */
  93. public function AtomParser() {
  94. self::__construct();
  95. }
  96. /**
  97. * Map attributes to key="val"
  98. *
  99. * @param string $k Key
  100. * @param string $v Value
  101. * @return string
  102. */
  103. public static function map_attrs($k, $v) {
  104. return "$k=\"$v\"";
  105. }
  106. /**
  107. * Map XML namespace to string.
  108. *
  109. * @param indexish $p XML Namespace element index
  110. * @param array $n Two-element array pair. [ 0 => {namespace}, 1 => {url} ]
  111. * @return string 'xmlns="{url}"' or 'xmlns:{namespace}="{url}"'
  112. */
  113. public static function map_xmlns($p, $n) {
  114. $xd = "xmlns";
  115. if( 0 < strlen($n[0]) ) {
  116. $xd .= ":{$n[0]}";
  117. }
  118. return "{$xd}=\"{$n[1]}\"";
  119. }
  120. function _p($msg) {
  121. if($this->debug) {
  122. print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";
  123. }
  124. }
  125. function error_handler($log_level, $log_text, $error_file, $error_line) {
  126. $this->error = $log_text;
  127. }
  128. function parse() {
  129. set_error_handler(array(&$this, 'error_handler'));
  130. array_unshift($this->ns_contexts, array());
  131. if ( ! function_exists( 'xml_parser_create_ns' ) ) {
  132. trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
  133. return false;
  134. }
  135. $parser = xml_parser_create_ns();
  136. xml_set_object($parser, $this);
  137. xml_set_element_handler($parser, "start_element", "end_element");
  138. xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
  139. xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
  140. xml_set_character_data_handler($parser, "cdata");
  141. xml_set_default_handler($parser, "_default");
  142. xml_set_start_namespace_decl_handler($parser, "start_ns");
  143. xml_set_end_namespace_decl_handler($parser, "end_ns");
  144. $this->content = '';
  145. $ret = true;
  146. $fp = fopen($this->FILE, "r");
  147. while ($data = fread($fp, 4096)) {
  148. if($this->debug) $this->content .= $data;
  149. if(!xml_parse($parser, $data, feof($fp))) {
  150. /* translators: 1: Error message, 2: Line number. */
  151. trigger_error(sprintf(__('XML Error: %1$s at line %2$s')."\n",
  152. xml_error_string(xml_get_error_code($parser)),
  153. xml_get_current_line_number($parser)));
  154. $ret = false;
  155. break;
  156. }
  157. }
  158. fclose($fp);
  159. xml_parser_free($parser);
  160. restore_error_handler();
  161. return $ret;
  162. }
  163. function start_element($parser, $name, $attrs) {
  164. $tag = array_pop(explode(":", $name));
  165. switch($name) {
  166. case $this->NS . ':feed':
  167. $this->current = $this->feed;
  168. break;
  169. case $this->NS . ':entry':
  170. $this->current = new AtomEntry();
  171. break;
  172. };
  173. $this->_p("start_element('$name')");
  174. #$this->_p(print_r($this->ns_contexts,true));
  175. #$this->_p('current(' . $this->current . ')');
  176. array_unshift($this->ns_contexts, $this->ns_decls);
  177. $this->depth++;
  178. if(!empty($this->in_content)) {
  179. $this->content_ns_decls = array();
  180. if($this->is_html || $this->is_text)
  181. trigger_error("Invalid content in element found. Content must not be of type text or html if it contains markup.");
  182. $attrs_prefix = array();
  183. // resolve prefixes for attributes
  184. foreach($attrs as $key => $value) {
  185. $with_prefix = $this->ns_to_prefix($key, true);
  186. $attrs_prefix[$with_prefix[1]] = $this->xml_escape($value);
  187. }
  188. $attrs_str = join(' ', array_map($this->map_attrs_func, array_keys($attrs_prefix), array_values($attrs_prefix)));
  189. if(strlen($attrs_str) > 0) {
  190. $attrs_str = " " . $attrs_str;
  191. }
  192. $with_prefix = $this->ns_to_prefix($name);
  193. if(!$this->is_declared_content_ns($with_prefix[0])) {
  194. array_push($this->content_ns_decls, $with_prefix[0]);
  195. }
  196. $xmlns_str = '';
  197. if(count($this->content_ns_decls) > 0) {
  198. array_unshift($this->content_ns_contexts, $this->content_ns_decls);
  199. $xmlns_str .= join(' ', array_map($this->map_xmlns_func, array_keys($this->content_ns_contexts[0]), array_values($this->content_ns_contexts[0])));
  200. if(strlen($xmlns_str) > 0) {
  201. $xmlns_str = " " . $xmlns_str;
  202. }
  203. }
  204. array_push($this->in_content, array($tag, $this->depth, "<". $with_prefix[1] ."{$xmlns_str}{$attrs_str}" . ">"));
  205. } else if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS) || in_array($tag, $this->ATOM_SIMPLE_ELEMENTS)) {
  206. $this->in_content = array();
  207. $this->is_xhtml = $attrs['type'] == 'xhtml';
  208. $this->is_html = $attrs['type'] == 'html' || $attrs['type'] == 'text/html';
  209. $this->is_text = !in_array('type',array_keys($attrs)) || $attrs['type'] == 'text';
  210. $type = $this->is_xhtml ? 'XHTML' : ($this->is_html ? 'HTML' : ($this->is_text ? 'TEXT' : $attrs['type']));
  211. if(in_array('src',array_keys($attrs))) {
  212. $this->current->$tag = $attrs;
  213. } else {
  214. array_push($this->in_content, array($tag,$this->depth, $type));
  215. }
  216. } else if($tag == 'link') {
  217. array_push($this->current->links, $attrs);
  218. } else if($tag == 'category') {
  219. array_push($this->current->categories, $attrs);
  220. }
  221. $this->ns_decls = array();
  222. }
  223. function end_element($parser, $name) {
  224. $tag = array_pop(explode(":", $name));
  225. $ccount = count($this->in_content);
  226. # if we are *in* content, then let's proceed to serialize it
  227. if(!empty($this->in_content)) {
  228. # if we are ending the original content element
  229. # then let's finalize the content
  230. if($this->in_content[0][0] == $tag &&
  231. $this->in_content[0][1] == $this->depth) {
  232. $origtype = $this->in_content[0][2];
  233. array_shift($this->in_content);
  234. $newcontent = array();
  235. foreach($this->in_content as $c) {
  236. if(count($c) == 3) {
  237. array_push($newcontent, $c[2]);
  238. } else {
  239. if($this->is_xhtml || $this->is_text) {
  240. array_push($newcontent, $this->xml_escape($c));
  241. } else {
  242. array_push($newcontent, $c);
  243. }
  244. }
  245. }
  246. if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS)) {
  247. $this->current->$tag = array($origtype, join('',$newcontent));
  248. } else {
  249. $this->current->$tag = join('',$newcontent);
  250. }
  251. $this->in_content = array();
  252. } else if($this->in_content[$ccount-1][0] == $tag &&
  253. $this->in_content[$ccount-1][1] == $this->depth) {
  254. $this->in_content[$ccount-1][2] = substr($this->in_content[$ccount-1][2],0,-1) . "/>";
  255. } else {
  256. # else, just finalize the current element's content
  257. $endtag = $this->ns_to_prefix($name);
  258. array_push($this->in_content, array($tag, $this->depth, "</$endtag[1]>"));
  259. }
  260. }
  261. array_shift($this->ns_contexts);
  262. $this->depth--;
  263. if($name == ($this->NS . ':entry')) {
  264. array_push($this->feed->entries, $this->current);
  265. $this->current = null;
  266. }
  267. $this->_p("end_element('$name')");
  268. }
  269. function start_ns($parser, $prefix, $uri) {
  270. $this->_p("starting: " . $prefix . ":" . $uri);
  271. array_push($this->ns_decls, array($prefix,$uri));
  272. }
  273. function end_ns($parser, $prefix) {
  274. $this->_p("ending: #" . $prefix . "#");
  275. }
  276. function cdata($parser, $data) {
  277. $this->_p("data: #" . str_replace(array("\n"), array("\\n"), trim($data)) . "#");
  278. if(!empty($this->in_content)) {
  279. array_push($this->in_content, $data);
  280. }
  281. }
  282. function _default($parser, $data) {
  283. # when does this gets called?
  284. }
  285. function ns_to_prefix($qname, $attr=false) {
  286. # split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div')
  287. $components = explode(":", $qname);
  288. # grab the last one (e.g 'div')
  289. $name = array_pop($components);
  290. if(!empty($components)) {
  291. # re-join back the namespace component
  292. $ns = join(":",$components);
  293. foreach($this->ns_contexts as $context) {
  294. foreach($context as $mapping) {
  295. if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
  296. return array($mapping, "$mapping[0]:$name");
  297. }
  298. }
  299. }
  300. }
  301. if($attr) {
  302. return array(null, $name);
  303. } else {
  304. foreach($this->ns_contexts as $context) {
  305. foreach($context as $mapping) {
  306. if(strlen($mapping[0]) == 0) {
  307. return array($mapping, $name);
  308. }
  309. }
  310. }
  311. }
  312. }
  313. function is_declared_content_ns($new_mapping) {
  314. foreach($this->content_ns_contexts as $context) {
  315. foreach($context as $mapping) {
  316. if($new_mapping == $mapping) {
  317. return true;
  318. }
  319. }
  320. }
  321. return false;
  322. }
  323. function xml_escape($string)
  324. {
  325. return str_replace(array('&','"',"'",'<','>'),
  326. array('&amp;','&quot;','&apos;','&lt;','&gt;'),
  327. $string );
  328. }
  329. }