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