2007-08-27 15:52:58 -04:00
|
|
|
<?php
|
2008-06-14 12:57:20 -04:00
|
|
|
/**
|
2007-12-20 17:28:54 -05:00
|
|
|
* Atom Syndication Format PHP Library
|
2007-08-27 15:52:58 -04:00
|
|
|
*
|
2007-12-20 17:28:54 -05:00
|
|
|
* @package AtomLib
|
|
|
|
* @link http://code.google.com/p/phpatomlib/
|
2007-09-03 19:32:58 -04:00
|
|
|
*
|
2008-06-20 16:56:40 -04:00
|
|
|
* @author Elias Torres <elias@torrez.us>
|
|
|
|
* @version 0.4
|
2013-12-24 13:57:12 -05:00
|
|
|
* @since 2.3.0
|
2007-08-27 15:52:58 -04:00
|
|
|
*/
|
|
|
|
|
2007-12-20 17:28:54 -05:00
|
|
|
/**
|
|
|
|
* Structure that store common Atom Feed Properties
|
|
|
|
*
|
|
|
|
* @package AtomLib
|
|
|
|
*/
|
2007-08-27 15:52:58 -04:00
|
|
|
class AtomFeed {
|
2007-12-20 17:28:54 -05:00
|
|
|
/**
|
|
|
|
* Stores Links
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-08-27 15:52:58 -04:00
|
|
|
var $links = array();
|
2007-12-20 17:28:54 -05:00
|
|
|
/**
|
|
|
|
* Stores Categories
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-08-27 15:52:58 -04:00
|
|
|
var $categories = array();
|
2007-12-20 17:28:54 -05:00
|
|
|
/**
|
|
|
|
* Stores Entries
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-08-27 15:52:58 -04:00
|
|
|
var $entries = array();
|
|
|
|
}
|
|
|
|
|
2007-12-20 17:28:54 -05:00
|
|
|
/**
|
|
|
|
* Structure that store Atom Entry Properties
|
|
|
|
*
|
|
|
|
* @package AtomLib
|
|
|
|
*/
|
2007-08-27 15:52:58 -04:00
|
|
|
class AtomEntry {
|
2007-12-20 17:28:54 -05:00
|
|
|
/**
|
|
|
|
* Stores Links
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-08-27 15:52:58 -04:00
|
|
|
var $links = array();
|
2007-12-20 17:28:54 -05:00
|
|
|
/**
|
|
|
|
* Stores Categories
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-08-27 15:52:58 -04:00
|
|
|
var $categories = array();
|
|
|
|
}
|
|
|
|
|
2007-12-20 17:28:54 -05:00
|
|
|
/**
|
|
|
|
* AtomLib Atom Parser API
|
|
|
|
*
|
|
|
|
* @package AtomLib
|
|
|
|
*/
|
2007-08-27 15:52:58 -04:00
|
|
|
class AtomParser {
|
|
|
|
|
|
|
|
var $NS = 'http://www.w3.org/2005/Atom';
|
|
|
|
var $ATOM_CONTENT_ELEMENTS = array('content','summary','title','subtitle','rights');
|
|
|
|
var $ATOM_SIMPLE_ELEMENTS = array('id','updated','published','draft');
|
|
|
|
|
|
|
|
var $debug = false;
|
|
|
|
|
|
|
|
var $depth = 0;
|
|
|
|
var $indent = 2;
|
|
|
|
var $in_content;
|
|
|
|
var $ns_contexts = array();
|
|
|
|
var $ns_decls = array();
|
|
|
|
var $content_ns_decls = array();
|
|
|
|
var $content_ns_contexts = array();
|
|
|
|
var $is_xhtml = false;
|
|
|
|
var $is_html = false;
|
|
|
|
var $is_text = true;
|
|
|
|
var $skipped_div = false;
|
|
|
|
|
|
|
|
var $FILE = "php://input";
|
|
|
|
|
|
|
|
var $feed;
|
|
|
|
var $current;
|
|
|
|
|
2015-06-28 11:27:24 -04:00
|
|
|
/**
|
|
|
|
* PHP5 constructor.
|
|
|
|
*/
|
|
|
|
function __construct() {
|
2007-09-03 19:19:20 -04:00
|
|
|
|
2007-08-27 15:52:58 -04:00
|
|
|
$this->feed = new AtomFeed();
|
|
|
|
$this->current = null;
|
2016-12-12 20:49:39 -05:00
|
|
|
$this->map_attrs_func = array( __CLASS__, 'map_attrs' );
|
|
|
|
$this->map_xmlns_func = array( __CLASS__, 'map_xmlns' );
|
2007-08-27 15:52:58 -04:00
|
|
|
}
|
|
|
|
|
2015-06-28 11:27:24 -04:00
|
|
|
/**
|
|
|
|
* PHP4 constructor.
|
|
|
|
*/
|
|
|
|
public function AtomParser() {
|
|
|
|
self::__construct();
|
|
|
|
}
|
|
|
|
|
2016-12-12 20:49:39 -05:00
|
|
|
/**
|
|
|
|
* Map attributes to key="val"
|
|
|
|
*
|
|
|
|
* @param string $k Key
|
|
|
|
* @param string $v Value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function map_attrs($k, $v) {
|
|
|
|
return "$k=\"$v\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map XML namespace to string.
|
|
|
|
*
|
|
|
|
* @param indexish $p XML Namespace element index
|
|
|
|
* @param array $n Two-element array pair. [ 0 => {namespace}, 1 => {url} ]
|
|
|
|
* @return string 'xmlns="{url}"' or 'xmlns:{namespace}="{url}"'
|
|
|
|
*/
|
|
|
|
public static function map_xmlns($p, $n) {
|
|
|
|
$xd = "xmlns";
|
|
|
|
if( 0 < strlen($n[0]) ) {
|
|
|
|
$xd .= ":{$n[0]}";
|
|
|
|
}
|
|
|
|
return "{$xd}=\"{$n[1]}\"";
|
|
|
|
}
|
|
|
|
|
2007-08-27 15:52:58 -04:00
|
|
|
function _p($msg) {
|
|
|
|
if($this->debug) {
|
|
|
|
print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function error_handler($log_level, $log_text, $error_file, $error_line) {
|
|
|
|
$this->error = $log_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse() {
|
|
|
|
|
|
|
|
set_error_handler(array(&$this, 'error_handler'));
|
|
|
|
|
|
|
|
array_unshift($this->ns_contexts, array());
|
|
|
|
|
2016-10-24 00:45:31 -04:00
|
|
|
if ( ! function_exists( 'xml_parser_create_ns' ) ) {
|
|
|
|
trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-27 15:52:58 -04:00
|
|
|
$parser = xml_parser_create_ns();
|
|
|
|
xml_set_object($parser, $this);
|
|
|
|
xml_set_element_handler($parser, "start_element", "end_element");
|
|
|
|
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
|
|
|
|
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
|
|
|
|
xml_set_character_data_handler($parser, "cdata");
|
|
|
|
xml_set_default_handler($parser, "_default");
|
|
|
|
xml_set_start_namespace_decl_handler($parser, "start_ns");
|
|
|
|
xml_set_end_namespace_decl_handler($parser, "end_ns");
|
|
|
|
|
|
|
|
$this->content = '';
|
|
|
|
|
|
|
|
$ret = true;
|
|
|
|
|
|
|
|
$fp = fopen($this->FILE, "r");
|
|
|
|
while ($data = fread($fp, 4096)) {
|
|
|
|
if($this->debug) $this->content .= $data;
|
2007-09-03 19:19:20 -04:00
|
|
|
|
2007-08-27 15:52:58 -04:00
|
|
|
if(!xml_parse($parser, $data, feof($fp))) {
|
2019-09-02 20:41:05 -04:00
|
|
|
/* translators: 1: Error message, 2: Line number. */
|
2015-05-09 14:27:26 -04:00
|
|
|
trigger_error(sprintf(__('XML Error: %1$s at line %2$s')."\n",
|
2013-05-07 12:37:21 -04:00
|
|
|
xml_error_string(xml_get_error_code($parser)),
|
|
|
|
xml_get_current_line_number($parser)));
|
2007-08-27 15:52:58 -04:00
|
|
|
$ret = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
xml_parser_free($parser);
|
2020-07-05 11:22:01 -04:00
|
|
|
unset($parser);
|
2007-08-27 15:52:58 -04:00
|
|
|
|
|
|
|
restore_error_handler();
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_element($parser, $name, $attrs) {
|
|
|
|
|
2020-10-17 11:45:06 -04:00
|
|
|
$name_parts = explode(":", $name);
|
|
|
|
$tag = array_pop($name_parts);
|
2007-08-27 15:52:58 -04:00
|
|
|
|
|
|
|
switch($name) {
|
|
|
|
case $this->NS . ':feed':
|
|
|
|
$this->current = $this->feed;
|
|
|
|
break;
|
|
|
|
case $this->NS . ':entry':
|
|
|
|
$this->current = new AtomEntry();
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->_p("start_element('$name')");
|
|
|
|
#$this->_p(print_r($this->ns_contexts,true));
|
|
|
|
#$this->_p('current(' . $this->current . ')');
|
|
|
|
|
|
|
|
array_unshift($this->ns_contexts, $this->ns_decls);
|
|
|
|
|
|
|
|
$this->depth++;
|
|
|
|
|
|
|
|
if(!empty($this->in_content)) {
|
|
|
|
|
|
|
|
$this->content_ns_decls = array();
|
|
|
|
|
|
|
|
if($this->is_html || $this->is_text)
|
|
|
|
trigger_error("Invalid content in element found. Content must not be of type text or html if it contains markup.");
|
|
|
|
|
|
|
|
$attrs_prefix = array();
|
|
|
|
|
|
|
|
// resolve prefixes for attributes
|
|
|
|
foreach($attrs as $key => $value) {
|
|
|
|
$with_prefix = $this->ns_to_prefix($key, true);
|
|
|
|
$attrs_prefix[$with_prefix[1]] = $this->xml_escape($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$attrs_str = join(' ', array_map($this->map_attrs_func, array_keys($attrs_prefix), array_values($attrs_prefix)));
|
|
|
|
if(strlen($attrs_str) > 0) {
|
|
|
|
$attrs_str = " " . $attrs_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
$with_prefix = $this->ns_to_prefix($name);
|
|
|
|
|
|
|
|
if(!$this->is_declared_content_ns($with_prefix[0])) {
|
|
|
|
array_push($this->content_ns_decls, $with_prefix[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$xmlns_str = '';
|
|
|
|
if(count($this->content_ns_decls) > 0) {
|
|
|
|
array_unshift($this->content_ns_contexts, $this->content_ns_decls);
|
|
|
|
$xmlns_str .= join(' ', array_map($this->map_xmlns_func, array_keys($this->content_ns_contexts[0]), array_values($this->content_ns_contexts[0])));
|
|
|
|
if(strlen($xmlns_str) > 0) {
|
|
|
|
$xmlns_str = " " . $xmlns_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array_push($this->in_content, array($tag, $this->depth, "<". $with_prefix[1] ."{$xmlns_str}{$attrs_str}" . ">"));
|
|
|
|
|
|
|
|
} else if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS) || in_array($tag, $this->ATOM_SIMPLE_ELEMENTS)) {
|
|
|
|
$this->in_content = array();
|
2007-09-03 19:32:58 -04:00
|
|
|
$this->is_xhtml = $attrs['type'] == 'xhtml';
|
|
|
|
$this->is_html = $attrs['type'] == 'html' || $attrs['type'] == 'text/html';
|
2007-08-27 15:52:58 -04:00
|
|
|
$this->is_text = !in_array('type',array_keys($attrs)) || $attrs['type'] == 'text';
|
|
|
|
$type = $this->is_xhtml ? 'XHTML' : ($this->is_html ? 'HTML' : ($this->is_text ? 'TEXT' : $attrs['type']));
|
|
|
|
|
|
|
|
if(in_array('src',array_keys($attrs))) {
|
|
|
|
$this->current->$tag = $attrs;
|
|
|
|
} else {
|
|
|
|
array_push($this->in_content, array($tag,$this->depth, $type));
|
|
|
|
}
|
|
|
|
} else if($tag == 'link') {
|
|
|
|
array_push($this->current->links, $attrs);
|
|
|
|
} else if($tag == 'category') {
|
|
|
|
array_push($this->current->categories, $attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->ns_decls = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
function end_element($parser, $name) {
|
|
|
|
|
2020-10-17 11:45:06 -04:00
|
|
|
$name_parts = explode(":", $name);
|
|
|
|
$tag = array_pop($name_parts);
|
2007-08-27 15:52:58 -04:00
|
|
|
|
|
|
|
$ccount = count($this->in_content);
|
|
|
|
|
|
|
|
# if we are *in* content, then let's proceed to serialize it
|
|
|
|
if(!empty($this->in_content)) {
|
|
|
|
# if we are ending the original content element
|
|
|
|
# then let's finalize the content
|
2007-09-03 19:32:58 -04:00
|
|
|
if($this->in_content[0][0] == $tag &&
|
2007-08-27 15:52:58 -04:00
|
|
|
$this->in_content[0][1] == $this->depth) {
|
|
|
|
$origtype = $this->in_content[0][2];
|
|
|
|
array_shift($this->in_content);
|
|
|
|
$newcontent = array();
|
|
|
|
foreach($this->in_content as $c) {
|
|
|
|
if(count($c) == 3) {
|
|
|
|
array_push($newcontent, $c[2]);
|
|
|
|
} else {
|
2007-09-02 16:42:00 -04:00
|
|
|
if($this->is_xhtml || $this->is_text) {
|
2007-08-27 15:52:58 -04:00
|
|
|
array_push($newcontent, $this->xml_escape($c));
|
|
|
|
} else {
|
|
|
|
array_push($newcontent, $c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS)) {
|
|
|
|
$this->current->$tag = array($origtype, join('',$newcontent));
|
|
|
|
} else {
|
|
|
|
$this->current->$tag = join('',$newcontent);
|
|
|
|
}
|
|
|
|
$this->in_content = array();
|
|
|
|
} else if($this->in_content[$ccount-1][0] == $tag &&
|
|
|
|
$this->in_content[$ccount-1][1] == $this->depth) {
|
|
|
|
$this->in_content[$ccount-1][2] = substr($this->in_content[$ccount-1][2],0,-1) . "/>";
|
|
|
|
} else {
|
|
|
|
# else, just finalize the current element's content
|
|
|
|
$endtag = $this->ns_to_prefix($name);
|
|
|
|
array_push($this->in_content, array($tag, $this->depth, "</$endtag[1]>"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array_shift($this->ns_contexts);
|
|
|
|
|
|
|
|
$this->depth--;
|
|
|
|
|
|
|
|
if($name == ($this->NS . ':entry')) {
|
|
|
|
array_push($this->feed->entries, $this->current);
|
|
|
|
$this->current = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_p("end_element('$name')");
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_ns($parser, $prefix, $uri) {
|
|
|
|
$this->_p("starting: " . $prefix . ":" . $uri);
|
|
|
|
array_push($this->ns_decls, array($prefix,$uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
function end_ns($parser, $prefix) {
|
|
|
|
$this->_p("ending: #" . $prefix . "#");
|
|
|
|
}
|
|
|
|
|
|
|
|
function cdata($parser, $data) {
|
|
|
|
$this->_p("data: #" . str_replace(array("\n"), array("\\n"), trim($data)) . "#");
|
|
|
|
if(!empty($this->in_content)) {
|
|
|
|
array_push($this->in_content, $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _default($parser, $data) {
|
|
|
|
# when does this gets called?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function ns_to_prefix($qname, $attr=false) {
|
|
|
|
# split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div')
|
2016-05-07 11:59:29 -04:00
|
|
|
$components = explode(":", $qname);
|
2007-08-27 15:52:58 -04:00
|
|
|
|
|
|
|
# grab the last one (e.g 'div')
|
|
|
|
$name = array_pop($components);
|
|
|
|
|
|
|
|
if(!empty($components)) {
|
|
|
|
# re-join back the namespace component
|
|
|
|
$ns = join(":",$components);
|
|
|
|
foreach($this->ns_contexts as $context) {
|
|
|
|
foreach($context as $mapping) {
|
|
|
|
if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
|
|
|
|
return array($mapping, "$mapping[0]:$name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-03 19:32:58 -04:00
|
|
|
}
|
2007-08-27 15:52:58 -04:00
|
|
|
|
|
|
|
if($attr) {
|
|
|
|
return array(null, $name);
|
|
|
|
} else {
|
|
|
|
foreach($this->ns_contexts as $context) {
|
|
|
|
foreach($context as $mapping) {
|
|
|
|
if(strlen($mapping[0]) == 0) {
|
|
|
|
return array($mapping, $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function is_declared_content_ns($new_mapping) {
|
|
|
|
foreach($this->content_ns_contexts as $context) {
|
|
|
|
foreach($context as $mapping) {
|
|
|
|
if($new_mapping == $mapping) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/atomlib.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$string` parameter to `$content` in `AtomParser::xml_escape()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53239
git-svn-id: http://core.svn.wordpress.org/trunk@52828 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-21 07:24:17 -04:00
|
|
|
function xml_escape($content)
|
2007-08-27 15:52:58 -04:00
|
|
|
{
|
2007-09-03 19:32:58 -04:00
|
|
|
return str_replace(array('&','"',"'",'<','>'),
|
|
|
|
array('&','"',''','<','>'),
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/atomlib.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$string` parameter to `$content` in `AtomParser::xml_escape()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53239
git-svn-id: http://core.svn.wordpress.org/trunk@52828 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-21 07:24:17 -04:00
|
|
|
$content );
|
2007-08-27 15:52:58 -04:00
|
|
|
}
|
|
|
|
}
|