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-2012, 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-2012 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
46 * Used for feed auto-discovery
49 * This class can be overloaded with {@see SimplePie::set_locator_class()}
53 class SimplePie_Locator
59 var $elsewhere = array();
60 var $cached_entities = array();
63 var $base_location = 0;
64 var $checked_feeds = 0;
65 var $max_checked_feeds = 10;
68 public function __construct(SimplePie_File $file, $timeout = 10, $useragent = null, $max_checked_feeds = 10)
71 $this->useragent = $useragent;
72 $this->timeout = $timeout;
73 $this->max_checked_feeds = $max_checked_feeds;
75 if (class_exists('DOMDocument'))
77 $this->dom = new DOMDocument();
79 set_error_handler(array('SimplePie_Misc', 'silence_errors'));
80 $this->dom->loadHTML($this->file->body);
81 restore_error_handler();
89 public function set_registry(SimplePie_Registry $registry)
91 $this->registry = $registry;
94 public function find($type = SIMPLEPIE_LOCATOR_ALL, &$working)
96 if ($this->is_feed($this->file))
101 if ($this->file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
103 $sniffer = $this->registry->create('Content_Type_Sniffer', array($this->file));
104 if ($sniffer->get_type() !== 'text/html')
110 if ($type & ~SIMPLEPIE_LOCATOR_NONE)
115 if ($type & SIMPLEPIE_LOCATOR_AUTODISCOVERY && $working = $this->autodiscovery())
120 if ($type & (SIMPLEPIE_LOCATOR_LOCAL_EXTENSION | SIMPLEPIE_LOCATOR_LOCAL_BODY | SIMPLEPIE_LOCATOR_REMOTE_EXTENSION | SIMPLEPIE_LOCATOR_REMOTE_BODY) && $this->get_links())
122 if ($type & SIMPLEPIE_LOCATOR_LOCAL_EXTENSION && $working = $this->extension($this->local))
127 if ($type & SIMPLEPIE_LOCATOR_LOCAL_BODY && $working = $this->body($this->local))
132 if ($type & SIMPLEPIE_LOCATOR_REMOTE_EXTENSION && $working = $this->extension($this->elsewhere))
137 if ($type & SIMPLEPIE_LOCATOR_REMOTE_BODY && $working = $this->body($this->elsewhere))
145 public function is_feed($file)
147 if ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
149 $sniffer = $this->registry->create('Content_Type_Sniffer', array($file));
150 $sniffed = $sniffer->get_type();
151 if (in_array($sniffed, array('application/rss+xml', 'application/rdf+xml', 'text/rdf', 'application/atom+xml', 'text/xml', 'application/xml')))
160 elseif ($file->method & SIMPLEPIE_FILE_SOURCE_LOCAL)
170 public function get_base()
172 if ($this->dom === null)
174 throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
176 $this->http_base = $this->file->url;
177 $this->base = $this->http_base;
178 $elements = $this->dom->getElementsByTagName('base');
179 foreach ($elements as $element)
181 if ($element->hasAttribute('href'))
183 $base = $this->registry->call('Misc', 'absolutize_url', array(trim($element->getAttribute('href')), $this->http_base));
189 $this->base_location = method_exists($element, 'getLineNo') ? $element->getLineNo() : 0;
195 public function autodiscovery()
199 $feeds = array_merge($feeds, $this->search_elements_by_tag('link', $done, $feeds));
200 $feeds = array_merge($feeds, $this->search_elements_by_tag('a', $done, $feeds));
201 $feeds = array_merge($feeds, $this->search_elements_by_tag('area', $done, $feeds));
205 return array_values($feeds);
213 protected function search_elements_by_tag($name, &$done, $feeds)
215 if ($this->dom === null)
217 throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
220 $links = $this->dom->getElementsByTagName($name);
221 foreach ($links as $link)
223 if ($this->checked_feeds === $this->max_checked_feeds)
227 if ($link->hasAttribute('href') && $link->hasAttribute('rel'))
229 $rel = array_unique($this->registry->call('Misc', 'space_seperated_tokens', array(strtolower($link->getAttribute('rel')))));
230 $line = method_exists($link, 'getLineNo') ? $link->getLineNo() : 1;
232 if ($this->base_location < $line)
234 $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->base));
238 $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->http_base));
245 if (!in_array($href, $done) && in_array('feed', $rel) || (in_array('alternate', $rel) && !in_array('stylesheet', $rel) && $link->hasAttribute('type') && in_array(strtolower($this->registry->call('Misc', 'parse_mime', array($link->getAttribute('type')))), array('application/rss+xml', 'application/atom+xml'))) && !isset($feeds[$href]))
247 $this->checked_feeds++;
249 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
251 $feed = $this->registry->create('File', array($href, $this->timeout, 5, $headers, $this->useragent));
252 if ($feed->success && ($feed->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed))
254 $feeds[$href] = $feed;
264 public function get_links()
266 if ($this->dom === null)
268 throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
271 $links = $this->dom->getElementsByTagName('a');
272 foreach ($links as $link)
274 if ($link->hasAttribute('href'))
276 $href = trim($link->getAttribute('href'));
277 $parsed = $this->registry->call('Misc', 'parse_url', array($href));
278 if ($parsed['scheme'] === '' || preg_match('/^(http(s)|feed)?$/i', $parsed['scheme']))
280 if (method_exists($link, 'getLineNo') && $this->base_location < $link->getLineNo())
282 $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->base));
286 $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->http_base));
293 $current = $this->registry->call('Misc', 'parse_url', array($this->file->url));
295 if ($parsed['authority'] === '' || $parsed['authority'] === $current['authority'])
297 $this->local[] = $href;
301 $this->elsewhere[] = $href;
306 $this->local = array_unique($this->local);
307 $this->elsewhere = array_unique($this->elsewhere);
308 if (!empty($this->local) || !empty($this->elsewhere))
315 public function extension(&$array)
317 foreach ($array as $key => $value)
319 if ($this->checked_feeds === $this->max_checked_feeds)
323 if (in_array(strtolower(strrchr($value, '.')), array('.rss', '.rdf', '.atom', '.xml')))
325 $this->checked_feeds++;
328 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
330 $feed = $this->registry->create('File', array($value, $this->timeout, 5, $headers, $this->useragent));
331 if ($feed->success && ($feed->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed))
344 public function body(&$array)
346 foreach ($array as $key => $value)
348 if ($this->checked_feeds === $this->max_checked_feeds)
352 if (preg_match('/(rss|rdf|atom|xml)/i', $value))
354 $this->checked_feeds++;
356 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
358 $feed = $this->registry->create('File', array($value, $this->timeout, 5, null, $this->useragent));
359 if ($feed->success && ($feed->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed))