5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
8 * Copyright (c) 2004-2009, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above copyright notice, this list
18 * of conditions and the following disclaimer in the documentation and/or other materials
19 * provided with the distribution.
21 * * Neither the name of the SimplePie Team nor the names of its contributors may be used
22 * to endorse or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
26 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
28 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * @copyright 2004-2010 Ryan Parman, Geoffrey Sneddon, Ryan McCue
39 * @author Geoffrey Sneddon
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @todo phpDoc comments
47 class SimplePie_Source
52 public function __construct($item, $data)
58 public function __toString()
60 return md5(serialize($this->data
));
63 public function get_source_tags($namespace, $tag)
65 if (isset($this->data
['child'][$namespace][$tag]))
67 return $this->data
['child'][$namespace][$tag];
75 public function get_base($element = array())
77 return $this->item
->get_base($element);
80 public function sanitize($data, $type, $base = '')
82 return $this->item
->sanitize($data, $type, $base);
85 public function get_item()
90 public function get_title()
92 if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10
, 'title'))
94 return $this->sanitize($return[0]['data'], SimplePie_Misc
::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0]));
96 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03
, 'title'))
98 return $this->sanitize($return[0]['data'], SimplePie_Misc
::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0]));
100 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_10
, 'title'))
102 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML
, $this->get_base($return[0]));
104 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_090
, 'title'))
106 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML
, $this->get_base($return[0]));
108 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20
, 'title'))
110 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML
, $this->get_base($return[0]));
112 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11
, 'title'))
114 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
116 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10
, 'title'))
118 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
126 public function get_category($key = 0)
128 $categories = $this->get_categories();
129 if (isset($categories[$key]))
131 return $categories[$key];
139 public function get_categories()
141 $categories = array();
143 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10
, 'category') as $category)
148 if (isset($category['attribs']['']['term']))
150 $term = $this->sanitize($category['attribs']['']['term'], SIMPLEPIE_CONSTRUCT_TEXT
);
152 if (isset($category['attribs']['']['scheme']))
154 $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT
);
156 if (isset($category['attribs']['']['label']))
158 $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT
);
160 $categories[] = new $this->item
->feed
->category_class($term, $scheme, $label);
162 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20
, 'category') as $category)
164 // This is really the label, but keep this as the term also for BC.
165 // Label will also work on retrieving because that falls back to term.
166 $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
167 if (isset($category['attribs']['']['domain']))
169 $scheme = $this->sanitize($category['attribs']['']['domain'], SIMPLEPIE_CONSTRUCT_TEXT
);
175 $categories[] = new $this->item
->feed
->category_class($term, $scheme, null);
177 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11
, 'subject') as $category)
179 $categories[] = new $this->item
->feed
->category_class($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT
), null, null);
181 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10
, 'subject') as $category)
183 $categories[] = new $this->item
->feed
->category_class($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT
), null, null);
186 if (!empty($categories))
188 return SimplePie_Misc
::array_unique($categories);
196 public function get_author($key = 0)
198 $authors = $this->get_authors();
199 if (isset($authors[$key]))
201 return $authors[$key];
209 public function get_authors()
212 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10
, 'author') as $author)
217 if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['name'][0]['data']))
219 $name = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
221 if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['uri'][0]['data']))
223 $uri = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['uri'][0]));
225 if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['email'][0]['data']))
227 $email = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
229 if ($name !== null || $email !== null || $uri !== null)
231 $authors[] = new $this->item
->feed
->author_class($name, $uri, $email);
234 if ($author = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03
, 'author'))
239 if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['name'][0]['data']))
241 $name = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
243 if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['url'][0]['data']))
245 $url = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['url'][0]));
247 if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['email'][0]['data']))
249 $email = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
251 if ($name !== null || $email !== null || $url !== null)
253 $authors[] = new $this->item
->feed
->author_class($name, $url, $email);
256 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11
, 'creator') as $author)
258 $authors[] = new $this->item
->feed
->author_class($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT
), null, null);
260 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10
, 'creator') as $author)
262 $authors[] = new $this->item
->feed
->author_class($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT
), null, null);
264 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ITUNES
, 'author') as $author)
266 $authors[] = new $this->item
->feed
->author_class($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT
), null, null);
269 if (!empty($authors))
271 return SimplePie_Misc
::array_unique($authors);
279 public function get_contributor($key = 0)
281 $contributors = $this->get_contributors();
282 if (isset($contributors[$key]))
284 return $contributors[$key];
292 public function get_contributors()
294 $contributors = array();
295 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10
, 'contributor') as $contributor)
300 if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['name'][0]['data']))
302 $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
304 if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['uri'][0]['data']))
306 $uri = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['uri'][0]));
308 if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['email'][0]['data']))
310 $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
312 if ($name !== null || $email !== null || $uri !== null)
314 $contributors[] = new $this->item
->feed
->author_class($name, $uri, $email);
317 foreach ((array) $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03
, 'contributor') as $contributor)
322 if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['name'][0]['data']))
324 $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
326 if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['url'][0]['data']))
328 $url = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['url'][0]));
330 if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['email'][0]['data']))
332 $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
334 if ($name !== null || $email !== null || $url !== null)
336 $contributors[] = new $this->item
->feed
->author_class($name, $url, $email);
340 if (!empty($contributors))
342 return SimplePie_Misc
::array_unique($contributors);
350 public function get_link($key = 0, $rel = 'alternate')
352 $links = $this->get_links($rel);
353 if (isset($links[$key]))
364 * Added for parity between the parent-level and the item/entry-level.
366 public function get_permalink()
368 return $this->get_link(0);
371 public function get_links($rel = 'alternate')
373 if (!isset($this->data
['links']))
375 $this->data
['links'] = array();
376 if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10
, 'link'))
378 foreach ($links as $link)
380 if (isset($link['attribs']['']['href']))
382 $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
383 $this->data
['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($link));
387 if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03
, 'link'))
389 foreach ($links as $link)
391 if (isset($link['attribs']['']['href']))
393 $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
394 $this->data
['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($link));
399 if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_10
, 'link'))
401 $this->data
['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($links[0]));
403 if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_090
, 'link'))
405 $this->data
['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($links[0]));
407 if ($links = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20
, 'link'))
409 $this->data
['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($links[0]));
412 $keys = array_keys($this->data
['links']);
413 foreach ($keys as $key)
415 if (SimplePie_Misc
::is_isegment_nz_nc($key))
417 if (isset($this->data
['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY
. $key]))
419 $this->data
['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY
. $key] = array_merge($this->data
['links'][$key], $this->data
['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY
. $key]);
420 $this->data
['links'][$key] =& $this->data
['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY
. $key];
424 $this->data
['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY
. $key] =& $this->data
['links'][$key];
427 elseif (substr($key, 0, 41) === SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY
)
429 $this->data
['links'][substr($key, 41)] =& $this->data
['links'][$key];
431 $this->data
['links'][$key] = array_unique($this->data
['links'][$key]);
435 if (isset($this->data
['links'][$rel]))
437 return $this->data
['links'][$rel];
445 public function get_description()
447 if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10
, 'subtitle'))
449 return $this->sanitize($return[0]['data'], SimplePie_Misc
::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0]));
451 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03
, 'tagline'))
453 return $this->sanitize($return[0]['data'], SimplePie_Misc
::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0]));
455 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_10
, 'description'))
457 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML
, $this->get_base($return[0]));
459 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_090
, 'description'))
461 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML
, $this->get_base($return[0]));
463 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20
, 'description'))
465 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML
, $this->get_base($return[0]));
467 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11
, 'description'))
469 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
471 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10
, 'description'))
473 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
475 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ITUNES
, 'summary'))
477 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML
, $this->get_base($return[0]));
479 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ITUNES
, 'subtitle'))
481 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML
, $this->get_base($return[0]));
489 public function get_copyright()
491 if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10
, 'rights'))
493 return $this->sanitize($return[0]['data'], SimplePie_Misc
::atom_10_construct_type($return[0]['attribs']), $this->get_base($return[0]));
495 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03
, 'copyright'))
497 return $this->sanitize($return[0]['data'], SimplePie_Misc
::atom_03_construct_type($return[0]['attribs']), $this->get_base($return[0]));
499 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20
, 'copyright'))
501 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
503 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11
, 'rights'))
505 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
507 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10
, 'rights'))
509 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
517 public function get_language()
519 if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20
, 'language'))
521 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
523 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_11
, 'language'))
525 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
527 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_DC_10
, 'language'))
529 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT
);
531 elseif (isset($this->data
['xml_lang']))
533 return $this->sanitize($this->data
['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT
);
541 public function get_latitude()
543 if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO
, 'lat'))
545 return (float) $return[0]['data'];
547 elseif (($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_GEORSS
, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
549 return (float) $match[1];
557 public function get_longitude()
559 if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO
, 'long'))
561 return (float) $return[0]['data'];
563 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO
, 'lon'))
565 return (float) $return[0]['data'];
567 elseif (($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_GEORSS
, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
569 return (float) $match[2];
577 public function get_image_url()
579 if ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ITUNES
, 'image'))
581 return $this->sanitize($return[0]['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI
);
583 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10
, 'logo'))
585 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($return[0]));
587 elseif ($return = $this->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10
, 'icon'))
589 return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI
, $this->get_base($return[0]));